Index: llvm/test/tools/llvm-readobj/ELF/mips-abiflags.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/mips-abiflags.test +++ llvm/test/tools/llvm-readobj/ELF/mips-abiflags.test @@ -349,6 +349,8 @@ Type: ET_REL Machine: EM_MIPS Sections: + - Type: SHT_NULL + ShName: [[NULLNAME=0x0]] - Name: .MIPS.abiflags Type: SHT_MIPS_ABIFLAGS ISA: MIPS32 @@ -364,3 +366,13 @@ # SIZE-ERR: warning: '[[FILE]]': unable to read the .MIPS.abiflags section: it has a wrong size (23) # SIZE-ERR-NEXT: There is no .MIPS.options section in the file. # SIZE-ERR-NEXT: There is no .reginfo section in the file. + +## Check we report a warning when we are unable to find the .MIPS.abiflags section due to an error. +# RUN: yaml2obj --docnum=3 -DNULLNAME=0xffff %s -o %t.err3 +# RUN: not llvm-readelf -A %t.err3 2>&1 | FileCheck %s -DFILE=%t.err3 --check-prefix=FIND-ERR --implicit-check-not=warning: +# RUN: not llvm-readobj -A %t.err3 2>&1 | FileCheck %s -DFILE=%t.err3 --check-prefix=FIND-ERR --implicit-check-not=warning: + +# FIND-ERR: warning: '[[FILE]]': error locating the .MIPS.abiflags section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: warning: '[[FILE]]': error locating the .MIPS.options section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: warning: '[[FILE]]': error locating the .reginfo section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: error: '[[FILE]]': error locating the .got section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table Index: llvm/test/tools/llvm-readobj/ELF/mips-got.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/mips-got.test +++ llvm/test/tools/llvm-readobj/ELF/mips-got.test @@ -547,3 +547,22 @@ - Tag: DT_PLTGOT Value: [[VAL2=0]] DynamicSymbols: [] + +## Check we report an error when we are unable to find the .got section due to an error. +# RUN: yaml2obj --docnum=3 -DNULLNAME=0xffff %s -o %t.err6.o +# RUN: not llvm-readobj -A %t.err6.o 2>&1 | FileCheck %s -DFILE=%t.err6.o -check-prefix=FIND-ERR +# RUN: not llvm-readelf -A %t.err6.o 2>&1 | FileCheck %s -DFILE=%t.err6.o -check-prefix=FIND-ERR + +# FIND-ERR: error: '[[FILE]]': error locating the .got section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_MIPS +Sections: + - Type: SHT_NULL + ShName: [[NULLNAME=0x0]] + - Name: .got + Type: SHT_PROGBITS Index: llvm/test/tools/llvm-readobj/ELF/mips-options-sec.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/mips-options-sec.test +++ llvm/test/tools/llvm-readobj/ELF/mips-options-sec.test @@ -1,12 +1,36 @@ -RUN: llvm-readobj -A %p/Inputs/options.obj.elf-mipsel | FileCheck %s - -CHECK: MIPS Options { -CHECK-NEXT: ODK_REGINFO { -CHECK-NEXT: GP: 0x0 -CHECK-NEXT: General Mask: 0xF2000017 -CHECK-NEXT: Co-Proc Mask0: 0x0 -CHECK-NEXT: Co-Proc Mask1: 0x0 -CHECK-NEXT: Co-Proc Mask2: 0x0 -CHECK-NEXT: Co-Proc Mask3: 0x0 -CHECK-NEXT: } -CHECK-NEXT: } +## Check that we are able to dump the SHT_MIPS_OPTIONS section using -A properly. + +# RUN: llvm-readobj -A %p/Inputs/options.obj.elf-mipsel | FileCheck %s + +# CHECK: MIPS Options { +# CHECK-NEXT: ODK_REGINFO { +# CHECK-NEXT: GP: 0x0 +# CHECK-NEXT: General Mask: 0xF2000017 +# CHECK-NEXT: Co-Proc Mask0: 0x0 +# CHECK-NEXT: Co-Proc Mask1: 0x0 +# CHECK-NEXT: Co-Proc Mask2: 0x0 +# CHECK-NEXT: Co-Proc Mask3: 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: } + +## Check we report a warning when we are unable to find the .MIPS.options section due to an error. +# RUN: yaml2obj --docnum=1 -DNULLNAME=0xffff %s -o %t.err1 +# RUN: not llvm-readelf -A %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=FIND-ERR --implicit-check-not=warning: +# RUN: not llvm-readobj -A %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=FIND-ERR --implicit-check-not=warning: + +# FIND-ERR: warning: '[[FILE]]': error locating the .MIPS.abiflags section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: warning: '[[FILE]]': error locating the .MIPS.options section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: warning: '[[FILE]]': error locating the .reginfo section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: error: '[[FILE]]': error locating the .got section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_MIPS +Sections: + - Type: SHT_NULL + ShName: [[NULLNAME=0x0]] + - Name: .MIPS.options + Type: SHT_MIPS_OPTIONS Index: llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test +++ llvm/test/tools/llvm-readobj/ELF/mips-reginfo.test @@ -1,10 +1,34 @@ -RUN: llvm-readobj -A %p/Inputs/reginfo.obj.elf-mipsel | FileCheck %s - -CHECK: MIPS RegInfo { -CHECK-NEXT: GP: 0x7FEF -CHECK-NEXT: General Mask: 0xB00001F6 -CHECK-NEXT: Co-Proc Mask0: 0x0 -CHECK-NEXT: Co-Proc Mask1: 0x0 -CHECK-NEXT: Co-Proc Mask2: 0x0 -CHECK-NEXT: Co-Proc Mask3: 0x0 -CHECK-NEXT: } +## Check that we are able to dump the SHT_MIPS_REGINFO section using -A properly. + +# RUN: llvm-readobj -A %p/Inputs/reginfo.obj.elf-mipsel | FileCheck %s + +# CHECK: MIPS RegInfo { +# CHECK-NEXT: GP: 0x7FEF +# CHECK-NEXT: General Mask: 0xB00001F6 +# CHECK-NEXT: Co-Proc Mask0: 0x0 +# CHECK-NEXT: Co-Proc Mask1: 0x0 +# CHECK-NEXT: Co-Proc Mask2: 0x0 +# CHECK-NEXT: Co-Proc Mask3: 0x0 +# CHECK-NEXT: } + +## Check we report a warning when we are unable to find the .reginfo section due to an error. +# RUN: yaml2obj --docnum=1 -DNULLNAME=0xffff %s -o %t.err1 +# RUN: not llvm-readelf -A %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=FIND-ERR --implicit-check-not=warning: +# RUN: not llvm-readobj -A %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=FIND-ERR --implicit-check-not=warning: + +# FIND-ERR: warning: '[[FILE]]': error locating the .MIPS.abiflags section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: warning: '[[FILE]]': error locating the .MIPS.options section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: warning: '[[FILE]]': error locating the .reginfo section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table +# FIND-ERR: error: '[[FILE]]': error locating the .got section: a section [index 0] has an invalid sh_name (0xffff) offset which goes past the end of the section name string table + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_MIPS +Sections: + - Type: SHT_NULL + ShName: [[NULLNAME=0x0]] + - Name: .reginfo + Type: SHT_MIPS_REGINFO Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -1267,11 +1267,16 @@ } template -static const typename ELFO::Elf_Shdr * +static Expected findSectionByName(const ELFO &Obj, StringRef FileName, StringRef Name) { - for (const typename ELFO::Elf_Shdr &Shdr : cantFail(Obj.sections())) - if (Name == unwrapOrError(FileName, Obj.getSectionName(&Shdr))) + for (const typename ELFO::Elf_Shdr &Shdr : cantFail(Obj.sections())) { + Expected NameOrErr = Obj.getSectionName(&Shdr); + if (!NameOrErr) + return createError("error locating the " + Name + + " section: " + toString(NameOrErr.takeError())); + if (Name == *NameOrErr) return &Shdr; + } return nullptr; } @@ -2979,7 +2984,13 @@ // Find static GOT secton. if (IsStatic) { - GotSec = findSectionByName(*Obj, FileName, ".got"); + Expected GotOrErr = + findSectionByName(*Obj, FileName, ".got"); + if (!GotOrErr) + return GotOrErr.takeError(); + else + GotSec = *GotOrErr; + if (!GotSec) return Error::success(); @@ -3298,13 +3309,20 @@ template void ELFDumper::printMipsReginfo() { const ELFFile *Obj = ObjF->getELFFile(); - const Elf_Shdr *Shdr = findSectionByName(*Obj, ObjF->getFileName(), ".reginfo"); - if (!Shdr) { + Expected RegInfoOrErr = + findSectionByName(*Obj, ObjF->getFileName(), ".reginfo"); + if (!RegInfoOrErr) { + reportUniqueWarning(RegInfoOrErr.takeError()); + return; + } + + if ((*RegInfoOrErr) == nullptr) { W.startLine() << "There is no .reginfo section in the file.\n"; return; } - ArrayRef Sec = - unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Shdr)); + + ArrayRef Sec = unwrapOrError(ObjF->getFileName(), + Obj->getSectionContents(*RegInfoOrErr)); if (Sec.size() != sizeof(Elf_Mips_RegInfo)) { W.startLine() << "The .reginfo section has a wrong size.\n"; return; @@ -3317,17 +3335,22 @@ template void ELFDumper::printMipsOptions() { const ELFFile *Obj = ObjF->getELFFile(); - const Elf_Shdr *Shdr = + Expected MipsOptOrErr = findSectionByName(*Obj, ObjF->getFileName(), ".MIPS.options"); - if (!Shdr) { + if (!MipsOptOrErr) { + reportUniqueWarning(MipsOptOrErr.takeError()); + return; + } + + if ((*MipsOptOrErr) == nullptr) { W.startLine() << "There is no .MIPS.options section in the file.\n"; return; } DictScope GS(W, "MIPS Options"); - ArrayRef Sec = - unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(Shdr)); + ArrayRef Sec = unwrapOrError(ObjF->getFileName(), + Obj->getSectionContents(*MipsOptOrErr)); while (!Sec.empty()) { if (Sec.size() < sizeof(Elf_Mips_Options)) { W.startLine() << "The .MIPS.options section has a wrong size.\n"; @@ -5930,13 +5953,15 @@ Expected *> getMipsAbiFlagsSection(const ELFObjectFile *ObjF) { const ELFFile *Obj = ObjF->getELFFile(); - const typename ELFT::Shdr *Shdr = + Expected SecOrErr = findSectionByName(*Obj, ObjF->getFileName(), ".MIPS.abiflags"); - if (!Shdr) + if (!SecOrErr) + return SecOrErr.takeError(); + if (!*SecOrErr) return nullptr; constexpr StringRef ErrPrefix = "unable to read the .MIPS.abiflags section: "; - Expected> DataOrErr = Obj->getSectionContents(Shdr); + Expected> DataOrErr = Obj->getSectionContents(*SecOrErr); if (!DataOrErr) return createError(ErrPrefix + toString(DataOrErr.takeError()));