Skip to content

Commit

Permalink
[llvm-readobj] - Introduce warnings for cases when unable to read str…
Browse files Browse the repository at this point in the history
…ings from string tables.

Currently we have no dedicated warnings, but we return error message instead of a result.
It is generally not consistent with another warnings we have.

This change was suggested and discussed here:
https://reviews.llvm.org/D77216#1954873

This change refines error messages we report and also I had to update the API
to implement it.

Differential revision: https://reviews.llvm.org/D77399
  • Loading branch information
Georgii Rymar committed Apr 7, 2020
1 parent e268ec8 commit 7fc599c
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 113 deletions.
104 changes: 69 additions & 35 deletions llvm/test/tools/llvm-readobj/ELF/dynamic-malformed.test
Expand Up @@ -90,26 +90,54 @@ ProgramHeaders:

## 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

# BAD-STRING-LLVM: 0x000000000000000A STRSZ 1 (bytes)
# BAD-STRING-LLVM: 0x0000000000000001 NEEDED Shared library: [<Invalid offset 0x1>]
# BAD-STRING-LLVM: 0x000000007FFFFFFF FILTER Filter library: [<Invalid offset 0x1>]
# BAD-STRING-LLVM: 0x000000007FFFFFFD AUXILIARY Auxiliary library: [<Invalid offset 0x1>]
# BAD-STRING-LLVM: 0x000000007FFFFFFE USED Not needed object: [<Invalid offset 0x1>]
# BAD-STRING-LLVM: 0x000000000000000E SONAME Library soname: [<Invalid offset 0x1>]
# BAD-STRING-LLVM: 0x000000000000000F RPATH Library rpath: [<Invalid offset 0x1>]
# BAD-STRING-LLVM: 0x000000000000001D RUNPATH Library runpath: [<Invalid offset 0x1>]

# BAD-STRING-GNU: 0x000000000000000a (STRSZ) 1 (bytes)
# BAD-STRING-GNU: 0x0000000000000001 (NEEDED) Shared library: [<Invalid offset 0x1>]
# BAD-STRING-GNU: 0x000000007fffffff (FILTER) Filter library: [<Invalid offset 0x1>]
# BAD-STRING-GNU: 0x000000007ffffffd (AUXILIARY) Auxiliary library: [<Invalid offset 0x1>]
# BAD-STRING-GNU: 0x000000007ffffffe (USED) Not needed object: [<Invalid offset 0x1>]
# BAD-STRING-GNU: 0x000000000000000e (SONAME) Library soname: [<Invalid offset 0x1>]
# BAD-STRING-GNU: 0x000000000000000f (RPATH) Library rpath: [<Invalid offset 0x1>]
# BAD-STRING-GNU: 0x000000000000001d (RUNPATH) Library runpath: [<Invalid offset 0x1>]
# RUN: llvm-readobj --dynamic-table %t.bad-string 2>&1 | \
# RUN: FileCheck %s --implicit-check-not=warning: --check-prefix BAD-STRING-LLVM -DFILE=%t.bad-string
# RUN: llvm-readelf --dynamic-table %t.bad-string 2>&1 | \
# RUN: FileCheck %s --implicit-check-not=warning: --check-prefix BAD-STRING-GNU -DFILE=%t.bad-string

