* dwarf2.c: Formatting.

(arange_add): Pass in unit rather than bfd.  Update callers.
	Ignore empty ranges.  Don't ask for cleared memory.
	(parse_comp_unit): Only set unit->base_address if processing
	DW_TAG_compile_unit.
	(find_debug_info): Optimise section lookup.
	(place_sections): Use bfd_alloc for stash->adjusted_sections.
	(find_line): Match previously parsed comp unit addresses as we
	do for newly parsed comp units.
This commit is contained in:
Alan Modra
2012-05-16 13:57:11 +00:00
parent df35687a29
commit a2a5095467
2 changed files with 128 additions and 74 deletions

View File

@ -1,3 +1,15 @@
2012-05-16 Alan Modra <amodra@gmail.com>
* dwarf2.c: Formatting.
(arange_add): Pass in unit rather than bfd. Update callers.
Ignore empty ranges. Don't ask for cleared memory.
(parse_comp_unit): Only set unit->base_address if processing
DW_TAG_compile_unit.
(find_debug_info): Optimise section lookup.
(place_sections): Use bfd_alloc for stash->adjusted_sections.
(find_line): Match previously parsed comp unit addresses as we
do for newly parsed comp units.
2012-05-16 Alan Modra <amodra@gmail.com> 2012-05-16 Alan Modra <amodra@gmail.com>
* archive.c (_bfd_generic_read_ar_hdr_mag): Ensure sscanf * archive.c (_bfd_generic_read_ar_hdr_mag): Ensure sscanf

View File

