4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
46 static const char *_libctf_zlib = "/usr/lib/64/libz.so";
48 static const char *_libctf_zlib = "/usr/lib/libz.so";
53 int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
54 const char *(*z_error)(int);
58 static size_t _PAGESIZE;
59 static size_t _PAGEMASK;
62 #pragma init(_libctf_init)
64 void _libctf_init(void) __attribute__ ((constructor));
70 const char *p = getenv("LIBCTF_DECOMPRESSOR");
73 _libctf_zlib = p; /* use alternate decompression library */
76 _libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
78 _PAGESIZE = getpagesize();
79 _PAGEMASK = ~(_PAGESIZE - 1);
83 * Attempt to dlopen the decompression library and locate the symbols of
84 * interest that we will need to call. This information in cached so
85 * that multiple calls to ctf_bufopen() do not need to reopen the library.
91 ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
93 if (zlib.z_dlp != NULL)
94 return (zlib.z_dlp); /* library is already loaded */
96 if (access(_libctf_zlib, R_OK) == -1)
97 return (ctf_set_open_errno(errp, ECTF_ZMISSING));
99 if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
100 return (ctf_set_open_errno(errp, ECTF_ZINIT));
102 zlib.z_uncompress = (int (*)(uchar_t *, ulong_t *, const uchar_t *, ulong_t)) dlsym(zlib.z_dlp, "uncompress");
103 zlib.z_error = (const char *(*)(int)) dlsym(zlib.z_dlp, "zError");
105 if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
106 (void) dlclose(zlib.z_dlp);
107 bzero(&zlib, sizeof (zlib));
108 return (ctf_set_open_errno(errp, ECTF_ZINIT));
111 zlib.z_uncompress = uncompress;
112 zlib.z_error = zError;
114 /* Dummy return variable as 'no error' */
115 zlib.z_dlp = (void *) (uintptr_t) 1;
122 * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
123 * which we then patch through to the functions in the decompression library.
126 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
128 return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
134 return (zlib.z_error(err));
138 * Convert a 32-bit ELF file header into GElf.
141 ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
143 bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
144 dst->e_type = src->e_type;
145 dst->e_machine = src->e_machine;
146 dst->e_version = src->e_version;
147 dst->e_entry = (Elf64_Addr)src->e_entry;
148 dst->e_phoff = (Elf64_Off)src->e_phoff;
149 dst->e_shoff = (Elf64_Off)src->e_shoff;
150 dst->e_flags = src->e_flags;
151 dst->e_ehsize = src->e_ehsize;
152 dst->e_phentsize = src->e_phentsize;
153 dst->e_phnum = src->e_phnum;
154 dst->e_shentsize = src->e_shentsize;
155 dst->e_shnum = src->e_shnum;
156 dst->e_shstrndx = src->e_shstrndx;
160 * Convert a 32-bit ELF section header into GElf.
163 shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
165 dst->sh_name = src->sh_name;
166 dst->sh_type = src->sh_type;
167 dst->sh_flags = src->sh_flags;
168 dst->sh_addr = src->sh_addr;
169 dst->sh_offset = src->sh_offset;
170 dst->sh_size = src->sh_size;
171 dst->sh_link = src->sh_link;
172 dst->sh_info = src->sh_info;
173 dst->sh_addralign = src->sh_addralign;
174 dst->sh_entsize = src->sh_entsize;
178 * In order to mmap a section from the ELF file, we must round down sh_offset
179 * to the previous page boundary, and mmap the surrounding page. We store
180 * the pointer to the start of the actual section data back into sp->cts_data.
183 ctf_sect_mmap(ctf_sect_t *sp, int fd)
185 size_t pageoff = sp->cts_offset & ~_PAGEMASK;
187 caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
188 MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
190 if (base != MAP_FAILED)
191 sp->cts_data = base + pageoff;
197 * Since sp->cts_data has the adjusted offset, we have to again round down
198 * to get the actual mmap address and round up to get the size.
201 ctf_sect_munmap(const ctf_sect_t *sp)
203 uintptr_t addr = (uintptr_t)sp->cts_data;
204 uintptr_t pageoff = addr & ~_PAGEMASK;
206 (void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
210 * Open the specified file descriptor and return a pointer to a CTF container.
211 * The file can be either an ELF file or raw CTF file. The caller is
212 * responsible for closing the file descriptor when it is no longer needed.
215 ctf_fdopen(int fd, int *errp)
217 ctf_sect_t ctfsect, symsect, strsect;
218 ctf_file_t *fp = NULL;
219 size_t shstrndx, shnum;
230 bzero(&ctfsect, sizeof (ctf_sect_t));
231 bzero(&symsect, sizeof (ctf_sect_t));
232 bzero(&strsect, sizeof (ctf_sect_t));
233 bzero(&hdr.ctf, sizeof (hdr));
235 if (fstat64(fd, &st) == -1)
236 return (ctf_set_open_errno(errp, errno));
238 if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
239 return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
242 * If we have read enough bytes to form a CTF header and the magic
243 * string matches, attempt to interpret the file as raw CTF.
245 if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&
246 hdr.ctf.ctp_magic == CTF_MAGIC) {
247 if (hdr.ctf.ctp_version > CTF_VERSION)
248 return (ctf_set_open_errno(errp, ECTF_CTFVERS));
250 ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
253 if (ctfsect.cts_data == MAP_FAILED)
254 return (ctf_set_open_errno(errp, errno));
256 ctfsect.cts_name = _CTF_SECTION;
257 ctfsect.cts_type = SHT_PROGBITS;
258 ctfsect.cts_flags = SHF_ALLOC;
259 ctfsect.cts_size = (size_t)st.st_size;
260 ctfsect.cts_entsize = 1;
261 ctfsect.cts_offset = 0;
263 if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
264 ctf_sect_munmap(&ctfsect);
270 * If we have read enough bytes to form an ELF header and the magic
271 * string matches, attempt to interpret the file as an ELF file. We
272 * do our own largefile ELF processing, and convert everything to
273 * GElf structures so that clients can operate on any data model.
275 if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&
276 bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
278 uchar_t order = ELFDATA2MSB;
280 uchar_t order = ELFDATA2LSB;
285 size_t strs_mapsz, i;
288 if (hdr.e32.e_ident[EI_DATA] != order)
289 return (ctf_set_open_errno(errp, ECTF_ENDIAN));
290 if (hdr.e32.e_version != EV_CURRENT)
291 return (ctf_set_open_errno(errp, ECTF_ELFVERS));
293 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
294 if (nbytes < (ssize_t) sizeof (GElf_Ehdr))
295 return (ctf_set_open_errno(errp, ECTF_FMT));
297 Elf32_Ehdr e32 = hdr.e32;
298 ehdr_to_gelf(&e32, &hdr.e64);
301 shnum = hdr.e64.e_shnum;
302 shstrndx = hdr.e64.e_shstrndx;
304 /* Extended ELF sections */
305 if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
306 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
309 if (pread64(fd, &x32, sizeof (x32),
310 hdr.e64.e_shoff) != sizeof (x32))
311 return (ctf_set_open_errno(errp,
315 shstrndx = x32.sh_link;
319 if (pread64(fd, &x64, sizeof (x64),
320 hdr.e64.e_shoff) != sizeof (x64))
321 return (ctf_set_open_errno(errp,
325 shstrndx = x64.sh_link;
329 if (shstrndx >= shnum)
330 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
332 nbytes = sizeof (GElf_Shdr) * shnum;
334 if ((sp = malloc(nbytes)) == NULL)
335 return (ctf_set_open_errno(errp, errno));
338 * Read in and convert to GElf the array of Shdr structures
339 * from e_shoff so we can locate sections of interest.
341 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
344 nbytes = sizeof (Elf32_Shdr) * shnum;
346 if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
347 sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
349 return (ctf_set_open_errno(errp, errno));
352 for (i = 0; i < shnum; i++)
353 shdr_to_gelf(&sp32[i], &sp[i]);
357 } else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
359 return (ctf_set_open_errno(errp, errno));
363 * Now mmap the section header strings section so that we can
364 * perform string comparison on the section names.
366 strs_mapsz = sp[shstrndx].sh_size +
367 (sp[shstrndx].sh_offset & ~_PAGEMASK);
369 strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
370 fd, sp[shstrndx].sh_offset & _PAGEMASK);
372 strs = (char *)strs_map +
373 (sp[shstrndx].sh_offset & ~_PAGEMASK);
375 if (strs_map == MAP_FAILED) {
377 return (ctf_set_open_errno(errp, ECTF_MMAP));
381 * Iterate over the section header array looking for the CTF
382 * section and symbol table. The strtab is linked to symtab.
384 for (i = 0; i < shnum; i++) {
385 const GElf_Shdr *shp = &sp[i];
386 const GElf_Shdr *lhp = &sp[shp->sh_link];
388 if (shp->sh_link >= shnum)
389 continue; /* corrupt sh_link field */
391 if (shp->sh_name >= sp[shstrndx].sh_size ||
392 lhp->sh_name >= sp[shstrndx].sh_size)
393 continue; /* corrupt sh_name field */
395 if (shp->sh_type == SHT_PROGBITS &&
396 strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
397 ctfsect.cts_name = strs + shp->sh_name;
398 ctfsect.cts_type = shp->sh_type;
399 ctfsect.cts_flags = shp->sh_flags;
400 ctfsect.cts_size = shp->sh_size;
401 ctfsect.cts_entsize = shp->sh_entsize;
402 ctfsect.cts_offset = (off64_t)shp->sh_offset;
404 } else if (shp->sh_type == SHT_SYMTAB) {
405 symsect.cts_name = strs + shp->sh_name;
406 symsect.cts_type = shp->sh_type;
407 symsect.cts_flags = shp->sh_flags;
408 symsect.cts_size = shp->sh_size;
409 symsect.cts_entsize = shp->sh_entsize;
410 symsect.cts_offset = (off64_t)shp->sh_offset;
412 strsect.cts_name = strs + lhp->sh_name;
413 strsect.cts_type = lhp->sh_type;
414 strsect.cts_flags = lhp->sh_flags;
415 strsect.cts_size = lhp->sh_size;
416 strsect.cts_entsize = lhp->sh_entsize;
417 strsect.cts_offset = (off64_t)lhp->sh_offset;
421 free(sp); /* free section header array */
423 if (ctfsect.cts_type == SHT_NULL) {
424 (void) munmap(strs_map, strs_mapsz);
425 return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
429 * Now mmap the CTF data, symtab, and strtab sections and
430 * call ctf_bufopen() to do the rest of the work.
432 if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
433 (void) munmap(strs_map, strs_mapsz);
434 return (ctf_set_open_errno(errp, ECTF_MMAP));
437 if (symsect.cts_type != SHT_NULL &&
438 strsect.cts_type != SHT_NULL) {
439 if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
440 ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
441 (void) ctf_set_open_errno(errp, ECTF_MMAP);
442 goto bad; /* unmap all and abort */
444 fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
446 fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
449 ctf_sect_munmap(&ctfsect);
450 ctf_sect_munmap(&symsect);
451 ctf_sect_munmap(&strsect);
453 fp->ctf_flags |= LCTF_MMAP;
455 (void) munmap(strs_map, strs_mapsz);
459 return (ctf_set_open_errno(errp, ECTF_FMT));
463 * Open the specified file and return a pointer to a CTF container. The file
464 * can be either an ELF file or raw CTF file. This is just a convenient
465 * wrapper around ctf_fdopen() for callers.
468 ctf_open(const char *filename, int *errp)
473 if ((fd = open64(filename, O_RDONLY)) == -1) {
479 fp = ctf_fdopen(fd, errp);
485 * Write the uncompressed CTF data stream to the specified file descriptor.
486 * This is useful for saving the results of dynamic CTF containers.
489 ctf_write(ctf_file_t *fp, int fd)
491 const uchar_t *buf = fp->ctf_base;
492 ssize_t resid = fp->ctf_size;
496 if ((len = write(fd, buf, resid)) <= 0)
497 return (ctf_set_errno(fp, errno));
506 * Set the CTF library client version to the specified version. If version is
507 * zero, we just return the default library version number.
510 ctf_version(int version)
518 if (version > CTF_VERSION) {
522 ctf_dprintf("ctf_version: client using version %d\n", version);
523 _libctf_version = version;
526 return (_libctf_version);