# BAD-STRING-LLVM: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb6, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM: LoadName: <?>
# BAD-STRING-LLVM: DynamicSection [ (10 entries)
# BAD-STRING-LLVM-NEXT: Tag Type Name/Value
# BAD-STRING-LLVM-NEXT: 0x0000000000000005 STRTAB 0x1000
# BAD-STRING-LLVM-NEXT: 0x000000000000000A STRSZ 1 (bytes)
# BAD-STRING-LLVM-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb2, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM-NEXT: 0x0000000000000001 NEEDED Shared library: [<?>]
# BAD-STRING-LLVM-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb3, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM-NEXT: 0x000000007FFFFFFF FILTER Filter library: [<?>]
# BAD-STRING-LLVM-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb4, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM-NEXT: 0x000000007FFFFFFD AUXILIARY Auxiliary library: [<?>]
# BAD-STRING-LLVM-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb5, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM-NEXT: 0x000000007FFFFFFE USED Not needed object: [<?>]
## Note: there is no "string table at offset 0xb0..." warning here, because it was printed earlier.
# BAD-STRING-LLVM-NEXT: 0x000000000000000E SONAME Library soname: [<?>]
# BAD-STRING-LLVM-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb7, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM-NEXT: 0x000000000000000F RPATH Library rpath: [<?>]
# BAD-STRING-LLVM-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb8, it goes past the end of the table (0xb1)
# BAD-STRING-LLVM-NEXT: 0x000000000000001D RUNPATH Library runpath: [<?>]
# BAD-STRING-LLVM-NEXT: 0x0000000000000000 NULL 0x0
# BAD-STRING-LLVM-NEXT: ]

# BAD-STRING-GNU: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb6, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: Dynamic section at offset 0xb1 contains 10 entries:
# BAD-STRING-GNU-NEXT: Tag Type Name/Value
# BAD-STRING-GNU-NEXT: 0x0000000000000005 (STRTAB) 0x1000
# BAD-STRING-GNU-NEXT: 0x000000000000000a (STRSZ) 1 (bytes)
# BAD-STRING-GNU-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb2, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: 0x0000000000000001 (NEEDED) Shared library: [<?>]
# BAD-STRING-GNU-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb3, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: 0x000000007fffffff (FILTER) Filter library: [<?>]
# BAD-STRING-GNU-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb4, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: 0x000000007ffffffd (AUXILIARY) Auxiliary library: [<?>]
# BAD-STRING-GNU-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb5, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: 0x000000007ffffffe (USED) Not needed object: [<?>]
## Note: there is no "string table at offset 0xb6..." warning here, because it was printed earlier.
# BAD-STRING-GNU-NEXT: 0x000000000000000e (SONAME) Library soname: [<?>]
# BAD-STRING-GNU-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb7, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: 0x000000000000000f (RPATH) Library rpath: [<?>]
# BAD-STRING-GNU-NEXT: warning: '[[FILE]]': string table at offset 0xb0: unable to read the string at 0xb8, it goes past the end of the table (0xb1)
# BAD-STRING-GNU-NEXT: 0x000000000000001d (RUNPATH) Library runpath: [<?>]
# BAD-STRING-GNU-NEXT: 0x0000000000000000 (NULL) 0x0

--- !ELF
FileHeader:
Expand All @@ -130,19 +158,19 @@ Sections:
- Tag: DT_STRSZ
Value: 1
- Tag: DT_NEEDED
Value: 1
Value: 2
- Tag: DT_FILTER
Value: 1
Value: 3
- Tag: DT_AUXILIARY
Value: 1
Value: 4
- Tag: DT_USED
Value: 1
Value: 5
- Tag: DT_SONAME
Value: 1
Value: 6
- Tag: DT_RPATH
Value: 1
Value: 7
- Tag: DT_RUNPATH
Value: 1
Value: 8
- Tag: DT_NULL
Value: 0
Symbols: []
Expand All @@ -160,17 +188,23 @@ ProgramHeaders:
## 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 -DFILE=%t.bad-strtab %s --check-prefix BAD-STRTAB-ERR
# RUN: llvm-readelf --dynamic-table %t.bad-strtab 2>&1 >/dev/null | FileCheck -DFILE=%t.bad-strtab %s --check-prefix BAD-STRTAB-ERR
# RUN: llvm-readobj --dynamic-table %t.bad-strtab 2>&1 >/dev/null | \
# RUN: FileCheck -DFILE=%t.bad-strtab %s --check-prefix BAD-STRTAB-ERR
# RUN: llvm-readelf --dynamic-table %t.bad-strtab 2>&1 >/dev/null | \
# RUN: FileCheck -DFILE=%t.bad-strtab %s --check-prefix BAD-STRTAB-ERR
# BAD-STRTAB-ERR: warning: '[[FILE]]': Unable to parse DT_STRTAB: virtual address is not in any segment: 0x2000000
# BAD-STRTAB-ERR: warning: '[[FILE]]': string table was not found

