[PATCH] D23165: [ELF] - Linkerscript: implement SIZEOF_HEADERS.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 10 01:07:25 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL278204: [ELF] - Linkerscript: implement SIZEOF_HEADERS. (authored by grimar).

Changed prior to commit:
  https://reviews.llvm.org/D23165?vs=67297&id=67478#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D23165

Files:
  lld/trunk/ELF/LinkerScript.cpp
  lld/trunk/ELF/LinkerScript.h
  lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s

Index: lld/trunk/ELF/LinkerScript.h
===================================================================
--- lld/trunk/ELF/LinkerScript.h
+++ lld/trunk/ELF/LinkerScript.h
@@ -149,6 +149,7 @@
   void addScriptedSymbols();
   bool hasPhdrsCommands();
   uintX_t getOutputSectionSize(StringRef Name);
+  uintX_t getSizeOfHeaders();
 
   std::vector<OutputSectionBase<ELFT> *> *OutputSections;
 
Index: lld/trunk/ELF/LinkerScript.cpp
===================================================================
--- lld/trunk/ELF/LinkerScript.cpp
+++ lld/trunk/ELF/LinkerScript.cpp
@@ -238,7 +238,7 @@
   }
 
   // Assign addresses as instructed by linker script SECTIONS sub-commands.
-  Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
+  Dot = getSizeOfHeaders();
   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
   uintX_t ThreadBssOffset = 0;
 
@@ -430,6 +430,11 @@
   return 0;
 }
 
+template <class ELFT>
+typename ELFT::uint LinkerScript<ELFT>::getSizeOfHeaders() {
+  return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
+}
+
 // Returns indices of ELF headers containing specific section, identified
 // by Name. Each index is a zero based number of ELF header listed within
 // PHDRS {} script block.
@@ -915,6 +920,21 @@
   }
 }
 
+static uint64_t getSizeOfHeaders() {
+  switch (Config->EKind) {
+  case ELF32LEKind:
+    return Script<ELF32LE>::X->getSizeOfHeaders();
+  case ELF32BEKind:
+    return Script<ELF32BE>::X->getSizeOfHeaders();
+  case ELF64LEKind:
+    return Script<ELF64LE>::X->getSizeOfHeaders();
+  case ELF64BEKind:
+    return Script<ELF64BE>::X->getSizeOfHeaders();
+  default:
+    llvm_unreachable("unsupported target");
+  }
+}
+
 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
   StringRef Op = next();
   assert(Op == "=" || Op == "+=");
@@ -1063,6 +1083,8 @@
     expect(")");
     return [=](uint64_t Dot) { return getSectionSize(Name); };
   }
+  if (Tok == "SIZEOF_HEADERS")
+    return [=](uint64_t Dot) { return getSizeOfHeaders(); };
 
   // Parse a symbol name or a number literal.
   uint64_t V = 0;
Index: lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s
===================================================================
--- lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s
+++ lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: echo " SECTIONS {             \
+# RUN:          . = SIZEOF_HEADERS;  \
+# RUN:          _size = SIZEOF_HEADERS;  \
+# RUN:          .text : {*(.text.*)} \
+# RUN:          }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -t %t1 | FileCheck %s
+
+#CHECK:      SYMBOL TABLE:
+#CHECK-NEXT:  0000000000000000 *UND* 00000000
+#CHECK-NEXT:  0000000000000120 .text 00000000 _start
+#CHECK-NEXT:  0000000000000120 *ABS* 00000000 _size
+
+.global _start
+_start:
+ nop


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23165.67478.patch
Type: text/x-patch
Size: 2982 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160810/3e8ad463/attachment.bin>


More information about the llvm-commits mailing list