]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSRuntime.cpp
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1997 Apple Computer, Inc.
26 #include <libkern/c++/OSMetaClass.h>
27 #include <libkern/c++/OSLib.h>
28 #include <libkern/c++/OSSymbol.h>
29 #include <libkern/c++/OSBoolean.h>
31 #include <sys/cdefs.h>
39 #include <mach/mach_types.h>
40 #include <mach-o/mach_header.h>
44 extern int debug_iomalloc_size
;
56 size_t memsize
= sizeof (*mem
) + size
;
61 mem
= (struct _mhead
*)kalloc(memsize
);
66 debug_iomalloc_size
+= memsize
;
70 (void) memset(mem
->dat
, 0, size
);
83 hdr
= (struct _mhead
*) addr
; hdr
--;
86 debug_iomalloc_size
-= hdr
->mlen
;
90 memset((vm_offset_t
)hdr
, 0xbb, hdr
->mlen
);
92 kfree((vm_offset_t
)hdr
, hdr
->mlen
);
96 void *kern_os_realloc(
102 size_t nmemsize
, osize
;
105 return (kern_os_malloc(nsize
));
107 ohdr
= (struct _mhead
*) addr
; ohdr
--;
108 osize
= ohdr
->mlen
- sizeof (*ohdr
);
117 nmemsize
= sizeof (*nmem
) + nsize
;
118 nmem
= (struct _mhead
*) kalloc(nmemsize
);
125 debug_iomalloc_size
+= (nmemsize
- ohdr
->mlen
);
128 nmem
->mlen
= nmemsize
;
130 (void) memset(&nmem
->dat
[osize
], 0, nsize
- osize
);
131 (void) memcpy(nmem
->dat
, ohdr
->dat
,
132 (nsize
> osize
) ? osize
: nsize
);
133 kfree((vm_offset_t
)ohdr
, ohdr
->mlen
);
138 size_t kern_os_malloc_size(
146 hdr
= (struct _mhead
*) addr
; hdr
--;
147 return( hdr
->mlen
- sizeof (struct _mhead
));
151 void __cxa_pure_virtual( void ) { panic(__FUNCTION__
); }
153 void __pure_virtual( void ) { panic(__FUNCTION__
); }
156 typedef void (*structor_t
)(void);
158 void OSRuntimeUnloadCPPForSegment(struct segment_command
* segment
) {
160 struct section
* section
;
162 for (section
= firstsect(segment
);
164 section
= nextsect(segment
, section
)) {
166 if (strcmp(section
->sectname
, "__destructor") == 0) {
167 structor_t
* destructors
= (structor_t
*)section
->addr
;
170 int num_destructors
= section
->size
/ sizeof(structor_t
);
172 for (int i
= 0; i
< num_destructors
; i
++) {
175 } /* if (destructors) */
176 } /* if (strcmp...) */
177 } /* for (section...) */
182 void OSRuntimeUnloadCPP(kmod_info_t
*ki
, void *)
184 if (ki
&& ki
->address
) {
186 struct segment_command
* segment
;
187 struct mach_header
*header
;
189 OSSymbol::checkForPageUnload((void *) ki
->address
,
190 (void *) (ki
->address
+ ki
->size
));
192 header
= (struct mach_header
*)ki
->address
;
193 segment
= firstsegfromheader(header
);
195 for (segment
= firstsegfromheader(header
);
197 segment
= nextseg(segment
)) {
199 OSRuntimeUnloadCPPForSegment(segment
);
204 kern_return_t
OSRuntimeFinalizeCPP(kmod_info_t
*ki
, void *)
208 if (OSMetaClass::modHasInstance(ki
->name
)) {
209 // @@@ gvdl should have a verbose flag
210 printf("Can't unload %s due to -\n", ki
->name
);
211 OSMetaClass::reportModInstances(ki
->name
);
212 return kOSMetaClassHasInstances
;
215 // Tell the meta class system that we are starting to unload
216 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
217 OSRuntimeUnloadCPP(ki
, 0); // Do the actual unload
218 (void) OSMetaClass::postModLoad(metaHandle
);
220 return KMOD_RETURN_SUCCESS
;
223 // Functions used by the extenTools/kmod library project
224 kern_return_t
OSRuntimeInitializeCPP(kmod_info_t
*ki
, void *)
226 struct mach_header
*header
;
229 struct segment_command
* segment
;
230 struct segment_command
* failure_segment
;
232 if (!ki
|| !ki
->address
)
233 return KMOD_RETURN_FAILURE
;
235 header
= (struct mach_header
*) ki
->address
;
237 // Tell the meta class system that we are starting the load
238 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
241 return KMOD_RETURN_FAILURE
;
246 /* Scan the header for all sections named "__constructor", in any
247 * segment, and invoke the constructors within those sections.
249 for (segment
= firstsegfromheader(header
);
250 segment
!= 0 && load_success
;
251 segment
= nextseg(segment
)) {
253 struct section
* section
;
255 /* Record the current segment in the event of a failure.
257 failure_segment
= segment
;
259 for (section
= firstsect(segment
);
260 section
!= 0 && load_success
;
261 section
= nextsect(segment
, section
)) {
263 if (strcmp(section
->sectname
, "__constructor") == 0) {
264 structor_t
* constructors
= (structor_t
*)section
->addr
;
267 // FIXME: can we break here under the assumption that
268 // section names are unique within a segment?
270 int num_constructors
= section
->size
/ sizeof(structor_t
);
271 int hit_null_constructor
= 0;
274 i
< num_constructors
&&
275 OSMetaClass::checkModLoad(metaHandle
);
278 if (constructors
[i
]) {
279 (*constructors
[i
])();
280 } else if (!hit_null_constructor
) {
281 hit_null_constructor
= 1;
282 printf("Error! Null constructor in segment %s.\n",
286 load_success
= OSMetaClass::checkModLoad(metaHandle
);
288 } /* if (constructors) */
289 } /* if (strcmp...) */
290 } /* for (section...) */
291 } /* for (segment...) */
294 // We failed so call all of the destructors
297 /* Scan the header for all sections named "__constructor", in any
298 * segment, and invoke the constructors within those sections.
300 for (segment
= firstsegfromheader(header
);
301 segment
!= failure_segment
&& segment
!= 0;
302 segment
= nextseg(segment
)) {
304 OSRuntimeUnloadCPPForSegment(segment
);
306 } /* for (segment...) */
309 return OSMetaClass::postModLoad(metaHandle
);
312 static KMOD_LIB_DECL(__kernel__
, 0);
313 void OSlibkernInit(void)
315 vm_address_t
*headerArray
= (vm_address_t
*) getmachheaders();
317 KMOD_INFO_NAME
.address
= headerArray
[0]; assert(!headerArray
[1]);
318 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(&KMOD_INFO_NAME
, 0))
319 panic("OSRuntime: C++ runtime failed to initialize");
321 OSBoolean::initialize();
326 void * operator new( size_t size
)
330 result
= (void *) kern_os_malloc( size
);
332 bzero( result
, size
);
336 void operator delete( void * addr
)