]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
RISC-V: Segment fault for kernel purgatory when linking.
authorNelson Chu <nelson@rivosinc.com>
Mon, 29 Apr 2024 09:44:51 +0000 (17:44 +0800)
committerNelson Chu <nelson@rivosinc.com>
Mon, 27 May 2024 17:38:17 +0000 (01:38 +0800)
This was originally reported by Ard Biesheuvel <ardb@kernel.org>.

The followings are reproduce steps,
https://lore.kernel.org/all/202404260640.9GQVTmrw-lkp@intel.com/T/#u

The segment fault happens in the riscv_elf_finish_dynamic_sections when the
output got section is an ABS.  Refer to MIPS code, they added an extra
bfd_is_abs_section check to avoid ABS got, so this seems the right and easier
way to go in the short-term.

bfd/
* elfnn-riscv.c (riscv_elf_finish_dynamic_sections): Set sh_entsize
and fill the got entries only when the got isn't an ABS section, and
the size of got is larger than zero.  The similar goes for gotplt,
except we already reported error when the gotplt is an ABS.

bfd/elfnn-riscv.c

index 604f6de451103e51c2ca28ac1a7569ddb54ba8dc..3fc87267f30f17737530eabc6f1ae5566167d84f 100644 (file)
@@ -3569,7 +3569,7 @@ riscv_elf_finish_dynamic_sections (bfd *output_bfd,
        }
     }
 
-  if (htab->elf.sgotplt)
+  if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
     {
       asection *output_section = htab->elf.sgotplt->output_section;
 
@@ -3580,31 +3580,28 @@ riscv_elf_finish_dynamic_sections (bfd *output_bfd,
          return false;
        }
 
-      if (htab->elf.sgotplt->size > 0)
-       {
-         /* Write the first two entries in .got.plt, needed for the dynamic
-            linker.  */
-         bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
-         bfd_put_NN (output_bfd, (bfd_vma) 0,
-                     htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
-       }
+      /* Write the first two entries in .got.plt, needed for the dynamic
+        linker.  */
+      bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
+      bfd_put_NN (output_bfd, (bfd_vma) 0,
+                 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
 
       elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
     }
 
-  if (htab->elf.sgot)
+  if (htab->elf.sgot && htab->elf.sgot->size > 0)
     {
       asection *output_section = htab->elf.sgot->output_section;
 
-      if (htab->elf.sgot->size > 0)
+      if (!bfd_is_abs_section (output_section))
        {
          /* Set the first entry in the global offset table to the address of
             the dynamic section.  */
          bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
          bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
-       }
 
-      elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
+         elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
+       }
     }
 
   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */