diff options
author | Eugene Leviant <evgeny.leviant@gmail.com> | 2016-09-29 09:20:33 +0000 |
---|---|---|
committer | Eugene Leviant <evgeny.leviant@gmail.com> | 2016-09-29 09:20:33 +0000 |
commit | 3d9abec09159db1fdd208cb750f7bf60ae94325f (patch) | |
tree | 1dac2a548846c4a75a5ee41dfd7e90346ed9291d | |
parent | 71e9e44ae97bcb72e0020961d8e10b57d05d57d4 (diff) | |
download | bcm5719-llvm-3d9abec09159db1fdd208cb750f7bf60ae94325f.tar.gz bcm5719-llvm-3d9abec09159db1fdd208cb750f7bf60ae94325f.zip |
[ELF] Change the way we compute file offsets
If we two sections reside in the same PT_LOAD segment,
we compute second section using the following formula:
Off2 = Off1 + VA2 - VA1. This allows OS kernel allocating
sections correctly when loading an image.
Differential revision: https://reviews.llvm.org/D25014
llvm-svn: 282705
-rw-r--r-- | lld/ELF/OutputSections.h | 9 | ||||
-rw-r--r-- | lld/ELF/Writer.cpp | 10 | ||||
-rw-r--r-- | lld/test/ELF/linkerscript/outsections-addr.s | 4 |
3 files changed, 20 insertions, 3 deletions
diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index fedda160f70..5dd6c46c25b 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -77,6 +77,7 @@ public: void setVA(uintX_t VA) { Header.sh_addr = VA; } uintX_t getVA() const { return Header.sh_addr; } void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } + uintX_t getFileOffset() { return Header.sh_offset; } void setSHName(unsigned Val) { Header.sh_name = Val; } void writeHeaderTo(Elf_Shdr *SHdr); StringRef getName() { return Name; } @@ -108,6 +109,14 @@ public: // Typically the first section of each PT_LOAD segment has this flag. bool PageAlign = false; + // Pointer to the first section in PT_LOAD segment, which this section + // also resides in. This field is used to correctly compute file offset + // of a section. When two sections share the same load segment, difference + // between their file offsets should be equal to difference between their + // virtual addresses. To compute some section offset we use the following + // formula: Off = Off_first + VA - VA_first. + OutputSectionBase<ELFT> *FirstInPtLoad = nullptr; + virtual void finalize() {} virtual void finalizePieces() {} virtual void assignOffsets() {} diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 71e9ba2fe77..9ac5135332d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -542,6 +542,8 @@ void PhdrEntry<ELFT>::add(OutputSectionBase<ELFT> *Sec) { if (!First) First = Sec; H.p_align = std::max<typename ELFT::uint>(H.p_align, Sec->getAlignment()); + if (H.p_type == PT_LOAD && !Config->OFormatBinary) + Sec->FirstInPtLoad = First; } template <class ELFT> @@ -1190,7 +1192,13 @@ static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase<ELFT> *Sec) { // and does not need any other offset adjusting. if (Config->Relocatable || !(Sec->getFlags() & SHF_ALLOC)) return Off; - return alignTo(Off, Config->MaxPageSize, Sec->getVA()); + + OutputSectionBase<ELFT> *First = Sec->FirstInPtLoad; + // If two sections share the same PT_LOAD the file offset is calculated using + // this formula: Off2 = Off1 + (VA2 - VA1). + if (!First || Sec == First) + return alignTo(Off, Target->MaxPageSize, Sec->getVA()); + return First->getFileOffset() + Sec->getVA() - First->getVA(); } template <class ELFT, class uintX_t> diff --git a/lld/test/ELF/linkerscript/outsections-addr.s b/lld/test/ELF/linkerscript/outsections-addr.s index 100eba7542e..003b2771c33 100644 --- a/lld/test/ELF/linkerscript/outsections-addr.s +++ b/lld/test/ELF/linkerscript/outsections-addr.s @@ -81,7 +81,7 @@ #CHECK: SHF_ALLOC #CHECK: ] #CHECK: Address: 0x4008 -#CHECK: Offset: 0x2008 +#CHECK: Offset: 0x3008 #CHECK: Size: 8 #CHECK: Link: 0 #CHECK: Info: 0 @@ -96,7 +96,7 @@ #CHECK: SHF_ALLOC #CHECK: ] #CHECK: Address: 0x5010 -#CHECK: Offset: 0x2010 +#CHECK: Offset: 0x4010 #CHECK: Size: 8 #CHECK: Link: 0 #CHECK: Info: 0 |