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 | /* * 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@ */ /* Copyright (c) 1998 Apple Computer, Inc. All rights reserved. * * File: bsd/kern/kern_symfile.c * * This file contains creates a dummy symbol file for mach_kernel based on * the symbol table information passed by the SecondaryLoader/PlatformExpert. * This allows us to correctly link other executables (drivers, etc) against the * the kernel in cases where the kernel image on the root device does not match * the live kernel. This can occur during net-booting where the actual kernel * image is obtained from the network via tftp rather than the root * device. * * If a symbol table is available, then the file /mach.sym will be created * containing a Mach Header and a LC_SYMTAB load command followed by the * the symbol table data for mach_kernel. * * HISTORY * * . */ #include <mach/vm_param.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/signalvar.h> #include <sys/resourcevar.h> #include <sys/namei.h> #include <sys/vnode.h> #include <sys/proc.h> #include <sys/timeb.h> #include <sys/times.h> #include <sys/buf.h> #include <sys/acct.h> #include <sys/file.h> #include <sys/uio.h> #include <sys/kernel.h> #include <sys/stat.h> #include <mach-o/loader.h> #include <mach-o/nlist.h> #include <vm/vm_kern.h> extern unsigned char rootdevice[]; extern struct mach_header _mh_execute_header; static int kernel_symfile_opened = 0; static int error_code = 0; extern int IODTGetLoaderInfo(char *key, void **infoAddr, int *infoSize); extern void IODTFreeLoaderInfo(char *key, void *infoAddr, int infoSize); /* * */ static int output_kernel_symbols(struct proc *p) { struct vnode *vp; struct pcred *pcred = p->p_cred; struct ucred *cred = pcred->pc_ucred; struct nameidata nd; struct vattr vattr; struct load_command *cmd; struct mach_header *orig_mh, *mh; struct segment_command *orig_ds, *orig_ts, *orig_le, *sg; struct section *se, *const_text; struct symtab_command *st, *orig_st; struct nlist *sym; vm_size_t orig_mhsize, orig_st_size; vm_offset_t header; vm_size_t header_size; int error, error1; int i, j; caddr_t addr; vm_offset_t offset; int rc_mh, rc_sc; error = EFAULT; vp = NULL; header = NULL; orig_mh = NULL; orig_st = NULL; // Dispose of unnecessary gumf, the booter doesn't need to load these rc_mh = IODTGetLoaderInfo("Kernel-__HEADER", (void **)&orig_mh, &orig_mhsize); if (rc_mh && orig_mh) IODTFreeLoaderInfo("Kernel-__HEADER", (void *)orig_mh, round_page(orig_mhsize)); rc_sc = IODTGetLoaderInfo("Kernel-__SYMTAB", (void **) &orig_st, &orig_st_size); if (rc_sc && orig_st) IODTFreeLoaderInfo("Kernel-__SYMTAB", (void *)orig_st, round_page(orig_st_size)); if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid) goto out; // Check to see if the root is 'e' or 'n', is this a test for network? if (rootdevice[0] == 'e' && rootdevice[1] == 'n') goto out; NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "mach.sym", p); if((error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IRGRP | S_IROTH))) goto out; vp = nd.ni_vp; /* Don't dump to non-regular files or files with links. */ error = EFAULT; if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) goto out; VATTR_NULL(&vattr); vattr.va_size = 0; VOP_LEASE(vp, p, cred, LEASE_WRITE); VOP_SETATTR(vp, &vattr, cred, p); p->p_acflag |= ACORE; // If the file type is MH_EXECUTE then this must be a kernel // as all Kernel extensions must be of type MH_OBJECT orig_ds = orig_ts = orig_le = NULL; orig_st = NULL; orig_mh = &_mh_execute_header; cmd = (struct load_command *) &orig_mh[1]; for (i = 0; i < orig_mh->ncmds; i++) { if (cmd->cmd == LC_SEGMENT) { struct segment_command *sg = (struct segment_command *) cmd; if (!strcmp(SEG_TEXT, sg->segname)) orig_ts = sg; else if (!strcmp(SEG_DATA, sg->segname)) orig_ds = sg; else if (!strcmp(SEG_LINKEDIT, sg->segname)) orig_le = sg; } else if (cmd->cmd == LC_SYMTAB) orig_st = (struct symtab_command *) cmd; cmd = (struct load_command *) ((caddr_t) cmd + cmd->cmdsize); } if (!orig_ts || !orig_ds || !orig_le || !orig_st) goto out; const_text = NULL; se = (struct section *) &orig_ts[1]; for (i = 0; i < orig_ts->nsects; i++, se++) { if (!strcmp("__const", se->sectname)) { const_text = se; break; } } if (!const_text) goto out; header_size = sizeof(struct mach_header) + orig_ts->cmdsize + orig_ds->cmdsize + sizeof(struct symtab_command); (void) kmem_alloc_wired(kernel_map, (vm_offset_t *) &header, (vm_size_t) header_size); if (header) bzero((void *) header, header_size); else goto out; /* * Set up Mach-O header. */ mh = (struct mach_header *) header; mh->magic = orig_mh->magic; mh->cputype = orig_mh->cputype; mh->cpusubtype = orig_mh->cpusubtype; mh->filetype = orig_mh->filetype; mh->ncmds = 3; mh->sizeofcmds = header_size - sizeof(struct mach_header); mh->flags = orig_mh->flags; // Initialise the current file offset and addr offset = round_page(header_size); addr = (caddr_t) const_text->addr; // Load address of __TEXT,__const /* * Construct a TEXT segment load command * the only part of the TEXT segment we keep is the __TEXT,__const * which contains the kernel vtables. */ sg = (struct segment_command *) &mh[1]; bcopy(orig_ts, sg, orig_ts->cmdsize); sg->vmaddr = (unsigned long) addr; sg->vmsize = const_text->size; sg->fileoff = 0; sg->filesize = const_text->size + round_page(header_size); sg->maxprot = 0; sg->initprot = 0; sg->flags = 0; se = (struct section *)(sg+1); for ( j = 0; j < sg->nsects; j++, se++ ) { se->addr = (unsigned long) addr; se->size = 0; se->offset = offset; se->nreloc = 0; if (!strcmp("__const", se->sectname)) { se->size = const_text->size; addr += const_text->size; offset += const_text->size; const_text = se; } } offset = round_page((vm_address_t) offset); // Now copy of the __DATA segment load command, the image need // not be stored to disk nobody needs it, yet! sg = (struct segment_command *)((int)sg + sg->cmdsize); bcopy(orig_ds, sg, orig_ds->cmdsize); sg->vmaddr = (unsigned long) addr; sg->vmsize = 0x1000; // One page for some reason? sg->fileoff = offset; sg->filesize = 0; sg->maxprot = 0; sg->initprot = 0; sg->flags = 0; se = (struct section *)(sg+1); for ( j = 0; j < sg->nsects; j++, se++ ) { se->addr = (unsigned long) addr; se->size = 0; se->offset = offset; se->nreloc = 0; } offset = round_page(offset); /* * Set up LC_SYMTAB command */ st = (struct symtab_command *)((int)sg + sg->cmdsize); st->cmd = LC_SYMTAB; st->cmdsize = sizeof(struct symtab_command); st->symoff = offset; st->nsyms = orig_st->nsyms; st->strsize = orig_st->strsize; st->stroff = offset + st->nsyms * sizeof(struct nlist); /* * Convert the symbol table in place from section references * to absolute references. */ sym = (struct nlist *) orig_le->vmaddr; for (i = 0; i < st->nsyms; i++, sym++ ) { if ( (sym->n_type & N_TYPE) == N_SECT) { sym->n_sect = NO_SECT; sym->n_type = (sym->n_type & ~N_TYPE) | N_ABS; } } /* * Write out the load commands at the beginning of the file. */ error = vn_rdwr(UIO_WRITE, vp, (caddr_t) mh, header_size, (off_t) 0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); if (error) goto out; /* * Write out the __TEXT,__const data segment. */ error = vn_rdwr(UIO_WRITE, vp, (caddr_t) const_text->addr, const_text->size, const_text->offset, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); if (error) goto out; /* * Write out kernel symbols */ offset = st->nsyms * sizeof(struct nlist) + st->strsize; // symtab size error = vn_rdwr(UIO_WRITE, vp, (caddr_t) orig_le->vmaddr, offset, st->symoff, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); if (error) goto out; out: if (header) kmem_free(kernel_map, header, header_size); if (vp) { VOP_UNLOCK(vp, 0, p); error1 = vn_close(vp, FWRITE, cred, p); if (!error) error = error1; } return(error); } /* * */ int get_kernel_symfile(struct proc *p, char **symfile) { if (!kernel_symfile_opened) { kernel_symfile_opened = 1; error_code = output_kernel_symbols(p); } if (!error_code) *symfile = "\\mach.sym"; return error_code; } |