fix weak external functions
[libfirm] / ir / be / begnuas.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Dumps global variables and constants as gas assembler.
23  * @author      Christian Wuerdig, Matthias Braun
24  * @date        04.11.2005
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include "begnuas.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <assert.h>
35
36 #include "obst.h"
37 #include "tv.h"
38 #include "irnode.h"
39 #include "irprog.h"
40 #include "entity_t.h"
41 #include "error.h"
42
43 #include "be_t.h"
44 #include "beemitter.h"
45 #include "be_dbgout.h"
46
47 /** by default, we generate assembler code for the Linux gas */
48 object_file_format_t  be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
49 bool                  be_gas_emit_types         = true;
50 char                  be_gas_elf_type_char      = '@';
51
52 static be_gas_section_t current_section = (be_gas_section_t) -1;
53
54 /**
55  * Return the pseudo-instruction to be issued for a section switch
56  * depending on the current flavour.
57  *
58  * @param section  the section to switch to
59  *
60  * @return  the pseudo-instruction
61  */
62 static const char *get_section_name(be_gas_section_t section)
63 {
64         static const char *text[OBJECT_FILE_FORMAT_LAST+1][GAS_SECTION_LAST+1] = {
65                 { /* OBJECT_FILE_FORMAT_ELF */
66                         ".section\t.text",
67                         ".section\t.data",
68                         ".section\t.rodata",
69                         ".section\t.bss",
70                         ".section\t.tdata,\"awT\",@progbits",
71                         ".section\t.tbss,\"awT\",@nobits",
72                         ".section\t.ctors,\"aw\",@progbits",
73                         ".section\t.dtors,\"aw\",@progbits",
74                         NULL, /* no cstring section */
75                         NULL,
76                         NULL
77                 },
78                 { /* OBJECT_FILE_FORMAT_COFF */
79                         ".section\t.text",
80                         ".section\t.data",
81                         ".section\t.rdata,\"dr\"",
82                         ".section\t.bss",
83                         ".section\t.tdata,\"awT\",@progbits",
84                         ".section\t.tbss,\"awT\",@nobits",
85                         ".section\t.ctors,\"w\"",
86                         ".section\t.dtors,\"w\"",
87                         NULL,
88                         NULL,
89                         NULL
90                 },
91                 { /* OBJECT_FILE_FORMAT_MACH_O */
92                         ".text",
93                         ".data",
94                         ".const",
95                         ".data",
96                         NULL,             /* TLS is not supported on Mach-O */
97                         NULL,             /* TLS is not supported on Mach-O */
98                         ".mod_init_func",
99                         ".mod_term_func",
100                         ".cstring",
101                         ".section\t__IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5",
102                         ".section\t__IMPORT,__pointers,non_lazy_symbol_pointers"
103                 }
104         };
105
106         assert((int) be_gas_object_file_format >= 0
107                         && be_gas_object_file_format <= OBJECT_FILE_FORMAT_LAST);
108         assert((int) section >= 0 && section <= GAS_SECTION_LAST);
109         return text[be_gas_object_file_format][section];
110 }
111
112 void be_gas_emit_switch_section(be_gas_section_t section)
113 {
114         if (current_section == section)
115                 return;
116
117         be_emit_char('\t');
118         be_emit_string(get_section_name(section));
119         be_emit_char('\n');
120         be_emit_write_line();
121         current_section = section;
122 }
123
124 static void emit_visibility(const ir_entity *entity)
125 {
126         if (get_entity_linkage(entity) & IR_LINKAGE_WEAK) {
127                 be_emit_cstring("\t.weak ");
128                 be_gas_emit_entity(entity);
129                 be_emit_char('\n');
130                 be_emit_write_line();
131                 /* Note: no need ot output .global after a .weak */
132         } else if (get_entity_visibility(entity) == ir_visibility_default) {
133                 be_emit_cstring(".globl ");
134                 be_gas_emit_entity(entity);
135                 be_emit_char('\n');
136                 be_emit_write_line();
137         }
138 }
139
140 void be_gas_emit_function_prolog(const ir_entity *entity, unsigned po2alignment)
141 {
142         be_gas_emit_switch_section(GAS_SECTION_TEXT);
143
144         /* write the begin line (makes the life easier for scripts parsing the
145          * assembler) */
146         be_emit_write_line();
147         be_emit_cstring("# -- Begin  ");
148         be_gas_emit_entity(entity);
149         be_emit_char('\n');
150         be_emit_write_line();
151
152         if (po2alignment > 0) {
153                 const char *fill_byte = "";
154                 unsigned    maximum_skip = (1 << po2alignment) - 1;
155                 /* gcc fills space between function with 0x90... */
156                 if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
157                         fill_byte = "0x90";
158                 }
159                 be_emit_cstring("\t.p2align ");
160                 be_emit_irprintf("%u,%s,%u\n", po2alignment, fill_byte, maximum_skip);
161                 be_emit_write_line();
162         }
163         emit_visibility(entity);
164
165         switch (be_gas_object_file_format) {
166         case OBJECT_FILE_FORMAT_ELF:
167                 be_emit_cstring("\t.type\t");
168                 be_gas_emit_entity(entity);
169                 be_emit_cstring(", ");
170                 be_emit_char(be_gas_elf_type_char);
171                 be_emit_cstring("function\n");
172                 be_emit_write_line();
173                 break;
174         case OBJECT_FILE_FORMAT_COFF:
175                 be_emit_cstring("\t.def\t");
176                 be_gas_emit_entity(entity);
177                 be_emit_cstring(";");
178                 if (get_entity_visibility(entity) == ir_visibility_local) {
179                         be_emit_cstring("\t.scl\t3;");
180                 } else {
181                         be_emit_cstring("\t.scl\t2;");
182                 }
183                 be_emit_cstring("\t.type\t32;\t.endef\n");
184                 be_emit_write_line();
185                 break;
186         case OBJECT_FILE_FORMAT_MACH_O:
187                 break;
188         }
189         be_gas_emit_entity(entity);
190         be_emit_cstring(":\n");
191         be_emit_write_line();
192 }
193
194 void be_gas_emit_function_epilog(const ir_entity *entity)
195 {
196         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF) {
197                 be_emit_cstring("\t.size\t");
198                 be_gas_emit_entity(entity);
199                 be_emit_cstring(", .-");
200                 be_gas_emit_entity(entity);
201                 be_emit_char('\n');
202                 be_emit_write_line();
203         }
204
205         be_emit_cstring("# -- End  ");
206         be_gas_emit_entity(entity);
207         be_emit_char('\n');
208         be_emit_write_line();
209 }
210
211 /**
212  * An environment containing all needed dumper data.
213  * Currently we create the file completely in memory first, then
214  * write it to the disk. This is an artifact from the old C-generating backend
215  * and even there NOT needed. So we might change it in the future.
216  */
217 typedef struct _be_gas_decl_env {
218         be_gas_section_t     section;
219         const be_main_env_t *main_env;
220 } be_gas_decl_env_t;
221
222 /************************************************************************/
223
224 /**
225  * Output a tarval.
226  *
227  * @param tv     the tarval
228  * @param bytes  the width of the tarvals value in bytes
229  */
230 static void dump_arith_tarval(tarval *tv, int bytes)
231 {
232         switch (bytes) {
233         case 1:
234                 be_emit_irprintf("0x%02x", get_tarval_sub_bits(tv, 0));
235                 return;
236
237         case 2:
238                 be_emit_irprintf("0x%02x%02x", get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
239                 return;
240
241         case 4:
242                 be_emit_irprintf("0x%02x%02x%02x%02x",
243                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
244                 return;
245
246         case 8:
247                 be_emit_irprintf("0x%02x%02x%02x%02x%02x%02x%02x%02x",
248                         get_tarval_sub_bits(tv, 7), get_tarval_sub_bits(tv, 6), get_tarval_sub_bits(tv, 5), get_tarval_sub_bits(tv, 4),
249                         get_tarval_sub_bits(tv, 3), get_tarval_sub_bits(tv, 2), get_tarval_sub_bits(tv, 1), get_tarval_sub_bits(tv, 0));
250                 return;
251
252         case 12:
253                 /* Beware: Mixed endian output!  One little endian number emitted as
254                  * three longs.  Each long initializer is written in big endian. */
255                 be_emit_irprintf(
256                         "\t.long\t0x%02x%02x%02x%02x\n"
257                         "\t.long\t0x%02x%02x%02x%02x\n"
258                         "\t.long\t0x%02x%02x%02x%02x",
259                         get_tarval_sub_bits(tv,  3), get_tarval_sub_bits(tv,  2),
260                         get_tarval_sub_bits(tv,  1), get_tarval_sub_bits(tv,  0),
261                         get_tarval_sub_bits(tv,  7), get_tarval_sub_bits(tv,  6),
262                         get_tarval_sub_bits(tv,  5), get_tarval_sub_bits(tv,  4),
263                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
264                         get_tarval_sub_bits(tv,  9), get_tarval_sub_bits(tv,  8)
265                 );
266                 return;
267
268         case 16:
269                 /* Beware: Mixed endian output!  One little endian number emitted as
270                  * three longs.  Each long initializer is written in big endian. */
271                 be_emit_irprintf(
272                         "\t.long\t0x%02x%02x%02x%02x\n"
273                         "\t.long\t0x%02x%02x%02x%02x\n"
274                         "\t.long\t0x%02x%02x%02x%02x\n"
275                         "\t.long\t0x%02x%02x%02x%02x",
276                         get_tarval_sub_bits(tv,  3), get_tarval_sub_bits(tv,  2),
277                         get_tarval_sub_bits(tv,  1), get_tarval_sub_bits(tv,  0),
278                         get_tarval_sub_bits(tv,  7), get_tarval_sub_bits(tv,  6),
279                         get_tarval_sub_bits(tv,  5), get_tarval_sub_bits(tv,  4),
280                         get_tarval_sub_bits(tv, 11), get_tarval_sub_bits(tv, 10),
281                         get_tarval_sub_bits(tv,  9), get_tarval_sub_bits(tv,  8),
282                         get_tarval_sub_bits(tv, 15), get_tarval_sub_bits(tv, 14),
283                         get_tarval_sub_bits(tv, 13), get_tarval_sub_bits(tv, 12)
284                 );
285                 return;
286         }
287
288         panic("Can't dump a tarval with %d bytes", bytes);
289 }
290
291 /**
292  * Return the label prefix for labeled blocks.
293  */
294 const char *be_gas_block_label_prefix(void)
295 {
296         return ".LG";
297 }
298
299 /**
300  * Return the label prefix for labeled instructions.
301  */
302 const char *be_gas_insn_label_prefix(void)
303 {
304         return ".LE";
305 }
306
307 /**
308  * Return the tarval of an atomic initializer.
309  *
310  * @param init  a node representing the initializer (on the const code irg)
311  *
312  * @return the tarval
313  */
314 static tarval *get_atomic_init_tv(ir_node *init)
315 {
316         for (;;) {
317                 ir_mode *mode = get_irn_mode(init);
318
319                 switch (get_irn_opcode(init)) {
320
321                 case iro_Cast:
322                         init = get_Cast_op(init);
323                         continue;
324
325                 case iro_Conv:
326                         init = get_Conv_op(init);
327                         continue;
328
329                 case iro_Const:
330                         return get_Const_tarval(init);
331
332                 case iro_SymConst:
333                         switch (get_SymConst_kind(init)) {
334                         case symconst_type_size:
335                                 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(init)), mode);
336
337                         case symconst_type_align:
338                                 return new_tarval_from_long(get_type_alignment_bytes(get_SymConst_type(init)), mode);
339
340                         case symconst_ofs_ent:
341                                 return new_tarval_from_long(get_entity_offset(get_SymConst_entity(init)), mode);
342
343                         case symconst_enum_const:
344                                 return get_enumeration_value(get_SymConst_enum(init));
345
346                         default:
347                                 return NULL;
348                         }
349
350                 default:
351                         return NULL;
352                 }
353         }
354 }
355
356 /**
357  * Dump an atomic value.
358  *
359  * @param env   the gas output environment
360  * @param init  a node representing the atomic value (on the const code irg)
361  */
362 static void do_dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
363 {
364         ir_mode *mode = get_irn_mode(init);
365         int bytes     = get_mode_size_bytes(mode);
366         tarval *tv;
367         ir_entity *ent;
368
369         init = skip_Id(init);
370
371         switch (get_irn_opcode(init)) {
372         case iro_Cast:
373                 do_dump_atomic_init(env, get_Cast_op(init));
374                 return;
375
376         case iro_Conv:
377                 do_dump_atomic_init(env, get_Conv_op(init));
378                 return;
379
380         case iro_Const:
381                 tv = get_Const_tarval(init);
382
383                 /* it's a arithmetic value */
384                 dump_arith_tarval(tv, bytes);
385                 return;
386
387         case iro_SymConst:
388                 switch (get_SymConst_kind(init)) {
389                 case symconst_addr_name:
390                         be_emit_ident(get_SymConst_name(init));
391                         break;
392
393                 case symconst_addr_ent:
394                         ent = get_SymConst_entity(init);
395                         be_gas_emit_entity(ent);
396                         break;
397
398                 case symconst_ofs_ent:
399                         ent = get_SymConst_entity(init);
400                         be_emit_irprintf("%d", get_entity_offset(ent));
401                         break;
402
403                 case symconst_type_size:
404                         be_emit_irprintf("%u", get_type_size_bytes(get_SymConst_type(init)));
405                         break;
406
407                 case symconst_type_align:
408                         be_emit_irprintf("%u", get_type_alignment_bytes(get_SymConst_type(init)));
409                         break;
410
411                 case symconst_enum_const:
412                         tv = get_enumeration_value(get_SymConst_enum(init));
413                         dump_arith_tarval(tv, bytes);
414                         break;
415
416                 default:
417                         assert(!"dump_atomic_init(): don't know how to init from this SymConst");
418                 }
419                 return;
420
421         case iro_Add:
422                 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
423                         panic("Constant must be int or pointer for '+' to work");
424                 }
425                 do_dump_atomic_init(env, get_Add_left(init));
426                 be_emit_cstring(" + ");
427                 do_dump_atomic_init(env, get_Add_right(init));
428                 return;
429
430         case iro_Sub:
431                 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
432                         panic("Constant must be int or pointer for '-' to work");
433                 }
434                 do_dump_atomic_init(env, get_Sub_left(init));
435                 be_emit_cstring(" - ");
436                 do_dump_atomic_init(env, get_Sub_right(init));
437                 return;
438
439         case iro_Mul:
440                 if (!mode_is_int(mode) && !mode_is_reference(mode)) {
441                         panic("Constant must be int or pointer for '*' to work");
442                 }
443                 do_dump_atomic_init(env, get_Mul_left(init));
444                 be_emit_cstring(" * ");
445                 do_dump_atomic_init(env, get_Mul_right(init));
446                 return;
447
448         case iro_Unknown:
449                 be_emit_cstring("0");
450                 return;
451
452         default:
453                 panic("dump_atomic_init(): unsupported IR-node %+F", init);
454         }
455 }
456
457 /**
458  * Dumps the type for given size (.byte, .long, ...)
459  *
460  * @param size  the size in bytes
461  */
462 static void dump_size_type(size_t size)
463 {
464         switch (size) {
465         case 1:
466                 be_emit_cstring("\t.byte\t");
467                 break;
468
469         case 2:
470                 be_emit_cstring("\t.short\t");
471                 break;
472
473         case 4:
474                 be_emit_cstring("\t.long\t");
475                 break;
476
477         case 8:
478                 be_emit_cstring("\t.quad\t");
479                 break;
480
481         case 10:
482         case 12:
483         case 16: /* Note: .octa does not work on mac */
484                 /* handled in arith */
485                 break;
486
487         default:
488                 panic("Try to dump a type with %u bytes", (unsigned)size);
489         }
490 }
491
492 /**
493  * Emit an atomic value.
494  *
495  * @param env   the gas output environment
496  * @param init  a node representing the atomic value (on the const code irg)
497  */
498 static void dump_atomic_init(be_gas_decl_env_t *env, ir_node *init)
499 {
500         ir_mode *mode = get_irn_mode(init);
501         int bytes     = get_mode_size_bytes(mode);
502
503         dump_size_type(bytes);
504         do_dump_atomic_init(env, init);
505         be_emit_char('\n');
506         be_emit_write_line();
507 }
508
509 /************************************************************************/
510 /* Routines to dump global variables                                    */
511 /************************************************************************/
512
513 static int initializer_is_string_const(const ir_initializer_t *initializer)
514 {
515         size_t i, len;
516         int found_printable = 0;
517
518         if (initializer->kind != IR_INITIALIZER_COMPOUND)
519                 return 0;
520
521         len = initializer->compound.n_initializers;
522         if (len < 1)
523                 return 0;
524         for (i = 0; i < len; ++i) {
525                 int               c;
526                 tarval           *tv;
527                 ir_mode          *mode;
528                 ir_initializer_t *sub_initializer
529                         = initializer->compound.initializers[i];
530
531                 if (sub_initializer->kind != IR_INITIALIZER_TARVAL)
532                         return 0;
533
534                 tv   = sub_initializer->tarval.value;
535                 mode = get_tarval_mode(tv);
536
537                 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
538                         return 0;
539
540                 c = get_tarval_long(tv);
541                 if (isgraph(c) || isspace(c))
542                         found_printable = 1;
543                 else if (c != 0)
544                         return 0;
545
546                 if (i == len - 1 && c != '\0')
547                         return 0;
548         }
549
550         return found_printable;
551 }
552
553 /**
554  * Determine if an entity is a string constant
555  * @param ent The entity
556  * @return 1 if it is a string constant, 0 otherwise
557  */
558 static int ent_is_string_const(const ir_entity *ent)
559 {
560         ir_type *type, *element_type;
561         ir_mode *mode;
562         int i, c, n;
563
564         type = get_entity_type(ent);
565
566         /* if it's an array */
567         if (!is_Array_type(type))
568                 return 0;
569
570         element_type = get_array_element_type(type);
571
572         /* and the array's element type is primitive */
573         if (!is_Primitive_type(element_type))
574                 return 0;
575
576         /* and the mode of the element type is an int of
577          * the same size as the byte mode */
578         mode = get_type_mode(element_type);
579         if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
580                 return 0;
581
582         if (ent->initializer != NULL) {
583                 return initializer_is_string_const(ent->initializer);
584         } else if (entity_has_compound_ent_values(ent)) {
585                 int found_printable = 0;
586                 /* if it contains only printable chars and a 0 at the end */
587                 n = get_compound_ent_n_values(ent);
588                 for (i = 0; i < n; ++i) {
589                         ir_node *irn = get_compound_ent_value(ent, i);
590                         if (! is_Const(irn))
591                                 return 0;
592
593                         c = (int) get_tarval_long(get_Const_tarval(irn));
594
595                         if (isgraph(c) || isspace(c))
596                                 found_printable = 1;
597                         else if (c != 0)
598                                 return 0;
599
600                         if (i == n - 1 && c != '\0')
601                                 return 0;
602                 }
603                 return found_printable;
604         }
605
606         return 0;
607 }
608
609 static bool initializer_is_null(const ir_initializer_t *initializer)
610 {
611         switch (initializer->kind) {
612         case IR_INITIALIZER_NULL:
613                 return true;
614         case IR_INITIALIZER_TARVAL: {
615                 tarval *tv = initializer->tarval.value;
616                 return tarval_is_null(tv);
617         }
618         case IR_INITIALIZER_CONST: {
619                 ir_node *value = initializer->consti.value;
620                 if (!is_Const(value))
621                         return false;
622                 return is_Const_null(value);
623         }
624         case IR_INITIALIZER_COMPOUND: {
625                 size_t i;
626                 for (i = 0; i < initializer->compound.n_initializers; ++i) {
627                         ir_initializer_t *subinitializer
628                                 = initializer->compound.initializers[i];
629                         if (!initializer_is_null(subinitializer))
630                                 return false;
631                 }
632                 return true;
633         }
634         }
635         panic("invalid initializer in initializer_is_null");
636 }
637
638 static bool entity_is_null(const ir_entity *entity)
639 {
640         if (entity->initializer != NULL) {
641                 return initializer_is_null(entity->initializer);
642         } else if (entity_has_compound_ent_values(entity)) {
643                 /* I'm too lazy to implement this case as compound graph paths will be
644                  * remove anyway in the future */
645         }
646         /* uninitialized, NULL is fine */
647         return true;
648 }
649
650 /**
651  * Dump a string constant.
652  * No checks are made!!
653  *
654  * @param ent  The entity to dump.
655  */
656 static void dump_string_cst(const ir_entity *ent)
657 {
658         int      i, len;
659         int      output_len;
660         ir_type *type;
661         int      type_size;
662         int      remaining_space;
663
664         len        = get_compound_ent_n_values(ent);
665         output_len = len;
666         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
667                 be_emit_cstring("\t.ascii \"");
668         } else {
669                 be_emit_cstring("\t.string \"");
670                 output_len -= 1;
671         }
672
673         for (i = 0; i < output_len; ++i) {
674                 ir_node *irn;
675                 int c;
676
677                 irn = get_compound_ent_value(ent, i);
678                 c = (int) get_tarval_long(get_Const_tarval(irn));
679
680                 switch (c) {
681                 case '"' : be_emit_cstring("\\\""); break;
682                 case '\n': be_emit_cstring("\\n"); break;
683                 case '\r': be_emit_cstring("\\r"); break;
684                 case '\t': be_emit_cstring("\\t"); break;
685                 case '\\': be_emit_cstring("\\\\"); break;
686                 default  :
687                         if (isprint(c))
688                                 be_emit_char(c);
689                         else
690                                 be_emit_irprintf("\\%o", c);
691                         break;
692                 }
693         }
694         be_emit_cstring("\"\n");
695         be_emit_write_line();
696
697         type            = get_entity_type(ent);
698         type_size       = get_type_size_bytes(type);
699         remaining_space = type_size - len;
700         assert(remaining_space >= 0);
701         if (remaining_space > 0) {
702                 be_emit_irprintf("\t.zero\t%d\n", remaining_space);
703         }
704 }
705
706 static void dump_string_initializer(const ir_initializer_t *initializer)
707 {
708         size_t i, len;
709
710         len = initializer->compound.n_initializers;
711         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
712                 be_emit_cstring("\t.ascii \"");
713         } else {
714                 be_emit_cstring("\t.string \"");
715                 len -= 1;
716         }
717
718         for (i = 0; i < len; ++i) {
719                 const ir_initializer_t *sub_initializer
720                         = get_initializer_compound_value(initializer, i);
721
722                 tarval *tv = get_initializer_tarval_value(sub_initializer);
723                 int     c  = get_tarval_long(tv);
724
725                 switch (c) {
726                 case '"' : be_emit_cstring("\\\""); break;
727                 case '\n': be_emit_cstring("\\n"); break;
728                 case '\r': be_emit_cstring("\\r"); break;
729                 case '\t': be_emit_cstring("\\t"); break;
730                 case '\\': be_emit_cstring("\\\\"); break;
731                 default  :
732                         if (isprint(c))
733                                 be_emit_char(c);
734                         else
735                                 be_emit_irprintf("\\%o", c);
736                         break;
737                 }
738         }
739         be_emit_cstring("\"\n");
740         be_emit_write_line();
741 }
742
743 enum normal_or_bitfield_kind {
744         NORMAL = 0,
745         TARVAL,
746         BITFIELD
747 };
748
749 typedef struct {
750         enum normal_or_bitfield_kind kind;
751         union {
752                 ir_node       *value;
753                 tarval        *tarval;
754                 unsigned char  bf_val;
755         } v;
756 } normal_or_bitfield;
757
758 static int is_type_variable_size(ir_type *type)
759 {
760         (void) type;
761         /* TODO */
762         return 0;
763 }
764
765 static size_t get_initializer_size(const ir_initializer_t *initializer,
766                                    ir_type *type)
767 {
768         switch (get_initializer_kind(initializer)) {
769         case IR_INITIALIZER_TARVAL:
770                 assert(get_tarval_mode(get_initializer_tarval_value(initializer)) == get_type_mode(type));
771                 return get_type_size_bytes(type);
772         case IR_INITIALIZER_CONST:
773         case IR_INITIALIZER_NULL:
774                 return get_type_size_bytes(type);
775         case IR_INITIALIZER_COMPOUND:
776                 if (!is_type_variable_size(type)) {
777                         return get_type_size_bytes(type);
778                 } else {
779                         unsigned n_entries
780                                 = get_initializer_compound_n_entries(initializer);
781                         unsigned i;
782                         unsigned initializer_size = get_type_size_bytes(type);
783                         for (i = 0; i < n_entries; ++i) {
784                                 ir_entity *entity = get_compound_member(type, i);
785                                 ir_type   *type   = get_entity_type(entity);
786
787                                 const ir_initializer_t *sub_initializer
788                                         = get_initializer_compound_value(initializer, i);
789
790                                 unsigned offset = get_entity_offset(entity);
791                                 unsigned size   = get_initializer_size(sub_initializer, type);
792
793                                 if (offset + size > initializer_size) {
794                                         initializer_size = offset + size;
795                                 }
796                         }
797                         return initializer_size;
798                 }
799         }
800
801         panic("found invalid initializer");
802 }
803
804 #ifndef NDEBUG
805 static normal_or_bitfield *glob_vals;
806 static size_t              max_vals;
807 #endif
808
809 static void dump_bitfield(normal_or_bitfield *vals, size_t offset_bits,
810                           const ir_initializer_t *initializer, ir_type *type)
811 {
812         unsigned char  last_bits = 0;
813         ir_mode       *mode      = get_type_mode(type);
814         tarval        *tv        = NULL;
815         unsigned char  curr_bits;
816         int            value_len;
817         int            j;
818
819         switch (get_initializer_kind(initializer)) {
820         case IR_INITIALIZER_NULL:
821                 return;
822         case IR_INITIALIZER_TARVAL:
823                 tv = get_initializer_tarval_value(initializer);
824                 break;
825         case IR_INITIALIZER_CONST: {
826                 ir_node *node = get_initializer_const_value(initializer);
827                 if (!is_Const(node)) {
828                         panic("bitfield initializer not a Const node");
829                 }
830                 tv = get_Const_tarval(node);
831                 break;
832         }
833         case IR_INITIALIZER_COMPOUND:
834                 panic("bitfield initializer is compound");
835         }
836         if (tv == NULL) {
837                 panic("Couldn't get numeric value for bitfield initializer");
838         }
839         tv = tarval_convert_to(tv, get_type_mode(type));
840
841         /* normalize offset */
842         vals        += offset_bits >> 3;
843         offset_bits &= 7;
844         value_len    = get_mode_size_bits(mode);
845
846         /* combine bits with existing bits */
847         for (j = 0; value_len + (int) offset_bits > 0; ++j) {
848                 assert((size_t) (vals - glob_vals) + j < max_vals);
849                 assert(vals[j].kind == BITFIELD ||
850                                 (vals[j].kind == NORMAL && vals[j].v.value == NULL));
851                 vals[j].kind = BITFIELD;
852                 curr_bits    = get_tarval_sub_bits(tv, j);
853                 vals[j].v.bf_val
854                         |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
855                 value_len -= 8;
856                 last_bits = curr_bits;
857         }
858 }
859
860 static void dump_ir_initializer(normal_or_bitfield *vals,
861                                 const ir_initializer_t *initializer,
862                                 ir_type *type)
863 {
864         assert((size_t) (vals - glob_vals) < max_vals);
865
866         switch (get_initializer_kind(initializer)) {
867         case IR_INITIALIZER_NULL:
868                 return;
869         case IR_INITIALIZER_TARVAL: {
870                 size_t i;
871
872                 assert(vals->kind != BITFIELD);
873                 vals->kind     = TARVAL;
874                 vals->v.tarval = get_initializer_tarval_value(initializer);
875                 assert(get_type_mode(type) == get_tarval_mode(vals->v.tarval));
876                 for (i = 1; i < get_type_size_bytes(type); ++i) {
877                         vals[i].kind    = NORMAL;
878                         vals[i].v.value = NULL;
879                 }
880                 return;
881         }
882         case IR_INITIALIZER_CONST: {
883                 size_t i;
884
885                 assert(vals->kind != BITFIELD);
886                 vals->kind    = NORMAL;
887                 vals->v.value = get_initializer_const_value(initializer);
888                 for (i = 1; i < get_type_size_bytes(type); ++i) {
889                         vals[i].kind    = NORMAL;
890                         vals[i].v.value = NULL;
891                 }
892                 return;
893         }
894         case IR_INITIALIZER_COMPOUND: {
895                 size_t i = 0;
896                 size_t n = get_initializer_compound_n_entries(initializer);
897
898                 if (is_Array_type(type)) {
899                         ir_type *element_type = get_array_element_type(type);
900                         size_t   skip         = get_type_size_bytes(element_type);
901                         size_t   alignment    = get_type_alignment_bytes(element_type);
902                         size_t   misalign     = skip % alignment;
903                         if (misalign != 0) {
904                                 skip += alignment - misalign;
905                         }
906
907                         for (i = 0; i < n; ++i) {
908                                 ir_initializer_t *sub_initializer
909                                         = get_initializer_compound_value(initializer, i);
910
911                                 dump_ir_initializer(vals, sub_initializer, element_type);
912
913                                 vals += skip;
914                         }
915                 } else {
916                         size_t n_members, i;
917                         assert(is_compound_type(type));
918                         n_members = get_compound_n_members(type);
919                         for (i = 0; i < n_members; ++i) {
920                                 ir_entity        *member    = get_compound_member(type, i);
921                                 size_t            offset    = get_entity_offset(member);
922                                 ir_type          *subtype   = get_entity_type(member);
923                                 ir_mode          *mode      = get_type_mode(subtype);
924                                 ir_initializer_t *sub_initializer;
925
926                                 assert(i < get_initializer_compound_n_entries(initializer));
927                                 sub_initializer
928                                         = get_initializer_compound_value(initializer, i);
929
930                                 if (mode != NULL) {
931                                         size_t offset_bits
932                                                 = get_entity_offset_bits_remainder(member);
933                                         size_t value_len   = get_mode_size_bits(mode);
934
935                                         if (offset_bits != 0 ||
936                                                 (value_len != 8 && value_len != 16 && value_len != 32
937                                                  && value_len != 64)) {
938                                                 dump_bitfield(&vals[offset], offset_bits,
939                                                               sub_initializer, subtype);
940                                                 continue;
941                                         }
942                                 }
943
944                                 dump_ir_initializer(&vals[offset], sub_initializer, subtype);
945                         }
946                 }
947
948                 return;
949         }
950         }
951         panic("invalid ir_initializer kind found");
952 }
953
954 static void dump_initializer(be_gas_decl_env_t *env, const ir_entity *entity)
955 {
956         const ir_initializer_t *initializer = entity->initializer;
957         ir_type                *type;
958         normal_or_bitfield     *vals;
959         size_t                  size;
960         size_t                  k;
961
962         if (initializer_is_string_const(initializer)) {
963                 dump_string_initializer(initializer);
964                 return;
965         }
966
967         type = get_entity_type(entity);
968         size = get_initializer_size(initializer, type);
969
970         if (size == 0)
971                 return;
972
973         /*
974          * In the worst case, every initializer allocates one byte.
975          * Moreover, initializer might be big, do not allocate on stack.
976          */
977         vals = XMALLOCNZ(normal_or_bitfield, size);
978
979 #ifndef NDEBUG
980         glob_vals = vals;
981         max_vals  = size;
982 #endif
983
984         dump_ir_initializer(vals, initializer, type);
985
986         /* now write values sorted */
987         for (k = 0; k < size; ) {
988                 int space     = 0;
989                 int elem_size = 1;
990                 if (vals[k].kind == NORMAL) {
991                         if (vals[k].v.value != NULL) {
992                                 dump_atomic_init(env, vals[k].v.value);
993                                 elem_size = get_mode_size_bytes(get_irn_mode(vals[k].v.value));
994                         } else {
995                                 elem_size = 0;
996                         }
997                 } else if (vals[k].kind == TARVAL) {
998                         tarval *tv   = vals[k].v.tarval;
999                         size_t  size = get_mode_size_bytes(get_tarval_mode(tv));
1000
1001                         assert(tv != NULL);
1002
1003                         elem_size = size;
1004                         dump_size_type(size);
1005                         dump_arith_tarval(tv, size);
1006                         be_emit_char('\n');
1007                         be_emit_write_line();
1008                 } else {
1009                         assert(vals[k].kind == BITFIELD);
1010                         be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1011                         be_emit_write_line();
1012                 }
1013
1014                 k += elem_size;
1015                 while (k < size && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1016                         ++space;
1017                         ++k;
1018                 }
1019
1020                 /* a gap */
1021                 if (space > 0) {
1022                         be_emit_irprintf("\t.space\t%d\n", space);
1023                         be_emit_write_line();
1024                 }
1025         }
1026         xfree(vals);
1027 }
1028
1029 static void dump_compound_graph_init(be_gas_decl_env_t *env,
1030                                      const ir_entity *ent)
1031 {
1032         normal_or_bitfield *vals;
1033         int i, j, n;
1034         unsigned k, last_ofs;
1035
1036         if (ent_is_string_const(ent)) {
1037                 dump_string_cst(ent);
1038                 return;
1039         }
1040
1041         n = get_compound_ent_n_values(ent);
1042
1043         /* Find the initializer size. Sorrily gcc support a nasty feature:
1044            The last field of a compound may be a flexible array. This allows
1045            initializers bigger than the type size. */
1046         last_ofs = get_type_size_bytes(get_entity_type(ent));
1047         for (i = 0; i < n; ++i) {
1048                 unsigned offset         = get_compound_ent_value_offset_bytes(ent, i);
1049                 unsigned bits_remainder = get_compound_ent_value_offset_bit_remainder(ent, i);
1050                 ir_node  *value         = get_compound_ent_value(ent, i);
1051                 unsigned value_len      = get_mode_size_bits(get_irn_mode(value));
1052
1053                 offset += (value_len + bits_remainder + 7) >> 3;
1054
1055                 if (offset > last_ofs) {
1056                         last_ofs = offset;
1057                 }
1058         }
1059
1060         /*
1061          * In the worst case, every initializer allocates one byte.
1062          * Moreover, initializer might be big, do not allocate on stack.
1063          */
1064         vals = XMALLOCNZ(normal_or_bitfield, last_ofs);
1065
1066         /* collect the values and store them at the offsets */
1067         for (i = 0; i < n; ++i) {
1068                 unsigned offset      = get_compound_ent_value_offset_bytes(ent, i);
1069                 int      offset_bits = get_compound_ent_value_offset_bit_remainder(ent, i);
1070                 ir_node  *value      = get_compound_ent_value(ent, i);
1071                 int      value_len   = get_mode_size_bits(get_irn_mode(value));
1072
1073                 assert(offset_bits >= 0);
1074
1075                 if (offset_bits != 0 ||
1076                                 (value_len != 8 && value_len != 16 && value_len != 32 && value_len != 64)) {
1077                         tarval *tv = get_atomic_init_tv(value);
1078                         unsigned char curr_bits, last_bits = 0;
1079                         if (tv == NULL) {
1080                                 panic("Couldn't get numeric value for bitfield initializer '%s'",
1081                                                 get_entity_ld_name(ent));
1082                         }
1083                         /* normalize offset */
1084                         offset += offset_bits >> 3;
1085                         offset_bits &= 7;
1086
1087                         for (j = 0; value_len + offset_bits > 0; ++j) {
1088                                 assert(offset + j < last_ofs);
1089                                 assert(vals[offset + j].kind == BITFIELD || vals[offset + j].v.value == NULL);
1090                                 vals[offset + j].kind = BITFIELD;
1091                                 curr_bits = get_tarval_sub_bits(tv, j);
1092                                 vals[offset + j].v.bf_val |= (last_bits >> (8 - offset_bits)) | (curr_bits << offset_bits);
1093                                 value_len -= 8;
1094                                 last_bits = curr_bits;
1095                         }
1096                 } else {
1097                         int i;
1098
1099                         assert(offset < last_ofs);
1100                         assert(vals[offset].kind == NORMAL);
1101                         for (i = 1; i < value_len / 8; ++i) {
1102                                 assert(vals[offset + i].v.value == NULL);
1103                         }
1104                         vals[offset].v.value = value;
1105                 }
1106         }
1107
1108         /* now write them sorted */
1109         for (k = 0; k < last_ofs; ) {
1110                 int space = 0, skip = 0;
1111                 if (vals[k].kind == NORMAL) {
1112                         if (vals[k].v.value != NULL) {
1113                                 dump_atomic_init(env, vals[k].v.value);
1114                                 skip = get_mode_size_bytes(get_irn_mode(vals[k].v.value)) - 1;
1115                         } else {
1116                                 space = 1;
1117                         }
1118                 } else {
1119                         assert(vals[k].kind == BITFIELD);
1120                         be_emit_irprintf("\t.byte\t%d\n", vals[k].v.bf_val);
1121                 }
1122
1123                 ++k;
1124                 while (k < last_ofs && vals[k].kind == NORMAL && vals[k].v.value == NULL) {
1125                         ++space;
1126                         ++k;
1127                 }
1128                 space -= skip;
1129                 assert(space >= 0);
1130
1131                 /* a gap */
1132                 if (space > 0) {
1133                         be_emit_irprintf("\t.space\t%d\n", space);
1134                         be_emit_write_line();
1135                 }
1136         }
1137         xfree(vals);
1138 }
1139
1140 static void emit_align(unsigned p2alignment)
1141 {
1142         be_emit_irprintf("\t.p2align\t%u\n", log2_floor(p2alignment));
1143         be_emit_write_line();
1144 }
1145
1146 static unsigned get_effective_entity_alignment(const ir_entity *entity)
1147 {
1148         unsigned alignment = get_entity_alignment(entity);
1149         if (alignment == 0) {
1150                 ir_type *type = get_entity_type(entity);
1151                 alignment     = get_type_alignment_bytes(type);
1152         }
1153         return alignment;
1154 }
1155
1156 static be_gas_section_t determine_section(be_gas_decl_env_t *env,
1157                                           const ir_entity *entity)
1158 {
1159         ir_type *owner = get_entity_owner(entity);
1160
1161         if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
1162                 if (is_method_entity(entity))
1163                         return GAS_SECTION_TEXT;
1164
1165                 ir_linkage linkage = get_entity_linkage(entity);
1166                 if (linkage & IR_LINKAGE_CONSTANT) {
1167                         /* mach-o is the only one with a cstring section */
1168                         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
1169                                         && ent_is_string_const(entity))
1170                                 return GAS_SECTION_CSTRING;
1171
1172                         return GAS_SECTION_RODATA;
1173                 }
1174                 if (entity_is_null(entity))
1175                         return GAS_SECTION_BSS;
1176
1177                 return GAS_SECTION_DATA;
1178         } else if (owner == env->main_env->pic_symbols_type) {
1179                 return GAS_SECTION_PIC_SYMBOLS;
1180         } else if (owner == env->main_env->pic_trampolines_type) {
1181                 return GAS_SECTION_PIC_TRAMPOLINES;
1182         } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
1183                 return GAS_SECTION_CONSTRUCTORS;
1184         } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
1185                 return GAS_SECTION_DESTRUCTORS;
1186         } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
1187                 ir_linkage linkage = get_entity_linkage(entity);
1188                 if (linkage & IR_LINKAGE_MERGE) {
1189                         panic("IR_LINKAGE_MERGE not supported for THREAD_LOCAL entities");
1190                 }
1191
1192                 if (entity_is_null(entity)) {
1193                         return GAS_SECTION_TLS_BSS;
1194                 }
1195                 return GAS_SECTION_TLS_DATA;
1196         }
1197
1198         panic("Couldn't determine section for %+F?!?", entity);
1199 }
1200
1201 static void emit_common(const ir_entity *entity)
1202 {
1203         const char    *name       = get_entity_ld_name(entity);
1204         unsigned       size       = get_type_size_bytes(get_entity_type(entity));
1205         unsigned       alignment  = get_effective_entity_alignment(entity);
1206         ir_visibility  visibility = get_entity_visibility(entity);
1207
1208         if (visibility == ir_visibility_local
1209                         || visibility == ir_visibility_private) {
1210                 /* counter the visibility_global effect of .comm
1211                  * ... and to be honest I have no idea what local common symbols
1212                  *     are good for...
1213                  */
1214                 be_emit_irprintf("\t.local %s\n", name);
1215                 be_emit_write_line();
1216         }
1217
1218         switch (be_gas_object_file_format) {
1219         case OBJECT_FILE_FORMAT_MACH_O:
1220                 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size,
1221                                  log2_floor(alignment));
1222                 be_emit_write_line();
1223                 return;
1224         case OBJECT_FILE_FORMAT_ELF:
1225                 be_emit_irprintf("\t.comm %s,%u,%u\n", name, size, alignment);
1226                 be_emit_write_line();
1227                 return;
1228         case OBJECT_FILE_FORMAT_COFF:
1229                 be_emit_irprintf("\t.comm %s,%u # %u\n", name, size, alignment);
1230                 be_emit_write_line();
1231                 return;
1232         }
1233         panic("invalid object file format");
1234 }
1235
1236 static void dump_indirect_symbol(const ir_entity *entity, be_gas_section_t section)
1237 {
1238         /* we can only do PIC code on macho so far */
1239         assert(be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O);
1240
1241         be_emit_ident(get_entity_ld_ident(entity));
1242         be_emit_cstring(":\n");
1243         be_emit_write_line();
1244         be_emit_cstring("\t.indirect_symbol ");
1245         be_emit_ident(get_entity_ident(entity));
1246         be_emit_char('\n');
1247         be_emit_write_line();
1248         if (section == GAS_SECTION_PIC_TRAMPOLINES) {
1249                 be_emit_cstring("\thlt ; hlt ; hlt ; hlt ; hlt\n");
1250                 be_emit_write_line();
1251         } else {
1252                 assert(section == GAS_SECTION_PIC_SYMBOLS);
1253                 be_emit_cstring("\t.long 0\n");
1254                 be_emit_write_line();
1255         }
1256 }
1257
1258 void be_gas_emit_entity(const ir_entity *entity)
1259 {
1260         if (entity->type == firm_code_type) {
1261                 ir_label_t label = get_entity_label(entity);
1262                 be_emit_string(be_gas_block_label_prefix());
1263                 be_emit_irprintf("%lu", label);
1264                 return;
1265         }
1266
1267         if (get_entity_visibility(entity) == ir_visibility_private) {
1268                 be_emit_cstring(".L");
1269         }
1270         be_emit_ident(get_entity_ld_ident(entity));
1271 }
1272
1273 /**
1274  * Dump a global entity.
1275  *
1276  * @param env  the gas output environment
1277  * @param ent  the entity to be dumped
1278  */
1279 static void dump_global(be_gas_decl_env_t *env, const ir_entity *ent)
1280 {
1281         ir_type          *type       = get_entity_type(ent);
1282         ident            *ld_ident   = get_entity_ld_ident(ent);
1283         unsigned          alignment  = get_effective_entity_alignment(ent);
1284         be_gas_section_t  section    = determine_section(env, ent);
1285         ir_visibility     visibility = get_entity_visibility(ent);
1286         ir_linkage        linkage    = get_entity_linkage(ent);
1287
1288         /* block labels are already emittet in the code */
1289         if (type == firm_code_type)
1290                 return;
1291
1292         /* we already emitted all methods. Except for the trampolines which
1293          * the assembler/linker generates */
1294         if (is_Method_type(type) && section != GAS_SECTION_PIC_TRAMPOLINES) {
1295                 /* functions with graph are already emitted with
1296                  * be_gas_emit_function_prolog */
1297                 if (get_entity_irg(ent) == NULL) {
1298                         emit_visibility(ent);
1299                 }
1300                 return;
1301         }
1302
1303         be_dbg_variable(ent);
1304
1305         emit_visibility(ent);
1306         if (visibility == ir_visibility_external) {
1307                 /* nothing to do for externally defined values */
1308                 return;
1309         }
1310
1311         if (!is_po2(alignment))
1312                 panic("alignment not a power of 2");
1313
1314         if (section == GAS_SECTION_BSS && (linkage & IR_LINKAGE_MERGE)) {
1315                 if (get_entity_visibility(ent) == ir_visibility_external) {
1316                         panic("merge link semantic not supported for extern entities");
1317                 }
1318                 emit_common(ent);
1319                 return;
1320         }
1321
1322         be_gas_emit_switch_section(section);
1323
1324         if (section == GAS_SECTION_PIC_TRAMPOLINES
1325                         || section == GAS_SECTION_PIC_SYMBOLS) {
1326                 dump_indirect_symbol(ent, section);
1327                 return;
1328         }
1329
1330         /* alignment */
1331         if (alignment > 1) {
1332                 emit_align(alignment);
1333         }
1334         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_ELF
1335                         && be_gas_emit_types) {
1336                 be_emit_cstring("\t.type\t");
1337                 be_gas_emit_entity(ent);
1338                 be_emit_cstring(", ");
1339                 be_emit_char(be_gas_elf_type_char);
1340                 be_emit_cstring("object\n\t.size\t");\
1341                 be_gas_emit_entity(ent);
1342                 be_emit_irprintf(", %u\n", get_type_size_bytes(type));
1343         }
1344
1345         if (get_id_str(ld_ident)[0] != '\0') {
1346             be_gas_emit_entity(ent);
1347                 be_emit_cstring(":\n");
1348                 be_emit_write_line();
1349         }
1350
1351         if (entity_is_null(ent)) {
1352                 be_emit_irprintf("\t.zero %u\n", get_type_size_bytes(type));
1353                 be_emit_write_line();
1354         } else if(entity_has_compound_ent_values(ent)) {
1355                 dump_compound_graph_init(env, ent);
1356         } else {
1357                 assert(ent->initializer != NULL);
1358                 dump_initializer(env, ent);
1359         }
1360 }
1361
1362 /**
1363  * Dumps declarations of global variables and the initialization code.
1364  *
1365  * @param gt                a global like type, either the global or the TLS one
1366  * @param env               an environment
1367  */
1368 static void be_gas_dump_globals(ir_type *gt, be_gas_decl_env_t *env)
1369 {
1370         int i, n = get_compound_n_members(gt);
1371
1372         for (i = 0; i < n; i++) {
1373                 ir_entity *ent = get_compound_member(gt, i);
1374                 dump_global(env, ent);
1375         }
1376 }
1377
1378 /************************************************************************/
1379
1380 /* Generate all entities. */
1381 void be_gas_emit_decls(const be_main_env_t *main_env)
1382 {
1383         be_gas_decl_env_t env;
1384         memset(&env, 0, sizeof(env));
1385
1386         /* dump global type */
1387         env.main_env = main_env;
1388         env.section  = (be_gas_section_t) -1;
1389
1390         be_gas_dump_globals(get_glob_type(), &env);
1391         be_gas_dump_globals(get_tls_type(), &env);
1392         be_gas_dump_globals(get_segment_type(IR_SEGMENT_CONSTRUCTORS), &env);
1393         be_gas_dump_globals(get_segment_type(IR_SEGMENT_DESTRUCTORS), &env);
1394         be_gas_dump_globals(main_env->pic_symbols_type, &env);
1395         be_gas_dump_globals(main_env->pic_trampolines_type, &env);
1396
1397         /**
1398          * ".subsections_via_symbols marks object files which are OK to divide
1399          * their section contents into individual blocks".
1400          * From my understanding this means no label points in the middle of an
1401          * object which we want to address as a whole. Firm code should be fine
1402          * with this.
1403          */
1404         if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O) {
1405                 be_emit_cstring("\t.subsections_via_symbols\n");
1406                 be_emit_write_line();
1407         }
1408 }