return Error::success();
}
-void executeObjcopyOnBinary(const CopyConfig &Config,
- COFFObjectFile &In, Buffer &Out) {
+Error executeObjcopyOnBinary(const CopyConfig &Config, COFFObjectFile &In,
+ Buffer &Out) {
COFFReader Reader(In);
Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create();
if (!ObjOrErr)
- reportError(Config.InputFilename, ObjOrErr.takeError());
+ return createFileError(Config.InputFilename, ObjOrErr.takeError());
Object *Obj = ObjOrErr->get();
assert(Obj && "Unable to deserialize COFF object");
if (Error E = handleArgs(Config, *Obj))
- reportError(Config.InputFilename, std::move(E));
+ return createFileError(Config.InputFilename, std::move(E));
COFFWriter Writer(*Obj, Out);
if (Error E = Writer.write())
- reportError(Config.OutputFilename, std::move(E));
+ return createFileError(Config.OutputFilename, std::move(E));
+ return Error::success();
}
} // end namespace coff
}
}
-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); });
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,
// 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;
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()),
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();
// (-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.
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
/// The function executeObjcopyOnRawBinary does the dispatch based on the format
/// of the output specified by the command line options.
-static void executeObjcopyOnRawBinary(const CopyConfig &Config,
- MemoryBuffer &In, Buffer &Out) {
+static Error executeObjcopyOnRawBinary(const CopyConfig &Config,
+ MemoryBuffer &In, Buffer &Out) {
// TODO: llvm-objcopy should parse CopyConfig.OutputFormat to recognize
// formats other than ELF / "binary" and invoke
// elf::executeObjcopyOnRawBinary, macho::executeObjcopyOnRawBinary or
/// The function executeObjcopyOnBinary does the dispatch based on the format
/// of the input binary (ELF, MachO or COFF).
-static void executeObjcopyOnBinary(const CopyConfig &Config, object::Binary &In,
- Buffer &Out) {
+static Error executeObjcopyOnBinary(const CopyConfig &Config,
+ object::Binary &In, Buffer &Out) {
if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In))
return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out);
else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In))
return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out);
else
- error("Unsupported object file format");
+ return createStringError(object_error::invalid_file_type,
+ "Unsupported object file format");
}
-static void executeObjcopyOnArchive(const CopyConfig &Config,
- const Archive &Ar) {
+static Error executeObjcopyOnArchive(const CopyConfig &Config,
+ const Archive &Ar) {
std::vector<NewArchiveMember> NewArchiveMembers;
Error Err = Error::success();
for (const Archive::Child &Child : Ar.children(Err)) {
reportError(Ar.getFileName(), ChildNameOrErr.takeError());
MemBuffer MB(ChildNameOrErr.get());
- executeObjcopyOnBinary(Config, *Bin, MB);
+ if (Error E = executeObjcopyOnBinary(Config, *Bin, MB))
+ return E;
Expected<NewArchiveMember> Member =
NewArchiveMember::getOldMember(Child, Config.DeterministicArchives);
Ar.hasSymbolTable(), Ar.kind(),
Config.DeterministicArchives, Ar.isThin()))
reportError(Config.OutputFilename, std::move(E));
+ return Error::success();
}
static void restoreDateOnFile(StringRef Filename,
if (!BufOrErr)
reportError(Config.InputFilename, BufOrErr.getError());
FileBuffer FB(Config.OutputFilename);
- executeObjcopyOnRawBinary(Config, *BufOrErr->get(), FB);
+ if (Error E = executeObjcopyOnRawBinary(Config, *BufOrErr->get(), FB))
+ error(std::move(E));
} else {
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
createBinary(Config.InputFilename);
reportError(Config.InputFilename, BinaryOrErr.takeError());
if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
- executeObjcopyOnArchive(Config, *Ar);
+ if (Error E = executeObjcopyOnArchive(Config, *Ar))
+ error(std::move(E));
} else {
FileBuffer FB(Config.OutputFilename);
- executeObjcopyOnBinary(Config, *BinaryOrErr.get().getBinary(), FB);
+ if (Error E = executeObjcopyOnBinary(Config,
+ *BinaryOrErr.get().getBinary(), FB))
+ error(std::move(E));
}
}