kern_ctf.c revision 1.6.16.1 1 1.6.16.1 pgoyette /* $NetBSD: kern_ctf.c,v 1.6.16.1 2018/06/25 07:26:04 pgoyette 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.6.16.1 pgoyette #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.6.16.1 pgoyette mod_ctf_get(struct module *mod, mod_ctf_t **mcp)
74 1.1 darran {
75 1.6.16.1 pgoyette 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.1 darran size_t ctfsize;
85 1.1 darran
86 1.6.16.1 pgoyette /*
87 1.6.16.1 pgoyette * Return the cached mc if there is one already.
88 1.6.16.1 pgoyette */
89 1.1 darran
90 1.6.16.1 pgoyette extern specificdata_key_t fbt_module_key;
91 1.1 darran
92 1.6.16.1 pgoyette mc = module_getspecific(mod, fbt_module_key);
93 1.6.16.1 pgoyette if (mc != NULL) {
94 1.6.16.1 pgoyette *mcp = mc;
95 1.1 darran return (0);
96 1.1 darran }
97 1.1 darran
98 1.6.16.1 pgoyette /*
99 1.6.16.1 pgoyette * Allocate and initialize a new mc.
100 1.6.16.1 pgoyette */
101 1.1 darran
102 1.6.16.1 pgoyette mc = kmem_zalloc(sizeof(mod_ctf_t), KM_SLEEP);
103 1.6.16.1 pgoyette st = ksyms_get_mod(module_name(mod));
104 1.1 darran if (st != NULL) {
105 1.1 darran mc->nmap = st->sd_nmap;
106 1.1 darran mc->nmapsize = st->sd_nmapsize;
107 1.1 darran }
108 1.1 darran
109 1.1 darran if (mod->mod_kobj == NULL) {
110 1.1 darran /* no kobj entry, try building from ksyms list */
111 1.1 darran if (st == NULL) {
112 1.6.16.1 pgoyette error = ENOENT;
113 1.6.16.1 pgoyette goto out;
114 1.1 darran }
115 1.1 darran
116 1.1 darran ctfaddr = st->sd_ctfstart;
117 1.1 darran ctfsize = st->sd_ctfsize;
118 1.1 darran
119 1.1 darran mc->symtab = st->sd_symstart;
120 1.1 darran mc->strtab = st->sd_strstart;
121 1.1 darran mc->strcnt = 0; /* XXX TBD */
122 1.1 darran mc->nsym = st->sd_symsize / sizeof(Elf_Sym);
123 1.1 darran } else {
124 1.3 darran if (kobj_find_section(mod->mod_kobj, ".SUNW_ctf", (void **)&ctfaddr, &ctfsize)) {
125 1.6.16.1 pgoyette error = ENOENT;
126 1.6.16.1 pgoyette goto out;
127 1.3 darran }
128 1.1 darran
129 1.1 darran mc->symtab = mod->mod_kobj->ko_symtab;
130 1.1 darran mc->strtab = mod->mod_kobj->ko_strtab;
131 1.1 darran mc->strcnt = 0; /* XXX TBD */
132 1.1 darran mc->nsym = mod->mod_kobj->ko_symcnt;
133 1.1 darran }
134 1.1 darran
135 1.1 darran if (ctfaddr == NULL) {
136 1.3 darran error = ENOENT;
137 1.1 darran goto out;
138 1.1 darran }
139 1.1 darran
140 1.1 darran /* Check the CTF magic number. (XXX check for big endian!) */
141 1.1 darran if (ctfaddr[0] != 0xf1 || ctfaddr[1] != 0xcf) {
142 1.3 darran error = EINVAL;
143 1.1 darran goto out;
144 1.1 darran }
145 1.1 darran
146 1.1 darran /* Check if version 2. */
147 1.3 darran if (ctfaddr[2] != 2) {
148 1.3 darran error = EINVAL;
149 1.1 darran goto out;
150 1.3 darran }
151 1.1 darran
152 1.1 darran /* Check if the data is compressed. */
153 1.1 darran if ((ctfaddr[3] & 0x1) != 0) {
154 1.1 darran uint32_t *u32 = (uint32_t *) ctfaddr;
155 1.1 darran
156 1.1 darran /*
157 1.1 darran * The last two fields in the CTF header are the offset
158 1.1 darran * from the end of the header to the start of the string
159 1.1 darran * data and the length of that string data. se this
160 1.1 darran * information to determine the decompressed CTF data
161 1.1 darran * buffer required.
162 1.1 darran */
163 1.1 darran sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
164 1.1 darran CTF_HDR_SIZE;
165 1.1 darran
166 1.1 darran compressed = 1;
167 1.1 darran } else {
168 1.1 darran /*
169 1.1 darran * The CTF data is not compressed, so the ELF section
170 1.1 darran * size is the same as the buffer size required.
171 1.1 darran */
172 1.1 darran sz = ctfsize;
173 1.1 darran }
174 1.1 darran
175 1.1 darran /*
176 1.5 snj * Allocate memory to buffer the CTF data in its decompressed
177 1.1 darran * form.
178 1.1 darran */
179 1.1 darran if (compressed) {
180 1.1 darran if ((ctfbuf = malloc(sz, M_TEMP, M_WAITOK)) == NULL) {
181 1.1 darran error = ENOMEM;
182 1.1 darran goto out;
183 1.1 darran }
184 1.1 darran ctftab = ctfbuf;
185 1.1 darran mc->ctfalloc = 1;
186 1.1 darran } else {
187 1.1 darran ctftab = (void *)ctfaddr;
188 1.1 darran }
189 1.1 darran
190 1.1 darran /* Check if decompression is required. */
191 1.1 darran if (compressed) {
192 1.1 darran z_stream zs;
193 1.1 darran int ret;
194 1.1 darran
195 1.1 darran /*
196 1.1 darran * The header isn't compressed, so copy that into the
197 1.1 darran * CTF buffer first.
198 1.1 darran */
199 1.1 darran memcpy(ctftab, ctfaddr, CTF_HDR_SIZE);
200 1.1 darran
201 1.1 darran /* Initialise the zlib structure. */
202 1.1 darran memset(&zs, 0, sizeof(zs));
203 1.1 darran zs.zalloc = z_alloc;
204 1.1 darran zs.zfree = z_free;
205 1.1 darran
206 1.1 darran if (inflateInit2(&zs, MAX_WBITS) != Z_OK) {
207 1.1 darran error = EIO;
208 1.1 darran goto out;
209 1.1 darran }
210 1.1 darran
211 1.1 darran zs.avail_in = ctfsize - CTF_HDR_SIZE;
212 1.4 christos zs.next_in = ctfaddr + CTF_HDR_SIZE;
213 1.1 darran zs.avail_out = sz - CTF_HDR_SIZE;
214 1.1 darran zs.next_out = ((uint8_t *) ctftab) + CTF_HDR_SIZE;
215 1.1 darran inflateReset(&zs);
216 1.1 darran if ((ret = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
217 1.1 darran printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
218 1.1 darran error = EIO;
219 1.1 darran goto out;
220 1.1 darran }
221 1.1 darran }
222 1.1 darran
223 1.1 darran /* Got the CTF data! */
224 1.1 darran mc->ctfcnt = ctfsize;
225 1.6.16.1 pgoyette mc->ctftab = ctftab;
226 1.1 darran ctfbuf = NULL;
227 1.1 darran
228 1.6.16.1 pgoyette module_setspecific(mod, fbt_module_key, mc);
229 1.6.16.1 pgoyette *mcp = mc;
230 1.6.16.1 pgoyette mc = NULL;
231 1.6.16.1 pgoyette
232 1.1 darran out:
233 1.1 darran if (ctfbuf != NULL)
234 1.1 darran free(ctfbuf, M_TEMP);
235 1.6.16.1 pgoyette if (mc != NULL)
236 1.6.16.1 pgoyette kmem_free(mc, sizeof(*mc));
237 1.1 darran
238 1.1 darran return (error);
239 1.1 darran }
240