llvm-project/lld/lib/ReaderWriter/MachO
Rui Ueyama ea65b5aa49 [MACH-O] Fix the ASM code generated for __stub_helpers section
Patch by Patricio Villalobos.

I discovered that lld for darwin is generating the wrong code for lazy
bindings in the __stub_helper section (at least for osx 10.12). This is
the way i can reproduce this problem, using this program:

  #include <stdio.h>

  int main(int argc, char **argv) {
      printf("C: printf!\n");
      puts("C: puts!\n");
      return 0;
  }

Then I link it using i have tested it in 3.9, 4.0 and 4.1 versions:

  $ clang -c hello.c
  $ lld -flavor darwin hello.o -o h1  -lc

When i execute the binary h1 the system gives me the following error:

  C: printf!
  dyld: lazy symbol binding failed:
  BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
  has segment 4 which is too large (0..3)
  dyld: BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB has segment 4 which is too
  large (0..3)
  Trace/BPT trap: 5

Investigating the code, it seems that the problem is that the asm code
generated in the file StubPass.cpp, specifically in the line 323,when it
adds, what it seems an arbitrary number (12) to the offset into the lazy
bind opcodes section, but it should be calculated depending on the
MachONormalizedFileBinaryWrite::lazyBindingInfo result.

I confirmed this bug by patching the code manually in the binary and
writing the right offset in the asm code (__stub_helper).

This patch fixes the content of the atom that contains the assembly code
when the offset is known.

Differential Revision: https://reviews.llvm.org/D35387

llvm-svn: 311734
2017-08-24 23:51:40 +00:00
..
ArchHandler.cpp Fix heap-buffer-overflow bugs identified by the Address Sanitizer 2015-02-19 20:42:23 +00:00
ArchHandler.h [MACH-O] Fix the ASM code generated for __stub_helpers section 2017-08-24 23:51:40 +00:00
ArchHandler_arm.cpp [MACH-O] Fix the ASM code generated for __stub_helpers section 2017-08-24 23:51:40 +00:00
ArchHandler_arm64.cpp [MACH-O] Fix the ASM code generated for __stub_helpers section 2017-08-24 23:51:40 +00:00
ArchHandler_x86.cpp [MACH-O] Fix the ASM code generated for __stub_helpers section 2017-08-24 23:51:40 +00:00
ArchHandler_x86_64.cpp [MACH-O] Fix the ASM code generated for __stub_helpers section 2017-08-24 23:51:40 +00:00
Atoms.h Fix some Include What You Use warnings; other minor fixes. 2016-04-29 19:47:09 +00:00
CMakeLists.txt [CMake] Fix pthread handling for out-of-tree builds 2017-02-10 01:59:20 +00:00
CompactUnwindPass.cpp Prevent at compile time converting from Error::success() to Expected<T> 2016-11-11 04:29:25 +00:00
DebugInfo.h [MachO] Remove code accidentally cut'n'pasted 3 times. 2016-09-10 20:41:26 +00:00
ExecutableAtoms.h Move Object format code to lib/BinaryFormat. 2017-06-07 03:48:56 +00:00
File.h Change when we choose to add an LC_LOAD_DYLIB to the final image. 2016-08-11 20:10:14 +00:00
FlatNamespaceFile.h Remove dead flags. 2016-03-28 20:59:47 +00:00
GOTPass.cpp Fix wrong formatting in lld introduced in r286561 (NFC) 2016-11-11 22:28:19 +00:00
LayoutPass.cpp [Support] Move Parallel algorithms from LLD to LLVM. 2017-05-11 00:03:52 +00:00
LayoutPass.h [lld][MachO] Remove SimpleFile::definedAtoms(). 2016-06-28 18:42:33 +00:00
MachOLinkingContext.cpp Move Object format code to lib/BinaryFormat. 2017-06-07 03:48:56 +00:00
MachONormalizedFile.h Move Object format code to lib/BinaryFormat. 2017-06-07 03:48:56 +00:00
MachONormalizedFileBinaryReader.cpp lld matching change for llvm change r308690 to add error handling to 2017-07-20 23:09:19 +00:00
MachONormalizedFileBinaryUtils.h Move Object format code to lib/BinaryFormat. 2017-06-07 03:48:56 +00:00
MachONormalizedFileBinaryWriter.cpp Move Object format code to lib/BinaryFormat. 2017-06-07 03:48:56 +00:00
MachONormalizedFileFromAtoms.cpp [MACH-O] Fix the ASM code generated for __stub_helpers section 2017-08-24 23:51:40 +00:00
MachONormalizedFileToAtoms.cpp Tweak to match change in LLVM API, in r306315. 2017-06-26 18:43:26 +00:00
MachONormalizedFileYAML.cpp Fix ODR violations due to abuse of LLVM_YAML_IS_(FLOW_)?SEQUENCE_VECTOR 2017-06-30 20:57:21 +00:00
MachOPasses.h Add an ObjCPass to the MachO linker. 2016-01-19 21:54:21 +00:00
ObjCPass.cpp Fix ObjCPass on big-endian host 2017-07-24 23:13:31 +00:00
SectCreateFile.h Use owning pointers instead of raw pointers for Atom's to fix leaks. 2016-03-22 17:15:50 +00:00
ShimPass.cpp Prevent at compile time converting from Error::success() to Expected<T> 2016-11-11 04:29:25 +00:00
StubsPass.cpp Prevent at compile time converting from Error::success() to Expected<T> 2016-11-11 04:29:25 +00:00
TLVPass.cpp Prevent at compile time converting from Error::success() to Expected<T> 2016-11-11 04:29:25 +00:00
WriterMachO.cpp Move Object format code to lib/BinaryFormat. 2017-06-07 03:48:56 +00:00