1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Routines for preparing tdata trees for conversion into CTF data, and 31*0Sstevel@tonic-gate * for placing the resulting data into an output file. 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #include <stdio.h> 35*0Sstevel@tonic-gate #include <stdlib.h> 36*0Sstevel@tonic-gate #include <strings.h> 37*0Sstevel@tonic-gate #include <sys/types.h> 38*0Sstevel@tonic-gate #include <sys/stat.h> 39*0Sstevel@tonic-gate #include <fcntl.h> 40*0Sstevel@tonic-gate #include <libelf.h> 41*0Sstevel@tonic-gate #include <gelf.h> 42*0Sstevel@tonic-gate #include <unistd.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #include "ctftools.h" 45*0Sstevel@tonic-gate #include "list.h" 46*0Sstevel@tonic-gate #include "memory.h" 47*0Sstevel@tonic-gate #include "traverse.h" 48*0Sstevel@tonic-gate #include "symbol.h" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate typedef struct iidesc_match { 51*0Sstevel@tonic-gate int iim_fuzzy; 52*0Sstevel@tonic-gate iidesc_t *iim_ret; 53*0Sstevel@tonic-gate char *iim_name; 54*0Sstevel@tonic-gate char *iim_file; 55*0Sstevel@tonic-gate uchar_t iim_bind; 56*0Sstevel@tonic-gate } iidesc_match_t; 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate static int 59*0Sstevel@tonic-gate burst_iitypes(void *data, void *arg) 60*0Sstevel@tonic-gate { 61*0Sstevel@tonic-gate iidesc_t *ii = data; 62*0Sstevel@tonic-gate iiburst_t *iiburst = arg; 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate switch (ii->ii_type) { 65*0Sstevel@tonic-gate case II_GFUN: 66*0Sstevel@tonic-gate case II_SFUN: 67*0Sstevel@tonic-gate case II_GVAR: 68*0Sstevel@tonic-gate case II_SVAR: 69*0Sstevel@tonic-gate if (!(ii->ii_flags & IIDESC_F_USED)) 70*0Sstevel@tonic-gate return (0); 71*0Sstevel@tonic-gate break; 72*0Sstevel@tonic-gate default: 73*0Sstevel@tonic-gate break; 74*0Sstevel@tonic-gate } 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate ii->ii_dtype->t_flags |= TDESC_F_ISROOT; 77*0Sstevel@tonic-gate (void) iitraverse_td(ii, iiburst->iib_tdtd); 78*0Sstevel@tonic-gate return (1); 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate /*ARGSUSED1*/ 82*0Sstevel@tonic-gate static int 83*0Sstevel@tonic-gate save_type_by_id(tdesc_t *tdp, tdesc_t **tdpp, void *private) 84*0Sstevel@tonic-gate { 85*0Sstevel@tonic-gate iiburst_t *iiburst = private; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate /* 88*0Sstevel@tonic-gate * Doing this on every node is horribly inefficient, but given that 89*0Sstevel@tonic-gate * we may be suppressing some types, we can't trust nextid in the 90*0Sstevel@tonic-gate * tdata_t. 91*0Sstevel@tonic-gate */ 92*0Sstevel@tonic-gate if (tdp->t_id > iiburst->iib_maxtypeid) 93*0Sstevel@tonic-gate iiburst->iib_maxtypeid = tdp->t_id; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate slist_add(&iiburst->iib_types, tdp, tdesc_idcmp); 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate return (1); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate static tdtrav_cb_f burst_types_cbs[] = { 101*0Sstevel@tonic-gate NULL, 102*0Sstevel@tonic-gate save_type_by_id, /* intrinsic */ 103*0Sstevel@tonic-gate save_type_by_id, /* pointer */ 104*0Sstevel@tonic-gate save_type_by_id, /* array */ 105*0Sstevel@tonic-gate save_type_by_id, /* function */ 106*0Sstevel@tonic-gate save_type_by_id, /* struct */ 107*0Sstevel@tonic-gate save_type_by_id, /* union */ 108*0Sstevel@tonic-gate save_type_by_id, /* enum */ 109*0Sstevel@tonic-gate save_type_by_id, /* forward */ 110*0Sstevel@tonic-gate save_type_by_id, /* typedef */ 111*0Sstevel@tonic-gate tdtrav_assert, /* typedef_unres */ 112*0Sstevel@tonic-gate save_type_by_id, /* volatile */ 113*0Sstevel@tonic-gate save_type_by_id, /* const */ 114*0Sstevel@tonic-gate save_type_by_id /* restrict */ 115*0Sstevel@tonic-gate }; 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate static iiburst_t * 119*0Sstevel@tonic-gate iiburst_new(tdata_t *td, int max) 120*0Sstevel@tonic-gate { 121*0Sstevel@tonic-gate iiburst_t *iiburst = xcalloc(sizeof (iiburst_t)); 122*0Sstevel@tonic-gate iiburst->iib_td = td; 123*0Sstevel@tonic-gate iiburst->iib_funcs = xcalloc(sizeof (iidesc_t *) * max); 124*0Sstevel@tonic-gate iiburst->iib_nfuncs = 0; 125*0Sstevel@tonic-gate iiburst->iib_objts = xcalloc(sizeof (iidesc_t *) * max); 126*0Sstevel@tonic-gate iiburst->iib_nobjts = 0; 127*0Sstevel@tonic-gate return (iiburst); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate static void 131*0Sstevel@tonic-gate iiburst_types(iiburst_t *iiburst) 132*0Sstevel@tonic-gate { 133*0Sstevel@tonic-gate tdtrav_data_t tdtd; 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate tdtrav_init(&tdtd, &iiburst->iib_td->td_curvgen, NULL, burst_types_cbs, 136*0Sstevel@tonic-gate NULL, (void *)iiburst); 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate iiburst->iib_tdtd = &tdtd; 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate (void) hash_iter(iiburst->iib_td->td_iihash, burst_iitypes, iiburst); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate static void 144*0Sstevel@tonic-gate iiburst_free(iiburst_t *iiburst) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate free(iiburst->iib_funcs); 147*0Sstevel@tonic-gate free(iiburst->iib_objts); 148*0Sstevel@tonic-gate list_free(iiburst->iib_types, NULL, NULL); 149*0Sstevel@tonic-gate free(iiburst); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* 153*0Sstevel@tonic-gate * See if this iidesc matches the ELF symbol data we pass in. 154*0Sstevel@tonic-gate * 155*0Sstevel@tonic-gate * A fuzzy match is where we have a local symbol matching the name of a 156*0Sstevel@tonic-gate * global type description. This is common when a mapfile is used for a 157*0Sstevel@tonic-gate * DSO, but we don't accept it by default. 158*0Sstevel@tonic-gate * 159*0Sstevel@tonic-gate * A weak fuzzy match is when a weak symbol was resolved and matched to 160*0Sstevel@tonic-gate * a global type description. 161*0Sstevel@tonic-gate */ 162*0Sstevel@tonic-gate static int 163*0Sstevel@tonic-gate matching_iidesc(iidesc_t *iidesc, iidesc_match_t *match) 164*0Sstevel@tonic-gate { 165*0Sstevel@tonic-gate if (streq(iidesc->ii_name, match->iim_name) == 0) 166*0Sstevel@tonic-gate return (0); 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate switch (iidesc->ii_type) { 169*0Sstevel@tonic-gate case II_GFUN: 170*0Sstevel@tonic-gate case II_GVAR: 171*0Sstevel@tonic-gate if (match->iim_bind == STB_GLOBAL) { 172*0Sstevel@tonic-gate match->iim_ret = iidesc; 173*0Sstevel@tonic-gate return (-1); 174*0Sstevel@tonic-gate } else if (match->iim_fuzzy && match->iim_ret == NULL) { 175*0Sstevel@tonic-gate match->iim_ret = iidesc; 176*0Sstevel@tonic-gate /* continue to look for strong match */ 177*0Sstevel@tonic-gate return (0); 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate break; 180*0Sstevel@tonic-gate case II_SFUN: 181*0Sstevel@tonic-gate case II_SVAR: 182*0Sstevel@tonic-gate if (match->iim_bind == STB_LOCAL && 183*0Sstevel@tonic-gate match->iim_file != NULL && 184*0Sstevel@tonic-gate streq(iidesc->ii_owner, match->iim_file)) { 185*0Sstevel@tonic-gate match->iim_ret = iidesc; 186*0Sstevel@tonic-gate return (-1); 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate break; 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate return (0); 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate static iidesc_t * 194*0Sstevel@tonic-gate find_iidesc(hash_t *hash, iidesc_match_t *match) 195*0Sstevel@tonic-gate { 196*0Sstevel@tonic-gate iidesc_t tmpdesc; 197*0Sstevel@tonic-gate match->iim_ret = NULL; 198*0Sstevel@tonic-gate bzero(&tmpdesc, sizeof (iidesc_t)); 199*0Sstevel@tonic-gate tmpdesc.ii_name = match->iim_name; 200*0Sstevel@tonic-gate (void) hash_match(hash, &tmpdesc, (int (*)())matching_iidesc, match); 201*0Sstevel@tonic-gate return (match->iim_ret); 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* 205*0Sstevel@tonic-gate * If we have a weak symbol, attempt to find the strong symbol it will 206*0Sstevel@tonic-gate * resolve to. Note: the code where this actually happens is in 207*0Sstevel@tonic-gate * sym_process() in cmd/sgs/libld/common/syms.c 208*0Sstevel@tonic-gate * 209*0Sstevel@tonic-gate * Finding the matching symbol is unfortunately not trivial. For a 210*0Sstevel@tonic-gate * symbol to be a candidate, it must: 211*0Sstevel@tonic-gate * 212*0Sstevel@tonic-gate * - have the same type (function, object) 213*0Sstevel@tonic-gate * - have the same value (address) 214*0Sstevel@tonic-gate * - have the same size 215*0Sstevel@tonic-gate * - not be another weak symbol 216*0Sstevel@tonic-gate * - belong to the same section (checked via section index) 217*0Sstevel@tonic-gate * 218*0Sstevel@tonic-gate * If such a candidate is global, then we assume we've found it. The 219*0Sstevel@tonic-gate * linker generates the symbol table such that the curfile might be 220*0Sstevel@tonic-gate * incorrect; this is OK for global symbols, since find_iidesc() doesn't 221*0Sstevel@tonic-gate * need to check for the source file for the symbol. 222*0Sstevel@tonic-gate * 223*0Sstevel@tonic-gate * We might have found a strong local symbol, where the curfile is 224*0Sstevel@tonic-gate * accurate and matches that of the weak symbol. We assume this is a 225*0Sstevel@tonic-gate * reasonable match. 226*0Sstevel@tonic-gate * 227*0Sstevel@tonic-gate * If we've got a local symbol with a non-matching curfile, there are 228*0Sstevel@tonic-gate * two possibilities. Either this is a completely different symbol, or 229*0Sstevel@tonic-gate * it's a once-global symbol that was scoped to local via a mapfile. In 230*0Sstevel@tonic-gate * the latter case, curfile is likely inaccurate since the linker does 231*0Sstevel@tonic-gate * not preserve the needed curfile in the order of the symbol table (see 232*0Sstevel@tonic-gate * the comments about locally scoped symbols in libld's update_osym()). 233*0Sstevel@tonic-gate * As we can't tell this case from the former one, we use this symbol 234*0Sstevel@tonic-gate * iff no other matching symbol is found. 235*0Sstevel@tonic-gate * 236*0Sstevel@tonic-gate * What we really need here is a SUNW section containing weak<->strong 237*0Sstevel@tonic-gate * mappings that we can consume. 238*0Sstevel@tonic-gate */ 239*0Sstevel@tonic-gate static int 240*0Sstevel@tonic-gate check_for_weak(GElf_Sym *weak, char const *weakfile, 241*0Sstevel@tonic-gate Elf_Data *data, int nent, Elf_Data *strdata, 242*0Sstevel@tonic-gate GElf_Sym *retsym, char **curfilep) 243*0Sstevel@tonic-gate { 244*0Sstevel@tonic-gate char *curfile = NULL; 245*0Sstevel@tonic-gate char *tmpfile; 246*0Sstevel@tonic-gate GElf_Sym tmpsym; 247*0Sstevel@tonic-gate int candidate = 0; 248*0Sstevel@tonic-gate int i; 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate if (GELF_ST_BIND(weak->st_info) != STB_WEAK) 251*0Sstevel@tonic-gate return (0); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate for (i = 0; i < nent; i++) { 254*0Sstevel@tonic-gate GElf_Sym sym; 255*0Sstevel@tonic-gate uchar_t type; 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate if (gelf_getsym(data, i, &sym) == NULL) 258*0Sstevel@tonic-gate continue; 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate type = GELF_ST_TYPE(sym.st_info); 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate if (type == STT_FILE) 263*0Sstevel@tonic-gate curfile = (char *)strdata->d_buf + sym.st_name; 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate if (GELF_ST_TYPE(weak->st_info) != type || 266*0Sstevel@tonic-gate weak->st_value != sym.st_value) 267*0Sstevel@tonic-gate continue; 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate if (weak->st_size != sym.st_size) 270*0Sstevel@tonic-gate continue; 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate if (GELF_ST_BIND(sym.st_info) == STB_WEAK) 273*0Sstevel@tonic-gate continue; 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate if (sym.st_shndx != weak->st_shndx) 276*0Sstevel@tonic-gate continue; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate if (GELF_ST_BIND(sym.st_info) == STB_LOCAL && 279*0Sstevel@tonic-gate (curfile == NULL || weakfile == NULL || 280*0Sstevel@tonic-gate strcmp(curfile, weakfile) != 0)) { 281*0Sstevel@tonic-gate candidate = 1; 282*0Sstevel@tonic-gate tmpfile = curfile; 283*0Sstevel@tonic-gate tmpsym = sym; 284*0Sstevel@tonic-gate continue; 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate *curfilep = curfile; 288*0Sstevel@tonic-gate *retsym = sym; 289*0Sstevel@tonic-gate return (1); 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate if (candidate) { 293*0Sstevel@tonic-gate *curfilep = tmpfile; 294*0Sstevel@tonic-gate *retsym = tmpsym; 295*0Sstevel@tonic-gate return (1); 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate return (0); 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate /* 302*0Sstevel@tonic-gate * When we've found the underlying symbol's type description 303*0Sstevel@tonic-gate * for a weak symbol, we need to copy it and rename it to match 304*0Sstevel@tonic-gate * the weak symbol. We also need to add it to the td so it's 305*0Sstevel@tonic-gate * handled along with the others later. 306*0Sstevel@tonic-gate */ 307*0Sstevel@tonic-gate static iidesc_t * 308*0Sstevel@tonic-gate copy_from_strong(tdata_t *td, GElf_Sym *sym, iidesc_t *strongdesc, 309*0Sstevel@tonic-gate const char *weakname, const char *weakfile) 310*0Sstevel@tonic-gate { 311*0Sstevel@tonic-gate iidesc_t *new = iidesc_dup_rename(strongdesc, weakname, weakfile); 312*0Sstevel@tonic-gate uchar_t type = GELF_ST_TYPE(sym->st_info); 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate switch (type) { 315*0Sstevel@tonic-gate case STT_OBJECT: 316*0Sstevel@tonic-gate new->ii_type = II_GVAR; 317*0Sstevel@tonic-gate break; 318*0Sstevel@tonic-gate case STT_FUNC: 319*0Sstevel@tonic-gate new->ii_type = II_GFUN; 320*0Sstevel@tonic-gate break; 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate hash_add(td->td_iihash, new); 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate return (new); 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate /* 329*0Sstevel@tonic-gate * Process the symbol table of the output file, associating each symbol 330*0Sstevel@tonic-gate * with a type description if possible, and sorting them into functions 331*0Sstevel@tonic-gate * and data, maintaining symbol table order. 332*0Sstevel@tonic-gate */ 333*0Sstevel@tonic-gate static iiburst_t * 334*0Sstevel@tonic-gate sort_iidescs(Elf *elf, const char *file, tdata_t *td, int fuzzymatch, 335*0Sstevel@tonic-gate int dynsym) 336*0Sstevel@tonic-gate { 337*0Sstevel@tonic-gate iiburst_t *iiburst; 338*0Sstevel@tonic-gate Elf_Scn *scn; 339*0Sstevel@tonic-gate GElf_Shdr shdr; 340*0Sstevel@tonic-gate Elf_Data *data, *strdata; 341*0Sstevel@tonic-gate int i, stidx; 342*0Sstevel@tonic-gate int nent; 343*0Sstevel@tonic-gate iidesc_match_t match; 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate match.iim_fuzzy = fuzzymatch; 346*0Sstevel@tonic-gate match.iim_file = NULL; 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate if ((stidx = findelfsecidx(elf, dynsym ? ".dynsym" : ".symtab")) < 0) 349*0Sstevel@tonic-gate terminate("%s: Can't open symbol table\n", file); 350*0Sstevel@tonic-gate scn = elf_getscn(elf, stidx); 351*0Sstevel@tonic-gate data = elf_getdata(scn, NULL); 352*0Sstevel@tonic-gate gelf_getshdr(scn, &shdr); 353*0Sstevel@tonic-gate nent = shdr.sh_size / shdr.sh_entsize; 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate scn = elf_getscn(elf, shdr.sh_link); 356*0Sstevel@tonic-gate strdata = elf_getdata(scn, NULL); 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate iiburst = iiburst_new(td, nent); 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate for (i = 0; i < nent; i++) { 361*0Sstevel@tonic-gate GElf_Sym sym; 362*0Sstevel@tonic-gate iidesc_t **tolist; 363*0Sstevel@tonic-gate GElf_Sym ssym; 364*0Sstevel@tonic-gate iidesc_match_t smatch; 365*0Sstevel@tonic-gate int *curr; 366*0Sstevel@tonic-gate iidesc_t *iidesc; 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate if (gelf_getsym(data, i, &sym) == NULL) 369*0Sstevel@tonic-gate elfterminate(file, "Couldn't read symbol %d", i); 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate match.iim_name = (char *)strdata->d_buf + sym.st_name; 372*0Sstevel@tonic-gate match.iim_bind = GELF_ST_BIND(sym.st_info); 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate switch (GELF_ST_TYPE(sym.st_info)) { 375*0Sstevel@tonic-gate case STT_FILE: 376*0Sstevel@tonic-gate match.iim_file = match.iim_name; 377*0Sstevel@tonic-gate continue; 378*0Sstevel@tonic-gate case STT_OBJECT: 379*0Sstevel@tonic-gate tolist = iiburst->iib_objts; 380*0Sstevel@tonic-gate curr = &iiburst->iib_nobjts; 381*0Sstevel@tonic-gate break; 382*0Sstevel@tonic-gate case STT_FUNC: 383*0Sstevel@tonic-gate tolist = iiburst->iib_funcs; 384*0Sstevel@tonic-gate curr = &iiburst->iib_nfuncs; 385*0Sstevel@tonic-gate break; 386*0Sstevel@tonic-gate default: 387*0Sstevel@tonic-gate continue; 388*0Sstevel@tonic-gate } 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate if (ignore_symbol(&sym, match.iim_name)) 391*0Sstevel@tonic-gate continue; 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gate iidesc = find_iidesc(td->td_iihash, &match); 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate if (iidesc != NULL) { 396*0Sstevel@tonic-gate tolist[*curr] = iidesc; 397*0Sstevel@tonic-gate iidesc->ii_flags |= IIDESC_F_USED; 398*0Sstevel@tonic-gate (*curr)++; 399*0Sstevel@tonic-gate continue; 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate if (!check_for_weak(&sym, match.iim_file, data, nent, strdata, 403*0Sstevel@tonic-gate &ssym, &smatch.iim_file)) { 404*0Sstevel@tonic-gate (*curr)++; 405*0Sstevel@tonic-gate continue; 406*0Sstevel@tonic-gate } 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate smatch.iim_fuzzy = fuzzymatch; 409*0Sstevel@tonic-gate smatch.iim_name = (char *)strdata->d_buf + ssym.st_name; 410*0Sstevel@tonic-gate smatch.iim_bind = GELF_ST_BIND(ssym.st_info); 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate debug(3, "Weak symbol %s resolved to %s\n", match.iim_name, 413*0Sstevel@tonic-gate smatch.iim_name); 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate iidesc = find_iidesc(td->td_iihash, &smatch); 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate if (iidesc != NULL) { 418*0Sstevel@tonic-gate tolist[*curr] = copy_from_strong(td, &sym, 419*0Sstevel@tonic-gate iidesc, match.iim_name, match.iim_file); 420*0Sstevel@tonic-gate tolist[*curr]->ii_flags |= IIDESC_F_USED; 421*0Sstevel@tonic-gate } 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate (*curr)++; 424*0Sstevel@tonic-gate } 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate /* 427*0Sstevel@tonic-gate * Stabs are generated for every function declared in a given C source 428*0Sstevel@tonic-gate * file. When converting an object file, we may encounter a stab that 429*0Sstevel@tonic-gate * has no symbol table entry because the optimizer has decided to omit 430*0Sstevel@tonic-gate * that item (for example, an unreferenced static function). We may 431*0Sstevel@tonic-gate * see iidescs that do not have an associated symtab entry, and so 432*0Sstevel@tonic-gate * we do not write records for those functions into the CTF data. 433*0Sstevel@tonic-gate * All others get marked as a root by this function. 434*0Sstevel@tonic-gate */ 435*0Sstevel@tonic-gate iiburst_types(iiburst); 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate /* 438*0Sstevel@tonic-gate * By not adding some of the functions and/or objects, we may have 439*0Sstevel@tonic-gate * caused some types that were referenced solely by those 440*0Sstevel@tonic-gate * functions/objects to be suppressed. This could cause a label, 441*0Sstevel@tonic-gate * generated prior to the evisceration, to be incorrect. Find the 442*0Sstevel@tonic-gate * highest type index, and change the label indicies to be no higher 443*0Sstevel@tonic-gate * than this value. 444*0Sstevel@tonic-gate */ 445*0Sstevel@tonic-gate tdata_label_newmax(td, iiburst->iib_maxtypeid); 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate return (iiburst); 448*0Sstevel@tonic-gate } 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate static void 451*0Sstevel@tonic-gate write_file(Elf *src, const char *srcname, Elf *dst, const char *dstname, 452*0Sstevel@tonic-gate caddr_t ctfdata, size_t ctfsize, int flags) 453*0Sstevel@tonic-gate { 454*0Sstevel@tonic-gate GElf_Ehdr sehdr, dehdr; 455*0Sstevel@tonic-gate Elf_Scn *sscn, *dscn; 456*0Sstevel@tonic-gate Elf_Data *sdata, *ddata; 457*0Sstevel@tonic-gate GElf_Shdr shdr; 458*0Sstevel@tonic-gate GElf_Word symtab_type; 459*0Sstevel@tonic-gate int symtab_idx = -1; 460*0Sstevel@tonic-gate off_t new_offset = 0; 461*0Sstevel@tonic-gate off_t ctfnameoff = 0; 462*0Sstevel@tonic-gate int dynsym = (flags & CTF_USE_DYNSYM); 463*0Sstevel@tonic-gate int keep_stabs = (flags & CTF_KEEP_STABS); 464*0Sstevel@tonic-gate int *secxlate; 465*0Sstevel@tonic-gate int srcidx, dstidx; 466*0Sstevel@tonic-gate int curnmoff = 0; 467*0Sstevel@tonic-gate int changing = 0; 468*0Sstevel@tonic-gate int pad; 469*0Sstevel@tonic-gate int i; 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate if (gelf_newehdr(dst, gelf_getclass(src)) == NULL) 472*0Sstevel@tonic-gate elfterminate(dstname, "Cannot copy ehdr to temp file"); 473*0Sstevel@tonic-gate gelf_getehdr(src, &sehdr); 474*0Sstevel@tonic-gate memcpy(&dehdr, &sehdr, sizeof (GElf_Ehdr)); 475*0Sstevel@tonic-gate gelf_update_ehdr(dst, &dehdr); 476*0Sstevel@tonic-gate 477*0Sstevel@tonic-gate symtab_type = dynsym ? SHT_DYNSYM : SHT_SYMTAB; 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate /* 480*0Sstevel@tonic-gate * Neither the existing stab sections nor the SUNW_ctf sections (new or 481*0Sstevel@tonic-gate * existing) are SHF_ALLOC'd, so they won't be in areas referenced by 482*0Sstevel@tonic-gate * program headers. As such, we can just blindly copy the program 483*0Sstevel@tonic-gate * headers from the existing file to the new file. 484*0Sstevel@tonic-gate */ 485*0Sstevel@tonic-gate if (sehdr.e_phnum != 0) { 486*0Sstevel@tonic-gate (void) elf_flagelf(dst, ELF_C_SET, ELF_F_LAYOUT); 487*0Sstevel@tonic-gate if (gelf_newphdr(dst, sehdr.e_phnum) == NULL) 488*0Sstevel@tonic-gate elfterminate(dstname, "Cannot make phdrs in temp file"); 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate for (i = 0; i < sehdr.e_phnum; i++) { 491*0Sstevel@tonic-gate GElf_Phdr phdr; 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gate gelf_getphdr(src, i, &phdr); 494*0Sstevel@tonic-gate gelf_update_phdr(dst, i, &phdr); 495*0Sstevel@tonic-gate } 496*0Sstevel@tonic-gate } 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate secxlate = xmalloc(sizeof (int) * sehdr.e_shnum); 499*0Sstevel@tonic-gate for (srcidx = dstidx = 0; srcidx < sehdr.e_shnum; srcidx++) { 500*0Sstevel@tonic-gate Elf_Scn *scn = elf_getscn(src, srcidx); 501*0Sstevel@tonic-gate GElf_Shdr shdr; 502*0Sstevel@tonic-gate char *sname; 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate gelf_getshdr(scn, &shdr); 505*0Sstevel@tonic-gate sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name); 506*0Sstevel@tonic-gate if (sname == NULL) { 507*0Sstevel@tonic-gate elfterminate(srcname, "Can't find string at %u", 508*0Sstevel@tonic-gate shdr.sh_name); 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate if (strcmp(sname, CTF_ELF_SCN_NAME) == 0) { 512*0Sstevel@tonic-gate secxlate[srcidx] = -1; 513*0Sstevel@tonic-gate } else if (!keep_stabs && 514*0Sstevel@tonic-gate (strncmp(sname, ".stab", 5) == 0 || 515*0Sstevel@tonic-gate strncmp(sname, ".debug", 6) == 0 || 516*0Sstevel@tonic-gate strncmp(sname, ".rel.debug", 10) == 0 || 517*0Sstevel@tonic-gate strncmp(sname, ".rela.debug", 11) == 0)) { 518*0Sstevel@tonic-gate secxlate[srcidx] = -1; 519*0Sstevel@tonic-gate } else if (dynsym && shdr.sh_type == SHT_SYMTAB) { 520*0Sstevel@tonic-gate /* 521*0Sstevel@tonic-gate * If we're building CTF against the dynsym, 522*0Sstevel@tonic-gate * we'll rip out the symtab so debuggers aren't 523*0Sstevel@tonic-gate * confused. 524*0Sstevel@tonic-gate */ 525*0Sstevel@tonic-gate secxlate[srcidx] = -1; 526*0Sstevel@tonic-gate } else { 527*0Sstevel@tonic-gate secxlate[srcidx] = dstidx++; 528*0Sstevel@tonic-gate curnmoff += strlen(sname) + 1; 529*0Sstevel@tonic-gate } 530*0Sstevel@tonic-gate 531*0Sstevel@tonic-gate new_offset = (off_t)dehdr.e_phoff; 532*0Sstevel@tonic-gate } 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate for (srcidx = 1; srcidx < sehdr.e_shnum; srcidx++) { 535*0Sstevel@tonic-gate char *sname; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate sscn = elf_getscn(src, srcidx); 538*0Sstevel@tonic-gate gelf_getshdr(sscn, &shdr); 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate if (secxlate[srcidx] == -1) { 541*0Sstevel@tonic-gate changing = 1; 542*0Sstevel@tonic-gate continue; 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gate dscn = elf_newscn(dst); 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate /* 548*0Sstevel@tonic-gate * If this file has program headers, we need to explicitly lay 549*0Sstevel@tonic-gate * out sections. If none of the sections prior to this one have 550*0Sstevel@tonic-gate * been removed, then we can just use the existing location. If 551*0Sstevel@tonic-gate * one or more sections have been changed, then we need to 552*0Sstevel@tonic-gate * adjust this one to avoid holes. 553*0Sstevel@tonic-gate */ 554*0Sstevel@tonic-gate if (changing && sehdr.e_phnum != 0) { 555*0Sstevel@tonic-gate pad = new_offset % shdr.sh_addralign; 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate if (pad) 558*0Sstevel@tonic-gate new_offset += shdr.sh_addralign - pad; 559*0Sstevel@tonic-gate shdr.sh_offset = new_offset; 560*0Sstevel@tonic-gate } 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate shdr.sh_link = secxlate[shdr.sh_link]; 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate if (shdr.sh_type == SHT_REL || shdr.sh_type == SHT_RELA) 565*0Sstevel@tonic-gate shdr.sh_info = secxlate[shdr.sh_info]; 566*0Sstevel@tonic-gate 567*0Sstevel@tonic-gate sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name); 568*0Sstevel@tonic-gate if (sname == NULL) { 569*0Sstevel@tonic-gate elfterminate(srcname, "Can't find string at %u", 570*0Sstevel@tonic-gate shdr.sh_name); 571*0Sstevel@tonic-gate } 572*0Sstevel@tonic-gate if ((sdata = elf_getdata(sscn, NULL)) == NULL) 573*0Sstevel@tonic-gate elfterminate(srcname, "Cannot get sect %s data", sname); 574*0Sstevel@tonic-gate if ((ddata = elf_newdata(dscn)) == NULL) 575*0Sstevel@tonic-gate elfterminate(dstname, "Can't make sect %s data", sname); 576*0Sstevel@tonic-gate bcopy(sdata, ddata, sizeof (Elf_Data)); 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gate if (srcidx == sehdr.e_shstrndx) { 579*0Sstevel@tonic-gate char seclen = strlen(CTF_ELF_SCN_NAME); 580*0Sstevel@tonic-gate 581*0Sstevel@tonic-gate ddata->d_buf = xmalloc(ddata->d_size + shdr.sh_size + 582*0Sstevel@tonic-gate seclen + 1); 583*0Sstevel@tonic-gate bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size); 584*0Sstevel@tonic-gate strcpy((caddr_t)ddata->d_buf + shdr.sh_size, 585*0Sstevel@tonic-gate CTF_ELF_SCN_NAME); 586*0Sstevel@tonic-gate ctfnameoff = (off_t)shdr.sh_size; 587*0Sstevel@tonic-gate shdr.sh_size += seclen + 1; 588*0Sstevel@tonic-gate ddata->d_size += seclen + 1; 589*0Sstevel@tonic-gate 590*0Sstevel@tonic-gate if (sehdr.e_phnum != 0) 591*0Sstevel@tonic-gate changing = 1; 592*0Sstevel@tonic-gate } 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gate if (shdr.sh_type == symtab_type && shdr.sh_entsize != 0) { 595*0Sstevel@tonic-gate int nsym = shdr.sh_size / shdr.sh_entsize; 596*0Sstevel@tonic-gate 597*0Sstevel@tonic-gate symtab_idx = secxlate[srcidx]; 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate ddata->d_buf = xmalloc(shdr.sh_size); 600*0Sstevel@tonic-gate bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size); 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate for (i = 0; i < nsym; i++) { 603*0Sstevel@tonic-gate GElf_Sym sym; 604*0Sstevel@tonic-gate short newscn; 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate (void) gelf_getsym(ddata, i, &sym); 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gate if (sym.st_shndx >= SHN_LORESERVE) 609*0Sstevel@tonic-gate continue; 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate if ((newscn = secxlate[sym.st_shndx]) != 612*0Sstevel@tonic-gate sym.st_shndx) { 613*0Sstevel@tonic-gate sym.st_shndx = 614*0Sstevel@tonic-gate (newscn == -1 ? 1 : newscn); 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate gelf_update_sym(ddata, i, &sym); 617*0Sstevel@tonic-gate } 618*0Sstevel@tonic-gate } 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gate if (gelf_update_shdr(dscn, &shdr) == NULL) 622*0Sstevel@tonic-gate elfterminate(dstname, "Cannot update sect %s", sname); 623*0Sstevel@tonic-gate 624*0Sstevel@tonic-gate new_offset = (off_t)shdr.sh_offset; 625*0Sstevel@tonic-gate if (shdr.sh_type != SHT_NOBITS) 626*0Sstevel@tonic-gate new_offset += shdr.sh_size; 627*0Sstevel@tonic-gate } 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate if (symtab_idx == -1) { 630*0Sstevel@tonic-gate terminate("Cannot find %s section\n", 631*0Sstevel@tonic-gate dynsym ? "SHT_DYNSYM" : "SHT_SYMTAB"); 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gate /* Add the ctf section */ 635*0Sstevel@tonic-gate dscn = elf_newscn(dst); 636*0Sstevel@tonic-gate gelf_getshdr(dscn, &shdr); 637*0Sstevel@tonic-gate shdr.sh_name = ctfnameoff; 638*0Sstevel@tonic-gate shdr.sh_type = SHT_PROGBITS; 639*0Sstevel@tonic-gate shdr.sh_size = ctfsize; 640*0Sstevel@tonic-gate shdr.sh_link = symtab_idx; 641*0Sstevel@tonic-gate shdr.sh_addralign = 4; 642*0Sstevel@tonic-gate if (changing && sehdr.e_phnum != 0) { 643*0Sstevel@tonic-gate pad = new_offset % shdr.sh_addralign; 644*0Sstevel@tonic-gate 645*0Sstevel@tonic-gate if (pad) 646*0Sstevel@tonic-gate new_offset += shdr.sh_addralign - pad; 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate shdr.sh_offset = new_offset; 649*0Sstevel@tonic-gate new_offset += shdr.sh_size; 650*0Sstevel@tonic-gate } 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gate ddata = elf_newdata(dscn); 653*0Sstevel@tonic-gate ddata->d_buf = ctfdata; 654*0Sstevel@tonic-gate ddata->d_size = ctfsize; 655*0Sstevel@tonic-gate ddata->d_align = shdr.sh_addralign; 656*0Sstevel@tonic-gate 657*0Sstevel@tonic-gate gelf_update_shdr(dscn, &shdr); 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate /* update the section header location */ 660*0Sstevel@tonic-gate if (sehdr.e_phnum != 0) { 661*0Sstevel@tonic-gate size_t align = gelf_fsize(dst, ELF_T_ADDR, 1, EV_CURRENT); 662*0Sstevel@tonic-gate size_t r = new_offset % align; 663*0Sstevel@tonic-gate 664*0Sstevel@tonic-gate if (r) 665*0Sstevel@tonic-gate new_offset += align - r; 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gate dehdr.e_shoff = new_offset; 668*0Sstevel@tonic-gate } 669*0Sstevel@tonic-gate 670*0Sstevel@tonic-gate /* commit to disk */ 671*0Sstevel@tonic-gate dehdr.e_shstrndx = secxlate[sehdr.e_shstrndx]; 672*0Sstevel@tonic-gate gelf_update_ehdr(dst, &dehdr); 673*0Sstevel@tonic-gate if (elf_update(dst, ELF_C_WRITE) < 0) 674*0Sstevel@tonic-gate elfterminate(dstname, "Cannot finalize temp file"); 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate free(secxlate); 677*0Sstevel@tonic-gate } 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate static caddr_t 680*0Sstevel@tonic-gate make_ctf_data(tdata_t *td, Elf *elf, const char *file, size_t *lenp, int flags) 681*0Sstevel@tonic-gate { 682*0Sstevel@tonic-gate iiburst_t *iiburst; 683*0Sstevel@tonic-gate caddr_t data; 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate iiburst = sort_iidescs(elf, file, td, flags & CTF_FUZZY_MATCH, 686*0Sstevel@tonic-gate flags & CTF_USE_DYNSYM); 687*0Sstevel@tonic-gate data = ctf_gen(iiburst, lenp, flags & CTF_COMPRESS); 688*0Sstevel@tonic-gate 689*0Sstevel@tonic-gate iiburst_free(iiburst); 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gate return (data); 692*0Sstevel@tonic-gate } 693*0Sstevel@tonic-gate 694*0Sstevel@tonic-gate void 695*0Sstevel@tonic-gate write_ctf(tdata_t *td, const char *curname, const char *newname, int flags) 696*0Sstevel@tonic-gate { 697*0Sstevel@tonic-gate struct stat st; 698*0Sstevel@tonic-gate Elf *elf = NULL; 699*0Sstevel@tonic-gate Elf *telf = NULL; 700*0Sstevel@tonic-gate caddr_t data; 701*0Sstevel@tonic-gate size_t len; 702*0Sstevel@tonic-gate int fd = -1; 703*0Sstevel@tonic-gate int tfd = -1; 704*0Sstevel@tonic-gate 705*0Sstevel@tonic-gate (void) elf_version(EV_CURRENT); 706*0Sstevel@tonic-gate if ((fd = open(curname, O_RDONLY)) < 0 || fstat(fd, &st) < 0) 707*0Sstevel@tonic-gate terminate("%s: Cannot open for re-reading", curname); 708*0Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) 709*0Sstevel@tonic-gate elfterminate(curname, "Cannot re-read"); 710*0Sstevel@tonic-gate 711*0Sstevel@tonic-gate if ((tfd = open(newname, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode)) < 0) 712*0Sstevel@tonic-gate terminate("Cannot open temp file %s for writing", newname); 713*0Sstevel@tonic-gate if ((telf = elf_begin(tfd, ELF_C_WRITE, NULL)) == NULL) 714*0Sstevel@tonic-gate elfterminate(curname, "Cannot write"); 715*0Sstevel@tonic-gate 716*0Sstevel@tonic-gate data = make_ctf_data(td, elf, curname, &len, flags); 717*0Sstevel@tonic-gate write_file(elf, curname, telf, newname, data, len, flags); 718*0Sstevel@tonic-gate free(data); 719*0Sstevel@tonic-gate 720*0Sstevel@tonic-gate elf_end(telf); 721*0Sstevel@tonic-gate elf_end(elf); 722*0Sstevel@tonic-gate (void) close(fd); 723*0Sstevel@tonic-gate (void) close(tfd); 724*0Sstevel@tonic-gate } 725