Index: include/llvm/Object/ELF.h =================================================================== --- include/llvm/Object/ELF.h +++ include/llvm/Object/ELF.h @@ -467,8 +467,9 @@ if (!Index) // no section string table. return ""; if (Index >= Sections.size()) - // TODO: this error is untested. - return createError("invalid section index"); + return createError( + "e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: " + + Twine(Index)); return getStringTable(&Sections[Index]); } Index: test/Object/invalid.test =================================================================== --- test/Object/invalid.test +++ test/Object/invalid.test @@ -536,3 +536,19 @@ FileSize: 0xffff0000 Sections: - Section: .dynamic + +# RUN: yaml2obj --docnum=25 %s -o %t25 +# RUN: not obj2yaml 2>&1 %t25 | FileCheck %s --check-prefix=INVALID-SHSTRNDX + +# INVALID-SHSTRNDX: Error reading file: {{.*}}25: e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: 255 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SHStrNdx: 0xFF +Sections: + - Name: .foo + Type: SHT_PROGBITS Index: test/tools/llvm-readobj/elf-invalid-shstrndx.test =================================================================== --- /dev/null +++ test/tools/llvm-readobj/elf-invalid-shstrndx.test @@ -0,0 +1,26 @@ +# RUN: yaml2obj %s -o %t +# RUN: not llvm-readelf --headers -S 2>&1 %t | FileCheck %s --check-prefix=GNU +# RUN: not llvm-readobj --headers -S 2>&1 %t | FileCheck %s --check-prefix=LLVM + +# GNU: ELF Header: +# GNU: Section header string table index: 255 +# GNU-NEXT: There are 4 section headers, starting at offset 0x40: +# GNU: Section Headers: +# GNU-NEXT: [Nr] Name +# GNU-EMPTY: +# GNU-NEXT: error: e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: 255 + +# LLVM: ElfHeader { +# LLVM: StringTableSectionIndex: 255 +# LLVM-NEXT: } +# LLVM-NEXT: Sections [ +# LLVM-EMPTY: +# LLVM-NEXT: error: e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: 255 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SHStrNdx: 0xFF Index: tools/llvm-readobj/ELFDumper.cpp =================================================================== --- tools/llvm-readobj/ELFDumper.cpp +++ tools/llvm-readobj/ELFDumper.cpp @@ -3017,7 +3017,9 @@ if (!Index) // no section string table. return ""; if (Index >= Sections.size()) - reportError("invalid section index"); + reportError( + "e_shstrndx/SHN_XINDEX refers to a section that doesn't exist: " + + Twine(Index)); StringRef Data = toStringRef(unwrapOrError( Obj.template getSectionContentsAsArray(&Sections[Index]))); return unwrapOrError(Obj.getSectionName(&Sec, Data)); Index: tools/llvm-readobj/llvm-readobj.cpp =================================================================== --- tools/llvm-readobj/llvm-readobj.cpp +++ tools/llvm-readobj/llvm-readobj.cpp @@ -371,6 +371,7 @@ namespace llvm { LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { + fouts().flush(); errs() << "\n"; WithColor::error(errs()) << Msg << "\n"; exit(1);