1179224Sjb/*-
2179224Sjb * Copyright (c) 2008 John Birrell <jb@freebsd.org>
3179224Sjb * All rights reserved.
4179224Sjb *
5179224Sjb * Redistribution and use in source and binary forms, with or without
6179224Sjb * modification, are permitted provided that the following conditions
7179224Sjb * are met:
8179224Sjb * 1. Redistributions of source code must retain the above copyright
9179224Sjb *    notice, this list of conditions and the following disclaimer.
10179224Sjb * 2. Redistributions in binary form must reproduce the above copyright
11179224Sjb *    notice, this list of conditions and the following disclaimer in the
12179224Sjb *    documentation and/or other materials provided with the distribution.
13179224Sjb *
14179224Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15179224Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16179224Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17179224Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18179224Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19179224Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20179224Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21179224Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22179224Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23179224Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24179224Sjb * SUCH DAMAGE.
25179224Sjb *
26179224Sjb * $FreeBSD$
27179224Sjb */
28179224Sjb
29179224Sjb/*
30179224Sjb * Note this file is included by both link_elf.c and link_elf_obj.c.
31179224Sjb *
32179224Sjb * The CTF header structure definition can't be used here because it's
33179224Sjb * (annoyingly) covered by the CDDL. We will just use a few bytes from
34179224Sjb * it as an integer array where we 'know' what they mean.
35179224Sjb */
36179224Sjb#define CTF_HDR_SIZE		36
37179224Sjb#define CTF_HDR_STRTAB_U32	7
38179224Sjb#define CTF_HDR_STRLEN_U32	8
39179224Sjb
40179224Sjb#ifdef DDB_CTF
41179224Sjbstatic void *
42179224Sjbz_alloc(void *nil, u_int items, u_int size)
43179224Sjb{
44179224Sjb	void *ptr;
45179224Sjb
46179224Sjb	ptr = malloc(items * size, M_TEMP, M_NOWAIT);
47179224Sjb	return ptr;
48179224Sjb}
49179224Sjb
50179224Sjbstatic void
51179224Sjbz_free(void *nil, void *ptr)
52179224Sjb{
53179224Sjb	free(ptr, M_TEMP);
54179224Sjb}
55179224Sjb
56179224Sjb#endif
57179224Sjb
58179224Sjbstatic int
59179224Sjblink_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
60179224Sjb{
61179224Sjb#ifdef DDB_CTF
62179224Sjb	Elf_Ehdr *hdr = NULL;
63179224Sjb	Elf_Shdr *shdr = NULL;
64179224Sjb	caddr_t ctftab = NULL;
65179224Sjb	caddr_t raw = NULL;
66179224Sjb	caddr_t shstrtab = NULL;
67179224Sjb	elf_file_t ef = (elf_file_t) lf;
68179224Sjb	int flags;
69179224Sjb	int i;
70179224Sjb	int nbytes;
71179224Sjb	size_t sz;
72179224Sjb	struct nameidata nd;
73179224Sjb	struct thread *td = curthread;
74179224Sjb	uint8_t ctf_hdr[CTF_HDR_SIZE];
75179224Sjb#endif
76179224Sjb	int error = 0;
77179224Sjb
78179224Sjb	if (lf == NULL || lc == NULL)
79179224Sjb		return (EINVAL);
80179224Sjb
81179224Sjb	/* Set the defaults for no CTF present. That's not a crime! */
82179224Sjb	bzero(lc, sizeof(*lc));
83179224Sjb
84179224Sjb#ifdef DDB_CTF
85179224Sjb	/*
86179224Sjb	 * First check if we've tried to load CTF data previously and the
87179224Sjb	 * CTF ELF section wasn't found. We flag that condition by setting
88179224Sjb	 * ctfcnt to -1. See below.
89179224Sjb	 */
90179224Sjb	if (ef->ctfcnt < 0)
91227342Srstone		return (EFTYPE);
92179224Sjb
93179224Sjb	/* Now check if we've already loaded the CTF data.. */
94179224Sjb	if (ef->ctfcnt > 0) {
95179224Sjb		/* We only need to load once. */
96179224Sjb		lc->ctftab = ef->ctftab;
97179224Sjb		lc->ctfcnt = ef->ctfcnt;
98179224Sjb		lc->symtab = ef->ddbsymtab;
99179224Sjb		lc->strtab = ef->ddbstrtab;
100179224Sjb		lc->strcnt = ef->ddbstrcnt;
101179224Sjb		lc->nsym   = ef->ddbsymcnt;
102179224Sjb		lc->ctfoffp = (uint32_t **) &ef->ctfoff;
103179224Sjb		lc->typoffp = (uint32_t **) &ef->typoff;
104179224Sjb		lc->typlenp = &ef->typlen;
105179224Sjb		return (0);
106179224Sjb	}
107179224Sjb
108179224Sjb	/*
109179224Sjb	 * We need to try reading the CTF data. Flag no CTF data present
110179224Sjb	 * by default and if we actually succeed in reading it, we'll
111179224Sjb	 * update ctfcnt to the number of bytes read.
112179224Sjb	 */
113179224Sjb	ef->ctfcnt = -1;
114179224Sjb
115241896Skib	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname, td);
116179224Sjb	flags = FREAD;
117179224Sjb	error = vn_open(&nd, &flags, 0, NULL);
118179224Sjb	if (error)
119179224Sjb		return (error);
120179224Sjb	NDFREE(&nd, NDF_ONLY_PNBUF);
121179224Sjb
122179224Sjb	/* Allocate memory for the FLF header. */
123278984Smarkj	hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
124179224Sjb
125179224Sjb	/* Read the ELF header. */
126179224Sjb	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
127279089Smarkj	    0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
128179224Sjb	    td)) != 0)
129179224Sjb		goto out;
130179224Sjb
131179224Sjb	/* Sanity check. */
132179224Sjb	if (!IS_ELF(*hdr)) {
133179224Sjb		error = ENOEXEC;
134179224Sjb		goto out;
135179224Sjb	}
136179224Sjb
137179224Sjb	nbytes = hdr->e_shnum * hdr->e_shentsize;
138179224Sjb	if (nbytes == 0 || hdr->e_shoff == 0 ||
139179224Sjb	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
140179224Sjb		error = ENOEXEC;
141179224Sjb		goto out;
142179224Sjb	}
143179224Sjb
144179224Sjb	/* Allocate memory for all the section headers */
145278984Smarkj	shdr = malloc(nbytes, M_LINKER, M_WAITOK);
146179224Sjb
147179224Sjb	/* Read all the section headers */
148179224Sjb	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
149179224Sjb	    hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
150279089Smarkj	    NULL, td)) != 0)
151179224Sjb		goto out;
152179224Sjb
153179224Sjb	/*
154179224Sjb	 * We need to search for the CTF section by name, so if the
155179224Sjb	 * section names aren't present, then we can't locate the
156179224Sjb	 * .SUNW_ctf section containing the CTF data.
157179224Sjb	 */
158226082Sdelphij	if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
159226082Sdelphij		printf("%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
160226082Sdelphij		    __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
161226082Sdelphij		    shdr[hdr->e_shstrndx].sh_type);
162226082Sdelphij		error = EFTYPE;
163179224Sjb		goto out;
164226082Sdelphij	}
165179224Sjb
166179224Sjb	/* Allocate memory to buffer the section header strings. */
167278984Smarkj	shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
168179224Sjb
169179224Sjb	/* Read the section header strings. */
170179224Sjb	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
171179224Sjb	    shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
172279089Smarkj	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
173179224Sjb		goto out;
174179224Sjb
175179224Sjb	/* Search for the section containing the CTF data. */
176179224Sjb	for (i = 0; i < hdr->e_shnum; i++)
177179224Sjb		if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
178179224Sjb			break;
179179224Sjb
180179224Sjb	/* Check if the CTF section wasn't found. */
181226082Sdelphij	if (i >= hdr->e_shnum) {
182226082Sdelphij		printf("%s(%d): module %s has no .SUNW_ctf section\n",
183226082Sdelphij		    __func__, __LINE__, lf->pathname);
184226082Sdelphij		error = EFTYPE;
185179224Sjb		goto out;
186226082Sdelphij	}
187179224Sjb
188179224Sjb	/* Read the CTF header. */
189179224Sjb	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, ctf_hdr, sizeof(ctf_hdr),
190179224Sjb	    shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
191279089Smarkj	    NOCRED, NULL, td)) != 0)
192179224Sjb		goto out;
193179224Sjb
194179224Sjb	/* Check the CTF magic number. (XXX check for big endian!) */
195226082Sdelphij	if (ctf_hdr[0] != 0xf1 || ctf_hdr[1] != 0xcf) {
196226082Sdelphij		printf("%s(%d): module %s has invalid format\n",
197226082Sdelphij		    __func__, __LINE__, lf->pathname);
198226082Sdelphij		error = EFTYPE;
199179224Sjb		goto out;
200226082Sdelphij	}
201179224Sjb
202179224Sjb	/* Check if version 2. */
203226082Sdelphij	if (ctf_hdr[2] != 2) {
204226082Sdelphij		printf("%s(%d): module %s CTF format version is %d "
205226082Sdelphij		    "(2 expected)\n",
206226082Sdelphij		    __func__, __LINE__, lf->pathname, ctf_hdr[2]);
207226082Sdelphij		error = EFTYPE;
208179224Sjb		goto out;
209226082Sdelphij	}
210179224Sjb
211179224Sjb	/* Check if the data is compressed. */
212179224Sjb	if ((ctf_hdr[3] & 0x1) != 0) {
213179224Sjb		uint32_t *u32 = (uint32_t *) ctf_hdr;
214179224Sjb
215179224Sjb		/*
216179224Sjb		 * The last two fields in the CTF header are the offset
217179224Sjb		 * from the end of the header to the start of the string
218179224Sjb		 * data and the length of that string data. se this
219179224Sjb		 * information to determine the decompressed CTF data
220179224Sjb		 * buffer required.
221179224Sjb		 */
222179224Sjb		sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
223179224Sjb		    sizeof(ctf_hdr);
224179224Sjb
225179224Sjb		/*
226179224Sjb		 * Allocate memory for the compressed CTF data, including
227179224Sjb		 * the header (which isn't compressed).
228179224Sjb		 */
229278984Smarkj		raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
230179224Sjb	} else {
231179224Sjb		/*
232179224Sjb		 * The CTF data is not compressed, so the ELF section
233179224Sjb		 * size is the same as the buffer size required.
234179224Sjb		 */
235179224Sjb		sz = shdr[i].sh_size;
236179224Sjb	}
237179224Sjb
238179224Sjb	/*
239179224Sjb	 * Allocate memory to buffer the CTF data in it's decompressed
240179224Sjb	 * form.
241179224Sjb	 */
242278984Smarkj	ctftab = malloc(sz, M_LINKER, M_WAITOK);
243179224Sjb
244179224Sjb	/*
245179224Sjb	 * Read the CTF data into the raw buffer if compressed, or
246179224Sjb	 * directly into the CTF buffer otherwise.
247179224Sjb	 */
248179224Sjb	if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
249179224Sjb	    shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
250279089Smarkj	    td->td_ucred, NOCRED, NULL, td)) != 0)
251179224Sjb		goto out;
252179224Sjb
253179224Sjb	/* Check if decompression is required. */
254179224Sjb	if (raw != NULL) {
255179224Sjb		z_stream zs;
256179224Sjb		int ret;
257179224Sjb
258179224Sjb		/*
259179224Sjb		 * The header isn't compressed, so copy that into the
260179224Sjb		 * CTF buffer first.
261179224Sjb		 */
262179224Sjb		bcopy(ctf_hdr, ctftab, sizeof(ctf_hdr));
263179224Sjb
264179224Sjb		/* Initialise the zlib structure. */
265179224Sjb		bzero(&zs, sizeof(zs));
266179224Sjb		zs.zalloc = z_alloc;
267179224Sjb		zs.zfree = z_free;
268179224Sjb
269179224Sjb		if (inflateInit(&zs) != Z_OK) {
270179224Sjb			error = EIO;
271179224Sjb			goto out;
272179224Sjb		}
273179224Sjb
274179224Sjb		zs.avail_in = shdr[i].sh_size - sizeof(ctf_hdr);
275179224Sjb		zs.next_in = ((uint8_t *) raw) + sizeof(ctf_hdr);
276179224Sjb		zs.avail_out = sz - sizeof(ctf_hdr);
277179224Sjb		zs.next_out = ((uint8_t *) ctftab) + sizeof(ctf_hdr);
278278983Smarkj		ret = inflate(&zs, Z_FINISH);
279278983Smarkj		inflateEnd(&zs);
280278983Smarkj		if (ret != Z_STREAM_END) {
281179224Sjb			printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
282179224Sjb			error = EIO;
283179224Sjb			goto out;
284179224Sjb		}
285179224Sjb	}
286179224Sjb
287179224Sjb	/* Got the CTF data! */
288179224Sjb	ef->ctftab = ctftab;
289179224Sjb	ef->ctfcnt = shdr[i].sh_size;
290179224Sjb
291179224Sjb	/* We'll retain the memory allocated for the CTF data. */
292179224Sjb	ctftab = NULL;
293179224Sjb
294179224Sjb	/* Let the caller use the CTF data read. */
295179224Sjb	lc->ctftab = ef->ctftab;
296179224Sjb	lc->ctfcnt = ef->ctfcnt;
297179224Sjb	lc->symtab = ef->ddbsymtab;
298179224Sjb	lc->strtab = ef->ddbstrtab;
299179224Sjb	lc->strcnt = ef->ddbstrcnt;
300179224Sjb	lc->nsym   = ef->ddbsymcnt;
301179224Sjb	lc->ctfoffp = (uint32_t **) &ef->ctfoff;
302179224Sjb	lc->typoffp = (uint32_t **) &ef->typoff;
303179224Sjb	lc->typlenp = &ef->typlen;
304179224Sjb
305179224Sjbout:
306179224Sjb	VOP_UNLOCK(nd.ni_vp, 0);
307179224Sjb	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
308179224Sjb
309179224Sjb	if (hdr != NULL)
310179224Sjb		free(hdr, M_LINKER);
311179224Sjb	if (shdr != NULL)
312179224Sjb		free(shdr, M_LINKER);
313179224Sjb	if (shstrtab != NULL)
314179224Sjb		free(shstrtab, M_LINKER);
315179224Sjb	if (ctftab != NULL)
316179224Sjb		free(ctftab, M_LINKER);
317179224Sjb	if (raw != NULL)
318179224Sjb		free(raw, M_LINKER);
319179224Sjb#else
320179224Sjb	error = EOPNOTSUPP;
321179224Sjb#endif
322179224Sjb
323179224Sjb	return (error);
324179224Sjb}
325