1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 * Copyright (c) 2009 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@
25 #ifndef __SYMBOL_TABLE_H__
26 #define __SYMBOL_TABLE_H__
29 #include <sys/types.h>
32 #include <sys/sysctl.h>
37 #include <mach/mach_time.h>
38 #include <mach/vm_statistics.h>
39 #include <mach/mach_init.h>
40 #include <mach/mach_host.h>
42 #include <mach-o/dyld.h>
45 #include <ext/hash_map>
54 class SymbolTable
: public ld::IndirectBindingTable
57 typedef uint32_t IndirectBindingSlot
;
62 bool operator()(const char* left
, const char* right
) const { return (strcmp(left
, right
) == 0); }
64 typedef __gnu_cxx::hash_map
<const char*, IndirectBindingSlot
, __gnu_cxx::hash
<const char*>, CStringEquals
> NameToSlot
;
68 size_t operator()(const ld::Atom
*) const;
69 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
) const;
71 typedef __gnu_cxx::hash_map
<const ld::Atom
*, IndirectBindingSlot
, ContentFuncs
, ContentFuncs
> ContentToSlot
;
73 class ReferencesHashFuncs
{
75 size_t operator()(const ld::Atom
*) const;
76 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
) const;
78 typedef __gnu_cxx::hash_map
<const ld::Atom
*, IndirectBindingSlot
, ReferencesHashFuncs
, ReferencesHashFuncs
> ReferencesToSlot
;
80 class CStringHashFuncs
{
82 size_t operator()(const ld::Atom
*) const;
83 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
) const;
85 typedef __gnu_cxx::hash_map
<const ld::Atom
*, IndirectBindingSlot
, CStringHashFuncs
, CStringHashFuncs
> CStringToSlot
;
87 class UTF16StringHashFuncs
{
89 size_t operator()(const ld::Atom
*) const;
90 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
) const;
92 typedef __gnu_cxx::hash_map
<const ld::Atom
*, IndirectBindingSlot
, UTF16StringHashFuncs
, UTF16StringHashFuncs
> UTF16StringToSlot
;
94 typedef std::map
<IndirectBindingSlot
, const char*> SlotToName
;
95 typedef __gnu_cxx::hash_map
<const char*, CStringToSlot
*, __gnu_cxx::hash
<const char*>, CStringEquals
> NameToMap
;
99 class byNameIterator
{
101 byNameIterator
& operator++(int) { ++_nameTableIterator
; return *this; }
102 const ld::Atom
* operator*() { return _slotTable
[_nameTableIterator
->second
]; }
103 bool operator!=(const byNameIterator
& lhs
) { return _nameTableIterator
!= lhs
._nameTableIterator
; }
106 friend class SymbolTable
;
107 byNameIterator(NameToSlot::iterator it
, std::vector
<const ld::Atom
*>& indirectTable
)
108 : _nameTableIterator(it
), _slotTable(indirectTable
) {}
110 NameToSlot::iterator _nameTableIterator
;
111 std::vector
<const ld::Atom
*>& _slotTable
;
114 SymbolTable(const Options
& opts
, std::vector
<const ld::Atom
*>& ibt
);
116 bool add(const ld::Atom
& atom
, bool ignoreDuplicates
);
117 IndirectBindingSlot
findSlotForName(const char* name
);
118 IndirectBindingSlot
findSlotForContent(const ld::Atom
* atom
, const ld::Atom
** existingAtom
);
119 IndirectBindingSlot
findSlotForReferences(const ld::Atom
* atom
, const ld::Atom
** existingAtom
);
120 const ld::Atom
* atomForSlot(IndirectBindingSlot s
) { return _indirectBindingTable
[s
]; }
121 unsigned int updateCount() { return _indirectBindingTable
.size(); }
122 void undefines(std::vector
<const char*>& undefines
);
123 void tentativeDefs(std::vector
<const char*>& undefines
);
124 bool hasName(const char* name
);
125 bool hasExternalTentativeDefinitions() { return _hasExternalTentativeDefinitions
; }
126 byNameIterator
begin() { return byNameIterator(_byNameTable
.begin(),_indirectBindingTable
); }
127 byNameIterator
end() { return byNameIterator(_byNameTable
.end(),_indirectBindingTable
); }
128 void printStatistics();
129 static const char* demangle(const char* sym
);
131 // from ld::IndirectBindingTable
132 virtual const char* indirectName(IndirectBindingSlot slot
) const;
133 virtual const ld::Atom
* indirectAtom(IndirectBindingSlot slot
) const;
136 bool addByName(const ld::Atom
& atom
, bool ignoreDuplicates
);
137 bool addByContent(const ld::Atom
& atom
);
138 bool addByReferences(const ld::Atom
& atom
);
139 void markCoalescedAway(const ld::Atom
* atom
);
141 const Options
& _options
;
142 NameToSlot _byNameTable
;
143 SlotToName _byNameReverseTable
;
144 ContentToSlot _literal4Table
;
145 ContentToSlot _literal8Table
;
146 ContentToSlot _literal16Table
;
147 UTF16StringToSlot _utf16Table
;
148 CStringToSlot _cstringTable
;
149 NameToMap _nonStdCStringSectionToMap
;
150 ReferencesToSlot _nonLazyPointerTable
;
151 ReferencesToSlot _cfStringTable
;
152 ReferencesToSlot _objc2ClassRefTable
;
153 ReferencesToSlot _pointerToCStringTable
;
154 std::vector
<const ld::Atom
*>& _indirectBindingTable
;
155 bool _hasExternalTentativeDefinitions
;
157 static bool _s_doDemangle
;
165 #endif // __SYMBOL_TABLE_H__