sol2.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* General Solaris system support.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by CodeSourcery, LLC.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "options.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "stringpool.h"
  31. #include "varasm.h"
  32. #include "output.h"
  33. #include "tm.h"
  34. #include "rtl.h"
  35. #include "target.h"
  36. #include "tm_p.h"
  37. #include "diagnostic-core.h"
  38. #include "ggc.h"
  39. #include "hash-table.h"
  40. tree solaris_pending_aligns, solaris_pending_inits, solaris_pending_finis;
  41. /* Attach any pending attributes for DECL to the list in *ATTRIBUTES.
  42. Pending attributes come from #pragma or _Pragma, so this code is
  43. only useful in the C family front ends, but it is included in
  44. all languages to avoid changing the target machine initializer
  45. depending on the language. */
  46. void
  47. solaris_insert_attributes (tree decl, tree *attributes)
  48. {
  49. tree *x, next;
  50. if (solaris_pending_aligns != NULL && TREE_CODE (decl) == VAR_DECL)
  51. for (x = &solaris_pending_aligns; *x; x = &TREE_CHAIN (*x))
  52. {
  53. tree name = TREE_PURPOSE (*x);
  54. tree value = TREE_VALUE (*x);
  55. if (DECL_NAME (decl) == name)
  56. {
  57. if (lookup_attribute ("aligned", DECL_ATTRIBUTES (decl))
  58. || lookup_attribute ("aligned", *attributes))
  59. warning (0, "ignoring %<#pragma align%> for explicitly "
  60. "aligned %q+D", decl);
  61. else
  62. *attributes = tree_cons (get_identifier ("aligned"), value,
  63. *attributes);
  64. next = TREE_CHAIN (*x);
  65. ggc_free (*x);
  66. *x = next;
  67. break;
  68. }
  69. }
  70. if (solaris_pending_inits != NULL && TREE_CODE (decl) == FUNCTION_DECL)
  71. for (x = &solaris_pending_inits; *x; x = &TREE_CHAIN (*x))
  72. {
  73. tree name = TREE_PURPOSE (*x);
  74. if (DECL_NAME (decl) == name)
  75. {
  76. *attributes = tree_cons (get_identifier ("init"), NULL,
  77. *attributes);
  78. TREE_USED (decl) = 1;
  79. DECL_PRESERVE_P (decl) = 1;
  80. next = TREE_CHAIN (*x);
  81. ggc_free (*x);
  82. *x = next;
  83. break;
  84. }
  85. }
  86. if (solaris_pending_finis != NULL && TREE_CODE (decl) == FUNCTION_DECL)
  87. for (x = &solaris_pending_finis; *x; x = &TREE_CHAIN (*x))
  88. {
  89. tree name = TREE_PURPOSE (*x);
  90. if (DECL_NAME (decl) == name)
  91. {
  92. *attributes = tree_cons (get_identifier ("fini"), NULL,
  93. *attributes);
  94. TREE_USED (decl) = 1;
  95. DECL_PRESERVE_P (decl) = 1;
  96. next = TREE_CHAIN (*x);
  97. ggc_free (*x);
  98. *x = next;
  99. break;
  100. }
  101. }
  102. }
  103. /* Output initializer or finalizer entries for DECL to FILE. */
  104. void
  105. solaris_output_init_fini (FILE *file, tree decl)
  106. {
  107. if (lookup_attribute ("init", DECL_ATTRIBUTES (decl)))
  108. {
  109. fprintf (file, "\t.pushsection\t" SECTION_NAME_FORMAT "\n", ".init");
  110. ASM_OUTPUT_CALL (file, decl);
  111. fprintf (file, "\t.popsection\n");
  112. }
  113. if (lookup_attribute ("fini", DECL_ATTRIBUTES (decl)))
  114. {
  115. fprintf (file, "\t.pushsection\t" SECTION_NAME_FORMAT "\n", ".fini");
  116. ASM_OUTPUT_CALL (file, decl);
  117. fprintf (file, "\t.popsection\n");
  118. }
  119. }
  120. /* Emit an assembler directive to set symbol for DECL visibility to
  121. the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
  122. void
  123. solaris_assemble_visibility (tree decl, int vis ATTRIBUTE_UNUSED)
  124. {
  125. #ifdef HAVE_GAS_HIDDEN
  126. /* Sun as uses .symbolic for STV_PROTECTED. STV_INTERNAL is marked as
  127. `currently reserved', but the linker treats it like STV_HIDDEN. Sun
  128. Studio 12.1 cc emits .hidden instead.
  129. There are 3 Sun extensions GCC doesn't yet know about: STV_EXPORTED,
  130. STV_SINGLETON, and STV_ELIMINATE.
  131. See Linker and Libraries Guide, Ch. 2, Link-Editor, Defining
  132. Additional Symbols, and Ch. 7, Object-File Format, Symbol Table
  133. Section. */
  134. static const char * const visibility_types[] = {
  135. NULL, "symbolic", "hidden", "hidden"
  136. };
  137. const char *name, *type;
  138. name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
  139. type = visibility_types[vis];
  140. fprintf (asm_out_file, "\t.%s\t", type);
  141. assemble_name (asm_out_file, name);
  142. fprintf (asm_out_file, "\n");
  143. #else
  144. if (!DECL_ARTIFICIAL (decl))
  145. warning (OPT_Wattributes, "visibility attribute not supported "
  146. "in this configuration; ignored");
  147. #endif
  148. }
  149. /* Group section information entry stored in solaris_comdat_htab. */
  150. typedef struct comdat_entry
  151. {
  152. const char *name;
  153. unsigned int flags;
  154. tree decl;
  155. const char *sig;
  156. } comdat_entry;
  157. /* Helpers for maintaining solaris_comdat_htab. */
  158. struct comdat_entry_hasher : typed_noop_remove <comdat_entry>
  159. {
  160. typedef comdat_entry value_type;
  161. typedef comdat_entry compare_type;
  162. static inline hashval_t hash (const value_type *);
  163. static inline bool equal (const value_type *, const compare_type *);
  164. static inline void remove (value_type *);
  165. };
  166. inline hashval_t
  167. comdat_entry_hasher::hash (const value_type *entry)
  168. {
  169. return htab_hash_string (entry->sig);
  170. }
  171. inline bool
  172. comdat_entry_hasher::equal (const value_type *entry1,
  173. const compare_type *entry2)
  174. {
  175. return strcmp (entry1->sig, entry2->sig) == 0;
  176. }
  177. /* Hash table of group signature symbols. */
  178. static hash_table<comdat_entry_hasher> *solaris_comdat_htab;
  179. /* Output assembly to switch to COMDAT group section NAME with attributes
  180. FLAGS and group signature symbol DECL, using Sun as syntax. */
  181. void
  182. solaris_elf_asm_comdat_section (const char *name, unsigned int flags, tree decl)
  183. {
  184. const char *signature;
  185. char *section;
  186. comdat_entry entry, **slot;
  187. if (TREE_CODE (decl) == IDENTIFIER_NODE)
  188. signature = IDENTIFIER_POINTER (decl);
  189. else
  190. signature = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl));
  191. /* Sun as requires group sections to be fragmented, i.e. to have names of
  192. the form <section>%<fragment>. Strictly speaking this is only
  193. necessary to support cc -xF, but is enforced globally in violation of
  194. the ELF gABI. We keep the section names generated by GCC (generally
  195. of the form .text.<signature>) and append %<signature> to pacify as,
  196. despite the redundancy. */
  197. section = concat (name, "%", signature, NULL);
  198. /* Clear SECTION_LINKONCE flag so targetm.asm_out.named_section only
  199. emits this as a regular section. Emit section before .group
  200. directive since Sun as treats undeclared sections as @progbits,
  201. which conflicts with .bss* sections which are @nobits. */
  202. targetm.asm_out.named_section (section, flags & ~SECTION_LINKONCE, decl);
  203. /* Sun as separates declaration of a group section and of the group
  204. itself, using the .group directive and the #comdat flag. */
  205. fprintf (asm_out_file, "\t.group\t%s," SECTION_NAME_FORMAT ",#comdat\n",
  206. signature, section);
  207. /* Unlike GNU as, group signature symbols need to be defined explicitly
  208. for Sun as. With a few exceptions, this is already the case. To
  209. identify the missing ones without changing the affected frontents,
  210. remember the signature symbols and emit those not marked
  211. TREE_SYMBOL_REFERENCED in solaris_file_end. */
  212. if (!solaris_comdat_htab)
  213. solaris_comdat_htab = new hash_table<comdat_entry_hasher> (37);
  214. entry.sig = signature;
  215. slot = solaris_comdat_htab->find_slot (&entry, INSERT);
  216. if (*slot == NULL)
  217. {
  218. *slot = XCNEW (comdat_entry);
  219. /* Remember fragmented section name. */
  220. (*slot)->name = section;
  221. /* Emit as regular section, .group declaration has already been done. */
  222. (*slot)->flags = flags & ~SECTION_LINKONCE;
  223. (*slot)->decl = decl;
  224. (*slot)->sig = signature;
  225. }
  226. }
  227. /* Define unreferenced COMDAT group signature symbol corresponding to SLOT. */
  228. int
  229. solaris_define_comdat_signature (comdat_entry **slot,
  230. void *aux ATTRIBUTE_UNUSED)
  231. {
  232. comdat_entry *entry = *slot;
  233. tree decl = entry->decl;
  234. if (TREE_CODE (decl) != IDENTIFIER_NODE)
  235. decl = DECL_COMDAT_GROUP (decl);
  236. if (!TREE_SYMBOL_REFERENCED (decl))
  237. {
  238. /* Switch to group section, otherwise Sun as complains
  239. `Group Id symbol defined outside of group'. */
  240. switch_to_section (get_section (entry->name, entry->flags, entry->decl));
  241. ASM_OUTPUT_LABEL (asm_out_file, entry->sig);
  242. }
  243. /* Continue with scan. */
  244. return 1;
  245. }
  246. /* Emit unreferenced COMDAT group signature symbols for Sun as. */
  247. void
  248. solaris_file_end (void)
  249. {
  250. if (!solaris_comdat_htab)
  251. return;
  252. solaris_comdat_htab->traverse <void *, solaris_define_comdat_signature>
  253. (NULL);
  254. }
  255. void
  256. solaris_override_options (void)
  257. {
  258. /* Older versions of Solaris ld cannot handle CIE version 3 in .eh_frame.
  259. Don't emit DWARF3/4 unless specifically selected if so. */
  260. if (!HAVE_LD_EH_FRAME_CIEV3 && !global_options_set.x_dwarf_version)
  261. dwarf_version = 2;
  262. }