[lld] r287565 - Fix address computation for headers.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 21 11:59:34 PST 2016


Author: rafael
Date: Mon Nov 21 13:59:33 2016
New Revision: 287565

URL: http://llvm.org/viewvc/llvm-project?rev=287565&view=rev
Log:
Fix address computation for headers.

If the linker script has SECTIONS, the address computation is now
always done in LinkerScript::assignAddresses, like for any other
section.

Before fixHeaders would do a tentative computation that
assignAddresses would sometimes override.

This patch also splits the cases where assignAddresses needs to add
the headers to the first PT_LOAD and the address computation. The net
effect is that we no longer create an empty page for no reason in the
included test case, which matches bfd behavior.

Added:
    lld/trunk/test/ELF/linkerscript/header-addr.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=287565&r1=287564&r2=287565&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Nov 21 13:59:33 2016
@@ -707,24 +707,21 @@ void LinkerScript<ELFT>::assignAddresses
   if (FirstPTLoad == Phdrs.end())
     return;
 
-  if (HeaderSize <= MinVA) {
-    // If linker script specifies program headers and first PT_LOAD doesn't
-    // have both PHDRS and FILEHDR attributes then do nothing
-    if (!Opt.PhdrsCommands.empty()) {
-      size_t SegNum = std::distance(Phdrs.begin(), FirstPTLoad);
-      if (!Opt.PhdrsCommands[SegNum].HasPhdrs ||
-          !Opt.PhdrsCommands[SegNum].HasFilehdr)
-        return;
-    }
-    // ELF and Program headers need to be right before the first section in
-    // memory. Set their addresses accordingly.
-    MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
-    Out<ELFT>::ElfHeader->Addr = MinVA;
-    Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
+  // If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
+  // now that we know we have space.
+  if (HeaderSize <= MinVA && !hasPhdrsCommands()) {
     FirstPTLoad->First = Out<ELFT>::ElfHeader;
     if (!FirstPTLoad->Last)
       FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
-  } else if (!FirstPTLoad->First) {
+  }
+
+  // ELF and Program headers need to be right before the first section in
+  // memory. Set their addresses accordingly.
+  MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
+  Out<ELFT>::ElfHeader->Addr = MinVA;
+  Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
+
+  if (!FirstPTLoad->First) {
     // Sometimes the very first PT_LOAD segment can be empty.
     // This happens if (all conditions met):
     //  - Linker script is used

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=287565&r1=287564&r2=287565&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Nov 21 13:59:33 2016
@@ -1267,11 +1267,13 @@ template <class ELFT> void Writer<ELFT>:
 // sections. These are special, we do not include them into output sections
 // list, but have them to simplify the code.
 template <class ELFT> void Writer<ELFT>::fixHeaders() {
-  uintX_t BaseVA = ScriptConfig->HasSections ? 0 : Config->ImageBase;
-  Out<ELFT>::ElfHeader->Addr = BaseVA;
-  uintX_t Off = Out<ELFT>::ElfHeader->Size;
-  Out<ELFT>::ProgramHeaders->Addr = Off + BaseVA;
   Out<ELFT>::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
+  // If the script has SECTIONS, assignAddresses will compute the values.
+  if (ScriptConfig->HasSections)
+    return;
+  uintX_t BaseVA = Config->ImageBase;
+  Out<ELFT>::ElfHeader->Addr = BaseVA;
+  Out<ELFT>::ProgramHeaders->Addr = BaseVA + Out<ELFT>::ElfHeader->Size;
 }
 
 // Assign VAs (addresses at run-time) to output sections.

Added: lld/trunk/test/ELF/linkerscript/header-addr.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/header-addr.s?rev=287565&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/header-addr.s (added)
+++ lld/trunk/test/ELF/linkerscript/header-addr.s Mon Nov 21 13:59:33 2016
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "PHDRS {all PT_LOAD PHDRS;} \
+# RUN:       SECTIONS { \
+# RUN:       . = 0x2000; \
+# RUN:       .text : {*(.text)} :all \
+# RUN:     }" > %t.script
+# RUN: ld.lld -o %t.so --script %t.script %t.o -shared
+# RUN: llvm-readobj -program-headers %t.so | FileCheck %s
+
+# CHECK:      ProgramHeaders [
+# CHECK-NEXT:   ProgramHeader {
+# CHECK-NEXT:     Type: PT_LOAD
+# CHECK-NEXT:     Offset: 0x40
+# CHECK-NEXT:     VirtualAddress: 0x1040
+# CHECK-NEXT:     PhysicalAddress: 0x1040
+# CHECK-NEXT:     FileSize: 4176
+# CHECK-NEXT:     MemSize: 4176
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:       PF_R (0x4)
+# CHECK-NEXT:       PF_W (0x2)
+# CHECK-NEXT:       PF_X (0x1)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Alignment: 4096
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]




More information about the llvm-commits mailing list