[PATCH] D27200: [ELF] - Do not create 4gb output when -obinary -Ttext and -omagic used together.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 29 04:23:05 PST 2016


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

This fixed PR31196,

problem is that ElfHeader address was taken in account when we calculated offsets:

  static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase *Sec) {
  ...
    return First->Offset + Sec->Addr - First->Addr;

Sec->Addr is 600 here.
First->Addr is 0x10000 (First here is Out<ELFT>::ElfHeader->Addr == ImageBase).

Binary output does not have headers, we do not need to rely on their address to
calculate file offset.


https://reviews.llvm.org/D27200

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
@@ -1124,8 +1124,11 @@
 
   // Add the first PT_LOAD segment for regular output sections.
   uintX_t Flags = computeFlags<ELFT>(PF_R);
-  Phdr *Load = AddHdr(PT_LOAD, Flags);
-  if (!ScriptConfig->HasSections) {
+
+  // Do not create PT_LOAD segment for binary output, it does not
+  // have file or program headers.
+  Phdr *Load = Config->OFormatBinary ? nullptr : AddHdr(PT_LOAD, Flags);
+  if (!ScriptConfig->HasSections && !Config->OFormatBinary) {
     Load->add(Out<ELFT>::ElfHeader);
     Load->add(Out<ELFT>::ProgramHeaders);
   }
@@ -1152,7 +1155,7 @@
     // different flags or is loaded at a discontiguous address using AT linker
     // script command.
     uintX_t NewFlags = computeFlags<ELFT>(Sec->getPhdrFlags());
-    if (Script<ELFT>::X->hasLMA(Sec->getName()) || Flags != NewFlags) {
+    if (!Load || Script<ELFT>::X->hasLMA(Sec->getName()) || Flags != NewFlags) {
       Load = AddHdr(PT_LOAD, NewFlags);
       Flags = NewFlags;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27200.79536.patch
Type: text/x-patch
Size: 1774 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161129/b10a2314/attachment.bin>


More information about the llvm-commits mailing list