Issue 243298
Summary [lld][MachO] ld64.lld asserts in getRelocAttrs on an out-of-range relocation type instead of reporting it
Labels lld:MachO, crash
Assignees
Reporter AnantaSrikar
    `TargetInfo::getRelocAttrs` asserts that the relocation type is in the target's table, then has a fallback for when it isn't: it returns `invalidRelocAttrs`, so that `validateRelocationInfo` can report an `INVALID relocation` error. `ObjFile::parseRelocations` passes the type straight from the input, so an object with an unknown relocation type makes an assertions build abort instead of reaching that error. This happens on ARM64 (type 14) and x86-64 (type 10).

**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 > reloc14.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: 14
            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 reloc14.yaml -o reloc14.o
ld64.lld -arch arm64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o reloc14.dylib reloc14.o
```

With `type: 2` (`ARM64_RELOC_BRANCH26`) instead, the same object links (status 0). An x86-64 object with a single `X86_64_RELOC_BRANCH` changed to type 10 fails the same assertion.

### Observed

```
ld64.lld: lld/MachO/Target.h:114: const RelocAttrs &lld::macho::TargetInfo::getRelocAttrs(uint8_t) const: Assertion `type < relocAttrs.size() && "invalid relocation type"' failed.
```

```
void lld::macho::ObjFile::parseRelocations<llvm::MachO::section_64>(llvm::ArrayRef<llvm::MachO::section_64>, llvm::MachO::section_64 const&, lld::macho::Section&)
void lld::macho::ObjFile::parseDeferredRelocationsImpl<lld::macho::LP64>()
lld::macho::parseDeferredRelocations()
```

### Expected

The error that the fallback is there for, for example `INVALID relocation ... at offset 0 of __TEXT,__text in reloc14.o`.

### Analysis

The assertion and the fallback it guards:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/Target.h#L113-L118

`parseRelocations` calls it through `hasAttr` with the input's `r_type` before anything has checked the type, and `validateRelocationInfo` calls it again to build its message:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L598-L608

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L529-L531

Since the type comes from the input, removing the assertion and relying on the existing fallback would fix it. Alternatively, `parseRelocations` could reject an unknown type before its first `hasAttr` call.

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

Reply via email to