Skip to content

Commit

Permalink
[ELF] Infer EI_OSABI from object files
Browse files Browse the repository at this point in the history
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: #97144
  • Loading branch information
MaskRay authored Jul 2, 2024
1 parent fdd3196 commit 5f1743c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2021,16 +2021,22 @@ void LinkerDriver::inferMachineType() {
if (config->ekind != ELFNoneKind)
return;

bool inferred = false;
for (InputFile *f : files) {
if (f->ekind == ELFNoneKind)
continue;
config->ekind = f->ekind;
config->emachine = f->emachine;
if (!inferred) {
inferred = true;
config->ekind = f->ekind;
config->emachine = f->emachine;
config->mipsN32Abi = config->emachine == EM_MIPS && isMipsN32Abi(f);
}
config->osabi = f->osabi;
config->mipsN32Abi = config->emachine == EM_MIPS && isMipsN32Abi(f);
return;
if (f->osabi != ELFOSABI_NONE)
return;
}
error("target emulation unknown: -m or at least one .o file required");
if (!inferred)
error("target emulation unknown: -m or at least one .o file required");
}

// Parse -z max-page-size=<value>. The default value is defined by
Expand Down
15 changes: 12 additions & 3 deletions lld/test/ELF/basic-freebsd.s
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
# REQUIRES: x86
# Verify that OSABI is set to the correct value.

# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %t
# RUN: ld.lld %t -o %t2
# RUN: llvm-readobj --file-headers %t2 | FileCheck %s
# RUN: rm -rf %t && split-file %s %t && cd %t
# RUN: llvm-mc -filetype=obj -triple=x86_64 empty.s -o empty.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd a.s -o a.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-linux gnu.s -o gnu.o
# RUN: ld.lld a.o -o out
# RUN: llvm-readobj --file-headers out | FileCheck %s
# RUN: ld.lld empty.o a.o gnu.o empty.o -o out2
# RUN: llvm-readobj --file-headers out2 | FileCheck %s

#--- empty.s
#--- a.s
.globl _start
_start:
mov $1, %rax
mov $42, %rdi
syscall
#--- gnu.s
.section retain,"aR"

# CHECK: ElfHeader {
# CHECK-NEXT: Ident {
Expand Down

0 comments on commit 5f1743c

Please sign in to comment.