2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 John Birrell <jb@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Note this file is included by both link_elf.c and link_elf_obj.c.
36 #include <contrib/zlib/zlib.h>
40 link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
44 Elf_Shdr *shdr = NULL;
45 caddr_t ctftab = NULL;
47 caddr_t shstrtab = NULL;
48 elf_file_t ef = (elf_file_t) lf;
54 struct thread *td = curthread;
55 struct ctf_header cth;
59 if (lf == NULL || lc == NULL)
62 /* Set the defaults for no CTF present. That's not a crime! */
63 bzero(lc, sizeof(*lc));
67 * First check if we've tried to load CTF data previously and the
68 * CTF ELF section wasn't found. We flag that condition by setting
69 * ctfcnt to -1. See below.
74 /* Now check if we've already loaded the CTF data.. */
76 /* We only need to load once. */
77 lc->ctftab = ef->ctftab;
78 lc->ctfcnt = ef->ctfcnt;
79 lc->symtab = ef->ddbsymtab;
80 lc->strtab = ef->ddbstrtab;
81 lc->strcnt = ef->ddbstrcnt;
82 lc->nsym = ef->ddbsymcnt;
83 lc->ctfoffp = (uint32_t **) &ef->ctfoff;
84 lc->typoffp = (uint32_t **) &ef->typoff;
85 lc->typlenp = &ef->typlen;
90 * We need to try reading the CTF data. Flag no CTF data present
91 * by default and if we actually succeed in reading it, we'll
92 * update ctfcnt to the number of bytes read.
96 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname, td);
98 error = vn_open(&nd, &flags, 0, NULL);
101 NDFREE(&nd, NDF_ONLY_PNBUF);
103 /* Allocate memory for the FLF header. */
104 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
106 /* Read the ELF header. */
107 if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
108 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
118 nbytes = hdr->e_shnum * hdr->e_shentsize;
119 if (nbytes == 0 || hdr->e_shoff == 0 ||
120 hdr->e_shentsize != sizeof(Elf_Shdr)) {
125 /* Allocate memory for all the section headers */
126 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
128 /* Read all the section headers */
129 if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
130 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
135 * We need to search for the CTF section by name, so if the
136 * section names aren't present, then we can't locate the
137 * .SUNW_ctf section containing the CTF data.
139 if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
140 printf("%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
141 __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
142 shdr[hdr->e_shstrndx].sh_type);
147 /* Allocate memory to buffer the section header strings. */
148 shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
150 /* Read the section header strings. */
151 if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
152 shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
153 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
156 /* Search for the section containing the CTF data. */
157 for (i = 0; i < hdr->e_shnum; i++)
158 if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
161 /* Check if the CTF section wasn't found. */
162 if (i >= hdr->e_shnum) {
163 printf("%s(%d): module %s has no .SUNW_ctf section\n",
164 __func__, __LINE__, lf->pathname);
169 /* Read the CTF header. */
170 if ((error = vn_rdwr(UIO_READ, nd.ni_vp, &cth, sizeof(cth),
171 shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
172 NOCRED, NULL, td)) != 0)
175 /* Check the CTF magic number. */
176 if (cth.cth_magic != CTF_MAGIC) {
177 printf("%s(%d): module %s has invalid format\n",
178 __func__, __LINE__, lf->pathname);
183 if (cth.cth_version != CTF_VERSION_2 &&
184 cth.cth_version != CTF_VERSION_3) {
186 "%s(%d): module %s CTF format has unsupported version %d\n",
187 __func__, __LINE__, lf->pathname, cth.cth_version);
192 /* Check if the data is compressed. */
193 if ((cth.cth_flags & CTF_F_COMPRESS) != 0) {
195 * The last two fields in the CTF header are the offset
196 * from the end of the header to the start of the string
197 * data and the length of that string data. Use this
198 * information to determine the decompressed CTF data
201 sz = cth.cth_stroff + cth.cth_strlen + sizeof(cth);
204 * Allocate memory for the compressed CTF data, including
205 * the header (which isn't compressed).
207 raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
210 * The CTF data is not compressed, so the ELF section
211 * size is the same as the buffer size required.
213 sz = shdr[i].sh_size;
217 * Allocate memory to buffer the CTF data in its decompressed
220 ctftab = malloc(sz, M_LINKER, M_WAITOK);
223 * Read the CTF data into the raw buffer if compressed, or
224 * directly into the CTF buffer otherwise.
226 if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
227 shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
228 td->td_ucred, NOCRED, NULL, td)) != 0)
231 /* Check if decompression is required. */
237 * The header isn't compressed, so copy that into the
240 bcopy(&cth, ctftab, sizeof(cth));
242 destlen = sz - sizeof(cth);
243 ret = uncompress(ctftab + sizeof(cth), &destlen,
244 raw + sizeof(cth), shdr[i].sh_size - sizeof(cth));
246 printf("%s(%d): zlib uncompress returned %d\n",
247 __func__, __LINE__, ret);
253 /* Got the CTF data! */
255 ef->ctfcnt = shdr[i].sh_size;
257 /* We'll retain the memory allocated for the CTF data. */
260 /* Let the caller use the CTF data read. */
261 lc->ctftab = ef->ctftab;
262 lc->ctfcnt = ef->ctfcnt;
263 lc->symtab = ef->ddbsymtab;
264 lc->strtab = ef->ddbstrtab;
265 lc->strcnt = ef->ddbstrcnt;
266 lc->nsym = ef->ddbsymcnt;
267 lc->ctfoffp = (uint32_t **) &ef->ctfoff;
268 lc->typoffp = (uint32_t **) &ef->typoff;
269 lc->typlenp = &ef->typlen;
272 VOP_UNLOCK(nd.ni_vp);
273 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
278 free(shdr, M_LINKER);
279 if (shstrtab != NULL)
280 free(shstrtab, M_LINKER);
282 free(ctftab, M_LINKER);