Index: llvm/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test =================================================================== --- llvm/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test +++ llvm/test/tools/llvm-objcopy/ELF/dynrelocsec-remove-shlink-reference.test @@ -3,7 +3,7 @@ ## Check we cannot remove the .dynsym symbol table because dynamic ## relocation section .rela.dyn still references it via sh_link field. # RUN: not llvm-objcopy -R .dynsym %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR -# ERR: error: Symbol table .dynsym cannot be removed because it is referenced by the relocation section .rela.dyn. +# ERR: error: '{{.*}}': Symbol table .dynsym cannot be removed because it is referenced by the relocation section .rela.dyn. ## Check we can remove .dynsym after removing the reference. # RUN: llvm-objcopy -R .dynsym -R .rela.dyn %t %t2 Index: llvm/test/tools/llvm-objcopy/ELF/strip-section-err.test =================================================================== --- llvm/test/tools/llvm-objcopy/ELF/strip-section-err.test +++ llvm/test/tools/llvm-objcopy/ELF/strip-section-err.test @@ -3,7 +3,7 @@ # RUN: yaml2obj %s > %t1 # RUN: not llvm-objcopy -R .data %t1 2>&1 | FileCheck %s -# CHECK: error: Section .data can't be removed: (.text+0x1) has relocation against symbol 'foo' +# CHECK: error: '{{.*}}': Section .data can't 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: llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -157,7 +157,11 @@ template static Expected> findBuildID(const object::ELFFile &In) { - for (const auto &Phdr : unwrapOrError(In.program_headers())) { + auto PhdrsOrErr = In.program_headers(); + if (auto Err = PhdrsOrErr.takeError()) + return std::move(Err); + + for (const auto &Phdr : *PhdrsOrErr) { if (Phdr.p_type != PT_NOTE) continue; Error Err = Error::success(); @@ -681,7 +685,7 @@ StringRef SecName = SecPair.first; StringRef File = SecPair.second; if (Error E = dumpSectionToFile(SecName, File, Obj)) - return createFileError(Config.InputFilename, std::move(E)); + return createFileError(File, std::move(E)); } if (!Config.AddGnuDebugLink.empty()) @@ -730,7 +734,11 @@ ArrayRef BuildIdBytes; if (!Config.BuildIdLinkDir.empty()) { - BuildIdBytes = unwrapOrError(findBuildID(In)); + auto BuildIdBytesOrErr = findBuildID(In); + if (auto E = BuildIdBytesOrErr.takeError()) + return createFileError(Config.InputFilename, std::move(E)); + BuildIdBytes = *BuildIdBytesOrErr; + if (BuildIdBytes.size() < 2) return createFileError( Config.InputFilename, @@ -742,21 +750,22 @@ if (Error E = linkToBuildIdDir(Config, Config.InputFilename, Config.BuildIdLinkInput.getValue(), BuildIdBytes)) - return E; + return createFileError(Config.InputFilename, std::move(E)); if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) - return E; + return createFileError(Config.InputFilename, std::move(E)); + std::unique_ptr 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; + return createFileError(Config.OutputFilename, std::move(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(); }