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