Index: ELF/InputFiles.h =================================================================== --- ELF/InputFiles.h +++ ELF/InputFiles.h @@ -151,6 +151,8 @@ InputSectionBase *getSection(const Elf_Sym &Sym) const; SymbolBody &getSymbolBody(uint32_t SymbolIndex) const { + if (SymbolIndex >= SymbolBodies.size()) + fatal("invalid symbol index"); return *SymbolBodies[SymbolIndex]; } Index: test/ELF/invalid/invalid-symbol-index.s =================================================================== --- test/ELF/invalid/invalid-symbol-index.s +++ test/ELF/invalid/invalid-symbol-index.s @@ -0,0 +1,21 @@ +## invalid-symbol-index.elf has incorrect type of .symtab section: +## Section Headers: +## [Nr] Name Type Address Off Size ES Flg Lk Inf Al +## [ 0] NULL 0000000000000000 000000 000000 00 0 0 0 +## ... +## [ 4] .symtab RELA 0000000000000000 000048 000030 18 1 2 8 +## There is no symbol bodies because of that but relocations resolution tries to access them. +# RUN: not ld.lld %p/Inputs/invalid-symbol-index.elf -o %t2 2>&1 | \ +# RUN: FileCheck --check-prefix=INVALID-SYMBOL-INDEX %s +# INVALID-SYMBOL-INDEX: invalid symbol index + +## invalid-symbol-index2.elf just contains wrong symbol index in relocation: +## Relocation section '.rela.text' at offset 0x1c0 contains 1 entries: +## Offset Info Type Symbol's Value Symbol's Name + Addend +## 0000000000000123 000000ff00000002 R_X86_64_PC32 bad symbol index: 000000ff +## +## Symbol table '.symtab' contains 1 entries: +## Num: Value Size Type Bind Vis Ndx Name +## 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND +# RUN: not ld.lld %p/Inputs/invalid-symbol-index2.elf -o %t2 2>&1 | \ +# RUN: FileCheck --check-prefix=INVALID-SYMBOL-INDEX %s