]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - binutils/readelf.c
Tidy up formatting of --help output.
[thirdparty/binutils-gdb.git] / binutils / readelf.c
index cd97886a4fd6202552ea02332ea6d9d4248b2832..2480c7659eac8c1717c543736b05dba485ad084f 100644 (file)
@@ -1,5 +1,5 @@
 /* readelf.c -- display contents of an ELF format file
-   Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
    Originally developed by Eric Youngdale <eric@andante.jic.com>
    Modifications by Nick Clifton <nickc@redhat.com>
@@ -153,11 +153,13 @@ print_mode;
 
 /* Forward declarations for dumb compilers.  */
 static void              print_vma                   PARAMS ((bfd_vma, print_mode));
+static void              print_symbol                PARAMS ((int, char *));
 static bfd_vma (*         byte_get)                   PARAMS ((unsigned char *, int));
 static bfd_vma            byte_get_little_endian      PARAMS ((unsigned char *, int));
 static bfd_vma            byte_get_big_endian         PARAMS ((unsigned char *, int));
 static const char *       get_mips_dynamic_type       PARAMS ((unsigned long));
 static const char *       get_sparc64_dynamic_type    PARAMS ((unsigned long));
+static const char *       get_ppc64_dynamic_type      PARAMS ((unsigned long));
 static const char *       get_parisc_dynamic_type     PARAMS ((unsigned long));
 static const char *       get_dynamic_type            PARAMS ((unsigned long));
 static int               slurp_rela_relocs           PARAMS ((FILE *, unsigned long, unsigned long, Elf_Internal_Rela **, unsigned long *));
@@ -246,7 +248,8 @@ static const char *       get_elf_class               PARAMS ((unsigned int));
 static const char *       get_data_encoding           PARAMS ((unsigned int));
 static const char *       get_osabi_name              PARAMS ((unsigned int));
 static int               guess_is_rela               PARAMS ((unsigned long));
-static char *            get_note_type                  PARAMS ((unsigned int));
+static const char *      get_note_type                  PARAMS ((unsigned int));
+static const char *      get_netbsd_elfcore_note_type   PARAMS ((unsigned int));
 static int               process_note                   PARAMS ((Elf32_Internal_Note *));
 static int               process_corefile_note_segment  PARAMS ((FILE *, bfd_vma, bfd_vma));
 static int               process_corefile_note_segments PARAMS ((FILE *));
@@ -511,6 +514,25 @@ print_vma (vma, mode)
 #endif
 }
 
+/* Display a symbol on stdout.  If do_wide is not true then
+   format the symbol to be at most WIDTH characters,
+   truhncating as necessary.  If WIDTH is negative then
+   format the string to be exactly - WIDTH characters,
+   truncating or padding as necessary.  */
+
+static void
+print_symbol (width, symbol)
+     int width;
+     char * symbol;
+{
+  if (do_wide)
+    printf (symbol);
+  else if (width < 0)
+    printf ("%-*.*s", width, width, symbol);
+  else 
+    printf ("%-.*s", width, symbol);
+}
+
 static bfd_vma
 byte_get_big_endian (field, size)
      unsigned char * field;
@@ -591,6 +613,7 @@ guess_is_rela (e_machine)
     case EM_SPARCV9:
     case EM_SPARC:
     case EM_PPC:
+    case EM_PPC64:
     case EM_V850:
     case EM_CYGNUS_V850:
     case EM_D30V:
@@ -1058,12 +1081,11 @@ dump_relocations (file, rel_offset, rel_size, symtab, nsyms, strtab, is_rela)
              printf ("  ");
 
              if (psym->st_name == 0)
-               printf ("%-25.25s",
-                       SECTION_NAME (SECTION_HEADER (psym->st_shndx)));
+               print_symbol (-25, SECTION_NAME (section_headers + psym->st_shndx));
              else if (strtab == NULL)
                printf (_("<string table index %3ld>"), psym->st_name);
              else
-               printf ("%-25.25s", strtab + psym->st_name);
+               print_symbol (-25, strtab + psym->st_name);
 
              if (is_rela)
                printf (" + %lx", (unsigned long) relas [i].r_addend);
@@ -1156,6 +1178,18 @@ get_sparc64_dynamic_type (type)
     }
 }
 
+static const char *
+get_ppc64_dynamic_type (type)
+     unsigned long type;
+{
+  switch (type)
+    {
+    case DT_PPC64_GLINK: return "PPC64_GLINK";
+    default:
+      return NULL;
+    }
+}
+
 static const char *
 get_parisc_dynamic_type (type)
      unsigned long type;
@@ -1268,6 +1302,9 @@ get_dynamic_type (type)
            case EM_SPARCV9:
              result = get_sparc64_dynamic_type (type);
              break;
