[binutils-gdb] LoongArch: Add support for OUTPUT_FORMAT("binary")

liu & zhensong liuzhensong@sourceware.org
Mon Aug 5 09:39:46 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3a83f0342e545a638aba06f27bba76938ef251ab

commit 3a83f0342e545a638aba06f27bba76938ef251ab
Author: mengqinggang <mengqinggang@loongson.cn>
Date:   Fri Jun 28 14:24:19 2024 +0800

    LoongArch: Add support for OUTPUT_FORMAT("binary")
    
    In binary output format, loongarch_elf_hash_table return NULL and result
    in segment fault.
    
    When ld output binary file, it seems that elf related functions should
    not be called. But loongarch_elf_relax_section be called and
    loongarch_elf_hash_table cause segment fault.
    
    Just redefined loongarch_elf_hash_table and always return
    link_info->hash.
    
    The tests of binutils, glibc and gcc is ok.
    
    0  loongarch_elf_relax_section ()
    1  0x000055555557ab28 in lang_size_sections_1 ()
    2  0x000055555557a16c in lang_size_sections_1 ()
    3  0x000055555557b0a8 in one_lang_size_sections_pass ()
    4  0x000055555557b478 in lang_size_sections ()
    5  0x000055555557e65c in lang_relax_sections ()
    6  0x000055555559f9c8 in ldelf_map_segments ()
    7  0x000055555559783c in gldelf64loongarch_after_allocation ()
    8  0x000055555558dac0 in ldemul_after_allocation ()
    9  0x000055555557f6c0 in lang_process ()
    10 0x0000555555585314 in main ()

Diff:
---
 bfd/elfnn-loongarch.c                              |  4 +---
 ld/emultempl/loongarchelf.em                       | 16 ----------------
 ld/testsuite/ld-loongarch-elf/binary.ld            |  1 +
 ld/testsuite/ld-loongarch-elf/binary.s             |  4 ++++
 ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp | 12 ++++++++++++
 5 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index db6d419a052..71fa8fa6f01 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -157,9 +157,7 @@ loongarch_elf_new_section_hook (bfd *abfd, asection *sec)
 
 /* Get the LoongArch ELF linker hash table from a link_info structure.  */
 #define loongarch_elf_hash_table(p)					\
-  (elf_hash_table_id (elf_hash_table (p)) == LARCH_ELF_DATA		\
-   ? ((struct loongarch_elf_link_hash_table *) ((p)->hash))		\
-   : NULL)
+    ((struct loongarch_elf_link_hash_table *) ((p)->hash))		\
 
 #define MINUS_ONE ((bfd_vma) 0 - 1)
 
diff --git a/ld/emultempl/loongarchelf.em b/ld/emultempl/loongarchelf.em
index 2da40582008..8a1a898f9e7 100644
--- a/ld/emultempl/loongarchelf.em
+++ b/ld/emultempl/loongarchelf.em
@@ -102,23 +102,7 @@ gld${EMULATION_NAME}_after_allocation (void)
   ldelf_map_segments (need_layout);
 }
 
-/* This is a convenient point to tell BFD about target specific flags.
-   After the output has been created, but before inputs are read.  */
-
-static void
-larch_create_output_section_statements (void)
-{
-  /* See PR 22920 for an example of why this is necessary.  */
-  if (strstr (bfd_get_target (link_info.output_bfd), "loong") == NULL)
-    {
-      einfo (_("%F%P: error: cannot change output format"
-	       " whilst linking %s binaries\n"), "LoongArch");
-      return;
-    }
-}
-
 EOF
 
 LDEMUL_BEFORE_ALLOCATION=larch_elf_before_allocation
 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
-LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=larch_create_output_section_statements
diff --git a/ld/testsuite/ld-loongarch-elf/binary.ld b/ld/testsuite/ld-loongarch-elf/binary.ld
new file mode 100644
index 00000000000..73cd4f2c1fc
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/binary.ld
@@ -0,0 +1 @@
+OUTPUT_FORMAT(binary);
diff --git a/ld/testsuite/ld-loongarch-elf/binary.s b/ld/testsuite/ld-loongarch-elf/binary.s
new file mode 100644
index 00000000000..b0aeb62af1c
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/binary.s
@@ -0,0 +1,4 @@
+.text
+  ret
+.data
+  .4byte 0x12345678
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index fb34eeb80cb..16d89cd5a43 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -118,6 +118,18 @@ if [istarget "loongarch64-*-*"] {
 	      "abi1_max_imm" \
 	  ] \
       ]
+
+  run_ld_link_tests \
+      [list \
+	   [list \
+		"binary output format" \
+		"-T binary.ld" "" \
+		"" \
+		{binary.s} \
+		{} \
+		"a.binary" \
+	   ] \
+      ]
 }
 
 if [istarget "loongarch64-*-*"] {


More information about the Binutils-cvs mailing list