# RUN: llvm-readobj --dynamic-table --needed-libs %t.bad-strtab 2>&1 | \
# RUN: FileCheck -DFILE=%t.bad-strtab %s --check-prefixes=BAD-STRTAB-ERR,BAD-STRTAB,BAD-STRTAB-LLVM
# RUN: llvm-readelf --dynamic-table --needed-libs %t.bad-strtab 2>&1 | \
# RUN: FileCheck -DFILE=%t.bad-strtab %s --check-prefixes=BAD-STRTAB-ERR,BAD-STRTAB,BAD-STRTAB-GNU

# RUN: llvm-readobj --dynamic-table --needed-libs %t.bad-strtab | FileCheck %s --check-prefixes=BAD-STRTAB,BAD-STRTAB-LLVM
# RUN: llvm-readelf --dynamic-table --needed-libs %t.bad-strtab | FileCheck %s --check-prefixes=BAD-STRTAB,BAD-STRTAB-GNU
# BAD-STRTAB-LLVM: LoadName: <String table is empty or was not found>
# BAD-STRTAB-LLVM: 0x0000000000000001 NEEDED Shared library: [<String table is empty or was not found>]
# BAD-STRTAB-GNU: 0x0000000000000001 (NEEDED) Shared library: [<String table is empty or was not found>]
# BAD-STRTAB-LLVM: LoadName: <?>
# BAD-STRTAB-LLVM: 0x0000000000000001 NEEDED Shared library: [<?>]
# BAD-STRTAB-GNU: 0x0000000000000001 (NEEDED) Shared library: [<?>]
# BAD-STRTAB: NeededLibraries [
# BAD-STRTAB: <String table is empty or was not found>
# BAD-STRTAB: <?>
# BAD-STRTAB: ]

--- !ELF
Expand Down
5 changes: 3 additions & 2 deletions llvm/test/tools/llvm-readobj/ELF/loadname.test
Expand Up @@ -61,5 +61,6 @@ ProgramHeaders:
# RUN: llvm-readelf --dynamic-table %t.err.1.o 2>&1 | FileCheck %s -DFILE=%t.err.1.o --check-prefixes=BROKEN-OFFSET,BROKEN-OFFSET-GNU

# BROKEN-OFFSET: warning: '[[FILE]]': Unable to parse DT_STRTAB: can't map virtual address 0xfffe to the segment with index 1: the segment ends at 0x10077, which is greater than the file size (0x228)
# BROKEN-OFFSET-LLVM: LoadName: <String table is empty or was not found>
# BROKEN-OFFSET-GNU: 0x000000000000000e (SONAME) Library soname: [<String table is empty or was not found>]
# BROKEN-OFFSET: warning: '[[FILE]]': string table was not found
# BROKEN-OFFSET-LLVM: LoadName: <?>
# BROKEN-OFFSET-GNU: 0x000000000000000e (SONAME) Library soname: [<?>]
39 changes: 25 additions & 14 deletions llvm/test/tools/llvm-readobj/ELF/needed-libs.test
@@ -1,21 +1,23 @@
## In this test we check the --needed-libs option.

# RUN: yaml2obj %s --docnum=1 -o %t1
# RUN: llvm-readobj --needed-libs %t1 \
# RUN: | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=NEEDED-LIBS
# RUN: llvm-readelf --needed-libs %t1 \
# RUN: | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=NEEDED-LIBS
# RUN: llvm-readobj --needed-libs %t1 2>&1 \
# RUN: | FileCheck %s -DFILE=%t1 --implicit-check-not=warning: --strict-whitespace --check-prefix=NEEDED-LIBS
# RUN: llvm-readelf --needed-libs %t1 2>&1 \
# RUN: | FileCheck %s -DFILE=%t1 --implicit-check-not=warning: --strict-whitespace --check-prefix=NEEDED-LIBS

