[llvm] r364955 - [llvm-objdump] Warn if no user specified sections (-j) are not found.

Yuanfang Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 2 11:38:17 PDT 2019


Author: yuanfang
Date: Tue Jul  2 11:38:17 2019
New Revision: 364955

URL: http://llvm.org/viewvc/llvm-project?rev=364955&view=rev
Log:
[llvm-objdump] Warn if no user specified sections (-j) are not found.

Match GNU objdump.

https://bugs.llvm.org/show_bug.cgi?id=41898

Reviewers: jhenderson, grimar, MaskRay, rupprecht

Reviewed by: jhenderson, grimar, MaskRay

Differential Revision: https://reviews.llvm.org/D63779

Added:
    llvm/trunk/test/tools/llvm-objdump/warn-missing-section.test
Modified:
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp

Added: llvm/trunk/test/tools/llvm-objdump/warn-missing-section.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/warn-missing-section.test?rev=364955&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/warn-missing-section.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/warn-missing-section.test Tue Jul  2 11:38:17 2019
@@ -0,0 +1,73 @@
+## This test checks the warning message when no user specified sections are
+## found in the object file.
+
+## Test relocatable file.
+# RUN: yaml2obj --docnum=1 %s -o %t.1.o
+# RUN: yaml2obj --docnum=2 %s -o %t.2.o
+
+## - Warn for one section is found case.
+# RUN: llvm-objdump --reloc --section=single %t.2.o 2>&1 \
+# RUN:   | FileCheck --check-prefix=SINGLE-WARN %s
+
+## - Don't warn twice for duplicate missing sections.
+# RUN: llvm-objdump --reloc --section=single --section=single %t.2.o 2>&1 \
+# RUN:   | FileCheck --check-prefixes=SINGLE-WARN --implicit-check-not=warning: %s
+
+## - Don't warn if any user specified section is found.
+# RUN: llvm-objdump --reloc --section=.text1 --section=.text2 %t.2.o \
+# RUN:   | FileCheck --implicit-check-not=warning: %s
+
+## - Warn for each specified section if none of them are found.
+# RUN: llvm-objdump --reloc --section=multi1 --section=multi2 %t.2.o 2>&1 \
+# RUN:   | FileCheck --check-prefix=MULTI-WARN %s
+
+## - Don't warn if the specified section has no name.
+# RUN: llvm-objdump --reloc --section="" %t.2.o 2>&1 \
+# RUN:   | FileCheck --implicit-check-not=warning: %s
+
+## - Warning for --section is applicable for various other options.
+# RUN: llvm-objdump --section-headers --section=single %t.2.o 2>&1 \
+# RUN:   | FileCheck --check-prefix=SINGLE-WARN %s
+# RUN: llvm-objdump --full-contents --section=single %t.2.o 2>&1 \
+# RUN:   | FileCheck --check-prefix=SINGLE-WARN %s
+
+## Test archive file.
+# RUN: rm -f %t.a
+# RUN: llvm-ar rc %t.a %t.1.o %t.2.o
+
+## - Warn for one section is found case.
+# RUN: llvm-objdump --reloc --section=single %t.a 2>&1 \
+# RUN:   | FileCheck --check-prefix=SINGLE-WARN %s
+
+## - Don't warn if any user specified section is found.
+# RUN: llvm-objdump --reloc --section=.text1 %t.a \
+# RUN:   | FileCheck --implicit-check-not=warning: %s
+
+## - Warn for each specified section if none of them are found.
+# RUN: llvm-objdump --reloc --section=multi1 --section=multi2 %t.a 2>&1 \
+# RUN:   | FileCheck --check-prefix=MULTI-WARN %s
+
+
+# SINGLE-WARN: warning: section 'single' mentioned in a -j/--section option, but not found in any input file
+# MULTI-WARN:      warning: section 'multi1' mentioned in a -j/--section option, but not found in any input file
+# MULTI-WARN-NEXT: warning: section 'multi2' mentioned in a -j/--section option, but not found in any input file
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+- Name:    .text1
+  Type:    SHT_PROGBITS
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+- Name:    .text2
+  Type:    SHT_PROGBITS

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=364955&r1=364954&r2=364955&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Tue Jul  2 11:38:17 2019
@@ -336,6 +336,7 @@ static cl::extrahelp
     HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
 
 static StringSet<> DisasmFuncsSet;
+static StringSet<> FoundSectionSet;
 static StringRef ToolName;
 
 typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
@@ -343,11 +344,15 @@ typedef std::vector<std::tuple<uint64_t,
 static bool shouldKeep(object::SectionRef S) {
   if (FilterSections.empty())
     return true;
-  StringRef String;
-  std::error_code error = S.getName(String);
+  StringRef SecName;
+  std::error_code error = S.getName(SecName);
   if (error)
     return false;
-  return is_contained(FilterSections, String);
+  // StringSet does not allow empty key so avoid adding sections with
+  // no name (such as the section with index 0) here.
+  if (!SecName.empty())
+    FoundSectionSet.insert(SecName);
+  return is_contained(FilterSections, SecName);
 }
 
 SectionFilter ToolSectionFilter(object::ObjectFile const &O) {
@@ -434,6 +439,22 @@ LLVM_ATTRIBUTE_NORETURN void report_erro
     report_error(std::move(E), ArchiveName, NameOrErr.get(), ArchitectureName);
 }
 
+static void warnOnNoMatchForSections() {
+  SetVector<StringRef> MissingSections;
+  for (StringRef S : FilterSections) {
+    if (FoundSectionSet.count(S))
+      return;
+    // User may specify a unnamed section. Don't warn for it.
+    if (!S.empty())
+      MissingSections.insert(S);
+  }
+
+  // Warn only if no section in FilterSections is matched.
+  for (StringRef S : MissingSections)
+    warn("section '" + S + "' mentioned in a -j/--section option, but not "
+         "found in any input file");
+}
+
 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
   // Figure out the target triple.
   Triple TheTriple("unknown-unknown-unknown");
@@ -2157,5 +2178,7 @@ int main(int argc, char **argv) {
 
   llvm::for_each(InputFilenames, dumpInput);
 
+  warnOnNoMatchForSections();
+
   return EXIT_SUCCESS;
 }




More information about the llvm-commits mailing list