vtv_end_preinit.c 3.1 KB

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