Skip to content

Commit e9c3461

Browse files
seehearfeelSixWeining
authored andcommitted
[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
1 parent 6aa672f commit e9c3461

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

lldb/include/lldb/Utility/ArchSpec.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class ArchSpec {
108108
eRISCVSubType_riscv64,
109109
};
110110

111+
enum LoongArchSubType {
112+
eLoongArchSubType_unknown,
113+
eLoongArchSubType_loongarch32,
114+
eLoongArchSubType_loongarch64,
115+
};
116+
111117
enum Core {
112118
eCore_arm_generic,
113119
eCore_arm_armv4,
@@ -204,6 +210,9 @@ class ArchSpec {
204210
eCore_riscv32,
205211
eCore_riscv64,
206212

213+
eCore_loongarch32,
214+
eCore_loongarch64,
215+
207216
eCore_uknownMach32,
208217
eCore_uknownMach64,
209218

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,27 @@ static uint32_t ppc64VariantFromElfFlags(const elf::ELFHeader &header) {
320320
return ArchSpec::eCore_ppc64_generic;
321321
}
322322

323+
static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) {
324+
uint32_t fileclass = header.e_ident[EI_CLASS];
325+
switch (fileclass) {
326+
case llvm::ELF::ELFCLASS32:
327+
return ArchSpec::eLoongArchSubType_loongarch32;
328+
case llvm::ELF::ELFCLASS64:
329+
return ArchSpec::eLoongArchSubType_loongarch64;
330+
default:
331+
return ArchSpec::eLoongArchSubType_unknown;
332+
}
333+
}
334+
323335
static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {
324336
if (header.e_machine == llvm::ELF::EM_MIPS)
325337
return mipsVariantFromElfFlags(header);
326338
else if (header.e_machine == llvm::ELF::EM_PPC64)
327339
return ppc64VariantFromElfFlags(header);
328340
else if (header.e_machine == llvm::ELF::EM_RISCV)
329341
return riscvVariantFromElfFlags(header);
342+
else if (header.e_machine == llvm::ELF::EM_LOONGARCH)
343+
return loongarchVariantFromElfFlags(header);
330344

331345
return LLDB_INVALID_CPUTYPE;
332346
}

lldb/source/Utility/ArchSpec.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ static const CoreDefinition g_core_definitions[] = {
220220
{eByteOrderLittle, 8, 2, 4, llvm::Triple::riscv64, ArchSpec::eCore_riscv64,
221221
"riscv64"},
222222

223+
{eByteOrderLittle, 4, 4, 4, llvm::Triple::loongarch32,
224+
ArchSpec::eCore_loongarch32, "loongarch32"},
225+
{eByteOrderLittle, 8, 4, 4, llvm::Triple::loongarch64,
226+
ArchSpec::eCore_loongarch64, "loongarch64"},
227+
223228
{eByteOrderLittle, 4, 4, 4, llvm::Triple::UnknownArch,
224229
ArchSpec::eCore_uknownMach32, "unknown-mach-32"},
225230
{eByteOrderLittle, 8, 4, 4, llvm::Triple::UnknownArch,
@@ -406,6 +411,12 @@ static const ArchDefinitionEntry g_elf_arch_entries[] = {
406411
ArchSpec::eRISCVSubType_riscv32, 0xFFFFFFFFu, 0xFFFFFFFFu}, // riscv32
407412
{ArchSpec::eCore_riscv64, llvm::ELF::EM_RISCV,
408413
ArchSpec::eRISCVSubType_riscv64, 0xFFFFFFFFu, 0xFFFFFFFFu}, // riscv64
414+
{ArchSpec::eCore_loongarch32, llvm::ELF::EM_LOONGARCH,
415+
ArchSpec::eLoongArchSubType_loongarch32, 0xFFFFFFFFu,
416+
0xFFFFFFFFu}, // loongarch32
417+
{ArchSpec::eCore_loongarch64, llvm::ELF::EM_LOONGARCH,
418+
ArchSpec::eLoongArchSubType_loongarch64, 0xFFFFFFFFu,
419+
0xFFFFFFFFu}, // loongarch64
409420
};
410421

411422
static const ArchDefinition g_elf_arch_def = {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# RUN: yaml2obj --docnum=1 %s > %t32
2+
# RUN: yaml2obj --docnum=2 %s > %t64
3+
# RUN: lldb-test object-file %t32 | FileCheck --check-prefix=CHECK-LA32 %s
4+
# RUN: lldb-test object-file %t64 | FileCheck --check-prefix=CHECK-LA64 %s
5+
6+
# CHECK-LA32: Architecture: loongarch32--
7+
8+
--- !ELF
9+
FileHeader:
10+
Class: ELFCLASS32
11+
Data: ELFDATA2LSB
12+
Type: ET_EXEC
13+
Machine: EM_LOONGARCH
14+
...
15+
16+
# CHECK-LA64: Architecture: loongarch64--
17+
18+
--- !ELF
19+
FileHeader:
20+
Class: ELFCLASS64
21+
Data: ELFDATA2LSB
22+
Type: ET_EXEC
23+
Machine: EM_LOONGARCH
24+
...

0 commit comments

Comments
 (0)