binutils 组的一个讨论
https://sourceware.org/ml/binutils/2009-04/msg00426.html
Detect ARM MOVW/MOVT relocations in shared library links
- From: "Joseph S. Myers" <joseph at codesourcery dot com>
- To: binutils at sourceware dot org
- Date: Mon, 27 Apr 2009 22:06:39 +0000 (UTC)
- Subject: Detect ARM MOVW/MOVT relocations in shared library links
ARMv7 has MOVW and MOVT instructions for loading constants without needing a constant pool, and associated relocations for when those constants are symbol addresses. Of course PIC code can't use these instructions to load absolute symbol addresses. This patch adds linker checks for linking objects using the relevant relocations into shared libraries, to detect the common mistake (for people coming from x86) of building objects without -fPIC and linking them into a shared library; the previous behavior was silently dropping the relocations and so generating a broken shared library, which is obviously undesirable. Tested with no regressions with cross to arm-none-eabi. OK to commit? This certainly does not fix every case of ARM relocations that are either always unsafe for objects linked into shared libraries, or unsafe for such objects if the symbol referenced might not be resolved within the library at runtime; there are many other cases that may have similar issues, but these are the ones observed to cause problems in practice. ("Unsafe" means not fully resolved at static link time and not converted to a dynamic relocation or no suitable dynamic relocation available to which to convert the relocation.) bfd: 2009-04-27 Joseph Myers <joseph@codesourcery.com> * elf32-arm.c (elf32_arm_check_relocs): Give errors for absolute MOVW and MOVT relocations in a shared library link ld/testsuite: 2009-04-27 Joseph Myers <joseph@codesourcery.com> * ld-arm/movw-shared-1.d, ld-arm/movw-shared-1.s, ld-arm/movw-shared-2.d, ld-arm/movw-shared-2.s, ld-arm/movw-shared-3.d, ld-arm/movw-shared-3.s, ld-arm/movw-shared-4.d, ld-arm/movw-shared-4.s: New tests. * ld-arm/arm-elf.exp: Run these new tests. Index: bfd/elf32-arm.c =================================================================== RCS file: /cvs/src/src/bfd/elf32-arm.c,v retrieving revision 1.189 diff -u -r1.189 elf32-arm.c --- bfd/elf32-arm.c 22 Apr 2009 14:01:30 -0000 1.189 +++ bfd/elf32-arm.c 27 Apr 2009 21:45:11 -0000 @@ -9808,16 +9808,27 @@ needs_plt = 1; goto normal_reloc; + case R_ARM_MOVW_ABS_NC: + case R_ARM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + case R_ARM_THM_MOVT_ABS: + if (info->shared) + { + (*_bfd_error_handler) + (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), + abfd, elf32_arm_howto_table_1[r_type].name, + (h) ? h->root.root.string : "a local symbol"); + bfd_set_error (bfd_error_bad_value); + return FALSE; + } + + /* Fall through. */ case R_ARM_ABS32: case R_ARM_ABS32_NOI: case R_ARM_REL32: case R_ARM_REL32_NOI: - case R_ARM_MOVW_ABS_NC: - case R_ARM_MOVT_ABS: case R_ARM_MOVW_PREL_NC: case R_ARM_MOVT_PREL: - case R_ARM_THM_MOVW_ABS_NC: - case R_ARM_THM_MOVT_ABS: case R_ARM_THM_MOVW_PREL_NC: case R_ARM_THM_MOVT_PREL: needs_plt = 0; Index: ld/testsuite/ld-arm/arm-elf.exp =================================================================== RCS file: /cvs/src/src/ld/testsuite/ld-arm/arm-elf.exp,v retrieving revision 1.54 diff -u -r1.54 arm-elf.exp --- ld/testsuite/ld-arm/arm-elf.exp 17 Apr 2009 13:04:41 -0000 1.54 +++ ld/testsuite/ld-arm/arm-elf.exp 27 Apr 2009 21:45:11 -0000 @@ -187,6 +187,10 @@ run_dump_test "thumb2-bl-undefweak" run_dump_test "thumb2-bl-undefweak1" run_dump_test "emit-relocs1" +run_dump_test "movw-shared-1" +run_dump_test "movw-shared-2" +run_dump_test "movw-shared-3" +run_dump_test "movw-shared-4" # Exclude non-ARM-EABI targets. Index: ld/testsuite/ld-arm/movw-shared-1.d =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-1.d diff -N ld/testsuite/ld-arm/movw-shared-1.d --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-1.d 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,4 @@ +#name: MOVW/MOVT shared libraries test 1 +#source: movw-shared-1.s +#ld: -shared +#error: .*: relocation R_ARM_MOVW_ABS_NC against `a' can not be used when making a shared object; recompile with -fPIC Index: ld/testsuite/ld-arm/movw-shared-1.s =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-1.s diff -N ld/testsuite/ld-arm/movw-shared-1.s --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-1.s 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,5 @@ +.arch armv7-a +.text +.syntax unified +f: +movw r0, #:lower16:a Index: ld/testsuite/ld-arm/movw-shared-2.d =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-2.d diff -N ld/testsuite/ld-arm/movw-shared-2.d --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-2.d 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,4 @@ +#name: MOVW/MOVT shared libraries test 2 +#source: movw-shared-2.s +#ld: -shared +#error: .*: relocation R_ARM_MOVT_ABS against `b' can not be used when making a shared object; recompile with -fPIC Index: ld/testsuite/ld-arm/movw-shared-2.s =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-2.s diff -N ld/testsuite/ld-arm/movw-shared-2.s --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-2.s 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,5 @@ +.arch armv7-a +.text +.syntax unified +f: +movt r0, #:upper16:b Index: ld/testsuite/ld-arm/movw-shared-3.d =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-3.d diff -N ld/testsuite/ld-arm/movw-shared-3.d --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-3.d 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,4 @@ +#name: MOVW/MOVT shared libraries test 3 +#source: movw-shared-3.s +#ld: -shared +#error: .*: relocation R_ARM_THM_MOVW_ABS_NC against `c' can not be used when making a shared object; recompile with -fPIC Index: ld/testsuite/ld-arm/movw-shared-3.s =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-3.s diff -N ld/testsuite/ld-arm/movw-shared-3.s --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-3.s 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,6 @@ +.arch armv7-a +.text +.syntax unified +f: +.thumb +movw r0, #:lower16:c Index: ld/testsuite/ld-arm/movw-shared-4.d =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-4.d diff -N ld/testsuite/ld-arm/movw-shared-4.d --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-4.d 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,4 @@ +#name: MOVW/MOVT shared libraries test 4 +#source: movw-shared-4.s +#ld: -shared +#error: .*: relocation R_ARM_THM_MOVT_ABS against `d' can not be used when making a shared object; recompile with -fPIC Index: ld/testsuite/ld-arm/movw-shared-4.s =================================================================== RCS file: ld/testsuite/ld-arm/movw-shared-4.s diff -N ld/testsuite/ld-arm/movw-shared-4.s --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ld/testsuite/ld-arm/movw-shared-4.s 27 Apr 2009 21:45:11 -0000 @@ -0,0 +1,6 @@ +.arch armv7-a +.text +.syntax unified +f: +.thumb +movt r0, #:upper16:d -- Joseph S. Myers joseph@codesourcery.com