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