xref: /titanic_50/usr/src/tools/ctf/cvt/output.c (revision 210938ebfcf386cf4819f16c23979173b0d78097)
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, file,
348 	    dynsym ? ".dynsym" : ".symtab")) < 0)
349 		terminate("%s: Can't open symbol table\n", file);
350 	scn = elf_getscn(elf, stidx);
351 	data = elf_getdata(scn, NULL);
352 	gelf_getshdr(scn, &shdr);
353 	nent = shdr.sh_size / shdr.sh_entsize;
354 
355 	scn = elf_getscn(elf, shdr.sh_link);
356 	strdata = elf_getdata(scn, NULL);
357 
358 	iiburst = iiburst_new(td, nent);
359 
360 	for (i = 0; i < nent; i++) {
361 		GElf_Sym sym;
362 		iidesc_t **tolist;
363 		GElf_Sym ssym;
364 		iidesc_match_t smatch;
365 		int *curr;
366 		iidesc_t *iidesc;
367 
368 		if (gelf_getsym(data, i, &sym) == NULL)
369 			elfterminate(file, "Couldn't read symbol %d", i);
370 
371 		match.iim_name = (char *)strdata->d_buf + sym.st_name;
372 		match.iim_bind = GELF_ST_BIND(sym.st_info);
373 
374 		switch (GELF_ST_TYPE(sym.st_info)) {
375 		case STT_FILE:
376 			match.iim_file = match.iim_name;
377 			continue;
378 		case STT_OBJECT:
379 			tolist = iiburst->iib_objts;
380 			curr = &iiburst->iib_nobjts;
381 			break;
382 		case STT_FUNC:
383 			tolist = iiburst->iib_funcs;
384 			curr = &iiburst->iib_nfuncs;
385 			break;
386 		default:
387 			continue;
388 		}
389 
390 		if (ignore_symbol(&sym, match.iim_name))
391 			continue;
392 
393 		iidesc = find_iidesc(td->td_iihash, &match);
394 
395 		if (iidesc != NULL) {
396 			tolist[*curr] = iidesc;
397 			iidesc->ii_flags |= IIDESC_F_USED;
398 			(*curr)++;
399 			continue;
400 		}
401 
402 		if (!check_for_weak(&sym, match.iim_file, data, nent, strdata,
403 		    &ssym, &smatch.iim_file)) {
404 			(*curr)++;
405 			continue;
406 		}
407 
408 		smatch.iim_fuzzy = fuzzymatch;
409 		smatch.iim_name = (char *)strdata->d_buf + ssym.st_name;
410 		smatch.iim_bind = GELF_ST_BIND(ssym.st_info);
411 
412 		debug(3, "Weak symbol %s resolved to %s\n", match.iim_name,
413 		    smatch.iim_name);
414 
415 		iidesc = find_iidesc(td->td_iihash, &smatch);
416 
417 		if (iidesc != NULL) {
418 			tolist[*curr] = copy_from_strong(td, &sym,
419 			    iidesc, match.iim_name, match.iim_file);
420 			tolist[*curr]->ii_flags |= IIDESC_F_USED;
421 		}
422 
423 		(*curr)++;
424 	}
425 
426 	/*
427 	 * Stabs are generated for every function declared in a given C source
428 	 * file.  When converting an object file, we may encounter a stab that
429 	 * has no symbol table entry because the optimizer has decided to omit
430 	 * that item (for example, an unreferenced static function).  We may
431 	 * see iidescs that do not have an associated symtab entry, and so
432 	 * we do not write records for those functions into the CTF data.
433 	 * All others get marked as a root by this function.
434 	 */
435 	iiburst_types(iiburst);
436 
437 	/*
438 	 * By not adding some of the functions and/or objects, we may have
439 	 * caused some types that were referenced solely by those
440 	 * functions/objects to be suppressed.  This could cause a label,
441 	 * generated prior to the evisceration, to be incorrect.  Find the
442 	 * highest type index, and change the label indicies to be no higher
443 	 * than this value.
444 	 */
445 	tdata_label_newmax(td, iiburst->iib_maxtypeid);
446 
447 	return (iiburst);
448 }
449 
450 static void
451 write_file(Elf *src, const char *srcname, Elf *dst, const char *dstname,
452     caddr_t ctfdata, size_t ctfsize, int flags)
453 {
454 	GElf_Ehdr sehdr, dehdr;
455 	Elf_Scn *sscn, *dscn;
456 	Elf_Data *sdata, *ddata;
457 	GElf_Shdr shdr;
458 	GElf_Word symtab_type;
459 	int symtab_idx = -1;
460 	off_t new_offset = 0;
461 	off_t ctfnameoff = 0;
462 	int dynsym = (flags & CTF_USE_DYNSYM);
463 	int keep_stabs = (flags & CTF_KEEP_STABS);
464 	int *secxlate;
465 	int srcidx, dstidx;
466 	int curnmoff = 0;
467 	int changing = 0;
468 	int pad;
469 	int i;
470 
471 	if (gelf_newehdr(dst, gelf_getclass(src)) == NULL)
472 		elfterminate(dstname, "Cannot copy ehdr to temp file");
473 	gelf_getehdr(src, &sehdr);
474 	memcpy(&dehdr, &sehdr, sizeof (GElf_Ehdr));
475 	gelf_update_ehdr(dst, &dehdr);
476 
477 	symtab_type = dynsym ? SHT_DYNSYM : SHT_SYMTAB;
478 
479 	/*
480 	 * Neither the existing stab sections nor the SUNW_ctf sections (new or
481 	 * existing) are SHF_ALLOC'd, so they won't be in areas referenced by
482 	 * program headers.  As such, we can just blindly copy the program
483 	 * headers from the existing file to the new file.
484 	 */
485 	if (sehdr.e_phnum != 0) {
486 		(void) elf_flagelf(dst, ELF_C_SET, ELF_F_LAYOUT);
487 		if (gelf_newphdr(dst, sehdr.e_phnum) == NULL)
488 			elfterminate(dstname, "Cannot make phdrs in temp file");
489 
490 		for (i = 0; i < sehdr.e_phnum; i++) {
491 			GElf_Phdr phdr;
492 
493 			gelf_getphdr(src, i, &phdr);
494 			gelf_update_phdr(dst, i, &phdr);
495 		}
496 	}
497 
498 	secxlate = xmalloc(sizeof (int) * sehdr.e_shnum);
499 	for (srcidx = dstidx = 0; srcidx < sehdr.e_shnum; srcidx++) {
500 		Elf_Scn *scn = elf_getscn(src, srcidx);
501 		GElf_Shdr shdr;
502 		char *sname;
503 
504 		gelf_getshdr(scn, &shdr);
505 		sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name);
506 		if (sname == NULL) {
507 			elfterminate(srcname, "Can't find string at %u",
508 			    shdr.sh_name);
509 		}
510 
511 		if (strcmp(sname, CTF_ELF_SCN_NAME) == 0) {
512 			secxlate[srcidx] = -1;
513 		} else if (!keep_stabs &&
514 		    (strncmp(sname, ".stab", 5) == 0 ||
515 		    strncmp(sname, ".debug", 6) == 0 ||
516 		    strncmp(sname, ".rel.debug", 10) == 0 ||
517 		    strncmp(sname, ".rela.debug", 11) == 0)) {
518 			secxlate[srcidx] = -1;
519 		} else if (dynsym && shdr.sh_type == SHT_SYMTAB) {
520 			/*
521 			 * If we're building CTF against the dynsym,
522 			 * we'll rip out the symtab so debuggers aren't
523 			 * confused.
524 			 */
525 			secxlate[srcidx] = -1;
526 		} else {
527 			secxlate[srcidx] = dstidx++;
528 			curnmoff += strlen(sname) + 1;
529 		}
530 
531 		new_offset = (off_t)dehdr.e_phoff;
532 	}
533 
534 	for (srcidx = 1; srcidx < sehdr.e_shnum; srcidx++) {
535 		char *sname;
536 
537 		sscn = elf_getscn(src, srcidx);
538 		gelf_getshdr(sscn, &shdr);
539 
540 		if (secxlate[srcidx] == -1) {
541 			changing = 1;
542 			continue;
543 		}
544 
545 		dscn = elf_newscn(dst);
546 
547 		/*
548 		 * If this file has program headers, we need to explicitly lay
549 		 * out sections.  If none of the sections prior to this one have
550 		 * been removed, then we can just use the existing location.  If
551 		 * one or more sections have been changed, then we need to
552 		 * adjust this one to avoid holes.
553 		 */
554 		if (changing && sehdr.e_phnum != 0) {
555 			pad = new_offset % shdr.sh_addralign;
556 
557 			if (pad)
558 				new_offset += shdr.sh_addralign - pad;
559 			shdr.sh_offset = new_offset;
560 		}
561 
562 		shdr.sh_link = secxlate[shdr.sh_link];
563 
564 		if (shdr.sh_type == SHT_REL || shdr.sh_type == SHT_RELA)
565 			shdr.sh_info = secxlate[shdr.sh_info];
566 
567 		sname = elf_strptr(src, sehdr.e_shstrndx, shdr.sh_name);
568 		if (sname == NULL) {
569 			elfterminate(srcname, "Can't find string at %u",
570 			    shdr.sh_name);
571 		}
572 		if ((sdata = elf_getdata(sscn, NULL)) == NULL)
573 			elfterminate(srcname, "Cannot get sect %s data", sname);
574 		if ((ddata = elf_newdata(dscn)) == NULL)
575 			elfterminate(dstname, "Can't make sect %s data", sname);
576 		bcopy(sdata, ddata, sizeof (Elf_Data));
577 
578 		if (srcidx == sehdr.e_shstrndx) {
579 			char seclen = strlen(CTF_ELF_SCN_NAME);
580 
581 			ddata->d_buf = xmalloc(ddata->d_size + shdr.sh_size +
582 			    seclen + 1);
583 			bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size);
584 			strcpy((caddr_t)ddata->d_buf + shdr.sh_size,
585 			    CTF_ELF_SCN_NAME);
586 			ctfnameoff = (off_t)shdr.sh_size;
587 			shdr.sh_size += seclen + 1;
588 			ddata->d_size += seclen + 1;
589 
590 			if (sehdr.e_phnum != 0)
591 				changing = 1;
592 		}
593 
594 		if (shdr.sh_type == symtab_type && shdr.sh_entsize != 0) {
595 			int nsym = shdr.sh_size / shdr.sh_entsize;
596 
597 			symtab_idx = secxlate[srcidx];
598 
599 			ddata->d_buf = xmalloc(shdr.sh_size);
600 			bcopy(sdata->d_buf, ddata->d_buf, shdr.sh_size);
601 
602 			for (i = 0; i < nsym; i++) {
603 				GElf_Sym sym;
604 				short newscn;
605 
606 				(void) gelf_getsym(ddata, i, &sym);
607 
608 				if (sym.st_shndx >= SHN_LORESERVE)
609 					continue;
610 
611 				if ((newscn = secxlate[sym.st_shndx]) !=
612 				    sym.st_shndx) {
613 					sym.st_shndx =
614 					    (newscn == -1 ? 1 : newscn);
615 
616 					gelf_update_sym(ddata, i, &sym);
617 				}
618 			}
619 		}
620 
621 		if (gelf_update_shdr(dscn, &shdr) == NULL)
622 			elfterminate(dstname, "Cannot update sect %s", sname);
623 
624 		new_offset = (off_t)shdr.sh_offset;
625 		if (shdr.sh_type != SHT_NOBITS)
626 			new_offset += shdr.sh_size;
627 	}
628 
629 	if (symtab_idx == -1) {
630 		terminate("%s: Cannot find %s section\n", srcname,
631 		    dynsym ? "SHT_DYNSYM" : "SHT_SYMTAB");
632 	}
633 
634 	/* Add the ctf section */
635 	dscn = elf_newscn(dst);
636 	gelf_getshdr(dscn, &shdr);
637 	shdr.sh_name = ctfnameoff;
638 	shdr.sh_type = SHT_PROGBITS;
639 	shdr.sh_size = ctfsize;
640 	shdr.sh_link = symtab_idx;
641 	shdr.sh_addralign = 4;
642 	if (changing && sehdr.e_phnum != 0) {
643 		pad = new_offset % shdr.sh_addralign;
644 
645 		if (pad)
646 			new_offset += shdr.sh_addralign - pad;
647 
648 		shdr.sh_offset = new_offset;
649 		new_offset += shdr.sh_size;
650 	}
651 
652 	ddata = elf_newdata(dscn);
653 	ddata->d_buf = ctfdata;
654 	ddata->d_size = ctfsize;
655 	ddata->d_align = shdr.sh_addralign;
656 
657 	gelf_update_shdr(dscn, &shdr);
658 
659 	/* update the section header location */
660 	if (sehdr.e_phnum != 0) {
661 		size_t align = gelf_fsize(dst, ELF_T_ADDR, 1, EV_CURRENT);
662 		size_t r = new_offset % align;
663 
664 		if (r)
665 			new_offset += align - r;
666 
667 		dehdr.e_shoff = new_offset;
668 	}
669 
670 	/* commit to disk */
671 	dehdr.e_shstrndx = secxlate[sehdr.e_shstrndx];
672 	gelf_update_ehdr(dst, &dehdr);
673 	if (elf_update(dst, ELF_C_WRITE) < 0)
674 		elfterminate(dstname, "Cannot finalize temp file");
675 
676 	free(secxlate);
677 }
678 
679 static caddr_t
680 make_ctf_data(tdata_t *td, Elf *elf, const char *file, size_t *lenp, int flags)
681 {
682 	iiburst_t *iiburst;
683 	caddr_t data;
684 
685 	iiburst = sort_iidescs(elf, file, td, flags & CTF_FUZZY_MATCH,
686 	    flags & CTF_USE_DYNSYM);
687 	data = ctf_gen(iiburst, lenp, flags & CTF_COMPRESS);
688 
689 	iiburst_free(iiburst);
690 
691 	return (data);
692 }
693 
694 void
695 write_ctf(tdata_t *td, const char *curname, const char *newname, int flags)
696 {
697 	struct stat st;
698 	Elf *elf = NULL;
699 	Elf *telf = NULL;
700 	caddr_t data;
701 	size_t len;
702 	int fd = -1;
703 	int tfd = -1;
704 
705 	(void) elf_version(EV_CURRENT);
706 	if ((fd = open(curname, O_RDONLY)) < 0 || fstat(fd, &st) < 0)
707 		terminate("%s: Cannot open for re-reading", curname);
708 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
709 		elfterminate(curname, "Cannot re-read");
710 
711 	if ((tfd = open(newname, O_RDWR | O_CREAT | O_TRUNC, st.st_mode)) < 0)
712 		terminate("Cannot open temp file %s for writing", newname);
713 	if ((telf = elf_begin(tfd, ELF_C_WRITE, NULL)) == NULL)
714 		elfterminate(curname, "Cannot write");
715 
716 	data = make_ctf_data(td, elf, curname, &len, flags);
717 	write_file(elf, curname, telf, newname, data, len, flags);
718 	free(data);
719 
720 	elf_end(telf);
721 	elf_end(elf);
722 	(void) close(fd);
723 	(void) close(tfd);
724 }
725