]>
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_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1997 Apple Computer, Inc.
34 #include <libkern/c++/OSMetaClass.h>
35 #include <libkern/c++/OSLib.h>
36 #include <libkern/c++/OSSymbol.h>
37 #include <libkern/c++/OSBoolean.h>
39 #include <sys/cdefs.h>
47 #include <mach/mach_types.h>
48 #include <mach-o/mach_header.h>
52 extern int debug_iomalloc_size
;
64 size_t memsize
= sizeof (*mem
) + size
;
69 mem
= (struct _mhead
*)kalloc(memsize
);
74 debug_iomalloc_size
+= memsize
;
78 bzero( mem
->dat
, size
);
91 hdr
= (struct _mhead
*) addr
; hdr
--;
94 debug_iomalloc_size
-= hdr
->mlen
;
98 memset((vm_offset_t
)hdr
, 0xbb, hdr
->mlen
);
100 kfree(hdr
, hdr
->mlen
);
104 void *kern_os_realloc(
110 size_t nmemsize
, osize
;
113 return (kern_os_malloc(nsize
));
115 ohdr
= (struct _mhead
*) addr
; ohdr
--;
116 osize
= ohdr
->mlen
- sizeof (*ohdr
);
125 nmemsize
= sizeof (*nmem
) + nsize
;
126 nmem
= (struct _mhead
*) kalloc(nmemsize
);
133 debug_iomalloc_size
+= (nmemsize
- ohdr
->mlen
);
136 nmem
->mlen
= nmemsize
;
138 (void) memset(&nmem
->dat
[osize
], 0, nsize
- osize
);
139 (void) memcpy(nmem
->dat
, ohdr
->dat
,
140 (nsize
> osize
) ? osize
: nsize
);
141 kfree(ohdr
, ohdr
->mlen
);
146 size_t kern_os_malloc_size(
154 hdr
= (struct _mhead
*) addr
; hdr
--;
155 return( hdr
->mlen
- sizeof (struct _mhead
));
159 void __cxa_pure_virtual( void ) { panic(__FUNCTION__
); }
161 void __pure_virtual( void ) { panic(__FUNCTION__
); }
164 typedef void (*structor_t
)(void);
166 // Given a pointer to a 32 bit mach object segment, iterate the segment to
167 // obtain a 32 bit destructor section for C++ objects, and call each of the
168 // destructors there.
170 OSRuntimeUnloadCPPForSegment(struct segment_command
* segment
) {
172 struct section
* section
;
174 for (section
= firstsect(segment
);
176 section
= nextsect(segment
, section
)) {
178 if (strcmp(section
->sectname
, "__destructor") == 0) {
179 structor_t
* destructors
= (structor_t
*)section
->addr
;
182 int num_destructors
= section
->size
/ sizeof(structor_t
);
184 for (int i
= 0; i
< num_destructors
; i
++) {
187 } /* if (destructors) */
188 } /* if (strcmp...) */
189 } /* for (section...) */
194 // This function will only operate on 32 bit kmods
195 void OSRuntimeUnloadCPP(kmod_info_t
*ki
, void *)
197 if (ki
&& ki
->address
) {
199 struct segment_command
* segment
;
200 struct mach_header
*header
;
202 OSSymbol::checkForPageUnload((void *) ki
->address
,
203 (void *) (ki
->address
+ ki
->size
));
205 header
= (struct mach_header
*)ki
->address
;
206 segment
= firstsegfromheader(header
);
208 for (segment
= firstsegfromheader(header
);
210 segment
= nextseg(segment
)) {
212 OSRuntimeUnloadCPPForSegment(segment
);
217 kern_return_t
OSRuntimeFinalizeCPP(kmod_info_t
*ki
, void *)
221 if (OSMetaClass::modHasInstance(ki
->name
)) {
222 // @@@ gvdl should have a verbose flag
223 printf("Can't unload %s due to -\n", ki
->name
);
224 OSMetaClass::reportModInstances(ki
->name
);
225 return kOSMetaClassHasInstances
;
228 // Tell the meta class system that we are starting to unload
229 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
230 OSRuntimeUnloadCPP(ki
, 0); // Do the actual unload
231 (void) OSMetaClass::postModLoad(metaHandle
);
233 return KMOD_RETURN_SUCCESS
;
236 // Functions used by the extenTools/kmod library project
237 // This function will only operate on 32 bit kmods
238 kern_return_t
OSRuntimeInitializeCPP(kmod_info_t
*ki
, void *)
240 struct mach_header
*header
;
243 struct segment_command
* segment
;
244 struct segment_command
* failure_segment
;
246 if (!ki
|| !ki
->address
)
247 return KMOD_RETURN_FAILURE
;
249 header
= (struct mach_header
*) ki
->address
;
251 // Tell the meta class system that we are starting the load
252 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
255 return KMOD_RETURN_FAILURE
;
260 /* Scan the header for all sections named "__constructor", in any
261 * segment, and invoke the constructors within those sections.
263 for (segment
= firstsegfromheader(header
);
264 segment
!= 0 && load_success
;
265 segment
= nextseg(segment
)) {
267 struct section
* section
;
269 /* Record the current segment in the event of a failure.
271 failure_segment
= segment
;
273 for (section
= firstsect(segment
);
274 section
!= 0 && load_success
;
275 section
= nextsect(segment
, section
)) {
277 if (strcmp(section
->sectname
, "__constructor") == 0) {
278 structor_t
* constructors
= (structor_t
*)section
->addr
;
281 // FIXME: can we break here under the assumption that
282 // section names are unique within a segment?
284 int num_constructors
= section
->size
/ sizeof(structor_t
);
285 int hit_null_constructor
= 0;
288 i
< num_constructors
&&
289 OSMetaClass::checkModLoad(metaHandle
);
292 if (constructors
[i
]) {
293 (*constructors
[i
])();
294 } else if (!hit_null_constructor
) {
295 hit_null_constructor
= 1;
296 printf("Error! Null constructor in segment %s.\n",
300 load_success
= OSMetaClass::checkModLoad(metaHandle
);
302 } /* if (constructors) */
303 } /* if (strcmp...) */
304 } /* for (section...) */
305 } /* for (segment...) */
308 // We failed so call all of the destructors
311 /* Scan the header for all sections named "__constructor", in any
312 * segment, and invoke the constructors within those sections.
314 for (segment
= firstsegfromheader(header
);
315 segment
!= failure_segment
&& segment
!= 0;
316 segment
= nextseg(segment
)) {
318 OSRuntimeUnloadCPPForSegment(segment
);
320 } /* for (segment...) */
323 return OSMetaClass::postModLoad(metaHandle
);
326 static KMOD_LIB_DECL(__kernel__
, 0);
327 void OSlibkernInit(void)
329 vm_address_t
*headerArray
= (vm_address_t
*) getmachheaders();
331 KMOD_INFO_NAME
.address
= headerArray
[0]; assert(!headerArray
[1]);
332 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(&KMOD_INFO_NAME
, 0))
333 panic("OSRuntime: C++ runtime failed to initialize");
335 OSBoolean::initialize();
340 void * operator new( size_t size
)
344 result
= (void *) kern_os_malloc( size
);
348 void operator delete( void * addr
)