Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -127,8 +127,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); @@ -139,6 +139,7 @@ int compareSections(StringRef A, StringRef B); void addScriptedSymbols(); bool hasPhdrsCommands(); + uintX_t getOutputSectionSize(StringRef Name); private: std::vector> @@ -150,14 +151,14 @@ // "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); void dispatchAssignment(SymbolAssignment *Cmd); + std::vector *> *OutputSections; uintX_t Dot; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -121,9 +121,10 @@ } template -std::vector *> -LinkerScript::createSections(OutputSectionFactory &Factory) { - std::vector *> Ret; +void LinkerScript::createSections( + std::vector *> *Out, + OutputSectionFactory &Factory) { + OutputSections = Out; // Add input section to output section. If there is no output section yet, // then create it and add to output section list. @@ -132,7 +133,7 @@ bool IsNew; std::tie(Sec, IsNew) = Factory.create(C, Name); if (IsNew) - Ret.push_back(Sec); + OutputSections->push_back(Sec); Sec->addSection(C); }; @@ -158,13 +159,11 @@ // 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) { @@ -175,10 +174,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; @@ -187,9 +186,8 @@ bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite); if ((RO && Writable) || (RW && !Writable)) - Sections.erase(It); + OutputSections->erase(It); } - return Sections; } template @@ -385,6 +383,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. @@ -812,6 +819,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 == "+="); @@ -920,6 +943,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: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ 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: test/ELF/linkerscript/linkerscript-sizeof.s =================================================================== --- test/ELF/linkerscript/linkerscript-sizeof.s +++ 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