Author: Tiezhu Yang
Date: 2022-11-01T17:06:04+08:00
New Revision: e9c34618c904e0b22b6c9fec1f4a410e7484b8d3

URL: 
https://github.com/llvm/llvm-project/commit/e9c34618c904e0b22b6c9fec1f4a410e7484b8d3
DIFF: 
https://github.com/llvm/llvm-project/commit/e9c34618c904e0b22b6c9fec1f4a410e7484b8d3.diff

LOG: [LLDB][LoongArch] Add LoongArch ArchSpec and subtype detection

Define LoongArch architecture subtypes, add the LoongArch ArchSpec bits,
and inspect the ELF header to detect the right subtype based on ELF class.

Here is a simple test:

```
[loongson@linux ~]$ cat hello.c

int main()
{
        printf("Hello, World!\n");
        return 0;
}
[loongson@linux ~]$ clang hello.c -g -o hello
```

Without this patch:

```
[loongson@linux ~]$ llvm-project/llvm/build/bin/lldb hello
(lldb) target create "hello"
error: '/home/loongson/hello' doesn't contain any 'host' platform 
architectures: unknown
```

With this patch:

```
[loongson@linux ~]$ llvm-project/llvm/build/bin/lldb hello
(lldb) target create "hello"
Current executable set to '/home/loongson/hello' (loongarch64).
(lldb) run
Process 735167 launched: '/home/loongson/hello' (loongarch64)
Hello, World!
Process 735167 exited with status = 0 (0x00000000)
(lldb) quit
[loongson@linux ~]$ llvm-project/llvm/build/bin/llvm-lit 
llvm-project/lldb/test/Shell/ObjectFile/ELF/loongarch-arch.yaml
llvm-lit: /home/loongson/llvm-project/llvm/utils/lit/lit/llvm/config.py:456: 
note: using clang: /home/loongson/llvm-project/llvm/build/bin/clang
-- Testing: 1 tests, 1 workers --
PASS: lldb-shell :: ObjectFile/ELF/loongarch-arch.yaml (1 of 1)

Testing Time: 0.09s
  Passed: 1
```

Reviewed By: SixWeining, xen0n, DavidSpickett

Differential Revision: https://reviews.llvm.org/D137057

Added: 
    lldb/test/Shell/ObjectFile/ELF/loongarch-arch.yaml

Modified: 
    lldb/include/lldb/Utility/ArchSpec.h
    lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
    lldb/source/Utility/ArchSpec.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/ArchSpec.h 
b/lldb/include/lldb/Utility/ArchSpec.h
index ae210a6d7bf17..371607175b1dd 100644
--- a/lldb/include/lldb/Utility/ArchSpec.h
+++ b/lldb/include/lldb/Utility/ArchSpec.h
@@ -108,6 +108,12 @@ class ArchSpec {
     eRISCVSubType_riscv64,
   };
 
