Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | /* * Copyright (c) 2021 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <climits> #include <sys/types.h> #include <assert.h> #include <limits.h> #include <stdlib.h> #include <string.h> #include <mach-o/loader.h> // mach_o #include "ExportsTrie.h" #include "Symbol.h" #include "Misc.h" #include "Header.h" namespace mach_o { // // MARK: --- GenericTrie methods --- // // construct from an already built trie GenericTrie::GenericTrie(const uint8_t* start, size_t size) : _trieStart(start), _trieEnd(start+size) { } uint32_t GenericTrie::entryCount() const { if ( _trieStart == _trieEnd ) return 0; __block uint32_t result = 0; bool stop = false; STACK_ALLOC_OVERFLOW_SAFE_ARRAY(char, cummulativeString, 4096); (void)this->recurseTrie(_trieStart, cummulativeString, 0, stop, ^(const char* name, std::span<const uint8_t> nodePayload, bool& innerStop) { ++result; }); return result; } void GenericTrie::forEachEntry(void (^callback)(const Entry& entry, bool& stop)) const { // ld64 will emit an empty export trie load command as a placeholder to show there are no exports. // In that case, don't start recursing as we'll immediately think we ran of the end of the buffer. if ( _trieStart == _trieEnd ) return; bool stop = false; STACK_ALLOC_OVERFLOW_SAFE_ARRAY(char, cummulativeString, 4096); (void)this->recurseTrie(_trieStart, cummulativeString, 0, stop, ^(const char* name, std::span<const uint8_t> nodePayload, bool& innerStop) { Entry entry { name, nodePayload }; callback(entry, innerStop); }); } Error GenericTrie::recurseTrie(const uint8_t* p, dyld3::OverflowSafeArray<char>& cummulativeString, int curStrOffset, bool& stop, void (^callback)(const char* name, std::span<const uint8_t> nodePayload, bool& stop)) const { if ( p >= _trieEnd ) { return Error("malformed trie, node past end"); } bool malformed; const uint64_t terminalSize = read_uleb128(p, _trieEnd, malformed); if ( malformed ) return Error("malformed uleb128"); const uint8_t* children = p + terminalSize; if ( children > _trieEnd ) return Error("malformed trie, terminalSize extends beyond trie data"); if ( terminalSize != 0 ) { if ( callback ) callback(cummulativeString.data(), std::span<const uint8_t>(p, p+terminalSize), stop); if ( stop ) return Error::none(); } const uint8_t childrenCount = *children++; const uint8_t* s = children; for ( uint8_t i = 0; (i < childrenCount) && !stop; ++i ) { int edgeStrLen = 0; while ( *s != '\0' ) { cummulativeString.resize(curStrOffset + edgeStrLen + 1); cummulativeString[curStrOffset + edgeStrLen] = *s++; ++edgeStrLen; if ( s > _trieEnd ) return Error("malformed trie node, child node name extends beyond trie data"); } cummulativeString.resize(curStrOffset + edgeStrLen + 1); cummulativeString[curStrOffset + edgeStrLen] = *s++; uint64_t childNodeOffset = read_uleb128(s, _trieEnd, malformed); if ( malformed ) return Error("malformed uleb128"); if ( childNodeOffset == 0 ) return Error("malformed trie, childNodeOffset==0"); if ( Error err = this->recurseTrie(_trieStart + childNodeOffset, cummulativeString, curStrOffset + edgeStrLen, stop, callback) ) return err; } return Error::none(); } #if 1 void GenericTrie::dump() const { fprintf(stderr, "trie terminal nodes:\n"); if ( _trieStart == _trieEnd ) return; STACK_ALLOC_OVERFLOW_SAFE_ARRAY(char, cummulativeString, 4096); bool stop = false; (void)this->recurseTrie(_trieStart, cummulativeString, 0, stop, ^(const char* name, std::span<const uint8_t> nodePayload, bool& trieStop) { fprintf(stderr, " 0x%04lX: ", (long)(nodePayload.data()-_trieStart - uleb128_size(nodePayload.size()))); for (size_t i=0; i < nodePayload.size(); ++i) fprintf(stderr, "0x%02X ", nodePayload[i]); fprintf(stderr, "%s\n", name); }); } #endif bool GenericTrie::hasEntry(const char* name, std::span<const uint8_t>& terminalPayload) const { STACK_ALLOC_OVERFLOW_SAFE_ARRAY(uint32_t, visitedNodeOffsets, 256); visitedNodeOffsets.push_back(0); const uint8_t* p = _trieStart; while ( p < _trieEnd ) { bool malformed; uint64_t terminalSize = *p++; if ( terminalSize > 127 ) { // except for re-export-with-rename, all terminal sizes fit in one byte --p; terminalSize = read_uleb128(p, _trieEnd, malformed); if ( malformed ) return false; } if ( (*name == '\0') && (terminalSize != 0) ) { terminalPayload = std::span<const uint8_t>(p, (size_t)terminalSize); return true; } const uint8_t* children = p + terminalSize; if ( children > _trieEnd ) { // diag.error("malformed trie node, terminalSize=0x%llX extends past end of trie\n", terminalSize); return false; } uint8_t childrenRemaining = *children++; p = children; uint64_t nodeOffset = 0; for ( ; childrenRemaining > 0; --childrenRemaining ) { const char* ss = name; bool wrongEdge = false; // scan whole edge to get to next edge // if edge is longer than target symbol name, don't read past end of symbol name char c = *p; while ( c != '\0' ) { if ( !wrongEdge ) { if ( c != *ss ) wrongEdge = true; ++ss; } ++p; c = *p; } if ( wrongEdge ) { // advance to next child ++p; // skip over zero terminator // skip over uleb128 until last byte is found while ( (*p & 0x80) != 0 ) ++p; ++p; // skip over last byte of uleb128 if ( p > _trieEnd ) { return false; } } else { // the symbol so far matches this edge (child) // so advance to the child's node ++p; nodeOffset = read_uleb128(p, _trieEnd, malformed); if ( malformed ) return false; if ( (nodeOffset == 0) || (&_trieStart[nodeOffset] > _trieEnd) ) return false; name = ss; break; } } if ( nodeOffset != 0 ) { if ( nodeOffset > (uint64_t)(_trieEnd - _trieStart) ) return false; // check for cycles for ( uint32_t visitedOffset : visitedNodeOffsets ) { if ( visitedOffset == nodeOffset ) return false; } visitedNodeOffsets.push_back((uint32_t)nodeOffset); p = &_trieStart[nodeOffset]; } else { p = _trieEnd; } } return false; } // // MARK: --- ExportsTrie methods --- // uint32_t ExportsTrie::symbolCount() const { return this->entryCount(); } bool ExportsTrie::hasExportedSymbol(const char* symbolName, Symbol& symbol) const { std::span<const uint8_t> terminalPayload; if ( this->hasEntry(symbolName, terminalPayload) ) { Entry entry = { symbolName, terminalPayload }; if ( terminalPayloadToSymbol(entry, symbol).noError() ) return true; } return false; } void ExportsTrie::forEachExportedSymbol(void (^callback)(const Symbol& symbol, bool& stop)) const { this->forEachEntry(^(const Entry& entry, bool& stop) { Symbol symbol; if ( terminalPayloadToSymbol(entry, symbol).noError() ) callback(symbol, stop); }); } Error ExportsTrie::valid(uint64_t baseAddr, uint64_t maxVmOffset) const { if ( _trieStart == _trieEnd ) return Error::none(); STACK_ALLOC_OVERFLOW_SAFE_ARRAY(char, cummulativeString, 4096); bool stop = false; __block Error contentErr; Error recurseErr = this->recurseTrie(_trieStart, cummulativeString, 0, stop, ^(const char* name, std::span<const uint8_t> nodePayload, bool& trieStop) { Entry entry { name, nodePayload }; Symbol symbol; if ( Error err = terminalPayloadToSymbol(entry, symbol) ) { contentErr = std::move(err); trieStop = true; return; } uint64_t absAddress; int libOrdinal; const char* importName; if ( !symbol.isAbsolute(absAddress) && !symbol.isReExport(libOrdinal, importName) ) { uint64_t vmOffset = symbol.implOffset(); if ( (int64_t)vmOffset < 0 ) // segments with address lower than __TEXT vmOffset = baseAddr - vmOffset; if ( vmOffset > maxVmOffset ) { contentErr = Error("vmOffset too large for %s", symbol.name().c_str()); trieStop = true; } } }); if ( recurseErr ) return recurseErr; return std::move(contentErr); } Error ExportsTrie::terminalPayloadToSymbol(const Entry& entry, Symbol& symInfo) const { bool malformed; const uint8_t* p = entry.terminalPayload.data(); const uint8_t* end = p+entry.terminalPayload.size(); uint64_t flags = read_uleb128(p, end, malformed); if ( malformed ) return Error("malformed uleb128"); if ( (flags >> 6) != 0 ) return Error("unknown exports flag bits"); uint8_t kind = (flags & EXPORT_SYMBOL_FLAGS_KIND_MASK); uint64_t value = read_uleb128(p, end, malformed); if ( malformed ) return Error("malformed uleb128"); if ( kind == EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE ) symInfo = Symbol::makeAbsolute(entry.name.data(), value, /* dont dead strip */ false, Symbol::Scope::global); else if ( kind == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL ) symInfo = Symbol::makeThreadLocalExport(entry.name.data(), value, 0, false, false, flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION); else if ( flags & EXPORT_SYMBOL_FLAGS_REEXPORT ) { const char* importName = entry.name.data(); if ( *p != '\0' ) { importName = (char*)p; while (*p != '\0') ++p; } ++p; symInfo = Symbol::makeReExport(entry.name.data(), (int)value, importName); if ( flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION ) symInfo.setWeakDef(); } else if ( flags & EXPORT_SYMBOL_FLAGS_FUNCTION_VARIANT ) { uint64_t tableIndex = read_uleb128(p, end, malformed); if ( malformed ) return Error("malformed uleb128"); symInfo = Symbol::makeFunctionVariantExport(entry.name.data(), 0, value, (uint32_t)tableIndex); } else if ( flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) { uint64_t funcOffset = read_uleb128(p, end, malformed); if ( malformed ) return Error("malformed uleb128"); symInfo = Symbol::makeDynamicResolver(entry.name.data(), 1, value, funcOffset); } else if ( flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION ) symInfo = Symbol::makeWeakDefExport(entry.name.data(), value, 0, false, false); else symInfo = Symbol::makeRegularExport(entry.name.data(), value, 0, false, false); return Error::none(); } // // MARK: --- DylibsPathTrie methods --- // bool DylibsPathTrie::entryToIndex(std::span<const uint8_t> payload, uint32_t& dylibIndex) const { const uint8_t* p = payload.data(); const uint8_t* end = p+payload.size(); bool malformed; uint64_t value = read_uleb128(p, end, malformed); if ( !malformed ) { dylibIndex = (uint32_t)value; return true; } return false; } bool DylibsPathTrie::hasPath(const char* path, uint32_t& dylibIndex) const { std::span<const uint8_t> terminalPayload; if ( this->hasEntry(path, terminalPayload) ) { if ( entryToIndex(terminalPayload, dylibIndex) ) return true; } return false; } void DylibsPathTrie::forEachDylibPath(void (^callback)(const DylibAndIndex& info, bool& stop)) const { this->forEachEntry(^(const Entry& entry, bool& stop) { DylibAndIndex info; info.path = entry.name; if ( entryToIndex(entry.terminalPayload, info.index) ) callback(info, stop); }); } } // namespace mach_o |