[PATCH, ARM/ld]Linker should return with error if failed to merge arch attribute

Terry Guo flameroc@gmail.com
Mon Sep 15 02:29:00 GMT 2014


Hi there,

As shown by the case in patch, current linker continues to finish the
linking even after generating an error message about arch conflicting.
The trouble is that it returns code 0 which indicates a successful
run, despite the produced elf file contains illegal information.

Below is my screen paste to demonstrate this issue:

terguo01@terry-pc01:case$ arm-none-eabi-ld m.o f.o -e main -o test.elf
arm-none-eabi-ld: error: f.o: Conflicting CPU architectures 13/0
terguo01@terry-pc01:case$ echo $?
0

terguo01@terry-pc01:case$ arm-none-eabi-readelf -A test.elf
Attribute Section: aeabi
File Attributes
Segmentation fault (core dumped)

The attached patch intends to fix this issue. It is tested with
Binutils regression test and no regressions.

OK to commit?

BR,
Terry

bfd/ChangeLog:

2014-09-15  Terry Guo  <terry.guo@arm.com>

     * elf32-arm.c (elf32_arm_merge_eabi_attributes): Return false if
failed to merge.


ld/testsuite/ChangeLog:

2014-09-15  Terry Guo  <terry.guo@arm.com>

     * ld-arm/attr-merge-arch-2.d: New test case.
     * ld-arm/attr-merge-arch-2a.s: New test case source file.
     * ld-arm/attr-merge-arch-2b.s: Likewise.
     * ld-arm/arm-elf.exp: Run new test case.
-------------- next part --------------
diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index 89d51c1..d30df68 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -11800,10 +11800,17 @@ elf32_arm_merge_eabi_attributes (bfd *ibfd, bfd *obfd)
 	    /* Merge Tag_CPU_arch and Tag_also_compatible_with.  */
 	    secondary_compat = get_secondary_compatible_arch (ibfd);
 	    secondary_compat_out = get_secondary_compatible_arch (obfd);
-	    out_attr[i].i = tag_cpu_arch_combine (ibfd, out_attr[i].i,
+	    int arch_attr = tag_cpu_arch_combine (ibfd, out_attr[i].i,
 						  &secondary_compat_out,
 						  in_attr[i].i,
 						  secondary_compat);
+
+	    /* Return with error if failed to merge.  */
+	    if (arch_attr == -1)
+	      return FALSE;
+	    else
+	      out_attr[i].i = arch_attr;
+
 	    set_secondary_compatible_arch (obfd, secondary_compat_out);
 
 	    /* Merge Tag_CPU_name and Tag_CPU_raw_name.  */
diff --git a/ld/testsuite/ld-arm/arm-elf.exp b/ld/testsuite/ld-arm/arm-elf.exp
index f971afc..3c8cc68 100644
--- a/ld/testsuite/ld-arm/arm-elf.exp
+++ b/ld/testsuite/ld-arm/arm-elf.exp
@@ -878,6 +878,7 @@ run_dump_test "attr-merge-vfp-6r"
 run_dump_test "attr-merge-vfp-7"
 run_dump_test "attr-merge-vfp-7r"
 run_dump_test "attr-merge-incompatible"
+run_dump_test "attr-merge-arch-2"
 run_dump_test "unresolved-1"
 if { ![istarget "arm*-*-nacl*"] } {
     run_dump_test "unresolved-1-dyn"
diff --git a/ld/testsuite/ld-arm/attr-merge-arch-2.d b/ld/testsuite/ld-arm/attr-merge-arch-2.d
new file mode 100644
index 0000000..0e98edb
--- /dev/null
+++ b/ld/testsuite/ld-arm/attr-merge-arch-2.d
@@ -0,0 +1,5 @@
+#source: attr-merge-arch-2a.s
+#source: attr-merge-arch-2b.s
+#as:
+#ld: -e main
+#error: Conflicting CPU architectures 13/0
diff --git a/ld/testsuite/ld-arm/attr-merge-arch-2a.s b/ld/testsuite/ld-arm/attr-merge-arch-2a.s
new file mode 100644
index 0000000..6235a3e
--- /dev/null
+++ b/ld/testsuite/ld-arm/attr-merge-arch-2a.s
@@ -0,0 +1,18 @@
+        .syntax unified
+        .cpu cortex-m4
+        .fpu softvfp
+        .thumb
+        .file   "m.c"
+        .text
+        .align  2
+        .global main
+        .thumb
+        .thumb_func
+        .type   main, %function
+main:
+        push    {r7, lr}
+        add     r7, sp, #0
+        bl      foo
+        mov     r3, r0
+        mov     r0, r3
+        pop     {r7, pc}
diff --git a/ld/testsuite/ld-arm/attr-merge-arch-2b.s b/ld/testsuite/ld-arm/attr-merge-arch-2b.s
new file mode 100644
index 0000000..5771835
--- /dev/null
+++ b/ld/testsuite/ld-arm/attr-merge-arch-2b.s
@@ -0,0 +1,8 @@
+        .eabi_attribute 6, 0  @Tag_CPU_arch, 0 means pre-v4.
+        .file   "f.c"
+        .text
+        .align  2
+        .global foo
+        .type   foo, %function
+foo:
+        bx      lr


More information about the Binutils mailing list