This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
RFC: Detecting debuginfo files
- From: Nick Clifton <nickc at redhat dot com>
- To: binutils at sourceware dot org
- Date: Wed, 26 Jun 2019 18:07:55 +0100
- Subject: RFC: Detecting debuginfo files
Hi Guys,
PR 24717 reports a problem with objcopy producing an unhelpful warning
message when copying a debuginfo file. The issue is that these files
do not strictly conform to the ELF standard, so whilst the warning is
technically correct, it is nonetheless misleading.
I have created a patch (attached) to add a function to the BFD library
to detect debuginfo files, and then use this function to suppress the
warning message. The function is intentionally made public so that
other users of the BFD library can access it if they need to.
The detection function currently uses a simple heuristic - any file
whose allocated sections are only of the SHT_NOBITS or SHT_NOTE types
are assumed to be debuginfo files. My intention is that if a new
heuristic comes along, or - better - a standard is agreed for marking
such files, then the function can be updated to suit.
I have however been unable to come up with a good way to test the
patch. Creating a debuginfo file with the required properties is not
easy as objcopy's --only-keep-debug option does not suffice. (It
does not put the .note.gnu.property section outside of a loadable
segment). It turns out that in real life, well Fedora life anyway,
debuginfo files are created by a whole series of steps involving both
eu-strip, objcopy, nm and readelf. So my question is, apart from
having any comments on the patch, does anyone have any ideas for a
simple way to create a slightly non-conformant debuginfo file ? :-)
Cheers
Nick
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 0d12f4533a..a6a831b206 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2751,6 +2751,8 @@ extern bfd_vma elf64_r_sym (bfd_vma);
extern bfd_vma elf32_r_info (bfd_vma, bfd_vma);
extern bfd_vma elf32_r_sym (bfd_vma);
+extern bfd_boolean is_debuginfo_file (bfd *);
+
/* Large common section. */
extern asection _bfd_elf_large_com_section;
diff --git a/bfd/elf.c b/bfd/elf.c
index 2094fca1ff..b60987296b 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5810,6 +5810,35 @@ assign_file_positions_for_load_sections (bfd *abfd,
return TRUE;
}
+/* Determine if a bfd is a debuginfo file. Unfortunately there
+ is no defined method for detecting such files, so we have to
+ use heuristics instead. */
+
+bfd_boolean
+is_debuginfo_file (bfd *abfd)
+{
+ if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
+ return FALSE;
+
+ Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
+ Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
+ Elf_Internal_Shdr **headerp;
+
+ for (headerp = start_headers; headerp < end_headers; headerp ++)
+ {
+ Elf_Internal_Shdr *header = * headerp;
+
+ /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
+ The only allocated sections are SHT_NOBITS or SHT_NOTES. */
+ if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
+ && header->sh_type != SHT_NOBITS
+ && header->sh_type != SHT_NOTE)
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
/* Assign file positions for the other sections. */
static bfd_boolean
@@ -5843,7 +5872,13 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
else if ((hdr->sh_flags & SHF_ALLOC) != 0)
{
- if (hdr->sh_size != 0)
+ if (hdr->sh_size != 0
+ /* PR 24717 - debuginfo files are known to be not strictly
+ complaint with the ELF stanard. In particular they often
+ have .note.gnu.property sections that are outside of any
+ loadable segment. This is not a problem for such files,
+ so do not warn about them. */
+ && ! is_debuginfo_file (abfd))
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: warning: allocated section `%s' not in segment"),