]>
Commit | Line | Data |
---|---|---|
9f1d377b ILT |
1 | // relro_test.cc -- test -z relro for gold |
2 | ||
3 | // Copyright 2008 Free Software Foundation, Inc. | |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
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 <csignal> | |
25 | #include <stdint.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | // This code is put into a shared library linked with -z relro. | |
29 | ||
30 | // i1 and i2 are not relro variables. | |
31 | int i1 = 1; | |
32 | static int i2 = 2; | |
33 | ||
34 | // P1 is a global relro variable. | |
35 | int* const p1 = &i1; | |
36 | ||
37 | // P2 is a local relro variable. | |
38 | int* const p2 = &i2; | |
39 | ||
40 | // Test symbol addresses. | |
41 | ||
42 | bool | |
43 | t1() | |
44 | { | |
45 | void* i1addr = static_cast<void*>(&i1); | |
46 | void* i2addr = static_cast<void*>(&i2); | |
47 | const void* p1addr = static_cast<const void*>(&p1); | |
48 | const void* p2addr = static_cast<const void*>(&p2); | |
49 | ||
50 | // The relro variables should precede the non-relro variables in the | |
51 | // memory image. | |
52 | assert(i1addr > p1addr); | |
53 | assert(i1addr > p2addr); | |
54 | assert(i2addr > p1addr); | |
55 | assert(i2addr > p2addr); | |
56 | ||
57 | // The relro variables should not be on the same page as the | |
58 | // non-relro variables. | |
59 | const size_t page_size = getpagesize(); | |
60 | uintptr_t i1page = reinterpret_cast<uintptr_t>(i1addr) & ~ (page_size - 1); | |
61 | uintptr_t i2page = reinterpret_cast<uintptr_t>(i2addr) & ~ (page_size - 1); | |
62 | uintptr_t p1page = reinterpret_cast<uintptr_t>(p1addr) & ~ (page_size - 1); | |
63 | uintptr_t p2page = reinterpret_cast<uintptr_t>(p2addr) & ~ (page_size - 1); | |
64 | assert(i1page != p1page); | |
65 | assert(i1page != p2page); | |
66 | assert(i2page != p1page); | |
67 | assert(i2page != p2page); | |
68 | ||
69 | return true; | |
70 | } | |
71 | ||
72 | // A signal handler for SIGSEGV. | |
73 | ||
74 | extern "C" | |
75 | void | |
76 | sigsegv_handler(int) | |
77 | { | |
78 | throw 0; | |
79 | } | |
80 | ||
81 | // Use a separate function to throw the exception, so that we don't | |
82 | // need to use -fnon-call-exceptions. | |
83 | ||
84 | void f2() __attribute__ ((noinline)); | |
85 | void | |
86 | f2() | |
87 | { | |
88 | int** pp1 = const_cast<int**>(&p1); | |
89 | *pp1 = &i2; | |
90 | ||
91 | // We shouldn't get here--the assignment to *pp1 should write to | |
92 | // memory which the dynamic linker marked as read-only, giving us a | |
93 | // SIGSEGV, causing sigsegv_handler to be invoked, to throw past us. | |
94 | assert(0); | |
95 | } | |
96 | ||
97 | // Changing a relro variable should give us a SIGSEGV. | |
98 | ||
99 | bool | |
100 | t2() | |
101 | { | |
102 | signal(SIGSEGV, sigsegv_handler); | |
103 | ||
104 | try | |
105 | { | |
106 | f2(); | |
107 | return false; | |
108 | } | |
109 | catch (int i) | |
110 | { | |
111 | assert(i == 0); | |
112 | return true; | |
113 | } | |
114 | } |