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