[PATCH] D76174: [ELF] Support linking ET_EXEC

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 13 23:57:43 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, psmith, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

GNU ld supports linking an ET_EXEC as if it were an ET_REL. This is an
extremely rare feature, but we can implement it with just one more line.

As a real world use case, the final link phase of seabios does:

`ld -N -T out/romlayout32flat.lds out/rom16.strip.o out/rom32seg.strip.o out/code32flat.o -o out/rom.o`

out/rom16.strip.o and out/rom32seg.strip.o are ET_EXEC created by previous steps.

romlayout32flat.lds collects the pieces and produces rom.o which
contains code of all x86 memory models.

With this lld patch and two seabios patches, I manage to link seabios with lld.

- https://mail.coreboot.org/hyperkitty/list/seabios@seabios.org/thread/CJR7WU6TTHVQEG5XHCRI75EA6SBV72SW/
- https://mail.coreboot.org/hyperkitty/list/seabios@seabios.org/thread/GUYWC4VOYH76JOYGBNT7J4FSHLKG53EO/


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76174

Files:
  lld/ELF/Driver.cpp
  lld/test/ELF/executable.test
  lld/test/ELF/invalid/core.test
  lld/test/ELF/invalid/executable.s


Index: lld/test/ELF/invalid/executable.s
===================================================================
--- lld/test/ELF/invalid/executable.s
+++ /dev/null
@@ -1,9 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: ld.lld -o %t1.exe %t.o
-# RUN: not ld.lld -o /dev/null %t1.exe 2>&1 | FileCheck %s
-# CHECK: unknown file type
-
-.global _start
-_start:
-  ret
Index: lld/test/ELF/invalid/core.test
===================================================================
--- /dev/null
+++ lld/test/ELF/invalid/core.test
@@ -0,0 +1,12 @@
+## Reject core files.
+# RUN: yaml2obj %s -o %t
+# RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: unknown file type
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_CORE
+  Machine: EM_X86_64
Index: lld/test/ELF/executable.test
===================================================================
--- /dev/null
+++ lld/test/ELF/executable.test
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+## Test an ET_EXEC can be linked like a relocatable object.
+
+# RUN: echo nop | llvm-mc -filetype=obj -triple=x86_64 - -o %t.o
+# RUN: ld.lld %t.o -o %t.exe
+
+# RUN: ld.lld %t.exe -o %t
+# RUN: cmp %t.exe %t
+
+## This demonstrates a use case that a linker script can
+## uses the section contents to create another output.
+# RUN: ld.lld -T %s %t.exe %t.exe -o %t2
+# RUN: llvm-readelf -x .foo %t2 | FileCheck %s
+
+# CHECK: 0x00000000 90cccccc 90
+
+SECTIONS {
+  .foo : { *(.text) }
+}
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -259,6 +259,7 @@
         make<SharedFile>(mbref, withLOption ? path::filename(path) : path));
     return;
   case file_magic::bitcode:
+  case file_magic::elf_executable:
   case file_magic::elf_relocatable:
     if (inLib)
       files.push_back(make<LazyObjFile>(mbref, "", 0));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76174.250349.patch
Type: text/x-patch
Size: 1936 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200314/c1378cae/attachment.bin>


More information about the llvm-commits mailing list