[lld] r345154 - Refactor assignFileOffsets. NFC.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 24 08:47:46 PDT 2018
Author: ruiu
Date: Wed Oct 24 08:47:46 2018
New Revision: 345154
URL: http://llvm.org/viewvc/llvm-project?rev=345154&view=rev
Log:
Refactor assignFileOffsets. NFC.
Modified:
lld/trunk/ELF/Writer.cpp
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=345154&r1=345153&r2=345154&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Oct 24 08:47:46 2018
@@ -2020,49 +2020,48 @@ template <class ELFT> void Writer<ELFT>:
}
}
-// Adjusts the file alignment for a given output section and returns
-// its new file offset. The file offset must be the same with its
-// virtual address (modulo the page size) so that the loader can load
-// executables without any address adjustment.
-static uint64_t getFileAlignment(uint64_t Off, OutputSection *Cmd) {
- OutputSection *First = Cmd->PtLoad ? Cmd->PtLoad->FirstSec : nullptr;
- // The first section in a PT_LOAD has to have congruent offset and address
- // module the page size.
- if (Cmd == First)
- return alignTo(Off, std::max<uint64_t>(Cmd->Alignment, Config->MaxPageSize),
- Cmd->Addr);
-
- // For SHT_NOBITS we don't want the alignment of the section to impact the
- // offset of the sections that follow. Since nothing seems to care about the
- // sh_offset of the SHT_NOBITS section itself, just ignore it.
- if (Cmd->Type == SHT_NOBITS)
+// Compute an in-file position for a given section. The file offset must be the
+// same with its virtual address modulo the page size, so that the loader can
+// load executables without any address adjustment.
+static uint64_t computeFileOffset(OutputSection *OS, uint64_t Off) {
+ // File offsets are not significant for .bss sections. By convention, we keep
+ // section offsets monotonically increasing rather than setting to zero.
+ if (OS->Type == SHT_NOBITS)
return Off;
// If the section is not in a PT_LOAD, we just have to align it.
- if (!Cmd->PtLoad)
- return alignTo(Off, Cmd->Alignment);
+ if (!OS->PtLoad)
+ return alignTo(Off, OS->Alignment);
+
+ // The first section in a PT_LOAD has to have congruent offset and address
+ // module the page size.
+ OutputSection *First = OS->PtLoad->FirstSec;
+ if (OS == First) {
+ uint64_t Alignment = std::max<uint64_t>(OS->Alignment, Config->MaxPageSize);
+ return alignTo(Off, Alignment, OS->Addr);
+ }
// If two sections share the same PT_LOAD the file offset is calculated
// using this formula: Off2 = Off1 + (VA2 - VA1).
- return First->Offset + Cmd->Addr - First->Addr;
+ return First->Offset + OS->Addr - First->Addr;
}
-static uint64_t setOffset(OutputSection *Cmd, uint64_t Off) {
- Off = getFileAlignment(Off, Cmd);
- Cmd->Offset = Off;
+// Set an in-file position to a given section and returns the end position of
+// the section.
+static uint64_t setFileOffset(OutputSection *OS, uint64_t Off) {
+ Off = computeFileOffset(OS, Off);
+ OS->Offset = Off;
- // For SHT_NOBITS we should not count the size.
- if (Cmd->Type == SHT_NOBITS)
+ if (OS->Type == SHT_NOBITS)
return Off;
-
- return Off + Cmd->Size;
+ return Off + OS->Size;
}
template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
uint64_t Off = 0;
for (OutputSection *Sec : OutputSections)
if (Sec->Flags & SHF_ALLOC)
- Off = setOffset(Sec, Off);
+ Off = setFileOffset(Sec, Off);
FileSize = alignTo(Off, Config->Wordsize);
}
@@ -2073,8 +2072,8 @@ static std::string rangeToString(uint64_
// Assign file offsets to output sections.
template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
uint64_t Off = 0;
- Off = setOffset(Out::ElfHeader, Off);
- Off = setOffset(Out::ProgramHeaders, Off);
+ Off = setFileOffset(Out::ElfHeader, Off);
+ Off = setFileOffset(Out::ProgramHeaders, Off);
PhdrEntry *LastRX = nullptr;
for (PhdrEntry *P : Phdrs)
@@ -2082,7 +2081,7 @@ template <class ELFT> void Writer<ELFT>:
LastRX = P;
for (OutputSection *Sec : OutputSections) {
- Off = setOffset(Sec, Off);
+ Off = setFileOffset(Sec, Off);
if (Script->HasSectionsCommand)
continue;
// If this is a last section of the last executable segment and that
More information about the llvm-commits
mailing list