[ELF] - Change the way how we compute offsets for binary output.
authorGeorge Rimar <grimar@accesssoftek.com>
Sat, 3 Dec 2016 07:23:30 +0000 (07:23 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Sat, 3 Dec 2016 07:23:30 +0000 (07:23 +0000)
Binary output feature is a bit confuzing. bfd and gold output differs a lot sometimes,
though it is important for FreeBSD mbr loaders.

Patch change the way how we compute file offsets for binary output.
This fixes PR31196.

Previously offsets were calculated basing on offsets and addresses of sections
from the same loads:
if (Sec == First)
  return alignTo(Off, Target->MaxPageSize, Sec->Addr);
return First->Offset + Sec->Addr - First->Addr;

bfd assigns offsets for each section to VA - MinVA:
https://github.com/redox-os/binutils-gdb/blob/master/bfd/binary.c#L27
https://github.com/redox-os/binutils-gdb/blob/master/bfd/binary.c#L255
(LMA == VA usually)

This patch for now just stops creating phdrs for binary output.
An effect from this that no any additional calculation for offset is performed:

uintX_t getFileAlignment(uintX_t Off, OutputSectionBase *Sec) {
OutputSectionBase *First = Sec->FirstInPtLoad;
// If the section is not in a PT_LOAD, we have no other constraint.
if (!First)
  return Off; //**First is always null, condition always happens**

That is enough now with combination of another patch to generate output
that is similar to what bfd produce for mbr loader.

Differential revision: https://reviews.llvm.org/D27341

llvm-svn: 288580

lld/ELF/Writer.cpp
lld/test/ELF/oformat-binary-ttext.s [new file with mode: 0644]

index 8b8217b..20c9d75 100644 (file)
@@ -180,10 +180,15 @@ template <class ELFT> void Writer<ELFT>::run() {
   if (Config->Relocatable) {
     assignFileOffsets();
   } else {
-    Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs()
-                                                : createPhdrs();
-    addPtArmExid(Phdrs);
-    fixHeaders();
+    // Binary output does not have PHDRS.
+    if (!Config->OFormatBinary) {
+      Phdrs = Script<ELFT>::X->hasPhdrsCommands()
+                  ? Script<ELFT>::X->createPhdrs()
+                  : createPhdrs();
+      addPtArmExid(Phdrs);
+      fixHeaders();
+    }
+
     if (ScriptConfig->HasSections) {
       Script<ELFT>::X->assignAddresses(Phdrs);
     } else {
diff --git a/lld/test/ELF/oformat-binary-ttext.s b/lld/test/ELF/oformat-binary-ttext.s
new file mode 100644 (file)
index 0000000..aced1a3
--- /dev/null
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: ld.lld -N -Ttext 0x100 -o %t.out %t --oformat binary
+# RUN: od -t x1 -v %t.out | FileCheck %s --check-prefix=BIN
+
+# BIN:      0000000 90 00 00 00 00 00 00 00
+# BIN-NEXT: 0000010
+# BIN-NOT:  0000020
+
+## The same but without OMAGIC.
+# RUN: ld.lld -Ttext 0x100 -o %t.out %t --oformat binary
+# RUN: od -t x1 -v %t.out | FileCheck %s --check-prefix=BIN
+
+.text
+.globl _start
+_start:
+ nop