]> git.saurik.com Git - apple/ld64.git/blame - src/ld/passes/objc.cpp
ld64-253.9.tar.gz
[apple/ld64.git] / src / ld / passes / objc.cpp
CommitLineData
a645023d
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
afe874b1 3 * Copyright (c) 2010-2011 Apple Inc. All rights reserved.
a645023d
A
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26#include <stdint.h>
27#include <math.h>
28#include <unistd.h>
29#include <dlfcn.h>
30#include <mach/machine.h>
31
32#include <vector>
33#include <map>
34#include <set>
35
36#include "Architectures.hpp"
37#include "MachOFileAbstraction.hpp"
38
39#include "ld.hpp"
40#include "objc.h"
41
42namespace ld {
43namespace passes {
44namespace objc {
45
46
47
48struct objc_image_info {
49 uint32_t version; // initially 0
50 uint32_t flags;
51};
52
a645023d
A
53#define OBJC_IMAGE_SUPPORTS_GC (1<<1)
54#define OBJC_IMAGE_REQUIRES_GC (1<<2)
55#define OBJC_IMAGE_OPTIMIZED_BY_DYLD (1<<3)
56#define OBJC_IMAGE_SUPPORTS_COMPACTION (1<<4)
f80fe69f 57#define OBJC_IMAGE_IS_SIMULATED (1<<5)
a645023d
A
58
59
60
61//
62// This class is the 8 byte section containing ObjC flags
63//
64template <typename A>
65class ObjCImageInfoAtom : public ld::Atom {
66public:
67 ObjCImageInfoAtom(ld::File::ObjcConstraint objcConstraint,
599556ff 68 bool compaction, bool abi2, uint8_t swiftVersion);
a645023d
A
69
70 virtual const ld::File* file() const { return NULL; }
a645023d
A
71 virtual const char* name() const { return "objc image info"; }
72 virtual uint64_t size() const { return sizeof(objc_image_info); }
73 virtual uint64_t objectAddress() const { return 0; }
74 virtual void setScope(Scope) { }
75 virtual void copyRawContent(uint8_t buffer[]) const {
76 memcpy(buffer, &_content, sizeof(objc_image_info));
77 }
78
79private:
80 objc_image_info _content;
81
82 static ld::Section _s_sectionABI1;
83 static ld::Section _s_sectionABI2;
84};
85
86template <typename A> ld::Section ObjCImageInfoAtom<A>::_s_sectionABI1("__OBJC", "__image_info", ld::Section::typeUnclassified);
87template <typename A> ld::Section ObjCImageInfoAtom<A>::_s_sectionABI2("__DATA", "__objc_imageinfo", ld::Section::typeUnclassified);
88
89
90template <typename A>
91ObjCImageInfoAtom<A>::ObjCImageInfoAtom(ld::File::ObjcConstraint objcConstraint, bool compaction,
599556ff 92 bool abi2, uint8_t swiftVersion)
a645023d
A
93 : ld::Atom(abi2 ? _s_sectionABI2 : _s_sectionABI1, ld::Atom::definitionRegular, ld::Atom::combineNever,
94 ld::Atom::scopeLinkageUnit, ld::Atom::typeUnclassified,
95 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2))
96{
97
98 uint32_t value = 0;
a645023d
A
99 switch ( objcConstraint ) {
100 case ld::File::objcConstraintNone:
101 case ld::File::objcConstraintRetainRelease:
102 if ( compaction )
103 warning("ignoring -objc_gc_compaction because code not compiled for ObjC garbage collection");
104 break;
105 case ld::File::objcConstraintRetainReleaseOrGC:
106 value |= OBJC_IMAGE_SUPPORTS_GC;
107 if ( compaction )
108 value |= OBJC_IMAGE_SUPPORTS_COMPACTION;
109 break;
110 case ld::File::objcConstraintGC:
111 value |= OBJC_IMAGE_SUPPORTS_GC | OBJC_IMAGE_REQUIRES_GC;
112 if ( compaction )
113 value |= OBJC_IMAGE_SUPPORTS_COMPACTION;
114 break;
f80fe69f
A
115 case ld::File::objcConstraintRetainReleaseForSimulator:
116 value |= OBJC_IMAGE_IS_SIMULATED;
117 break;
a645023d
A
118 }
119
599556ff
A
120 // provide swift language version in final binary for runtime to inspect
121 value |= (swiftVersion << 8);
122
a645023d
A
123 _content.version = 0;
124 A::P::E::set32(_content.flags, value);
125}
126
127
128
129//
130// This class is for a new Atom which is an ObjC method list created by merging method lists from categories
131//
132template <typename A>
133class MethodListAtom : public ld::Atom {
134public:
135 MethodListAtom(ld::Internal& state, const ld::Atom* baseMethodList, bool meta,
136 const std::vector<const ld::Atom*>* categories,
137 std::set<const ld::Atom*>& deadAtoms);
138
139 virtual const ld::File* file() const { return _file; }
a645023d
A
140 virtual const char* name() const { return "objc merged method list"; }
141 virtual uint64_t size() const { return _methodCount*3*sizeof(pint_t) + 8; }
142 virtual uint64_t objectAddress() const { return 0; }
143 virtual void setScope(Scope) { }
144 virtual void copyRawContent(uint8_t buffer[]) const {
145 bzero(buffer, size());
ebf6f434 146 A::P::E::set32(*((uint32_t*)(&buffer[0])), 3*sizeof(pint_t)); // entry size
a645023d
A
147 A::P::E::set32(*((uint32_t*)(&buffer[4])), _methodCount);
148 }
149 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixups[0]; }
150 virtual ld::Fixup::iterator fixupsEnd() const { return (ld::Fixup*)&_fixups[_fixups.size()]; }
151
152private:
153 typedef typename A::P::uint_t pint_t;
154
155 const ld::File* _file;
156 unsigned int _methodCount;
157 std::vector<ld::Fixup> _fixups;
158
159 static ld::Section _s_section;
160};
161
162template <typename A>
163ld::Section MethodListAtom<A>::_s_section("__DATA", "__objc_const", ld::Section::typeUnclassified);
164
165
166//
167// This class is for a new Atom which is an ObjC protocol list created by merging protocol lists from categories
168//
169template <typename A>
170class ProtocolListAtom : public ld::Atom {
171public:
172 ProtocolListAtom(ld::Internal& state, const ld::Atom* baseProtocolList,
173 const std::vector<const ld::Atom*>* categories,
174 std::set<const ld::Atom*>& deadAtoms);
175
176 virtual const ld::File* file() const { return _file; }
a645023d
A
177 virtual const char* name() const { return "objc merged protocol list"; }
178 virtual uint64_t size() const { return (_protocolCount+1)*sizeof(pint_t); }
179 virtual uint64_t objectAddress() const { return 0; }
180 virtual void setScope(Scope) { }
181 virtual void copyRawContent(uint8_t buffer[]) const {
182 bzero(buffer, size());
183 A::P::setP(*((pint_t*)(buffer)), _protocolCount);
184 }
185 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixups[0]; }
186 virtual ld::Fixup::iterator fixupsEnd() const { return (ld::Fixup*)&_fixups[_fixups.size()]; }
187
188private:
189 typedef typename A::P::uint_t pint_t;
190
191 const ld::File* _file;
192 unsigned int _protocolCount;
193 std::vector<ld::Fixup> _fixups;
194
195 static ld::Section _s_section;
196};
197
198template <typename A>
199ld::Section ProtocolListAtom<A>::_s_section("__DATA", "__objc_const", ld::Section::typeUnclassified);
200
201
202
203//
204// This class is for a new Atom which is an ObjC property list created by merging property lists from categories
205//
206template <typename A>
207class PropertyListAtom : public ld::Atom {
208public:
209 PropertyListAtom(ld::Internal& state, const ld::Atom* baseProtocolList,
210 const std::vector<const ld::Atom*>* categories,
211 std::set<const ld::Atom*>& deadAtoms);
212
213 virtual const ld::File* file() const { return _file; }
a645023d
A
214 virtual const char* name() const { return "objc merged property list"; }
215 virtual uint64_t size() const { return _propertyCount*2*sizeof(pint_t) + 8; }
216 virtual uint64_t objectAddress() const { return 0; }
217 virtual void setScope(Scope) { }
218 virtual void copyRawContent(uint8_t buffer[]) const {
219 bzero(buffer, size());
220 A::P::E::set32(((uint32_t*)(buffer))[0], 2*sizeof(pint_t)); // sizeof(objc_property)
221 A::P::E::set32(((uint32_t*)(buffer))[1], _propertyCount);
222 }
223 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixups[0]; }
224 virtual ld::Fixup::iterator fixupsEnd() const { return (ld::Fixup*)&_fixups[_fixups.size()]; }
225
226private:
227 typedef typename A::P::uint_t pint_t;
228
229 const ld::File* _file;
230 unsigned int _propertyCount;
231 std::vector<ld::Fixup> _fixups;
232
233 static ld::Section _s_section;
234};
235
236template <typename A>
237ld::Section PropertyListAtom<A>::_s_section("__DATA", "__objc_const", ld::Section::typeUnclassified);
238
239
240
241
242
243//
244// This class is used to create an Atom that replaces an atom from a .o file that holds a class_ro_t.
245// It is needed because there is no way to add Fixups to an existing atom.
246//
247template <typename A>
248class ClassROOverlayAtom : public ld::Atom {
249public:
250 ClassROOverlayAtom(const ld::Atom* classROAtom);
251
252 // overrides of ld::Atom
253 virtual const ld::File* file() const { return _atom->file(); }
a645023d
A
254 virtual const char* name() const { return _atom->name(); }
255 virtual uint64_t size() const { return _atom->size(); }
256 virtual uint64_t objectAddress() const { return _atom->objectAddress(); }
257 virtual void copyRawContent(uint8_t buffer[]) const
258 { _atom->copyRawContent(buffer); }
259 virtual const uint8_t* rawContentPointer() const
260 { return _atom->rawContentPointer(); }
261 virtual unsigned long contentHash(const class ld::IndirectBindingTable& ibt) const
262 { return _atom->contentHash(ibt); }
263 virtual bool canCoalesceWith(const ld::Atom& rhs, const class ld::IndirectBindingTable& ibt) const
264 { return _atom->canCoalesceWith(rhs,ibt); }
265
266 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixups[0]; }
267 virtual ld::Fixup::iterator fixupsEnd() const { return (ld::Fixup*)&_fixups[_fixups.size()]; }
268
269 void addProtocolListFixup();
270 void addPropertyListFixup();
271 void addMethodListFixup();
272
273private:
274 typedef typename A::P::uint_t pint_t;
275
276 const ld::Atom* _atom;
277 std::vector<ld::Fixup> _fixups;
278};
279
280template <typename A>
281ClassROOverlayAtom<A>::ClassROOverlayAtom(const ld::Atom* classROAtom)
282 : ld::Atom(classROAtom->section(), ld::Atom::definitionRegular, ld::Atom::combineNever,
283 ld::Atom::scopeLinkageUnit, ld::Atom::typeUnclassified,
284 classROAtom->symbolTableInclusion(), false, false, false, classROAtom->alignment()),
285 _atom(classROAtom)
286{
287 // ensure all attributes are same as original
288 this->setAttributesFromAtom(*classROAtom);
289
290 // copy fixups from orginal atom
291 for (ld::Fixup::iterator fit=classROAtom->fixupsBegin(); fit != classROAtom->fixupsEnd(); ++fit) {
292 ld::Fixup fixup = *fit;
293 _fixups.push_back(fixup);
294 }
295}
296
297
298//
299// Base class for reading and updating existing ObjC atoms from .o files
300//
301template <typename A>
302class ObjCData {
303public:
304 static const ld::Atom* getPointerInContent(ld::Internal& state, const ld::Atom* contentAtom, unsigned int offset, bool* hasAddend=NULL);
305 static void setPointerInContent(ld::Internal& state, const ld::Atom* contentAtom,
306 unsigned int offset, const ld::Atom* newAtom);
307 typedef typename A::P::uint_t pint_t;
308};
309
310template <typename A>
311const ld::Atom* ObjCData<A>::getPointerInContent(ld::Internal& state, const ld::Atom* contentAtom, unsigned int offset, bool* hasAddend)
312{
313 const ld::Atom* target = NULL;
314 if ( hasAddend != NULL )
315 *hasAddend = false;
316 for (ld::Fixup::iterator fit=contentAtom->fixupsBegin(); fit != contentAtom->fixupsEnd(); ++fit) {
317 if ( fit->offsetInAtom == offset ) {
318 switch ( fit->binding ) {
319 case ld::Fixup::bindingsIndirectlyBound:
320 target = state.indirectBindingTable[fit->u.bindingIndex];
321 break;
322 case ld::Fixup::bindingDirectlyBound:
323 target = fit->u.target;
324 break;
325 case ld::Fixup::bindingNone:
326 if ( fit->kind == ld::Fixup::kindAddAddend ) {
327 if ( hasAddend != NULL )
328 *hasAddend = true;
329 }
330 break;
331 default:
332 break;
333 }
334 }
335 }
336 return target;
337}
338
339template <typename A>
340void ObjCData<A>::setPointerInContent(ld::Internal& state, const ld::Atom* contentAtom,
341 unsigned int offset, const ld::Atom* newAtom)
342{
343 for (ld::Fixup::iterator fit=contentAtom->fixupsBegin(); fit != contentAtom->fixupsEnd(); ++fit) {
344 if ( fit->offsetInAtom == offset ) {
345 switch ( fit->binding ) {
346 case ld::Fixup::bindingsIndirectlyBound:
347 state.indirectBindingTable[fit->u.bindingIndex] = newAtom;
348 return;
349 case ld::Fixup::bindingDirectlyBound:
350 fit->u.target = newAtom;
351 return;
352 default:
353 break;
354 }
355 }
356 }
357 assert(0 && "could not update method list");
358}
359
360
361
362//
363// Helper class for reading and updating existing ObjC category atoms from .o files
364//
365template <typename A>
366class Category : public ObjCData<A> {
367public:
599556ff 368 static const ld::Atom* getClass(ld::Internal& state, const ld::Atom* contentAtom, bool& hasAddend);
a645023d
A
369 static const ld::Atom* getInstanceMethods(ld::Internal& state, const ld::Atom* contentAtom);
370 static const ld::Atom* getClassMethods(ld::Internal& state, const ld::Atom* contentAtom);
371 static const ld::Atom* getProtocols(ld::Internal& state, const ld::Atom* contentAtom);
372 static const ld::Atom* getProperties(ld::Internal& state, const ld::Atom* contentAtom);
373 static uint32_t size() { return 6*sizeof(pint_t); }
374private:
375 typedef typename A::P::uint_t pint_t;
376};
377
378
379template <typename A>
599556ff 380const ld::Atom* Category<A>::getClass(ld::Internal& state, const ld::Atom* contentAtom, bool& hasAddend)
a645023d 381{
599556ff 382 return ObjCData<A>::getPointerInContent(state, contentAtom, sizeof(pint_t), &hasAddend); // category_t.cls
a645023d
A
383}
384
385template <typename A>
386const ld::Atom* Category<A>::getInstanceMethods(ld::Internal& state, const ld::Atom* contentAtom)
387{
388 return ObjCData<A>::getPointerInContent(state, contentAtom, 2*sizeof(pint_t)); // category_t.instanceMethods
389}
390
391template <typename A>
392const ld::Atom* Category<A>::getClassMethods(ld::Internal& state, const ld::Atom* contentAtom)
393{
394 return ObjCData<A>::getPointerInContent(state, contentAtom, 3*sizeof(pint_t)); // category_t.classMethods
395}
396
397template <typename A>
398const ld::Atom* Category<A>::getProtocols(ld::Internal& state, const ld::Atom* contentAtom)
399{
400 return ObjCData<A>::getPointerInContent(state, contentAtom, 4*sizeof(pint_t)); // category_t.protocols
401}
402
403template <typename A>
404const ld::Atom* Category<A>::getProperties(ld::Internal& state, const ld::Atom* contentAtom)
405{
406 return ObjCData<A>::getPointerInContent(state, contentAtom, 5*sizeof(pint_t)); // category_t.instanceProperties
407}
408
409
410template <typename A>
411class MethodList : public ObjCData<A> {
412public:
413 static uint32_t count(ld::Internal& state, const ld::Atom* methodListAtom) {
414 const uint32_t* methodListData = (uint32_t*)(methodListAtom->rawContentPointer());
415 return A::P::E::get32(methodListData[1]); // method_list_t.count
416 }
417};
418
419template <typename A>
420class ProtocolList : public ObjCData<A> {
421public:
422 static uint32_t count(ld::Internal& state, const ld::Atom* protocolListAtom) {
423 pint_t* protocolListData = (pint_t*)(protocolListAtom->rawContentPointer());
424 return A::P::getP(*protocolListData); // protocol_list_t.count
425 }
426private:
427 typedef typename A::P::uint_t pint_t;
428};
429
430template <typename A>
431class PropertyList : public ObjCData<A> {
432public:
433 static uint32_t count(ld::Internal& state, const ld::Atom* protocolListAtom) {
434 uint32_t* protocolListData = (uint32_t*)(protocolListAtom->rawContentPointer());
435 return A::P::E::get32(protocolListData[1]); // property_list_t.count
436 }
437private:
438 typedef typename A::P::uint_t pint_t;
439};
440
441
442
443//
444// Helper class for reading and updating existing ObjC class atoms from .o files
445//
446template <typename A>
447class Class : public ObjCData<A> {
448public:
449 static const ld::Atom* getInstanceMethodList(ld::Internal& state, const ld::Atom* classAtom);
450 static const ld::Atom* getInstanceProtocolList(ld::Internal& state, const ld::Atom* classAtom);
451 static const ld::Atom* getInstancePropertyList(ld::Internal& state, const ld::Atom* classAtom);
452 static const ld::Atom* getClassMethodList(ld::Internal& state, const ld::Atom* classAtom);
453 static const ld::Atom* setInstanceMethodList(ld::Internal& state, const ld::Atom* classAtom,
454 const ld::Atom* methodListAtom, std::set<const ld::Atom*>& deadAtoms);
455 static const ld::Atom* setInstanceProtocolList(ld::Internal& state, const ld::Atom* classAtom,
456 const ld::Atom* protocolListAtom, std::set<const ld::Atom*>& deadAtoms);
457 static const ld::Atom* setInstancePropertyList(ld::Internal& state, const ld::Atom* classAtom,
458 const ld::Atom* propertyListAtom, std::set<const ld::Atom*>& deadAtoms);
459 static const ld::Atom* setClassMethodList(ld::Internal& state, const ld::Atom* classAtom,
460 const ld::Atom* methodListAtom, std::set<const ld::Atom*>& deadAtoms);
461 static const ld::Atom* setClassProtocolList(ld::Internal& state, const ld::Atom* classAtom,
462 const ld::Atom* protocolListAtom, std::set<const ld::Atom*>& deadAtoms);
463 static uint32_t size() { return 5*sizeof(pint_t); }
464 static unsigned int class_ro_header_size();
465private:
466 typedef typename A::P::uint_t pint_t;
467 static const ld::Atom* getROData(ld::Internal& state, const ld::Atom* classAtom);
468};
469
470template <> unsigned int Class<x86_64>::class_ro_header_size() { return 16; }
471template <> unsigned int Class<arm>::class_ro_header_size() { return 12;}
472template <> unsigned int Class<x86>::class_ro_header_size() { return 12; }
473
474
475template <typename A>
476const ld::Atom* Class<A>::getROData(ld::Internal& state, const ld::Atom* classAtom)
477{
478 return ObjCData<A>::getPointerInContent(state, classAtom, 4*sizeof(pint_t)); // class_t.data
479
480}
481
482template <typename A>
483const ld::Atom* Class<A>::getInstanceMethodList(ld::Internal& state, const ld::Atom* classAtom)
484{
485 const ld::Atom* classROAtom = getROData(state, classAtom); // class_t.data
486 assert(classROAtom != NULL);
487 return ObjCData<A>::getPointerInContent(state, classROAtom, class_ro_header_size() + 2*sizeof(pint_t)); // class_ro_t.baseMethods
488}
489
490template <typename A>
491const ld::Atom* Class<A>::getInstanceProtocolList(ld::Internal& state, const ld::Atom* classAtom)
492{
493 const ld::Atom* classROAtom = getROData(state, classAtom); // class_t.data
494 assert(classROAtom != NULL);
495 return ObjCData<A>::getPointerInContent(state, classROAtom, class_ro_header_size() + 3*sizeof(pint_t)); // class_ro_t.baseProtocols
496}
497
498template <typename A>
499const ld::Atom* Class<A>::getInstancePropertyList(ld::Internal& state, const ld::Atom* classAtom)
500{
501 const ld::Atom* classROAtom = getROData(state, classAtom); // class_t.data
502 assert(classROAtom != NULL);
503 return ObjCData<A>::getPointerInContent(state, classROAtom, class_ro_header_size() + 6*sizeof(pint_t)); // class_ro_t.baseProperties
504}
505
506template <typename A>
507const ld::Atom* Class<A>::getClassMethodList(ld::Internal& state, const ld::Atom* classAtom)
508{
509 const ld::Atom* metaClassAtom = ObjCData<A>::getPointerInContent(state, classAtom, 0); // class_t.isa
510 assert(metaClassAtom != NULL);
511 return Class<A>::getInstanceMethodList(state, metaClassAtom);
512}
513
514template <typename A>
515const ld::Atom* Class<A>::setInstanceMethodList(ld::Internal& state, const ld::Atom* classAtom,
516 const ld::Atom* methodListAtom, std::set<const ld::Atom*>& deadAtoms)
517{
518 const ld::Atom* classROAtom = getROData(state, classAtom); // class_t.data
519 assert(classROAtom != NULL);
520 // if the base class does not already have a method list, we need to create an overlay
521 if ( getInstanceMethodList(state, classAtom) == NULL ) {
522 ClassROOverlayAtom<A>* overlay = new ClassROOverlayAtom<A>(classROAtom);
523 //fprintf(stderr, "replace class RO atom %p with %p for method list in class atom %s\n", classROAtom, overlay, classAtom->name());
524 overlay->addMethodListFixup();
525 ObjCData<A>::setPointerInContent(state, classAtom, 4*sizeof(pint_t), overlay); // class_t.data
526 deadAtoms.insert(classROAtom);
527 ObjCData<A>::setPointerInContent(state, overlay, class_ro_header_size() + 2*sizeof(pint_t), methodListAtom); // class_ro_t.baseMethods
528 return overlay;
529 }
530 ObjCData<A>::setPointerInContent(state, classROAtom, class_ro_header_size() + 2*sizeof(pint_t), methodListAtom); // class_ro_t.baseMethods
531 return NULL; // means classRO atom was not replaced
532}
533
534template <typename A>
535const ld::Atom* Class<A>::setInstanceProtocolList(ld::Internal& state, const ld::Atom* classAtom,
536 const ld::Atom* protocolListAtom, std::set<const ld::Atom*>& deadAtoms)
537{
538 const ld::Atom* classROAtom = getROData(state, classAtom); // class_t.data
539 assert(classROAtom != NULL);
540 // if the base class does not already have a protocol list, we need to create an overlay
541 if ( getInstanceProtocolList(state, classAtom) == NULL ) {
542 ClassROOverlayAtom<A>* overlay = new ClassROOverlayAtom<A>(classROAtom);
543 //fprintf(stderr, "replace class RO atom %p with %p for protocol list in class atom %s\n", classROAtom, overlay, classAtom->name());
544 overlay->addProtocolListFixup();
545 ObjCData<A>::setPointerInContent(state, classAtom, 4*sizeof(pint_t), overlay); // class_t.data
546 deadAtoms.insert(classROAtom);
547 ObjCData<A>::setPointerInContent(state, overlay, class_ro_header_size() + 3*sizeof(pint_t), protocolListAtom); // class_ro_t.baseProtocols
548 return overlay;
549 }
550 //fprintf(stderr, "set class RO atom %p protocol list in class atom %s\n", classROAtom, classAtom->name());
551 ObjCData<A>::setPointerInContent(state, classROAtom, class_ro_header_size() + 3*sizeof(pint_t), protocolListAtom); // class_ro_t.baseProtocols
552 return NULL; // means classRO atom was not replaced
553}
554
555template <typename A>
556const ld::Atom* Class<A>::setClassProtocolList(ld::Internal& state, const ld::Atom* classAtom,
557 const ld::Atom* protocolListAtom, std::set<const ld::Atom*>& deadAtoms)
558{
559 // meta class also points to same protocol list as class
560 const ld::Atom* metaClassAtom = ObjCData<A>::getPointerInContent(state, classAtom, 0); // class_t.isa
561 //fprintf(stderr, "setClassProtocolList(), classAtom=%p %s, metaClass=%p %s\n", classAtom, classAtom->name(), metaClassAtom, metaClassAtom->name());
562 assert(metaClassAtom != NULL);
563 return setInstanceProtocolList(state, metaClassAtom, protocolListAtom, deadAtoms);
564}
565
566
567
568template <typename A>
569const ld::Atom* Class<A>::setInstancePropertyList(ld::Internal& state, const ld::Atom* classAtom,
570 const ld::Atom* propertyListAtom, std::set<const ld::Atom*>& deadAtoms)
571{
572 const ld::Atom* classROAtom = getROData(state, classAtom); // class_t.data
573 assert(classROAtom != NULL);
574 // if the base class does not already have a property list, we need to create an overlay
575 if ( getInstancePropertyList(state, classAtom) == NULL ) {
576 ClassROOverlayAtom<A>* overlay = new ClassROOverlayAtom<A>(classROAtom);
577 //fprintf(stderr, "replace class RO atom %p with %p for property list in class atom %s\n", classROAtom, overlay, classAtom->name());
578 overlay->addPropertyListFixup();
579 ObjCData<A>::setPointerInContent(state, classAtom, 4*sizeof(pint_t), overlay); // class_t.data
580 deadAtoms.insert(classROAtom);
581 ObjCData<A>::setPointerInContent(state, overlay, class_ro_header_size() + 6*sizeof(pint_t), propertyListAtom); // class_ro_t.baseProperties
582 return overlay;
583 }
584 ObjCData<A>::setPointerInContent(state, classROAtom, class_ro_header_size() + 6*sizeof(pint_t), propertyListAtom); // class_ro_t.baseProperties
585 return NULL; // means classRO atom was not replaced
586}
587
588template <typename A>
589const ld::Atom* Class<A>::setClassMethodList(ld::Internal& state, const ld::Atom* classAtom,
590 const ld::Atom* methodListAtom, std::set<const ld::Atom*>& deadAtoms)
591{
592 // class methods is just instance methods of metaClass
593 const ld::Atom* metaClassAtom = ObjCData<A>::getPointerInContent(state, classAtom, 0); // class_t.isa
594 assert(metaClassAtom != NULL);
595 return setInstanceMethodList(state, metaClassAtom, methodListAtom, deadAtoms);
596}
597
598
599
600template <>
601void ClassROOverlayAtom<x86_64>::addMethodListFixup()
602{
603 const ld::Atom* targetAtom = this; // temporary
604 uint32_t offset = Class<x86_64>::class_ro_header_size() + 2*8; // class_ro_t.baseMethods
605 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, targetAtom));
606}
607
608template <>
609void ClassROOverlayAtom<arm>::addMethodListFixup()
610{
611 const ld::Atom* targetAtom = this; // temporary
612 uint32_t offset = Class<arm>::class_ro_header_size() + 2*4; // class_ro_t.baseMethods
613 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, targetAtom));
614}
615
616template <>
617void ClassROOverlayAtom<x86>::addMethodListFixup()
618{
619 const ld::Atom* targetAtom = this; // temporary
620 uint32_t offset = Class<x86>::class_ro_header_size() + 2*4; // class_ro_t.baseMethods
621 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, targetAtom));
622}
623
624
625
626template <>
627void ClassROOverlayAtom<x86_64>::addProtocolListFixup()
628{
629 const ld::Atom* targetAtom = this; // temporary
630 uint32_t offset = Class<x86_64>::class_ro_header_size() + 3*8; // class_ro_t.baseProtocols
631 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, targetAtom));
632}
633
634template <>
635void ClassROOverlayAtom<arm>::addProtocolListFixup()
636{
637 const ld::Atom* targetAtom = this; // temporary
638 uint32_t offset = Class<arm>::class_ro_header_size() + 3*4; // class_ro_t.baseProtocols
639 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, targetAtom));
640}
641
642template <>
643void ClassROOverlayAtom<x86>::addProtocolListFixup()
644{
645 const ld::Atom* targetAtom = this; // temporary
646 uint32_t offset = Class<x86>::class_ro_header_size() + 3*4; // class_ro_t.baseProtocols
647 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, targetAtom));
648}
649
650
651template <>
652void ClassROOverlayAtom<x86_64>::addPropertyListFixup()
653{
654 const ld::Atom* targetAtom = this; // temporary
655 uint32_t offset = Class<x86_64>::class_ro_header_size() + 6*8; // class_ro_t.baseProperties
656 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, targetAtom));
657}
658
659template <>
660void ClassROOverlayAtom<arm>::addPropertyListFixup()
661{
662 const ld::Atom* targetAtom = this; // temporary
663 uint32_t offset = Class<arm>::class_ro_header_size() + 6*4; // class_ro_t.baseProperties
664 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, targetAtom));
665}
666
667template <>
668void ClassROOverlayAtom<x86>::addPropertyListFixup()
669{
670 const ld::Atom* targetAtom = this; // temporary
671 uint32_t offset = Class<x86>::class_ro_header_size() + 6*4; // class_ro_t.baseProperties
672 _fixups.push_back(ld::Fixup(offset, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, targetAtom));
673}
674
675
676
677
678//
679// Encapsulates merging of ObjC categories
680//
681template <typename A>
682class OptimizeCategories {
683public:
684 static void doit(const Options& opts, ld::Internal& state);
685 static bool hasInstanceMethods(ld::Internal& state, const std::vector<const ld::Atom*>* categories);
686 static bool hasClassMethods(ld::Internal& state, const std::vector<const ld::Atom*>* categories);
687 static bool hasProtocols(ld::Internal& state, const std::vector<const ld::Atom*>* categories);
688 static bool hasProperties(ld::Internal& state, const std::vector<const ld::Atom*>* categories);
689
690
691 static unsigned int class_ro_baseMethods_offset();
692private:
693 typedef typename A::P::uint_t pint_t;
694
695};
696
697
698template <typename A>
699bool OptimizeCategories<A>::hasInstanceMethods(ld::Internal& state, const std::vector<const ld::Atom*>* categories)
700{
701 for (std::vector<const ld::Atom*>::const_iterator it=categories->begin(); it != categories->end(); ++it) {
702 const ld::Atom* categoryAtom = *it;
703 const ld::Atom* methodList = Category<A>::getInstanceMethods(state, categoryAtom);
704 if ( methodList != NULL ) {
705 if ( MethodList<A>::count(state, methodList) > 0 )
706 return true;
707 }
708 }
709 return false;
710}
711
712
713template <typename A>
714bool OptimizeCategories<A>::hasClassMethods(ld::Internal& state, const std::vector<const ld::Atom*>* categories)
715{
716 for (std::vector<const ld::Atom*>::const_iterator it=categories->begin(); it != categories->end(); ++it) {
717 const ld::Atom* categoryAtom = *it;
718 const ld::Atom* methodList = Category<A>::getClassMethods(state, categoryAtom);
719 if ( methodList != NULL ) {
720 if ( MethodList<A>::count(state, methodList) > 0 )
721 return true;
722 }
723 }
724 return false;
725}
726
727template <typename A>
728bool OptimizeCategories<A>::hasProtocols(ld::Internal& state, const std::vector<const ld::Atom*>* categories)
729{
730 for (std::vector<const ld::Atom*>::const_iterator it=categories->begin(); it != categories->end(); ++it) {
731 const ld::Atom* categoryAtom = *it;
732 const ld::Atom* protocolListAtom = Category<A>::getProtocols(state, categoryAtom);
733 if ( protocolListAtom != NULL ) {
734 if ( ProtocolList<A>::count(state, protocolListAtom) > 0 ) {
735 return true;
736 }
737 }
738 }
739 return false;
740}
741
742
743template <typename A>
744bool OptimizeCategories<A>::hasProperties(ld::Internal& state, const std::vector<const ld::Atom*>* categories)
745{
746 for (std::vector<const ld::Atom*>::const_iterator it=categories->begin(); it != categories->end(); ++it) {
747 const ld::Atom* categoryAtom = *it;
748 const ld::Atom* propertyListAtom = Category<A>::getProperties(state, categoryAtom);
749 if ( propertyListAtom != NULL ) {
750 if ( PropertyList<A>::count(state, propertyListAtom) > 0 )
751 return true;
752 }
753 }
754 return false;
755}
756
757
758
759//
760// Helper for std::remove_if
761//
762class OptimizedAway {
763public:
764 OptimizedAway(const std::set<const ld::Atom*>& oa) : _dead(oa) {}
765 bool operator()(const ld::Atom* atom) const {
766 return ( _dead.count(atom) != 0 );
767 }
768private:
769 const std::set<const ld::Atom*>& _dead;
770};
771
ebf6f434
A
772 struct AtomSorter
773 {
774 bool operator()(const Atom* left, const Atom* right)
775 {
776 // sort by file ordinal, then object address, then zero size, then symbol name
777 // only file based atoms are supported (file() != NULL)
778 if (left==right) return false;
779 const File *leftf = left->file();
780 const File *rightf = right->file();
781
782 if (leftf == rightf) {
783 if (left->objectAddress() != right->objectAddress()) {
784 return left->objectAddress() < right->objectAddress();
785 } else {
786 // for atoms in the same file with the same address, zero sized
787 // atoms must sort before nonzero sized atoms
788 if ((left->size() == 0 && right->size() > 0) || (left->size() > 0 && right->size() == 0))
789 return left->size() < right->size();
790 return strcmp(left->name(), right->name());
791 }
792 }
793 return (leftf->ordinal() < rightf->ordinal());
794 }
795 };
796
797 static void sortAtomVector(std::vector<const Atom*> &atoms) {
798 std::sort(atoms.begin(), atoms.end(), AtomSorter());
799 }
800
a645023d
A
801template <typename A>
802void OptimizeCategories<A>::doit(const Options& opts, ld::Internal& state)
803{
804 // first find all categories referenced by __objc_nlcatlist section
805 std::set<const ld::Atom*> nlcatListAtoms;
806 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
807 ld::Internal::FinalSection* sect = *sit;
eaf282aa 808 if ( (strcmp(sect->sectionName(), "__objc_nlcatlist") == 0) && (strncmp(sect->segmentName(), "__DATA", 6) == 0) ) {
a645023d
A
809 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
810 const ld::Atom* categoryListElementAtom = *ait;
811 for (unsigned int offset=0; offset < categoryListElementAtom->size(); offset += sizeof(pint_t)) {
812 const ld::Atom* categoryAtom = ObjCData<A>::getPointerInContent(state, categoryListElementAtom, offset);
813 //fprintf(stderr, "offset=%d, cat=%p %s\n", offset, categoryAtom, categoryAtom->name());
814 assert(categoryAtom != NULL);
815 nlcatListAtoms.insert(categoryAtom);
816 }
817 }
818 }
819 }
820
821 // build map of all classes in this image that have categories on them
822 typedef std::map<const ld::Atom*, std::vector<const ld::Atom*>*> CatMap;
823 CatMap classToCategories;
ebf6f434 824 std::vector<const ld::Atom*> classOrder;
a645023d
A
825 std::set<const ld::Atom*> deadAtoms;
826 ld::Internal::FinalSection* methodListSection = NULL;
827 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
828 ld::Internal::FinalSection* sect = *sit;
829 if ( sect->type() == ld::Section::typeObjC2CategoryList ) {
830 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
831 const ld::Atom* categoryListElementAtom = *ait;
832 bool hasAddend;
833 const ld::Atom* categoryAtom = ObjCData<A>::getPointerInContent(state, categoryListElementAtom, 0, &hasAddend);
834 if ( hasAddend || (categoryAtom->symbolTableInclusion() == ld::Atom::symbolTableNotIn)) {
835 //<rdar://problem/8309530> gcc-4.0 uses 'L' labels on categories which disables this optimization
836 //warning("__objc_catlist element does not point to start of category");
837 continue;
838 }
839 assert(categoryAtom != NULL);
ebf6f434 840 assert(categoryAtom->size() >= Category<A>::size());
a645023d
A
841 // ignore categories also in __objc_nlcatlist
842 if ( nlcatListAtoms.count(categoryAtom) != 0 )
843 continue;
599556ff 844 const ld::Atom* categoryOnClassAtom = Category<A>::getClass(state, categoryAtom, hasAddend);
a645023d 845 assert(categoryOnClassAtom != NULL);
599556ff 846 // only look at classes defined in this image
a645023d 847 if ( categoryOnClassAtom->definition() != ld::Atom::definitionProxy ) {
599556ff
A
848 // <rdar://problem/16107696> for now, back off optimization on new style classes
849 if ( hasAddend != 0 )
850 continue;
851 // <rdar://problem/17249777> don't apply categories to swift classes
852 if ( categoryOnClassAtom->hasFixupsOfKind(ld::Fixup::kindNoneGroupSubordinate) )
853 continue;
854
a645023d
A
855 CatMap::iterator pos = classToCategories.find(categoryOnClassAtom);
856 if ( pos == classToCategories.end() ) {
857 classToCategories[categoryOnClassAtom] = new std::vector<const ld::Atom*>();
ebf6f434 858 classOrder.push_back(categoryOnClassAtom);
a645023d
A
859 }
860 classToCategories[categoryOnClassAtom]->push_back(categoryAtom);
861 // mark category atom and catlist atom as dead
862 deadAtoms.insert(categoryAtom);
863 deadAtoms.insert(categoryListElementAtom);
864 }
865 }
866 }
867 // record method list section
eaf282aa 868 if ( (strcmp(sect->sectionName(), "__objc_const") == 0) && (strncmp(sect->segmentName(), "__DATA", 6) == 0) )
a645023d
A
869 methodListSection = sect;
870 }
871
872 // if found some categories
873 if ( classToCategories.size() != 0 ) {
874 assert(methodListSection != NULL);
ebf6f434 875 sortAtomVector(classOrder);
a645023d 876 // alter each class definition to have new method list which includes all category methods
ebf6f434
A
877 for (std::vector<const ld::Atom*>::iterator it = classOrder.begin(); it != classOrder.end(); it++) {
878 const ld::Atom* classAtom = *it;
879 const std::vector<const ld::Atom*>* categories = classToCategories[classAtom];
a645023d
A
880 assert(categories->size() != 0);
881 // if any category adds instance methods, generate new merged method list, and replace
882 if ( OptimizeCategories<A>::hasInstanceMethods(state, categories) ) {
883 const ld::Atom* baseInstanceMethodListAtom = Class<A>::getInstanceMethodList(state, classAtom);
884 const ld::Atom* newInstanceMethodListAtom = new MethodListAtom<A>(state, baseInstanceMethodListAtom, false, categories, deadAtoms);
885 const ld::Atom* newClassRO = Class<A>::setInstanceMethodList(state, classAtom, newInstanceMethodListAtom, deadAtoms);
886 // add new method list to final sections
887 methodListSection->atoms.push_back(newInstanceMethodListAtom);
eaf282aa 888 state.atomToSection[newInstanceMethodListAtom] = methodListSection;
a645023d
A
889 if ( newClassRO != NULL ) {
890 assert(strcmp(newClassRO->section().sectionName(), "__objc_const") == 0);
891 methodListSection->atoms.push_back(newClassRO);
eaf282aa 892 state.atomToSection[newClassRO] = methodListSection;
a645023d
A
893 }
894 }
895 // if any category adds class methods, generate new merged method list, and replace
896 if ( OptimizeCategories<A>::hasClassMethods(state, categories) ) {
897 const ld::Atom* baseClassMethodListAtom = Class<A>::getClassMethodList(state, classAtom);
898 const ld::Atom* newClassMethodListAtom = new MethodListAtom<A>(state, baseClassMethodListAtom, true, categories, deadAtoms);
899 const ld::Atom* newClassRO = Class<A>::setClassMethodList(state, classAtom, newClassMethodListAtom, deadAtoms);
900 // add new method list to final sections
901 methodListSection->atoms.push_back(newClassMethodListAtom);
eaf282aa 902 state.atomToSection[newClassMethodListAtom] = methodListSection;
a645023d
A
903 if ( newClassRO != NULL ) {
904 assert(strcmp(newClassRO->section().sectionName(), "__objc_const") == 0);
905 methodListSection->atoms.push_back(newClassRO);
eaf282aa 906 state.atomToSection[newClassRO] = methodListSection;
a645023d
A
907 }
908 }
909 // if any category adds protocols, generate new merged protocol list, and replace
910 if ( OptimizeCategories<A>::hasProtocols(state, categories) ) {
911 const ld::Atom* baseProtocolListAtom = Class<A>::getInstanceProtocolList(state, classAtom);
912 const ld::Atom* newProtocolListAtom = new ProtocolListAtom<A>(state, baseProtocolListAtom, categories, deadAtoms);
913 const ld::Atom* newClassRO = Class<A>::setInstanceProtocolList(state, classAtom, newProtocolListAtom, deadAtoms);
914 const ld::Atom* newMetaClassRO = Class<A>::setClassProtocolList(state, classAtom, newProtocolListAtom, deadAtoms);
915 // add new protocol list to final sections
916 methodListSection->atoms.push_back(newProtocolListAtom);
eaf282aa 917 state.atomToSection[newProtocolListAtom] = methodListSection;
a645023d
A
918 if ( newClassRO != NULL ) {
919 assert(strcmp(newClassRO->section().sectionName(), "__objc_const") == 0);
920 methodListSection->atoms.push_back(newClassRO);
eaf282aa 921 state.atomToSection[newClassRO] = methodListSection;
a645023d
A
922 }
923 if ( newMetaClassRO != NULL ) {
924 assert(strcmp(newMetaClassRO->section().sectionName(), "__objc_const") == 0);
925 methodListSection->atoms.push_back(newMetaClassRO);
eaf282aa 926 state.atomToSection[newMetaClassRO] = methodListSection;
a645023d
A
927 }
928 }
929 // if any category adds properties, generate new merged property list, and replace
930 if ( OptimizeCategories<A>::hasProperties(state, categories) ) {
931 const ld::Atom* basePropertyListAtom = Class<A>::getInstancePropertyList(state, classAtom);
932 const ld::Atom* newPropertyListAtom = new PropertyListAtom<A>(state, basePropertyListAtom, categories, deadAtoms);
933 const ld::Atom* newClassRO = Class<A>::setInstancePropertyList(state, classAtom, newPropertyListAtom, deadAtoms);
934 // add new property list to final sections
935 methodListSection->atoms.push_back(newPropertyListAtom);
eaf282aa 936 state.atomToSection[newPropertyListAtom] = methodListSection;
a645023d
A
937 if ( newClassRO != NULL ) {
938 assert(strcmp(newClassRO->section().sectionName(), "__objc_const") == 0);
939 methodListSection->atoms.push_back(newClassRO);
eaf282aa 940 state.atomToSection[newClassRO] = methodListSection;
a645023d
A
941 }
942 }
943
944 }
945
946 // remove dead atoms
947 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
948 ld::Internal::FinalSection* sect = *sit;
949 sect->atoms.erase(std::remove_if(sect->atoms.begin(), sect->atoms.end(), OptimizedAway(deadAtoms)), sect->atoms.end());
950 }
951 }
952}
953
954
955template <typename A>
956MethodListAtom<A>::MethodListAtom(ld::Internal& state, const ld::Atom* baseMethodList, bool meta,
957 const std::vector<const ld::Atom*>* categories, std::set<const ld::Atom*>& deadAtoms)
958 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
959 ld::Atom::scopeLinkageUnit, ld::Atom::typeUnclassified,
960 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)), _file(NULL), _methodCount(0)
961{
962 unsigned int fixupCount = 0;
afe874b1 963 std::set<const ld::Atom*> baseMethodListMethodNameAtoms;
a645023d
A
964 // if base class has method list, then associate new method list with file defining class
965 if ( baseMethodList != NULL ) {
966 _file = baseMethodList->file();
967 // calculate total size of merge method lists
968 _methodCount = MethodList<A>::count(state, baseMethodList);
969 deadAtoms.insert(baseMethodList);
970 fixupCount = baseMethodList->fixupsEnd() - baseMethodList->fixupsBegin();
afe874b1
A
971 for (ld::Fixup::iterator fit=baseMethodList->fixupsBegin(); fit != baseMethodList->fixupsEnd(); ++fit) {
972 if ( (fit->offsetInAtom - 8) % (3*sizeof(pint_t)) == 0 ) {
973 assert(fit->binding == ld::Fixup::bindingsIndirectlyBound && "malformed method list");
974 const ld::Atom* target = state.indirectBindingTable[fit->u.bindingIndex];
975 assert(target->contentType() == ld::Atom::typeCString && "malformed method list");
976 baseMethodListMethodNameAtoms.insert(target);
977 }
978 }
a645023d
A
979 }
980 for (std::vector<const ld::Atom*>::const_iterator ait=categories->begin(); ait != categories->end(); ++ait) {
981 const ld::Atom* categoryMethodListAtom;
982 if ( meta )
983 categoryMethodListAtom = Category<A>::getClassMethods(state, *ait);
984 else
985 categoryMethodListAtom = Category<A>::getInstanceMethods(state, *ait);
986 if ( categoryMethodListAtom != NULL ) {
987 _methodCount += MethodList<A>::count(state, categoryMethodListAtom);
988 fixupCount += (categoryMethodListAtom->fixupsEnd() - categoryMethodListAtom->fixupsBegin());
989 deadAtoms.insert(categoryMethodListAtom);
990 // if base class did not have method list, associate new method list with file the defined category
991 if ( _file == NULL )
992 _file = categoryMethodListAtom->file();
993 }
994 }
995 //if ( baseMethodList != NULL )
996 // fprintf(stderr, "total merged method count=%u for baseMethodList=%s\n", _methodCount, baseMethodList->name());
997 //else
998 // fprintf(stderr, "total merged method count=%u\n", _methodCount);
999 //fprintf(stderr, "total merged fixup count=%u\n", fixupCount);
1000
1001 // copy fixups and adjust offsets (in reverse order to simulator objc runtime)
1002 _fixups.reserve(fixupCount);
1003 uint32_t slide = 0;
afe874b1 1004 std::set<const ld::Atom*> categoryMethodNameAtoms;
a645023d
A
1005 for (std::vector<const ld::Atom*>::const_reverse_iterator rit=categories->rbegin(); rit != categories->rend(); ++rit) {
1006 const ld::Atom* categoryMethodListAtom;
1007 if ( meta )
1008 categoryMethodListAtom = Category<A>::getClassMethods(state, *rit);
1009 else
1010 categoryMethodListAtom = Category<A>::getInstanceMethods(state, *rit);
1011 if ( categoryMethodListAtom != NULL ) {
1012 for (ld::Fixup::iterator fit=categoryMethodListAtom->fixupsBegin(); fit != categoryMethodListAtom->fixupsEnd(); ++fit) {
1013 ld::Fixup fixup = *fit;
1014 fixup.offsetInAtom += slide;
1015 _fixups.push_back(fixup);
afe874b1
A
1016 if ( (fixup.offsetInAtom - 8) % (3*sizeof(pint_t)) == 0 ) {
1017 // <rdar://problem/8642343> warning when a method is overridden in a category in the same link unit
1018 assert(fixup.binding == ld::Fixup::bindingsIndirectlyBound && "malformed category method list");
1019 const ld::Atom* target = state.indirectBindingTable[fixup.u.bindingIndex];
1020 assert(target->contentType() == ld::Atom::typeCString && "malformed method list");
1021 // this objc pass happens after cstrings are coalesced, so we can just compare the atom addres instead of its content
1022 if ( baseMethodListMethodNameAtoms.count(target) != 0 ) {
1023 warning("%s method '%s' in category from %s overrides method from class in %s",
1024 (meta ? "meta" : "instance"), target->rawContentPointer(),
1025 categoryMethodListAtom->file()->path(), baseMethodList->file()->path() );
1026 }
1027 if ( categoryMethodNameAtoms.count(target) != 0 ) {
1028 warning("%s method '%s' in category from %s conflicts with same method from another category",
1029 (meta ? "meta" : "instance"), target->rawContentPointer(),
1030 categoryMethodListAtom->file()->path());
1031 }
1032 categoryMethodNameAtoms.insert(target);
1033 }
a645023d
A
1034 }
1035 slide += 3*sizeof(pint_t) * MethodList<A>::count(state, categoryMethodListAtom);
1036 }
1037 }
1038 // add method list from base class last
1039 if ( baseMethodList != NULL ) {
1040 for (ld::Fixup::iterator fit=baseMethodList->fixupsBegin(); fit != baseMethodList->fixupsEnd(); ++fit) {
1041 ld::Fixup fixup = *fit;
1042 fixup.offsetInAtom += slide;
1043 _fixups.push_back(fixup);
1044 }
1045 }
1046}
1047
1048
1049template <typename A>
1050ProtocolListAtom<A>::ProtocolListAtom(ld::Internal& state, const ld::Atom* baseProtocolList,
1051 const std::vector<const ld::Atom*>* categories, std::set<const ld::Atom*>& deadAtoms)
1052 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
1053 ld::Atom::scopeLinkageUnit, ld::Atom::typeUnclassified,
1054 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)), _file(NULL), _protocolCount(0)
1055{
1056 unsigned int fixupCount = 0;
1057 if ( baseProtocolList != NULL ) {
1058 // if base class has protocol list, then associate new protocol list with file defining class
1059 _file = baseProtocolList->file();
1060 // calculate total size of merged protocol list
1061 _protocolCount = ProtocolList<A>::count(state, baseProtocolList);
1062 deadAtoms.insert(baseProtocolList);
1063 fixupCount = baseProtocolList->fixupsEnd() - baseProtocolList->fixupsBegin();
1064 }
1065 for (std::vector<const ld::Atom*>::const_iterator ait=categories->begin(); ait != categories->end(); ++ait) {
1066 const ld::Atom* categoryProtocolListAtom = Category<A>::getProtocols(state, *ait);
1067 if ( categoryProtocolListAtom != NULL ) {
1068 _protocolCount += ProtocolList<A>::count(state, categoryProtocolListAtom);
1069 fixupCount += (categoryProtocolListAtom->fixupsEnd() - categoryProtocolListAtom->fixupsBegin());
1070 deadAtoms.insert(categoryProtocolListAtom);
1071 // if base class did not have protocol list, associate new protocol list with file the defined category
1072 if ( _file == NULL )
1073 _file = categoryProtocolListAtom->file();
1074 }
1075 }
1076 //fprintf(stderr, "total merged protocol count=%u\n", _protocolCount);
1077 //fprintf(stderr, "total merged fixup count=%u\n", fixupCount);
1078
1079 // copy fixups and adjust offsets
1080 _fixups.reserve(fixupCount);
1081 uint32_t slide = 0;
1082 for (std::vector<const ld::Atom*>::const_iterator it=categories->begin(); it != categories->end(); ++it) {
1083 const ld::Atom* categoryProtocolListAtom = Category<A>::getProtocols(state, *it);
1084 if ( categoryProtocolListAtom != NULL ) {
1085 for (ld::Fixup::iterator fit=categoryProtocolListAtom->fixupsBegin(); fit != categoryProtocolListAtom->fixupsEnd(); ++fit) {
1086 ld::Fixup fixup = *fit;
1087 fixup.offsetInAtom += slide;
1088 _fixups.push_back(fixup);
1089 //if ( fixup.binding == ld::Fixup::bindingDirectlyBound )
1090 // fprintf(stderr, "offset=0x%08X, name=%s\n", fixup.offsetInAtom, fixup.u.target->name());
1091 }
1092 slide += sizeof(pint_t) * ProtocolList<A>::count(state, categoryProtocolListAtom);
1093 }
1094 }
1095 // add method list from base class last
1096 if ( baseProtocolList != NULL ) {
1097 for (ld::Fixup::iterator fit=baseProtocolList->fixupsBegin(); fit != baseProtocolList->fixupsEnd(); ++fit) {
1098 ld::Fixup fixup = *fit;
1099 fixup.offsetInAtom += slide;
1100 _fixups.push_back(fixup);
1101 }
1102 }
1103}
1104
1105
1106template <typename A>
1107PropertyListAtom<A>::PropertyListAtom(ld::Internal& state, const ld::Atom* basePropertyList,
1108 const std::vector<const ld::Atom*>* categories, std::set<const ld::Atom*>& deadAtoms)
1109 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
1110 ld::Atom::scopeLinkageUnit, ld::Atom::typeUnclassified,
1111 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)), _file(NULL), _propertyCount(0)
1112{
1113 unsigned int fixupCount = 0;
1114 if ( basePropertyList != NULL ) {
1115 // if base class has property list, then associate new property list with file defining class
1116 _file = basePropertyList->file();
1117 // calculate total size of merged property list
1118 _propertyCount = PropertyList<A>::count(state, basePropertyList);
1119 deadAtoms.insert(basePropertyList);
1120 fixupCount = basePropertyList->fixupsEnd() - basePropertyList->fixupsBegin();
1121 }
1122 for (std::vector<const ld::Atom*>::const_iterator ait=categories->begin(); ait != categories->end(); ++ait) {
1123 const ld::Atom* categoryPropertyListAtom = Category<A>::getProperties(state, *ait);
1124 if ( categoryPropertyListAtom != NULL ) {
1125 _propertyCount += PropertyList<A>::count(state, categoryPropertyListAtom);
1126 fixupCount += (categoryPropertyListAtom->fixupsEnd() - categoryPropertyListAtom->fixupsBegin());
1127 deadAtoms.insert(categoryPropertyListAtom);
1128 // if base class did not have property list, associate new property list with file the defined category
1129 if ( _file == NULL )
1130 _file = categoryPropertyListAtom->file();
1131 }
1132 }
1133 //fprintf(stderr, "total merged property count=%u\n", _propertyCount);
1134 //fprintf(stderr, "total merged fixup count=%u\n", fixupCount);
1135
1136 // copy fixups and adjust offsets
1137 _fixups.reserve(fixupCount);
1138 uint32_t slide = 0;
1139 for (std::vector<const ld::Atom*>::const_iterator it=categories->begin(); it != categories->end(); ++it) {
1140 const ld::Atom* categoryPropertyListAtom = Category<A>::getProperties(state, *it);
1141 if ( categoryPropertyListAtom != NULL ) {
1142 for (ld::Fixup::iterator fit=categoryPropertyListAtom->fixupsBegin(); fit != categoryPropertyListAtom->fixupsEnd(); ++fit) {
1143 ld::Fixup fixup = *fit;
1144 fixup.offsetInAtom += slide;
1145 _fixups.push_back(fixup);
1146 //fprintf(stderr, "offset=0x%08X, binding=%d\n", fixup.offsetInAtom, fixup.binding);
1147 //if ( fixup.binding == ld::Fixup::bindingDirectlyBound )
1148 // fprintf(stderr, "offset=0x%08X, name=%s\n", fixup.offsetInAtom, fixup.u.target->name());
1149 //else if ( fixup.binding == ld::Fixup::bindingsIndirectlyBound )
1150 // fprintf(stderr, "offset=0x%08X, indirect index=%u, name=%s\n", fixup.offsetInAtom, fixup.u.bindingIndex,
1151 // (char*)(state.indirectBindingTable[fixup.u.bindingIndex]->rawContentPointer()));
1152 }
1153 slide += 2*sizeof(pint_t) * PropertyList<A>::count(state, categoryPropertyListAtom);
1154 }
1155 }
1156 // add method list from base class last
1157 if ( basePropertyList != NULL ) {
1158 for (ld::Fixup::iterator fit=basePropertyList->fixupsBegin(); fit != basePropertyList->fixupsEnd(); ++fit) {
1159 ld::Fixup fixup = *fit;
1160 fixup.offsetInAtom += slide;
1161 _fixups.push_back(fixup);
1162 }
1163 }
1164}
1165
1166
1167
1168
1169void doPass(const Options& opts, ld::Internal& state)
1170{
1171 // only make image info section if objc was used
1172 if ( state.objcObjectConstraint != ld::File::objcConstraintNone ) {
1173
1174 // verify dylibs are GC compatible with object files
1175 if ( state.objcObjectConstraint != state.objcDylibConstraint ) {
1176 if ( (state.objcDylibConstraint == ld::File::objcConstraintRetainRelease)
1177 && (state.objcObjectConstraint == ld::File::objcConstraintGC) ) {
1178 throw "Linked dylibs built for retain/release but object files built for GC-only";
1179 }
1180 else if ( (state.objcDylibConstraint == ld::File::objcConstraintGC)
1181 && (state.objcObjectConstraint == ld::File::objcConstraintRetainRelease) ) {
1182 throw "Linked dylibs built for GC-only but object files built for retain/release";
1183 }
1184 }
1185
1186 const bool compaction = opts.objcGcCompaction();
1187
1188 // add image info atom
1189 switch ( opts.architecture() ) {
ebf6f434 1190#if SUPPORT_ARCH_x86_64
a645023d
A
1191 case CPU_TYPE_X86_64:
1192 state.addAtom(*new ObjCImageInfoAtom<x86_64>(state.objcObjectConstraint, compaction,
599556ff 1193 true, state.swiftVersion));
a645023d 1194 break;
ebf6f434
A
1195#endif
1196#if SUPPORT_ARCH_i386
a645023d
A
1197 case CPU_TYPE_I386:
1198 state.addAtom(*new ObjCImageInfoAtom<x86>(state.objcObjectConstraint, compaction,
599556ff 1199 opts.objCABIVersion2POverride() ? true : false, state.swiftVersion));
a645023d 1200 break;
ebf6f434 1201#endif
f80fe69f 1202#if SUPPORT_ARCH_arm_any
a645023d
A
1203 case CPU_TYPE_ARM:
1204 state.addAtom(*new ObjCImageInfoAtom<arm>(state.objcObjectConstraint, compaction,
599556ff 1205 true, state.swiftVersion));
a645023d 1206 break;
f80fe69f
A
1207#endif
1208#if SUPPORT_ARCH_arm64
1209 case CPU_TYPE_ARM64:
1210 state.addAtom(*new ObjCImageInfoAtom<arm64>(state.objcObjectConstraint, compaction,
599556ff 1211 true, state.swiftVersion));
f80fe69f
A
1212 break;
1213#endif
a645023d
A
1214 default:
1215 assert(0 && "unknown objc arch");
1216 }
1217 }
1218
1219 if ( opts.objcCategoryMerging() ) {
1220 // optimize classes defined in this linkage unit by merging in categories also in this linkage unit
1221 switch ( opts.architecture() ) {
ebf6f434 1222#if SUPPORT_ARCH_x86_64
a645023d
A
1223 case CPU_TYPE_X86_64:
1224 OptimizeCategories<x86_64>::doit(opts, state);
1225 break;
ebf6f434
A
1226#endif
1227#if SUPPORT_ARCH_i386
a645023d 1228 case CPU_TYPE_I386:
ebf6f434
A
1229 if ( opts.objCABIVersion2POverride() )
1230 OptimizeCategories<x86>::doit(opts, state);
a645023d 1231 break;
ebf6f434
A
1232#endif
1233#if SUPPORT_ARCH_arm_any
a645023d 1234 case CPU_TYPE_ARM:
ebf6f434 1235 OptimizeCategories<arm>::doit(opts, state);
a645023d 1236 break;
f80fe69f
A
1237#endif
1238#if SUPPORT_ARCH_arm64
1239 case CPU_TYPE_ARM64:
1240 // disabled until tested
1241 break;
ebf6f434 1242#endif
a645023d
A
1243 default:
1244 assert(0 && "unknown objc arch");
1245 }
1246 }
1247}
1248
1249
1250} // namespace objc
1251} // namespace passes
1252} // namespace ld