1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Routines for retrieving CTF data from a .SUNW_ctf ELF section 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 35*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 36*7c478bd9Sstevel@tonic-gate #include <unistd.h> 37*7c478bd9Sstevel@tonic-gate #include <gelf.h> 38*7c478bd9Sstevel@tonic-gate #include <strings.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include "ctftools.h" 42*7c478bd9Sstevel@tonic-gate #include "memory.h" 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate typedef int read_cb_f(tdata_t *, char *, void *); 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate /* 47*7c478bd9Sstevel@tonic-gate * Return the source types that the object was generated from. 48*7c478bd9Sstevel@tonic-gate */ 49*7c478bd9Sstevel@tonic-gate source_types_t 50*7c478bd9Sstevel@tonic-gate built_source_types(Elf *elf, char const *file) 51*7c478bd9Sstevel@tonic-gate { 52*7c478bd9Sstevel@tonic-gate source_types_t types = SOURCE_NONE; 53*7c478bd9Sstevel@tonic-gate symit_data_t *si; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate if ((si = symit_new(elf, file)) == NULL) 56*7c478bd9Sstevel@tonic-gate return (SOURCE_NONE); 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate while (symit_next(si, STT_FILE) != NULL) { 59*7c478bd9Sstevel@tonic-gate char *name = symit_name(si); 60*7c478bd9Sstevel@tonic-gate size_t len = strlen(name); 61*7c478bd9Sstevel@tonic-gate if (len < 2 || name[len - 2] != '.') { 62*7c478bd9Sstevel@tonic-gate types |= SOURCE_UNKNOWN; 63*7c478bd9Sstevel@tonic-gate continue; 64*7c478bd9Sstevel@tonic-gate } 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate switch (name[len - 1]) { 67*7c478bd9Sstevel@tonic-gate case 'c': 68*7c478bd9Sstevel@tonic-gate types |= SOURCE_C; 69*7c478bd9Sstevel@tonic-gate break; 70*7c478bd9Sstevel@tonic-gate case 'h': 71*7c478bd9Sstevel@tonic-gate /* ignore */ 72*7c478bd9Sstevel@tonic-gate break; 73*7c478bd9Sstevel@tonic-gate case 's': 74*7c478bd9Sstevel@tonic-gate types |= SOURCE_S; 75*7c478bd9Sstevel@tonic-gate break; 76*7c478bd9Sstevel@tonic-gate default: 77*7c478bd9Sstevel@tonic-gate types |= SOURCE_UNKNOWN; 78*7c478bd9Sstevel@tonic-gate } 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate symit_free(si); 82*7c478bd9Sstevel@tonic-gate return (types); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate static int 86*7c478bd9Sstevel@tonic-gate read_file(Elf *elf, char *file, char *label, read_cb_f *func, void *arg, 87*7c478bd9Sstevel@tonic-gate int require_ctf) 88*7c478bd9Sstevel@tonic-gate { 89*7c478bd9Sstevel@tonic-gate Elf_Scn *ctfscn; 90*7c478bd9Sstevel@tonic-gate Elf_Data *ctfdata; 91*7c478bd9Sstevel@tonic-gate symit_data_t *si = NULL; 92*7c478bd9Sstevel@tonic-gate int ctfscnidx; 93*7c478bd9Sstevel@tonic-gate tdata_t *td; 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if ((ctfscnidx = findelfsecidx(elf, ".SUNW_ctf")) < 0) { 96*7c478bd9Sstevel@tonic-gate if (require_ctf && 97*7c478bd9Sstevel@tonic-gate (built_source_types(elf, file) & SOURCE_C)) { 98*7c478bd9Sstevel@tonic-gate terminate("Input file %s was partially built from " 99*7c478bd9Sstevel@tonic-gate "C sources, but no CTF data was present\n", file); 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate return (0); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate if ((ctfscn = elf_getscn(elf, ctfscnidx)) == NULL || 105*7c478bd9Sstevel@tonic-gate (ctfdata = elf_getdata(ctfscn, NULL)) == NULL) 106*7c478bd9Sstevel@tonic-gate elfterminate(file, "Cannot read CTF section"); 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate /* Reconstruction of type tree */ 109*7c478bd9Sstevel@tonic-gate if ((si = symit_new(elf, file)) == NULL) { 110*7c478bd9Sstevel@tonic-gate warning("%s has no symbol table - skipping", file); 111*7c478bd9Sstevel@tonic-gate return (0); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate td = ctf_load(file, ctfdata->d_buf, ctfdata->d_size, si, label); 115*7c478bd9Sstevel@tonic-gate tdata_build_hashes(td); 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate symit_free(si); 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate if (td != NULL) { 120*7c478bd9Sstevel@tonic-gate if (func(td, file, arg) < 0) 121*7c478bd9Sstevel@tonic-gate return (-1); 122*7c478bd9Sstevel@tonic-gate else 123*7c478bd9Sstevel@tonic-gate return (1); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate return (0); 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate static int 129*7c478bd9Sstevel@tonic-gate read_archive(int fd, Elf *elf, char *file, char *label, read_cb_f *func, 130*7c478bd9Sstevel@tonic-gate void *arg, int require_ctf) 131*7c478bd9Sstevel@tonic-gate { 132*7c478bd9Sstevel@tonic-gate Elf *melf; 133*7c478bd9Sstevel@tonic-gate Elf_Cmd cmd = ELF_C_READ; 134*7c478bd9Sstevel@tonic-gate Elf_Arhdr *arh; 135*7c478bd9Sstevel@tonic-gate int secnum = 1, found = 0; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate while ((melf = elf_begin(fd, cmd, elf)) != NULL) { 138*7c478bd9Sstevel@tonic-gate int rc = 0; 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate if ((arh = elf_getarhdr(melf)) == NULL) { 141*7c478bd9Sstevel@tonic-gate elfterminate(file, "Can't get archive header for " 142*7c478bd9Sstevel@tonic-gate "member %d", secnum); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate /* skip special sections - their names begin with "/" */ 146*7c478bd9Sstevel@tonic-gate if (*arh->ar_name != '/') { 147*7c478bd9Sstevel@tonic-gate size_t memlen = strlen(file) + 1 + 148*7c478bd9Sstevel@tonic-gate strlen(arh->ar_name) + 1 + 1; 149*7c478bd9Sstevel@tonic-gate char *memname = xmalloc(memlen); 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate snprintf(memname, memlen, "%s(%s)", file, arh->ar_name); 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate switch (elf_kind(melf)) { 154*7c478bd9Sstevel@tonic-gate case ELF_K_AR: 155*7c478bd9Sstevel@tonic-gate rc = read_archive(fd, melf, memname, label, 156*7c478bd9Sstevel@tonic-gate func, arg, require_ctf); 157*7c478bd9Sstevel@tonic-gate break; 158*7c478bd9Sstevel@tonic-gate case ELF_K_ELF: 159*7c478bd9Sstevel@tonic-gate rc = read_file(melf, memname, label, 160*7c478bd9Sstevel@tonic-gate func, arg, require_ctf); 161*7c478bd9Sstevel@tonic-gate break; 162*7c478bd9Sstevel@tonic-gate default: 163*7c478bd9Sstevel@tonic-gate terminate("%s: Unknown elf kind %d\n", 164*7c478bd9Sstevel@tonic-gate memname, elf_kind(melf)); 165*7c478bd9Sstevel@tonic-gate } 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate free(memname); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate cmd = elf_next(melf); 171*7c478bd9Sstevel@tonic-gate (void) elf_end(melf); 172*7c478bd9Sstevel@tonic-gate secnum++; 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate if (rc < 0) 175*7c478bd9Sstevel@tonic-gate return (rc); 176*7c478bd9Sstevel@tonic-gate else 177*7c478bd9Sstevel@tonic-gate found += rc; 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate return (found); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate 183*7c478bd9Sstevel@tonic-gate static int 184*7c478bd9Sstevel@tonic-gate read_ctf_common(char *file, char *label, read_cb_f *func, void *arg, 185*7c478bd9Sstevel@tonic-gate int require_ctf) 186*7c478bd9Sstevel@tonic-gate { 187*7c478bd9Sstevel@tonic-gate Elf *elf; 188*7c478bd9Sstevel@tonic-gate int found = 0; 189*7c478bd9Sstevel@tonic-gate int fd; 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate debug(3, "Reading %s (label %s)\n", file, (label ? label : "NONE")); 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate (void) elf_version(EV_CURRENT); 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate if ((fd = open(file, O_RDONLY)) < 0) 196*7c478bd9Sstevel@tonic-gate terminate("%s: Cannot open for reading", file); 197*7c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) 198*7c478bd9Sstevel@tonic-gate elfterminate(file, "Cannot read"); 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) { 201*7c478bd9Sstevel@tonic-gate case ELF_K_AR: 202*7c478bd9Sstevel@tonic-gate found = read_archive(fd, elf, file, label, 203*7c478bd9Sstevel@tonic-gate func, arg, require_ctf); 204*7c478bd9Sstevel@tonic-gate break; 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate case ELF_K_ELF: 207*7c478bd9Sstevel@tonic-gate found = read_file(elf, file, label, 208*7c478bd9Sstevel@tonic-gate func, arg, require_ctf); 209*7c478bd9Sstevel@tonic-gate break; 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate default: 212*7c478bd9Sstevel@tonic-gate terminate("%s: Unknown elf kind %d\n", file, elf_kind(elf)); 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate (void) elf_end(elf); 216*7c478bd9Sstevel@tonic-gate (void) close(fd); 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate return (found); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 222*7c478bd9Sstevel@tonic-gate int 223*7c478bd9Sstevel@tonic-gate read_ctf_save_cb(tdata_t *td, char *name, void *retp) 224*7c478bd9Sstevel@tonic-gate { 225*7c478bd9Sstevel@tonic-gate tdata_t **tdp = retp; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate *tdp = td; 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate return (1); 230*7c478bd9Sstevel@tonic-gate } 231*7c478bd9Sstevel@tonic-gate 232*7c478bd9Sstevel@tonic-gate int 233*7c478bd9Sstevel@tonic-gate read_ctf(char **files, int n, char *label, read_cb_f *func, void *private, 234*7c478bd9Sstevel@tonic-gate int require_ctf) 235*7c478bd9Sstevel@tonic-gate { 236*7c478bd9Sstevel@tonic-gate int found; 237*7c478bd9Sstevel@tonic-gate int i, rc; 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate for (i = 0, found = 0; i < n; i++) { 240*7c478bd9Sstevel@tonic-gate if ((rc = read_ctf_common(files[i], label, func, 241*7c478bd9Sstevel@tonic-gate private, require_ctf)) < 0) 242*7c478bd9Sstevel@tonic-gate return (rc); 243*7c478bd9Sstevel@tonic-gate found += rc; 244*7c478bd9Sstevel@tonic-gate } 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate return (found); 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate static int 250*7c478bd9Sstevel@tonic-gate count_archive(int fd, Elf *elf, char *file) 251*7c478bd9Sstevel@tonic-gate { 252*7c478bd9Sstevel@tonic-gate Elf *melf; 253*7c478bd9Sstevel@tonic-gate Elf_Cmd cmd = ELF_C_READ; 254*7c478bd9Sstevel@tonic-gate Elf_Arhdr *arh; 255*7c478bd9Sstevel@tonic-gate int nfiles = 0, err = 0; 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate while ((melf = elf_begin(fd, cmd, elf)) != NULL) { 258*7c478bd9Sstevel@tonic-gate if ((arh = elf_getarhdr(melf)) == NULL) { 259*7c478bd9Sstevel@tonic-gate warning("Can't process input archive %s\n", 260*7c478bd9Sstevel@tonic-gate file); 261*7c478bd9Sstevel@tonic-gate err++; 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate if (*arh->ar_name != '/') 265*7c478bd9Sstevel@tonic-gate nfiles++; 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate cmd = elf_next(melf); 268*7c478bd9Sstevel@tonic-gate (void) elf_end(melf); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate 271*7c478bd9Sstevel@tonic-gate if (err > 0) 272*7c478bd9Sstevel@tonic-gate return (-1); 273*7c478bd9Sstevel@tonic-gate 274*7c478bd9Sstevel@tonic-gate return (nfiles); 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate 277*7c478bd9Sstevel@tonic-gate int 278*7c478bd9Sstevel@tonic-gate count_files(char **files, int n) 279*7c478bd9Sstevel@tonic-gate { 280*7c478bd9Sstevel@tonic-gate int nfiles = 0, err = 0; 281*7c478bd9Sstevel@tonic-gate Elf *elf; 282*7c478bd9Sstevel@tonic-gate int fd, rc, i; 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate (void) elf_version(EV_CURRENT); 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) { 287*7c478bd9Sstevel@tonic-gate char *file = files[i]; 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate if ((fd = open(file, O_RDONLY)) < 0) { 290*7c478bd9Sstevel@tonic-gate warning("Can't read input file %s", file); 291*7c478bd9Sstevel@tonic-gate err++; 292*7c478bd9Sstevel@tonic-gate continue; 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { 296*7c478bd9Sstevel@tonic-gate warning("Can't open input file %s: %s\n", file, 297*7c478bd9Sstevel@tonic-gate elf_errmsg(elf_errno())); 298*7c478bd9Sstevel@tonic-gate err++; 299*7c478bd9Sstevel@tonic-gate (void) close(fd); 300*7c478bd9Sstevel@tonic-gate continue; 301*7c478bd9Sstevel@tonic-gate } 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate switch (elf_kind(elf)) { 304*7c478bd9Sstevel@tonic-gate case ELF_K_AR: 305*7c478bd9Sstevel@tonic-gate if ((rc = count_archive(fd, elf, file)) < 0) 306*7c478bd9Sstevel@tonic-gate err++; 307*7c478bd9Sstevel@tonic-gate else 308*7c478bd9Sstevel@tonic-gate nfiles += rc; 309*7c478bd9Sstevel@tonic-gate break; 310*7c478bd9Sstevel@tonic-gate case ELF_K_ELF: 311*7c478bd9Sstevel@tonic-gate nfiles++; 312*7c478bd9Sstevel@tonic-gate break; 313*7c478bd9Sstevel@tonic-gate default: 314*7c478bd9Sstevel@tonic-gate warning("Input file %s is corrupt\n", file); 315*7c478bd9Sstevel@tonic-gate err++; 316*7c478bd9Sstevel@tonic-gate } 317*7c478bd9Sstevel@tonic-gate 318*7c478bd9Sstevel@tonic-gate (void) elf_end(elf); 319*7c478bd9Sstevel@tonic-gate (void) close(fd); 320*7c478bd9Sstevel@tonic-gate } 321*7c478bd9Sstevel@tonic-gate 322*7c478bd9Sstevel@tonic-gate if (err > 0) 323*7c478bd9Sstevel@tonic-gate return (-1); 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate debug(2, "Found %d files in %d input files\n", nfiles, n); 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate return (nfiles); 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate struct symit_data { 331*7c478bd9Sstevel@tonic-gate GElf_Shdr si_shdr; 332*7c478bd9Sstevel@tonic-gate Elf_Data *si_symd; 333*7c478bd9Sstevel@tonic-gate Elf_Data *si_strd; 334*7c478bd9Sstevel@tonic-gate GElf_Sym si_cursym; 335*7c478bd9Sstevel@tonic-gate char *si_curname; 336*7c478bd9Sstevel@tonic-gate char *si_curfile; 337*7c478bd9Sstevel@tonic-gate int si_nument; 338*7c478bd9Sstevel@tonic-gate int si_next; 339*7c478bd9Sstevel@tonic-gate }; 340*7c478bd9Sstevel@tonic-gate 341*7c478bd9Sstevel@tonic-gate symit_data_t * 342*7c478bd9Sstevel@tonic-gate symit_new(Elf *elf, const char *file) 343*7c478bd9Sstevel@tonic-gate { 344*7c478bd9Sstevel@tonic-gate symit_data_t *si; 345*7c478bd9Sstevel@tonic-gate Elf_Scn *scn; 346*7c478bd9Sstevel@tonic-gate int symtabidx; 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate if ((symtabidx = findelfsecidx(elf, ".symtab")) < 0) 349*7c478bd9Sstevel@tonic-gate return (NULL); 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate si = xcalloc(sizeof (symit_data_t)); 352*7c478bd9Sstevel@tonic-gate 353*7c478bd9Sstevel@tonic-gate if ((scn = elf_getscn(elf, symtabidx)) == NULL || 354*7c478bd9Sstevel@tonic-gate gelf_getshdr(scn, &si->si_shdr) == NULL || 355*7c478bd9Sstevel@tonic-gate (si->si_symd = elf_getdata(scn, NULL)) == NULL) 356*7c478bd9Sstevel@tonic-gate elfterminate(file, "Cannot read .symtab"); 357*7c478bd9Sstevel@tonic-gate 358*7c478bd9Sstevel@tonic-gate if ((scn = elf_getscn(elf, si->si_shdr.sh_link)) == NULL || 359*7c478bd9Sstevel@tonic-gate (si->si_strd = elf_getdata(scn, NULL)) == NULL) 360*7c478bd9Sstevel@tonic-gate elfterminate(file, "Cannot read strings for .symtab"); 361*7c478bd9Sstevel@tonic-gate 362*7c478bd9Sstevel@tonic-gate si->si_nument = si->si_shdr.sh_size / si->si_shdr.sh_entsize; 363*7c478bd9Sstevel@tonic-gate 364*7c478bd9Sstevel@tonic-gate return (si); 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate void 368*7c478bd9Sstevel@tonic-gate symit_free(symit_data_t *si) 369*7c478bd9Sstevel@tonic-gate { 370*7c478bd9Sstevel@tonic-gate free(si); 371*7c478bd9Sstevel@tonic-gate } 372*7c478bd9Sstevel@tonic-gate 373*7c478bd9Sstevel@tonic-gate void 374*7c478bd9Sstevel@tonic-gate symit_reset(symit_data_t *si) 375*7c478bd9Sstevel@tonic-gate { 376*7c478bd9Sstevel@tonic-gate si->si_next = 0; 377*7c478bd9Sstevel@tonic-gate } 378*7c478bd9Sstevel@tonic-gate 379*7c478bd9Sstevel@tonic-gate char * 380*7c478bd9Sstevel@tonic-gate symit_curfile(symit_data_t *si) 381*7c478bd9Sstevel@tonic-gate { 382*7c478bd9Sstevel@tonic-gate return (si->si_curfile); 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate 385*7c478bd9Sstevel@tonic-gate GElf_Sym * 386*7c478bd9Sstevel@tonic-gate symit_next(symit_data_t *si, int type) 387*7c478bd9Sstevel@tonic-gate { 388*7c478bd9Sstevel@tonic-gate GElf_Sym sym; 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate for (; si->si_next < si->si_nument; si->si_next++) { 391*7c478bd9Sstevel@tonic-gate gelf_getsym(si->si_symd, si->si_next, &si->si_cursym); 392*7c478bd9Sstevel@tonic-gate gelf_getsym(si->si_symd, si->si_next, &sym); 393*7c478bd9Sstevel@tonic-gate si->si_curname = (caddr_t)si->si_strd->d_buf + sym.st_name; 394*7c478bd9Sstevel@tonic-gate 395*7c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) == STT_FILE) 396*7c478bd9Sstevel@tonic-gate si->si_curfile = si->si_curname; 397*7c478bd9Sstevel@tonic-gate 398*7c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) != type || 399*7c478bd9Sstevel@tonic-gate sym.st_shndx == SHN_UNDEF) 400*7c478bd9Sstevel@tonic-gate continue; 401*7c478bd9Sstevel@tonic-gate 402*7c478bd9Sstevel@tonic-gate si->si_next++; 403*7c478bd9Sstevel@tonic-gate 404*7c478bd9Sstevel@tonic-gate return (&si->si_cursym); 405*7c478bd9Sstevel@tonic-gate } 406*7c478bd9Sstevel@tonic-gate 407*7c478bd9Sstevel@tonic-gate return (NULL); 408*7c478bd9Sstevel@tonic-gate } 409*7c478bd9Sstevel@tonic-gate 410*7c478bd9Sstevel@tonic-gate char * 411*7c478bd9Sstevel@tonic-gate symit_name(symit_data_t *si) 412*7c478bd9Sstevel@tonic-gate { 413*7c478bd9Sstevel@tonic-gate return (si->si_curname); 414*7c478bd9Sstevel@tonic-gate } 415