+ return (uint8_t*)fFirstLinkEditSegment->mappedAddress() + fLinkEditsTotalOptimizedSize;
+}
+
+
+template <typename A>
+class ObjCSelectorUniquer
+{
+private:
+ objc_opt::string_map fSelectorStrings;
+ SharedCache<A> *fCache;
+ size_t fCount;
+
+public:
+
+ ObjCSelectorUniquer(SharedCache<A> *newCache)
+ : fSelectorStrings()
+ , fCache(newCache)
+ , fCount(0)
+ { }
+
+ typename A::P::uint_t visit(typename A::P::uint_t oldValue)
+ {
+ fCount++;
+ const char *s = (const char *)
+ fCache->mappedAddressForVMAddress(oldValue);
+ objc_opt::string_map::iterator element =
+ fSelectorStrings.insert(objc_opt::string_map::value_type(s, oldValue)).first;
+ return (typename A::P::uint_t)element->second;
+ }
+
+ objc_opt::string_map& strings() {
+ return fSelectorStrings;
+ }
+
+ size_t count() const { return fCount; }
+};
+
+
+template <typename A>
+class ClassListBuilder
+{
+private:
+ typedef typename A::P P;
+
+ objc_opt::string_map fClassNames;
+ objc_opt::class_map fClasses;
+ size_t fCount;
+ HeaderInfoOptimizer<A>& fHinfos;
+
+public:
+
+ ClassListBuilder(HeaderInfoOptimizer<A>& hinfos)
+ : fClassNames()
+ , fClasses()
+ , fCount(0)
+ , fHinfos(hinfos)
+ { }
+
+ void visitClass(SharedCache<A>* cache,
+ const macho_header<P>* header,
+ objc_class_t<A>* cls)
+ {
+ if (cls->isMetaClass(cache)) return;
+
+ const char *name = cls->getName(cache);
+ uint64_t name_vmaddr = cache->VMAddressForMappedAddress(name);
+ uint64_t cls_vmaddr = cache->VMAddressForMappedAddress(cls);
+ uint64_t hinfo_vmaddr = cache->VMAddressForMappedAddress(fHinfos.hinfoForHeader(cache, header));
+ fClassNames.insert(objc_opt::string_map::value_type(name, name_vmaddr));
+ fClasses.insert(objc_opt::class_map::value_type(name, std::pair<uint64_t, uint64_t>(cls_vmaddr, hinfo_vmaddr)));
+ fCount++;
+ }
+
+ objc_opt::string_map& classNames() {
+ return fClassNames;
+ }
+
+ objc_opt::class_map& classes() {
+ return fClasses;
+ }
+
+ size_t count() const { return fCount; }
+};
+
+
+static int percent(size_t num, size_t denom) {
+ if (denom) return (int)(num / (double)denom * 100);
+ else return 100;
+}
+
+template <typename A>
+void SharedCache<A>::optimizeObjC(std::vector<void*>& pointersInData)
+{
+ const char *err;
+
+ if ( verbose ) {
+ fprintf(stderr, "update_dyld_shared_cache: for %s, optimizing objc metadata\n", archName());
+ }
+
+ size_t headerSize = P::round_up(sizeof(objc_opt::objc_opt_t));
+ if (headerSize != sizeof(objc_opt::objc_opt_t)) {
+ warn(archName(), "libobjc's optimization structure size is wrong (metadata not optimized)");
+ }
+
+ // Find libobjc's empty sections to fill in
+ const macho_section<P> *optROSection = NULL;
+ const macho_section<P> *optRWSection = NULL;
+ for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
+ if ( strstr(it->layout->getFilePath(), "libobjc") != NULL ) {
+ const macho_header<P>* mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ optROSection = mh->getSection("__TEXT", "__objc_opt_ro");
+ optRWSection = mh->getSection("__DATA", "__objc_opt_rw");
+ break;
+ }
+ }
+
+ if ( optROSection == NULL ) {
+ warn(archName(), "libobjc's read-only section missing (metadata not optimized)");
+ return;
+ }
+
+ if ( optRWSection == NULL ) {
+ warn(archName(), "libobjc's read/write section missing (metadata not optimized)");
+ return;
+ }
+
+ uint8_t* optROData = (uint8_t*)mappedAddressForVMAddress(optROSection->addr());
+ size_t optRORemaining = optROSection->size();
+
+ uint8_t* optRWData = (uint8_t*)mappedAddressForVMAddress(optRWSection->addr());
+ size_t optRWRemaining = optRWSection->size();
+
+ if (optRORemaining < headerSize) {
+ warn(archName(), "libobjc's read-only section is too small (metadata not optimized)");
+ return;
+ }
+ objc_opt::objc_opt_t* optROHeader = (objc_opt::objc_opt_t *)optROData;
+ optROData += headerSize;
+ optRORemaining -= headerSize;
+
+ if (E::get32(optROHeader->version) != objc_opt::VERSION) {
+ warn(archName(), "libobjc's read-only section version is unrecognized (metadata not optimized)");
+ return;
+ }
+
+ // Write nothing to optROHeader until everything else is written.
+ // If something fails below, libobjc will not use the section.
+
+ // Find objc-containing dylibs
+ std::vector<LayoutInfo> objcDylibs;
+ for(typename std::vector<LayoutInfo>::const_iterator it = fDylibs.begin(); it != fDylibs.end(); ++it) {
+ macho_header<P> *mh = (macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ if (mh->getSection("__DATA", "__objc_imageinfo") || mh->getSegment("__OBJC")) {
+ objcDylibs.push_back(*it);
+ }
+ }
+
+ // Build image list
+
+ // This is SAFE: the binaries themselves are unmodified.
+
+ std::vector<LayoutInfo> addressSortedDylibs = objcDylibs;
+ std::sort(addressSortedDylibs.begin(), addressSortedDylibs.end(), ByAddressSorter());
+
+ uint64_t hinfoVMAddr = optRWSection->addr() + optRWSection->size() - optRWRemaining;
+ HeaderInfoOptimizer<A> hinfoOptimizer;
+ err = hinfoOptimizer.init(objcDylibs.size(), optRWData, optRWRemaining);
+ if (err) {
+ warn(archName(), err);
+ return;
+ }
+ for(typename std::vector<LayoutInfo>::const_iterator it = addressSortedDylibs.begin(); it != addressSortedDylibs.end(); ++it) {
+ const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ hinfoOptimizer.update(this, mh, pointersInData);
+ }
+
+
+ // Update selector references and build selector list
+
+ // This is SAFE: if we run out of room for the selector table,
+ // the modified binaries are still usable.
+
+ // Heuristic: choose selectors from libraries with more cstring data first.
+ // This tries to localize selector cstring memory.
+ ObjCSelectorUniquer<A> uniq(this);
+ std::vector<LayoutInfo> sizeSortedDylibs = objcDylibs;
+ std::sort(sizeSortedDylibs.begin(), sizeSortedDylibs.end(), ByCStringSectionSizeSorter());
+
+ SelectorOptimizer<A, ObjCSelectorUniquer<A> > selOptimizer(uniq);
+ for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
+ const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ LegacySelectorUpdater<A, ObjCSelectorUniquer<A> >::update(this, mh, uniq);
+ selOptimizer.optimize(this, mh);
+ }
+
+ uint64_t seloptVMAddr = optROSection->addr() + optROSection->size() - optRORemaining;
+ objc_opt::objc_selopt_t *selopt = new(optROData) objc_opt::objc_selopt_t;
+ err = selopt->write(seloptVMAddr, optRORemaining, uniq.strings());
+ if (err) {
+ warn(archName(), err);
+ return;
+ }
+ optROData += selopt->size();
+ optRORemaining -= selopt->size();
+ selopt->byteswap(E::little_endian), selopt = NULL;
+
+
+ // Build class table.
+
+ // This is SAFE: the binaries themselves are unmodified.
+
+ ClassListBuilder<A> classes(hinfoOptimizer);
+ ClassWalker< A, ClassListBuilder<A> > classWalker(classes);
+ for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
+ const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ classWalker.walk(this, mh);
+ }
+
+ uint64_t clsoptVMAddr = optROSection->addr() + optROSection->size() - optRORemaining;
+ objc_opt::objc_clsopt_t *clsopt = new(optROData) objc_opt::objc_clsopt_t;
+ err = clsopt->write(clsoptVMAddr, optRORemaining,
+ classes.classNames(), classes.classes(), verbose);
+ if (err) {
+ warn(archName(), err);
+ return;
+ }
+ optROData += clsopt->size();
+ optRORemaining -= clsopt->size();
+ size_t duplicateCount = clsopt->duplicateCount();
+ clsopt->byteswap(E::little_endian), clsopt = NULL;
+
+
+ // Sort method lists.
+
+ // This is SAFE: modified binaries are still usable as unsorted lists.
+ // This must be done AFTER uniquing selectors.
+
+ MethodListSorter<A> methodSorter;
+ for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
+ macho_header<P> *mh = (macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ methodSorter.optimize(this, mh);
+ }
+
+
+ // Repair ivar offsets.
+
+ // This is SAFE: the runtime always validates ivar offsets at runtime.
+
+ IvarOffsetOptimizer<A> ivarOffsetOptimizer;
+ for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
+ const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ ivarOffsetOptimizer.optimize(this, mh);
+ }
+
+
+ // Success. Mark dylibs as optimized.
+ for(typename std::vector<LayoutInfo>::const_iterator it = sizeSortedDylibs.begin(); it != sizeSortedDylibs.end(); ++it) {
+ const macho_header<P> *mh = (const macho_header<P>*)(*it->layout).getSegments()[0].mappedAddress();
+ const macho_section<P> *imageInfoSection;
+ imageInfoSection = mh->getSection("__DATA", "__objc_imageinfo");
+ if (!imageInfoSection) {
+ imageInfoSection = mh->getSection("__OBJC", "__image_info");
+ }
+ if (imageInfoSection) {
+ objc_image_info<A> *info = (objc_image_info<A> *)
+ mappedAddressForVMAddress(imageInfoSection->addr());
+ info->setOptimizedByDyld();
+ }
+ }
+
+
+ // Success. Update RO header last.
+ E::set32(optROHeader->selopt_offset, seloptVMAddr - optROSection->addr());
+ E::set32(optROHeader->clsopt_offset, clsoptVMAddr - optROSection->addr());
+ E::set32(optROHeader->headeropt_offset, hinfoVMAddr - optROSection->addr());
+
+ if ( verbose ) {
+ size_t roSize = optROSection->size() - optRORemaining;
+ size_t rwSize = optRWSection->size() - optRWRemaining;
+ fprintf(stderr, "update_dyld_shared_cache: for %s, %zu/%llu bytes "
+ "(%d%%) used in libobjc read-only optimization section\n",
+ archName(), roSize, optROSection->size(),
+ percent(roSize, optROSection->size()));
+ fprintf(stderr, "update_dyld_shared_cache: for %s, %zu/%llu bytes "
+ "(%d%%) used in libobjc read/write optimization section\n",
+ archName(), rwSize, optRWSection->size(),
+ percent(rwSize, optRWSection->size()));
+ fprintf(stderr, "update_dyld_shared_cache: for %s, "
+ "uniqued %zu selectors\n",
+ archName(), uniq.strings().size());
+ fprintf(stderr, "update_dyld_shared_cache: for %s, "
+ "updated %zu selector references\n",
+ archName(), uniq.count());
+ fprintf(stderr, "update_dyld_shared_cache: for %s, "
+ "updated %zu ivar offsets\n",
+ archName(), ivarOffsetOptimizer.optimized());
+ fprintf(stderr, "update_dyld_shared_cache: for %s, "
+ "sorted %zu method lists\n",
+ archName(), methodSorter.optimized());
+ fprintf(stderr, "update_dyld_shared_cache: for %s, "
+ "recorded %zu classes (%zu duplicates)\n",
+ archName(), classes.classNames().size(), duplicateCount);
+ fprintf(stderr, "update_dyld_shared_cache: for %s, "
+ "wrote objc metadata optimization version %d\n",
+ archName(), objc_opt::VERSION);
+ }
+
+ return;