| Issue |
243295
|
| Summary |
[lld][MachO] ld64.lld reads past the relocation table when an ARM64 ADDEND or SUBTRACTOR relocation has no paired record
|
| Labels |
lld:MachO,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
I'm Ananta Srikar Puranam, a PhD student at Washington State University. I'm reporting this bug on behalf of my team.
`ARM64_RELOC_ADDEND` and `ARM64_RELOC_SUBTRACTOR` must each be followed by a paired relocation record. `ObjFile::parseRelocations` reads that record with `relInfos[++i]` without checking that it exists. If either one is the last relocation of a section, an assertions build fails `Invalid index!`, and a release build reads one record past the section's relocation table.
**Version**: main at c7014922f582 (2026-11-16), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`, on x86_64 Linux.
### Reproducer
```sh
cat > addend.yaml <<'YAML'
--- !mach-o
IsLittleEndian: true
FileHeader:
magic: 0xFEEDFACF
cputype: 0x100000C
cpusubtype: 0x0
filetype: 0x1
ncmds: 2
sizeofcmds: 176
flags: 0x0
reserved: 0x0
LoadCommands:
- cmd: LC_SEGMENT_64
cmdsize: 152
segname: ''
vmaddr: 0
vmsize: 9
fileoff: 208
filesize: 9
maxprot: 7
initprot: 7
nsects: 1
flags: 0
Sections:
- sectname: __text
segname: __TEXT
addr: 0x0
size: 9
offset: 0xD0
align: 2
reloff: 0xE0
nreloc: 1
flags: 0x80000400
reserved1: 0x0
reserved2: 0x0
reserved3: 0x0
content: 00000094C0035FD600
relocations:
- address: 0x0
symbolnum: 1
pcrel: true
length: 2
extern: true
type: 10
scattered: false
value: 0
- cmd: LC_SYMTAB
cmdsize: 24
symoff: 232
nsyms: 2
stroff: 264
strsize: 34
LinkEditData:
NameList:
- n_strx: 1
n_type: 0xF
n_sect: 1
n_desc: 0
n_value: 0
- n_strx: 17
n_type: 0x1
n_sect: 0
n_desc: 0
n_value: 0
StringTable:
- ''
- _entry_00000000
- _target_00000000
...
YAML
yaml2obj addend.yaml -o addend.o
ld64.lld -arch arm64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o addend.dylib addend.o
```
The only relocation is `ADDEND` (type 10). With `type: 2` (`ARM64_RELOC_BRANCH26`) instead, the same object links (status 0). A lone `SUBTRACTOR` fails the same assertion: use `type: 1`, `pcrel: false` and `length: 3`.
### Observed
```
ld64.lld: llvm/include/llvm/ADT/ArrayRef.h:247: const T &llvm::ArrayRef<llvm::MachO::relocation_info>::operator[](size_t) const [T = llvm::MachO::relocation_info]: Assertion `Index < Length && "Invalid index!"' failed.
```
```
#9 void lld::macho::ObjFile::parseRelocations<llvm::MachO::section_64>(llvm::ArrayRef<llvm::MachO::section_64>, llvm::MachO::section_64 const&, lld::macho::Section&)
#10 void lld::macho::ObjFile::parseDeferredRelocationsImpl<lld::macho::LP64>()
#13 lld::macho::parseDeferredRelocations()
#14 lld::macho::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```
### Expected
An error saying the `ADDEND` (or `SUBTRACTOR`) relocation is missing its paired relocation.
### Analysis
The `ADDEND` branch increments `i` and indexes before the `i < relInfos.size()` assertion:
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L601-L606
The `SUBTRACTOR` branch reads its minuend the same way:
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L676-L681
Checking `i + 1 < relInfos.size()` before each `relInfos[++i]`, and reporting an error for the input file otherwise, would fix both.
This report was drafted with help from an AI assistant (Claude); I reviewed it and reproduced it myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs