+
+/* Generate stabs debugging information to denote the main source file. */
+
+void
+stabs_generate_asm_file (void)
+{
+ const char *file;
+ unsigned int lineno;
+
+ file = as_where (&lineno);
+ if (use_gnu_debug_info_extensions)
+ {
+ const char *dir;
+ char *dir2;
+
+ dir = remap_debug_filename (getpwd ());
+ dir2 = concat (dir, "/", NULL);
+ generate_asm_file (N_SO, dir2);
+ free (dir2);
+ xfree ((char *) dir);
+ }
+ generate_asm_file (N_SO, file);
+}
+
+/* Generate stabs debugging information to denote the source file.
+ TYPE is one of N_SO, N_SOL. */
+
+static void
+generate_asm_file (int type, const char *file)
+{
+ static char *last_file;
+ static int label_count;
+ char sym[30];
+ char *buf;
+ const char *tmp = file;
+ const char *file_endp = file + strlen (file);
+ char *bufp;
+
+ if (last_file != NULL
+ && filename_cmp (last_file, file) == 0)
+ return;
+
+ /* Rather than try to do this in some efficient fashion, we just
+ generate a string and then parse it again. That lets us use the
+ existing stabs hook, which expect to see a string, rather than
+ inventing new ones. */
+ sprintf (sym, "%sF%d", FAKE_LABEL_NAME, label_count);
+ ++label_count;
+
+ /* Allocate enough space for the file name (possibly extended with
+ doubled up backslashes), the symbol name, and the other characters
+ that make up a stabs file directive. */
+ bufp = buf = XNEWVEC (char, 2 * strlen (file) + strlen (sym) + 12);
+
+ *bufp++ = '"';
+
+ while (tmp < file_endp)
+ {
+ const char *bslash = strchr (tmp, '\\');
+ size_t len = bslash != NULL ? bslash - tmp + 1 : file_endp - tmp;
+
+ /* Double all backslashes, since demand_copy_C_string (used by
+ s_stab to extract the part in quotes) will try to replace them as
+ escape sequences. backslash may appear in a filespec. */
+ memcpy (bufp, tmp, len);
+
+ tmp += len;
+ bufp += len;
+
+ if (bslash != NULL)
+ *bufp++ = '\\';
+ }
+
+ sprintf (bufp, "\",%d,0,0,%s\n", type, sym);
+
+ temp_ilp (buf);
+ s_stab ('s');
+ restore_ilp ();
+
+ colon (sym);
+
+ if (last_file != NULL)
+ free (last_file);
+ last_file = xstrdup (file);
+
+ free (buf);
+}
+
+/* Generate stabs debugging information for the current line. This is
+ used to produce debugging information for an assembler file. */
+
+void
+stabs_generate_asm_lineno (void)
+{
+ static int label_count;
+ const char *file;
+ unsigned int lineno;
+ char *buf;
+ char sym[30];
+ /* Remember the last file/line and avoid duplicates. */
+ static unsigned int prev_lineno = -1;
+ static char *prev_file = NULL;
+
+ /* Rather than try to do this in some efficient fashion, we just
+ generate a string and then parse it again. That lets us use the
+ existing stabs hook, which expect to see a string, rather than
+ inventing new ones. */
+
+ file = as_where (&lineno);
+
+ /* Don't emit sequences of stabs for the same line. */
+ if (prev_file == NULL)
+ {
+ /* First time through. */
+ prev_file = xstrdup (file);
+ prev_lineno = lineno;
+ }
+ else if (lineno == prev_lineno
+ && filename_cmp (file, prev_file) == 0)
+ {
+ /* Same file/line as last time. */
+ return;
+ }
+ else
+ {
+ /* Remember file/line for next time. */
+ prev_lineno = lineno;
+ if (filename_cmp (file, prev_file) != 0)
+ {
+ free (prev_file);
+ prev_file = xstrdup (file);
+ }
+ }
+
+ /* Let the world know that we are in the middle of generating a
+ piece of stabs line debugging information. */
+ outputting_stabs_line_debug = 1;
+
+ generate_asm_file (N_SOL, file);
+
+ sprintf (sym, "%sL%d", FAKE_LABEL_NAME, label_count);
+ ++label_count;
+
+ if (in_dot_func_p)
+ {
+ buf = XNEWVEC (char, 100 + strlen (current_function_label));
+ sprintf (buf, "%d,0,%d,%s-%s\n", N_SLINE, lineno,
+ sym, current_function_label);
+ }
+ else
+ {
+ buf = XNEWVEC (char, 100);
+ sprintf (buf, "%d,0,%d,%s\n", N_SLINE, lineno, sym);
+ }
+
+ temp_ilp (buf);
+ s_stab ('n');
+ restore_ilp ();
+
+ colon (sym);
+
+ outputting_stabs_line_debug = 0;
+ free (buf);
+}
+
+/* Emit a function stab.
+ All assembler functions are assumed to have return type `void'. */
+
+void
+stabs_generate_asm_func (const char *funcname, const char *startlabname)
+{
+ static bfd_boolean void_emitted_p = FALSE;
+ char *buf;
+ unsigned int lineno;
+
+ if (! void_emitted_p)
+ {
+ temp_ilp ((char *) "\"void:t1=1\",128,0,0,0");
+ s_stab ('s');
+ restore_ilp ();
+ void_emitted_p = TRUE;
+ }
+
+ as_where (&lineno);
+ if (asprintf (&buf, "\"%s:F1\",%d,0,%d,%s",
+ funcname, N_FUN, lineno + 1, startlabname) == -1)
+ as_fatal ("%s", xstrerror (errno));
+
+ temp_ilp (buf);
+ s_stab ('s');
+ restore_ilp ();
+ free (buf);
+
+ current_function_label = xstrdup (startlabname);
+ in_dot_func_p = TRUE;
+}
+
+/* Emit a stab to record the end of a function. */
+
+void
+stabs_generate_asm_endfunc (const char *funcname ATTRIBUTE_UNUSED,
+ const char *startlabname)
+{
+ static int label_count;
+ char *buf;
+ char sym[30];
+
+ sprintf (sym, "%sendfunc%d", FAKE_LABEL_NAME, label_count);
+ ++label_count;
+ colon (sym);
+
+ if (asprintf (&buf, "\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname) == -1)
+ as_fatal ("%s", xstrerror (errno));
+
+ temp_ilp (buf);
+ s_stab ('s');
+ restore_ilp ();
+ free (buf);
+
+ in_dot_func_p = FALSE;
+ current_function_label = NULL;
+}