Repository: rust
Issue: rustc generates invalid DWARF when LTO is enabled
Labels: A-LLVM,A-debuginfo,E-needs-test,T-compiler,C-bug

When combining `lto = true` with `debug = true` in Cargo.toml, the compiler generates invalid DWARF, according to [Gimli](https://github.com/gimli-rs/gimli.git).

I've created a minimal example here: https://github.com/hannobraun/dwarf-test

If you follow the instructions in the example's README, you should see something like this:
```
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13d1b to 0x492
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13d30 to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13d4a to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13d59 to 0x5e2
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13d91 to 0x4857
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13da6 to 0x536c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13dce to 0xf5
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13de2 to 0xe8
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13dfb to 0x2b31
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13e10 to 0x4857
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13e25 to 0x536c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13e4d to 0xf5
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13e61 to 0xe8
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13e87 to 0x4857
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13e9c to 0x536c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13ec4 to 0xf5
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13ed8 to 0xe8
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13ef1 to 0x2b31
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13f06 to 0x4857
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13f1b to 0x536c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13f43 to 0xf5
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13f57 to 0xe8
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13fb5 to 0x4ac
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13fca to 0x49f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13fdf to 0x492b
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x13ff4 to 0x523c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14009 to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14022 to 0x4893
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1402f to 0x4880
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14049 to 0x4b9
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14056 to 0x5ef
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14063 to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14085 to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1409c to 0x5fc
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x140b1 to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x140db to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x140f2 to 0x609
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14107 to 0x616
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1411c to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14146 to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1415d to 0x609
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14172 to 0x616
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14187 to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x141b1 to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x141c8 to 0x609
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x141dd to 0x616
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x141f7 to 0x4e0
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1420c to 0x62d
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14221 to 0x4d3
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14236 to 0x4945
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1424b to 0x4938
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14279 to 0x492
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1428e to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x142a8 to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x142b7 to 0x5e2
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x142c5 to 0x492
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x142da to 0x491e
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x142f4 to 0x522f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14303 to 0x5e2
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14326 to 0x4857
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1433b to 0x536c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14363 to 0xf5
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14377 to 0xe8
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14390 to 0x2b31
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x143a5 to 0x4857
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x143ba to 0x536c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x143e2 to 0xf5
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x143f6 to 0xe8
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x144ea to 0x3e22
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14528 to 0x4531
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14551 to 0x4ed
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14582 to 0x453d
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14d39 to 0x4100
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14dcb to 0x3e38
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14e10 to 0x412c
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14e29 to 0x3e4b
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x14e3e to 0x4b11
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x174a6 to 0x6cf
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x17532 to 0x6cf
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x175be to 0x6cf
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x17652 to 0x6cf
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x1770d to 0x7ac
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x178c3 to 0x816
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x178d8 to 0x36f
DWARF error in ../dwarf-test/target/debug/dwarf-test: Invalid intra-unit reference in unit 0x102 from DIE 0x17903 to 0x37c
```

I've seen this first in a larger project. There, the combination of `lto = true` and `debug = true`  also causes GDB to freeze when reading the symbols from the binary. I wasn't able to reproduce this in my minimal example, but in the larger project, all binaries that caused GDB to freeze produced these DWARF errors, while I didn't see these errors in the binaries that worked normally.

So while I'm not 100% sure that these DWARF errors are a real problem, I suspect that they're the cause of the GDB freezes I was seeing.