Issue 239540
Summary [lld][COFF] lld-link hits llvm_unreachable on a relocation in an object with IMAGE_FILE_MACHINE_UNKNOWN
Labels lld:COFF, 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.

`lld-link` accepts objects whose `Machine` is `IMAGE_FILE_MACHINE_UNKNOWN` (`compatibleMachineType` returns true for it). That's fine for objects without code. But if such an object has a relocation, the relocation code switches on the object's own architecture, which is `Triple::UnknownArch`, and reaches `llvm_unreachable("unknown machine type")`. That happens in `getBaserelType`, or in `SectionChunk::applyRelocation` with `/fixed`. An assertions build aborts.

**Version**: main at 46ca55573ce6 (2026-11-06), 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 > unk.yaml <<'YAML'
--- !COFF
header:
  Machine: IMAGE_FILE_MACHINE_UNKNOWN
  Characteristics: [ ]
sections:
  - Name: .text
    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
    Alignment:       16
 SectionData:     E800000000C3
    Relocations:
      - VirtualAddress:  1
 SymbolName:      g
        Type:            20
symbols:
  - Name: f
    Value:           0
    SectionNumber:   1
    SimpleType: IMAGE_SYM_TYPE_NULL
    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
 StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
  - Name:            g
    Value: 5
    SectionNumber:   1
    SimpleType: IMAGE_SYM_TYPE_NULL
    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
 StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj unk.yaml -o unk.obj
lld-link /dll /noentry /machine:x64 /out:unk.dll unk.obj
```

Type 20 is `IMAGE_REL_I386_REL32`, but the crash doesn't depend on the type. The same object without the relocation links (status 0). It crashes the same way with `/machine:x86` or with no `/machine`. With `/fixed` added, it fails later, at the `applyRelocation` default.

### Observed

```
unknown machine type
UNREACHABLE executed at lld/COFF/Chunks.cpp:601!
```

```
lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
```

`getBaserelType` and `SectionChunk::getBaserels` are inlined in this build. With `/fixed`:

```
UNREACHABLE executed at lld/COFF/Chunks.cpp:513!
lld::coff::SectionChunk::writeTo(unsigned char*) const
```

Release builds print `error: unsupported relocation type 0x14 in unk.obj`. That message comes from whichever `applyRel*` handler the undefined `llvm_unreachable` falls into.

### Expected

An error such as `unk.obj: relocations in an object with unknown machine type`. Alternatively, relocations in such an object could be interpreted using the output machine (`ctx.config.machine`).

### Analysis

`compatibleMachineType` accepts UNKNOWN unconditionally:

https://github.com/llvm/llvm-project/blob/46ca55573ce669e89f3c452a745d4f471b74f20c/lld/COFF/Driver.cpp#L191-L193

Base relocation collection switches on the chunk's architecture:

https://github.com/llvm/llvm-project/blob/46ca55573ce669e89f3c452a745d4f471b74f20c/lld/COFF/Chunks.cpp#L575-L603

and so does applying relocations:

https://github.com/llvm/llvm-project/blob/46ca55573ce669e89f3c452a745d4f471b74f20c/lld/COFF/Chunks.cpp#L496-L514

Both defaults are `llvm_unreachable`. Rejecting an UNKNOWN-machine object that has relocations, when it is read (or in `addFile`), would cover both paths.

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