xref: /netbsd-src/external/gpl3/gcc.old/dist/libgcc/vtv_start_preinit.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1*8feb0f0bSmrg /* Copyright (C) 2012-2020 Free Software Foundation, Inc.
21debfc3dSmrg 
31debfc3dSmrg This file is part of GCC.
41debfc3dSmrg 
51debfc3dSmrg GCC is free software; you can redistribute it and/or modify it under
61debfc3dSmrg the terms of the GNU General Public License as published by the Free
71debfc3dSmrg Software Foundation; either version 3, or (at your option) any later
81debfc3dSmrg version.
91debfc3dSmrg 
101debfc3dSmrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
111debfc3dSmrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
121debfc3dSmrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
131debfc3dSmrg for more details.
141debfc3dSmrg 
151debfc3dSmrg Under Section 7 of GPL version 3, you are granted additional
161debfc3dSmrg permissions described in the GCC Runtime Library Exception, version
171debfc3dSmrg 3.1, as published by the Free Software Foundation.
181debfc3dSmrg 
191debfc3dSmrg You should have received a copy of the GNU General Public License and
201debfc3dSmrg a copy of the GCC Runtime Library Exception along with this program;
211debfc3dSmrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
221debfc3dSmrg <http://www.gnu.org/licenses/>.  */
231debfc3dSmrg 
241debfc3dSmrg /* This file is part of the vtable verification feature (for a
251debfc3dSmrg    detailed description of the feature, see comments in
261debfc3dSmrg    vtable-verify.c).  The vtable verification feature creates
271debfc3dSmrg    certain global symbols that need to be read-write sometimes during
281debfc3dSmrg    program execution, and read-only at others.  It uses 'mprotect' to
291debfc3dSmrg    change the memory protections of the pages on which these variables
301debfc3dSmrg    are stored.  In order to not affect the protections of other
311debfc3dSmrg    program variables, these variables are put into a special named
321debfc3dSmrg    section, ".vtable_map_vars", which is page-aligned at the start,
331debfc3dSmrg    and which is padded with a page-sized amount of zeros at the end.
341debfc3dSmrg    To make this section page aligned, we create a special symbol,
351debfc3dSmrg    "_vtable_map_vars_start" which we make the very first thing that
361debfc3dSmrg    goes into the section.  This file defines that symbol (and only
371debfc3dSmrg    that symbol).  GCC compiles this file into vtv_start.o, and
381debfc3dSmrg    inserts vtv_start.o into the link line immediately after
391debfc3dSmrg    crtbegin.o, if the program is compiled with -fvtable.verify.
401debfc3dSmrg 
411debfc3dSmrg    In order to pad the ".vtable_map_vars" section with a page-sized
421debfc3dSmrg    amount of zeros at the end, there is a second symbol,
431debfc3dSmrg    _vtable_map_vars_end, which is defined in another file, vtv_end.c.
441debfc3dSmrg    This second symbol is a page-sized array of chars, zero-filled, and
451debfc3dSmrg    is the very last thing to go into the section.  When the GCC driver
461debfc3dSmrg    inserts vtv_start.o into the link line (just after crtbegin.o) it
471debfc3dSmrg    also inserts vtv_end.o into the link line, just before crtend.o.
481debfc3dSmrg    This has the desired effect of making our section page-aligned and
491debfc3dSmrg    page-size paded, ensuring that no other program data lands on our
501debfc3dSmrg    pages.  */
511debfc3dSmrg 
521debfc3dSmrg #include "vtv-change-permission.h"
531debfc3dSmrg 
541debfc3dSmrg void
__VLTUnprotectPreinit(void)551debfc3dSmrg __VLTUnprotectPreinit (void)
561debfc3dSmrg {
571debfc3dSmrg   __VLTChangePermission (__VLTP_READ_WRITE);
581debfc3dSmrg }
591debfc3dSmrg 
601debfc3dSmrg /* Page-aligned symbol to mark beginning of .vtable_map_vars section.  */
611debfc3dSmrg char _vtable_map_vars_start []
621debfc3dSmrg __attribute__ ((__visibility__ ("protected"), used, aligned(VTV_PAGE_SIZE),
631debfc3dSmrg 		section(".vtable_map_vars")))
641debfc3dSmrg   = { };
651debfc3dSmrg 
661debfc3dSmrg 
671debfc3dSmrg /* Put the function __VLTUnprotectPreinit into the .preinit_array
681debfc3dSmrg    section.  */
691debfc3dSmrg 
701debfc3dSmrg __attribute__ ((section (".preinit_array")))
711debfc3dSmrg     typeof (__VLTUnprotectPreinit) *__preinit = __VLTUnprotectPreinit;
721debfc3dSmrg 
73