]> granicus.if.org Git - llvm/commitdiff
[llvm-objcopy] Add file names to error messages
authorSeiya Nuta <nuta@seiya.me>
Thu, 23 May 2019 00:42:46 +0000 (00:42 +0000)
committerSeiya Nuta <nuta@seiya.me>
Thu, 23 May 2019 00:42:46 +0000 (00:42 +0000)
Summary:
This patch adds the file names to llvm-objcopy error messages. It makes easy to identify which file causes an error.

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

Reviewers: espindola, alexshap, rupprecht, jhenderson, jakehehrlich

Reviewed By: rupprecht, jhenderson, jakehehrlich

Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits

Tags: #llvm

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361450 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objcopy/ELF/dump-section.test
test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test
test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test
test/tools/llvm-objcopy/ELF/no-build-id.test
test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test
test/tools/llvm-objcopy/ELF/remove-linked-section.test
test/tools/llvm-objcopy/ELF/remove-shstrtab-error.test
test/tools/llvm-objcopy/ELF/strip-section-err.test
test/tools/llvm-objcopy/ELF/strip-symbol-and-relocation.test
test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test
tools/llvm-objcopy/ELF/ELFObjcopy.cpp

index 7547fa0395c1bf23223214819ac3e209edc21ce7..ed46c3061ddda2aa54a4483957812a452eb3aaa8 100644 (file)
@@ -3,7 +3,7 @@
 # RUN: llvm-objcopy -O binary --only-section .text %t %t3
 # RUN: llvm-objcopy --dump-section .text=%t4 %t %t5
 # RUN: llvm-objcopy --dump-section .foo=%t6 %t %t7
-# RUN: not llvm-objcopy --dump-section .bar=%t8 %t %t9 2>&1 | FileCheck %s --check-prefix=NOBITS
+# RUN: not llvm-objcopy --dump-section .bar=%t8 %t %t9 2>&1 | FileCheck %s --check-prefix=NOBITS -DINPUT=%t
 # RUN: od -t x1 %t2 | FileCheck %s
 # RUN: od -t x1 %t6 | FileCheck %s --check-prefix=NON-ALLOC
 # RUN: wc -c %t2 | FileCheck %s --check-prefix=SIZE
@@ -41,4 +41,4 @@ ProgramHeaders:
 
 #SIZE: 4
 
-#NOBITS: cannot dump section '.bar': it has no contents
+#NOBITS: error: '[[INPUT]]': cannot dump section '.bar': it has no contents
index 1f07fdf3496f2016652bd1e948a22e9b32bf3b36..2ff68de381bff05ac6d5e2f52ffc91c8b5371257 100644 (file)
@@ -2,8 +2,8 @@
 \r
 ## Check we cannot remove the .dynsym symbol table because dynamic\r
 ## relocation section .rela.dyn still references it via sh_link field.\r
-# RUN: not llvm-objcopy -R .dynsym %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR\r
-# ERR: error: symbol table '.dynsym' cannot be removed because it is referenced by the relocation section '.rela.dyn'\r
+# RUN: not llvm-objcopy -R .dynsym %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR -DINPUT=%t\r
+# ERR: error: '[[INPUT]]': symbol table '.dynsym' cannot be removed because it is referenced by the relocation section '.rela.dyn'\r
 \r
 ## Check we can remove .dynsym after removing the reference.\r
 # RUN: llvm-objcopy -R .dynsym -R .rela.dyn %t %t2\r
index 0a2964bb50f3aff1cf88c3b7e337e364fc559589..49779e0a4a7779bf771e2dbae09c25ac49cf031d 100644 (file)
@@ -1,8 +1,9 @@
-# RUN: not llvm-objcopy -R .dynstr %p/Inputs/dynsym.so %t 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-objcopy -R .dynstr %p/Inputs/dynsym.so %t 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%p/Inputs/dynsym.so
 # RUN: cp  %p/Inputs/dynsym.so %t2
-# RUN: not llvm-strip --no-strip-all -R .dynstr %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-strip --no-strip-all -R .dynstr %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t2
 
-# ERR: section '.dynstr' cannot be removed because it is referenced by the section '.dynsym'
+# ERR1: error: '[[INPUT]]': section '.dynstr' cannot be removed because it is referenced by the section '.dynsym'
+# ERR2: error: '[[INPUT]]': section '.dynstr' cannot be removed because it is referenced by the section '.dynsym'
 
 # RUN: llvm-objcopy --allow-broken-links -R .dynstr %p/Inputs/dynsym.so %t3
 # RUN: llvm-objdump --section-headers %t3 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.dynstr
index 24aa4c12f3eca35ea819dcf9f94d5b28eaa774dc..a66af944d2a55d876cd73d26207687fd5420f9b6 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: yaml2obj %s > %t
-# RUN: not llvm-objcopy --build-id-link-dir=%t-dir --build-id-link-input=.debug %t 2>&1 >/dev/null | FileCheck %s
+# RUN: not llvm-objcopy --build-id-link-dir=%t-dir --build-id-link-input=.debug %t 2>&1 >/dev/null | FileCheck %s -DINPUT=%t
 
