Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -19,8 +19,10 @@ namespace lld { namespace elf { +class LocCounterEvaluator; class ScriptParser; template class InputSectionBase; +template class OutputSectionBase; // This class represents each rule in SECTIONS command. class SectionRule { @@ -40,6 +42,18 @@ StringRef SectionPattern; }; +// This enum represents what we can observe in SECTIONS tag of script: +// LocSet is a location counter change, like ". = . + 0x1000" +// LocOutSec is a description of output section, like ".data :..." +enum LocNodeType { LocSet, LocOutSec }; +struct LocationNode { + LocNodeType Type; + + // For LocSet this keeps list of tokens to evaluate location. + // For LocOutSec first element is a output section name. + std::vector Args; +}; + // This is a runner of the linker script. class LinkerScript { friend class ScriptParser; @@ -53,8 +67,18 @@ ArrayRef getFiller(StringRef Name); template bool isDiscarded(InputSectionBase *S); template bool shouldKeep(InputSectionBase *S); + template + void layoutSections(std::vector *> S); + uint64_t getSecVA(StringRef Name); + uint64_t getDummieSecVA(size_t Ndx); int compareSections(StringRef A, StringRef B); + bool Exist = false; + + LocationNode &addLocation(LocNodeType Type) { + return *Locations.insert(Locations.end(), {Type}); + } + private: template SectionRule *find(InputSectionBase *S); @@ -67,6 +91,10 @@ // Section fill attribute for each section. llvm::StringMap> Filler; + // Used to assign addresses to sections. + std::vector Locations; + std::vector> SecVA; + llvm::BumpPtrAllocator Alloc; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -17,18 +17,60 @@ #include "Config.h" #include "Driver.h" #include "InputSection.h" +#include "OutputSections.h" #include "SymbolTable.h" +#include "llvm/Support/ELF.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/StringSaver.h" using namespace llvm; +using namespace llvm::ELF; using namespace llvm::object; using namespace lld; using namespace lld::elf; +unsigned dummySectionsNum(); + +class elf::LocCounterEvaluator { +public: + void setLocationCounter(uint64_t VA) { Values["."] = VA; } + + // Evaluates the expression given by list of tokens. + uint64_t evaluate(std::vector &Tokens) { + uint64_t Result = 0; + for (size_t I = 0, E = Tokens.size(); I < E; ++I) { + // Each second token should be '+' as this is the + // only operator we support now + if (I % 2 == 1) { + if (Tokens[I] == "+") + continue; + error("error in location counter expression"); + return 0; + } + Result += getValue(Tokens[I]); + } + return Result; + } + + uint64_t getValue(StringRef V) { + uint64_t Val; + if (!V.getAsInteger(0, Val)) + return Val; + auto I = Values.find(V); + if (I != Values.end()) + return I->second; + error("undeclared variable " + V); + return 0; + } + +protected: + std::map Values; +}; + LinkerScript *elf::Script; +LocCounterEvaluator Eval; template SectionRule *LinkerScript::find(InputSectionBase *S) { @@ -54,6 +96,81 @@ return R && R->Keep; } +uint64_t LinkerScript::getSecVA(StringRef Name) { + auto I = std::find_if( + SecVA.begin(), SecVA.end(), + [&](std::pair &P) { return P.first == Name; }); + return I->second; +} + +// Dummies has empty name and therefore requires special handling. +uint64_t LinkerScript::getDummieSecVA(size_t Ndx) { return SecVA[Ndx].second; } + +template +void LinkerScript::layoutSections(std::vector *> S) { + typedef typename ELFT::uint uintX_t; + + auto FindSec = [&](StringRef Name) { + return std::find_if(S.begin(), S.end(), [&](OutputSectionBase *Sec) { + return Sec->getName() == Name; + }); + }; + + uintX_t ThreadBssOffset = 0; + uintX_t VA = 0; + auto VisitLocations = [&](std::vector &Loc) { + for (LocationNode &Node : Loc) { + if (Node.Type == LocSet) { + Eval.setLocationCounter(VA); + VA = Eval.evaluate(Node.Args); + continue; + } + + StringRef Name = Node.Args[0]; + auto I = FindSec(Name); + if (I == S.end()) + continue; + + OutputSectionBase *Sec = *I; + S.erase(I); + + uintX_t Align = Sec->getAlign(); + if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) { + uintX_t TVA = VA + ThreadBssOffset; + TVA = alignTo(TVA, Align); + SecVA.push_back({Name, TVA}); + ThreadBssOffset = TVA - VA + Sec->getSize(); + continue; + } + + if (!(Sec->getFlags() & SHF_ALLOC)) { + SecVA.push_back({Name, 0}); + continue; + } + + VA = alignTo(VA, Align); + SecVA.push_back({Name, VA}); + VA += Sec->getSize(); + } + }; + + // At first assign addresses to sections directly specified in linkerscript. + VisitLocations(Locations); + + // Now create list of unvisited sections, that were not mentioned in script, + // but exist in output sections list. They will be put after all others, + // visit them to assign VA's. + std::vector Left; + for (OutputSectionBase *Sec : S) { + (*Left.insert(Left.end(), {LocOutSec})).Args.push_back(Sec->getName()); + } + VisitLocations(Left); + + // Update "_end" and "end" symbols so that they + // point to the end of the data segment. + ElfSym::End.st_value = VA; +} + ArrayRef LinkerScript::getFiller(StringRef Name) { auto I = Filler.find(Name); if (I == Filler.end()) @@ -134,6 +251,7 @@ void readSearchDir(); void readSections(); + void readLocationCounterValue(); void readOutputSectionDescription(); void readSectionPatterns(StringRef OutSec, bool Keep); @@ -163,6 +281,9 @@ {";", &ScriptParser::readNothing}}; void ScriptParser::run() { + for (unsigned I = 0; I < dummySectionsNum(); ++I) + Script->addLocation(LocOutSec).Args.push_back(""); + while (!atEOF()) { StringRef Tok = next(); if (Handler Fn = Cmd.lookup(Tok)) @@ -404,8 +525,13 @@ void ScriptParser::readSections() { expect("{"); - while (!Error && !skip("}")) - readOutputSectionDescription(); + while (!Error && !skip("}")) { + StringRef Tok = peek(); + if (Tok == ".") + readLocationCounterValue(); + else + readOutputSectionDescription(); + } } void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) { @@ -438,9 +564,22 @@ return Hex; } +void ScriptParser::readLocationCounterValue() { + expect("."); + expect("="); + LocationNode &Node = Script->addLocation(LocSet); + while (!Error) { + StringRef Tok = next(); + if (Tok == ";") + break; + Node.Args.push_back(Tok); + } +} + void ScriptParser::readOutputSectionDescription() { StringRef OutSec = next(); Script->SectionOrder.push_back(OutSec); + Script->addLocation(LocOutSec).Args.push_back(OutSec); expect(":"); expect("{"); while (!Error && !skip("}")) { @@ -481,6 +620,7 @@ void LinkerScript::read(MemoryBufferRef MB) { StringRef Path = MB.getBufferIdentifier(); ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run(); + Exist = true; } template StringRef LinkerScript::getOutputSection(InputSectionBase *); @@ -497,3 +637,12 @@ template bool LinkerScript::shouldKeep(InputSectionBase *); template bool LinkerScript::shouldKeep(InputSectionBase *); template bool LinkerScript::shouldKeep(InputSectionBase *); + +template void +LinkerScript::layoutSections(std::vector *> S); +template void +LinkerScript::layoutSections(std::vector *> S); +template void +LinkerScript::layoutSections(std::vector *> S); +template void +LinkerScript::layoutSections(std::vector *> S); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -71,7 +71,11 @@ void createPhdrs(); void assignAddresses(); void assignAddressesRelocatable(); + void assignAddressesScript(); void fixSectionAlignments(); + + void assignPhdrs(); + void fixAbsoluteSymbols(); bool openFile(); void writeHeader(); @@ -103,9 +107,6 @@ unsigned getNumSections() const { return OutputSections.size() + 1 - dummySectionsNum(); } - // Usually there are 2 dummies sections: ELF header and program header. - // Relocatable output does not require program headers to be created. - unsigned dummySectionsNum() const { return Config->Relocatable ? 1 : 2; } void addRelIpltSymbols(); void addStartEndSymbols(); @@ -123,6 +124,10 @@ }; } // anonymous namespace +// Usually there are 2 dummies sections: ELF header and program header. +// Relocatable output does not require program headers to be created. +unsigned dummySectionsNum() { return Config->Relocatable ? 1 : 2; } + template void elf::writeResult(SymbolTable *Symtab) { typedef typename ELFT::uint uintX_t; @@ -211,13 +216,19 @@ addReservedSymbols(); if (!createSections()) return; + if (!Config->Relocatable) { createPhdrs(); fixSectionAlignments(); - assignAddresses(); - } else { - assignAddressesRelocatable(); } + + if (Script->Exist) + assignAddressesScript(); + else if (Config->Relocatable) + assignAddressesRelocatable(); + else + assignAddresses(); + fixAbsoluteSymbols(); if (!openFile()) return; @@ -1397,6 +1408,41 @@ // point to the end of the data segment. ElfSym::End.st_value = VA; + assignPhdrs(); +} + +template void Writer::assignAddressesScript() { + Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); + size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size(); + Out::ProgramHeaders->setSize(PhdrSize); + + Script->layoutSections(OutputSections); + + uintX_t FileOff = 0; + size_t I = 0; + for (OutputSectionBase *Sec : OutputSections) { + uintX_t Align = Sec->getAlign(); + if (Sec->getType() != SHT_NOBITS) + FileOff = alignTo(FileOff, Align); + Sec->setFileOffset(FileOff); + if (Sec->getType() != SHT_NOBITS) + FileOff += Sec->getSize(); + + StringRef Name = Sec->getName(); + uintX_t VA = (I < dummySectionsNum()) ? (uintX_t)Script->getDummieSecVA(I) + : (uintX_t)Script->getSecVA(Name); + Sec->setVA(VA); + ++I; + } + + // Add space for section headers. + SectionHeaderOff = alignTo(FileOff, sizeof(uintX_t)); + FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr); + + assignPhdrs(); +} + +template void Writer::assignPhdrs() { for (Phdr &PHdr : Phdrs) { Elf_Phdr &H = PHdr.H; if (PHdr.First) { Index: test/ELF/end.s =================================================================== --- test/ELF/end.s +++ test/ELF/end.s @@ -36,13 +36,13 @@ // NOBSS-NEXT: SHF_ALLOC // NOBSS-NEXT: SHF_WRITE // NOBSS-NEXT: ] -// NOBSS-NEXT: Address: 0x12000 +// NOBSS-NEXT: Address: 0x159 // NOBSS-NEXT: Offset: // NOBSS-NEXT: Size: 2 // NOBSS: ] // NOBSS: Symbols [ // NOBSS: Name: _end -// NOBSS-NEXT: Value: 0x12002 +// NOBSS-NEXT: Value: 0x15B // NOBSS: ] // If the layout of the sections is changed, "_end" should point to the end of allocated address space. @@ -60,13 +60,13 @@ // TEXTATEND-NEXT: SHF_ALLOC // TEXTATEND-NEXT: SHF_EXECINSTR // TEXTATEND-NEXT: ] -// TEXTATEND-NEXT: Address: 0x12000 +// TEXTATEND-NEXT: Address: 0x160 // TEXTATEND-NEXT: Offset: // TEXTATEND-NEXT: Size: 1 // TEXTATEND: ] // TEXTATEND: Symbols [ // TEXTATEND: Name: _end -// TEXTATEND-NEXT: Value: 0x12001 +// TEXTATEND-NEXT: Value: 0x161 // TEXTATEND: ] .global _start,_end Index: test/ELF/linkerscript-locationcounter.s =================================================================== --- test/ELF/linkerscript-locationcounter.s +++ test/ELF/linkerscript-locationcounter.s @@ -0,0 +1,67 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "SECTIONS { \ +# RUN: . = 0x12341; \ +# RUN: .data : { *(.data) } \ +# RUN: . = . + 0x10000; \ +# RUN: .text : { *(.text) } \ +# RUN: }" > %t.script + +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-readobj -s %t2 | FileCheck %s + +# CHECK: Sections [ +# CHECK-NEXT: Section { +# CHECK-NEXT: Index: 0 +# CHECK-NEXT: Name: (0) +# CHECK-NEXT: Type: SHT_NULL +# CHECK-NEXT: Flags [ +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x0 +# CHECK-NEXT: Offset: 0x0 +# CHECK-NEXT: Size: 0 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 0 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: } +# CHECK-NEXT: Section { +# CHECK-NEXT: Index: 1 +# CHECK-NEXT: Name: .data +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: SHF_WRITE +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x12341 +# CHECK-NEXT: Offset: 0x158 +# CHECK-NEXT: Size: 8 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 1 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: } +# CHECK-NEXT: Section { +# CHECK-NEXT: Index: 2 +# CHECK-NEXT: Name: .text +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags [ +# CHECK-NEXT: SHF_ALLOC +# CHECK-NEXT: SHF_EXECINSTR +# CHECK-NEXT: ] +# CHECK-NEXT: Address: 0x2234C +# CHECK-NEXT: Offset: 0x160 +# CHECK-NEXT: Size: 1 +# CHECK-NEXT: Link: 0 +# CHECK-NEXT: Info: 0 +# CHECK-NEXT: AddressAlignment: 4 +# CHECK-NEXT: EntrySize: 0 +# CHECK-NEXT: } + +.globl _start; +_start: +nop + +.section .data +.quad 0 + Index: test/ELF/linkerscript-sections-keep.s =================================================================== --- test/ELF/linkerscript-sections-keep.s +++ test/ELF/linkerscript-sections-keep.s @@ -12,8 +12,8 @@ # SECGC: Sections: # SECGC-NEXT: Idx Name Size Address Type # SECGC-NEXT: 0 00000000 0000000000000000 -# SECGC-NEXT: 1 .text 00000007 0000000000011000 TEXT DATA -# SECGC-NEXT: 2 .temp 00000004 0000000000012000 DATA +# SECGC-NEXT: 1 .text 00000007 0000000000000158 TEXT DATA +# SECGC-NEXT: 2 .temp 00000004 000000000000015f DATA ## Now apply KEEP command to preserve the section. # RUN: echo "SECTIONS { \ @@ -26,9 +26,9 @@ # SECNOGC: Sections: # SECNOGC-NEXT: Idx Name Size Address Type # SECNOGC-NEXT: 0 00000000 0000000000000000 -# SECNOGC-NEXT: 1 .text 00000007 0000000000011000 TEXT DATA -# SECNOGC-NEXT: 2 .keep 00000004 0000000000012000 DATA -# SECNOGC-NEXT: 3 .temp 00000004 0000000000012004 DATA +# SECNOGC-NEXT: 1 .text 00000007 0000000000000158 TEXT DATA +# SECNOGC-NEXT: 2 .keep 00000004 000000000000015f DATA +# SECNOGC-NEXT: 3 .temp 00000004 0000000000000163 DATA ## A section name matches two entries in the SECTIONS directive. The ## first one doesn't have KEEP, the second one does. If section that have @@ -42,9 +42,9 @@ # KEEP-AT-FIRST: Sections: # KEEP-AT-FIRST-NEXT: Idx Name Size Address Type # KEEP-AT-FIRST-NEXT: 0 00000000 0000000000000000 -# KEEP-AT-FIRST-NEXT: 1 .keep 00000004 0000000000010120 DATA -# KEEP-AT-FIRST-NEXT: 2 .temp 00000004 0000000000010124 DATA -# KEEP-AT-FIRST-NEXT: 3 .text 00000007 0000000000011000 TEXT DATA +# KEEP-AT-FIRST-NEXT: 1 .keep 00000004 0000000000000120 DATA +# KEEP-AT-FIRST-NEXT: 2 .temp 00000004 0000000000000124 DATA +# KEEP-AT-FIRST-NEXT: 3 .text 00000007 0000000000000128 TEXT DATA # KEEP-AT-FIRST-NEXT: 4 .symtab 00000060 0000000000000000 # KEEP-AT-FIRST-NEXT: 5 .shstrtab 0000002d 0000000000000000 # KEEP-AT-FIRST-NEXT: 6 .strtab 00000012 0000000000000000 @@ -63,8 +63,8 @@ # KEEP-AT-SECOND: Sections: # KEEP-AT-SECOND-NEXT: Idx Name Size Address Type # KEEP-AT-SECOND-NEXT: 0 00000000 0000000000000000 -# KEEP-AT-SECOND-NEXT: 1 .temp 00000004 0000000000010120 DATA -# KEEP-AT-SECOND-NEXT: 2 .text 00000007 0000000000011000 TEXT DATA +# KEEP-AT-SECOND-NEXT: 1 .temp 00000004 0000000000000120 DATA +# KEEP-AT-SECOND-NEXT: 2 .text 00000007 0000000000000124 TEXT DATA # KEEP-AT-SECOND-NEXT: 3 .symtab 00000048 0000000000000000 # KEEP-AT-SECOND-NEXT: 4 .shstrtab 00000027 0000000000000000 # KEEP-AT-SECOND-NEXT: 5 .strtab 0000000d 0000000000000000 Index: test/ELF/linkerscript-va.s =================================================================== --- test/ELF/linkerscript-va.s +++ test/ELF/linkerscript-va.s @@ -0,0 +1,24 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -section-headers %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address Type +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .foo 00000004 0000000000000120 DATA +# CHECK-NEXT: 2 .boo 00000004 0000000000000124 DATA +# CHECK-NEXT: 3 .text 00000001 0000000000000128 TEXT DATA + +.global _start +_start: + nop + +.section .foo, "a" +foo: + .long 0 + +.section .boo, "a" +boo: + .long 0 Index: test/ELF/wildcards.s =================================================================== --- test/ELF/wildcards.s +++ test/ELF/wildcards.s @@ -10,10 +10,10 @@ # SEC-DEFAULT: Sections: # SEC-DEFAULT-NEXT: Idx Name Size Address Type # SEC-DEFAULT-NEXT: 0 00000000 0000000000000000 -# SEC-DEFAULT-NEXT: 1 .text 00000008 0000000000011000 TEXT DATA -# SEC-DEFAULT-NEXT: 2 .abcd 00000004 0000000000011008 TEXT DATA -# SEC-DEFAULT-NEXT: 3 .ad 00000004 000000000001100c TEXT DATA -# SEC-DEFAULT-NEXT: 4 .ag 00000004 0000000000011010 TEXT DATA +# SEC-DEFAULT-NEXT: 1 .text 00000008 0000000000000120 TEXT DATA +# SEC-DEFAULT-NEXT: 2 .abcd 00000004 0000000000000128 TEXT DATA +# SEC-DEFAULT-NEXT: 3 .ad 00000004 000000000000012c TEXT DATA +# SEC-DEFAULT-NEXT: 4 .ag 00000004 0000000000000130 TEXT DATA # SEC-DEFAULT-NEXT: 5 .symtab 00000030 0000000000000000 # SEC-DEFAULT-NEXT: 6 .shstrtab 0000002f 0000000000000000 # SEC-DEFAULT-NEXT: 7 .strtab 00000008 0000000000000000 @@ -34,9 +34,9 @@ # SEC-ALL: Sections: # SEC-ALL-NEXT: Idx Name Size Address Type # SEC-ALL-NEXT: 0 00000000 0000000000000000 -# SEC-ALL-NEXT: 1 .text 0000000c 0000000000011000 TEXT DATA -# SEC-ALL-NEXT: 2 .ad 00000004 000000000001100c TEXT DATA -# SEC-ALL-NEXT: 3 .ag 00000004 0000000000011010 TEXT DATA +# SEC-ALL-NEXT: 1 .text 0000000c 0000000000000120 TEXT DATA +# SEC-ALL-NEXT: 2 .ad 00000004 000000000000012c TEXT DATA +# SEC-ALL-NEXT: 3 .ag 00000004 0000000000000130 TEXT DATA # SEC-ALL-NEXT: 4 .symtab 00000030 0000000000000000 # SEC-ALL-NEXT: 5 .shstrtab 00000029 0000000000000000 # SEC-ALL-NEXT: 6 .strtab 00000008 0000000000000000 @@ -50,7 +50,7 @@ # SEC-NO: Sections: # SEC-NO-NEXT: Idx Name Size Address Type # SEC-NO-NEXT: 0 00000000 0000000000000000 -# SEC-NO-NEXT: 1 .text 00000014 0000000000011000 TEXT DATA +# SEC-NO-NEXT: 1 .text 00000014 0000000000000120 TEXT DATA # SEC-NO-NEXT: 2 .symtab 00000030 0000000000000000 # SEC-NO-NEXT: 3 .shstrtab 00000021 0000000000000000 # SEC-NO-NEXT: 4 .strtab 00000008 0000000000000000