Skip to content

Commit 5aedebf

Browse files
committedJul 5, 2017
[ELF] Extract allocateHeaders() from assignAddresses()
The allocateHeaders() function is called at the end of assignAddresses(), it decides whether the ELF header and program header table can be allocated to a PT_LOAD program header. As the function alters state, it prevents assignAddresses() from being called multiple times. This change splits out the call to allocateHeaders() from assignAddresses() this will permit assignAddresses() to be called while processing range extension thunks without trying to allocateHeaders(). Differential Revision: https://reviews.llvm.org/D34344 llvm-svn: 307131
1 parent 040c0f9 commit 5aedebf

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed
 

‎lld/ELF/LinkerScript.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,14 @@ void LinkerScript::processNonSectionCommands() {
783783
}
784784
}
785785

786-
static void
787-
allocateHeaders(std::vector<PhdrEntry> &Phdrs,
788-
ArrayRef<OutputSectionCommand *> OutputSectionCommands,
789-
uint64_t Min) {
786+
void LinkerScript::allocateHeaders(std::vector<PhdrEntry> &Phdrs) {
787+
uint64_t Min = std::numeric_limits<uint64_t>::max();
788+
for (OutputSectionCommand *Cmd : OutputSectionCommands) {
789+
OutputSection *Sec = Cmd->Sec;
790+
if (Sec->Flags & SHF_ALLOC)
791+
Min = std::min<uint64_t>(Min, Sec->Addr);
792+
}
793+
790794
auto FirstPTLoad = llvm::find_if(
791795
Phdrs, [](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
792796
if (FirstPTLoad == Phdrs.end())
@@ -826,7 +830,7 @@ allocateHeaders(std::vector<PhdrEntry> &Phdrs,
826830
Phdrs.erase(PhdrI);
827831
}
828832

829-
void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
833+
void LinkerScript::assignAddresses() {
830834
// Assign addresses as instructed by linker script SECTIONS sub-commands.
831835
Dot = 0;
832836
ErrorOnMissingSection = true;
@@ -846,15 +850,6 @@ void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
846850
auto *Cmd = cast<OutputSectionCommand>(Base);
847851
assignOffsets(Cmd);
848852
}
849-
850-
uint64_t MinVA = std::numeric_limits<uint64_t>::max();
851-
for (OutputSectionCommand *Cmd : OutputSectionCommands) {
852-
OutputSection *Sec = Cmd->Sec;
853-
if (Sec->Flags & SHF_ALLOC)
854-
MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
855-
}
856-
857-
allocateHeaders(Phdrs, OutputSectionCommands, MinVA);
858853
}
859854

860855
// Creates program headers as instructed by PHDRS linker script command.

‎lld/ELF/LinkerScript.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ class LinkerScript final {
281281
void assignOffsets(OutputSectionCommand *Cmd);
282282
void createOrphanCommands();
283283
void processNonSectionCommands();
284-
void assignAddresses(std::vector<PhdrEntry> &Phdrs);
285-
284+
void assignAddresses();
285+
void allocateHeaders(std::vector<PhdrEntry> &Phdrs);
286286
void addSymbol(SymbolAssignment *Cmd);
287287
void processCommands(OutputSectionFactory &Factory);
288288

‎lld/ELF/Writer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ template <class ELFT> void Writer<ELFT>::run() {
217217
OutputSectionCommands.begin(), OutputSectionCommands.end(),
218218
[](OutputSectionCommand *Cmd) { Cmd->maybeCompress<ELFT>(); });
219219

220-
Script->assignAddresses(Phdrs);
220+
Script->assignAddresses();
221+
Script->allocateHeaders(Phdrs);
221222

222223
// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
223224
// 0 sized region. This has to be done late since only after assignAddresses

0 commit comments

Comments
 (0)
Please sign in to comment.