Fix address computation for headers.

If the linker script has SECTIONS, the address computation is now
always done in LinkerScript::assignAddresses, like for any other
section.

Before fixHeaders would do a tentative computation that
assignAddresses would sometimes override.

This patch also splits the cases where assignAddresses needs to add
the headers to the first PT_LOAD and the address computation. The net
effect is that we no longer create an empty page for no reason in the
included test case, which matches bfd behavior.

llvm-svn: 287565
This commit is contained in:
Rafael Espindola 2016-11-21 19:59:33 +00:00
parent b38ddb15dd
commit 1c57007ec8
3 changed files with 44 additions and 19 deletions

View File

@ -707,24 +707,21 @@ void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs) {
if (FirstPTLoad == Phdrs.end())
return;
if (HeaderSize <= MinVA) {
// If linker script specifies program headers and first PT_LOAD doesn't
// have both PHDRS and FILEHDR attributes then do nothing
if (!Opt.PhdrsCommands.empty()) {
size_t SegNum = std::distance(Phdrs.begin(), FirstPTLoad);
if (!Opt.PhdrsCommands[SegNum].HasPhdrs ||
!Opt.PhdrsCommands[SegNum].HasFilehdr)
return;
}
// ELF and Program headers need to be right before the first section in
// memory. Set their addresses accordingly.
MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
Out<ELFT>::ElfHeader->Addr = MinVA;
Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
// If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
// now that we know we have space.
if (HeaderSize <= MinVA && !hasPhdrsCommands()) {
FirstPTLoad->First = Out<ELFT>::ElfHeader;
if (!FirstPTLoad->Last)
FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
} else if (!FirstPTLoad->First) {
}
// ELF and Program headers need to be right before the first section in
// memory. Set their addresses accordingly.
MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
Out<ELFT>::ElfHeader->Addr = MinVA;
Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
if (!FirstPTLoad->First) {
// Sometimes the very first PT_LOAD segment can be empty.
// This happens if (all conditions met):
// - Linker script is used

View File

@ -1267,11 +1267,13 @@ template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
// sections. These are special, we do not include them into output sections
// list, but have them to simplify the code.
template <class ELFT> void Writer<ELFT>::fixHeaders() {
uintX_t BaseVA = ScriptConfig->HasSections ? 0 : Config->ImageBase;
Out<ELFT>::ElfHeader->Addr = BaseVA;
uintX_t Off = Out<ELFT>::ElfHeader->Size;
Out<ELFT>::ProgramHeaders->Addr = Off + BaseVA;
Out<ELFT>::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
// If the script has SECTIONS, assignAddresses will compute the values.
if (ScriptConfig->HasSections)
return;
uintX_t BaseVA = Config->ImageBase;
Out<ELFT>::ElfHeader->Addr = BaseVA;
Out<ELFT>::ProgramHeaders->Addr = BaseVA + Out<ELFT>::ElfHeader->Size;
}
// Assign VAs (addresses at run-time) to output sections.

View File

@ -0,0 +1,26 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
# RUN: echo "PHDRS {all PT_LOAD PHDRS;} \
# RUN: SECTIONS { \
# RUN: . = 0x2000; \
# RUN: .text : {*(.text)} :all \
# RUN: }" > %t.script
# RUN: ld.lld -o %t.so --script %t.script %t.o -shared
# RUN: llvm-readobj -program-headers %t.so | FileCheck %s
# CHECK: ProgramHeaders [
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_LOAD
# CHECK-NEXT: Offset: 0x40
# CHECK-NEXT: VirtualAddress: 0x1040
# CHECK-NEXT: PhysicalAddress: 0x1040
# CHECK-NEXT: FileSize: 4176
# CHECK-NEXT: MemSize: 4176
# CHECK-NEXT: Flags [
# CHECK-NEXT: PF_R (0x4)
# CHECK-NEXT: PF_W (0x2)
# CHECK-NEXT: PF_X (0x1)
# CHECK-NEXT: ]
# CHECK-NEXT: Alignment: 4096
# CHECK-NEXT: }
# CHECK-NEXT: ]