17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 54d232658Sjohnlev * Common Development and Distribution License (the "License"). 64d232658Sjohnlev * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 224d232658Sjohnlev * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Routines for preparing tdata trees for conversion into CTF data, and 307c478bd9Sstevel@tonic-gate * for placing the resulting data into an output file. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <stdio.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <strings.h> 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <fcntl.h> 397c478bd9Sstevel@tonic-gate #include <libelf.h> 407c478bd9Sstevel@tonic-gate #include <gelf.h> 417c478bd9Sstevel@tonic-gate #include <unistd.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "ctftools.h" 447c478bd9Sstevel@tonic-gate #include "list.h" 457c478bd9Sstevel@tonic-gate #include "memory.h" 467c478bd9Sstevel@tonic-gate #include "traverse.h" 477c478bd9Sstevel@tonic-gate #include "symbol.h" 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate typedef struct iidesc_match { 507c478bd9Sstevel@tonic-gate int iim_fuzzy; 517c478bd9Sstevel@tonic-gate iidesc_t *iim_ret; 527c478bd9Sstevel@tonic-gate char *iim_name; 537c478bd9Sstevel@tonic-gate char *iim_file; 547c478bd9Sstevel@tonic-gate uchar_t iim_bind; 557c478bd9Sstevel@tonic-gate } iidesc_match_t; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static int 587c478bd9Sstevel@tonic-gate burst_iitypes(void *data, void *arg) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate iidesc_t *ii = data; 617c478bd9Sstevel@tonic-gate iiburst_t *iiburst = arg; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate switch (ii->ii_type) { 647c478bd9Sstevel@tonic-gate case II_GFUN: 657c478bd9Sstevel@tonic-gate case II_SFUN: 667c478bd9Sstevel@tonic-gate case II_GVAR: 677c478bd9Sstevel@tonic-gate case II_SVAR: 687c478bd9Sstevel@tonic-gate if (!(ii->ii_flags & IIDESC_F_USED)) 697c478bd9Sstevel@tonic-gate return (0); 707c478bd9Sstevel@tonic-gate break; 717c478bd9Sstevel@tonic-gate default: 727c478bd9Sstevel@tonic-gate break; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate ii->ii_dtype->t_flags |= TDESC_F_ISROOT; 767c478bd9Sstevel@tonic-gate (void) iitraverse_td(ii, iiburst->iib_tdtd); 777c478bd9Sstevel@tonic-gate return (1); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 817c478bd9Sstevel@tonic-gate static int 827c478bd9Sstevel@tonic-gate save_type_by_id(tdesc_t *tdp, tdesc_t **tdpp, void *private) 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate iiburst_t *iiburst = private; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Doing this on every node is horribly inefficient, but given that 887c478bd9Sstevel@tonic-gate * we may be suppressing some types, we can't trust nextid in the 897c478bd9Sstevel@tonic-gate * tdata_t. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate if (tdp->t_id > iiburst->iib_maxtypeid) 927c478bd9Sstevel@tonic-gate iiburst->iib_maxtypeid = tdp->t_id; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate slist_add(&iiburst->iib_types, tdp, tdesc_idcmp); 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate return (1); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate static tdtrav_cb_f burst_types_cbs[] = { 1007c478bd9Sstevel@tonic-gate NULL, 1017c478bd9Sstevel@tonic-gate save_type_by_id, /* intrinsic */ 1027c478bd9Sstevel@tonic-gate save_type_by_id, /* pointer */ 1037c478bd9Sstevel@tonic-gate save_type_by_id, /* array */ 1047c478bd9Sstevel@tonic-gate save_type_by_id, /* function */ 1057c478bd9Sstevel@tonic-gate save_type_by_id, /* struct */ 1067c478bd9Sstevel@tonic-gate save_type_by_id, /* union */ 1077c478bd9Sstevel@tonic-gate save_type_by_id, /* enum */ 1087c478bd9Sstevel@tonic-gate save_type_by_id, /* forward */ 1097c478bd9Sstevel@tonic-gate save_type_by_id, /* typedef */ 1107c478bd9Sstevel@tonic-gate tdtrav_assert, /* typedef_unres */ 1117c478bd9Sstevel@tonic-gate save_type_by_id, /* volatile */ 1127c478bd9Sstevel@tonic-gate save_type_by_id, /* const */ 1137c478bd9Sstevel@tonic-gate save_type_by_id /* restrict */ 1147c478bd9Sstevel@tonic-gate }; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static iiburst_t * 1187c478bd9Sstevel@tonic-gate iiburst_new(tdata_t *td, int max) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate iiburst_t *iiburst = xcalloc(sizeof (iiburst_t)); 1217c478bd9Sstevel@tonic-gate iiburst->iib_td = td; 1227c478bd9Sstevel@tonic-gate iiburst->iib_funcs = xcalloc(sizeof (iidesc_t *) * max); 1237c478bd9Sstevel@tonic-gate iiburst->iib_nfuncs = 0; 1247c478bd9Sstevel@tonic-gate iiburst->iib_objts = xcalloc(sizeof (iidesc_t *) * max); 1257c478bd9Sstevel@tonic-gate iiburst->iib_nobjts = 0; 1267c478bd9Sstevel@tonic-gate return (iiburst); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate static void 1307c478bd9Sstevel@tonic-gate iiburst_types(iiburst_t *iiburst) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate tdtrav_data_t tdtd; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate tdtrav_init(&tdtd, &iiburst->iib_td->td_curvgen, NULL, burst_types_cbs, 1357c478bd9Sstevel@tonic-gate NULL, (void *)iiburst); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate iiburst->iib_tdtd = &tdtd; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate (void) hash_iter(iiburst->iib_td->td_iihash, burst_iitypes, iiburst); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate static void 1437c478bd9Sstevel@tonic-gate iiburst_free(iiburst_t *iiburst) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate free(iiburst->iib_funcs); 1467c478bd9Sstevel@tonic-gate free(iiburst->iib_objts); 1477c478bd9Sstevel@tonic-gate list_free(iiburst->iib_types, NULL, NULL); 1487c478bd9Sstevel@tonic-gate free(iiburst); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * See if this iidesc matches the ELF symbol data we pass in. 1537c478bd9Sstevel@tonic-gate * 1547c478bd9Sstevel@tonic-gate * A fuzzy match is where we have a local symbol matching the name of a 1557c478bd9Sstevel@tonic-gate * global type description. This is common when a mapfile is used for a 1567c478bd9Sstevel@tonic-gate * DSO, but we don't accept it by default. 1577c478bd9Sstevel@tonic-gate * 1587c478bd9Sstevel@tonic-gate * A weak fuzzy match is when a weak symbol was resolved and matched to 1597c478bd9Sstevel@tonic-gate * a global type description. 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate static int 1627c478bd9Sstevel@tonic-gate matching_iidesc(iidesc_t *iidesc, iidesc_match_t *match) 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate if (streq(iidesc->ii_name, match->iim_name) == 0) 1657c478bd9Sstevel@tonic-gate return (0); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate switch (iidesc->ii_type) { 1687c478bd9Sstevel@tonic-gate case II_GFUN: 1697c478bd9Sstevel@tonic-gate case II_GVAR: 1707c478bd9Sstevel@tonic-gate if (match->iim_bind == STB_GLOBAL) { 1717c478bd9Sstevel@tonic-gate match->iim_ret = iidesc; 1727c478bd9Sstevel@tonic-gate return (-1); 1737c478bd9Sstevel@tonic-gate } else if (match->iim_fuzzy && match->iim_ret == NULL) { 1747c478bd9Sstevel@tonic-gate match->iim_ret = iidesc; 1757c478bd9Sstevel@tonic-gate /* continue to look for strong match */ 1767c478bd9Sstevel@tonic-gate return (0); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate break; 1797c478bd9Sstevel@tonic-gate case II_SFUN: 1807c478bd9Sstevel@tonic-gate case II_SVAR: 1817c478bd9Sstevel@tonic-gate if (match->iim_bind == STB_LOCAL && 1827c478bd9Sstevel@tonic-gate match->iim_file != NULL && 1837c478bd9Sstevel@tonic-gate streq(iidesc->ii_owner, match->iim_file)) { 1847c478bd9Sstevel@tonic-gate match->iim_ret = iidesc; 1857c478bd9Sstevel@tonic-gate return (-1); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate return (0); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate static iidesc_t * 1937c478bd9Sstevel@tonic-gate find_iidesc(hash_t *hash, iidesc_match_t *match) 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate iidesc_t tmpdesc; 1967c478bd9Sstevel@tonic-gate match->iim_ret = NULL; 1977c478bd9Sstevel@tonic-gate bzero(&tmpdesc, sizeof (iidesc_t)); 1987c478bd9Sstevel@tonic-gate tmpdesc.ii_name = match->iim_name; 1997c478bd9Sstevel@tonic-gate (void) hash_match(hash, &tmpdesc, (int (*)())matching_iidesc, match); 2007c478bd9Sstevel@tonic-gate return (match->iim_ret); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* 2047c478bd9Sstevel@tonic-gate * If we have a weak symbol, attempt to find the strong symbol it will 2057c478bd9Sstevel@tonic-gate * resolve to. Note: the code where this actually happens is in 2067c478bd9Sstevel@tonic-gate * sym_process() in cmd/sgs/libld/common/syms.c 2077c478bd9Sstevel@tonic-gate * 2087c478bd9Sstevel@tonic-gate * Finding the matching symbol is unfortunately not trivial. For a 2097c478bd9Sstevel@tonic-gate * symbol to be a candidate, it must: 2107c478bd9Sstevel@tonic-gate * 2117c478bd9Sstevel@tonic-gate * - have the same type (function, object) 2127c478bd9Sstevel@tonic-gate * - have the same value (address) 2137c478bd9Sstevel@tonic-gate * - have the same size 2147c478bd9Sstevel@tonic-gate * - not be another weak symbol 2157c478bd9Sstevel@tonic-gate * - belong to the same section (checked via section index) 2167c478bd9Sstevel@tonic-gate * 2177c478bd9Sstevel@tonic-gate * If such a candidate is global, then we assume we've found it. The 2187c478bd9Sstevel@tonic-gate * linker generates the symbol table such that the curfile might be 2197c478bd9Sstevel@tonic-gate * incorrect; this is OK for global symbols, since find_iidesc() doesn't 2207c478bd9Sstevel@tonic-gate * need to check for the source file for the symbol. 2217c478bd9Sstevel@tonic-gate * 2227c478bd9Sstevel@tonic-gate * We might have found a strong local symbol, where the curfile is 2237c478bd9Sstevel@tonic-gate * accurate and matches that of the weak symbol. We assume this is a 2247c478bd9Sstevel@tonic-gate * reasonable match. 2257c478bd9Sstevel@tonic-gate * 2267c478bd9Sstevel@tonic-gate * If we've got a local symbol with a non-matching curfile, there are 2277c478bd9Sstevel@tonic-gate * two possibilities. Either this is a completely different symbol, or 2287c478bd9Sstevel@tonic-gate * it's a once-global symbol that was scoped to local via a mapfile. In 2297c478bd9Sstevel@tonic-gate * the latter case, curfile is likely inaccurate since the linker does 2307c478bd9Sstevel@tonic-gate * not preserve the needed curfile in the order of the symbol table (see 2317c478bd9Sstevel@tonic-gate * the comments about locally scoped symbols in libld's update_osym()). 2327c478bd9Sstevel@tonic-gate * As we can't tell this case from the former one, we use this symbol 2337c478bd9Sstevel@tonic-gate * iff no other matching symbol is found. 2347c478bd9Sstevel@tonic-gate * 2357c478bd9Sstevel@tonic-gate * What we really need here is a SUNW section containing weak<->strong 2367c478bd9Sstevel@tonic-gate * mappings that we can consume. 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate static int 2397c478bd9Sstevel@tonic-gate check_for_weak(GElf_Sym *weak, char const *weakfile, 2407c478bd9Sstevel@tonic-gate Elf_Data *data, int nent, Elf_Data *strdata, 2417c478bd9Sstevel@tonic-gate GElf_Sym *retsym, char **curfilep) 2427c478bd9Sstevel@tonic-gate { 2437c478bd9Sstevel@tonic-gate char *curfile = NULL; 2447c478bd9Sstevel@tonic-gate char *tmpfile; 2457c478bd9Sstevel@tonic-gate GElf_Sym tmpsym; 2467c478bd9Sstevel@tonic-gate int candidate = 0; 2477c478bd9Sstevel@tonic-gate int i; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(weak->st_info) != STB_WEAK) 2507c478bd9Sstevel@tonic-gate return (0); 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate for (i = 0; i < nent; i++) { 2537c478bd9Sstevel@tonic-gate GElf_Sym sym; 2547c478bd9Sstevel@tonic-gate uchar_t type; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (gelf_getsym(data, i, &sym) == NULL) 2577c478bd9Sstevel@tonic-gate continue; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate type = GELF_ST_TYPE(sym.st_info); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate if (type == STT_FILE) 2627c478bd9Sstevel@tonic-gate curfile = (char *)strdata->d_buf + sym.st_name; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate if (GELF_ST_TYPE(weak->st_info) != type || 2657c478bd9Sstevel@tonic-gate weak->st_value != sym.st_value) 2667c478bd9Sstevel@tonic-gate continue; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate if (weak->st_size != sym.st_size) 2697c478bd9Sstevel@tonic-gate continue; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(sym.st_info) == STB_WEAK) 2727c478bd9Sstevel@tonic-gate continue; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (sym.st_shndx != weak->st_shndx) 2757c478bd9Sstevel@tonic-gate continue; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (GELF_ST_BIND(sym.st_info) == STB_LOCAL && 2787c478bd9Sstevel@tonic-gate (curfile == NULL || weakfile == NULL || 2797c478bd9Sstevel@tonic-gate strcmp(curfile, weakfile) != 0)) { 2807c478bd9Sstevel@tonic-gate candidate = 1; 2817c478bd9Sstevel@tonic-gate tmpfile = curfile; 2827c478bd9Sstevel@tonic-gate tmpsym = sym; 2837c478bd9Sstevel@tonic-gate continue; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate *curfilep = curfile; 2877c478bd9Sstevel@tonic-gate *retsym = sym; 2887c478bd9Sstevel@tonic-gate return (1); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (candidate) { 2927c478bd9Sstevel@tonic-gate *curfilep = tmpfile; 2937c478bd9Sstevel@tonic-gate *retsym = tmpsym; 2947c478bd9Sstevel@tonic-gate return (1); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate return (0); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate /* 3017c478bd9Sstevel@tonic-gate * When we've found the underlying symbol's type description 3027c478bd9Sstevel@tonic-gate * for a weak symbol, we need to copy it and rename it to match 3037c478bd9Sstevel@tonic-gate * the weak symbol. We also need to add it to the td so it's 3047c478bd9Sstevel@tonic-gate * handled along with the others later. 3057c478bd9Sstevel@tonic-gate */ 3067c478bd9Sstevel@tonic-gate static iidesc_t * 3077c478bd9Sstevel@tonic-gate copy_from_strong(tdata_t *td, GElf_Sym *sym, iidesc_t *strongdesc, 3087c478bd9Sstevel@tonic-gate const char *weakname, const char *weakfile) 3097c478bd9Sstevel@tonic-gate { 3107c478bd9Sstevel@tonic-gate iidesc_t *new = iidesc_dup_rename(strongdesc, weakname, weakfile); 3117c478bd9Sstevel@tonic-gate uchar_t type = GELF_ST_TYPE(sym->st_info); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate switch (type) { 3147c478bd9Sstevel@tonic-gate case STT_OBJECT: 3157c478bd9Sstevel@tonic-gate new->ii_type = II_GVAR; 3167c478bd9Sstevel@tonic-gate break; 3177c478bd9Sstevel@tonic-gate case STT_FUNC: 3187c478bd9Sstevel@tonic-gate new->ii_type = II_GFUN; 3197c478bd9Sstevel@tonic-gate break; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate hash_add(td->td_iihash, new); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate return (new); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate /* 3287c478bd9Sstevel@tonic-gate * Process the symbol table of the output file, associating each symbol 3297c478bd9Sstevel@tonic-gate * with a type description if possible, and sorting them into functions 3307c478bd9Sstevel@tonic-gate * and data, maintaining symbol table order. 3317c478bd9Sstevel@tonic-gate */ 3327c478bd9Sstevel@tonic-gate static iiburst_t * 3337c478bd9Sstevel@tonic-gate sort_iidescs(Elf *elf, const char *file, tdata_t *td, int fuzzymatch, 3347c478bd9Sstevel@tonic-gate int dynsym) 3357c478bd9Sstevel@tonic-gate { 3367c478bd9Sstevel@tonic-gate iiburst_t *iiburst; 3377c478bd9Sstevel@tonic-gate Elf_Scn *scn; 3387c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 3397c478bd9Sstevel@tonic-gate Elf_Data *data, *strdata; 3407c478bd9Sstevel@tonic-gate int i, stidx; 3417c478bd9Sstevel@tonic-gate int nent; 3427c478bd9Sstevel@tonic-gate iidesc_match_t match; 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate match.iim_fuzzy = fuzzymatch; 3457c478bd9Sstevel@tonic-gate match.iim_file = NULL; 3467c478bd9Sstevel@tonic-gate 347*c168da27Sjohnlev if ((stidx = findelfsecidx(elf, file, 348*c168da27Sjohnlev dynsym ? ".dynsym" : ".symtab")) < 0) 3497c478bd9Sstevel@tonic-gate terminate("%s: Can't open symbol table\n", file); 3507c478bd9Sstevel@tonic-gate scn = elf_getscn(elf, stidx); 3517c478bd9Sstevel@tonic-gate data = elf_getdata(scn, NULL); 3527c478bd9Sstevel@tonic-gate gelf_getshdr(scn, &shdr); 3537c478bd9Sstevel@tonic-gate nent = shdr.sh_size / shdr.sh_entsize; 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate scn = elf_getscn(elf, shdr.sh_link); 3567c478bd9Sstevel@tonic-gate strdata = elf_getdata(scn, NULL); 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate iiburst = iiburst_new(td, nent); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate for (i = 0; i < nent; i++) { 3617c478bd9Sstevel@tonic-gate GElf_Sym sym; 3627c478bd9Sstevel@tonic-gate iidesc_t **tolist; 3637c478bd9Sstevel@tonic-gate GElf_Sym ssym; 3647c478bd9Sstevel@tonic-gate iidesc_match_t smatch; 3657c478bd9Sstevel@tonic-gate int *curr; 3667c478bd9Sstevel@tonic-gate iidesc_t *iidesc; 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate if (gelf_getsym(data, i, &sym) == NULL) 3697c478bd9Sstevel@tonic-gate elfterminate(file, "Couldn't read symbol %d", i); 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate match.iim_name = (char *)strdata->d_buf + sym.st_name; 3727c478bd9Sstevel@tonic-gate match.iim_bind = GELF_ST_BIND(sym.st_info); 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate switch (GELF_ST_TYPE(sym.st_info)) { 3757c478bd9Sstevel@tonic-gate case STT_FILE: 3767c478bd9Sstevel@tonic-gate match.iim_file = match.iim_name; 3777c478bd9Sstevel@tonic-gate continue; 3787c478bd9Sstevel@tonic-gate case STT_OBJECT: 3797c478bd9Sstevel@tonic-gate tolist = iiburst->iib_objts; 3807c478bd9Sstevel@tonic-gate curr = &iiburst->iib_nobjts; 3817c478bd9Sstevel@tonic-gate break; 3827c478bd9Sstevel@tonic-gate case STT_FUNC: 3837c478bd9Sstevel@tonic-gate tolist = iiburst->iib_funcs; 3847c478bd9Sstevel@tonic-gate curr = &iiburst->iib_nfuncs; 3857c478bd9Sstevel@tonic-gate break; 3867c478bd9Sstevel@tonic-gate default: 3877c478bd9Sstevel@tonic-gate continue; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate if (ignore_symbol(&sym, match.iim_name)) 3917c478bd9Sstevel@tonic-gate continue; 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate iidesc = find_iidesc(td->td_iihash, &match); 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate if (iidesc != NULL) { 3967c478bd9Sstevel@tonic-gate tolist[*curr] = iidesc; 3977c478bd9Sstevel@tonic-gate iidesc->ii_flags |= IIDESC_F_USED; 3987c478bd9Sstevel@tonic-gate (*curr)++; 3997c478bd9Sstevel@tonic-gate continue; 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate if (!check_for_weak(&sym, match.iim_file, data, nent, strdata, 4037c478bd9Sstevel@tonic-gate &ssym, &smatch.iim_file)) { 4047c478bd9Sstevel@tonic-gate (*curr)++; 4057c478bd9Sstevel@tonic-gate continue; 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate smatch.iim_fuzzy = fuzzymatch; 4097c478bd9Sstevel@tonic-gate smatch.iim_name = (char *)strdata->d_buf + ssym.st_name; 4107c478bd9Sstevel@tonic-gate smatch.iim_bind = GELF_ST_BIND(ssym.st_info); 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate debug(3, "Weak symbol %s resolved to %s\n", match.iim_name, 4137c478bd9Sstevel@tonic-gate smatch.iim_name); 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate iidesc = find_iidesc(td->td_iihash, &smatch); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (iidesc != NULL) { 4187c478bd9Sstevel@tonic-gate tolist[*curr] = copy_from_strong(td, &sym, 4197c478bd9Sstevel@tonic-gate iidesc, match.iim_name, match.iim_file); 4207c478bd9Sstevel@tonic-gate tolist[*curr]->ii_flags |= IIDESC_F_USED; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate (*curr)++; 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate /* 4277c478bd9Sstevel@tonic-gate * Stabs are generated for every function declared in a given C source 4287c478bd9Sstevel@tonic-gate * file. When converting an object file, we may encounter a stab that 4297c478bd9Sstevel@tonic-gate * has no symbol table entry because the optimizer has decided to omit 4307c478bd9Sstevel@tonic-gate * that item (for example, an unreferenced static function). We may 4317c478bd9Sstevel@tonic-gate * see iidescs that do not have an associated symtab entry, and so 4327c478bd9Sstevel@tonic-gate * we do not write records for those functions into the CTF data. 4337c478bd9Sstevel@tonic-gate * All others get marked as a root by this function. 4347c478bd9Sstevel@tonic-gate */ 4357c478bd9Sstevel@tonic-gate iiburst_types(iiburst); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * By not adding some of the functions and/or objects, we may have 4397c478bd9Sstevel@tonic-gate * caused some types that were referenced solely by those 4407c478bd9Sstevel@tonic-gate * functions/objects to be suppressed. This could cause a label, 4417c478bd9Sstevel@tonic-gate * generated prior to the evisceration, to be incorrect. Find the 4427c478bd9Sstevel@tonic-gate * highest type index, and change the label indicies to be no higher 4437c478bd9Sstevel@tonic-gate * than this value. 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate tdata_label_newmax(td, iiburst->iib_maxtypeid); 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate return (iiburst); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate static void 4517c478bd9Sstevel@tonic-gate write_file(Elf *src, const char *srcname, Elf *dst, const char *dstname, 4527c478bd9Sstevel@tonic-gate caddr_t ctfdata, size_t ctfsize, int flags) 4537c478bd9Sstevel@tonic-gate { 4547c478bd9Sstevel@tonic-gate GElf_Ehdr sehdr, dehdr; 4557c478bd9Sstevel@tonic-gate Elf_Scn *sscn, *dscn; 4567c478bd9Sstevel@tonic-gate Elf_Data *sdata, *ddata; 4577c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 4587c478bd9Sstevel@tonic-gate GElf_Word symtab_type; 4597c478bd9Sstevel@tonic-gate int symtab_idx = -1; 4607c478bd9Sstevel@tonic-gate off_t new_offset = 0; 4617c478bd9Sstevel@tonic-gate off_t ctfnameoff = 0; 4627c478bd9Sstevel@tonic-gate int dynsym = (flags & CTF_USE_DYNSYM); 4637c478bd9Sstevel@tonic-gate int keep_stabs = (flags & CTF_KEEP_STABS); 4647c478bd9Sstevel@tonic-gate int *secxlate; 4657c478bd9Sstevel@tonic-gate int srcidx, dstidx; 4667c478bd9Sstevel@tonic-gate int curnmoff = 0; 4677c478bd9Sstevel@tonic-gate int changing = 0; 4687c478bd9Sstevel@tonic-gate int pad; 4697c478bd9Sstevel@tonic-gate int i; 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate if (gelf_newehdr(dst, gelf_getclass(src)) == NULL) 4727c478bd9Sstevel@tonic-gate elfterminate(dstname, "Cannot copy ehdr to temp file"); 4737c478bd9Sstevel@tonic-gate gelf_getehdr(src, &sehdr); 4747c478bd9Sstevel@tonic-gate memcpy(&dehdr, &sehdr, sizeof (GElf_Ehdr)); 4757c478bd9Sstevel@tonic-gate gelf_update_ehdr(dst, &dehdr); 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate symtab_type = dynsym ? SHT_DYNSYM : SHT_SYMTAB; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate /* 4807c478bd9Sstevel@tonic-gate * Neither the existing stab sections nor the SUNW_ctf sections (new or 4817c478bd9Sstevel@tonic-gate * existing) are SHF_ALLOC'd, so they won't be in areas referenced by 4827c478bd9Sstevel@tonic-gate * program headers. As such, we can just blindly copy the program 4837c478bd9Sstevel@tonic-gate * headers from the existing file to the new file. 4847c478bd9Sstevel@tonic-gate */ 4857c478bd9Sstevel@tonic-gate if (sehdr.e_phnum != 0) { 4867c478bd9Sstevel@tonic-gate (void) elf_flagelf(dst, ELF_C_SET, ELF_F_LAYOUT); 4877c478bd9Sstevel@tonic-gate if (gelf_newphdr(dst, sehdr.e_phnum) == NULL) 4887c478bd9Sstevel@tonic-gate elfterminate(dstname, "Cannot make phdrs in temp file"); 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate for (i = 0; i < sehdr.e_phnum; i++) { 4917c478bd9Sstevel@tonic-gate GElf_Phdr phdr; 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate gelf_getphdr(src, i, &phdr); 4947c478bd9Sstevel@tonic-gate gelf_update_phdr(dst, i, &phdr); 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate secxlate = xmalloc(sizeof (int) * sehdr.e_shnum); 4997c478bd9Sstevel@tonic-gate for (srcidx = dstidx = 0; srcidx < sehdr.e_shnum; srcidx++) { 5007c478bd9Sstevel@tonic-gate Elf_Scn *scn = elf_getscn(src, srcidx); 5017c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 5027c478bd9Sstevel@tonic-gate char *sname; 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate gelf_getshdr(scn, &shdr); 5057c478bd9Sstevel@tonic-gate sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name); 5067c478bd9Sstevel@tonic-gate if (sname == NULL) { 5077c478bd9Sstevel@tonic-gate elfterminate(srcname, "Can't find string at %u", 5087c478bd9Sstevel@tonic-gate shdr.sh_name); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate if (strcmp(sname, CTF_ELF_SCN_NAME) == 0) { 5127c478bd9Sstevel@tonic-gate secxlate[srcidx] = -1; 5137c478bd9Sstevel@tonic-gate } else if (!keep_stabs && 5147c478bd9Sstevel@tonic-gate (strncmp(sname, ".stab", 5) == 0 || 5157c478bd9Sstevel@tonic-gate strncmp(sname, ".debug", 6) == 0 || 5167c478bd9Sstevel@tonic-gate strncmp(sname, ".rel.debug", 10) == 0 || 5177c478bd9Sstevel@tonic-gate strncmp(sname, ".rela.debug", 11) == 0)) { 5187c478bd9Sstevel@tonic-gate secxlate[srcidx] = -1; 5197c478bd9Sstevel@tonic-gate } else if (dynsym && shdr.sh_type == SHT_SYMTAB) { 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * If we're building CTF against the dynsym, 5227c478bd9Sstevel@tonic-gate * we'll rip out the symtab so debuggers aren't 5237c478bd9Sstevel@tonic-gate * confused. 5247c478bd9Sstevel@tonic-gate */ 5257c478bd9Sstevel@tonic-gate secxlate[srcidx] = -1; 5267c478bd9Sstevel@tonic-gate } else { 5277c478bd9Sstevel@tonic-gate secxlate[srcidx] = dstidx++; 5287c478bd9Sstevel@tonic-gate curnmoff += strlen(sname) + 1; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate new_offset = (off_t)dehdr.e_phoff; 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate for (srcidx = 1; srcidx < sehdr.e_shnum; srcidx++) { 5357c478bd9Sstevel@tonic-gate char *sname; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate sscn = elf_getscn(src, srcidx); 5387c478bd9Sstevel@tonic-gate gelf_getshdr(sscn, &shdr); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (secxlate[srcidx] == -1) { 5417c478bd9Sstevel@tonic-gate changing = 1; 5427c478bd9Sstevel@tonic-gate continue; 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate dscn = elf_newscn(dst); 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate /* 5487c478bd9Sstevel@tonic-gate * If this file has program headers, we need to explicitly lay 5497c478bd9Sstevel@tonic-gate * out sections. If none of the sections prior to this one have 5507c478bd9Sstevel@tonic-gate * been removed, then we can just use the existing location. If 5517c478bd9Sstevel@tonic-gate * one or more sections have been changed, then we need to 5527c478bd9Sstevel@tonic-gate * adjust this one to avoid holes. 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate if (changing && sehdr.e_phnum != 0) { 5557c478bd9Sstevel@tonic-gate pad = new_offset % shdr.sh_addralign; 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (pad) 5587c478bd9Sstevel@tonic-gate new_offset += shdr.sh_addralign - pad; 5597c478bd9Sstevel@tonic-gate shdr.sh_offset = new_offset; 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate shdr.sh_link = secxlate[shdr.sh_link]; 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate if (shdr.sh_type == SHT_REL || shdr.sh_type == SHT_RELA) 5657c478bd9Sstevel@tonic-gate shdr.sh_info = secxlate[shdr.sh_info]; 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name); 5687c478bd9Sstevel@tonic-gate if (sname == NULL) { 5697c478bd9Sstevel@tonic-gate elfterminate(srcname, "Can't find string at %u", 5707c478bd9Sstevel@tonic-gate shdr.sh_name); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate if ((sdata = elf_getdata(sscn, NULL)) == NULL) 5737c478bd9Sstevel@tonic-gate elfterminate(srcname, "Cannot get sect %s data", sname); 5747c478bd9Sstevel@tonic-gate if ((ddata = elf_newdata(dscn)) == NULL) 5757c478bd9Sstevel@tonic-gate elfterminate(dstname, "Can't make sect %s data", sname); 5767c478bd9Sstevel@tonic-gate bcopy(sdata, ddata, sizeof (Elf_Data)); 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate if (srcidx == sehdr.e_shstrndx) { 5797c478bd9Sstevel@tonic-gate char seclen = strlen(CTF_ELF_SCN_NAME); 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate ddata->d_buf = xmalloc(ddata->d_size + shdr.sh_size + 5827c478bd9Sstevel@tonic-gate seclen + 1); 5837c478bd9Sstevel@tonic-gate bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size); 5847c478bd9Sstevel@tonic-gate strcpy((caddr_t)ddata->d_buf + shdr.sh_size, 5857c478bd9Sstevel@tonic-gate CTF_ELF_SCN_NAME); 5867c478bd9Sstevel@tonic-gate ctfnameoff = (off_t)shdr.sh_size; 5877c478bd9Sstevel@tonic-gate shdr.sh_size += seclen + 1; 5887c478bd9Sstevel@tonic-gate ddata->d_size += seclen + 1; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate if (sehdr.e_phnum != 0) 5917c478bd9Sstevel@tonic-gate changing = 1; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate if (shdr.sh_type == symtab_type && shdr.sh_entsize != 0) { 5957c478bd9Sstevel@tonic-gate int nsym = shdr.sh_size / shdr.sh_entsize; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate symtab_idx = secxlate[srcidx]; 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate ddata->d_buf = xmalloc(shdr.sh_size); 6007c478bd9Sstevel@tonic-gate bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size); 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate for (i = 0; i < nsym; i++) { 6037c478bd9Sstevel@tonic-gate GElf_Sym sym; 6047c478bd9Sstevel@tonic-gate short newscn; 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate (void) gelf_getsym(ddata, i, &sym); 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate if (sym.st_shndx >= SHN_LORESERVE) 6097c478bd9Sstevel@tonic-gate continue; 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate if ((newscn = secxlate[sym.st_shndx]) != 6127c478bd9Sstevel@tonic-gate sym.st_shndx) { 6137c478bd9Sstevel@tonic-gate sym.st_shndx = 6147c478bd9Sstevel@tonic-gate (newscn == -1 ? 1 : newscn); 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate gelf_update_sym(ddata, i, &sym); 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate if (gelf_update_shdr(dscn, &shdr) == NULL) 6227c478bd9Sstevel@tonic-gate elfterminate(dstname, "Cannot update sect %s", sname); 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate new_offset = (off_t)shdr.sh_offset; 6257c478bd9Sstevel@tonic-gate if (shdr.sh_type != SHT_NOBITS) 6267c478bd9Sstevel@tonic-gate new_offset += shdr.sh_size; 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate if (symtab_idx == -1) { 630*c168da27Sjohnlev terminate("%s: Cannot find %s section\n", srcname, 6317c478bd9Sstevel@tonic-gate dynsym ? "SHT_DYNSYM" : "SHT_SYMTAB"); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* Add the ctf section */ 6357c478bd9Sstevel@tonic-gate dscn = elf_newscn(dst); 6367c478bd9Sstevel@tonic-gate gelf_getshdr(dscn, &shdr); 6377c478bd9Sstevel@tonic-gate shdr.sh_name = ctfnameoff; 6387c478bd9Sstevel@tonic-gate shdr.sh_type = SHT_PROGBITS; 6397c478bd9Sstevel@tonic-gate shdr.sh_size = ctfsize; 6407c478bd9Sstevel@tonic-gate shdr.sh_link = symtab_idx; 6417c478bd9Sstevel@tonic-gate shdr.sh_addralign = 4; 6427c478bd9Sstevel@tonic-gate if (changing && sehdr.e_phnum != 0) { 6437c478bd9Sstevel@tonic-gate pad = new_offset % shdr.sh_addralign; 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate if (pad) 6467c478bd9Sstevel@tonic-gate new_offset += shdr.sh_addralign - pad; 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate shdr.sh_offset = new_offset; 6497c478bd9Sstevel@tonic-gate new_offset += shdr.sh_size; 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate ddata = elf_newdata(dscn); 6537c478bd9Sstevel@tonic-gate ddata->d_buf = ctfdata; 6547c478bd9Sstevel@tonic-gate ddata->d_size = ctfsize; 6557c478bd9Sstevel@tonic-gate ddata->d_align = shdr.sh_addralign; 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate gelf_update_shdr(dscn, &shdr); 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate /* update the section header location */ 6607c478bd9Sstevel@tonic-gate if (sehdr.e_phnum != 0) { 6617c478bd9Sstevel@tonic-gate size_t align = gelf_fsize(dst, ELF_T_ADDR, 1, EV_CURRENT); 6627c478bd9Sstevel@tonic-gate size_t r = new_offset % align; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate if (r) 6657c478bd9Sstevel@tonic-gate new_offset += align - r; 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate dehdr.e_shoff = new_offset; 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate /* commit to disk */ 6717c478bd9Sstevel@tonic-gate dehdr.e_shstrndx = secxlate[sehdr.e_shstrndx]; 6727c478bd9Sstevel@tonic-gate gelf_update_ehdr(dst, &dehdr); 6737c478bd9Sstevel@tonic-gate if (elf_update(dst, ELF_C_WRITE) < 0) 6747c478bd9Sstevel@tonic-gate elfterminate(dstname, "Cannot finalize temp file"); 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate free(secxlate); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate static caddr_t 6807c478bd9Sstevel@tonic-gate make_ctf_data(tdata_t *td, Elf *elf, const char *file, size_t *lenp, int flags) 6817c478bd9Sstevel@tonic-gate { 6827c478bd9Sstevel@tonic-gate iiburst_t *iiburst; 6837c478bd9Sstevel@tonic-gate caddr_t data; 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate iiburst = sort_iidescs(elf, file, td, flags & CTF_FUZZY_MATCH, 6867c478bd9Sstevel@tonic-gate flags & CTF_USE_DYNSYM); 6877c478bd9Sstevel@tonic-gate data = ctf_gen(iiburst, lenp, flags & CTF_COMPRESS); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate iiburst_free(iiburst); 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate return (data); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate void 6957c478bd9Sstevel@tonic-gate write_ctf(tdata_t *td, const char *curname, const char *newname, int flags) 6967c478bd9Sstevel@tonic-gate { 6977c478bd9Sstevel@tonic-gate struct stat st; 6987c478bd9Sstevel@tonic-gate Elf *elf = NULL; 6997c478bd9Sstevel@tonic-gate Elf *telf = NULL; 7007c478bd9Sstevel@tonic-gate caddr_t data; 7017c478bd9Sstevel@tonic-gate size_t len; 7027c478bd9Sstevel@tonic-gate int fd = -1; 7037c478bd9Sstevel@tonic-gate int tfd = -1; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate (void) elf_version(EV_CURRENT); 7067c478bd9Sstevel@tonic-gate if ((fd = open(curname, O_RDONLY)) < 0 || fstat(fd, &st) < 0) 7077c478bd9Sstevel@tonic-gate terminate("%s: Cannot open for re-reading", curname); 7087c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) 7097c478bd9Sstevel@tonic-gate elfterminate(curname, "Cannot re-read"); 7107c478bd9Sstevel@tonic-gate 7114d232658Sjohnlev if ((tfd = open(newname, O_RDWR | O_CREAT | O_TRUNC, st.st_mode)) < 0) 7127c478bd9Sstevel@tonic-gate terminate("Cannot open temp file %s for writing", newname); 7137c478bd9Sstevel@tonic-gate if ((telf = elf_begin(tfd, ELF_C_WRITE, NULL)) == NULL) 7147c478bd9Sstevel@tonic-gate elfterminate(curname, "Cannot write"); 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate data = make_ctf_data(td, elf, curname, &len, flags); 7177c478bd9Sstevel@tonic-gate write_file(elf, curname, telf, newname, data, len, flags); 7187c478bd9Sstevel@tonic-gate free(data); 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate elf_end(telf); 7217c478bd9Sstevel@tonic-gate elf_end(elf); 7227c478bd9Sstevel@tonic-gate (void) close(fd); 7237c478bd9Sstevel@tonic-gate (void) close(tfd); 7247c478bd9Sstevel@tonic-gate } 725