+           case EM_PPC64:
+             result = get_ppc64_dynamic_type (type);
+             break;
            default:
              result = NULL;
              break;
@@ -1354,6 +1391,7 @@ get_machine_name (e_machine)
     case EM_SPARC32PLUS:       return "Sparc v8+" ;
     case EM_960:               return "Intel 90860";
     case EM_PPC:               return "PowerPC";
+    case EM_PPC64:             return "PowerPC64";
     case EM_V800:              return "NEC V800";
     case EM_FR20:              return "Fujitsu FR20";
     case EM_RH32:              return "TRW RH32";
@@ -1647,30 +1685,12 @@ get_machine_flags (e_flags, e_machine)
          if (e_flags & EF_MIPS_ABI2)
            strcat (buf, ", abi2");
 
+         if (e_flags & EF_MIPS_OPTIONS_FIRST)
+           strcat (buf, ", odk first");
+
          if (e_flags & EF_MIPS_32BITMODE)
            strcat (buf, ", 32bitmode");
 
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
-           strcat (buf, ", mips1");
-
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
-           strcat (buf, ", mips2");
-
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
-           strcat (buf, ", mips3");
-
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
-           strcat (buf, ", mips4");
-
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
-           strcat (buf, ", mips5");
-
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
-           strcat (buf, ", mips32");
-
-         if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
-           strcat (buf, ", mips64");
-
          switch ((e_flags & EF_MIPS_MACH))
            {
            case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
@@ -1679,8 +1699,47 @@ get_machine_flags (e_flags, e_machine)
            case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
            case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
            case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
-           default: strcat (buf, " UNKNOWN"); break;
+           case 0:
+           /* We simply ignore the field in this case to avoid confusion:
+              MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
+              extension.  */
+             break;
+           default: strcat (buf, ", unknown CPU"); break;
+           }
+
+         switch ((e_flags & EF_MIPS_ABI))
+           {
+           case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
+           case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
+           case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
+           case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
+           case 0:
+           /* We simply ignore the field in this case to avoid confusion:
+              MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
+              This means it is likely to be an o32 file, but not for
+              sure.  */
+             break;
+           default: strcat (buf, ", unknown ABI"); break;
+           }
+
+         if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
+           strcat (buf, ", mdmx");
+
+         if (e_flags & EF_MIPS_ARCH_ASE_M16)
+           strcat (buf, ", mips16");
+
+         switch ((e_flags & EF_MIPS_ARCH))
+           {
+           case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
+           case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
+           case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
+           case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
+           case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
+           case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
+           case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
+           default: strcat (buf, ", unknown ISA"); break;
            }
+
          break;
 
        case EM_SPARCV9:
@@ -1821,6 +1880,10 @@ get_ia64_segment_type (type)
     {
     case PT_IA_64_ARCHEXT:     return "IA_64_ARCHEXT";
     case PT_IA_64_UNWIND:      return "IA_64_UNWIND";
+    case PT_HP_TLS:            return "HP_TLS";
+    case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
+    case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
+    case PT_IA_64_HP_STACK:    return "HP_STACK";
     default:
       break;
     }
@@ -1883,6 +1946,9 @@ get_segment_type (p_type)
            case EM_PARISC:
              result = get_parisc_segment_type (p_type);
              break;
+           case EM_IA_64:
+             result = get_ia64_segment_type (p_type);
+             break;
            default:
              result = NULL;
              break;
