ld/pdb: Avoid quadratic section lookup in handle_debugs_section

Message ID 20261102225947.1725-2-oleg.tolmatcev@gmail.com
State New
Headers
Series ld/pdb: Avoid quadratic section lookup in handle_debugs_section |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Oleg Tolmatcev Nov. 2, 2026, 10:59 p.m. UTC
  To map symbols to their sections, handle_debugs_section scanned all
symbols once per section, for every .debug$S section.  With many
sections and symbols per object, this took about half of ld's time
when linking with --pdb.

Use coff_section_from_bfd_index, which looks sections up in a hash
table cached on the BFD.

ld/
	* pdb.c (handle_debugs_section): Use coff_section_from_bfd_index
	to find the sections of symbols.
---
 ld/pdb.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
  

Comments

Jan Beulich Nov. 6, 2026, 9:18 a.m. UTC | #1
On 02.11.2026 23:59, Oleg Tolmatcev wrote:
> To map symbols to their sections, handle_debugs_section scanned all
> symbols once per section, for every .debug$S section.  With many
> sections and symbols per object, this took about half of ld's time
> when linking with --pdb.
> 
> Use coff_section_from_bfd_index, which looks sections up in a hash
> table cached on the BFD.
> 
> ld/
> 	* pdb.c (handle_debugs_section): Use coff_section_from_bfd_index
> 	to find the sections of symbols.

Okay.

A remark on your sending of this and the other 5 patches: If they're
meant to be a series, they should be numbered accordingly. A series
ideally would also have a cover letter. If they're not meant to be a
series, later patches shouldn't be replies to earlier ones.

Jan
  
Oleg Tolmatcev Nov. 6, 2026, 9:28 a.m. UTC | #2
пт, 6 нояб. 2026 г. в 10:18, Jan Beulich <jbeulich@suse.com>:
>
> On 02.11.2026 23:59, Oleg Tolmatcev wrote:
> > To map symbols to their sections, handle_debugs_section scanned all
> > symbols once per section, for every .debug$S section.  With many
> > sections and symbols per object, this took about half of ld's time
> > when linking with --pdb.
> >
> > Use coff_section_from_bfd_index, which looks sections up in a hash
> > table cached on the BFD.
> >
> > ld/
> >       * pdb.c (handle_debugs_section): Use coff_section_from_bfd_index
> >       to find the sections of symbols.
>
> Okay.
>
> A remark on your sending of this and the other 5 patches: If they're
> meant to be a series, they should be numbered accordingly. A series
> ideally would also have a cover letter. If they're not meant to be a
> series, later patches shouldn't be replies to earlier ones.
>
> Jan

Sorry, I forgot to add "--no-thread" to "git send-email".

Thanks
Oleg
  
Jan Beulich Nov. 6, 2026, 9:32 a.m. UTC | #3
On 06.11.2026 10:28, Oleg Tolmatcev wrote:
> пт, 6 нояб. 2026 г. в 10:18, Jan Beulich <jbeulich@suse.com>:
>> On 02.11.2026 23:59, Oleg Tolmatcev wrote:
>>> To map symbols to their sections, handle_debugs_section scanned all
>>> symbols once per section, for every .debug$S section.  With many
>>> sections and symbols per object, this took about half of ld's time
>>> when linking with --pdb.
>>>
>>> Use coff_section_from_bfd_index, which looks sections up in a hash
>>> table cached on the BFD.
>>>
>>> ld/
>>>       * pdb.c (handle_debugs_section): Use coff_section_from_bfd_index
>>>       to find the sections of symbols.
>>
>> Okay.
>>
>> A remark on your sending of this and the other 5 patches: If they're
>> meant to be a series, they should be numbered accordingly. A series
>> ideally would also have a cover letter. If they're not meant to be a
>> series, later patches shouldn't be replies to earlier ones.
> 
> Sorry, I forgot to add "--no-thread" to "git send-email".

Except that, now that I looked at patch 2 as well, that one depends on
patch 1 (contextually, not functionally). I haven't looked further yet.

Jan
  

Patch

diff --git a/ld/pdb.c b/ld/pdb.c
index 19799929c3a..b940c509de1 100644
--- a/ld/pdb.c
+++ b/ld/pdb.c
@@ -1940,7 +1940,6 @@  handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
       struct internal_syment *symbols;
       asection **sectlist;
       unsigned int syment_count;
-      int sect_num;
       struct external_syment *ext;
 
       syment_count = obj_raw_syment_count (mod);
@@ -1955,19 +1954,8 @@  handle_debugs_section (asection *s, bfd *mod, struct string_table *strings,
       for (unsigned int i = 0; i < syment_count; i++)
 	{
 	  bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]);
-	}
-
-      sect_num = 1;
-
-      for (asection *sect = mod->sections; sect; sect = sect->next)
-	{
-	  for (unsigned int i = 0; i < syment_count; i++)
-	    {
-	      if (symbols[i].n_scnum == sect_num)
-		sectlist[i] = sect;
-	    }
-
-	  sect_num++;
+	  if (symbols[i].n_scnum > 0)
+	    sectlist[i] = coff_section_from_bfd_index (mod, symbols[i].n_scnum);
 	}
 
       if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod,