llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.36k stars 11.71k forks source link

lld:MachO Crash while trying to link simple object file #60966

Open nike4613 opened 1 year ago

nike4613 commented 1 year ago
$ ld64.lld --version
Homebrew LLD 15.0.7
PLEASE submit a bug report to https://github.com/Homebrew/homebrew-core/issues and include the crash backtrace.
Stack dump:
0.      Program arguments: ld64.lld -dylib -arch x86_64 -platform_version macos 10.6 10.6 -x -o exhelper_macos_x86_64.dylib exhelper_macos.o
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVM.dylib            0x000000010f7d43ba llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 42
1  libLLVM.dylib            0x00000001133b18eb SignalHandler(int) + 235
2  libsystem_platform.dylib 0x00007ff8036bedfd _sigtramp + 29
3  libsystem_platform.dylib 0x00007ff7bb4a38a0 _sigtramp + 18446744072499382976
4  lld                      0x0000000104d1aebd void lld::macho::ObjFile::parse<lld::macho::LP64>() + 973
5  lld                      0x0000000104d1a665 lld::macho::ObjFile::ObjFile(llvm::MemoryBufferRef, unsigned int, llvm::StringRef, bool, bool) + 421
6  lld                      0x0000000104d09bd6 lld::macho::ObjFile* lld::make<lld::macho::ObjFile, llvm::MemoryBufferRef&, unsigned int, char const (&) [1], bool&>(llvm::MemoryBufferRef&, unsigned int&&, char const (&) [1], bool&) + 486
7  lld                      0x0000000104d06867 addFile(llvm::StringRef, LoadType, bool, bool, bool, bool) + 583
8  lld                      0x0000000104d036fe lld::macho::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) + 32206
9  lld                      0x0000000104a6141c lldMain(int, char const**, llvm::raw_ostream&, llvm::raw_ostream&, bool) + 1580
10 lld                      0x0000000104a60c04 main + 244
11 dyld                     0x000000010f58f52e start + 462

This issue also occurs using the same command line on Windows:

$ ld64.lld --version
LLD 15.0.5
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: "C:\\Program Files\\LLVM\\bin\\ld64.lld.exe" -dylib -arch x86_64 -platform_version macos 10.6 10.6 -x -o exhelper_macos_x86_64.dylib exhelper_macos.o
Exception Code: 0xC0000005
 #0 0x00007ff60d169573 C:\Program Files\LLVM\bin\ld64.lld.exe 0x4f9573 C:\Program Files\LLVM\bin\ld64.lld.exe 0x4f7819
 #1 0x00007ff60d169573 C:\Program Files\LLVM\bin\ld64.lld.exe 0x4f6ede C:\Program Files\LLVM\bin\ld64.lld.exe 0x98fd7
 #2 0x00007ff60d169573 C:\Program Files\LLVM\bin\ld64.lld.exe 0x95e34 C:\Program Files\LLVM\bin\ld64.lld.exe 0x93e4c
 #3 0x00007ff60d169573 C:\Program Files\LLVM\bin\ld64.lld.exe 0x1cc8 C:\Program Files\LLVM\bin\ld64.lld.exe 0x1446
 #4 0x00007ff60d169573 C:\Program Files\LLVM\bin\ld64.lld.exe 0x2a981e4 (C:\Program Files\LLVM\bin\ld64.lld.exe+0x4f9573)
 #5 0x00007ff60d169573
 #6 0x00007ff60d169573 (C:\Program Files\LLVM\bin\ld64.lld.exe+0x4f9573)
0x00007FF60D169573, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x4F9573 byte(s), Ordinal0() + 0x4F9573 byte(s)
0x00007FF60D167819, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x4F7819 byte(s), Ordinal0() + 0x4F7819 byte(s)
0x00007FF60D166EDE, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x4F6EDE byte(s), Ordinal0() + 0x4F6EDE byte(s)
0x00007FF60CD08FD7, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x98FD7 byte(s), Ordinal0() + 0x98FD7 byte(s)
0x00007FF60CD05E34, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x95E34 byte(s), Ordinal0() + 0x95E34 byte(s)
0x00007FF60CD03E4C, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x93E4C byte(s), Ordinal0() + 0x93E4C byte(s)
0x00007FF60CC71CC8, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x1CC8 byte(s), Ordinal0() + 0x1CC8 byte(s)
0x00007FF60CC71446, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x1446 byte(s), Ordinal0() + 0x1446 byte(s)
0x00007FF60F7081E4, C:\Program Files\LLVM\bin\ld64.lld.exe(0x00007FF60CC70000) + 0x2A981E4 byte(s), xmlLinkGetData() + 0x214 byte(s)
0x00007FF8A3507614, C:\Windows\System32\KERNEL32.DLL(0x00007FF8A34F0000) + 0x17614 byte(s), BaseThreadInitThunk() + 0x14 byte(s)
0x00007FF8A53E26A1, C:\Windows\SYSTEM32\ntdll.dll(0x00007FF8A5390000) + 0x526A1 byte(s), RtlUserThreadStart() + 0x21 byte(s)

To reproduce:

  1. Acquire this folder and its siblings
  2. Assemble using nasm -f macho64 -Ox exhelper_macos_x86_64.asm -o exhelper_macos_x86_64.o
  3. Strip local symbols using llvm-objcopy -x exhelper_macos_x86_64.o exhelper_macos.o
    • This is needed because the next stage will fail in an entirely unrelated way otherwise:
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.ID at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.Version at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.AugString at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.DataAlignmentFactor at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.AugmentationData at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.PointerEncoding at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.PersonalityEncoding at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.PersonalityRoutine at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.LSDAEncoding at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.AugEnd at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@11.CIE.InitialInstructions at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@26.FDE.pCIE at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@26.FDE.PCBegin at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@26.FDE.PCRange at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@26.FDE.AugmentationLength at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@26.FDE.LSDA at misaligned offset
      ld64.lld: error: exhelper_macos_x86_64.o:(__eh_frame):  symbol L_..@26.FDE.AugmentationData at misaligned offset
      ld64.lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
  4. Attempt to link with ld64.lld -dylib -arch x86_64 -platform_version macos 10.6 10.6 -x -o exhelper_macos_x86_64.dylib exhelper_macos.o
llvmbot commented 1 year ago

@llvm/issue-subscribers-lld-macho

int3 commented 1 year ago

This boils down to nasm encoding unwind info quite differently from llvm-mc. Not handling it is definitely a shortcoming on LLD's side, but adding support for this will require quite a bit of rewriting that I'm not available to do now. Sorry about that. Porting the assembler inputs from nasm to llvm-mc is probably your best bet here if you wish to use LLD.

nike4613 commented 1 year ago

We ended up creating patches for both NASM and LLD, which gives us sane output: The LLD patch The NASM patch

The person who created those patches (@Popax21) noted that particularly the LLVM patch is very hacky and likely incorrect, but sufficient for our purposes.