Skip to content

Commit 25f4f0a

Browse files
authored
[llvm-ifs] Handle more e_machine values for --target (#128559)
This adds ELF::convertTripleArchTypeToEMachine and uses it in llvm-ifs. It handles many more Triple::ArchType values than the old code, though not all since I couldn't quickly discern what all the mappings are.
1 parent 51bceb4 commit 25f4f0a

File tree

4 files changed

+84
-21
lines changed

4 files changed

+84
-21
lines changed

llvm/include/llvm/BinaryFormat/ELF.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define LLVM_BINARYFORMAT_ELF_H
2121

2222
#include "llvm/ADT/StringRef.h"
23+
#include "llvm/TargetParser/Triple.h"
2324
#include <cstdint>
2425
#include <cstring>
2526
#include <type_traits>
@@ -2018,6 +2019,9 @@ uint16_t convertArchNameToEMachine(StringRef Arch);
20182019
/// Convert an ELF's e_machine value into an architecture name.
20192020
StringRef convertEMachineToArchName(uint16_t EMachine);
20202021

2022+
// Convert a triple's architecture to ELF's e_machine value.
2023+
uint16_t convertTripleArchTypeToEMachine(Triple::ArchType ArchType);
2024+
20212025
// Convert a lowercase string identifier into an OSABI value.
20222026
uint8_t convertNameToOSABI(StringRef Name);
20232027

llvm/lib/BinaryFormat/ELF.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,71 @@ uint16_t ELF::convertArchNameToEMachine(StringRef Arch) {
200200
.Default(EM_NONE);
201201
}
202202

203+
uint16_t ELF::convertTripleArchTypeToEMachine(Triple::ArchType ArchType) {
204+
switch (ArchType) {
205+
case Triple::UnknownArch:
206+
default:
207+
return EM_NONE;
208+
209+
case Triple::arm:
210+
case Triple::armeb:
211+
case Triple::thumb:
212+
case Triple::thumbeb:
213+
return EM_ARM;
214+
case Triple::aarch64:
215+
case Triple::aarch64_be:
216+
case Triple::aarch64_32:
217+
return EM_AARCH64;
218+
case Triple::arc:
219+
return EM_ARC;
220+
case Triple::avr:
221+
return EM_AVR;
222+
case Triple::bpfel:
223+
case Triple::bpfeb:
224+
return EM_BPF;
225+
case Triple::csky:
226+
return EM_CSKY;
227+
case Triple::hexagon:
228+
return EM_HEXAGON;
229+
case Triple::loongarch32:
230+
case Triple::loongarch64:
231+
return EM_LOONGARCH;
232+
case Triple::m68k:
233+
return EM_68K;
234+
case Triple::mips:
235+
case Triple::mipsel:
236+
case Triple::mips64:
237+
case Triple::mips64el:
238+
return EM_MIPS;
239+
case Triple::msp430:
240+
return EM_MSP430;
241+
case Triple::ppc:
242+
case Triple::ppcle:
243+
return EM_PPC;
244+
case Triple::ppc64:
245+
case Triple::ppc64le:
246+
return EM_PPC;
247+
case Triple::riscv32:
248+
case Triple::riscv64:
249+
return EM_RISCV;
250+
case Triple::sparc:
251+
case Triple::sparcel:
252+
return EM_SPARC;
253+
case Triple::sparcv9:
254+
return EM_SPARCV9;
255+
case Triple::systemz:
256+
return EM_S390;
257+
case Triple::x86:
258+
return EM_386;
259+
case Triple::x86_64:
260+
return EM_X86_64;
261+
case Triple::xcore:
262+
return EM_XCORE;
263+
case Triple::xtensa:
264+
return EM_XTENSA;
265+
}
266+
}
267+
203268
/// Convert an ELF's e_machine value into an architecture name.
204269
StringRef ELF::convertEMachineToArchName(uint16_t EMachine) {
205270
switch (EMachine) {

llvm/lib/InterfaceStub/IFSHandler.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,10 @@ Error ifs::validateIFSTarget(IFSStub &Stub, bool ParseTriple) {
304304
IFSTarget ifs::parseTriple(StringRef TripleStr) {
305305
Triple IFSTriple(TripleStr);
306306
IFSTarget RetTarget;
307-
// TODO: Implement a Triple Arch enum to e_machine map.
308-
switch (IFSTriple.getArch()) {
309-
case Triple::ArchType::aarch64:
310-
RetTarget.Arch = (IFSArch)ELF::EM_AARCH64;
311-
break;
312-
case Triple::ArchType::x86_64:
313-
RetTarget.Arch = (IFSArch)ELF::EM_X86_64;
314-
break;
315-
case Triple::ArchType::riscv64:
316-
RetTarget.Arch = (IFSArch)ELF::EM_RISCV;
317-
break;
318-
default:
319-
RetTarget.Arch = (IFSArch)ELF::EM_NONE;
307+
IFSArch TripleArch =
308+
ELF::convertTripleArchTypeToEMachine(IFSTriple.getArch());
309+
if (TripleArch != ELF::EM_NONE) {
310+
RetTarget.Arch = TripleArch;
320311
}
321312
RetTarget.Endianness = IFSTriple.isLittleEndian() ? IFSEndiannessType::Little
322313
: IFSEndiannessType::Big;

llvm/test/tools/llvm-ifs/write-stub.test

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
## Test writing stub elf with minimal sections.
22

33
# RUN: llvm-ifs --output-elf=%t.elf32l --arch=x86_64 --bitwidth=32 --endianness=little %s
4-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf32l | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="LittleEndian (0x1)" -DHS=52 -DPHES=32 -DSHES=40 -DDYNSYMAL=4 -DDYNSYMES=16 -DDYNAMICAL=4 -DDYNAMICES=8 -DDYNTABZ=0
4+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf32l | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="LittleEndian (0x1)" -DHS=52 -DPHES=32 -DSHES=40 -DDYNSYMAL=4 -DDYNSYMES=16 -DDYNAMICAL=4 -DDYNAMICES=8 -DDYNTABZ=0 -DMACHINE="EM_X86_64 (0x3E)"
55

66
# RUN: llvm-ifs --output-elf=%t.elf32b --arch=x86_64 --bitwidth=32 --endianness=big %s
7-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf32b | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="BigEndian (0x2)" -DHS=52 -DPHES=32 -DSHES=40 -DDYNSYMAL=4 -DDYNSYMES=16 -DDYNAMICAL=4 -DDYNAMICES=8 -DDYNTABZ=0
7+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf32b | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="BigEndian (0x2)" -DHS=52 -DPHES=32 -DSHES=40 -DDYNSYMAL=4 -DDYNSYMES=16 -DDYNAMICAL=4 -DDYNAMICES=8 -DDYNTABZ=0 -DMACHINE="EM_X86_64 (0x3E)"
88

99
# RUN: llvm-ifs --output-elf=%t.elf64l --arch=x86_64 --bitwidth=64 --endianness=little %s
10-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf64l | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000
10+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf64l | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000 -DMACHINE="EM_X86_64 (0x3E)"
1111

1212
# RUN: llvm-ifs --output-elf=%t.elf64l --target=x86_64-linux-gnu %s
13-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf64l | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000
13+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf64l | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000 -DMACHINE="EM_X86_64 (0x3E)"
1414

1515
# RUN: llvm-ifs --output-elf=%t.elf64b --arch=x86_64 --bitwidth=64 --endianness=big %s
16-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf64b | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="BigEndian (0x2)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000
16+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf64b | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="BigEndian (0x2)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000 -DMACHINE="EM_X86_64 (0x3E)"
1717

1818
# RUN: not llvm-ifs --output-elf=%t --arch=x86_64 --bitwidth=64 --endianness=big --target=x86_64-linux-gnu %s 2>&1 | FileCheck %s --check-prefix=TRIPLEERR
1919

@@ -33,13 +33,16 @@
3333

3434
# RUN: llvm-ifs --output-ifs=%t.ifs --output-elf=%t.elf --target=x86_64-linux-gnu %s
3535
# RUN: llvm-ifs --output-elf=%t.elf2 --target=x86_64-linux-gnu %t.ifs
36-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000
37-
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf2 | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000
36+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000 -DMACHINE="EM_X86_64 (0x3E)"
37+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.elf2 | FileCheck %s -DCLASS="64-bit (0x2)" -DDE="LittleEndian (0x1)" -DHS=64 -DPHES=56 -DSHES=64 -DDYNSYMAL=8 -DDYNSYMES=24 -DDYNAMICAL=8 -DDYNAMICES=16 -DDYNTABZ=000000000 -DMACHINE="EM_X86_64 (0x3E)"
3838

3939
# RUN: llvm-ifs --output-elf=- --target=riscv64-linux-gnu %s | llvm-readelf -h - | FileCheck %s --check-prefix=MACHINE
4040

4141
# RUN: not llvm-ifs --output-elf=- --arch=riscv64 --endianness=little --bitwidth=64 %s 2>&1 | FileCheck %s -DMSG=riscv64 --check-prefix=ARCHERR
4242

43+
# RUN: llvm-ifs --output-elf=%t.armel --target=armv7-linux-gnueabihf %s
44+
# RUN: llvm-readobj -h -S --string-dump .dynstr --string-dump .shstrtab --dyn-symbols --dynamic-table %t.armel | FileCheck %s -DCLASS="32-bit (0x1)" -DDE="LittleEndian (0x1)" -DHS=52 -DPHES=32 -DSHES=40 -DDYNSYMAL=4 -DDYNSYMES=16 -DDYNAMICAL=4 -DDYNAMICES=8 -DDYNTABZ=0 -DMACHINE="EM_ARM (0x28)"
45+
4346
--- !ifs-v1
4447
IfsVersion: 3.0
4548
NeededLibs:
@@ -61,7 +64,7 @@ Symbols:
6164
# CHECK-NEXT: Unused: (00 00 00 00 00 00 00)
6265
# CHECK-NEXT: }
6366
# CHECK-NEXT: Type: SharedObject (0x3)
64-
# CHECK-NEXT: Machine: EM_X86_64 (0x3E)
67+
# CHECK-NEXT: Machine: [[MACHINE]]
6568
# CHECK-NEXT: Version: 1
6669
# CHECK-NEXT: Entry: 0x0
6770
# CHECK: Flags [ (0x0)

0 commit comments

Comments
 (0)