Avoid duplicated code.

This also fixes the linker script accounting for the ELF header in
some places but not in others.

llvm-svn: 282173
This commit is contained in:
Rafael Espindola 2016-09-22 16:47:21 +00:00
parent 7ffdb1556b
commit 0d4b6d5c98
4 changed files with 13 additions and 8 deletions

View File

@ -587,8 +587,7 @@ template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
Sec->setVA(0);
}
uintX_t HeaderSize =
Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
uintX_t HeaderSize = getHeaderSize();
if (HeaderSize > MinVA)
fatal("Not enough space for ELF and program headers");
@ -723,7 +722,7 @@ uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
}
template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
return elf::getHeaderSize<ELFT>();
}
template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {

View File

@ -812,6 +812,12 @@ private:
llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
};
template <class ELFT> uint64_t getHeaderSize() {
if (Config->OFormatBinary)
return 0;
return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
}
template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;

View File

@ -1148,11 +1148,7 @@ template <class ELFT> void Writer<ELFT>::fixHeaders() {
// Assign VAs (addresses at run-time) to output sections.
template <class ELFT> void Writer<ELFT>::assignAddresses() {
uintX_t VA = Config->ImageBase;
if (!Config->OFormatBinary)
VA +=
Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
uintX_t VA = Config->ImageBase + getHeaderSize<ELFT>();
uintX_t ThreadBssOffset = 0;
for (OutputSectionBase<ELFT> *Sec : OutputSections) {
uintX_t Alignment = Sec->getAlignment();

View File

@ -11,6 +11,10 @@
# RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary
# RUN: od -t x1 -v %t2.out | FileCheck %s
# RUN: echo "SECTIONS { }" > %t.script
# RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary
# RUN: od -t x1 -v %t2.out | FileCheck %s
# RUN: not ld.lld -o %t3.out %t --oformat foo 2>&1 \
# RUN: | FileCheck %s --check-prefix ERR
# ERR: unknown --oformat value: foo