@@ -139,35 +139,28 @@ static void doList(opt::InputArgList& Args) {
139
139
fatalOpenError (std::move (Err), B->getBufferIdentifier ());
140
140
}
141
141
142
- static COFF::MachineTypes getCOFFFileMachine (MemoryBufferRef MB) {
142
+ static Expected< COFF::MachineTypes> getCOFFFileMachine (MemoryBufferRef MB) {
143
143
std::error_code EC;
144
144
auto Obj = object::COFFObjectFile::create (MB);
145
- if (!Obj) {
146
- llvm::errs () << MB.getBufferIdentifier ()
147
- << " : failed to open: " << Obj.takeError () << ' \n ' ;
148
- exit (1 );
149
- }
145
+ if (!Obj)
146
+ return Obj.takeError ();
150
147
151
148
uint16_t Machine = (*Obj)->getMachine ();
152
149
if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
153
150
Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
154
151
Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
155
152
Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
156
- llvm::errs () << MB.getBufferIdentifier () << " : unknown machine: " << Machine
157
- << ' \n ' ;
158
- exit (1 );
153
+ return createStringError (inconvertibleErrorCode (),
154
+ " unknown machine: " + std::to_string (Machine));
159
155
}
160
156
161
157
return static_cast <COFF::MachineTypes>(Machine);
162
158
}
163
159
164
- static COFF::MachineTypes getBitcodeFileMachine (MemoryBufferRef MB) {
160
+ static Expected< COFF::MachineTypes> getBitcodeFileMachine (MemoryBufferRef MB) {
165
161
Expected<std::string> TripleStr = getBitcodeTargetTriple (MB);
166
- if (!TripleStr) {
167
- llvm::errs () << MB.getBufferIdentifier ()
168
- << " : failed to get target triple from bitcode\n " ;
169
- exit (1 );
170
- }
162
+ if (!TripleStr)
163
+ return TripleStr.takeError ();
171
164
172
165
switch (Triple (*TripleStr).getArch ()) {
173
166
case Triple::x86:
@@ -179,9 +172,8 @@ static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) {
179
172
case Triple::aarch64:
180
173
return COFF::IMAGE_FILE_MACHINE_ARM64;
181
174
default :
182
- llvm::errs () << MB.getBufferIdentifier ()
183
- << " : unknown arch in target triple " << *TripleStr << ' \n ' ;
184
- exit (1 );
175
+ return createStringError (inconvertibleErrorCode (),
176
+ " unknown arch in target triple: " + *TripleStr);
185
177
}
186
178
}
187
179
@@ -201,7 +193,7 @@ static void appendFile(std::vector<NewArchiveMember> &Members,
201
193
202
194
// If a user attempts to add an archive to another archive, llvm-lib doesn't
203
195
// handle the first archive file as a single file. Instead, it extracts all
204
- // members from the archive and add them to the second archive. This beahvior
196
+ // members from the archive and add them to the second archive. This behavior
205
197
// is for compatibility with Microsoft's lib command.
206
198
if (Magic == file_magic::archive) {
207
199
Error Err = Error::success ();
@@ -233,9 +225,17 @@ static void appendFile(std::vector<NewArchiveMember> &Members,
233
225
// in writeArchive() which needs to support many tools, can't assume the
234
226
// input is COFF, and doesn't have a good way to report errors.
235
227
if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
236
- COFF::MachineTypes FileMachine = (Magic == file_magic::coff_object)
237
- ? getCOFFFileMachine (MB)
238
- : getBitcodeFileMachine (MB);
228
+ Expected<COFF::MachineTypes> MaybeFileMachine =
229
+ (Magic == file_magic::coff_object) ? getCOFFFileMachine (MB)
230
+ : getBitcodeFileMachine (MB);
231
+ if (!MaybeFileMachine) {
232
+ handleAllErrors (MaybeFileMachine.takeError (), [&](const ErrorInfoBase &EIB) {
233
+ llvm::errs () << MB.getBufferIdentifier () << " : " << EIB.message ()
234
+ << " \n " ;
235
+ });
236
+ exit (1 );
237
+ }
238
+ COFF::MachineTypes FileMachine = *MaybeFileMachine;
239
239
240
240
// FIXME: Once lld-link rejects multiple resource .obj files:
241
241
// Call convertResToCOFF() on .res files and add the resulting
0 commit comments