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