[llvm] [Archive][COFF] Split hybrid COFF files when adding them to an archive (PR #205160)
Martin Storsjö via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 02:33:56 PDT 2026
================
@@ -1580,6 +1580,69 @@ std::optional<MemoryBufferRef> COFFObjectFile::findHybridObjectSection() const {
return std::nullopt;
}
+std::unique_ptr<MemoryBuffer> COFFObjectFile::stripHybridSection() const {
+ if (getDOSHeader() || getMachine() != COFF::IMAGE_FILE_MACHINE_ARM64)
+ return nullptr;
+
+ for (SectionRef S : sections()) {
+ Expected<StringRef> Name = S.getName();
+ if (errorToBool(Name.takeError()) || *Name != kArm64ECSectionName)
+ continue;
+
+ const coff_section *HybridSec = getCOFFSection(S);
+ if (!HybridSec->SizeOfRawData)
+ continue;
+
+ // Copy the original buffer, skipping the hybrid section content.
+ std::unique_ptr<WritableMemoryBuffer> NativeView =
+ WritableMemoryBuffer::getNewUninitMemBuffer(Data.getBufferSize() -
+ HybridSec->SizeOfRawData);
+ uint32_t HybridEnd = HybridSec->PointerToRawData + HybridSec->SizeOfRawData;
+ memcpy(NativeView->getBufferStart(), Data.getBufferStart(),
+ HybridSec->PointerToRawData);
+ memcpy(NativeView->getBufferStart() + HybridSec->PointerToRawData,
+ Data.getBufferStart() + HybridEnd, Data.getBufferSize() - HybridEnd);
+
+ auto getTargetPtr = [&](const void *Ptr) {
+ return NativeView->getBufferStart() +
+ (reinterpret_cast<const char *>(Ptr) - Data.getBufferStart());
+ };
+
+ // Adjust the COFF header, if necessary.
+ if (COFFHeader) {
+ auto Header =
+ reinterpret_cast<coff_file_header *>(getTargetPtr(COFFHeader));
+ if (Header->PointerToSymbolTable >= HybridEnd)
+ Header->PointerToSymbolTable -= HybridSec->SizeOfRawData;
+ } else {
+ auto Header = reinterpret_cast<coff_bigobj_file_header *>(
+ getTargetPtr(COFFBigObjHeader));
+ if (Header->PointerToSymbolTable >= HybridEnd)
+ Header->PointerToSymbolTable -= HybridSec->SizeOfRawData;
+ }
+
+ // Adjust section headers.
+ auto COFFSec = reinterpret_cast<coff_section *>(getTargetPtr(HybridSec));
+ COFFSec->PointerToRawData = 0;
+ COFFSec->SizeOfRawData = 0;
----------------
mstorsjo wrote:
Would it make sense to clear `VirtualSize` for good measure? Although I guess it's usually zero for sections in object files anyway...
Separately - it might be good to clarify the aspect of the stripping here; the section is still present but just gets all the data removed. This has the implication that all consumers of the section also need to check for both that the section is present and is nonempty.
(I understand that it's a much more involved operation to remove the section entirely, so this is fine, but it would be good to have the implication pointed out.)
https://github.com/llvm/llvm-project/pull/205160
More information about the llvm-commits
mailing list