void coff_solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms) { asection *libsect; if (!readsyms) return; libsect = bfd_get_section_by_name (exec_bfd, ".lib"); if (libsect) { int libsize; unsigned char *lib; struct libent { bfd_byte len[4]; bfd_byte nameoffset[4]; }; libsize = bfd_section_size (exec_bfd, libsect); lib = (unsigned char *) alloca (libsize); bfd_get_section_contents (exec_bfd, libsect, lib, 0, libsize); while (libsize > 0) { struct libent *ent; struct objfile *objfile; int len, nameoffset; char *filename; ent = (struct libent *) lib; len = bfd_get_32 (exec_bfd, ent->len); nameoffset = bfd_get_32 (exec_bfd, ent->nameoffset); if (len <= 0) break; filename = (char *) ent + nameoffset * 4; objfile = symbol_file_add (filename, from_tty, NULL, /* no offsets */ 0, /* not mainline */ OBJF_SHARED); /* flags */ libsize -= len * 4; lib += len * 4; } /* Getting new symbols may change our opinion about what is frameless. */ reinit_frame_cache (); } }
static void pex64_get_runtime_function (bfd *abfd, struct pex64_runtime_function *rf, const void *data) { const struct external_pex64_runtime_function *ex_rf = (const struct external_pex64_runtime_function *) data; rf->rva_BeginAddress = bfd_get_32 (abfd, ex_rf->rva_BeginAddress); rf->rva_EndAddress = bfd_get_32 (abfd, ex_rf->rva_EndAddress); rf->rva_UnwindData = bfd_get_32 (abfd, ex_rf->rva_UnwindData); }
static void pex64_get_runtime_function (bfd *abfd, struct pex64_runtime_function *rf, const void *data) { const struct external_pex64_runtime_function *ex_rf = (const struct external_pex64_runtime_function *) data; rf->rva_BeginAddress = bfd_get_32 (abfd, ex_rf->rva_BeginAddress); rf->rva_EndAddress = bfd_get_32 (abfd, ex_rf->rva_EndAddress); rf->rva_UnwindData = bfd_get_32 (abfd, ex_rf->rva_UnwindData); rf->isChained = PEX64_IS_RUNTIME_FUNCTION_CHAINED (rf); rf->rva_UnwindData = PEX64_GET_UNWINDDATA_UNIFIED_RVA (rf); }
static void pex64_get_scope_entry (bfd *abfd, struct pex64_scope_entry *se, bfd_vma idx, const bfd_byte *x) { const struct external_pex64_scope_entry *ex_se; x += (idx * PEX64_SCOPE_ENTRY_SIZE); ex_se = (const struct external_pex64_scope_entry *) x; memset (se, 0, sizeof (struct pex64_scope_entry)); se->rva_BeginAddress = bfd_get_32 (abfd, ex_se->rva_BeginAddress); se->rva_EndAddress = bfd_get_32 (abfd, ex_se->rva_EndAddress); se->rva_HandlerAddress = bfd_get_32 (abfd, ex_se->rva_HandlerAddress); se->rva_JumpAddress = bfd_get_32 (abfd, ex_se->rva_JumpAddress); }
/* Support for core dump NOTE sections. */ static bfd_boolean cgc32_am33lin_grok_prstatus (bfd *abfd, Cgc_Internal_Note *note) { int offset; unsigned int size; switch (note->descsz) { default: return FALSE; case 184: case 188: /* Linux/am33 */ /* pr_cursig */ cgc_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12); /* pr_pid */ cgc_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24); /* pr_reg */ offset = 72; size = 112; break; } /* Make a ".reg/999" section. */ return _bfd_cgccore_make_pseudosection (abfd, ".reg", size, note->descpos + offset); }
static bfd_boolean tilegx_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) { int offset; size_t size; if (note->descsz != TILEGX_PRSTATUS_SIZEOF) return FALSE; /* pr_cursig */ elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + TILEGX_PRSTATUS_OFFSET_PR_CURSIG); /* pr_pid */ elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + TILEGX_PRSTATUS_OFFSET_PR_PID); /* pr_reg */ offset = TILEGX_PRSTATUS_OFFSET_PR_REG; size = TILEGX_GREGSET_T_SIZE; /* Make a ".reg/999" section. */ return _bfd_elfcore_make_pseudosection (abfd, ".reg", size, note->descpos + offset); }
static bfd_vma read_value (bfd *abfd, bfd_byte *buf, int width, int is_signed) { bfd_vma value; switch (width) { case 2: if (is_signed) value = bfd_get_signed_16 (abfd, buf); else value = bfd_get_16 (abfd, buf); break; case 4: if (is_signed) value = bfd_get_signed_32 (abfd, buf); else value = bfd_get_32 (abfd, buf); break; case 8: if (is_signed) value = bfd_get_signed_64 (abfd, buf); else value = bfd_get_64 (abfd, buf); break; default: BFD_FAIL (); return 0; } return value; }
void aarch64_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc) { bfd_vma pc, dest_pc, offset; unsigned int insn; Sym *child; DBG (CALLDEBUG, printf ("[find_call] %s: 0x%lx to 0x%lx\n", parent->name, (unsigned long) p_lowpc, (unsigned long) p_highpc)); for (pc = p_lowpc; pc < p_highpc; pc += 4) { insn = bfd_get_32 (core_bfd, ((unsigned char *) core_text_space + pc - core_text_sect->vma)); if ((insn & BRANCH_MASK) == BRANCH_PATTERN) { DBG (CALLDEBUG, printf ("[find_call] 0x%lx: bl", (unsigned long) pc)); /* Regular pc relative addressing check that this is the address of a function. */ offset = ((((bfd_vma) insn & 0x3ffffff) ^ 0x2000000) - 0x2000000) << 2; dest_pc = pc + offset; if (hist_check_address (dest_pc)) { child = sym_lookup (&symtab, dest_pc); if (child) { DBG (CALLDEBUG, printf ("\tdest_pc=0x%lx, (name=%s, addr=0x%lx)\n", (unsigned long) dest_pc, child->name, (unsigned long) child->addr)); if (child->addr == dest_pc) { /* a hit. */ arc_add (parent, child, (unsigned long) 0); continue; } } } /* Something funny going on. */ DBG (CALLDEBUG, printf ("\tbut it's a botch\n")); } } }
void i386_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc) { unsigned char *instructp; Sym *child; bfd_vma pc, destpc; DBG (CALLDEBUG, printf ("[findcall] %s: 0x%lx to 0x%lx\n", parent->name, (unsigned long) p_lowpc, (unsigned long) p_highpc)); for (pc = p_lowpc; pc < p_highpc; ++pc) { instructp = (unsigned char *) core_text_space + pc - core_text_sect->vma; if (i386_iscall (instructp)) { DBG (CALLDEBUG, printf ("[findcall]\t0x%lx:call", (unsigned long) pc)); /* * regular pc relative addressing * check that this is the address of * a function. */ destpc = bfd_get_32 (core_bfd[0], instructp + 1) + pc + 5; if (hist_check_address (destpc)) { child = sym_lookup (&symtab, destpc); if (child && child->addr == destpc) { /* * a hit */ DBG (CALLDEBUG, printf ("\tdestpc 0x%lx (%s)\n", (unsigned long) destpc, child->name)); arc_add (parent, child, (unsigned long) 0); instructp += 4; /* call is a 5 byte instruction */ continue; } } /* * else: * it looked like a callf, but it: * a) wasn't actually a callf, or * b) didn't point to a known function in the symtab, or * c) something funny is going on. */ DBG (CALLDEBUG, printf ("\tbut it's a botch\n")); } } }
static bfd *find_debug_file(bfd *lib, const char *aFileName) { // check for a separate debug file with symbols asection *sect = bfd_get_section_by_name(lib, ".gnu_debuglink"); if (!sect) return nullptr; bfd_size_type debuglinkSize = bfd_section_size (objfile->obfd, sect); char *debuglink = new char[debuglinkSize]; bfd_get_section_contents(lib, sect, debuglink, 0, debuglinkSize); // crc checksum is aligned to 4 bytes, and after the NUL. int crc_offset = (int(strlen(debuglink)) & ~3) + 4; unsigned long crc32 = bfd_get_32(lib, debuglink + crc_offset); // directory component char *dirbuf = strdup(aFileName); const char *dir = dirname(dirbuf); static const char debug_subdir[] = ".debug"; // This is gdb's default global debugging info directory, but gdb can // be instructed to use a different directory. static const char global_debug_dir[] = "/usr/lib/debug"; char *filename = new char[strlen(global_debug_dir) + strlen(dir) + crc_offset + 3]; // /path/debuglink sprintf(filename, "%s/%s", dir, debuglink); bfd *debugFile = try_debug_file(filename, crc32); if (!debugFile) { // /path/.debug/debuglink sprintf(filename, "%s/%s/%s", dir, debug_subdir, debuglink); debugFile = try_debug_file(filename, crc32); if (!debugFile) { // /usr/lib/debug/path/debuglink sprintf(filename, "%s/%s/%s", global_debug_dir, dir, debuglink); debugFile = try_debug_file(filename, crc32); } } delete[] filename; free(dirbuf); delete[] debuglink; return debugFile; }
static void pex64_get_unwind_info (bfd *abfd, struct pex64_unwind_info *ui, void *data) { struct external_pex64_unwind_info *ex_ui = (struct external_pex64_unwind_info *) data; bfd_byte *ex_dta = (bfd_byte *) data; memset (ui, 0, sizeof (struct pex64_unwind_info)); ui->Version = PEX64_UWI_VERSION (ex_ui->Version_Flags); ui->Flags = PEX64_UWI_FLAGS (ex_ui->Version_Flags); ui->SizeOfPrologue = (bfd_vma) ex_ui->SizeOfPrologue; ui->CountOfCodes = (bfd_vma) ex_ui->CountOfCodes; ui->FrameRegister = PEX64_UWI_FRAMEREG (ex_ui->FrameRegisterOffset); ui->FrameOffset = PEX64_UWI_FRAMEOFF (ex_ui->FrameRegisterOffset); ui->sizeofUnwindCodes = PEX64_UWI_SIZEOF_UWCODE_ARRAY (ui->CountOfCodes); ui->SizeOfBlock = ui->sizeofUnwindCodes + 4; ui->rawUnwindCodes = &ex_dta[4]; ex_dta += ui->SizeOfBlock; switch (ui->Flags) { case UNW_FLAG_CHAININFO: ui->rva_BeginAddress = bfd_get_32 (abfd, ex_dta + 0); ui->rva_EndAddress = bfd_get_32 (abfd, ex_dta + 4); ui->rva_UnwindData = bfd_get_32 (abfd, ex_dta + 8); ui->SizeOfBlock += 12; return; case UNW_FLAG_EHANDLER: case UNW_FLAG_UHANDLER: case UNW_FLAG_FHANDLER: ui->rva_ExceptionHandler = bfd_get_32 (abfd, ex_dta); ui->SizeOfBlock += 4; return; default: return; } }
static void pex64_get_unwind_info (bfd *abfd, struct pex64_unwind_info *ui, void *data) { struct external_pex64_unwind_info *ex_ui = (struct external_pex64_unwind_info *) data; bfd_byte *ex_dta = (bfd_byte *) data; memset (ui, 0, sizeof (struct pex64_unwind_info)); ui->Version = PEX64_UWI_VERSION (ex_ui->Version_Flags); ui->Flags = PEX64_UWI_FLAGS (ex_ui->Version_Flags); ui->SizeOfPrologue = (bfd_vma) ex_ui->SizeOfPrologue; ui->CountOfCodes = (bfd_vma) ex_ui->CountOfCodes; ui->FrameRegister = PEX64_UWI_FRAMEREG (ex_ui->FrameRegisterOffset); ui->FrameOffset = PEX64_UWI_FRAMEOFF (ex_ui->FrameRegisterOffset); ui->sizeofUnwindCodes = PEX64_UWI_SIZEOF_UWCODE_ARRAY (ui->CountOfCodes); ui->SizeOfBlock = ui->sizeofUnwindCodes + 4; ui->rawUnwindCodes = &ex_dta[4]; ex_dta += ui->SizeOfBlock; switch (ui->Flags) { case UNW_FLAG_EHANDLER: ui->rva_ExceptionHandler = bfd_get_32 (abfd, ex_dta); break; case UNW_FLAG_UHANDLER: ui->rva_TerminationHandler = bfd_get_32 (abfd, ex_dta); break; case UNW_FLAG_FHANDLER: ui->rva_FrameHandler = bfd_get_32 (abfd, ex_dta); ui->FrameHandlerArgument = bfd_get_32 (abfd, ex_dta + 4); ui->SizeOfBlock += 8; return; case UNW_FLAG_CHAININFO: ui->rva_FunctionEntry = bfd_get_32 (abfd, ex_dta); ui->SizeOfBlock += 4; return; default: return; } ex_dta += 4; ui->SizeOfBlock += 8; ui->CountOfScopes = bfd_get_32 (abfd, ex_dta); ex_dta += 4; ui->rawScopeEntries = ex_dta; ui->SizeOfBlock += (ui->CountOfScopes * PEX64_SCOPE_ENTRY_SIZE); }
static bfd_boolean read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, void *dhandle, bfd_boolean *pfound) { static struct { const char *secname; const char *strsecname; } names[] = { { ".stab", ".stabstr" }, { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" }, { "$GDB_SYMBOLS$", "$GDB_STRINGS$" } }; unsigned int i; void *shandle; *pfound = FALSE; shandle = NULL; for (i = 0; i < sizeof names / sizeof names[0]; i++) { asection *sec, *strsec; sec = bfd_get_section_by_name (abfd, names[i].secname); strsec = bfd_get_section_by_name (abfd, names[i].strsecname); if (sec != NULL && strsec != NULL) { bfd_size_type stabsize, strsize; bfd_byte *stabs, *strings; bfd_byte *stab; bfd_size_type stroff, next_stroff; stabsize = bfd_section_size (abfd, sec); stabs = (bfd_byte *) xmalloc (stabsize); if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize)) { fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), names[i].secname, bfd_errmsg (bfd_get_error ())); return FALSE; } strsize = bfd_section_size (abfd, strsec); strings = (bfd_byte *) xmalloc (strsize + 1); if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize)) { fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), names[i].strsecname, bfd_errmsg (bfd_get_error ())); return FALSE; } /* Zero terminate the strings table, just in case. */ strings [strsize] = 0; if (shandle == NULL) { shandle = start_stab (dhandle, abfd, TRUE, syms, symcount); if (shandle == NULL) return FALSE; } *pfound = TRUE; stroff = 0; next_stroff = 0; /* PR 17512: file: 078-60391-0.001:0.1. */ for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12) { unsigned int strx; int type; int other ATTRIBUTE_UNUSED; int desc; bfd_vma value; /* This code presumes 32 bit values. */ strx = bfd_get_32 (abfd, stab); type = bfd_get_8 (abfd, stab + 4); other = bfd_get_8 (abfd, stab + 5); desc = bfd_get_16 (abfd, stab + 6); value = bfd_get_32 (abfd, stab + 8); if (type == 0) { /* Special type 0 stabs indicate the offset to the next string table. */ stroff = next_stroff; next_stroff += value; } else { size_t len; char *f, *s; if (stroff + strx >= strsize) { fprintf (stderr, _("%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d\n"), bfd_get_filename (abfd), names[i].secname, (long) (stab - stabs) / 12, strx, type); continue; } s = (char *) strings + stroff + strx; f = NULL; /* PR 17512: file: 002-87578-0.001:0.1. It is possible to craft a file where, without the 'strlen (s) > 0', an attempt to read the byte before 'strings' would occur. */ while ((len = strlen (s)) > 0 && s[len - 1] == '\\' && stab + 12 < stabs + stabsize) { char *p; stab += 12; p = s + len - 1; *p = '\0'; strx = stroff + bfd_get_32 (abfd, stab); if (strx >= strsize) { fprintf (stderr, _("%s: %s: stab entry %ld is corrupt\n"), bfd_get_filename (abfd), names[i].secname, (long) (stab - stabs) / 12); break; } else s = concat (s, (char *) strings + strx, (const char *) NULL); /* We have to restore the backslash, because, if the linker is hashing stabs strings, we may see the same string more than once. */ *p = '\\'; if (f != NULL) free (f); f = s; } save_stab (type, desc, value, s); if (! parse_stab (dhandle, shandle, type, desc, value, s)) { stab_context (); free_saved_stabs (); return FALSE; } /* Don't free f, since I think the stabs code expects strings to hang around. This should be straightened out. FIXME. */ } } free_saved_stabs (); free (stabs); /* Don't free strings, since I think the stabs code expects the strings to hang around. This should be straightened out. FIXME. */ } } if (shandle != NULL) { if (! finish_stab (dhandle, shandle)) return FALSE; } return TRUE; }
static struct mdebug_extra_func_info * non_heuristic_proc_desc (CORE_ADDR pc, CORE_ADDR *addrptr) { CORE_ADDR startaddr; struct mdebug_extra_func_info *proc_desc; struct block *b = block_for_pc (pc); struct symbol *sym; struct obj_section *sec; struct mips_objfile_private *priv; find_pc_partial_function (pc, NULL, &startaddr, NULL); if (addrptr) *addrptr = startaddr; priv = NULL; sec = find_pc_section (pc); if (sec != NULL) { priv = (struct mips_objfile_private *) objfile_data (sec->objfile, mips_pdr_data); /* Search the ".pdr" section generated by GAS. This includes most of the information normally found in ECOFF PDRs. */ the_bfd = sec->objfile->obfd; if (priv == NULL && (the_bfd->format == bfd_object && bfd_get_flavour (the_bfd) == bfd_target_elf_flavour && elf_elfheader (the_bfd)->e_ident[EI_CLASS] == ELFCLASS64)) { /* Right now GAS only outputs the address as a four-byte sequence. This means that we should not bother with this method on 64-bit targets (until that is fixed). */ priv = obstack_alloc (&sec->objfile->objfile_obstack, sizeof (struct mips_objfile_private)); priv->size = 0; set_objfile_data (sec->objfile, mips_pdr_data, priv); } else if (priv == NULL) { asection *bfdsec; priv = obstack_alloc (&sec->objfile->objfile_obstack, sizeof (struct mips_objfile_private)); bfdsec = bfd_get_section_by_name (sec->objfile->obfd, ".pdr"); if (bfdsec != NULL) { priv->size = bfd_section_size (sec->objfile->obfd, bfdsec); priv->contents = obstack_alloc (&sec->objfile->objfile_obstack, priv->size); bfd_get_section_contents (sec->objfile->obfd, bfdsec, priv->contents, 0, priv->size); /* In general, the .pdr section is sorted. However, in the presence of multiple code sections (and other corner cases) it can become unsorted. Sort it so that we can use a faster binary search. */ qsort (priv->contents, priv->size / 32, 32, compare_pdr_entries); } else priv->size = 0; set_objfile_data (sec->objfile, mips_pdr_data, priv); } the_bfd = NULL; if (priv->size != 0) { int low, mid, high; char *ptr; CORE_ADDR pdr_pc; low = 0; high = priv->size / 32; /* We've found a .pdr section describing this objfile. We want to find the entry which describes this code address. The .pdr information is not very descriptive; we have only a function start address. We have to look for the closest entry, because the local symbol at the beginning of this function may have been stripped - so if we ask the symbol table for the start address we may get a preceding global function. */ /* First, find the last .pdr entry starting at or before PC. */ do { mid = (low + high) / 2; ptr = priv->contents + mid * 32; pdr_pc = bfd_get_signed_32 (sec->objfile->obfd, ptr); pdr_pc += ANOFFSET (sec->objfile->section_offsets, SECT_OFF_TEXT (sec->objfile)); if (pdr_pc > pc) high = mid; else low = mid + 1; } while (low != high); /* Both low and high point one past the PDR of interest. If both are zero, that means this PC is before any region covered by a PDR, i.e. pdr_pc for the first PDR entry is greater than PC. */ if (low > 0) { ptr = priv->contents + (low - 1) * 32; pdr_pc = bfd_get_signed_32 (sec->objfile->obfd, ptr); pdr_pc += ANOFFSET (sec->objfile->section_offsets, SECT_OFF_TEXT (sec->objfile)); } /* We don't have a range, so we have no way to know for sure whether we're in the correct PDR or a PDR for a preceding function and the current function was a stripped local symbol. But if the PDR's PC is at least as great as the best guess from the symbol table, assume that it does cover the right area; if a .pdr section is present at all then nearly every function will have an entry. The biggest exception will be the dynamic linker stubs; conveniently these are placed before .text instead of after. */ if (pc >= pdr_pc && pdr_pc >= startaddr) { struct symbol *sym = find_pc_function (pc); if (addrptr) *addrptr = pdr_pc; /* Fill in what we need of the proc_desc. */ proc_desc = (struct mdebug_extra_func_info *) obstack_alloc (&sec->objfile->objfile_obstack, sizeof (struct mdebug_extra_func_info)); PROC_LOW_ADDR (proc_desc) = pdr_pc; PROC_FRAME_OFFSET (proc_desc) = bfd_get_signed_32 (sec->objfile->obfd, ptr + 20); PROC_FRAME_REG (proc_desc) = bfd_get_32 (sec->objfile->obfd, ptr + 24); PROC_REG_MASK (proc_desc) = bfd_get_32 (sec->objfile->obfd, ptr + 4); PROC_FREG_MASK (proc_desc) = bfd_get_32 (sec->objfile->obfd, ptr + 12); PROC_REG_OFFSET (proc_desc) = bfd_get_signed_32 (sec->objfile->obfd, ptr + 8); PROC_FREG_OFFSET (proc_desc) = bfd_get_signed_32 (sec->objfile->obfd, ptr + 16); PROC_PC_REG (proc_desc) = bfd_get_32 (sec->objfile->obfd, ptr + 28); proc_desc->pdr.isym = (long) sym; return proc_desc; } } } if (b == NULL) return NULL; if (startaddr > BLOCK_START (b)) { /* This is the "pathological" case referred to in a comment in print_frame_info. It might be better to move this check into symbol reading. */ return NULL; } sym = lookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, LABEL_DOMAIN, 0); /* If we never found a PDR for this function in symbol reading, then examine prologues to find the information. */ if (sym) { proc_desc = (struct mdebug_extra_func_info *) SYMBOL_VALUE (sym); if (PROC_FRAME_REG (proc_desc) == -1) return NULL; else return proc_desc; } else return NULL; }
void bb_read_rec (FILE *ifp, const char *filename) { unsigned int nblocks, b; bfd_vma addr, ncalls; Sym *sym; if (gmon_io_read_32 (ifp, &nblocks)) { fprintf (stderr, _("%s: %s: unexpected end of file\n"), whoami, filename); done (1); } nblocks = bfd_get_32 (core_bfd, (bfd_byte *) & nblocks); if (gmon_file_version == 0) fskip_string (ifp); for (b = 0; b < nblocks; ++b) { if (gmon_file_version == 0) { int line_num; /* Version 0 had lots of extra stuff that we don't care about anymore. */ if ((fread (&ncalls, sizeof (ncalls), 1, ifp) != 1) || (fread (&addr, sizeof (addr), 1, ifp) != 1) || (fskip_string (ifp), FALSE) || (fskip_string (ifp), FALSE) || (fread (&line_num, sizeof (line_num), 1, ifp) != 1)) { perror (filename); done (1); } } else if (gmon_io_read_vma (ifp, &addr) || gmon_io_read_vma (ifp, &ncalls)) { perror (filename); done (1); } /* Basic-block execution counts are meaningful only if we're profiling at the line-by-line level: */ if (line_granularity) { sym = sym_lookup (&symtab, addr); if (sym) { int i; DBG (BBDEBUG, printf ("[bb_read_rec] 0x%lx->0x%lx (%s:%d) cnt=%lu\n", (unsigned long) addr, (unsigned long) sym->addr, sym->name, sym->line_num, (unsigned long) ncalls)); for (i = 0; i < NBBS; i++) { if (! sym->bb_addr[i] || sym->bb_addr[i] == addr) { sym->bb_addr[i] = addr; sym->bb_calls[i] += ncalls; break; } } } } else { static bfd_boolean user_warned = FALSE; if (!user_warned) { user_warned = TRUE; fprintf (stderr, _("%s: warning: ignoring basic-block exec counts (use -l or --line)\n"), whoami); } } } return; }
bfd_boolean _bfd_link_section_stabs (bfd *abfd, struct stab_info *sinfo, asection *stabsec, asection *stabstrsec, void * *psecinfo, bfd_size_type *pstring_offset) { bfd_boolean first; bfd_size_type count, amt; struct stab_section_info *secinfo; bfd_byte *stabbuf = NULL; bfd_byte *stabstrbuf = NULL; bfd_byte *sym, *symend; bfd_size_type stroff, next_stroff, skip; bfd_size_type *pstridx; if (stabsec->size == 0 || stabstrsec->size == 0) /* This file does not contain stabs debugging information. */ return TRUE; if (stabsec->size % STABSIZE != 0) /* Something is wrong with the format of these stab symbols. Don't try to optimize them. */ return TRUE; if ((stabstrsec->flags & SEC_RELOC) != 0) /* We shouldn't see relocations in the strings, and we aren't prepared to handle them. */ return TRUE; if (bfd_is_abs_section (stabsec->output_section) || bfd_is_abs_section (stabstrsec->output_section)) /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return TRUE; first = FALSE; if (sinfo->stabstr == NULL) { flagword flags; /* Initialize the stabs information we need to keep track of. */ first = TRUE; sinfo->strings = _bfd_stringtab_init (); if (sinfo->strings == NULL) goto error_return; /* Make sure the first byte is zero. */ (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE); if (! bfd_hash_table_init (&sinfo->includes, stab_link_includes_newfunc, sizeof (struct stab_link_includes_entry))) goto error_return; flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING | SEC_LINKER_CREATED); sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr", flags); if (sinfo->stabstr == NULL) goto error_return; } /* Initialize the information we are going to store for this .stab section. */ count = stabsec->size / STABSIZE; amt = sizeof (struct stab_section_info); amt += (count - 1) * sizeof (bfd_size_type); *psecinfo = bfd_alloc (abfd, amt); if (*psecinfo == NULL) goto error_return; secinfo = (struct stab_section_info *) *psecinfo; secinfo->excls = NULL; stabsec->rawsize = stabsec->size; secinfo->cumulative_skips = NULL; memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type)); /* Read the stabs information from abfd. */ if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf) || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf)) goto error_return; /* Look through the stabs symbols, work out the new string indices, and identify N_BINCL symbols which can be eliminated. */ stroff = 0; /* The stabs sections can be split when -split-by-reloc/-split-by-file is used. We must keep track of each stab section's place in the single concatenated string table. */ next_stroff = pstring_offset ? *pstring_offset : 0; skip = 0; symend = stabbuf + stabsec->size; for (sym = stabbuf, pstridx = secinfo->stridxs; sym < symend; sym += STABSIZE, ++pstridx) { bfd_size_type symstroff; int type; const char *string; if (*pstridx != 0) /* This symbol has already been handled by an N_BINCL pass. */ continue; type = sym[TYPEOFF]; if (type == 0) { /* Special type 0 stabs indicate the offset to the next string table. We only copy the very first one. */ stroff = next_stroff; next_stroff += bfd_get_32 (abfd, sym + 8); if (pstring_offset) *pstring_offset = next_stroff; if (! first) { *pstridx = (bfd_size_type) -1; ++skip; continue; } first = FALSE; } /* Store the string in the hash table, and record the index. */ symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF); if (symstroff >= stabstrsec->size) { _bfd_error_handler /* xgettext:c-format */ (_("%B(%A+0x%lx): Stabs entry has invalid string index."), abfd, stabsec, (long) (sym - stabbuf)); bfd_set_error (bfd_error_bad_value); goto error_return; } string = (char *) stabstrbuf + symstroff; *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE); /* An N_BINCL symbol indicates the start of the stabs entries for a header file. We need to scan ahead to the next N_EINCL symbol, ignoring nesting, adding up all the characters in the symbol names, not including the file numbers in types (the first number after an open parenthesis). */ if (type == (int) N_BINCL) { bfd_vma sum_chars; bfd_vma num_chars; bfd_vma buf_len = 0; char * symb; char * symb_rover; int nest; bfd_byte * incl_sym; struct stab_link_includes_entry * incl_entry; struct stab_link_includes_totals * t; struct stab_excl_list * ne; symb = symb_rover = NULL; sum_chars = num_chars = 0; nest = 0; for (incl_sym = sym + STABSIZE; incl_sym < symend; incl_sym += STABSIZE) { int incl_type; incl_type = incl_sym[TYPEOFF]; if (incl_type == 0) break; else if (incl_type == (int) N_EXCL) continue; else if (incl_type == (int) N_EINCL) { if (nest == 0) break; --nest; } else if (incl_type == (int) N_BINCL) ++nest; else if (nest == 0) { const char *str; str = ((char *) stabstrbuf + stroff + bfd_get_32 (abfd, incl_sym + STRDXOFF)); for (; *str != '\0'; str++) { if (num_chars >= buf_len) { buf_len += 32 * 1024; symb = (char *) bfd_realloc_or_free (symb, buf_len); if (symb == NULL) goto error_return; symb_rover = symb + num_chars; } * symb_rover ++ = * str; sum_chars += *str; num_chars ++; if (*str == '(') { /* Skip the file number. */ ++str; while (ISDIGIT (*str)) ++str; --str; } } } } BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb)); /* If we have already included a header file with the same value, then replaced this one with an N_EXCL symbol. */ incl_entry = (struct stab_link_includes_entry * ) bfd_hash_lookup (&sinfo->includes, string, TRUE, TRUE); if (incl_entry == NULL) goto error_return; for (t = incl_entry->totals; t != NULL; t = t->next) if (t->sum_chars == sum_chars && t->num_chars == num_chars && memcmp (t->symb, symb, num_chars) == 0) break; /* Record this symbol, so that we can set the value correctly. */ amt = sizeof *ne; ne = (struct stab_excl_list *) bfd_alloc (abfd, amt); if (ne == NULL) goto error_return; ne->offset = sym - stabbuf; ne->val = sum_chars; ne->type = (int) N_BINCL; ne->next = secinfo->excls; secinfo->excls = ne; if (t == NULL) { /* This is the first time we have seen this header file with this set of stabs strings. */ t = (struct stab_link_includes_totals *) bfd_hash_allocate (&sinfo->includes, sizeof *t); if (t == NULL) goto error_return; t->sum_chars = sum_chars; t->num_chars = num_chars; /* Trim data down. */ t->symb = symb = (char *) bfd_realloc_or_free (symb, num_chars); t->next = incl_entry->totals; incl_entry->totals = t; } else { bfd_size_type *incl_pstridx; /* We have seen this header file before. Tell the final pass to change the type to N_EXCL. */ ne->type = (int) N_EXCL; /* Free off superfluous symbols. */ free (symb); /* Mark the skipped symbols. */ nest = 0; for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1; incl_sym < symend; incl_sym += STABSIZE, ++incl_pstridx) { int incl_type; incl_type = incl_sym[TYPEOFF]; if (incl_type == (int) N_EINCL) { if (nest == 0) { *incl_pstridx = (bfd_size_type) -1; ++skip; break; } --nest; } else if (incl_type == (int) N_BINCL) ++nest; else if (incl_type == (int) N_EXCL) /* Keep existing exclusion marks. */ continue; else if (nest == 0) { *incl_pstridx = (bfd_size_type) -1; ++skip; } } } } } free (stabbuf); stabbuf = NULL; free (stabstrbuf); stabstrbuf = NULL; /* We need to set the section sizes such that the linker will compute the output section sizes correctly. We set the .stab size to not include the entries we don't want. We set SEC_EXCLUDE for the .stabstr section, so that it will be dropped from the link. We record the size of the strtab in the first .stabstr section we saw, and make sure we don't set SEC_EXCLUDE for that section. */ stabsec->size = (count - skip) * STABSIZE; if (stabsec->size == 0) stabsec->flags |= SEC_EXCLUDE | SEC_KEEP; stabstrsec->flags |= SEC_EXCLUDE | SEC_KEEP; sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings); /* Calculate the `cumulative_skips' array now that stabs have been deleted for this section. */ if (skip != 0) { bfd_size_type i, offset; bfd_size_type *pskips; amt = count * sizeof (bfd_size_type); secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt); if (secinfo->cumulative_skips == NULL) goto error_return; pskips = secinfo->cumulative_skips; pstridx = secinfo->stridxs; offset = 0; for (i = 0; i < count; i++, pskips++, pstridx++) { *pskips = offset; if (*pstridx == (bfd_size_type) -1) offset += STABSIZE; } BFD_ASSERT (offset != 0); } return TRUE; error_return: if (stabbuf != NULL) free (stabbuf); if (stabstrbuf != NULL) free (stabstrbuf); return FALSE; }
static void pex64_xdata_print_uwd_codes (FILE *file, bfd *abfd, struct pex64_unwind_info *ui, struct pex64_runtime_function *rf) { unsigned int i; unsigned int tmp; /* At least 32 bits. */ int save_allowed; if (ui->CountOfCodes == 0 || ui->rawUnwindCodes == NULL) return; /* According to UNWIND_CODE documentation: If an FP reg is used, the any unwind code taking an offset must only be used after the FP reg is established in the prolog. But there are counter examples of that in system dlls... */ save_allowed = TRUE; i = 0; if (ui->Version == 2 && PEX64_UNWCODE_CODE (ui->rawUnwindCodes[1]) == UWOP_EPILOG) { /* Display epilog opcode (whose docoding is not fully documented). Looks to be designed to speed-up unwinding, as there is no need to decode instruction flow if outside an epilog. */ unsigned int func_size = rf->rva_EndAddress - rf->rva_BeginAddress; fprintf (file, "\tv2 epilog (length: %02x) at pc+:", ui->rawUnwindCodes[0]); if (PEX64_UNWCODE_INFO (ui->rawUnwindCodes[1])) fprintf (file, " 0x%x", func_size - ui->rawUnwindCodes[0]); i++; for (; i < ui->CountOfCodes; i++) { const bfd_byte *dta = ui->rawUnwindCodes + 2 * i; unsigned int off; if (PEX64_UNWCODE_CODE (dta[1]) != UWOP_EPILOG) break; off = dta[0] | (PEX64_UNWCODE_INFO (dta[1]) << 8); if (off == 0) fprintf (file, " [pad]"); else fprintf (file, " 0x%x", func_size - off); } fputc ('\n', file); } for (; i < ui->CountOfCodes; i++) { const bfd_byte *dta = ui->rawUnwindCodes + 2 * i; unsigned int info = PEX64_UNWCODE_INFO (dta[1]); int unexpected = FALSE; fprintf (file, "\t pc+0x%02x: ", (unsigned int) dta[0]); switch (PEX64_UNWCODE_CODE (dta[1])) { case UWOP_PUSH_NONVOL: fprintf (file, "push %s", pex_regs[info]); break; case UWOP_ALLOC_LARGE: if (info == 0) { tmp = bfd_get_16 (abfd, &dta[2]) * 8; i++; } else { tmp = bfd_get_32 (abfd, &dta[2]); i += 2; } fprintf (file, "alloc large area: rsp = rsp - 0x%x", tmp); break; case UWOP_ALLOC_SMALL: fprintf (file, "alloc small area: rsp = rsp - 0x%x", (info + 1) * 8); break; case UWOP_SET_FPREG: /* According to the documentation, info field is unused. */ fprintf (file, "FPReg: %s = rsp + 0x%x (info = 0x%x)", pex_regs[ui->FrameRegister], (unsigned int) ui->FrameOffset * 16, info); unexpected = ui->FrameRegister == 0; save_allowed = FALSE; break; case UWOP_SAVE_NONVOL: tmp = bfd_get_16 (abfd, &dta[2]) * 8; i++; fprintf (file, "save %s at rsp + 0x%x", pex_regs[info], tmp); unexpected = !save_allowed; break; case UWOP_SAVE_NONVOL_FAR: tmp = bfd_get_32 (abfd, &dta[2]); i += 2; fprintf (file, "save %s at rsp + 0x%x", pex_regs[info], tmp); unexpected = !save_allowed; break; case UWOP_SAVE_XMM: if (ui->Version == 1) { tmp = bfd_get_16 (abfd, &dta[2]) * 8; i++; fprintf (file, "save mm%u at rsp + 0x%x", info, tmp); unexpected = !save_allowed; } else if (ui->Version == 2) { fprintf (file, "epilog %02x %01x", dta[0], info); unexpected = TRUE; } break; case UWOP_SAVE_XMM_FAR: tmp = bfd_get_32 (abfd, &dta[2]) * 8; i += 2; fprintf (file, "save mm%u at rsp + 0x%x", info, tmp); unexpected = !save_allowed; break; case UWOP_SAVE_XMM128: tmp = bfd_get_16 (abfd, &dta[2]) * 16; i++; fprintf (file, "save xmm%u at rsp + 0x%x", info, tmp); unexpected = !save_allowed; break; case UWOP_SAVE_XMM128_FAR: tmp = bfd_get_32 (abfd, &dta[2]) * 16; i += 2; fprintf (file, "save xmm%u at rsp + 0x%x", info, tmp); unexpected = !save_allowed; break; case UWOP_PUSH_MACHFRAME: fprintf (file, "interrupt entry (SS, old RSP, EFLAGS, CS, RIP"); if (info == 0) fprintf (file, ")"); else if (info == 1) fprintf (file, ",ErrorCode)"); else fprintf (file, ", unknown(%u))", info); break; default: /* PR 17512: file: 2245-7442-0.004. */ fprintf (file, _("Unknown: %x"), PEX64_UNWCODE_CODE (dta[1])); break; } if (unexpected) fprintf (file, " [Unexpected!]"); fputc ('\n', file); } }
/* * On the Alpha we can only detect PC relative calls, which are * usually generated for calls to functions within the same * object file only. This is still better than nothing, however. * (In particular it should be possible to find functions that * potentially call integer division routines, for example.) */ void alpha_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc) { bfd_vma pc, dest_pc; unsigned int insn; Sym *child; if (indirect_child.name == NULL) { sym_init (&indirect_child); indirect_child.name = _("<indirect child>"); indirect_child.cg.prop.fract = 1.0; indirect_child.cg.cyc.head = &indirect_child; } DBG (CALLDEBUG, printf (_("[find_call] %s: 0x%lx to 0x%lx\n"), parent->name, (unsigned long) p_lowpc, (unsigned long) p_highpc)); for (pc = (p_lowpc + 3) & ~(bfd_vma) 3; pc < p_highpc; pc += 4) { insn = bfd_get_32 (core_bfd[0], ((unsigned char *) core_text_space + pc - core_text_sect->vma)); switch (insn & (0x3f << 26)) { case OP_Jxx << 26: /* * There is no simple and reliable way to determine the * target of a jsr (the hint bits help, but there aren't * enough bits to get a satisfactory hit rate). Instead, * for any indirect jump we simply add an arc from PARENT * to INDIRECT_CHILD---that way the user it at least able * to see that there are other calls as well. */ if ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14 || (insn & (3 << 14)) == Jxx_FUNC_JSR_COROUTINE << 14) { DBG (CALLDEBUG, printf (_("[find_call] 0x%lx: jsr%s <indirect_child>\n"), (unsigned long) pc, ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14 ? "" : "_coroutine"))); arc_add (parent, &indirect_child, (unsigned long) 0); } break; case OP_BSR << 26: DBG (CALLDEBUG, printf (_("[find_call] 0x%lx: bsr"), (unsigned long) pc)); /* * Regular PC relative addressing. Check that this is the * address of a function. The linker sometimes redirects * the entry point by 8 bytes to skip loading the global * pointer, so we allow for either address: */ dest_pc = pc + 4 + (((bfd_signed_vma) (insn & 0x1fffff) ^ 0x100000) - 0x100000); if (hist_check_address (dest_pc)) { child = sym_lookup (&symtab, dest_pc); if (child) { DBG (CALLDEBUG, printf (" 0x%lx\t; name=%s, addr=0x%lx", (unsigned long) dest_pc, child->name, (unsigned long) child->addr)); if (child->addr == dest_pc || child->addr == dest_pc - 8) { DBG (CALLDEBUG, printf ("\n")); /* a hit: */ arc_add (parent, child, (unsigned long) 0); continue; } } } /* * Something funny going on. */ DBG (CALLDEBUG, printf ("\tbut it's a botch\n")); break; default: break; } } }
/* Insert the addend/value into the instruction or data object being relocated. */ bfd_reloc_status_type _bfd_aarch64_elf_put_addend (bfd *abfd, bfd_byte *address, bfd_reloc_code_real_type r_type, reloc_howto_type *howto, bfd_signed_vma addend) { bfd_reloc_status_type status = bfd_reloc_ok; bfd_signed_vma old_addend = addend; bfd_vma contents; int size; size = bfd_get_reloc_size (howto); switch (size) { case 2: contents = bfd_get_16 (abfd, address); break; case 4: if (howto->src_mask != 0xffffffff) /* Must be 32-bit instruction, always little-endian. */ contents = bfd_getl32 (address); else /* Must be 32-bit data (endianness dependent). */ contents = bfd_get_32 (abfd, address); break; case 8: contents = bfd_get_64 (abfd, address); break; default: abort (); } switch (howto->complain_on_overflow) { case complain_overflow_dont: break; case complain_overflow_signed: status = aarch64_signed_overflow (addend, howto->bitsize + howto->rightshift); break; case complain_overflow_unsigned: status = aarch64_unsigned_overflow (addend, howto->bitsize + howto->rightshift); break; case complain_overflow_bitfield: default: abort (); } addend >>= howto->rightshift; switch (r_type) { case BFD_RELOC_AARCH64_JUMP26: case BFD_RELOC_AARCH64_CALL26: contents = reencode_branch_ofs_26 (contents, addend); break; case BFD_RELOC_AARCH64_BRANCH19: contents = reencode_cond_branch_ofs_19 (contents, addend); break; case BFD_RELOC_AARCH64_TSTBR14: contents = reencode_tst_branch_ofs_14 (contents, addend); break; case BFD_RELOC_AARCH64_LD_LO19_PCREL: case BFD_RELOC_AARCH64_GOT_LD_PREL19: if (old_addend & ((1 << howto->rightshift) - 1)) return bfd_reloc_overflow; contents = reencode_ld_lit_ofs_19 (contents, addend); break; case BFD_RELOC_AARCH64_TLSDESC_CALL: break; case BFD_RELOC_AARCH64_TLSGD_ADR_PAGE21: case BFD_RELOC_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: case BFD_RELOC_AARCH64_TLSDESC_ADR_PAGE21: case BFD_RELOC_AARCH64_ADR_GOT_PAGE: case BFD_RELOC_AARCH64_ADR_LO21_PCREL: case BFD_RELOC_AARCH64_ADR_HI21_PCREL: case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL: contents = reencode_adr_imm (contents, addend); break; case BFD_RELOC_AARCH64_TLSGD_ADD_LO12_NC: case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12: case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_HI12: case BFD_RELOC_AARCH64_TLSLE_ADD_TPREL_LO12_NC: case BFD_RELOC_AARCH64_TLSDESC_ADD_LO12_NC: case BFD_RELOC_AARCH64_ADD_LO12: /* Corresponds to: add rd, rn, #uimm12 to provide the low order 12 bits of the page offset following BFD_RELOC_AARCH64_ADR_HI21_PCREL which computes the (pc-relative) page base. */ contents = reencode_add_imm (contents, addend); break; case BFD_RELOC_AARCH64_LDST8_LO12: case BFD_RELOC_AARCH64_LDST16_LO12: case BFD_RELOC_AARCH64_LDST32_LO12: case BFD_RELOC_AARCH64_LDST64_LO12: case BFD_RELOC_AARCH64_LDST128_LO12: case BFD_RELOC_AARCH64_TLSDESC_LD64_LO12_NC: case BFD_RELOC_AARCH64_TLSDESC_LD32_LO12_NC: case BFD_RELOC_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: case BFD_RELOC_AARCH64_TLSIE_LD32_GOTTPREL_LO12_NC: case BFD_RELOC_AARCH64_LD64_GOT_LO12_NC: case BFD_RELOC_AARCH64_LD32_GOT_LO12_NC: if (old_addend & ((1 << howto->rightshift) - 1)) return bfd_reloc_overflow; /* Used for ldr*|str* rt, [rn, #uimm12] to provide the low order 12 bits of the page offset following BFD_RELOC_AARCH64_ADR_HI21_PCREL which computes the (pc-relative) page base. */ contents = reencode_ldst_pos_imm (contents, addend); break; /* Group relocations to create high bits of a 16, 32, 48 or 64 bit signed data or abs address inline. Will change instruction to MOVN or MOVZ depending on sign of calculated value. */ case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G2: case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1: case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G1_NC: case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0: case BFD_RELOC_AARCH64_TLSLE_MOVW_TPREL_G0_NC: case BFD_RELOC_AARCH64_MOVW_G0_S: case BFD_RELOC_AARCH64_MOVW_G1_S: case BFD_RELOC_AARCH64_MOVW_G2_S: /* NOTE: We can only come here with movz or movn. */ if (addend < 0) { /* Force use of MOVN. */ addend = ~addend; contents = reencode_movzn_to_movn (contents); } else { /* Force use of MOVZ. */ contents = reencode_movzn_to_movz (contents); } /* fall through */ /* Group relocations to create a 16, 32, 48 or 64 bit unsigned data or abs address inline. */ case BFD_RELOC_AARCH64_MOVW_G0: case BFD_RELOC_AARCH64_MOVW_G0_NC: case BFD_RELOC_AARCH64_MOVW_G1: case BFD_RELOC_AARCH64_MOVW_G1_NC: case BFD_RELOC_AARCH64_MOVW_G2: case BFD_RELOC_AARCH64_MOVW_G2_NC: case BFD_RELOC_AARCH64_MOVW_G3: contents = reencode_movw_imm (contents, addend); break; default: /* Repack simple data */ if (howto->dst_mask & (howto->dst_mask + 1)) return bfd_reloc_notsupported; contents = ((contents & ~howto->dst_mask) | (addend & howto->dst_mask)); break; } switch (size) { case 2: bfd_put_16 (abfd, contents, address); break; case 4: if (howto->dst_mask != 0xffffffff) /* must be 32-bit instruction, always little-endian */ bfd_putl32 (contents, address); else /* must be 32-bit data (endianness dependent) */ bfd_put_32 (abfd, contents, address); break; case 8: bfd_put_64 (abfd, contents, address); break; default: abort (); } return status; }
static bfd_boolean parse_line_table (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit) { bfd_byte *xptr; /* Load the ".line" section from the bfd if we haven't already. */ if (stash->line_section == 0) { asection *msec; bfd_size_type size; msec = bfd_get_section_by_name (stash->abfd, ".line"); if (! msec) return FALSE; size = msec->rawsize ? msec->rawsize : msec->size; stash->line_section = bfd_simple_get_relocated_section_contents (stash->abfd, msec, NULL, stash->syms); if (! stash->line_section) return FALSE; stash->line_section_end = stash->line_section + size; } xptr = stash->line_section + aUnit->stmt_list_offset; if (xptr < stash->line_section_end) { unsigned long eachLine; bfd_byte *tblend; unsigned long base; bfd_size_type amt; /* First comes the length. */ tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr; xptr += 4; /* Then the base address for each address in the table. */ base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr); xptr += 4; /* How many line entrys? 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */ aUnit->line_count = (tblend - xptr) / 10; /* Allocate an array for the entries. */ amt = sizeof (struct linenumber) * aUnit->line_count; aUnit->linenumber_table = (struct linenumber *) bfd_alloc (stash->abfd, amt); if (!aUnit->linenumber_table) return FALSE; for (eachLine = 0; eachLine < aUnit->line_count; eachLine++) { /* A line number. */ aUnit->linenumber_table[eachLine].linenumber = bfd_get_32 (stash->abfd, (bfd_byte *) xptr); xptr += 4; /* Skip the position within the line. */ xptr += 2; /* And finally the address. */ aUnit->linenumber_table[eachLine].addr = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr); xptr += 4; } } return TRUE; }
static bfd_boolean parse_die (bfd * abfd, struct die_info * aDieInfo, bfd_byte * aDiePtr, bfd_byte * aDiePtrEnd) { bfd_byte *this_die = aDiePtr; bfd_byte *xptr = this_die; memset (aDieInfo, 0, sizeof (* aDieInfo)); /* First comes the length. */ aDieInfo->length = bfd_get_32 (abfd, (bfd_byte *) xptr); xptr += 4; if (aDieInfo->length == 0 || (this_die + aDieInfo->length) >= aDiePtrEnd) return FALSE; if (aDieInfo->length < 6) { /* Just padding bytes. */ aDieInfo->tag = TAG_padding; return TRUE; } /* Then the tag. */ aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr); xptr += 2; /* Then the attributes. */ while (xptr < (this_die + aDieInfo->length)) { unsigned short attr; /* Parse the attribute based on its form. This section must handle all dwarf1 forms, but need only handle the actual attributes that we care about. */ attr = bfd_get_16 (abfd, (bfd_byte *) xptr); xptr += 2; switch (FORM_FROM_ATTR (attr)) { case FORM_DATA2: xptr += 2; break; case FORM_DATA4: case FORM_REF: if (attr == AT_sibling) aDieInfo->sibling = bfd_get_32 (abfd, (bfd_byte *) xptr); else if (attr == AT_stmt_list) { aDieInfo->stmt_list_offset = bfd_get_32 (abfd, (bfd_byte *) xptr); aDieInfo->has_stmt_list = 1; } xptr += 4; break; case FORM_DATA8: xptr += 8; break; case FORM_ADDR: if (attr == AT_low_pc) aDieInfo->low_pc = bfd_get_32 (abfd, (bfd_byte *) xptr); else if (attr == AT_high_pc) aDieInfo->high_pc = bfd_get_32 (abfd, (bfd_byte *) xptr); xptr += 4; break; case FORM_BLOCK2: xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr); break; case FORM_BLOCK4: xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr); break; case FORM_STRING: if (attr == AT_name) aDieInfo->name = (char *) xptr; xptr += strlen ((char *) xptr) + 1; break; } } return TRUE; }
bfd_boolean _bfd_discard_section_stabs (bfd *abfd, asection *stabsec, void * psecinfo, bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *), void * cookie) { bfd_size_type count, amt; struct stab_section_info *secinfo; bfd_byte *stabbuf = NULL; bfd_byte *sym, *symend; bfd_size_type skip; bfd_size_type *pstridx; int deleting; if (stabsec->size == 0) /* This file does not contain stabs debugging information. */ return FALSE; if (stabsec->size % STABSIZE != 0) /* Something is wrong with the format of these stab symbols. Don't try to optimize them. */ return FALSE; if ((stabsec->output_section != NULL && bfd_is_abs_section (stabsec->output_section))) /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return FALSE; /* We should have initialized our data in _bfd_link_section_stabs. If there was some bizarre error reading the string sections, though, we might not have. Bail rather than asserting. */ if (psecinfo == NULL) return FALSE; count = stabsec->rawsize / STABSIZE; secinfo = (struct stab_section_info *) psecinfo; /* Read the stabs information from abfd. */ if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf)) goto error_return; /* Look through the stabs symbols and discard any information for discarded functions. */ skip = 0; deleting = -1; symend = stabbuf + stabsec->rawsize; for (sym = stabbuf, pstridx = secinfo->stridxs; sym < symend; sym += STABSIZE, ++pstridx) { int type; if (*pstridx == (bfd_size_type) -1) /* This stab was deleted in a previous pass. */ continue; type = sym[TYPEOFF]; if (type == (int) N_FUN) { int strx = bfd_get_32 (abfd, sym + STRDXOFF); if (strx == 0) { if (deleting) { skip++; *pstridx = -1; } deleting = -1; continue; } deleting = 0; if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie)) deleting = 1; } if (deleting == 1) { *pstridx = -1; skip++; } else if (deleting == -1) { /* Outside of a function. Check for deleted variables. */ if (type == (int) N_STSYM || type == (int) N_LCSYM) if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie)) { *pstridx = -1; skip ++; } /* We should also check for N_GSYM entries which reference a deleted global, but those are less harmful to debuggers and would require parsing the stab strings. */ } } free (stabbuf); stabbuf = NULL; /* Shrink the stabsec as needed. */ stabsec->size -= skip * STABSIZE; if (stabsec->size == 0) stabsec->flags |= SEC_EXCLUDE | SEC_KEEP; /* Recalculate the `cumulative_skips' array now that stabs have been deleted for this section. */ if (skip != 0) { bfd_size_type i, offset; bfd_size_type *pskips; if (secinfo->cumulative_skips == NULL) { amt = count * sizeof (bfd_size_type); secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt); if (secinfo->cumulative_skips == NULL) goto error_return; } pskips = secinfo->cumulative_skips; pstridx = secinfo->stridxs; offset = 0; for (i = 0; i < count; i++, pskips++, pstridx++) { *pskips = offset; if (*pstridx == (bfd_size_type) -1) offset += STABSIZE; } BFD_ASSERT (offset != 0); } return skip > 0; error_return: if (stabbuf != NULL) free (stabbuf); return FALSE; }
static const bfd_target * cisco_core_file_validate (bfd *abfd, int crash_info_loc) { char buf[4]; unsigned int crashinfo_offset; crashinfo_external crashinfo; bfd_size_type nread; unsigned int magic; unsigned int version; unsigned int rambase; sec_ptr asect; struct stat statbuf; bfd_size_type amt; flagword flags; if (bfd_seek (abfd, (file_ptr) crash_info_loc, SEEK_SET) != 0) return NULL; nread = bfd_bread (buf, (bfd_size_type) 4, abfd); if (nread != 4) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return NULL; } crashinfo_offset = MASK_ADDR (bfd_get_32 (abfd, buf)); if (bfd_seek (abfd, (file_ptr) crashinfo_offset, SEEK_SET) != 0) { /* Most likely we failed because of a bogus (huge) offset */ bfd_set_error (bfd_error_wrong_format); return NULL; } nread = bfd_bread (&crashinfo, (bfd_size_type) sizeof (crashinfo), abfd); if (nread != sizeof (crashinfo)) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return NULL; } if (bfd_stat (abfd, &statbuf) < 0) { bfd_set_error (bfd_error_system_call); return NULL; } magic = bfd_get_32 (abfd, crashinfo.magic); if (magic != CRASH_MAGIC) { bfd_set_error (bfd_error_wrong_format); return NULL; } version = bfd_get_32 (abfd, crashinfo.version); if (version == 0) { bfd_set_error (bfd_error_wrong_format); return NULL; } else if (version == 1) { /* V1 core dumps don't specify the dump base, assume 0 */ rambase = 0; } else { rambase = bfd_get_32 (abfd, crashinfo.rambase); } /* OK, we believe you. You're a core file. */ amt = sizeof (struct cisco_core_struct); abfd->tdata.cisco_core_data = (struct cisco_core_struct *) bfd_zmalloc (amt); if (abfd->tdata.cisco_core_data == NULL) return NULL; switch ((crashreason) bfd_get_32 (abfd, crashinfo.reason)) { case CRASH_REASON_NOTCRASHED: /* Crash file probably came from write core. */ abfd->tdata.cisco_core_data->sig = 0; break; case CRASH_REASON_CORRUPT: /* The crash context area was corrupt -- proceed with caution. We have no way of passing this information back to the caller. */ abfd->tdata.cisco_core_data->sig = 0; break; case CRASH_REASON_EXCEPTION: /* Crash occured due to CPU exception. */ /* This is 68k-specific; for MIPS we'll need to interpret cpu_vector differently based on the target configuration (since CISCO core files don't seem to have the processor encoded in them). */ switch (bfd_get_32 (abfd, crashinfo.cpu_vector)) { /* bus error */ case 2 : abfd->tdata.cisco_core_data->sig = SIGBUS; break; /* address error */ case 3 : abfd->tdata.cisco_core_data->sig = SIGBUS; break; /* illegal instruction */ case 4 : abfd->tdata.cisco_core_data->sig = SIGILL; break; /* zero divide */ case 5 : abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* chk instruction */ case 6 : abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* trapv instruction */ case 7 : abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* privilege violation */ case 8 : abfd->tdata.cisco_core_data->sig = SIGSEGV; break; /* trace trap */ case 9 : abfd->tdata.cisco_core_data->sig = SIGTRAP; break; /* line 1010 emulator */ case 10: abfd->tdata.cisco_core_data->sig = SIGILL; break; /* line 1111 emulator */ case 11: abfd->tdata.cisco_core_data->sig = SIGILL; break; /* Coprocessor protocol violation. Using a standard MMU or FPU this cannot be triggered by software. Call it a SIGBUS. */ case 13: abfd->tdata.cisco_core_data->sig = SIGBUS; break; /* interrupt */ case 31: abfd->tdata.cisco_core_data->sig = SIGINT; break; /* breakpoint */ case 33: abfd->tdata.cisco_core_data->sig = SIGTRAP; break; /* floating point err */ case 48: abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* floating point err */ case 49: abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* zero divide */ case 50: abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* underflow */ case 51: abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* operand error */ case 52: abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* overflow */ case 53: abfd->tdata.cisco_core_data->sig = SIGFPE; break; /* NAN */ case 54: abfd->tdata.cisco_core_data->sig = SIGFPE; break; default: #ifndef SIGEMT #define SIGEMT SIGTRAP #endif /* "software generated"*/ abfd->tdata.cisco_core_data->sig = SIGEMT; } break; default: /* Unknown crash reason. */ abfd->tdata.cisco_core_data->sig = 0; break; } /* Create a ".data" section that maps the entire file, which is essentially a dump of the target system's RAM. */ flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; asect = bfd_make_section_anyway_with_flags (abfd, ".data", flags); if (asect == NULL) goto error_return; /* The size of memory is the size of the core file itself. */ asect->size = statbuf.st_size; asect->vma = rambase; asect->filepos = 0; /* Create a ".crash" section to allow access to the saved crash information. */ flags = SEC_HAS_CONTENTS; asect = bfd_make_section_anyway_with_flags (abfd, ".crash", flags); if (asect == NULL) goto error_return; asect->vma = 0; asect->filepos = crashinfo_offset; asect->size = sizeof (crashinfo); /* Create a ".reg" section to allow access to the saved registers. */ asect = bfd_make_section_anyway_with_flags (abfd, ".reg", flags); if (asect == NULL) goto error_return; asect->vma = 0; asect->filepos = bfd_get_32 (abfd, crashinfo.registers) - rambase; /* Since we don't know the exact size of the saved register info, choose a register section size that is either the remaining part of the file, or 1024, whichever is smaller. */ nread = statbuf.st_size - asect->filepos; asect->size = (nread < 1024) ? nread : 1024; return abfd->xvec; /* Get here if we have already started filling out the BFD and there is an error of some kind. */ error_return: bfd_release (abfd, abfd->tdata.any); abfd->tdata.any = NULL; bfd_section_list_clear (abfd); return NULL; }
bfd_boolean _bfd_elf_discard_section_eh_frame (bfd *abfd, struct bfd_link_info *info, asection *sec, bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *), struct elf_reloc_cookie *cookie) { #define REQUIRE(COND) \ do \ if (!(COND)) \ goto free_no_table; \ while (0) bfd_byte *ehbuf = NULL, *buf; bfd_byte *last_fde; struct eh_cie_fde *ent, *this_inf; unsigned int hdr_length, hdr_id; struct extended_cie { struct cie cie; unsigned int offset; unsigned int usage_count; unsigned int entry; } *ecies = NULL, *ecie; unsigned int ecie_count = 0, ecie_alloced = 0; struct cie *cie; struct elf_link_hash_table *htab; struct eh_frame_hdr_info *hdr_info; struct eh_frame_sec_info *sec_info = NULL; unsigned int offset; unsigned int ptr_size; unsigned int entry_alloced; if (sec->size == 0) { /* This file does not contain .eh_frame information. */ return FALSE; } if (bfd_is_abs_section (sec->output_section)) { /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return FALSE; } htab = elf_hash_table (info); hdr_info = &htab->eh_info; if (hdr_info->cies == NULL && !info->relocatable) hdr_info->cies = htab_try_create (1, cie_hash, cie_eq, free); /* Read the frame unwind information from abfd. */ REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf)); if (sec->size >= 4 && bfd_get_32 (abfd, ehbuf) == 0 && cookie->rel == cookie->relend) { /* Empty .eh_frame section. */ free (ehbuf); return FALSE; } /* If .eh_frame section size doesn't fit into int, we cannot handle it (it would need to use 64-bit .eh_frame format anyway). */ REQUIRE (sec->size == (unsigned int) sec->size); ptr_size = (get_elf_backend_data (abfd) ->elf_backend_eh_frame_address_size (abfd, sec)); REQUIRE (ptr_size != 0); buf = ehbuf; sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info) + 99 * sizeof (struct eh_cie_fde)); REQUIRE (sec_info); entry_alloced = 100; #define ENSURE_NO_RELOCS(buf) \ REQUIRE (!(cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf)) \ && cookie->rel->r_info != 0)) #define SKIP_RELOCS(buf) \ while (cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf))) \ cookie->rel++ #define GET_RELOC(buf) \ ((cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ == (bfd_size_type) ((buf) - ehbuf))) \ ? cookie->rel : NULL) for (;;) { char *aug; bfd_byte *start, *end, *insns, *insns_end; bfd_size_type length; unsigned int set_loc_count; if (sec_info->count == entry_alloced) { sec_info = bfd_realloc (sec_info, sizeof (struct eh_frame_sec_info) + ((entry_alloced + 99) * sizeof (struct eh_cie_fde))); REQUIRE (sec_info); memset (&sec_info->entry[entry_alloced], 0, 100 * sizeof (struct eh_cie_fde)); entry_alloced += 100; } this_inf = sec_info->entry + sec_info->count; last_fde = buf; if ((bfd_size_type) (buf - ehbuf) == sec->size) break; /* Read the length of the entry. */ REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4)); hdr_length = bfd_get_32 (abfd, buf - 4); /* 64-bit .eh_frame is not supported. */ REQUIRE (hdr_length != 0xffffffff); /* The CIE/FDE must be fully contained in this input section. */ REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size); end = buf + hdr_length; this_inf->offset = last_fde - ehbuf; this_inf->size = 4 + hdr_length; if (hdr_length == 0) { /* A zero-length CIE should only be found at the end of the section. */ REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size); ENSURE_NO_RELOCS (buf); sec_info->count++; break; } REQUIRE (skip_bytes (&buf, end, 4)); hdr_id = bfd_get_32 (abfd, buf - 4); if (hdr_id == 0) { unsigned int initial_insn_length; /* CIE */ this_inf->cie = 1; if (ecie_count == ecie_alloced) { ecies = bfd_realloc (ecies, (ecie_alloced + 20) * sizeof (*ecies)); REQUIRE (ecies); memset (&ecies[ecie_alloced], 0, 20 * sizeof (*ecies)); ecie_alloced += 20; } cie = &ecies[ecie_count].cie; ecies[ecie_count].offset = this_inf->offset; ecies[ecie_count++].entry = sec_info->count; cie->length = hdr_length; start = buf; REQUIRE (read_byte (&buf, end, &cie->version)); /* Cannot handle unknown versions. */ REQUIRE (cie->version == 1 || cie->version == 3); REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation)); strcpy (cie->augmentation, (char *) buf); buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1; ENSURE_NO_RELOCS (buf); if (buf[0] == 'e' && buf[1] == 'h') { /* GCC < 3.0 .eh_frame CIE */ /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__ is private to each CIE, so we don't need it for anything. Just skip it. */ REQUIRE (skip_bytes (&buf, end, ptr_size)); SKIP_RELOCS (buf); } REQUIRE (read_uleb128 (&buf, end, &cie->code_align)); REQUIRE (read_sleb128 (&buf, end, &cie->data_align)); if (cie->version == 1) { REQUIRE (buf < end); cie->ra_column = *buf++; } else REQUIRE (read_uleb128 (&buf, end, &cie->ra_column)); ENSURE_NO_RELOCS (buf); cie->lsda_encoding = DW_EH_PE_omit; cie->fde_encoding = DW_EH_PE_omit; cie->per_encoding = DW_EH_PE_omit; aug = cie->augmentation; if (aug[0] != 'e' || aug[1] != 'h') { if (*aug == 'z') { aug++; REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size)); ENSURE_NO_RELOCS (buf); } while (*aug != '\0') switch (*aug++) { case 'L': REQUIRE (read_byte (&buf, end, &cie->lsda_encoding)); ENSURE_NO_RELOCS (buf); REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size)); break; case 'R': REQUIRE (read_byte (&buf, end, &cie->fde_encoding)); ENSURE_NO_RELOCS (buf); REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size)); break; case 'S': break; case 'P': { int per_width; REQUIRE (read_byte (&buf, end, &cie->per_encoding)); per_width = get_DW_EH_PE_width (cie->per_encoding, ptr_size); REQUIRE (per_width); if ((cie->per_encoding & 0xf0) == DW_EH_PE_aligned) { length = -(buf - ehbuf) & (per_width - 1); REQUIRE (skip_bytes (&buf, end, length)); } ENSURE_NO_RELOCS (buf); /* Ensure we have a reloc here. */ if (GET_RELOC (buf) != NULL) { unsigned long r_symndx; #ifdef BFD64 if (ptr_size == 8) r_symndx = ELF64_R_SYM (cookie->rel->r_info); else #endif r_symndx = ELF32_R_SYM (cookie->rel->r_info); if (r_symndx >= cookie->locsymcount || ELF_ST_BIND (cookie->locsyms[r_symndx] .st_info) != STB_LOCAL) { struct elf_link_hash_entry *h; r_symndx -= cookie->extsymoff; h = cookie->sym_hashes[r_symndx]; while (h->root.type == bfd_link_hash_indirect || h->root.type == bfd_link_hash_warning) h = (struct elf_link_hash_entry *) h->root.u.i.link; cie->personality.h = h; } else { Elf_Internal_Sym *sym; asection *sym_sec; bfd_vma val; sym = &cookie->locsyms[r_symndx]; sym_sec = (bfd_section_from_elf_index (abfd, sym->st_shndx)); if (sym_sec != NULL) { if (sym_sec->kept_section != NULL) sym_sec = sym_sec->kept_section; if (sym_sec->output_section != NULL) { val = (sym->st_value + sym_sec->output_offset + sym_sec->output_section->vma); cie->personality.val = val; cie->local_personality = 1; } } } /* Cope with MIPS-style composite relocations. */ do cookie->rel++; while (GET_RELOC (buf) != NULL); } REQUIRE (skip_bytes (&buf, end, per_width)); REQUIRE (cie->local_personality || cie->personality.h); } break; default: /* Unrecognized augmentation. Better bail out. */ goto free_no_table; } } /* For shared libraries, try to get rid of as many RELATIVE relocs as possible. */ if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_relative_eh_frame (abfd, info, sec))) { if ((cie->fde_encoding & 0xf0) == DW_EH_PE_absptr) cie->make_relative = 1; /* If the CIE doesn't already have an 'R' entry, it's fairly easy to add one, provided that there's no aligned data after the augmentation string. */ else if (cie->fde_encoding == DW_EH_PE_omit && (cie->per_encoding & 0xf0) != DW_EH_PE_aligned) { if (*cie->augmentation == 0) this_inf->add_augmentation_size = 1; this_inf->add_fde_encoding = 1; cie->make_relative = 1; } } if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_lsda_relative_eh_frame (abfd, info, sec)) && (cie->lsda_encoding & 0xf0) == DW_EH_PE_absptr) cie->make_lsda_relative = 1; /* If FDE encoding was not specified, it defaults to DW_EH_absptr. */ if (cie->fde_encoding == DW_EH_PE_omit) cie->fde_encoding = DW_EH_PE_absptr; initial_insn_length = end - buf; if (initial_insn_length <= sizeof (cie->initial_instructions)) { cie->initial_insn_length = initial_insn_length; memcpy (cie->initial_instructions, buf, initial_insn_length); } insns = buf; buf += initial_insn_length; ENSURE_NO_RELOCS (buf); } else { /* Find the corresponding CIE. */ unsigned int cie_offset = this_inf->offset + 4 - hdr_id; for (ecie = ecies; ecie < ecies + ecie_count; ++ecie) if (cie_offset == ecie->offset) break; /* Ensure this FDE references one of the CIEs in this input section. */ REQUIRE (ecie != ecies + ecie_count); cie = &ecie->cie; ENSURE_NO_RELOCS (buf); REQUIRE (GET_RELOC (buf)); if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie)) /* This is a FDE against a discarded section. It should be deleted. */ this_inf->removed = 1; else { if (info->shared && (((cie->fde_encoding & 0xf0) == DW_EH_PE_absptr && cie->make_relative == 0) || (cie->fde_encoding & 0xf0) == DW_EH_PE_aligned)) { /* If a shared library uses absolute pointers which we cannot turn into PC relative, don't create the binary search table, since it is affected by runtime relocations. */ hdr_info->table = FALSE; (*info->callbacks->einfo) (_("%P: fde encoding in %B(%A) prevents .eh_frame_hdr" " table being created.\n"), abfd, sec); } ecie->usage_count++; hdr_info->fde_count++; this_inf->cie_inf = (void *) (ecie - ecies); } /* Skip the initial location and address range. */ start = buf; length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size); REQUIRE (skip_bytes (&buf, end, 2 * length)); /* Skip the augmentation size, if present. */ if (cie->augmentation[0] == 'z') REQUIRE (read_uleb128 (&buf, end, &length)); else length = 0; /* Of the supported augmentation characters above, only 'L' adds augmentation data to the FDE. This code would need to be adjusted if any future augmentations do the same thing. */ if (cie->lsda_encoding != DW_EH_PE_omit) { this_inf->lsda_offset = buf - start; /* If there's no 'z' augmentation, we don't know where the CFA insns begin. Assume no padding. */ if (cie->augmentation[0] != 'z') length = end - buf; } /* Skip over the augmentation data. */ REQUIRE (skip_bytes (&buf, end, length)); insns = buf; buf = last_fde + 4 + hdr_length; SKIP_RELOCS (buf); } /* Try to interpret the CFA instructions and find the first padding nop. Shrink this_inf's size so that it doesn't include the padding. */ length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size); set_loc_count = 0; insns_end = skip_non_nops (insns, end, length, &set_loc_count); /* If we don't understand the CFA instructions, we can't know what needs to be adjusted there. */ if (insns_end == NULL /* For the time being we don't support DW_CFA_set_loc in CIE instructions. */ || (set_loc_count && this_inf->cie)) goto free_no_table; this_inf->size -= end - insns_end; if (insns_end != end && this_inf->cie) { cie->initial_insn_length -= end - insns_end; cie->length -= end - insns_end; } if (set_loc_count && ((cie->fde_encoding & 0xf0) == DW_EH_PE_pcrel || cie->make_relative)) { unsigned int cnt; bfd_byte *p; this_inf->set_loc = bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int)); REQUIRE (this_inf->set_loc); this_inf->set_loc[0] = set_loc_count; p = insns; cnt = 0; while (p < end) { if (*p == DW_CFA_set_loc) this_inf->set_loc[++cnt] = p + 1 - start; REQUIRE (skip_cfa_op (&p, end, length)); } } this_inf->fde_encoding = cie->fde_encoding; this_inf->lsda_encoding = cie->lsda_encoding; sec_info->count++; } elf_section_data (sec)->sec_info = sec_info; sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME; /* Look at all CIEs in this section and determine which can be removed as unused, which can be merged with previous duplicate CIEs and which need to be kept. */ for (ecie = ecies; ecie < ecies + ecie_count; ++ecie) { if (ecie->usage_count == 0) { sec_info->entry[ecie->entry].removed = 1; continue; } ecie->cie.output_sec = sec->output_section; ecie->cie.cie_inf = sec_info->entry + ecie->entry; cie_compute_hash (&ecie->cie); if (hdr_info->cies != NULL) { void **loc = htab_find_slot_with_hash (hdr_info->cies, &ecie->cie, ecie->cie.hash, INSERT); if (loc != NULL) { if (*loc != HTAB_EMPTY_ENTRY) { sec_info->entry[ecie->entry].removed = 1; ecie->cie.cie_inf = ((struct cie *) *loc)->cie_inf; continue; } *loc = malloc (sizeof (struct cie)); if (*loc == NULL) *loc = HTAB_DELETED_ENTRY; else memcpy (*loc, &ecie->cie, sizeof (struct cie)); } } ecie->cie.cie_inf->make_relative = ecie->cie.make_relative; ecie->cie.cie_inf->make_lsda_relative = ecie->cie.make_lsda_relative; ecie->cie.cie_inf->per_encoding_relative = (ecie->cie.per_encoding & 0x70) == DW_EH_PE_pcrel; } /* Ok, now we can assign new offsets. */ offset = 0; for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) if (!ent->removed) { if (!ent->cie) { ecie = ecies + (bfd_hostptr_t) ent->cie_inf; ent->cie_inf = ecie->cie.cie_inf; } ent->new_offset = offset; offset += size_of_output_cie_fde (ent, ptr_size); } /* Resize the sec as needed. */ sec->rawsize = sec->size; sec->size = offset; free (ehbuf); if (ecies) free (ecies); return offset != sec->rawsize; free_no_table: (*info->callbacks->einfo) (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"), abfd, sec); if (ehbuf) free (ehbuf); if (sec_info) free (sec_info); if (ecies) free (ecies); hdr_info->table = FALSE; return FALSE; #undef REQUIRE }
void _bfd_elf_parse_eh_frame (bfd *abfd, struct bfd_link_info *info, asection *sec, struct elf_reloc_cookie *cookie) { #define REQUIRE(COND) \ do \ if (!(COND)) \ goto free_no_table; \ while (0) bfd_byte *ehbuf = NULL, *buf, *end; bfd_byte *last_fde; struct eh_cie_fde *this_inf; unsigned int hdr_length, hdr_id; unsigned int cie_count; struct cie *cie, *local_cies = NULL; struct elf_link_hash_table *htab; struct eh_frame_hdr_info *hdr_info; struct eh_frame_sec_info *sec_info = NULL; unsigned int ptr_size; unsigned int num_cies; unsigned int num_entries; elf_gc_mark_hook_fn gc_mark_hook; htab = elf_hash_table (info); hdr_info = &htab->eh_info; if (hdr_info->parsed_eh_frames) return; if (sec->size == 0) { /* This file does not contain .eh_frame information. */ return; } if (bfd_is_abs_section (sec->output_section)) { /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return; } /* Read the frame unwind information from abfd. */ REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf)); if (sec->size >= 4 && bfd_get_32 (abfd, ehbuf) == 0 && cookie->rel == cookie->relend) { /* Empty .eh_frame section. */ free (ehbuf); return; } /* If .eh_frame section size doesn't fit into int, we cannot handle it (it would need to use 64-bit .eh_frame format anyway). */ REQUIRE (sec->size == (unsigned int) sec->size); ptr_size = (get_elf_backend_data (abfd) ->elf_backend_eh_frame_address_size (abfd, sec)); REQUIRE (ptr_size != 0); /* Go through the section contents and work out how many FDEs and CIEs there are. */ buf = ehbuf; end = ehbuf + sec->size; num_cies = 0; num_entries = 0; while (buf != end) { num_entries++; /* Read the length of the entry. */ REQUIRE (skip_bytes (&buf, end, 4)); hdr_length = bfd_get_32 (abfd, buf - 4); /* 64-bit .eh_frame is not supported. */ REQUIRE (hdr_length != 0xffffffff); if (hdr_length == 0) break; REQUIRE (skip_bytes (&buf, end, 4)); hdr_id = bfd_get_32 (abfd, buf - 4); if (hdr_id == 0) num_cies++; REQUIRE (skip_bytes (&buf, end, hdr_length - 4)); } sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info) + (num_entries - 1) * sizeof (struct eh_cie_fde)); REQUIRE (sec_info); /* We need to have a "struct cie" for each CIE in this section. */ local_cies = bfd_zmalloc (num_cies * sizeof (*local_cies)); REQUIRE (local_cies); #define ENSURE_NO_RELOCS(buf) \ REQUIRE (!(cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf)) \ && cookie->rel->r_info != 0)) #define SKIP_RELOCS(buf) \ while (cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf))) \ cookie->rel++ #define GET_RELOC(buf) \ ((cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ == (bfd_size_type) ((buf) - ehbuf))) \ ? cookie->rel : NULL) buf = ehbuf; cie_count = 0; gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook; while ((bfd_size_type) (buf - ehbuf) != sec->size) { char *aug; bfd_byte *start, *insns, *insns_end; bfd_size_type length; unsigned int set_loc_count; this_inf = sec_info->entry + sec_info->count; last_fde = buf; /* Read the length of the entry. */ REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4)); hdr_length = bfd_get_32 (abfd, buf - 4); /* The CIE/FDE must be fully contained in this input section. */ REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr_length <= sec->size); end = buf + hdr_length; this_inf->offset = last_fde - ehbuf; this_inf->size = 4 + hdr_length; this_inf->reloc_index = cookie->rel - cookie->rels; if (hdr_length == 0) { /* A zero-length CIE should only be found at the end of the section. */ REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size); ENSURE_NO_RELOCS (buf); sec_info->count++; break; } REQUIRE (skip_bytes (&buf, end, 4)); hdr_id = bfd_get_32 (abfd, buf - 4); if (hdr_id == 0) { unsigned int initial_insn_length; /* CIE */ this_inf->cie = 1; /* Point CIE to one of the section-local cie structures. */ cie = local_cies + cie_count++; cie->cie_inf = this_inf; cie->length = hdr_length; cie->output_sec = sec->output_section; start = buf; REQUIRE (read_byte (&buf, end, &cie->version)); /* Cannot handle unknown versions. */ REQUIRE (cie->version == 1 || cie->version == 3); REQUIRE (strlen ((char *) buf) < sizeof (cie->augmentation)); strcpy (cie->augmentation, (char *) buf); buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1; ENSURE_NO_RELOCS (buf); if (buf[0] == 'e' && buf[1] == 'h') { /* GCC < 3.0 .eh_frame CIE */ /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__ is private to each CIE, so we don't need it for anything. Just skip it. */ REQUIRE (skip_bytes (&buf, end, ptr_size)); SKIP_RELOCS (buf); } REQUIRE (read_uleb128 (&buf, end, &cie->code_align)); REQUIRE (read_sleb128 (&buf, end, &cie->data_align)); if (cie->version == 1) { REQUIRE (buf < end); cie->ra_column = *buf++; } else REQUIRE (read_uleb128 (&buf, end, &cie->ra_column)); ENSURE_NO_RELOCS (buf); cie->lsda_encoding = DW_EH_PE_omit; cie->fde_encoding = DW_EH_PE_omit; cie->per_encoding = DW_EH_PE_omit; aug = cie->augmentation; if (aug[0] != 'e' || aug[1] != 'h') { if (*aug == 'z') { aug++; REQUIRE (read_uleb128 (&buf, end, &cie->augmentation_size)); ENSURE_NO_RELOCS (buf); } while (*aug != '\0') switch (*aug++) { case 'L': REQUIRE (read_byte (&buf, end, &cie->lsda_encoding)); ENSURE_NO_RELOCS (buf); REQUIRE (get_DW_EH_PE_width (cie->lsda_encoding, ptr_size)); break; case 'R': REQUIRE (read_byte (&buf, end, &cie->fde_encoding)); ENSURE_NO_RELOCS (buf); REQUIRE (get_DW_EH_PE_width (cie->fde_encoding, ptr_size)); break; case 'S': break; case 'P': { int per_width; REQUIRE (read_byte (&buf, end, &cie->per_encoding)); per_width = get_DW_EH_PE_width (cie->per_encoding, ptr_size); REQUIRE (per_width); if ((cie->per_encoding & 0xf0) == DW_EH_PE_aligned) { length = -(buf - ehbuf) & (per_width - 1); REQUIRE (skip_bytes (&buf, end, length)); } ENSURE_NO_RELOCS (buf); /* Ensure we have a reloc here. */ REQUIRE (GET_RELOC (buf)); cie->personality.reloc_index = cookie->rel - cookie->rels; /* Cope with MIPS-style composite relocations. */ do cookie->rel++; while (GET_RELOC (buf) != NULL); REQUIRE (skip_bytes (&buf, end, per_width)); } break; default: /* Unrecognized augmentation. Better bail out. */ goto free_no_table; } } /* For shared libraries, try to get rid of as many RELATIVE relocs as possible. */ if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_relative_eh_frame (abfd, info, sec))) { if ((cie->fde_encoding & 0xf0) == DW_EH_PE_absptr) this_inf->make_relative = 1; /* If the CIE doesn't already have an 'R' entry, it's fairly easy to add one, provided that there's no aligned data after the augmentation string. */ else if (cie->fde_encoding == DW_EH_PE_omit && (cie->per_encoding & 0xf0) != DW_EH_PE_aligned) { if (*cie->augmentation == 0) this_inf->add_augmentation_size = 1; this_inf->u.cie.add_fde_encoding = 1; this_inf->make_relative = 1; } } if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_lsda_relative_eh_frame (abfd, info, sec)) && (cie->lsda_encoding & 0xf0) == DW_EH_PE_absptr) cie->can_make_lsda_relative = 1; /* If FDE encoding was not specified, it defaults to DW_EH_absptr. */ if (cie->fde_encoding == DW_EH_PE_omit) cie->fde_encoding = DW_EH_PE_absptr; initial_insn_length = end - buf; if (initial_insn_length <= sizeof (cie->initial_instructions)) { cie->initial_insn_length = initial_insn_length; memcpy (cie->initial_instructions, buf, initial_insn_length); } insns = buf; buf += initial_insn_length; ENSURE_NO_RELOCS (buf); if (hdr_info->merge_cies) this_inf->u.cie.u.full_cie = cie; this_inf->u.cie.per_encoding_relative = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel; } else { asection *rsec; /* Find the corresponding CIE. */ unsigned int cie_offset = this_inf->offset + 4 - hdr_id; for (cie = local_cies; cie < local_cies + cie_count; cie++) if (cie_offset == cie->cie_inf->offset) break; /* Ensure this FDE references one of the CIEs in this input section. */ REQUIRE (cie != local_cies + cie_count); this_inf->u.fde.cie_inf = cie->cie_inf; this_inf->make_relative = cie->cie_inf->make_relative; this_inf->add_augmentation_size = cie->cie_inf->add_augmentation_size; ENSURE_NO_RELOCS (buf); REQUIRE (GET_RELOC (buf)); /* Chain together the FDEs for each section. */ rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); /* RSEC will be NULL if FDE was cleared out as it was belonging to a discarded SHT_GROUP. */ if (rsec) { REQUIRE (rsec->owner == abfd); this_inf->u.fde.next_for_section = elf_fde_list (rsec); elf_fde_list (rsec) = this_inf; } /* Skip the initial location and address range. */ start = buf; length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size); REQUIRE (skip_bytes (&buf, end, 2 * length)); /* Skip the augmentation size, if present. */ if (cie->augmentation[0] == 'z') REQUIRE (read_uleb128 (&buf, end, &length)); else length = 0; /* Of the supported augmentation characters above, only 'L' adds augmentation data to the FDE. This code would need to be adjusted if any future augmentations do the same thing. */ if (cie->lsda_encoding != DW_EH_PE_omit) { SKIP_RELOCS (buf); if (cie->can_make_lsda_relative && GET_RELOC (buf)) cie->cie_inf->u.cie.make_lsda_relative = 1; this_inf->lsda_offset = buf - start; /* If there's no 'z' augmentation, we don't know where the CFA insns begin. Assume no padding. */ if (cie->augmentation[0] != 'z') length = end - buf; } /* Skip over the augmentation data. */ REQUIRE (skip_bytes (&buf, end, length)); insns = buf; buf = last_fde + 4 + hdr_length; /* For NULL RSEC (cleared FDE belonging to a discarded section) the relocations are commonly cleared. We do not sanity check if all these relocations are cleared as (1) relocations to .gcc_except_table will remain uncleared (they will get dropped with the drop of this unused FDE) and (2) BFD already safely drops relocations of any type to .eh_frame by elf_section_ignore_discarded_relocs. TODO: The .gcc_except_table entries should be also filtered as .eh_frame entries; or GCC could rather use COMDAT for them. */ SKIP_RELOCS (buf); } /* Try to interpret the CFA instructions and find the first padding nop. Shrink this_inf's size so that it doesn't include the padding. */ length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size); set_loc_count = 0; insns_end = skip_non_nops (insns, end, length, &set_loc_count); /* If we don't understand the CFA instructions, we can't know what needs to be adjusted there. */ if (insns_end == NULL /* For the time being we don't support DW_CFA_set_loc in CIE instructions. */ || (set_loc_count && this_inf->cie)) goto free_no_table; this_inf->size -= end - insns_end; if (insns_end != end && this_inf->cie) { cie->initial_insn_length -= end - insns_end; cie->length -= end - insns_end; } if (set_loc_count && ((cie->fde_encoding & 0xf0) == DW_EH_PE_pcrel || this_inf->make_relative)) { unsigned int cnt; bfd_byte *p; this_inf->set_loc = bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int)); REQUIRE (this_inf->set_loc); this_inf->set_loc[0] = set_loc_count; p = insns; cnt = 0; while (p < end) { if (*p == DW_CFA_set_loc) this_inf->set_loc[++cnt] = p + 1 - start; REQUIRE (skip_cfa_op (&p, end, length)); } } this_inf->removed = 1; this_inf->fde_encoding = cie->fde_encoding; this_inf->lsda_encoding = cie->lsda_encoding; sec_info->count++; } BFD_ASSERT (sec_info->count == num_entries); BFD_ASSERT (cie_count == num_cies); elf_section_data (sec)->sec_info = sec_info; sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME; if (hdr_info->merge_cies) { sec_info->cies = local_cies; local_cies = NULL; } goto success; free_no_table: (*info->callbacks->einfo) (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"), abfd, sec); hdr_info->table = FALSE; if (sec_info) free (sec_info); success: if (ehbuf) free (ehbuf); if (local_cies) free (local_cies); #undef REQUIRE }
bfd_boolean _bfd_elf_discard_section_eh_frame (bfd *abfd, struct bfd_link_info *info, asection *sec, bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *), struct elf_reloc_cookie *cookie) { bfd_byte *ehbuf = NULL, *buf; bfd_byte *last_cie, *last_fde; struct cie_header hdr; struct cie cie; struct elf_link_hash_table *htab; struct eh_frame_hdr_info *hdr_info; struct eh_frame_sec_info *sec_info = NULL; unsigned int leb128_tmp; unsigned int cie_usage_count, last_cie_ndx, i, offset; unsigned int make_relative, make_lsda_relative; bfd_size_type new_size; unsigned int ptr_size; if (sec->_raw_size == 0) { /* This file does not contain .eh_frame information. */ return FALSE; } if ((sec->output_section != NULL && bfd_is_abs_section (sec->output_section))) { /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return FALSE; } htab = elf_hash_table (info); hdr_info = &htab->eh_info; /* Read the frame unwind information from abfd. */ ehbuf = bfd_malloc (sec->_raw_size); if (ehbuf == NULL) goto free_no_table; if (! bfd_get_section_contents (abfd, sec, ehbuf, 0, sec->_raw_size)) goto free_no_table; if (sec->_raw_size >= 4 && bfd_get_32 (abfd, ehbuf) == 0 && cookie->rel == cookie->relend) { /* Empty .eh_frame section. */ free (ehbuf); return FALSE; } /* If .eh_frame section size doesn't fit into int, we cannot handle it (it would need to use 64-bit .eh_frame format anyway). */ if (sec->_raw_size != (unsigned int) sec->_raw_size) goto free_no_table; ptr_size = (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) ? 8 : 4; buf = ehbuf; last_cie = NULL; last_cie_ndx = 0; memset (&cie, 0, sizeof (cie)); cie_usage_count = 0; new_size = sec->_raw_size; make_relative = hdr_info->last_cie.make_relative; make_lsda_relative = hdr_info->last_cie.make_lsda_relative; sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info) + 99 * sizeof (struct eh_cie_fde)); if (sec_info == NULL) goto free_no_table; sec_info->alloced = 100; #define ENSURE_NO_RELOCS(buf) \ if (cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf)) \ && cookie->rel->r_info != 0) \ goto free_no_table #define SKIP_RELOCS(buf) \ while (cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf))) \ cookie->rel++ #define GET_RELOC(buf) \ ((cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ == (bfd_size_type) ((buf) - ehbuf))) \ ? cookie->rel : NULL) for (;;) { unsigned char *aug; if (sec_info->count == sec_info->alloced) { sec_info = bfd_realloc (sec_info, sizeof (struct eh_frame_sec_info) + (sec_info->alloced + 99) * sizeof (struct eh_cie_fde)); if (sec_info == NULL) goto free_no_table; memset (&sec_info->entry[sec_info->alloced], 0, 100 * sizeof (struct eh_cie_fde)); sec_info->alloced += 100; } last_fde = buf; /* If we are at the end of the section, we still need to decide on whether to output or discard last encountered CIE (if any). */ if ((bfd_size_type) (buf - ehbuf) == sec->_raw_size) hdr.id = (unsigned int) -1; else { if ((bfd_size_type) (buf + 4 - ehbuf) > sec->_raw_size) /* No space for CIE/FDE header length. */ goto free_no_table; hdr.length = bfd_get_32 (abfd, buf); if (hdr.length == 0xffffffff) /* 64-bit .eh_frame is not supported. */ goto free_no_table; buf += 4; if ((bfd_size_type) (buf - ehbuf) + hdr.length > sec->_raw_size) /* CIE/FDE not contained fully in this .eh_frame input section. */ goto free_no_table; sec_info->entry[sec_info->count].offset = last_fde - ehbuf; sec_info->entry[sec_info->count].size = 4 + hdr.length; if (hdr.length == 0) { /* CIE with length 0 must be only the last in the section. */ if ((bfd_size_type) (buf - ehbuf) < sec->_raw_size) goto free_no_table; ENSURE_NO_RELOCS (buf); sec_info->count++; /* Now just finish last encountered CIE processing and break the loop. */ hdr.id = (unsigned int) -1; } else { hdr.id = bfd_get_32 (abfd, buf); buf += 4; if (hdr.id == (unsigned int) -1) goto free_no_table; } } if (hdr.id == 0 || hdr.id == (unsigned int) -1) { unsigned int initial_insn_length; /* CIE */ if (last_cie != NULL) { /* Now check if this CIE is identical to the last CIE, in which case we can remove it provided we adjust all FDEs. Also, it can be removed if we have removed all FDEs using it. */ if ((!info->relocatable && hdr_info->last_cie_sec && (sec->output_section == hdr_info->last_cie_sec->output_section) && cie_compare (&cie, &hdr_info->last_cie) == 0) || cie_usage_count == 0) { new_size -= cie.hdr.length + 4; sec_info->entry[last_cie_ndx].removed = 1; sec_info->entry[last_cie_ndx].sec = hdr_info->last_cie_sec; sec_info->entry[last_cie_ndx].new_offset = hdr_info->last_cie_offset; } else { hdr_info->last_cie = cie; hdr_info->last_cie_sec = sec; hdr_info->last_cie_offset = last_cie - ehbuf; sec_info->entry[last_cie_ndx].make_relative = cie.make_relative; sec_info->entry[last_cie_ndx].make_lsda_relative = cie.make_lsda_relative; sec_info->entry[last_cie_ndx].per_encoding_relative = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel; } } if (hdr.id == (unsigned int) -1) break; last_cie_ndx = sec_info->count; sec_info->entry[sec_info->count].cie = 1; cie_usage_count = 0; memset (&cie, 0, sizeof (cie)); cie.hdr = hdr; cie.version = *buf++; /* Cannot handle unknown versions. */ if (cie.version != 1) goto free_no_table; if (strlen (buf) > sizeof (cie.augmentation) - 1) goto free_no_table; strcpy (cie.augmentation, buf); buf = strchr (buf, '\0') + 1; ENSURE_NO_RELOCS (buf); if (buf[0] == 'e' && buf[1] == 'h') { /* GCC < 3.0 .eh_frame CIE */ /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__ is private to each CIE, so we don't need it for anything. Just skip it. */ buf += ptr_size; SKIP_RELOCS (buf); } read_uleb128 (cie.code_align, buf); read_sleb128 (cie.data_align, buf); /* Note - in DWARF2 the return address column is an unsigned byte. In DWARF3 it is a ULEB128. We are following DWARF3. For most ports this will not matter as the value will be less than 128. For the others (eg FRV, SH, MMIX, IA64) they need a fixed GCC which conforms to the DWARF3 standard. */ read_uleb128 (cie.ra_column, buf); ENSURE_NO_RELOCS (buf); cie.lsda_encoding = DW_EH_PE_omit; cie.fde_encoding = DW_EH_PE_omit; cie.per_encoding = DW_EH_PE_omit; aug = cie.augmentation; if (aug[0] != 'e' || aug[1] != 'h') { if (*aug == 'z') { aug++; read_uleb128 (cie.augmentation_size, buf); ENSURE_NO_RELOCS (buf); } while (*aug != '\0') switch (*aug++) { case 'L': cie.lsda_encoding = *buf++; ENSURE_NO_RELOCS (buf); if (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size) == 0) goto free_no_table; break; case 'R': cie.fde_encoding = *buf++; ENSURE_NO_RELOCS (buf); if (get_DW_EH_PE_width (cie.fde_encoding, ptr_size) == 0) goto free_no_table; break; case 'P': { int per_width; cie.per_encoding = *buf++; per_width = get_DW_EH_PE_width (cie.per_encoding, ptr_size); if (per_width == 0) goto free_no_table; if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned) buf = (ehbuf + ((buf - ehbuf + per_width - 1) & ~((bfd_size_type) per_width - 1))); ENSURE_NO_RELOCS (buf); /* Ensure we have a reloc here, against a global symbol. */ if (GET_RELOC (buf) != NULL) { unsigned long r_symndx; #ifdef BFD64 if (ptr_size == 8) r_symndx = ELF64_R_SYM (cookie->rel->r_info); else #endif r_symndx = ELF32_R_SYM (cookie->rel->r_info); if (r_symndx >= cookie->locsymcount) { struct elf_link_hash_entry *h; r_symndx -= cookie->extsymoff; h = cookie->sym_hashes[r_symndx]; while (h->root.type == bfd_link_hash_indirect || h->root.type == bfd_link_hash_warning) h = (struct elf_link_hash_entry *) h->root.u.i.link; cie.personality = h; } cookie->rel++; } buf += per_width; } break; default: /* Unrecognized augmentation. Better bail out. */ goto free_no_table; } } /* For shared libraries, try to get rid of as many RELATIVE relocs as possible. */ if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_relative_eh_frame (abfd, info, sec)) && (cie.fde_encoding & 0xf0) == DW_EH_PE_absptr) cie.make_relative = 1; if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_lsda_relative_eh_frame (abfd, info, sec)) && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr) cie.make_lsda_relative = 1; /* If FDE encoding was not specified, it defaults to DW_EH_absptr. */ if (cie.fde_encoding == DW_EH_PE_omit) cie.fde_encoding = DW_EH_PE_absptr; initial_insn_length = cie.hdr.length - (buf - last_fde - 4); if (initial_insn_length <= 50) { cie.initial_insn_length = initial_insn_length; memcpy (cie.initial_instructions, buf, initial_insn_length); } buf += initial_insn_length; ENSURE_NO_RELOCS (buf); last_cie = last_fde; } else { /* Ensure this FDE uses the last CIE encountered. */ if (last_cie == NULL || hdr.id != (unsigned int) (buf - 4 - last_cie)) goto free_no_table; ENSURE_NO_RELOCS (buf); if (GET_RELOC (buf) == NULL) /* This should not happen. */ goto free_no_table; if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie)) { /* This is a FDE against a discarded section. It should be deleted. */ new_size -= hdr.length + 4; sec_info->entry[sec_info->count].removed = 1; } else { if (info->shared && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr && cie.make_relative == 0) || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned)) { /* If a shared library uses absolute pointers which we cannot turn into PC relative, don't create the binary search table, since it is affected by runtime relocations. */ hdr_info->table = FALSE; } cie_usage_count++; hdr_info->fde_count++; } if (cie.lsda_encoding != DW_EH_PE_omit) { unsigned int dummy; aug = buf; buf += 2 * get_DW_EH_PE_width (cie.fde_encoding, ptr_size); if (cie.augmentation[0] == 'z') read_uleb128 (dummy, buf); /* If some new augmentation data is added before LSDA in FDE augmentation area, this need to be adjusted. */ sec_info->entry[sec_info->count].lsda_offset = (buf - aug); } buf = last_fde + 4 + hdr.length; SKIP_RELOCS (buf); } sec_info->entry[sec_info->count].fde_encoding = cie.fde_encoding; sec_info->entry[sec_info->count].lsda_encoding = cie.lsda_encoding; sec_info->count++; } elf_section_data (sec)->sec_info = sec_info; sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME; /* Ok, now we can assign new offsets. */ offset = 0; last_cie_ndx = 0; for (i = 0; i < sec_info->count; i++) { if (! sec_info->entry[i].removed) { sec_info->entry[i].new_offset = offset; offset += sec_info->entry[i].size; if (sec_info->entry[i].cie) { last_cie_ndx = i; make_relative = sec_info->entry[i].make_relative; make_lsda_relative = sec_info->entry[i].make_lsda_relative; } else { sec_info->entry[i].make_relative = make_relative; sec_info->entry[i].make_lsda_relative = make_lsda_relative; sec_info->entry[i].per_encoding_relative = 0; } } else if (sec_info->entry[i].cie && sec_info->entry[i].sec == sec) { /* Need to adjust new_offset too. */ BFD_ASSERT (sec_info->entry[last_cie_ndx].offset == sec_info->entry[i].new_offset); sec_info->entry[i].new_offset = sec_info->entry[last_cie_ndx].new_offset; } } if (hdr_info->last_cie_sec == sec) { BFD_ASSERT (sec_info->entry[last_cie_ndx].offset == hdr_info->last_cie_offset); hdr_info->last_cie_offset = sec_info->entry[last_cie_ndx].new_offset; } /* FIXME: Currently it is not possible to shrink sections to zero size at this point, so build a fake minimal CIE. */ if (new_size == 0) new_size = 16; /* Shrink the sec as needed. */ sec->_cooked_size = new_size; if (sec->_cooked_size == 0) sec->flags |= SEC_EXCLUDE; free (ehbuf); return new_size != sec->_raw_size; free_no_table: if (ehbuf) free (ehbuf); if (sec_info) free (sec_info); hdr_info->table = FALSE; hdr_info->last_cie.hdr.length = 0; return FALSE; }
static bfd_boolean sh64_address_in_cranges (asection *cranges, bfd_vma addr, sh64_elf_crange *rangep) { bfd_byte *cranges_contents; bfd_byte *found_rangep; bfd_size_type cranges_size = cranges->size; /* If the size is not a multiple of the cranges entry size, then something is badly wrong. */ if ((cranges_size % SH64_CRANGE_SIZE) != 0) return FALSE; /* If this section has relocations, then we can't do anything sane. */ if (bfd_get_section_flags (cranges->owner, cranges) & SEC_RELOC) return FALSE; /* Has some kind soul (or previous call) left processed, sorted contents for us? */ if ((bfd_get_section_flags (cranges->owner, cranges) & SEC_IN_MEMORY) && elf_section_data (cranges)->this_hdr.sh_type == SHT_SH5_CR_SORTED) cranges_contents = cranges->contents; else { if (!bfd_malloc_and_get_section (cranges->owner, cranges, &cranges_contents)) goto error_return; /* Is it sorted? */ if (elf_section_data (cranges)->this_hdr.sh_type != SHT_SH5_CR_SORTED) /* Nope. Lets sort it. */ qsort (cranges_contents, cranges_size / SH64_CRANGE_SIZE, SH64_CRANGE_SIZE, bfd_big_endian (cranges->owner) ? _bfd_sh64_crange_qsort_cmpb : _bfd_sh64_crange_qsort_cmpl); /* Let's keep it around. */ cranges->contents = cranges_contents; bfd_set_section_flags (cranges->owner, cranges, bfd_get_section_flags (cranges->owner, cranges) | SEC_IN_MEMORY); /* It's sorted now. */ elf_section_data (cranges)->this_hdr.sh_type = SHT_SH5_CR_SORTED; } /* Try and find a matching range. */ found_rangep = bsearch (&addr, cranges_contents, cranges_size / SH64_CRANGE_SIZE, SH64_CRANGE_SIZE, bfd_big_endian (cranges->owner) ? _bfd_sh64_crange_bsearch_cmpb : _bfd_sh64_crange_bsearch_cmpl); /* Fill in a few return values if we found a matching range. */ if (found_rangep) { enum sh64_elf_cr_type cr_type = bfd_get_16 (cranges->owner, SH64_CRANGE_CR_TYPE_OFFSET + found_rangep); bfd_vma cr_addr = bfd_get_32 (cranges->owner, SH64_CRANGE_CR_ADDR_OFFSET + (char *) found_rangep); bfd_size_type cr_size = bfd_get_32 (cranges->owner, SH64_CRANGE_CR_SIZE_OFFSET + (char *) found_rangep); rangep->cr_addr = cr_addr; rangep->cr_size = cr_size; rangep->cr_type = cr_type; return TRUE; } /* There is a .cranges section, but it does not have a descriptor matching this address. */ return FALSE; error_return: if (cranges_contents != NULL) free (cranges_contents); return FALSE; }
/* Parse an object attributes section. */ void _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr) { bfd_byte *contents; bfd_byte *p; bfd_vma len; const char *std_sec; contents = (bfd_byte *) bfd_malloc (hdr->sh_size); if (!contents) return; if (!bfd_get_section_contents (abfd, hdr->bfd_section, contents, 0, hdr->sh_size)) { free (contents); return; } p = contents; std_sec = get_elf_backend_data (abfd)->obj_attrs_vendor; if (*(p++) == 'A') { len = hdr->sh_size - 1; while (len > 0) { int namelen; bfd_vma section_len; int vendor; section_len = bfd_get_32 (abfd, p); p += 4; if (section_len > len) section_len = len; len -= section_len; namelen = strlen ((char *) p) + 1; section_len -= namelen + 4; if (std_sec && strcmp ((char *) p, std_sec) == 0) vendor = OBJ_ATTR_PROC; else if (strcmp ((char *) p, "gnu") == 0) vendor = OBJ_ATTR_GNU; else { /* Other vendor section. Ignore it. */ p += namelen + section_len; continue; } p += namelen; while (section_len > 0) { int tag; unsigned int n; unsigned int val; bfd_vma subsection_len; bfd_byte *end; tag = read_unsigned_leb128 (abfd, p, &n); p += n; subsection_len = bfd_get_32 (abfd, p); p += 4; if (subsection_len > section_len) subsection_len = section_len; section_len -= subsection_len; subsection_len -= n + 4; end = p + subsection_len; switch (tag) { case Tag_File: while (p < end) { int type; tag = read_unsigned_leb128 (abfd, p, &n); p += n; type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); switch (type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) { case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: val = read_unsigned_leb128 (abfd, p, &n); p += n; bfd_elf_add_obj_attr_int_string (abfd, vendor, tag, val, (char *)p); p += strlen ((char *)p) + 1; break; case ATTR_TYPE_FLAG_STR_VAL: bfd_elf_add_obj_attr_string (abfd, vendor, tag, (char *)p); p += strlen ((char *)p) + 1; break; case ATTR_TYPE_FLAG_INT_VAL: val = read_unsigned_leb128 (abfd, p, &n); p += n; bfd_elf_add_obj_attr_int (abfd, vendor, tag, val); break; default: abort (); } } break; case Tag_Section: case Tag_Symbol: /* Don't have anywhere convenient to attach these. Fall through for now. */ default: /* Ignore things we don't kow about. */ p += subsection_len; subsection_len = 0; break; } } } } free (contents); }
static bfd_boolean read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, void *dhandle, bfd_boolean *pfound) { static struct { const char *secname; const char *strsecname; } names[] = { { ".stab", ".stabstr" }, { "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr" } }; unsigned int i; void *shandle; *pfound = FALSE; shandle = NULL; for (i = 0; i < sizeof names / sizeof names[0]; i++) { asection *sec, *strsec; sec = bfd_get_section_by_name (abfd, names[i].secname); strsec = bfd_get_section_by_name (abfd, names[i].strsecname); if (sec != NULL && strsec != NULL) { bfd_size_type stabsize, strsize; bfd_byte *stabs, *strings; bfd_byte *stab; bfd_size_type stroff, next_stroff; stabsize = bfd_section_size (abfd, sec); stabs = (bfd_byte *) xmalloc (stabsize); if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize)) { fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), names[i].secname, bfd_errmsg (bfd_get_error ())); return FALSE; } strsize = bfd_section_size (abfd, strsec); strings = (bfd_byte *) xmalloc (strsize); if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize)) { fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), names[i].strsecname, bfd_errmsg (bfd_get_error ())); return FALSE; } if (shandle == NULL) { shandle = start_stab (dhandle, abfd, TRUE, syms, symcount); if (shandle == NULL) return FALSE; } *pfound = TRUE; stroff = 0; next_stroff = 0; for (stab = stabs; stab < (stabs + stabsize); stab += 12) { unsigned int strx; int type; int other; int desc; bfd_vma value; /* This code presumes 32 bit values: */ strx = bfd_get_32(abfd, stab); type = bfd_get_8(abfd, stab + 4); other = bfd_get_8(abfd, stab + 5); desc = bfd_get_16(abfd, stab + 6); value = bfd_get_32(abfd, stab + 8); if (type == 0) { /* Special type 0 stabs indicate the offset to the next string table. */ stroff = next_stroff; next_stroff += value; } else { char *f, *s; f = NULL; if ((stroff + strx) > strsize) { fprintf(stderr, "%s: %s: stab entry %ld is corrupt, strx = 0x%x, type = %d, other = %d\n", bfd_get_filename(abfd), names[i].secname, (long)(stab - stabs) / 12, strx, type, other); continue; } s = (char *) strings + stroff + strx; while (s[strlen (s) - 1] == '\\' && stab + 12 < stabs + stabsize) { char *p; stab += 12; p = s + strlen (s) - 1; *p = '\0'; s = concat (s, ((char *) strings + stroff + bfd_get_32 (abfd, stab)), (const char *) NULL); /* We have to restore the backslash, because, if the linker is hashing stabs strings, we may see the same string more than once. */ *p = '\\'; if (f != NULL) free (f); f = s; } save_stab (type, desc, value, s); if (! parse_stab (dhandle, shandle, type, desc, value, s)) { stab_context (); free_saved_stabs (); return FALSE; } /* Don't free f, since I think the stabs code expects strings to hang around. This should be straightened out. FIXME. */ } } free_saved_stabs (); free (stabs); /* Don't free strings, since I think the stabs code expects the strings to hang around. This should be straightened out. FIXME. */ } } if (shandle != NULL) { if (! finish_stab (dhandle, shandle)) return FALSE; } return TRUE; }
bfd_boolean _bfd_elf_discard_section_eh_frame (bfd *abfd, struct bfd_link_info *info, asection *sec, bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *), struct elf_reloc_cookie *cookie) { #define REQUIRE(COND) \ do \ if (!(COND)) \ goto free_no_table; \ while (0) bfd_byte *ehbuf = NULL, *buf; bfd_byte *last_cie, *last_fde; struct eh_cie_fde *ent, *last_cie_inf, *this_inf; struct cie_header hdr; struct cie cie; struct elf_link_hash_table *htab; struct eh_frame_hdr_info *hdr_info; struct eh_frame_sec_info *sec_info = NULL; unsigned int cie_usage_count, offset; unsigned int ptr_size; if (sec->size == 0) { /* This file does not contain .eh_frame information. */ return FALSE; } if ((sec->output_section != NULL && bfd_is_abs_section (sec->output_section))) { /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return FALSE; } htab = elf_hash_table (info); hdr_info = &htab->eh_info; /* Read the frame unwind information from abfd. */ REQUIRE (bfd_malloc_and_get_section (abfd, sec, &ehbuf)); if (sec->size >= 4 && bfd_get_32 (abfd, ehbuf) == 0 && cookie->rel == cookie->relend) { /* Empty .eh_frame section. */ free (ehbuf); return FALSE; } /* If .eh_frame section size doesn't fit into int, we cannot handle it (it would need to use 64-bit .eh_frame format anyway). */ REQUIRE (sec->size == (unsigned int) sec->size); ptr_size = (get_elf_backend_data (abfd) ->elf_backend_eh_frame_address_size (abfd, sec)); REQUIRE (ptr_size != 0); buf = ehbuf; last_cie = NULL; last_cie_inf = NULL; memset (&cie, 0, sizeof (cie)); cie_usage_count = 0; sec_info = bfd_zmalloc (sizeof (struct eh_frame_sec_info) + 99 * sizeof (struct eh_cie_fde)); REQUIRE (sec_info); sec_info->alloced = 100; #define ENSURE_NO_RELOCS(buf) \ REQUIRE (!(cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf)) \ && cookie->rel->r_info != 0)) #define SKIP_RELOCS(buf) \ while (cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ < (bfd_size_type) ((buf) - ehbuf))) \ cookie->rel++ #define GET_RELOC(buf) \ ((cookie->rel < cookie->relend \ && (cookie->rel->r_offset \ == (bfd_size_type) ((buf) - ehbuf))) \ ? cookie->rel : NULL) for (;;) { char *aug; bfd_byte *start, *end, *insns; bfd_size_type length; if (sec_info->count == sec_info->alloced) { struct eh_cie_fde *old_entry = sec_info->entry; sec_info = bfd_realloc (sec_info, sizeof (struct eh_frame_sec_info) + ((sec_info->alloced + 99) * sizeof (struct eh_cie_fde))); REQUIRE (sec_info); memset (&sec_info->entry[sec_info->alloced], 0, 100 * sizeof (struct eh_cie_fde)); sec_info->alloced += 100; /* Now fix any pointers into the array. */ if (last_cie_inf >= old_entry && last_cie_inf < old_entry + sec_info->count) last_cie_inf = sec_info->entry + (last_cie_inf - old_entry); } this_inf = sec_info->entry + sec_info->count; last_fde = buf; /* If we are at the end of the section, we still need to decide on whether to output or discard last encountered CIE (if any). */ if ((bfd_size_type) (buf - ehbuf) == sec->size) { hdr.length = 0; hdr.id = (unsigned int) -1; end = buf; } else { /* Read the length of the entry. */ REQUIRE (skip_bytes (&buf, ehbuf + sec->size, 4)); hdr.length = bfd_get_32 (abfd, buf - 4); /* 64-bit .eh_frame is not supported. */ REQUIRE (hdr.length != 0xffffffff); /* The CIE/FDE must be fully contained in this input section. */ REQUIRE ((bfd_size_type) (buf - ehbuf) + hdr.length <= sec->size); end = buf + hdr.length; this_inf->offset = last_fde - ehbuf; this_inf->size = 4 + hdr.length; if (hdr.length == 0) { /* A zero-length CIE should only be found at the end of the section. */ REQUIRE ((bfd_size_type) (buf - ehbuf) == sec->size); ENSURE_NO_RELOCS (buf); sec_info->count++; /* Now just finish last encountered CIE processing and break the loop. */ hdr.id = (unsigned int) -1; } else { REQUIRE (skip_bytes (&buf, end, 4)); hdr.id = bfd_get_32 (abfd, buf - 4); REQUIRE (hdr.id != (unsigned int) -1); } } if (hdr.id == 0 || hdr.id == (unsigned int) -1) { unsigned int initial_insn_length; /* CIE */ if (last_cie != NULL) { /* Now check if this CIE is identical to the last CIE, in which case we can remove it provided we adjust all FDEs. Also, it can be removed if we have removed all FDEs using it. */ if ((!info->relocatable && hdr_info->last_cie_sec && (sec->output_section == hdr_info->last_cie_sec->output_section) && cie_compare (&cie, &hdr_info->last_cie) == 0) || cie_usage_count == 0) last_cie_inf->removed = 1; else { hdr_info->last_cie = cie; hdr_info->last_cie_sec = sec; last_cie_inf->make_relative = cie.make_relative; last_cie_inf->make_lsda_relative = cie.make_lsda_relative; last_cie_inf->per_encoding_relative = (cie.per_encoding & 0x70) == DW_EH_PE_pcrel; } } if (hdr.id == (unsigned int) -1) break; last_cie_inf = this_inf; this_inf->cie = 1; cie_usage_count = 0; memset (&cie, 0, sizeof (cie)); cie.hdr = hdr; REQUIRE (read_byte (&buf, end, &cie.version)); /* Cannot handle unknown versions. */ REQUIRE (cie.version == 1 || cie.version == 3); REQUIRE (strlen ((char *) buf) < sizeof (cie.augmentation)); strcpy (cie.augmentation, (char *) buf); buf = (bfd_byte *) strchr ((char *) buf, '\0') + 1; ENSURE_NO_RELOCS (buf); if (buf[0] == 'e' && buf[1] == 'h') { /* GCC < 3.0 .eh_frame CIE */ /* We cannot merge "eh" CIEs because __EXCEPTION_TABLE__ is private to each CIE, so we don't need it for anything. Just skip it. */ REQUIRE (skip_bytes (&buf, end, ptr_size)); SKIP_RELOCS (buf); } REQUIRE (read_uleb128 (&buf, end, &cie.code_align)); REQUIRE (read_sleb128 (&buf, end, &cie.data_align)); if (cie.version == 1) { REQUIRE (buf < end); cie.ra_column = *buf++; } else REQUIRE (read_uleb128 (&buf, end, &cie.ra_column)); ENSURE_NO_RELOCS (buf); cie.lsda_encoding = DW_EH_PE_omit; cie.fde_encoding = DW_EH_PE_omit; cie.per_encoding = DW_EH_PE_omit; aug = cie.augmentation; if (aug[0] != 'e' || aug[1] != 'h') { if (*aug == 'z') { aug++; REQUIRE (read_uleb128 (&buf, end, &cie.augmentation_size)); ENSURE_NO_RELOCS (buf); } while (*aug != '\0') switch (*aug++) { case 'L': REQUIRE (read_byte (&buf, end, &cie.lsda_encoding)); ENSURE_NO_RELOCS (buf); REQUIRE (get_DW_EH_PE_width (cie.lsda_encoding, ptr_size)); break; case 'R': REQUIRE (read_byte (&buf, end, &cie.fde_encoding)); ENSURE_NO_RELOCS (buf); REQUIRE (get_DW_EH_PE_width (cie.fde_encoding, ptr_size)); break; case 'P': { int per_width; REQUIRE (read_byte (&buf, end, &cie.per_encoding)); per_width = get_DW_EH_PE_width (cie.per_encoding, ptr_size); REQUIRE (per_width); if ((cie.per_encoding & 0xf0) == DW_EH_PE_aligned) { length = -(buf - ehbuf) & (per_width - 1); REQUIRE (skip_bytes (&buf, end, length)); } ENSURE_NO_RELOCS (buf); /* Ensure we have a reloc here, against a global symbol. */ if (GET_RELOC (buf) != NULL) { unsigned long r_symndx; #ifdef BFD64 if (ptr_size == 8) r_symndx = ELF64_R_SYM (cookie->rel->r_info); else #endif r_symndx = ELF32_R_SYM (cookie->rel->r_info); if (r_symndx >= cookie->locsymcount) { struct elf_link_hash_entry *h; r_symndx -= cookie->extsymoff; h = cookie->sym_hashes[r_symndx]; while (h->root.type == bfd_link_hash_indirect || h->root.type == bfd_link_hash_warning) h = (struct elf_link_hash_entry *) h->root.u.i.link; cie.personality = h; } /* Cope with MIPS-style composite relocations. */ do cookie->rel++; while (GET_RELOC (buf) != NULL); } REQUIRE (skip_bytes (&buf, end, per_width)); } break; default: /* Unrecognized augmentation. Better bail out. */ goto free_no_table; } } /* For shared libraries, try to get rid of as many RELATIVE relocs as possible. */ if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_relative_eh_frame (abfd, info, sec))) { if ((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr) cie.make_relative = 1; /* If the CIE doesn't already have an 'R' entry, it's fairly easy to add one, provided that there's no aligned data after the augmentation string. */ else if (cie.fde_encoding == DW_EH_PE_omit && (cie.per_encoding & 0xf0) != DW_EH_PE_aligned) { if (*cie.augmentation == 0) this_inf->add_augmentation_size = 1; this_inf->add_fde_encoding = 1; cie.make_relative = 1; } } if (info->shared && (get_elf_backend_data (abfd) ->elf_backend_can_make_lsda_relative_eh_frame (abfd, info, sec)) && (cie.lsda_encoding & 0xf0) == DW_EH_PE_absptr) cie.make_lsda_relative = 1; /* If FDE encoding was not specified, it defaults to DW_EH_absptr. */ if (cie.fde_encoding == DW_EH_PE_omit) cie.fde_encoding = DW_EH_PE_absptr; initial_insn_length = end - buf; if (initial_insn_length <= 50) { cie.initial_insn_length = initial_insn_length; memcpy (cie.initial_instructions, buf, initial_insn_length); } insns = buf; buf += initial_insn_length; ENSURE_NO_RELOCS (buf); last_cie = last_fde; } else { /* Ensure this FDE uses the last CIE encountered. */ REQUIRE (last_cie); REQUIRE (hdr.id == (unsigned int) (buf - 4 - last_cie)); ENSURE_NO_RELOCS (buf); REQUIRE (GET_RELOC (buf)); if ((*reloc_symbol_deleted_p) (buf - ehbuf, cookie)) /* This is a FDE against a discarded section. It should be deleted. */ this_inf->removed = 1; else { if (info->shared && (((cie.fde_encoding & 0xf0) == DW_EH_PE_absptr && cie.make_relative == 0) || (cie.fde_encoding & 0xf0) == DW_EH_PE_aligned)) { /* If a shared library uses absolute pointers which we cannot turn into PC relative, don't create the binary search table, since it is affected by runtime relocations. */ hdr_info->table = FALSE; } cie_usage_count++; hdr_info->fde_count++; } /* Skip the initial location and address range. */ start = buf; length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size); REQUIRE (skip_bytes (&buf, end, 2 * length)); /* Skip the augmentation size, if present. */ if (cie.augmentation[0] == 'z') REQUIRE (read_uleb128 (&buf, end, &length)); else length = 0; /* Of the supported augmentation characters above, only 'L' adds augmentation data to the FDE. This code would need to be adjusted if any future augmentations do the same thing. */ if (cie.lsda_encoding != DW_EH_PE_omit) { this_inf->lsda_offset = buf - start; /* If there's no 'z' augmentation, we don't know where the CFA insns begin. Assume no padding. */ if (cie.augmentation[0] != 'z') length = end - buf; } /* Skip over the augmentation data. */ REQUIRE (skip_bytes (&buf, end, length)); insns = buf; buf = last_fde + 4 + hdr.length; SKIP_RELOCS (buf); } /* Try to interpret the CFA instructions and find the first padding nop. Shrink this_inf's size so that it doesn't including the padding. */ length = get_DW_EH_PE_width (cie.fde_encoding, ptr_size); insns = skip_non_nops (insns, end, length); if (insns != 0) this_inf->size -= end - insns; this_inf->fde_encoding = cie.fde_encoding; this_inf->lsda_encoding = cie.lsda_encoding; sec_info->count++; } elf_section_data (sec)->sec_info = sec_info; sec->sec_info_type = ELF_INFO_TYPE_EH_FRAME; /* Ok, now we can assign new offsets. */ offset = 0; last_cie_inf = hdr_info->last_cie_inf; for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent) if (!ent->removed) { if (ent->cie) last_cie_inf = ent; else ent->cie_inf = last_cie_inf; ent->new_offset = offset; offset += size_of_output_cie_fde (ent, ptr_size); } hdr_info->last_cie_inf = last_cie_inf; /* Resize the sec as needed. */ sec->rawsize = sec->size; sec->size = offset; if (sec->size == 0) sec->flags |= SEC_EXCLUDE; free (ehbuf); return offset != sec->rawsize; free_no_table: if (ehbuf) free (ehbuf); if (sec_info) free (sec_info); hdr_info->table = FALSE; hdr_info->last_cie.hdr.length = 0; return FALSE; #undef REQUIRE }