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