+int
+load_debug_section (enum dwarf_section_display_enum debug, void *file)
+{
+ struct dwarf_section *section = &debug_displays [debug].section;
+ bfd *abfd = file;
+ asection *sec;
+ bfd_boolean ret;
+
+ /* If it is already loaded, do nothing. */
+ if (section->start != NULL)
+ return 1;
+
+ /* Locate the debug section. */
+ sec = bfd_get_section_by_name (abfd, section->name);
+ if (sec == NULL)
+ return 0;
+
+ section->address = bfd_get_section_vma (abfd, sec);
+ section->size = bfd_get_section_size (sec);
+ section->start = xmalloc (section->size);
+
+ if (is_relocatable && debug_displays [debug].relocate)
+ ret = bfd_simple_get_relocated_section_contents (abfd,
+ sec,
+ section->start,
+ syms) != NULL;
+ else
+ ret = bfd_get_section_contents (abfd, sec, section->start, 0,
+ section->size);
+
+ if (!ret)
+ {
+ free_debug_section (debug);
+ printf (_("\nCan't get contents for section '%s'.\n"),
+ section->name);
+ }
+
+ return ret;
+}
+
+void
+free_debug_section (enum dwarf_section_display_enum debug)
+{
+ struct dwarf_section *section = &debug_displays [debug].section;
+
+ if (section->start == NULL)
+ return;
+
+ free ((char *) section->start);
+ section->start = NULL;
+ section->address = 0;
+ section->size = 0;
+}
+
+static void
+dump_dwarf_section (bfd *abfd, asection *section,
+ void *arg ATTRIBUTE_UNUSED)
+{
+ const char *name = bfd_get_section_name (abfd, section);
+ const char *match;
+ enum dwarf_section_display_enum i;
+
+ if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
+ match = ".debug_info";
+ else
+ match = name;
+
+ for (i = 0; i < max; i++)
+ if (strcmp (debug_displays[i].section.name, match) == 0)
+ {
+ if (!debug_displays[i].eh_frame)
+ {
+ struct dwarf_section *sec = &debug_displays [i].section;
+
+ if (load_debug_section (i, abfd))
+ {
+ debug_displays[i].display (sec, abfd);
+
+ if (i != info && i != abbrev)
+ free_debug_section (i);
+ }
+ }
+ break;
+ }
+}
+
+static const char *mach_o_dwarf_sections [] = {
+ "LC_SEGMENT.__DWARFA.__debug_abbrev", /* .debug_abbrev */
+ "LC_SEGMENT.__DWARFA.__debug_aranges", /* .debug_aranges */
+ "LC_SEGMENT.__DWARFA.__debug_frame", /* .debug_frame */
+ "LC_SEGMENT.__DWARFA.__debug_info", /* .debug_info */
+ "LC_SEGMENT.__DWARFA.__debug_line", /* .debug_line */
+ "LC_SEGMENT.__DWARFA.__debug_pubnames", /* .debug_pubnames */
+ ".eh_frame", /* .eh_frame */
+ "LC_SEGMENT.__DWARFA.__debug_macinfo", /* .debug_macinfo */
+ "LC_SEGMENT.__DWARFA.__debug_str", /* .debug_str */
+ "LC_SEGMENT.__DWARFA.__debug_loc", /* .debug_loc */
+ "LC_SEGMENT.__DWARFA.__debug_pubtypes", /* .debug_pubtypes */
+ "LC_SEGMENT.__DWARFA.__debug_ranges", /* .debug_ranges */
+ "LC_SEGMENT.__DWARFA.__debug_static_func", /* .debug_static_func */
+ "LC_SEGMENT.__DWARFA.__debug_static_vars", /* .debug_static_vars */
+ "LC_SEGMENT.__DWARFA.__debug_types", /* .debug_types */
+ "LC_SEGMENT.__DWARFA.__debug_weaknames" /* .debug_weaknames */
+};
+
+static const char *generic_dwarf_sections [max];
+
+static void
+check_mach_o_dwarf (bfd *abfd)
+{
+ static enum bfd_flavour old_flavour = bfd_target_unknown_flavour;
+ enum bfd_flavour current_flavour = bfd_get_flavour (abfd);
+ enum dwarf_section_display_enum i;
+
+ if (generic_dwarf_sections [0] == NULL)
+ for (i = 0; i < max; i++)
+ generic_dwarf_sections [i] = debug_displays[i].section.name;
+
+ if (old_flavour != current_flavour)
+ {
+ if (current_flavour == bfd_target_mach_o_flavour)
+ for (i = 0; i < max; i++)
+ debug_displays[i].section.name = mach_o_dwarf_sections [i];
+ else if (old_flavour == bfd_target_mach_o_flavour)
+ for (i = 0; i < max; i++)
+ debug_displays[i].section.name = generic_dwarf_sections [i];
+
+ old_flavour = current_flavour;
+ }
+}
+
+/* Dump the dwarf debugging information. */
+
+static void
+dump_dwarf (bfd *abfd)
+{
+ is_relocatable = ((abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
+ == HAS_RELOC);
+
+ /* FIXME: bfd_get_arch_size may return -1. We assume that 64bit
+ targets will return 64. */
+ eh_addr_size = bfd_get_arch_size (abfd) == 64 ? 8 : 4;
+
+ if (bfd_big_endian (abfd))
+ byte_get = byte_get_big_endian;
+ else if (bfd_little_endian (abfd))
+ byte_get = byte_get_little_endian;
+ else
+ abort ();
+
+ check_mach_o_dwarf (abfd);
+
+ bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
+
+ free_debug_memory ();
+}
+\f