llvm-project/lld/test/mach-o
Rui Ueyama 7f8ca6e367 lld: Fix initial Mach-O load commands size calculation omitting LC_FUNCTION_STARTS
Patch by Nicholas Allegra.

The Mach-O writer calculates the size of load commands multiple times.

First, Util::assignAddressesToSections() (in MachONormalizedFileFromAtoms.cpp)
calculates the size using headerAndLoadCommandsSize() (in
MachONormalizedFileBinaryWriter.cpp), which creates a temporary
MachOFileLayout for the NormalizedFile, only to retrieve its
headerAndLoadCommandsSize.  Later, writeBinary() (in
MachONormalizedFileBinaryWriter.cpp) creates a new layout and uses the offsets
from that layout to actually write out everything in the NormalizedFile.

But the NormalizedFile changes between the first computation and the second.
When Util::assignAddressesToSections is called, file.functionStarts is always
empty because Util::addFunctionStarts has not yet been called. Yet
MachOFileLayout decides whether to include a LC_FUNCTION_STARTS command based
on whether file.functionStarts is nonempty. Therefore, the initial computation
always omits it.

Because padding for the __TEXT segment (to make its size a multiple of the
page size) is added between the load commands and the first section, LLD still
generates a valid binary as long as the amount of padding happens to be large
enough to fit LC_FUNCTION_STARTS command, which it usually is.

However, it's easy to reproduce the issue by adding a section of a precise
size. Given foo.c:

  __attribute__((section("__TEXT,__foo")))
  char foo[0xd78] = {0};

Run:

  clang -dynamiclib -o foo.dylib foo.c -fuse-ld=lld -install_name
  /usr/lib/foo.dylib
  otool -lvv foo.dylib

This should produce:

  truncated or malformed object (offset field of section 1 in LC_SEGMENT_64
  command 0 not past the headers of the file)

This commit:

 - Changes MachOFileLayout to always assume LC_FUNCTION_STARTS is present for
   the initial computation, as long as generating LC_FUNCTION_STARTS is
   enabled. It would be slightly better to check whether there are actually
   any functions, since no LC_FUNCTION_STARTS will be generated if not, but it
   doesn't cause a problem if the initial computation is too high.

 - Adds a test.

 - Adds an assert in MachOFileLayout::writeSectionContent() that we are not
   writing section content into the load commands region (which would happen
   if the offset was calculated too low due to the initial load commands size
   calculation being too low).  Adds an assert in
   MachOFileLayout::writeLoadCommands to validate a similar situation where
   two size-of-load-commands computations are expected to be equivalent.

llvm-svn: 358545
2019-04-17 01:47:16 +00:00
..
Inputs Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
PIE.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
align_text.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm-interworking-movw.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm-interworking.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm-shims.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm-subsections-via-symbols.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm64-reloc-negDelta32-fixup.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm64-relocs-errors-delta64-offset.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
arm64-section-order.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
bind-opcodes.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
cstring-sections.yaml Make YAML quote forward slashes. 2018-10-12 16:31:20 +00:00
data-in-code-load-command.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
data-only-dylib.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
dead-strip-globals.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
debug-syms.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
demangle.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
dependency_info.yaml [lit, python] Always add quotes around the python path in lit 2018-08-06 22:37:49 +00:00
do-not-emit-unwind-fde-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
dso_handle.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
dylib-install-names.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
eh-frame-relocs-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
error-simulator-vs-macosx.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
exe-offsets.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
exe-segment-overlap.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
executable-exports.yaml [MachO] Add -macho to llvm-objdump commands 2019-04-16 03:51:53 +00:00
export-trie-order.yaml [MachO] Add -macho to llvm-objdump commands 2019-04-16 03:51:53 +00:00
exported_symbols_list-dylib.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
exported_symbols_list-obj.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
exported_symbols_list-undef.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
fat-archive.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
filelist.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
flat_namespace_undef_error.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
flat_namespace_undef_suppress.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
force_load-dylib.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
force_load-x86_64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
framework-user-paths.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
function-starts-load-command.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
gcc_except_tab-got-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
got-order.yaml [MachO] Add -macho to llvm-objdump commands 2019-04-16 03:51:53 +00:00
hello-world-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
hello-world-armv6.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
hello-world-armv7.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
hello-world-x86.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
hello-world-x86_64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
image-base.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
infer-arch.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
interposing-section.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
keep_private_externs.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
lazy-bind-x86_64.yaml [MachO] Add -macho to llvm-objdump commands 2019-04-16 03:51:53 +00:00
lc_segment_filesize.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
lib-search-paths.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
library-order.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
library-rescan.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
libresolve-bizarre-root-override.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
libresolve-multiple-syslibroots.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
libresolve-one-syslibroot.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
libresolve-simple.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
libresolve-user-paths.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
libresolve-z.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
linker-as-ld.yaml [CMake] Fix linker-as-ld to symlink instead of copy lld 2016-09-12 22:38:43 +00:00
lit.local.cfg Remove trailing whitespace. 2016-10-05 20:09:50 +00:00
load-commands-size.yaml lld: Fix initial Mach-O load commands size calculation omitting LC_FUNCTION_STARTS 2019-04-17 01:47:16 +00:00
mach_header-cpusubtype.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
mh_bundle_header.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
mh_dylib_header.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-category-list-atom.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-host-vs-simulator.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-invalid-size.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-invalid-version.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-mismatched-swift-version.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-pass-output.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-simulator-vs-host.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc-image-info-unsupported-gc.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
objc_export_list.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
order_file-basic.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-aliases.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-arm-relocs.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-cfstring32.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-cfstring64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-compact-unwind32.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-compact-unwind64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-data-in-code-armv7.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-data-in-code-x86.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-data-relocs-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-data-relocs-x86_64.yaml Make YAML quote forward slashes. 2018-10-12 16:31:20 +00:00
parse-data.yaml Make YAML quote forward slashes. 2018-10-12 16:31:20 +00:00
parse-eh-frame-relocs-x86_64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-eh-frame-x86-anon.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-eh-frame-x86-labeled.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-eh-frame.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-function.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-initializers32.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-initializers64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-literals-error.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-literals.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-non-lazy-pointers.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-relocs-x86.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-section-no-symbol.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-tentative-defs.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-text-relocs-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-text-relocs-x86_64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
parse-tlv-relocs-x86-64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
re-exported-dylib-ordinal.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
rpath.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
run-tlv-pass-x86-64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
sdk-version-error.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
sectalign.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
sectattrs.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
sectcreate.yaml Make YAML quote forward slashes. 2018-10-12 16:31:20 +00:00
seg-protection-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
seg-protection-x86_64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
source-version.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
stack-size.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
string-table.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
subsections-via-symbols-default.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
twolevel_namespace_undef_dynamic_lookup.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
twolevel_namespace_undef_warning_suppress.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
unwind-info-simple-arm64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
unwind-info-simple-x86_64.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
upward-dylib-load-command.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
upward-dylib-paths.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
usage.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
use-dylib.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
use-simple-dylib.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
version-min-load-command-object.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
version-min-load-command.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
write-final-sections.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00
wrong-arch-error.yaml Replace -flavor {gnu,darwin} with ld64.lld or ld.lld. 2018-02-16 21:16:57 +00:00