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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | /* * Copyright (c) 2017 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 <string.h> #include <stdio.h> #include <stdint.h> #include <_simple.h> #include <sys/errno.h> #include <sys/mman.h> #include <sys/stat.h> #include <dirent.h> #include <fcntl.h> #include <TargetConditionals.h> #include <malloc/malloc.h> #include <algorithm> #include "dlfcn.h" #include "dyld_priv.h" #include "AllImages.h" #include "Loading.h" #include "Logging.h" #include "Diagnostics.h" #include "DyldSharedCache.h" #include "APIs.h" namespace dyld3 { // from APIs.cpp void parseDlHandle(void* h, const MachOLoaded** mh, bool* dontContinue); // only in macOS and deprecated #if __MAC_OS_X_VERSION_MIN_REQUIRED // macOS needs to support an old API that only works with fileype==MH_BUNDLE. // In this deprecated API (unlike dlopen), loading and linking are separate steps. // NSCreateObjectFileImageFrom*() just maps in the bundle mach-o file. // NSLinkModule() does the load of dependent modules and rebasing/binding. // To unload one of these, you must call NSUnLinkModule() and NSDestroyObjectFileImage() in any order! // NSObjectFileImageReturnCode NSCreateObjectFileImageFromFile(const char* path, NSObjectFileImage* ofi) { log_apis("NSCreateObjectFileImageFromFile(\"%s\", %p)\n", path, ofi); // verify path exists struct stat statbuf; if ( ::stat(path, &statbuf) == -1 ) return NSObjectFileImageFailure; // create ofi that just contains path. NSLinkModule does all the work OFIInfo result; result.path = strdup(path); result.memSource = nullptr; result.memLength = 0; result.loadAddress = nullptr; result.imageNum = 0; *ofi = gAllImages.addNSObjectFileImage(result); log_apis("NSCreateObjectFileImageFromFile() => %p\n", *ofi); return NSObjectFileImageSuccess; } NSObjectFileImageReturnCode NSCreateObjectFileImageFromMemory(const void* memImage, size_t memImageSize, NSObjectFileImage *ofi) { log_apis("NSCreateObjectFileImageFromMemory(%p, 0x%0lX, %p)\n", memImage, memImageSize, ofi); // sanity check the buffer is a mach-o file __block Diagnostics diag; // check if it is current arch mach-o or fat with slice for current arch bool usable = false; const MachOFile* mf = (MachOFile*)memImage; if ( mf->hasMachOMagic() && mf->isMachO(diag, memImageSize) ) { if ( strcmp(mf->archName(), MachOFile::currentArchName()) == 0 ) usable = true; #if __x86_64__ // <rdar://problem/42727628> support thin x86_64 on haswell machines else if ( (strcmp(MachOFile::currentArchName(), "x86_64h") == 0) && (strcmp(mf->archName(), "x86_64") == 0) ) usable = true; #endif } else if ( const FatFile* ff = FatFile::isFatFile(memImage) ) { uint64_t sliceOffset; uint64_t sliceLen; bool missingSlice; if ( ff->isFatFileWithSlice(diag, memImageSize, MachOFile::currentArchName(), sliceOffset, sliceLen, missingSlice) ) { mf = (MachOFile*)((long)memImage+sliceOffset); if ( mf->isMachO(diag, sliceLen) ) { usable = true; } } } if ( usable ) { if ( !mf->supportsPlatform(Platform::macOS) ) usable = false; } if ( !usable ) { log_apis("NSCreateObjectFileImageFromMemory() not mach-o\n"); return NSObjectFileImageFailure; } // this API can only be used with bundles if ( !mf->isBundle() ) { log_apis("NSCreateObjectFileImageFromMemory() not a bundle\n"); return NSObjectFileImageInappropriateFile; } // allocate ofi that just lists the memory range OFIInfo result; result.path = nullptr; result.memSource = memImage; result.memLength = memImageSize; result.loadAddress = nullptr; result.imageNum = 0; *ofi = gAllImages.addNSObjectFileImage(result); log_apis("NSCreateObjectFileImageFromMemory() => %p\n", *ofi); return NSObjectFileImageSuccess; } NSModule NSLinkModule(NSObjectFileImage ofi, const char* moduleName, uint32_t options) { DYLD_LOAD_LOCK_THIS_BLOCK log_apis("NSLinkModule(%p, \"%s\", 0x%08X)\n", ofi, moduleName, options); __block const char* path = nullptr; bool foundImage = gAllImages.forNSObjectFileImage(ofi, ^(OFIInfo &image) { // if this is memory based image, write to temp file, then use file based loading if ( image.memSource != nullptr ) { // make temp file with content of memory buffer bool successfullyWritten = false; image.path = ::tempnam(nullptr, "NSCreateObjectFileImageFromMemory-"); if ( image.path != nullptr ) { int fd = ::open(image.path, O_WRONLY | O_CREAT | O_EXCL, 0644); if ( fd != -1 ) { ssize_t writtenSize = ::pwrite(fd, image.memSource, image.memLength, 0); if ( writtenSize == image.memLength ) successfullyWritten = true; ::close(fd); } } if ( !successfullyWritten ) { if ( image.path != nullptr ) { free((void*)image.path); image.path = nullptr; } log_apis("NSLinkModule() => NULL (could not save memory image to temp file)\n"); return; } } path = image.path; }); if (!foundImage) { // ofi is invalid if not in list log_apis("NSLinkModule() => NULL (invalid NSObjectFileImage)\n"); return nullptr; } if (!path) return nullptr; // dlopen the binary outside of the read lock as we don't want to risk deadlock Diagnostics diag; void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue const MachOLoaded* loadAddress = gAllImages.dlopen(diag, path, false, false, false, true, callerAddress); if ( diag.hasError() ) { log_apis(" NSLinkModule: failed: %s\n", diag.errorMessage()); return nullptr; } // Now update the load address of this object gAllImages.forNSObjectFileImage(ofi, ^(OFIInfo &image) { image.loadAddress = loadAddress; // if memory based load, delete temp file if ( image.memSource != nullptr ) { log_apis(" NSLinkModule: delete temp file: %s\n", image.path); ::unlink(image.path); } }); log_apis("NSLinkModule() => %p\n", loadAddress); return (NSModule)loadAddress; } // NSUnLinkModule unmaps the image, but does not release the NSObjectFileImage bool NSUnLinkModule(NSModule module, uint32_t options) { DYLD_LOAD_LOCK_THIS_BLOCK log_apis("NSUnLinkModule(%p, 0x%08X)\n", module, options); __block const mach_header* mh = nullptr; gAllImages.infoForImageMappedAt(module, ^(const LoadedImage& foundImage, uint8_t permissions) { mh = foundImage.loadedAddress(); }); if ( mh != nullptr ) gAllImages.decRefCount(mh); // removes image if reference count went to zero log_apis("NSUnLinkModule() => %d\n", mh != nullptr); return mh != nullptr; } // NSDestroyObjectFileImage releases the NSObjectFileImage, but the mapped image may remain in use bool NSDestroyObjectFileImage(NSObjectFileImage imageHandle) { log_apis("NSDestroyObjectFileImage(%p)\n", imageHandle); __block const void* memSource = nullptr; __block size_t memLength = 0; __block const char* path = nullptr; bool foundImage = gAllImages.forNSObjectFileImage(imageHandle, ^(OFIInfo &image) { // keep copy of info memSource = image.memSource; memLength = image.memLength; path = image.path; }); if (!foundImage) return false; // remove from list gAllImages.removeNSObjectFileImage(imageHandle); // if object was created from a memory, release that memory // NOTE: this is the way dyld has always done this. NSCreateObjectFileImageFromMemory() hands ownership of the memory to dyld if ( memSource != nullptr ) { // we don't know if memory came from malloc or vm_allocate, so ask malloc if ( malloc_size(memSource) != 0 ) free((void*)(memSource)); else vm_deallocate(mach_task_self(), (vm_address_t)memSource, memLength); } free((void*)path); return true; } uint32_t NSSymbolDefinitionCountInObjectFileImage(NSObjectFileImage objectFileImage) { halt("NSSymbolDefinitionCountInObjectFileImage() is obsolete"); } const char* NSSymbolDefinitionNameInObjectFileImage(NSObjectFileImage objectFileImage, uint32_t ordinal) { halt("NSSymbolDefinitionNameInObjectFileImage() is obsolete"); } uint32_t NSSymbolReferenceCountInObjectFileImage(NSObjectFileImage objectFileImage) { halt("NSSymbolReferenceCountInObjectFileImage() is obsolete"); } const char* NSSymbolReferenceNameInObjectFileImage(NSObjectFileImage objectFileImage, uint32_t ordinal, bool *tentative_definition) { halt("NSSymbolReferenceNameInObjectFileImage() is obsolete"); } bool NSIsSymbolDefinedInObjectFileImage(NSObjectFileImage imageHandle, const char* symbolName) { log_apis("NSIsSymbolDefinedInObjectFileImage(%p, %s)\n", imageHandle, symbolName); __block bool hasSymbol = false; bool foundImage = gAllImages.forNSObjectFileImage(imageHandle, ^(OFIInfo &image) { void* addr; bool resultPointsToInstructions = false; hasSymbol = image.loadAddress->hasExportedSymbol(symbolName, nullptr, &addr, &resultPointsToInstructions); }); // ofi is invalid if not in list if (!foundImage) return false; return hasSymbol; } void* NSGetSectionDataInObjectFileImage(NSObjectFileImage imageHandle, const char* segmentName, const char* sectionName, size_t* size) { __block const void* result = nullptr; bool foundImage = gAllImages.forNSObjectFileImage(imageHandle, ^(OFIInfo &image) { uint64_t sz; result = image.loadAddress->findSectionContent(segmentName, sectionName, sz); *size = (size_t)sz; }); // ofi is invalid if not in list if (!foundImage) return nullptr; return (void*)result; } const char* NSNameOfModule(NSModule m) { log_apis("NSNameOfModule(%p)\n", m); __block const char* result = nullptr; gAllImages.infoForImageMappedAt(m, ^(const LoadedImage& foundImage, uint8_t permissions) { result = gAllImages.imagePath(foundImage.image()); }); return result; } const char* NSLibraryNameForModule(NSModule m) { log_apis("NSLibraryNameForModule(%p)\n", m); __block const char* result = nullptr; gAllImages.infoForImageMappedAt(m, ^(const LoadedImage& foundImage, uint8_t permissions) { result = gAllImages.imagePath(foundImage.image()); }); return result; } static bool flatFindSymbol(const char* symbolName, void** symbolAddress, const mach_header** foundInImageAtLoadAddress) { __block bool result = false; gAllImages.forEachImage(^(const LoadedImage& loadedImage, bool& stop) { bool resultPointsToInstructions = false; if ( loadedImage.loadedAddress()->hasExportedSymbol(symbolName, nullptr, symbolAddress, &resultPointsToInstructions) ) { *foundInImageAtLoadAddress = loadedImage.loadedAddress(); stop = true; result = true; } }); return result; } bool NSIsSymbolNameDefined(const char* symbolName) { log_apis("NSIsSymbolNameDefined(%s)\n", symbolName); const mach_header* foundInImageAtLoadAddress; void* address; return flatFindSymbol(symbolName, &address, &foundInImageAtLoadAddress); } bool NSIsSymbolNameDefinedWithHint(const char* symbolName, const char* libraryNameHint) { log_apis("NSIsSymbolNameDefinedWithHint(%s, %s)\n", symbolName, libraryNameHint); const mach_header* foundInImageAtLoadAddress; void* address; return flatFindSymbol(symbolName, &address, &foundInImageAtLoadAddress); } bool NSIsSymbolNameDefinedInImage(const struct mach_header* mh, const char* symbolName) { log_apis("NSIsSymbolNameDefinedInImage(%p, %s)\n", mh, symbolName); void* addr; bool resultPointsToInstructions = false; return ((MachOLoaded*)mh)->hasExportedSymbol(symbolName, nullptr, &addr, &resultPointsToInstructions); } NSSymbol NSLookupAndBindSymbol(const char* symbolName) { log_apis("NSLookupAndBindSymbol(%s)\n", symbolName); const mach_header* foundInImageAtLoadAddress; void* symbolAddress; if ( flatFindSymbol(symbolName, &symbolAddress, &foundInImageAtLoadAddress) ) { return (NSSymbol)symbolAddress; } return nullptr; } NSSymbol NSLookupAndBindSymbolWithHint(const char* symbolName, const char* libraryNameHint) { log_apis("NSLookupAndBindSymbolWithHint(%s, %s)\n", symbolName, libraryNameHint); const mach_header* foundInImageAtLoadAddress; void* symbolAddress; if ( flatFindSymbol(symbolName, &symbolAddress, &foundInImageAtLoadAddress) ) { return (NSSymbol)symbolAddress; } return nullptr; } NSSymbol NSLookupSymbolInModule(NSModule module, const char* symbolName) { log_apis("NSLookupSymbolInModule(%p. %s)\n", module, symbolName); const MachOLoaded* mh = (const MachOLoaded*)module; void* addr; bool resultPointsToInstructions = false; if ( mh->hasExportedSymbol(symbolName, nullptr, &addr, &resultPointsToInstructions) ) { return (NSSymbol)addr; } return nullptr; } NSSymbol NSLookupSymbolInImage(const mach_header* mh, const char* symbolName, uint32_t options) { log_apis("NSLookupSymbolInImage(%p, \"%s\", 0x%08X)\n", mh, symbolName, options); void* addr; bool resultPointsToInstructions = false; if ( ((MachOLoaded*)mh)->hasExportedSymbol(symbolName, nullptr, &addr, &resultPointsToInstructions) ) { log_apis(" NSLookupSymbolInImage() => %p\n", addr); return (NSSymbol)addr; } if ( options & NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR ) { log_apis(" NSLookupSymbolInImage() => NULL\n"); return nullptr; } // FIXME: abort(); return nullptr; } const char* NSNameOfSymbol(NSSymbol symbol) { halt("NSNameOfSymbol() is obsolete"); } void* NSAddressOfSymbol(NSSymbol symbol) { log_apis("NSAddressOfSymbol(%p)\n", symbol); // in dyld 1.0, NSSymbol was a pointer to the nlist entry in the symbol table return (void*)symbol; } NSModule NSModuleForSymbol(NSSymbol symbol) { log_apis("NSModuleForSymbol(%p)\n", symbol); __block NSModule result = nullptr; gAllImages.infoForImageMappedAt(symbol, ^(const LoadedImage& foundImage, uint8_t permissions) { result = (NSModule)foundImage.loadedAddress(); }); return result; } void NSLinkEditError(NSLinkEditErrors *c, int *errorNumber, const char** fileName, const char** errorString) { log_apis("NSLinkEditError(%p, %p, %p, %p)\n", c, errorNumber, fileName, errorString); *c = NSLinkEditOtherError; *errorNumber = 0; *fileName = NULL; *errorString = NULL; } bool NSAddLibrary(const char* pathName) { log_apis("NSAddLibrary(%s)\n", pathName); void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue return ( dlopen_internal(pathName, 0, callerAddress) != nullptr); } bool NSAddLibraryWithSearching(const char* pathName) { log_apis("NSAddLibraryWithSearching(%s)\n", pathName); void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue return ( dlopen_internal(pathName, 0, callerAddress) != nullptr); } const mach_header* NSAddImage(const char* imageName, uint32_t options) { log_apis("NSAddImage(\"%s\", 0x%08X)\n", imageName, options); // Note: this is a quick and dirty implementation that just uses dlopen() and ignores some option flags uint32_t dloptions = 0; if ( (options & NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED) != 0 ) dloptions |= RTLD_NOLOAD; void* callerAddress = __builtin_return_address(1); // note layers: 1: real client, 0: libSystem glue void* h = dlopen_internal(imageName, dloptions, callerAddress); if ( h != nullptr ) { const MachOLoaded* mh; bool dontContinue; parseDlHandle(h, &mh, &dontContinue); return mh; } if ( (options & (NSADDIMAGE_OPTION_RETURN_ON_ERROR|NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED)) == 0 ) { halt("NSAddImage() image not found"); } return nullptr; } void NSInstallLinkEditErrorHandlers(const NSLinkEditErrorHandlers *handlers) { halt("NSInstallLinkEditErrorHandlers() is obsolete"); } bool _dyld_present(void) { log_apis("_dyld_present()\n"); return true; } bool _dyld_launched_prebound(void) { halt("_dyld_launched_prebound() is obsolete"); } bool _dyld_all_twolevel_modules_prebound(void) { halt("_dyld_all_twolevel_modules_prebound() is obsolete"); } bool _dyld_bind_fully_image_containing_address(const void* address) { log_apis("_dyld_bind_fully_image_containing_address(%p)\n", address); // in dyld3, everything is always fully bound return true; } bool _dyld_image_containing_address(const void* address) { log_apis("_dyld_image_containing_address(%p)\n", address); return (dyld_image_header_containing_address(address) != nullptr); } void _dyld_lookup_and_bind(const char* symbolName, void **address, NSModule* module) { log_apis("_dyld_lookup_and_bind(%s, %p, %p)\n", symbolName, address, module); const mach_header* foundInImageAtLoadAddress; if ( flatFindSymbol(symbolName, address, &foundInImageAtLoadAddress) ) { *module = (NSModule)foundInImageAtLoadAddress; return; } *address = 0; *module = 0; } void _dyld_lookup_and_bind_with_hint(const char* symbolName, const char* libraryNameHint, void** address, NSModule* module) { log_apis("_dyld_lookup_and_bind_with_hint(%s, %s, %p, %p)\n", symbolName, libraryNameHint, address, module); const mach_header* foundInImageAtLoadAddress; if ( flatFindSymbol(symbolName, address, &foundInImageAtLoadAddress) ) { *module = (NSModule)foundInImageAtLoadAddress; return; } *address = 0; *module = 0; } void _dyld_lookup_and_bind_fully(const char* symbolName, void** address, NSModule* module) { log_apis("_dyld_lookup_and_bind_fully(%s, %p, %p)\n", symbolName, address, module); const mach_header* foundInImageAtLoadAddress; if ( flatFindSymbol(symbolName, address, &foundInImageAtLoadAddress) ) { *module = (NSModule)foundInImageAtLoadAddress; return; } *address = 0; *module = 0; } const struct mach_header* _dyld_get_image_header_containing_address(const void* address) { log_apis("_dyld_get_image_header_containing_address(%p)\n", address); return dyld_image_header_containing_address(address); } #endif } // namespace dyld3 |