Home | History | Annotate | Line # | Download | only in kern
kern_ctf.c revision 1.2.2.1
      1  1.2.2.1     rmind /*	$NetBSD: kern_ctf.c,v 1.2.2.1 2010/05/30 05:17:56 rmind Exp $	*/
      2      1.1    darran /*-
      3      1.1    darran  * Copyright (c) 2008 John Birrell <jb (at) freebsd.org>
      4      1.1    darran  * All rights reserved.
      5      1.1    darran  *
      6      1.1    darran  * Redistribution and use in source and binary forms, with or without
      7      1.1    darran  * modification, are permitted provided that the following conditions
      8      1.1    darran  * are met:
      9      1.1    darran  * 1. Redistributions of source code must retain the above copyright
     10      1.1    darran  *    notice, this list of conditions and the following disclaimer.
     11      1.1    darran  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1    darran  *    notice, this list of conditions and the following disclaimer in the
     13      1.1    darran  *    documentation and/or other materials provided with the distribution.
     14      1.1    darran  *
     15      1.1    darran  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16      1.1    darran  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17      1.1    darran  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18      1.1    darran  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19      1.1    darran  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20      1.1    darran  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21      1.1    darran  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22      1.1    darran  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23      1.1    darran  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24      1.1    darran  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25      1.1    darran  * SUCH DAMAGE.
     26      1.1    darran  *
     27      1.1    darran  * $FreeBSD: src/sys/kern/kern_ctf.c,v 1.1.4.1 2009/08/03 08:13:06 kensmith Exp $
     28      1.1    darran  */
     29      1.1    darran 
     30      1.1    darran #define ELFSIZE ARCH_ELFSIZE
     31      1.1    darran #include <sys/module.h>
     32      1.2  christos #include <sys/exec.h>
     33      1.1    darran #include <sys/exec_elf.h>
     34      1.1    darran #include <sys/kmem.h>
     35      1.1    darran #include <sys/malloc.h>
     36      1.1    darran #include <sys/kobj_impl.h>
     37      1.1    darran #include <sys/kobj.h>
     38      1.1    darran #include <sys/kern_ctf.h>
     39      1.1    darran 
     40      1.1    darran #define _KSYMS_PRIVATE
     41      1.1    darran #include <sys/ksyms.h>
     42      1.1    darran 
     43      1.1    darran #include <net/zlib.h>
     44      1.1    darran 
     45      1.1    darran /*
     46      1.1    darran  * Note this file is included by both link_elf.c and link_elf_obj.c.
     47      1.1    darran  *
     48      1.1    darran  * The CTF header structure definition can't be used here because it's
     49      1.1    darran  * (annoyingly) covered by the CDDL. We will just use a few bytes from
     50      1.1    darran  * it as an integer array where we 'know' what they mean.
     51      1.1    darran  */
     52      1.1    darran #define CTF_HDR_SIZE		36
     53      1.1    darran #define CTF_HDR_STRTAB_U32	7
     54      1.1    darran #define CTF_HDR_STRLEN_U32	8
     55      1.1    darran 
     56      1.1    darran static void *
     57      1.1    darran z_alloc(void *nil, u_int items, u_int size)
     58      1.1    darran {
     59      1.1    darran 	void *ptr;
     60      1.1    darran 
     61      1.1    darran 	ptr = malloc(items * size, M_TEMP, M_NOWAIT);
     62      1.1    darran 	return ptr;
     63      1.1    darran }
     64      1.1    darran 
     65      1.1    darran static void
     66      1.1    darran z_free(void *nil, void *ptr)
     67      1.1    darran {
     68      1.1    darran 	free(ptr, M_TEMP);
     69      1.1    darran }
     70      1.1    darran 
     71      1.1    darran int
     72      1.1    darran mod_ctf_get(struct module *mod, mod_ctf_t *mc)
     73      1.1    darran {
     74      1.1    darran 	mod_ctf_t *cmc;
     75      1.1    darran 	struct ksyms_symtab *st;
     76      1.1    darran 	void * ctftab = NULL;
     77      1.1    darran 	size_t sz;
     78      1.1    darran 	int error = 0;
     79      1.1    darran 	int compressed = 0;
     80      1.1    darran 
     81      1.1    darran 	void *ctfbuf = NULL;
     82      1.1    darran 	uint8_t *ctfaddr;
     83      1.1    darran 	size_t ctfsize;
     84      1.1    darran 
     85  1.2.2.1     rmind 	if (mc == NULL) {
     86      1.1    darran 		return EINVAL;
     87  1.2.2.1     rmind 	}
     88      1.1    darran 
     89      1.1    darran 	/* Set the defaults for no CTF present. That's not a crime! */
     90      1.1    darran 	memset(mc, 0, sizeof(*mc));
     91      1.1    darran 
     92      1.1    darran 	/* cached mc? */
     93      1.1    darran 	if (mod->mod_ctf != NULL) {
     94      1.1    darran 		cmc = mod->mod_ctf;
     95      1.1    darran 		*mc = *cmc;
     96      1.1    darran 		return (0);
     97      1.1    darran 	}
     98      1.1    darran 
     99      1.1    darran 	st = ksyms_get_mod(mod->mod_info->mi_name);
    100      1.1    darran 
    101      1.1    darran 	if (st != NULL) {
    102      1.1    darran 		mc->nmap     = st->sd_nmap;
    103      1.1    darran 		mc->nmapsize = st->sd_nmapsize;
    104      1.1    darran 	}
    105      1.1    darran 
    106      1.1    darran 	if (mod->mod_kobj == NULL) {
    107      1.1    darran 	    	/* no kobj entry, try building from ksyms list */
    108      1.1    darran 		if (st == NULL) {
    109      1.1    darran 			return ENOENT;
    110      1.1    darran 		}
    111      1.1    darran 
    112      1.1    darran 		ctfaddr = st->sd_ctfstart;
    113      1.1    darran 		ctfsize = st->sd_ctfsize;
    114      1.1    darran 
    115      1.1    darran 		mc->symtab = st->sd_symstart;
    116      1.1    darran 		mc->strtab = st->sd_strstart;
    117      1.1    darran 		mc->strcnt = 0;		/* XXX TBD */
    118      1.1    darran 		mc->nsym   = st->sd_symsize / sizeof(Elf_Sym);
    119      1.1    darran 	} else {
    120  1.2.2.1     rmind 		if (kobj_find_section(mod->mod_kobj, ".SUNW_ctf", (void **)&ctfaddr, &ctfsize)) {
    121      1.1    darran 			return ENOENT;
    122  1.2.2.1     rmind 		}
    123      1.1    darran 
    124      1.1    darran 		mc->symtab = mod->mod_kobj->ko_symtab;
    125      1.1    darran 		mc->strtab = mod->mod_kobj->ko_strtab;
    126      1.1    darran 		mc->strcnt = 0;		/* XXX TBD */
    127      1.1    darran 		mc->nsym   = mod->mod_kobj->ko_symcnt;
    128      1.1    darran 	}
    129      1.1    darran 
    130      1.1    darran 	if (ctfaddr == NULL) {
    131  1.2.2.1     rmind 	    	error = ENOENT;
    132      1.1    darran 		goto out;
    133      1.1    darran 	}
    134      1.1    darran 
    135      1.1    darran 	/* Check the CTF magic number. (XXX check for big endian!) */
    136      1.1    darran 	if (ctfaddr[0] != 0xf1 || ctfaddr[1] != 0xcf) {
    137  1.2.2.1     rmind 	    	error = EINVAL;
    138      1.1    darran 		goto out;
    139      1.1    darran 	}
    140      1.1    darran 
    141      1.1    darran 	/* Check if version 2. */
    142  1.2.2.1     rmind 	if (ctfaddr[2] != 2) {
    143  1.2.2.1     rmind 	    	error = EINVAL;
    144      1.1    darran 		goto out;
    145  1.2.2.1     rmind 	}
    146      1.1    darran 
    147      1.1    darran 	/* Check if the data is compressed. */
    148      1.1    darran 	if ((ctfaddr[3] & 0x1) != 0) {
    149      1.1    darran 		uint32_t *u32 = (uint32_t *) ctfaddr;
    150      1.1    darran 
    151      1.1    darran 		/*
    152      1.1    darran 		 * The last two fields in the CTF header are the offset
    153      1.1    darran 		 * from the end of the header to the start of the string
    154      1.1    darran 		 * data and the length of that string data. se this
    155      1.1    darran 		 * information to determine the decompressed CTF data
    156      1.1    darran 		 * buffer required.
    157      1.1    darran 		 */
    158      1.1    darran 		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
    159      1.1    darran 		    CTF_HDR_SIZE;
    160      1.1    darran 
    161      1.1    darran 		compressed = 1;
    162      1.1    darran 	} else {
    163      1.1    darran 		/*
    164      1.1    darran 		 * The CTF data is not compressed, so the ELF section
    165      1.1    darran 		 * size is the same as the buffer size required.
    166      1.1    darran 		 */
    167      1.1    darran 		sz = ctfsize;
    168      1.1    darran 	}
    169      1.1    darran 
    170      1.1    darran 	/*
    171      1.1    darran 	 * Allocate memory to buffer the CTF data in it's decompressed
    172      1.1    darran 	 * form.
    173      1.1    darran 	 */
    174      1.1    darran 	if (compressed) {
    175      1.1    darran 		if ((ctfbuf = malloc(sz, M_TEMP, M_WAITOK)) == NULL) {
    176      1.1    darran 			error = ENOMEM;
    177      1.1    darran 			goto out;
    178      1.1    darran 		}
    179      1.1    darran 		ctftab = ctfbuf;
    180      1.1    darran 		mc->ctfalloc = 1;
    181      1.1    darran 	} else {
    182      1.1    darran 		ctftab = (void *)ctfaddr;
    183      1.1    darran 	}
    184      1.1    darran 
    185      1.1    darran 	/* Check if decompression is required. */
    186      1.1    darran 	if (compressed) {
    187      1.1    darran 		z_stream zs;
    188      1.1    darran 		int ret;
    189      1.1    darran 
    190      1.1    darran 		/*
    191      1.1    darran 		 * The header isn't compressed, so copy that into the
    192      1.1    darran 		 * CTF buffer first.
    193      1.1    darran 		 */
    194      1.1    darran 		memcpy(ctftab, ctfaddr, CTF_HDR_SIZE);
    195      1.1    darran 
    196      1.1    darran 		/* Initialise the zlib structure. */
    197      1.1    darran 		memset(&zs, 0, sizeof(zs));
    198      1.1    darran 		zs.zalloc = z_alloc;
    199      1.1    darran 		zs.zfree = z_free;
    200      1.1    darran 
    201      1.1    darran 		if (inflateInit2(&zs, MAX_WBITS) != Z_OK) {
    202      1.1    darran 			error = EIO;
    203      1.1    darran 			goto out;
    204      1.1    darran 		}
    205      1.1    darran 
    206      1.1    darran 		zs.avail_in = ctfsize - CTF_HDR_SIZE;
    207      1.1    darran 		zs.next_in = ((uint8_t *) ctfaddr) + CTF_HDR_SIZE;
    208      1.1    darran 		zs.avail_out = sz - CTF_HDR_SIZE;
    209      1.1    darran 		zs.next_out = ((uint8_t *) ctftab) + CTF_HDR_SIZE;
    210      1.1    darran 		inflateReset(&zs);
    211      1.1    darran 		if ((ret = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
    212      1.1    darran 			printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
    213      1.1    darran 			error = EIO;
    214      1.1    darran 			goto out;
    215      1.1    darran 		}
    216      1.1    darran 	}
    217      1.1    darran 
    218      1.1    darran 	/* Got the CTF data! */
    219      1.1    darran 	mc->ctftab = ctftab;
    220      1.1    darran 	mc->ctfcnt = ctfsize;
    221      1.1    darran 
    222      1.1    darran 	/* cache it */
    223      1.1    darran 	cmc = kmem_alloc(sizeof(mod_ctf_t), KM_SLEEP);
    224      1.1    darran 
    225      1.1    darran 	*cmc = *mc;
    226      1.1    darran 	mod->mod_ctf = cmc;
    227      1.1    darran 
    228      1.1    darran 	/* We'll retain the memory allocated for the CTF data. */
    229      1.1    darran 	ctfbuf = NULL;
    230      1.1    darran 
    231      1.1    darran out:
    232      1.1    darran 	if (ctfbuf != NULL)
    233      1.1    darran 		free(ctfbuf, M_TEMP);
    234      1.1    darran 
    235      1.1    darran 	return (error);
    236      1.1    darran }
    237