This is an archive of the discontinued LLVM Phabricator instance.

Align output segments correctly
ClosedPublic

Authored by ruiu on Sep 4 2019, 4:10 AM.

Details

Summary

Previously, segments were aligned according to their first section's
alignment requirements. That was not correct, but segments are also
aligned to a page boundary, and a page boundary is usually much larger
than a section alignment requirement, so no one noticed this bug before.

Now, lld has --nmagic option which sets maxPageSize to 1 to effectively
disable page alignment, which reveals the issue.

Fixes https://bugs.llvm.org/show_bug.cgi?id=43212

Diff Detail

Repository
rL LLVM

Event Timeline

ruiu created this revision.Sep 4 2019, 4:10 AM
Herald added a project: Restricted Project. · View Herald Transcript
MaskRay added inline comments.Sep 4 2019, 4:47 AM
lld/test/ELF/nmagic.s
4 ↗(On Diff #218640)

-unknown-linux can be deleted.

5 ↗(On Diff #218640)

Use -n for a nmagic test.. (-N is omagic which does more)

6 ↗(On Diff #218640)

-S or --section-headers. The single-dash form -long-option is deprecated in llvm binutils that intend to match GNU binutils.

MaskRay added inline comments.Sep 4 2019, 4:48 AM
lld/test/ELF/nmagic.s
22 ↗(On Diff #218640)

If I assemble this file as x86-64, .rodata happens to be aligned without the change. A larger alignment make more likely reveal problems if we have some layout changes.

peter.smith added inline comments.Sep 4 2019, 5:29 AM
lld/ELF/Writer.cpp
2276 ↗(On Diff #218640)

Just a thought, could this be reduced to os->ptLoad->p_align? I think config->maxPageSize would already be reflected in ptLoad->p_align. I'll have a check.

MaskRay added inline comments.Sep 4 2019, 5:39 AM
lld/ELF/Writer.cpp
2276 ↗(On Diff #218640)

It can't. p_align can be smaller than maxPageSize at this point.
I think this can be reduced if we move the code below

// setPhdrs
    if (p->p_type == PT_LOAD) {
      p->p_align = std::max<uint64_t>(p->p_align, config->maxPageSize);

to the constructor of PhdrEntry.

ruiu marked 4 inline comments as done.Sep 4 2019, 9:27 PM
ruiu added inline comments.
lld/test/ELF/nmagic.s
22 ↗(On Diff #218640)

That is true, but this file should suffice to reveal the bug, and it also captures the real usage, so I want to keep this as-is.

ruiu updated this revision to Diff 218832.Sep 4 2019, 9:27 PM
  • address review comments
MaskRay accepted this revision.Sep 4 2019, 9:56 PM
This revision is now accepted and ready to land.Sep 4 2019, 9:56 PM
MaskRay added inline comments.Sep 4 2019, 9:57 PM
lld/test/ELF/nmagic.s
22 ↗(On Diff #218640)

Since -Ttext 0 is used, no layout change can affect the addresses. I think keeping it as-is is fine.

This revision was automatically updated to reflect the committed changes.