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