]>
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 * 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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1997 Apple Computer, Inc.
27 #include <libkern/c++/OSMetaClass.h>
28 #include <libkern/c++/OSLib.h>
29 #include <libkern/c++/OSSymbol.h>
30 #include <libkern/c++/OSBoolean.h>
32 #include <sys/cdefs.h>
40 #include <mach/mach_types.h>
41 #include <mach-o/mach_header.h>
45 extern int debug_iomalloc_size
;
57 size_t memsize
= sizeof (*mem
) + size
;
62 mem
= (struct _mhead
*)kalloc(memsize
);
67 debug_iomalloc_size
+= memsize
;
71 bzero( mem
->dat
, size
);
84 hdr
= (struct _mhead
*) addr
; hdr
--;
87 debug_iomalloc_size
-= hdr
->mlen
;
91 memset((vm_offset_t
)hdr
, 0xbb, hdr
->mlen
);
93 kfree(hdr
, hdr
->mlen
);
97 void *kern_os_realloc(
103 size_t nmemsize
, osize
;
106 return (kern_os_malloc(nsize
));
108 ohdr
= (struct _mhead
*) addr
; ohdr
--;
109 osize
= ohdr
->mlen
- sizeof (*ohdr
);
118 nmemsize
= sizeof (*nmem
) + nsize
;
119 nmem
= (struct _mhead
*) kalloc(nmemsize
);
126 debug_iomalloc_size
+= (nmemsize
- ohdr
->mlen
);
129 nmem
->mlen
= nmemsize
;
131 (void) memset(&nmem
->dat
[osize
], 0, nsize
- osize
);
132 (void) memcpy(nmem
->dat
, ohdr
->dat
,
133 (nsize
> osize
) ? osize
: nsize
);
134 kfree(ohdr
, ohdr
->mlen
);
139 size_t kern_os_malloc_size(
147 hdr
= (struct _mhead
*) addr
; hdr
--;
148 return( hdr
->mlen
- sizeof (struct _mhead
));
152 void __cxa_pure_virtual( void ) { panic(__FUNCTION__
); }
154 void __pure_virtual( void ) { panic(__FUNCTION__
); }
157 typedef void (*structor_t
)(void);
159 // Given a pointer to a 32 bit mach object segment, iterate the segment to
160 // obtain a 32 bit destructor section for C++ objects, and call each of the
161 // destructors there.
163 OSRuntimeUnloadCPPForSegment(struct segment_command
* segment
) {
165 struct section
* section
;
167 for (section
= firstsect(segment
);
169 section
= nextsect(segment
, section
)) {
171 if (strcmp(section
->sectname
, "__destructor") == 0) {
172 structor_t
* destructors
= (structor_t
*)section
->addr
;
175 int num_destructors
= section
->size
/ sizeof(structor_t
);
177 for (int i
= 0; i
< num_destructors
; i
++) {
180 } /* if (destructors) */
181 } /* if (strcmp...) */
182 } /* for (section...) */
187 // This function will only operate on 32 bit kmods
188 void OSRuntimeUnloadCPP(kmod_info_t
*ki
, void *)
190 if (ki
&& ki
->address
) {
192 struct segment_command
* segment
;
193 struct mach_header
*header
;
195 OSSymbol::checkForPageUnload((void *) ki
->address
,
196 (void *) (ki
->address
+ ki
->size
));
198 header
= (struct mach_header
*)ki
->address
;
199 segment
= firstsegfromheader(header
);
201 for (segment
= firstsegfromheader(header
);
203 segment
= nextseg(segment
)) {
205 OSRuntimeUnloadCPPForSegment(segment
);
210 kern_return_t
OSRuntimeFinalizeCPP(kmod_info_t
*ki
, void *)
214 if (OSMetaClass::modHasInstance(ki
->name
)) {
215 // @@@ gvdl should have a verbose flag
216 printf("Can't unload %s due to -\n", ki
->name
);
217 OSMetaClass::reportModInstances(ki
->name
);
218 return kOSMetaClassHasInstances
;
221 // Tell the meta class system that we are starting to unload
222 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
223 OSRuntimeUnloadCPP(ki
, 0); // Do the actual unload
224 (void) OSMetaClass::postModLoad(metaHandle
);
226 return KMOD_RETURN_SUCCESS
;
229 // Functions used by the extenTools/kmod library project
230 // This function will only operate on 32 bit kmods
231 kern_return_t
OSRuntimeInitializeCPP(kmod_info_t
*ki
, void *)
233 struct mach_header
*header
;
236 struct segment_command
* segment
;
237 struct segment_command
* failure_segment
;
239 if (!ki
|| !ki
->address
)
240 return KMOD_RETURN_FAILURE
;
242 header
= (struct mach_header
*) ki
->address
;
244 // Tell the meta class system that we are starting the load
245 metaHandle
= OSMetaClass::preModLoad(ki
->name
);
248 return KMOD_RETURN_FAILURE
;
253 /* Scan the header for all sections named "__constructor", in any
254 * segment, and invoke the constructors within those sections.
256 for (segment
= firstsegfromheader(header
);
257 segment
!= 0 && load_success
;
258 segment
= nextseg(segment
)) {
260 struct section
* section
;
262 /* Record the current segment in the event of a failure.
264 failure_segment
= segment
;
266 for (section
= firstsect(segment
);
267 section
!= 0 && load_success
;
268 section
= nextsect(segment
, section
)) {
270 if (strcmp(section
->sectname
, "__constructor") == 0) {
271 structor_t
* constructors
= (structor_t
*)section
->addr
;
274 // FIXME: can we break here under the assumption that
275 // section names are unique within a segment?
277 int num_constructors
= section
->size
/ sizeof(structor_t
);
278 int hit_null_constructor
= 0;
281 i
< num_constructors
&&
282 OSMetaClass::checkModLoad(metaHandle
);
285 if (constructors
[i
]) {
286 (*constructors
[i
])();
287 } else if (!hit_null_constructor
) {
288 hit_null_constructor
= 1;
289 printf("Error! Null constructor in segment %s.\n",
293 load_success
= OSMetaClass::checkModLoad(metaHandle
);
295 } /* if (constructors) */
296 } /* if (strcmp...) */
297 } /* for (section...) */
298 } /* for (segment...) */
301 // We failed so call all of the destructors
304 /* Scan the header for all sections named "__constructor", in any
305 * segment, and invoke the constructors within those sections.
307 for (segment
= firstsegfromheader(header
);
308 segment
!= failure_segment
&& segment
!= 0;
309 segment
= nextseg(segment
)) {
311 OSRuntimeUnloadCPPForSegment(segment
);
313 } /* for (segment...) */
316 return OSMetaClass::postModLoad(metaHandle
);
319 static KMOD_LIB_DECL(__kernel__
, 0);
320 void OSlibkernInit(void)
322 vm_address_t
*headerArray
= (vm_address_t
*) getmachheaders();
324 KMOD_INFO_NAME
.address
= headerArray
[0]; assert(!headerArray
[1]);
325 if (kOSReturnSuccess
!= OSRuntimeInitializeCPP(&KMOD_INFO_NAME
, 0))
326 panic("OSRuntime: C++ runtime failed to initialize");
328 OSBoolean::initialize();
333 void * operator new( size_t size
)
337 result
= (void *) kern_os_malloc( size
);
341 void operator delete( void * addr
)