-/*
- *
- */
-int output_kernel_symbols( register struct proc *p )
-{
- register struct vnode *vp;
- register struct pcred *pcred = p->p_cred;
- register struct ucred *cred = pcred->pc_ucred;
- struct nameidata nd;
- struct vattr vattr;
- struct mach_header *orig_mh, *mh;
- struct load_command *lc;
- struct segment_command *orig_ds, *orig_ts, *sg;
- struct section *se;
- struct symtab_command *sc, *sc0;
- struct nlist *nl;
- vm_size_t orig_mhsize, sc0_size;
- vm_offset_t header;
- vm_size_t header_size;
- int error, error1;
- int i, j;
- int symfoffset, symsize;
- int rc_mh, rc_sc;
-
- error = EFAULT;
-
- vp = NULL;
- header = NULL;
- orig_mh = NULL;
- sc0 = NULL;
-
- rc_mh = IODTGetLoaderInfo( "Kernel-__HEADER", (void **)&orig_mh, &orig_mhsize );
- rc_sc = IODTGetLoaderInfo( "Kernel-__SYMTAB", (void **)&sc0, &sc0_size );
-
- if ( rc_mh != 0 || orig_mh == 0 || orig_mhsize < sizeof(struct mach_header) ) goto out;
- if ( rc_sc != 0 || sc0 == 0 || sc0_size < sizeof(struct symtab_command) ) goto out;
-
- if ( pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid ) goto out;
-
- 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 )) != 0 ) 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;
-
- orig_ts = findSegmentByName(orig_mh, "__TEXT");
- orig_ds = findSegmentByName(orig_mh, "__DATA");
-
- if ( orig_ts == NULL || orig_ds == NULL ) 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 == NULL ) goto out;
-
- bzero( (void *)header, header_size );
-
- /*
- * 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);
-
- /*
- * Copy __DATA and __TEXT segment commands from mach_kernel so loadable drivers
- * get correct section alignment hints.
- */
- sg = (struct segment_command *)(mh+1);
- bcopy( orig_ts, sg, orig_ts->cmdsize );
-
- sg = (struct segment_command *)((int)sg + sg->cmdsize);
- bcopy( orig_ds, sg, orig_ds->cmdsize );
-
- sg = (struct segment_command *)(mh+1);
-
- for ( i = 0; i < 2; i++ )
- {
- sg->vmaddr = 0;
- sg->vmsize = 0x1000;
- sg->fileoff = 0;
- 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 = 0;
- se->size = 0;
- se->offset = 0;
- se->nreloc = 0;
- }
-
- sg = (struct segment_command *)((int)sg + sg->cmdsize);
- }
-
- symfoffset = round_page(header_size);
-
- /*
- * Set up LC_SYMTAB command
- */
- sc = (struct symtab_command *)sg;
- sc->cmd = LC_SYMTAB;
- sc->cmdsize = sizeof(struct symtab_command);
- sc->symoff = symfoffset;
- sc->nsyms = sc0->nsyms;
- sc->strsize = sc0->strsize;
- sc->stroff = symfoffset + sc->nsyms * sizeof(struct nlist);
-
- symsize = sc->nsyms * sizeof(struct nlist) + sc->strsize;
-
- nl = (struct nlist *)(sc0+1);
- for (i = 0; i < sc->nsyms; i++, nl++ )
- {
- if ( (nl->n_type & N_TYPE) == N_SECT )
- {
- nl->n_sect = NO_SECT;
- nl->n_type = (nl->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 != 0 ) goto out;
-
- /*
- * Write out kernel symbols
- */
- error = vn_rdwr(UIO_WRITE, vp, (caddr_t)(sc0+1), symsize, symfoffset,
- UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
- if ( error != 0 ) 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_32(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_32(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_32((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_32(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;