ubsan: integer overflow in section filepos subtraction
authorAlan Modra <amodra@gmail.com>
Sat, 1 Jan 2022 03:22:26 +0000 (13:52 +1030)
committerAlan Modra <amodra@gmail.com>
Sat, 1 Jan 2022 03:52:09 +0000 (14:22 +1030)
* elf.c (assign_file_positions_for_non_load_sections): Avoid
signed integer overflow.

bfd/elf.c

index 92c06f2e44f8aab4616f2595952ce90b34a55459..d8a270d5219f27b8622f8df0bbcf511bb993c9ad 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6289,8 +6289,7 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
                  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
                  if (hdr->sh_type != SHT_NOBITS)
                    {
-                     p->p_filesz = (sect->filepos - m->sections[0]->filepos
-                                    + hdr->sh_size);
+                     p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
                      /* NB: p_memsz of the loadable PT_NOTE segment
                         should be the same as p_filesz.  */
                      if (p->p_type == PT_NOTE