-# CHECK: could not find build ID
+# CHECK: error: '[[INPUT]]': could not find build ID
 
 --- !ELF
 FileHeader:
index 0724abfd6b63a073fb01fa8b7c63ee969d9414d6..4c161de1cc65742120f20e9d0300796166a28d48 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: yaml2obj %s > %t
-# RUN: not llvm-objcopy -R .symtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-objcopy -R .symtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%t
 # RUN: cp %t %t3
-# RUN: not llvm-strip --no-strip-all -R .symtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-strip --no-strip-all -R .symtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t3
 
 !ELF
 FileHeader:
@@ -31,7 +31,8 @@ Symbols:
     Size:     4
     Binding:  STB_GLOBAL
 
-# ERR: symbol table '.symtab' cannot be removed because it is referenced by the relocation section '.rel.text'
+# ERR1: error: '[[INPUT]]': symbol table '.symtab' cannot be removed because it is referenced by the relocation section '.rel.text'
+# ERR2: error: '[[INPUT]]': symbol table '.symtab' cannot be removed because it is referenced by the relocation section '.rel.text'
 
 # RUN: llvm-objcopy --allow-broken-links -R .symtab %t %t4
 # RUN: llvm-readobj --sections %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.symtab
index 909c6d06647167d8c307934fd09b62096bd64917..53ffc9ea4352d50b84011b031ce5250d6c4fefec 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: yaml2obj %s -o %t.o
-# RUN: not llvm-objcopy -R .foo %t.o %t1 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-objcopy -R .foo %t.o %t1 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%t.o
 # RUN: cp %t.o %t2
-# RUN: not llvm-strip --no-strip-all -R .foo %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-strip --no-strip-all -R .foo %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t2
 
 --- !ELF
 FileHeader:
@@ -16,7 +16,8 @@ Sections:
     Type:            SHT_PROGBITS
     Link:            .foo
 
-# ERR: section '.foo' cannot be removed because it is referenced by the section '.bar'
+# ERR1: error: '[[INPUT]]': section '.foo' cannot be removed because it is referenced by the section '.bar'
+# ERR2: error: '[[INPUT]]': section '.foo' cannot be removed because it is referenced by the section '.bar'
 
 # RUN: llvm-objcopy --allow-broken-links -R .foo %t.o %t3
 # RUN: llvm-readobj --sections %t3 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.foo
index 4b7bc0eab017eb590f7a03d7f7e058dbc329d78a..797681fa036b316ac12fa326dbd6820e3b011850 100644 (file)
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s > %t
-# RUN: not llvm-objcopy -R .shstrtab %t %t2 2>&1 >/dev/null | FileCheck %s
+# RUN: not llvm-objcopy -R .shstrtab %t %t2 2>&1 >/dev/null | FileCheck %s -DINPUT=%t
 
 !ELF
 FileHeader:
@@ -8,4 +8,4 @@ FileHeader:
   Type:            ET_REL
   Machine:         EM_X86_64
 
-# CHECK: cannot write section header table because section header string table was removed
+# CHECK: error: '[[INPUT]]': cannot write section header table because section header string table was removed
index b0ee7573c2a43afe37ad5713a64439974577d3ea..8f0a809b7f87d682dcbe53f7bc976966b5d685b1 100644 (file)
@@ -2,8 +2,8 @@
 ## referenced by relocations contained in the object.
 
 # RUN: yaml2obj %s > %t1
-# RUN: not llvm-objcopy -R .data %t1 2>&1 | FileCheck %s
-# CHECK: error: section '.data' cannot be removed: (.text+0x1) has relocation against symbol 'foo'
+# RUN: not llvm-objcopy -R .data %t1 2>&1 | FileCheck %s -DINPUT=%t1
+# CHECK: error: '[[INPUT]]': section '.data' cannot be removed: (.text+0x1) has relocation against symbol 'foo'
 
 ## Check the behavior when we also remove the relocation section.
 ## We have no reference in this case and hence no error should be emitted.
index 007acfbe3ac506c5e4bd0d8199a9889c6a503de5..76833c04713ad8ad691e155c745112ac46a3dbd9 100644 (file)
@@ -20,8 +20,8 @@
 # STRIPSYM-NOT: bar
 # STRIPSYM-NOT: .rela.text
 
-# RUN: not llvm-objcopy --strip-symbol=bar %t %t2 2>&1 | FileCheck %s --check-prefix=ERR
-# ERR: not stripping symbol 'bar' because it is named in a relocation
+# RUN: not llvm-objcopy --strip-symbol=bar %t %t2 2>&1 | FileCheck %s --check-prefix=ERR -DINPUT=%t
+# ERR: error: '[[INPUT]]': not stripping symbol 'bar' because it is named in a relocation
 
 --- !ELF
 FileHeader:
