Re: [PATCH] alpha: reallocate .got contents when relaxation grows a subsection
Matt Turner <[email protected]>
| Newsgroups | gmane.comp.gnu.binutils |
|---|---|
| Message-ID | <CAEdQ38FE5MmPb+=Vy_Tu635MTHab=Y2_JtNE_6d8W4M=n+YTkw@mail.gmail.com> |
On Wed, Aug 26, 2026 at 12:25 AM Alan Modra <[email protected]> wrote: > The above happens once per relaxation pass. Worse than that, actually: it happens once per relaxation *trip*. lang_relax_sections loops on relax_trip until relaxation converges, and the guard here is relax_trip, so the re-size runs on every trip of pass 0. > You say "we may only merge got sections during the first pass". Is > that really true? Yes, and it is enforced rather than merely documented: may_merge is relax_pass == 0, and merging is the only thing that can grow a subsection. alphaelf.em sets relax_pass = 2, so pass 1 never merges. But as above, pass 0 has an unbounded number of trips. > If not, then it may be better to bfd_malloc/bfd_realloc that memory. I think there is a simpler answer that avoids reallocation entirely. Every got subsection is capped at MAX_GOT_SIZE for the whole link: elf64_alpha_size_got_sections rejects any single input object whose got exceeds it, and elf64_alpha_can_merge_gots refuses any merge that would cross it. So elf64_alpha_early_size_sections can allocate MAX_GOT_SIZE for each subsection up front and the buffer is big enough no matter what later merges do. That drops elf64_alpha_realloc_got_contents and the got_alloced field, and the assertion in alpha_got_slot can just test against the section's current size. The cost is at most 64K of slack per got subsection. Merging packs subsections close to the limit, so in practice that is one partly-used subsection, and it is bfd_alloc memory freed with the bfd. Note that switching to bfd_malloc would not remove the leak on its own: s->alloced has to stay set either way, since it means "do not free these contents", and _bfd_elf_munmap_section_contents would otherwise free the buffer and have ld re-read the got from the input file. Malloced memory with alloced set is simply never freed. I'll respin with the up-front MAX_GOT_SIZE allocation unless you prefer the realloc approach.