1/*-
2 * Copyright (c) 2008 John Birrell <jb@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29/*
30 * Note this file is included by both link_elf.c and link_elf_obj.c.
31 *
32 * The CTF header structure definition can't be used here because it's
33 * (annoyingly) covered by the CDDL. We will just use a few bytes from
34 * it as an integer array where we 'know' what they mean.
35 */
36#define CTF_HDR_SIZE		36
37#define CTF_HDR_STRTAB_U32	7
38#define CTF_HDR_STRLEN_U32	8
39
40#ifdef DDB_CTF
41static void *
42z_alloc(void *nil, u_int items, u_int size)
43{
44	void *ptr;
45
46	ptr = malloc(items * size, M_TEMP, M_NOWAIT);
47	return ptr;
48}
49
50static void
51z_free(void *nil, void *ptr)
52{
53	free(ptr, M_TEMP);
54}
55
56#endif
57
58static int
59link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
60{
61#ifdef DDB_CTF
62	Elf_Ehdr *hdr = NULL;
63	Elf_Shdr *shdr = NULL;
64	caddr_t ctftab = NULL;
65	caddr_t raw = NULL;
66	caddr_t shstrtab = NULL;
67	elf_file_t ef = (elf_file_t) lf;
68	int flags;
69	int i;
70	int nbytes;
71	size_t sz;
72	struct nameidata nd;
73	struct thread *td = curthread;
74	uint8_t ctf_hdr[CTF_HDR_SIZE];
75#endif
76	int error = 0;
77
78	if (lf == NULL || lc == NULL)
79		return (EINVAL);
80
81	/* Set the defaults for no CTF present. That's not a crime! */
82	bzero(lc, sizeof(*lc));
83
84#ifdef DDB_CTF
85	/*
86	 * First check if we've tried to load CTF data previously and the
87	 * CTF ELF section wasn't found. We flag that condition by setting
88	 * ctfcnt to -1. See below.
89	 */
90	if (ef->ctfcnt < 0)
91		return (EFTYPE);
92
93	/* Now check if we've already loaded the CTF data.. */
94	if (ef->ctfcnt > 0) {
95		/* We only need to load once. */
96		lc->ctftab = ef->ctftab;
97		lc->ctfcnt = ef->ctfcnt;
98		lc->symtab = ef->ddbsymtab;
99		lc->strtab = ef->ddbstrtab;
100		lc->strcnt = ef->ddbstrcnt;
101		lc->nsym   = ef->ddbsymcnt;
102		lc->ctfoffp = (uint32_t **) &ef->ctfoff;
103		lc->typoffp = (uint32_t **) &ef->typoff;
104		lc->typlenp = &ef->typlen;
105		return (0);
106	}
107
108	/*
109	 * We need to try reading the CTF data. Flag no CTF data present
110	 * by default and if we actually succeed in reading it, we'll
111	 * update ctfcnt to the number of bytes read.
112	 */
113	ef->ctfcnt = -1;
114
115	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname, td);
116	flags = FREAD;
117	error = vn_open(&nd, &flags, 0, NULL);
118	if (error)
119		return (error);
120	NDFREE(&nd, NDF_ONLY_PNBUF);
121
122	/* Allocate memory for the FLF header. */
123	hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
124
125	/* Read the ELF header. */
126	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
127	    0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
128	    td)) != 0)
129		goto out;
130
131	/* Sanity check. */
132	if (!IS_ELF(*hdr)) {
133		error = ENOEXEC;
134		goto out;
135	}
136
137	nbytes = hdr->e_shnum * hdr->e_shentsize;
138	if (nbytes == 0 || hdr->e_shoff == 0 ||
139	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
140		error = ENOEXEC;
141		goto out;
142	}
143
144	/* Allocate memory for all the section headers */
145	shdr = malloc(nbytes, M_LINKER, M_WAITOK);
146
147	/* Read all the section headers */
148	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
149	    hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
150	    NULL, td)) != 0)
151		goto out;
152
153	/*
154	 * We need to search for the CTF section by name, so if the
155	 * section names aren't present, then we can't locate the
156	 * .SUNW_ctf section containing the CTF data.
157	 */
158	if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
159		printf("%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
160		    __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
161		    shdr[hdr->e_shstrndx].sh_type);
162		error = EFTYPE;
163		goto out;
164	}
165
166	/* Allocate memory to buffer the section header strings. */
167	shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
168
169	/* Read the section header strings. */
170	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
171	    shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
172	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
173		goto out;
174
175	/* Search for the section containing the CTF data. */
176	for (i = 0; i < hdr->e_shnum; i++)
177		if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
178			break;
179
180	/* Check if the CTF section wasn't found. */
181	if (i >= hdr->e_shnum) {
182		printf("%s(%d): module %s has no .SUNW_ctf section\n",
183		    __func__, __LINE__, lf->pathname);
184		error = EFTYPE;
185		goto out;
186	}
187
188	/* Read the CTF header. */
189	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, ctf_hdr, sizeof(ctf_hdr),
190	    shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
191	    NOCRED, NULL, td)) != 0)
192		goto out;
193
194	/* Check the CTF magic number. (XXX check for big endian!) */
195	if (ctf_hdr[0] != 0xf1 || ctf_hdr[1] != 0xcf) {
196		printf("%s(%d): module %s has invalid format\n",
197		    __func__, __LINE__, lf->pathname);
198		error = EFTYPE;
199		goto out;
200	}
201
202	/* Check if version 2. */
203	if (ctf_hdr[2] != 2) {
204		printf("%s(%d): module %s CTF format version is %d "
205		    "(2 expected)\n",
206		    __func__, __LINE__, lf->pathname, ctf_hdr[2]);
207		error = EFTYPE;
208		goto out;
209	}
210
211	/* Check if the data is compressed. */
212	if ((ctf_hdr[3] & 0x1) != 0) {
213		uint32_t *u32 = (uint32_t *) ctf_hdr;
214
215		/*
216		 * The last two fields in the CTF header are the offset
217		 * from the end of the header to the start of the string
218		 * data and the length of that string data. se this
219		 * information to determine the decompressed CTF data
220		 * buffer required.
221		 */
222		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
223		    sizeof(ctf_hdr);
224
225		/*
226		 * Allocate memory for the compressed CTF data, including
227		 * the header (which isn't compressed).
228		 */
229		raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
230	} else {
231		/*
232		 * The CTF data is not compressed, so the ELF section
233		 * size is the same as the buffer size required.
234		 */
235		sz = shdr[i].sh_size;
236	}
237
238	/*
239	 * Allocate memory to buffer the CTF data in it's decompressed
240	 * form.
241	 */
242	ctftab = malloc(sz, M_LINKER, M_WAITOK);
243
244	/*
245	 * Read the CTF data into the raw buffer if compressed, or
246	 * directly into the CTF buffer otherwise.
247	 */
248	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
249	    shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
250	    td->td_ucred, NOCRED, NULL, td)) != 0)
251		goto out;
252
253	/* Check if decompression is required. */
254	if (raw != NULL) {
255		z_stream zs;
256		int ret;
257
258		/*
259		 * The header isn't compressed, so copy that into the
260		 * CTF buffer first.
261		 */
262		bcopy(ctf_hdr, ctftab, sizeof(ctf_hdr));
263
264		/* Initialise the zlib structure. */
265		bzero(&zs, sizeof(zs));
266		zs.zalloc = z_alloc;
267		zs.zfree = z_free;
268
269		if (inflateInit(&zs) != Z_OK) {
270			error = EIO;
271			goto out;
272		}
273
274		zs.avail_in = shdr[i].sh_size - sizeof(ctf_hdr);
275		zs.next_in = ((uint8_t *) raw) + sizeof(ctf_hdr);
276		zs.avail_out = sz - sizeof(ctf_hdr);
277		zs.next_out = ((uint8_t *) ctftab) + sizeof(ctf_hdr);
278		ret = inflate(&zs, Z_FINISH);
279		inflateEnd(&zs);
280		if (ret != Z_STREAM_END) {
281			printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
282			error = EIO;
283			goto out;
284		}
285	}
286
287	/* Got the CTF data! */
288	ef->ctftab = ctftab;
289	ef->ctfcnt = shdr[i].sh_size;
290
291	/* We'll retain the memory allocated for the CTF data. */
292	ctftab = NULL;
293
294	/* Let the caller use the CTF data read. */
295	lc->ctftab = ef->ctftab;
296	lc->ctfcnt = ef->ctfcnt;
297	lc->symtab = ef->ddbsymtab;
298	lc->strtab = ef->ddbstrtab;
299	lc->strcnt = ef->ddbstrcnt;
300	lc->nsym   = ef->ddbsymcnt;
301	lc->ctfoffp = (uint32_t **) &ef->ctfoff;
302	lc->typoffp = (uint32_t **) &ef->typoff;
303	lc->typlenp = &ef->typlen;
304
305out:
306	VOP_UNLOCK(nd.ni_vp, 0);
307	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
308
309	if (hdr != NULL)
310		free(hdr, M_LINKER);
311	if (shdr != NULL)
312		free(shdr, M_LINKER);
313	if (shstrtab != NULL)
314		free(shstrtab, M_LINKER);
315	if (ctftab != NULL)
316		free(ctftab, M_LINKER);
317	if (raw != NULL)
318		free(raw, M_LINKER);
319#else
320	error = EOPNOTSUPP;
321#endif
322
323	return (error);
324}
325