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