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