[ELF] - Linkerscript: implement SIZEOF_HEADERS.

SIZEOF_HEADERS - Return the size in bytes of the output file’s headers.

It is is a feature used in FreeBsd script, for example.
There is a discussion on PR28688 page about it.

Differential revision: https://reviews.llvm.org/D23165

llvm-svn: 278204
This commit is contained in:
George Rimar 2016-08-10 07:59:34 +00:00
parent 0215e76836
commit e32a3598bc
3 changed files with 42 additions and 1 deletions

View File

@ -238,7 +238,7 @@ template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
}
// Assign addresses as instructed by linker script SECTIONS sub-commands.
Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Dot = getSizeOfHeaders();
uintX_t MinVA = std::numeric_limits<uintX_t>::max();
uintX_t ThreadBssOffset = 0;
@ -430,6 +430,11 @@ typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
return 0;
}
template <class ELFT>
typename ELFT::uint LinkerScript<ELFT>::getSizeOfHeaders() {
return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::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 getSectionSize(StringRef Name) {
}
}
static uint64_t getSizeOfHeaders() {
switch (Config->EKind) {
case ELF32LEKind:
return Script<ELF32LE>::X->getSizeOfHeaders();
case ELF32BEKind:
return Script<ELF32BE>::X->getSizeOfHeaders();
case ELF64LEKind:
return Script<ELF64LE>::X->getSizeOfHeaders();
case ELF64BEKind:
return Script<ELF64BE>::X->getSizeOfHeaders();
default:
llvm_unreachable("unsupported target");
}
}
SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
StringRef Op = next();
assert(Op == "=" || Op == "+=");
@ -1063,6 +1083,8 @@ Expr ScriptParser::readPrimary() {
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;

View File

@ -149,6 +149,7 @@ public:
void addScriptedSymbols();
bool hasPhdrsCommands();
uintX_t getOutputSectionSize(StringRef Name);
uintX_t getSizeOfHeaders();
std::vector<OutputSectionBase<ELFT> *> *OutputSections;

View File

@ -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