[PATCH] D43944: [ELF] - Refactoring of the elf/program headers allocation code.
George Rimar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 1 07:50:08 PST 2018
grimar created this revision.
grimar added reviewers: ruiu, espindola.
Herald added subscribers: arichardson, emaste.
I suggest this refactoring for code that allocates
elf/program headers.
I believe it is a bit easier to read.
https://reviews.llvm.org/D43944
Files:
ELF/LinkerScript.cpp
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -897,13 +897,27 @@
return nullptr;
}
-static uint64_t computeBase(uint64_t Min, bool AllocateHeaders) {
+static bool hasExplicitHeaders(ArrayRef<PhdrsCommand> Phdrs) {
+ return llvm::any_of(Phdrs, [](const PhdrsCommand &Cmd) {
+ return Cmd.HasPhdrs || Cmd.HasFilehdr;
+ });
+}
+
+static Optional<uint64_t> getHeadersVA(uint64_t MinVA, uint64_t HeaderSize,
+ ArrayRef<PhdrsCommand> Phdrs) {
+ // Unable to allocate headers when their size is greater than minimal VA.
+ if (HeaderSize > MinVA)
+ return None;
+
// If there is no SECTIONS or if the linkerscript is explicit about program
// headers, do our best to allocate them.
- if (!Script->HasSectionsCommand || AllocateHeaders)
- return 0;
+ if (!Script->HasSectionsCommand || hasExplicitHeaders(Phdrs))
+ return alignDown(MinVA - HeaderSize, Config->MaxPageSize);
+
// Otherwise only allocate program headers if that would not add a page.
- return alignDown(Min, Config->MaxPageSize);
+ if (HeaderSize <= MinVA - alignDown(MinVA, Config->MaxPageSize))
+ return alignDown(MinVA - HeaderSize, Config->MaxPageSize);
+ return None;
}
// Try to find an address for the file and program headers output sections,
@@ -927,24 +941,19 @@
Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
if (It == Phdrs.end())
return;
- PhdrEntry *FirstPTLoad = *It;
- bool HasExplicitHeaders =
- llvm::any_of(PhdrsCommands, [](const PhdrsCommand &Cmd) {
- return Cmd.HasPhdrs || Cmd.HasFilehdr;
- });
- uint64_t HeaderSize = getHeaderSize();
- if (HeaderSize <= Min - computeBase(Min, HasExplicitHeaders)) {
- Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
- Out::ElfHeader->Addr = Min;
- Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
+ if (Optional<uint64_t> VA =
+ getHeadersVA(Min, getHeaderSize(), PhdrsCommands)) {
+ Out::ElfHeader->Addr = *VA;
+ Out::ProgramHeaders->Addr = *VA + Out::ElfHeader->Size;
return;
}
// Error if we were explicitly asked to allocate headers.
- if (HasExplicitHeaders)
+ if (hasExplicitHeaders(PhdrsCommands))
error("could not allocate headers");
+ PhdrEntry *FirstPTLoad = *It;
Out::ElfHeader->PtLoad = nullptr;
Out::ProgramHeaders->PtLoad = nullptr;
FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43944.136525.patch
Type: text/x-patch
Size: 2528 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180301/b5f6b21a/attachment.bin>
More information about the llvm-commits
mailing list