* [PATCH v2 1/5] gdb: LoongArch: Add initial target description support
2022-01-20 0:50 [PATCH v2 0/5] gdb: Add basic support for LoongArch Tiezhu Yang
@ 2022-01-20 0:50 ` Tiezhu Yang
2022-01-20 0:50 ` [PATCH v2 2/5] gdb: LoongArch: Add initial baremetal support Tiezhu Yang
` (5 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Tiezhu Yang @ 2022-01-20 0:50 UTC (permalink / raw)
To: gdb-patches, Tom Tromey, Andrew Burgess
This commit adds initial target description support for LoongArch.
Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
gdb/arch/loongarch.c | 88 +++++++++++++++++++++++++++++++
gdb/arch/loongarch.h | 73 +++++++++++++++++++++++++
gdb/doc/gdb.texinfo | 10 ++++
gdb/features/loongarch/base32.c | 47 +++++++++++++++++
gdb/features/loongarch/base32.xml | 44 ++++++++++++++++
gdb/features/loongarch/base64.c | 47 +++++++++++++++++
gdb/features/loongarch/base64.xml | 44 ++++++++++++++++
7 files changed, 353 insertions(+)
create mode 100644 gdb/arch/loongarch.c
create mode 100644 gdb/arch/loongarch.h
create mode 100644 gdb/features/loongarch/base32.c
create mode 100644 gdb/features/loongarch/base32.xml
create mode 100644 gdb/features/loongarch/base64.c
create mode 100644 gdb/features/loongarch/base64.xml
diff --git a/gdb/arch/loongarch.c b/gdb/arch/loongarch.c
new file mode 100644
index 00000000000..934f6e489c5
--- /dev/null
+++ b/gdb/arch/loongarch.c
@@ -0,0 +1,88 @@
+/* Copyright (C) 2022 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdbsupport/common-defs.h"
+#include "loongarch.h"
+#include <stdlib.h>
+#include <unordered_map>
+
+/* Target description features. */
+
+#include "../features/loongarch/base32.c"
+#include "../features/loongarch/base64.c"
+
+static target_desc_up
+loongarch_create_target_description (const struct loongarch_gdbarch_features features)
+{
+ /* Now we should create a new target description. */
+ target_desc_up tdesc = allocate_target_description ();
+
+ std::string arch_name = "loongarch";
+
+ if (features.xlen == 4)
+ arch_name.append ("32");
+ else if (features.xlen == 8)
+ arch_name.append ("64");
+
+ set_tdesc_architecture (tdesc.get (), arch_name.c_str ());
+
+ long regnum = 0;
+
+ /* For now we only support creating 32-bit or 64-bit x-registers. */
+ if (features.xlen == 4)
+ regnum = create_feature_loongarch_base32 (tdesc.get (), regnum);
+ else if (features.xlen == 8)
+ regnum = create_feature_loongarch_base64 (tdesc.get (), regnum);
+
+ return tdesc;
+}
+
+/* Wrapper used by std::unordered_map to generate hash for feature set. */
+struct loongarch_gdbarch_features_hasher
+{
+ std::size_t
+ operator() (const loongarch_gdbarch_features &features) const noexcept
+ {
+ return features.hash ();
+ }
+};
+
+/* Cache of previously seen target descriptions, indexed by the feature set
+ that created them. */
+static std::unordered_map<loongarch_gdbarch_features,
+ const target_desc_up,
+ loongarch_gdbarch_features_hasher> loongarch_tdesc_cache;
+
+const target_desc *
+loongarch_lookup_target_description (const struct loongarch_gdbarch_features features)
+{
+ /* Lookup in the cache. If we find it then return the pointer out of
+ the target_desc_up (which is a unique_ptr). This is safe as the
+ loongarch_tdesc_cache will exist until GDB exits. */
+ const auto it = loongarch_tdesc_cache.find (features);
+ if (it != loongarch_tdesc_cache.end ())
+ return it->second.get ();
+
+ target_desc_up tdesc (loongarch_create_target_description (features));
+
+ /* Add to the cache, and return a pointer borrowed from the
+ target_desc_up. This is safe as the cache (and the pointers
+ contained within it) are not deleted until GDB exits. */
+ target_desc *ptr = tdesc.get ();
+ loongarch_tdesc_cache.emplace (features, std::move (tdesc));
+ return ptr;
+}
diff --git a/gdb/arch/loongarch.h b/gdb/arch/loongarch.h
new file mode 100644
index 00000000000..9e10df967d1
--- /dev/null
+++ b/gdb/arch/loongarch.h
@@ -0,0 +1,73 @@
+/* Common target-dependent functionality for LoongArch
+
+ Copyright (C) 2022 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef ARCH_LOONGARCH_H
+#define ARCH_LOONGARCH_H
+
+#include "gdbsupport/tdesc.h"
+
+/* The set of LoongArch architectural features that we track that impact how
+ we configure the actual gdbarch instance. We hold one of these in the
+ gdbarch_tdep structure, and use it to distinguish between different
+ LoongArch gdbarch instances.
+
+ The information in here ideally comes from the target description,
+ however, if the target doesn't provide a target description then we will
+ create a default target description by first populating one of these
+ based on what we know about the binary being executed, and using that to
+ drive default target description creation. */
+
+struct loongarch_gdbarch_features
+{
+ /* The size of the x-registers in bytes. This is either 4 (loongarch32)
+ or 8 (loongarch64). No other value is valid. Initialise to the invalid
+ 0 value so we can spot if one of these is used uninitialised. */
+ int xlen = 0;
+
+ /* Equality operator. */
+ bool operator== (const struct loongarch_gdbarch_features &rhs) const
+ {
+ return (xlen == rhs.xlen);
+ }
+
+ /* Inequality operator. */
+ bool operator!= (const struct loongarch_gdbarch_features &rhs) const
+ {
+ return !((*this) == rhs);
+ }
+
+ /* Used by std::unordered_map to hash feature sets. */
+ std::size_t hash () const noexcept
+ {
+ std::size_t val = (xlen & 0x1f) << 5;
+ return val;
+ }
+};
+
+/* Lookup an already existing target description matching FEATURES, or
+ create a new target description if this is the first time we have seen
+ FEATURES. For the same FEATURES the same target_desc is always
+ returned. This is important when trying to lookup gdbarch objects as
+ GDBARCH_LIST_LOOKUP_BY_INFO performs a pointer comparison on target
+ descriptions to find candidate gdbarch objects. */
+
+const target_desc *loongarch_lookup_target_description
+ (const struct loongarch_gdbarch_features features);
+
+#endif /* ARCH_LOONGARCH_H */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index fe81687a66c..2ef0368127b 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -46223,6 +46223,7 @@ registers using the capitalization used in the description.
* ARC Features::
* ARM Features::
* i386 Features::
+* LoongArch Features::
* MicroBlaze Features::
* MIPS Features::
* M68K Features::
@@ -46451,6 +46452,15 @@ The @samp{org.gnu.gdb.i386.pkeys} feature is optional. It should
describe a single register, @samp{pkru}. It is a 32-bit register
valid for i386 and amd64.
+@node LoongArch Features
+@subsection LoongArch Features
+@cindex target descriptions, LoongArch Features
+
+The @samp{org.gnu.gdb.loongarch.base} feature is required for LoongArch
+targets. It should contain the registers @samp{r0} through @samp{r31},
+@samp{pc}, and @samp{badv}. Either the architectural names (@samp{r0},
+@samp{r1}, etc) can be used, or the ABI names (@samp{zero}, @samp{ra}, etc).
+
@node MicroBlaze Features
@subsection MicroBlaze Features
@cindex target descriptions, MicroBlaze features
diff --git a/gdb/features/loongarch/base32.c b/gdb/features/loongarch/base32.c
new file mode 100644
index 00000000000..7105c152aed
--- /dev/null
+++ b/gdb/features/loongarch/base32.c
@@ -0,0 +1,47 @@
+/* THIS FILE IS GENERATED. -*- buffer-read-only: t -*- vi:set ro:
+ Original: base32.xml */
+
+#include "gdbsupport/tdesc.h"
+
+static int
+create_feature_loongarch_base32 (struct target_desc *result, long regnum)
+{
+ struct tdesc_feature *feature;
+
+ feature = tdesc_create_feature (result, "org.gnu.gdb.loongarch.base");
+ tdesc_create_reg (feature, "r0", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r1", regnum++, 1, "general", 32, "code_ptr");
+ tdesc_create_reg (feature, "r2", regnum++, 1, "general", 32, "data_ptr");
+ tdesc_create_reg (feature, "r3", regnum++, 1, "general", 32, "data_ptr");
+ tdesc_create_reg (feature, "r4", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r5", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r6", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r7", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r8", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r9", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r10", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r11", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r12", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r13", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r14", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r15", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r16", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r17", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r18", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r19", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r20", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r21", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r22", regnum++, 1, "general", 32, "data_ptr");
+ tdesc_create_reg (feature, "r23", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r24", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r25", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r26", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r27", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r28", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r29", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r30", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "r31", regnum++, 1, "general", 32, "uint32");
+ tdesc_create_reg (feature, "pc", regnum++, 1, "general", 32, "code_ptr");
+ tdesc_create_reg (feature, "badv", regnum++, 1, "general", 32, "code_ptr");
+ return regnum;
+}
diff --git a/gdb/features/loongarch/base32.xml b/gdb/features/loongarch/base32.xml
new file mode 100644
index 00000000000..5b00f8a8d37
--- /dev/null
+++ b/gdb/features/loongarch/base32.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2022 Free Software Foundation, Inc.
+
+ Copying and distribution of this file, with or without modification,
+ are permitted in any medium without royalty provided the copyright
+ notice and this notice are preserved. -->
+
+<!DOCTYPE feature SYSTEM "gdb-target.dtd">
+<feature name="org.gnu.gdb.loongarch.base">
+ <reg name="r0" bitsize="32" type="uint32" group="general"/>
+ <reg name="r1" bitsize="32" type="code_ptr" group="general"/>
+ <reg name="r2" bitsize="32" type="data_ptr" group="general"/>
+ <reg name="r3" bitsize="32" type="data_ptr" group="general"/>
+ <reg name="r4" bitsize="32" type="uint32" group="general"/>
+ <reg name="r5" bitsize="32" type="uint32" group="general"/>
+ <reg name="r6" bitsize="32" type="uint32" group="general"/>
+ <reg name="r7" bitsize="32" type="uint32" group="general"/>
+ <reg name="r8" bitsize="32" type="uint32" group="general"/>
+ <reg name="r9" bitsize="32" type="uint32" group="general"/>
+ <reg name="r10" bitsize="32" type="uint32" group="general"/>
+ <reg name="r11" bitsize="32" type="uint32" group="general"/>
+ <reg name="r12" bitsize="32" type="uint32" group="general"/>
+ <reg name="r13" bitsize="32" type="uint32" group="general"/>
+ <reg name="r14" bitsize="32" type="uint32" group="general"/>
+ <reg name="r15" bitsize="32" type="uint32" group="general"/>
+ <reg name="r16" bitsize="32" type="uint32" group="general"/>
+ <reg name="r17" bitsize="32" type="uint32" group="general"/>
+ <reg name="r18" bitsize="32" type="uint32" group="general"/>
+ <reg name="r19" bitsize="32" type="uint32" group="general"/>
+ <reg name="r20" bitsize="32" type="uint32" group="general"/>
+ <reg name="r21" bitsize="32" type="uint32" group="general"/>
+ <reg name="r22" bitsize="32" type="data_ptr" group="general"/>
+ <reg name="r23" bitsize="32" type="uint32" group="general"/>
+ <reg name="r24" bitsize="32" type="uint32" group="general"/>
+ <reg name="r25" bitsize="32" type="uint32" group="general"/>
+ <reg name="r26" bitsize="32" type="uint32" group="general"/>
+ <reg name="r27" bitsize="32" type="uint32" group="general"/>
+ <reg name="r28" bitsize="32" type="uint32" group="general"/>
+ <reg name="r29" bitsize="32" type="uint32" group="general"/>
+ <reg name="r30" bitsize="32" type="uint32" group="general"/>
+ <reg name="r31" bitsize="32" type="uint32" group="general"/>
+ <reg name="pc" bitsize="32" type="code_ptr" group="general"/>
+ <reg name="badv" bitsize="32" type="code_ptr" group="general"/>
+</feature>
diff --git a/gdb/features/loongarch/base64.c b/gdb/features/loongarch/base64.c
new file mode 100644
index 00000000000..63eee024554
--- /dev/null
+++ b/gdb/features/loongarch/base64.c
@@ -0,0 +1,47 @@
+/* THIS FILE IS GENERATED. -*- buffer-read-only: t -*- vi:set ro:
+ Original: base64.xml */
+
+#include "gdbsupport/tdesc.h"
+
+static int
+create_feature_loongarch_base64 (struct target_desc *result, long regnum)
+{
+ struct tdesc_feature *feature;
+
+ feature = tdesc_create_feature (result, "org.gnu.gdb.loongarch.base");
+ tdesc_create_reg (feature, "r0", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r1", regnum++, 1, "general", 64, "code_ptr");
+ tdesc_create_reg (feature, "r2", regnum++, 1, "general", 64, "data_ptr");
+ tdesc_create_reg (feature, "r3", regnum++, 1, "general", 64, "data_ptr");
+ tdesc_create_reg (feature, "r4", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r5", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r6", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r7", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r8", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r9", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r10", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r11", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r12", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r13", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r14", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r15", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r16", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r17", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r18", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r19", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r20", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r21", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r22", regnum++, 1, "general", 64, "data_ptr");
+ tdesc_create_reg (feature, "r23", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r24", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r25", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r26", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r27", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r28", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r29", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r30", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "r31", regnum++, 1, "general", 64, "uint64");
+ tdesc_create_reg (feature, "pc", regnum++, 1, "general", 64, "code_ptr");
+ tdesc_create_reg (feature, "badv", regnum++, 1, "general", 64, "code_ptr");
+ return regnum;
+}
diff --git a/gdb/features/loongarch/base64.xml b/gdb/features/loongarch/base64.xml
new file mode 100644
index 00000000000..bef91e50dd7
--- /dev/null
+++ b/gdb/features/loongarch/base64.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2022 Free Software Foundation, Inc.
+
+ Copying and distribution of this file, with or without modification,
+ are permitted in any medium without royalty provided the copyright
+ notice and this notice are preserved. -->
+
+<!DOCTYPE feature SYSTEM "gdb-target.dtd">
+<feature name="org.gnu.gdb.loongarch.base">
+ <reg name="r0" bitsize="64" type="uint64" group="general"/>
+ <reg name="r1" bitsize="64" type="code_ptr" group="general"/>
+ <reg name="r2" bitsize="64" type="data_ptr" group="general"/>
+ <reg name="r3" bitsize="64" type="data_ptr" group="general"/>
+ <reg name="r4" bitsize="64" type="uint64" group="general"/>
+ <reg name="r5" bitsize="64" type="uint64" group="general"/>
+ <reg name="r6" bitsize="64" type="uint64" group="general"/>
+ <reg name="r7" bitsize="64" type="uint64" group="general"/>
+ <reg name="r8" bitsize="64" type="uint64" group="general"/>
+ <reg name="r9" bitsize="64" type="uint64" group="general"/>
+ <reg name="r10" bitsize="64" type="uint64" group="general"/>
+ <reg name="r11" bitsize="64" type="uint64" group="general"/>
+ <reg name="r12" bitsize="64" type="uint64" group="general"/>
+ <reg name="r13" bitsize="64" type="uint64" group="general"/>
+ <reg name="r14" bitsize="64" type="uint64" group="general"/>
+ <reg name="r15" bitsize="64" type="uint64" group="general"/>
+ <reg name="r16" bitsize="64" type="uint64" group="general"/>
+ <reg name="r17" bitsize="64" type="uint64" group="general"/>
+ <reg name="r18" bitsize="64" type="uint64" group="general"/>
+ <reg name="r19" bitsize="64" type="uint64" group="general"/>
+ <reg name="r20" bitsize="64" type="uint64" group="general"/>
+ <reg name="r21" bitsize="64" type="uint64" group="general"/>
+ <reg name="r22" bitsize="64" type="data_ptr" group="general"/>
+ <reg name="r23" bitsize="64" type="uint64" group="general"/>
+ <reg name="r24" bitsize="64" type="uint64" group="general"/>
+ <reg name="r25" bitsize="64" type="uint64" group="general"/>
+ <reg name="r26" bitsize="64" type="uint64" group="general"/>
+ <reg name="r27" bitsize="64" type="uint64" group="general"/>
+ <reg name="r28" bitsize="64" type="uint64" group="general"/>
+ <reg name="r29" bitsize="64" type="uint64" group="general"/>
+ <reg name="r30" bitsize="64" type="uint64" group="general"/>
+ <reg name="r31" bitsize="64" type="uint64" group="general"/>
+ <reg name="pc" bitsize="64" type="code_ptr" group="general"/>
+ <reg name="badv" bitsize="64" type="code_ptr" group="general"/>
+</feature>
--
2.27.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 2/5] gdb: LoongArch: Add initial baremetal support
2022-01-20 0:50 [PATCH v2 0/5] gdb: Add basic support for LoongArch Tiezhu Yang
2022-01-20 0:50 ` [PATCH v2 1/5] gdb: LoongArch: Add initial target description support Tiezhu Yang
@ 2022-01-20 0:50 ` Tiezhu Yang
2022-01-20 0:50 ` [PATCH v2 3/5] gdb: LoongArch: Add initial Linux target support Tiezhu Yang
` (4 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Tiezhu Yang @ 2022-01-20 0:50 UTC (permalink / raw)
To: gdb-patches, Tom Tromey, Andrew Burgess
This commit adds initial baremetal support for LoongArch.
Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
gdb/loongarch-tdep.c | 316 +++++++++++++++++++++++++++++++++++++++++++
gdb/loongarch-tdep.h | 49 +++++++
2 files changed, 365 insertions(+)
create mode 100644 gdb/loongarch-tdep.c
create mode 100644 gdb/loongarch-tdep.h
diff --git a/gdb/loongarch-tdep.c b/gdb/loongarch-tdep.c
new file mode 100644
index 00000000000..ffcff03109a
--- /dev/null
+++ b/gdb/loongarch-tdep.c
@@ -0,0 +1,316 @@
+/* Target-dependent code for the LoongArch architecture, for GDB.
+
+ Copyright (C) 2022 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "arch-utils.h"
+#include "dwarf2/frame.h"
+#include "elf-bfd.h"
+#include "frame-unwind.h"
+#include "loongarch-tdep.h"
+#include "target-descriptions.h"
+#include "trad-frame.h"
+#include "user-regs.h"
+
+/* Implement the loongarch_skip_prologue gdbarch method. */
+
+static CORE_ADDR
+loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
+{
+ CORE_ADDR func_addr;
+
+ /* See if we can determine the end of the prologue via the symbol table.
+ If so, then return either PC, or the PC after the prologue, whichever
+ is greater. */
+ if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr))
+ {
+ CORE_ADDR post_prologue_pc
+ = skip_prologue_using_sal (gdbarch, func_addr);
+ if (post_prologue_pc != 0)
+ return std::max (pc, post_prologue_pc);
+ }
+
+ return 0;
+}
+
+/* Adjust the address downward (direction of stack growth) so that it
+ is correctly aligned for a new stack frame. */
+
+static CORE_ADDR
+loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
+{
+ return align_down (addr, 16);
+}
+
+/* Generate, or return the cached frame cache for LoongArch frame unwinder. */
+
+static struct trad_frame_cache *
+loongarch_frame_cache (struct frame_info *this_frame, void **this_cache)
+{
+ struct gdbarch *gdbarch = get_frame_arch (this_frame);
+ struct trad_frame_cache *cache;
+ CORE_ADDR pc;
+
+ if (*this_cache != nullptr)
+ return (struct trad_frame_cache *) *this_cache;
+
+ cache = trad_frame_cache_zalloc (this_frame);
+ *this_cache = cache;
+
+ loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+ trad_frame_set_reg_realreg (cache, gdbarch_pc_regnum (gdbarch), tdep->regs.ra);
+
+ pc = get_frame_address_in_block (this_frame);
+ trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
+
+ return cache;
+}
+
+/* Implement the this_id callback for LoongArch frame unwinder. */
+
+static void
+loongarch_frame_this_id (struct frame_info *this_frame, void **prologue_cache,
+ struct frame_id *this_id)
+{
+ struct trad_frame_cache *info;
+
+ info = loongarch_frame_cache (this_frame, prologue_cache);
+ trad_frame_get_id (info, this_id);
+}
+
+/* Implement the prev_register callback for LoongArch frame unwinder. */
+
+static struct value *
+loongarch_frame_prev_register (struct frame_info *this_frame,
+ void **prologue_cache, int regnum)
+{
+ struct trad_frame_cache *info;
+
+ info = loongarch_frame_cache (this_frame, prologue_cache);
+ return trad_frame_get_register (info, this_frame, regnum);
+}
+
+static const struct frame_unwind loongarch_frame_unwind = {
+ "loongarch prologue",
+ /*.type =*/NORMAL_FRAME,
+ /*.stop_reason =*/default_frame_unwind_stop_reason,
+ /*.this_id =*/loongarch_frame_this_id,
+ /*.prev_register =*/loongarch_frame_prev_register,
+ /*.unwind_data =*/nullptr,
+ /*.sniffer =*/default_frame_sniffer,
+ /*.dealloc_cache =*/nullptr,
+ /*.prev_arch =*/nullptr,
+};
+
+/* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
+
+static int
+loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
+{
+ loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+ auto regs = tdep->regs;
+
+ if (0 <= num && num < 32)
+ return regs.r + num;
+ else
+ return -1;
+}
+
+static constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
+typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
+
+/* Extract a set of required target features out of ABFD. If ABFD is nullptr
+ then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
+
+static struct loongarch_gdbarch_features
+loongarch_features_from_bfd (const bfd *abfd)
+{
+ struct loongarch_gdbarch_features features;
+
+ /* Now try to improve on the defaults by looking at the binary we are
+ going to execute. We assume the user knows what they are doing and
+ that the target will match the binary. Remember, this code path is
+ only used at all if the target hasn't given us a description, so this
+ is really a last ditched effort to do something sane before giving
+ up. */
+ if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+ {
+ unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
+
+ if (eclass == ELFCLASS32)
+ features.xlen = 4;
+ else if (eclass == ELFCLASS64)
+ features.xlen = 8;
+ else
+ internal_error (__FILE__, __LINE__,
+ _("unknown ELF header class %d"), eclass);
+ }
+
+ return features;
+}
+
+/* Find a suitable default target description. Use the contents of INFO,
+ specifically the bfd object being executed, to guide the selection of a
+ suitable default target description. */
+
+static const struct target_desc *
+loongarch_find_default_target_description (const struct gdbarch_info info)
+{
+ /* Extract desired feature set from INFO. */
+ struct loongarch_gdbarch_features features
+ = loongarch_features_from_bfd (info.abfd);
+
+ /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
+ maybe there was no bfd object. In this case we fall back to a minimal
+ useful target with no floating point, the x-register size is selected
+ based on the architecture from INFO. */
+ if (features.xlen == 0)
+ features.xlen = info.bfd_arch_info->bits_per_address == 32 ? 4 : 8;
+
+ /* Now build a target description based on the feature set. */
+ return loongarch_lookup_target_description (features);
+}
+
+/* Initialize the current architecture based on INFO */
+
+static struct gdbarch *
+loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
+{
+ const struct target_desc *tdesc = info.target_desc;
+
+ /* Ensure we always have a target description. */
+ if (!tdesc_has_registers (tdesc))
+ tdesc = loongarch_find_default_target_description (info);
+
+ const struct tdesc_feature *feature_cpu
+ = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
+ if (feature_cpu == nullptr)
+ return nullptr;
+
+ int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
+ struct loongarch_gdbarch_features features;
+ features.xlen = (xlen_bitsize / 8);
+
+ size_t regnum = 0;
+ tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
+ loongarch_gdbarch_tdep *tdep = new loongarch_gdbarch_tdep;
+ tdep->regs.r = regnum;
+
+ /* Validate the description provides the mandatory base registers
+ and allocate their numbers. */
+ bool valid_p = true;
+ for (int i = 0; i < 32; i++)
+ valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++,
+ loongarch_r_normal_name[i] + 1);
+ valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (),
+ tdep->regs.pc = regnum++, "pc");
+ valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (),
+ tdep->regs.badv = regnum++, "badv");
+ if (!valid_p)
+ return nullptr;
+
+ /* LoongArch code is always little-endian. */
+ info.byte_order_for_code = BFD_ENDIAN_LITTLE;
+
+ /* Have a look at what the supplied (if any) bfd object requires of the
+ target, then check that this matches with what the target is
+ providing. */
+ struct loongarch_gdbarch_features abi_features
+ = loongarch_features_from_bfd (info.abfd);
+
+ /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
+ features from the INFO object. In this case we just treat the
+ hardware features as defining the abi. */
+ if (abi_features.xlen == 0)
+ abi_features = features;
+
+ /* Find a candidate among the list of pre-declared architectures. */
+ for (arches = gdbarch_list_lookup_by_info (arches, &info);
+ arches != nullptr;
+ arches = gdbarch_list_lookup_by_info (arches->next, &info))
+ {
+ /* Check that the feature set of the ARCHES matches the feature set
+ we are looking for. If it doesn't then we can't reuse this
+ gdbarch. */
+ loongarch_gdbarch_tdep *candidate_tdep
+ = (loongarch_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
+
+ if (candidate_tdep->abi_features != abi_features)
+ continue;
+
+ break;
+ }
+
+ if (arches != nullptr)
+ return arches->gdbarch;
+
+ /* None found, so create a new architecture from the information provided. */
+ struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
+ tdep->abi_features = abi_features;
+
+ /* Target data types. */
+ set_gdbarch_short_bit (gdbarch, 16);
+ set_gdbarch_int_bit (gdbarch, 32);
+ set_gdbarch_long_bit (gdbarch, info.bfd_arch_info->bits_per_address);
+ set_gdbarch_long_long_bit (gdbarch, 64);
+ set_gdbarch_ptr_bit (gdbarch, info.bfd_arch_info->bits_per_address);
+ set_gdbarch_char_signed (gdbarch, 0);
+
+ info.target_desc = tdesc;
+ info.tdesc_data = tdesc_data.get ();
+
+ /* Information about registers. */
+ tdep->regs.ra = tdep->regs.r + 1;
+ tdep->regs.sp = tdep->regs.r + 3;
+ set_gdbarch_num_regs (gdbarch, regnum);
+ set_gdbarch_sp_regnum (gdbarch, tdep->regs.sp);
+ set_gdbarch_pc_regnum (gdbarch, tdep->regs.pc);
+
+ /* Finalise the target description registers. */
+ tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
+
+ /* Advance PC across function entry code. */
+ set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
+
+ /* Stack grows downward. */
+ set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
+
+ /* Frame info. */
+ set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
+
+ /* Breakpoint manipulation. */
+ set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
+ set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
+
+ /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
+ set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
+ dwarf2_append_unwinders (gdbarch);
+ frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
+
+ /* Hook in OS ABI-specific overrides, if they have been registered. */
+ gdbarch_init_osabi (info, gdbarch);
+
+ return gdbarch;
+}
+
+void _initialize_loongarch_tdep ();
+void
+_initialize_loongarch_tdep ()
+{
+ gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, nullptr);
+}
diff --git a/gdb/loongarch-tdep.h b/gdb/loongarch-tdep.h
new file mode 100644
index 00000000000..b2fd16e25a7
--- /dev/null
+++ b/gdb/loongarch-tdep.h
@@ -0,0 +1,49 @@
+/* Target-dependent header for the LoongArch architecture, for GDB.
+
+ Copyright (C) 2022 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef LOONGARCH_TDEP_H
+#define LOONGARCH_TDEP_H
+
+#include "gdbarch.h"
+#include "arch/loongarch.h"
+#include "regset.h"
+
+#include "elf/loongarch.h"
+#include "opcode/loongarch.h"
+
+/* Register set definitions. */
+extern const struct regset loongarch_gregset;
+
+/* Target-dependent structure in gdbarch. */
+struct loongarch_gdbarch_tdep : gdbarch_tdep
+{
+ /* Features about the abi that impact how the gdbarch is configured. */
+ struct loongarch_gdbarch_features abi_features;
+
+ struct
+ {
+ int r; /* General register. */
+ int ra; /* Return Address. */
+ int sp; /* Stack Pointer. */
+ int pc; /* Program Counter. */
+ int badv; /* Bad vaddr for addressing exception. */
+ } regs; /* LoongArch registers */
+};
+
+#endif /* LOONGARCH_TDEP_H */
--
2.27.0
^ permalink raw reply [flat|nested] 11+ messages in thread