/* * This routine returns the highest address of the segments in the program (NOT * the shared libraries). It is intended to be used as a stop gap for programs * that make UNIX style assumptions about how memory is allocated. Typicly the * asumptions under which this is used is that memory is contiguously allocated * by the program's text and data from address 0 with no gaps. The value of * this differs from the value of &_end in a UNIX program in that this routine * returns the address of the end of the segment not the end of the last section * in that segment as would be the value of the symbol &_end. */ unsigned long get_end(void) { #ifndef __LP64__ struct segment_command *sgp; unsigned long _end; uint32_t i; #ifndef __OPENSTEP__ struct mach_header *mhp = _NSGetMachExecuteHeader(); #else /* defined(__OPENSTEP__) */ static struct mach_header *mhp = NULL; DECLARE_VAR(_mh_execute_header, struct mach_header); SETUP_VAR(_mh_execute_header); mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header)); #endif /* __OPENSTEP__ */ _end = 0; sgp = (struct segment_command *) ((char *)mhp + sizeof(struct mach_header)); for(i = 0; i < mhp->ncmds; i++){ if(sgp->cmd == LC_SEGMENT) if(sgp->vmaddr + sgp->vmsize > _end) _end = sgp->vmaddr + sgp->vmsize; sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return(_end); #else /* defined(__LP64__) */ struct mach_header_64 *mhp = _NSGetMachExecuteHeader(); struct segment_command_64 *sgp; unsigned long _end; uint32_t i; _end = 0; sgp = (struct segment_command_64 *) ((char *)mhp + sizeof(struct mach_header_64)); for(i = 0; i < mhp->ncmds; i++){ if(sgp->cmd == LC_SEGMENT_64) if(sgp->vmaddr + sgp->vmsize > _end) _end = sgp->vmaddr + sgp->vmsize; sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); } return(_end); #endif /* defined(__LP64__) */ }
const struct segment_command * getsegbyname( char *segname) { struct segment_command *sgp; uint32_t i; #ifndef RLD #ifndef __OPENSTEP__ struct mach_header *mhp = _NSGetMachExecuteHeader(); #else /* defined(__OPENSTEP__) */ static struct mach_header *mhp = NULL; DECLARE_VAR(_mh_execute_header, struct mach_header); SETUP_VAR(_mh_execute_header); mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header)); #endif /* __OPENSTEP__ */ #else /* defined(RLD) */ mhp = (struct mach_header *)(&_mh_execute_header); #endif /* defined(RLD) */ sgp = (struct segment_command *) ((char *)mhp + sizeof(struct mach_header)); for(i = 0; i < mhp->ncmds; i++){ if(sgp->cmd == LC_SEGMENT) if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) return(sgp); sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return(NULL); }
int main(int argc, const char* argv[]) { printf("[BEGIN] crt-vars-libSystem\n"); bool success = true; if ( _NSGetArgv() != &NXArgv ) { printf("[FAIL] crt-libSystem: _NSGetArgv() != &NXArgv (%p!=%p) for %s", _NSGetArgv(), &NXArgv, argv[0]); success = false; } if ( _NSGetArgc() != &NXArgc ) { printf("[FAIL] crt-libSystem: _NSGetArgc() != &NXArgc (%p!=%p) for %s", _NSGetArgc(), &NXArgc, argv[0]); success = false; } if ( _NSGetEnviron() != &environ ) { printf("[FAIL] crt-libSystem: _NSGetEnviron() != &environv (%p!=%p) for %s", _NSGetEnviron(), &environ, argv[0]); success = false; } if ( _NSGetProgname() != &__progname ) { printf("[FAIL] crt-libSystem: _NSGetProgname() != &__progname (%p!=%p) for %s", _NSGetProgname(), &__progname, argv[0]); success = false; } if ( _NSGetMachExecuteHeader() != &_mh_execute_header ) { printf("[FAIL] crt-libSystem: _NSGetMachExecuteHeader() != &_mh_execute_headerv (%p!=%p) for %s", _NSGetMachExecuteHeader(), &_mh_execute_header, argv[0]); success = false; } if ( sVars->NXArgvPtr != &NXArgv ) { printf("[FAIL] crt-libSystem: sVars->NXArgvPtr != &NXArg (%p!=%p) for %s", sVars->NXArgvPtr, &NXArgv, argv[0]); success = false; } if ( sVars->NXArgcPtr != &NXArgc ) { printf("[FAIL] crt-libSystem: sVars->NXArgcPtr != &NXArgc (%p!=%p) for %s", sVars->NXArgcPtr, &NXArgc, argv[0]); success = false; } if ( sVars->environPtr != &environ ) { printf("[FAIL] crt-libSystem: sVars->environPtr != &environ (%p!=%p) for %s", sVars->environPtr, &environ, argv[0]); success = false; } if ( sVars->__prognamePtr != &__progname ) { printf("[FAIL] crt-libSystem: sVars->__prognamePtr != &__progname (%p!=%p) for %s", sVars->__prognamePtr, &__progname, argv[0]); success = false; } if ( sVars->mh != &_mh_execute_header ) { printf("[FAIL] crt-libSystem: sVars->mh != &_mh_execute_header (%p!=%p) for %s", sVars->mh, &_mh_execute_header, argv[0]); success = false; } if ( success ) printf("[PASS] crt-vars-libSystem\n"); return 0; }
CF_PRIVATE CFArrayRef _CFBundleDYLDCopyLoadedImagePathsIfChanged(void) { // This returns an array of the paths of all the dyld images in the process. These paths may not be absolute, they may point at things that are not bundles, they may be staticly linked bundles or dynamically loaded bundles, they may be NULL. uint32_t i, numImages = _dyld_image_count(); CFMutableArrayRef result = NULL; static uint32_t _cachedDYLDImageCount = -1; if (numImages != _cachedDYLDImageCount) { const char *curName; char *cleanedCurName = NULL; CFStringRef curStr; const char *processPath = _CFProcessPath(); const void *mhp = (const void *)_NSGetMachExecuteHeader(); result = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks); for (i = 0; i < numImages; i++) { curName = _dyld_get_image_name(i); if (curName && i == 0) cleanedCurName = _cleanedPathForPath(curName); if (curName && (!processPath || 0 != strcmp(curName, processPath)) && (!processPath || !cleanedCurName || 0 != strcmp(cleanedCurName, processPath)) && mhp != (void *)_dyld_get_image_header(i)) { curStr = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, curName); if (curStr) { CFArrayAppendValue(result, curStr); CFRelease(curStr); } } if (cleanedCurName) { free(cleanedCurName); cleanedCurName = NULL; } } _cachedDYLDImageCount = numImages; } return result; }
const struct segment_command_64 * getsegbyname( char *segname) { static struct mach_header_64 *mhp = NULL; struct segment_command_64 *sgp; unsigned long i; if(mhp == NULL) #ifndef RLD mhp = _NSGetMachExecuteHeader(); #else /* defined(RLD) */ mhp = (struct mach_header_64 *)(&_mh_execute_header); #endif /* defined(RLD) */ sgp = (struct segment_command_64 *) ((char *)mhp + sizeof(struct mach_header_64)); for(i = 0; i < mhp->ncmds; i++){ if(sgp->cmd == LC_SEGMENT_64) if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) return(sgp); sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); } return(NULL); }
const struct section_64 * getsectbyname( const char *segname, const char *sectname) { struct mach_header_64 *mhp = _NSGetMachExecuteHeader(); return(getsectbynamefromheader_64(mhp, segname, sectname)); }
const struct section * getsectbyname( const char *segname, const char *sectname) { #ifndef __OPENSTEP__ struct mach_header *mhp = _NSGetMachExecuteHeader(); #else /* defined(__OPENSTEP__) */ static struct mach_header *mhp = NULL; DECLARE_VAR(_mh_execute_header, struct mach_header); SETUP_VAR(_mh_execute_header); mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header)); #endif /* __OPENSTEP__ */ return(getsectbynamefromheader(mhp, segname, sectname)); }
CF_PRIVATE CFArrayRef _CFBundleDYLDCopyLoadedImagePathsForHint(CFStringRef hint) { uint32_t i, numImages = _dyld_image_count(); CFMutableArrayRef result = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks); CFRange range = CFRangeMake(0, CFStringGetLength(hint)), altRange = CFRangeMake(0, 0), testRange = CFRangeMake(0, 0); const char *processPath = _CFProcessPath(); const void *mhp = (const void *)_NSGetMachExecuteHeader(); if (range.length > 14) { // handle some common variations on framework bundle identifiers if (CFStringFindWithOptions(hint, CFSTR(".framework"), range, kCFCompareAnchored|kCFCompareBackwards|kCFCompareCaseInsensitive, &testRange) && testRange.location > 0 && testRange.length > 0) { // identifier has .framework appended altRange.length = testRange.location; } else if (CFStringFindWithOptions(hint, CFSTR("framework"), range, kCFCompareAnchored|kCFCompareBackwards|kCFCompareCaseInsensitive, &testRange) && testRange.location > 0 && testRange.length > 0) { // identifier has Framework appended altRange.length = testRange.location; } else if (CFStringFindWithOptions(hint, CFSTR("fw"), range, kCFCompareAnchored|kCFCompareBackwards|kCFCompareCaseInsensitive, &testRange) && testRange.location > 0 && testRange.length > 0) { // identifier has FW appended altRange.length = testRange.location; } } for (i = 0; i < numImages; i++) { const char *curName = _dyld_get_image_name(i), *lastComponent = NULL; if (curName && (!processPath || 0 != strcmp(curName, processPath)) && mhp != (void *)_dyld_get_image_header(i)) lastComponent = strrchr(curName, '/'); if (lastComponent) { CFStringRef str = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, lastComponent + 1); if (str) { if (CFStringFindWithOptions(hint, str, range, kCFCompareAnchored|kCFCompareBackwards|kCFCompareCaseInsensitive, NULL) || (altRange.length > 0 && CFStringFindWithOptions(hint, str, altRange, kCFCompareAnchored|kCFCompareBackwards|kCFCompareCaseInsensitive, NULL))) { CFStringRef curStr = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, curName); if (curStr) { CFArrayAppendValue(result, curStr); CFRelease(curStr); } } CFRelease(str); } } } return result; }
static cpu_type_t current_program_arch(void) { cpu_type_t current_arch = (_NSGetMachExecuteHeader())->cputype; return current_arch; }