[PATCH] D34956: [ELF] - Store pointer to PT_LOAD instead of pointer to first section (FirstInPtLoad) in OutputSection

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 21 17:46:33 PDT 2017


phosek added inline comments.


================
Comment at: ELF/LinkerScript.cpp:794-795
+
 static void
-allocateHeaders(std::vector<PhdrEntry> &Phdrs,
+allocateHeaders(std::vector<PhdrEntry *> &Phdrs,
                 ArrayRef<OutputSectionCommand *> OutputSectionCommands,
----------------
grimar wrote:
> ruiu wrote:
> > grimar wrote:
> > > ruiu wrote:
> > > > This change seems fine, but I realized that I do not understand what this function is supposed to do. What is this for?
> > > Function checks if elf header and program header can be allocated. 
> > > If there is no room for them, PT_PHDR should not be emited.
> > > 
> > > See:
> > > `PT_PHDR` description:
> > > "Specifies the location and size of the program header table itself, both in the file and in the memory image of the program. This segment type cannot occur more than once in a file. **Moreover, it can occur only if the program header table is part of the memory image of the program**." (https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-83432/index.html)
> > > 
> > > Imagine we have script like:
> > > `SECTIONS {foo 0 : {*(foo*)} }` (took it from no-space.s testcase)
> > > 
> > > In that case there is no VA space for headers. That means we do not allocate them in PT_LOAD. Output will be:
> > > 
> > > ```
> > > Program Headers:
> > > Type  Offset   VirtAddr           PhysAddr
> > > LOAD  0x001000 0x0000000000000000 0x0000000000000000
> > > ```
> > There's no rule or something that says a program header must be at beginning of a file, right? If there's no space at beginning of a file for a program header, why don't you put it at end of the file?
> But why would we want to do that ? Neither bfd or gold allocate program headers in this case I think, why should we ?
> 
> Spec says about `PT_PHDR`: "This type, if present, must precede any loadable segment entry." (https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-83432/index.html).
> In that case we would need probably to place program headers at the end of last segment and move PT_PHDR before it. Additional complication for no visible reason probably.
> 
> Also I think if script is writen in the way when there is no room for headers it may assume them are not allocated.
> Script may include asserts of location counter or something that may be broken after this change.
> 
> And the last - there is still ELF file header there which must be placed at the begining of the file and we can't move it anyways.
> 
> Basing on all above I believe moving of program headers at least does not make sense (or even may be harmfull).
> 
> Just in case, this patch does not change any logic, only simplifies the existent code.
We actually ran into this in our kernel: we're using a custom linker script to avoid emitting ELF file or program headers because we don't want them in the kernel image. Since the kernel doesn't start at the beginning of the file, there's always a space so LLD always emits those headers unlike BFD ld or gold.

I have a WIP change that avoids emitting the headers if there's no output section covering them which is the behavior BFD ld and gold use.


https://reviews.llvm.org/D34956





More information about the llvm-commits mailing list