Index: test/Object/invalid.test =================================================================== --- test/Object/invalid.test +++ test/Object/invalid.test @@ -126,12 +126,12 @@ Symbols: - Name: foo -## Check that llvm-readobj reports an error if .dynsym has an invalid sh_entsize. +## Check that llvm-readobj reports a warning if .dynsym has an invalid sh_entsize. # RUN: yaml2obj %s --docnum=7 -o %t7 -# RUN: not llvm-readobj --dyn-symbols %t7 2>&1 | FileCheck --check-prefix=INVALID-DYNSYM-SIZE %s +# RUN: llvm-readobj --dyn-symbols %t7 2>&1 | FileCheck --check-prefix=INVALID-DYNSYM-SIZE %s -# INVALID-DYNSYM-SIZE: error: Invalid entity size +# INVALID-DYNSYM-SIZE: warning: invalid section size (48) or entity size (32) --- !ELF FileHeader: @@ -458,22 +458,22 @@ Sections: - Section: .dynamic -## Check that llvm-readobj reports an error when a dynamic relocation section +## Check that llvm-readobj reports a warning when a dynamic relocation section ## has sh_entsize field with size != sizeof(Elf_Rela). -# RUN: not llvm-readobj --dyn-relocations \ +# RUN: llvm-readobj --dyn-relocations \ # RUN: %p/Inputs/corrupt-invalid-relocation-size.elf.x86-64 2>&1 \ # RUN: | FileCheck --check-prefix=RELOC-BROKEN-ENTSIZE %s -# RELOC-BROKEN-ENTSIZE: error: Invalid entity size +# RELOC-BROKEN-ENTSIZE: warning: invalid section size (24) or entity size (25) -## Check that llvm-readobj reports an error when .dynamic section has an invalid +## 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: not llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck --check-prefix=DYN-TABLE-SIZE %s +# RUN: llvm-readobj --dyn-relocations %t22 2>&1 | FileCheck --check-prefix=DYN-TABLE-SIZE %s -# DYN-TABLE-SIZE: error: Invalid entity size +# DYN-TABLE-SIZE: warning: invalid section size (1) or entity size (16) --- !ELF FileHeader: Index: test/tools/llvm-readobj/elf-dynamic-malformed.test =================================================================== --- test/tools/llvm-readobj/elf-dynamic-malformed.test +++ test/tools/llvm-readobj/elf-dynamic-malformed.test @@ -1,9 +1,226 @@ -# Test handling of a dynamic section size which is not a multiple of its entry size. +## Test handling of a dynamic section size which is not a multiple of its entry size. +## Test the full output to demonstrate how we print the warnings. + # RUN: yaml2obj %s --docnum=1 -o %t.bad-size -# RUN: not llvm-readobj --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE -# RUN: not llvm-readelf --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE +# RUN: llvm-readobj --all %t.bad-size 2>&1 | FileCheck %s --check-prefix WARN +# RUN: llvm-readelf --all %t.bad-size 2>&1 | FileCheck %s --check-prefix WARN-GNU + +# WARN-NOT: warning +# WARN: warning: invalid section size (4) or entity size (16) +# WARN-EMPTY: +# WARN-NEXT: File: {{.*}}bad-size +# WARN-NEXT: Format: ELF64-x86-64 +# WARN-NEXT: Arch: x86_64 +# WARN-NEXT: AddressSize: 64bit +# WARN-NEXT: LoadName: +# WARN-NEXT: ElfHeader { +# WARN-NEXT: Ident { +# WARN-NEXT: Magic: (7F 45 4C 46) +# WARN-NEXT: Class: 64-bit (0x2) +# WARN-NEXT: DataEncoding: LittleEndian (0x1) +# WARN-NEXT: FileVersion: 1 +# WARN-NEXT: OS/ABI: SystemV (0x0) +# WARN-NEXT: ABIVersion: 0 +# WARN-NEXT: Unused: (00 00 00 00 00 00 00) +# WARN-NEXT: } +# WARN-NEXT: Type: Executable (0x2) +# WARN-NEXT: Machine: EM_X86_64 (0x3E) +# WARN-NEXT: Version: 1 +# WARN-NEXT: Entry: 0x0 +# WARN-NEXT: ProgramHeaderOffset: 0x40 +# WARN-NEXT: SectionHeaderOffset: 0xB0 +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: HeaderSize: 64 +# WARN-NEXT: ProgramHeaderEntrySize: 56 +# WARN-NEXT: ProgramHeaderCount: 2 +# WARN-NEXT: SectionHeaderEntrySize: 64 +# WARN-NEXT: SectionHeaderCount: 5 +# WARN-NEXT: StringTableSectionIndex: 4 +# WARN-NEXT: } +# WARN-NEXT: Sections [ +# WARN-NEXT: Section { +# WARN-NEXT: Index: 0 +# WARN-NEXT: Name: (0) +# WARN-NEXT: Type: SHT_NULL (0x0) +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Address: 0x0 +# WARN-NEXT: Offset: 0x0 +# WARN-NEXT: Size: 0 +# WARN-NEXT: Link: 0 +# WARN-NEXT: Info: 0 +# WARN-NEXT: AddressAlignment: 0 +# WARN-NEXT: EntrySize: 0 +# WARN-NEXT: } +# WARN-NEXT: Section { +# WARN-NEXT: Index: 1 +# WARN-NEXT: Name: .dynamic (1) +# WARN-NEXT: Type: SHT_DYNAMIC (0x6) +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Address: 0x1000 +# WARN-NEXT: Offset: 0x1F0 +# WARN-NEXT: Size: 4 +# WARN-NEXT: Link: 0 +# WARN-NEXT: Info: 0 +# WARN-NEXT: AddressAlignment: 0 +# WARN-NEXT: EntrySize: 16 +# WARN-NEXT: } +# WARN-NEXT: Section { +# WARN-NEXT: Index: 2 +# WARN-NEXT: Name: .symtab (28) +# WARN-NEXT: Type: SHT_SYMTAB (0x2) +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Address: 0x0 +# WARN-NEXT: Offset: 0x1F8 +# WARN-NEXT: Size: 24 +# WARN-NEXT: Link: 3 +# WARN-NEXT: Info: 1 +# WARN-NEXT: AddressAlignment: 8 +# WARN-NEXT: EntrySize: 24 +# WARN-NEXT: } +# WARN-NEXT: Section { +# WARN-NEXT: Index: 3 +# WARN-NEXT: Name: .strtab (20) +# WARN-NEXT: Type: SHT_STRTAB (0x3) +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Address: 0x0 +# WARN-NEXT: Offset: 0x210 +# WARN-NEXT: Size: 1 +# WARN-NEXT: Link: 0 +# WARN-NEXT: Info: 0 +# WARN-NEXT: AddressAlignment: 1 +# WARN-NEXT: EntrySize: 0 +# WARN-NEXT: } +# WARN-NEXT: Section { +# WARN-NEXT: Index: 4 +# WARN-NEXT: Name: .shstrtab (10) +# WARN-NEXT: Type: SHT_STRTAB (0x3) +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Address: 0x0 +# WARN-NEXT: Offset: 0x211 +# WARN-NEXT: Size: 36 +# WARN-NEXT: Link: 0 +# WARN-NEXT: Info: 0 +# WARN-NEXT: AddressAlignment: 1 +# WARN-NEXT: EntrySize: 0 +# WARN-NEXT: } +# WARN-NEXT: ] +# WARN-NEXT: Relocations [ +# WARN-NEXT: ] +# WARN-NEXT: Symbols [ +# WARN-NEXT: Symbol { +# WARN-NEXT: Name: (0) +# WARN-NEXT: Value: 0x0 +# WARN-NEXT: Size: 0 +# WARN-NEXT: Binding: Local (0x0) +# WARN-NEXT: Type: None (0x0) +# WARN-NEXT: Other: 0 +# WARN-NEXT: Section: Undefined (0x0) +# WARN-NEXT: } +# WARN-NEXT: ] +# WARN-EMPTY: +# WARN-NEXT: warning: invalid section size (4) or entity size (16) +# WARN-NEXT: ProgramHeaders [ +# WARN-NEXT: ProgramHeader { +# WARN-NEXT: Type: PT_LOAD (0x1) +# WARN-NEXT: Offset: 0x1F0 +# WARN-NEXT: VirtualAddress: 0x1000 +# WARN-NEXT: PhysicalAddress: 0x0 +# WARN-NEXT: FileSize: 4 +# WARN-NEXT: MemSize: 4 +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Alignment: 1 +# WARN-NEXT: } +# WARN-NEXT: ProgramHeader { +# WARN-NEXT: Type: PT_DYNAMIC (0x2) +# WARN-NEXT: Offset: 0x1F0 +# WARN-NEXT: VirtualAddress: 0x1000 +# WARN-NEXT: PhysicalAddress: 0x0 +# WARN-NEXT: FileSize: 4 +# WARN-NEXT: MemSize: 4 +# WARN-NEXT: Flags [ (0x0) +# WARN-NEXT: ] +# WARN-NEXT: Alignment: 1 +# WARN-NEXT: } +# WARN-NEXT: ] +# WARN-NEXT: Version symbols { +# WARN-NEXT: } +# WARN-NEXT: SHT_GNU_verdef { +# WARN-NEXT: } +# WARN-NEXT: SHT_GNU_verneed { +# WARN-NEXT: } +# WARN-NEXT: Groups { +# WARN-NEXT: There are no group sections in the file. +# WARN-NEXT: } +# WARN-NEXT: Hash Histogram not implemented! +# WARN-NEXT: Notes [ +# WARN-NEXT: ] -# ERR-SIZE: error: Invalid entity size +# WARN-GNU-NOT: warning +# WARN-GNU: warning: invalid section size (4) or entity size (16) +# WARN-GNU-NEXT: ELF Header: +# WARN-GNU-NEXT: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 +# WARN-GNU-NEXT: Class: ELF64 +# WARN-GNU-NEXT: Data: 2's complement, little endian +# WARN-GNU-NEXT: Version: 1 (current) +# WARN-GNU-NEXT: OS/ABI: UNIX - System V +# WARN-GNU-NEXT: ABI Version: 0x0 +# WARN-GNU-NEXT: Type: EXEC (Executable file) +# WARN-GNU-NEXT: Machine: Advanced Micro Devices X86-64 +# WARN-GNU-NEXT: Version: 0x1 +# WARN-GNU-NEXT: Entry point address: 0x0 +# WARN-GNU-NEXT: Start of program headers: 64 (bytes into file) +# WARN-GNU-NEXT: Start of section headers: 176 (bytes into file) +# WARN-GNU-NEXT: Flags: 0x0 +# WARN-GNU-NEXT: Size of this header: 64 (bytes) +# WARN-GNU-NEXT: Size of program headers: 56 (bytes) +# WARN-GNU-NEXT: Number of program headers: 2 +# WARN-GNU-NEXT: Size of section headers: 64 (bytes) +# WARN-GNU-NEXT: Number of section headers: 5 +# WARN-GNU-NEXT: Section header string table index: 4 +# WARN-GNU-NEXT: There are 5 section headers, starting at offset 0xb0: +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: Section Headers: +# WARN-GNU-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al +# WARN-GNU-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0 +# WARN-GNU-NEXT: [ 1] .dynamic DYNAMIC 0000000000001000 0001f0 000004 10 0 0 0 +# WARN-GNU-NEXT: [ 2] .symtab SYMTAB 0000000000000000 0001f8 000018 18 3 1 8 +# WARN-GNU-NEXT: [ 3] .strtab STRTAB 0000000000000000 000210 000001 00 0 0 1 +# WARN-GNU-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 000211 000024 00 0 0 1 +# WARN-GNU-NEXT: Key to Flags: +# WARN-GNU-NEXT: W (write), A (alloc), X (execute), M (merge), S (strings), l (large) +# WARN-GNU-NEXT: I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) +# WARN-GNU-NEXT: O (extra OS processing required) o (OS specific), p (processor specific) +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: There are no relocations in this file. +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: Symbol table '.symtab' contains 1 entries: +# WARN-GNU-NEXT: Num: Value Size Type Bind Vis Ndx Name +# WARN-GNU-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: warning: invalid section size (4) or entity size (16) +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: Elf file type is EXEC (Executable file) +# WARN-GNU-NEXT: Entry point 0x0 +# WARN-GNU-NEXT: There are 2 program headers, starting at offset 64 +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: Program Headers: +# WARN-GNU-NEXT: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align +# WARN-GNU-NEXT: LOAD 0x0001f0 0x0000000000001000 0x0000000000000000 0x000004 0x000004 0x1 +# WARN-GNU-NEXT: DYNAMIC 0x0001f0 0x0000000000001000 0x0000000000000000 0x000004 0x000004 0x1 +# WARN-GNU-EMPTY: +# WARN-GNU-NEXT: Section to Segment mapping: +# WARN-GNU-NEXT: Segment Sections... +# WARN-GNU-NEXT: 00 .dynamic +# WARN-GNU-NEXT: 01 .dynamic +# WARN-GNU-NEXT: None .symtab .strtab .shstrtab +# WARN-GNU-NEXT: There are no section groups in this file. --- !ELF FileHeader: @@ -26,7 +243,7 @@ Sections: - Section: .dynamic -# Test handling of a .dynamic section with an invalid entsize (i.e. not 2 * sizeof(Elf_Dyn)). +## Test handling of a .dynamic section with an invalid entsize (i.e. not 2 * sizeof(Elf_Dyn)). # RUN: yaml2obj %s --docnum=2 -o %t.bad-entsize # RUN: llvm-readobj --dynamic-table %t.bad-entsize | FileCheck %s --check-prefix BAD-ENTSIZE-LLVM # RUN: llvm-readelf --dynamic-table %t.bad-entsize | FileCheck %s --check-prefix BAD-ENTSIZE-GNU @@ -68,7 +285,7 @@ Sections: - Section: .dynamic -# Test handling of string references pointing past the end of the dynamic string table. +## Test handling of string references pointing past the end of the dynamic string table. # RUN: yaml2obj %s --docnum=3 -o %t.bad-string # RUN: llvm-readobj --dynamic-table %t.bad-string | FileCheck %s --check-prefix BAD-STRING-LLVM # RUN: llvm-readelf --dynamic-table %t.bad-string | FileCheck %s --check-prefix BAD-STRING-GNU @@ -136,7 +353,7 @@ Sections: - Section: .dynamic -# Test handling of DT_STRTAB pointing outside the file's address space. +## Test handling of DT_STRTAB pointing outside the file's address space. # RUN: yaml2obj %s --docnum=4 -o %t.bad-strtab # RUN: llvm-readobj --dynamic-table %t.bad-strtab 2>&1 >/dev/null | FileCheck %s --check-prefix BAD-STRTAB-ERR @@ -181,7 +398,7 @@ Sections: - Section: .dynamic -# Test handling of other d_ptr tags pointing outside the file's address space. +## Test handling of other d_ptr tags pointing outside the file's address space. # RUN: yaml2obj %s --docnum=5 -o %t.bad-rela # RUN: llvm-readobj --dynamic-table %t.bad-rela 2>&1 | FileCheck %s --check-prefixes=CHECK,BAD-RELA # RUN: llvm-readelf --dynamic-table %t.bad-rela 2>&1 | FileCheck %s --check-prefixes=CHECK,BAD-RELA-GNU Index: tools/llvm-readobj/ELFDumper.cpp =================================================================== --- tools/llvm-readobj/ELFDumper.cpp +++ tools/llvm-readobj/ELFDumper.cpp @@ -134,8 +134,12 @@ const Type *Start = reinterpret_cast(Addr); if (!Start) return {Start, Start}; - if (EntSize != sizeof(Type) || Size % EntSize) - reportError("Invalid entity size"); + if (EntSize != sizeof(Type) || Size % EntSize) { + // TODO: Add a section index to this warning. + reportWarning("invalid section size (" + Twine(Size) + + ") or entity size (" + Twine(EntSize) + ")"); + return {Start, Start}; + } return {Start, Start + (Size / EntSize)}; } }; Index: tools/llvm-readobj/llvm-readobj.cpp =================================================================== --- tools/llvm-readobj/llvm-readobj.cpp +++ tools/llvm-readobj/llvm-readobj.cpp @@ -377,8 +377,10 @@ } void reportWarning(Twine Msg) { + fouts().flush(); errs() << "\n"; WithColor::warning(errs()) << Msg << "\n"; + errs().flush(); } void warn(Error Err) {