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