xref: /titanic_51/usr/src/tools/ctf/cvt/output.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Routines for preparing tdata trees for conversion into CTF data, and
31  * for placing the resulting data into an output file.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <libelf.h>
41 #include <gelf.h>
42 #include <unistd.h>
43 
44 #include "ctftools.h"
45 #include "list.h"
46 #include "memory.h"
47 #include "traverse.h"
48 #include "symbol.h"
49 
50 typedef struct iidesc_match {
51 	int iim_fuzzy;
52 	iidesc_t *iim_ret;
53 	char *iim_name;
54 	char *iim_file;
55 	uchar_t iim_bind;
56 } iidesc_match_t;
57 
58 static int
59 burst_iitypes(void *data, void *arg)
60 {
61 	iidesc_t *ii = data;
62 	iiburst_t *iiburst = arg;
63 
64 	switch (ii->ii_type) {
65 	case II_GFUN:
66 	case II_SFUN:
67 	case II_GVAR:
68 	case II_SVAR:
69 		if (!(ii->ii_flags & IIDESC_F_USED))
70 			return (0);
71 		break;
72 	default:
73 		break;
74 	}
75 
76 	ii->ii_dtype->t_flags |= TDESC_F_ISROOT;
77 	(void) iitraverse_td(ii, iiburst->iib_tdtd);
78 	return (1);
79 }
80 
81 /*ARGSUSED1*/
82 static int
83 save_type_by_id(tdesc_t *tdp, tdesc_t **tdpp, void *private)
84 {
85 	iiburst_t *iiburst = private;
86 
87 	/*
88 	 * Doing this on every node is horribly inefficient, but given that
89 	 * we may be suppressing some types, we can't trust nextid in the
90 	 * tdata_t.
91 	 */
92 	if (tdp->t_id > iiburst->iib_maxtypeid)
93 		iiburst->iib_maxtypeid = tdp->t_id;
94 
95 	slist_add(&iiburst->iib_types, tdp, tdesc_idcmp);
96 
97 	return (1);
98 }
99 
100 static tdtrav_cb_f burst_types_cbs[] = {
101 	NULL,
102 	save_type_by_id,	/* intrinsic */
103 	save_type_by_id,	/* pointer */
104 	save_type_by_id,	/* array */
105 	save_type_by_id,	/* function */
106 	save_type_by_id,	/* struct */
107 	save_type_by_id,	/* union */
108 	save_type_by_id,	/* enum */
109 	save_type_by_id,	/* forward */
110 	save_type_by_id,	/* typedef */
111 	tdtrav_assert,		/* typedef_unres */
112 	save_type_by_id,	/* volatile */
113 	save_type_by_id,	/* const */
114 	save_type_by_id		/* restrict */
115 };
116 
117 
118 static iiburst_t *
119 iiburst_new(tdata_t *td, int max)
120 {
121 	iiburst_t *iiburst = xcalloc(sizeof (iiburst_t));
122 	iiburst->iib_td = td;
123 	iiburst->iib_funcs = xcalloc(sizeof (iidesc_t *) * max);
124 	iiburst->iib_nfuncs = 0;
125 	iiburst->iib_objts = xcalloc(sizeof (iidesc_t *) * max);
126 	iiburst->iib_nobjts = 0;
127 	return (iiburst);
128 }
129 
130 static void
131 iiburst_types(iiburst_t *iiburst)
132 {
133 	tdtrav_data_t tdtd;
134 
135 	tdtrav_init(&tdtd, &iiburst->iib_td->td_curvgen, NULL, burst_types_cbs,
136 	    NULL, (void *)iiburst);
137 
138 	iiburst->iib_tdtd = &tdtd;
139 
140 	(void) hash_iter(iiburst->iib_td->td_iihash, burst_iitypes, iiburst);
141 }
142 
143 static void
144 iiburst_free(iiburst_t *iiburst)
145 {
146 	free(iiburst->iib_funcs);
147 	free(iiburst->iib_objts);
148 	list_free(iiburst->iib_types, NULL, NULL);
149 	free(iiburst);
150 }
151 
152 /*
153  * See if this iidesc matches the ELF symbol data we pass in.
154  *
155  * A fuzzy match is where we have a local symbol matching the name of a
156  * global type description. This is common when a mapfile is used for a
157  * DSO, but we don't accept it by default.
158  *
159  * A weak fuzzy match is when a weak symbol was resolved and matched to
160  * a global type description.
161  */
162 static int
163 matching_iidesc(iidesc_t *iidesc, iidesc_match_t *match)
164 {
165 	if (streq(iidesc->ii_name, match->iim_name) == 0)
166 		return (0);
167 
168 	switch (iidesc->ii_type) {
169 	case II_GFUN:
170 	case II_GVAR:
171 		if (match->iim_bind == STB_GLOBAL) {
172 			match->iim_ret = iidesc;
173 			return (-1);
174 		} else if (match->iim_fuzzy && match->iim_ret == NULL) {
175 			match->iim_ret = iidesc;
176 			/* continue to look for strong match */
177 			return (0);
178 		}
179 		break;
180 	case II_SFUN:
181 	case II_SVAR:
182 		if (match->iim_bind == STB_LOCAL &&
183 		    match->iim_file != NULL &&
184 		    streq(iidesc->ii_owner, match->iim_file)) {
185 			match->iim_ret = iidesc;
186 			return (-1);
187 		}
188 		break;
189 	}
190 	return (0);
191 }
192 
193 static iidesc_t *
194 find_iidesc(hash_t *hash, iidesc_match_t *match)
195 {
196 	iidesc_t tmpdesc;
197 	match->iim_ret = NULL;
198 	bzero(&tmpdesc, sizeof (iidesc_t));
199 	tmpdesc.ii_name = match->iim_name;
200 	(void) hash_match(hash, &tmpdesc, (int (*)())matching_iidesc, match);
201 	return (match->iim_ret);
202 }
203 
204 /*
205  * If we have a weak symbol, attempt to find the strong symbol it will
206  * resolve to.  Note: the code where this actually happens is in
207  * sym_process() in cmd/sgs/libld/common/syms.c
208  *
209  * Finding the matching symbol is unfortunately not trivial.  For a
210  * symbol to be a candidate, it must:
211  *
212  * - have the same type (function, object)
213  * - have the same value (address)
214  * - have the same size
215  * - not be another weak symbol
216  * - belong to the same section (checked via section index)
217  *
218  * If such a candidate is global, then we assume we've found it.  The
219  * linker generates the symbol table such that the curfile might be
220  * incorrect; this is OK for global symbols, since find_iidesc() doesn't
221  * need to check for the source file for the symbol.
222  *
223  * We might have found a strong local symbol, where the curfile is
224  * accurate and matches that of the weak symbol.  We assume this is a
225  * reasonable match.
226  *
227  * If we've got a local symbol with a non-matching curfile, there are
228  * two possibilities.  Either this is a completely different symbol, or
229  * it's a once-global symbol that was scoped to local via a mapfile.  In
230  * the latter case, curfile is likely inaccurate since the linker does
231  * not preserve the needed curfile in the order of the symbol table (see
232  * the comments about locally scoped symbols in libld's update_osym()).
233  * As we can't tell this case from the former one, we use this symbol
234  * iff no other matching symbol is found.
235  *
236  * What we really need here is a SUNW section containing weak<->strong
237  * mappings that we can consume.
238  */
239 static int
240 check_for_weak(GElf_Sym *weak, char const *weakfile,
241     Elf_Data *data, int nent, Elf_Data *strdata,
242     GElf_Sym *retsym, char **curfilep)
243 {
244 	char *curfile = NULL;
245 	char *tmpfile;
246 	GElf_Sym tmpsym;
247 	int candidate = 0;
248 	int i;
249 
250 	if (GELF_ST_BIND(weak->st_info) != STB_WEAK)
251 		return (0);
252 
253 	for (i = 0; i < nent; i++) {
254 		GElf_Sym sym;
255 		uchar_t type;
256 
257 		if (gelf_getsym(data, i, &sym) == NULL)
258 			continue;
259 
260 		type = GELF_ST_TYPE(sym.st_info);
261 
262 		if (type == STT_FILE)
263 			curfile = (char *)strdata->d_buf + sym.st_name;
264 
265 		if (GELF_ST_TYPE(weak->st_info) != type ||
266 		    weak->st_value != sym.st_value)
267 			continue;
268 
269 		if (weak->st_size != sym.st_size)
270 			continue;
271 
272 		if (GELF_ST_BIND(sym.st_info) == STB_WEAK)
273 			continue;
274 
275 		if (sym.st_shndx != weak->st_shndx)
276 			continue;
277 
278 		if (GELF_ST_BIND(sym.st_info) == STB_LOCAL &&
279 		    (curfile == NULL || weakfile == NULL ||
280 		    strcmp(curfile, weakfile) != 0)) {
281 			candidate = 1;
282 			tmpfile = curfile;
283 			tmpsym = sym;
284 			continue;
285 		}
286 
287 		*curfilep = curfile;
288 		*retsym = sym;
289 		return (1);
290 	}
291 
292 	if (candidate) {
293 		*curfilep = tmpfile;
294 		*retsym = tmpsym;
295 		return (1);
296 	}
297 
298 	return (0);
299 }
300 
301 /*
302  * When we've found the underlying symbol's type description
303  * for a weak symbol, we need to copy it and rename it to match
304  * the weak symbol. We also need to add it to the td so it's
305  * handled along with the others later.
306  */
307 static iidesc_t *
308 copy_from_strong(tdata_t *td, GElf_Sym *sym, iidesc_t *strongdesc,
309     const char *weakname, const char *weakfile)
310 {
311 	iidesc_t *new = iidesc_dup_rename(strongdesc, weakname, weakfile);
312 	uchar_t type = GELF_ST_TYPE(sym->st_info);
313 
314 	switch (type) {
315 	case STT_OBJECT:
316 		new->ii_type = II_GVAR;
317 		break;
318 	case STT_FUNC:
319 		new->ii_type = II_GFUN;
320 		break;
321 	}
322 
323 	hash_add(td->td_iihash, new);
324 
325 	return (new);
326 }
327 
328 /*
329  * Process the symbol table of the output file, associating each symbol
330  * with a type description if possible, and sorting them into functions
331  * and data, maintaining symbol table order.
332  */
333 static iiburst_t *
334 sort_iidescs(Elf *elf, const char *file, tdata_t *td, int fuzzymatch,
335     int dynsym)
336 {
337 	iiburst_t *iiburst;
338 	Elf_Scn *scn;
339 	GElf_Shdr shdr;
340 	Elf_Data *data, *strdata;
341 	int i, stidx;
342 	int nent;
343 	iidesc_match_t match;
344 
345 	match.iim_fuzzy = fuzzymatch;
346 	match.iim_file = NULL;
347 
348 	if ((stidx = findelfsecidx(elf, 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("Cannot find %s section\n",
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_WRONLY | 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