Path: blob/main/cddl/contrib/opensolaris/lib/libctf/common/ctf_lib.c
39535 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2003 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728#include <sys/types.h>29#include <sys/endian.h>30#include <sys/stat.h>31#include <sys/mman.h>32#include <sys/zmod.h>33#include <ctf_impl.h>34#include <unistd.h>35#include <fcntl.h>36#include <errno.h>37#ifdef illumos38#include <dlfcn.h>39#else40#include <zlib.h>41#endif42#include <gelf.h>4344#ifdef illumos45#ifdef _LP6446static const char *_libctf_zlib = "/usr/lib/64/libz.so";47#else48static const char *_libctf_zlib = "/usr/lib/libz.so";49#endif50#endif5152static struct {53int (*z_uncompress)(uchar_t *, ulong_t *, const uchar_t *, ulong_t);54const char *(*z_error)(int);55void *z_dlp;56} zlib;5758static size_t _PAGESIZE;59static size_t _PAGEMASK;6061#ifdef illumos62#pragma init(_libctf_init)63#else64void _libctf_init(void) __attribute__ ((constructor));65#endif66void67_libctf_init(void)68{69#ifdef illumos70const char *p = getenv("LIBCTF_DECOMPRESSOR");7172if (p != NULL)73_libctf_zlib = p; /* use alternate decompression library */74#endif7576_libctf_debug = getenv("LIBCTF_DEBUG") != NULL;7778_PAGESIZE = getpagesize();79_PAGEMASK = ~(_PAGESIZE - 1);80}8182/*83* Attempt to dlopen the decompression library and locate the symbols of84* interest that we will need to call. This information in cached so85* that multiple calls to ctf_bufopen() do not need to reopen the library.86*/87void *88ctf_zopen(int *errp)89{90#ifdef illumos91ctf_dprintf("decompressing CTF data using %s\n", _libctf_zlib);9293if (zlib.z_dlp != NULL)94return (zlib.z_dlp); /* library is already loaded */9596if (access(_libctf_zlib, R_OK) == -1)97return (ctf_set_open_errno(errp, ECTF_ZMISSING));9899if ((zlib.z_dlp = dlopen(_libctf_zlib, RTLD_LAZY | RTLD_LOCAL)) == NULL)100return (ctf_set_open_errno(errp, ECTF_ZINIT));101102zlib.z_uncompress = (int (*)(uchar_t *, ulong_t *, const uchar_t *, ulong_t)) dlsym(zlib.z_dlp, "uncompress");103zlib.z_error = (const char *(*)(int)) dlsym(zlib.z_dlp, "zError");104105if (zlib.z_uncompress == NULL || zlib.z_error == NULL) {106(void) dlclose(zlib.z_dlp);107bzero(&zlib, sizeof (zlib));108return (ctf_set_open_errno(errp, ECTF_ZINIT));109}110#else111zlib.z_uncompress = uncompress;112zlib.z_error = zError;113114/* Dummy return variable as 'no error' */115zlib.z_dlp = (void *) (uintptr_t) 1;116#endif117118return (zlib.z_dlp);119}120121/*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.124*/125int126z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)127{128return (zlib.z_uncompress(dst, (ulong_t *)dstlen, src, srclen));129}130131const char *132z_strerror(int err)133{134return (zlib.z_error(err));135}136137/*138* Convert a 32-bit ELF file header into GElf.139*/140static void141ehdr_to_gelf(const Elf32_Ehdr *src, GElf_Ehdr *dst)142{143bcopy(src->e_ident, dst->e_ident, EI_NIDENT);144dst->e_type = src->e_type;145dst->e_machine = src->e_machine;146dst->e_version = src->e_version;147dst->e_entry = (Elf64_Addr)src->e_entry;148dst->e_phoff = (Elf64_Off)src->e_phoff;149dst->e_shoff = (Elf64_Off)src->e_shoff;150dst->e_flags = src->e_flags;151dst->e_ehsize = src->e_ehsize;152dst->e_phentsize = src->e_phentsize;153dst->e_phnum = src->e_phnum;154dst->e_shentsize = src->e_shentsize;155dst->e_shnum = src->e_shnum;156dst->e_shstrndx = src->e_shstrndx;157}158159/*160* Convert a 32-bit ELF section header into GElf.161*/162static void163shdr_to_gelf(const Elf32_Shdr *src, GElf_Shdr *dst)164{165dst->sh_name = src->sh_name;166dst->sh_type = src->sh_type;167dst->sh_flags = src->sh_flags;168dst->sh_addr = src->sh_addr;169dst->sh_offset = src->sh_offset;170dst->sh_size = src->sh_size;171dst->sh_link = src->sh_link;172dst->sh_info = src->sh_info;173dst->sh_addralign = src->sh_addralign;174dst->sh_entsize = src->sh_entsize;175}176177/*178* In order to mmap a section from the ELF file, we must round down sh_offset179* to the previous page boundary, and mmap the surrounding page. We store180* the pointer to the start of the actual section data back into sp->cts_data.181*/182const void *183ctf_sect_mmap(ctf_sect_t *sp, int fd)184{185size_t pageoff = sp->cts_offset & ~_PAGEMASK;186187caddr_t base = mmap64(NULL, sp->cts_size + pageoff, PROT_READ,188MAP_PRIVATE, fd, sp->cts_offset & _PAGEMASK);189190if (base != MAP_FAILED)191sp->cts_data = base + pageoff;192193return (base);194}195196/*197* Since sp->cts_data has the adjusted offset, we have to again round down198* to get the actual mmap address and round up to get the size.199*/200void201ctf_sect_munmap(const ctf_sect_t *sp)202{203uintptr_t addr = (uintptr_t)sp->cts_data;204uintptr_t pageoff = addr & ~_PAGEMASK;205206(void) munmap((void *)(addr - pageoff), sp->cts_size + pageoff);207}208209/*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 is212* responsible for closing the file descriptor when it is no longer needed.213*/214ctf_file_t *215ctf_fdopen(int fd, int *errp)216{217ctf_sect_t ctfsect, symsect, strsect;218ctf_file_t *fp = NULL;219size_t shstrndx, shnum;220221struct stat64 st;222ssize_t nbytes;223224union {225ctf_preamble_t ctf;226Elf32_Ehdr e32;227GElf_Ehdr e64;228} hdr;229230bzero(&ctfsect, sizeof (ctf_sect_t));231bzero(&symsect, sizeof (ctf_sect_t));232bzero(&strsect, sizeof (ctf_sect_t));233bzero(&hdr, sizeof (hdr));234235if (fstat64(fd, &st) == -1)236return (ctf_set_open_errno(errp, errno));237238if ((nbytes = pread64(fd, &hdr, sizeof (hdr), 0)) <= 0)239return (ctf_set_open_errno(errp, nbytes < 0? errno : ECTF_FMT));240241/*242* If we have read enough bytes to form a CTF header and the magic243* string matches, attempt to interpret the file as raw CTF.244*/245if (nbytes >= (ssize_t) sizeof (ctf_preamble_t) &&246hdr.ctf.ctp_magic == CTF_MAGIC) {247if (hdr.ctf.ctp_version != CTF_VERSION_2 &&248hdr.ctf.ctp_version != CTF_VERSION_3)249return (ctf_set_open_errno(errp, ECTF_CTFVERS));250251ctfsect.cts_data = mmap64(NULL, st.st_size, PROT_READ,252MAP_PRIVATE, fd, 0);253254if (ctfsect.cts_data == MAP_FAILED)255return (ctf_set_open_errno(errp, errno));256257ctfsect.cts_name = _CTF_SECTION;258ctfsect.cts_type = SHT_PROGBITS;259ctfsect.cts_flags = SHF_ALLOC;260ctfsect.cts_size = (size_t)st.st_size;261ctfsect.cts_entsize = 1;262ctfsect.cts_offset = 0;263264if ((fp = ctf_bufopen(&ctfsect, NULL, NULL, errp)) == NULL)265ctf_sect_munmap(&ctfsect);266267return (fp);268}269270/*271* If we have read enough bytes to form an ELF header and the magic272* string matches, attempt to interpret the file as an ELF file. We273* do our own largefile ELF processing, and convert everything to274* GElf structures so that clients can operate on any data model.275*/276if (nbytes >= (ssize_t) sizeof (Elf32_Ehdr) &&277bcmp(&hdr.e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0) {278#if BYTE_ORDER == _BIG_ENDIAN279uchar_t order = ELFDATA2MSB;280#else281uchar_t order = ELFDATA2LSB;282#endif283GElf_Shdr *sp;284285void *strs_map;286size_t strs_mapsz, i;287char *strs;288289if (hdr.e32.e_ident[EI_DATA] != order)290return (ctf_set_open_errno(errp, ECTF_ENDIAN));291if (hdr.e32.e_version != EV_CURRENT)292return (ctf_set_open_errno(errp, ECTF_ELFVERS));293294if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS64) {295if (nbytes < (ssize_t) sizeof (GElf_Ehdr))296return (ctf_set_open_errno(errp, ECTF_FMT));297} else {298Elf32_Ehdr e32 = hdr.e32;299ehdr_to_gelf(&e32, &hdr.e64);300}301302shnum = hdr.e64.e_shnum;303shstrndx = hdr.e64.e_shstrndx;304305/* Extended ELF sections */306if ((shstrndx == SHN_XINDEX) || (shnum == 0)) {307if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {308Elf32_Shdr x32;309310if (pread64(fd, &x32, sizeof (x32),311hdr.e64.e_shoff) != sizeof (x32))312return (ctf_set_open_errno(errp,313errno));314315shnum = x32.sh_size;316shstrndx = x32.sh_link;317} else {318Elf64_Shdr x64;319320if (pread64(fd, &x64, sizeof (x64),321hdr.e64.e_shoff) != sizeof (x64))322return (ctf_set_open_errno(errp,323errno));324325shnum = x64.sh_size;326shstrndx = x64.sh_link;327}328}329330if (shstrndx >= shnum)331return (ctf_set_open_errno(errp, ECTF_CORRUPT));332333nbytes = sizeof (GElf_Shdr) * shnum;334335if ((sp = malloc(nbytes)) == NULL)336return (ctf_set_open_errno(errp, errno));337338/*339* Read in and convert to GElf the array of Shdr structures340* from e_shoff so we can locate sections of interest.341*/342if (hdr.e32.e_ident[EI_CLASS] == ELFCLASS32) {343Elf32_Shdr *sp32;344345nbytes = sizeof (Elf32_Shdr) * shnum;346347if ((sp32 = malloc(nbytes)) == NULL || pread64(fd,348sp32, nbytes, hdr.e64.e_shoff) != nbytes) {349free(sp);350free(sp32);351return (ctf_set_open_errno(errp, errno));352}353354for (i = 0; i < shnum; i++)355shdr_to_gelf(&sp32[i], &sp[i]);356357free(sp32);358359} else if (pread64(fd, sp, nbytes, hdr.e64.e_shoff) != nbytes) {360free(sp);361return (ctf_set_open_errno(errp, errno));362}363364/*365* Now mmap the section header strings section so that we can366* perform string comparison on the section names.367*/368strs_mapsz = sp[shstrndx].sh_size +369(sp[shstrndx].sh_offset & ~_PAGEMASK);370371strs_map = mmap64(NULL, strs_mapsz, PROT_READ, MAP_PRIVATE,372fd, sp[shstrndx].sh_offset & _PAGEMASK);373374strs = (char *)strs_map +375(sp[shstrndx].sh_offset & ~_PAGEMASK);376377if (strs_map == MAP_FAILED) {378free(sp);379return (ctf_set_open_errno(errp, ECTF_MMAP));380}381382/*383* Iterate over the section header array looking for the CTF384* section and symbol table. The strtab is linked to symtab.385*/386for (i = 0; i < shnum; i++) {387const GElf_Shdr *shp = &sp[i];388const GElf_Shdr *lhp = &sp[shp->sh_link];389390if (shp->sh_link >= shnum)391continue; /* corrupt sh_link field */392393if (shp->sh_name >= sp[shstrndx].sh_size ||394lhp->sh_name >= sp[shstrndx].sh_size)395continue; /* corrupt sh_name field */396397if (shp->sh_type == SHT_PROGBITS &&398strcmp(strs + shp->sh_name, _CTF_SECTION) == 0) {399ctfsect.cts_name = strs + shp->sh_name;400ctfsect.cts_type = shp->sh_type;401ctfsect.cts_flags = shp->sh_flags;402ctfsect.cts_size = shp->sh_size;403ctfsect.cts_entsize = shp->sh_entsize;404ctfsect.cts_offset = (off64_t)shp->sh_offset;405406} else if (shp->sh_type == SHT_SYMTAB) {407symsect.cts_name = strs + shp->sh_name;408symsect.cts_type = shp->sh_type;409symsect.cts_flags = shp->sh_flags;410symsect.cts_size = shp->sh_size;411symsect.cts_entsize = shp->sh_entsize;412symsect.cts_offset = (off64_t)shp->sh_offset;413414strsect.cts_name = strs + lhp->sh_name;415strsect.cts_type = lhp->sh_type;416strsect.cts_flags = lhp->sh_flags;417strsect.cts_size = lhp->sh_size;418strsect.cts_entsize = lhp->sh_entsize;419strsect.cts_offset = (off64_t)lhp->sh_offset;420}421}422423free(sp); /* free section header array */424425if (ctfsect.cts_type == SHT_NULL) {426(void) munmap(strs_map, strs_mapsz);427return (ctf_set_open_errno(errp, ECTF_NOCTFDATA));428}429430/*431* Now mmap the CTF data, symtab, and strtab sections and432* call ctf_bufopen() to do the rest of the work.433*/434if (ctf_sect_mmap(&ctfsect, fd) == MAP_FAILED) {435(void) munmap(strs_map, strs_mapsz);436return (ctf_set_open_errno(errp, ECTF_MMAP));437}438439if (symsect.cts_type != SHT_NULL &&440strsect.cts_type != SHT_NULL) {441if (ctf_sect_mmap(&symsect, fd) == MAP_FAILED ||442ctf_sect_mmap(&strsect, fd) == MAP_FAILED) {443(void) ctf_set_open_errno(errp, ECTF_MMAP);444goto bad; /* unmap all and abort */445}446fp = ctf_bufopen(&ctfsect, &symsect, &strsect, errp);447} else448fp = ctf_bufopen(&ctfsect, NULL, NULL, errp);449bad:450if (fp == NULL) {451ctf_sect_munmap(&ctfsect);452ctf_sect_munmap(&symsect);453ctf_sect_munmap(&strsect);454} else455fp->ctf_flags |= LCTF_MMAP;456457(void) munmap(strs_map, strs_mapsz);458return (fp);459}460461return (ctf_set_open_errno(errp, ECTF_FMT));462}463464/*465* Open the specified file and return a pointer to a CTF container. The file466* can be either an ELF file or raw CTF file. This is just a convenient467* wrapper around ctf_fdopen() for callers.468*/469ctf_file_t *470ctf_open(const char *filename, int *errp)471{472ctf_file_t *fp;473int fd;474475if ((fd = open64(filename, O_RDONLY)) == -1) {476if (errp != NULL)477*errp = errno;478return (NULL);479}480481fp = ctf_fdopen(fd, errp);482(void) close(fd);483return (fp);484}485486/*487* Write the uncompressed CTF data stream to the specified file descriptor.488* This is useful for saving the results of dynamic CTF containers.489*/490int491ctf_write(ctf_file_t *fp, int fd)492{493const uchar_t *buf = fp->ctf_base;494ssize_t resid = fp->ctf_size;495ssize_t len;496497while (resid != 0) {498if ((len = write(fd, buf, resid)) <= 0)499return (ctf_set_errno(fp, errno));500resid -= len;501buf += len;502}503504return (0);505}506507/*508* Set the CTF library client version to the specified version. If version is509* zero, we just return the default library version number.510*/511int512ctf_version(int version)513{514if (version < 0) {515errno = EINVAL;516return (-1);517}518519if (version > 0) {520if (version > CTF_VERSION) {521errno = ENOTSUP;522return (-1);523}524ctf_dprintf("ctf_version: client using version %d\n", version);525_libctf_version = version;526}527528return (_libctf_version);529}530531532