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