175d94ef6SJohn Birrell /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni *
475d94ef6SJohn Birrell * Copyright (c) 2008 John Birrell <jb@freebsd.org>
575d94ef6SJohn Birrell * All rights reserved.
675d94ef6SJohn Birrell *
775d94ef6SJohn Birrell * Redistribution and use in source and binary forms, with or without
875d94ef6SJohn Birrell * modification, are permitted provided that the following conditions
975d94ef6SJohn Birrell * are met:
1075d94ef6SJohn Birrell * 1. Redistributions of source code must retain the above copyright
1175d94ef6SJohn Birrell * notice, this list of conditions and the following disclaimer.
1275d94ef6SJohn Birrell * 2. Redistributions in binary form must reproduce the above copyright
1375d94ef6SJohn Birrell * notice, this list of conditions and the following disclaimer in the
1475d94ef6SJohn Birrell * documentation and/or other materials provided with the distribution.
1575d94ef6SJohn Birrell *
1675d94ef6SJohn Birrell * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1775d94ef6SJohn Birrell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1875d94ef6SJohn Birrell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1975d94ef6SJohn Birrell * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2075d94ef6SJohn Birrell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2175d94ef6SJohn Birrell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2275d94ef6SJohn Birrell * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2375d94ef6SJohn Birrell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2475d94ef6SJohn Birrell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2575d94ef6SJohn Birrell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2675d94ef6SJohn Birrell * SUCH DAMAGE.
2775d94ef6SJohn Birrell */
2875d94ef6SJohn Birrell
29cab9382aSMark Johnston #include <sys/ctf.h>
30c21bc6f3SBojan Novković #include <sys/kdb.h>
31c21bc6f3SBojan Novković #include <sys/linker.h>
32c21bc6f3SBojan Novković
33c21bc6f3SBojan Novković #include <ddb/db_ctf.h>
34cab9382aSMark Johnston
3575d94ef6SJohn Birrell /*
3675d94ef6SJohn Birrell * Note this file is included by both link_elf.c and link_elf_obj.c.
3775d94ef6SJohn Birrell */
3875d94ef6SJohn Birrell
3975d94ef6SJohn Birrell #ifdef DDB_CTF
4022bbc4b2SXin LI #include <contrib/zlib/zlib.h>
4175d94ef6SJohn Birrell #endif
4275d94ef6SJohn Birrell
4375d94ef6SJohn Birrell static int
link_elf_ctf_get(linker_file_t lf,linker_ctf_t * lc)4475d94ef6SJohn Birrell link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
4575d94ef6SJohn Birrell {
4675d94ef6SJohn Birrell #ifdef DDB_CTF
4775d94ef6SJohn Birrell Elf_Ehdr *hdr = NULL;
4875d94ef6SJohn Birrell Elf_Shdr *shdr = NULL;
4975d94ef6SJohn Birrell caddr_t ctftab = NULL;
5075d94ef6SJohn Birrell caddr_t raw = NULL;
5175d94ef6SJohn Birrell caddr_t shstrtab = NULL;
5275d94ef6SJohn Birrell elf_file_t ef = (elf_file_t) lf;
5375d94ef6SJohn Birrell int flags;
5475d94ef6SJohn Birrell int i;
5575d94ef6SJohn Birrell int nbytes;
5675d94ef6SJohn Birrell size_t sz;
5775d94ef6SJohn Birrell struct nameidata nd;
5875d94ef6SJohn Birrell struct thread *td = curthread;
59cab9382aSMark Johnston struct ctf_header cth;
6075d94ef6SJohn Birrell #endif
6175d94ef6SJohn Birrell int error = 0;
6275d94ef6SJohn Birrell
6375d94ef6SJohn Birrell if (lf == NULL || lc == NULL)
6475d94ef6SJohn Birrell return (EINVAL);
6575d94ef6SJohn Birrell
6675d94ef6SJohn Birrell /* Set the defaults for no CTF present. That's not a crime! */
6775d94ef6SJohn Birrell bzero(lc, sizeof(*lc));
6875d94ef6SJohn Birrell
6975d94ef6SJohn Birrell #ifdef DDB_CTF
7075d94ef6SJohn Birrell /*
7175d94ef6SJohn Birrell * First check if we've tried to load CTF data previously and the
7275d94ef6SJohn Birrell * CTF ELF section wasn't found. We flag that condition by setting
7375d94ef6SJohn Birrell * ctfcnt to -1. See below.
7475d94ef6SJohn Birrell */
7575d94ef6SJohn Birrell if (ef->ctfcnt < 0)
766f6924e5SRyan Stone return (EFTYPE);
7775d94ef6SJohn Birrell
7875d94ef6SJohn Birrell /* Now check if we've already loaded the CTF data.. */
7975d94ef6SJohn Birrell if (ef->ctfcnt > 0) {
8075d94ef6SJohn Birrell /* We only need to load once. */
8175d94ef6SJohn Birrell lc->ctftab = ef->ctftab;
8275d94ef6SJohn Birrell lc->ctfcnt = ef->ctfcnt;
8375d94ef6SJohn Birrell lc->symtab = ef->ddbsymtab;
8475d94ef6SJohn Birrell lc->strtab = ef->ddbstrtab;
8575d94ef6SJohn Birrell lc->strcnt = ef->ddbstrcnt;
8675d94ef6SJohn Birrell lc->nsym = ef->ddbsymcnt;
8775d94ef6SJohn Birrell lc->ctfoffp = (uint32_t **) &ef->ctfoff;
8875d94ef6SJohn Birrell lc->typoffp = (uint32_t **) &ef->typoff;
8975d94ef6SJohn Birrell lc->typlenp = &ef->typlen;
9075d94ef6SJohn Birrell return (0);
9175d94ef6SJohn Birrell }
9275d94ef6SJohn Birrell
93c21bc6f3SBojan Novković if (panicstr != NULL || kdb_active)
94c21bc6f3SBojan Novković return (ENXIO);
95c21bc6f3SBojan Novković
9675d94ef6SJohn Birrell /*
9775d94ef6SJohn Birrell * We need to try reading the CTF data. Flag no CTF data present
9875d94ef6SJohn Birrell * by default and if we actually succeed in reading it, we'll
9975d94ef6SJohn Birrell * update ctfcnt to the number of bytes read.
10075d94ef6SJohn Birrell */
10175d94ef6SJohn Birrell ef->ctfcnt = -1;
10275d94ef6SJohn Birrell
1037e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname);
10475d94ef6SJohn Birrell flags = FREAD;
10575d94ef6SJohn Birrell error = vn_open(&nd, &flags, 0, NULL);
10675d94ef6SJohn Birrell if (error)
10775d94ef6SJohn Birrell return (error);
108bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd);
10975d94ef6SJohn Birrell
11075d94ef6SJohn Birrell /* Allocate memory for the FLF header. */
111ce47682cSMark Johnston hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
11275d94ef6SJohn Birrell
11375d94ef6SJohn Birrell /* Read the ELF header. */
11475d94ef6SJohn Birrell if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
1157abb0b09SMark Johnston 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
11675d94ef6SJohn Birrell td)) != 0)
11775d94ef6SJohn Birrell goto out;
11875d94ef6SJohn Birrell
11975d94ef6SJohn Birrell /* Sanity check. */
12075d94ef6SJohn Birrell if (!IS_ELF(*hdr)) {
12175d94ef6SJohn Birrell error = ENOEXEC;
12275d94ef6SJohn Birrell goto out;
12375d94ef6SJohn Birrell }
12475d94ef6SJohn Birrell
12575d94ef6SJohn Birrell nbytes = hdr->e_shnum * hdr->e_shentsize;
12675d94ef6SJohn Birrell if (nbytes == 0 || hdr->e_shoff == 0 ||
12775d94ef6SJohn Birrell hdr->e_shentsize != sizeof(Elf_Shdr)) {
12875d94ef6SJohn Birrell error = ENOEXEC;
12975d94ef6SJohn Birrell goto out;
13075d94ef6SJohn Birrell }
13175d94ef6SJohn Birrell
13275d94ef6SJohn Birrell /* Allocate memory for all the section headers */
133ce47682cSMark Johnston shdr = malloc(nbytes, M_LINKER, M_WAITOK);
13475d94ef6SJohn Birrell
13575d94ef6SJohn Birrell /* Read all the section headers */
13675d94ef6SJohn Birrell if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
13775d94ef6SJohn Birrell hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1387abb0b09SMark Johnston NULL, td)) != 0)
13975d94ef6SJohn Birrell goto out;
14075d94ef6SJohn Birrell
14175d94ef6SJohn Birrell /*
14275d94ef6SJohn Birrell * We need to search for the CTF section by name, so if the
14375d94ef6SJohn Birrell * section names aren't present, then we can't locate the
14475d94ef6SJohn Birrell * .SUNW_ctf section containing the CTF data.
14575d94ef6SJohn Birrell */
1462b03effaSXin LI if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
147*bdc90346SBojan Novković if (bootverbose) {
148*bdc90346SBojan Novković printf(
149*bdc90346SBojan Novković "%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
1502b03effaSXin LI __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
1512b03effaSXin LI shdr[hdr->e_shstrndx].sh_type);
152*bdc90346SBojan Novković }
1532b03effaSXin LI error = EFTYPE;
15475d94ef6SJohn Birrell goto out;
1552b03effaSXin LI }
15675d94ef6SJohn Birrell
15775d94ef6SJohn Birrell /* Allocate memory to buffer the section header strings. */
158ce47682cSMark Johnston shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
15975d94ef6SJohn Birrell
16075d94ef6SJohn Birrell /* Read the section header strings. */
16175d94ef6SJohn Birrell if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
16275d94ef6SJohn Birrell shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
1637abb0b09SMark Johnston UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
16475d94ef6SJohn Birrell goto out;
16575d94ef6SJohn Birrell
16675d94ef6SJohn Birrell /* Search for the section containing the CTF data. */
16775d94ef6SJohn Birrell for (i = 0; i < hdr->e_shnum; i++)
16875d94ef6SJohn Birrell if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
16975d94ef6SJohn Birrell break;
17075d94ef6SJohn Birrell
17175d94ef6SJohn Birrell /* Check if the CTF section wasn't found. */
1722b03effaSXin LI if (i >= hdr->e_shnum) {
173*bdc90346SBojan Novković if (bootverbose) {
1742b03effaSXin LI printf("%s(%d): module %s has no .SUNW_ctf section\n",
1752b03effaSXin LI __func__, __LINE__, lf->pathname);
176*bdc90346SBojan Novković }
1772b03effaSXin LI error = EFTYPE;
17875d94ef6SJohn Birrell goto out;
1792b03effaSXin LI }
18075d94ef6SJohn Birrell
18175d94ef6SJohn Birrell /* Read the CTF header. */
182cab9382aSMark Johnston if ((error = vn_rdwr(UIO_READ, nd.ni_vp, &cth, sizeof(cth),
18375d94ef6SJohn Birrell shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1847abb0b09SMark Johnston NOCRED, NULL, td)) != 0)
18575d94ef6SJohn Birrell goto out;
18675d94ef6SJohn Birrell
187a5868885SJustin Hibbits /* Check the CTF magic number. */
188cab9382aSMark Johnston if (cth.cth_magic != CTF_MAGIC) {
189*bdc90346SBojan Novković if (bootverbose) {
1902b03effaSXin LI printf("%s(%d): module %s has invalid format\n",
1912b03effaSXin LI __func__, __LINE__, lf->pathname);
192*bdc90346SBojan Novković }
1932b03effaSXin LI error = EFTYPE;
19475d94ef6SJohn Birrell goto out;
1952b03effaSXin LI }
19675d94ef6SJohn Birrell
1978dbae4ceSMark Johnston if (cth.cth_version != CTF_VERSION_2 &&
1988dbae4ceSMark Johnston cth.cth_version != CTF_VERSION_3) {
199*bdc90346SBojan Novković if (bootverbose) {
2008dbae4ceSMark Johnston printf(
2018dbae4ceSMark Johnston "%s(%d): module %s CTF format has unsupported version %d\n",
202cab9382aSMark Johnston __func__, __LINE__, lf->pathname, cth.cth_version);
203*bdc90346SBojan Novković }
2042b03effaSXin LI error = EFTYPE;
20575d94ef6SJohn Birrell goto out;
2062b03effaSXin LI }
20775d94ef6SJohn Birrell
20875d94ef6SJohn Birrell /* Check if the data is compressed. */
209cab9382aSMark Johnston if ((cth.cth_flags & CTF_F_COMPRESS) != 0) {
21075d94ef6SJohn Birrell /*
21175d94ef6SJohn Birrell * The last two fields in the CTF header are the offset
21275d94ef6SJohn Birrell * from the end of the header to the start of the string
213cab9382aSMark Johnston * data and the length of that string data. Use this
21475d94ef6SJohn Birrell * information to determine the decompressed CTF data
21575d94ef6SJohn Birrell * buffer required.
21675d94ef6SJohn Birrell */
217cab9382aSMark Johnston sz = cth.cth_stroff + cth.cth_strlen + sizeof(cth);
21875d94ef6SJohn Birrell
21975d94ef6SJohn Birrell /*
22075d94ef6SJohn Birrell * Allocate memory for the compressed CTF data, including
22175d94ef6SJohn Birrell * the header (which isn't compressed).
22275d94ef6SJohn Birrell */
223ce47682cSMark Johnston raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
22475d94ef6SJohn Birrell } else {
22575d94ef6SJohn Birrell /*
22675d94ef6SJohn Birrell * The CTF data is not compressed, so the ELF section
22775d94ef6SJohn Birrell * size is the same as the buffer size required.
22875d94ef6SJohn Birrell */
22975d94ef6SJohn Birrell sz = shdr[i].sh_size;
23075d94ef6SJohn Birrell }
23175d94ef6SJohn Birrell
23275d94ef6SJohn Birrell /*
23328323addSBryan Drewery * Allocate memory to buffer the CTF data in its decompressed
23475d94ef6SJohn Birrell * form.
23575d94ef6SJohn Birrell */
236ce47682cSMark Johnston ctftab = malloc(sz, M_LINKER, M_WAITOK);
23775d94ef6SJohn Birrell
23875d94ef6SJohn Birrell /*
23975d94ef6SJohn Birrell * Read the CTF data into the raw buffer if compressed, or
24075d94ef6SJohn Birrell * directly into the CTF buffer otherwise.
24175d94ef6SJohn Birrell */
24275d94ef6SJohn Birrell if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
24375d94ef6SJohn Birrell shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
2447abb0b09SMark Johnston td->td_ucred, NOCRED, NULL, td)) != 0)
24575d94ef6SJohn Birrell goto out;
24675d94ef6SJohn Birrell
24775d94ef6SJohn Birrell /* Check if decompression is required. */
24875d94ef6SJohn Birrell if (raw != NULL) {
249cb17f4a6SYoshihiro Ota uLongf destlen;
25075d94ef6SJohn Birrell int ret;
25175d94ef6SJohn Birrell
25275d94ef6SJohn Birrell /*
25375d94ef6SJohn Birrell * The header isn't compressed, so copy that into the
25475d94ef6SJohn Birrell * CTF buffer first.
25575d94ef6SJohn Birrell */
256cab9382aSMark Johnston bcopy(&cth, ctftab, sizeof(cth));
25775d94ef6SJohn Birrell
258cab9382aSMark Johnston destlen = sz - sizeof(cth);
259cab9382aSMark Johnston ret = uncompress(ctftab + sizeof(cth), &destlen,
260cab9382aSMark Johnston raw + sizeof(cth), shdr[i].sh_size - sizeof(cth));
261cb17f4a6SYoshihiro Ota if (ret != Z_OK) {
262*bdc90346SBojan Novković if (bootverbose) {
263cab9382aSMark Johnston printf("%s(%d): zlib uncompress returned %d\n",
264cab9382aSMark Johnston __func__, __LINE__, ret);
265*bdc90346SBojan Novković }
26675d94ef6SJohn Birrell error = EIO;
26775d94ef6SJohn Birrell goto out;
26875d94ef6SJohn Birrell }
26975d94ef6SJohn Birrell }
27075d94ef6SJohn Birrell
27175d94ef6SJohn Birrell /* Got the CTF data! */
27275d94ef6SJohn Birrell ef->ctftab = ctftab;
27375d94ef6SJohn Birrell ef->ctfcnt = shdr[i].sh_size;
27475d94ef6SJohn Birrell
27575d94ef6SJohn Birrell /* We'll retain the memory allocated for the CTF data. */
27675d94ef6SJohn Birrell ctftab = NULL;
27775d94ef6SJohn Birrell
27875d94ef6SJohn Birrell /* Let the caller use the CTF data read. */
27975d94ef6SJohn Birrell lc->ctftab = ef->ctftab;
28075d94ef6SJohn Birrell lc->ctfcnt = ef->ctfcnt;
28175d94ef6SJohn Birrell lc->symtab = ef->ddbsymtab;
28275d94ef6SJohn Birrell lc->strtab = ef->ddbstrtab;
28375d94ef6SJohn Birrell lc->strcnt = ef->ddbstrcnt;
28475d94ef6SJohn Birrell lc->nsym = ef->ddbsymcnt;
28575d94ef6SJohn Birrell lc->ctfoffp = (uint32_t **) &ef->ctfoff;
28675d94ef6SJohn Birrell lc->typoffp = (uint32_t **) &ef->typoff;
28775d94ef6SJohn Birrell lc->typlenp = &ef->typlen;
28875d94ef6SJohn Birrell
28975d94ef6SJohn Birrell out:
290b249ce48SMateusz Guzik VOP_UNLOCK(nd.ni_vp);
29175d94ef6SJohn Birrell vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
29275d94ef6SJohn Birrell
29375d94ef6SJohn Birrell if (hdr != NULL)
29475d94ef6SJohn Birrell free(hdr, M_LINKER);
29575d94ef6SJohn Birrell if (shdr != NULL)
29675d94ef6SJohn Birrell free(shdr, M_LINKER);
29775d94ef6SJohn Birrell if (shstrtab != NULL)
29875d94ef6SJohn Birrell free(shstrtab, M_LINKER);
29975d94ef6SJohn Birrell if (ctftab != NULL)
30075d94ef6SJohn Birrell free(ctftab, M_LINKER);
30175d94ef6SJohn Birrell if (raw != NULL)
30275d94ef6SJohn Birrell free(raw, M_LINKER);
30375d94ef6SJohn Birrell #else
30475d94ef6SJohn Birrell error = EOPNOTSUPP;
30575d94ef6SJohn Birrell #endif
30675d94ef6SJohn Birrell
30775d94ef6SJohn Birrell return (error);
30875d94ef6SJohn Birrell }
309c21bc6f3SBojan Novković
310c21bc6f3SBojan Novković static int
link_elf_ctf_get_ddb(linker_file_t lf,linker_ctf_t * lc)311c21bc6f3SBojan Novković link_elf_ctf_get_ddb(linker_file_t lf, linker_ctf_t *lc)
312c21bc6f3SBojan Novković {
313c21bc6f3SBojan Novković elf_file_t ef = (elf_file_t)lf;
314c21bc6f3SBojan Novković
315c21bc6f3SBojan Novković /*
316c21bc6f3SBojan Novković * Check whether CTF data was loaded or if a
317c21bc6f3SBojan Novković * previous loading attempt failed (ctfcnt == -1).
318c21bc6f3SBojan Novković */
319c21bc6f3SBojan Novković if (ef->ctfcnt <= 0) {
320c21bc6f3SBojan Novković return (ENOENT);
321c21bc6f3SBojan Novković }
322c21bc6f3SBojan Novković
323c21bc6f3SBojan Novković lc->ctftab = ef->ctftab;
324c21bc6f3SBojan Novković lc->ctfcnt = ef->ctfcnt;
325c21bc6f3SBojan Novković lc->symtab = ef->ddbsymtab;
326c21bc6f3SBojan Novković lc->strtab = ef->ddbstrtab;
327c21bc6f3SBojan Novković lc->strcnt = ef->ddbstrcnt;
328c21bc6f3SBojan Novković lc->nsym = ef->ddbsymcnt;
329c21bc6f3SBojan Novković
330c21bc6f3SBojan Novković return (0);
331c21bc6f3SBojan Novković }
332c21bc6f3SBojan Novković
333c21bc6f3SBojan Novković static int
link_elf_ctf_lookup_typename(linker_file_t lf,linker_ctf_t * lc,const char * typename)334c21bc6f3SBojan Novković link_elf_ctf_lookup_typename(linker_file_t lf, linker_ctf_t *lc,
335c21bc6f3SBojan Novković const char *typename)
336c21bc6f3SBojan Novković {
337c21bc6f3SBojan Novković if (link_elf_ctf_get_ddb(lf, lc))
338c21bc6f3SBojan Novković return (ENOENT);
339c21bc6f3SBojan Novković
340dc7ae2bcSMitchell Horne #ifdef DDB
341c21bc6f3SBojan Novković return (db_ctf_lookup_typename(lc, typename) ? 0 : ENOENT);
342dc7ae2bcSMitchell Horne #else
343dc7ae2bcSMitchell Horne return (ENOENT);
344dc7ae2bcSMitchell Horne #endif
345c21bc6f3SBojan Novković }
346