Index: lld/trunk/COFF/Chunks.cpp =================================================================== --- lld/trunk/COFF/Chunks.cpp +++ lld/trunk/COFF/Chunks.cpp @@ -252,7 +252,19 @@ // Get the output section of the symbol for this relocation. The output // section is needed to compute SECREL and SECTION relocations used in debug // info. - Defined *Sym = cast(File->getSymbol(Rel.SymbolTableIndex)); + auto *Sym = + dyn_cast_or_null(File->getSymbol(Rel.SymbolTableIndex)); + if (!Sym) { + if (isCodeView() || isDWARF()) + continue; + // Symbols in early discarded sections are represented using null pointers, + // so we need to retrieve the name from the object file. + COFFSymbolRef Sym = + check(File->getCOFFObj()->getSymbol(Rel.SymbolTableIndex)); + StringRef Name; + File->getCOFFObj()->getSymbolName(Sym, Name); + fatal("relocation against symbol in discarded section: " + Name); + } Chunk *C = Sym->getChunk(); OutputSection *OS = C ? C->getOutputSection() : nullptr; @@ -328,7 +340,8 @@ uint8_t Ty = getBaserelType(Rel); if (Ty == IMAGE_REL_BASED_ABSOLUTE) continue; - if (isa(File->getSymbol(Rel.SymbolTableIndex))) + Symbol *Target = File->getSymbol(Rel.SymbolTableIndex); + if (!Target || isa(Target)) continue; Res->emplace_back(RVA + Rel.VirtualAddress, Ty); } Index: lld/trunk/COFF/MarkLive.cpp =================================================================== --- lld/trunk/COFF/MarkLive.cpp +++ lld/trunk/COFF/MarkLive.cpp @@ -63,7 +63,8 @@ // Mark all symbols listed in the relocation table for this section. for (Symbol *B : SC->symbols()) - AddSym(B); + if (B) + AddSym(B); // Mark associative sections if any. for (SectionChunk *C : SC->children()) Index: lld/trunk/test/COFF/reloc-discarded-early.s =================================================================== --- lld/trunk/test/COFF/reloc-discarded-early.s +++ lld/trunk/test/COFF/reloc-discarded-early.s @@ -0,0 +1,8 @@ +# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s +# RUN: lld-link -entry:__ImageBase -subsystem:console -debug %t.obj + +.section .debug_info,"dr" +.quad .Ldrectve + +.section .drectve +.Ldrectve: Index: lld/trunk/test/COFF/reloc-discarded-early2.s =================================================================== --- lld/trunk/test/COFF/reloc-discarded-early2.s +++ lld/trunk/test/COFF/reloc-discarded-early2.s @@ -0,0 +1,9 @@ +# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s +# RUN: not lld-link -entry:__ImageBase -subsystem:console %t.obj 2>&1 | FileCheck %s + +.text +# CHECK: error: relocation against symbol in discarded section: .drectve +.quad .Ldrectve + +.section .drectve +.Ldrectve: