Issue 239535
Summary [lld][COFF] lld-link hits llvm_unreachable in machineToStr for R4000 (/machine:mips)
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 `/machine:mips`, which `getMachineType` maps to `IMAGE_FILE_MACHINE_R4000`. But `machineToStr` has no case for R4000 and falls through to `llvm_unreachable("unknown machine type")`. Any machine mismatch diagnostic that involves R4000 reaches it, whether it comes from `/machine:mips` or from an object whose `Machine` is R4000. An assertions build aborts. Release builds print a wrong name, for example `machine type x64 conflicts with x64`.

**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 > x64.yaml <<'YAML'
--- !COFF
header:
  Machine: IMAGE_FILE_MACHINE_AMD64
  Characteristics: [ ]
sections:
  - Name: .text
    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
    Alignment:       16
    SectionData: C3
symbols:
  - Name:            f
    Value:           0
 SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj x64.yaml -o x64.obj
lld-link /dll /noentry /machine:mips /out:x64.dll x64.obj
```

With `/machine:x64`, the same command links (status 0). The other direction aborts the same way: the same YAML with `Machine: IMAGE_FILE_MACHINE_R4000`, linked with `/machine:x64`.

### Observed

```
unknown machine type
UNREACHABLE executed at llvm/lib/Object/WindowsMachineFlag.cpp:51!
```

```
lld::coff::LinkerDriver::addFile(lld::coff::InputFile*)
lld::coff::LinkerDriver::addObjectFile(lld::coff::COFFLinkerContext&, llvm::MemoryBufferRef, llvm::StringRef, unsigned long, bool)
lld::coff::LinkerDriver::addBuffer(...)
```

`machineToStr` is inlined into `addFile` in this build.

### Expected

`error: x64.obj: machine type x64 conflicts with mips`.

### Analysis

`getMachineType` accepts `mips`:

https://github.com/llvm/llvm-project/blob/46ca55573ce669e89f3c452a745d4f471b74f20c/llvm/lib/Object/WindowsMachineFlag.cpp#L23-L34

`machineToStr` has no R4000 case:

https://github.com/llvm/llvm-project/blob/46ca55573ce669e89f3c452a745d4f471b74f20c/llvm/lib/Object/WindowsMachineFlag.cpp#L36-L53

`addFile` formats both machine types when they conflict:

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

The `mips` mapping was added by #121254 ([llvm-lib] Handle MIPS architecture) without a matching `machineToStr` case. Adding `case COFF::IMAGE_FILE_MACHINE_R4000: return "mips";` would fix both paths. `machineToStr` could also return a placeholder for other machine types instead of reaching `llvm_unreachable`, since callers pass it values read from input files.

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