+ char * name;
+ const char * sec_name;
+
+ /* Switch to the relevent sub-section before we start to emit
+ the line number table.
+
+ FIXME: These sub-sections do not have a normal Line Number
+ Program Header, thus strictly speaking they are not valid
+ DWARF sections. Unfortunately the DWARF standard assumes
+ a one-to-one relationship between compilation units and
+ line number tables. Thus we have to have a .debug_line
+ section, as well as our sub-sections, and we have to ensure
+ that all of the sub-sections are merged into a proper
+ .debug_line section before a debugger sees them. */
+
+ sec_name = bfd_get_section_name (stdoutput, seg);
+ if (strcmp (sec_name, ".text") != 0)
+ {
+ name = concat (".debug_line", sec_name, (char *) NULL);
+ subseg_set (subseg_get (name, FALSE), 0);
+ }
+ else
+ /* Don't create a .debug_line.text section -
+ that is redundant. Instead just switch back to the
+ normal .debug_line section. */
+ subseg_set (subseg_get (".debug_line", FALSE), 0);
+ }
+
+ do
+ {
+ int line_delta;
+
+ if (filenum != e->loc.filenum)
+ {
+ filenum = e->loc.filenum;
+ out_opcode (DW_LNS_set_file);
+ out_uleb128 (filenum);
+ }
+
+ if (column != e->loc.column)
+ {
+ column = e->loc.column;
+ out_opcode (DW_LNS_set_column);
+ out_uleb128 (column);
+ }
+
+ if (e->loc.discriminator != 0)
+ {
+ out_opcode (DW_LNS_extended_op);
+ out_leb128 (1 + sizeof_leb128 (e->loc.discriminator, 0));
+ out_opcode (DW_LNE_set_discriminator);
+ out_uleb128 (e->loc.discriminator);
+ }
+
+ if (isa != e->loc.isa)
+ {
+ isa = e->loc.isa;
+ out_opcode (DW_LNS_set_isa);
+ out_uleb128 (isa);
+ }
+
+ if ((e->loc.flags ^ flags) & DWARF2_FLAG_IS_STMT)
+ {
+ flags = e->loc.flags;
+ out_opcode (DW_LNS_negate_stmt);
+ }
+
+ if (e->loc.flags & DWARF2_FLAG_BASIC_BLOCK)
+ out_opcode (DW_LNS_set_basic_block);
+
+ if (e->loc.flags & DWARF2_FLAG_PROLOGUE_END)
+ out_opcode (DW_LNS_set_prologue_end);
+
+ if (e->loc.flags & DWARF2_FLAG_EPILOGUE_BEGIN)
+ out_opcode (DW_LNS_set_epilogue_begin);
+
+ /* Don't try to optimize away redundant entries; gdb wants two
+ entries for a function where the code starts on the same line as
+ the {, and there's no way to identify that case here. Trust gcc
+ to optimize appropriately. */
+ line_delta = e->loc.line - line;
+ lab = e->label;
+ frag = symbol_get_frag (lab);
+ frag_ofs = S_GET_VALUE (lab);
+
+ if (last_frag == NULL)
+ {
+ out_set_addr (lab);
+ out_inc_line_addr (line_delta, 0);
+ }
+ else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
+ out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
+ else
+ relax_inc_line_addr (line_delta, lab, last_lab);
+
+ line = e->loc.line;
+ last_lab = lab;
+ last_frag = frag;
+ last_frag_ofs = frag_ofs;
+
+ next = e->next;
+ free (e);
+ e = next;
+ }
+ while (e);
+
+ /* Emit a DW_LNE_end_sequence for the end of the section. */
+ frag = last_frag_for_seg (seg);
+ frag_ofs = get_frag_fix (frag, seg);
+ if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
+ out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
+ else
+ {
+ lab = symbol_temp_new (seg, frag_ofs, frag);
+ relax_inc_line_addr (INT_MAX, lab, last_lab);
+ }
+}
+
+/* Emit the directory and file tables for .debug_line. */
+
+static void
+out_file_list (void)
+{
+ size_t size;
+ const char *dir;
+ char *cp;
+ unsigned int i;
+
+ /* Emit directory list. */
+ for (i = 1; i < dirs_in_use; ++i)
+ {
+ dir = remap_debug_filename (dirs[i]);
+ size = strlen (dir) + 1;
+ cp = frag_more (size);
+ memcpy (cp, dir, size);
+ }
+ /* Terminate it. */
+ out_byte ('\0');
+
+ for (i = 1; i < files_in_use; ++i)
+ {
+ const char *fullfilename;
+
+ if (files[i].filename == NULL)
+ {
+ as_bad (_("unassigned file number %ld"), (long) i);
+ /* Prevent a crash later, particularly for file 1. */
+ files[i].filename = "";
+ continue;
+ }
+
+ fullfilename = DWARF2_FILE_NAME (files[i].filename,
+ files[i].dir ? dirs [files [i].dir] : "");
+ size = strlen (fullfilename) + 1;
+ cp = frag_more (size);
+ memcpy (cp, fullfilename, size);
+
+ out_uleb128 (files[i].dir); /* directory number */
+ /* Output the last modification timestamp. */
+ out_uleb128 (DWARF2_FILE_TIME_NAME (files[i].filename,
+ files[i].dir ? dirs [files [i].dir] : ""));
+ /* Output the filesize. */
+ out_uleb128 (DWARF2_FILE_SIZE_NAME (files[i].filename,
+ files[i].dir ? dirs [files [i].dir] : ""));
+ }
+
+ /* Terminate filename list. */
+ out_byte (0);
+}
+
+/* Switch to SEC and output a header length field. Return the size of
+ offsets used in SEC. The caller must set EXPR->X_add_symbol value
+ to the end of the section. EXPR->X_add_number will be set to the
+ negative size of the header. */
+
+static int
+out_header (asection *sec, expressionS *exp)
+{
+ symbolS *start_sym;
+ symbolS *end_sym;
+
+ subseg_set (sec, 0);
+
+ if (flag_dwarf_sections)
+ {
+ /* If we are going to put the start and end symbols in different
+ sections, then we need real symbols, not just fake, local ones. */
+ frag_now_fix ();
+ start_sym = symbol_make (".Ldebug_line_start");
+ end_sym = symbol_make (".Ldebug_line_end");
+ symbol_set_value_now (start_sym);
+ }
+ else
+ {
+ start_sym = symbol_temp_new_now ();
+ end_sym = symbol_temp_make ();
+ }
+
+ /* Total length of the information. */
+ exp->X_op = O_subtract;
+ exp->X_add_symbol = end_sym;
+ exp->X_op_symbol = start_sym;
+
+ switch (DWARF2_FORMAT (sec))
+ {
+ case dwarf2_format_32bit:
+ exp->X_add_number = -4;
+ emit_expr (exp, 4);
+ return 4;
+
+ case dwarf2_format_64bit:
+ exp->X_add_number = -12;
+ out_four (-1);
+ emit_expr (exp, 8);
+ return 8;
+
+ case dwarf2_format_64bit_irix:
+ exp->X_add_number = -8;
+ emit_expr (exp, 8);
+ return 8;
+ }
+
+ as_fatal (_("internal error: unknown dwarf2 format"));
+ return 0;
+}
+
+/* Emit the collected .debug_line data. */
+
+static void
+out_debug_line (segT line_seg)
+{
+ expressionS exp;
+ symbolS *prologue_start, *prologue_end;
+ symbolS *line_end;
+ struct line_seg *s;
+ int sizeof_offset;
+
+ sizeof_offset = out_header (line_seg, &exp);
+ line_end = exp.X_add_symbol;
+
+ /* Version. */
+ out_two (DWARF2_LINE_VERSION);
+
+ /* Length of the prologue following this length. */
+ prologue_start = symbol_temp_make ();
+ prologue_end = symbol_temp_make ();
+ exp.X_op = O_subtract;
+ exp.X_add_symbol = prologue_end;
+ exp.X_op_symbol = prologue_start;
+ exp.X_add_number = 0;
+ emit_expr (&exp, sizeof_offset);
+ symbol_set_value_now (prologue_start);
+
+ /* Parameters of the state machine. */
+ out_byte (DWARF2_LINE_MIN_INSN_LENGTH);
+ out_byte (DWARF2_LINE_DEFAULT_IS_STMT);
+ out_byte (DWARF2_LINE_BASE);
+ out_byte (DWARF2_LINE_RANGE);
+ out_byte (DWARF2_LINE_OPCODE_BASE);
+
+ /* Standard opcode lengths. */
+ out_byte (0); /* DW_LNS_copy */
+ out_byte (1); /* DW_LNS_advance_pc */
+ out_byte (1); /* DW_LNS_advance_line */
+ out_byte (1); /* DW_LNS_set_file */
+ out_byte (1); /* DW_LNS_set_column */
+ out_byte (0); /* DW_LNS_negate_stmt */
+ out_byte (0); /* DW_LNS_set_basic_block */
+ out_byte (0); /* DW_LNS_const_add_pc */
+ out_byte (1); /* DW_LNS_fixed_advance_pc */
+ out_byte (0); /* DW_LNS_set_prologue_end */
+ out_byte (0); /* DW_LNS_set_epilogue_begin */
+ out_byte (1); /* DW_LNS_set_isa */
+
+ out_file_list ();
+
+ symbol_set_value_now (prologue_end);
+
+ /* For each section, emit a statement program. */
+ for (s = all_segs; s; s = s->next)
+ if (SEG_NORMAL (s->seg))
+ process_entries (s->seg, s->head->head);
+ else
+ as_warn ("dwarf line number information for %s ignored",
+ segment_name (s->seg));
+
+ if (flag_dwarf_sections)
+ /* We have to switch to the special .debug_line_end section
+ before emitting the end-of-debug_line symbol. The linker
+ script arranges for this section to be placed after all the
+ (potentially garbage collected) .debug_line.<foo> sections.
+ This section contains the line_end symbol which is used to
+ compute the size of the linked .debug_line section, as seen
+ in the DWARF Line Number header. */
+ subseg_set (subseg_get (".debug_line_end", FALSE), 0);
+
+ symbol_set_value_now (line_end);
+}
+
+static void
+out_debug_ranges (segT ranges_seg)
+{
+ unsigned int addr_size = sizeof_address;
+ struct line_seg *s;
+ expressionS exp;
+ unsigned int i;
+
+ subseg_set (ranges_seg, 0);
+
+ /* Base Address Entry. */
+ for (i = 0; i < addr_size; i++)
+ out_byte (0xff);
+ for (i = 0; i < addr_size; i++)
+ out_byte (0);
+
+ /* Range List Entry. */
+ for (s = all_segs; s; s = s->next)
+ {
+ fragS *frag;
+ symbolS *beg, *end;
+
+ frag = first_frag_for_seg (s->seg);
+ beg = symbol_temp_new (s->seg, 0, frag);
+ s->text_start = beg;
+
+ frag = last_frag_for_seg (s->seg);
+ end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
+ s->text_end = end;
+
+ exp.X_op = O_symbol;
+ exp.X_add_symbol = beg;
+ exp.X_add_number = 0;
+ emit_expr (&exp, addr_size);
+
+ exp.X_op = O_symbol;
+ exp.X_add_symbol = end;
+ exp.X_add_number = 0;
+ emit_expr (&exp, addr_size);
+ }
+
+ /* End of Range Entry. */
+ for (i = 0; i < addr_size; i++)
+ out_byte (0);
+ for (i = 0; i < addr_size; i++)
+ out_byte (0);
+}
+
+/* Emit data for .debug_aranges. */
+
+static void
+out_debug_aranges (segT aranges_seg, segT info_seg)
+{
+ unsigned int addr_size = sizeof_address;
+ offsetT size;
+ struct line_seg *s;
+ expressionS exp;
+ symbolS *aranges_end;
+ char *p;
+ int sizeof_offset;
+
+ sizeof_offset = out_header (aranges_seg, &exp);
+ aranges_end = exp.X_add_symbol;
+ size = -exp.X_add_number;
+
+ /* Version. */
+ out_two (DWARF2_ARANGES_VERSION);
+ size += 2;
+
+ /* Offset to .debug_info. */
+ TC_DWARF2_EMIT_OFFSET (section_symbol (info_seg), sizeof_offset);
+ size += sizeof_offset;
+
+ /* Size of an address (offset portion). */
+ out_byte (addr_size);
+ size++;
+
+ /* Size of a segment descriptor. */
+ out_byte (0);
+ size++;
+
+ /* Align the header. */
+ while ((size++ % (2 * addr_size)) > 0)
+ out_byte (0);
+
+ for (s = all_segs; s; s = s->next)
+ {
+ fragS *frag;
+ symbolS *beg, *end;
+
+ frag = first_frag_for_seg (s->seg);
+ beg = symbol_temp_new (s->seg, 0, frag);
+ s->text_start = beg;
+
+ frag = last_frag_for_seg (s->seg);
+ end = symbol_temp_new (s->seg, get_frag_fix (frag, s->seg), frag);
+ s->text_end = end;
+
+ exp.X_op = O_symbol;
+ exp.X_add_symbol = beg;
+ exp.X_add_number = 0;
+ emit_expr (&exp, addr_size);
+
+ exp.X_op = O_subtract;
+ exp.X_add_symbol = end;
+ exp.X_op_symbol = beg;
+ exp.X_add_number = 0;
+ emit_expr (&exp, addr_size);
+ }
+
+ p = frag_more (2 * addr_size);
+ md_number_to_chars (p, 0, addr_size);
+ md_number_to_chars (p + addr_size, 0, addr_size);
+
+ symbol_set_value_now (aranges_end);
+}
+
+/* Emit data for .debug_abbrev. Note that this must be kept in
+ sync with out_debug_info below. */
+
+static void
+out_debug_abbrev (segT abbrev_seg,
+ segT info_seg ATTRIBUTE_UNUSED,
+ segT line_seg ATTRIBUTE_UNUSED)
+{
+ subseg_set (abbrev_seg, 0);
+
+ out_uleb128 (1);
+ out_uleb128 (DW_TAG_compile_unit);
+ out_byte (DW_CHILDREN_no);
+ if (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit)
+ out_abbrev (DW_AT_stmt_list, DW_FORM_data4);
+ else
+ out_abbrev (DW_AT_stmt_list, DW_FORM_data8);
+ if (all_segs->next == NULL)
+ {
+ out_abbrev (DW_AT_low_pc, DW_FORM_addr);
+ if (DWARF2_VERSION < 4)
+ out_abbrev (DW_AT_high_pc, DW_FORM_addr);
+ else
+ out_abbrev (DW_AT_high_pc, (sizeof_address == 4
+ ? DW_FORM_data4 : DW_FORM_data8));
+ }
+ else
+ {
+ if (DWARF2_FORMAT (info_seg) == dwarf2_format_32bit)
+ out_abbrev (DW_AT_ranges, DW_FORM_data4);
+ else
+ out_abbrev (DW_AT_ranges, DW_FORM_data8);
+ }
+ out_abbrev (DW_AT_name, DW_FORM_string);
+ out_abbrev (DW_AT_comp_dir, DW_FORM_string);
+ out_abbrev (DW_AT_producer, DW_FORM_string);
+ out_abbrev (DW_AT_language, DW_FORM_data2);
+ out_abbrev (0, 0);
+
+ /* Terminate the abbreviations for this compilation unit. */
+ out_byte (0);
+}
+
+/* Emit a description of this compilation unit for .debug_info. */
+
+static void
+out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT ranges_seg)
+{
+ char producer[128];
+ const char *comp_dir;
+ const char *dirname;
+ expressionS exp;
+ symbolS *info_end;
+ char *p;
+ int len;
+ int sizeof_offset;
+
+ sizeof_offset = out_header (info_seg, &exp);
+ info_end = exp.X_add_symbol;
+
+ /* DWARF version. */
+ out_two (DWARF2_VERSION);
+
+ /* .debug_abbrev offset */
+ TC_DWARF2_EMIT_OFFSET (section_symbol (abbrev_seg), sizeof_offset);
+
+ /* Target address size. */
+ out_byte (sizeof_address);
+
+ /* DW_TAG_compile_unit DIE abbrev */
+ out_uleb128 (1);
+
+ /* DW_AT_stmt_list */
+ TC_DWARF2_EMIT_OFFSET (section_symbol (line_seg),
+ (DWARF2_FORMAT (line_seg) == dwarf2_format_32bit
+ ? 4 : 8));
+
+ /* These two attributes are emitted if all of the code is contiguous. */
+ if (all_segs->next == NULL)
+ {
+ /* DW_AT_low_pc */
+ exp.X_op = O_symbol;
+ exp.X_add_symbol = all_segs->text_start;
+ exp.X_add_number = 0;
+ emit_expr (&exp, sizeof_address);
+
+ /* DW_AT_high_pc */
+ if (DWARF2_VERSION < 4)
+ exp.X_op = O_symbol;
+ else
+ {
+ exp.X_op = O_subtract;
+ exp.X_op_symbol = all_segs->text_start;
+ }
+ exp.X_add_symbol = all_segs->text_end;
+ exp.X_add_number = 0;
+ emit_expr (&exp, sizeof_address);
+ }
+ else
+ {
+ /* This attribute is emitted if the code is disjoint. */
+ /* DW_AT_ranges. */
+ TC_DWARF2_EMIT_OFFSET (section_symbol (ranges_seg), sizeof_offset);
+ }
+
+ /* DW_AT_name. We don't have the actual file name that was present
+ on the command line, so assume files[1] is the main input file.
+ We're not supposed to get called unless at least one line number
+ entry was emitted, so this should always be defined. */
+ if (files_in_use == 0)
+ abort ();
+ if (files[1].dir)
+ {
+ dirname = remap_debug_filename (dirs[files[1].dir]);
+ len = strlen (dirname);
+#ifdef TE_VMS
+ /* Already has trailing slash. */
+ p = frag_more (len);
+ memcpy (p, dirname, len);
+#else
+ p = frag_more (len + 1);
+ memcpy (p, dirname, len);
+ INSERT_DIR_SEPARATOR (p, len);
+#endif
+ }
+ len = strlen (files[1].filename) + 1;
+ p = frag_more (len);
+ memcpy (p, files[1].filename, len);
+
+ /* DW_AT_comp_dir */
+ comp_dir = remap_debug_filename (getpwd ());
+ len = strlen (comp_dir) + 1;
+ p = frag_more (len);
+ memcpy (p, comp_dir, len);
+
+ /* DW_AT_producer */
+ sprintf (producer, "GNU AS %s", VERSION);
+ len = strlen (producer) + 1;
+ p = frag_more (len);
+ memcpy (p, producer, len);
+
+ /* DW_AT_language. Yes, this is probably not really MIPS, but the
+ dwarf2 draft has no standard code for assembler. */
+ out_two (DW_LANG_Mips_Assembler);
+
+ symbol_set_value_now (info_end);
+}
+
+void
+dwarf2_init (void)
+{
+ last_seg_ptr = &all_segs;
+}
+
+
+/* Finish the dwarf2 debug sections. We emit .debug.line if there
+ were any .file/.loc directives, or --gdwarf2 was given, or if the
+ file has a non-empty .debug_info section and an empty .debug_line
+ section. If we emit .debug_line, and the .debug_info section is
+ empty, we also emit .debug_info, .debug_aranges and .debug_abbrev.
+ ALL_SEGS will be non-null if there were any .file/.loc directives,
+ or --gdwarf2 was given and there were any located instructions
+ emitted. */
+
+void
+dwarf2_finish (void)
+{
+ segT line_seg;
+ struct line_seg *s;
+ segT info_seg;
+ int emit_other_sections = 0;
+ int empty_debug_line = 0;
+
+ info_seg = bfd_get_section_by_name (stdoutput, ".debug_info");
+ emit_other_sections = info_seg == NULL || !seg_not_empty_p (info_seg);
+
+ line_seg = bfd_get_section_by_name (stdoutput, ".debug_line");
+ empty_debug_line = line_seg == NULL || !seg_not_empty_p (line_seg);
+
+ /* We can't construct a new debug_line section if we already have one.
+ Give an error. */
+ if (all_segs && !empty_debug_line)
+ as_fatal ("duplicate .debug_line sections");
+
+ if ((!all_segs && emit_other_sections)
+ || (!emit_other_sections && !empty_debug_line))
+ /* If there is no line information and no non-empty .debug_info
+ section, or if there is both a non-empty .debug_info and a non-empty
+ .debug_line, then we do nothing. */
+ return;
+
+ /* Calculate the size of an address for the target machine. */
+ sizeof_address = DWARF2_ADDR_SIZE (stdoutput);
+
+ /* Create and switch to the line number section. */
+ line_seg = subseg_new (".debug_line", 0);
+ bfd_set_section_flags (stdoutput, line_seg, SEC_READONLY | SEC_DEBUGGING);
+
+ /* For each subsection, chain the debug entries together. */
+ for (s = all_segs; s; s = s->next)
+ {
+ struct line_subseg *lss = s->head;
+ struct line_entry **ptail = lss->ptail;
+
+ while ((lss = lss->next) != NULL)
+ {
+ *ptail = lss->head;
+ ptail = lss->ptail;
+ }
+ }
+
+ out_debug_line (line_seg);
+
+ /* If this is assembler generated line info, and there is no
+ debug_info already, we need .debug_info and .debug_abbrev
+ sections as well. */
+ if (emit_other_sections)
+ {
+ segT abbrev_seg;
+ segT aranges_seg;
+ segT ranges_seg;
+
+ gas_assert (all_segs);
+
+ info_seg = subseg_new (".debug_info", 0);
+ abbrev_seg = subseg_new (".debug_abbrev", 0);
+ aranges_seg = subseg_new (".debug_aranges", 0);
+
+ bfd_set_section_flags (stdoutput, info_seg,
+ SEC_READONLY | SEC_DEBUGGING);
+ bfd_set_section_flags (stdoutput, abbrev_seg,
+ SEC_READONLY | SEC_DEBUGGING);
+ bfd_set_section_flags (stdoutput, aranges_seg,
+ SEC_READONLY | SEC_DEBUGGING);
+
+ record_alignment (aranges_seg, ffs (2 * sizeof_address) - 1);
+
+ if (all_segs->next == NULL)
+ ranges_seg = NULL;
+ else
+ {
+ ranges_seg = subseg_new (".debug_ranges", 0);
+ bfd_set_section_flags (stdoutput, ranges_seg,
+ SEC_READONLY | SEC_DEBUGGING);
+ record_alignment (ranges_seg, ffs (2 * sizeof_address) - 1);
+ out_debug_ranges (ranges_seg);
+ }
+
+ out_debug_aranges (aranges_seg, info_seg);
+ out_debug_abbrev (abbrev_seg, info_seg, line_seg);
+ out_debug_info (info_seg, abbrev_seg, line_seg, ranges_seg);