]> git.saurik.com Git - apple/dyld.git/blob - src/dyldNew.cpp
dyld-750.5.tar.gz
[apple/dyld.git] / src / dyldNew.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <mach/mach.h>
30 #include <sys/mman.h>
31
32 extern "C" void* __dso_handle;
33
34 #include "dyld2.h"
35 #include "dyldLibSystemInterface.h"
36
37 //
38 // dyld initially allocates all memory from a pool inside dyld.
39 // Once libSystem.dylib is initialized, dyld uses libSystem's malloc/free.
40 //
41
42 #if __LP64__
43 // room for about ~1000 initial dylibs
44 #define DYLD_POOL_CHUNK_SIZE 200*1024
45 #else
46 // room for about ~900 initial dylibs
47 #define DYLD_POOL_CHUNK_SIZE 150*1024
48 #endif
49
50 struct dyld_static_pool {
51 dyld_static_pool* previousPool;
52 uint8_t* current;
53 uint8_t* end;
54 uint8_t pool[1];
55 };
56
57 // allocate initial pool independently of pool header to take less space on disk
58 static uint8_t initialPoolContent[DYLD_POOL_CHUNK_SIZE] __attribute__((__aligned__(16)));
59 static dyld_static_pool initialPool = { NULL, initialPoolContent, &initialPoolContent[DYLD_POOL_CHUNK_SIZE] };
60 static dyld_static_pool* currentPool = &initialPool;
61
62
63 void* malloc(size_t size)
64 {
65 if ( (dyld::gLibSystemHelpers != NULL) && dyld::gProcessInfo->libSystemInitialized ) {
66 void* p = dyld::gLibSystemHelpers->malloc(size);
67 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
68 return p;
69 }
70 else {
71 if ( size > DYLD_POOL_CHUNK_SIZE ) {
72 dyld::log("dyld malloc overflow: size=%lu\n", size);
73 dyld::halt("dyld malloc overflow\n");
74 }
75 size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
76 uint8_t* result = currentPool->current;
77 currentPool->current += size;
78 if ( currentPool->current > currentPool->end ) {
79 vm_address_t addr = 0;
80 kern_return_t r = vm_allocate(mach_task_self(), &addr, DYLD_POOL_CHUNK_SIZE, VM_FLAGS_ANYWHERE);
81 if ( r != KERN_SUCCESS ) {
82 dyld::halt("out of address space for dyld memory pool\n");
83 }
84 dyld_static_pool* newPool = (dyld_static_pool*)addr;
85 newPool->previousPool = NULL;
86 newPool->current = newPool->pool;
87 newPool->end = (uint8_t*)(addr + DYLD_POOL_CHUNK_SIZE);
88 newPool->previousPool = currentPool;
89 currentPool = newPool;
90 if ( (currentPool->current + size) > currentPool->end ) {
91 dyld::log("dyld memory pool exhausted: size=%lu\n", size);
92 dyld::halt("dyld memory pool exhausted\n");
93 }
94 result = currentPool->current;
95 currentPool->current += size;
96 }
97 //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current));
98 return result;
99 }
100 }
101
102
103 void free(void* ptr)
104 {
105 // ignore any pointer within dyld (i.e. stuff from pool or static strings)
106 if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &initialPoolContent[DYLD_POOL_CHUNK_SIZE])) ) {
107 // ignore stuff in any dynamically alloated dyld pools
108 for (dyld_static_pool* p = currentPool; p != NULL; p = p->previousPool) {
109 if ( (p->pool <= ptr) && (ptr < p->end) ) {
110 // do nothing, pool entries can't be reclaimed
111 //dyld::log("free(%p) from dynamic pool\n", ptr);
112 return;
113 }
114 }
115
116 //dyld::log("free(%p) from libSystem\n", ptr);
117 return dyld::gLibSystemHelpers->free(ptr);
118 }
119 else {
120 // do nothing, pool entries can't be reclaimed
121 //dyld::log("free(%p) from static pool\n", ptr);
122 }
123 }
124
125
126 void* calloc(size_t count, size_t size)
127 {
128 // Check for overflow of integer multiplication
129 size_t total = count * size;
130 if ( total/count != size ) {
131 dyld::log("dyld calloc overflow: count=%zu, size=%zu\n", count, size);
132 dyld::halt("dyld calloc overflow");
133 }
134 if ( dyld::gLibSystemHelpers != NULL ) {
135 void* result = dyld::gLibSystemHelpers->malloc(total);
136 if ( result != NULL )
137 bzero(result, total);
138 return result;
139 }
140 else {
141 // this allocates out of static buffer which is already zero filled
142 return malloc(total);
143 }
144 }
145
146
147 void* realloc(void *ptr, size_t size)
148 {
149 void* result = malloc(size);
150 memcpy(result, ptr, size);
151 return result;
152 }
153
154 // void* reallocf(void *ptr, size_t size);
155 // void* valloc(size_t size);
156
157 // needed __libc_init()
158 extern "C" int _malloc_lock;
159 int _malloc_lock = 0;
160
161
162 // <rdar://problem/12857033> dyld calls this which uses libSystem.dylib's vm_allocate if available
163 int vm_alloc(vm_address_t* addr, vm_size_t size, uint32_t flags)
164 {
165 if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) {
166 return dyld::gLibSystemHelpers->vm_alloc(mach_task_self(), addr, size, flags);
167 }
168 else {
169 return ::vm_allocate(mach_task_self(), addr, size, flags);
170 }
171 }
172
173 void* xmmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset)
174 {
175 if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) {
176 return dyld::gLibSystemHelpers->mmap(addr, len, prot, flags, fd, offset);
177 }
178 else {
179 return ::mmap(addr, len, prot, flags, fd, offset);
180 }
181 }
182
183
184