]>
Commit | Line | Data |
---|---|---|
b733bcb7 CC |
1 | // copy_test_relro.cc -- test copy relocs against read-only and relro symbols. |
2 | ||
b3adc24a | 3 | // Copyright (C) 2016-2020 Free Software Foundation, Inc. |
b733bcb7 CC |
4 | // Written by Cary Coutant <[email protected]>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #include <cassert> | |
24 | #include <stdint.h> | |
f7fd19e2 AM |
25 | #include <signal.h> |
26 | #include <setjmp.h> | |
b733bcb7 CC |
27 | |
28 | extern int* const p; | |
29 | extern const int b[]; | |
f7fd19e2 AM |
30 | extern const int* const q; |
31 | extern const int c; | |
b733bcb7 CC |
32 | int a = 123; |
33 | ||
f7fd19e2 AM |
34 | extern const int* const cp __attribute__ ((section (".rodata"))) = &c; |
35 | extern const int* const* const qp __attribute__ ((section (".rodata"))) = &q; | |
36 | ||
37 | volatile int segfaults = 0; | |
38 | sigjmp_buf jmp; | |
39 | ||
40 | void segv(int) | |
41 | { | |
42 | ++segfaults; | |
43 | siglongjmp(jmp, 1); | |
44 | } | |
45 | ||
b733bcb7 CC |
46 | int main() |
47 | { | |
48 | assert(*p == 123); | |
49 | assert(b[0] == 100); | |
50 | assert(b[1] == 200); | |
51 | assert(b[2] == 300); | |
52 | assert(b[3] == 400); | |
f7fd19e2 AM |
53 | assert(c == 500); |
54 | ||
55 | struct sigaction act; | |
56 | act.sa_handler = segv; | |
57 | sigemptyset(&act.sa_mask); | |
58 | act.sa_flags = 0; | |
59 | sigaction(SIGSEGV, &act, 0); | |
60 | ||
61 | assert(segfaults == 0); | |
62 | if (sigsetjmp(jmp, 1) == 0) | |
63 | *const_cast<const int **>(&p) = &c; | |
64 | assert(segfaults == 1); | |
65 | if (sigsetjmp(jmp, 1) == 0) | |
66 | *const_cast<int *>(b) = 99; | |
67 | assert(segfaults == 2); | |
68 | if (sigsetjmp(jmp, 1) == 0) | |
69 | *const_cast<int *>(cp) = c - 1; | |
70 | assert(segfaults == 3); | |
71 | if (sigsetjmp(jmp, 1) == 0) | |
72 | *const_cast<int **>(qp) = &a; | |
73 | assert(segfaults == 4); | |
74 | ||
b733bcb7 CC |
75 | return 0; |
76 | } |