[PATCH] D24801: [ELF] - Fix combination of -script and relocatable output.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 21 08:00:04 PDT 2016


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

Previously this was completely broken. If -script and -r were used together,
section headers had size 0, because assignOffsets or assignAddresses() were called.

This patch fixes that, testcase demonstrates that linkerscript section rules (if any)
also works correctly.

https://reviews.llvm.org/D24801

Files:
  ELF/LinkerScript.cpp
  ELF/Writer.cpp
  test/ELF/linkerscript/relocatable.s

Index: test/ELF/linkerscript/relocatable.s
===================================================================
--- test/ELF/linkerscript/relocatable.s
+++ test/ELF/linkerscript/relocatable.s
@@ -0,0 +1,34 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { foo : { *(.foo*) } }" > %t.script
+# RUN: ld.lld -r -o %t.out --script %t.script %t
+# RUN: llvm-objdump -section-headers %t.out | FileCheck %s
+
+# CHECK:      Sections:
+# CHECK-NEXT: Idx Name          Size      Address          Type
+# CHECK-NEXT:   0               00000000 0000000000000000
+# CHECK-NEXT:   1 foo           00000048 0000000000000000 DATA
+# CHECK-NEXT:   2 .text         00000049 0000000000000000 TEXT DATA
+# CHECK-NEXT:   3 .bar1         0000004d 0000000000000000 DATA
+# CHECK-NEXT:   4 .bar2         00000051 0000000000000000 DATA
+
+.global _start
+_start:
+  nop
+
+.section .foo1, "a"
+foo1:
+ .long 1
+
+.section .foo2, "a"
+foo2:
+ .long 1
+
+.section .bar1, "aw"
+bar1:
+ .long 1
+
+.section .bar2, "aw"
+bar2:
+ .long 1
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -266,6 +266,8 @@
     return;
 
   if (Config->Relocatable) {
+    if (ScriptConfig->HasSections)
+      Script<ELFT>::X->assignAddresses();
     assignFileOffsets();
   } else {
     Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs()
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -398,7 +398,7 @@
   CurOutSec = Sec;
 
   Dot = alignTo(Dot, CurOutSec->getAlignment());
-  CurOutSec->setVA(Dot);
+  CurOutSec->setVA(Config->Relocatable ? 0 : Dot);
 }
 
 template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
@@ -551,6 +551,12 @@
     assignOffsets(Cmd);
   }
 
+  if (Config->Relocatable) {
+    Out<ELFT>::ElfHeader->setVA(0);
+    Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize());
+    return;
+  }
+
   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
     if (Sec->getFlags() & SHF_ALLOC)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24801.72043.patch
Type: text/x-patch
Size: 2226 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160921/07766c5e/attachment.bin>


More information about the llvm-commits mailing list