GCC with patches for OS216
修訂 | be7c41a556464680b17a2c3d5d099ec56a2c223e (tree) |
---|---|
時間 | 2020-07-02 05:56:16 |
作者 | Omar Tahir <omar.tahir@arm....> |
Commiter | Richard Sandiford |
aarch64: Fix missing BTI instruction in trampolines
If two functions require trampolines, and the first has BTI enabled
while the second doesn't, the generated template will be lacking
a BTI instruction. This patch fixes this by always adding a BTI
instruction, which is safe as BTI instructions are ignored on
unsupported architecture versions.
2020-07-01 Omar Tahir <omar.tahir@arm.com>
gcc/
* config/aarch64/aarch64.c (aarch64_asm_trampoline_template): Always
generate a BTI instruction.
gcc/testsuite/
* gcc.target/aarch64/bti-4.c: New test.
@@ -10833,40 +10833,26 @@ aarch64_return_addr (int count, rtx frame ATTRIBUTE_UNUSED) | ||
10833 | 10833 | return get_hard_reg_initial_val (Pmode, LR_REGNUM); |
10834 | 10834 | } |
10835 | 10835 | |
10836 | - | |
10837 | 10836 | static void |
10838 | 10837 | aarch64_asm_trampoline_template (FILE *f) |
10839 | 10838 | { |
10840 | - int offset1 = 16; | |
10841 | - int offset2 = 20; | |
10842 | - | |
10843 | - if (aarch64_bti_enabled ()) | |
10844 | - { | |
10845 | - asm_fprintf (f, "\thint\t34 // bti c\n"); | |
10846 | - offset1 -= 4; | |
10847 | - offset2 -= 4; | |
10848 | - } | |
10839 | + /* Even if the current function doesn't have branch protection, some | |
10840 | + later function might, so since this template is only generated once | |
10841 | + we have to add a BTI just in case. */ | |
10842 | + asm_fprintf (f, "\thint\t34 // bti c\n"); | |
10849 | 10843 | |
10850 | 10844 | if (TARGET_ILP32) |
10851 | 10845 | { |
10852 | - asm_fprintf (f, "\tldr\tw%d, .+%d\n", IP1_REGNUM - R0_REGNUM, offset1); | |
10853 | - asm_fprintf (f, "\tldr\tw%d, .+%d\n", STATIC_CHAIN_REGNUM - R0_REGNUM, | |
10854 | - offset1); | |
10846 | + asm_fprintf (f, "\tldr\tw%d, .+12\n", IP1_REGNUM - R0_REGNUM); | |
10847 | + asm_fprintf (f, "\tldr\tw%d, .+12\n", STATIC_CHAIN_REGNUM - R0_REGNUM); | |
10855 | 10848 | } |
10856 | 10849 | else |
10857 | 10850 | { |
10858 | - asm_fprintf (f, "\tldr\t%s, .+%d\n", reg_names [IP1_REGNUM], offset1); | |
10859 | - asm_fprintf (f, "\tldr\t%s, .+%d\n", reg_names [STATIC_CHAIN_REGNUM], | |
10860 | - offset2); | |
10851 | + asm_fprintf (f, "\tldr\t%s, .+12\n", reg_names [IP1_REGNUM]); | |
10852 | + asm_fprintf (f, "\tldr\t%s, .+16\n", reg_names [STATIC_CHAIN_REGNUM]); | |
10861 | 10853 | } |
10862 | 10854 | asm_fprintf (f, "\tbr\t%s\n", reg_names [IP1_REGNUM]); |
10863 | 10855 | |
10864 | - /* The trampoline needs an extra padding instruction. In case if BTI is | |
10865 | - enabled the padding instruction is replaced by the BTI instruction at | |
10866 | - the beginning. */ | |
10867 | - if (!aarch64_bti_enabled ()) | |
10868 | - assemble_aligned_integer (4, const0_rtx); | |
10869 | - | |
10870 | 10856 | assemble_aligned_integer (POINTER_BYTES, const0_rtx); |
10871 | 10857 | assemble_aligned_integer (POINTER_BYTES, const0_rtx); |
10872 | 10858 | } |
@@ -0,0 +1,60 @@ | ||
1 | +/* { dg-do compile } */ | |
2 | +/* If configured with --enable-standard-branch-protection, disable it. */ | |
3 | +/* { dg-additional-options "-mbranch-protection=none" { target { default_branch_protection } } } */ | |
4 | + | |
5 | +void f1 (void *); | |
6 | +void f2 (void *); | |
7 | +void f3 (void *, void (*)(void *)); | |
8 | + | |
9 | +int | |
10 | +retbr_trampolines (void *a, int b) | |
11 | +{ | |
12 | + if (!b) | |
13 | + { | |
14 | + f1 (a); | |
15 | + return 1; | |
16 | + } | |
17 | + if (b) | |
18 | + { | |
19 | + /* Suppress "ISO C forbids nested functions" warning. */ | |
20 | + _Pragma("GCC diagnostic push") | |
21 | + _Pragma("GCC diagnostic ignored \"-Wpedantic\"") | |
22 | + void retbr_tramp_internal (void *c) | |
23 | + { | |
24 | + _Pragma("GCC diagnostic pop") | |
25 | + if (c == a) | |
26 | + f2 (c); | |
27 | + } | |
28 | + f3 (a, retbr_tramp_internal); | |
29 | + } | |
30 | + return 0; | |
31 | +} | |
32 | + | |
33 | +__attribute__((target("branch-protection=bti,arch=armv8.3-a"))) | |
34 | +int | |
35 | +retbr_trampolines2 (void *a, int b) | |
36 | +{ | |
37 | + if (!b) | |
38 | + { | |
39 | + f1 (a); | |
40 | + return 1; | |
41 | + } | |
42 | + if (b) | |
43 | + { | |
44 | + /* Suppress "ISO C forbids nested functions" warning. */ | |
45 | + _Pragma("GCC diagnostic push") | |
46 | + _Pragma("GCC diagnostic ignored \"-Wpedantic\"") | |
47 | + __attribute__((target("branch-protection=bti,arch=armv8.3-a"))) | |
48 | + void retbr_tramp_internal2 (void *c) | |
49 | + { | |
50 | + _Pragma("GCC diagnostic pop") | |
51 | + if (c == a) | |
52 | + f2 (c); | |
53 | + } | |
54 | + f3 (a, retbr_tramp_internal2); | |
55 | + } | |
56 | + return 0; | |
57 | +} | |
58 | + | |
59 | +/* Trampoline should have BTI C. */ | |
60 | +/* { dg-final { scan-assembler "\.LTRAMP0:\n\thint\t34" } } */ |