[lld] r305766 - Prefer -Ttext over linker script values.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 19 18:51:50 PDT 2017


Author: rafael
Date: Mon Jun 19 20:51:50 2017
New Revision: 305766

URL: http://llvm.org/viewvc/llvm-project?rev=305766&view=rev
Log:
Prefer -Ttext over linker script values.

I found this while trying to build u-boot. It uses -Ttext in
combination with linker scripts.

My first reaction was to change the linker scripts to have the correct
value, but I found that it is actually quite convenient to have -Ttext
take precedence.

By having just

.text : { *(.text) }

In the script, they can define the text address in a single Makefile
and pass it to ld with -Ttext and for the C code with
-DFoo=value. Doing the same with linker scripts would require them to
be generated during the build.

Added:
    lld/trunk/test/ELF/linkerscript/ttext-script.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=305766&r1=305765&r2=305766&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Jun 19 20:51:50 2017
@@ -464,11 +464,6 @@ void LinkerScript::fabricateDefaultComma
     OSCmd->Sec = Sec;
     SecToCommand[Sec] = OSCmd;
 
-    // Prefer user supplied address over additional alignment constraint
-    auto I = Config->SectionStartMap.find(Sec->Name);
-    if (I != Config->SectionStartMap.end())
-      OSCmd->AddrExpr = [=] { return I->second; };
-
     Commands.push_back(OSCmd);
     if (Sec->Sections.size()) {
       auto *ISD = make<InputSectionDescription>("");

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=305766&r1=305765&r2=305766&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Jun 19 20:51:50 2017
@@ -1231,6 +1231,13 @@ template <class ELFT> void Writer<ELFT>:
     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
       OutputSectionCommands.push_back(Cmd);
 
+  // Prefer command line supplied address over other constraints.
+  for (OutputSectionCommand *Cmd : OutputSectionCommands) {
+    auto I = Config->SectionStartMap.find(Cmd->Name);
+    if (I != Config->SectionStartMap.end())
+      Cmd->AddrExpr = [=] { return I->second; };
+  }
+
   // This is a bit of a hack. A value of 0 means undef, so we set it
   // to 1 t make __ehdr_start defined. The section number is not
   // particularly relevant.

Added: lld/trunk/test/ELF/linkerscript/ttext-script.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/ttext-script.s?rev=305766&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/ttext-script.s (added)
+++ lld/trunk/test/ELF/linkerscript/ttext-script.s Mon Jun 19 20:51:50 2017
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { .text 0x200000 : { *(.text) } }" > %t.script
+# RUN: ld.lld -T %t.script -Ttext 0x100000 %t.o -o %t
+# RUN: llvm-readobj --elf-output-style=GNU -s  %t | FileCheck %s
+
+# CHECK: .text             PROGBITS        0000000000100000
+
+.global _start
+_start:
+nop




More information about the llvm-commits mailing list