Handle empty sections with symbol assignments.

Before the symbols were becoming undefined.

llvm-svn: 282159
This commit is contained in:
Rafael Espindola 2016-09-22 14:40:50 +00:00
parent e5555e217c
commit 9546fffbfe
5 changed files with 105 additions and 1 deletions

View File

@ -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.

View File

@ -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();

View File

@ -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

View File

@ -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

View File

@ -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"