@ -405,8 +405,8 @@ create_info_hash_table (bfd *abfd)
{ {
struct info_hash_table *hash_table; struct info_hash_table *hash_table;
hash_table = (struct info_hash_table *) hash_table = ((struct info_hash_table *)
bfd_alloc (abfd, sizeof (struct info_hash_table)); bfd_alloc (abfd, sizeof (struct info_hash_table)));
if (!hash_table) if (!hash_table)
return hash_table; return hash_table;
@ -522,7 +522,8 @@ read_section (bfd * abfd,
that the client wants. Validate it here to avoid trouble later. */ that the client wants. Validate it here to avoid trouble later. */
if (offset != 0 && offset >= *section_size) if (offset != 0 && offset >= *section_size)
{ {
(*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."), (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu)"
" greater than or equal to %s size (%lu)."),
(long) offset, section_name, *section_size); (long) offset, section_name, *section_size);
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
return FALSE; return FALSE;
@ -1018,16 +1019,23 @@ struct line_info_table
struct funcinfo struct funcinfo
{ {
struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */ /* Pointer to previous function in list of all functions. */
struct funcinfo *caller_func; /* Pointer to function one scope higher */ struct funcinfo *prev_func;
char *caller_file; /* Source location file name where caller_func inlines this func */ /* Pointer to function one scope higher. */
int caller_line; /* Source location line number where caller_func inlines this func */ struct funcinfo *caller_func;
char *file; /* Source location file name */ /* Source location file name where caller_func inlines this func. */
int line; /* Source location line number */ char *caller_file;
/* Source location line number where caller_func inlines this func. */
int caller_line;
/* Source location file name. */
char *file;
/* Source location line number. */
int line;
int tag; int tag;
char *name; char *name;
struct arange arange; struct arange arange;
asection *sec; /* Where the symbol is defined */ /* Where the symbol is defined. */
asection *sec;
}; };
struct varinfo struct varinfo
@ -1248,11 +1256,15 @@ concat_filename (struct line_info_table *table, unsigned int file)
} }
static bfd_boolean static bfd_boolean
arange_add (bfd *abfd, struct arange *first_arange, arange_add (const struct comp_unit *unit, struct arange *first_arange,
bfd_vma low_pc, bfd_vma high_pc) bfd_vma low_pc, bfd_vma high_pc)
{ {
struct arange *arange; struct arange *arange;
/* Ignore empty ranges. */
if (low_pc == high_pc)
return TRUE;
/* If the first arange is empty, use it. */ /* If the first arange is empty, use it. */
if (first_arange->high == 0) if (first_arange->high == 0)
{ {
@ -1281,7 +1293,7 @@ arange_add (bfd *abfd, struct arange *first_arange,
/* Need to allocate a new arange and insert it into the arange list. /* Need to allocate a new arange and insert it into the arange list.
Order isn't significant, so just insert after the first arange. */ Order isn't significant, so just insert after the first arange. */
arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange)); arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
if (arange == NULL) if (arange == NULL)
return FALSE; return FALSE;
arange->low = low_pc; arange->low = low_pc;
@ -1582,15 +1594,15 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
/* Special operand. */ /* Special operand. */
adj_opcode = op_code - lh.opcode_base; adj_opcode = op_code - lh.opcode_base;
if (lh.maximum_ops_per_insn == 1) if (lh.maximum_ops_per_insn == 1)
address += (adj_opcode / lh.line_range) address += (adj_opcode / lh.line_range
* lh.minimum_instruction_length; * lh.minimum_instruction_length);
else else
{ {
address += ((op_index + (adj_opcode / lh.line_range)) address += ((op_index + adj_opcode / lh.line_range)
/ lh.maximum_ops_per_insn) / lh.maximum_ops_per_insn
* lh.minimum_instruction_length; * lh.minimum_instruction_length);
op_index = (op_index + (adj_opcode / lh.line_range)) op_index = ((op_index + adj_opcode / lh.line_range)
% lh.maximum_ops_per_insn; % lh.maximum_ops_per_insn);
} }
line += lh.line_base + (adj_opcode % lh.line_range); line += lh.line_base + (adj_opcode % lh.line_range);
/* Append row to matrix using current values. */ /* Append row to matrix using current values. */
@ -1621,7 +1633,7 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
low_pc = address; low_pc = address;
if (address > high_pc) if (address > high_pc)
high_pc = address; high_pc = address;
if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc)) if (!arange_add (unit, &unit->arange, low_pc, high_pc))
goto line_fail; goto line_fail;
break; break;
case DW_LNE_set_address: case DW_LNE_set_address:
@ -1663,7 +1675,8 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
line_ptr += exop_len - 1; line_ptr += exop_len - 1;
break; break;
default: default:
(*_bfd_error_handler) (_("Dwarf Error: mangled line number section.")); (*_bfd_error_handler)
(_("Dwarf Error: mangled line number section."));
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
line_fail: line_fail:
if (filename != NULL) if (filename != NULL)
@ -1682,15 +1695,15 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
break; break;
case DW_LNS_advance_pc: case DW_LNS_advance_pc:
if (lh.maximum_ops_per_insn == 1) if (lh.maximum_ops_per_insn == 1)
address += lh.minimum_instruction_length address += (lh.minimum_instruction_length
* read_unsigned_leb128 (abfd, line_ptr, * read_unsigned_leb128 (abfd, line_ptr,
&bytes_read); &bytes_read));
else else
{ {
bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr, bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
&bytes_read); &bytes_read);
address = ((op_index + adjust) / lh.maximum_ops_per_insn) address = ((op_index + adjust) / lh.maximum_ops_per_insn
* lh.minimum_instruction_length; * lh.minimum_instruction_length);
op_index = (op_index + adjust) % lh.maximum_ops_per_insn; op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
} }
line_ptr += bytes_read; line_ptr += bytes_read;
@ -1723,13 +1736,14 @@ decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
break; break;
case DW_LNS_const_add_pc: case DW_LNS_const_add_pc:
if (lh.maximum_ops_per_insn == 1) if (lh.maximum_ops_per_insn == 1)
address += lh.minimum_instruction_length address += (lh.minimum_instruction_length
* ((255 - lh.opcode_base) / lh.line_range); * ((255 - lh.opcode_base) / lh.line_range));
else else
{ {
bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range); bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
address += lh.minimum_instruction_length address += (lh.minimum_instruction_length
* ((op_index + adjust) / lh.maximum_ops_per_insn); * ((op_index + adjust)
/ lh.maximum_ops_per_insn));
op_index = (op_index + adjust) % lh.maximum_ops_per_insn; op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
} }
break; break;
@ -1855,8 +1869,9 @@ lookup_address_in_function_table (struct comp_unit *unit,
{ {
if (addr >= arange->low && addr < arange->high) if (addr >= arange->low && addr < arange->high)
{ {
if (!best_fit || if (!best_fit
((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low))) || (arange->high - arange->low
< best_fit->arange.high - best_fit->arange.low))
best_fit = each_func; best_fit = each_func;
} }
} }
@ -1904,8 +1919,8 @@ lookup_symbol_in_function_table (struct comp_unit *unit,
&& each_func->name && each_func->name
&& strcmp (name, each_func->name) == 0 && strcmp (name, each_func->name) == 0
&& (!best_fit && (!best_fit
|| ((arange->high - arange->low) || (arange->high - arange->low
< (best_fit->arange.high - best_fit->arange.low)))) < best_fit->arange.high - best_fit->arange.low)))
best_fit = each_func; best_fit = each_func;
} }
} }
@ -1990,8 +2005,8 @@ find_abstract_instance_name (struct comp_unit *unit,
abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
if (! abbrev) if (! abbrev)
{ {
(*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), (*_bfd_error_handler)
abbrev_number); (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number);
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
} }
else else
@ -2056,7 +2071,7 @@ read_rangelist (struct comp_unit *unit, struct arange *arange,
base_address = high_pc; base_address = high_pc;
else else
{ {
if (!arange_add (unit->abfd, arange, if (!arange_add (unit, arange,
base_address + low_pc, base_address + high_pc)) base_address + low_pc, base_address + high_pc))
return FALSE; return FALSE;
} }
@ -2286,7 +2301,7 @@ scan_unit_for_symbols (struct comp_unit *unit)
if (func && high_pc != 0) if (func && high_pc != 0)
{ {
if (!arange_add (unit->abfd, &func->arange, low_pc, high_pc)) if (!arange_add (unit, &func->arange, low_pc, high_pc))
goto fail; goto fail;
} }
@ -2301,7 +2316,7 @@ scan_unit_for_symbols (struct comp_unit *unit)
nested_funcs_size *= 2; nested_funcs_size *= 2;
tmp = (struct funcinfo **) tmp = (struct funcinfo **)
bfd_realloc (nested_funcs, bfd_realloc (nested_funcs,
(nested_funcs_size * sizeof (struct funcinfo *))); nested_funcs_size * sizeof (struct funcinfo *));
if (tmp == NULL) if (tmp == NULL)
goto fail; goto fail;
nested_funcs = tmp; nested_funcs = tmp;
@ -2362,14 +2377,18 @@ parse_comp_unit (struct dwarf2_debug *stash,
if (version != 2 && version != 3 && version != 4) if (version != 2 && version != 3 && version != 4)
{ {
(*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2, 3 and 4 information."), version); (*_bfd_error_handler)
(_("Dwarf Error: found dwarf version '%u', this reader"
" only handles version 2, 3 and 4 information."), version);
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
return 0; return 0;
} }
if (addr_size > sizeof (bfd_vma)) if (addr_size > sizeof (bfd_vma))
{ {
(*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."), (*_bfd_error_handler)
(_("Dwarf Error: found address size '%u', this reader"
" can not handle sizes greater than '%u'."),
addr_size, addr_size,
(unsigned int) sizeof (bfd_vma)); (unsigned int) sizeof (bfd_vma));
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
@ -2378,7 +2397,9 @@ parse_comp_unit (struct dwarf2_debug *stash,
if (addr_size != 2 && addr_size != 4 && addr_size != 8) if (addr_size != 2 && addr_size != 4 && addr_size != 8)
{ {
(*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size); (*_bfd_error_handler)
("Dwarf Error: found address size '%u', this reader"
" can only handle address sizes '2', '4' and '8'.", addr_size);
bfd_set_error (bfd_error_bad_value); bfd_set_error (bfd_error_bad_value);
return 0; return 0;
} }
@ -2445,6 +2466,7 @@ parse_comp_unit (struct dwarf2_debug *stash,
/* If the compilation unit DIE has a DW_AT_low_pc attribute, /* If the compilation unit DIE has a DW_AT_low_pc attribute,
this is the base address to use when reading location this is the base address to use when reading location
lists or range lists. */ lists or range lists. */
if (abbrev->tag == DW_TAG_compile_unit)
unit->base_address = low_pc; unit->base_address = low_pc;
break; break;
@ -2482,7 +2504,7 @@ parse_comp_unit (struct dwarf2_debug *stash,
high_pc += low_pc; high_pc += low_pc;
if (high_pc != 0) if (high_pc != 0)
{ {
if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc)) if (!arange_add (unit, &unit->arange, low_pc, high_pc))
return NULL; return NULL;
} }
@ -2752,25 +2774,43 @@ static asection *
find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections, find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
asection *after_sec) asection *after_sec)
{ {
asection * msec; asection *msec;
const char *look;
msec = after_sec != NULL ? after_sec->next : abfd->sections; if (after_sec == NULL)
while (msec)
{ {
if (strcmp (msec->name, look = debug_sections[debug_info].uncompressed_name;
debug_sections[debug_info].uncompressed_name) == 0) msec = bfd_get_section_by_name (abfd, look);
if (msec != NULL)
return msec; return msec;
if (debug_sections[debug_info].compressed_name != NULL look = debug_sections[debug_info].compressed_name;
&& strcmp (msec->name, if (look != NULL)
debug_sections[debug_info].compressed_name) == 0) {
msec = bfd_get_section_by_name (abfd, look);
if (msec != NULL)
return msec;
}
for (msec = abfd->sections; msec != NULL; msec = msec->next)
if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
return msec;
return NULL;
}
for (msec = after_sec->next; msec != NULL; msec = msec->next)
{
look = debug_sections[debug_info].uncompressed_name;
if (strcmp (msec->name, look) == 0)
return msec;
look = debug_sections[debug_info].compressed_name;
if (look != NULL && strcmp (msec->name, look) == 0)
return msec; return msec;
if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
return msec; return msec;
msec = msec->next;
} }
return NULL; return NULL;
@ -2844,7 +2884,7 @@ place_sections (bfd *abfd, struct dwarf2_debug *stash)
} }
amt = i * sizeof (struct adjusted_section); amt = i * sizeof (struct adjusted_section);
p = (struct adjusted_section *) bfd_zalloc (abfd, amt); p = (struct adjusted_section *) bfd_alloc (abfd, amt);
if (! p) if (! p)
return FALSE; return FALSE;
@ -2938,8 +2978,8 @@ info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
&& addr >= arange->low && addr >= arange->low
&& addr < arange->high && addr < arange->high
&& (!best_fit && (!best_fit
|| ((arange->high - arange->low) || (arange->high - arange->low
< (best_fit->arange.high - best_fit->arange.low)))) < best_fit->arange.high - best_fit->arange.low)))
best_fit = each_func; best_fit = each_func;
} }
} }
@ -3359,6 +3399,7 @@ find_line (bfd *abfd,
/* Check the previously read comp. units first. */ /* Check the previously read comp. units first. */
for (each = stash->all_comp_units; each; each = each->next_unit) for (each = stash->all_comp_units; each; each = each->next_unit)
if ((symbol->flags & BSF_FUNCTION) == 0 if ((symbol->flags & BSF_FUNCTION) == 0
|| each->arange.high == 0
|| comp_unit_contains_address (each, addr)) || comp_unit_contains_address (each, addr))
{ {
found = comp_unit_find_line (each, symbol, addr, filename_ptr, found = comp_unit_find_line (each, symbol, addr, filename_ptr,
@ -3372,7 +3413,8 @@ find_line (bfd *abfd,
{ {
for (each = stash->all_comp_units; each; each = each->next_unit) for (each = stash->all_comp_units; each; each = each->next_unit)
{ {
found = (comp_unit_contains_address (each, addr) found = ((each->arange.high == 0
|| comp_unit_contains_address (each, addr))
&& comp_unit_find_nearest_line (each, addr, && comp_unit_find_nearest_line (each, addr,
filename_ptr, filename_ptr,
functionname_ptr, functionname_ptr,
@ -3485,7 +3527,7 @@ find_line (bfd *abfd,
} }
} }
done: done:
if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
unset_sections (stash); unset_sections (stash);