index cb4cc26e3f0607231f9c6bc825a84657c7a386c3..7c9a46062d9ed8727b766a5fad5b1b4f37b36abf 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: yaml2obj %s > %t
-# RUN: not llvm-objcopy -R .strtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-objcopy -R .strtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR1 -DINPUT=%t
 # RUN: cp %t %t3
-# RUN: not llvm-strip --no-strip-all -R .strtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR
+# RUN: not llvm-strip --no-strip-all -R .strtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR2 -DINPUT=%t3
 
 !ELF
 FileHeader:
@@ -10,7 +10,8 @@ FileHeader:
   Type:            ET_REL
   Machine:         EM_X86_64
 
-# ERR: string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
+# ERR1: error: '[[INPUT]]': string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
+# ERR2: error: '[[INPUT]]': string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
 
 # RUN: llvm-objcopy --allow-broken-links -R .strtab %t %t4
 # RUN: llvm-objdump --section-headers %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.strtab
index b14af2c128b001f658695d645155f578486ca91f..b2e750d15f03f0881eeb3ad83bee9539aa45a1fa 100644 (file)
@@ -156,8 +156,12 @@ static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
 
 template <class ELFT>
 static Expected<ArrayRef<uint8_t>>
-findBuildID(const object::ELFFile<ELFT> &In) {
-  for (const auto &Phdr : unwrapOrError(In.program_headers())) {
+findBuildID(const CopyConfig &Config, const object::ELFFile<ELFT> &In) {
+  auto PhdrsOrErr = In.program_headers();
+  if (auto Err = PhdrsOrErr.takeError())
+    return createFileError(Config.InputFilename, std::move(Err));
+
+  for (const auto &Phdr : *PhdrsOrErr) {
     if (Phdr.p_type != PT_NOTE)
       continue;
     Error Err = Error::success();
@@ -165,22 +169,25 @@ findBuildID(const object::ELFFile<ELFT> &In) {
       if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
         return Note.getDesc();
     if (Err)
-      return std::move(Err);
+      return createFileError(Config.InputFilename, std::move(Err));
   }
-  return createStringError(llvm::errc::invalid_argument,
-                           "could not find build ID");
+
+  return createFileError(
+      Config.InputFilename,
+      createStringError(llvm::errc::invalid_argument,
+                        "could not find build ID"));
 }
 
 static Expected<ArrayRef<uint8_t>>
-findBuildID(const object::ELFObjectFileBase &In) {
+findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) {
   if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
-    return findBuildID(*O->getELFFile());
+    return findBuildID(Config, *O->getELFFile());
   else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
-    return findBuildID(*O->getELFFile());
+    return findBuildID(Config, *O->getELFFile());
   else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
-    return findBuildID(*O->getELFFile());
+    return findBuildID(Config, *O->getELFFile());
   else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
-    return findBuildID(*O->getELFFile());
+    return findBuildID(Config, *O->getELFFile());
 
   llvm_unreachable("Bad file format");
 }
@@ -681,7 +688,7 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
     StringRef SecName = SecPair.first;
     StringRef File = SecPair.second;
     if (Error E = dumpSectionToFile(SecName, File, Obj))
-      return createFileError(Config.InputFilename, std::move(E));
+      return E;
   }
 
   if (!Config.AddGnuDebugLink.empty())
@@ -730,7 +737,11 @@ Error executeObjcopyOnBinary(const CopyConfig &Config,
   ArrayRef<uint8_t> BuildIdBytes;
 
   if (!Config.BuildIdLinkDir.empty()) {
-    BuildIdBytes = unwrapOrError(findBuildID(In));
+    auto BuildIdBytesOrErr = findBuildID(Config, In);
+    if (auto E = BuildIdBytesOrErr.takeError())
+      return E;
+    BuildIdBytes = *BuildIdBytesOrErr;
+
     if (BuildIdBytes.size() < 2)
       return createFileError(
           Config.InputFilename,
@@ -745,18 +756,19 @@ Error executeObjcopyOnBinary(const CopyConfig &Config,
       return E;
 
   if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
-    return E;
+    return createFileError(Config.InputFilename, std::move(E));
+
   std::unique_ptr<Writer> Writer =
       createWriter(Config, *Obj, Out, OutputElfType);
   if (Error E = Writer->finalize())
-    return E;
+    return createFileError(Config.InputFilename, std::move(E));
   if (Error E = Writer->write())
     return E;
   if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
     if (Error E =
             linkToBuildIdDir(Config, Config.OutputFilename,
                              Config.BuildIdLinkOutput.getValue(), BuildIdBytes))
-      return E;
+      return createFileError(Config.OutputFilename, std::move(E));
 
   return Error::success();
 }