vtv_start_preinit.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 2012-2022 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. This file defines that symbol (and only
  31. that symbol). GCC compiles this file into vtv_start.o, and
  32. inserts vtv_start.o into the link line immediately after
  33. crtbegin.o, if the program is compiled with -fvtable.verify.
  34. In order to pad the ".vtable_map_vars" section with a page-sized
  35. amount of zeros at the end, there is a second symbol,
  36. _vtable_map_vars_end, which is defined in another file, vtv_end.c.
  37. This second symbol is a page-sized array of chars, zero-filled, and
  38. is the very last thing to go into the section. When the GCC driver
  39. inserts vtv_start.o into the link line (just after crtbegin.o) it
  40. also inserts vtv_end.o into the link line, just before crtend.o.
  41. This has the desired effect of making our section page-aligned and
  42. page-size paded, ensuring that no other program data lands on our
  43. pages. */
  44. #include "vtv-change-permission.h"
  45. void
  46. __VLTUnprotectPreinit (void)
  47. {
  48. __VLTChangePermission (__VLTP_READ_WRITE);
  49. }
  50. /* Page-aligned symbol to mark beginning of .vtable_map_vars section. */
  51. char _vtable_map_vars_start []
  52. __attribute__ ((__visibility__ ("protected"), used, aligned(VTV_PAGE_SIZE),
  53. section(".vtable_map_vars")))
  54. = { };
  55. /* Put the function __VLTUnprotectPreinit into the .preinit_array
  56. section. */
  57. __attribute__ ((section (".preinit_array")))
  58. typeof (__VLTUnprotectPreinit) *__preinit = __VLTUnprotectPreinit;