From 467c4d5538e877a8aa5ccbfe88c20ec5d82ad5ad Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Fri, 1 Jul 2016 10:27:36 +0000 Subject: [PATCH] [ELF] Fix first PT_LOAD segment VA calculation, when linker script is used llvm-svn: 274342 --- lld/ELF/LinkerScript.cpp | 13 ++++++++++++- lld/ELF/Writer.cpp | 21 ++++++++++++--------- lld/test/ELF/linkerscript-phdr-check.s | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 lld/test/ELF/linkerscript-phdr-check.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 0bc7dd6d7f92..d10fc5bc629f 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -21,6 +21,7 @@ #include "ScriptParser.h" #include "Strings.h" #include "SymbolTable.h" +#include "Target.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ELF.h" #include "llvm/Support/FileSystem.h" @@ -220,7 +221,8 @@ void LinkerScript::assignAddresses( } // Assign addresses as instructed by linker script SECTIONS sub-commands. - Dot = Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); + Dot = Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); + uintX_t MinVA = std::numeric_limits::max(); uintX_t ThreadBssOffset = 0; for (SectionsCommand &Cmd : Opt.Commands) { @@ -247,11 +249,20 @@ void LinkerScript::assignAddresses( if (Sec->getFlags() & SHF_ALLOC) { Dot = alignTo(Dot, Sec->getAlignment()); Sec->setVA(Dot); + MinVA = std::min(MinVA, Dot); Dot += Sec->getSize(); continue; } } } + + // ELF and Program headers need to be right before the first section in memory. + // Set their addresses accordingly. + MinVA = alignDown(MinVA - Out::ElfHeader->getSize() - + Out::ProgramHeaders->getSize(), + Target->PageSize); + Out::ElfHeader->setVA(MinVA); + Out::ProgramHeaders->setVA(Out::ElfHeader->getSize() + MinVA); } template diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 28036d3dbeb6..799580d9a276 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1131,11 +1131,9 @@ template void Writer::fixSectionAlignments() { // list, but have them to simplify the code. template void Writer::fixHeaders() { uintX_t BaseVA = ScriptConfig->DoLayout ? 0 : Target->getVAStart(); - Out::ElfHeader->setVA(BaseVA); - Out::ElfHeader->setFileOffset(0); + Out::ElfHeader->setVA(BaseVA); uintX_t Off = Out::ElfHeader->getSize(); - Out::ProgramHeaders->setVA(Off + BaseVA); - Out::ProgramHeaders->setFileOffset(Off); + Out::ProgramHeaders->setVA(Off + BaseVA); } // Assign VAs (addresses at run-time) to output sections. @@ -1183,19 +1181,24 @@ static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase *Sec) { // Assign file offsets to output sections. template void Writer::assignFileOffsets() { - uintX_t Off = - Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); + uintX_t Off = 0; - for (OutputSectionBase *Sec : OutputSections) { + auto Set = [&](OutputSectionBase *Sec) { if (Sec->getType() == SHT_NOBITS) { Sec->setFileOffset(Off); - continue; + return; } Off = getFileAlignment(Off, Sec); Sec->setFileOffset(Off); Off += Sec->getSize(); - } + }; + + Set(Out::ElfHeader); + Set(Out::ProgramHeaders); + for (OutputSectionBase *Sec : OutputSections) + Set(Sec); + SectionHeaderOff = alignTo(Off, sizeof(uintX_t)); FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr); } diff --git a/lld/test/ELF/linkerscript-phdr-check.s b/lld/test/ELF/linkerscript-phdr-check.s new file mode 100644 index 000000000000..c7229ed3312c --- /dev/null +++ b/lld/test/ELF/linkerscript-phdr-check.s @@ -0,0 +1,15 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { . = 0x10000000; .text : {*(.text.*)} }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-readobj -program-headers %t1 | FileCheck %s +# CHECK: ProgramHeaders [ +# CHECK-NEXT: ProgramHeader { +# CHECK-NEXT: Type: PT_PHDR (0x6) +# CHECK-NEXT: Offset: 0x40 +# CHECK-NEXT: VirtualAddress: 0xFFFF040 + +.global _start +_start: + nop