Skip to content

Commit 83f7a00

Browse files
MaskRaylravenclaw
authored andcommitted
[ELF] Infer EI_OSABI from object files
The first object file whose EI_OSABI is not ELFOSABI_NONE is selected. This is useful for some OSes to identify themselves. This achieves similar effects to BFD emulations `ld.lld -m *_fbsd` but is more lightweight. Pull Request: llvm#97144
1 parent a4bd043 commit 83f7a00

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lld/ELF/Driver.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -2021,16 +2021,22 @@ void LinkerDriver::inferMachineType() {
20212021
if (config->ekind != ELFNoneKind)
20222022
return;
20232023

2024+
bool inferred = false;
20242025
for (InputFile *f : files) {
20252026
if (f->ekind == ELFNoneKind)
20262027
continue;
2027-
config->ekind = f->ekind;
2028-
config->emachine = f->emachine;
2028+
if (!inferred) {
2029+
inferred = true;
2030+
config->ekind = f->ekind;
2031+
config->emachine = f->emachine;
2032+
config->mipsN32Abi = config->emachine == EM_MIPS && isMipsN32Abi(f);
2033+
}
20292034
config->osabi = f->osabi;
2030-
config->mipsN32Abi = config->emachine == EM_MIPS && isMipsN32Abi(f);
2031-
return;
2035+
if (f->osabi != ELFOSABI_NONE)
2036+
return;
20322037
}
2033-
error("target emulation unknown: -m or at least one .o file required");
2038+
if (!inferred)
2039+
error("target emulation unknown: -m or at least one .o file required");
20342040
}
20352041

20362042
// Parse -z max-page-size=<value>. The default value is defined by

lld/test/ELF/basic-freebsd.s

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# REQUIRES: x86
22
# Verify that OSABI is set to the correct value.
33

4-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %t
5-
# RUN: ld.lld %t -o %t2
6-
# RUN: llvm-readobj --file-headers %t2 | FileCheck %s
4+
# RUN: rm -rf %t && split-file %s %t && cd %t
5+
# RUN: llvm-mc -filetype=obj -triple=x86_64 empty.s -o empty.o
6+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd a.s -o a.o
7+
# RUN: llvm-mc -filetype=obj -triple=x86_64-linux gnu.s -o gnu.o
8+
# RUN: ld.lld a.o -o out
9+
# RUN: llvm-readobj --file-headers out | FileCheck %s
10+
# RUN: ld.lld empty.o a.o gnu.o empty.o -o out2
11+
# RUN: llvm-readobj --file-headers out2 | FileCheck %s
712

13+
#--- empty.s
14+
#--- a.s
815
.globl _start
916
_start:
1017
mov $1, %rax
1118
mov $42, %rdi
1219
syscall
20+
#--- gnu.s
21+
.section retain,"aR"
1322

1423
# CHECK: ElfHeader {
1524
# CHECK-NEXT: Ident {

0 commit comments

Comments
 (0)