+  enum LoongArchSubType {
+    eLoongArchSubType_unknown,
+    eLoongArchSubType_loongarch32,
+    eLoongArchSubType_loongarch64,
+  };
+
   enum Core {
     eCore_arm_generic,
     eCore_arm_armv4,
@@ -204,6 +210,9 @@ class ArchSpec {
     eCore_riscv32,
     eCore_riscv64,
 
+    eCore_loongarch32,
+    eCore_loongarch64,
+
     eCore_uknownMach32,
     eCore_uknownMach64,
 

diff  --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp 
b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index af1b0930c1b7f..cf1b375001d62 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -320,6 +320,18 @@ static uint32_t ppc64VariantFromElfFlags(const 
elf::ELFHeader &header) {
     return ArchSpec::eCore_ppc64_generic;
 }
 
+static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) {
+  uint32_t fileclass = header.e_ident[EI_CLASS];
+  switch (fileclass) {
+  case llvm::ELF::ELFCLASS32:
+    return ArchSpec::eLoongArchSubType_loongarch32;
+  case llvm::ELF::ELFCLASS64:
+    return ArchSpec::eLoongArchSubType_loongarch64;
+  default:
+    return ArchSpec::eLoongArchSubType_unknown;
+  }
+}
+
 static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
   if (header.e_machine == llvm::ELF::EM_MIPS)
     return mipsVariantFromElfFlags(header);
@@ -327,6 +339,8 @@ static uint32_t subTypeFromElfHeader(const elf::ELFHeader 
&header) {
     return ppc64VariantFromElfFlags(header);
   else if (header.e_machine == llvm::ELF::EM_RISCV)
     return riscvVariantFromElfFlags(header);
+  else if (header.e_machine == llvm::ELF::EM_LOONGARCH)
+    return loongarchVariantFromElfFlags(header);
 
   return LLDB_INVALID_CPUTYPE;
 }

diff  --git a/lldb/source/Utility/ArchSpec.cpp 
b/lldb/source/Utility/ArchSpec.cpp
index 0850e49b87307..126bedc209232 100644
--- a/lldb/source/Utility/ArchSpec.cpp
+++ b/lldb/source/Utility/ArchSpec.cpp
@@ -220,6 +220,11 @@ static const CoreDefinition g_core_definitions[] = {
     {eByteOrderLittle, 8, 2, 4, llvm::Triple::riscv64, ArchSpec::eCore_riscv64,
      "riscv64"},
 
+    {eByteOrderLittle, 4, 4, 4, llvm::Triple::loongarch32,
+     ArchSpec::eCore_loongarch32, "loongarch32"},
+    {eByteOrderLittle, 8, 4, 4, llvm::Triple::loongarch64,
+     ArchSpec::eCore_loongarch64, "loongarch64"},
+
     {eByteOrderLittle, 4, 4, 4, llvm::Triple::UnknownArch,
      ArchSpec::eCore_uknownMach32, "unknown-mach-32"},
     {eByteOrderLittle, 8, 4, 4, llvm::Triple::UnknownArch,
@@ -406,6 +411,12 @@ static const ArchDefinitionEntry g_elf_arch_entries[] = {
      ArchSpec::eRISCVSubType_riscv32, 0xFFFFFFFFu, 0xFFFFFFFFu}, // riscv32
     {ArchSpec::eCore_riscv64, llvm::ELF::EM_RISCV,
      ArchSpec::eRISCVSubType_riscv64, 0xFFFFFFFFu, 0xFFFFFFFFu}, // riscv64
+    {ArchSpec::eCore_loongarch32, llvm::ELF::EM_LOONGARCH,
+     ArchSpec::eLoongArchSubType_loongarch32, 0xFFFFFFFFu,
+     0xFFFFFFFFu}, // loongarch32
+    {ArchSpec::eCore_loongarch64, llvm::ELF::EM_LOONGARCH,
+     ArchSpec::eLoongArchSubType_loongarch64, 0xFFFFFFFFu,
+     0xFFFFFFFFu}, // loongarch64
 };
 
 static const ArchDefinition g_elf_arch_def = {

diff  --git a/lldb/test/Shell/ObjectFile/ELF/loongarch-arch.yaml 
b/lldb/test/Shell/ObjectFile/ELF/loongarch-arch.yaml
new file mode 100644
index 0000000000000..d57831ce5789e
--- /dev/null
+++ b/lldb/test/Shell/ObjectFile/ELF/loongarch-arch.yaml
@@ -0,0 +1,24 @@
+# RUN: yaml2obj --docnum=1 %s > %t32
+# RUN: yaml2obj --docnum=2 %s > %t64
+# RUN: lldb-test object-file %t32 | FileCheck --check-prefix=CHECK-LA32 %s
+# RUN: lldb-test object-file %t64 | FileCheck --check-prefix=CHECK-LA64 %s
+
+# CHECK-LA32: Architecture: loongarch32--
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_LOONGARCH
+...
+
+# CHECK-LA64: Architecture: loongarch64--
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_LOONGARCH
+...


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to