@@ -1059,39 +1059,49 @@ class ArchiveFileHandler final : public FileHandler {
1059
1059
for (auto &C : Ar->children (Err)) {
1060
1060
++ChildIndex;
1061
1061
auto BinOrErr = C.getAsBinary ();
1062
+
1063
+ std::unique_ptr<FileHandler> FH{nullptr };
1064
+ std::unique_ptr<MemoryBuffer> Buf{nullptr };
1065
+
1062
1066
if (!BinOrErr) {
1063
1067
if (auto Err = isNotObjectErrorInvalidFileType (BinOrErr.takeError ()))
1064
1068
return Err;
1065
- continue ;
1066
- }
1067
1069
1068
- auto &Bin = BinOrErr.get ();
1069
- if (!Bin->isObject ())
1070
- continue ;
1070
+ // Handle bundled BC Files
1071
+ FH = std::make_unique<BinaryFileHandler>(BundlerConfig);
1072
+ auto MR = C.getMemoryBufferRef ();
1073
+ assert (MR);
1074
+ Buf = MemoryBuffer::getMemBuffer (*MR, false );
1075
+ } else {
1076
+ auto &Bin = BinOrErr.get ();
1077
+ if (!Bin->isObject ())
1078
+ continue ;
1071
1079
1072
- auto CheckOrErr = CheckIfObjectFileContainsExcludedTargets (C);
1073
- if (!CheckOrErr)
1074
- return CheckOrErr.takeError ();
1080
+ auto CheckOrErr = CheckIfObjectFileContainsExcludedTargets (C);
1081
+ if (!CheckOrErr)
1082
+ return CheckOrErr.takeError ();
1075
1083
1076
- if (*CheckOrErr) {
1077
- LLVM_DEBUG (outs () << " Add child to ban list. Index: " << ChildIndex
1078
- << " \n " );
1079
- ExcludedChildIndexes.emplace (ChildIndex);
1080
- }
1084
+ if (*CheckOrErr) {
1085
+ LLVM_DEBUG (outs ()
1086
+ << " Add child to ban list. Index: " << ChildIndex << " \n " );
1087
+ ExcludedChildIndexes.emplace (ChildIndex);
1088
+ }
1081
1089
1082
- auto Obj = std::unique_ptr<ObjectFile>(cast<ObjectFile>(Bin.release ()));
1083
- auto Buf = MemoryBuffer::getMemBuffer (Obj->getMemoryBufferRef (), false );
1090
+ auto Obj = std::unique_ptr<ObjectFile>(cast<ObjectFile>(Bin.release ()));
1091
+ Buf = MemoryBuffer::getMemBuffer (Obj->getMemoryBufferRef (), false );
1084
1092
1085
- // Collect the list of bundles from the object.
1086
- ObjectFileHandler OFH (std::move (Obj), BundlerConfig);
1087
- if (Error Err = OFH.ReadHeader (*Buf))
1093
+ FH = std::make_unique<ObjectFileHandler>(std::move (Obj), BundlerConfig);
1094
+ }
1095
+
1096
+ // Collect the list of bundles from the object or bundled BC file.
1097
+ if (Error Err = FH->ReadHeader (*Buf))
1088
1098
return Err;
1089
- Expected<std::optional<StringRef>> NameOrErr = OFH. ReadBundleStart (*Buf);
1099
+ Expected<std::optional<StringRef>> NameOrErr = FH-> ReadBundleStart (*Buf);
1090
1100
if (!NameOrErr)
1091
1101
return NameOrErr.takeError ();
1092
1102
while (*NameOrErr) {
1093
1103
++Bundles[**NameOrErr];
1094
- NameOrErr = OFH. ReadBundleStart (*Buf);
1104
+ NameOrErr = FH-> ReadBundleStart (*Buf);
1095
1105
if (!NameOrErr)
1096
1106
return NameOrErr.takeError ();
1097
1107
}
@@ -1144,28 +1154,40 @@ class ArchiveFileHandler final : public FileHandler {
1144
1154
continue ;
1145
1155
}
1146
1156
1157
+ std::unique_ptr<FileHandler> FH{nullptr };
1158
+ std::unique_ptr<MemoryBuffer> Buf{nullptr };
1159
+ StringRef Ext (" o" );
1160
+ if (BundlerConfig.FilesType == " aocr" ||
1161
+ BundlerConfig.FilesType == " aocx" )
1162
+ Ext = BundlerConfig.FilesType ;
1163
+
1147
1164
auto BinOrErr = C.getAsBinary ();
1148
1165
if (!BinOrErr) {
1166
+ // Not a recognized binary file. Specifically not an object file
1149
1167
if (auto Err = isNotObjectErrorInvalidFileType (BinOrErr.takeError ()))
1150
1168
return Err;
1151
- continue ;
1152
- }
1153
-
1154
- auto &Bin = BinOrErr.get ();
1155
- if (!Bin->isObject ())
1156
- continue ;
1157
-
1158
- auto Obj = std::unique_ptr<ObjectFile>(cast<ObjectFile>(Bin.release ()));
1159
- auto Buf = MemoryBuffer::getMemBuffer (Obj->getMemoryBufferRef (), false );
1160
1169
1161
- auto ChildNameOrErr = C.getName ();
1162
- if (!ChildNameOrErr)
1163
- return ChildNameOrErr.takeError ();
1170
+ if (BundlerConfig.FilesType == " aoo" ) {
1171
+ // Handle bundled BC Files
1172
+ Ext = " bc" ;
1173
+ FH = std::make_unique<BinaryFileHandler>(BundlerConfig);
1174
+ auto MR = C.getMemoryBufferRef ();
1175
+ assert (MR);
1176
+ Buf = MemoryBuffer::getMemBuffer (*MR, false );
1177
+ } else
1178
+ continue ;
1179
+ } else {
1180
+ auto &Bin = BinOrErr.get ();
1181
+ if (!Bin->isObject ())
1182
+ continue ;
1183
+ auto Obj = std::unique_ptr<ObjectFile>(cast<ObjectFile>(Bin.release ()));
1184
+ Buf = MemoryBuffer::getMemBuffer (Obj->getMemoryBufferRef (), false );
1185
+ FH = std::make_unique<ObjectFileHandler>(std::move (Obj), BundlerConfig);
1186
+ }
1164
1187
1165
- ObjectFileHandler OFH (std::move (Obj), BundlerConfig);
1166
- if (Error Err = OFH.ReadHeader (*Buf))
1188
+ if (Error Err = FH->ReadHeader (*Buf))
1167
1189
return Err;
1168
- Expected<std::optional<StringRef>> NameOrErr = OFH. ReadBundleStart (*Buf);
1190
+ Expected<std::optional<StringRef>> NameOrErr = FH-> ReadBundleStart (*Buf);
1169
1191
if (!NameOrErr)
1170
1192
return NameOrErr.takeError ();
1171
1193
while (*NameOrErr) {
@@ -1175,10 +1197,7 @@ class ArchiveFileHandler final : public FileHandler {
1175
1197
if (Mode == OutputType::FileList) {
1176
1198
// Create temporary file where the device part will be extracted to.
1177
1199
SmallString<128u > ChildFileName;
1178
- StringRef Ext (" o" );
1179
- if (BundlerConfig.FilesType == " aocr" ||
1180
- BundlerConfig.FilesType == " aocx" )
1181
- Ext = BundlerConfig.FilesType ;
1200
+
1182
1201
auto EC = sys::fs::createTemporaryFile (TempFileNameBase, Ext,
1183
1202
ChildFileName);
1184
1203
if (EC)
@@ -1188,7 +1207,7 @@ class ArchiveFileHandler final : public FileHandler {
1188
1207
if (EC)
1189
1208
return createFileError (ChildFileName, EC);
1190
1209
1191
- if (Error Err = OFH. ReadBundle (ChildOS, *Buf))
1210
+ if (Error Err = FH-> ReadBundle (ChildOS, *Buf))
1192
1211
return Err;
1193
1212
1194
1213
if (ChildOS.has_error ())
@@ -1199,13 +1218,17 @@ class ArchiveFileHandler final : public FileHandler {
1199
1218
OS << ChildFileName << " \n " ;
1200
1219
} else if (Mode == OutputType::Object) {
1201
1220
// Extract the bundle to the output file in single file mode.
1202
- if (Error Err = OFH. ReadBundle (OS, *Buf))
1221
+ if (Error Err = FH-> ReadBundle (OS, *Buf))
1203
1222
return Err;
1204
1223
} else if (Mode == OutputType::Archive) {
1224
+ auto ChildNameOrErr = C.getName ();
1225
+ if (!ChildNameOrErr)
1226
+ return ChildNameOrErr.takeError ();
1227
+
1205
1228
// Extract the bundle to a buffer.
1206
1229
SmallVector<char > Data;
1207
1230
raw_svector_ostream ChildOS{Data};
1208
- if (Error Err = OFH. ReadBundle (ChildOS, *Buf))
1231
+ if (Error Err = FH-> ReadBundle (ChildOS, *Buf))
1209
1232
return Err;
1210
1233
1211
1234
// Add new archive member.
@@ -1214,10 +1237,10 @@ class ArchiveFileHandler final : public FileHandler {
1214
1237
Member.Buf = MemoryBuffer::getMemBufferCopy (ChildOS.str (), Name);
1215
1238
Member.MemberName = Member.Buf ->getBufferIdentifier ();
1216
1239
}
1217
- if (Error Err = OFH. ReadBundleEnd (*Buf))
1240
+ if (Error Err = FH-> ReadBundleEnd (*Buf))
1218
1241
return Err;
1219
1242
}
1220
- NameOrErr = OFH. ReadBundleStart (*Buf);
1243
+ NameOrErr = FH-> ReadBundleStart (*Buf);
1221
1244
if (!NameOrErr)
1222
1245
return NameOrErr.takeError ();
1223
1246
}
0 commit comments