## Check that library names are sorted when printed.
## Document that we also sort error entries.

# NEEDED-LIBS:NeededLibraries [
# NEEDED-LIBS-NEXT: <Invalid offset 0x1111111>
# NEEDED-LIBS-NEXT: <Invalid offset 0x9999999>
# NEEDED-LIBS-NEXT: aaa
# NEEDED-LIBS-NEXT: bbb
# NEEDED-LIBS-NEXT: ccc
# NEEDED-LIBS-NEXT:]
# NEEDED-LIBS:{{^}}NeededLibraries [{{$}}
# NEEDED-LIBS-NEXT: warning: '[[FILE]]': string table at offset 0x78: unable to read the string at 0x9999a11, it goes past the end of the table (0x85){{$}}
# NEEDED-LIBS-NEXT: warning: '[[FILE]]': string table at offset 0x78: unable to read the string at 0x1111189, it goes past the end of the table (0x85){{$}}
# NEEDED-LIBS-NEXT:{{^}} <?>{{$}}
# NEEDED-LIBS-NEXT:{{^}} <?>{{$}}
# NEEDED-LIBS-NEXT:{{^}} aaa{{$}}
# NEEDED-LIBS-NEXT:{{^}} bbb{{$}}
# NEEDED-LIBS-NEXT:{{^}} ccc{{$}}
# NEEDED-LIBS-NEXT:{{^}}]{{$}}

--- !ELF
FileHeader:
Expand Down Expand Up @@ -56,11 +58,18 @@ ProgramHeaders:

## Check what we print when the dynamic string table is empty.
# RUN: yaml2obj %s --docnum=2 -o %t2
# RUN: llvm-readobj --needed-libs %t2 | FileCheck %s --check-prefix=EMPTY-DYNSTR
# RUN: llvm-readelf --needed-libs %t2 | FileCheck %s --check-prefix=EMPTY-DYNSTR
# RUN: llvm-readobj --needed-libs %t2 2>&1 | \
# RUN: FileCheck %s -DFILE=%t2 --implicit-check-not=warning: --check-prefixes=EMPTY-DYNSTR,EMPTY-DYNSTR-LLVM
# RUN: llvm-readelf --needed-libs %t2 2>&1 | \
# RUN: FileCheck %s -DFILE=%t2 --implicit-check-not=warning: --check-prefix=EMPTY-DYNSTR

# EMPTY-DYNSTR: warning: '[[FILE]]': string table at offset 0x78: unable to read the string at 0x78, it goes past the end of the table (0x78)
# EMPTY-DYNSTR-LLVM: LoadName: <?>
# EMPTY-DYNSTR: NeededLibraries [
# EMPTY-DYNSTR-NEXT: <String table is empty or was not found>
# EMPTY-DYNSTR-NEXT: warning: '[[FILE]]': string table at offset 0x78: unable to read the string at 0x79, it goes past the end of the table (0x78)
# EMPTY-DYNSTR-NEXT: warning: '[[FILE]]': string table at offset 0x78: unable to read the string at 0x7a, it goes past the end of the table (0x78)
# EMPTY-DYNSTR-NEXT: <?>
# EMPTY-DYNSTR-NEXT: <?>
# EMPTY-DYNSTR-NEXT: ]

--- !ELF
Expand All @@ -82,6 +91,8 @@ Sections:
Value: 0x0000000000000000
- Tag: DT_NEEDED
Value: 1
- Tag: DT_NEEDED
Value: 2
- Tag: DT_STRSZ
Value: 0x0
- Tag: DT_NULL
Expand Down

0 comments on commit 7fc599c

Please sign in to comment.