The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_ctf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2008 John Birrell <jb@freebsd.org>
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  *
   28  * $FreeBSD$
   29  */
   30 
   31 /*
   32  * Note this file is included by both link_elf.c and link_elf_obj.c.
   33  *
   34  * The CTF header structure definition can't be used here because it's
   35  * (annoyingly) covered by the CDDL. We will just use a few bytes from
   36  * it as an integer array where we 'know' what they mean.
   37  */
   38 #define CTF_HDR_SIZE            36
   39 #define CTF_HDR_STRTAB_U32      7
   40 #define CTF_HDR_STRLEN_U32      8
   41 
   42 #ifdef DDB_CTF
   43 #include <contrib/zlib/zlib.h>
   44 #endif
   45 
   46 static int
   47 link_elf_ctf_get(linker_file_t lf, linker_ctf_t *lc)
   48 {
   49 #ifdef DDB_CTF
   50         Elf_Ehdr *hdr = NULL;
   51         Elf_Shdr *shdr = NULL;
   52         caddr_t ctftab = NULL;
   53         caddr_t raw = NULL;
   54         caddr_t shstrtab = NULL;
   55         elf_file_t ef = (elf_file_t) lf;
   56         int flags;
   57         int i;
   58         int nbytes;
   59         size_t sz;
   60         struct nameidata nd;
   61         struct thread *td = curthread;
   62         uint8_t ctf_hdr[CTF_HDR_SIZE];
   63 #endif
   64         int error = 0;
   65 
   66         if (lf == NULL || lc == NULL)
   67                 return (EINVAL);
   68 
   69         /* Set the defaults for no CTF present. That's not a crime! */
   70         bzero(lc, sizeof(*lc));
   71 
   72 #ifdef DDB_CTF
   73         /*
   74          * First check if we've tried to load CTF data previously and the
   75          * CTF ELF section wasn't found. We flag that condition by setting
   76          * ctfcnt to -1. See below.
   77          */
   78         if (ef->ctfcnt < 0)
   79                 return (EFTYPE);
   80 
   81         /* Now check if we've already loaded the CTF data.. */
   82         if (ef->ctfcnt > 0) {
   83                 /* We only need to load once. */
   84                 lc->ctftab = ef->ctftab;
   85                 lc->ctfcnt = ef->ctfcnt;
   86                 lc->symtab = ef->ddbsymtab;
   87                 lc->strtab = ef->ddbstrtab;
   88                 lc->strcnt = ef->ddbstrcnt;
   89                 lc->nsym   = ef->ddbsymcnt;
   90                 lc->ctfoffp = (uint32_t **) &ef->ctfoff;
   91                 lc->typoffp = (uint32_t **) &ef->typoff;
   92                 lc->typlenp = &ef->typlen;
   93                 return (0);
   94         }
   95 
   96         /*
   97          * We need to try reading the CTF data. Flag no CTF data present
   98          * by default and if we actually succeed in reading it, we'll
   99          * update ctfcnt to the number of bytes read.
  100          */
  101         ef->ctfcnt = -1;
  102 
  103         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, lf->pathname, td);
  104         flags = FREAD;
  105         error = vn_open(&nd, &flags, 0, NULL);
  106         if (error)
  107                 return (error);
  108         NDFREE(&nd, NDF_ONLY_PNBUF);
  109 
  110         /* Allocate memory for the FLF header. */
  111         hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
  112 
  113         /* Read the ELF header. */
  114         if ((error = vn_rdwr(UIO_READ, nd.ni_vp, hdr, sizeof(*hdr),
  115             0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL,
  116             td)) != 0)
  117                 goto out;
  118 
  119         /* Sanity check. */
  120         if (!IS_ELF(*hdr)) {
  121                 error = ENOEXEC;
  122                 goto out;
  123         }
  124 
  125         nbytes = hdr->e_shnum * hdr->e_shentsize;
  126         if (nbytes == 0 || hdr->e_shoff == 0 ||
  127             hdr->e_shentsize != sizeof(Elf_Shdr)) {
  128                 error = ENOEXEC;
  129                 goto out;
  130         }
  131 
  132         /* Allocate memory for all the section headers */
  133         shdr = malloc(nbytes, M_LINKER, M_WAITOK);
  134 
  135         /* Read all the section headers */
  136         if ((error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes,
  137             hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  138             NULL, td)) != 0)
  139                 goto out;
  140 
  141         /*
  142          * We need to search for the CTF section by name, so if the
  143          * section names aren't present, then we can't locate the
  144          * .SUNW_ctf section containing the CTF data.
  145          */
  146         if (hdr->e_shstrndx == 0 || shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
  147                 printf("%s(%d): module %s e_shstrndx is %d, sh_type is %d\n",
  148                     __func__, __LINE__, lf->pathname, hdr->e_shstrndx,
  149                     shdr[hdr->e_shstrndx].sh_type);
  150                 error = EFTYPE;
  151                 goto out;
  152         }
  153 
  154         /* Allocate memory to buffer the section header strings. */
  155         shstrtab = malloc(shdr[hdr->e_shstrndx].sh_size, M_LINKER, M_WAITOK);
  156 
  157         /* Read the section header strings. */
  158         if ((error = vn_rdwr(UIO_READ, nd.ni_vp, shstrtab,
  159             shdr[hdr->e_shstrndx].sh_size, shdr[hdr->e_shstrndx].sh_offset,
  160             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, NULL, td)) != 0)
  161                 goto out;
  162 
  163         /* Search for the section containing the CTF data. */
  164         for (i = 0; i < hdr->e_shnum; i++)
  165                 if (strcmp(".SUNW_ctf", shstrtab + shdr[i].sh_name) == 0)
  166                         break;
  167 
  168         /* Check if the CTF section wasn't found. */
  169         if (i >= hdr->e_shnum) {
  170                 printf("%s(%d): module %s has no .SUNW_ctf section\n",
  171                     __func__, __LINE__, lf->pathname);
  172                 error = EFTYPE;
  173                 goto out;
  174         }
  175 
  176         /* Read the CTF header. */
  177         if ((error = vn_rdwr(UIO_READ, nd.ni_vp, ctf_hdr, sizeof(ctf_hdr),
  178             shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
  179             NOCRED, NULL, td)) != 0)
  180                 goto out;
  181 
  182         /* Check the CTF magic number. */
  183 #ifdef __LITTLE_ENDIAN__
  184         if (ctf_hdr[0] != 0xf1 || ctf_hdr[1] != 0xcf) {
  185 #else
  186         if (ctf_hdr[0] != 0xcf || ctf_hdr[1] != 0xf1) {
  187 #endif
  188                 printf("%s(%d): module %s has invalid format\n",
  189                     __func__, __LINE__, lf->pathname);
  190                 error = EFTYPE;
  191                 goto out;
  192         }
  193 
  194         /* Check if version 2. */
  195         if (ctf_hdr[2] != 2) {
  196                 printf("%s(%d): module %s CTF format version is %d "
  197                     "(2 expected)\n",
  198                     __func__, __LINE__, lf->pathname, ctf_hdr[2]);
  199                 error = EFTYPE;
  200                 goto out;
  201         }
  202 
  203         /* Check if the data is compressed. */
  204         if ((ctf_hdr[3] & 0x1) != 0) {
  205                 uint32_t *u32 = (uint32_t *) ctf_hdr;
  206 
  207                 /*
  208                  * The last two fields in the CTF header are the offset
  209                  * from the end of the header to the start of the string
  210                  * data and the length of that string data. se this
  211                  * information to determine the decompressed CTF data
  212                  * buffer required.
  213                  */
  214                 sz = u32[CTF_HDR_STRTAB_U32] + u32[CTF_HDR_STRLEN_U32] +
  215                     sizeof(ctf_hdr);
  216 
  217                 /*
  218                  * Allocate memory for the compressed CTF data, including
  219                  * the header (which isn't compressed).
  220                  */
  221                 raw = malloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
  222         } else {
  223                 /*
  224                  * The CTF data is not compressed, so the ELF section
  225                  * size is the same as the buffer size required.
  226                  */
  227                 sz = shdr[i].sh_size;
  228         }
  229 
  230         /*
  231          * Allocate memory to buffer the CTF data in its decompressed
  232          * form.
  233          */
  234         ctftab = malloc(sz, M_LINKER, M_WAITOK);
  235 
  236         /*
  237          * Read the CTF data into the raw buffer if compressed, or
  238          * directly into the CTF buffer otherwise.
  239          */
  240         if ((error = vn_rdwr(UIO_READ, nd.ni_vp, raw == NULL ? ctftab : raw,
  241             shdr[i].sh_size, shdr[i].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
  242             td->td_ucred, NOCRED, NULL, td)) != 0)
  243                 goto out;
  244 
  245         /* Check if decompression is required. */
  246         if (raw != NULL) {
  247                 z_stream zs;
  248                 int ret;
  249 
  250                 /*
  251                  * The header isn't compressed, so copy that into the
  252                  * CTF buffer first.
  253                  */
  254                 bcopy(ctf_hdr, ctftab, sizeof(ctf_hdr));
  255 
  256                 /* Initialise the zlib structure. */
  257                 bzero(&zs, sizeof(zs));
  258 
  259                 if (inflateInit(&zs) != Z_OK) {
  260                         error = EIO;
  261                         goto out;
  262                 }
  263 
  264                 zs.avail_in = shdr[i].sh_size - sizeof(ctf_hdr);
  265                 zs.next_in = ((uint8_t *) raw) + sizeof(ctf_hdr);
  266                 zs.avail_out = sz - sizeof(ctf_hdr);
  267                 zs.next_out = ((uint8_t *) ctftab) + sizeof(ctf_hdr);
  268                 ret = inflate(&zs, Z_FINISH);
  269                 inflateEnd(&zs);
  270                 if (ret != Z_STREAM_END) {
  271                         printf("%s(%d): zlib inflate returned %d\n", __func__, __LINE__, ret);
  272                         error = EIO;
  273                         goto out;
  274                 }
  275         }
  276 
  277         /* Got the CTF data! */
  278         ef->ctftab = ctftab;
  279         ef->ctfcnt = shdr[i].sh_size;
  280 
  281         /* We'll retain the memory allocated for the CTF data. */
  282         ctftab = NULL;
  283 
  284         /* Let the caller use the CTF data read. */
  285         lc->ctftab = ef->ctftab;
  286         lc->ctfcnt = ef->ctfcnt;
  287         lc->symtab = ef->ddbsymtab;
  288         lc->strtab = ef->ddbstrtab;
  289         lc->strcnt = ef->ddbstrcnt;
  290         lc->nsym   = ef->ddbsymcnt;
  291         lc->ctfoffp = (uint32_t **) &ef->ctfoff;
  292         lc->typoffp = (uint32_t **) &ef->typoff;
  293         lc->typlenp = &ef->typlen;
  294 
  295 out:
  296         VOP_UNLOCK(nd.ni_vp, 0);
  297         vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
  298 
  299         if (hdr != NULL)
  300                 free(hdr, M_LINKER);
  301         if (shdr != NULL)
  302                 free(shdr, M_LINKER);
  303         if (shstrtab != NULL)
  304                 free(shstrtab, M_LINKER);
  305         if (ctftab != NULL)
  306                 free(ctftab, M_LINKER);
  307         if (raw != NULL)
  308                 free(raw, M_LINKER);
  309 #else
  310         error = EOPNOTSUPP;
  311 #endif
  312 
  313         return (error);
  314 }

Cache object: 65dcd966a9f0b51ccb0bdaf80f216f38


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.