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