Index: lld/trunk/ELF/LinkerScript.h =================================================================== --- lld/trunk/ELF/LinkerScript.h +++ lld/trunk/ELF/LinkerScript.h @@ -149,6 +149,7 @@ void addScriptedSymbols(); bool hasPhdrsCommands(); uintX_t getOutputSectionSize(StringRef Name); + uintX_t getSizeOfHeaders(); std::vector *> *OutputSections; Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -238,7 +238,7 @@ } // Assign addresses as instructed by linker script SECTIONS sub-commands. - Dot = Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); + Dot = getSizeOfHeaders(); uintX_t MinVA = std::numeric_limits::max(); uintX_t ThreadBssOffset = 0; @@ -430,6 +430,11 @@ return 0; } +template +typename ELFT::uint LinkerScript::getSizeOfHeaders() { + return Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); +} + // 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. @@ -915,6 +920,21 @@ } } +static uint64_t getSizeOfHeaders() { + switch (Config->EKind) { + case ELF32LEKind: + return Script::X->getSizeOfHeaders(); + case ELF32BEKind: + return Script::X->getSizeOfHeaders(); + case ELF64LEKind: + return Script::X->getSizeOfHeaders(); + case ELF64BEKind: + return Script::X->getSizeOfHeaders(); + default: + llvm_unreachable("unsupported target"); + } +} + SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { StringRef Op = next(); assert(Op == "=" || Op == "+="); @@ -1063,6 +1083,8 @@ expect(")"); return [=](uint64_t Dot) { return getSectionSize(Name); }; } + if (Tok == "SIZEOF_HEADERS") + return [=](uint64_t Dot) { return getSizeOfHeaders(); }; // Parse a symbol name or a number literal. uint64_t V = 0; Index: lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s =================================================================== --- lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s +++ lld/trunk/test/ELF/linkerscript/linkerscript-sizeofheaders.s @@ -0,0 +1,18 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo " SECTIONS { \ +# RUN: . = SIZEOF_HEADERS; \ +# RUN: _size = SIZEOF_HEADERS; \ +# RUN: .text : {*(.text.*)} \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -t %t1 | FileCheck %s + +#CHECK: SYMBOL TABLE: +#CHECK-NEXT: 0000000000000000 *UND* 00000000 +#CHECK-NEXT: 0000000000000120 .text 00000000 _start +#CHECK-NEXT: 0000000000000120 *ABS* 00000000 _size + +.global _start +_start: + nop