[lld-macho] Ensure segments are laid out contiguously
authorJez Ng <jezng@fb.com>
Tue, 20 Apr 2021 20:58:07 +0000 (16:58 -0400)
committerJez Ng <jezng@fb.com>
Tue, 20 Apr 2021 20:58:57 +0000 (16:58 -0400)
commitbb62ef9943008281a2223c942c71e67b3902a07e
tree4d6ea5b60ac78693039eb8690e8d9f1124bb6506
parent1aa29dffceff63c7f2bdecc03ec9c0922a100082
[lld-macho] Ensure segments are laid out contiguously

codesign/libstuff checks that the `__LLVM` segment is directly
before `__LINKEDIT` by checking that `fileOff + fileSize == next segment
fileOff`. Previously, there would be gaps between the segments due to
the fact that their fileOffs are page-aligned but their fileSizes
aren't. In order to satisfy codesign, we page-align fileOff *before*
calculating fileSize. (I don't think codesign checks for the relative
ordering of other segments, so in theory we could do this just for
`__LLVM`, but ld64 seems to do it for all segments.)

Note that we *don't* round up the fileSize of the `__LINKEDIT` segment.
Since it's the last segment, it doesn't need to worry about contiguity;
in addition, codesign checks that the last (hidden) section in
`__LINKEDIT` covers the last byte of the segment, so if we rounded up
`__LINKEDIT`'s size we would have to do the same for its last section,
which is a bother.

While at it, I also addressed a FIXME in the linkedit-contiguity.s test
to cover more `__LINKEDIT` sections.

Reviewed By: #lld-macho, thakis, alexshap

Differential Revision: https://reviews.llvm.org/D100848
lld/MachO/OutputSegment.h
lld/MachO/Writer.cpp
lld/test/MachO/bitcode-bundle.ll
lld/test/MachO/bss.s
lld/test/MachO/linkedit-contiguity.s
lld/test/MachO/section-headers.s
lld/test/MachO/segments.s