diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2015-08-28 19:14:49 -0700 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2016-04-22 15:35:22 -0700 |
commit | 13c0312b6922320d1b30f2ac5f3d1bcffbeb9ca0 (patch) | |
tree | a6797a99651c527fce38f748e5a925a50e354741 | |
parent | c268ae83811503630dec543329ab4c2762d3916d (diff) | |
download | gcc-13c0312b6922320d1b30f2ac5f3d1bcffbeb9ca0.tar.gz |
Load external function address via GOT slot
For -fno-plt, we load the external function address via the GOT slot
so that linker won't create an PLT entry for extern function address.
gcc/
PR target/pr67400
* config/i386/i386-protos.h (ix86_force_load_from_GOT_p): New.
* config/i386/i386.c (ix86_force_load_from_GOT_p): New function.
(ix86_legitimate_address_p): Allow UNSPEC_GOTPCREL if
ix86_force_load_from_GOT_p returns true.
(ix86_print_operand_address): Support UNSPEC_GOTPCREL if
ix86_force_load_from_GOT_p returns true.
(ix86_expand_move): Load the external function address via the
GOT slot if ix86_force_load_from_GOT_p returns true.
* config/i386/predicates.md (x86_64_immediate_operand): Return
false if ix86_force_load_from_GOT_p returns true.
gcc/testsuite/
PR target/pr67400
* gcc.target/i386/pr67400-1.c: New test.
* gcc.target/i386/pr67400-2.c: Likewise.
* gcc.target/i386/pr67400-3.c: Likewise.
* gcc.target/i386/pr67400-4.c: Likewise.
-rw-r--r-- | gcc/config/i386/i386-protos.h | 1 | ||||
-rw-r--r-- | gcc/config/i386/i386.c | 42 | ||||
-rw-r--r-- | gcc/config/i386/predicates.md | 4 | ||||
-rw-r--r-- | gcc/testsuite/gcc.target/i386/pr67400-1.c | 13 | ||||
-rw-r--r-- | gcc/testsuite/gcc.target/i386/pr67400-2.c | 14 | ||||
-rw-r--r-- | gcc/testsuite/gcc.target/i386/pr67400-3.c | 16 | ||||
-rw-r--r-- | gcc/testsuite/gcc.target/i386/pr67400-4.c | 13 |
7 files changed, 103 insertions, 0 deletions
diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h index ff47bc15600..42c941d691b 100644 --- a/gcc/config/i386/i386-protos.h +++ b/gcc/config/i386/i386-protos.h @@ -69,6 +69,7 @@ extern bool ix86_expand_set_or_movmem (rtx, rtx, rtx, rtx, rtx, rtx, extern bool constant_address_p (rtx); extern bool legitimate_pic_operand_p (rtx); extern bool legitimate_pic_address_disp_p (rtx); +extern bool ix86_force_load_from_GOT_p (rtx); extern void print_reg (rtx, int, FILE*); extern void ix86_print_operand (FILE *, rtx, int); diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 6379313ef38..793b142d376 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -14443,6 +14443,24 @@ ix86_legitimate_constant_p (machine_mode, rtx x) return true; } +/* True if operand X should be loaded from GOT. */ + +bool +ix86_force_load_from_GOT_p (rtx x) +{ + /* External function symbol should be loaded via the GOT slot for + -fno-plt. */ + return (!flag_plt + && !flag_pic + && ix86_cmodel != CM_LARGE + && TARGET_64BIT + && !TARGET_PECOFF + && !TARGET_MACHO + && GET_CODE (x) == SYMBOL_REF + && SYMBOL_REF_FUNCTION_P (x) + && !SYMBOL_REF_LOCAL_P (x)); +} + /* Determine if it's legal to put X into the constant pool. This is not possible for the address of thread-local symbols, which is checked above. */ @@ -14823,6 +14841,10 @@ ix86_legitimate_address_p (machine_mode, rtx addr, bool strict) return false; case UNSPEC_GOTPCREL: + gcc_assert (flag_pic + || ix86_force_load_from_GOT_p (XVECEXP (XEXP (disp, 0), 0, 0))); + goto is_legitimate_pic; + case UNSPEC_PCREL: gcc_assert (flag_pic); goto is_legitimate_pic; @@ -17427,6 +17449,11 @@ ix86_print_operand_address_as (FILE *file, rtx addr, } else if (flag_pic) output_pic_addr_const (file, disp, 0); + else if (GET_CODE (disp) == CONST + && GET_CODE (XEXP (disp, 0)) == UNSPEC + && XINT (XEXP (disp, 0), 1) == UNSPEC_GOTPCREL + && ix86_force_load_from_GOT_p (XVECEXP (XEXP (disp, 0), 0, 0))) + output_pic_addr_const (file, XEXP (disp, 0), code); else output_addr_const (file, disp); } @@ -18692,6 +18719,21 @@ ix86_expand_move (machine_mode mode, rtx operands[]) op1 = convert_to_mode (mode, op1, 1); } } + } + else if (ix86_force_load_from_GOT_p (op1)) + { + /* Load the external function address via the GOT slot to + avoid PLT. */ + op1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op1), + (TARGET_64BIT + ? UNSPEC_GOTPCREL + : UNSPEC_GOT)); + op1 = gen_rtx_CONST (Pmode, op1); + op1 = gen_const_mem (Pmode, op1); + op1 = convert_to_mode (mode, op1, 1); + /* Force OP1 into register to prevent cse and fwprop from + replacing a GOT load with a constant. */ + op1 = force_reg (mode, op1); } else { diff --git a/gcc/config/i386/predicates.md b/gcc/config/i386/predicates.md index 93dda7bb0e7..e6e8638e165 100644 --- a/gcc/config/i386/predicates.md +++ b/gcc/config/i386/predicates.md @@ -159,6 +159,10 @@ /* TLS symbols are not constant. */ if (SYMBOL_REF_TLS_MODEL (op)) return false; + /* Load the external function address via the GOT slot to avoid + PLT. */ + if (ix86_force_load_from_GOT_p (op)) + return false; return (ix86_cmodel == CM_SMALL || ix86_cmodel == CM_KERNEL || (ix86_cmodel == CM_MEDIUM && !SYMBOL_REF_FAR_ADDR_P (op))); diff --git a/gcc/testsuite/gcc.target/i386/pr67400-1.c b/gcc/testsuite/gcc.target/i386/pr67400-1.c new file mode 100644 index 00000000000..a875b76324e --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr67400-1.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target *-*-linux* } } */ +/* { dg-options "-O2 -fno-pic -fno-plt" } */ + +extern void bar (void); + +void * +foo (void) +{ + return &bar; +} + +/* { dg-final { scan-assembler "mov\(l|q\)\[ \t\]*bar@GOTPCREL" { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-not "mov\(l|q\)\[ \t\]*\\\$bar," { target { ! ia32 } } } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr67400-2.c b/gcc/testsuite/gcc.target/i386/pr67400-2.c new file mode 100644 index 00000000000..9f3f4bc9b36 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr67400-2.c @@ -0,0 +1,14 @@ +/* { dg-do compile { target *-*-linux* } } */ +/* { dg-options "-O2 -fno-pic -fno-plt" } */ + +extern void bar (void); +extern void *p; + +void +foo (void) +{ + p = &bar; +} + +/* { dg-final { scan-assembler "mov\(l|q\)\[ \t\]*bar@GOTPCREL" { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-not "mov\(l|q\)\[ \t\]*\\\$bar," { target { ! ia32 } } } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr67400-3.c b/gcc/testsuite/gcc.target/i386/pr67400-3.c new file mode 100644 index 00000000000..045974e2e61 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr67400-3.c @@ -0,0 +1,16 @@ +/* { dg-do compile { target *-*-linux* } } */ +/* { dg-options "-O2 -fno-pic -fno-plt" } */ + +static void +bar (void) +{ +} + +void * +foo (void) +{ + return &bar; +} + +/* { dg-final { scan-assembler "mov\(l|q\)\[ \t\]*\\\$bar," } } */ +/* { dg-final { scan-assembler-not "mov\(l|q\)\[ \t\]*bar@GOTPCREL" { target { ! ia32 } } } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr67400-4.c b/gcc/testsuite/gcc.target/i386/pr67400-4.c new file mode 100644 index 00000000000..fd373dbdf7f --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr67400-4.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target *-*-linux* } } */ +/* { dg-options "-O2 -fno-pic -fno-plt" } */ + +extern void bar (void) __attribute__ ((visibility ("hidden"))); + +void * +foo (void) +{ + return &bar; +} + +/* { dg-final { scan-assembler "mov\(l|q\)\[ \t\]*\\\$bar," } } */ +/* { dg-final { scan-assembler-not "mov\(l|q\)\[ \t\]*bar@GOTPCREL" { target { ! ia32 } } } } */ |