Loading...
cache_builder/NewAdjustDylibSegments.cpp dyld-1340 dyld-1162
--- dyld/dyld-1340/cache_builder/NewAdjustDylibSegments.cpp
+++ dyld/dyld-1162/cache_builder/NewAdjustDylibSegments.cpp
@@ -248,11 +248,6 @@
                 _exportTrieCmd = (linkedit_data_command*)cmd;
                 this->adjustLinkeditLoadCommand(MovedLinkedit::Kind::exportTrie, _exportTrieCmd->dataoff, _exportTrieCmd->datasize);
                 break;
-            case LC_FUNCTION_VARIANTS: {
-                linkedit_data_command* functionVariantsCmd = (linkedit_data_command*)cmd;
-                this->adjustLinkeditLoadCommand(MovedLinkedit::Kind::functionVariants, functionVariantsCmd->dataoff, functionVariantsCmd->datasize);
-                break;
-            }
             case macho_segment_command<P>::CMD:
                 macho_segment_command<P>* segCmd = (macho_segment_command<P>*)cmd;
                 _segCmds.push_back(segCmd);
@@ -538,8 +533,6 @@
             case LC_DYLIB_CODE_SIGN_DRS:
             case LC_DYLD_CHAINED_FIXUPS:
             case LC_SEGMENT_SPLIT_INFO:
-            case LC_ENCRYPTION_INFO:
-            case LC_ENCRYPTION_INFO_64:
                 remove = true;
                 break;
             default:
@@ -1281,6 +1274,7 @@
     std::vector<std::string_view>   sectionNames;
 
     // Also track coalesced sections, if we have any
+    typedef DylibSectionCoalescer::OptimizedSection OptimizedSection;
     std::vector<uint64_t>                           coalescedSectionOriginalVMAddrs;
     std::vector<const OptimizedSection*>            coalescedSectionData;
     std::vector<cache_builder::ASLR_Tracker*>       aslrTrackers;
@@ -1399,19 +1393,32 @@
 
                     uint64_t toNewAddress   = 0;
                     uint64_t toAtomSlide    = 0;
-                    std::optional<uint64_t> coalescedToNewAddress;
                     if ( coalescedSectionData[toSectionIndex] != nullptr ) {
                         // To was optimized/coalesced
-                        coalescedToNewAddress = coalescedSectionData[toSectionIndex]->cacheVMAddress((uint32_t)toSectionOffset);
-                    }
-
-                    if ( coalescedToNewAddress.has_value() ) {
-                        toNewAddress = coalescedToNewAddress.value();
-
-                        // The 'to' section is gone, but we still need the 'to' slide.  Instead of a section slide,
-                        // compute the slide for this individual atom
-                        uint64_t toAtomOriginalVMAddr = coalescedSectionOriginalVMAddrs[toSectionIndex] + toSectionOffset;
-                        toAtomSlide = toNewAddress - toAtomOriginalVMAddr;
+                        const auto* offsetMap = &coalescedSectionData[toSectionIndex]->offsetMap;
+                        auto offsetIt = offsetMap->find((uint32_t)toSectionOffset);
+                        if ( coalescedSectionData[toSectionIndex]->sectionWillBeRemoved ) {
+                            // If the section was removed then we have to find an entry for every atom in there
+                            assert(offsetIt != offsetMap->end());
+                        } else {
+                            // Not all GOTs are optimized, but we should find the element somewhere
+                            assert((offsetIt != offsetMap->end()) || coalescedSectionData[toSectionIndex]->unoptimizedOffsets.count((uint32_t)toSectionOffset));
+                        }
+
+                        if ( offsetIt == offsetMap->end() ) {
+                            // To was not fully optimized/coalesced
+                            // FIXME: Unify this with the else branch below where we didn't have a coalesced section
+                            toNewAddress = toSectionNewAddress + toSectionOffset;
+                            toAtomSlide = toSectionSlide;
+                        } else {
+                            uint64_t baseVMAddr = coalescedSectionData[toSectionIndex]->subCacheSection->cacheChunk->cacheVMAddress.rawValue();
+                            toNewAddress = baseVMAddr + offsetIt->second;
+
+                            // The 'to' section is gone, but we still need the 'to' slide.  Instead of a section slide,
+                            // compute the slide for this individual atom
+                            uint64_t toAtomOriginalVMAddr = coalescedSectionOriginalVMAddrs[toSectionIndex] + toSectionOffset;
+                            toAtomSlide = toNewAddress - toAtomOriginalVMAddr;
+                        }
                     } else {
                         // To was not optimized/coalesced
                         toNewAddress = toSectionNewAddress + toSectionOffset;