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

Cache object: 2df8000a80be656f9d8b99cf26e661b9


[ 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.