Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -379,7 +379,7 @@ if (!Out::SymTab) return; for (elf::ObjectFile *F : Symtab::X->getObjectFiles()) { - const char *StrTab = F->getStringTable().data(); + StringRef StrTab = F->getStringTable(); for (SymbolBody *B : F->getLocalSymbols()) { auto *DR = dyn_cast>(B); // No reason to keep local undefined symbol in symtab. @@ -387,7 +387,9 @@ continue; if (!includeInSymtab(*B)) continue; - StringRef SymName(StrTab + B->getNameOffset()); + if (B->getNameOffset() >= StrTab.size()) + fatal("invalid symbol name offset"); + StringRef SymName(StrTab.data() + B->getNameOffset()); InputSectionBase *Sec = DR->Section; if (!shouldKeepInSymtab(Sec, SymName, *B)) continue; Index: test/ELF/invalid-symbol-name.s =================================================================== --- test/ELF/invalid-symbol-name.s +++ test/ELF/invalid-symbol-name.s @@ -0,0 +1,5 @@ +# REQUIRES: x86 + +# RUN: not ld.lld %S/Inputs/invalid-symbol-name-offset.elf \ +# RUN: -o %t 2>&1 | FileCheck %s +# CHECK: invalid symbol name offset