1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2011 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
28 #include <sys/param.h>
29 #include <mach-o/ranlib.h>
36 #include <ext/hash_map>
38 #include "MachOFileAbstraction.hpp"
39 #include "Architectures.hpp"
41 #include "macho_relocatable_file.h"
43 #include "archive_file.h"
48 typedef const struct ranlib
* ConstRanLibPtr
;
51 template <typename A
> class File
;
58 typedef typename
A::P P
;
60 static bool validFile(const uint8_t* fileContent
, uint64_t fileLength
,
61 const mach_o::relocatable::ParserOptions
& opts
) {
62 return File
<A
>::validFile(fileContent
, fileLength
, opts
); }
63 static File
<A
>* parse(const uint8_t* fileContent
, uint64_t fileLength
,
64 const char* path
, time_t mTime
,
65 ld::File::Ordinal ordinal
, const ParserOptions
& opts
) {
66 return new File
<A
>(fileContent
, fileLength
, path
, mTime
,
73 class File
: public ld::archive::File
76 static bool validFile(const uint8_t* fileContent
, uint64_t fileLength
,
77 const mach_o::relocatable::ParserOptions
& opts
);
78 File(const uint8_t* fileContent
, uint64_t fileLength
,
79 const char* pth
, time_t modTime
,
80 ld::File::Ordinal ord
, const ParserOptions
& opts
);
83 // overrides of ld::File
84 virtual bool forEachAtom(ld::File::AtomHandler
&) const;
85 virtual bool justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
&) const;
86 virtual uint32_t subFileCount() const { return _archiveFilelength
/sizeof(ar_hdr
); }
88 // overrides of ld::archive::File
89 virtual bool justInTimeDataOnlyforEachAtom(const char* name
, ld::File::AtomHandler
& handler
) const;
92 static bool validMachOFile(const uint8_t* fileContent
, uint64_t fileLength
,
93 const mach_o::relocatable::ParserOptions
& opts
);
94 static bool validLTOFile(const uint8_t* fileContent
, uint64_t fileLength
,
95 const mach_o::relocatable::ParserOptions
& opts
);
96 static cpu_type_t
architecture();
101 void getName(char *, int) const;
102 time_t modificationTime() const;
103 const uint8_t* content() const;
104 uint32_t contentSize() const;
105 const Entry
* next() const;
107 bool hasLongName() const;
108 unsigned int getLongNameSpace() const;
112 struct MemberState
{ ld::relocatable::File
* file
; const Entry
*entry
; bool logged
; bool loaded
; uint16_t index
;};
113 bool loadMember(MemberState
& state
, ld::File::AtomHandler
& handler
, const char *format
, ...) const;
118 bool operator()(const char* left
, const char* right
) const { return (strcmp(left
, right
) == 0); }
120 typedef __gnu_cxx::hash_map
<const char*, const struct ranlib
*, __gnu_cxx::hash
<const char*>, CStringEquals
> NameToEntryMap
;
122 typedef typename
A::P P
;
123 typedef typename
A::P::E E
;
125 typedef std::map
<const class Entry
*, MemberState
> MemberToStateMap
;
127 const struct ranlib
* ranlibHashSearch(const char* name
) const;
128 MemberState
& makeObjectFileForMember(const Entry
* member
) const;
129 bool memberHasObjCCategories(const Entry
* member
) const;
130 void dumpTableOfContents();
131 void buildHashTable();
133 const uint8_t* _archiveFileContent
;
134 uint64_t _archiveFilelength
;
135 const struct ranlib
* _tableOfContents
;
136 uint32_t _tableOfContentCount
;
137 const char* _tableOfContentStrings
;
138 mutable MemberToStateMap _instantiatedEntries
;
139 NameToEntryMap _hashTable
;
140 const bool _forceLoadAll
;
141 const bool _forceLoadObjC
;
142 const bool _forceLoadThis
;
143 const bool _objc2ABI
;
144 const bool _verboseLoad
;
145 const bool _logAllFiles
;
146 const mach_o::relocatable::ParserOptions _objOpts
;
150 template <typename A
>
151 bool File
<A
>::Entry::hasLongName() const
153 return ( strncmp(this->ar_name
, AR_EFMT1
, strlen(AR_EFMT1
)) == 0 );
156 template <typename A
>
157 unsigned int File
<A
>::Entry::getLongNameSpace() const
160 long result
= strtol(&this->ar_name
[strlen(AR_EFMT1
)], &endptr
, 10);
164 template <typename A
>
165 void File
<A
>::Entry::getName(char *buf
, int bufsz
) const
167 if ( this->hasLongName() ) {
168 int len
= this->getLongNameSpace();
169 assert(bufsz
>= len
+1);
170 strncpy(buf
, ((char*)this)+sizeof(ar_hdr
), len
);
174 assert(bufsz
>= 16+1);
175 strncpy(buf
, this->ar_name
, 16);
177 char* space
= strchr(buf
, ' ');
183 template <typename A
>
184 time_t File
<A
>::Entry::modificationTime() const
187 strncpy(temp
, this->ar_date
, 12);
190 return (time_t)strtol(temp
, &endptr
, 10);
194 template <typename A
>
195 const uint8_t* File
<A
>::Entry::content() const
197 if ( this->hasLongName() )
198 return ((uint8_t*)this) + sizeof(ar_hdr
) + this->getLongNameSpace();
200 return ((uint8_t*)this) + sizeof(ar_hdr
);
204 template <typename A
>
205 uint32_t File
<A
>::Entry::contentSize() const
208 strncpy(temp
, this->ar_size
, 10);
211 long size
= strtol(temp
, &endptr
, 10);
212 // long name is included in ar_size
213 if ( this->hasLongName() )
214 size
-= this->getLongNameSpace();
219 template <typename A
>
220 const class File
<A
>::Entry
* File
<A
>::Entry::next() const
222 const uint8_t* p
= this->content() + contentSize();
223 p
= (const uint8_t*)(((uintptr_t)p
+3) & (-4)); // 4-byte align
224 return (class File
<A
>::Entry
*)p
;
228 template <> cpu_type_t File
<x86
>::architecture() { return CPU_TYPE_I386
; }
229 template <> cpu_type_t File
<x86_64
>::architecture() { return CPU_TYPE_X86_64
; }
230 template <> cpu_type_t File
<arm
>::architecture() { return CPU_TYPE_ARM
; }
233 template <typename A
>
234 bool File
<A
>::validMachOFile(const uint8_t* fileContent
, uint64_t fileLength
, const mach_o::relocatable::ParserOptions
& opts
)
236 return mach_o::relocatable::isObjectFile(fileContent
, fileLength
, opts
);
239 template <typename A
>
240 bool File
<A
>::validLTOFile(const uint8_t* fileContent
, uint64_t fileLength
, const mach_o::relocatable::ParserOptions
& opts
)
242 return lto::isObjectFile(fileContent
, fileLength
, opts
.architecture
, opts
.subType
);
247 template <typename A
>
248 bool File
<A
>::validFile(const uint8_t* fileContent
, uint64_t fileLength
, const mach_o::relocatable::ParserOptions
& opts
)
250 // must have valid archive header
251 if ( strncmp((const char*)fileContent
, "!<arch>\n", 8) != 0 )
254 // peak at first .o file and verify it is correct architecture
255 const Entry
* const start
= (Entry
*)&fileContent
[8];
256 const Entry
* const end
= (Entry
*)&fileContent
[fileLength
];
257 for (const Entry
* p
=start
; p
< end
; p
= p
->next()) {
258 char memberName
[256];
259 p
->getName(memberName
, sizeof(memberName
));
260 // skip option table-of-content member
261 if ( (p
==start
) && ((strcmp(memberName
, SYMDEF_SORTED
) == 0) || (strcmp(memberName
, SYMDEF
) == 0)) )
263 // archive is valid if first .o file is valid
264 return (validMachOFile(p
->content(), p
->contentSize(), opts
) || validLTOFile(p
->content(), p
->contentSize(), opts
));
271 template <typename A
>
272 File
<A
>::File(const uint8_t fileContent
[], uint64_t fileLength
, const char* pth
, time_t modTime
,
273 ld::File::Ordinal ord
, const ParserOptions
& opts
)
274 : ld::archive::File(strdup(pth
), modTime
, ord
),
275 _archiveFileContent(fileContent
), _archiveFilelength(fileLength
),
276 _tableOfContents(NULL
), _tableOfContentCount(0), _tableOfContentStrings(NULL
),
277 _forceLoadAll(opts
.forceLoadAll
), _forceLoadObjC(opts
.forceLoadObjC
),
278 _forceLoadThis(opts
.forceLoadThisArchive
), _objc2ABI(opts
.objcABI2
), _verboseLoad(opts
.verboseLoad
),
279 _logAllFiles(opts
.logAllFiles
), _objOpts(opts
.objOpts
)
281 if ( strncmp((const char*)fileContent
, "!<arch>\n", 8) != 0 )
282 throw "not an archive";
284 if ( !_forceLoadAll
) {
285 const Entry
* const firstMember
= (Entry
*)&_archiveFileContent
[8];
286 char memberName
[256];
287 firstMember
->getName(memberName
, sizeof(memberName
));
288 if ( (strcmp(memberName
, SYMDEF_SORTED
) == 0) || (strcmp(memberName
, SYMDEF
) == 0) ) {
289 const uint8_t* contents
= firstMember
->content();
290 uint32_t ranlibArrayLen
= E::get32(*((uint32_t*)contents
));
291 _tableOfContents
= (const struct ranlib
*)&contents
[4];
292 _tableOfContentCount
= ranlibArrayLen
/ sizeof(struct ranlib
);
293 _tableOfContentStrings
= (const char*)&contents
[ranlibArrayLen
+8];
294 if ( ((uint8_t*)(&_tableOfContents
[_tableOfContentCount
]) > &fileContent
[fileLength
])
295 || ((uint8_t*)_tableOfContentStrings
> &fileContent
[fileLength
]) )
296 throw "malformed archive, perhaps wrong architecture";
297 this->buildHashTable();
300 throw "archive has no table of contents";
305 bool File
<x86
>::memberHasObjCCategories(const Entry
* member
) const
308 // i386 for iOS simulator uses ObjC2 which has no global symbol for categories
309 return mach_o::relocatable::hasObjC2Categories(member
->content());
312 // i386 uses ObjC1 ABI which has .objc_category* global symbols
313 // <rdar://problem/11342022> strip -S on i386 pulls out .objc_category_name symbols from static frameworks
314 return mach_o::relocatable::hasObjC1Categories(member
->content());
320 template <typename A
>
321 bool File
<A
>::memberHasObjCCategories(const Entry
* member
) const
323 // x86_64 and ARM use ObjC2 which has no global symbol for categories
324 return mach_o::relocatable::hasObjC2Categories(member
->content());
328 template <typename A
>
329 typename File
<A
>::MemberState
& File
<A
>::makeObjectFileForMember(const Entry
* member
) const
331 uint16_t memberIndex
= 0;
332 // in case member was instantiated earlier but not needed yet
333 typename
MemberToStateMap::iterator pos
= _instantiatedEntries
.find(member
);
334 if ( pos
== _instantiatedEntries
.end() ) {
335 // Have to find the index of this member
338 if (_instantiatedEntries
.size() == 0) {
339 start
= (Entry
*)&_archiveFileContent
[8];
342 MemberState
&lastKnown
= _instantiatedEntries
.rbegin()->second
;
343 start
= lastKnown
.entry
->next();
344 index
= lastKnown
.index
+1;
346 for (const Entry
* p
=start
; p
<= member
; p
= p
->next(), index
++) {
347 MemberState state
= {NULL
, p
, false, false, index
};
348 _instantiatedEntries
[p
] = state
;
354 MemberState
& state
= pos
->second
;
357 memberIndex
= state
.index
;
359 assert(memberIndex
!= 0);
360 char memberName
[256];
361 member
->getName(memberName
, sizeof(memberName
));
362 char memberPath
[strlen(this->path()) + strlen(memberName
)+4];
363 strcpy(memberPath
, this->path());
364 strcat(memberPath
, "(");
365 strcat(memberPath
, memberName
);
366 strcat(memberPath
, ")");
367 //fprintf(stderr, "using %s from %s\n", memberName, this->path());
370 if ( member
> (Entry
*)(_archiveFileContent
+_archiveFilelength
) )
371 throwf("corrupt archive, member starts past end of file");
372 if ( (member
->content() + member
->contentSize()) > (_archiveFileContent
+_archiveFilelength
) )
373 throwf("corrupt archive, member contents extends past end of file");
374 const char* mPath
= strdup(memberPath
);
375 // see if member is mach-o file
376 ld::File::Ordinal ordinal
= this->ordinal().archiveOrdinalWithMemberIndex(memberIndex
);
377 ld::relocatable::File
* result
= mach_o::relocatable::parse(member
->content(), member
->contentSize(),
378 mPath
, member
->modificationTime(),
380 if ( result
!= NULL
) {
381 MemberState state
= {result
, member
, false, false, memberIndex
};
382 _instantiatedEntries
[member
] = state
;
383 return _instantiatedEntries
[member
];
385 // see if member is llvm bitcode file
386 result
= lto::parse(member
->content(), member
->contentSize(),
387 mPath
, member
->modificationTime(),
388 _objOpts
.architecture
, _objOpts
.subType
, _logAllFiles
);
389 if ( result
!= NULL
) {
390 MemberState state
= {result
, member
, false, false, memberIndex
};
391 _instantiatedEntries
[member
] = state
;
392 return _instantiatedEntries
[member
];
395 throwf("archive member '%s' with length %d is not mach-o or llvm bitcode", memberName
, member
->contentSize());
397 catch (const char* msg
) {
398 throwf("in %s, %s", memberPath
, msg
);
403 template <typename A
>
404 bool File
<A
>::loadMember(MemberState
& state
, ld::File::AtomHandler
& handler
, const char *format
, ...) const
406 bool didSomething
= false;
408 if ( _verboseLoad
&& !state
.logged
) {
410 va_start(list
, format
);
411 vprintf(format
, list
);
416 didSomething
= state
.file
->forEachAtom(handler
);
422 template <typename A
>
423 bool File
<A
>::forEachAtom(ld::File::AtomHandler
& handler
) const
425 bool didSome
= false;
426 if ( _forceLoadAll
|| _forceLoadThis
) {
427 // call handler on all .o files in this archive
428 const Entry
* const start
= (Entry
*)&_archiveFileContent
[8];
429 const Entry
* const end
= (Entry
*)&_archiveFileContent
[_archiveFilelength
];
430 for (const Entry
* p
=start
; p
< end
; p
= p
->next()) {
431 char memberName
[256];
432 p
->getName(memberName
, sizeof(memberName
));
433 if ( (p
==start
) && ((strcmp(memberName
, SYMDEF_SORTED
) == 0) || (strcmp(memberName
, SYMDEF
) == 0)) )
435 MemberState
& state
= this->makeObjectFileForMember(p
);
436 didSome
|= loadMember(state
, handler
, "%s forced load of %s(%s)\n", _forceLoadThis
? "-force_load" : "-all_load", this->path(), memberName
);
439 else if ( _forceLoadObjC
) {
440 // call handler on all .o files in this archive containing objc classes
441 for(typename
NameToEntryMap::const_iterator it
= _hashTable
.begin(); it
!= _hashTable
.end(); ++it
) {
442 if ( (strncmp(it
->first
, ".objc_c", 7) == 0) || (strncmp(it
->first
, "_OBJC_CLASS_$_", 14) == 0) ) {
443 const Entry
* member
= (Entry
*)&_archiveFileContent
[E::get32(it
->second
->ran_off
)];
444 MemberState
& state
= this->makeObjectFileForMember(member
);
445 char memberName
[256];
446 member
->getName(memberName
, sizeof(memberName
));
447 didSome
|= loadMember(state
, handler
, "-ObjC forced load of %s(%s)\n", this->path(), memberName
);
450 // ObjC2 has no symbols in .o files with categories but not classes, look deeper for those
451 const Entry
* const start
= (Entry
*)&_archiveFileContent
[8];
452 const Entry
* const end
= (Entry
*)&_archiveFileContent
[_archiveFilelength
];
453 for (const Entry
* member
=start
; member
< end
; member
= member
->next()) {
455 member
->getName(mname
, sizeof(mname
));
456 // skip table-of-content member
457 if ( (member
==start
) && ((strcmp(mname
, SYMDEF_SORTED
) == 0) || (strcmp(mname
, SYMDEF
) == 0)) )
459 MemberState
& state
= this->makeObjectFileForMember(member
);
460 // only look at files not already loaded
461 if ( ! state
.loaded
) {
462 if ( this->memberHasObjCCategories(member
) ) {
463 MemberState
& state
= this->makeObjectFileForMember(member
);
464 char memberName
[256];
465 member
->getName(memberName
, sizeof(memberName
));
466 didSome
|= loadMember(state
, handler
, "-ObjC forced load of %s(%s)\n", this->path(), memberName
);
474 template <typename A
>
475 bool File
<A
>::justInTimeforEachAtom(const char* name
, ld::File::AtomHandler
& handler
) const
477 // in force load case, all members already loaded
478 if ( _forceLoadAll
|| _forceLoadThis
)
481 // do a hash search of table of contents looking for requested symbol
482 const struct ranlib
* result
= ranlibHashSearch(name
);
483 if ( result
!= NULL
) {
484 const Entry
* member
= (Entry
*)&_archiveFileContent
[E::get32(result
->ran_off
)];
485 MemberState
& state
= this->makeObjectFileForMember(member
);
486 char memberName
[256];
487 member
->getName(memberName
, sizeof(memberName
));
488 return loadMember(state
, handler
, "%s forced load of %s(%s)\n", name
, this->path(), memberName
);
490 //fprintf(stderr, "%s NOT found in archive %s\n", name, this->path());
494 class CheckIsDataSymbolHandler
: public ld::File::AtomHandler
497 CheckIsDataSymbolHandler(const char* n
) : _name(n
), _isData(false) {}
498 virtual void doAtom(const class ld::Atom
& atom
) {
499 if ( strcmp(atom
.name(), _name
) == 0 ) {
500 if ( atom
.section().type() != ld::Section::typeCode
)
504 virtual void doFile(const class ld::File
&) {}
505 bool symbolIsDataDefinition() { return _isData
; }
513 template <typename A
>
514 bool File
<A
>::justInTimeDataOnlyforEachAtom(const char* name
, ld::File::AtomHandler
& handler
) const
516 // in force load case, all members already loaded
517 if ( _forceLoadAll
|| _forceLoadThis
)
520 // do a hash search of table of contents looking for requested symbol
521 const struct ranlib
* result
= ranlibHashSearch(name
);
522 if ( result
!= NULL
) {
523 const Entry
* member
= (Entry
*)&_archiveFileContent
[E::get32(result
->ran_off
)];
524 MemberState
& state
= this->makeObjectFileForMember(member
);
525 // only call handler for each member once
526 if ( ! state
.loaded
) {
527 CheckIsDataSymbolHandler
checker(name
);
528 state
.file
->forEachAtom(checker
);
529 if ( checker
.symbolIsDataDefinition() ) {
530 char memberName
[256];
531 member
->getName(memberName
, sizeof(memberName
));
532 return loadMember(state
, handler
, "%s forced load of %s(%s)\n", name
, this->path(), memberName
);
536 //fprintf(stderr, "%s NOT found in archive %s\n", name, this->path());
541 typedef const struct ranlib
* ConstRanLibPtr
;
543 template <typename A
>
544 ConstRanLibPtr File
<A
>::ranlibHashSearch(const char* name
) const
546 typename
NameToEntryMap::const_iterator pos
= _hashTable
.find(name
);
547 if ( pos
!= _hashTable
.end() )
553 template <typename A
>
554 void File
<A
>::buildHashTable()
556 // walk through list backwards, adding/overwriting entries
557 // this assures that with duplicates those earliest in the list will be found
558 for (int i
= _tableOfContentCount
-1; i
>= 0; --i
) {
559 const struct ranlib
* entry
= &_tableOfContents
[i
];
560 const char* entryName
= &_tableOfContentStrings
[E::get32(entry
->ran_un
.ran_strx
)];
561 if ( E::get32(entry
->ran_off
) > _archiveFilelength
) {
562 throwf("malformed archive TOC entry for %s, offset %d is beyond end of file %lld\n",
563 entryName
, entry
->ran_off
, _archiveFilelength
);
566 //const Entry* member = (Entry*)&_archiveFileContent[E::get32(entry->ran_off)];
567 //fprintf(stderr, "adding hash %d, %s -> %p\n", i, entryName, entry);
568 _hashTable
[entryName
] = entry
;
572 template <typename A
>
573 void File
<A
>::dumpTableOfContents()
575 for (unsigned int i
=0; i
< _tableOfContentCount
; ++i
) {
576 const struct ranlib
* e
= &_tableOfContents
[i
];
577 printf("%s in %s\n", &_tableOfContentStrings
[E::get32(e
->ran_un
.ran_strx
)], ((Entry
*)&_archiveFileContent
[E::get32(e
->ran_off
)])->name());
583 // main function used by linker to instantiate archive files
585 ld::archive::File
* parse(const uint8_t* fileContent
, uint64_t fileLength
,
586 const char* path
, time_t modTime
, ld::File::Ordinal ordinal
, const ParserOptions
& opts
)
588 switch ( opts
.objOpts
.architecture
) {
589 #if SUPPORT_ARCH_x86_64
590 case CPU_TYPE_X86_64
:
591 if ( archive::Parser
<x86_64
>::validFile(fileContent
, fileLength
, opts
.objOpts
) )
592 return archive::Parser
<x86_64
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
595 #if SUPPORT_ARCH_i386
597 if ( archive::Parser
<x86
>::validFile(fileContent
, fileLength
, opts
.objOpts
) )
598 return archive::Parser
<x86
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
601 #if SUPPORT_ARCH_arm_any
603 if ( archive::Parser
<arm
>::validFile(fileContent
, fileLength
, opts
.objOpts
) )
604 return archive::Parser
<arm
>::parse(fileContent
, fileLength
, path
, modTime
, ordinal
, opts
);
613 }; // namespace archive