[llvm] 3ed04f9 - [llvm-objcopy][NFC] refactor error handling. part 1.
Alexey Lapshin via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 24 04:39:28 PDT 2020
Author: Alexey Lapshin
Date: 2020-09-24T14:37:30+03:00
New Revision: 3ed04f93e30121867a813a220452b97aebeb1730
URL: https://github.com/llvm/llvm-project/commit/3ed04f93e30121867a813a220452b97aebeb1730
DIFF: https://github.com/llvm/llvm-project/commit/3ed04f93e30121867a813a220452b97aebeb1730.diff
LOG: [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
Added:
Modified:
llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
llvm/tools/llvm-objcopy/MachO/MachOReader.h
Removed:
################################################################################
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp b/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
index e87487eb5f8f..47a08d33002a 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
@@ -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();
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
index 99bcec7f6b51..6b08f294c1d4 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -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);
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.h b/llvm/tools/llvm-objcopy/MachO/MachOReader.h
index 65824b6eb389..b446e02865e5 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOReader.h
+++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.h
@@ -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
More information about the llvm-commits
mailing list