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