Skip to content

Commit 3ed04f9

Browse files
committed
[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
1 parent 4e53490 commit 3ed04f9

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,11 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
360360
Error executeObjcopyOnBinary(const CopyConfig &Config,
361361
object::MachOObjectFile &In, Buffer &Out) {
362362
MachOReader Reader(In);
363-
std::unique_ptr<Object> O = Reader.create();
363+
Expected<std::unique_ptr<Object>> O = Reader.create();
364364
if (!O)
365-
return createFileError(
366-
Config.InputFilename,
367-
createStringError(object_error::parse_failed,
368-
"unable to deserialize MachO object"));
365+
return createFileError(Config.InputFilename, O.takeError());
369366

370-
if (Error E = handleArgs(Config, *O))
367+
if (Error E = handleArgs(Config, **O))
371368
return createFileError(Config.InputFilename, std::move(E));
372369

373370
// Page size used for alignment of segment sizes in Mach-O executables and
@@ -383,7 +380,7 @@ Error executeObjcopyOnBinary(const CopyConfig &Config,
383380
PageSize = 4096;
384381
}
385382

386-
MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
383+
MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
387384
if (auto E = Writer.finalize())
388385
return E;
389386
return Writer.write();

llvm/tools/llvm-objcopy/MachO/MachOReader.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Object.h"
1212
#include "llvm/BinaryFormat/MachO.h"
1313
#include "llvm/Object/MachO.h"
14+
#include "llvm/Support/Errc.h"
1415
#include <memory>
1516

1617
namespace llvm {
@@ -59,9 +60,8 @@ template <> Section constructSection(MachO::section_64 Sec, uint32_t Index) {
5960
return S;
6061
}
6162

62-
// TODO: get rid of reportError and make MachOReader return Expected<> instead.
6363
template <typename SectionType, typename SegmentType>
64-
std::vector<std::unique_ptr<Section>>
64+
Expected<std::vector<std::unique_ptr<Section>>>
6565
extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
6666
const object::MachOObjectFile &MachOObj,
6767
uint32_t &NextSectionIndex) {
@@ -86,14 +86,15 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
8686
Expected<object::SectionRef> SecRef =
8787
MachOObj.getSection(NextSectionIndex++);
8888
if (!SecRef)
89-
reportError(MachOObj.getFileName(), SecRef.takeError());
89+
return SecRef.takeError();
9090

91-
if (Expected<ArrayRef<uint8_t>> E =
92-
MachOObj.getSectionContents(SecRef->getRawDataRefImpl()))
93-
S.Content =
94-
StringRef(reinterpret_cast<const char *>(E->data()), E->size());
95-
else
96-
reportError(MachOObj.getFileName(), E.takeError());
91+
Expected<ArrayRef<uint8_t>> Data =
92+
MachOObj.getSectionContents(SecRef->getRawDataRefImpl());
93+
if (!Data)
94+
return Data.takeError();
95+
96+
S.Content =
97+
StringRef(reinterpret_cast<const char *>(Data->data()), Data->size());
9798

9899
S.Relocations.reserve(S.NReloc);
99100
for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()),
@@ -113,7 +114,7 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
113114
return Sections;
114115
}
115116

116-
void MachOReader::readLoadCommands(Object &O) const {
117+
Error MachOReader::readLoadCommands(Object &O) const {
117118
// For MachO sections indices start from 1.
118119
uint32_t NextSectionIndex = 1;
119120
for (auto LoadCmd : MachOObj.load_commands()) {
@@ -123,13 +124,20 @@ void MachOReader::readLoadCommands(Object &O) const {
123124
O.CodeSignatureCommandIndex = O.LoadCommands.size();
124125
break;
125126
case MachO::LC_SEGMENT:
126-
LC.Sections = extractSections<MachO::section, MachO::segment_command>(
127-
LoadCmd, MachOObj, NextSectionIndex);
127+
if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
128+
extractSections<MachO::section, MachO::segment_command>(
129+
LoadCmd, MachOObj, NextSectionIndex))
130+
LC.Sections = std::move(*Sections);
131+
else
132+
return Sections.takeError();
128133
break;
129134
case MachO::LC_SEGMENT_64:
130-
LC.Sections =
131-
extractSections<MachO::section_64, MachO::segment_command_64>(
132-
LoadCmd, MachOObj, NextSectionIndex);
135+
if (Expected<std::vector<std::unique_ptr<Section>>> Sections =
136+
extractSections<MachO::section_64, MachO::segment_command_64>(
137+
LoadCmd, MachOObj, NextSectionIndex))
138+
LC.Sections = std::move(*Sections);
139+
else
140+
return Sections.takeError();
133141
break;
134142
case MachO::LC_SYMTAB:
135143
O.SymTabCommandIndex = O.LoadCommands.size();
@@ -177,6 +185,7 @@ void MachOReader::readLoadCommands(Object &O) const {
177185
}
178186
O.LoadCommands.push_back(std::move(LC));
179187
}
188+
return Error::success();
180189
}
181190

182191
template <typename nlist_t>
@@ -308,10 +317,11 @@ void MachOReader::readSwiftVersion(Object &O) const {
308317
}
309318
}
310319

311-
std::unique_ptr<Object> MachOReader::create() const {
320+
Expected<std::unique_ptr<Object>> MachOReader::create() const {
312321
auto Obj = std::make_unique<Object>();
313322
readHeader(*Obj);
314-
readLoadCommands(*Obj);
323+
if (Error E = readLoadCommands(*Obj))
324+
return std::move(E);
315325
readSymbolTable(*Obj);
316326
setSymbolInRelocationInfo(*Obj);
317327
readRebaseInfo(*Obj);

llvm/tools/llvm-objcopy/MachO/MachOReader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ namespace macho {
2121
class Reader {
2222
public:
2323
virtual ~Reader(){};
24-
virtual std::unique_ptr<Object> create() const = 0;
24+
virtual Expected<std::unique_ptr<Object>> create() const = 0;
2525
};
2626

2727
class MachOReader : public Reader {
2828
const object::MachOObjectFile &MachOObj;
2929

3030
void readHeader(Object &O) const;
31-
void readLoadCommands(Object &O) const;
31+
Error readLoadCommands(Object &O) const;
3232
void readSymbolTable(Object &O) const;
3333
void setSymbolInRelocationInfo(Object &O) const;
3434
void readRebaseInfo(Object &O) const;
@@ -46,7 +46,7 @@ class MachOReader : public Reader {
4646
public:
4747
explicit MachOReader(const object::MachOObjectFile &Obj) : MachOObj(Obj) {}
4848

49-
std::unique_ptr<Object> create() const override;
49+
Expected<std::unique_ptr<Object>> create() const override;
5050
};
5151

5252
} // end namespace macho

0 commit comments

Comments
 (0)