Print this page
libconv
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sgs/dump/common/dump.c
+++ new/usr/src/cmd/sgs/dump/common/dump.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 /*
23 23 * Copyright (c) 1988 AT&T
24 24 * All Rights Reserved
25 25 *
26 26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 27 */
28 28
29 29 /* Get definitions for the relocation types supported. */
30 30 #define ELF_TARGET_ALL
31 31
32 32 #include <stdio.h>
33 33 #include <stdlib.h>
34 34 #include <locale.h>
35 35 #include <unistd.h>
36 36 #include <libelf.h>
37 37 #include <sys/link.h>
38 38 #include <sys/elf.h>
39 39 #include <sys/machelf.h>
40 40 #include <fcntl.h>
41 41 #include <sys/stat.h>
42 42 #include <errno.h>
43 43 #include <string.h>
44 44 #include "sgs.h"
45 45 #include "conv.h"
46 46 #include "dump.h"
47 47
48 48
49 49 #define OPTSTR "agcd:fhn:oprstvCLT:V?" /* option string for getopt() */
50 50
51 51 const char *UNKNOWN = "<unknown>";
52 52
53 53 static SCNTAB *p_symtab, *p_head_scns, *p_dynsym;
54 54
55 55 static int
56 56 x_flag = 0, /* option requires section header table */
57 57 z_flag = 0, /* process files within an archive */
58 58 rn_flag = 0; /* dump named relocation information */
59 59
60 60 static int
61 61 /* flags: ?_flag corresponds to ? option */
62 62 a_flag = 0, /* dump archive header of each member of archive */
63 63 g_flag = 0, /* dump archive symbol table */
64 64 c_flag = 0, /* dump the string table */
65 65 d_flag = 0, /* dump range of sections */
66 66 f_flag = 0, /* dump each file header */
67 67 h_flag = 0, /* dump section headers */
68 68 n_flag = 0, /* dump named section */
69 69 o_flag = 0, /* dump each program execution header */
70 70 r_flag = 0, /* dump relocation information */
71 71 s_flag = 0, /* dump section contents */
72 72 t_flag = 0, /* dump symbol table entries */
73 73 C_flag = 0, /* dump decoded C++ symbol names */
74 74 L_flag = 0, /* dump dynamic linking information */
75 75 T_flag = 0, /* dump symbol table range */
76 76 V_flag = 0; /* dump version information */
77 77
78 78 int p_flag = 0, /* suppress printing of headings */
79 79 v_flag = 0; /* print information in verbose form */
80 80
81 81 static int
82 82 d_low = 0, /* range for use with -d */
83 83 d_hi = 0,
84 84 d_num = 0;
85 85
86 86 static int
87 87 T_low = 0, /* range for use with -T */
88 88 T_hi = 0,
89 89 T_num = 0;
90 90
91 91 static char *name = NULL; /* for use with -n option */
92 92 char *prog_name;
93 93 static int errflag = 0;
94 94
95 95 static struct stab_list_s {
96 96 struct stab_list_s *next;
97 97 char *strings;
98 98 size_t size;
99 99 } *StringTableList = (void *)0;
100 100
101 101 extern void ar_sym_read();
102 102 extern void dump_exec_header();
103 103
104 104
105 105 /*
106 106 * Get the section descriptor and set the size of the
107 107 * data returned. Data is byte-order converted.
108 108 */
109 109 void *
110 110 get_scndata(Elf_Scn *fd_scn, size_t *size)
111 111 {
112 112 Elf_Data *p_data;
113 113
114 114 p_data = 0;
115 115 if ((p_data = elf_getdata(fd_scn, p_data)) == 0 ||
116 116 p_data->d_size == 0) {
117 117 return (NULL);
118 118 }
119 119 *size = p_data->d_size;
120 120 return (p_data->d_buf);
121 121 }
122 122
123 123 /*
124 124 * Get the section descriptor and set the size of the
125 125 * data returned. Data is raw (i.e., not byte-order converted).
126 126 */
127 127 static void *
128 128 get_rawscn(Elf_Scn *fd_scn, size_t *size)
129 129 {
130 130 Elf_Data *p_data;
131 131
132 132 p_data = 0;
133 133 if ((p_data = elf_rawdata(fd_scn, p_data)) == 0 ||
134 134 p_data->d_size == 0) {
135 135 return (NULL);
136 136 }
137 137
138 138 *size = p_data->d_size;
139 139 return (p_data->d_buf);
140 140 }
141 141
142 142 /*
143 143 * Print out a usage message in short form when program is invoked
144 144 * with insufficient or no arguments, and in long form when given
145 145 * either a ? or an invalid option.
146 146 */
147 147 static void
148 148 usage()
149 149 {
150 150 (void) fprintf(stderr,
151 151 "Usage: %s [-%s] file(s) ...\n", prog_name, OPTSTR);
152 152 if (errflag) {
153 153 (void) fprintf(stderr,
154 154 "\t\t[-a dump archive header of each member of archive]\n\
155 155 [-g dump archive global symbol table]\n\
156 156 [-c dump the string table]\n\
157 157 [-d dump range of sections]\n\
158 158 [-f dump each file header]\n\
159 159 [-h dump section headers]\n\
160 160 [-n dump named section]\n\
161 161 [-o dump each program execution header]\n\
162 162 [-p suppress printing of headings]\n\
163 163 [-r dump relocation information]\n\
164 164 [-s dump section contents]\n\
165 165 [-t dump symbol table entries]\n\
166 166 [-v print information in verbose form]\n\
167 167 [-C dump decoded C++ symbol names]\n\
168 168 [-L dump the .dynamic structure]\n\
169 169 [-T dump symbol table range]\n\
170 170 [-V dump version information]\n");
171 171 }
172 172 }
173 173
174 174 /*
175 175 * Set a range. Input is a character string, a lower
176 176 * bound and an upper bound. This function converts
177 177 * a character string into its correct integer values,
178 178 * setting the first value as the lower bound, and
179 179 * the second value as the upper bound. If more values
180 180 * are given they are ignored with a warning.
181 181 */
182 182 static void
183 183 set_range(char *s, int *low, int *high)
184 184 {
185 185 char *w;
186 186 char *lasts;
187 187
188 188 while ((w = strtok_r(s, ",", &lasts)) != NULL) {
189 189 if (!(*low))
190 190 /* LINTED */
191 191 *low = (int)atol(w);
192 192 else
193 193 if (!(*high))
194 194 /* LINTED */
195 195 *high = (int)atol(w);
196 196 else {
197 197 (void) fprintf(stderr,
198 198 "%s: too many arguments - %s ignored\n",
199 199 prog_name, w);
200 200 return;
201 201 }
202 202 s = NULL;
203 203 } /* end while */
204 204 }
205 205
206 206
207 207 /*
208 208 * Print static shared library information.
209 209 */
210 210 static void
211 211 print_static(SCNTAB *l_scns, char *filename)
212 212 {
213 213 size_t section_size;
214 214 unsigned char *strtab;
215 215 unsigned char *path, buf[1024];
216 216 unsigned long *temp;
217 217 unsigned long total, topath;
218 218
219 219 (void) printf("\n **** STATIC SHARED LIBRARY INFORMATION ****\n");
220 220 (void) printf("\n%s:\n", filename);
221 221 (void) printf("\t");
222 222 section_size = 0;
223 223 if ((strtab = (unsigned char *)
224 224 get_scndata(l_scns->p_sd, §ion_size)) == NULL) {
225 225 return;
226 226 }
227 227
228 228 while (section_size != 0) {
229 229 /* LINTED */
230 230 temp = (unsigned long *)strtab;
231 231 total = temp[0];
232 232 topath = temp[1];
233 233 path = strtab + (topath*sizeof (long));
234 234 (void) strncpy((char *)buf, (char *)path,
235 235 (total - topath)*sizeof (long));
236 236 (void) fprintf(stdout, "%s\n", buf);
237 237 strtab += total*sizeof (long);
238 238 section_size -= (total*sizeof (long));
239 239 }
240 240 }
241 241
242 242 /*
243 243 * Print raw data in hexidecimal. Input is the section data to
244 244 * be printed out and the size of the data. Output is relative
245 245 * to a table lookup in dumpmap.h.
246 246 */
247 247 static void
248 248 print_rawdata(unsigned char *p_sec, size_t size)
249 249 {
250 250 size_t j;
251 251 size_t count;
252 252
253 253 count = 1;
254 254
255 255 (void) printf("\t");
256 256 for (j = size/sizeof (short); j != 0; --j, ++count) {
257 257 (void) printf("%.2x %.2x ", p_sec[0], p_sec[1]);
258 258 p_sec += 2;
259 259 if (count == 12) {
260 260 (void) printf("\n\t");
261 261 count = 0;
262 262 }
263 263 }
264 264
265 265 /*
266 266 * take care of last byte if odd byte section
267 267 */
268 268 if ((size & 0x1L) == 1L)
269 269 (void) printf("%.2x", *p_sec);
270 270 (void) printf("\n");
271 271 }
272 272
273 273
274 274
275 275 /*
276 276 * Print relocation data of type SHT_RELA
277 277 * If d_flag, print data corresponding only to
278 278 * the section or range of sections specified.
279 279 * If n_flag, print data corresponding only to
280 280 * the named section.
281 281 */
282 282 static void
283 283 print_rela(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
284 284 GElf_Ehdr * p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
285 285 SCNTAB *reloc_symtab)
286 286 {
287 287 GElf_Rela rela;
288 288 GElf_Sym sym;
289 289 size_t no_entries;
290 290 size_t rel_entsize;
291 291 size_t no_syms;
292 292 int type, symid;
293 293 static int n_title = 0;
294 294 int ndx = 0;
295 295 char *sym_name;
296 296 int adj = 0;
297 297
298 298 if (gelf_getclass(elf_file) == ELFCLASS64)
299 299 adj = 8;
300 300
301 301 rel_entsize = p_scns->p_shdr.sh_entsize;
302 302 if ((rel_entsize == 0) ||
303 303 (rel_entsize > p_scns->p_shdr.sh_size)) {
304 304 rel_entsize = gelf_fsize(elf_file, ELF_T_RELA, 1,
305 305 EV_CURRENT);
306 306 }
307 307 no_entries = reloc_size / rel_entsize;
308 308
309 309 no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
310 310 while (no_entries--) {
311 311 (void) gelf_getrela(rdata, ndx, &rela);
312 312 /* LINTED */
313 313 type = (int)GELF_R_TYPE(rela.r_info);
314 314 /* LINTED */
315 315 symid = (int)GELF_R_SYM(rela.r_info);
316 316 /* LINTED */
317 317 if ((symid > (no_syms - 1)) || (symid < 0)) {
318 318 (void) fprintf(stderr, "%s: %s: invalid symbol table "
319 319 "offset - %d - in %s\n", prog_name, filename,
320 320 symid, p_scns->scn_name);
321 321 ndx++;
322 322 continue;
323 323 }
324 324 (void) gelf_getsym(sym_data, symid, &sym);
325 325 sym_name = (char *)elf_strptr(elf_file,
326 326 reloc_symtab->p_shdr.sh_link, sym.st_name);
327 327 if (sym_name == NULL)
328 328 sym_name = (char *)UNKNOWN;
329 329 if (r_flag && rn_flag) {
330 330 if (strcmp(name, p_scns->scn_name) != 0) {
331 331 ndx++;
332 332 continue;
333 333 }
334 334 if (!n_title) {
335 335 (void) printf("\n%s:\n", p_scns->scn_name);
336 336 (void) printf("%-*s%-*s%-*s%s\n\n",
337 337 12 + adj, "Offset", 22, "Symndx",
338 338 16, "Type", "Addend");
339 339 n_title = 1;
340 340 }
341 341 }
342 342 if (d_flag) {
343 343 if (!d_hi)
344 344 d_hi = d_low;
345 345 if ((symid < d_low) || (symid > d_hi)) {
346 346 ndx++;
347 347 continue;
348 348 }
349 349 }
350 350
351 351 (void) printf("%-#*llx", 12 + adj, EC_XWORD(rela.r_offset));
352 352 if (!v_flag) {
353 353 (void) printf("%-22d%-18d", symid, type);
354 354 } else {
355 355 Conv_inv_buf_t inv_buf;
356 356
357 357 if (strlen(sym_name)) {
358 358 size_t len = strlen(sym_name) + 1;
359 359 char tmpstr[10];
360 360 if (len > 22) {
361 361 (void) sprintf(tmpstr, "%%-%ds",
362 362 /* LINTED */
363 363 (int)len);
364 364 /*LINTED: E_SEC_PRINTF_VAR_FMT*/
365 365 (void) printf(tmpstr, sym_name);
366 366 } else
367 367 (void) printf("%-22s", sym_name);
368 368 } else {
369 369 (void) printf("%-22d", symid);
370 370 }
371 371 (void) printf("%-20s",
372 372 conv_reloc_type(p_ehdr->e_machine,
373 373 type, DUMP_CONVFMT, &inv_buf));
374 374 }
375 375 (void) printf("%lld\n", EC_SXWORD(rela.r_addend));
376 376 ndx++;
377 377 }
378 378 }
379 379
380 380 /*
381 381 * Print relocation data of type SHT_REL.
382 382 * If d_flag, print data corresponding only to
383 383 * the section or range of sections specified.
384 384 * If n_flag, print data corresponding only to
385 385 * the named section.
386 386 */
387 387 static void
388 388 print_rel(Elf *elf_file, SCNTAB *p_scns, Elf_Data *rdata, Elf_Data *sym_data,
389 389 GElf_Ehdr *p_ehdr, size_t reloc_size, size_t sym_size, char *filename,
390 390 SCNTAB *reloc_symtab)
391 391 {
392 392 GElf_Rel rel;
393 393 GElf_Sym sym;
394 394 size_t no_entries;
395 395 size_t rel_entsize;
396 396 int type, symid;
397 397 size_t no_syms;
398 398 static int n_title = 0;
399 399 int ndx = 0;
400 400 char *sym_name;
401 401 int adj = 0;
402 402
403 403 if (gelf_getclass(elf_file) == ELFCLASS64)
404 404 adj = 8;
405 405
406 406 rel_entsize = p_scns->p_shdr.sh_entsize;
407 407 if ((rel_entsize == 0) ||
408 408 (rel_entsize > p_scns->p_shdr.sh_size)) {
409 409 rel_entsize = gelf_fsize(elf_file, ELF_T_REL, 1,
410 410 EV_CURRENT);
411 411 }
412 412 no_entries = reloc_size / rel_entsize;
413 413
414 414 no_syms = sym_size / gelf_fsize(elf_file, ELF_T_SYM, 1, EV_CURRENT);
415 415 while (no_entries--) {
416 416 (void) gelf_getrel(rdata, ndx, &rel);
417 417 /* LINTED */
418 418 type = (int)GELF_R_TYPE(rel.r_info);
419 419 /* LINTED */
420 420 symid = (int)GELF_R_SYM(rel.r_info);
421 421 /* LINTED */
422 422 if ((symid > (no_syms - 1)) || (symid < 0)) {
423 423 (void) fprintf(stderr, "%s: %s: invalid symbol table "
424 424 "offset - %d - in %s\n", prog_name, filename,
425 425 symid, p_scns->scn_name);
426 426 ndx++;
427 427 continue;
428 428 }
429 429 (void) gelf_getsym(sym_data, symid, &sym);
430 430 sym_name = (char *)elf_strptr(elf_file,
431 431 reloc_symtab->p_shdr.sh_link, sym.st_name);
432 432 if (sym_name == NULL)
433 433 sym_name = (char *)UNKNOWN;
434 434 if (r_flag && rn_flag) {
435 435 if (strcmp(name, p_scns->scn_name) != 0) {
436 436 ndx++;
437 437 continue;
438 438 }
439 439 if (!n_title) {
440 440 (void) printf("\n%s:\n", p_scns->scn_name);
441 441 (void) printf("%-*s%-*s%s\n\n",
442 442 12 + adj, "Offset", 20, "Symndx", "Type");
443 443 n_title = 1;
444 444 }
445 445 }
446 446 if (d_flag) {
447 447 if (!d_hi)
448 448 d_hi = d_low;
449 449 if ((symid < d_low) || (symid > d_hi)) {
450 450 ndx++;
451 451 continue;
452 452 }
453 453 }
454 454
455 455 (void) printf("%-#*llx", 12 + adj, EC_ADDR(rel.r_offset));
456 456 if (!v_flag) {
457 457 (void) printf("%-20d%-18d", symid, type);
458 458 } else {
459 459 Conv_inv_buf_t inv_buf;
460 460
461 461 if (strlen(sym_name))
462 462 (void) printf("%-20s", sym_name);
463 463 else {
464 464 (void) printf("%-20d", sym.st_name);
465 465 }
466 466 (void) printf("%-20s",
467 467 conv_reloc_type(p_ehdr->e_machine,
468 468 type, DUMP_CONVFMT, &inv_buf));
469 469 }
↓ open down ↓ |
469 lines elided |
↑ open up ↑ |
470 470 (void) printf("\n");
471 471 ndx++;
472 472 }
473 473 }
474 474
475 475 /* demangle C++ names */
476 476 static char *
477 477 demangled_name(char *s)
478 478 {
479 479 static char *buf = NULL;
480 + size_t buflen = 0;
480 481 const char *dn;
481 482 size_t len;
482 483
483 484 dn = conv_demangle_name(s);
484 485
485 486 /*
486 487 * If not demangled, just return the symbol name
487 488 */
488 - if (strcmp(s, dn) == 0)
489 + if (dn == s)
489 490 return (s);
490 491
492 + len = strlen(dn) + strlen(s) + 4;
493 +
494 + if (buflen < len) {
495 + free(buf);
496 + if ((buf = malloc(len)) == NULL)
497 + return (s);
498 + buflen = len;
499 + }
500 +
491 501 /*
492 502 * Demangled. Format it
493 503 */
494 - if (buf != NULL)
495 - free(buf);
496 -
497 - len = strlen(dn) + strlen(s) + 4;
498 - if ((buf = malloc(len)) == NULL)
499 - return (s);
500 -
501 - (void) snprintf(buf, len, "%s\t[%s]", dn, s);
504 + (void) snprintf(buf, buflen, "%s\t[%s]", dn, s);
505 + free(dn);
502 506 return (buf);
503 507 }
504 508
505 509 /*
506 510 * Print the symbol table. Input is an ELF file descriptor, a
507 511 * pointer to the symbol table SCNTAB structure,
508 512 * the number of symbols, a range of symbols to print,
509 513 * an index which is the number of the
510 514 * section in the file, and the filename. The number of sections,
511 515 * the range, and the index are set in
512 516 * dump_symbol_table, depending on whether -n or -T were set.
513 517 */
514 518 static void
515 519 print_symtab(Elf *elf_file, SCNTAB *p_symtab, Elf_Data *sym_data,
516 520 long range, int index)
517 521 {
518 522 GElf_Sym sym;
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
519 523 int adj = 0; /* field adjustment for elf64 */
520 524 Elf32_Word *symshndx = 0;
521 525 unsigned int nosymshndx = 0;
522 526 Conv_inv_buf_t inv_buf;
523 527
524 528
525 529 if (gelf_getclass(elf_file) == ELFCLASS64)
526 530 adj = 8;
527 531
528 532 while (range > 0) {
529 - char *sym_name = (char *)0;
533 + char *sym_name = NULL;
530 534 int type, bind;
531 535 int specsec;
532 536 unsigned int shndx;
533 537
534 538 (void) gelf_getsym(sym_data, index, &sym);
535 539 type = (int)GELF_ST_TYPE(sym.st_info);
536 540 bind = (int)GELF_ST_BIND(sym.st_info);
537 541
538 542 if ((sym.st_shndx == SHN_XINDEX) &&
539 543 (symshndx == 0) && (nosymshndx == 0)) {
540 544 Elf_Scn *_scn;
541 545 GElf_Shdr _shdr;
542 546 size_t symscnndx;
543 547
544 548 symscnndx = elf_ndxscn(p_symtab->p_sd);
545 549 _scn = 0;
546 550 while ((_scn = elf_nextscn(elf_file, _scn)) != 0) {
547 551 if (gelf_getshdr(_scn, &_shdr) == 0)
548 552 break;
549 553 if ((_shdr.sh_type == SHT_SYMTAB_SHNDX) &&
550 554 /* LINTED */
551 555 (_shdr.sh_link == (GElf_Word)symscnndx)) {
552 556 Elf_Data *_data;
553 557
554 558 if ((_data = elf_getdata(_scn, 0)) == 0)
555 559 continue;
556 560
557 561 symshndx = (Elf32_Word *)_data->d_buf;
558 562 nosymshndx = 0;
559 563 break;
560 564 }
561 565 }
562 566 nosymshndx = 1;
563 567 }
564 568
565 569 if ((symshndx) && (sym.st_shndx == SHN_XINDEX)) {
566 570 shndx = symshndx[index];
567 571 specsec = 0;
568 572 } else {
569 573 shndx = sym.st_shndx;
570 574 if ((sym.st_shndx == SHN_UNDEF) ||
571 575 (sym.st_shndx >= SHN_LORESERVE))
572 576 specsec = 1;
573 577 else
574 578 specsec = 0;
575 579 }
576 580
577 581
578 582 (void) printf("[%d]\t ", index++);
579 583
580 584 if (v_flag && (type == STT_SPARC_REGISTER)) {
581 585 /*
582 586 * The strings "REG_G1" through "REG_G7" are intended
583 587 * to be consistent with output from elfdump(1).
584 588 */
585 589 (void) printf("%-*s", 12 + adj,
586 590 conv_sym_SPARC_value(sym.st_value,
587 591 DUMP_CONVFMT, &inv_buf));
588 592 } else {
589 593 (void) printf("0x%-*llx", 10 + adj,
590 594 EC_ADDR(sym.st_value));
591 595 }
592 596
593 597 (void) printf("%-*lld", 9 + adj, EC_XWORD(sym.st_size));
594 598
595 599 if (!v_flag) {
596 600 (void) printf("%d\t\t%d\t%d\t%#x\t",
597 601 type, bind, (int)sym.st_other, (int)shndx);
598 602 } else {
599 603 GElf_Ehdr p_ehdr;
600 604 (void) gelf_getehdr(elf_file, &p_ehdr);
601 605 (void) printf("%s\t",
602 606 conv_sym_info_type(p_ehdr.e_machine, type,
603 607 DUMP_CONVFMT, &inv_buf));
604 608 (void) printf("%s",
605 609 conv_sym_info_bind(bind, DUMP_CONVFMT, &inv_buf));
606 610 (void) printf("\t %d\t", EC_WORD(sym.st_other));
607 611
608 612 if (specsec)
609 613 (void) printf("%s",
610 614 conv_sym_shndx(p_ehdr.e_ident[EI_OSABI],
611 615 p_ehdr.e_machine, shndx,
612 616 CONV_FMT_DECIMAL, &inv_buf));
613 617 else
614 618 (void) printf("%d", EC_WORD(shndx));
615 619 (void) printf("\t");
616 620 }
617 621
618 622 /* support machines where NULL-deref causes core dump */
619 623 if (sym.st_name == 0)
620 624 sym_name = (char *)UNKNOWN;
621 625 else
622 626 if (C_flag)
623 627 sym_name = demangled_name(
624 628 (char *)elf_strptr(elf_file,
625 629 p_symtab->p_shdr.sh_link,
626 630 sym.st_name));
627 631 else
628 632 sym_name = (char *)elf_strptr(elf_file,
629 633 p_symtab->p_shdr.sh_link, sym.st_name);
630 634 if (sym_name == NULL)
631 635 sym_name = (char *)UNKNOWN;
632 636 (void) printf("%s\n", sym_name);
633 637
634 638 range--;
635 639 } /* end while */
636 640 }
637 641
638 642 /*
639 643 * Print the section header table. Input is the SCNTAB structure,
640 644 * the number of sections, an index which is the number of the
641 645 * section in the file, and the filename. The values of the SCNTAB
642 646 * structure, the number of sections, and the index are set in
643 647 * dump_shdr depending on whether the -n or -d modifiers were set.
644 648 */
645 649 static void
646 650 print_shdr(Elf *elf_file, SCNTAB *s, int num_scns, int index)
647 651 {
648 652 SCNTAB *p;
649 653 int num;
650 654 int field;
651 655 GElf_Ehdr p_ehdr;
652 656
653 657 if (gelf_getclass(elf_file) == ELFCLASS64)
654 658 field = 21;
655 659 else
656 660 field = 13;
657 661
658 662 p = s;
659 663 (void) gelf_getehdr(elf_file, &p_ehdr);
660 664
661 665 for (num = 0; num < num_scns; num++, p++) {
662 666 (void) printf("[%d]\t", index++);
663 667 if (!v_flag) {
664 668 (void) printf("%u\t%llu\t",
665 669 EC_WORD(p->p_shdr.sh_type),
666 670 EC_XWORD(p->p_shdr.sh_flags));
667 671 } else {
668 672 Conv_inv_buf_t inv_buf;
669 673
670 674 /*LINTED: E_SEC_PRINTF_VAR_FMT*/
671 675 (void) printf(conv_sec_type(
672 676 p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine,
673 677 p->p_shdr.sh_type, DUMP_CONVFMT, &inv_buf));
674 678 (void) printf(" ");
675 679
676 680 if (p->p_shdr.sh_flags & SHF_WRITE)
677 681 (void) printf("W");
678 682 else
679 683 (void) printf("-");
680 684 if (p->p_shdr.sh_flags & SHF_ALLOC)
681 685 (void) printf("A");
682 686 else
683 687 (void) printf("-");
684 688 if (p->p_shdr.sh_flags & SHF_EXECINSTR)
685 689 (void) printf("I");
686 690 else
687 691 (void) printf("-");
688 692
689 693 if (p->p_shdr.sh_flags & SHF_ORDERED)
690 694 (void) printf("O");
691 695 if (p->p_shdr.sh_flags & SHF_EXCLUDE)
692 696 (void) printf("E");
693 697
694 698 (void) printf("\t");
695 699
696 700 }
697 701 (void) printf("%-#*llx%-#*llx%-#*llx%s%s\n",
698 702 field, EC_ADDR(p->p_shdr.sh_addr),
699 703 field, EC_OFF(p->p_shdr.sh_offset),
700 704 field, EC_XWORD(p->p_shdr.sh_size),
701 705 /* compatibility: tab for elf32 */
702 706 (field == 13) ? "\t" : " ", p->scn_name);
703 707
704 708 (void) printf("\t%u\t%u\t%-#*llx%-#*llx\n\n",
705 709 EC_WORD(p->p_shdr.sh_link),
706 710 EC_WORD(p->p_shdr.sh_info),
707 711 field, EC_XWORD(p->p_shdr.sh_addralign),
708 712 field, EC_XWORD(p->p_shdr.sh_entsize));
709 713 }
710 714 }
711 715
712 716 /*
713 717 * Check that a range of numbers is valid. Input is
714 718 * a lower bound, an upper bound, a boundary condition,
715 719 * and the filename. Negative numbers and numbers greater
716 720 * than the bound are invalid. low must be smaller than hi.
717 721 * The returned integer is the number of items in the
718 722 * range if it is valid and -1 otherwise.
719 723 */
720 724 static int
721 725 check_range(int low, int hi, size_t bound, char *filename)
722 726 {
723 727 if (((size_t)low > bound) || (low <= 0)) {
724 728 (void) fprintf(stderr,
725 729 "%s: %s: number out of range, %d\n",
726 730 prog_name, filename, low);
727 731 return (-1);
728 732 }
729 733 if (((size_t)hi > bound) || (hi < 0)) {
730 734 (void) fprintf(stderr,
731 735 "%s: %s: number out of range, %d\n",
732 736 prog_name, filename, hi);
733 737 return (-1);
734 738 }
735 739
736 740 if (hi && (low > hi)) {
737 741 (void) fprintf(stderr,
738 742 "%s: %s: invalid range, %d,%d\n",
739 743 prog_name, filename, low, hi);
740 744 return (-1);
741 745 }
742 746 if (hi)
743 747 return (hi - low + 1);
744 748 else
745 749 return (1);
746 750 }
747 751
748 752 /*
749 753 * Print relocation information. Since this information is
750 754 * machine dependent, new sections must be added for each machine
751 755 * that is supported. Input is an ELF file descriptor, the ELF header,
752 756 * the SCNTAB structure, the number of sections, and a filename.
753 757 * Set up necessary information to print relocation information
754 758 * and call the appropriate print function depending on the
755 759 * type of relocation information. If the symbol table is
756 760 * absent, no relocation data is processed. Input is an
757 761 * ELF file descriptor, the ELF header, the SCNTAB structure,
758 762 * and the filename. Set range of d_flag and name if n_flag.
759 763 */
760 764 static void
761 765 dump_reloc_table(Elf *elf_file, GElf_Ehdr *p_ehdr,
762 766 SCNTAB *p_scns, int num_scns, char *filename)
763 767 {
764 768 Elf_Data *rel_data;
765 769 Elf_Data *sym_data;
766 770 size_t sym_size;
767 771 size_t reloc_size;
768 772 SCNTAB *reloc_symtab;
769 773 SCNTAB *head_scns;
770 774 int r_title = 0;
771 775 int adj = 0;
772 776 size_t shnum;
773 777
774 778 if (gelf_getclass(elf_file) == ELFCLASS64)
775 779 adj = 8;
776 780
777 781 if ((!p_flag) && (!r_title)) {
778 782 (void) printf("\n **** RELOCATION INFORMATION ****\n");
779 783 r_title = 1;
780 784 }
781 785
782 786 while (num_scns-- > 0) {
783 787 if ((p_scns->p_shdr.sh_type != SHT_RELA) &&
784 788 (p_scns->p_shdr.sh_type != SHT_REL)) {
785 789 p_scns++;
786 790 continue;
787 791 }
788 792
789 793 head_scns = p_head_scns;
790 794
791 795 if (elf_getshdrnum(elf_file, &shnum) == -1) {
792 796 (void) fprintf(stderr,
793 797 "%s: %s: elf_getshdrnum failed: %s\n",
794 798 prog_name, filename, elf_errmsg(-1));
795 799 return;
796 800 }
797 801
798 802 if ((p_scns->p_shdr.sh_link == 0) ||
799 803 /* LINTED */
800 804 (p_scns->p_shdr.sh_link >= (GElf_Word)shnum)) {
801 805 (void) fprintf(stderr, "%s: %s: invalid sh_link field: "
802 806 "section #: %d sh_link: %d\n",
803 807 /* LINTED */
804 808 prog_name, filename, (int)elf_ndxscn(p_scns->p_sd),
805 809 (int)p_scns->p_shdr.sh_link);
806 810 return;
807 811 }
808 812 head_scns += (p_scns->p_shdr.sh_link -1);
809 813
810 814 if (head_scns->p_shdr.sh_type == SHT_SYMTAB) {
811 815 reloc_symtab = p_symtab;
812 816 } else if (head_scns->p_shdr.sh_type == SHT_DYNSYM) {
813 817 reloc_symtab = p_dynsym;
814 818 } else {
815 819 (void) fprintf(stderr,
816 820 "%s: %s: could not get symbol table\n", prog_name, filename);
817 821 return;
818 822 }
819 823
820 824 sym_data = NULL;
821 825 sym_size = 0;
822 826 reloc_size = 0;
823 827
824 828 if ((sym_data = elf_getdata(reloc_symtab->p_sd, NULL)) == NULL) {
825 829 (void) fprintf(stderr,
826 830 "%s: %s: no symbol table data\n", prog_name, filename);
827 831 return;
828 832 }
829 833 sym_size = sym_data->d_size;
830 834
831 835 if (p_scns == NULL) {
832 836 (void) fprintf(stderr,
833 837 "%s: %s: no section table data\n", prog_name, filename);
834 838 return;
835 839 }
836 840
837 841 if (p_scns->p_shdr.sh_type == SHT_RELA) {
838 842 if (!n_flag && r_flag)
839 843 (void) printf("\n%s:\n", p_scns->scn_name);
840 844 if (!p_flag && (!n_flag && r_flag))
841 845 (void) printf("%-*s%-*s%-*s%s\n\n",
842 846 12 + adj, "Offset", 22, "Symndx",
843 847 18, "Type", "Addend");
844 848 if ((rel_data = elf_getdata(p_scns->p_sd, NULL)) == NULL) {
845 849 (void) fprintf(stderr,
846 850 "%s: %s: no relocation information\n", prog_name, filename);
847 851 return;
848 852 }
849 853 reloc_size = rel_data->d_size;
850 854
851 855 if (n_flag) {
852 856 rn_flag = 1;
853 857 print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
854 858 reloc_size, sym_size, filename, reloc_symtab);
855 859 }
856 860 if (d_flag) {
857 861 rn_flag = 0;
858 862 print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
859 863 reloc_size, sym_size, filename, reloc_symtab);
860 864 }
861 865 if (!n_flag && !d_flag)
862 866 print_rela(elf_file, p_scns, rel_data, sym_data, p_ehdr,
863 867 reloc_size, sym_size, filename, reloc_symtab);
864 868 } else {
865 869 if (p_scns->p_shdr.sh_type == SHT_REL) {
866 870 if (!n_flag && r_flag)
867 871 (void) printf("\n%s:\n", p_scns->scn_name);
868 872 if (!p_flag && (!n_flag && r_flag)) {
869 873 (void) printf("%-*s%-*s%s\n\n",
870 874 12 + adj, "Offset", 20, "Symndx", "Type");
871 875 }
872 876 if ((rel_data = elf_getdata(p_scns->p_sd, NULL))
873 877 == NULL) {
874 878 (void) fprintf(stderr,
875 879 "%s: %s: no relocation information\n", prog_name, filename);
876 880 return;
877 881 }
878 882 reloc_size = rel_data->d_size;
879 883 if (n_flag) {
880 884 rn_flag = 1;
881 885 print_rel(elf_file, p_scns, rel_data, sym_data,
882 886 p_ehdr, reloc_size, sym_size,
883 887 filename, reloc_symtab);
884 888 }
885 889 if (d_flag) {
886 890 rn_flag = 0;
887 891 print_rel(elf_file, p_scns, rel_data, sym_data,
888 892 p_ehdr, reloc_size, sym_size,
889 893 filename, reloc_symtab);
890 894 }
891 895 if (!n_flag && !d_flag)
892 896 print_rel(elf_file, p_scns, rel_data, sym_data,
893 897 p_ehdr, reloc_size, sym_size,
894 898 filename, reloc_symtab);
895 899 }
896 900 }
897 901 p_scns++;
898 902 }
899 903 }
900 904
901 905 /*
902 906 * Print out the string tables. Input is an opened ELF file,
903 907 * the SCNTAB structure, the number of sections, and the filename.
904 908 * Since there can be more than one string table, all sections are
905 909 * examined and any with the correct type are printed out.
906 910 */
907 911 static void
908 912 dump_string_table(SCNTAB *s, int num_scns)
909 913 {
910 914 size_t section_size;
911 915 unsigned char *strtab;
912 916 int beg_of_string;
913 917 int counter = 0;
914 918 int str_off;
915 919 int i;
916 920
917 921 if (!p_flag) {
918 922 (void) printf("\n **** STRING TABLE INFORMATION ****\n");
919 923 }
920 924
921 925 for (i = 0; i < num_scns; i++, s++) {
922 926 if (s->p_shdr.sh_type != SHT_STRTAB)
923 927 continue;
924 928
925 929 str_off = 0;
926 930
927 931 if (!p_flag) {
928 932 (void) printf("\n%s:\n", s->scn_name);
929 933 (void) printf(" <offset> \tName\n");
930 934 }
931 935 section_size = 0;
932 936 if ((strtab = (unsigned char *)
933 937 get_scndata(s->p_sd, §ion_size)) == NULL) {
934 938 continue;
935 939 }
936 940
937 941 if (section_size != 0) {
938 942 (void) printf(" <%d> \t", str_off);
939 943 beg_of_string = 0;
940 944 while (section_size--) {
941 945 unsigned char c = *strtab++;
942 946
943 947 if (beg_of_string) {
944 948 (void) printf(" <%d> \t", str_off);
945 949 counter++;
946 950 beg_of_string = 0;
947 951 }
948 952 str_off++;
949 953 switch (c) {
950 954 case '\0':
951 955 (void) printf("\n");
952 956 beg_of_string = 1;
953 957 break;
954 958 default:
955 959 (void) putchar(c);
956 960 }
957 961 }
958 962 }
959 963 }
960 964 (void) printf("\n");
961 965 }
962 966
963 967 /*
964 968 * Print the symbol table. This function does not print the contents
965 969 * of the symbol table but sets up the parameters and then calls
966 970 * print_symtab to print the symbols. Calling another function to print
967 971 * the symbols allows both -T and -n to work correctly
968 972 * simultaneously. Input is an opened ELF file, a pointer to the
969 973 * symbol table SCNTAB structure, and the filename.
970 974 * Set the range of symbols to print if T_flag, and set
971 975 * name of symbol to print if n_flag.
972 976 */
973 977 static void
974 978 dump_symbol_table(Elf *elf_file, SCNTAB *p_symtab, char *filename)
975 979 {
976 980 Elf_Data *sym_data;
977 981 GElf_Sym T_range, n_range; /* for use with -T and -n */
978 982 size_t count = 0;
979 983 size_t sym_size;
980 984 int index = 1;
981 985 int found_it = 0;
982 986 int i;
983 987 int adj = 0; /* field adjustment for elf64 */
984 988
985 989 if (gelf_getclass(elf_file) == ELFCLASS64)
986 990 adj = 8;
987 991
988 992 if (p_symtab == NULL) {
989 993 (void) fprintf(stderr,
990 994 "%s: %s: could not get symbol table\n", prog_name, filename);
991 995 return;
992 996 }
993 997
994 998 /* get symbol table data */
995 999 sym_data = NULL;
996 1000 sym_size = 0;
997 1001 if ((sym_data =
998 1002 elf_getdata(p_symtab->p_sd, NULL)) == NULL) {
999 1003 (void) printf("\n%s:\n", p_symtab->scn_name);
1000 1004 (void) printf("No symbol table data\n");
1001 1005 return;
1002 1006 }
1003 1007 sym_size = sym_data->d_size;
1004 1008
1005 1009 count = sym_size / p_symtab->p_shdr.sh_entsize;
1006 1010
1007 1011 if (n_flag && t_flag && !T_flag) {
1008 1012 /* LINTED */
1009 1013 for (i = 1; i < count; i++) {
1010 1014 (void) gelf_getsym(sym_data, i, &n_range);
1011 1015 if (strcmp(name, (char *)
1012 1016 elf_strptr(elf_file,
1013 1017 p_symtab->p_shdr.sh_link,
1014 1018 n_range.st_name)) != 0) {
1015 1019 continue;
1016 1020 } else {
1017 1021 found_it = 1;
1018 1022 if (!p_flag) {
1019 1023 (void) printf(
1020 1024 "\n ***** SYMBOL TABLE INFORMATION *****\n");
1021 1025 (void) printf(
1022 1026 "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName",
1023 1027 12 + adj, "Value", 9 + adj, "Size");
1024 1028 }
1025 1029 (void) printf("\n%s:\n", p_symtab->scn_name);
1026 1030 print_symtab(elf_file, p_symtab, sym_data,
1027 1031 1, i);
1028 1032 }
1029 1033 } /* end for */
1030 1034 if (!found_it) {
1031 1035 (void) fprintf(stderr, "%s: %s: %s not found\n",
1032 1036 prog_name, filename, name);
1033 1037 }
1034 1038 } else if (T_flag) {
1035 1039 T_num = check_range(T_low, T_hi, count, filename);
1036 1040 if (T_num < 0)
1037 1041 return;
1038 1042
1039 1043 (void) gelf_getsym(sym_data, T_low-1, &T_range);
1040 1044 index = T_low;
1041 1045
1042 1046 if (!p_flag) {
1043 1047 (void) printf(
1044 1048 "\n ***** SYMBOL TABLE INFORMATION *****\n");
1045 1049 (void) printf(
1046 1050 "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName",
1047 1051 12 + adj, "Value", 9 + adj, "Size");
1048 1052 }
1049 1053 (void) printf("\n%s:\n", p_symtab->scn_name);
1050 1054 print_symtab(elf_file, p_symtab, sym_data, T_num, index);
1051 1055 } else {
1052 1056 if (!p_flag) {
1053 1057 (void) printf(
1054 1058 "\n ***** SYMBOL TABLE INFORMATION *****\n");
1055 1059 (void) printf(
1056 1060 "[Index] %-*s%-*sType\tBind\tOther\tShndx\tName",
1057 1061 12 + adj, "Value", 9 + adj, "Size");
1058 1062 }
1059 1063 (void) printf("\n%s:\n", p_symtab->scn_name);
1060 1064 print_symtab(elf_file, p_symtab, sym_data, count-1, 1);
1061 1065 }
1062 1066 }
1063 1067
1064 1068
1065 1069 /*
1066 1070 * Print dynamic linking information. Input is an ELF
1067 1071 * file descriptor, the SCNTAB structure, the number of
1068 1072 * sections, and the filename.
1069 1073 */
1070 1074 static void
1071 1075 dump_dynamic(Elf *elf_file, SCNTAB *p_scns, int num_scns, char *filename)
1072 1076 {
1073 1077 #define pdyn_Fmtptr "%#llx"
1074 1078
1075 1079 Elf_Data *dyn_data;
1076 1080 GElf_Dyn p_dyn;
1077 1081 GElf_Phdr p_phdr;
1078 1082 GElf_Ehdr p_ehdr;
1079 1083 int index = 1;
1080 1084 int lib_scns = num_scns;
1081 1085 SCNTAB *l_scns = p_scns;
1082 1086 int header_num = 0;
1083 1087 const char *str;
1084 1088
1085 1089 (void) gelf_getehdr(elf_file, &p_ehdr);
1086 1090
1087 1091 if (!p_flag)
1088 1092 (void) printf("\n **** DYNAMIC SECTION INFORMATION ****\n");
1089 1093
1090 1094 for (; num_scns > 0; num_scns--, p_scns++) {
1091 1095 GElf_Word link;
1092 1096 int ii;
1093 1097
1094 1098
1095 1099 if (p_scns->p_shdr.sh_type != SHT_DYNAMIC)
1096 1100 continue;
1097 1101
1098 1102 if (!p_flag) {
1099 1103 (void) printf("%s:\n", p_scns->scn_name);
1100 1104 (void) printf("[INDEX]\tTag Value\n");
1101 1105 }
1102 1106
1103 1107 if ((dyn_data = elf_getdata(p_scns->p_sd, NULL)) == 0) {
1104 1108 (void) fprintf(stderr, "%s: %s: no data in "
1105 1109 "%s section\n", prog_name, filename,
1106 1110 p_scns->scn_name);
1107 1111 return;
1108 1112 }
1109 1113
1110 1114 link = p_scns->p_shdr.sh_link;
1111 1115 ii = 0;
1112 1116
1113 1117 (void) gelf_getdyn(dyn_data, ii++, &p_dyn);
1114 1118 while (p_dyn.d_tag != DT_NULL) {
1115 1119 union {
1116 1120 Conv_inv_buf_t inv;
1117 1121 Conv_dyn_flag_buf_t dyn_flag;
1118 1122 Conv_dyn_flag1_buf_t dyn_flag1;
1119 1123 Conv_dyn_feature1_buf_t dyn_feature1;
1120 1124 Conv_dyn_posflag1_buf_t dyn_posflag1;
1121 1125 } conv_buf;
1122 1126
1123 1127 (void) printf("[%d]\t%-15.15s ", index++,
1124 1128 conv_dyn_tag(p_dyn.d_tag,
1125 1129 p_ehdr.e_ident[EI_OSABI], p_ehdr.e_machine,
1126 1130 DUMP_CONVFMT, &conv_buf.inv));
1127 1131
1128 1132 /*
1129 1133 * It would be nice to use a table driven loop
1130 1134 * here, but the address space is too sparse
1131 1135 * and irregular. A switch is simple and robust.
1132 1136 */
1133 1137 switch (p_dyn.d_tag) {
1134 1138 /*
1135 1139 * Items with an address value
1136 1140 */
1137 1141 case DT_PLTGOT:
1138 1142 case DT_HASH:
1139 1143 case DT_STRTAB:
1140 1144 case DT_RELA:
1141 1145 case DT_SYMTAB:
1142 1146 case DT_INIT:
1143 1147 case DT_FINI:
1144 1148 case DT_REL:
1145 1149 case DT_DEBUG:
1146 1150 case DT_TEXTREL:
1147 1151 case DT_JMPREL:
1148 1152 case DT_INIT_ARRAY:
1149 1153 case DT_FINI_ARRAY:
1150 1154 case DT_INIT_ARRAYSZ:
1151 1155 case DT_FINI_ARRAYSZ:
1152 1156 case DT_PREINIT_ARRAY:
1153 1157 case DT_PREINIT_ARRAYSZ:
1154 1158 case DT_SUNW_RTLDINF:
1155 1159 case DT_SUNW_CAP:
1156 1160 case DT_SUNW_CAPINFO:
1157 1161 case DT_SUNW_CAPCHAIN:
1158 1162 case DT_SUNW_SYMTAB:
1159 1163 case DT_SUNW_SYMSORT:
1160 1164 case DT_SUNW_TLSSORT:
1161 1165 case DT_PLTPAD:
1162 1166 case DT_MOVETAB:
1163 1167 case DT_SYMINFO:
1164 1168 case DT_RELACOUNT:
1165 1169 case DT_RELCOUNT:
1166 1170 case DT_VERSYM:
1167 1171 case DT_VERDEF:
1168 1172 case DT_VERDEFNUM:
1169 1173 case DT_VERNEED:
1170 1174 (void) printf(pdyn_Fmtptr,
1171 1175 EC_ADDR(p_dyn.d_un.d_ptr));
1172 1176 break;
1173 1177
1174 1178 /*
1175 1179 * Items with a string value
1176 1180 */
1177 1181 case DT_NEEDED:
1178 1182 case DT_SONAME:
1179 1183 case DT_RPATH:
1180 1184 case DT_RUNPATH:
1181 1185 case DT_SUNW_AUXILIARY:
1182 1186 case DT_SUNW_FILTER:
1183 1187 case DT_CONFIG:
1184 1188 case DT_DEPAUDIT:
1185 1189 case DT_AUDIT:
1186 1190 case DT_AUXILIARY:
1187 1191 case DT_USED:
1188 1192 case DT_FILTER:
1189 1193 if (v_flag) { /* Look up the string */
1190 1194 str = (char *)elf_strptr(elf_file, link,
1191 1195 p_dyn.d_un.d_ptr);
1192 1196 if (!(str && *str))
1193 1197 str = (char *)UNKNOWN;
1194 1198 (void) printf("%s", str);
1195 1199 } else { /* Show the address */
1196 1200 (void) printf(pdyn_Fmtptr,
1197 1201 EC_ADDR(p_dyn.d_un.d_ptr));
1198 1202 }
1199 1203 break;
1200 1204
1201 1205 /*
1202 1206 * Items with a literal value
1203 1207 */
1204 1208 case DT_PLTRELSZ:
1205 1209 case DT_RELASZ:
1206 1210 case DT_RELAENT:
1207 1211 case DT_STRSZ:
1208 1212 case DT_SYMENT:
1209 1213 case DT_RELSZ:
1210 1214 case DT_RELENT:
1211 1215 case DT_PLTREL:
1212 1216 case DT_BIND_NOW:
1213 1217 case DT_CHECKSUM:
1214 1218 case DT_PLTPADSZ:
1215 1219 case DT_MOVEENT:
1216 1220 case DT_MOVESZ:
1217 1221 case DT_SYMINSZ:
1218 1222 case DT_SYMINENT:
1219 1223 case DT_VERNEEDNUM:
1220 1224 case DT_SPARC_REGISTER:
1221 1225 case DT_SUNW_SYMSZ:
1222 1226 case DT_SUNW_SORTENT:
1223 1227 case DT_SUNW_SYMSORTSZ:
1224 1228 case DT_SUNW_TLSSORTSZ:
1225 1229 case DT_SUNW_STRPAD:
1226 1230 case DT_SUNW_CAPCHAINENT:
1227 1231 case DT_SUNW_CAPCHAINSZ:
1228 1232 case DT_SUNW_ASLR:
1229 1233 (void) printf(pdyn_Fmtptr,
1230 1234 EC_XWORD(p_dyn.d_un.d_val));
1231 1235 break;
1232 1236
1233 1237 /*
1234 1238 * Integer items that are bitmasks, or which
1235 1239 * can be otherwise formatted in symbolic form.
1236 1240 */
1237 1241 case DT_FLAGS:
1238 1242 case DT_FEATURE_1:
1239 1243 case DT_POSFLAG_1:
1240 1244 case DT_FLAGS_1:
1241 1245 case DT_SUNW_LDMACH:
1242 1246 str = NULL;
1243 1247 if (v_flag) {
1244 1248 switch (p_dyn.d_tag) {
1245 1249 case DT_FLAGS:
1246 1250 str = conv_dyn_flag(
1247 1251 p_dyn.d_un.d_val,
1248 1252 DUMP_CONVFMT,
1249 1253 &conv_buf.dyn_flag);
1250 1254 break;
1251 1255 case DT_FEATURE_1:
1252 1256 str = conv_dyn_feature1(
1253 1257 p_dyn.d_un.d_val,
1254 1258 DUMP_CONVFMT,
1255 1259 &conv_buf.dyn_feature1);
1256 1260 break;
1257 1261 case DT_POSFLAG_1:
1258 1262 str = conv_dyn_posflag1(
1259 1263 p_dyn.d_un.d_val,
1260 1264 DUMP_CONVFMT,
1261 1265 &conv_buf.dyn_posflag1);
1262 1266 break;
1263 1267 case DT_FLAGS_1:
1264 1268 str = conv_dyn_flag1(
1265 1269 p_dyn.d_un.d_val, 0,
1266 1270 &conv_buf.dyn_flag1);
1267 1271 break;
1268 1272 case DT_SUNW_LDMACH:
1269 1273 str = conv_ehdr_mach(
1270 1274 p_dyn.d_un.d_val, 0,
1271 1275 &conv_buf.inv);
1272 1276 break;
1273 1277 }
1274 1278 }
1275 1279 if (str) { /* Show as string */
1276 1280 (void) printf("%s", str);
1277 1281 } else { /* Numeric form */
1278 1282 (void) printf(pdyn_Fmtptr,
1279 1283 EC_ADDR(p_dyn.d_un.d_ptr));
1280 1284 }
1281 1285 break;
1282 1286
1283 1287 /*
1284 1288 * Depreciated items with a literal value
1285 1289 */
1286 1290 case DT_DEPRECATED_SPARC_REGISTER:
1287 1291 (void) printf(pdyn_Fmtptr
1288 1292 " (deprecated value)",
1289 1293 EC_XWORD(p_dyn.d_un.d_val));
1290 1294 break;
1291 1295
1292 1296 /* Ignored items */
1293 1297 case DT_SYMBOLIC:
1294 1298 (void) printf("(ignored)");
1295 1299 break;
1296 1300 }
1297 1301 (void) printf("\n");
1298 1302 (void) gelf_getdyn(dyn_data, ii++, &p_dyn);
1299 1303 }
1300 1304 }
1301 1305
1302 1306 /*
1303 1307 * Check for existence of static shared library information.
1304 1308 */
1305 1309 while (header_num < p_ehdr.e_phnum) {
1306 1310 (void) gelf_getphdr(elf_file, header_num, &p_phdr);
1307 1311 if (p_phdr.p_type == PT_SHLIB) {
1308 1312 while (--lib_scns > 0) {
1309 1313 if (strcmp(l_scns->scn_name, ".lib") == 0) {
1310 1314 print_static(l_scns, filename);
1311 1315 }
1312 1316 l_scns++;
1313 1317 }
1314 1318 }
1315 1319 header_num++;
1316 1320 }
1317 1321 #undef pdyn_Fmtptr
1318 1322 }
1319 1323
1320 1324 /*
1321 1325 * Print the ELF header. Input is an ELF file descriptor
1322 1326 * and the filename. If f_flag is set, the ELF header is
1323 1327 * printed to stdout, otherwise the function returns after
1324 1328 * setting the pointer to the ELF header. Any values which
1325 1329 * are not known are printed in decimal. Fields must be updated
1326 1330 * as new values are added.
1327 1331 */
1328 1332 static GElf_Ehdr *
1329 1333 dump_elf_header(Elf *elf_file, char *filename, GElf_Ehdr * elf_head_p)
1330 1334 {
1331 1335 int class;
1332 1336 int field;
1333 1337
1334 1338 if (gelf_getehdr(elf_file, elf_head_p) == NULL) {
1335 1339 (void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
1336 1340 elf_errmsg(-1));
1337 1341 return (NULL);
1338 1342 }
1339 1343
1340 1344 class = (int)elf_head_p->e_ident[4];
1341 1345
1342 1346 if (class == ELFCLASS64)
1343 1347 field = 21;
1344 1348 else
1345 1349 field = 13;
1346 1350
1347 1351 if (!f_flag)
1348 1352 return (elf_head_p);
1349 1353
1350 1354 if (!p_flag) {
1351 1355 (void) printf("\n **** ELF HEADER ****\n");
1352 1356 (void) printf("%-*s%-11s%-*sMachine Version\n",
1353 1357 field, "Class", "Data", field, "Type");
1354 1358 (void) printf("%-*s%-11s%-*sFlags Ehsize\n",
1355 1359 field, "Entry", "Phoff", field, "Shoff");
1356 1360 (void) printf("%-*s%-11s%-*sShnum Shstrndx\n\n",
1357 1361 field, "Phentsize", "Phnum", field, "Shentsz");
1358 1362 }
1359 1363
1360 1364 if (!v_flag) {
1361 1365 (void) printf("%-*d%-11d%-*d%-12d%d\n",
1362 1366 field, elf_head_p->e_ident[4], elf_head_p->e_ident[5],
1363 1367 field, (int)elf_head_p->e_type, (int)elf_head_p->e_machine,
1364 1368 elf_head_p->e_version);
1365 1369 } else {
1366 1370 Conv_inv_buf_t inv_buf;
1367 1371
1368 1372 (void) printf("%-*s", field,
1369 1373 conv_ehdr_class(class, DUMP_CONVFMT, &inv_buf));
1370 1374 (void) printf("%-11s",
1371 1375 conv_ehdr_data(elf_head_p->e_ident[5], DUMP_CONVFMT,
1372 1376 &inv_buf));
1373 1377 (void) printf("%-*s", field,
1374 1378 conv_ehdr_type(elf_head_p->e_ident[EI_OSABI],
1375 1379 elf_head_p->e_type, DUMP_CONVFMT, &inv_buf));
1376 1380 (void) printf("%-12s",
1377 1381 conv_ehdr_mach(elf_head_p->e_machine, DUMP_CONVFMT,
1378 1382 &inv_buf));
1379 1383 (void) printf("%s\n",
1380 1384 conv_ehdr_vers(elf_head_p->e_version, DUMP_CONVFMT,
1381 1385 &inv_buf));
1382 1386 }
1383 1387 (void) printf("%-#*llx%-#11llx%-#*llx%-#12x%#x\n",
1384 1388 field, EC_ADDR(elf_head_p->e_entry), EC_OFF(elf_head_p->e_phoff),
1385 1389 field, EC_OFF(elf_head_p->e_shoff), EC_WORD(elf_head_p->e_flags),
1386 1390 EC_WORD(elf_head_p->e_ehsize));
1387 1391 if (!v_flag || (elf_head_p->e_shstrndx != SHN_XINDEX)) {
1388 1392 (void) printf("%-#*x%-11u%-#*x%-12u%u\n",
1389 1393 field, EC_WORD(elf_head_p->e_phentsize),
1390 1394 EC_WORD(elf_head_p->e_phnum),
1391 1395 field, EC_WORD(elf_head_p->e_shentsize),
1392 1396 EC_WORD(elf_head_p->e_shnum),
1393 1397 EC_WORD(elf_head_p->e_shstrndx));
1394 1398 } else {
1395 1399 (void) printf("%-#*x%-11u%-#*x%-12uXINDEX\n",
1396 1400 field, EC_WORD(elf_head_p->e_phentsize),
1397 1401 EC_WORD(elf_head_p->e_phnum),
1398 1402 field, EC_WORD(elf_head_p->e_shentsize),
1399 1403 EC_WORD(elf_head_p->e_shnum));
1400 1404 }
1401 1405 if ((elf_head_p->e_shnum == 0) && (elf_head_p->e_shoff > 0)) {
1402 1406 Elf_Scn *scn;
1403 1407 GElf_Shdr shdr0;
1404 1408 int field;
1405 1409
1406 1410 if (gelf_getclass(elf_file) == ELFCLASS64)
1407 1411 field = 21;
1408 1412 else
1409 1413 field = 13;
1410 1414 if (!p_flag) {
1411 1415 (void) printf("\n **** SECTION HEADER[0] "
1412 1416 "{Elf Extensions} ****\n");
1413 1417 (void) printf(
1414 1418 "[No]\tType\tFlags\t%-*s %-*s%-*s%sName\n",
1415 1419 field, "Addr", field, "Offset", field,
1416 1420 "Size(shnum)",
1417 1421 /* compatibility: tab for elf32 */
1418 1422 (field == 13) ? "\t" : " ");
1419 1423 (void) printf("\tLn(strndx) Info\t%-*s Entsize\n",
1420 1424 field, "Adralgn");
1421 1425 }
1422 1426 if ((scn = elf_getscn(elf_file, 0)) == NULL) {
1423 1427 (void) fprintf(stderr,
1424 1428 "%s: %s: elf_getscn failed: %s\n",
1425 1429 prog_name, filename, elf_errmsg(-1));
1426 1430 return (NULL);
1427 1431 }
1428 1432 if (gelf_getshdr(scn, &shdr0) == 0) {
1429 1433 (void) fprintf(stderr,
1430 1434 "%s: %s: gelf_getshdr: %s\n",
1431 1435 prog_name, filename, elf_errmsg(-1));
1432 1436 return (NULL);
1433 1437 }
1434 1438 (void) printf("[0]\t%u\t%llu\t", EC_WORD(shdr0.sh_type),
1435 1439 EC_XWORD(shdr0.sh_flags));
1436 1440
1437 1441 (void) printf("%-#*llx %-#*llx%-*llu%s%-*u\n",
1438 1442 field, EC_ADDR(shdr0.sh_addr),
1439 1443 field, EC_OFF(shdr0.sh_offset),
1440 1444 field, EC_XWORD(shdr0.sh_size),
1441 1445 /* compatibility: tab for elf32 */
1442 1446 ((field == 13) ? "\t" : " "),
1443 1447 field, EC_WORD(shdr0.sh_name));
1444 1448
1445 1449 (void) printf("\t%u\t%u\t%-#*llx %-#*llx\n",
1446 1450 EC_WORD(shdr0.sh_link),
1447 1451 EC_WORD(shdr0.sh_info),
1448 1452 field, EC_XWORD(shdr0.sh_addralign),
1449 1453 field, EC_XWORD(shdr0.sh_entsize));
1450 1454 }
1451 1455 (void) printf("\n");
1452 1456
1453 1457 return (elf_head_p);
1454 1458 }
1455 1459
1456 1460 /*
1457 1461 * Print section contents. Input is an ELF file descriptor,
1458 1462 * the ELF header, the SCNTAB structure,
1459 1463 * the number of symbols, and the filename.
1460 1464 * The number of sections,
1461 1465 * and the offset into the SCNTAB structure will be
1462 1466 * set in dump_section if d_flag or n_flag are set.
1463 1467 * If v_flag is set, sections which can be interpreted will
1464 1468 * be interpreted, otherwise raw data will be output in hexidecimal.
1465 1469 */
1466 1470 static void
1467 1471 print_section(Elf *elf_file,
1468 1472 GElf_Ehdr *p_ehdr, SCNTAB *p, int num_scns, char *filename)
1469 1473 {
1470 1474 unsigned char *p_sec;
1471 1475 int i;
1472 1476 size_t size;
1473 1477
1474 1478 for (i = 0; i < num_scns; i++, p++) {
1475 1479 GElf_Shdr shdr;
1476 1480
1477 1481 size = 0;
1478 1482 if (s_flag && !v_flag)
1479 1483 p_sec = (unsigned char *)get_rawscn(p->p_sd, &size);
1480 1484 else
1481 1485 p_sec = (unsigned char *)get_scndata(p->p_sd, &size);
1482 1486
1483 1487 if ((gelf_getshdr(p->p_sd, &shdr) != NULL) &&
1484 1488 (shdr.sh_type == SHT_NOBITS)) {
1485 1489 continue;
1486 1490 }
1487 1491 if (s_flag && !v_flag) {
1488 1492 (void) printf("\n%s:\n", p->scn_name);
1489 1493 print_rawdata(p_sec, size);
1490 1494 continue;
1491 1495 }
1492 1496 if (shdr.sh_type == SHT_SYMTAB) {
1493 1497 dump_symbol_table(elf_file, p, filename);
1494 1498 continue;
1495 1499 }
1496 1500 if (shdr.sh_type == SHT_DYNSYM) {
1497 1501 dump_symbol_table(elf_file, p, filename);
1498 1502 continue;
1499 1503 }
1500 1504 if (shdr.sh_type == SHT_STRTAB) {
1501 1505 dump_string_table(p, 1);
1502 1506 continue;
1503 1507 }
1504 1508 if (shdr.sh_type == SHT_RELA) {
1505 1509 dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
1506 1510 continue;
1507 1511 }
1508 1512 if (shdr.sh_type == SHT_REL) {
1509 1513 dump_reloc_table(elf_file, p_ehdr, p, 1, filename);
1510 1514 continue;
1511 1515 }
1512 1516 if (shdr.sh_type == SHT_DYNAMIC) {
1513 1517 dump_dynamic(elf_file, p, 1, filename);
1514 1518 continue;
1515 1519 }
1516 1520
1517 1521 (void) printf("\n%s:\n", p->scn_name);
1518 1522 print_rawdata(p_sec, size);
1519 1523 }
1520 1524 (void) printf("\n");
1521 1525 }
1522 1526
1523 1527 /*
1524 1528 * Print section contents. This function does not print the contents
1525 1529 * of the sections but sets up the parameters and then calls
1526 1530 * print_section to print the contents. Calling another function to print
1527 1531 * the contents allows both -d and -n to work correctly
1528 1532 * simultaneously. Input is an ELF file descriptor, the ELF header,
1529 1533 * the SCNTAB structure, the number of sections, and the filename.
1530 1534 * Set the range of sections if d_flag, and set section name if
1531 1535 * n_flag.
1532 1536 */
1533 1537 static void
1534 1538 dump_section(Elf *elf_file,
1535 1539 GElf_Ehdr *p_ehdr, SCNTAB *s, int num_scns, char *filename)
1536 1540 {
1537 1541 SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
1538 1542 int i;
1539 1543 int found_it = 0; /* for use with -n section_name */
1540 1544
1541 1545 if (n_flag) {
1542 1546 n_range = s;
1543 1547
1544 1548 for (i = 0; i < num_scns; i++, n_range++) {
1545 1549 if ((strcmp(name, n_range->scn_name)) != 0)
1546 1550 continue;
1547 1551 else {
1548 1552 found_it = 1;
1549 1553 print_section(elf_file, p_ehdr,
1550 1554 n_range, 1, filename);
1551 1555 }
1552 1556 }
1553 1557
1554 1558 if (!found_it) {
1555 1559 (void) fprintf(stderr, "%s: %s: %s not found\n",
1556 1560 prog_name, filename, name);
1557 1561 }
1558 1562 } /* end n_flag */
1559 1563
1560 1564 if (d_flag) {
1561 1565 d_range = s;
1562 1566 d_num = check_range(d_low, d_hi, num_scns, filename);
1563 1567 if (d_num < 0)
1564 1568 return;
1565 1569 d_range += d_low - 1;
1566 1570
1567 1571 print_section(elf_file, p_ehdr, d_range, d_num, filename);
1568 1572 } /* end d_flag */
1569 1573
1570 1574 if (!n_flag && !d_flag)
1571 1575 print_section(elf_file, p_ehdr, s, num_scns, filename);
1572 1576 }
1573 1577
1574 1578 /*
1575 1579 * Print the section header table. This function does not print the contents
1576 1580 * of the section headers but sets up the parameters and then calls
1577 1581 * print_shdr to print the contents. Calling another function to print
1578 1582 * the contents allows both -d and -n to work correctly
1579 1583 * simultaneously. Input is the SCNTAB structure,
1580 1584 * the number of sections from the ELF header, and the filename.
1581 1585 * Set the range of section headers to print if d_flag, and set
1582 1586 * name of section header to print if n_flag.
1583 1587 */
1584 1588 static void
1585 1589 dump_shdr(Elf *elf_file, SCNTAB *s, int num_scns, char *filename)
1586 1590 {
1587 1591
1588 1592 SCNTAB *n_range, *d_range; /* for use with -n and -d modifiers */
1589 1593 int field;
1590 1594 int i;
1591 1595 int found_it = 0; /* for use with -n section_name */
1592 1596
1593 1597 if (gelf_getclass(elf_file) == ELFCLASS64)
1594 1598 field = 21;
1595 1599 else
1596 1600 field = 13;
1597 1601
1598 1602 if (!p_flag) {
1599 1603 (void) printf("\n **** SECTION HEADER TABLE ****\n");
1600 1604 (void) printf("[No]\tType\tFlags\t%-*s %-*s %-*s%sName\n",
1601 1605 field, "Addr", field, "Offset", field, "Size",
1602 1606 /* compatibility: tab for elf32 */
1603 1607 (field == 13) ? "\t" : " ");
1604 1608 (void) printf("\tLink\tInfo\t%-*s Entsize\n\n",
1605 1609 field, "Adralgn");
1606 1610 }
1607 1611
1608 1612 if (n_flag) {
1609 1613 n_range = s;
1610 1614
1611 1615 for (i = 1; i <= num_scns; i++, n_range++) {
1612 1616 if ((strcmp(name, n_range->scn_name)) != 0)
1613 1617 continue;
1614 1618 else {
1615 1619 found_it = 1;
1616 1620 print_shdr(elf_file, n_range, 1, i);
1617 1621 }
1618 1622 }
1619 1623
1620 1624 if (!found_it) {
1621 1625 (void) fprintf(stderr, "%s: %s: %s not found\n",
1622 1626 prog_name, filename, name);
1623 1627 }
1624 1628 } /* end n_flag */
1625 1629
1626 1630 if (d_flag) {
1627 1631 d_range = s;
1628 1632 d_num = check_range(d_low, d_hi, num_scns, filename);
1629 1633 if (d_num < 0)
1630 1634 return;
1631 1635 d_range += d_low - 1;
1632 1636
1633 1637 print_shdr(elf_file, d_range, d_num, d_low);
1634 1638 } /* end d_flag */
1635 1639
1636 1640 if (!n_flag && !d_flag)
1637 1641 print_shdr(elf_file, s, num_scns, 1);
1638 1642 }
1639 1643
1640 1644 /*
1641 1645 * Process all of the command line options (except
1642 1646 * for -a, -g, -f, and -o). All of the options processed
1643 1647 * by this function require the presence of the section
1644 1648 * header table and will not be processed if it is not present.
1645 1649 * Set up a buffer containing section name, section header,
1646 1650 * and section descriptor for each section in the file. This
1647 1651 * structure is used to avoid duplicate calls to libelf functions.
1648 1652 * Structure members for the symbol table, the debugging information,
1649 1653 * and the line number information are global. All of the
1650 1654 * rest are local.
1651 1655 */
1652 1656 static void
1653 1657 dump_section_table(Elf *elf_file, GElf_Ehdr *elf_head_p, char *filename)
1654 1658 {
1655 1659
1656 1660 static SCNTAB *buffer, *p_scns;
1657 1661 Elf_Scn *scn = 0;
1658 1662 char *s_name = NULL;
1659 1663 int found = 0;
1660 1664 unsigned int num_scns;
1661 1665 size_t shstrndx;
1662 1666 size_t shnum;
1663 1667
1664 1668
1665 1669 if (elf_getshdrnum(elf_file, &shnum) == -1) {
1666 1670 (void) fprintf(stderr,
1667 1671 "%s: %s: elf_getshdrnum failed: %s\n",
1668 1672 prog_name, filename, elf_errmsg(-1));
1669 1673 return;
1670 1674 }
1671 1675 if (elf_getshdrstrndx(elf_file, &shstrndx) == -1) {
1672 1676 (void) fprintf(stderr,
1673 1677 "%s: %s: elf_getshdrstrndx failed: %s\n",
1674 1678 prog_name, filename, elf_errmsg(-1));
1675 1679 return;
1676 1680 }
1677 1681
1678 1682 if ((buffer = calloc(shnum, sizeof (SCNTAB))) == NULL) {
1679 1683 (void) fprintf(stderr, "%s: %s: cannot calloc space\n",
1680 1684 prog_name, filename);
1681 1685 return;
1682 1686 }
1683 1687 /* LINTED */
1684 1688 num_scns = (int)shnum - 1;
1685 1689
1686 1690 p_symtab = (SCNTAB *)0;
1687 1691 p_dynsym = (SCNTAB *)0;
1688 1692 p_scns = buffer;
1689 1693 p_head_scns = buffer;
1690 1694
1691 1695 while ((scn = elf_nextscn(elf_file, scn)) != 0) {
1692 1696 if ((gelf_getshdr(scn, &buffer->p_shdr)) == 0) {
1693 1697 (void) fprintf(stderr,
1694 1698 "%s: %s: %s\n", prog_name, filename,
1695 1699 elf_errmsg(-1));
1696 1700 return;
1697 1701 }
1698 1702 s_name = (char *)
1699 1703 elf_strptr(elf_file, shstrndx, buffer->p_shdr.sh_name);
1700 1704 buffer->scn_name = s_name ? s_name : (char *)UNKNOWN;
1701 1705 buffer->p_sd = scn;
1702 1706
1703 1707 if (buffer->p_shdr.sh_type == SHT_SYMTAB) {
1704 1708 found += 1;
1705 1709 p_symtab = buffer;
1706 1710 }
1707 1711 if (buffer->p_shdr.sh_type == SHT_DYNSYM)
1708 1712 p_dynsym = buffer;
1709 1713 buffer++;
1710 1714 }
1711 1715
1712 1716 /*
1713 1717 * These functions depend upon the presence of the section header table
1714 1718 * and will not be invoked in its absence
1715 1719 */
1716 1720 if (h_flag) {
1717 1721 dump_shdr(elf_file, p_scns, num_scns, filename);
1718 1722 }
1719 1723 if (p_symtab && (t_flag || T_flag)) {
1720 1724 dump_symbol_table(elf_file, p_symtab, filename);
1721 1725 }
1722 1726 if (c_flag) {
1723 1727 dump_string_table(p_scns, num_scns);
1724 1728 }
1725 1729 if (r_flag) {
1726 1730 dump_reloc_table(elf_file, elf_head_p,
1727 1731 p_scns, num_scns, filename);
1728 1732 }
1729 1733 if (L_flag) {
1730 1734 dump_dynamic(elf_file, p_scns, num_scns, filename);
1731 1735 }
1732 1736 if (s_flag) {
1733 1737 dump_section(elf_file, elf_head_p, p_scns,
1734 1738 num_scns, filename);
1735 1739 }
1736 1740 }
1737 1741
1738 1742 /*
1739 1743 * Load the archive string table(s) (for extended-length strings)
1740 1744 * into an in-core table/list
1741 1745 */
1742 1746 static struct stab_list_s *
1743 1747 load_arstring_table(struct stab_list_s *STabList,
1744 1748 int fd, Elf *elf_file, Elf_Arhdr *p_ar, char *filename)
1745 1749 {
1746 1750 off_t here;
1747 1751 struct stab_list_s *STL_entry, *STL_next;
1748 1752
1749 1753 if (p_ar) {
1750 1754 STL_entry = malloc(sizeof (struct stab_list_s));
1751 1755 STL_entry->next = 0;
1752 1756 STL_entry->strings = 0;
1753 1757 STL_entry->size = 0;
1754 1758
1755 1759 if (!STabList)
1756 1760 STabList = STL_entry;
1757 1761 else {
1758 1762 STL_next = STabList;
1759 1763 while (STL_next->next != (void *)0)
1760 1764 STL_next = STL_next->next;
1761 1765 STL_next->next = STL_entry;
1762 1766 }
1763 1767
1764 1768 STL_entry->size = p_ar->ar_size;
1765 1769 STL_entry->strings = malloc(p_ar->ar_size);
1766 1770 here = elf_getbase(elf_file);
1767 1771 if ((lseek(fd, here, 0)) != here) {
1768 1772 (void) fprintf(stderr,
1769 1773 "%s: %s: could not lseek\n", prog_name, filename);
1770 1774 }
1771 1775
1772 1776 if ((read(fd, STL_entry->strings, p_ar->ar_size)) == -1) {
1773 1777 (void) fprintf(stderr,
1774 1778 "%s: %s: could not read\n", prog_name, filename);
1775 1779 }
1776 1780 }
1777 1781 return (STabList);
1778 1782 }
1779 1783
1780 1784 /*
1781 1785 * Print the archive header for each member of an archive.
1782 1786 * Also call ar_sym_read to print the symbols in the
1783 1787 * archive symbol table if g_flag. Input is a file descriptor,
1784 1788 * an ELF file descriptor, and the filename. Putting the call
1785 1789 * to dump the archive symbol table in this function is more
1786 1790 * efficient since it is necessary to examine the archive member
1787 1791 * name in the archive header to determine which member is the
1788 1792 * symbol table.
1789 1793 */
1790 1794 static void
1791 1795 dump_ar_hdr(int fd, Elf *elf_file, char *filename)
1792 1796 {
1793 1797 extern int v_flag, g_flag, a_flag, p_flag;
1794 1798 Elf_Arhdr *p_ar;
1795 1799 Elf *arf;
1796 1800 Elf_Cmd cmd;
1797 1801 int title = 0;
1798 1802 int err = 0;
1799 1803
1800 1804 char buf[DATESIZE];
1801 1805
1802 1806 cmd = ELF_C_READ;
1803 1807 while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
1804 1808 p_ar = elf_getarhdr(arf);
1805 1809 if (p_ar == NULL) {
1806 1810 (void) fprintf(stderr,
1807 1811 "%s: %s: %s\n", prog_name, filename,
1808 1812 elf_errmsg(-1));
1809 1813 continue;
1810 1814 }
1811 1815 if ((strcmp(p_ar->ar_name, "/") == 0) ||
1812 1816 (strcmp(p_ar->ar_name, "/SYM64/") == 0)) {
1813 1817 if (g_flag)
1814 1818 ar_sym_read(elf_file, filename);
1815 1819 } else if (strcmp(p_ar->ar_name, "//") == 0) {
1816 1820 StringTableList = load_arstring_table(
1817 1821 StringTableList, fd, arf, p_ar, filename);
1818 1822 cmd = elf_next(arf);
1819 1823 (void) elf_end(arf);
1820 1824 continue;
1821 1825 } else {
1822 1826 if (a_flag) {
1823 1827 (void) printf("%s[%s]:\n", filename,
1824 1828 p_ar->ar_name);
1825 1829 if (!p_flag && title == 0) {
1826 1830 if (!v_flag)
1827 1831 (void) printf(
1828 1832 "\n\n\t\t\t***ARCHIVE HEADER***"
1829 1833 "\n Date Uid Gid Mode Size Member Name\n\n");
1830 1834 else
1831 1835 (void) printf(
1832 1836 "\n\n\t\t\t***ARCHIVE HEADER***"
1833 1837 "\n Date Uid Gid Mode Size Member Name\n\n");
1834 1838 title = 1;
1835 1839 }
1836 1840 if (!v_flag) {
1837 1841 (void) printf(
1838 1842 "\t0x%.8lx %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
1839 1843 p_ar->ar_date, (int)p_ar->ar_uid,
1840 1844 (int)p_ar->ar_gid,
1841 1845 (int)p_ar->ar_mode,
1842 1846 p_ar->ar_size, p_ar->ar_name);
1843 1847 } else {
1844 1848 if ((strftime(buf, DATESIZE,
1845 1849 "%b %d %H:%M:%S %Y",
1846 1850 localtime(
1847 1851 &(p_ar->ar_date)))) == 0) {
1848 1852 (void) fprintf(stderr,
1849 1853 "%s: %s: don't have enough space to store the date\n", prog_name, filename);
1850 1854 exit(1);
1851 1855 }
1852 1856 (void) printf(
1853 1857 "\t%s %6d %6d 0%.6ho 0x%.8lx %-s\n\n",
1854 1858 buf, (int)p_ar->ar_uid,
1855 1859 (int)p_ar->ar_gid,
1856 1860 (int)p_ar->ar_mode,
1857 1861 p_ar->ar_size, p_ar->ar_name);
1858 1862 }
1859 1863 }
1860 1864 }
1861 1865 cmd = elf_next(arf);
1862 1866 (void) elf_end(arf);
1863 1867 } /* end while */
1864 1868
1865 1869 err = elf_errno();
1866 1870 if (err != 0) {
1867 1871 (void) fprintf(stderr,
1868 1872 "%s: %s: %s\n", prog_name, filename, elf_errmsg(err));
1869 1873 }
1870 1874 }
1871 1875
1872 1876 /*
1873 1877 * Process member files of an archive. This function provides
1874 1878 * a loop through an archive equivalent the processing of
1875 1879 * each_file for individual object files.
1876 1880 */
1877 1881 static void
1878 1882 dump_ar_files(int fd, Elf *elf_file, char *filename)
1879 1883 {
1880 1884 Elf_Arhdr *p_ar;
1881 1885 Elf *arf;
1882 1886 Elf_Cmd cmd;
1883 1887 Elf_Kind file_type;
1884 1888 GElf_Ehdr elf_head;
1885 1889 char *fullname;
1886 1890
1887 1891 cmd = ELF_C_READ;
1888 1892 while ((arf = elf_begin(fd, cmd, elf_file)) != 0) {
1889 1893 size_t len;
1890 1894
1891 1895 p_ar = elf_getarhdr(arf);
1892 1896 if (p_ar == NULL) {
1893 1897 (void) fprintf(stderr, "%s: %s: %s\n",
1894 1898 prog_name, filename, elf_errmsg(-1));
1895 1899 return;
1896 1900 }
1897 1901 if (p_ar->ar_name[0] == '/') {
1898 1902 cmd = elf_next(arf);
1899 1903 (void) elf_end(arf);
1900 1904 continue;
1901 1905 }
1902 1906
1903 1907 len = strlen(filename) + strlen(p_ar->ar_name) + 3;
1904 1908 if ((fullname = malloc(len)) == NULL)
1905 1909 return;
1906 1910 (void) snprintf(fullname, len, "%s[%s]", filename,
1907 1911 p_ar->ar_name);
1908 1912 (void) printf("\n%s:\n", fullname);
1909 1913 file_type = elf_kind(arf);
1910 1914 if (file_type == ELF_K_ELF) {
1911 1915 if (dump_elf_header(arf, fullname, &elf_head) == NULL)
1912 1916 return;
1913 1917 if (o_flag)
1914 1918 dump_exec_header(arf,
1915 1919 (unsigned)elf_head.e_phnum, fullname);
1916 1920 if (x_flag)
1917 1921 dump_section_table(arf, &elf_head, fullname);
1918 1922 } else {
1919 1923 (void) fprintf(stderr, "%s: %s: invalid file type\n",
1920 1924 prog_name, fullname);
1921 1925 cmd = elf_next(arf);
1922 1926 (void) elf_end(arf);
1923 1927 continue;
1924 1928 }
1925 1929
1926 1930 cmd = elf_next(arf);
1927 1931 (void) elf_end(arf);
1928 1932 } /* end while */
1929 1933 }
1930 1934
1931 1935 /*
1932 1936 * Takes a filename as input. Test first for a valid version
1933 1937 * of libelf.a and exit on error. Process each valid file
1934 1938 * or archive given as input on the command line. Check
1935 1939 * for file type. If it is an archive, process the archive-
1936 1940 * specific options first, then files within the archive.
1937 1941 * If it is an ELF object file, process it; otherwise
1938 1942 * warn that it is an invalid file type.
1939 1943 * All options except the archive-specific and program
1940 1944 * execution header are processed in the function, dump_section_table.
1941 1945 */
1942 1946 static void
1943 1947 each_file(char *filename)
1944 1948 {
1945 1949 Elf *elf_file;
1946 1950 GElf_Ehdr elf_head;
1947 1951 int fd;
1948 1952 Elf_Kind file_type;
1949 1953
1950 1954 struct stat buf;
1951 1955
1952 1956 Elf_Cmd cmd;
1953 1957 errno = 0;
1954 1958
1955 1959 if (stat(filename, &buf) == -1) {
1956 1960 int err = errno;
1957 1961 (void) fprintf(stderr, "%s: %s: %s", prog_name, filename,
1958 1962 strerror(err));
1959 1963 return;
1960 1964 }
1961 1965
1962 1966 if ((fd = open((filename), O_RDONLY)) == -1) {
1963 1967 (void) fprintf(stderr, "%s: %s: cannot read\n", prog_name,
1964 1968 filename);
1965 1969 return;
1966 1970 }
1967 1971 cmd = ELF_C_READ;
1968 1972 if ((elf_file = elf_begin(fd, cmd, (Elf *)0)) == NULL) {
1969 1973 (void) fprintf(stderr, "%s: %s: %s\n", prog_name, filename,
1970 1974 elf_errmsg(-1));
1971 1975 return;
1972 1976 }
1973 1977
1974 1978 file_type = elf_kind(elf_file);
1975 1979 if (file_type == ELF_K_AR) {
1976 1980 if (a_flag || g_flag) {
1977 1981 dump_ar_hdr(fd, elf_file, filename);
1978 1982 elf_file = elf_begin(fd, cmd, (Elf *)0);
1979 1983 }
1980 1984 if (z_flag)
1981 1985 dump_ar_files(fd, elf_file, filename);
1982 1986 } else {
1983 1987 if (file_type == ELF_K_ELF) {
1984 1988 (void) printf("\n%s:\n", filename);
1985 1989 if (dump_elf_header(elf_file, filename, &elf_head)) {
1986 1990 if (o_flag)
1987 1991 dump_exec_header(elf_file,
1988 1992 (unsigned)elf_head.e_phnum,
1989 1993 filename);
1990 1994 if (x_flag)
1991 1995 dump_section_table(elf_file,
1992 1996 &elf_head, filename);
1993 1997 }
1994 1998 } else {
1995 1999 (void) fprintf(stderr, "%s: %s: invalid file type\n",
1996 2000 prog_name, filename);
1997 2001 }
1998 2002 }
1999 2003 (void) elf_end(elf_file);
2000 2004 (void) close(fd);
2001 2005 }
2002 2006
2003 2007 /*
2004 2008 * Sets up flags for command line options given and then
2005 2009 * calls each_file() to process each file.
2006 2010 */
2007 2011 int
2008 2012 main(int argc, char *argv[], char *envp[])
2009 2013 {
2010 2014 char *optstr = OPTSTR; /* option string used by getopt() */
2011 2015 int optchar;
2012 2016
2013 2017 /*
2014 2018 * Check for a binary that better fits this architecture.
2015 2019 */
2016 2020 (void) conv_check_native(argv, envp);
2017 2021
2018 2022 prog_name = argv[0];
2019 2023
2020 2024 (void) setlocale(LC_ALL, "");
2021 2025 while ((optchar = getopt(argc, argv, optstr)) != -1) {
2022 2026 switch (optchar) {
2023 2027 case 'a':
2024 2028 a_flag = 1;
2025 2029 x_flag = 1;
2026 2030 break;
2027 2031 case 'g':
2028 2032 g_flag = 1;
2029 2033 x_flag = 1;
2030 2034 break;
2031 2035 case 'v':
2032 2036 v_flag = 1;
2033 2037 break;
2034 2038 case 'p':
2035 2039 p_flag = 1;
2036 2040 break;
2037 2041 case 'f':
2038 2042 f_flag = 1;
2039 2043 z_flag = 1;
2040 2044 break;
2041 2045 case 'o':
2042 2046 o_flag = 1;
2043 2047 z_flag = 1;
2044 2048 break;
2045 2049 case 'h':
2046 2050 h_flag = 1;
2047 2051 x_flag = 1;
2048 2052 z_flag = 1;
2049 2053 break;
2050 2054 case 's':
2051 2055 s_flag = 1;
2052 2056 x_flag = 1;
2053 2057 z_flag = 1;
2054 2058 break;
2055 2059 case 'd':
2056 2060 d_flag = 1;
2057 2061 x_flag = 1;
2058 2062 z_flag = 1;
2059 2063 set_range(optarg, &d_low, &d_hi);
2060 2064 break;
2061 2065 case 'n':
2062 2066 n_flag++;
2063 2067 x_flag = 1;
2064 2068 z_flag = 1;
2065 2069 name = optarg;
2066 2070 break;
2067 2071 case 'r':
2068 2072 r_flag = 1;
2069 2073 x_flag = 1;
2070 2074 z_flag = 1;
2071 2075 break;
2072 2076 case 't':
2073 2077 t_flag = 1;
2074 2078 x_flag = 1;
2075 2079 z_flag = 1;
2076 2080 break;
2077 2081 case 'C':
2078 2082 C_flag = 1;
2079 2083 t_flag = 1;
2080 2084 x_flag = 1;
2081 2085 z_flag = 1;
2082 2086 break;
2083 2087 case 'T':
2084 2088 T_flag = 1;
2085 2089 x_flag = 1;
2086 2090 z_flag = 1;
2087 2091 set_range(optarg, &T_low, &T_hi);
2088 2092 break;
2089 2093 case 'c':
2090 2094 c_flag = 1;
2091 2095 x_flag = 1;
2092 2096 z_flag = 1;
2093 2097 break;
2094 2098 case 'L':
2095 2099 L_flag = 1;
2096 2100 x_flag = 1;
2097 2101 z_flag = 1;
2098 2102 break;
2099 2103 case 'V':
2100 2104 V_flag = 1;
2101 2105 (void) fprintf(stderr, "dump: %s %s\n",
2102 2106 (const char *)SGU_PKG,
2103 2107 (const char *)SGU_REL);
2104 2108 break;
2105 2109 case '?':
2106 2110 errflag += 1;
2107 2111 break;
2108 2112 default:
2109 2113 break;
2110 2114 }
2111 2115 }
2112 2116
2113 2117 if (errflag || (optind >= argc) || (!z_flag && !x_flag)) {
2114 2118 if (!(V_flag && (argc == 2))) {
2115 2119 usage();
2116 2120 exit(269);
2117 2121 }
2118 2122 }
2119 2123
2120 2124 if (elf_version(EV_CURRENT) == EV_NONE) {
2121 2125 (void) fprintf(stderr, "%s: libelf is out of date\n",
2122 2126 prog_name);
2123 2127 exit(101);
2124 2128 }
2125 2129
2126 2130 while (optind < argc) {
2127 2131 each_file(argv[optind]);
2128 2132 optind++;
2129 2133 }
2130 2134 return (0);
2131 2135 }
↓ open down ↓ |
1592 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX