Skip to content

[ELF] Infer EI_OSABI from object files #97144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Jun 29, 2024

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.

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented Jun 29, 2024

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Fangrui Song (MaskRay)

Changes

The first object file whose EI_OSABI is not 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.


Full diff: https://github.com/llvm/llvm-project/pull/97144.diff

2 Files Affected:

  • (modified) lld/ELF/Driver.cpp (+11-5)
  • (modified) lld/test/ELF/basic-freebsd.s (+12-3)
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index ed773f5e69f77..73e260073da0c 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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
diff --git a/lld/test/ELF/basic-freebsd.s b/lld/test/ELF/basic-freebsd.s
index 078f974424463..a34f568bbcf81 100644
--- a/lld/test/ELF/basic-freebsd.s
+++ b/lld/test/ELF/basic-freebsd.s
@@ -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 {

Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! I forgot that -- duh -- the input objects also have OSABI fields.

@MaskRay MaskRay requested a review from smithp35 July 1, 2024 17:57
Copy link
Collaborator

@smithp35 smithp35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too.

Created using spr 1.3.5-bogner
@MaskRay MaskRay merged commit 5f1743c into main Jul 2, 2024
4 of 5 checks passed
@MaskRay MaskRay deleted the users/MaskRay/spr/elf-infer-ei_osabi-from-object-files branch July 2, 2024 18:00
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
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
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants