+#include "unwind-ia64.h"
+
+/* An absolute address consists of a section and an offset. If the
+ section is NULL, the offset itself is the address, otherwise, the
+ address equals to LOAD_ADDRESS(section) + offset. */
+
+struct absaddr
+ {
+ unsigned short section;
+ bfd_vma offset;
+ };
+
+struct unw_aux_info
+ {
+ struct unw_table_entry
+ {
+ struct absaddr start;
+ struct absaddr end;
+ struct absaddr info;
+ }
+ *table; /* Unwind table. */
+ unsigned long table_len; /* Length of unwind table. */
+ unsigned char * info; /* Unwind info. */
+ unsigned long info_size; /* Size of unwind info. */
+ bfd_vma info_addr; /* starting address of unwind info. */
+ bfd_vma seg_base; /* Starting address of segment. */
+ Elf_Internal_Sym * symtab; /* The symbol table. */
+ unsigned long nsyms; /* Number of symbols. */
+ char * strtab; /* The string table. */
+ unsigned long strtab_size; /* Size of string table. */
+ };
+
+static void find_symbol_for_address PARAMS ((struct unw_aux_info *,
+ struct absaddr, const char **,
+ bfd_vma *));
+static void dump_ia64_unwind PARAMS ((struct unw_aux_info *));
+static int slurp_ia64_unwind_table PARAMS ((FILE *, struct unw_aux_info *,
+ Elf_Internal_Shdr *));
+
+static void
+find_symbol_for_address (aux, addr, symname, offset)
+ struct unw_aux_info *aux;
+ struct absaddr addr;
+ const char **symname;
+ bfd_vma *offset;
+{
+ bfd_vma dist = (bfd_vma) 0x100000;
+ Elf_Internal_Sym *sym, *best = NULL;
+ unsigned long i;
+
+ for (i = 0, sym = aux->symtab; i < aux->nsyms; ++i, ++sym)
+ {
+ if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
+ && sym->st_name != 0
+ && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
+ && addr.offset >= sym->st_value
+ && addr.offset - sym->st_value < dist)
+ {
+ best = sym;
+ dist = addr.offset - sym->st_value;
+ if (!dist)
+ break;
+ }
+ }
+ if (best)
+ {
+ *symname = (best->st_name >= aux->strtab_size
+ ? "<corrupt>" : aux->strtab + best->st_name);
+ *offset = dist;
+ return;
+ }
+ *symname = NULL;
+ *offset = addr.offset;
+}
+
+static void
+dump_ia64_unwind (aux)
+ struct unw_aux_info *aux;
+{
+ bfd_vma addr_size;
+ struct unw_table_entry * tp;
+ int in_body;
+
+ addr_size = is_32bit_elf ? 4 : 8;
+
+ for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
+ {
+ bfd_vma stamp;
+ bfd_vma offset;
+ const unsigned char * dp;
+ const unsigned char * head;
+ const char * procname;
+
+ find_symbol_for_address (aux, tp->start, &procname, &offset);
+
+ fputs ("\n<", stdout);
+
+ if (procname)
+ {
+ fputs (procname, stdout);
+
+ if (offset)
+ printf ("+%lx", (unsigned long) offset);
+ }
+
+ fputs (">: [", stdout);
+ print_vma (tp->start.offset, PREFIX_HEX);
+ fputc ('-', stdout);
+ print_vma (tp->end.offset, PREFIX_HEX);
+ printf ("], info at +0x%lx\n",
+ (unsigned long) (tp->info.offset - aux->seg_base));
+
+ head = aux->info + (tp->info.offset - aux->info_addr);
+ stamp = BYTE_GET8 ((unsigned char *) head);
+
+ printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
+ (unsigned) UNW_VER (stamp),
+ (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
+ UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
+ UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
+ (unsigned long) (addr_size * UNW_LENGTH (stamp)));
+
+ if (UNW_VER (stamp) != 1)
+ {
+ printf ("\tUnknown version.\n");
+ continue;
+ }
+
+ in_body = 0;
+ for (dp = head + 8; dp < head + 8 + addr_size * UNW_LENGTH (stamp);)
+ dp = unw_decode (dp, in_body, & in_body);
+ }
+}
+
+static int
+slurp_ia64_unwind_table (file, aux, sec)
+ FILE *file;
+ struct unw_aux_info *aux;
+ Elf_Internal_Shdr *sec;
+{
+ unsigned long size, addr_size, nrelas, i;
+ Elf_Internal_Phdr *prog_hdrs, *seg;
+ struct unw_table_entry *tep;
+ Elf_Internal_Shdr *relsec;
+ Elf_Internal_Rela *rela, *rp;
+ unsigned char *table, *tp;
+ Elf_Internal_Sym *sym;
+ const char *relname;
+ int result;
+
+ addr_size = is_32bit_elf ? 4 : 8;
+
+ /* First, find the starting address of the segment that includes
+ this section: */
+
+ if (elf_header.e_phnum)
+ {
+ prog_hdrs = (Elf_Internal_Phdr *)
+ xmalloc (elf_header.e_phnum * sizeof (Elf_Internal_Phdr));
+
+ if (is_32bit_elf)
+ result = get_32bit_program_headers (file, prog_hdrs);
+ else
+ result = get_64bit_program_headers (file, prog_hdrs);
+
+ if (!result)
+ {
+ free (prog_hdrs);
+ return 0;
+ }
+
+ for (seg = prog_hdrs; seg < prog_hdrs + elf_header.e_phnum; ++seg)
+ {
+ if (seg->p_type != PT_LOAD)
+ continue;
+
+ if (sec->sh_addr >= seg->p_vaddr
+ && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
+ {
+ aux->seg_base = seg->p_vaddr;
+ break;
+ }
+ }
+
+ free (prog_hdrs);
+ }
+
+ /* Second, build the unwind table from the contents of the unwind section: */
+ size = sec->sh_size;
+ table = (char *) get_data (NULL, file, sec->sh_offset,
+ size, _("unwind table"));
+ if (!table)
+ return 0;
+
+ tep = aux->table = xmalloc (size / (3 * addr_size) * sizeof (aux->table[0]));
+ for (tp = table; tp < table + size; tp += 3 * addr_size, ++ tep)
+ {
+ tep->start.section = SHN_UNDEF;
+ tep->end.section = SHN_UNDEF;
+ tep->info.section = SHN_UNDEF;
+ if (is_32bit_elf)
+ {
+ tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
+ tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
+ tep->info.offset = byte_get ((unsigned char *) tp + 8, 4);
+ }
+ else
+ {
+ tep->start.offset = BYTE_GET8 ((unsigned char *) tp + 0);
+ tep->end.offset = BYTE_GET8 ((unsigned char *) tp + 8);
+ tep->info.offset = BYTE_GET8 ((unsigned char *) tp + 16);
+ }
+ tep->start.offset += aux->seg_base;
+ tep->end.offset += aux->seg_base;
+ tep->info.offset += aux->seg_base;
+ }
+ free (table);
+
+ /* Third, apply any relocations to the unwind table: */
+
+ for (relsec = section_headers;
+ relsec < section_headers + elf_header.e_shnum;
+ ++relsec)
+ {
+ if (relsec->sh_type != SHT_RELA
+ || SECTION_HEADER (relsec->sh_info) != sec)
+ continue;
+
+ if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
+ & rela, & nrelas))
+ return 0;
+
+ for (rp = rela; rp < rela + nrelas; ++rp)
+ {
+ if (is_32bit_elf)
+ {
+ relname = elf_ia64_reloc_type (ELF32_R_TYPE (rp->r_info));
+ sym = aux->symtab + ELF32_R_SYM (rp->r_info);
+
+ if (ELF32_ST_TYPE (sym->st_info) != STT_SECTION)
+ {
+ warn (_("Skipping unexpected symbol type %u\n"),
+ ELF32_ST_TYPE (sym->st_info));
+ continue;
+ }
+ }
+ else
+ {
+ relname = elf_ia64_reloc_type (ELF64_R_TYPE (rp->r_info));
+ sym = aux->symtab + ELF64_R_SYM (rp->r_info);
+
+ if (ELF64_ST_TYPE (sym->st_info) != STT_SECTION)
+ {
+ warn (_("Skipping unexpected symbol type %u\n"),
+ ELF64_ST_TYPE (sym->st_info));
+ continue;
+ }
+ }
+
+ if (strncmp (relname, "R_IA64_SEGREL", 13) != 0)
+ {
+ warn (_("Skipping unexpected relocation type %s\n"), relname);
+ continue;
+ }
+
+ i = rp->r_offset / (3 * addr_size);
+
+ switch (rp->r_offset/addr_size % 3)
+ {
+ case 0:
+ aux->table[i].start.section = sym->st_shndx;
+ aux->table[i].start.offset += rp->r_addend;
+ break;
+ case 1:
+ aux->table[i].end.section = sym->st_shndx;
+ aux->table[i].end.offset += rp->r_addend;
+ break;
+ case 2:
+ aux->table[i].info.section = sym->st_shndx;
+ aux->table[i].info.offset += rp->r_addend;
+ break;
+ default:
+ break;
+ }
+ }
+
+ free (rela);
+ }
+
+ aux->table_len = size / (3 * addr_size);
+ return 1;
+}
+
+static int
+process_unwind (file)
+ FILE * file;
+{
+ Elf_Internal_Shdr *sec, *unwsec = NULL, *strsec;
+ unsigned long i, addr_size, unwcount = 0, unwstart = 0;
+ struct unw_aux_info aux;
+
+ if (!do_unwind)
+ return 1;
+
+ if (elf_header.e_machine != EM_IA_64)
+ {
+ printf (_("\nThere are no unwind sections in this file.\n"));
+ return 1;
+ }
+
+ memset (& aux, 0, sizeof (aux));
+
+ addr_size = is_32bit_elf ? 4 : 8;
+
+ for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
+ {
+ if (sec->sh_type == SHT_SYMTAB)
+ {
+ aux.nsyms = sec->sh_size / sec->sh_entsize;
+ aux.symtab = GET_ELF_SYMBOLS (file, sec);
+
+ strsec = SECTION_HEADER (sec->sh_link);
+ aux.strtab_size = strsec->sh_size;
+ aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
+ aux.strtab_size, _("string table"));
+ }
+ else if (sec->sh_type == SHT_IA_64_UNWIND)
+ unwcount++;
+ }
+
+ if (!unwcount)
+ printf (_("\nThere are no unwind sections in this file.\n"));
+
+ while (unwcount-- > 0)
+ {
+ char *suffix;
+ size_t len, len2;
+
+ for (i = unwstart, sec = section_headers + unwstart;
+ i < elf_header.e_shnum; ++i, ++sec)
+ if (sec->sh_type == SHT_IA_64_UNWIND)
+ {
+ unwsec = sec;
+ break;
+ }
+
+ unwstart = i + 1;
+ len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
+
+ if (strncmp (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once,
+ len) == 0)
+ {
+ /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO */
+ len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
+ suffix = SECTION_NAME (unwsec) + len;
+ for (i = 0, sec = section_headers; i < elf_header.e_shnum;
+ ++i, ++sec)
+ if (strncmp (SECTION_NAME (sec),
+ ELF_STRING_ia64_unwind_info_once, len2) == 0
+ && strcmp (SECTION_NAME (sec) + len2, suffix) == 0)
+ break;
+ }
+ else
+ {
+ /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
+ .IA_64.unwind or BAR -> .IA_64.unwind_info */
+ len = sizeof (ELF_STRING_ia64_unwind) - 1;
+ len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
+ suffix = "";
+ if (strncmp (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind,
+ len) == 0)
+ suffix = SECTION_NAME (unwsec) + len;
+ for (i = 0, sec = section_headers; i < elf_header.e_shnum;
+ ++i, ++sec)
+ if (strncmp (SECTION_NAME (sec),
+ ELF_STRING_ia64_unwind_info, len2) == 0
+ && strcmp (SECTION_NAME (sec) + len2, suffix) == 0)
+ break;
+ }
+
+ if (i == elf_header.e_shnum)
+ {
+ printf (_("\nCould not find unwind info section for "));
+
+ if (string_table == NULL)
+ printf ("%d", unwsec->sh_name);
+ else
+ printf (_("'%s'"), SECTION_NAME (unwsec));
+ }
+ else
+ {
+ aux.info_size = sec->sh_size;
+ aux.info_addr = sec->sh_addr;
+ aux.info = (char *) get_data (NULL, file, sec->sh_offset,
+ aux.info_size, _("unwind info"));
+
+ printf (_("\nUnwind section "));
+
+ if (string_table == NULL)
+ printf ("%d", unwsec->sh_name);
+ else
+ printf (_("'%s'"), SECTION_NAME (unwsec));
+
+ printf (_(" at offset 0x%lx contains %lu entries:\n"),
+ (unsigned long) unwsec->sh_offset,
+ (unsigned long) (unwsec->sh_size / (3 * addr_size)));
+
+ (void) slurp_ia64_unwind_table (file, & aux, unwsec);
+
+ if (aux.table_len > 0)
+ dump_ia64_unwind (& aux);
+
+ if (aux.table)
+ free ((char *) aux.table);
+ if (aux.info)
+ free ((char *) aux.info);
+ aux.table = NULL;
+ aux.info = NULL;
+ }
+ }
+
+ if (aux.symtab)
+ free (aux.symtab);
+ if (aux.strtab)
+ free ((char *) aux.strtab);
+
+ return 1;
+}