[PATCH] D29607: Handle symbol assignments before the first section switch

Rafael Ávila de Espíndola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 13:49:40 PST 2017


rafael created this revision.

This is an alternative to https://reviews.llvm.org/D29391 which is a lot simpler IMHO.


https://reviews.llvm.org/D29607

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/non-absolute2.s


Index: test/ELF/linkerscript/non-absolute2.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/non-absolute2.s
@@ -0,0 +1,12 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
+# RUN: echo "SECTIONS { A = . + 0x1; . += 0x1000; }" > %t.script
+# RUN: ld.lld -shared %t1.o --script %t.script -o %t
+# RUN: llvm-objdump -section-headers -t %t | FileCheck %s
+
+# CHECK:       Sections:
+# CHECK-NEXT:   Idx Name          Size      Address
+# CHECK-NEXT:    0               00000000 0000000000000000
+# CHECK-NEXT:    1 .text         00000000 0000000000001000
+
+# CHECK: 0000000000000001         .text            00000000 A
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -776,6 +776,19 @@
   // Assign addresses as instructed by linker script SECTIONS sub-commands.
   Dot = 0;
 
+  // A symbol can be assigned before any section is mention in the linker
+  // script. In an DSO, the symbol values are addresses, so the only important
+  // section values are:
+  // * SHN_UNDEF
+  // * SHN_ABS
+  // * Any value meaning a regular section.
+  // To handle that, create a dummy aether section that fills the void before
+  // the linker scripts switches to another section. It has an index of one
+  // which will map to whatever the first actual section is.
+  auto *Aether = make<OutputSectionBase>("", 0, SHF_ALLOC);
+  Aether->SectionIndex = 1;
+  switchTo(Aether);
+
   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
       if (Cmd->Name == ".") {
@@ -973,14 +986,9 @@
 // to find suitable section for it as well.
 template <class ELFT>
 const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) {
-  SymbolBody *Sym = Symtab<ELFT>::X->find(S);
-  if (!Sym) {
-    if (OutputSections->empty())
-      return nullptr;
-    return CurOutSec ? CurOutSec : (*OutputSections)[0];
-  }
-
-  return SymbolTableSection<ELFT>::getOutputSection(Sym);
+  if (SymbolBody *Sym = Symtab<ELFT>::X->find(S))
+    return SymbolTableSection<ELFT>::getOutputSection(Sym);
+  return CurOutSec;
 }
 
 // Returns indices of ELF headers containing specific section, identified


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29607.87301.patch
Type: text/x-patch
Size: 2347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170206/88a20dca/attachment.bin>


More information about the llvm-commits mailing list