Skip to content

Commit 0d656cb

Browse files
author
Georgii Rymar
committed
[llvm-readobj] - Refine the error reporting in LLVMStyle<ELFT>::printELFLinkerOptions.
It is possible to: 1) Avoid using the `unwrapOrError` calls and hence allow to continue dumping even when something is not OK with one of SHT_LLVM_LINKER_OPTIONS sections. 2) replace `reportWarning` with `reportUniqueWarning` calls. In this method it is no-op, because it is not possible to have a duplicated warnings anyways, but since we probably want to switch to `reportUniqueWarning` globally, this is a good thing to do. This patch addresses both these points. Differential revision: https://reviews.llvm.org/D83131
1 parent 8f0f7db commit 0d656cb

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

llvm/test/tools/llvm-readobj/ELF/linker-options.test

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
## to dump SHT_LLVM_LINKER_OPTIONS sections.
33

44
# RUN: yaml2obj --docnum=1 %s -o %t1
5-
# RUN: llvm-readobj --elf-linker-options %t1 2>&1 | FileCheck %s --check-prefix=CHECK -DFILE=%t1
5+
# RUN: llvm-readobj --elf-linker-options %t1 2>&1 | FileCheck %s -DFILE=%t1
66

77
# CHECK: LinkerOptions [
88
# CHECK: option 0: value 0
99
# CHECK: option 1: value 1
1010
# CHECK-NEXT: warning: '[[FILE]]': SHT_LLVM_LINKER_OPTIONS section at index 2 is broken: an incomplete key-value pair was found. The last possible key was: "c"
1111
# CHECK-NEXT: warning: '[[FILE]]': SHT_LLVM_LINKER_OPTIONS section at index 4 is broken: the content is not null-terminated
12+
# CHECK-NEXT: warning: '[[FILE]]': unable to read the content of the SHT_LLVM_LINKER_OPTIONS section: section [index 5] has a sh_offset (0xffffffff) + sh_size (0x8) that is greater than the file size (0x370)
1213
# CHECK-NEXT: option 3: value 3
1314
# CHECK-NEXT: ]
1415

@@ -44,7 +45,15 @@ Sections:
4445
- Name: .linker-options.nonul
4546
Type: SHT_LLVM_LINKER_OPTIONS
4647
Content: "61"
47-
## Case 5: another correct case to show we do not stop dumping after reporting a warning.
48+
## Case 5: check we report a warning when it is not possible to read
49+
## the content of the SHT_LLVM_LINKER_OPTIONS section.
50+
- Name: .linker-options.broken.content
51+
Type: SHT_LLVM_LINKER_OPTIONS
52+
ShOffset: 0xffffffff
53+
Options:
54+
- Name: foo
55+
Value: bar
56+
## Case 6: another correct case to show we do not stop dumping after reporting a warning.
4857
- Name: .linker-options.valid2
4958
Type: SHT_LLVM_LINKER_OPTIONS
5059
Options:

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6794,30 +6794,33 @@ void LLVMStyle<ELFT>::printELFLinkerOptions(const ELFFile<ELFT> *Obj) {
67946794
if (Shdr.sh_type != ELF::SHT_LLVM_LINKER_OPTIONS)
67956795
continue;
67966796

6797-
ArrayRef<uint8_t> Contents =
6798-
unwrapOrError(this->FileName, Obj->getSectionContents(&Shdr));
6799-
if (Contents.empty())
6797+
Expected<ArrayRef<uint8_t>> ContentsOrErr = Obj->getSectionContents(&Shdr);
6798+
if (!ContentsOrErr) {
6799+
this->reportUniqueWarning(
6800+
createError("unable to read the content of the "
6801+
"SHT_LLVM_LINKER_OPTIONS section: " +
6802+
toString(ContentsOrErr.takeError())));
6803+
continue;
6804+
}
6805+
if (ContentsOrErr->empty())
68006806
continue;
68016807

6802-
if (Contents.back() != 0) {
6803-
reportWarning(createError("SHT_LLVM_LINKER_OPTIONS section at index " +
6804-
Twine(I) +
6805-
" is broken: the "
6806-
"content is not null-terminated"),
6807-
this->FileName);
6808+
if (ContentsOrErr->back() != 0) {
6809+
this->reportUniqueWarning(
6810+
createError("SHT_LLVM_LINKER_OPTIONS section at index " + Twine(I) +
6811+
" is broken: the "
6812+
"content is not null-terminated"));
68086813
continue;
68096814
}
68106815

68116816
SmallVector<StringRef, 16> Strings;
6812-
toStringRef(Contents.drop_back()).split(Strings, '\0');
6817+
toStringRef(ContentsOrErr->drop_back()).split(Strings, '\0');
68136818
if (Strings.size() % 2 != 0) {
6814-
reportWarning(
6815-
createError(
6816-
"SHT_LLVM_LINKER_OPTIONS section at index " + Twine(I) +
6817-
" is broken: an incomplete "
6818-
"key-value pair was found. The last possible key was: \"" +
6819-
Strings.back() + "\""),
6820-
this->FileName);
6819+
this->reportUniqueWarning(createError(
6820+
"SHT_LLVM_LINKER_OPTIONS section at index " + Twine(I) +
6821+
" is broken: an incomplete "
6822+
"key-value pair was found. The last possible key was: \"" +
6823+
Strings.back() + "\""));
68216824
continue;
68226825
}
68236826

0 commit comments

Comments
 (0)