1 /* $NetBSD: kern_ctf.c,v 1.2 2010/03/13 01:41:14 christos Exp $ */ 2 /*- 3 * Copyright (c) 2008 John Birrell <jb@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/module.h> 32 #include <sys/exec.h> 33 #include <sys/exec_elf.h> 34 #include <sys/kmem.h> 35 #include <sys/malloc.h> 36 #include <sys/kobj_impl.h> 37 #include <sys/kobj.h> 38 #include <sys/kern_ctf.h> 39 40 #define _KSYMS_PRIVATE 41 #include <sys/ksyms.h> 42 43 #include <net/zlib.h> 44 45 /* 46 * Note this file is included by both link_elf.c and link_elf_obj.c. 47 * 48 * The CTF header structure definition can't be used here because it's 49 * (annoyingly) covered by the CDDL. We will just use a few bytes from 50 * it as an integer array where we 'know' what they mean. 51 */ 52 #define CTF_HDR_SIZE 36 53 #define CTF_HDR_STRTAB_U32 7 54 #define CTF_HDR_STRLEN_U32 8 55 56 static void * 57 z_alloc(void *nil, u_int items, u_int size) 58 { 59 void *ptr; 60 61 ptr = malloc(items * size, M_TEMP, M_NOWAIT); 62 return ptr; 63 } 64 65 static void 66 z_free(void *nil, void *ptr) 67 { 68 free(ptr, M_TEMP); 69 } 70 71 int 72 mod_ctf_get(struct module *mod, mod_ctf_t *mc) 73 { 74 mod_ctf_t *cmc; 75 struct ksyms_symtab *st; 76 void * ctftab = NULL; 77 size_t sz; 78 int error = 0; 79 int compressed = 0; 80 81 void *ctfbuf = NULL; 82 uint8_t *ctfaddr; 83 size_t ctfsize; 84 85 if (mc == NULL) 86 return EINVAL; 87 88 /* Set the defaults for no CTF present. That's not a crime! */ 89 memset(mc, 0, sizeof(*mc)); 90 91 /* cached mc? */ 92 if (mod->mod_ctf != NULL) { 93 cmc = mod->mod_ctf; 94 *mc = *cmc; 95 return (0); 96 } 97 98 st = ksyms_get_mod(mod->mod_info->mi_name); 99 100 if (st != NULL) { 101 mc->nmap = st->sd_nmap; 102 mc->nmapsize = st->sd_nmapsize; 103 } 104 105 if (mod->mod_kobj == NULL) { 106 /* no kobj entry, try building from ksyms list */ 107 if (st == NULL) { 108 return ENOENT; 109 } 110 111 ctfaddr = st->sd_ctfstart; 112 ctfsize = st->sd_ctfsize; 113 114 mc->symtab = st->sd_symstart; 115 mc->strtab = st->sd_strstart; 116 mc->strcnt = 0; /* XXX TBD */ 117 mc->nsym = st->sd_symsize / sizeof(Elf_Sym); 118 } else { 119 if (kobj_find_section(mod->mod_kobj, ".SUNW_ctf", (void **)&ctfaddr, &ctfsize)) 120 return ENOENT; 121 122 mc->symtab = mod->mod_kobj->ko_symtab; 123 mc->strtab = mod->mod_kobj->ko_strtab; 124 mc->strcnt = 0; /* XXX TBD */ 125 mc->nsym = mod->mod_kobj->ko_symcnt; 126 } 127 128 if (ctfaddr == NULL) { 129 goto out; 130 } 131 132 /* Check the CTF magic number. (XXX check for big endian!) */ 133 if (ctfaddr[0] != 0xf1 || ctfaddr[1] != 0xcf) { 134 goto out; 135 } 136 137 /* Check if version 2. */ 138 if (ctfaddr[2] != 2) 139 goto out; 140 141 /* Check if the data is compressed. */ 142 if ((ctfaddr[3] & 0x1) != 0) { 143 uint32_t *u32 = (uint32_t *) ctfaddr; 144 145 /* 146 * The last two fields in the CTF header are the offset 147 * from the end of the header to the start of the string 148 * data and the length of that string data. se this 149 * information to determine the decompressed CTF data 150 * buffer required. 151 */ 152 sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] + 153 CTF_HDR_SIZE; 154 155 compressed = 1; 156 } else { 157 /* 158 * The CTF data is not compressed, so the ELF section 159 * size is the same as the buffer size required. 160 */ 161 sz = ctfsize; 162 } 163 164 /* 165 * Allocate memory to buffer the CTF data in it's decompressed 166 * form. 167 */ 168 if (compressed) { 169 if ((ctfbuf = malloc(sz, M_TEMP, M_WAITOK)) == NULL) { 170 error = ENOMEM; 171 goto out; 172 } 173 ctftab = ctfbuf; 174 mc->ctfalloc = 1; 175 } else { 176 ctftab = (void *)ctfaddr; 177 } 178 179 /* Check if decompression is required. */ 180 if (compressed) { 181 z_stream zs; 182 int ret; 183 184 /* 185 * The header isn't compressed, so copy that into the 186 * CTF buffer first. 187 */ 188 memcpy(ctftab, ctfaddr, CTF_HDR_SIZE); 189 190 /* Initialise the zlib structure. */ 191 memset(&zs, 0, sizeof(zs)); 192 zs.zalloc = z_alloc; 193 zs.zfree = z_free; 194 195 if (inflateInit2(&zs, MAX_WBITS) != Z_OK) { 196 error = EIO; 197 goto out; 198 } 199 200 zs.avail_in = ctfsize - CTF_HDR_SIZE; 201 zs.next_in = ((uint8_t *) ctfaddr) + CTF_HDR_SIZE; 202 zs.avail_out = sz - CTF_HDR_SIZE; 203 zs.next_out = ((uint8_t *) ctftab) + CTF_HDR_SIZE; 204 inflateReset(&zs); 205 if ((ret = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { 206 printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret); 207 error = EIO; 208 goto out; 209 } 210 } 211 212 /* Got the CTF data! */ 213 mc->ctftab = ctftab; 214 mc->ctfcnt = ctfsize; 215 216 /* cache it */ 217 cmc = kmem_alloc(sizeof(mod_ctf_t), KM_SLEEP); 218 219 *cmc = *mc; 220 mod->mod_ctf = cmc; 221 222 /* We'll retain the memory allocated for the CTF data. */ 223 ctfbuf = NULL; 224 225 out: 226 if (ctfbuf != NULL) 227 free(ctfbuf, M_TEMP); 228 229 return (error); 230 } 231