]>
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 bzero( mem
->dat
, size
);
83 hdr
= (struct _mhead
*) addr
; hdr
--;
86 debug_iomalloc_size
-= hdr
->mlen
;
90 memset((vm_offset_t
)hdr
, 0xbb, hdr
->mlen
);
92 kfree(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(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 // Given a pointer to a 32 bit mach object segment, iterate the segment to
159 // obtain a 32 bit destructor section for C++ objects, and call each of the
160 // destructors there.
162 OSRuntimeUnloadCPPForSegment(struct segment_command
* segment
) {
164 struct section
* section
;
166 for (section
= firstsect(segment
);
168 section
= nextsect(segment
, section
)) {
170 if (strcmp(section
->sectname
, "__destructor") == 0) {
171 structor_t
* destructors
= (structor_t
*)section
->addr
;
174 int num_destructors
= section
->size
/ sizeof(structor_t
);
176 for (int i
= 0; i
< num_destructors
; i
++) {
179 } /* if (destructors) */
180 } /* if (strcmp...) */
181 } /* for (section...) */
186 // This function will only operate on 32 bit kmods
187 void OSRuntimeUnloadCPP(kmod_info_t
*ki
, void *)
189 if (ki
&& ki
->address
) {
191 struct segment_command
* segment
;
192 struct mach_header
*header
;
194 OSSymbol::checkForPageUnload((void *) ki
->address
,
195 (void *) (ki
->address
+ ki
->size
));
197 header
= (struct mach_header
*)ki
->address
;
198 segment
= firstsegfromheader(header
);
200 for (segment
= firstsegfromheader(header
);
202 segment
= nextseg(segment
)) {
204 OSRuntimeUnloadCPPForSegment(segment
);
209 kern_return_t
OSRuntimeFinalizeCPP(kmod_info_t
*ki
, void *)
213 if (OSMetaClass::modHasInstance(ki
->name
)) {
214 // @@@ gvdl should have a verbose flag
215 printf("Can't unload %s due to -\n", ki
->name
);
216 OSMetaClass::reportModInstances(ki
->name
);
217 return kOSMetaClassHasInstances
;
220 // Tell the meta class system that we are starting to unload
221 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
222 OSRuntimeUnloadCPP(ki
, 0); // Do the actual unload
223 (void) OSMetaClass::postModLoad(metaHandle
);
225 return KMOD_RETURN_SUCCESS
;
228 // Functions used by the extenTools/kmod library project
229 // This function will only operate on 32 bit kmods
230 kern_return_t
OSRuntimeInitializeCPP(kmod_info_t
*ki
, void *)
232 struct mach_header
*header
;
235 struct segment_command
* segment
;
236 struct segment_command
* failure_segment
;
238 if (!ki
|| !ki
->address
)
239 return KMOD_RETURN_FAILURE
;
241 header
= (struct mach_header
*) ki
->address
;
243 // Tell the meta class system that we are starting the load
244 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
247 return KMOD_RETURN_FAILURE
;
252 /* Scan the header for all sections named "__constructor", in any
253 * segment, and invoke the constructors within those sections.
255 for (segment
= firstsegfromheader(header
);
256 segment
!= 0 && load_success
;
257 segment
= nextseg(segment
)) {
259 struct section
* section
;
261 /* Record the current segment in the event of a failure.
263 failure_segment
= segment
;
265 for (section
= firstsect(segment
);
266 section
!= 0 && load_success
;
267 section
= nextsect(segment
, section
)) {
269 if (strcmp(section
->sectname
, "__constructor") == 0) {
270 structor_t
* constructors
= (structor_t
*)section
->addr
;
273 // FIXME: can we break here under the assumption that
274 // section names are unique within a segment?
276 int num_constructors
= section
->size
/ sizeof(structor_t
);
277 int hit_null_constructor
= 0;
280 i
< num_constructors
&&
281 OSMetaClass::checkModLoad(metaHandle
);
284 if (constructors
[i
]) {
285 (*constructors
[i
])();
286 } else if (!hit_null_constructor
) {
287 hit_null_constructor
= 1;
288 printf("Error! Null constructor in segment %s.\n",
292 load_success
= OSMetaClass::checkModLoad(metaHandle
);
294 } /* if (constructors) */
295 } /* if (strcmp...) */
296 } /* for (section...) */
297 } /* for (segment...) */
300 // We failed so call all of the destructors
303 /* Scan the header for all sections named "__constructor", in any
304 * segment, and invoke the constructors within those sections.
306 for (segment
= firstsegfromheader(header
);
307 segment
!= failure_segment
&& segment
!= 0;
308 segment
= nextseg(segment
)) {
310 OSRuntimeUnloadCPPForSegment(segment
);
312 } /* for (segment...) */
315 return OSMetaClass::postModLoad(metaHandle
);
318 static KMOD_LIB_DECL(__kernel__
, 0);
319 void OSlibkernInit(void)
321 vm_address_t
*headerArray
= (vm_address_t
*) getmachheaders();
323 KMOD_INFO_NAME
.address
= headerArray
[0]; assert(!headerArray
[1]);
324 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(&KMOD_INFO_NAME
, 0))
325 panic("OSRuntime: C++ runtime failed to initialize");
327 OSBoolean::initialize();
332 void * operator new( size_t size
)
336 result
= (void *) kern_os_malloc( size
);
340 void operator delete( void * addr
)