Index: lld/test/ELF/invalid/dynamic-section-broken.test =================================================================== --- lld/test/ELF/invalid/dynamic-section-broken.test +++ lld/test/ELF/invalid/dynamic-section-broken.test @@ -1,7 +1,7 @@ ## .dynamic section has invalid sh_entsize, check we report it. # RUN: yaml2obj --docnum=1 %s -o %t.so # RUN: not ld.lld %t.so -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR1 -# ERR1: error: {{.*}}.so: section [index 1] has an invalid sh_entsize: 291 +# ERR1: error: {{.*}}.so: section [index 1] has invalid sh_entsize: expected 16, but got 291 --- !ELF FileHeader: Index: llvm/include/llvm/Object/ELF.h =================================================================== --- llvm/include/llvm/Object/ELF.h +++ llvm/include/llvm/Object/ELF.h @@ -410,7 +410,8 @@ ELFFile::getSectionContentsAsArray(const Elf_Shdr &Sec) const { if (Sec.sh_entsize != sizeof(T) && sizeof(T) != 1) return createError("section " + getSecIndexForError(*this, Sec) + - " has an invalid sh_entsize: " + Twine(Sec.sh_entsize)); + " has invalid sh_entsize: expected " + Twine(sizeof(T)) + + ", but got " + Twine(Sec.sh_entsize)); uintX_t Offset = Sec.sh_offset; uintX_t Size = Sec.sh_size; @@ -616,17 +617,17 @@ template Expected ELFFile::getEntry(const Elf_Shdr &Section, uint32_t Entry) const { - if (sizeof(T) != Section.sh_entsize) - return createError("section " + getSecIndexForError(*this, Section) + - " has invalid sh_entsize: expected " + Twine(sizeof(T)) + - ", but got " + Twine(Section.sh_entsize)); - uint64_t Pos = Section.sh_offset + (uint64_t)Entry * sizeof(T); - if (Pos + sizeof(T) > Buf.size()) - return createError("unable to access section " + - getSecIndexForError(*this, Section) + " data at 0x" + - Twine::utohexstr(Pos) + - ": offset goes past the end of file"); - return reinterpret_cast(base() + Pos); + Expected> EntriesOrErr = getSectionContentsAsArray(Section); + if (!EntriesOrErr) + return EntriesOrErr.takeError(); + + ArrayRef Arr = *EntriesOrErr; + if (Entry >= Arr.size()) + return createError("can't read an entry at 0x" + + Twine::utohexstr(Entry * sizeof(T)) + + ": it goes past the end of the section (0x" + + Twine::utohexstr(Section.sh_size) + ")"); + return &Arr[Entry]; } template Index: llvm/include/llvm/Object/ELFObjectFile.h =================================================================== --- llvm/include/llvm/Object/ELFObjectFile.h +++ llvm/include/llvm/Object/ELFObjectFile.h @@ -411,7 +411,7 @@ const Elf_Sym *getSymbol(DataRefImpl Sym) const { auto Ret = EF.template getEntry(Sym.d.a, Sym.d.b); if (!Ret) - report_fatal_error(errorToErrorCode(Ret.takeError()).message()); + report_fatal_error(Ret.takeError()); return *Ret; } Index: llvm/test/Object/invalid.test =================================================================== --- llvm/test/Object/invalid.test +++ llvm/test/Object/invalid.test @@ -118,7 +118,7 @@ # RUN: yaml2obj %s --docnum=6 -o %t6 # RUN: llvm-readobj --symbols %t6 2>&1 | FileCheck -DFILE=%t6 --check-prefix=INVALID-SYM-SIZE %s -# INVALID-SYM-SIZE: warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has an invalid sh_entsize: 32 +# INVALID-SYM-SIZE: warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has invalid sh_entsize: expected 24, but got 32 --- !ELF FileHeader: @@ -630,7 +630,7 @@ # RUN: yaml2obj %s --docnum=29 -o %t29 # RUN: llvm-readobj -V %t29 2>&1 | FileCheck -DFILE=%t29 --check-prefix=INVALID-VER-SHENTSIZE %s -# INVALID-VER-SHENTSIZE: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has an invalid sh_entsize: 3 +# INVALID-VER-SHENTSIZE: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has invalid sh_entsize: expected 2, but got 3 --- !ELF FileHeader: Index: llvm/test/tools/llvm-nm/invalid-symbol-table-size.test =================================================================== --- llvm/test/tools/llvm-nm/invalid-symbol-table-size.test +++ llvm/test/tools/llvm-nm/invalid-symbol-table-size.test @@ -3,17 +3,17 @@ ## a) Broken .symtab symbol table. Valid .dynsym symbol table. # RUN: yaml2obj -DBITS=32 -DSIZE=33 -DDYNSIZE=32 %s -o %t.32-bit.o -# RUN: not llvm-nm %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=2 %s +# RUN: not --crash llvm-nm %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=2 %s # RUN: yaml2obj -DBITS=64 -DSIZE=49 -DDYNSIZE=48 %s -o %t.64-bit.o -# RUN: not llvm-nm %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=2 %s +# RUN: not --crash llvm-nm %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=2 %s ## b) Broken .dynsym symbol table. Valid .symtab symbol table. # RUN: yaml2obj -DBITS=32 -DSIZE=32 -DDYNSIZE=33 %s -o %t.32-bit.o -# RUN: not llvm-nm --dynamic %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=3 %s +# RUN: not --crash llvm-nm --dynamic %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=3 %s # RUN: yaml2obj -DBITS=64 -DSIZE=48 -DDYNSIZE=49 %s -o %t.64-bit.o -# RUN: not llvm-nm --dynamic %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=3 %s +# RUN: not --crash llvm-nm --dynamic %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=3 %s -# CHECK: error: {{.*}} section [index [[INDEX]]] has an invalid sh_size ([[SIZE]]) which is not a multiple of its sh_entsize ([[SYMSIZE]]) +# CHECK: LLVM ERROR: section [index [[INDEX]]] has an invalid sh_size ([[SIZE]]) which is not a multiple of its sh_entsize ([[SYMSIZE]]) --- !ELF FileHeader: Index: llvm/test/tools/llvm-objdump/invalid-symbol-table-size.test =================================================================== --- llvm/test/tools/llvm-objdump/invalid-symbol-table-size.test +++ llvm/test/tools/llvm-objdump/invalid-symbol-table-size.test @@ -3,17 +3,17 @@ ## a) Broken .symtab symbol table. Valid .dynsym symbol table. # RUN: yaml2obj -DBITS=32 -DSIZE=33 -DDYNSIZE=32 %s -o %t.32-bit.o -# RUN: not llvm-objdump --syms %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=2 %s +# RUN: not --crash llvm-objdump --syms %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=2 %s # RUN: yaml2obj -DBITS=64 -DSIZE=49 -DDYNSIZE=48 %s -o %t.64-bit.o -# RUN: not llvm-objdump --syms %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=2 %s +# RUN: not --crash llvm-objdump --syms %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=2 %s ## b) Broken .dynsym symbol table. Valid .symtab symbol table. # RUN: yaml2obj -DBITS=32 -DSIZE=32 -DDYNSIZE=33 %s -o %t.32-bit.o -# RUN: not llvm-objdump --dynamic-syms %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=3 %s +# RUN: not --crash llvm-objdump --dynamic-syms %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 -DINDEX=3 %s # RUN: yaml2obj -DBITS=64 -DSIZE=48 -DDYNSIZE=49 %s -o %t.64-bit.o -# RUN: not llvm-objdump --dynamic-syms %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=3 %s +# RUN: not --crash llvm-objdump --dynamic-syms %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 -DINDEX=3 %s -# CHECK: error: {{.*}} section [index [[INDEX]]] has an invalid sh_size ([[SIZE]]) which is not a multiple of its sh_entsize ([[SYMSIZE]]) +# CHECK: LLVM ERROR: section [index [[INDEX]]] has an invalid sh_size ([[SIZE]]) which is not a multiple of its sh_entsize ([[SYMSIZE]]) --- !ELF FileHeader: Index: llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test +++ llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test @@ -47,7 +47,7 @@ # RUN: llvm-readelf %t2.o --cg-profile | FileCheck %s --check-prefix=GNU # LLVM-ERR: CGProfile [ -# LLVM-ERR-NEXT: warning: '[[FILE]]': unable to dump the SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 1] has an invalid sh_entsize: 15 +# LLVM-ERR-NEXT: warning: '[[FILE]]': unable to dump the SHT_LLVM_CALL_GRAPH_PROFILE section: section [index 1] has invalid sh_entsize: expected 16, but got 15 # LLVM-ERR-NEXT: ] ## Check we report a warning when unable to dump a name of a symbol. Index: llvm/test/tools/llvm-readobj/ELF/reloc-symbol-with-versioning.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/reloc-symbol-with-versioning.test +++ llvm/test/tools/llvm-readobj/ELF/reloc-symbol-with-versioning.test @@ -1,6 +1,6 @@ # RUN: yaml2obj %s -o %t.o -# RUN: llvm-readobj --demangle -r %t.o | FileCheck %s --check-prefix LLVM -# RUN: llvm-readelf --demangle -r %t.o | FileCheck %s --check-prefix GNU +# RUN: llvm-readobj --demangle -r %t.o 2>&1 | FileCheck %s --check-prefix LLVM +# RUN: llvm-readelf --demangle -r %t.o 2>&1 | FileCheck %s --check-prefix GNU # GNU: Relocation section '.rela.plt' at offset {{.*}} contains 5 entries: # GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend @@ -30,7 +30,7 @@ - Name: .gnu.version Type: SHT_GNU_versym Flags: [ SHF_ALLOC ] - Entries: [ 0, 2, 3, 4, 2 ] + Entries: [ 0, 2, 3, 4, 2, 1 ] - Name: .gnu.version_r Type: SHT_GNU_verneed Flags: [ SHF_ALLOC ] Index: llvm/test/tools/llvm-readobj/ELF/relocation-errors.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/relocation-errors.test +++ llvm/test/tools/llvm-readobj/ELF/relocation-errors.test @@ -6,8 +6,8 @@ # LLVM: Relocations [ # LLVM-NEXT: Section (3) .rel.text { -# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 1 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: offset goes past the end of file -# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 2 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: offset goes past the end of file +# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 1 in SHT_REL section with index 3: unable to read an entry with index 4278124286 from SHT_SYMTAB section with index 6: can't read an entry at 0x17e7e7e7d0: it goes past the end of the section (0x90) +# LLVM-NEXT: warning: '[[FILE]]': unable to print relocation 2 in SHT_REL section with index 3: unable to read an entry with index 4278124286 from SHT_SYMTAB section with index 6: can't read an entry at 0x17e7e7e7d0: it goes past the end of the section (0x90) # LLVM-NEXT: 0x2 R_X86_64_NONE -{{$}} # LLVM-NEXT: 0x3 R_X86_64_NONE .sec.symbol1{{$}} # LLVM-NEXT: warning: '[[FILE]]': invalid section index: 255 @@ -23,8 +23,8 @@ # GNU: Relocation section '.rel.text' at offset 0x41 contains 7 entries: # GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name -# GNU-NEXT: warning: '[[FILE]]': unable to print relocation 1 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: offset goes past the end of file -# GNU-NEXT: warning: '[[FILE]]': unable to print relocation 2 in SHT_REL section with index 3: unable to access section [index 6] data at 0x17e7e7e8d0: offset goes past the end of file +# GNU-NEXT: warning: '[[FILE]]': unable to print relocation 1 in SHT_REL section with index 3: unable to read an entry with index 4278124286 from SHT_SYMTAB section with index 6: can't read an entry at 0x17e7e7e7d0: it goes past the end of the section (0x90) +# GNU-NEXT: warning: '[[FILE]]': unable to print relocation 2 in SHT_REL section with index 3: unable to read an entry with index 4278124286 from SHT_SYMTAB section with index 6: can't read an entry at 0x17e7e7e7d0: it goes past the end of the section (0x90) # GNU-NEXT: 0000000000000002 0000000000000000 R_X86_64_NONE # GNU-NEXT: 0000000000000003 0000000200000000 R_X86_64_NONE 0000000000000000 .sec.symbol1 # GNU-NEXT: warning: '[[FILE]]': invalid section index: 255 Index: llvm/test/tools/llvm-readobj/ELF/relocations.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/relocations.test +++ llvm/test/tools/llvm-readobj/ELF/relocations.test @@ -207,7 +207,7 @@ # BROKEN-REL-LLVM: Relocations [ # BROKEN-REL-LLVM-NEXT: Section (2) .rel.text { -# BROKEN-REL-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_REL section with index 2: section [index 2] has an invalid sh_entsize: 1 +# BROKEN-REL-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_REL section with index 2: section [index 2] has invalid sh_entsize: expected 16, but got 1 # BROKEN-REL-LLVM-NEXT: } # BROKEN-REL-LLVM-NEXT: Section (3) .rela.text { # BROKEN-REL-LLVM-NEXT: 0x0 R_X86_64_NONE rela_0 0x0 @@ -220,7 +220,7 @@ # BROKEN-REL-GNU: Relocation section '.rel.text' at offset 0x51 contains 64 entries: # BROKEN-REL-GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name -# BROKEN-REL-GNU-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_REL section with index 2: section [index 2] has an invalid sh_entsize: 1 +# BROKEN-REL-GNU-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_REL section with index 2: section [index 2] has invalid sh_entsize: expected 16, but got 1 # BROKEN-REL-GNU: Relocation section '.rela.text' at offset 0x91 contains 5 entries: # BROKEN-REL-GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name # BROKEN-REL-GNU-NEXT: 0000000000000000 0000000500000000 R_X86_64_NONE 0000000000000000 rela_0 + 0 @@ -245,7 +245,7 @@ # BROKEN-RELA-LLVM-NEXT: 0x9 R_X86_64_64 rel_64{{$}} # BROKEN-RELA-LLVM-NEXT: } # BROKEN-RELA-LLVM-NEXT: Section (3) .rela.text { -# BROKEN-RELA-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_RELA section with index 3: section [index 3] has an invalid sh_entsize: 1 +# BROKEN-RELA-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_RELA section with index 3: section [index 3] has invalid sh_entsize: expected 24, but got 1 # BROKEN-RELA-LLVM-NEXT: } # BROKEN-RELA-LLVM-NEXT: ] @@ -258,7 +258,7 @@ # BROKEN-RELA-GNU-EMPTY: # BROKEN-RELA-GNU-NEXT: Relocation section '.rela.text' at offset 0x91 contains 120 entries: # BROKEN-RELA-GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend -# BROKEN-RELA-GNU-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_RELA section with index 3: section [index 3] has an invalid sh_entsize: 1 +# BROKEN-RELA-GNU-NEXT: warning: '[[FILE]]': unable to read relocations from SHT_RELA section with index 3: section [index 3] has invalid sh_entsize: expected 24, but got 1 ## Case C: check the case when relocations can't be read from SHT_REL/SHT_RELA sections ## because of broken sh_link fields. @@ -498,3 +498,45 @@ # GNU-SHNAME-NEXT: 0000000000000005 0000000700000004 R_X86_64_PLT32 0000000000000002 rela_pos + 2 # GNU-SHNAME-NEXT: ffffffffffffffff 0000000800000001 R_X86_64_64 0000000000000003 rela_minneg - 8000000000000000 # GNU-SHNAME-NEXT: 0000000000000009 000000090000000b R_X86_64_32S ffffffffffffffff rela_maxpos + 7fffffffffffffff + +## Check that we report a warning when a relocation refers to a symbol +## that is not exist in the symbol table. + +# RUN: yaml2obj %s --docnum=3 -o %t3 +# RUN: llvm-readobj --relocs %t3 2>&1 | \ +# RUN: FileCheck %s --implicit-check-not=warning: -DFILE=%t3 --check-prefix=LLVM-SYMNDX +# RUN: llvm-readelf --relocs %t3 2>&1 | \ +# RUN: FileCheck %s --implicit-check-not=warning: -DFILE=%t3 --check-prefix=GNU-SYMNDX + +# LLVM-SYMNDX: Relocations [ +# LLVM-SYMNDX-NEXT: Section (1) .rela.text { +# LLVM-SYMNDX-NEXT: 0x0 R_X86_64_NONE - 0x0 +# LLVM-SYMNDX-NEXT: 0x0 R_X86_64_NONE foo 0x0 +# LLVM-SYMNDX-NEXT: warning: '[[FILE]]': unable to print relocation 3 in SHT_RELA section with index 1: unable to read an entry with index 2 from SHT_SYMTAB section with index 2: can't read an entry at 0x30: it goes past the end of the section (0x30) +# LLVM-SYMNDX-NEXT: } +# LLVM-SYMNDX-NEXT: ] + +# GNU-SYMNDX: Relocation section '.rela.text' at offset 0x40 contains 3 entries: +# GNU-SYMNDX-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend +# GNU-SYMNDX-NEXT: 0000000000000000 0000000000000000 R_X86_64_NONE 0 +# GNU-SYMNDX-NEXT: 0000000000000000 0000000100000000 R_X86_64_NONE 0000000000000000 foo + 0 +# GNU-SYMNDX-NEXT: warning: '[[FILE]]': unable to print relocation 3 in SHT_RELA section with index 1: unable to read an entry with index 2 from SHT_SYMTAB section with index 2: can't read an entry at 0x30: it goes past the end of the section (0x30) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .rela.text + Type: SHT_RELA + Relocations: + - Type: R_X86_64_NONE + Symbol: 0x0 + - Type: R_X86_64_NONE + Symbol: 0x1 + - Type: R_X86_64_NONE + Symbol: 0x2 +Symbols: + - Name: foo Index: llvm/test/tools/llvm-readobj/ELF/relr-relocs.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/relr-relocs.test +++ llvm/test/tools/llvm-readobj/ELF/relr-relocs.test @@ -169,14 +169,14 @@ # BROKEN-LLVM: Relocations [ # BROKEN-LLVM-NEXT: Section (1) .relr.dyn { -# BROKEN-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from [[SECNAME]] section with index 1: section [index 1] has an invalid sh_entsize: 1 +# BROKEN-LLVM-NEXT: warning: '[[FILE]]': unable to read relocations from [[SECNAME]] section with index 1: section [index 1] has invalid sh_entsize: expected 4, but got 1 # BROKEN-LLVM-NEXT: } # BROKEN-LLVM-NEXT: ] -# BROKEN-GNU: warning: '[[FILE]]': unable to get the number of relocations in [[SECNAME]] section with index 1: section [index 1] has an invalid sh_entsize: 1 +# BROKEN-GNU: warning: '[[FILE]]': unable to get the number of relocations in [[SECNAME]] section with index 1: section [index 1] has invalid sh_entsize: expected 4, but got 1 # BROKEN-GNU: Relocation section '.relr.dyn' at offset 0x34 contains entries: # BROKEN-GNU-NEXT: Offset Info Type Sym. Value Symbol's Name -# BROKEN-GNU-NEXT: warning: '[[FILE]]': unable to read relocations from [[SECNAME]] section with index 1: section [index 1] has an invalid sh_entsize: 1 +# BROKEN-GNU-NEXT: warning: '[[FILE]]': unable to read relocations from [[SECNAME]] section with index 1: section [index 1] has invalid sh_entsize: expected 4, but got 1 ## Case B: check the case when relocations can't be read from an SHT_ANDROID_RELR section. ## SHT_ANDROID_RELR = 0x6fffff00. Index: llvm/test/tools/llvm-readobj/ELF/stack-sizes.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/stack-sizes.test +++ llvm/test/tools/llvm-readobj/ELF/stack-sizes.test @@ -106,30 +106,30 @@ # SYMTAB-GNU: Stack Sizes: # SYMTAB-GNU-NEXT: Size Function -# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 5: unable to access section [index 7] data at 0xffffef36: offset goes past the end of file +# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 5: unable to read an entry with index 3 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to read the symbol table: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 3 # SYMTAB-GNU-NEXT: 16 ? -# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 5: unable to access section [index 7] data at 0xffffef1e: offset goes past the end of file +# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 5: unable to read an entry with index 2 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-GNU-NEXT: 32 ? -# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 6: unable to access section [index 7] data at 0xffffef06: offset goes past the end of file +# SYMTAB-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 6: unable to read an entry with index 1 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 4 # SYMTAB-GNU-NEXT: 8 ? # SYMTAB-LLVM: StackSizes [ -# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 5: unable to access section [index 7] data at 0xffffef36: offset goes past the end of file +# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 5: unable to read an entry with index 3 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to read the symbol table: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 3 # SYMTAB-LLVM-NEXT: Entry { # SYMTAB-LLVM-NEXT: Function: ? # SYMTAB-LLVM-NEXT: Size: 0x10 # SYMTAB-LLVM-NEXT: } -# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 5: unable to access section [index 7] data at 0xffffef1e: offset goes past the end of file +# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 5: unable to read an entry with index 2 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-LLVM-NEXT: Entry { # SYMTAB-LLVM-NEXT: Function: ? # SYMTAB-LLVM-NEXT: Size: 0x20 # SYMTAB-LLVM-NEXT: } -# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 6: unable to access section [index 7] data at 0xffffef06: offset goes past the end of file +# SYMTAB-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 1 in SHT_RELA section with index 6: unable to read an entry with index 1 from SHT_SYMTAB section with index 7: section [index 7] has a sh_offset (0xffffeeee) + sh_size (0x78) that is greater than the file size (0x450) # SYMTAB-LLVM-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 4 # SYMTAB-LLVM-NEXT: Entry { # SYMTAB-LLVM-NEXT: Function: ? @@ -489,9 +489,9 @@ # BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get address of symbol '_Z3foof': invalid section index: 10 # BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': could not identify function symbol for stack size entry in SHT_PROGBITS section with index 2 # BADSECTION-OUT-GNU-NEXT: 8 ? -# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 3: unable to access section [index 4] data at 0x18a0: offset goes past the end of file +# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30) # BADSECTION-OUT-GNU-NEXT: 22 ? -# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 3 in SHT_RELA section with index 3: unable to access section [index 4] data at 0x18a0: offset goes past the end of file +# BADSECTION-OUT-GNU-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 3 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30) # BADSECTION-OUT-GNU-NEXT: 36 ? # BADSECTION-OUT-LLVM: StackSizes [ @@ -502,12 +502,12 @@ # BADSECTION-OUT-LLVM-NEXT: Function: ? # BADSECTION-OUT-LLVM-NEXT: Size: 0x8 # BADSECTION-OUT-LLVM-NEXT: } -# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 3: unable to access section [index 4] data at 0x18a0: offset goes past the end of file +# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 2 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30) # BADSECTION-OUT-LLVM-NEXT: Entry { # BADSECTION-OUT-LLVM-NEXT: Function: ? # BADSECTION-OUT-LLVM-NEXT: Size: 0x16 # BADSECTION-OUT-LLVM-NEXT: } -# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 3 in SHT_RELA section with index 3: unable to access section [index 4] data at 0x18a0: offset goes past the end of file +# BADSECTION-OUT-LLVM-NEXT: warning: '[[FILE]]': unable to get the target of relocation with index 3 in SHT_RELA section with index 3: unable to read an entry with index 255 from SHT_SYMTAB section with index 4: can't read an entry at 0x17e8: it goes past the end of the section (0x30) # BADSECTION-OUT-LLVM-NEXT: Entry { # BADSECTION-OUT-LLVM-NEXT: Function: ? # BADSECTION-OUT-LLVM-NEXT: Size: 0x24 Index: llvm/test/tools/llvm-readobj/ELF/symbols.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/symbols.test +++ llvm/test/tools/llvm-readobj/ELF/symbols.test @@ -191,10 +191,10 @@ # RUN: FileCheck %s -DFILE=%t64.err2 --check-prefix=SYMTAB-ENTSIZE-ERR-GNU # SYMTAB-ENTSIZE-ERR-LLVM: Symbols [ -# SYMTAB-ENTSIZE-ERR-LLVM-NEXT: warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has an invalid sh_entsize: 255 +# SYMTAB-ENTSIZE-ERR-LLVM-NEXT: warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has invalid sh_entsize: expected 24, but got 255 # SYMTAB-ENTSIZE-ERR-LLVM: ] -# SYMTAB-ENTSIZE-ERR-GNU: warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has an invalid sh_entsize: 255 +# SYMTAB-ENTSIZE-ERR-GNU: warning: '[[FILE]]': unable to read symbols from the SHT_SYMTAB section: section [index 1] has invalid sh_entsize: expected 24, but got 255 # SYMTAB-ENTSIZE-ERR-GNU-NOT: {{.}} ## Case 3: check we report a warning, but continue dumping, when we are unable to read the name of the SHT_SYMTAB section. Index: llvm/test/tools/llvm-readobj/ELF/versym-invalid.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/versym-invalid.test +++ llvm/test/tools/llvm-readobj/ELF/versym-invalid.test @@ -150,7 +150,7 @@ # INVALID-ENT-SIZE-GNU-NEXT: 1: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND foo@ # INVALID-ENT-SIZE-GNU: Version symbols section '.gnu.version' contains 1 entries: # INVALID-ENT-SIZE-GNU-NEXT: Addr: 0000000000000000 Offset: 0x000040 Link: 0 () -# INVALID-ENT-SIZE-GNU-NEXT: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has an invalid sh_entsize: 3 +# INVALID-ENT-SIZE-GNU-NEXT: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has invalid sh_entsize: expected 2, but got 3 # INVALID-ENT-SIZE-LLVM: DynamicSymbols [ # INVALID-ENT-SIZE-LLVM-NEXT: warning: '[[FILE]]': section [index 1] has invalid sh_entsize: expected 2, but got 3 @@ -174,7 +174,7 @@ # INVALID-ENT-SIZE-LLVM-NEXT: } # INVALID-ENT-SIZE-LLVM-NEXT: ] # INVALID-ENT-SIZE-LLVM: VersionSymbols [ -# INVALID-ENT-SIZE-LLVM-NEXT: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has an invalid sh_entsize: 3 +# INVALID-ENT-SIZE-LLVM-NEXT: warning: '[[FILE]]': cannot read content of SHT_GNU_versym section with index 1: section [index 1] has invalid sh_entsize: expected 2, but got 3 # INVALID-ENT-SIZE-LLVM-NEXT: ] --- !ELF Index: llvm/test/tools/llvm-size/invalid-symbol-table-size.test =================================================================== --- llvm/test/tools/llvm-size/invalid-symbol-table-size.test +++ llvm/test/tools/llvm-size/invalid-symbol-table-size.test @@ -3,15 +3,15 @@ ## a) Test sysv output format. # RUN: yaml2obj -DBITS=32 -DSIZE=33 -DDYNSIZE=32 %s -o %t.32-bit.o -# RUN: not llvm-size --common --format=sysv %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 %s +# RUN: not --crash llvm-size --common --format=sysv %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 %s # RUN: yaml2obj -DBITS=64 -DSIZE=49 -DDYNSIZE=48 %s -o %t.64-bit.o -# RUN: not llvm-size --common --format=sysv %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 %s +# RUN: not --crash llvm-size --common --format=sysv %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 %s ## b) Test berkeley output format. -# RUN: not llvm-size --common --format=berkeley %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 %s -# RUN: not llvm-size --common --format=berkeley %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 %s +# RUN: not --crash llvm-size --common --format=berkeley %t.32-bit.o 2>&1 | FileCheck -DSIZE=33 -DSYMSIZE=16 %s +# RUN: not --crash llvm-size --common --format=berkeley %t.64-bit.o 2>&1 | FileCheck -DSIZE=49 -DSYMSIZE=24 %s -# CHECK: error: {{.*}} section [index 1] has an invalid sh_size ([[SIZE]]) which is not a multiple of its sh_entsize ([[SYMSIZE]]) +# CHECK: LLVM ERROR: section [index 1] has an invalid sh_size ([[SIZE]]) which is not a multiple of its sh_entsize ([[SYMSIZE]]) --- !ELF FileHeader: Index: llvm/test/tools/obj2yaml/ELF/dynamic-section.yaml =================================================================== --- llvm/test/tools/obj2yaml/ELF/dynamic-section.yaml +++ llvm/test/tools/obj2yaml/ELF/dynamic-section.yaml @@ -255,4 +255,4 @@ # RUN: yaml2obj -DENTSIZE=0xff %s -o %t2 # RUN: not obj2yaml %t2 2>&1 | FileCheck %s --check-prefix=ENTSIZE -# ENTSIZE: section [index 1] has an invalid sh_entsize: 255 +# ENTSIZE: section [index 1] has invalid sh_entsize: expected 16, but got 255 Index: llvm/test/tools/obj2yaml/ELF/rel-rela-section.yaml =================================================================== --- llvm/test/tools/obj2yaml/ELF/rel-rela-section.yaml +++ llvm/test/tools/obj2yaml/ELF/rel-rela-section.yaml @@ -30,11 +30,12 @@ ## Here we use the 0xFE value instead of expected 0x18/0x10. # RUN: yaml2obj -DTYPE=SHT_RELA --docnum=2 %s -o %t2.rela -# RUN: not obj2yaml %t2.rela 2>&1 | FileCheck %s --check-prefix=ERR +# RUN: not obj2yaml %t2.rela 2>&1 | FileCheck %s --check-prefix=ERR1 # RUN: yaml2obj -DTYPE=SHT_REL --docnum=2 %s -o %t2.rel -# RUN: not obj2yaml %t2.rel 2>&1 | FileCheck %s --check-prefix=ERR +# RUN: not obj2yaml %t2.rel 2>&1 | FileCheck %s --check-prefix=ERR2 -# ERR: section [index 1] has an invalid sh_entsize: 254 +# ERR1: section [index 1] has invalid sh_entsize: expected 24, but got 254 +# ERR2: section [index 1] has invalid sh_entsize: expected 16, but got 254 --- !ELF FileHeader: Index: llvm/test/tools/yaml2obj/ELF/sht-symtab-shndx.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/sht-symtab-shndx.yaml +++ llvm/test/tools/yaml2obj/ELF/sht-symtab-shndx.yaml @@ -109,7 +109,7 @@ # RUN: yaml2obj --docnum=5 %s -o %t5 # RUN: llvm-readelf -S 2>&1 %t5 | FileCheck %s -DFILE=%t5 --check-prefix=CASE5 -# CASE5: warning: '[[FILE]]': section [index 1] has an invalid sh_entsize: 2 +# CASE5: warning: '[[FILE]]': section [index 1] has invalid sh_entsize: expected 4, but got 2 --- !ELF FileHeader: Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -1119,7 +1119,9 @@ Expected SymOrErr = Obj.template getEntry(*SymTab, R.Symbol); if (!SymOrErr) - return SymOrErr.takeError(); + return createError("unable to read an entry with index " + Twine(R.Symbol) + + " from " + describe(*SymTab) + ": " + + toString(SymOrErr.takeError())); const Elf_Sym *Sym = *SymOrErr; if (!Sym) return RelSymbol(nullptr, ""); @@ -6211,9 +6213,8 @@ OS << " Address Initial Sym.Val. Type Ndx Name\n"; for (auto &E : Parser.getPltEntries()) { const Elf_Sym &Sym = *Parser.getPltSym(&E); - const Elf_Sym &FirstSym = - *cantFail(this->Obj.template getEntry( - *Parser.getPltSymTable(), 0)); + const Elf_Sym &FirstSym = *cantFail( + this->Obj.template getEntry(*Parser.getPltSymTable(), 0)); std::string SymName = this->dumper().getFullSymbolName( Sym, &Sym - &FirstSym, this->dumper().getDynamicStringTable(), false); @@ -7078,9 +7079,8 @@ W.printEnum("Type", Sym.getType(), makeArrayRef(ElfSymbolTypes)); printSymbolSection(Sym, &Sym - this->dumper().dynamic_symbols().begin()); - const Elf_Sym *FirstSym = - cantFail(this->Obj.template getEntry( - *Parser.getPltSymTable(), 0)); + const Elf_Sym *FirstSym = cantFail( + this->Obj.template getEntry(*Parser.getPltSymTable(), 0)); std::string SymName = this->dumper().getFullSymbolName( Sym, &Sym - FirstSym, Parser.getPltStrTable(), true); W.printNumber("Name", SymName, Sym.st_name); Index: llvm/unittests/Object/ELFObjectFileTest.cpp =================================================================== --- llvm/unittests/Object/ELFObjectFileTest.cpp +++ llvm/unittests/Object/ELFObjectFileTest.cpp @@ -328,3 +328,33 @@ ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded()); } + +// This is a test for ELFFile::getEntry() API. +TEST(ELFObjectFileTest, InvalidEntryIndexTest) { + SmallString<0> Storage; + Expected> ElfOrErr = toBinary(Storage, R"( +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + EntSize: 0x1 + Size: 0x1 +)"); + + ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded()); + const ELFFile &Elf = ElfOrErr->getELFFile(); + + Expected SecOrErr = Elf.getSection(1); + if (!SecOrErr) + GTEST_FAIL() << toString(SecOrErr.takeError()); + + EXPECT_THAT_EXPECTED(Elf.getEntry(**SecOrErr, 0), Succeeded()); + EXPECT_THAT_ERROR(Elf.getEntry(**SecOrErr, 1).takeError(), + FailedWithMessage("can't read an entry at 0x1: it goes " + "past the end of the section (0x1)")); +}