<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/65855>65855</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [lld-link] Failures/crashes due to dropping referenced empty sections for x86_64-pc-windows-msvc
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          danielframpton
      </td>
    </tr>
</table>

<pre>
    Linking the object compiled from `zst.ll` (see IR below) with `libcmt.lib` for the `x86_64-pc-windows-msvc` target fails with:
```
lld-link: error: relocation against symbol in discarded section: nothing
>>> referenced by zst.o:(pointer_to_nothing)
```
This is because we drop empty sections (as necessary for a valid PE) but do not handle the case where there are references into that section.

This is a minimized reproduction from a Rust program that hit this issue when an array constructed using linker sections was empty. In that case the only reference (after `/opt:ref`) was from debug info and so the linker crashes writing debug information where we were trying to compute the offset of a global and the section was null:

```
lld-link: llvm-project/lld/COFF/PDB.cpp:1145: llvm::pdb::BulkPublic createPublic(lld::coff::COFFLinkerContext&, lld::coff::Defined*): Assertion `os && "all publics should be in final image"' failed.

          ...
          #9 0x00007f32c4984e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
          #10 0x00005622018608a0 createPublic(lld::coff::COFFLinkerContext&, lld::coff::Defined*) llvm-project/lld/COFF/PDB.cpp:1145:27
          #11 0x0000562201860ef1 (anonymous namespace)::PDBLinker::addPublicsToPDB()::'lambda'(lld::coff::Symbol*)::operator()(lld::coff::Symbol*) const llvm-project/lld/COFF/PDB.cpp:1224:24
          #12 0x0000562201865dbc void lld::coff::SymbolTable::forEachSymbol<(anonymous namespace)::PDBLinker::addPublicsToPDB()::'lambda'(lld::coff::Symbol*)>((anonymous namespace)::PDBLinker::addPublicsToPDB()::'lambda'(lld::coff::Symbol*)) llvm-project/lld/COFF/SymbolTable.h:119:5
          #13 0x0000562201860fb2 (anonymous namespace)::PDBLinker::addPublicsToPDB() llvm-project/lld/COFF/PDB.cpp:1228:21
          #14 0x00005622018639e9 lld::coff::createPDB(lld::coff::COFFLinkerContext&, llvm::ArrayRef<unsigned char>, llvm::codeview::DebugInfo*) llvm-project/lld/COFF/PDB.cpp:1610:22
 #15 0x00005622017c49fb (anonymous namespace)::Writer::run() llvm-project/lld/COFF/Writer.cpp:694:15
          #16 0x00005622017c2d88 lld::coff::writeResult(lld::coff::COFFLinkerContext&) llvm-project/lld/COFF/Writer.cpp:323:55
          #17 0x000056220173d7df lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) llvm-project/lld/COFF/Driver.cpp:2532:17
          #18 0x000056220172a077 lld::coff::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) llvm-project/lld/COFF/Driver.cpp:77:20
 #19 0x00005622016fc19f lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) llvm-project/lld/Common/DriverDispatcher.cpp:164:11
          #20 0x00005622015d08dc lld_main(int, char**, llvm::ToolContext const&) llvm-project/lld/tools/lld/lld.cpp:92:27
 ...
```

`zst.ll`:
```
target triple = "x86_64-pc-windows-msvc"

@nothing = internal constant <{}> zeroinitializer, section ".nothing", align 1
@pointer_to_nothing = internal constant <{ ptr }> <{ ptr @nothing }>, align 8

define dso_local i32 @main() {
start:
  ret i32 0
}
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWNFv274R_mvol0MMibJk6cEPTpwABTos6ArsMaDIk8WVIgWSiuP-9QMp2bFddWmGDb_CaCzpePfddx9PPDPn5F4jbkh-T_Ldgg2-NXYjmJaoGsu63hu9qI04br5K_UPqPfgWwdT_Qu6Bm66XCgU01nRAiuSn80ulSJEAoaVDhC_foEZlDoRWcJC-DUZK1rzzSyXrYNgYG12SInkri5diddfzu4PUwhzcXedeeTDyzO7RQ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