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