1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00

[llvm-objcopy][NFC] More error propagation

Summary: Do some more error cleanup, removing some dependencies from llvm-objcopy's error/reportError in [ELF/COFF]Objcopy methods.

Reviewers: jhenderson, alexshap, jakehehrlich, mstorsjo, espindola

Subscribers: emaste, arichardson

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

llvm-svn: 352625
This commit is contained in:
Jordan Rupprecht 2019-01-30 14:36:53 +00:00
parent 3a34ed2049
commit aa17cc1f08
6 changed files with 65 additions and 48 deletions

View 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
# CHECK: build ID in file {{.*}} is smaller than two bytes.
# CHECK: build ID is smaller than two bytes.
--- !ELF
FileHeader:

View File

@ -188,19 +188,20 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
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

View File

@ -10,6 +10,7 @@
#define LLVM_TOOLS_OBJCOPY_COFFOBJCOPY_H
namespace llvm {
class Error;
namespace object {
class COFFObjectFile;
@ -20,7 +21,7 @@ struct CopyConfig;
class Buffer;
namespace coff {
void executeObjcopyOnBinary(const CopyConfig &Config,
Error executeObjcopyOnBinary(const CopyConfig &Config,
object::COFFObjectFile &In, Buffer &Out);
} // end namespace coff

View File

@ -178,7 +178,7 @@ static void linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
}
}
static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
StringRef File, ElfType OutputElfType) {
auto DWOFile = Reader.create();
DWOFile->removeSections(
@ -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,
static Error handleArgs(const CopyConfig &Config, Object &Obj,
const Reader &Reader, ElfType OutputElfType) {
if (!Config.SplitDWO.empty()) {
splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType);
}
if (!Config.SplitDWO.empty())
if (Error E =
splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType))
return E;
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,15 +539,17 @@ 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,
Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
Buffer &Out) {
BinaryReader Reader(Config.BinaryArch, &In);
std::unique_ptr<Object> Obj = Reader.create();
@ -555,16 +558,16 @@ 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,
Error executeObjcopyOnBinary(const CopyConfig &Config,
object::ELFObjectFileBase &In, Buffer &Out) {
ELFReader Reader(&In);
std::unique_ptr<Object> Obj = Reader.create();
@ -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

View File

@ -10,6 +10,7 @@
#define LLVM_TOOLS_OBJCOPY_ELFOBJCOPY_H
namespace llvm {
class Error;
class MemoryBuffer;
namespace object {
@ -21,9 +22,9 @@ struct CopyConfig;
class Buffer;
namespace elf {
void executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
Buffer &Out);
void executeObjcopyOnBinary(const CopyConfig &Config,
Error executeObjcopyOnBinary(const CopyConfig &Config,
object::ELFObjectFileBase &In, Buffer &Out);
} // end namespace elf

View File

@ -122,7 +122,7 @@ static Error deepWriteArchive(StringRef ArcName,
/// 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,
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
@ -133,17 +133,18 @@ static void executeObjcopyOnRawBinary(const CopyConfig &Config,
/// 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,
static Error executeObjcopyOnArchive(const CopyConfig &Config,
const Archive &Ar) {
std::vector<NewArchiveMember> NewArchiveMembers;
Error Err = Error::success();
@ -158,7 +159,8 @@ static void executeObjcopyOnArchive(const CopyConfig &Config,
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);
@ -175,6 +177,7 @@ static void executeObjcopyOnArchive(const CopyConfig &Config,
Ar.hasSymbolTable(), Ar.kind(),
Config.DeterministicArchives, Ar.isThin()))
reportError(Config.OutputFilename, std::move(E));
return Error::success();
}
static void restoreDateOnFile(StringRef Filename,
@ -207,7 +210,8 @@ static void executeObjcopy(const CopyConfig &Config) {
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);
@ -215,10 +219,13 @@ static void executeObjcopy(const CopyConfig &Config) {
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));
}
}