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