11673e404SJohn Birrell /*
21673e404SJohn Birrell * CDDL HEADER START
31673e404SJohn Birrell *
41673e404SJohn Birrell * The contents of this file are subject to the terms of the
51673e404SJohn Birrell * Common Development and Distribution License (the "License").
61673e404SJohn Birrell * You may not use this file except in compliance with the License.
71673e404SJohn Birrell *
81673e404SJohn Birrell * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91673e404SJohn Birrell * or http://www.opensolaris.org/os/licensing.
101673e404SJohn Birrell * See the License for the specific language governing permissions
111673e404SJohn Birrell * and limitations under the License.
121673e404SJohn Birrell *
131673e404SJohn Birrell * When distributing Covered Code, include this CDDL HEADER in each
141673e404SJohn Birrell * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151673e404SJohn Birrell * If applicable, add the following below this CDDL HEADER, with the
161673e404SJohn Birrell * fields enclosed by brackets "[]" replaced with your own identifying
171673e404SJohn Birrell * information: Portions Copyright [yyyy] [name of copyright owner]
181673e404SJohn Birrell *
191673e404SJohn Birrell * CDDL HEADER END
201673e404SJohn Birrell */
211673e404SJohn Birrell /*
221673e404SJohn Birrell * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
231673e404SJohn Birrell * Use is subject to license terms.
241673e404SJohn Birrell */
251673e404SJohn Birrell
261673e404SJohn Birrell /*
271673e404SJohn Birrell * Routines for preparing tdata trees for conversion into CTF data, and
281673e404SJohn Birrell * for placing the resulting data into an output file.
291673e404SJohn Birrell */
301673e404SJohn Birrell
311673e404SJohn Birrell #include <stdio.h>
321673e404SJohn Birrell #include <stdlib.h>
331673e404SJohn Birrell #include <strings.h>
341673e404SJohn Birrell #include <sys/types.h>
351673e404SJohn Birrell #include <sys/stat.h>
361673e404SJohn Birrell #include <fcntl.h>
371673e404SJohn Birrell #include <libelf.h>
381673e404SJohn Birrell #include <gelf.h>
391673e404SJohn Birrell #include <unistd.h>
401673e404SJohn Birrell
411673e404SJohn Birrell #include "ctftools.h"
421673e404SJohn Birrell #include "list.h"
431673e404SJohn Birrell #include "memory.h"
441673e404SJohn Birrell #include "traverse.h"
451673e404SJohn Birrell #include "symbol.h"
461673e404SJohn Birrell
471673e404SJohn Birrell typedef struct iidesc_match {
481673e404SJohn Birrell int iim_fuzzy;
491673e404SJohn Birrell iidesc_t *iim_ret;
501673e404SJohn Birrell char *iim_name;
511673e404SJohn Birrell char *iim_file;
521673e404SJohn Birrell uchar_t iim_bind;
531673e404SJohn Birrell } iidesc_match_t;
541673e404SJohn Birrell
551673e404SJohn Birrell static int
burst_iitypes(void * data,void * arg)561673e404SJohn Birrell burst_iitypes(void *data, void *arg)
571673e404SJohn Birrell {
581673e404SJohn Birrell iidesc_t *ii = data;
591673e404SJohn Birrell iiburst_t *iiburst = arg;
601673e404SJohn Birrell
611673e404SJohn Birrell switch (ii->ii_type) {
621673e404SJohn Birrell case II_GFUN:
631673e404SJohn Birrell case II_SFUN:
641673e404SJohn Birrell case II_GVAR:
651673e404SJohn Birrell case II_SVAR:
661673e404SJohn Birrell if (!(ii->ii_flags & IIDESC_F_USED))
671673e404SJohn Birrell return (0);
681673e404SJohn Birrell break;
691673e404SJohn Birrell default:
701673e404SJohn Birrell break;
711673e404SJohn Birrell }
721673e404SJohn Birrell
731673e404SJohn Birrell ii->ii_dtype->t_flags |= TDESC_F_ISROOT;
741673e404SJohn Birrell (void) iitraverse_td(ii, iiburst->iib_tdtd);
751673e404SJohn Birrell return (1);
761673e404SJohn Birrell }
771673e404SJohn Birrell
781673e404SJohn Birrell /*ARGSUSED1*/
791673e404SJohn Birrell static int
save_type_by_id(tdesc_t * tdp,tdesc_t ** tdpp __unused,void * private)804cc75139SJohn Birrell save_type_by_id(tdesc_t *tdp, tdesc_t **tdpp __unused, void *private)
811673e404SJohn Birrell {
821673e404SJohn Birrell iiburst_t *iiburst = private;
831673e404SJohn Birrell
841673e404SJohn Birrell /*
851673e404SJohn Birrell * Doing this on every node is horribly inefficient, but given that
861673e404SJohn Birrell * we may be suppressing some types, we can't trust nextid in the
871673e404SJohn Birrell * tdata_t.
881673e404SJohn Birrell */
891673e404SJohn Birrell if (tdp->t_id > iiburst->iib_maxtypeid)
901673e404SJohn Birrell iiburst->iib_maxtypeid = tdp->t_id;
911673e404SJohn Birrell
921673e404SJohn Birrell slist_add(&iiburst->iib_types, tdp, tdesc_idcmp);
931673e404SJohn Birrell
941673e404SJohn Birrell return (1);
951673e404SJohn Birrell }
961673e404SJohn Birrell
971673e404SJohn Birrell static tdtrav_cb_f burst_types_cbs[] = {
981673e404SJohn Birrell NULL,
991673e404SJohn Birrell save_type_by_id, /* intrinsic */
1001673e404SJohn Birrell save_type_by_id, /* pointer */
1011673e404SJohn Birrell save_type_by_id, /* array */
1021673e404SJohn Birrell save_type_by_id, /* function */
1031673e404SJohn Birrell save_type_by_id, /* struct */
1041673e404SJohn Birrell save_type_by_id, /* union */
1051673e404SJohn Birrell save_type_by_id, /* enum */
1061673e404SJohn Birrell save_type_by_id, /* forward */
1071673e404SJohn Birrell save_type_by_id, /* typedef */
1081673e404SJohn Birrell tdtrav_assert, /* typedef_unres */
1091673e404SJohn Birrell save_type_by_id, /* volatile */
1101673e404SJohn Birrell save_type_by_id, /* const */
1111673e404SJohn Birrell save_type_by_id /* restrict */
1121673e404SJohn Birrell };
1131673e404SJohn Birrell
1141673e404SJohn Birrell
1151673e404SJohn Birrell static iiburst_t *
iiburst_new(tdata_t * td,int max)1161673e404SJohn Birrell iiburst_new(tdata_t *td, int max)
1171673e404SJohn Birrell {
1181673e404SJohn Birrell iiburst_t *iiburst = xcalloc(sizeof (iiburst_t));
1191673e404SJohn Birrell iiburst->iib_td = td;
1201673e404SJohn Birrell iiburst->iib_funcs = xcalloc(sizeof (iidesc_t *) * max);
1211673e404SJohn Birrell iiburst->iib_nfuncs = 0;
1221673e404SJohn Birrell iiburst->iib_objts = xcalloc(sizeof (iidesc_t *) * max);
1231673e404SJohn Birrell iiburst->iib_nobjts = 0;
1241673e404SJohn Birrell return (iiburst);
1251673e404SJohn Birrell }
1261673e404SJohn Birrell
1271673e404SJohn Birrell static void
iiburst_types(iiburst_t * iiburst)1281673e404SJohn Birrell iiburst_types(iiburst_t *iiburst)
1291673e404SJohn Birrell {
1301673e404SJohn Birrell tdtrav_data_t tdtd;
1311673e404SJohn Birrell
1321673e404SJohn Birrell tdtrav_init(&tdtd, &iiburst->iib_td->td_curvgen, NULL, burst_types_cbs,
1331673e404SJohn Birrell NULL, (void *)iiburst);
1341673e404SJohn Birrell
1351673e404SJohn Birrell iiburst->iib_tdtd = &tdtd;
1361673e404SJohn Birrell
1371673e404SJohn Birrell (void) hash_iter(iiburst->iib_td->td_iihash, burst_iitypes, iiburst);
1381673e404SJohn Birrell }
1391673e404SJohn Birrell
1401673e404SJohn Birrell static void
iiburst_free(iiburst_t * iiburst)1411673e404SJohn Birrell iiburst_free(iiburst_t *iiburst)
1421673e404SJohn Birrell {
1431673e404SJohn Birrell free(iiburst->iib_funcs);
1441673e404SJohn Birrell free(iiburst->iib_objts);
1451673e404SJohn Birrell list_free(iiburst->iib_types, NULL, NULL);
1461673e404SJohn Birrell free(iiburst);
1471673e404SJohn Birrell }
1481673e404SJohn Birrell
1491673e404SJohn Birrell /*
1501673e404SJohn Birrell * See if this iidesc matches the ELF symbol data we pass in.
1511673e404SJohn Birrell *
1521673e404SJohn Birrell * A fuzzy match is where we have a local symbol matching the name of a
1531673e404SJohn Birrell * global type description. This is common when a mapfile is used for a
1541673e404SJohn Birrell * DSO, but we don't accept it by default.
1551673e404SJohn Birrell *
1561673e404SJohn Birrell * A weak fuzzy match is when a weak symbol was resolved and matched to
1571673e404SJohn Birrell * a global type description.
1581673e404SJohn Birrell */
1591673e404SJohn Birrell static int
matching_iidesc(void * arg1,void * arg2)1604cc75139SJohn Birrell matching_iidesc(void *arg1, void *arg2)
1611673e404SJohn Birrell {
1624cc75139SJohn Birrell iidesc_t *iidesc = arg1;
1634cc75139SJohn Birrell iidesc_match_t *match = arg2;
1641673e404SJohn Birrell if (streq(iidesc->ii_name, match->iim_name) == 0)
1651673e404SJohn Birrell return (0);
1661673e404SJohn Birrell
1671673e404SJohn Birrell switch (iidesc->ii_type) {
1681673e404SJohn Birrell case II_GFUN:
1691673e404SJohn Birrell case II_GVAR:
1701673e404SJohn Birrell if (match->iim_bind == STB_GLOBAL) {
1711673e404SJohn Birrell match->iim_ret = iidesc;
1721673e404SJohn Birrell return (-1);
1731673e404SJohn Birrell } else if (match->iim_fuzzy && match->iim_ret == NULL) {
1741673e404SJohn Birrell match->iim_ret = iidesc;
1751673e404SJohn Birrell /* continue to look for strong match */
1761673e404SJohn Birrell return (0);
1771673e404SJohn Birrell }
1781673e404SJohn Birrell break;
1791673e404SJohn Birrell case II_SFUN:
1801673e404SJohn Birrell case II_SVAR:
1811673e404SJohn Birrell if (match->iim_bind == STB_LOCAL &&
1821673e404SJohn Birrell match->iim_file != NULL &&
1831673e404SJohn Birrell streq(iidesc->ii_owner, match->iim_file)) {
1841673e404SJohn Birrell match->iim_ret = iidesc;
1851673e404SJohn Birrell return (-1);
1861673e404SJohn Birrell }
1871673e404SJohn Birrell break;
1884cc75139SJohn Birrell default:
1894cc75139SJohn Birrell break;
1901673e404SJohn Birrell }
1911673e404SJohn Birrell return (0);
1921673e404SJohn Birrell }
1931673e404SJohn Birrell
1941673e404SJohn Birrell static iidesc_t *
find_iidesc(tdata_t * td,iidesc_match_t * match)1951673e404SJohn Birrell find_iidesc(tdata_t *td, iidesc_match_t *match)
1961673e404SJohn Birrell {
1971673e404SJohn Birrell match->iim_ret = NULL;
1981673e404SJohn Birrell iter_iidescs_by_name(td, match->iim_name,
1994cc75139SJohn Birrell matching_iidesc, match);
2001673e404SJohn Birrell return (match->iim_ret);
2011673e404SJohn Birrell }
2021673e404SJohn Birrell
2031673e404SJohn Birrell /*
2041673e404SJohn Birrell * If we have a weak symbol, attempt to find the strong symbol it will
2051673e404SJohn Birrell * resolve to. Note: the code where this actually happens is in
2061673e404SJohn Birrell * sym_process() in cmd/sgs/libld/common/syms.c
2071673e404SJohn Birrell *
2081673e404SJohn Birrell * Finding the matching symbol is unfortunately not trivial. For a
2091673e404SJohn Birrell * symbol to be a candidate, it must:
2101673e404SJohn Birrell *
2111673e404SJohn Birrell * - have the same type (function, object)
2121673e404SJohn Birrell * - have the same value (address)
2131673e404SJohn Birrell * - have the same size
2141673e404SJohn Birrell * - not be another weak symbol
2151673e404SJohn Birrell * - belong to the same section (checked via section index)
2161673e404SJohn Birrell *
2171673e404SJohn Birrell * If such a candidate is global, then we assume we've found it. The
2181673e404SJohn Birrell * linker generates the symbol table such that the curfile might be
2191673e404SJohn Birrell * incorrect; this is OK for global symbols, since find_iidesc() doesn't
2201673e404SJohn Birrell * need to check for the source file for the symbol.
2211673e404SJohn Birrell *
2221673e404SJohn Birrell * We might have found a strong local symbol, where the curfile is
2231673e404SJohn Birrell * accurate and matches that of the weak symbol. We assume this is a
2241673e404SJohn Birrell * reasonable match.
2251673e404SJohn Birrell *
2261673e404SJohn Birrell * If we've got a local symbol with a non-matching curfile, there are
2271673e404SJohn Birrell * two possibilities. Either this is a completely different symbol, or
2281673e404SJohn Birrell * it's a once-global symbol that was scoped to local via a mapfile. In
2291673e404SJohn Birrell * the latter case, curfile is likely inaccurate since the linker does
2301673e404SJohn Birrell * not preserve the needed curfile in the order of the symbol table (see
2311673e404SJohn Birrell * the comments about locally scoped symbols in libld's update_osym()).
2321673e404SJohn Birrell * As we can't tell this case from the former one, we use this symbol
2331673e404SJohn Birrell * iff no other matching symbol is found.
2341673e404SJohn Birrell *
2351673e404SJohn Birrell * What we really need here is a SUNW section containing weak<->strong
2361673e404SJohn Birrell * mappings that we can consume.
2371673e404SJohn Birrell */
2381673e404SJohn Birrell static int
check_for_weak(GElf_Sym * weak,char const * weakfile,Elf_Data * data,int nent,Elf_Data * strdata,GElf_Sym * retsym,char ** curfilep)2391673e404SJohn Birrell check_for_weak(GElf_Sym *weak, char const *weakfile,
2401673e404SJohn Birrell Elf_Data *data, int nent, Elf_Data *strdata,
2411673e404SJohn Birrell GElf_Sym *retsym, char **curfilep)
2421673e404SJohn Birrell {
2431673e404SJohn Birrell char *curfile = NULL;
2444cc75139SJohn Birrell char *tmpfile1 = NULL;
2451673e404SJohn Birrell GElf_Sym tmpsym;
2461673e404SJohn Birrell int candidate = 0;
2471673e404SJohn Birrell int i;
2484cc75139SJohn Birrell tmpsym.st_info = 0;
2494cc75139SJohn Birrell tmpsym.st_name = 0;
2501673e404SJohn Birrell
2511673e404SJohn Birrell if (GELF_ST_BIND(weak->st_info) != STB_WEAK)
2521673e404SJohn Birrell return (0);
2531673e404SJohn Birrell
2541673e404SJohn Birrell for (i = 0; i < nent; i++) {
2551673e404SJohn Birrell GElf_Sym sym;
2561673e404SJohn Birrell uchar_t type;
2571673e404SJohn Birrell
2581673e404SJohn Birrell if (gelf_getsym(data, i, &sym) == NULL)
2591673e404SJohn Birrell continue;
2601673e404SJohn Birrell
2611673e404SJohn Birrell type = GELF_ST_TYPE(sym.st_info);
2621673e404SJohn Birrell
2631673e404SJohn Birrell if (type == STT_FILE)
2641673e404SJohn Birrell curfile = (char *)strdata->d_buf + sym.st_name;
2651673e404SJohn Birrell
2661673e404SJohn Birrell if (GELF_ST_TYPE(weak->st_info) != type ||
2671673e404SJohn Birrell weak->st_value != sym.st_value)
2681673e404SJohn Birrell continue;
2691673e404SJohn Birrell
2701673e404SJohn Birrell if (weak->st_size != sym.st_size)
2711673e404SJohn Birrell continue;
2721673e404SJohn Birrell
2731673e404SJohn Birrell if (GELF_ST_BIND(sym.st_info) == STB_WEAK)
2741673e404SJohn Birrell continue;
2751673e404SJohn Birrell
2761673e404SJohn Birrell if (sym.st_shndx != weak->st_shndx)
2771673e404SJohn Birrell continue;
2781673e404SJohn Birrell
2791673e404SJohn Birrell if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
2801673e404SJohn Birrell (curfile == NULL || weakfile == NULL ||
2811673e404SJohn Birrell strcmp(curfile, weakfile) != 0)) {
2821673e404SJohn Birrell candidate = 1;
2834cc75139SJohn Birrell tmpfile1 = curfile;
2841673e404SJohn Birrell tmpsym = sym;
2851673e404SJohn Birrell continue;
2861673e404SJohn Birrell }
2871673e404SJohn Birrell
2881673e404SJohn Birrell *curfilep = curfile;
2891673e404SJohn Birrell *retsym = sym;
2901673e404SJohn Birrell return (1);
2911673e404SJohn Birrell }
2921673e404SJohn Birrell
2931673e404SJohn Birrell if (candidate) {
2944cc75139SJohn Birrell *curfilep = tmpfile1;
2951673e404SJohn Birrell *retsym = tmpsym;
2961673e404SJohn Birrell return (1);
2971673e404SJohn Birrell }
2981673e404SJohn Birrell
2991673e404SJohn Birrell return (0);
3001673e404SJohn Birrell }
3011673e404SJohn Birrell
3021673e404SJohn Birrell /*
3031673e404SJohn Birrell * When we've found the underlying symbol's type description
3041673e404SJohn Birrell * for a weak symbol, we need to copy it and rename it to match
3051673e404SJohn Birrell * the weak symbol. We also need to add it to the td so it's
3061673e404SJohn Birrell * handled along with the others later.
3071673e404SJohn Birrell */
3081673e404SJohn Birrell static iidesc_t *
copy_from_strong(tdata_t * td,GElf_Sym * sym,iidesc_t * strongdesc,const char * weakname,const char * weakfile)3091673e404SJohn Birrell copy_from_strong(tdata_t *td, GElf_Sym *sym, iidesc_t *strongdesc,
3101673e404SJohn Birrell const char *weakname, const char *weakfile)
3111673e404SJohn Birrell {
3121673e404SJohn Birrell iidesc_t *new = iidesc_dup_rename(strongdesc, weakname, weakfile);
3131673e404SJohn Birrell uchar_t type = GELF_ST_TYPE(sym->st_info);
3141673e404SJohn Birrell
3151673e404SJohn Birrell switch (type) {
3161673e404SJohn Birrell case STT_OBJECT:
3171673e404SJohn Birrell new->ii_type = II_GVAR;
3181673e404SJohn Birrell break;
3191673e404SJohn Birrell case STT_FUNC:
3201673e404SJohn Birrell new->ii_type = II_GFUN;
3211673e404SJohn Birrell break;
3221673e404SJohn Birrell }
3231673e404SJohn Birrell
3241673e404SJohn Birrell hash_add(td->td_iihash, new);
3251673e404SJohn Birrell
3261673e404SJohn Birrell return (new);
3271673e404SJohn Birrell }
3281673e404SJohn Birrell
3291673e404SJohn Birrell /*
3301673e404SJohn Birrell * Process the symbol table of the output file, associating each symbol
3311673e404SJohn Birrell * with a type description if possible, and sorting them into functions
3321673e404SJohn Birrell * and data, maintaining symbol table order.
3331673e404SJohn Birrell */
3341673e404SJohn Birrell static iiburst_t *
sort_iidescs(Elf * elf,const char * file,tdata_t * td,int fuzzymatch,int dynsym)3351673e404SJohn Birrell sort_iidescs(Elf *elf, const char *file, tdata_t *td, int fuzzymatch,
3361673e404SJohn Birrell int dynsym)
3371673e404SJohn Birrell {
3381673e404SJohn Birrell iiburst_t *iiburst;
3391673e404SJohn Birrell Elf_Scn *scn;
3401673e404SJohn Birrell GElf_Shdr shdr;
3411673e404SJohn Birrell Elf_Data *data, *strdata;
3421673e404SJohn Birrell int i, stidx;
3431673e404SJohn Birrell int nent;
3441673e404SJohn Birrell iidesc_match_t match;
3451673e404SJohn Birrell
3461673e404SJohn Birrell match.iim_fuzzy = fuzzymatch;
3471673e404SJohn Birrell match.iim_file = NULL;
3481673e404SJohn Birrell
3491673e404SJohn Birrell if ((stidx = findelfsecidx(elf, file,
3501673e404SJohn Birrell dynsym ? ".dynsym" : ".symtab")) < 0)
3511673e404SJohn Birrell terminate("%s: Can't open symbol table\n", file);
3521673e404SJohn Birrell scn = elf_getscn(elf, stidx);
3531673e404SJohn Birrell data = elf_getdata(scn, NULL);
3541673e404SJohn Birrell gelf_getshdr(scn, &shdr);
3551673e404SJohn Birrell nent = shdr.sh_size / shdr.sh_entsize;
3561673e404SJohn Birrell
3571673e404SJohn Birrell scn = elf_getscn(elf, shdr.sh_link);
3581673e404SJohn Birrell strdata = elf_getdata(scn, NULL);
3591673e404SJohn Birrell
3601673e404SJohn Birrell iiburst = iiburst_new(td, nent);
3611673e404SJohn Birrell
3621673e404SJohn Birrell for (i = 0; i < nent; i++) {
3631673e404SJohn Birrell GElf_Sym sym;
3648b0d8e9fSDimitry Andric char *bname;
3651673e404SJohn Birrell iidesc_t **tolist;
3661673e404SJohn Birrell GElf_Sym ssym;
3671673e404SJohn Birrell iidesc_match_t smatch;
3681673e404SJohn Birrell int *curr;
3691673e404SJohn Birrell iidesc_t *iidesc;
3701673e404SJohn Birrell
3711673e404SJohn Birrell if (gelf_getsym(data, i, &sym) == NULL)
3721673e404SJohn Birrell elfterminate(file, "Couldn't read symbol %d", i);
3731673e404SJohn Birrell
3741673e404SJohn Birrell match.iim_name = (char *)strdata->d_buf + sym.st_name;
3751673e404SJohn Birrell match.iim_bind = GELF_ST_BIND(sym.st_info);
3761673e404SJohn Birrell
3771673e404SJohn Birrell switch (GELF_ST_TYPE(sym.st_info)) {
3781673e404SJohn Birrell case STT_FILE:
3798b0d8e9fSDimitry Andric bname = strrchr(match.iim_name, '/');
380e86b103cSDimitry Andric match.iim_file = bname == NULL ? match.iim_name : bname + 1;
3811673e404SJohn Birrell continue;
3821673e404SJohn Birrell case STT_OBJECT:
3831673e404SJohn Birrell tolist = iiburst->iib_objts;
3841673e404SJohn Birrell curr = &iiburst->iib_nobjts;
3851673e404SJohn Birrell break;
3861673e404SJohn Birrell case STT_FUNC:
3871673e404SJohn Birrell tolist = iiburst->iib_funcs;
3881673e404SJohn Birrell curr = &iiburst->iib_nfuncs;
3891673e404SJohn Birrell break;
3901673e404SJohn Birrell default:
3911673e404SJohn Birrell continue;
3921673e404SJohn Birrell }
3931673e404SJohn Birrell
3941673e404SJohn Birrell if (ignore_symbol(&sym, match.iim_name))
3951673e404SJohn Birrell continue;
3961673e404SJohn Birrell
3971673e404SJohn Birrell iidesc = find_iidesc(td, &match);
3981673e404SJohn Birrell
3991673e404SJohn Birrell if (iidesc != NULL) {
4001673e404SJohn Birrell tolist[*curr] = iidesc;
4011673e404SJohn Birrell iidesc->ii_flags |= IIDESC_F_USED;
4021673e404SJohn Birrell (*curr)++;
4031673e404SJohn Birrell continue;
4041673e404SJohn Birrell }
4051673e404SJohn Birrell
4061673e404SJohn Birrell if (!check_for_weak(&sym, match.iim_file, data, nent, strdata,
4071673e404SJohn Birrell &ssym, &smatch.iim_file)) {
4081673e404SJohn Birrell (*curr)++;
4091673e404SJohn Birrell continue;
4101673e404SJohn Birrell }
4111673e404SJohn Birrell
4121673e404SJohn Birrell smatch.iim_fuzzy = fuzzymatch;
4131673e404SJohn Birrell smatch.iim_name = (char *)strdata->d_buf + ssym.st_name;
4141673e404SJohn Birrell smatch.iim_bind = GELF_ST_BIND(ssym.st_info);
4151673e404SJohn Birrell
4161673e404SJohn Birrell debug(3, "Weak symbol %s resolved to %s\n", match.iim_name,
4171673e404SJohn Birrell smatch.iim_name);
4181673e404SJohn Birrell
4191673e404SJohn Birrell iidesc = find_iidesc(td, &smatch);
4201673e404SJohn Birrell
4211673e404SJohn Birrell if (iidesc != NULL) {
4221673e404SJohn Birrell tolist[*curr] = copy_from_strong(td, &sym,
4231673e404SJohn Birrell iidesc, match.iim_name, match.iim_file);
4241673e404SJohn Birrell tolist[*curr]->ii_flags |= IIDESC_F_USED;
4251673e404SJohn Birrell }
4261673e404SJohn Birrell
4271673e404SJohn Birrell (*curr)++;
4281673e404SJohn Birrell }
4291673e404SJohn Birrell
4301673e404SJohn Birrell /*
4311673e404SJohn Birrell * Stabs are generated for every function declared in a given C source
4321673e404SJohn Birrell * file. When converting an object file, we may encounter a stab that
4331673e404SJohn Birrell * has no symbol table entry because the optimizer has decided to omit
4341673e404SJohn Birrell * that item (for example, an unreferenced static function). We may
4351673e404SJohn Birrell * see iidescs that do not have an associated symtab entry, and so
4361673e404SJohn Birrell * we do not write records for those functions into the CTF data.
4371673e404SJohn Birrell * All others get marked as a root by this function.
4381673e404SJohn Birrell */
4391673e404SJohn Birrell iiburst_types(iiburst);
4401673e404SJohn Birrell
4411673e404SJohn Birrell /*
4421673e404SJohn Birrell * By not adding some of the functions and/or objects, we may have
4431673e404SJohn Birrell * caused some types that were referenced solely by those
4441673e404SJohn Birrell * functions/objects to be suppressed. This could cause a label,
4451673e404SJohn Birrell * generated prior to the evisceration, to be incorrect. Find the
4461673e404SJohn Birrell * highest type index, and change the label indicies to be no higher
4471673e404SJohn Birrell * than this value.
4481673e404SJohn Birrell */
4491673e404SJohn Birrell tdata_label_newmax(td, iiburst->iib_maxtypeid);
4501673e404SJohn Birrell
4511673e404SJohn Birrell return (iiburst);
4521673e404SJohn Birrell }
4531673e404SJohn Birrell
4541673e404SJohn Birrell static void
write_file(Elf * src,const char * srcname,Elf * dst,const char * dstname,caddr_t ctfdata,size_t ctfsize,int flags)4551673e404SJohn Birrell write_file(Elf *src, const char *srcname, Elf *dst, const char *dstname,
4561673e404SJohn Birrell caddr_t ctfdata, size_t ctfsize, int flags)
4571673e404SJohn Birrell {
4581673e404SJohn Birrell GElf_Ehdr sehdr, dehdr;
4591673e404SJohn Birrell Elf_Scn *sscn, *dscn;
4601673e404SJohn Birrell Elf_Data *sdata, *ddata;
4611673e404SJohn Birrell GElf_Shdr shdr;
4621673e404SJohn Birrell GElf_Word symtab_type;
4631673e404SJohn Birrell int symtab_idx = -1;
4641673e404SJohn Birrell off_t new_offset = 0;
4651673e404SJohn Birrell off_t ctfnameoff = 0;
4661673e404SJohn Birrell int dynsym = (flags & CTF_USE_DYNSYM);
4671673e404SJohn Birrell int keep_stabs = (flags & CTF_KEEP_STABS);
4681673e404SJohn Birrell int *secxlate;
4691673e404SJohn Birrell int srcidx, dstidx;
4701673e404SJohn Birrell int changing = 0;
4711673e404SJohn Birrell int pad;
4721673e404SJohn Birrell int i;
4731673e404SJohn Birrell
4741673e404SJohn Birrell if (gelf_newehdr(dst, gelf_getclass(src)) == NULL)
4751673e404SJohn Birrell elfterminate(dstname, "Cannot copy ehdr to temp file");
4761673e404SJohn Birrell gelf_getehdr(src, &sehdr);
4771673e404SJohn Birrell memcpy(&dehdr, &sehdr, sizeof (GElf_Ehdr));
4781673e404SJohn Birrell gelf_update_ehdr(dst, &dehdr);
4791673e404SJohn Birrell
4801673e404SJohn Birrell symtab_type = dynsym ? SHT_DYNSYM : SHT_SYMTAB;
4811673e404SJohn Birrell
4821673e404SJohn Birrell /*
4831673e404SJohn Birrell * Neither the existing stab sections nor the SUNW_ctf sections (new or
4841673e404SJohn Birrell * existing) are SHF_ALLOC'd, so they won't be in areas referenced by
4851673e404SJohn Birrell * program headers. As such, we can just blindly copy the program
4861673e404SJohn Birrell * headers from the existing file to the new file.
4871673e404SJohn Birrell */
4881673e404SJohn Birrell if (sehdr.e_phnum != 0) {
4891673e404SJohn Birrell (void) elf_flagelf(dst, ELF_C_SET, ELF_F_LAYOUT);
4901673e404SJohn Birrell if (gelf_newphdr(dst, sehdr.e_phnum) == NULL)
4911673e404SJohn Birrell elfterminate(dstname, "Cannot make phdrs in temp file");
4921673e404SJohn Birrell
4931673e404SJohn Birrell for (i = 0; i < sehdr.e_phnum; i++) {
4941673e404SJohn Birrell GElf_Phdr phdr;
4951673e404SJohn Birrell
4961673e404SJohn Birrell gelf_getphdr(src, i, &phdr);
4971673e404SJohn Birrell gelf_update_phdr(dst, i, &phdr);
4981673e404SJohn Birrell }
4991673e404SJohn Birrell }
5001673e404SJohn Birrell
5011673e404SJohn Birrell secxlate = xmalloc(sizeof (int) * sehdr.e_shnum);
5021673e404SJohn Birrell for (srcidx = dstidx = 0; srcidx < sehdr.e_shnum; srcidx++) {
5031673e404SJohn Birrell Elf_Scn *scn = elf_getscn(src, srcidx);
5044cc75139SJohn Birrell GElf_Shdr shdr1;
5051673e404SJohn Birrell char *sname;
5061673e404SJohn Birrell
5074cc75139SJohn Birrell gelf_getshdr(scn, &shdr1);
5084cc75139SJohn Birrell sname = elf_strptr(src, sehdr.e_shstrndx, shdr1.sh_name);
5091673e404SJohn Birrell if (sname == NULL) {
5101673e404SJohn Birrell elfterminate(srcname, "Can't find string at %u",
5114cc75139SJohn Birrell shdr1.sh_name);
5121673e404SJohn Birrell }
5131673e404SJohn Birrell
5141673e404SJohn Birrell if (strcmp(sname, CTF_ELF_SCN_NAME) == 0) {
5151673e404SJohn Birrell secxlate[srcidx] = -1;
5161673e404SJohn Birrell } else if (!keep_stabs &&
5171673e404SJohn Birrell (strncmp(sname, ".stab", 5) == 0 ||
5181673e404SJohn Birrell strncmp(sname, ".debug", 6) == 0 ||
5191673e404SJohn Birrell strncmp(sname, ".rel.debug", 10) == 0 ||
5201673e404SJohn Birrell strncmp(sname, ".rela.debug", 11) == 0)) {
5211673e404SJohn Birrell secxlate[srcidx] = -1;
5224cc75139SJohn Birrell } else if (dynsym && shdr1.sh_type == SHT_SYMTAB) {
5231673e404SJohn Birrell /*
5241673e404SJohn Birrell * If we're building CTF against the dynsym,
5251673e404SJohn Birrell * we'll rip out the symtab so debuggers aren't
5261673e404SJohn Birrell * confused.
5271673e404SJohn Birrell */
5281673e404SJohn Birrell secxlate[srcidx] = -1;
5291673e404SJohn Birrell } else {
5301673e404SJohn Birrell secxlate[srcidx] = dstidx++;
5311673e404SJohn Birrell }
5321673e404SJohn Birrell
5331673e404SJohn Birrell new_offset = (off_t)dehdr.e_phoff;
5341673e404SJohn Birrell }
5351673e404SJohn Birrell
5361673e404SJohn Birrell for (srcidx = 1; srcidx < sehdr.e_shnum; srcidx++) {
5371673e404SJohn Birrell char *sname;
5381673e404SJohn Birrell
5391673e404SJohn Birrell sscn = elf_getscn(src, srcidx);
5401673e404SJohn Birrell gelf_getshdr(sscn, &shdr);
5411673e404SJohn Birrell
5421673e404SJohn Birrell if (secxlate[srcidx] == -1) {
5431673e404SJohn Birrell changing = 1;
5441673e404SJohn Birrell continue;
5451673e404SJohn Birrell }
5461673e404SJohn Birrell
5471673e404SJohn Birrell dscn = elf_newscn(dst);
5481673e404SJohn Birrell
5491673e404SJohn Birrell /*
5501673e404SJohn Birrell * If this file has program headers, we need to explicitly lay
5511673e404SJohn Birrell * out sections. If none of the sections prior to this one have
5521673e404SJohn Birrell * been removed, then we can just use the existing location. If
5531673e404SJohn Birrell * one or more sections have been changed, then we need to
5541673e404SJohn Birrell * adjust this one to avoid holes.
5551673e404SJohn Birrell */
5561673e404SJohn Birrell if (changing && sehdr.e_phnum != 0) {
5571673e404SJohn Birrell pad = new_offset % shdr.sh_addralign;
5581673e404SJohn Birrell
5591673e404SJohn Birrell if (pad)
5601673e404SJohn Birrell new_offset += shdr.sh_addralign - pad;
5611673e404SJohn Birrell shdr.sh_offset = new_offset;
5621673e404SJohn Birrell }
5631673e404SJohn Birrell
5641673e404SJohn Birrell shdr.sh_link = secxlate[shdr.sh_link];
5651673e404SJohn Birrell
5661673e404SJohn Birrell if (shdr.sh_type == SHT_REL || shdr.sh_type == SHT_RELA)
5671673e404SJohn Birrell shdr.sh_info = secxlate[shdr.sh_info];
5681673e404SJohn Birrell
5691673e404SJohn Birrell sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name);
5701673e404SJohn Birrell if (sname == NULL) {
5711673e404SJohn Birrell elfterminate(srcname, "Can't find string at %u",
5721673e404SJohn Birrell shdr.sh_name);
5731673e404SJohn Birrell }
5744cc75139SJohn Birrell
575*bc96366cSSteven Hartland #ifndef illumos
5764cc75139SJohn Birrell if (gelf_update_shdr(dscn, &shdr) == 0)
5774cc75139SJohn Birrell elfterminate(dstname, "Cannot update sect %s", sname);
5784cc75139SJohn Birrell #endif
5794cc75139SJohn Birrell
5801673e404SJohn Birrell if ((sdata = elf_getdata(sscn, NULL)) == NULL)
5811673e404SJohn Birrell elfterminate(srcname, "Cannot get sect %s data", sname);
5821673e404SJohn Birrell if ((ddata = elf_newdata(dscn)) == NULL)
5831673e404SJohn Birrell elfterminate(dstname, "Can't make sect %s data", sname);
584*bc96366cSSteven Hartland #ifdef illumos
5851673e404SJohn Birrell bcopy(sdata, ddata, sizeof (Elf_Data));
5864cc75139SJohn Birrell #else
5874cc75139SJohn Birrell /*
5884cc75139SJohn Birrell * FreeBSD's Elf_Data has private fields which the
5894cc75139SJohn Birrell * elf_* routines manage. Simply copying the
5904cc75139SJohn Birrell * entire structure corrupts the data. So we need
5914cc75139SJohn Birrell * to copy the public fields explictly.
5924cc75139SJohn Birrell */
5934cc75139SJohn Birrell ddata->d_align = sdata->d_align;
5944cc75139SJohn Birrell ddata->d_off = sdata->d_off;
5954cc75139SJohn Birrell ddata->d_size = sdata->d_size;
5964cc75139SJohn Birrell ddata->d_type = sdata->d_type;
5974cc75139SJohn Birrell ddata->d_version = sdata->d_version;
5984cc75139SJohn Birrell #endif
5991673e404SJohn Birrell
6001673e404SJohn Birrell if (srcidx == sehdr.e_shstrndx) {
6011673e404SJohn Birrell char seclen = strlen(CTF_ELF_SCN_NAME);
6021673e404SJohn Birrell
6031673e404SJohn Birrell ddata->d_buf = xmalloc(ddata->d_size + shdr.sh_size +
6041673e404SJohn Birrell seclen + 1);
6051673e404SJohn Birrell bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size);
6061673e404SJohn Birrell strcpy((caddr_t)ddata->d_buf + shdr.sh_size,
6071673e404SJohn Birrell CTF_ELF_SCN_NAME);
6081673e404SJohn Birrell ctfnameoff = (off_t)shdr.sh_size;
6091673e404SJohn Birrell shdr.sh_size += seclen + 1;
6101673e404SJohn Birrell ddata->d_size += seclen + 1;
6111673e404SJohn Birrell
6121673e404SJohn Birrell if (sehdr.e_phnum != 0)
6131673e404SJohn Birrell changing = 1;
6141673e404SJohn Birrell }
6151673e404SJohn Birrell
6161673e404SJohn Birrell if (shdr.sh_type == symtab_type && shdr.sh_entsize != 0) {
6171673e404SJohn Birrell int nsym = shdr.sh_size / shdr.sh_entsize;
6181673e404SJohn Birrell
6191673e404SJohn Birrell symtab_idx = secxlate[srcidx];
6201673e404SJohn Birrell
6211673e404SJohn Birrell ddata->d_buf = xmalloc(shdr.sh_size);
6221673e404SJohn Birrell bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size);
6231673e404SJohn Birrell
6241673e404SJohn Birrell for (i = 0; i < nsym; i++) {
6251673e404SJohn Birrell GElf_Sym sym;
6261673e404SJohn Birrell short newscn;
6271673e404SJohn Birrell
6284cc75139SJohn Birrell if (gelf_getsym(ddata, i, &sym) == NULL)
6294cc75139SJohn Birrell printf("Could not get symbol %d\n",i);
6301673e404SJohn Birrell
6311673e404SJohn Birrell if (sym.st_shndx >= SHN_LORESERVE)
6321673e404SJohn Birrell continue;
6331673e404SJohn Birrell
6341673e404SJohn Birrell if ((newscn = secxlate[sym.st_shndx]) !=
6351673e404SJohn Birrell sym.st_shndx) {
6361673e404SJohn Birrell sym.st_shndx =
6371673e404SJohn Birrell (newscn == -1 ? 1 : newscn);
6381673e404SJohn Birrell
6391673e404SJohn Birrell gelf_update_sym(ddata, i, &sym);
6401673e404SJohn Birrell }
6411673e404SJohn Birrell }
6421673e404SJohn Birrell }
6431673e404SJohn Birrell
644*bc96366cSSteven Hartland #ifndef illumos
64596d1cd18SNavdeep Parhar if (ddata->d_buf == NULL && sdata->d_buf != NULL) {
6464cc75139SJohn Birrell ddata->d_buf = xmalloc(shdr.sh_size);
6474cc75139SJohn Birrell bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size);
6484cc75139SJohn Birrell }
6494cc75139SJohn Birrell #endif
6504cc75139SJohn Birrell
6514cc75139SJohn Birrell if (gelf_update_shdr(dscn, &shdr) == 0)
6521673e404SJohn Birrell elfterminate(dstname, "Cannot update sect %s", sname);
6531673e404SJohn Birrell
6541673e404SJohn Birrell new_offset = (off_t)shdr.sh_offset;
6551673e404SJohn Birrell if (shdr.sh_type != SHT_NOBITS)
6561673e404SJohn Birrell new_offset += shdr.sh_size;
6571673e404SJohn Birrell }
6581673e404SJohn Birrell
6591673e404SJohn Birrell if (symtab_idx == -1) {
6601673e404SJohn Birrell terminate("%s: Cannot find %s section\n", srcname,
6611673e404SJohn Birrell dynsym ? "SHT_DYNSYM" : "SHT_SYMTAB");
6621673e404SJohn Birrell }
6631673e404SJohn Birrell
6641673e404SJohn Birrell /* Add the ctf section */
6651673e404SJohn Birrell dscn = elf_newscn(dst);
6661673e404SJohn Birrell gelf_getshdr(dscn, &shdr);
6671673e404SJohn Birrell shdr.sh_name = ctfnameoff;
6681673e404SJohn Birrell shdr.sh_type = SHT_PROGBITS;
6691673e404SJohn Birrell shdr.sh_size = ctfsize;
6701673e404SJohn Birrell shdr.sh_link = symtab_idx;
6711673e404SJohn Birrell shdr.sh_addralign = 4;
6721673e404SJohn Birrell if (changing && sehdr.e_phnum != 0) {
6731673e404SJohn Birrell pad = new_offset % shdr.sh_addralign;
6741673e404SJohn Birrell
6751673e404SJohn Birrell if (pad)
6761673e404SJohn Birrell new_offset += shdr.sh_addralign - pad;
6771673e404SJohn Birrell
6781673e404SJohn Birrell shdr.sh_offset = new_offset;
6791673e404SJohn Birrell new_offset += shdr.sh_size;
6801673e404SJohn Birrell }
6811673e404SJohn Birrell
6821673e404SJohn Birrell ddata = elf_newdata(dscn);
6831673e404SJohn Birrell ddata->d_buf = ctfdata;
6841673e404SJohn Birrell ddata->d_size = ctfsize;
6851673e404SJohn Birrell ddata->d_align = shdr.sh_addralign;
6864cc75139SJohn Birrell ddata->d_off = 0;
6871673e404SJohn Birrell
6881673e404SJohn Birrell gelf_update_shdr(dscn, &shdr);
6891673e404SJohn Birrell
6901673e404SJohn Birrell /* update the section header location */
6911673e404SJohn Birrell if (sehdr.e_phnum != 0) {
6921673e404SJohn Birrell size_t align = gelf_fsize(dst, ELF_T_ADDR, 1, EV_CURRENT);
6931673e404SJohn Birrell size_t r = new_offset % align;
6941673e404SJohn Birrell
6951673e404SJohn Birrell if (r)
6961673e404SJohn Birrell new_offset += align - r;
6971673e404SJohn Birrell
6981673e404SJohn Birrell dehdr.e_shoff = new_offset;
6991673e404SJohn Birrell }
7001673e404SJohn Birrell
7011673e404SJohn Birrell /* commit to disk */
7021673e404SJohn Birrell dehdr.e_shstrndx = secxlate[sehdr.e_shstrndx];
7031673e404SJohn Birrell gelf_update_ehdr(dst, &dehdr);
7041673e404SJohn Birrell if (elf_update(dst, ELF_C_WRITE) < 0)
7051673e404SJohn Birrell elfterminate(dstname, "Cannot finalize temp file");
7061673e404SJohn Birrell
7071673e404SJohn Birrell free(secxlate);
7081673e404SJohn Birrell }
7091673e404SJohn Birrell
7101673e404SJohn Birrell static caddr_t
make_ctf_data(tdata_t * td,Elf * elf,const char * file,size_t * lenp,int flags)7111673e404SJohn Birrell make_ctf_data(tdata_t *td, Elf *elf, const char *file, size_t *lenp, int flags)
7121673e404SJohn Birrell {
7131673e404SJohn Birrell iiburst_t *iiburst;
7141673e404SJohn Birrell caddr_t data;
7151673e404SJohn Birrell
7161673e404SJohn Birrell iiburst = sort_iidescs(elf, file, td, flags & CTF_FUZZY_MATCH,
7171673e404SJohn Birrell flags & CTF_USE_DYNSYM);
718a6425ab5SOleksandr Tymoshenko data = ctf_gen(iiburst, lenp, flags & (CTF_COMPRESS | CTF_SWAP_BYTES));
7191673e404SJohn Birrell
7201673e404SJohn Birrell iiburst_free(iiburst);
7211673e404SJohn Birrell
7221673e404SJohn Birrell return (data);
7231673e404SJohn Birrell }
7241673e404SJohn Birrell
7251673e404SJohn Birrell void
write_ctf(tdata_t * td,const char * curname,const char * newname,int flags)7261673e404SJohn Birrell write_ctf(tdata_t *td, const char *curname, const char *newname, int flags)
7271673e404SJohn Birrell {
7281673e404SJohn Birrell struct stat st;
7291673e404SJohn Birrell Elf *elf = NULL;
7301673e404SJohn Birrell Elf *telf = NULL;
731a6425ab5SOleksandr Tymoshenko GElf_Ehdr ehdr;
7321673e404SJohn Birrell caddr_t data;
7331673e404SJohn Birrell size_t len;
7341673e404SJohn Birrell int fd = -1;
7351673e404SJohn Birrell int tfd = -1;
736a6425ab5SOleksandr Tymoshenko int byteorder;
7371673e404SJohn Birrell
7381673e404SJohn Birrell (void) elf_version(EV_CURRENT);
7391673e404SJohn Birrell if ((fd = open(curname, O_RDONLY)) < 0 || fstat(fd, &st) < 0)
7401673e404SJohn Birrell terminate("%s: Cannot open for re-reading", curname);
7411673e404SJohn Birrell if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
7421673e404SJohn Birrell elfterminate(curname, "Cannot re-read");
7431673e404SJohn Birrell
7441673e404SJohn Birrell if ((tfd = open(newname, O_RDWR | O_CREAT | O_TRUNC, st.st_mode)) < 0)
7451673e404SJohn Birrell terminate("Cannot open temp file %s for writing", newname);
7461673e404SJohn Birrell if ((telf = elf_begin(tfd, ELF_C_WRITE, NULL)) == NULL)
7471673e404SJohn Birrell elfterminate(curname, "Cannot write");
7481673e404SJohn Birrell
749a6425ab5SOleksandr Tymoshenko if (gelf_getehdr(elf, &ehdr)) {
750a6425ab5SOleksandr Tymoshenko #if BYTE_ORDER == _BIG_ENDIAN
751a6425ab5SOleksandr Tymoshenko byteorder = ELFDATA2MSB;
752a6425ab5SOleksandr Tymoshenko #else
753a6425ab5SOleksandr Tymoshenko byteorder = ELFDATA2LSB;
754a6425ab5SOleksandr Tymoshenko #endif
755a6425ab5SOleksandr Tymoshenko /*
756a6425ab5SOleksandr Tymoshenko * If target and host has the same byte order
757a6425ab5SOleksandr Tymoshenko * clear byte swapping request
758a6425ab5SOleksandr Tymoshenko */
759a6425ab5SOleksandr Tymoshenko if (ehdr.e_ident[EI_DATA] == byteorder)
760a6425ab5SOleksandr Tymoshenko flags &= ~CTF_SWAP_BYTES;
761a6425ab5SOleksandr Tymoshenko }
762a6425ab5SOleksandr Tymoshenko else
763a6425ab5SOleksandr Tymoshenko elfterminate(curname, "Failed to get EHDR");
764a6425ab5SOleksandr Tymoshenko
7651673e404SJohn Birrell data = make_ctf_data(td, elf, curname, &len, flags);
7661673e404SJohn Birrell write_file(elf, curname, telf, newname, data, len, flags);
7671673e404SJohn Birrell free(data);
7681673e404SJohn Birrell
7691673e404SJohn Birrell elf_end(telf);
7701673e404SJohn Birrell elf_end(elf);
7711673e404SJohn Birrell (void) close(fd);
7721673e404SJohn Birrell (void) close(tfd);
7731673e404SJohn Birrell }
774