2 * Copyright (c) 2008 John Birrell <jb@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Note this file is included by both link_elf.c and link_elf_obj.c.
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.
36 #define CTF_HDR_SIZE 36
37 #define CTF_HDR_STRTAB_U32 7
38 #define CTF_HDR_STRLEN_U32 8
42 z_alloc(void *nil
, u_int items
, u_int size
)
46 ptr
= malloc(items
* size
, M_TEMP
, M_NOWAIT
);
51 z_free(void *nil
, void *ptr
)
59 link_elf_ctf_get(linker_file_t lf
, linker_ctf_t
*lc
)
63 Elf_Shdr
*shdr
= NULL
;
64 caddr_t ctftab
= NULL
;
66 caddr_t shstrtab
= NULL
;
67 elf_file_t ef
= (elf_file_t
) lf
;
75 struct thread
*td
= curthread
;
76 uint8_t ctf_hdr
[CTF_HDR_SIZE
];
80 if (lf
== NULL
|| lc
== NULL
)
83 /* Set the defaults for no CTF present. That's not a crime! */
84 bzero(lc
, sizeof(*lc
));
88 * First check if we've tried to load CTF data previously and the
89 * CTF ELF section wasn't found. We flag that condition by setting
90 * ctfcnt to -1. See below.
95 /* Now check if we've already loaded the CTF data.. */
97 /* We only need to load once. */
98 lc
->ctftab
= ef
->ctftab
;
99 lc
->ctfcnt
= ef
->ctfcnt
;
100 lc
->symtab
= ef
->ddbsymtab
;
101 lc
->strtab
= ef
->ddbstrtab
;
102 lc
->strcnt
= ef
->ddbstrcnt
;
103 lc
->nsym
= ef
->ddbsymcnt
;
104 lc
->ctfoffp
= (uint32_t **) &ef
->ctfoff
;
105 lc
->typoffp
= (uint32_t **) &ef
->typoff
;
106 lc
->typlenp
= &ef
->typlen
;
111 * We need to try reading the CTF data. Flag no CTF data present
112 * by default and if we actually succeed in reading it, we'll
113 * update ctfcnt to the number of bytes read.
117 NDINIT(&nd
, LOOKUP
, FOLLOW
| MPSAFE
, UIO_SYSSPACE
, lf
->pathname
, td
);
119 error
= vn_open(&nd
, &flags
, 0, NULL
);
122 vfslocked
= NDHASGIANT(&nd
);
123 NDFREE(&nd
, NDF_ONLY_PNBUF
);
125 /* Allocate memory for the FLF header. */
126 if ((hdr
= malloc(sizeof(*hdr
), M_LINKER
, M_WAITOK
)) == NULL
) {
131 /* Read the ELF header. */
132 if ((error
= vn_rdwr(UIO_READ
, nd
.ni_vp
, hdr
, sizeof(*hdr
),
133 0, UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
, &resid
,
143 nbytes
= hdr
->e_shnum
* hdr
->e_shentsize
;
144 if (nbytes
== 0 || hdr
->e_shoff
== 0 ||
145 hdr
->e_shentsize
!= sizeof(Elf_Shdr
)) {
150 /* Allocate memory for all the section headers */
151 if ((shdr
= malloc(nbytes
, M_LINKER
, M_WAITOK
)) == NULL
) {
156 /* Read all the section headers */
157 if ((error
= vn_rdwr(UIO_READ
, nd
.ni_vp
, (caddr_t
)shdr
, nbytes
,
158 hdr
->e_shoff
, UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
,
163 * We need to search for the CTF section by name, so if the
164 * section names aren't present, then we can't locate the
165 * .SUNW_ctf section containing the CTF data.
167 if (hdr
->e_shstrndx
== 0 || shdr
[hdr
->e_shstrndx
].sh_type
!= SHT_STRTAB
)
170 /* Allocate memory to buffer the section header strings. */
171 if ((shstrtab
= malloc(shdr
[hdr
->e_shstrndx
].sh_size
, M_LINKER
,
172 M_WAITOK
)) == NULL
) {
177 /* Read the section header strings. */
178 if ((error
= vn_rdwr(UIO_READ
, nd
.ni_vp
, shstrtab
,
179 shdr
[hdr
->e_shstrndx
].sh_size
, shdr
[hdr
->e_shstrndx
].sh_offset
,
180 UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
, &resid
,
184 /* Search for the section containing the CTF data. */
185 for (i
= 0; i
< hdr
->e_shnum
; i
++)
186 if (strcmp(".SUNW_ctf", shstrtab
+ shdr
[i
].sh_name
) == 0)
189 /* Check if the CTF section wasn't found. */
190 if (i
>= hdr
->e_shnum
)
193 /* Read the CTF header. */
194 if ((error
= vn_rdwr(UIO_READ
, nd
.ni_vp
, ctf_hdr
, sizeof(ctf_hdr
),
195 shdr
[i
].sh_offset
, UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
,
196 NOCRED
, &resid
, td
)) != 0)
199 /* Check the CTF magic number. (XXX check for big endian!) */
200 if (ctf_hdr
[0] != 0xf1 || ctf_hdr
[1] != 0xcf)
203 /* Check if version 2. */
207 /* Check if the data is compressed. */
208 if ((ctf_hdr
[3] & 0x1) != 0) {
209 uint32_t *u32
= (uint32_t *) ctf_hdr
;
212 * The last two fields in the CTF header are the offset
213 * from the end of the header to the start of the string
214 * data and the length of that string data. se this
215 * information to determine the decompressed CTF data
218 sz
= u32
[CTF_HDR_STRTAB_U32
] + u32
[CTF_HDR_STRLEN_U32
] +
222 * Allocate memory for the compressed CTF data, including
223 * the header (which isn't compressed).
225 if ((raw
= malloc(shdr
[i
].sh_size
, M_LINKER
, M_WAITOK
)) == NULL
) {
231 * The CTF data is not compressed, so the ELF section
232 * size is the same as the buffer size required.
234 sz
= shdr
[i
].sh_size
;
238 * Allocate memory to buffer the CTF data in it's decompressed
241 if ((ctftab
= malloc(sz
, M_LINKER
, M_WAITOK
)) == NULL
) {
247 * Read the CTF data into the raw buffer if compressed, or
248 * directly into the CTF buffer otherwise.
250 if ((error
= vn_rdwr(UIO_READ
, nd
.ni_vp
, raw
== NULL
? ctftab
: raw
,
251 shdr
[i
].sh_size
, shdr
[i
].sh_offset
, UIO_SYSSPACE
, IO_NODELOCKED
,
252 td
->td_ucred
, NOCRED
, &resid
, td
)) != 0)
255 /* Check if decompression is required. */
261 * The header isn't compressed, so copy that into the
264 bcopy(ctf_hdr
, ctftab
, sizeof(ctf_hdr
));
266 /* Initialise the zlib structure. */
267 bzero(&zs
, sizeof(zs
));
271 if (inflateInit(&zs
) != Z_OK
) {
276 zs
.avail_in
= shdr
[i
].sh_size
- sizeof(ctf_hdr
);
277 zs
.next_in
= ((uint8_t *) raw
) + sizeof(ctf_hdr
);
278 zs
.avail_out
= sz
- sizeof(ctf_hdr
);
279 zs
.next_out
= ((uint8_t *) ctftab
) + sizeof(ctf_hdr
);
280 if ((ret
= inflate(&zs
, Z_FINISH
)) != Z_STREAM_END
) {
281 printf("%s(%d): zlib inflate returned %d\n", __func__
, __LINE__
, ret
);
287 /* Got the CTF data! */
289 ef
->ctfcnt
= shdr
[i
].sh_size
;
291 /* We'll retain the memory allocated for the CTF data. */
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
;
306 VOP_UNLOCK(nd
.ni_vp
, 0);
307 vn_close(nd
.ni_vp
, FREAD
, td
->td_ucred
, td
);
308 VFS_UNLOCK_GIANT(vfslocked
);
313 free(shdr
, M_LINKER
);
314 if (shstrtab
!= NULL
)
315 free(shstrtab
, M_LINKER
);
317 free(ctftab
, M_LINKER
);