1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008-2010 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@
24 #ifndef __MACH_O_TRIE__
25 #define __MACH_O_TRIE__
29 #include "MachOFileAbstraction.hpp"
37 Edge(const char* s, struct Node* n) : fSubString(s), fChild(n) { }
39 const char* fSubString;
46 Node(const char* s) : fCummulativeString(s), fAddress(0), fFlags(0),
47 fOther(0), fImportedName(NULL), fOrdered(false),
48 fHaveExportInfo(false), fTrieOffset(0) {}
50 const char* fCummulativeString;
51 std::vector<Edge> fChildren;
55 const char* fImportedName;
60 void addSymbol(const char* fullStr, uint64_t address, uint64_t flags, uint64_t other, const char* importName) {
61 const char* partialStr = &fullStr[strlen(fCummulativeString)];
62 for (std::vector<Edge>::iterator it = fChildren.begin(); it != fChildren.end(); ++it) {
64 int subStringLen = strlen(e.fSubString);
65 if ( strncmp(e.fSubString, partialStr, subStringLen) == 0 ) {
66 // already have matching edge, go down that path
67 e.fChild->addSymbol(fullStr, address, flags, other, importName);
71 for (int i=subStringLen-1; i > 0; --i) {
72 if ( strncmp(e.fSubString, partialStr, i) == 0 ) {
73 // found a common substring, splice in new node
74 // was A -> C, now A -> B -> C
75 char* bNodeCummStr = strdup(e.fChild->fCummulativeString);
76 bNodeCummStr[strlen(bNodeCummStr)+i-subStringLen] = '\0';
78 Node* bNode = new Node(bNodeCummStr);
79 Node* cNode = e.fChild;
80 char* abEdgeStr = strdup(e.fSubString);
82 char* bcEdgeStr = strdup(&e.fSubString[i]);
84 abEdge.fSubString = abEdgeStr;
85 abEdge.fChild = bNode;
86 Edge bcEdge(bcEdgeStr, cNode);
87 bNode->fChildren.push_back(bcEdge);
88 bNode->addSymbol(fullStr, address, flags, other, importName);
95 // no commonality with any existing child, make a new edge that is this whole string
96 Node* newNode = new Node(strdup(fullStr));
97 Edge newEdge(strdup(partialStr), newNode);
98 fChildren.push_back(newEdge);
99 newNode->fAddress = address;
100 newNode->fFlags = flags;
101 newNode->fOther = other;
102 if ( (flags & EXPORT_SYMBOL_FLAGS_REEXPORT) && (importName != NULL) && (strcmp(fullStr,importName) != 0) )
103 newNode->fImportedName = importName;
105 newNode->fImportedName = NULL;
106 newNode->fHaveExportInfo = true;
109 void addOrderedNodes(const char* name, std::vector<Node*>& orderedNodes) {
111 orderedNodes.push_back(this);
112 //fprintf(stderr, "ordered %p %s\n", this, fCummulativeString);
115 const char* partialStr = &name[strlen(fCummulativeString)];
116 for (std::vector<Edge>::iterator it = fChildren.begin(); it != fChildren.end(); ++it) {
118 int subStringLen = strlen(e.fSubString);
119 if ( strncmp(e.fSubString, partialStr, subStringLen) == 0 ) {
120 // already have matching edge, go down that path
121 e.fChild->addOrderedNodes(name, orderedNodes);
127 // byte for terminal node size in bytes, or 0x00 if not terminal node
128 // teminal node (uleb128 flags, uleb128 addr [uleb128 other])
129 // byte for child node count
130 // each child: zero terminated substring, uleb128 node offset
131 bool updateOffset(uint32_t& offset) {
132 uint32_t nodeSize = 1; // length of export info when no export info
133 if ( fHaveExportInfo ) {
134 if ( fFlags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
135 nodeSize = uleb128_size(fFlags) + uleb128_size(fOther); // ordinal
136 if ( fImportedName != NULL )
137 nodeSize += strlen(fImportedName);
138 ++nodeSize; // trailing zero in imported name
141 nodeSize = uleb128_size(fFlags) + uleb128_size(fAddress);
142 if ( fFlags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
143 nodeSize += uleb128_size(fOther);
145 // do have export info, overall node size so far is uleb128 of export info + export info
146 nodeSize += uleb128_size(nodeSize);
149 ++nodeSize; // byte for count of chidren
150 for (std::vector<Edge>::iterator it = fChildren.begin(); it != fChildren.end(); ++it) {
152 nodeSize += strlen(e.fSubString) + 1 + uleb128_size(e.fChild->fTrieOffset);
154 bool result = (fTrieOffset != offset);
155 fTrieOffset = offset;
156 //fprintf(stderr, "updateOffset %p %05d %s\n", this, fTrieOffset, fCummulativeString);
158 // return true if fTrieOffset was changed
162 void appendToStream(std::vector<uint8_t>& out) {
163 if ( fHaveExportInfo ) {
164 if ( fFlags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
165 if ( fImportedName != NULL ) {
166 // nodes with re-export info: size, flags, ordinal, string
167 uint32_t nodeSize = uleb128_size(fFlags) + uleb128_size(fOther) + strlen(fImportedName) + 1;
168 out.push_back(nodeSize);
169 append_uleb128(fFlags, out);
170 append_uleb128(fOther, out);
171 append_string(fImportedName, out);
174 // nodes with re-export info: size, flags, ordinal, empty-string
175 uint32_t nodeSize = uleb128_size(fFlags) + uleb128_size(fOther) + 1;
176 out.push_back(nodeSize);
177 append_uleb128(fFlags, out);
178 append_uleb128(fOther, out);
182 else if ( fFlags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) {
183 // nodes with export info: size, flags, address, other
184 uint32_t nodeSize = uleb128_size(fFlags) + uleb128_size(fAddress) + uleb128_size(fOther);
185 out.push_back(nodeSize);
186 append_uleb128(fFlags, out);
187 append_uleb128(fAddress, out);
188 append_uleb128(fOther, out);
191 // nodes with export info: size, flags, address
192 uint32_t nodeSize = uleb128_size(fFlags) + uleb128_size(fAddress);
193 out.push_back(nodeSize);
194 append_uleb128(fFlags, out);
195 append_uleb128(fAddress, out);
199 // no export info uleb128 of zero is one byte of zero
202 // write number of children
203 out.push_back(fChildren.size());
205 for (std::vector<Edge>::iterator it = fChildren.begin(); it != fChildren.end(); ++it) {
207 append_string(e.fSubString, out);
208 append_uleb128(e.fChild->fTrieOffset, out);
213 static void append_uleb128(uint64_t value, std::vector<uint8_t>& out) {
222 } while( byte >= 0x80 );
225 static void append_string(const char* str, std::vector<uint8_t>& out) {
226 for (const char* s = str; *s != '\0'; ++s)
231 static unsigned int uleb128_size(uint64_t value) {
236 } while ( value != 0 );
243 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
248 throw "malformed uleb128 extends beyond trie";
250 uint64_t slice = *p & 0x7f;
252 if (bit >= 64 || slice << bit >> bit != slice)
253 throw "uleb128 too big for 64-bits";
255 result |= (slice << bit);
271 const char* importName;
276 inline void makeTrie(const std::vector<Entry>& entries, std::vector<uint8_t>& output)
278 Node start(strdup(""));
280 // make nodes for all exported symbols
281 for (std::vector<Entry>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
282 start.addSymbol(it->name, it->address, it->flags, it->other, it->importName);
285 // create vector of nodes
286 std::vector<Node*> orderedNodes;
287 orderedNodes.reserve(entries.size()*2);
288 for (std::vector<Entry>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
289 start.addOrderedNodes(it->name, orderedNodes);
292 // assign each node in the vector an offset in the trie stream, iterating until all uleb128 sizes have stabilized
297 for (std::vector<Node*>::iterator it = orderedNodes.begin(); it != orderedNodes.end(); ++it) {
298 if ( (*it)->updateOffset(offset) )
303 // create trie stream
304 for (std::vector<Node*>::iterator it = orderedNodes.begin(); it != orderedNodes.end(); ++it) {
305 (*it)->appendToStream(output);
309 struct EntryWithOffset
311 uintptr_t nodeOffset;
314 bool operator<(const EntryWithOffset& other) const { return ( nodeOffset < other.nodeOffset ); }
319 static inline void processExportNode(const uint8_t* const start, const uint8_t* p, const uint8_t* const end,
320 char* cummulativeString, int curStrOffset,
321 std::vector<EntryWithOffset>& output)
324 throw "malformed trie, node past end";
325 const uint8_t terminalSize = read_uleb128(p, end);
326 const uint8_t* children = p + terminalSize;
327 if ( terminalSize != 0 ) {
329 e.nodeOffset = p-start;
330 e.entry.name = strdup(cummulativeString);
331 e.entry.flags = read_uleb128(p, end);
332 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) {
334 e.entry.other = read_uleb128(p, end); // dylib ordinal
335 e.entry.importName = (char*)p;
338 e.entry.address = read_uleb128(p, end);
339 if ( e.entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER )
340 e.entry.other = read_uleb128(p, end);
343 e.entry.importName = NULL;
347 const uint8_t childrenCount = *children++;
348 const uint8_t* s = children;
349 for (uint8_t i=0; i < childrenCount; ++i) {
352 cummulativeString[curStrOffset+edgeStrLen] = *s++;
355 cummulativeString[curStrOffset+edgeStrLen] = *s++;
356 uint32_t childNodeOffet = read_uleb128(s, end);
357 processExportNode(start, start+childNodeOffet, end, cummulativeString, curStrOffset+edgeStrLen, output);
362 inline void parseTrie(const uint8_t* start, const uint8_t* end, std::vector<Entry>& output)
364 // empty trie has no entries
367 char cummulativeString[4000];
368 std::vector<EntryWithOffset> entries;
369 processExportNode(start, start, end, cummulativeString, 0, entries);
370 // to preserve tie layout order, sort by node offset
371 std::sort(entries.begin(), entries.end());
373 output.reserve(entries.size());
374 for (std::vector<EntryWithOffset>::iterator it=entries.begin(); it != entries.end(); ++it)
375 output.push_back(it->entry);
382 }; // namespace mach_o
385 #endif // __MACH_O_TRIE__