2013-11-15 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libgcc / vtv_end_preinit.c
blob53d12b07ea1b7ddb69a318881f48b875a497fc59
1 /* Copyright (C) 2012, 2013
2 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This file is part of the vtable verification feature (for a
26 detailed description of the feature, see comments in
27 vtable-verify.c). The vtable verification feature creates
28 certain global symbols that need to be read-write sometimes during
29 program execution, and read-only at others. It uses 'mprotect' to
30 change the memory protections of the pages on which these variables
31 are stored. In order to not affect the protections of other
32 program variables, these variables are put into a special named
33 section, ".vtable_map_vars", which is page-aligned at the start,
34 and which is padded with a page-sized amount of zeros at the end.
35 To make this section page aligned, we create a special symbol,
36 "_vtable_map_vars_start" which we make the very first thing that
37 goes into the section. That is defined in vtv_start.c (which
38 contains nothing else). vtv_start.c gest compiled into
39 vtv_start.o, and vtv_start.o gets inserted into the link line
40 immediately after crtbegin.o, if the program is compiled with
41 -fvtable.verify.
43 In order to pad the ".vtable_map_vars" section with a page-sized
44 amount of zeros at the end, there is a second symbol,
45 _vtable_map_vars_end. This file defines that symbol (and only this
46 symbol). This second symbol is a page-sized array of chars,
47 zero-filled, and is the very last thing to go into the section.
48 When the GCC driver inserts vtv_start.o into the link line (just
49 after crtbegin.o) it also inserts vtv_end.o into the link line,
50 just before crtend.o. This has the desired effect of making our
51 section page-aligned and page-size paded, ensuring that no other
52 program data lands on our pages. */
54 #include "vtv-change-permission.h"
56 void
57 __VLTProtectPreinit (void)
59 __VLTChangePermission (__VLTP_READ_ONLY);
62 /* Page-sized variable to mark end of .vtable_map_vars section. */
63 char _vtable_map_vars_end[VTV_PAGE_SIZE]
64 __attribute__ ((__visibility__ ("protected"), used,
65 section(".vtable_map_vars")));
67 /* Put the function __VLTProtectPreinit into the .preinit_array
68 section. */
70 __attribute__ ((section (".preinit_array")))
71 typeof (__VLTProtectPreinit) *__preinit_end = __VLTProtectPreinit;