diff options
Diffstat (limited to 'llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp')
| -rw-r--r-- | llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp index 925999630e6..7ba8db6d38d 100644 --- a/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -178,8 +178,8 @@ static void linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink, } } -static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader, - StringRef File, ElfType OutputElfType) { +static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader, + StringRef File, ElfType OutputElfType) { auto DWOFile = Reader.create(); DWOFile->removeSections( [&](const SectionBase &Sec) { return onlyKeepDWOPred(*DWOFile, Sec); }); @@ -188,9 +188,8 @@ static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader, FileBuffer FB(File); auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType); if (Error E = Writer->finalize()) - error(std::move(E)); - if (Error E = Writer->write()) - error(std::move(E)); + return E; + return Writer->write(); } static Error dumpSectionToFile(StringRef SecName, StringRef Filename, @@ -269,12 +268,14 @@ static void replaceDebugSections( // any previous removals. Lastly whether or not something is removed shouldn't // depend a) on the order the options occur in or b) on some opaque priority // system. The only priority is that keeps/copies overrule removes. -static void handleArgs(const CopyConfig &Config, Object &Obj, - const Reader &Reader, ElfType OutputElfType) { +static Error handleArgs(const CopyConfig &Config, Object &Obj, + const Reader &Reader, ElfType OutputElfType) { + + if (!Config.SplitDWO.empty()) + if (Error E = + splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType)) + return E; - if (!Config.SplitDWO.empty()) { - splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType); - } if (Config.OutputArch) Obj.Machine = Config.OutputArch.getValue().EMachine; @@ -520,7 +521,7 @@ static void handleArgs(const CopyConfig &Config, Object &Obj, ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = MemoryBuffer::getFile(File); if (!BufOrErr) - reportError(File, BufOrErr.getError()); + return createFileError(File, errorCodeToError(BufOrErr.getError())); std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr); ArrayRef<uint8_t> Data( reinterpret_cast<const uint8_t *>(Buf->getBufferStart()), @@ -538,16 +539,18 @@ static void handleArgs(const CopyConfig &Config, Object &Obj, StringRef SecName = SecPair.first; StringRef File = SecPair.second; if (Error E = dumpSectionToFile(SecName, File, Obj)) - reportError(Config.InputFilename, std::move(E)); + return createFileError(Config.InputFilename, std::move(E)); } } if (!Config.AddGnuDebugLink.empty()) Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink); + + return Error::success(); } -void executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, - Buffer &Out) { +Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, + Buffer &Out) { BinaryReader Reader(Config.BinaryArch, &In); std::unique_ptr<Object> Obj = Reader.create(); @@ -555,17 +558,17 @@ void executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, // (-B<arch>). const ElfType OutputElfType = getOutputElfType( Config.OutputArch ? Config.OutputArch.getValue() : Config.BinaryArch); - handleArgs(Config, *Obj, Reader, OutputElfType); + if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) + return E; std::unique_ptr<Writer> Writer = createWriter(Config, *Obj, Out, OutputElfType); if (Error E = Writer->finalize()) - error(std::move(E)); - if (Error E = Writer->write()) - error(std::move(E)); + return E; + return Writer->write(); } -void executeObjcopyOnBinary(const CopyConfig &Config, - object::ELFObjectFileBase &In, Buffer &Out) { +Error executeObjcopyOnBinary(const CopyConfig &Config, + object::ELFObjectFileBase &In, Buffer &Out) { ELFReader Reader(&In); std::unique_ptr<Object> Obj = Reader.create(); // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input. @@ -577,25 +580,29 @@ void executeObjcopyOnBinary(const CopyConfig &Config, if (!Config.BuildIdLinkDir.empty()) { BuildIdBytes = unwrapOrError(findBuildID(In)); if (BuildIdBytes.size() < 2) - error("build ID in file '" + Config.InputFilename + - "' is smaller than two bytes"); + return createFileError( + Config.InputFilename, + createStringError(object_error::parse_failed, + "build ID is smaller than two bytes.")); } if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput) { linkToBuildIdDir(Config, Config.InputFilename, Config.BuildIdLinkInput.getValue(), BuildIdBytes); } - handleArgs(Config, *Obj, Reader, OutputElfType); + if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) + return E; std::unique_ptr<Writer> Writer = createWriter(Config, *Obj, Out, OutputElfType); if (Error E = Writer->finalize()) - error(std::move(E)); + return E; if (Error E = Writer->write()) - error(std::move(E)); + return E; if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput) { linkToBuildIdDir(Config, Config.OutputFilename, Config.BuildIdLinkOutput.getValue(), BuildIdBytes); } + return Error::success(); } } // end namespace elf |

