/* Generic stabs parsing for gas.
Copyright (C) 1989-2024 Free Software Foundation, Inc.
This file is part of GAS, the GNU Assembler.
GAS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3,
or (at your option) any later version.
GAS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GAS; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
/* Label at start of current function if we're in the middle of a
.func function, in which case stabs_generate_asm_lineno emits
function relative line number stabs. Otherwise it emits line
number stabs with absolute addresses. Note that both cases only
apply to assembler code assembled with -gstabs. */
static const char *current_function_label;
/* State used by generate_asm_file. */
static char *last_asm_file;
static int file_label_count;
/* State used by stabs_generate_asm_lineno. */
static int line_label_count;
static unsigned int prev_lineno;
static char *prev_line_file;
/* State used by stabs_generate_asm_func. */
static bool void_emitted_p;
/* State used by stabs_generate_asm_endfunc. */
static int endfunc_label_count;
/*
* Handle .stabX directives, which used to be open-coded.
* So much creeping featurism overloaded the semantics that we decided
* to put all .stabX thinking in one place. Here.
*
* We try to make any .stabX directive legal. Other people's AS will often
* do assembly-time consistency checks: eg assigning meaning to n_type bits
* and "protecting" you from setting them to certain values. (They also zero
* certain bits before emitting symbols. Tut tut.)
*
* If an expression is not absolute we either gripe or use the relocation
* information. Other people's assemblers silently forget information they
* don't need and invent information they need that you didn't supply.
*/
/*
* Build a string dictionary entry for a .stabX symbol.
* The symbol is added to the .<secname>str section.
*/
/* Here instead of obj-aout.c because other formats use it too. */
void
aout_process_stab (int what, const char *string, int type, int other, int desc)
{
/* Put the stab information in the symbol table. */
symbolS *symbol;
/* Create the symbol now, but only insert it into the symbol chain
after any symbols mentioned in the value expression get into the
symbol chain. This is to avoid "continuation symbols" (where one
ends in "\" and the debug info is continued in the next .stabs
directive) from being separated by other random symbols. */
symbol = symbol_create (string, undefined_section, &zero_address_frag, 0);
if (what == 's' || what == 'n')
{
/* Pick up the value from the input line. */
pseudo_set (symbol);
}
else
{
/* .stabd sets the name to NULL. Why? */
S_SET_NAME (symbol, NULL);
symbol_set_frag (symbol, frag_now);
S_SET_VALUE (symbol, (valueT) frag_now_fix ());
}
/* This can handle different kinds of stabs (s,n,d) and different
kinds of stab sections. If FREENAMES is true, then STAB_SECNAME
and STABSTR_SECNAME are allocated in that order on the notes
obstack and will be freed if possible. */
/* The general format is:
.stabs "STRING",TYPE,OTHER,DESC,VALUE
.stabn TYPE,OTHER,DESC,VALUE
.stabd TYPE,OTHER,DESC
At this point input_line_pointer points after the pseudo-op and
any trailing whitespace. The argument what is one of 's', 'n' or
'd' indicating which type of .stab this is. */
saved_string_obstack_end = NULL;
if (what != 's')
string = "";
else
{
int length;
string = demand_copy_C_string (&length);
if (string == NULL)
{
as_warn (_(".stab%c: missing string"), what);
ignore_rest_of_line ();
goto out2;
}
/* FIXME: We should probably find some other temporary storage
for string, rather than leaking memory if someone else
happens to use the notes obstack. */
saved_string_obstack_end = obstack_next_free (¬es);
SKIP_WHITESPACE ();
if (!eat_comma (what))
goto out;
}
type = get_absolute_expression ();
if (!eat_comma (what))
goto out;
other = get_absolute_expression ();
if (!eat_comma (what))
goto out;
desc = get_absolute_expression ();
if ((desc > 0xffff) || (desc < -0x8000))
/* This could happen for example with a source file with a huge
number of lines. The only cure is to use a different debug
format, probably DWARF. */
as_warn (_(".stab%c: description field '%x' too big, try a different debug format"),
what, desc);
if (what == 's' || what == 'n')
{
if (!eat_comma (what))
goto out;
SKIP_WHITESPACE ();
}
#ifdef TC_PPC
#ifdef OBJ_ELF
/* Solaris on PowerPC has decided that .stabd can take 4 arguments, so if we were
given 4 arguments, make it a .stabn */
else if (what == 'd')
{
char *save_location = input_line_pointer;
#ifndef NO_LISTING
if (listing)
{
switch (type)
{
case N_SLINE:
listing_source_line ((unsigned int) desc);
break;
case N_SO:
case N_SOL:
listing_source_file (string);
break;
}
}
#endif /* ! NO_LISTING */
/* We have now gathered the type, other, and desc information. For
.stabs or .stabn, input_line_pointer is now pointing at the
value. */
if (SEPARATE_STAB_SECTIONS)
/* Output the stab information in a separate section. This is used
at least for COFF and ELF. */
{
unsigned int stroff;
char *p;
/* Release the string, if nobody else has used the obstack.
This must be done before creating symbols below, which uses
the notes obstack. */
if (saved_string_obstack_end == obstack_next_free (¬es))
{
obstack_free (¬es, string);
saved_string_obstack_end = NULL;
}
/* At least for now, stabs in a special stab section are always
output as 12 byte blocks of information. */
p = frag_more (8);
md_number_to_chars (p, (valueT) stroff, 4);
md_number_to_chars (p + 4, (valueT) type, 1);
md_number_to_chars (p + 5, (valueT) other, 1);
md_number_to_chars (p + 6, (valueT) desc, 2);
if (what == 's' || what == 'n')
{
/* Pick up the value from the input line. */
cons (4);
input_line_pointer--;
}
else
{
symbolS *symbol;
expressionS exp;
/* Arrange for a value representing the current location. */
symbol = symbol_temp_new (saved_seg, saved_frag, dot);
/* 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, file_label_count);
++file_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);
/* 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);
/* 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_line_file != NULL
&& filename_cmp (file, prev_line_file) == 0)
{
if (lineno == prev_lineno)
/* Same file/line as last time. */
return;
}
else
{
/* Remember file/line for next time. */
free (prev_line_file);
prev_line_file = xstrdup (file);
}
prev_lineno = lineno;
/* Let the world know that we are in the middle of generating a
piece of stabs line debugging information. */
outputting_stabs_line_debug = 1;