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