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