• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GCC with patches for OS216


Commit MetaInfo

修訂be7c41a556464680b17a2c3d5d099ec56a2c223e (tree)
時間2020-07-02 05:56:16
作者Omar Tahir <omar.tahir@arm....>
CommiterRichard Sandiford

Log Message

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.

Change Summary

差異

--- a/gcc/config/aarch64/aarch64.c
+++ b/gcc/config/aarch64/aarch64.c
@@ -10833,40 +10833,26 @@ aarch64_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
1083310833 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
1083410834 }
1083510835
10836-
1083710836 static void
1083810837 aarch64_asm_trampoline_template (FILE *f)
1083910838 {
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");
1084910843
1085010844 if (TARGET_ILP32)
1085110845 {
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);
1085510848 }
1085610849 else
1085710850 {
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]);
1086110853 }
1086210854 asm_fprintf (f, "\tbr\t%s\n", reg_names [IP1_REGNUM]);
1086310855
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-
1087010856 assemble_aligned_integer (POINTER_BYTES, const0_rtx);
1087110857 assemble_aligned_integer (POINTER_BYTES, const0_rtx);
1087210858 }
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/bti-4.c
@@ -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" } } */