@@ -590,7 +590,8 @@ class ObjectFileHandler final : public FileHandler {
590
590
// Copy fat object contents to the output when extracting host bundle.
591
591
std::string ModifiedContent;
592
592
if (Content.size () == 1u && Content.front () == 0 ) {
593
- auto HostBundleOrErr = getHostBundle ();
593
+ auto HostBundleOrErr = getHostBundle (
594
+ StringRef (Input.getBufferStart (), Input.getBufferSize ()));
594
595
if (!HostBundleOrErr)
595
596
return HostBundleOrErr.takeError ();
596
597
@@ -700,7 +701,7 @@ class ObjectFileHandler final : public FileHandler {
700
701
return Error::success ();
701
702
}
702
703
703
- Expected<std::string> getHostBundle () {
704
+ Expected<std::string> getHostBundle (StringRef Input ) {
704
705
TempFileHandlerRAII TempFiles;
705
706
706
707
auto ModifiedObjPathOrErr = TempFiles.Create (std::nullopt);
@@ -715,7 +716,24 @@ class ObjectFileHandler final : public FileHandler {
715
716
ObjcopyArgs.push_back (" --regex" );
716
717
ObjcopyArgs.push_back (" --remove-section=__CLANG_OFFLOAD_BUNDLE__.*" );
717
718
ObjcopyArgs.push_back (" --" );
718
- ObjcopyArgs.push_back (BundlerConfig.InputFileNames .front ());
719
+
720
+ StringRef ObjcopyInputFileName;
721
+ // When unbundling an archive, the content of each object file in the
722
+ // archive is passed to this function by parameter Input, which is different
723
+ // from the content of the original input archive file, therefore it needs
724
+ // to be saved to a temporary file before passed to llvm-objcopy. Otherwise,
725
+ // Input is the same as the content of the original input file, therefore
726
+ // temporary file is not needed.
727
+ if (StringRef (BundlerConfig.FilesType ).starts_with (" a" )) {
728
+ auto InputFileOrErr =
729
+ TempFiles.Create (ArrayRef<char >(Input.data (), Input.size ()));
730
+ if (!InputFileOrErr)
731
+ return InputFileOrErr.takeError ();
732
+ ObjcopyInputFileName = *InputFileOrErr;
733
+ } else
734
+ ObjcopyInputFileName = BundlerConfig.InputFileNames .front ();
735
+
736
+ ObjcopyArgs.push_back (ObjcopyInputFileName);
719
737
ObjcopyArgs.push_back (ModifiedObjPath);
720
738
721
739
if (Error Err = executeObjcopy (BundlerConfig.ObjcopyPath , ObjcopyArgs))
@@ -1628,10 +1646,8 @@ Error OffloadBundler::UnbundleArchive() {
1628
1646
while (!CodeObject.empty ()) {
1629
1647
SmallVector<StringRef> CompatibleTargets;
1630
1648
auto CodeObjectInfo = OffloadTargetInfo (CodeObject, BundlerConfig);
1631
- if (CodeObjectInfo.hasHostKind ()) {
1632
- // Do nothing, we don't extract host code yet.
1633
- } else if (getCompatibleOffloadTargets (CodeObjectInfo, CompatibleTargets,
1634
- BundlerConfig)) {
1649
+ if (getCompatibleOffloadTargets (CodeObjectInfo, CompatibleTargets,
1650
+ BundlerConfig)) {
1635
1651
std::string BundleData;
1636
1652
raw_string_ostream DataStream (BundleData);
1637
1653
if (Error Err = FileHandler->ReadBundle (DataStream, CodeObjectBuffer))
0 commit comments