]> git.saurik.com Git - apple/dyld.git/blob - src/dyldNew.cpp
dyld-832.7.1.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
55 // libunwind, and probably others, need the pool to be 16-byte aligned as malloc guarantees that
56 __attribute__((__aligned__(16)))
57 uint8_t pool[1];
58 };
59
60 // allocate initial pool independently of pool header to take less space on disk
61 static uint8_t initialPoolContent[DYLD_POOL_CHUNK_SIZE] __attribute__((__aligned__(16)));
62 static dyld_static_pool initialPool = { NULL, initialPoolContent, &initialPoolContent[DYLD_POOL_CHUNK_SIZE] };
63 static dyld_static_pool* currentPool = &initialPool;
64
65
66 void* malloc(size_t size)
67 {
68 if ( (dyld::gLibSystemHelpers != NULL) && dyld::gProcessInfo->libSystemInitialized ) {
69 void* p = dyld::gLibSystemHelpers->malloc(size);
70 //dyld::log("malloc(%lu) => %p from libSystem\n", size, p);
71 return p;
72 }
73 else {
74 // keep allocations 16-byte aligned
75 size = ((size + 15) & -16);
76
77 if ( size > DYLD_POOL_CHUNK_SIZE ) {
78 dyld::log("dyld malloc overflow: size=%lu\n", size);
79 dyld::halt("dyld malloc overflow\n");
80 }
81 size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align
82 uint8_t* result = currentPool->current;
83 currentPool->current += size;
84 if ( currentPool->current > currentPool->end ) {
85 vm_address_t addr = 0;
86 kern_return_t r = vm_allocate(mach_task_self(), &addr, DYLD_POOL_CHUNK_SIZE, VM_FLAGS_ANYWHERE);
87 if ( r != KERN_SUCCESS ) {
88 dyld::halt("out of address space for dyld memory pool\n");
89 }
90 dyld_static_pool* newPool = (dyld_static_pool*)addr;
91 newPool->previousPool = NULL;
92 newPool->current = newPool->pool;
93 newPool->end = (uint8_t*)(addr + DYLD_POOL_CHUNK_SIZE);
94 newPool->previousPool = currentPool;
95 currentPool = newPool;
96 if ( (currentPool->current + size) > currentPool->end ) {
97 dyld::log("dyld memory pool exhausted: size=%lu\n", size);
98 dyld::halt("dyld memory pool exhausted\n");
99 }
100 result = currentPool->current;
101 currentPool->current += size;
102 }
103 //dyld::log("%p = malloc(%3lu) from pool %p, free space = %lu\n", result, size, currentPool, (long)(currentPool->end - currentPool->current));
104 return result;
105 }
106 }
107
108
109 void free(void* ptr)
110 {
111 // ignore any pointer within dyld (i.e. stuff from pool or static strings)
112 if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &initialPoolContent[DYLD_POOL_CHUNK_SIZE])) ) {
113 // ignore stuff in any dynamically alloated dyld pools
114 for (dyld_static_pool* p = currentPool; p != NULL; p = p->previousPool) {
115 if ( (p->pool <= ptr) && (ptr < p->end) ) {
116 // do nothing, pool entries can't be reclaimed
117 //dyld::log("free(%p) from dynamic pool\n", ptr);
118 return;
119 }
120 }
121
122 //dyld::log("free(%p) from libSystem\n", ptr);
123 return dyld::gLibSystemHelpers->free(ptr);
124 }
125 else {
126 // do nothing, pool entries can't be reclaimed
127 //dyld::log("free(%p) from static pool\n", ptr);
128 }
129 }
130
131
132 void* calloc(size_t count, size_t size)
133 {
134 // Check for overflow of integer multiplication
135 size_t total = count * size;
136 if ( total/count != size ) {
137 dyld::log("dyld calloc overflow: count=%zu, size=%zu\n", count, size);
138 dyld::halt("dyld calloc overflow");
139 }
140 if ( dyld::gLibSystemHelpers != NULL ) {
141 void* result = dyld::gLibSystemHelpers->malloc(total);
142 if ( result != NULL )
143 bzero(result, total);
144 return result;
145 }
146 else {
147 // this allocates out of static buffer which is already zero filled
148 return malloc(total);
149 }
150 }
151
152
153 void* realloc(void *ptr, size_t size)
154 {
155 void* result = malloc(size);
156 memcpy(result, ptr, size);
157 return result;
158 }
159
160 // void* reallocf(void *ptr, size_t size);
161 // void* valloc(size_t size);
162
163 // needed __libc_init()
164 extern "C" int _malloc_lock;
165 int _malloc_lock = 0;
166
167
168 // <rdar://problem/12857033> dyld calls this which uses libSystem.dylib's vm_allocate if available
169 int vm_alloc(vm_address_t* addr, vm_size_t size, uint32_t flags)
170 {
171 if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) {
172 return dyld::gLibSystemHelpers->vm_alloc(mach_task_self(), addr, size, flags);
173 }
174 else {
175 return ::vm_allocate(mach_task_self(), addr, size, flags);
176 }
177 }
178
179 void* xmmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset)
180 {
181 if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) {
182 return dyld::gLibSystemHelpers->mmap(addr, len, prot, flags, fd, offset);
183 }
184 else {
185 return ::mmap(addr, len, prot, flags, fd, offset);
186 }
187 }
188
189
190