objdump: is_same_section

This fixes a deficiency in commit 660df28acf, which should have used
the same logic as that in sym_ok.  Ideally both places would not
compare section names, but it can be a little tricky to match a
section in the real object file with a section in a debug file.
Extend commit 39f0547e55 to use section name, vma and size.

	* objcopy (is_same_section): New function.
	(compare_symbols, sym_ok): Use it here.
This commit is contained in:
Alan Modra
2025-02-28 17:56:48 +10:30
parent 7166166ec0
commit a8a9fc7429

View File

@@ -1107,6 +1107,28 @@ remove_useless_symbols (asymbol **symbols, long count)
return out_ptr - symbols; return out_ptr - symbols;
} }
/* Return true iff SEC1 and SEC2 are the same section.
This would just be a simple pointer comparison except that one of
the sections might be from a separate debug info file. */
static bool
is_same_section (const asection *sec1, const asection *sec2)
{
if (sec1 == sec2)
return true;
if (sec1->owner == sec2->owner
|| sec1->owner == NULL
|| sec2->owner == NULL)
return false;
/* OK, so we have one section in a debug info file. (Or they both
are, but the way this function is currently used sec1 will be in
a normal object.) Compare names, vma and size. This ought to
cover all the usual cases. */
return (sec1->vma == sec2->vma
&& sec1->size == sec2->size
&& strcmp (sec1->name, sec2->name) == 0);
}
static const asection *compare_section; static const asection *compare_section;
/* Sort symbols into value order. */ /* Sort symbols into value order. */
@@ -1131,10 +1153,9 @@ compare_symbols (const void *ap, const void *bp)
/* Prefer symbols from the section currently being disassembled. /* Prefer symbols from the section currently being disassembled.
Don't sort symbols from other sections by section, since there Don't sort symbols from other sections by section, since there
isn't much reason to prefer one section over another otherwise. isn't much reason to prefer one section over another otherwise. */
See sym_ok comment for why we compare by section name. */ as = is_same_section (compare_section, a->section);
as = strcmp (compare_section->name, a->section->name) == 0; bs = is_same_section (compare_section, b->section);
bs = strcmp (compare_section->name, b->section->name) == 0;
if (as && !bs) if (as && !bs)
return -1; return -1;
if (!as && bs) if (!as && bs)
@@ -1353,26 +1374,8 @@ sym_ok (bool want_section,
asection *sec, asection *sec,
struct disassemble_info *inf) struct disassemble_info *inf)
{ {
if (want_section) if (want_section && !is_same_section (sec, sorted_syms[place]->section))
{ return false;
/* NB: An object file can have different sections with the same
section name. Compare compare section pointers if they have
the same owner. */
if (sorted_syms[place]->section->owner == sec->owner
&& sorted_syms[place]->section != sec)
return false;
/* Note - we cannot just compare section pointers because they could
be different, but the same... Ie the symbol that we are trying to
find could have come from a separate debug info file. Under such
circumstances the symbol will be associated with a section in the
debug info file, whilst the section we want is in a normal file.
So the section pointers will be different, but the section names
will be the same. */
if (strcmp (bfd_section_name (sorted_syms[place]->section),
bfd_section_name (sec)) != 0)
return false;
}
return inf->symbol_is_valid (sorted_syms[place], inf); return inf->symbol_is_valid (sorted_syms[place], inf);
} }