]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
bfd: section merging for PE/COFF images
authorJan Beulich <jbeulich@suse.com>
Fri, 24 Oct 2025 13:12:07 +0000 (15:12 +0200)
committerJan Beulich <jbeulich@suse.com>
Fri, 24 Oct 2025 13:12:07 +0000 (15:12 +0200)
Leverage the generalized section merging to enable it also when linking
PE/COFF images (from ELF objects).

Sadly the previous hack in bfd_generic_get_relocated_section_contents()
(from "bfd: generalize _bfd_elf_merge_sections()") is getting yet more
bogus.

bfd/coffgen.c
bfd/cofflink.c
bfd/merge.c
bfd/peicode.h

index c1811cbce1cd8877e89c846b9207122a11d70de0..071ca0d133c15ba4a07dde17d210e42e65ec101c 100644 (file)
@@ -1259,9 +1259,17 @@ coff_write_alien_symbol (bfd *abfd,
     }
   else
     {
+      asection *isec = symbol->section;
+
       native->u.syment.n_scnum = output_section->target_index;
-      native->u.syment.n_value = (symbol->value
-                                 + symbol->section->output_offset);
+      native->u.syment.n_value = symbol->value;
+
+      if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
+         && !(symbol->flags & (BSF_SECTION_SYM | BSF_MERGE_RESOLVED)))
+       native->u.syment.n_value =
+         _bfd_merged_section_offset (abfd, &isec, symbol->value);
+
+      native->u.syment.n_value += isec->output_offset;
       if (! obj_pe (abfd))
        native->u.syment.n_value += output_section->vma;
 
index 2ce5a0273ae24e84489fb6616aedfa1b4c65f7d4..d1658ac1e89f9b416cfb262a4403e96466f14d1e 100644 (file)
@@ -2608,13 +2608,18 @@ _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
       {
        asection *sec;
 
-       sec = h->root.u.def.section->output_section;
+       sec = h->root.u.def.section;
+       isym.n_value = h->root.u.def.value;
+       if (sec->sec_info_type == SEC_INFO_TYPE_MERGE)
+         isym.n_value =
+           _bfd_merged_section_offset (output_bfd, &sec, isym.n_value);
+       isym.n_value += sec->output_offset;
+
+       sec = sec->output_section;
        if (bfd_is_abs_section (sec))
          isym.n_scnum = N_ABS;
        else
          isym.n_scnum = sec->target_index;
-       isym.n_value = (h->root.u.def.value
-                       + h->root.u.def.section->output_offset);
        if (! obj_pe (flaginfo->output_bfd))
          isym.n_value += sec->vma;
 #ifdef BFD64
index 0de35da5cf48687c11a475da0534ff4bccc32ffc..b25d55d85398a3488a42cf25467daba276767041 100644 (file)
@@ -1090,7 +1090,7 @@ _bfd_write_merged_section (bfd *output_bfd, asection *sec)
   struct sec_merge_sec_info *secinfo = sec->sec_info;
   file_ptr pos;
   unsigned char *contents;
-  Elf_Internal_Shdr *hdr;
+  Elf_Internal_Shdr *hdr = NULL;
 
   if (!secinfo)
     return false;
@@ -1099,8 +1099,9 @@ _bfd_write_merged_section (bfd *output_bfd, asection *sec)
     return true;
 
   /* FIXME: octets_per_byte.  */
-  hdr = &elf_section_data (sec->output_section)->this_hdr;
-  if (hdr->sh_offset == (file_ptr) -1)
+  if (bfd_get_flavour (output_bfd) == bfd_target_elf_flavour)
+    hdr = &elf_section_data (sec->output_section)->this_hdr;
+  if (hdr != NULL && hdr->sh_offset == (file_ptr) -1)
     {
       /* We must compress this section.  Write output to the
         buffer.  */
index 3a93e0087999421caecee9858e1290930d54c137..8192bb9fa4b743382b0b99b86c1c0bc64b6db6ce 100644 (file)
@@ -82,6 +82,11 @@ static bool pe_bfd_copy_private_bfd_data (bfd *, bfd *);
 #define coff_mkobject_hook pe_mkobject_hook
 
 #ifdef COFF_IMAGE_WITH_PE
+
+/* For the case of linking ELF objects into a PE binary.  */
+#undef TARGET_MERGE_SECTIONS
+#define TARGET_MERGE_SECTIONS true
+
 /* This structure contains static variables used by the ILF code.  */
 typedef asection * asection_ptr;