This is an archive of the discontinued LLVM Phabricator instance.

[ELF] - Align sections file offsets correctly.
ClosedPublic

Authored by grimar on Apr 22 2016, 9:16 AM.

Details

Summary

System V ABI 4.1 specifies that program header's p_vaddr should equal p_offset, modulo p_align.
(https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-83432/index.html).
I was able to violate this using the linkerscript.
Patch fixes the issue.

Diff Detail

Repository
rL LLVM

Event Timeline

grimar updated this revision to Diff 54667.Apr 22 2016, 9:16 AM
grimar retitled this revision from to [ELF] - Assign correct value to Phdr's p_offset field..
grimar updated this object.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar.
ruiu added inline comments.Apr 22 2016, 1:42 PM
ELF/Writer.cpp
1641–1644 ↗(On Diff #54667)
// 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.
1646 ↗(On Diff #54667)

Rename this getFileAlignment.

1674–1676 ↗(On Diff #54667)

You want to move these three lines of code to the new function because it handles alignment as well. Then you can remove Align parameter from the function.

grimar updated this revision to Diff 54824.Apr 25 2016, 1:41 AM
grimar retitled this revision from [ELF] - Assign correct value to Phdr's p_offset field. to [ELF] - Align sections file offsets correctly..
grimar marked 3 inline comments as done.
grimar added inline comments.
ELF/Writer.cpp
1646 ↗(On Diff #54667)

Done.

1658 ↗(On Diff #54667)

Added Mask variable to simplify.

1674–1676 ↗(On Diff #54667)

Done.

ruiu added inline comments.Apr 25 2016, 9:28 AM
ELF/Writer.cpp
1672–1679 ↗(On Diff #54824)

Can this be

return alignTo(Off, PageSize, Sec->getVA())?
ruiu added inline comments.Apr 25 2016, 9:31 AM
ELF/Writer.cpp
1667–1670 ↗(On Diff #54824)

For relocatable output, do we even have to adjust file offset?

grimar updated this revision to Diff 54992.Apr 26 2016, 4:27 AM
  • Addressed review comments.
grimar added inline comments.Apr 26 2016, 4:28 AM
ELF/Writer.cpp
1667–1670 ↗(On Diff #54824)

Did not find anywhere that we should not do that for -r. But gold and bfd both do that, so I guess yes,
probably there is no special rules for relocation outputs.

1672–1679 ↗(On Diff #54824)

I think yes.

ruiu accepted this revision.Apr 26 2016, 2:05 PM
ruiu edited edge metadata.

LGTM

ELF/Writer.cpp
1687–1694 ↗(On Diff #54992)

Nice. I knew alignTo accepts the third argument, but this is the first time I see that is useful.

This revision is now accepted and ready to land.Apr 26 2016, 2:05 PM
grimar added inline comments.Apr 27 2016, 1:25 AM
ELF/Writer.cpp
1687–1694 ↗(On Diff #54992)

Yeah, I also saw it, but it was completely not obvious for me that it can be replacement for what was wrote before.

This revision was automatically updated to reflect the committed changes.