Issue 238273
Summary [llvm-objcopy][MachO] arm64_32: plain copy crashes with SIGSEGV
Labels new issue
Assignees
Reporter ashoror
    On arm64_32 (watchos) MachO object files, a plain llvm-objcopy copy can crash. It seems that the value of an ARM64_RELOC_ADDEND relocation is treated as a section index.

Tested on latest release LLVM 23.1.4 and 21.1.8, macOS 26.6.1 arm64:
```
llvm-objcopy --version
llvm-objcopy, compatible with GNU objcopy
Homebrew LLVM version 23.1.4
  Optimized build.
```

### Sample
A plain C function that returns a pointer to element 13 of a global array (64 elements, 4 bytes each):
```c
// in.c
__attribute__((visibility("hidden"))) int global_value[64];
int *foo(void) {
  return &global_value[13];
}
```
So the offset is 0x000034 (52 bytes):
```console
clang --target=arm64_32-apple-watchos -O2 -c in.c -o in64_32.o
llvm-objdump --macho -r in64_32.o | grep ADDEND
00000004 False long   False  ADDEND False     addend = 0x000034
00000000 False long   False  ADDEND  False addend = 0x000034
```

On a simple copy it crashes with SIGSEGV:
```console
llvm-objcopy in64_32.o copy64_32.o
PLEASE submit a bug report ...
 #3 llvm::objcopy::macho::MachOWriter::writeSections()
 #4 llvm::objcopy::macho::MachOWriter::write()
 #5 llvm::objcopy::macho::executeObjcopyOnBinary(...)
echo $?
139
```

arm64 works fine with the same offset:

```console
clang --target=arm64-apple-watchos -O2 -c in.c -o in64.o
llvm-objcopy in64.o copy64.o
echo $?
0
llvm-objdump --macho -r copy64.o | grep ADDEND
00000004 False long   False  ADDEND  False     addend = 0x000034
00000000 False long   False  ADDEND  False     addend = 0x000034
```

If foo returns &global_value[0] instead, there is no offset, and the copy works:
```c
// in.c
__attribute__((visibility("hidden"))) int global_value[64];
int *foo(void) {
  return &global_value[0];
}
```
```console
clang --target=arm64_32-apple-watchos -O2 -c in.c -o in64_32.o
llvm-objcopy in64_32.o copy64_32.o
echo $?
0
```

The cause seems to be MachOReader.cpp:104: IsAddend is set only for CPU_TYPE_ARM64. There is also _// TODO Support CPU_TYPE_ARM_ above, arm64_32 uses the ARM64 relocation types, including ARM64_RELOC_ADDEND, so it looks like it was missed when ADDEND support was added. 
[MachOReader.cpp:104](https://github.com/llvm/llvm-project/blob/5046886db84caff182582daa813f7459ec7d5814/llvm/lib/ObjCopy/MachO/MachOReader.cpp#L104)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to