Index: lld/trunk/ELF/LinkerScript.h =================================================================== --- lld/trunk/ELF/LinkerScript.h +++ lld/trunk/ELF/LinkerScript.h @@ -128,8 +128,8 @@ typedef typename ELFT::uint uintX_t; public: - std::vector *> - createSections(OutputSectionFactory &Factory); + void createSections(std::vector *> *Out, + OutputSectionFactory &Factory); std::vector> createPhdrs(ArrayRef *> S); @@ -140,6 +140,7 @@ int compareSections(StringRef A, StringRef B); void addScriptedSymbols(); bool hasPhdrsCommands(); + uintX_t getOutputSectionSize(StringRef Name); private: std::vector> @@ -151,13 +152,13 @@ // "ScriptConfig" is a bit too long, so define a short name for it. ScriptConfiguration &Opt = *ScriptConfig; - std::vector *> - filter(std::vector *> &Sections); + void filter(); int getSectionIndex(StringRef Name); std::vector getPhdrIndices(StringRef SectionName); size_t getPhdrIndex(StringRef PhdrName); + std::vector *> *OutputSections; uintX_t Dot; }; Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -138,9 +138,10 @@ } template -std::vector *> -LinkerScript::createSections(OutputSectionFactory &Factory) { - std::vector *> Ret; +void LinkerScript::createSections( + std::vector *> *Out, + OutputSectionFactory &Factory) { + OutputSections = Out; for (auto &P : getSectionMap()) { std::vector *> Sections; @@ -157,7 +158,7 @@ if (I->Sort) std::stable_sort(Sections.begin(), Sections.end(), compareByName); for (InputSectionBase *S : Sections) - addSection(Factory, Ret, S, OutputName); + addSection(Factory, *Out, S, OutputName); } // Add all other input sections, which are not listed in script. @@ -165,17 +166,15 @@ Symtab::X->getObjectFiles()) for (InputSectionBase *S : F->getSections()) if (!isDiscarded(S) && !S->OutSec) - addSection(Factory, Ret, S, getOutputSectionName(S)); + addSection(Factory, *Out, S, getOutputSectionName(S)); // Remove from the output all the sections which did not meet // the optional constraints. - return filter(Ret); + filter(); } // Process ONLY_IF_RO and ONLY_IF_RW. -template -std::vector *> -LinkerScript::filter(std::vector *> &Sections) { +template void LinkerScript::filter() { // In this loop, we remove output sections if they don't satisfy // requested properties. for (const std::unique_ptr &Base : Opt.Commands) { @@ -186,10 +185,10 @@ if (Cmd->Constraint == ConstraintKind::NoConstraint) continue; - auto It = llvm::find_if(Sections, [&](OutputSectionBase *S) { + auto It = llvm::find_if(*OutputSections, [&](OutputSectionBase *S) { return S->getName() == Cmd->Name; }); - if (It == Sections.end()) + if (It == OutputSections->end()) continue; OutputSectionBase *Sec = *It; @@ -198,9 +197,8 @@ bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite); if ((RO && Writable) || (RW && !Writable)) - Sections.erase(It); + OutputSections->erase(It); } - return Sections; } template @@ -396,6 +394,15 @@ return !Opt.PhdrsCommands.empty(); } +template +typename ELFT::uint LinkerScript::getOutputSectionSize(StringRef Name) { + for (OutputSectionBase *Sec : *OutputSections) + if (Sec->getName() == Name) + return Sec->getSize(); + error("undefined section " + Name); + return 0; +} + // Returns indices of ELF headers containing specific section, identified // by Name. Each index is a zero based number of ELF header listed within // PHDRS {} script block. @@ -838,6 +845,22 @@ return 0; } +static uint64_t getSectionSize(StringRef Name) { + switch (Config->EKind) { + case ELF32LEKind: + return Script::X->getOutputSectionSize(Name); + case ELF32BEKind: + return Script::X->getOutputSectionSize(Name); + case ELF64LEKind: + return Script::X->getOutputSectionSize(Name); + case ELF64BEKind: + return Script::X->getOutputSectionSize(Name); + default: + llvm_unreachable("unsupported target"); + } + return 0; +} + SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { StringRef Op = next(); assert(Op == "=" || Op == "+="); @@ -946,6 +969,12 @@ expect(")"); return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; } + if (Tok == "SIZEOF") { + expect("("); + StringRef Name = next(); + expect(")"); + return [=](uint64_t Dot) { return getSectionSize(Name); }; + } // Parse a symbol name or a number literal. uint64_t V = 0; Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -48,7 +48,7 @@ void copyLocalSymbols(); void addReservedSymbols(); - std::vector *> createSections(); + void createSections(); void forEachRelSec( std::function &, const typename ELFT::Shdr &)> Fn); @@ -233,9 +233,11 @@ CommonInputSection Common(getCommonSymbols()); CommonInputSection::X = &Common; - OutputSections = ScriptConfig->HasContents - ? Script::X->createSections(Factory) - : createSections(); + if (ScriptConfig->HasContents) + Script::X->createSections(&OutputSections, Factory); + else + createSections(); + finalizeSections(); if (HasError) return; @@ -635,10 +637,7 @@ } } -template -std::vector *> Writer::createSections() { - std::vector *> Result; - +template void Writer::createSections() { for (const std::unique_ptr> &F : Symtab.getObjectFiles()) { for (InputSectionBase *C : F->getSections()) { @@ -650,11 +649,10 @@ bool IsNew; std::tie(Sec, IsNew) = Factory.create(C, getOutputSectionName(C)); if (IsNew) - Result.push_back(Sec); + OutputSections.push_back(Sec); Sec->addSection(C); } } - return Result; } // Create output section objects and add them to OutputSections. Index: lld/trunk/test/ELF/linkerscript/linkerscript-sizeof.s =================================================================== --- lld/trunk/test/ELF/linkerscript/linkerscript-sizeof.s +++ lld/trunk/test/ELF/linkerscript/linkerscript-sizeof.s @@ -0,0 +1,53 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { \ +# RUN: .aaa : { *(.aaa) } \ +# RUN: .bbb : { *(.bbb) } \ +# RUN: .ccc : { *(.ccc) } \ +# RUN: _aaa = SIZEOF(.aaa); \ +# RUN: _bbb = SIZEOF(.bbb); \ +# RUN: _ccc = SIZEOF(.ccc); \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -t -section-headers %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address Type +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .aaa 00000008 0000000000000120 DATA +# CHECK-NEXT: 2 .bbb 00000010 0000000000000128 DATA +# CHECK-NEXT: 3 .ccc 00000018 0000000000000138 DATA +# CHECK: SYMBOL TABLE: +# CHECK-NEXT: 0000000000000000 *UND* 00000000 +# CHECK-NEXT: 0000000000000150 .text 00000000 _start +# CHECK-NEXT: 0000000000000008 *ABS* 00000000 _aaa +# CHECK-NEXT: 0000000000000010 *ABS* 00000000 _bbb +# CHECK-NEXT: 0000000000000018 *ABS* 00000000 _ccc + +## Check that we error out if trying to get size of +## section that does not exist. +# RUN: echo "SECTIONS { \ +# RUN: .aaa : { *(.aaa) } \ +# RUN: .bbb : { *(.bbb) } \ +# RUN: .ccc : { *(.ccc) } \ +# RUN: _aaa = SIZEOF(.foo); \ +# RUN: }" > %t.script +# RUN: not ld.lld -o %t1 --script %t.script %t 2>&1 \ +# RUN: | FileCheck -check-prefix=ERR %s +# ERR: undefined section .foo + +.global _start +_start: + nop + +.section .aaa,"a" + .quad 0 + +.section .bbb,"a" + .quad 0 + .quad 0 + +.section .ccc,"a" + .quad 0 + .quad 0 + .quad 0