1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[llvm-objcopy][NFC] refactor error handling. part 1.

Remove usages of special error reporting functions(error(),
reportError()). This patch is extracted from D87987.
Errors are reported as Expected<>/Error returning values.
This part is for MachO subfolder of llvm-objcopy.

Testing: check-all.

Reviewed By: jhenderson, alexshap

Differential Revision: https://reviews.llvm.org/D88113
This commit is contained in:
Alexey Lapshin 2020-09-22 20:47:02 +03:00
parent 4e395520a6
commit 5fe04015c0
3 changed files with 34 additions and 27 deletions

View File

@ -360,14 +360,11 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
Error executeObjcopyOnBinary(const CopyConfig &Config,
object::MachOObjectFile &In, Buffer &Out) {
MachOReader Reader(In);
std::unique_ptr<Object> O = Reader.create();
Expected<std::unique_ptr<Object>> O = Reader.create();
if (!O)
return createFileError(
Config.InputFilename,
createStringError(object_error::parse_failed,
"unable to deserialize MachO object"));
return createFileError(Config.InputFilename, O.takeError());
if (Error E = handleArgs(Config, *O))
if (Error E = handleArgs(Config, **O))
return createFileError(Config.InputFilename, std::move(E));
// Page size used for alignment of segment sizes in Mach-O executables and
@ -383,7 +380,7 @@ Error executeObjcopyOnBinary(const CopyConfig &Config,
PageSize = 4096;
}
MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
if (auto E = Writer.finalize())
return E;
return Writer.write();

View File

@ -11,6 +11,7 @@
#include "Object.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/Object/MachO.h"
#include "llvm/Support/Errc.h"
#include <memory>
namespace llvm {
@ -59,9 +60,8 @@ template <> Section constructSection(MachO::section_64 Sec, uint32_t Index) {
return S;
}
// TODO: get rid of reportError and make MachOReader return Expected<> instead.
template <typename SectionType, typename SegmentType>
std::vector<std::unique_ptr<Section>>
Expected<std::vector<std::unique_ptr<Section>>>
extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
const object::MachOObjectFile &MachOObj,
uint32_t &NextSectionIndex) {
@ -86,14 +86,15 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
Expected<object::SectionRef> SecRef =
MachOObj.getSection(NextSectionIndex++);
if (!SecRef)
reportError(MachOObj.getFileName(), SecRef.takeError());
return SecRef.takeError();
if (Expected<ArrayRef<uint8_t>> E =
MachOObj.getSectionContents(SecRef->getRawDataRefImpl()))
S.Content =
StringRef(reinterpret_cast<const char *>(E->data()), E->size());
else
reportError(MachOObj.getFileName(), E.takeError());
Expected<ArrayRef<uint8_t>> Data =
MachOObj.getSectionContents(SecRef->getRawDataRefImpl());
if (!Data)
return Data.takeError();
S.Content =
StringRef(reinterpret_cast<const char *>(Data->data()), Data->size());
S.Relocations.reserve(S.NReloc);
for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()),
@ -113,7 +114,7 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
return Sections;
}
void MachOReader::readLoadCommands(Object &O) const {
Error MachOReader::readLoadCommands(Object &O) const {
// For MachO sections indices start from 1.
uint32_t NextSectionIndex = 1;
for (auto LoadCmd : MachOObj.load_commands()) {
@ -123,13 +124,20 @@ void MachOReader::readLoadCommands(Object &O) const {
O.CodeSignatureCommandIndex = O.LoadCommands.size();
break;
case MachO::LC_SEGMENT:
LC.Sections = extractSections<MachO::section, MachO::segment_command>(
LoadCmd, MachOObj, NextSectionIndex);
if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
extractSections<MachO::section, MachO::segment_command>(
LoadCmd, MachOObj, NextSectionIndex))
LC.Sections = std::move(*Sections);
else
return Sections.takeError();
break;
case MachO::LC_SEGMENT_64:
LC.Sections =
extractSections<MachO::section_64, MachO::segment_command_64>(
LoadCmd, MachOObj, NextSectionIndex);
if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
extractSections<MachO::section_64, MachO::segment_command_64>(
LoadCmd, MachOObj, NextSectionIndex))
LC.Sections = std::move(*Sections);
else
return Sections.takeError();
break;
case MachO::LC_SYMTAB:
O.SymTabCommandIndex = O.LoadCommands.size();
@ -177,6 +185,7 @@ void MachOReader::readLoadCommands(Object &O) const {
}
O.LoadCommands.push_back(std::move(LC));
}
return Error::success();
}
template <typename nlist_t>
@ -308,10 +317,11 @@ void MachOReader::readSwiftVersion(Object &O) const {
}
}
std::unique_ptr<Object> MachOReader::create() const {
Expected<std::unique_ptr<Object>> MachOReader::create() const {
auto Obj = std::make_unique<Object>();
readHeader(*Obj);
readLoadCommands(*Obj);
if (Error E = readLoadCommands(*Obj))
return std::move(E);
readSymbolTable(*Obj);
setSymbolInRelocationInfo(*Obj);
readRebaseInfo(*Obj);

View File

@ -21,14 +21,14 @@ namespace macho {
class Reader {
public:
virtual ~Reader(){};
virtual std::unique_ptr<Object> create() const = 0;
virtual Expected<std::unique_ptr<Object>> create() const = 0;
};
class MachOReader : public Reader {
const object::MachOObjectFile &MachOObj;
void readHeader(Object &O) const;
void readLoadCommands(Object &O) const;
Error readLoadCommands(Object &O) const;
void readSymbolTable(Object &O) const;
void setSymbolInRelocationInfo(Object &O) const;
void readRebaseInfo(Object &O) const;
@ -46,7 +46,7 @@ class MachOReader : public Reader {
public:
explicit MachOReader(const object::MachOObjectFile &Obj) : MachOObj(Obj) {}
std::unique_ptr<Object> create() const override;
Expected<std::unique_ptr<Object>> create() const override;
};
} // end namespace macho