+static unsigned get_effective_entity_alignment(const ir_entity *entity)
+{
+ unsigned alignment = get_entity_alignment(entity);
+ if (alignment == 0) {
+ ir_type *type = get_entity_type(entity);
+ alignment = get_type_alignment_bytes(type);
+ }
+ return alignment;
+}
+
+static be_gas_section_t determine_section(be_gas_decl_env_t *env,
+ const ir_entity *entity)
+{
+ ir_type *owner = get_entity_owner(entity);
+
+ if (owner == get_segment_type(IR_SEGMENT_GLOBAL)) {
+ ir_linkage linkage = get_entity_linkage(entity);
+ if (linkage & IR_LINKAGE_CONSTANT) {
+ /* mach-o is the only one with a cstring section */
+ if (be_gas_object_file_format == OBJECT_FILE_FORMAT_MACH_O
+ && ent_is_string_const(entity))
+ return GAS_SECTION_CSTRING;
+
+ return GAS_SECTION_RODATA;
+ }
+ if (!entity_has_definition(entity))
+ return GAS_SECTION_BSS;
+
+ return GAS_SECTION_DATA;
+
+ } else if (owner == env->main_env->pic_symbols_type) {
+ return GAS_SECTION_PIC_SYMBOLS;
+ } else if (owner == env->main_env->pic_trampolines_type) {
+ return GAS_SECTION_PIC_TRAMPOLINES;
+ } else if (owner == get_segment_type(IR_SEGMENT_CONSTRUCTORS)) {
+ return GAS_SECTION_CONSTRUCTORS;
+ } else if (owner == get_segment_type(IR_SEGMENT_DESTRUCTORS)) {
+ return GAS_SECTION_DESTRUCTORS;
+ } else if (owner == get_segment_type(IR_SEGMENT_THREAD_LOCAL)) {
+ return GAS_SECTION_TLS;
+ }
+
+ panic("Couldn't determine section for %+F?!?", entity);
+}
+
+static void emit_common(const ir_entity *ent)
+{
+ const char *name = get_entity_ld_name(ent);
+ unsigned size = get_type_size_bytes(get_entity_type(ent));
+ unsigned alignment = get_effective_entity_alignment(ent);
+
+ switch (be_gas_object_file_format) {
+ case OBJECT_FILE_FORMAT_MACH_O:
+ be_emit_irprintf("\t.comm %s,%u,%u\n", name, size,
+ log2_floor(alignment));
+ be_emit_write_line();
+ return;
+ case OBJECT_FILE_FORMAT_ELF:
+ be_emit_irprintf("\t.comm %s,%u,%u\n", name, size, alignment);
+ be_emit_write_line();
+ return;
+ case OBJECT_FILE_FORMAT_COFF:
+ be_emit_irprintf("\t.comm %s,%u # %u\n", name, size, alignment);
+ be_emit_write_line();
+ return;
+ }
+ panic("invalid object file format");
+}
+