[PATCH] D27341: [ELF] - Change the way how we compute offsets for binary output.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 2 06:33:39 PST 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael, emaste.
grimar added subscribers: llvm-commits, grimar, evgeny777.

Binary output feature is a bit confuxing. 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 a sections offsets in the same loads.

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

This patch for now just stops creating phdrs for binary output. An effect from this that
that offset remains unchanged:

  OutputSectionBase *First = Sec->FirstInPtLoad;
  // If the section is not in a PT_LOAD, we have no other constraint.
  if (!First)
    return Off; //**Always happens**
  if (Sec == First)
    return alignTo(Off, Target->MaxPageSize, Sec->Addr);
  return First->Offset + Sec->Addr - First->Addr;

That is enough with combination of another 2 patches to generate output
that is similar to what bfd produce.
I uploaded the demo patch showing that: https://reviews.llvm.org/D27340


https://reviews.llvm.org/D27341

Files:
  ELF/Writer.cpp
  test/ELF/oformat-binary-ttext.s


Index: test/ELF/oformat-binary-ttext.s
===================================================================
--- test/ELF/oformat-binary-ttext.s
+++ test/ELF/oformat-binary-ttext.s
@@ -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
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -180,10 +180,15 @@
   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 {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27341.80062.patch
Type: text/x-patch
Size: 1473 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161202/f3675b16/attachment.bin>


More information about the llvm-commits mailing list