2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/mman.h>
2N/A#include <ctf_impl.h>
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <errno.h>
2N/A#include <dlfcn.h>
2N/A#include <gelf.h>
2N/A
2N/A#ifdef _LP64
2N/Astatic const char *_libctf_zlib = "/usr/lib/64/libz.so";
2N/A#else
2N/Astatic const char *_libctf_zlib = "/usr/lib/libz.so";
2N/A#endif
2N/A
2N/Astatic struct {
2N/A int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);
2N/A const char *(*z_error)(int);
2N/A void *z_dlp;
2N/A} zlib;
2N/A
2N/Astatic size_t _PAGESIZE;
2N/Astatic size_t _PAGEMASK;
2N/A
2N/A#pragma init(_libctf_init)
2N/Avoid
2N/A_libctf_init(void)
2N/A{
2N/A const char *p = getenv("LIBCTF_DECOMPRESSOR");
2N/A
2N/A if (p != NULL)
2N/A _libctf_zlib = p; /* use alternate decompression library */
2N/A
2N/A _libctf_debug = getenv("LIBCTF_DEBUG") != NULL;
2N/A
2N/A _PAGESIZE = getpagesize();
2N/A _PAGEMASK = ~(_PAGESIZE - 1);
2N/A}
2N/A
2N/A/*
2N/A * Attempt to dlopen the decompression library and locate the symbols of
2N/A * interest that we will need to call. This information in cached so
2N/A * that multiple calls to ctf_bufopen() do not need to reopen the library.
2N/A */
2N/Avoid *
2N/Actf_zopen(int *errp)
2N/A{
2N/A ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);
2N/A
2N/A if (zlib.z_dlp != NULL)
2N/A return (zlib.z_dlp); /* library is already loaded */
2N/A
2N/A if (access(_libctf_zlib, R_OK) == -1)
2N/A return (ctf_set_open_errno(errp, ECTF_ZMISSING));
2N/A
2N/A if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)
2N/A return (ctf_set_open_errno(errp, ECTF_ZINIT));
2N/A
2N/A zlib.z_uncompress = (int (*)()) dlsym(zlib.z_dlp, "uncompress");
2N/A zlib.z_error = (const char *(*)()) dlsym(zlib.z_dlp, "zError");
2N/A
2N/A if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {
2N/A (void) dlclose(zlib.z_dlp);
2N/A bzero(&zlib, sizeof (zlib));
2N/A return (ctf_set_open_errno(errp, ECTF_ZINIT));
2N/A }
2N/A
2N/A return (zlib.z_dlp);
2N/A}
2N/A
2N/A/*
2N/A * The ctf_bufopen() routine calls these subroutines, defined by <sys/zmod.h>,
2N/A * which we then patch through to the functions in the decompression library.
2N/A */
2N/Aint
2N/Az_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
2N/A{
2N/A return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));
2N/A}
2N/A
2N/Aconst char *
2N/Az_strerror(int err)
2N/A{
2N/A return (zlib.z_error(err));
2N/A}
2N/A
2N/A/*
2N/A * Convert a 32-bit ELF file header into GElf.
2N/A */
2N/Astatic void
2N/Aehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)
2N/A{
2N/A bcopy(src->e_ident, dst->e_ident, EI_NIDENT);
2N/A dst->e_type = src->e_type;
2N/A dst->e_machine = src->e_machine;
2N/A dst->e_version = src->e_version;
2N/A dst->e_entry = (Elf64_Addr)src->e_entry;
2N/A dst->e_phoff = (Elf64_Off)src->e_phoff;
2N/A dst->e_shoff = (Elf64_Off)src->e_shoff;
2N/A dst->e_flags = src->e_flags;
2N/A dst->e_ehsize = src->e_ehsize;
2N/A dst->e_phentsize = src->e_phentsize;
2N/A dst->e_phnum = src->e_phnum;
2N/A dst->e_shentsize = src->e_shentsize;
2N/A dst->e_shnum = src->e_shnum;
2N/A dst->e_shstrndx = src->e_shstrndx;
2N/A}
2N/A
2N/A/*
2N/A * Convert a 32-bit ELF section header into GElf.
2N/A */
2N/Astatic void
2N/Ashdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)
2N/A{
2N/A dst->sh_name = src->sh_name;
2N/A dst->sh_type = src->sh_type;
2N/A dst->sh_flags = src->sh_flags;
2N/A dst->sh_addr = src->sh_addr;
2N/A dst->sh_offset = src->sh_offset;
2N/A dst->sh_size = src->sh_size;
2N/A dst->sh_link = src->sh_link;
2N/A dst->sh_info = src->sh_info;
2N/A dst->sh_addralign = src->sh_addralign;
2N/A dst->sh_entsize = src->sh_entsize;
2N/A}
2N/A
2N/A/*
2N/A * In order to mmap a section from the ELF file, we must round down sh_offset
2N/A * to the previous page boundary, and mmap the surrounding page. We store
2N/A * the pointer to the start of the actual section data back into sp->cts_data.
2N/A */
2N/Aconst void *
2N/Actf_sect_mmap(ctf_sect_t *sp, int fd)
2N/A{
2N/A size_t pageoff = sp->cts_offset & ~_PAGEMASK;
2N/A
2N/A caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,
2N/A MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);
2N/A
2N/A if (base != MAP_FAILED)
2N/A sp->cts_data = base + pageoff;
2N/A
2N/A return (base);
2N/A}
2N/A
2N/A/*
2N/A * Since sp->cts_data has the adjusted offset, we have to again round down
2N/A * to get the actual mmap address and round up to get the size.
2N/A */
2N/Avoid
2N/Actf_sect_munmap(const ctf_sect_t *sp)
2N/A{
2N/A uintptr_t addr = (uintptr_t)sp->cts_data;
2N/A uintptr_t pageoff = addr & ~_PAGEMASK;
2N/A
2N/A (void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);
2N/A}
2N/A
2N/A/*
2N/A * Open the specified file descriptor and return a pointer to a CTF container.
2N/A * The file can be either an ELF file or raw CTF file. The caller is
2N/A * responsible for closing the file descriptor when it is no longer needed.
2N/A */
2N/Actf_file_t *
2N/Actf_fdopen(int fd, int *errp)
2N/A{
2N/A ctf_sect_t ctfsect, symsect, strsect;
2N/A ctf_file_t *fp = NULL;
2N/A
2N/A struct stat64 st;
2N/A ssize_t nbytes;
2N/A
2N/A union {
2N/A ctf_preamble_t ctf;
2N/A Elf32_Ehdr e32;
2N/A GElf_Ehdr e64;
2N/A } hdr;
2N/A
2N/A bzero(&ctfsect, sizeof (ctf_sect_t));
2N/A bzero(&symsect, sizeof (ctf_sect_t));
2N/A bzero(&strsect, sizeof (ctf_sect_t));
2N/A bzero(&hdr.ctf, sizeof (hdr));
2N/A
2N/A if (fstat64(fd, &st) == -1)
2N/A return (ctf_set_open_errno(errp, errno));
2N/A
2N/A if ((nbytes = pread64(fd, &hdr.ctf, sizeof (hdr), 0)) <= 0)
2N/A return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));
2N/A
2N/A /*
2N/A * If we have read enough bytes to form a CTF header and the magic
2N/A * string matches, attempt to interpret the file as raw CTF.
2N/A */
2N/A if (nbytes >= sizeof (ctf_preamble_t) &&
2N/A hdr.ctf.ctp_magic == CTF_MAGIC) {
2N/A if (hdr.ctf.ctp_version > CTF_VERSION)
2N/A return (ctf_set_open_errno(errp, ECTF_CTFVERS));
2N/A
2N/A ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,
2N/A MAP_PRIVATE, fd, 0);
2N/A
2N/A if (ctfsect.cts_data == MAP_FAILED)
2N/A return (ctf_set_open_errno(errp, errno));
2N/A
2N/A ctfsect.cts_name = _CTF_SECTION;
2N/A ctfsect.cts_type = SHT_PROGBITS;
2N/A ctfsect.cts_flags = SHF_ALLOC;
2N/A ctfsect.cts_size = (size_t)st.st_size;
2N/A ctfsect.cts_entsize = 1;
2N/A ctfsect.cts_offset = 0;
2N/A
2N/A if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)
2N/A ctf_sect_munmap(&ctfsect);
2N/A
2N/A return (fp);
2N/A }
2N/A
2N/A /*
2N/A * If we have read enough bytes to form an ELF header and the magic
2N/A * string matches, attempt to interpret the file as an ELF file. We
2N/A * do our own largefile ELF processing, and convert everything to
2N/A * GElf structures so that clients can operate on any data model.
2N/A */
2N/A if (nbytes >= sizeof (Elf32_Ehdr) &&
2N/A bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {
2N/A#ifdef _BIG_ENDIAN
2N/A uchar_t order = ELFDATA2MSB;
2N/A#else
2N/A uchar_t order = ELFDATA2LSB;
2N/A#endif
2N/A GElf_Half i, n;
2N/A GElf_Shdr *sp;
2N/A
2N/A void *strs_map;
2N/A size_t strs_mapsz;
2N/A const char *strs;
2N/A
2N/A if (hdr.e32.e_ident[EI_DATA] != order)
2N/A return (ctf_set_open_errno(errp, ECTF_ENDIAN));
2N/A if (hdr.e32.e_version != EV_CURRENT)
2N/A return (ctf_set_open_errno(errp, ECTF_ELFVERS));
2N/A
2N/A if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {
2N/A if (nbytes < sizeof (GElf_Ehdr))
2N/A return (ctf_set_open_errno(errp, ECTF_FMT));
2N/A } else {
2N/A Elf32_Ehdr e32 = hdr.e32;
2N/A ehdr_to_gelf(&e32, &hdr.e64);
2N/A }
2N/A
2N/A if (hdr.e64.e_shstrndx >= hdr.e64.e_shnum)
2N/A return (ctf_set_open_errno(errp, ECTF_CORRUPT));
2N/A
2N/A n = hdr.e64.e_shnum;
2N/A nbytes = sizeof (GElf_Shdr) * n;
2N/A
2N/A if ((sp = malloc(nbytes)) == NULL)
2N/A return (ctf_set_open_errno(errp, errno));
2N/A
2N/A /*
2N/A * Read in and convert to GElf the array of Shdr structures
2N/A * from e_shoff so we can locate sections of interest.
2N/A */
2N/A if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {
2N/A Elf32_Shdr *sp32;
2N/A
2N/A nbytes = sizeof (Elf32_Shdr) * n;
2N/A
2N/A if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,
2N/A sp32, nbytes, hdr.e64.e_shoff) != nbytes) {
2N/A free(sp);
2N/A return (ctf_set_open_errno(errp, errno));
2N/A }
2N/A
2N/A for (i = 0; i < n; i++)
2N/A shdr_to_gelf(&sp32[i], &sp[i]);
2N/A
2N/A free(sp32);
2N/A
2N/A } else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {
2N/A free(sp);
2N/A return (ctf_set_open_errno(errp, errno));
2N/A }
2N/A
2N/A /*
2N/A * Now mmap the section header strings section so that we can
2N/A * perform string comparison on the section names.
2N/A */
2N/A strs_mapsz = sp[hdr.e64.e_shstrndx].sh_size +
2N/A (sp[hdr.e64.e_shstrndx].sh_offset & ~_PAGEMASK);
2N/A
2N/A strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,
2N/A fd, sp[hdr.e64.e_shstrndx].sh_offset & _PAGEMASK);
2N/A
2N/A strs = (const char *)strs_map +
2N/A (sp[hdr.e64.e_shstrndx].sh_offset & ~_PAGEMASK);
2N/A
2N/A if (strs_map == MAP_FAILED) {
2N/A free(sp);
2N/A return (ctf_set_open_errno(errp, ECTF_MMAP));
2N/A }
2N/A
2N/A /*
2N/A * Iterate over the section header array looking for the CTF
2N/A * section and symbol table. The strtab is linked to symtab.
2N/A */
2N/A for (i = 0; i < n; i++) {
2N/A const GElf_Shdr *shp = &sp[i];
2N/A const GElf_Shdr *lhp = &sp[shp->sh_link];
2N/A
2N/A if (shp->sh_link >= hdr.e64.e_shnum)
2N/A continue; /* corrupt sh_link field */
2N/A
2N/A if (shp->sh_name >= sp[hdr.e64.e_shstrndx].sh_size ||
2N/A lhp->sh_name >= sp[hdr.e64.e_shstrndx].sh_size)
2N/A continue; /* corrupt sh_name field */
2N/A
2N/A if (shp->sh_type == SHT_PROGBITS &&
2N/A strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {
2N/A ctfsect.cts_name = strs + shp->sh_name;
2N/A ctfsect.cts_type = shp->sh_type;
2N/A ctfsect.cts_flags = shp->sh_flags;
2N/A ctfsect.cts_size = shp->sh_size;
2N/A ctfsect.cts_entsize = shp->sh_entsize;
2N/A ctfsect.cts_offset = (off64_t)shp->sh_offset;
2N/A
2N/A } else if (shp->sh_type == SHT_SYMTAB) {
2N/A symsect.cts_name = strs + shp->sh_name;
2N/A symsect.cts_type = shp->sh_type;
2N/A symsect.cts_flags = shp->sh_flags;
2N/A symsect.cts_size = shp->sh_size;
2N/A symsect.cts_entsize = shp->sh_entsize;
2N/A symsect.cts_offset = (off64_t)shp->sh_offset;
2N/A
2N/A strsect.cts_name = strs + lhp->sh_name;
2N/A strsect.cts_type = lhp->sh_type;
2N/A strsect.cts_flags = lhp->sh_flags;
2N/A strsect.cts_size = lhp->sh_size;
2N/A strsect.cts_entsize = lhp->sh_entsize;
2N/A strsect.cts_offset = (off64_t)lhp->sh_offset;
2N/A }
2N/A }
2N/A
2N/A free(sp); /* free section header array */
2N/A
2N/A if (ctfsect.cts_type == SHT_NULL) {
2N/A (void) munmap(strs_map, strs_mapsz);
2N/A return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));
2N/A }
2N/A
2N/A /*
2N/A * Now mmap the CTF data, symtab, and strtab sections and
2N/A * call ctf_bufopen() to do the rest of the work.
2N/A */
2N/A if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {
2N/A (void) munmap(strs_map, strs_mapsz);
2N/A return (ctf_set_open_errno(errp, ECTF_MMAP));
2N/A }
2N/A
2N/A if (symsect.cts_type != SHT_NULL &&
2N/A strsect.cts_type != SHT_NULL) {
2N/A if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||
2N/A ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {
2N/A (void) ctf_set_open_errno(errp, ECTF_MMAP);
2N/A goto bad; /* unmap all and abort */
2N/A }
2N/A fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);
2N/A } else
2N/A fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);
2N/Abad:
2N/A if (fp == NULL) {
2N/A ctf_sect_munmap(&ctfsect);
2N/A ctf_sect_munmap(&symsect);
2N/A ctf_sect_munmap(&strsect);
2N/A } else
2N/A fp->ctf_flags |= LCTF_MMAP;
2N/A
2N/A (void) munmap(strs_map, strs_mapsz);
2N/A return (fp);
2N/A }
2N/A
2N/A return (ctf_set_open_errno(errp, ECTF_FMT));
2N/A}
2N/A
2N/A/*
2N/A * Open the specified file and return a pointer to a CTF container. The file
2N/A * can be either an ELF file or raw CTF file. This is just a convenient
2N/A * wrapper around ctf_fdopen() for callers.
2N/A */
2N/Actf_file_t *
2N/Actf_open(const char *filename, int *errp)
2N/A{
2N/A ctf_file_t *fp;
2N/A int fd;
2N/A
2N/A if ((fd = open64(filename, O_RDONLY)) == -1) {
2N/A if (errp != NULL)
2N/A *errp = errno;
2N/A return (NULL);
2N/A }
2N/A
2N/A fp = ctf_fdopen(fd, errp);
2N/A (void) close(fd);
2N/A return (fp);
2N/A}
2N/A
2N/A/*
2N/A * Write the uncompressed CTF data stream to the specified file descriptor.
2N/A * This is useful for saving the results of dynamic CTF containers.
2N/A */
2N/Aint
2N/Actf_write(ctf_file_t *fp, int fd)
2N/A{
2N/A const uchar_t *buf = fp->ctf_base;
2N/A ssize_t resid = fp->ctf_size;
2N/A ssize_t len;
2N/A
2N/A while (resid != 0) {
2N/A if ((len = write(fd, buf, resid)) <= 0)
2N/A return (ctf_set_errno(fp, errno));
2N/A resid -= len;
2N/A buf += len;
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Set the CTF library client version to the specified version. If version is
2N/A * zero, we just return the default library version number.
2N/A */
2N/Aint
2N/Actf_version(int version)
2N/A{
2N/A if (version < 0) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A
2N/A if (version > 0) {
2N/A if (version > CTF_VERSION) {
2N/A errno = ENOTSUP;
2N/A return (-1);
2N/A }
2N/A ctf_dprintf("ctf_version: client using version %d\n", version);
2N/A _libctf_version = version;
2N/A }
2N/A
2N/A return (_libctf_version);
2N/A}