Index: llvm/include/llvm/Object/ELF.h =================================================================== --- llvm/include/llvm/Object/ELF.h +++ llvm/include/llvm/Object/ELF.h @@ -670,7 +670,6 @@ Elf_Shdr_Range Sections) const { if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM) - // TODO: this error is untested. return createError( "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM"); auto SectionOrErr = object::getSection(Sections, Sec.sh_link); Index: llvm/test/Object/invalid.test =================================================================== --- llvm/test/Object/invalid.test +++ llvm/test/Object/invalid.test @@ -312,30 +312,6 @@ Machine: EM_X86_64 SHNum: 0xFF -## Check that llvm-readobj reports an error if a relocation contains an -## incorrect (too large) symbol index. - -# RUN: yaml2obj %s --docnum=16 -o %t16 -# RUN: not llvm-readobj -r %t16 2>&1 | FileCheck -DFILE=%t16 --check-prefix=INVALID-REL-SYM %s - -# INVALID-REL-SYM: error: '[[FILE]]': unable to access section [index 2] data at 0x18000040: offset goes past the end of file - ---- !ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_REL - Machine: EM_X86_64 -Sections: - - Name: .rela.text - Type: SHT_RELA - Info: 0 - Relocations: - - Offset: 0x0 - Type: R_X86_64_64 - Symbol: 0xFFFFFF -Symbols: [] - ## Check llvm-readobj does not crash on a truncated ELF. ## Create a truncated ELF object with ELFCLASSNONE class using echo. @@ -352,8 +328,8 @@ ## Check that llvm-readobj reports an error if section name offset ## overflows the section name string table. -# RUN: yaml2obj %s --docnum=17 -o %t17 -# RUN: llvm-readobj --sections %t17 2>&1 | FileCheck -DFILE=%t17 --check-prefix=BROKEN-SECNAME %s +# RUN: yaml2obj %s --docnum=16 -o %t16 +# RUN: llvm-readobj --sections %t16 2>&1 | FileCheck -DFILE=%t16 --check-prefix=BROKEN-SECNAME %s ## BROKEN-SECNAME: warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0xb) offset which goes past the end of the section name string table @@ -371,9 +347,9 @@ ## Check that llvm-readobj reports an error if a section has a broken offset ## that goes past the end of the file. -# RUN: yaml2obj %s --docnum=18 -o %t18 -# RUN: not llvm-readobj --sections --section-data %t18 2>&1 \ -# RUN: | FileCheck -DFILE=%t18 --check-prefix=BROKEN-SECSHOFFSET %s +# RUN: yaml2obj %s --docnum=17 -o %t17 +# RUN: not llvm-readobj --sections --section-data %t17 2>&1 \ +# RUN: | FileCheck -DFILE=%t17 --check-prefix=BROKEN-SECSHOFFSET %s # BROKEN-SECSHOFFSET: error: '[[FILE]]': section [index 1] has a sh_offset (0xffff0000) + sh_size (0x0) that is greater than the file size (0x160) @@ -391,8 +367,8 @@ ## Check that llvm-readobj reports an error if symbol name ## offset goes past the end of the symbol string table. -# RUN: yaml2obj %s --docnum=19 -o %t19 -# RUN: not llvm-readobj --symbols %t19 2>&1 | FileCheck -DFILE=%t19 --check-prefix=INVALID-SYM-NAME %s +# RUN: yaml2obj %s --docnum=18 -o %t18 +# RUN: not llvm-readobj --symbols %t18 2>&1 | FileCheck -DFILE=%t18 --check-prefix=INVALID-SYM-NAME %s # INVALID-SYM-NAME: error: '[[FILE]]': st_name (0x1) is past the end of the string table of size 0x1 @@ -412,8 +388,8 @@ ## Version index in .gnu.version overflows the version map. ## Check llvm-readobj reports it. -# RUN: yaml2obj %s --docnum=20 -o %t20 -# RUN: llvm-readobj -dt %t20 2>&1 | FileCheck -DFILE=%t20 --check-prefix=INVALID-VERSION %s +# RUN: yaml2obj %s --docnum=19 -o %t19 +# RUN: llvm-readobj -dt %t19 2>&1 | FileCheck -DFILE=%t19 --check-prefix=INVALID-VERSION %s # INVALID-VERSION: warning: '[[FILE]]': SHT_GNU_versym section refers to a version index 255 which is missing @@ -441,8 +417,8 @@ ## The dynamic table contains DT_STRTAB with a value that is not in any loadable segment. ## Check llvm-readobj reports it. -# RUN: yaml2obj %s --docnum=21 -o %t21 -# RUN: llvm-readobj --dynamic-table %t21 2>&1 | FileCheck -DFILE=%t21 --check-prefix=INVALID-DTSTRTAB %s +# RUN: yaml2obj %s --docnum=20 -o %t20 +# RUN: llvm-readobj --dynamic-table %t20 2>&1 | FileCheck -DFILE=%t20 --check-prefix=INVALID-DTSTRTAB %s # INVALID-DTSTRTAB: warning: '[[FILE]]': Unable to parse DT_STRTAB: virtual address is not in any segment: 0xffff0000 @@ -479,8 +455,8 @@ ## Check that llvm-readobj reports a warning when .dynamic section has an invalid ## size, which isn't a multiple of the dynamic entry size. -# RUN: yaml2obj %s --docnum=22 -o %t22 -# RUN: llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck -DFILE=%t22 --check-prefix=DYN-TABLE-SIZE %s +# RUN: yaml2obj %s --docnum=21 -o %t21 +# RUN: llvm-readobj --dyn-relocations %t21 2>&1 | FileCheck -DFILE=%t21 --check-prefix=DYN-TABLE-SIZE %s # DYN-TABLE-SIZE: warning: '[[FILE]]': section with index 1 has invalid size (0x1){{$}} @@ -498,8 +474,8 @@ ## PT_DYNAMIC's p_offset field is so large that p_offset + p_filesz is larger ## than the object size. Check llvm-readobj reports it. -# RUN: yaml2obj %s --docnum=23 -o %t23 -# RUN: llvm-readobj --dyn-relocations %t23 2>&1 | FileCheck -DFILE=%t23 --check-prefix=DYN-TABLE-PHDR %s +# RUN: yaml2obj %s --docnum=22 -o %t22 +# RUN: llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck -DFILE=%t22 --check-prefix=DYN-TABLE-PHDR %s # DYN-TABLE-PHDR: warning: '[[FILE]]': PT_DYNAMIC segment offset + size exceeds the size of the file @@ -524,9 +500,9 @@ ## PT_DYNAMIC's p_filesz field is so large that p_offset + p_filesz is larger ## than the object size. Check llvm-readobj reports it. -# RUN: yaml2obj %s --docnum=24 -o %t24 -# RUN: llvm-readobj --dyn-relocations %t24 2>&1 \ -# RUN: | FileCheck -DFILE=%t24 --check-prefix=DYN-TABLE-PHDR %s +# RUN: yaml2obj %s --docnum=23 -o %t23 +# RUN: llvm-readobj --dyn-relocations %t23 2>&1 \ +# RUN: | FileCheck -DFILE=%t23 --check-prefix=DYN-TABLE-PHDR %s --- !ELF FileHeader: @@ -546,8 +522,8 @@ Sections: - Section: .dynamic -# RUN: yaml2obj --docnum=25 %s -o %t25 -# RUN: not obj2yaml 2>&1 %t25 | FileCheck %s -DFILE=%t25 --check-prefix=INVALID-SHSTRNDX +# RUN: yaml2obj --docnum=24 %s -o %t24 +# RUN: not obj2yaml 2>&1 %t24 | FileCheck %s -DFILE=%t24 --check-prefix=INVALID-SHSTRNDX # INVALID-SHSTRNDX: Error reading file: [[FILE]]: section header string table index 255 does not exist @@ -566,8 +542,8 @@ ## is greater than UINT64_MAX / sizeof(Elf_Shdr) == 288230376151711743. ## Here we check that do not crash on a border value. -# RUN: yaml2obj --docnum=26 %s -o %t26 -# RUN: not llvm-readobj -h %t26 2>&1 | FileCheck -DFILE=%t26 --check-prefix=INVALID-SEC-NUM1 %s +# RUN: yaml2obj --docnum=25 %s -o %t25 +# RUN: not llvm-readobj -h %t25 2>&1 | FileCheck -DFILE=%t25 --check-prefix=INVALID-SEC-NUM1 %s # INVALID-SEC-NUM1: error: '[[FILE]]': invalid section header table offset (e_shoff = 0x58) or invalid number of sections specified in the first section header's sh_size field (0x3ffffffffffffff) @@ -585,8 +561,8 @@ ## See above, but now we test the UINT64_MAX / sizeof(Elf_Shdr) value. ## The error is slightly different in this case. -# RUN: yaml2obj --docnum=27 %s -o %t27 -# RUN: not llvm-readobj -h %t27 2>&1 | FileCheck -DFILE=%t27 --check-prefix=INVALID-SEC-NUM2 %s +# RUN: yaml2obj --docnum=26 %s -o %t26 +# RUN: not llvm-readobj -h %t26 2>&1 | FileCheck -DFILE=%t26 --check-prefix=INVALID-SEC-NUM2 %s # INVALID-SEC-NUM2: error: '[[FILE]]': invalid number of sections specified in the NULL section's sh_size field (288230376151711744) @@ -603,8 +579,8 @@ ## Check the case when SHOff is too large. SHOff + sizeof(Elf_Shdr) overflows the uint64 type. -# RUN: yaml2obj --docnum=28 %s -o %t28 -# RUN: not llvm-readobj -h %t28 2>&1 | FileCheck -DFILE=%t28 --check-prefix=INVALID-SEC-NUM3 %s +# RUN: yaml2obj --docnum=27 %s -o %t27 +# RUN: not llvm-readobj -h %t27 2>&1 | FileCheck -DFILE=%t27 --check-prefix=INVALID-SEC-NUM3 %s # INVALID-SEC-NUM3: error: '[[FILE]]': section header table goes past the end of the file: e_shoff = 0xffffffffffffffff @@ -619,8 +595,8 @@ ## Check that llvm-objdump reports an error when it tries to dump a ## symbol name and .strtab is empty. -# RUN: yaml2obj %s --docnum=29 -o %t29 -# RUN: not llvm-objdump -syms %t29 2>&1 | FileCheck -DFILE=%t29 --check-prefix=STRTAB-EMPTY2 %s +# RUN: yaml2obj %s --docnum=28 -o %t28 +# RUN: not llvm-objdump -syms %t28 2>&1 | FileCheck -DFILE=%t28 --check-prefix=STRTAB-EMPTY2 %s # STRTAB-EMPTY2: error: '[[FILE]]': SHT_STRTAB string table section [index 1] is empty @@ -640,8 +616,8 @@ ## Check that we report a warning if SHT_GNU_versym has invalid ## sh_entsize value (3 instead of 2) when trying to access the entries. -# RUN: yaml2obj %s --docnum=30 -o %t30 -# RUN: llvm-readobj -V %t30 2>&1 | FileCheck -DFILE=%t30 --check-prefix=INVALID-VER-SHENTSIZE %s +# 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 @@ -666,8 +642,8 @@ ## Check the case when e_shstrndx == SHN_XINDEX, but null section's sh_link contains ## the index of a section header string table that is larger than the number of the sections. -# RUN: yaml2obj --docnum=31 %s -o %t31 -# RUN: not llvm-objcopy %t31 2>&1 | FileCheck %s -DFILE=%t31 --check-prefix=INVALID-SHSTRTAB-INDEX +# RUN: yaml2obj --docnum=30 %s -o %t30 +# RUN: not llvm-objcopy %t30 2>&1 | FileCheck %s -DFILE=%t30 --check-prefix=INVALID-SHSTRTAB-INDEX # INVALID-SHSTRTAB-INDEX: error: section header string table index 255 does not exist Index: llvm/test/tools/llvm-readobj/ELF/relocation-errors.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-readobj/ELF/relocation-errors.test @@ -0,0 +1,93 @@ +## Check how do we report warnings when dumping an object with broken relocations. + +# RUN: yaml2obj %s -o %t64 +# RUN: llvm-readobj --relocations %t64 2>&1 | FileCheck %s -DFILE=%t64 --check-prefix=LLVM +# RUN: llvm-readelf --relocations %t64 2>&1 | FileCheck %s -DFILE=%t64 --check-prefix=GNU + +# LLVM: Relocations [ +# LLVM-NEXT: Section (3) .rel.text { +# LLVM-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x0: unable to access section [index 6] data at 0x17e7e7e8b0: offset goes past the end of file +# LLVM-NEXT: 0x2 R_X86_64_NONE - 0x0 +# LLVM-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x3: invalid section index: 255 +# LLVM-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x4: a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table +# LLVM-NEXT: } +# LLVM-NEXT: Section (4) .rela.text { +# LLVM-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x5: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM +# LLVM-NEXT: } +# LLVM-NEXT: ] + +# GNU: Relocation section '.rel.text' at offset 0x41 contains 5 entries: +# GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name +# GNU-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x0: unable to access section [index 6] data at 0x17e7e7e8b0: offset goes past the end of file +# GNU-NEXT: 0000000000000002 0000000000000000 R_X86_64_NONE +# GNU-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x3: invalid section index: 255 +# GNU-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x4: a section [index 2] has an invalid sh_name (0xfefefefe) offset which goes past the end of the section name string table +# GNU-EMPTY: +# GNU-NEXT: Relocation section '.rela.text' at offset 0x91 contains 1 entries: +# GNU-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend +# GNU-NEXT: warning: '[[FILE]]': error dumping the relocation at offset 0x5: invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Content: '00' + - Name: .foo + Type: SHT_PROGBITS + ShName: 0xFEFEFEFE + - Name: .rel.text + Type: SHT_REL + Info: .text + Relocations: +## Case 1: There is no a symbol with index of 0xFEFEFEFE. Also, check +## that warnings we report are uniqified. + - Offset: 0x0 + Symbol: 0xFEFEFEFE + Type: R_X86_64_NONE + - Offset: 0x0 + Symbol: 0xFEFEFEFE + Type: R_X86_64_NONE +## Case 2: Test that no warning is reported for a relocation against a symbol with index 0. + - Offset: 0x2 + Symbol: 0 + Type: R_X86_64_NONE +## Case 3: Test a relocation against a section symbol that has an invalid +## section index (larger than the number of sections). + - Offset: 0x3 + Symbol: .sec.symbol1 + Type: R_X86_64_NONE +## Case 4: Test a relocation against a section symbol that has an invalid +## sh_name offset that goes past the end of the section name string table. + - Offset: 0x4 + Symbol: .sec.symbol2 + Type: R_X86_64_NONE +## Case 5: Test a relocation in the section that is linked to a symbol table that +## has a section type that is not a SHT_SYMTAB or SHT_DYNSYM. +## In this case the code fails to find a corresponding symbol string table. + - Name: .rela.text + Type: SHT_RELA + Info: .text + Link: .fake.symtab + Relocations: + - Offset: 0x5 + Symbol: symbol + Type: R_X86_64_NONE + - Name: .fake.symtab + Type: SHT_PROGBITS + EntSize: 24 + Size: 48 +Symbols: + - Name: symbol + Section: .text + Value: 0 + - Name: .sec.symbol1 + Type: STT_SECTION + Index: 0xFF + - Name: .sec.symbol2 + Type: STT_SECTION + Index: 0x2 Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -3319,11 +3319,15 @@ template void GNUStyle::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab, const Elf_Rela &R, bool IsRela) { - const typename ELFT::Sym *Sym; - std::string Name; - std::tie(Sym, Name) = unwrapOrError( - this->FileName, this->dumper()->getRelocationTarget(SymTab, R)); - printRelocation(Obj, Sym, Name, R, IsRela); + Expected> Target = + this->dumper()->getRelocationTarget(SymTab, R); + if (!Target) + this->reportUniqueWarning(createError( + "error dumping the relocation at offset 0x" + + Twine::utohexstr(R.r_offset) + ": " + toString(Target.takeError()))); + else + printRelocation(Obj, /*Sym=*/Target->first, /*Name=*/Target->second, R, + IsRela); } template @@ -5736,11 +5740,16 @@ template void LLVMStyle::printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab) { - std::string TargetName = - unwrapOrError(this->FileName, - this->dumper()->getRelocationTarget(SymTab, Rel)) - .second; + Expected> Target = + this->dumper()->getRelocationTarget(SymTab, Rel); + if (!Target) { + this->reportUniqueWarning(createError( + "error dumping the relocation at offset 0x" + + Twine::utohexstr(Rel.r_offset) + ": " + toString(Target.takeError()))); + return; + } + std::string TargetName = Target->second; SmallString<32> RelocName; Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);