diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2016-09-22 14:40:50 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2016-09-22 14:40:50 +0000 |
commit | 9546fffbfe2be1709a0ca14229d40dde1a9d9666 (patch) | |
tree | 0d1ac23b7bf44f248fe248c8b753b52120142212 | |
parent | e5555e217cbfc3f8a8d04c5d8cab64d8a6df2004 (diff) | |
download | bcm5719-llvm-9546fffbfe2be1709a0ca14229d40dde1a9d9666.tar.gz bcm5719-llvm-9546fffbfe2be1709a0ca14229d40dde1a9d9666.zip |
Handle empty sections with symbol assignments.
Before the symbols were becoming undefined.
llvm-svn: 282159
-rw-r--r-- | lld/ELF/LinkerScript.cpp | 27 | ||||
-rw-r--r-- | lld/ELF/LinkerScript.h | 1 | ||||
-rw-r--r-- | lld/ELF/Writer.cpp | 1 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/extend-pt-load.s | 57 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/symbol-only.s | 20 |
5 files changed, 105 insertions, 1 deletions
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 922c23e2862..acdc43519b8 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -466,7 +466,7 @@ void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) { [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); }); } -template <class ELFT> void LinkerScript<ELFT>::assignAddresses() { +template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() { // It is common practice to use very generic linker scripts. So for any // given run some of the output sections in the script will be empty. // We could create corresponding empty output sections, but that would @@ -490,6 +490,31 @@ template <class ELFT> void LinkerScript<ELFT>::assignAddresses() { }); Opt.Commands.erase(Pos, Opt.Commands.end()); + // If the output section contains only symbol assignments, create a + // corresponding output section. The bfd linker seems to only create them if + // '.' is assigned to, but creating these section should not have any bad + // consequeces and gives us a section to put the symbol in. + uintX_t Flags = SHF_ALLOC; + uint32_t Type = 0; + for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { + auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); + if (!Cmd) + continue; + std::vector<OutputSectionBase<ELFT> *> Secs = + findSections(*Cmd, *OutputSections); + if (!Secs.empty()) { + Flags = Secs[0]->getFlags(); + Type = Secs[0]->getType(); + continue; + } + + auto *OutSec = new OutputSection<ELFT>(Cmd->Name, Type, Flags); + Out<ELFT>::Pool.emplace_back(OutSec); + OutputSections->push_back(OutSec); + } +} + +template <class ELFT> void LinkerScript<ELFT>::assignAddresses() { // Orphan sections are sections present in the input files which // are not explicitly placed into the output file by the linker script. // We place orphan sections at end of file. diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 6791e73af2b..654aae21dc6 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -187,6 +187,7 @@ public: ~LinkerScript(); void processCommands(OutputSectionFactory<ELFT> &Factory); void createSections(OutputSectionFactory<ELFT> &Factory); + void adjustSectionsBeforeSorting(); std::vector<PhdrEntry<ELFT>> createPhdrs(); bool ignoreInterpSection(); diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index d7e32e3c0e4..6cdf0978e5a 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -723,6 +723,7 @@ template <class ELFT> void Writer<ELFT>::sortSections() { compareSectionsNonScript<ELFT>); return; } + Script<ELFT>::X->adjustSectionsBeforeSorting(); // The order of the sections in the script is arbitrary and may not agree with // compareSectionsNonScript. This means that we cannot easily define a diff --git a/lld/test/ELF/linkerscript/extend-pt-load.s b/lld/test/ELF/linkerscript/extend-pt-load.s new file mode 100644 index 00000000000..b662fe79402 --- /dev/null +++ b/lld/test/ELF/linkerscript/extend-pt-load.s @@ -0,0 +1,57 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o + +# This test demonstrates an odd consequence of the way we handle sections with just symbol +# assignments. + +# First, run a test with no such section. + +# RUN: echo "SECTIONS { \ +# RUN: .text : { *(.text) } \ +# RUN: . = ALIGN(0x1000); \ +# RUN: .data.rel.ro : { *(.data.rel.ro) } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t.o -shared +# RUN: llvm-readobj --elf-output-style=GNU -l -s %t1 | FileCheck --check-prefix=CHECK1 %s + +# CHECK1: .text PROGBITS 00000000000001bc 0001bc 000001 00 AX +# CHECK1-NEXT: .data.rel.ro PROGBITS 0000000000001000 001000 000001 00 WA + +# CHECK1: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x0001bd 0x0001bd R E +# CHECK1-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000068 0x000068 RW + +# Then add the section bar. Note how bar is given AX flags, which causes the PT_LOAD to now +# cover the padding bits created by ALIGN. + +# RUN: echo "SECTIONS { \ +# RUN: .text : { *(.text) } \ +# RUN: . = ALIGN(0x1000); \ +# RUN: bar : { HIDDEN(bar_sym = .); } \ +# RUN: .data.rel.ro : { *(.data.rel.ro) } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t2 --script %t.script %t.o -shared +# RUN: llvm-readobj --elf-output-style=GNU -l -s %t2 | FileCheck --check-prefix=CHECK2 %s + +# CHECK2: .text PROGBITS 00000000000001bc 0001bc 000001 00 AX +# CHECK2-NEXT: bar PROGBITS 0000000000001000 001000 000000 00 AX +# CHECK2-NEXT: .data.rel.ro PROGBITS 0000000000001000 001000 000001 00 WA + +# CHECK2: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x001000 0x001000 R E +# CHECK2-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000068 0x000068 RW + +# If the current behavior becomes a problem we should consider just moving the commands out +# of the section. That is, handle the above like the following test. + +# RUN: echo "SECTIONS { \ +# RUN: .text : { *(.text) } \ +# RUN: . = ALIGN(0x1000); \ +# RUN: HIDDEN(bar_sym = .); \ +# RUN: .data.rel.ro : { *(.data.rel.ro) } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t3 --script %t.script %t.o -shared +# RUN: llvm-readobj --elf-output-style=GNU -l -s %t3 | FileCheck --check-prefix=CHECK1 %s + +nop + +.section .data.rel.ro, "aw" +.byte 0 diff --git a/lld/test/ELF/linkerscript/symbol-only.s b/lld/test/ELF/linkerscript/symbol-only.s new file mode 100644 index 00000000000..7b69a512aa1 --- /dev/null +++ b/lld/test/ELF/linkerscript/symbol-only.s @@ -0,0 +1,20 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { \ +# RUN: abc : { foo = .; } \ +# RUN: . = ALIGN(0x1000); \ +# RUN: bar : { *(bar) } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t -shared +# RUN: llvm-objdump -section-headers -t %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 abc 00000000 [[ADDR:[0-9a-f]*]] +# CHECK-NEXT: 2 bar 00000000 0000000000001000 DATA + +# CHECK: SYMBOL TABLE: +# CHECK: [[ADDR]] abc 00000000 foo + +.section bar, "a" |