coff make_a_section_from_file tidy

Also support compressing a few more sections.

	* coffgen.c (make_a_section_from_file): Rename return_section
	to newsect.  Don't try to be clever matching section name.
	Compress .gnu.debuglto_.debug_ and .gnu.linkonce.wi. too.
	Only rename debug sections when decompressing for linker.
This commit is contained in:
Alan Modra
2022-12-07 15:07:47 +10:30
parent 0f45491c0d
commit 3198c863f6

View File

@ -51,7 +51,7 @@ make_a_section_from_file (bfd *abfd,
struct internal_scnhdr *hdr, struct internal_scnhdr *hdr,
unsigned int target_index) unsigned int target_index)
{ {
asection *return_section; asection *newsect;
char *name; char *name;
bool result = true; bool result = true;
flagword flags; flagword flags;
@ -107,34 +107,33 @@ make_a_section_from_file (bfd *abfd,
name[sizeof (hdr->s_name)] = 0; name[sizeof (hdr->s_name)] = 0;
} }
return_section = bfd_make_section_anyway (abfd, name); newsect = bfd_make_section_anyway (abfd, name);
if (return_section == NULL) if (newsect == NULL)
return false; return false;
return_section->vma = hdr->s_vaddr; newsect->vma = hdr->s_vaddr;
return_section->lma = hdr->s_paddr; newsect->lma = hdr->s_paddr;
return_section->size = hdr->s_size; newsect->size = hdr->s_size;
return_section->filepos = hdr->s_scnptr; newsect->filepos = hdr->s_scnptr;
return_section->rel_filepos = hdr->s_relptr; newsect->rel_filepos = hdr->s_relptr;
return_section->reloc_count = hdr->s_nreloc; newsect->reloc_count = hdr->s_nreloc;
bfd_coff_set_alignment_hook (abfd, return_section, hdr); bfd_coff_set_alignment_hook (abfd, newsect, hdr);
return_section->line_filepos = hdr->s_lnnoptr; newsect->line_filepos = hdr->s_lnnoptr;
return_section->lineno_count = hdr->s_nlnno; newsect->lineno_count = hdr->s_nlnno;
return_section->userdata = NULL; newsect->userdata = NULL;
return_section->next = NULL; newsect->next = NULL;
return_section->target_index = target_index; newsect->target_index = target_index;
if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section, if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
& flags))
result = false; result = false;
/* At least on i386-coff, the line number count for a shared library /* At least on i386-coff, the line number count for a shared library
section must be ignored. */ section must be ignored. */
if ((flags & SEC_COFF_SHARED_LIBRARY) != 0) if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
return_section->lineno_count = 0; newsect->lineno_count = 0;
if (hdr->s_nreloc != 0) if (hdr->s_nreloc != 0)
flags |= SEC_RELOC; flags |= SEC_RELOC;
@ -142,20 +141,19 @@ make_a_section_from_file (bfd *abfd,
if (hdr->s_scnptr != 0) if (hdr->s_scnptr != 0)
flags |= SEC_HAS_CONTENTS; flags |= SEC_HAS_CONTENTS;
return_section->flags = flags; newsect->flags = flags;
/* Compress/decompress DWARF debug sections with names: .debug_* and /* Compress/decompress DWARF debug sections. */
.zdebug_*, after the section flags is set. */
if ((flags & SEC_DEBUGGING) != 0 if ((flags & SEC_DEBUGGING) != 0
&& (flags & SEC_HAS_CONTENTS) != 0 && (flags & SEC_HAS_CONTENTS) != 0
&& strlen (name) > 7 && (startswith (name, ".debug_")
&& ((name[1] == 'd' && name[6] == '_') || startswith (name, ".zdebug_")
|| (strlen (name) > 8 && name[1] == 'z' && name[7] == '_'))) || startswith (name, ".gnu.debuglto_.debug_")
|| startswith (name, ".gnu.linkonce.wi.")))
{ {
enum { nothing, compress, decompress } action = nothing; enum { nothing, compress, decompress } action = nothing;
char *new_name = NULL;
if (bfd_is_section_compressed (abfd, return_section)) if (bfd_is_section_compressed (abfd, newsect))
{ {
/* Compressed section. Check if we should decompress. */ /* Compressed section. Check if we should decompress. */
if ((abfd->flags & BFD_DECOMPRESS)) if ((abfd->flags & BFD_DECOMPRESS))
@ -164,50 +162,40 @@ make_a_section_from_file (bfd *abfd,
else else
{ {
/* Normal section. Check if we should compress. */ /* Normal section. Check if we should compress. */
if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0) if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
action = compress; action = compress;
} }
switch (action) if (action == compress)
{ {
case nothing: if (!bfd_init_section_compress_status (abfd, newsect))
break;
case compress:
if (!bfd_init_section_compress_status (abfd, return_section))
{ {
_bfd_error_handler _bfd_error_handler
/* xgettext: c-format */ /* xgettext:c-format */
(_("%pB: unable to compress section %s"), (_("%pB: unable to compress section %s"), abfd, name);
abfd, name);
return false; return false;
} }
if (return_section->compress_status == COMPRESS_SECTION_DONE
&& name[1] != 'z')
{
new_name = bfd_debug_name_to_zdebug (abfd, name);
if (new_name == NULL)
return false;
}
break;
case decompress:
if (!bfd_init_section_decompress_status (abfd, return_section))
{
_bfd_error_handler
/* xgettext: c-format */
(_("%pB: unable to decompress section %s"),
abfd, name);
return false;
}
if (name[1] == 'z')
{
new_name = bfd_zdebug_name_to_debug (abfd, name);
if (new_name == NULL)
return false;
}
break;
} }
if (new_name != NULL) else if (action == decompress)
bfd_rename_section (return_section, new_name); {
if (!bfd_init_section_decompress_status (abfd, newsect))
{
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: unable to decompress section %s"), abfd, name);
return false;
}
if (abfd->is_linker_input
&& name[1] == 'z')
{
/* Rename section from .zdebug_* to .debug_* so that ld
scripts will see this section as a debug section. */
char *new_name = bfd_zdebug_name_to_debug (abfd, name);
if (new_name == NULL)
return false;
bfd_rename_section (newsect, new_name);
}
}
} }
return result; return result;