Index: llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test +++ llvm/test/tools/llvm-readobj/ELF/gnu-phdrs.test @@ -1,7 +1,7 @@ ## Check how llvm-readelf dumps program headers and prints sections to segments mapping. ## Check that -l, --program-headers and --segments are the same option. -# RUN: yaml2obj -DBITS=32 -DMACHINE=EM_386 %s -o %t32.elf +# RUN: yaml2obj --docnum=1 -DBITS=32 -DMACHINE=EM_386 %s -o %t32.elf # RUN: llvm-readelf -l %t32.elf 2>&1 > %t.readelf-l.txt # RUN: llvm-readelf --program-headers %t32.elf 2>&1 > %t.readelf-pheaders.txt # RUN: cmp %t.readelf-l.txt %t.readelf-pheaders.txt @@ -12,7 +12,7 @@ # RUN: llvm-readelf -l %t32.elf | \ # RUN: FileCheck %s --check-prefixes=ELF32,MAPPING --strict-whitespace --match-full-lines -# RUN: yaml2obj -DBITS=64 -DMACHINE=EM_X86_64 %s -o %t64.elf +# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_X86_64 %s -o %t64.elf # RUN: llvm-readelf -l %t64.elf | \ # RUN: FileCheck %s --check-prefixes=ELF64,MAPPING --strict-whitespace --match-full-lines @@ -293,7 +293,7 @@ # RUN: | FileCheck %s --check-prefix=ELF64 --implicit-check-not="Section to Segment mapping:" ## Check how we dump ARM specific program headers. -# RUN: yaml2obj -DBITS=64 -DMACHINE=EM_ARM %s -o %tarm.elf +# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_ARM %s -o %tarm.elf # RUN: llvm-readelf --program-headers %tarm.elf | FileCheck %s --check-prefix=ARM # ARM: : 0x70000000 0x000548 0x0000000000001000 0x0000000000001000 0x000003 0x000003 0x1 @@ -301,10 +301,70 @@ # ARM-NEXT: : 0x70000002 0x000548 0x0000000000001000 0x0000000000001000 0x000003 0x000003 0x1 ## Check how we dump MIPS specific program headers. -# RUN: yaml2obj -DBITS=64 -DMACHINE=EM_MIPS %s -o %tmips.elf +# RUN: yaml2obj --docnum=1 -DBITS=64 -DMACHINE=EM_MIPS %s -o %tmips.elf # RUN: llvm-readelf --program-headers %tmips.elf | FileCheck %s --check-prefix=MIPS # MIPS: REGINFO 0x000548 0x0000000000001000 0x0000000000001000 0x000003 0x000003 0x1 # MIPS-NEXT: RTPROC 0x000548 0x0000000000001000 0x0000000000001000 0x000003 0x000003 0x1 # MIPS-NEXT: OPTIONS 0x000548 0x0000000000001000 0x0000000000001000 0x000003 0x000003 0x1 # MIPS-NEXT: ABIFLAGS 0x000548 0x0000000000001000 0x0000000000001000 0x000003 0x000003 0x1 + +## Check we report a warning when PT_INTERP has an offset that points past the end of the file. +# RUN: yaml2obj --docnum=2 %s -o %t.err1 +# RUN: llvm-readelf --program-headers %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=ERROR-INTERP1 + +## ERROR-INTERP1: Type Offset +## ERROR-INTERP1-NEXT: INTERP 0xffffffff +## ERROR-INTERP1-NEXT: warning: '[[FILE]]': unable to read program interpreter name at offset 0xffffffff: it goes past the end of the file (0x150) +## ERROR-INTERP1-EMPTY: +## ERROR-INTERP1-NEXT: Section to Segment mapping: + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +ProgramHeaders: + - Type: PT_INTERP + Offset: 0xffffffff + +## Check we report a warning when a program interpreter name is non-null-terminated. +# RUN: yaml2obj --docnum=3 %s -o %t.err2 + +## Show the size of the output produced. It is used below. +# RUN: wc -c < %t.err2 | FileCheck %s --check-prefix=SIZE +# SIZE: 448 + +## Write the additional 'C', '\0, 'C' bytes to the end. +# RUN: echo -n -e "C\x00C" >> %t.err2 + +# RUN: llvm-readelf --program-headers %t.err2 2>&1 | \ +# RUN: FileCheck %s -DFILE=%t.err2 --check-prefix=ERROR-INTERP2 + +# ERROR-INTERP2: Type Offset +# ERROR-INTERP2-NEXT: INTERP 0x0001c0 +# ERROR-INTERP2-NEXT: [Requesting program interpreter: C] +# ERROR-INTERP2-NEXT: INTERP 0x0001c1 +# ERROR-INTERP2-NEXT: [Requesting program interpreter: ] +# ERROR-INTERP2-NEXT: INTERP 0x0001c2 +# ERROR-INTERP2-NEXT: warning: '[[FILE]]': unable to read program interpreter name at offset 0x1c2: it is not null-terminated + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +ProgramHeaders: +## Case 1: the offset points to the first additional byte. + - Type: PT_INTERP + Offset: 448 +## Case 1: the offset points to the second additional byte, +## which is null byte. + - Type: PT_INTERP + Offset: 449 +## Case 3: the offset points to the third additional +## byte, which is the last byte in the file. + - Type: PT_INTERP + Offset: 450 Index: llvm/tools/llvm-readobj/ELFDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ELFDumper.cpp +++ llvm/tools/llvm-readobj/ELFDumper.cpp @@ -4128,8 +4128,31 @@ for (auto Field : Fields) printField(Field); if (Phdr.p_type == ELF::PT_INTERP) { - OS << "\n [Requesting program interpreter: "; - OS << reinterpret_cast(Obj->base()) + Phdr.p_offset << "]"; + OS << "\n"; + auto ReportError = [&](const Twine &Msg) { + reportWarning( + createError("unable to read program interpreter name at offset 0x" + + Twine::utohexstr(Phdr.p_offset) + ": " + Msg), + this->FileName); + }; + + if (Phdr.p_offset >= Obj->getBufSize()) { + ReportError("it goes past the end of the file (0x" + + Twine::utohexstr(Obj->getBufSize()) + ")"); + continue; + } + + const char *Data = + reinterpret_cast(Obj->base()) + Phdr.p_offset; + size_t MaxSize = Obj->getBufSize() - Phdr.p_offset; + size_t Len = strnlen(Data, MaxSize); + if (Len == MaxSize) { + ReportError("it is not null-terminated"); + continue; + } + + OS << " [Requesting program interpreter: "; + OS << StringRef(Data, Len) << "]"; } OS << "\n"; }