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>
30 #include <sys/endian.h>
47 static const char *_libctf_zlib = "/usr/lib/64/libz.so";
49 static const char *_libctf_zlib = "/usr/lib/libz.so";
54 int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
55 const char *(*z_error)(int);
59 static size_t _PAGESIZE;
60 static size_t _PAGEMASK;
63 #pragma init(_libctf_init)
65 void _libctf_init(void) __attribute__ ((constructor));
71 const char *p = getenv("LIBCTF_DECOMPRESSOR");
74 _libctf_zlib = p; /* use alternate decompression library */
77 _libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
79 _PAGESIZE = getpagesize();
80 _PAGEMASK = ~(_PAGESIZE - 1);
84 * Attempt to dlopen the decompression library and locate the symbols of
85 * interest that we will need to call. This information in cached so
86 * that multiple calls to ctf_bufopen() do not need to reopen the library.
92 ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
94 if (zlib.z_dlp != NULL)
95 return (zlib.z_dlp); /* library is already loaded */
97 if (access(_libctf_zlib, R_OK) == -1)
98 return (ctf_set_open_errno(errp, ECTF_ZMISSING));
100 if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
101 return (ctf_set_open_errno(errp, ECTF_ZINIT));
103 zlib.z_uncompress = (int (*)(uchar_t *, ulong_t *, const uchar_t *, ulong_t)) dlsym(zlib.z_dlp, "uncompress");
104 zlib.z_error = (const char *(*)(int)) dlsym(zlib.z_dlp, "zError");
106 if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
107 (void) dlclose(zlib.z_dlp);
108 bzero(&zlib, sizeof (zlib));
109 return (ctf_set_open_errno(errp, ECTF_ZINIT));
112 zlib.z_uncompress = uncompress;
113 zlib.z_error = zError;
115 /* Dummy return variable as 'no error' */
116 zlib.z_dlp = (void *) (uintptr_t) 1;
123 * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
124 * which we then patch through to the functions in the decompression library.
127 z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
129 return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
135 return (zlib.z_error(err));
139 * Convert a 32-bit ELF file header into GElf.
142 ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
144 bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
145 dst->e_type = src->e_type;
146 dst->e_machine = src->e_machine;
147 dst->e_version = src->e_version;
148 dst->e_entry = (Elf64_Addr)src->e_entry;
149 dst->e_phoff = (Elf64_Off)src->e_phoff;
150 dst->e_shoff = (Elf64_Off)src->e_shoff;
151 dst->e_flags = src->e_flags;
152 dst->e_ehsize = src->e_ehsize;
153 dst->e_phentsize = src->e_phentsize;
154 dst->e_phnum = src->e_phnum;
155 dst->e_shentsize = src->e_shentsize;
156 dst->e_shnum = src->e_shnum;
157 dst->e_shstrndx = src->e_shstrndx;
161 * Convert a 32-bit ELF section header into GElf.
164 shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
166 dst->sh_name = src->sh_name;
167 dst->sh_type = src->sh_type;
168 dst->sh_flags = src->sh_flags;
169 dst->sh_addr = src->sh_addr;
170 dst->sh_offset = src->sh_offset;
171 dst->sh_size = src->sh_size;
172 dst->sh_link = src->sh_link;
173 dst->sh_info = src->sh_info;
174 dst->sh_addralign = src->sh_addralign;
175 dst->sh_entsize = src->sh_entsize;
179 * In order to mmap a section from the ELF file, we must round down sh_offset
180 * to the previous page boundary, and mmap the surrounding page. We store
181 * the pointer to the start of the actual section data back into sp->cts_data.
184 ctf_sect_mmap(ctf_sect_t *sp, int fd)
186 size_t pageoff = sp->cts_offset & ~_PAGEMASK;
188 caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
189 MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
191 if (base != MAP_FAILED)
192 sp->cts_data = base + pageoff;
198 * Since sp->cts_data has the adjusted offset, we have to again round down
199 * to get the actual mmap address and round up to get the size.
202 ctf_sect_munmap(const ctf_sect_t *sp)
204 uintptr_t addr = (uintptr_t)sp->cts_data;
205 uintptr_t pageoff = addr & ~_PAGEMASK;
207 (void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
211 * Open the specified file descriptor and return a pointer to a CTF container.
212 * The file can be either an ELF file or raw CTF file. The caller is
213 * responsible for closing the file descriptor when it is no longer needed.
216 ctf_fdopen(int fd, int *errp)
218 ctf_sect_t ctfsect, symsect, strsect;
219 ctf_file_t *fp = NULL;
220 size_t shstrndx, shnum;
231 bzero(&ctfsect, sizeof (ctf_sect_t));
232 bzero(&symsect, sizeof (ctf_sect_t));
233 bzero(&strsect, sizeof (ctf_sect_t));
234 bzero(&hdr, sizeof (hdr));
236 if (fstat64(fd, &st) == -1)
237 return (ctf_set_open_errno(errp, errno));
239 if ((nbytes = pread64(fd, &hdr, sizeof (hdr), 0)) <= 0)
240 return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
243 * If we have read enough bytes to form a CTF header and the magic
244 * string matches, attempt to interpret the file as raw CTF.
246 if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&
247 hdr.ctf.ctp_magic == CTF_MAGIC) {
248 if (hdr.ctf.ctp_version != CTF_VERSION_2 &&
249 hdr.ctf.ctp_version != CTF_VERSION_3)
250 return (ctf_set_open_errno(errp, ECTF_CTFVERS));
252 ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
255 if (ctfsect.cts_data == MAP_FAILED)
256 return (ctf_set_open_errno(errp, errno));
258 ctfsect.cts_name = _CTF_SECTION;
259 ctfsect.cts_type = SHT_PROGBITS;
260 ctfsect.cts_flags = SHF_ALLOC;
261 ctfsect.cts_size = (size_t)st.st_size;
262 ctfsect.cts_entsize = 1;
263 ctfsect.cts_offset = 0;
265 if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
266 ctf_sect_munmap(&ctfsect);
272 * If we have read enough bytes to form an ELF header and the magic
273 * string matches, attempt to interpret the file as an ELF file. We
274 * do our own largefile ELF processing, and convert everything to
275 * GElf structures so that clients can operate on any data model.
277 if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&
278 bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
279 #if BYTE_ORDER == _BIG_ENDIAN
280 uchar_t order = ELFDATA2MSB;
282 uchar_t order = ELFDATA2LSB;
287 size_t strs_mapsz, i;
290 if (hdr.e32.e_ident[EI_DATA] != order)
291 return (ctf_set_open_errno(errp, ECTF_ENDIAN));
292 if (hdr.e32.e_version != EV_CURRENT)
293 return (ctf_set_open_errno(errp, ECTF_ELFVERS));
295 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
296 if (nbytes < (ssize_t) sizeof (GElf_Ehdr))
297 return (ctf_set_open_errno(errp, ECTF_FMT));
299 Elf32_Ehdr e32 = hdr.e32;
300 ehdr_to_gelf(&e32, &hdr.e64);
303 shnum = hdr.e64.e_shnum;
304 shstrndx = hdr.e64.e_shstrndx;
306 /* Extended ELF sections */
307 if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {
308 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
311 if (pread64(fd, &x32, sizeof (x32),
312 hdr.e64.e_shoff) != sizeof (x32))
313 return (ctf_set_open_errno(errp,
317 shstrndx = x32.sh_link;
321 if (pread64(fd, &x64, sizeof (x64),
322 hdr.e64.e_shoff) != sizeof (x64))
323 return (ctf_set_open_errno(errp,
327 shstrndx = x64.sh_link;
331 if (shstrndx >= shnum)
332 return (ctf_set_open_errno(errp, ECTF_CORRUPT));
334 nbytes = sizeof (GElf_Shdr) * shnum;
336 if ((sp = malloc(nbytes)) == NULL)
337 return (ctf_set_open_errno(errp, errno));
340 * Read in and convert to GElf the array of Shdr structures
341 * from e_shoff so we can locate sections of interest.
343 if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
346 nbytes = sizeof (Elf32_Shdr) * shnum;
348 if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
349 sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
352 return (ctf_set_open_errno(errp, errno));
355 for (i = 0; i < shnum; i++)
356 shdr_to_gelf(&sp32[i], &sp[i]);
360 } else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
362 return (ctf_set_open_errno(errp, errno));
366 * Now mmap the section header strings section so that we can
367 * perform string comparison on the section names.
369 strs_mapsz = sp[shstrndx].sh_size +
370 (sp[shstrndx].sh_offset & ~_PAGEMASK);
372 strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
373 fd, sp[shstrndx].sh_offset & _PAGEMASK);
375 strs = (char *)strs_map +
376 (sp[shstrndx].sh_offset & ~_PAGEMASK);
378 if (strs_map == MAP_FAILED) {
380 return (ctf_set_open_errno(errp, ECTF_MMAP));
384 * Iterate over the section header array looking for the CTF
385 * section and symbol table. The strtab is linked to symtab.
387 for (i = 0; i < shnum; i++) {
388 const GElf_Shdr *shp = &sp[i];
389 const GElf_Shdr *lhp = &sp[shp->sh_link];
391 if (shp->sh_link >= shnum)
392 continue; /* corrupt sh_link field */
394 if (shp->sh_name >= sp[shstrndx].sh_size ||
395 lhp->sh_name >= sp[shstrndx].sh_size)
396 continue; /* corrupt sh_name field */
398 if (shp->sh_type == SHT_PROGBITS &&
399 strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
400 ctfsect.cts_name = strs + shp->sh_name;
401 ctfsect.cts_type = shp->sh_type;
402 ctfsect.cts_flags = shp->sh_flags;
403 ctfsect.cts_size = shp->sh_size;
404 ctfsect.cts_entsize = shp->sh_entsize;
405 ctfsect.cts_offset = (off64_t)shp->sh_offset;
407 } else if (shp->sh_type == SHT_SYMTAB) {
408 symsect.cts_name = strs + shp->sh_name;
409 symsect.cts_type = shp->sh_type;
410 symsect.cts_flags = shp->sh_flags;
411 symsect.cts_size = shp->sh_size;
412 symsect.cts_entsize = shp->sh_entsize;
413 symsect.cts_offset = (off64_t)shp->sh_offset;
415 strsect.cts_name = strs + lhp->sh_name;
416 strsect.cts_type = lhp->sh_type;
417 strsect.cts_flags = lhp->sh_flags;
418 strsect.cts_size = lhp->sh_size;
419 strsect.cts_entsize = lhp->sh_entsize;
420 strsect.cts_offset = (off64_t)lhp->sh_offset;
424 free(sp); /* free section header array */
426 if (ctfsect.cts_type == SHT_NULL) {
427 (void) munmap(strs_map, strs_mapsz);
428 return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
432 * Now mmap the CTF data, symtab, and strtab sections and
433 * call ctf_bufopen() to do the rest of the work.
435 if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
436 (void) munmap(strs_map, strs_mapsz);
437 return (ctf_set_open_errno(errp, ECTF_MMAP));
440 if (symsect.cts_type != SHT_NULL &&
441 strsect.cts_type != SHT_NULL) {
442 if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
443 ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
444 (void) ctf_set_open_errno(errp, ECTF_MMAP);
445 goto bad; /* unmap all and abort */
447 fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
449 fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
452 ctf_sect_munmap(&ctfsect);
453 ctf_sect_munmap(&symsect);
454 ctf_sect_munmap(&strsect);
456 fp->ctf_flags |= LCTF_MMAP;
458 (void) munmap(strs_map, strs_mapsz);
462 return (ctf_set_open_errno(errp, ECTF_FMT));
466 * Open the specified file and return a pointer to a CTF container. The file
467 * can be either an ELF file or raw CTF file. This is just a convenient
468 * wrapper around ctf_fdopen() for callers.
471 ctf_open(const char *filename, int *errp)
476 if ((fd = open64(filename, O_RDONLY)) == -1) {
482 fp = ctf_fdopen(fd, errp);
488 * Write the uncompressed CTF data stream to the specified file descriptor.
489 * This is useful for saving the results of dynamic CTF containers.
492 ctf_write(ctf_file_t *fp, int fd)
494 const uchar_t *buf = fp->ctf_base;
495 ssize_t resid = fp->ctf_size;
499 if ((len = write(fd, buf, resid)) <= 0)
500 return (ctf_set_errno(fp, errno));
509 * Set the CTF library client version to the specified version. If version is
510 * zero, we just return the default library version number.
513 ctf_version(int version)
521 if (version > CTF_VERSION) {
525 ctf_dprintf("ctf_version: client using version %d\n", version);
526 _libctf_version = version;
529 return (_libctf_version);