+static int
+display_debug_info (section, start, file)
+ Elf32_Internal_Shdr * section;
+ unsigned char * start;
+ FILE * file;
+{
+ unsigned char * end = start + section->sh_size;
+ unsigned char * section_begin = start;
+
+ printf (_("The section %s contains:\n\n"), SECTION_NAME (section));
+
+ while (start < end)
+ {
+ DWARF2_External_CompUnit * external;
+ DWARF2_Internal_CompUnit compunit;
+ unsigned char * tags;
+ int i;
+ int level;
+ unsigned long cu_offset;
+
+ external = (DWARF2_External_CompUnit *) start;
+
+ compunit.cu_length = BYTE_GET (external->cu_length);
+ compunit.cu_version = BYTE_GET (external->cu_version);
+ compunit.cu_abbrev_offset = BYTE_GET (external->cu_abbrev_offset);
+ compunit.cu_pointer_size = BYTE_GET (external->cu_pointer_size);
+
+ tags = start + sizeof (* external);
+ cu_offset = start - section_begin;
+ start += compunit.cu_length + sizeof (external->cu_length);
+
+ printf (_(" Compilation Unit @ %lx:\n"), cu_offset);
+ printf (_(" Length: %ld\n"), compunit.cu_length);
+ printf (_(" Version: %d\n"), compunit.cu_version);
+ printf (_(" Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
+ printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
+
+ if (compunit.cu_version != 2)
+ {
+ warn (_("Only version 2 DWARF debug information is currently supported.\n"));
+ continue;
+ }
+
+ if (first_abbrev != NULL)
+ free_abbrevs ();
+
+ /* Read in the abbrevs used by this compilation unit. */
+
+ {
+ Elf32_Internal_Shdr * sec;
+ unsigned char * begin;
+
+ /* Locate the .debug_abbrev section and process it. */
+ for (i = 0, sec = section_headers;
+ i < elf_header.e_shnum;
+ i ++, sec ++)
+ if (strcmp (SECTION_NAME (sec), ".debug_abbrev") == 0)
+ break;
+
+ if (i == -1 || sec->sh_size == 0)
+ {
+ warn (_("Unable to locate .debug_abbrev section!\n"));
+ return 0;
+ }
+
+ GET_DATA_ALLOC (sec->sh_offset, sec->sh_size, begin, unsigned char *,
+ "debug_abbrev section data");
+
+ process_abbrev_section (begin + compunit.cu_abbrev_offset,
+ begin + sec->sh_size);
+
+ free (begin);
+ }
+
+ level = 0;
+ while (tags < start)
+ {
+ int bytes_read;
+ unsigned long abbrev_number;
+ abbrev_entry * entry;
+ abbrev_attr * attr;
+
+ abbrev_number = read_leb128 (tags, & bytes_read, 0);
+ tags += bytes_read;
+
+ /* A null DIE marks the end of a list of children. */
+ if (abbrev_number == 0)
+ {
+ --level;
+ continue;
+ }
+
+ /* Scan through the abbreviation list until we reach the
+ correct entry. */
+ for (entry = first_abbrev;
+ entry && entry->entry != abbrev_number;
+ entry = entry->next)
+ continue;
+
+ if (entry == NULL)
+ {
+ warn (_("Unable to locate entry %lu in the abbreviation table\n"),
+ abbrev_number);
+ return 0;
+ }
+
+ printf (_(" <%d><%x>: Abbrev Number: %lu (%s)\n"),
+ level, tags - section_begin - bytes_read,
+ abbrev_number,
+ get_TAG_name (entry->tag));
+
+ for (attr = entry->first_attr; attr; attr = attr->next)
+ tags = read_and_display_attr (attr->attribute,
+ attr->form,
+ tags, cu_offset,
+ compunit.cu_pointer_size);
+
+ if (entry->children)
+ ++level;
+ }
+ }
+
+ printf ("\n");
+
+ return 1;
+}
+
+static int
+display_debug_aranges (section, start, file)
+ Elf32_Internal_Shdr * section;
+ unsigned char * start;
+ FILE * file ATTRIBUTE_UNUSED;
+{
+ unsigned char * end = start + section->sh_size;
+
+ printf (_("The section %s contains:\n\n"), SECTION_NAME (section));
+
+ while (start < end)
+ {
+ DWARF2_External_ARange * external;
+ DWARF2_Internal_ARange arange;
+ unsigned char * ranges;
+ unsigned long length;
+ unsigned long address;
+ int excess;
+
+ external = (DWARF2_External_ARange *) start;
+
+ arange.ar_length = BYTE_GET (external->ar_length);
+ arange.ar_version = BYTE_GET (external->ar_version);
+ arange.ar_info_offset = BYTE_GET (external->ar_info_offset);
+ arange.ar_pointer_size = BYTE_GET (external->ar_pointer_size);
+ arange.ar_segment_size = BYTE_GET (external->ar_segment_size);
+
+ if (arange.ar_version != 2)
+ {
+ warn (_("Only DWARF 2 aranges are currently supported.\n"));
+ break;
+ }
+
+ printf (_(" Length: %ld\n"), arange.ar_length);
+ printf (_(" Version: %d\n"), arange.ar_version);
+ printf (_(" Offset into .debug_info: %lx\n"), arange.ar_info_offset);
+ printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
+ printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
+
+ printf (_("\n Address Length\n"));
+
+ ranges = start + sizeof (* external);
+
+ /* Must pad to an alignment boundary that is twice the pointer size. */
+ excess = sizeof (* external) % (2 * arange.ar_pointer_size);
+ if (excess)
+ ranges += (2 * arange.ar_pointer_size) - excess;
+
+ for (;;)
+ {
+ address = byte_get (ranges, arange.ar_pointer_size);
+
+ ranges += arange.ar_pointer_size;
+
+ length = byte_get (ranges, arange.ar_pointer_size);
+
+ ranges += arange.ar_pointer_size;
+
+ /* A pair of zeros marks the end of the list. */
+ if (address == 0 && length == 0)
+ break;
+
+ printf (" %8.8lx %lu\n", address, length);
+ }
+
+ start += arange.ar_length + sizeof (external->ar_length);
+ }
+
+ printf ("\n");
+
+ return 1;
+}
+
+typedef struct Frame_Chunk
+{
+ struct Frame_Chunk * next;
+ unsigned char * chunk_start;
+ int ncols;
+ /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
+ short int * col_type;
+ int * col_offset;
+ char * augmentation;
+ unsigned int code_factor;
+ unsigned int data_factor;
+ unsigned long pc_begin;
+ unsigned long pc_range;
+ int cfa_reg;
+ int cfa_offset;
+ int ra;
+}
+Frame_Chunk;
+
+/* A marker for a col_type that means this column was never referenced
+ in the frame info. */
+#define DW_CFA_unreferenced (-1)
+
+static void
+frame_need_space (fc, reg)
+ Frame_Chunk * fc;
+ int reg;
+{
+ int prev = fc->ncols;
+
+ if (reg < fc->ncols)
+ return;
+
+ fc->ncols = reg + 1;
+ fc->col_type = (short int *) xrealloc (fc->col_type,
+ fc->ncols * sizeof (short int));
+ fc->col_offset = (int *) xrealloc (fc->col_offset,
+ fc->ncols * sizeof (int));
+
+ while (prev < fc->ncols)
+ {
+ fc->col_type[prev] = DW_CFA_unreferenced;
+ fc->col_offset[prev] = 0;
+ prev++;
+ }
+}
+
+static void
+frame_display_row (fc, need_col_headers, max_regs)
+ Frame_Chunk * fc;
+ int * need_col_headers;
+ int * max_regs;
+{
+ int r;
+ char tmp[100];
+
+ if (* max_regs < fc->ncols)
+ * max_regs = fc->ncols;
+
+ if (* need_col_headers)
+ {
+ * need_col_headers = 0;
+
+ printf (" LOC CFA ");
+
+ for (r = 0; r < * max_regs; r++)
+ if (fc->col_type[r] != DW_CFA_unreferenced)
+ {
+ if (r == fc->ra)
+ printf ("ra ");
+ else
+ printf ("r%-4d", r);
+ }
+
+ printf ("\n");
+ }
+
+ printf ("%08x ", (unsigned int) fc->pc_begin);
+ sprintf (tmp, "r%d%+d", fc->cfa_reg, fc->cfa_offset);
+ printf ("%-8s ", tmp);
+
+ for (r = 0; r < fc->ncols; r++)
+ {
+ if (fc->col_type[r] != DW_CFA_unreferenced)
+ {
+ switch (fc->col_type[r])
+ {
+ case DW_CFA_undefined:
+ strcpy (tmp, "u");
+ break;
+ case DW_CFA_same_value:
+ strcpy (tmp, "s");
+ break;
+ case DW_CFA_offset:
+ sprintf (tmp, "c%+d", fc->col_offset[r]);
+ break;
+ case DW_CFA_register:
+ sprintf (tmp, "r%d", fc->col_offset[r]);
+ break;
+ default:
+ strcpy (tmp, "n/a");
+ break;
+ }
+ printf ("%-5s", tmp);
+ }
+ }
+ printf ("\n");
+}
+
+#define GET(N) byte_get (start, N); start += N
+#define LEB() read_leb128 (start, & length_return, 0); start += length_return
+#define SLEB() read_leb128 (start, & length_return, 1); start += length_return
+
+static int
+display_debug_frames (section, start, file)
+ Elf32_Internal_Shdr * section;
+ unsigned char * start;
+ FILE * file ATTRIBUTE_UNUSED;
+{
+ unsigned char * end = start + section->sh_size;
+ unsigned char * section_start = start;
+ Frame_Chunk * chunks = 0;
+ Frame_Chunk * remembered_state = 0;
+ Frame_Chunk * rs;
+ int is_eh = (strcmp (SECTION_NAME (section), ".eh_frame") == 0);
+ int length_return;
+ int max_regs = 0;
+
+ printf (_("The section %s contains:\n"), SECTION_NAME (section));
+
+ while (start < end)
+ {
+ unsigned char * saved_start;
+ unsigned char * block_end;
+ unsigned long length;
+ unsigned long cie_id;
+ Frame_Chunk * fc;
+ Frame_Chunk * cie;
+ int need_col_headers = 1;
+
+ saved_start = start;
+ length = byte_get (start, 4); start += 4;
+
+ if (length == 0)
+ return 1;
+
+ block_end = saved_start + length + 4;
+ cie_id = byte_get (start, 4); start += 4;
+
+ printf ("\n%08x %08lx %08lx ", saved_start - section_start, length, cie_id);
+
+ if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
+ {
+ fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
+ memset (fc, 0, sizeof (Frame_Chunk));
+
+ fc->next = chunks;
+ chunks = fc;
+ fc->chunk_start = saved_start;
+ fc->ncols = 0;
+ fc->col_type = (short int *) xmalloc (sizeof (short int));
+ fc->col_offset = (int *) xmalloc (sizeof (int));
+ frame_need_space (fc, max_regs-1);
+
+ start ++; /* version */
+ fc->augmentation = start;
+
+ while (* start)
+ start++;
+
+ start++; /* skip past NUL */
+
+ if (fc->augmentation[0] == 'z')
+ {
+ int xtra;
+ fc->code_factor = LEB ();
+ fc->data_factor = SLEB ();
+ fc->ra = byte_get (start, 1); start += 1;
+ xtra = LEB ();
+ printf ("skipping %d extra bytes\n", xtra);
+ start += xtra;
+ }
+ else if (strcmp (fc->augmentation, "eh") == 0)
+ {
+ start += 4;
+ fc->code_factor = LEB ();
+ fc->data_factor = SLEB ();
+ fc->ra = byte_get (start, 1); start += 1;
+ }
+ else
+ {
+ fc->code_factor = LEB ();
+ fc->data_factor = SLEB ();
+ fc->ra = byte_get (start, 1); start += 1;
+ }
+ cie = fc;
+ printf ("CIE \"%s\" cf=%d df=%d ra=%d\n",
+ fc->augmentation, fc->code_factor, fc->data_factor, fc->ra);
+
+ frame_need_space (fc, fc->ra);
+ }
+ else
+ {
+ unsigned char * look_for;
+ static Frame_Chunk fde_fc;
+
+ fc = & fde_fc;
+ memset (fc, 0, sizeof (Frame_Chunk));
+
+ look_for = is_eh ? start-4-cie_id : (unsigned char *) cie_id;
+
+ fc->pc_begin = byte_get (start, 4); start += 4;
+ fc->pc_range = byte_get (start, 4); start += 4;
+
+ for (cie=chunks; cie && (cie->chunk_start != look_for); cie = cie->next);
+ if (!cie)
+ {
+ warn ("Invalid CIE pointer %08x in FDE at %08x\n", cie_id, saved_start);
+ start = block_end;
+ fc->ncols = 0;
+ fc->col_type = (short int *) xmalloc (sizeof (short int));
+ fc->col_offset = (int *) xmalloc (sizeof (int));
+ frame_need_space (fc, max_regs - 1);
+ cie = fc;
+ fc->augmentation = "";
+ }
+ else
+ {
+ fc->ncols = cie->ncols;
+ fc->col_type = (short int *) xmalloc (fc->ncols * sizeof (short int));
+ fc->col_offset = (int *) xmalloc (fc->ncols * sizeof (int));
+ memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
+ memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
+ fc->augmentation = cie->augmentation;
+ fc->code_factor = cie->code_factor;
+ fc->data_factor = cie->data_factor;
+ fc->cfa_reg = cie->cfa_reg;
+ fc->cfa_offset = cie->cfa_offset;
+ fc->ra = cie->ra;
+ frame_need_space (fc, max_regs-1);
+ }
+
+ if (cie->augmentation[0] == 'z')
+ {
+ unsigned long l = LEB ();
+ start += l;
+ }
+
+ printf ("FDE cie=%08x pc=%08lx..%08lx\n",
+ cie->chunk_start-section_start, fc->pc_begin,
+ fc->pc_begin + fc->pc_range);
+ }
+
+ /* At this point, fc is the current chunk, cie (if any) is set, and we're
+ about to interpret instructions for the chunk. */
+
+ /* This exists for readelf maintainers. */
+#define FDEBUG 0
+
+ {
+ /* Start by making a pass over the chunk, allocating storage
+ and taking note of what registers are used. */
+ unsigned char * tmp = start;
+
+ while (start < block_end)
+ {
+ unsigned op, opa;
+ unsigned long reg;
+ bfd_vma vma;
+
+ op = * start ++;
+ opa = op & 0x3f;
+ if (op & 0xc0)
+ op &= 0xc0;
+
+ /* Warning: if you add any more cases to this switch, be
+ sure to add them to the corresponding switch below. */
+ switch (op)
+ {
+ case DW_CFA_advance_loc:
+ break;
+ case DW_CFA_offset:
+ LEB ();
+ frame_need_space (fc, opa);
+ fc->col_type[opa] = DW_CFA_undefined;
+ break;
+ case DW_CFA_restore:
+ frame_need_space (fc, opa);
+ fc->col_type[opa] = DW_CFA_undefined;
+ break;
+ case DW_CFA_set_loc:
+ start += sizeof (vma);
+ break;
+ case DW_CFA_advance_loc1:
+ start += 1;
+ break;
+ case DW_CFA_advance_loc2:
+ start += 2;
+ break;
+ case DW_CFA_advance_loc4:
+ start += 4;
+ break;
+ case DW_CFA_offset_extended:
+ reg = LEB (); LEB ();
+ frame_need_space (fc, reg);
+ fc->col_type[reg] = DW_CFA_undefined;
+ break;
+ case DW_CFA_restore_extended:
+ reg = LEB ();
+ frame_need_space (fc, reg);
+ fc->col_type[reg] = DW_CFA_undefined;
+ break;
+ case DW_CFA_undefined:
+ reg = LEB ();
+ frame_need_space (fc, reg);
+ fc->col_type[reg] = DW_CFA_undefined;
+ break;
+ case DW_CFA_same_value:
+ reg = LEB ();
+ frame_need_space (fc, reg);
+ fc->col_type[reg] = DW_CFA_undefined;
+ break;
+ case DW_CFA_register:
+ reg = LEB (); LEB ();
+ frame_need_space (fc, reg);
+ fc->col_type[reg] = DW_CFA_undefined;
+ break;
+ case DW_CFA_def_cfa:
+ LEB (); LEB ();
+ break;
+ case DW_CFA_def_cfa_register:
+ LEB ();
+ break;
+ case DW_CFA_def_cfa_offset:
+ LEB ();
+ break;
+#ifndef DW_CFA_GNU_args_size
+#define DW_CFA_GNU_args_size 0x2e
+#endif
+ case DW_CFA_GNU_args_size:
+ LEB ();
+ break;
+#ifndef DW_CFA_GNU_negative_offset_extended
+#define DW_CFA_GNU_negative_offset_extended 0x2f
+#endif
+ case DW_CFA_GNU_negative_offset_extended:
+ reg = LEB (); LEB ();
+ frame_need_space (fc, reg);
+ fc->col_type[reg] = DW_CFA_undefined;
+
+ default:
+ break;
+ }
+ }
+ start = tmp;
+ }
+
+ /* Now we know what registers are used, make a second pass over
+ the chunk, this time actually printing out the info. */
+
+ while (start < block_end)
+ {
+ unsigned op, opa;
+ unsigned long ul, reg, roffs;
+ long l, ofs;
+ bfd_vma vma;
+
+ op = * start ++;
+ opa = op & 0x3f;
+ if (op & 0xc0)
+ op &= 0xc0;
+
+ /* Warning: if you add any more cases to this switch, be
+ sure to add them to the corresponding switch above. */
+ switch (op)
+ {
+ case DW_CFA_advance_loc:
+ frame_display_row (fc, &need_col_headers, &max_regs);
+#if FDEBUG
+ printf (" DW_CFA_advance_loc: %08x = %08x + %d*%d\n",
+ fc->pc_begin + opa * fc->code_factor, fc->pc_begin, opa, fc->code_factor);
+#endif
+ fc->pc_begin += opa * fc->code_factor;
+ break;
+
+ case DW_CFA_offset:
+ roffs = LEB ();
+#if FDEBUG
+ printf (" DW_CFA_offset: r%d = cfa[%d*%d]\n", opa, roffs, fc->data_factor);
+#endif
+ fc->col_type[opa] = DW_CFA_offset;
+ fc->col_offset[opa] = roffs * fc->data_factor;
+ break;
+
+ case DW_CFA_restore:
+#if FDEBUG
+ printf (" DW_CFA_restore: r%d\n", opa);
+#endif
+ fc->col_type[opa] = cie->col_type[opa];
+ fc->col_offset[opa] = cie->col_offset[opa];
+ break;
+
+ case DW_CFA_set_loc:
+ frame_display_row (fc, &need_col_headers, &max_regs);
+ vma = byte_get (start, sizeof (vma)); start += sizeof (vma);
+#if FDEBUG
+ printf (" DW_CFA_set_loc: %08x\n", vma);
+#endif
+ fc->pc_begin = vma;
+ break;
+
+ case DW_CFA_advance_loc1:
+ frame_display_row (fc, &need_col_headers, &max_regs);
+ ofs = byte_get (start, 1); start += 1;
+#if FDEBUG
+ printf (" DW_CFA_advance_loc1: %08x = %08x + %d*%d\n",
+ fc->pc_begin + ofs * fc->code_factor, fc->pc_begin, ofs, fc->code_factor);
+#endif
+ fc->pc_begin += ofs * fc->code_factor;
+ break;
+
+ case DW_CFA_advance_loc2:
+ frame_display_row (fc, &need_col_headers, &max_regs);
+ ofs = byte_get (start, 2); start += 2;
+#if FDEBUG
+ printf (" DW_CFA_advance_loc2: %08x = %08x + %d*%d\n",
+ fc->pc_begin + ofs * fc->code_factor, fc->pc_begin, ofs, fc->code_factor);
+#endif
+ fc->pc_begin += ofs * fc->code_factor;
+ break;
+
+ case DW_CFA_advance_loc4:
+ frame_display_row (fc, &need_col_headers, &max_regs);
+ ofs = byte_get (start, 4); start += 4;
+#if FDEBUG
+ printf (" DW_CFA_advance_loc4: %08x = %08x + %d*%d\n",
+ fc->pc_begin + ofs * fc->code_factor, fc->pc_begin, ofs, fc->code_factor);
+#endif
+ fc->pc_begin += ofs * fc->code_factor;
+ break;
+
+ case DW_CFA_offset_extended:
+ reg = LEB ();
+ roffs = LEB ();
+#if FDEBUG
+ printf (" DW_CFA_offset_extended: r%d = cfa[%d*%d]\n", reg, roffs, fc->data_factor);
+#endif
+ fc->col_type[reg] = DW_CFA_offset;
+ fc->col_offset[reg] = roffs * fc->data_factor;
+ break;
+
+ case DW_CFA_restore_extended:
+ reg = LEB ();
+#if FDEBUG
+ printf (" DW_CFA_restore_extended: r%d\n", reg);
+#endif
+ fc->col_type[reg] = cie->col_type[reg];
+ fc->col_offset[reg] = cie->col_offset[reg];
+ break;
+
+ case DW_CFA_undefined:
+ reg = LEB ();
+#if FDEBUG
+ printf (" DW_CFA_undefined: r%d\n", reg);
+#endif
+ fc->col_type[reg] = DW_CFA_undefined;
+ fc->col_offset[reg] = 0;
+ break;
+
+ case DW_CFA_same_value:
+ reg = LEB ();
+#if FDEBUG
+ printf (" DW_CFA_same_value: r%d\n", reg);
+#endif
+ fc->col_type[reg] = DW_CFA_same_value;
+ fc->col_offset[reg] = 0;
+ break;
+
+ case DW_CFA_register:
+ reg = LEB ();
+ roffs = LEB ();
+#if FDEBUG
+ printf (" DW_CFA_register: r%d\n", reg);
+#endif
+ fc->col_type[reg] = DW_CFA_register;
+ fc->col_offset[reg] = roffs;
+ break;
+
+ case DW_CFA_remember_state:
+#if FDEBUG
+ printf (" DW_CFA_remember_state\n");
+#endif
+ rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
+ rs->ncols = fc->ncols;
+ rs->col_type = (short int *) xmalloc (rs->ncols * sizeof (short int));
+ rs->col_offset = (int *) xmalloc (rs->ncols * sizeof (int));
+ memcpy (rs->col_type, fc->col_type, rs->ncols);
+ memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
+ rs->next = remembered_state;
+ remembered_state = rs;
+ break;
+
+ case DW_CFA_restore_state:
+#if FDEBUG
+ printf (" DW_CFA_restore_state\n");
+#endif
+ rs = remembered_state;
+ remembered_state = rs->next;
+ frame_need_space (fc, rs->ncols-1);
+ memcpy (fc->col_type, rs->col_type, rs->ncols);
+ memcpy (fc->col_offset, rs->col_offset, rs->ncols * sizeof (int));
+ free (rs->col_type);
+ free (rs->col_offset);
+ free (rs);
+ break;