Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | /* * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * File: kern/mach_header.c * * Functions for accessing mach-o headers. * * HISTORY * 27-MAR-97 Umesh Vaishampayan (umeshv@NeXT.com) * Added getsegdatafromheader(); * * 29-Jan-92 Mike DeMoney (mike@next.com) * Made into machine independent form from machdep/m68k/mach_header.c. * Ifdef'ed out most of this since I couldn't find any references. */ #if !defined(KERNEL_PRELOAD) #include <kern/mach_header.h> extern struct mach_header _mh_execute_header; struct section *getsectbynamefromheader( struct mach_header *header, char *seg_name, char *sect_name); struct segment_command *getsegbynamefromheader( struct mach_header *header, char *seg_name); /* * return the last address (first avail) */ vm_offset_t getlastaddr(void) { struct segment_command *sgp; vm_offset_t last_addr = 0; struct mach_header *header = &_mh_execute_header; int i; sgp = (struct segment_command *) ((char *)header + sizeof(struct mach_header)); for (i = 0; i < header->ncmds; i++){ if ( sgp->cmd == LC_SEGMENT) { if (sgp->vmaddr + sgp->vmsize > last_addr) last_addr = sgp->vmaddr + sgp->vmsize; } sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return last_addr; } #if FIXME /* [ */ struct mach_header ** getmachheaders(void) { extern struct mach_header _mh_execute_header; struct mach_header **tl; tl = (struct mach_header **)malloc(2*sizeof(struct mach_header *)); tl[0] = &_mh_execute_header; tl[1] = (struct mach_header *)0; return tl; } #endif /* FIXME ] */ /* * This routine returns the a pointer to the data for the named section in the * named segment if it exist in the mach header passed to it. Also it returns * the size of the section data indirectly through the pointer size. Otherwise * it returns zero for the pointer and the size. */ void * getsectdatafromheader( struct mach_header *mhp, char *segname, char *sectname, int *size) { const struct section *sp; void *result; sp = getsectbynamefromheader(mhp, segname, sectname); if(sp == (struct section *)0){ *size = 0; return((char *)0); } *size = sp->size; result = (void *)sp->addr; return result; } /* * This routine returns the a pointer to the data for the named segment * if it exist in the mach header passed to it. Also it returns * the size of the segment data indirectly through the pointer size. * Otherwise it returns zero for the pointer and the size. */ void * getsegdatafromheader( struct mach_header *mhp, char *segname, int *size) { const struct segment_command *sc; void *result; sc = getsegbynamefromheader(mhp, segname); if(sc == (struct segment_command *)0){ *size = 0; return((char *)0); } *size = sc->vmsize; result = (void *)sc->vmaddr; return result; } /* * This routine returns the section structure for the named section in the * named segment for the mach_header pointer passed to it if it exist. * Otherwise it returns zero. */ struct section * getsectbynamefromheader( struct mach_header *mhp, char *segname, char *sectname) { struct segment_command *sgp; struct section *sp; long i, j; sgp = (struct segment_command *) ((char *)mhp + sizeof(struct mach_header)); for(i = 0; i < mhp->ncmds; i++){ if(sgp->cmd == LC_SEGMENT) if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 || mhp->filetype == MH_OBJECT){ sp = (struct section *)((char *)sgp + sizeof(struct segment_command)); for(j = 0; j < sgp->nsects; j++){ if(strncmp(sp->sectname, sectname, sizeof(sp->sectname)) == 0 && strncmp(sp->segname, segname, sizeof(sp->segname)) == 0) return(sp); sp = (struct section *)((char *)sp + sizeof(struct section)); } } sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return((struct section *)0); } struct segment_command *getsegbynamefromheader( struct mach_header *header, char *seg_name) { struct segment_command *sgp; int i; sgp = (struct segment_command *) ((char *)header + sizeof(struct mach_header)); for (i = 0; i < header->ncmds; i++){ if ( sgp->cmd == LC_SEGMENT && !strncmp(sgp->segname, seg_name, sizeof(sgp->segname))) return sgp; sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return (struct segment_command *)0; } /* * For now at least, all the rest of this seems unused. * NOTE: The constant in here for segment alignment is machine-dependent, * so if you include this, define a machine dependent constant for it's * value. */ static struct { struct segment_command seg; struct section sect; } fvm_data = { { LC_SEGMENT, // cmd sizeof(fvm_data), // cmdsize "__USER", // segname 0, // vmaddr 0, // vmsize 0, // fileoff 0, // filesize VM_PROT_READ, // maxprot VM_PROT_READ, // initprot, 1, // nsects 0 // flags }, { "", // sectname "__USER", // segname 0, // addr 0, // size 0, // offset 4, // align 0, // reloff 0, // nreloc 0 // flags } }; struct segment_command *fvm_seg; static struct fvmfile_command *fvmfilefromheader(struct mach_header *header); static vm_offset_t getsizeofmacho(struct mach_header *header); /* * Return the first segment_command in the header. */ struct segment_command *firstseg(void) { return firstsegfromheader(&_mh_execute_header); } struct segment_command *firstsegfromheader(struct mach_header *header) { struct segment_command *sgp; int i; sgp = (struct segment_command *) ((char *)header + sizeof(struct mach_header)); for (i = 0; i < header->ncmds; i++){ if (sgp->cmd == LC_SEGMENT) return sgp; sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return (struct segment_command *)0; } struct segment_command *nextseg(struct segment_command *sgp) { struct segment_command *this; this = nextsegfromheader(&_mh_execute_header, sgp); /* * For the kernel's header add on the faked segment for the * USER boot code identified by a FVMFILE_COMMAND in the mach header. */ if (!this && sgp != fvm_seg) this = fvm_seg; return this; } struct segment_command *nextsegfromheader( struct mach_header *header, struct segment_command *seg) { struct segment_command *sgp; int i; sgp = (struct segment_command *) ((char *)header + sizeof(struct mach_header)); for (i = 0; i < header->ncmds; i++) { if (sgp == seg) break; sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } if (i == header->ncmds) return (struct segment_command *)0; sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); for (; i < header->ncmds; i++) { if (sgp->cmd == LC_SEGMENT) return sgp; sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); } return (struct segment_command *)0; } /* * Return the address of the named Mach-O segment, or NULL. */ struct segment_command *getsegbyname(char *seg_name) { struct segment_command *this; this = getsegbynamefromheader(&_mh_execute_header, seg_name); /* * For the kernel's header add on the faked segment for the * USER boot code identified by a FVMFILE_COMMAND in the mach header. */ if (!this && strcmp(seg_name, fvm_seg->segname) == 0) this = fvm_seg; return this; } /* * This routine returns the a pointer the section structure of the named * section in the named segment if it exist in the mach executable it is * linked into. Otherwise it returns zero. */ struct section * getsectbyname( char *segname, char *sectname) { return(getsectbynamefromheader( (struct mach_header *)&_mh_execute_header, segname, sectname)); } struct section *firstsect(struct segment_command *sgp) { struct section *sp; if (!sgp || sgp->nsects == 0) return (struct section *)0; return (struct section *)(sgp+1); } struct section *nextsect(struct segment_command *sgp, struct section *sp) { struct section *fsp = firstsect(sgp); if (sp - fsp >= sgp->nsects-1) return (struct section *)0; return sp+1; } static struct fvmfile_command *fvmfilefromheader(struct mach_header *header) { struct fvmfile_command *fvp; int i; fvp = (struct fvmfile_command *) ((char *)header + sizeof(struct mach_header)); for (i = 0; i < header->ncmds; i++){ if (fvp->cmd == LC_FVMFILE) return fvp; fvp = (struct fvmfile_command *)((char *)fvp + fvp->cmdsize); } return (struct fvmfile_command *)0; } /* * Create a fake USER seg if a fvmfile_command is present. */ struct segment_command *getfakefvmseg(void) { struct segment_command *sgp = getsegbyname("__USER"); struct fvmfile_command *fvp = fvmfilefromheader(&_mh_execute_header); struct section *sp; if (sgp) return sgp; if (!fvp) return (struct segment_command *)0; fvm_seg = &fvm_data.seg; sgp = fvm_seg; sp = &fvm_data.sect; sgp->vmaddr = fvp->header_addr; sgp->vmsize = getsizeofmacho((struct mach_header *)(sgp->vmaddr)); strcpy(sp->sectname, fvp->name.ptr); sp->addr = sgp->vmaddr; sp->size = sgp->vmsize; #if DEBUG printf("fake fvm seg __USER/\"%s\" at 0x%x, size 0x%x\n", sp->sectname, sp->addr, sp->size); #endif /* DEBUG */ } /* * Figure out the size the size of the data associated with a * loaded mach_header. */ static vm_offset_t getsizeofmacho(struct mach_header *header) { struct segment_command *sgp; struct section *sp; vm_offset_t last_addr; last_addr = 0; for ( sgp = firstsegfromheader(header) ; sgp ; sgp = nextsegfromheader(header, sgp)) { if (sgp->fileoff + sgp->filesize > last_addr) last_addr = sgp->fileoff + sgp->filesize; } return last_addr; } #endif /* !defined(KERNEL_PRELOAD) */ |