patch-gcc_targhooks_c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. $OpenBSD: patch-gcc_targhooks_c,v 1.3 2013/06/07 07:54:23 pascal Exp $
  2. --- gcc/targhooks.c.orig Fri Aug 10 15:26:44 2012
  3. +++ gcc/targhooks.c Sat Apr 13 16:20:32 2013
  4. @@ -56,6 +56,7 @@ along with GCC; see the file COPYING3. If not see
  5. #include "tree.h"
  6. #include "expr.h"
  7. #include "output.h"
  8. +#include "c-family/c-common.h"
  9. #include "diagnostic-core.h"
  10. #include "function.h"
  11. #include "target.h"
  12. @@ -658,7 +659,7 @@ default_stack_protect_guard (void)
  13. rtx x;
  14. t = build_decl (UNKNOWN_LOCATION,
  15. - VAR_DECL, get_identifier ("__stack_chk_guard"),
  16. + VAR_DECL, get_identifier ("__guard_local"),
  17. ptr_type_node);
  18. TREE_STATIC (t) = 1;
  19. TREE_PUBLIC (t) = 1;
  20. @@ -667,6 +668,8 @@ default_stack_protect_guard (void)
  21. TREE_THIS_VOLATILE (t) = 1;
  22. DECL_ARTIFICIAL (t) = 1;
  23. DECL_IGNORED_P (t) = 1;
  24. + DECL_VISIBILITY (t) = VISIBILITY_HIDDEN;
  25. + DECL_VISIBILITY_SPECIFIED (t) = 1;
  26. /* Do not share RTL as the declaration is visible outside of
  27. current function. */
  28. @@ -679,67 +682,68 @@ default_stack_protect_guard (void)
  29. return t;
  30. }
  31. -static GTY(()) tree stack_chk_fail_decl;
  32. +static GTY(()) int stack_protect_labelno;
  33. tree
  34. default_external_stack_protect_fail (void)
  35. {
  36. - tree t = stack_chk_fail_decl;
  37. + tree t, func, type, init, stack_smash_handler;
  38. + const char *name;
  39. + size_t length;
  40. + char name_buf[32];
  41. - if (t == NULL_TREE)
  42. - {
  43. - t = build_function_type_list (void_type_node, NULL_TREE);
  44. - t = build_decl (UNKNOWN_LOCATION,
  45. - FUNCTION_DECL, get_identifier ("__stack_chk_fail"), t);
  46. - TREE_STATIC (t) = 1;
  47. - TREE_PUBLIC (t) = 1;
  48. - DECL_EXTERNAL (t) = 1;
  49. - TREE_USED (t) = 1;
  50. - TREE_THIS_VOLATILE (t) = 1;
  51. - TREE_NOTHROW (t) = 1;
  52. - DECL_ARTIFICIAL (t) = 1;
  53. - DECL_IGNORED_P (t) = 1;
  54. - DECL_VISIBILITY (t) = VISIBILITY_DEFAULT;
  55. - DECL_VISIBILITY_SPECIFIED (t) = 1;
  56. + if (NULL == (name = fname_as_string (0))) {
  57. + name = (char *)xmalloc(32);
  58. + strlcpy (name, "*unknown*", 32);
  59. + }
  60. + length = strlen (name);
  61. + /* Build a decl for __func__. */
  62. + type = build_array_type (char_type_node,
  63. + build_index_type (size_int (length)));
  64. + type = build_qualified_type (type, TYPE_QUAL_CONST);
  65. - stack_chk_fail_decl = t;
  66. - }
  67. + init = build_string (length + 1, name);
  68. + free ((char *) name);
  69. + TREE_TYPE (init) = type;
  70. - return build_call_expr (t, 0);
  71. + func = build_decl (UNKNOWN_LOCATION, VAR_DECL, NULL_TREE, type);
  72. + TREE_STATIC (func) = 1;
  73. + TREE_READONLY (func) = 1;
  74. + DECL_ARTIFICIAL (func) = 1;
  75. + ASM_GENERATE_INTERNAL_LABEL (name_buf, "LSSH", stack_protect_labelno++);
  76. + DECL_NAME (func) = get_identifier (name_buf);
  77. + DECL_INITIAL (func) = init;
  78. +
  79. + assemble_variable (func, 0, 0, 0);
  80. +
  81. + /* Build a decl for __stack_smash_handler. */
  82. + t = build_pointer_type (TREE_TYPE (func));
  83. + t = build_function_type_list (void_type_node, t, NULL_TREE);
  84. + t = build_decl (UNKNOWN_LOCATION,
  85. + FUNCTION_DECL, get_identifier ("__stack_smash_handler"), t);
  86. + /* t = build_fn_decl ("__stack_smash_handler", t); */
  87. + TREE_STATIC (t) = 1;
  88. + TREE_PUBLIC (t) = 1;
  89. + DECL_EXTERNAL (t) = 1;
  90. + TREE_USED (t) = 1;
  91. + TREE_THIS_VOLATILE (t) = 1;
  92. + TREE_NOTHROW (t) = 1;
  93. + DECL_ARTIFICIAL (t) = 1;
  94. + DECL_IGNORED_P (t) = 1;
  95. + DECL_VISIBILITY (t) = VISIBILITY_DEFAULT;
  96. + DECL_VISIBILITY_SPECIFIED (t) = 1;
  97. +
  98. + stack_smash_handler = t;
  99. +
  100. + /* Generate a call to __stack_smash_handler(__func__). */
  101. + t = build_fold_addr_expr (func);
  102. + return build_call_expr (stack_smash_handler, 1, t);
  103. }
  104. tree
  105. default_hidden_stack_protect_fail (void)
  106. {
  107. -#ifndef HAVE_GAS_HIDDEN
  108. return default_external_stack_protect_fail ();
  109. -#else
  110. - tree t = stack_chk_fail_decl;
  111. -
  112. - if (!flag_pic)
  113. - return default_external_stack_protect_fail ();
  114. -
  115. - if (t == NULL_TREE)
  116. - {
  117. - t = build_function_type_list (void_type_node, NULL_TREE);
  118. - t = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
  119. - get_identifier ("__stack_chk_fail_local"), t);
  120. - TREE_STATIC (t) = 1;
  121. - TREE_PUBLIC (t) = 1;
  122. - DECL_EXTERNAL (t) = 1;
  123. - TREE_USED (t) = 1;
  124. - TREE_THIS_VOLATILE (t) = 1;
  125. - TREE_NOTHROW (t) = 1;
  126. - DECL_ARTIFICIAL (t) = 1;
  127. - DECL_IGNORED_P (t) = 1;
  128. - DECL_VISIBILITY_SPECIFIED (t) = 1;
  129. - DECL_VISIBILITY (t) = VISIBILITY_HIDDEN;
  130. -
  131. - stack_chk_fail_decl = t;
  132. - }
  133. -
  134. - return build_call_expr (t, 0);
  135. -#endif
  136. }
  137. bool