@@ -2085,35 +2151,38 @@ struct option options [] =
 static void
 usage ()
 {
-  fprintf (stdout, _("Usage: readelf {options} elf-file(s)\n"));
-  fprintf (stdout, _("  Options are:\n"));
-  fprintf (stdout, _("  -a or --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
-  fprintf (stdout, _("  -h or --file-header       Display the ELF file header\n"));
-  fprintf (stdout, _("  -l or --program-headers or --segments\n"));
-  fprintf (stdout, _("                            Display the program headers\n"));
-  fprintf (stdout, _("  -S or --section-headers or --sections\n"));
-  fprintf (stdout, _("                            Display the sections' header\n"));
-  fprintf (stdout, _("  -e or --headers           Equivalent to: -h -l -S\n"));
-  fprintf (stdout, _("  -s or --syms or --symbols Display the symbol table\n"));
-  fprintf (stdout, _("  -n or --notes             Display the core notes (if present)\n"));
-  fprintf (stdout, _("  -r or --relocs            Display the relocations (if present)\n"));
-  fprintf (stdout, _("  -u or --unwind            Display the unwind info (if present)\n"));
-  fprintf (stdout, _("  -d or --dynamic           Display the dynamic segment (if present)\n"));
-  fprintf (stdout, _("  -V or --version-info      Display the version sections (if present)\n"));
-  fprintf (stdout, _("  -A or --arch-specific     Display architecture specific information (if any).\n"));
-  fprintf (stdout, _("  -D or --use-dynamic       Use the dynamic section info when displaying symbols\n"));
-  fprintf (stdout, _("  -x <number> or --hex-dump=<number>\n"));
-  fprintf (stdout, _("                            Dump the contents of section <number>\n"));
-  fprintf (stdout, _("  -w[liaprmfs] or --debug-dump[=line,=info,=abbrev,=pubnames,=ranges,=macro,=frames,=str]\n"));
-  fprintf (stdout, _("                            Display the contents of DWARF2 debug sections\n"));
+  fprintf (stdout, _("Usage: readelf <option(s)> elf-file(s)\n"));
+  fprintf (stdout, _(" Display information about the contents of ELF format files\n"));
+  fprintf (stdout, _(" Options are:\n\
+  -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
+  -h --file-header       Display the ELF file header\n\
+  -l --program-headers   Display the program headers\n\
+     --segments          An alias for --program-headers\n\
+  -S --section-headers   Display the sections' header\n\
+     --sections          An alias for --section-headers\n\
+  -e --headers           Equivalent to: -h -l -S\n\
+  -s --syms              Display the symbol table\n\
+      --symbols          An alias for --syms\n\
+  -n --notes             Display the core notes (if present)\n\
+  -r --relocs            Display the relocations (if present)\n\
+  -u --unwind            Display the unwind info (if present)\n\
+  -d --dynamic           Display the dynamic segment (if present)\n\
+  -V --version-info      Display the version sections (if present)\n\
+  -A --arch-specific     Display architecture specific information (if any).\n\
+  -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
+  -x --hex-dump=<number> Dump the contents of section <number>\n\
+  -w --debug-dump[=line,=info,=abbrev,=pubnames,=ranges,=macro,=frames,=str]\n\
+                         Display the contents of DWARF2 debug sections\n"));
 #ifdef SUPPORT_DISASSEMBLY
-  fprintf (stdout, _("  -i <number> or --instruction-dump=<number>\n"));
-  fprintf (stdout, _("                            Disassemble the contents of section <number>\n"));
+  fprintf (stdout, _("\
+  -i --instruction-dump=<number>\n\
+                         Disassemble the contents of section <number>\n"));
 #endif
-  fprintf (stdout, _("  -I or --histogram         Display histogram of bucket list lengths\n"));
-  fprintf (stdout, _("  -v or --version           Display the version number of readelf\n"));
-  fprintf (stdout, _("  -W or --wide              Don't split lines to fit into 80 columns\n"));
-  fprintf (stdout, _("  -H or --help              Display this information\n"));
+  fprintf (stdout, _("\
+  -I --histogram         Display histogram of bucket list lengths\n\
+  -W --wide              Allow output width to exceed 80 characters\n\
+  -H --help              Display this information\n\
+  -v --version           Display the version number of readelf\n"));
   fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
 
   exit (0);
@@ -5284,8 +5353,9 @@ process_symbol_table (file)
              printf ("  %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
              printf (" %6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
              printf (" %3s",  get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
-             printf (" %3.3s", get_symbol_index_type (psym->st_shndx));
-             printf (" %s\n", dynamic_strings + psym->st_name);
+             printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
+             print_symbol (25, dynamic_strings + psym->st_name);
+             putchar ('\n');
            }
        }
     }
@@ -5343,8 +5413,8 @@ process_symbol_table (file)
              printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
              printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
              printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
-             printf (" %4s", get_symbol_index_type (psym->st_shndx));
-             printf (" %s", strtab + psym->st_name);
+             printf (" %4s ", get_symbol_index_type (psym->st_shndx));
+             print_symbol (25, strtab + psym->st_name);
 
              if (section->sh_type == SHT_DYNSYM &&
                  version_info [DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
@@ -5581,8 +5651,9 @@ process_syminfo (file)
     {
       unsigned short int flags = dynamic_syminfo[i].si_flags;
 
-      printf ("%4d: %-30s ", i,
-             dynamic_strings + dynamic_symbols[i].st_name);
+      printf ("%4d: ", i);
+      print_symbol (30, dynamic_strings + dynamic_symbols[i].st_name);
+      putchar (' ');
 
       switch (dynamic_syminfo[i].si_boundto)
        {
@@ -5595,9 +5666,12 @@ process_syminfo (file)
        default:
          if (dynamic_syminfo[i].si_boundto > 0
              && dynamic_syminfo[i].si_boundto < dynamic_size)
-           printf ("%-10s ",
-                   dynamic_strings
-                   + dynamic_segment[dynamic_syminfo[i].si_boundto].d_un.d_val);
+           {
+             print_symbol (10, dynamic_strings
+                           + dynamic_segment
+                           [dynamic_syminfo[i].si_boundto].d_un.d_val);
+             putchar (' ' );
+           }
          else
            printf ("%-10d ", dynamic_syminfo[i].si_boundto);
          break;
@@ -8687,9 +8761,10 @@ process_mips_specific (file)
                       tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
                       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 
-             printf ("%3lu: %-20s %s %#10lx %-7ld", (unsigned long) cnt,
-                     dynamic_strings + liblist.l_name, timebuf,
-                     liblist.l_checksum, liblist.l_version);
+             printf ("%3lu: ", (unsigned long) cnt);
+             print_symbol (20, dynamic_strings + liblist.l_name);
+             printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
+                     liblist.l_version);
 
              if (liblist.l_flags == 0)
                puts (" NONE");
@@ -8987,11 +9062,13 @@ process_mips_specific (file)
 
       for (cnt = 0; cnt < conflictsno; ++cnt)
        {
-         Elf_Internal_Sym * psym = &dynamic_symbols[iconf[cnt]];
+         Elf_Internal_Sym * psym = & dynamic_symbols [iconf [cnt]];
 
-         printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
+         printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf [cnt]);
          print_vma (psym->st_value, FULL_HEX);
-         printf ("  %s\n", dynamic_strings + psym->st_name);
+         putchar (' ');
+         print_symbol (25, dynamic_strings + psym->st_name);
+         putchar ('\n');
        }
 
       free (iconf);
@@ -9000,7 +9077,7 @@ process_mips_specific (file)
   return 1;
 }
 
-static char *
+static const char *
 get_note_type (e_type)
      unsigned e_type;
 {
@@ -9025,6 +9102,68 @@ get_note_type (e_type)
     }
 }
 
+static const char *
+get_netbsd_elfcore_note_type (e_type)
+     unsigned e_type;
+{
+  static char buff[64];
+
+  if (e_type == NT_NETBSDCORE_PROCINFO)
+    {
+      /* NetBSD core "procinfo" structure.  */
+      return _("NetBSD procinfo structure");
+    }
+
+  /* As of Jan 2002 there are no other machine-independent notes
+     defined for NetBSD core files.  If the note type is less
+     than the start of the machine-dependent note types, we don't
+     understand it.  */
+
+  if (e_type < NT_NETBSDCORE_FIRSTMACH)
+    {
+      sprintf (buff, _("Unknown note type: (0x%08x)"), e_type);
+      return buff;
+    }
+
+  switch (elf_header.e_machine)
+    {
+    /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
+       and PT_GETFPREGS == mach+2.  */
+
+    case EM_OLD_ALPHA:
+    case EM_ALPHA:
+    case EM_SPARC:
+    case EM_SPARC32PLUS:
+    case EM_SPARCV9:
+      switch (e_type)
+       {
+       case NT_NETBSDCORE_FIRSTMACH+0:
+         return _("PT_GETREGS (reg structure)");
+       case NT_NETBSDCORE_FIRSTMACH+2:
+         return _("PT_GETFPREGS (fpreg structure)");
+       default:
+         break;
+       }
+      break;
+
+    /* On all other arch's, PT_GETREGS == mach+1 and
+       PT_GETFPREGS == mach+3.  */
+    default:
+      switch (e_type)
+       {
+       case NT_NETBSDCORE_FIRSTMACH+1:
+         return _("PT_GETREGS (reg structure)");
+       case NT_NETBSDCORE_FIRSTMACH+3:
+         return _("PT_GETFPREGS (fpreg structure)");
+       default:
+         break;
+       }
+    }
+
+  sprintf (buff, _("PT_FIRSTMACH+%d"), e_type - NT_NETBSDCORE_FIRSTMACH);
+  return buff;
+}
+
 /* Note that by the ELF standard, the name field is already null byte
    terminated, and namesz includes the terminating null byte.
    I.E. the value of namesz for the name "FSF" is 4.
@@ -9034,9 +9173,29 @@ static int
 process_note (pnote)
   Elf32_Internal_Note * pnote;
 {
+  const char *nt;
+
+  if (pnote->namesz == 0)
+    {
+      /* If there is no note name, then use the default set of
+        note type strings.  */
+      nt = get_note_type (pnote->type);
+    }
+  else if (strncmp (pnote->namedata, "NetBSD-CORE", 11) == 0)
+    {
+      /* NetBSD-specific core file notes.  */
+      nt = get_netbsd_elfcore_note_type (pnote->type);
+    }
+  else
+    {
+      /* Don't recognize this note name; just use the default set of
+        note type strings.  */
+      nt = get_note_type (pnote->type);
+    }
+
   printf ("  %s\t\t0x%08lx\t%s\n",
          pnote->namesz ? pnote->namedata : "(NONE)",
-         pnote->descsz, get_note_type (pnote->type));
+         pnote->descsz, nt);
   return 1;
 }