<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Sep 24, 2015 at 7:25 PM, Sean Silva via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Thu, Sep 24, 2015 at 6:59 PM, Davide Italiano via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: davide<br>
Date: Thu Sep 24 20:59:13 2015<br>
New Revision: 248554<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=248554&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=248554&view=rev</a><br>
Log:<br>
[ELF2] Fix binaries so they actually run on FreeBSD.<br>
<br>
Since FreeBSD 4.1, the kernel expects binaries to be marked with<br>
ELFOSABI_FREEBSD in the ELF header to exec() them. LLD unconditionally<br>
sets OSABI to ELF_OSABINONE, and everything linked with it won't run<br>
on FreeBSD (unless explicitly rebranded).<br>
<br>
Example:<br>
% ./aarch64-hello<br>
ELF binary type "0" not known.<br>
zsh: exec format error: ./aarch64-hello<br>
<br>
FreeBSD could be modified to accept ELF_OSABINONE, but that would break all<br>
existing binaries, so the kernel needs to support both ABINONE and ABIFREEBSD.<br>
I plan to push this change in FreeBSD one day, which, unfortunately, is<br>
not today. This code patches lld so it sets the header field correctly.<br>
<br>
For completeness, the rationale of this change is explained in the FreeBSD<br>
commit message, and it's apparently to pleasee binutils maintainers at the time.<br>
<a href="https://svnweb.freebsd.org/base?view=revision&revision=59342" rel="noreferrer" target="_blank">https://svnweb.freebsd.org/base?view=revision&revision=59342</a><br>
<br>
Differential Revision:   <a href="http://reviews.llvm.org/D13140" rel="noreferrer" target="_blank">http://reviews.llvm.org/D13140</a><br>
<br>
Added:<br>
    lld/trunk/test/elf2/basic-freebsd.s<br>
Modified:<br>
    lld/trunk/ELF/InputFiles.h<br>
    lld/trunk/ELF/Writer.cpp<br>
<br>
Modified: lld/trunk/ELF/InputFiles.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=248554&r1=248553&r2=248554&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=248554&r1=248553&r2=248554&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/ELF/InputFiles.h (original)<br>
+++ lld/trunk/ELF/InputFiles.h Thu Sep 24 20:59:13 2015<br>
@@ -105,6 +105,9 @@ public:<br>
   llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }<br>
<br>
   uint16_t getEMachine() const { return getObj().getHeader()->e_machine; }<br>
+  uint8_t getOSABI() const {<br>
+    return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];<br>
+  }<br>
<br>
   StringRef getStringTable() const { return StringTable; }<br>
<br>
<br>
Modified: lld/trunk/ELF/Writer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=248554&r1=248553&r2=248554&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=248554&r1=248553&r2=248554&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/ELF/Writer.cpp (original)<br>
+++ lld/trunk/ELF/Writer.cpp Thu Sep 24 20:59:13 2015<br>
@@ -500,14 +500,15 @@ template <class ELFT> void Writer<ELFT>:<br>
                                ? ELFDATA2LSB<br>
                                : ELFDATA2MSB;<br>
   EHdr->e_ident[EI_VERSION] = EV_CURRENT;<br>
-  EHdr->e_ident[EI_OSABI] = ELFOSABI_NONE;<br>
+<br>
+  const SymbolTable &Symtab = SymTabSec.getSymTable();<br>
+  auto &FirstObj = cast<ObjectFile<ELFT>>(*Symtab.getFirstELF());<br>
+  EHdr->e_ident[EI_OSABI] = FirstObj.getOSABI();<br>
<br>
   // FIXME: Generalize the segment construction similar to how we create<br>
   // output sections.<br>
-  const SymbolTable &Symtab = SymTabSec.getSymTable();<br>
<br>
   EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC;<br>
-  auto &FirstObj = cast<ObjectFile<ELFT>>(*Symtab.getFirstELF());<br>
   EHdr->e_machine = FirstObj.getEMachine();<br>
   EHdr->e_version = EV_CURRENT;<br>
   SymbolBody *Entry = Symtab.getEntrySym();<br>
<br>
Added: lld/trunk/test/elf2/basic-freebsd.s<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic-freebsd.s?rev=248554&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic-freebsd.s?rev=248554&view=auto</a><br>
==============================================================================<br>
--- lld/trunk/test/elf2/basic-freebsd.s (added)<br>
+++ lld/trunk/test/elf2/basic-freebsd.s Thu Sep 24 20:59:13 2015<br>
@@ -0,0 +1,25 @@<br>
+# Verify that OSABI is set to the correct value.<br>
+<br>
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %t<br>
+# RUN: lld -flavor gnu2 %t -o %t2<br>
+# RUN: llvm-readobj -file-headers %t2 | FileCheck %s<br>
+# REQUIRES: x86<br>
+<br>
+.globl _start<br>
+_start:<br>
+  mov $1, %rax<br>
+  mov $42, %rdi<br>
+  syscall<br>
+<br>
+# CHECK: ElfHeader {<br>
+# CHECK-NEXT:   Ident {<br>
+# CHECK-NEXT:     Magic: (7F 45 4C 46)<br>
+# CHECK-NEXT:     Class: 64-bit (0x2)<br>
+# CHECK-NEXT:     DataEncoding: LittleEndian (0x1)<br>
+# CHECK-NEXT:     FileVersion: 1<br>
+# CHECK-NEXT:     OS/ABI: FreeBSD (0x9)<br>
+# CHECK-NEXT:     ABIVersion: 0<br>
+# CHECK-NEXT:     Unused: (00 00 00 00 00 00 00)<br>
+# CHECK-NEXT:   }<br>
+# CHECK-NEXT:   Type: Executable (0x2)<br>
+# CHECK-NEXT:   Machine: EM_X86_64 (0x3E)<br></blockquote><div><br></div></div></div><div>Presumably the more important thing for this test is that it actually loads and runs on amd64 freebsd. Any ideas how we could fit that into our testing? (I don't think we have something like `REQUIRES: amd64-host,freebsd-host` that we could conditionalize actually running it). Hopefully it won't happen, but it's conceivable that some low-level layout change or something could cause it to start failing without us noticing (e.g. something gets pushed out of the first page).</div></div></div></div></blockquote><div><br></div><div>What do we do for clang? Do we have a test which runs clang and then execute its output?</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>-- Sean Silva</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></span></div><br></div></div>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div></div>