xref: /netbsd-src/external/gpl3/gcc/dist/libgcc/vtv_end_preinit.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
24d5abbe8Smrg 
34d5abbe8Smrg This file is part of GCC.
44d5abbe8Smrg 
54d5abbe8Smrg GCC is free software; you can redistribute it and/or modify it under
64d5abbe8Smrg the terms of the GNU General Public License as published by the Free
74d5abbe8Smrg Software Foundation; either version 3, or (at your option) any later
84d5abbe8Smrg version.
94d5abbe8Smrg 
104d5abbe8Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
114d5abbe8Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
124d5abbe8Smrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
134d5abbe8Smrg for more details.
144d5abbe8Smrg 
154d5abbe8Smrg Under Section 7 of GPL version 3, you are granted additional
164d5abbe8Smrg permissions described in the GCC Runtime Library Exception, version
174d5abbe8Smrg 3.1, as published by the Free Software Foundation.
184d5abbe8Smrg 
194d5abbe8Smrg You should have received a copy of the GNU General Public License and
204d5abbe8Smrg a copy of the GCC Runtime Library Exception along with this program;
214d5abbe8Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
224d5abbe8Smrg <http://www.gnu.org/licenses/>.  */
234d5abbe8Smrg 
244d5abbe8Smrg /* This file is part of the vtable verification feature (for a
254d5abbe8Smrg    detailed description of the feature, see comments in
264d5abbe8Smrg    vtable-verify.c).  The vtable verification feature creates
274d5abbe8Smrg    certain global symbols that need to be read-write sometimes during
284d5abbe8Smrg    program execution, and read-only at others.  It uses 'mprotect' to
294d5abbe8Smrg    change the memory protections of the pages on which these variables
304d5abbe8Smrg    are stored.  In order to not affect the protections of other
314d5abbe8Smrg    program variables, these variables are put into a special named
324d5abbe8Smrg    section, ".vtable_map_vars", which is page-aligned at the start,
334d5abbe8Smrg    and which is padded with a page-sized amount of zeros at the end.
344d5abbe8Smrg    To make this section page aligned, we create a special symbol,
354d5abbe8Smrg    "_vtable_map_vars_start" which we make the very first thing that
364d5abbe8Smrg    goes into the section.  That is defined in vtv_start.c (which
374d5abbe8Smrg    contains nothing else).  vtv_start.c gest compiled into
384d5abbe8Smrg    vtv_start.o, and vtv_start.o gets inserted into the link line
394d5abbe8Smrg    immediately after crtbegin.o, if the program is compiled with
404d5abbe8Smrg    -fvtable.verify.
414d5abbe8Smrg 
424d5abbe8Smrg    In order to pad the ".vtable_map_vars" section with a page-sized
434d5abbe8Smrg    amount of zeros at the end, there is a second symbol,
444d5abbe8Smrg    _vtable_map_vars_end.  This file defines that symbol (and only this
454d5abbe8Smrg    symbol).  This second symbol is a page-sized array of chars,
464d5abbe8Smrg    zero-filled, and is the very last thing to go into the section.
474d5abbe8Smrg    When the GCC driver inserts vtv_start.o into the link line (just
484d5abbe8Smrg    after crtbegin.o) it also inserts vtv_end.o into the link line,
494d5abbe8Smrg    just before crtend.o.  This has the desired effect of making our
504d5abbe8Smrg    section page-aligned and page-size paded, ensuring that no other
514d5abbe8Smrg    program data lands on our pages.  */
524d5abbe8Smrg 
534d5abbe8Smrg #include "vtv-change-permission.h"
544d5abbe8Smrg 
554d5abbe8Smrg void
__VLTProtectPreinit(void)564d5abbe8Smrg __VLTProtectPreinit (void)
574d5abbe8Smrg {
584d5abbe8Smrg   __VLTChangePermission (__VLTP_READ_ONLY);
594d5abbe8Smrg }
604d5abbe8Smrg 
614d5abbe8Smrg /* Page-sized variable to mark end of .vtable_map_vars section.  */
624d5abbe8Smrg char _vtable_map_vars_end[VTV_PAGE_SIZE]
634d5abbe8Smrg   __attribute__ ((__visibility__ ("protected"), used,
644d5abbe8Smrg 		  section(".vtable_map_vars")));
654d5abbe8Smrg 
664d5abbe8Smrg /* Put the function __VLTProtectPreinit into the .preinit_array
674d5abbe8Smrg    section.  */
684d5abbe8Smrg 
694d5abbe8Smrg __attribute__ ((section (".preinit_array")))
704d5abbe8Smrg     typeof (__VLTProtectPreinit) *__preinit_end = __VLTProtectPreinit;
71