11
11
#include " Object.h"
12
12
#include " llvm/BinaryFormat/MachO.h"
13
13
#include " llvm/Object/MachO.h"
14
+ #include " llvm/Support/Errc.h"
14
15
#include < memory>
15
16
16
17
namespace llvm {
@@ -59,9 +60,8 @@ template <> Section constructSection(MachO::section_64 Sec, uint32_t Index) {
59
60
return S;
60
61
}
61
62
62
- // TODO: get rid of reportError and make MachOReader return Expected<> instead.
63
63
template <typename SectionType, typename SegmentType>
64
- std::vector<std::unique_ptr<Section>>
64
+ Expected< std::vector<std::unique_ptr<Section> >>
65
65
extractSections (const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
66
66
const object::MachOObjectFile &MachOObj,
67
67
uint32_t &NextSectionIndex) {
@@ -86,14 +86,15 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
86
86
Expected<object::SectionRef> SecRef =
87
87
MachOObj.getSection (NextSectionIndex++);
88
88
if (!SecRef)
89
- reportError (MachOObj. getFileName (), SecRef.takeError () );
89
+ return SecRef.takeError ();
90
90
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 ());
97
98
98
99
S.Relocations .reserve (S.NReloc );
99
100
for (auto RI = MachOObj.section_rel_begin (SecRef->getRawDataRefImpl ()),
@@ -113,7 +114,7 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
113
114
return Sections;
114
115
}
115
116
116
- void MachOReader::readLoadCommands (Object &O) const {
117
+ Error MachOReader::readLoadCommands (Object &O) const {
117
118
// For MachO sections indices start from 1.
118
119
uint32_t NextSectionIndex = 1 ;
119
120
for (auto LoadCmd : MachOObj.load_commands ()) {
@@ -123,13 +124,20 @@ void MachOReader::readLoadCommands(Object &O) const {
123
124
O.CodeSignatureCommandIndex = O.LoadCommands .size ();
124
125
break ;
125
126
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 ();
128
133
break ;
129
134
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 ();
133
141
break ;
134
142
case MachO::LC_SYMTAB:
135
143
O.SymTabCommandIndex = O.LoadCommands .size ();
@@ -177,6 +185,7 @@ void MachOReader::readLoadCommands(Object &O) const {
177
185
}
178
186
O.LoadCommands .push_back (std::move (LC));
179
187
}
188
+ return Error::success ();
180
189
}
181
190
182
191
template <typename nlist_t >
@@ -308,10 +317,11 @@ void MachOReader::readSwiftVersion(Object &O) const {
308
317
}
309
318
}
310
319
311
- std::unique_ptr<Object> MachOReader::create () const {
320
+ Expected< std::unique_ptr<Object> > MachOReader::create () const {
312
321
auto Obj = std::make_unique<Object>();
313
322
readHeader (*Obj);
314
- readLoadCommands (*Obj);
323
+ if (Error E = readLoadCommands (*Obj))
324
+ return std::move (E);
315
325
readSymbolTable (*Obj);
316
326
setSymbolInRelocationInfo (*Obj);
317
327
readRebaseInfo (*Obj);
0 commit comments