This is an archive of the discontinued LLVM Phabricator instance.

[elf2] Assign output sections to PHDRs.
ClosedPublic

Authored by Bigcheese on Sep 8 2015, 7:06 PM.

Details

Reviewers
rafael
Summary

This is a minimal implementation to produce legal output. Future patches will combine multiple compatible PT_LOADs.

It uses the term PHDR instead of segment as not all PHDRs represent segments.

Diff Detail

Event Timeline

Bigcheese updated this revision to Diff 34290.Sep 8 2015, 7:06 PM
Bigcheese retitled this revision from to [elf2] Assign output sections to PHDRs..
Bigcheese updated this object.
Bigcheese added a reviewer: rafael.
Bigcheese added a subscriber: llvm-commits.
rafael edited edge metadata.Sep 9 2015, 7:28 AM
rafael added a subscriber: rafael.

+ static const uint64_t BaseAddress = 0x400000;

Any reason to change this in this patch? It can be done in another
patch if there is any advantage to it, no?

+ FileOff = RoundUpToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4);

That is not needed. The file header size is already a multiple of 8 or

  1. You can assert it if you want.

+ for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections)
+ if (outputSectionHasPHDR<ELFT>(Sec)) {
+ FileOff += sizeof(Elf_Phdr_Impl<ELFT>);

Use Elf_Phdr instead of Elf_Phdr_Impl

+ ++NumPhdrs;
+ }

Include {} around the for. Alternatively, write it as

for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections)
  if (outputSectionHasPHDR<ELFT>(Sec))
    ++NumPhdrs;

FileOff += NumPhdrs * sizeof(Elf_Phdr);

Even better, we can merge the loop with the following one since all
the program headers should fit in the first page (which is plenty of
space, specially once we create one per flag combination instead of
one per output section):

https://github.com/freebsd/freebsd/blob/a8fae65d69bf46d8a4aa6351092235ee691df503/sys/kern/imgact_elf.c#L674

+ FileSize = RoundUpToAlignment(FileOff, 8);

This can just be

FileSize = FileOff;

no? Why do we have to pad the end of the file?

An attached patch with most of these comments is attached.

rebased patch attached.

emaste added a subscriber: emaste.Sep 9 2015, 9:18 AM
Bigcheese updated this revision to Diff 34365.Sep 9 2015, 1:23 PM
Bigcheese edited edge metadata.
rafael accepted this revision.Sep 9 2015, 1:29 PM
rafael edited edge metadata.

LGTM

This revision is now accepted and ready to land.Sep 9 2015, 1:29 PM
Bigcheese closed this revision.Sep 9 2015, 1:51 PM

closed by r247185.