xref: /openbsd/gnu/usr.bin/binutils-2.17/gas/stabs.c (revision d89ec533)
1 /* Generic stabs parsing for gas.
2    Copyright 1989, 1990, 1991, 1993, 1995, 1996, 1997, 1998, 2000, 2001
3    2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 
5 This file is part of GAS, the GNU Assembler.
6 
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
11 
12 GAS is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15 the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21 
22 #include "as.h"
23 #include "obstack.h"
24 #include "subsegs.h"
25 #include "ecoff.h"
26 
27 /* We need this, despite the apparent object format dependency, since
28    it defines stab types, which all object formats can use now.  */
29 
30 #include "aout/stab_gnu.h"
31 
32 /* Holds whether the assembler is generating stabs line debugging
33    information or not.  Potentially used by md_cleanup function.  */
34 
35 int outputting_stabs_line_debug = 0;
36 
37 static void s_stab_generic (int, char *, char *);
38 static void generate_asm_file (int, char *);
39 
40 /* Allow backends to override the names used for the stab sections.  */
41 #ifndef STAB_SECTION_NAME
42 #define STAB_SECTION_NAME ".stab"
43 #endif
44 
45 #ifndef STAB_STRING_SECTION_NAME
46 #define STAB_STRING_SECTION_NAME ".stabstr"
47 #endif
48 
49 /* Non-zero if we're in the middle of a .func function, in which case
50    stabs_generate_asm_lineno emits function relative line number stabs.
51    Otherwise it emits line number stabs with absolute addresses.  Note that
52    both cases only apply to assembler code assembled with -gstabs.  */
53 static int in_dot_func_p;
54 
55 /* Label at start of current function if in_dot_func_p != 0.  */
56 static const char *current_function_label;
57 
58 /*
59  * Handle .stabX directives, which used to be open-coded.
60  * So much creeping featurism overloaded the semantics that we decided
61  * to put all .stabX thinking in one place. Here.
62  *
63  * We try to make any .stabX directive legal. Other people's AS will often
64  * do assembly-time consistency checks: eg assigning meaning to n_type bits
65  * and "protecting" you from setting them to certain values. (They also zero
66  * certain bits before emitting symbols. Tut tut.)
67  *
68  * If an expression is not absolute we either gripe or use the relocation
69  * information. Other people's assemblers silently forget information they
70  * don't need and invent information they need that you didn't supply.
71  */
72 
73 /*
74  * Build a string dictionary entry for a .stabX symbol.
75  * The symbol is added to the .<secname>str section.
76  */
77 
78 #ifndef SEPARATE_STAB_SECTIONS
79 #define SEPARATE_STAB_SECTIONS 0
80 #endif
81 
82 unsigned int
83 get_stab_string_offset (const char *string, const char *stabstr_secname)
84 {
85   unsigned int length;
86   unsigned int retval;
87   segT save_seg;
88   subsegT save_subseg;
89   segT seg;
90   char *p;
91 
92   if (! SEPARATE_STAB_SECTIONS)
93     abort ();
94 
95   length = strlen (string);
96 
97   save_seg = now_seg;
98   save_subseg = now_subseg;
99 
100   /* Create the stab string section.  */
101   seg = subseg_new (stabstr_secname, 0);
102 
103   retval = seg_info (seg)->stabu.stab_string_size;
104   if (retval <= 0)
105     {
106       /* Make sure the first string is empty.  */
107       p = frag_more (1);
108       *p = 0;
109       retval = seg_info (seg)->stabu.stab_string_size = 1;
110       bfd_set_section_flags (stdoutput, seg, SEC_READONLY | SEC_DEBUGGING);
111       if (seg->name == stabstr_secname)
112 	seg->name = xstrdup (stabstr_secname);
113     }
114 
115   if (length > 0)
116     {				/* Ordinary case.  */
117       p = frag_more (length + 1);
118       strcpy (p, string);
119 
120       seg_info (seg)->stabu.stab_string_size += length + 1;
121     }
122   else
123     retval = 0;
124 
125   subseg_set (save_seg, save_subseg);
126 
127   return retval;
128 }
129 
130 #ifdef AOUT_STABS
131 #ifndef OBJ_PROCESS_STAB
132 #define OBJ_PROCESS_STAB(SEG,W,S,T,O,D)	aout_process_stab(W,S,T,O,D)
133 #endif
134 
135 /* Here instead of obj-aout.c because other formats use it too.  */
136 void
137 aout_process_stab (what, string, type, other, desc)
138      int what;
139      const char *string;
140      int type, other, desc;
141 {
142   /* Put the stab information in the symbol table.  */
143   symbolS *symbol;
144 
145   /* Create the symbol now, but only insert it into the symbol chain
146      after any symbols mentioned in the value expression get into the
147      symbol chain.  This is to avoid "continuation symbols" (where one
148      ends in "\" and the debug info is continued in the next .stabs
149      directive) from being separated by other random symbols.  */
150   symbol = symbol_create (string, undefined_section, 0,
151 			  &zero_address_frag);
152   if (what == 's' || what == 'n')
153     {
154       /* Pick up the value from the input line.  */
155       pseudo_set (symbol);
156     }
157   else
158     {
159       /* .stabd sets the name to NULL.  Why?  */
160       S_SET_NAME (symbol, NULL);
161       symbol_set_frag (symbol, frag_now);
162       S_SET_VALUE (symbol, (valueT) frag_now_fix ());
163     }
164 
165   symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
166 
167   S_SET_TYPE (symbol, type);
168   S_SET_OTHER (symbol, other);
169   S_SET_DESC (symbol, desc);
170 }
171 #endif
172 
173 /* This can handle different kinds of stabs (s,n,d) and different
174    kinds of stab sections.  */
175 
176 static void
177 s_stab_generic (int what, char *stab_secname, char *stabstr_secname)
178 {
179   long longint;
180   char *string, *saved_string_obstack_end;
181   int type;
182   int other;
183   int desc;
184 
185   /* The general format is:
186      .stabs "STRING",TYPE,OTHER,DESC,VALUE
187      .stabn TYPE,OTHER,DESC,VALUE
188      .stabd TYPE,OTHER,DESC
189      At this point input_line_pointer points after the pseudo-op and
190      any trailing whitespace.  The argument what is one of 's', 'n' or
191      'd' indicating which type of .stab this is.  */
192 
193   if (what != 's')
194     {
195       string = "";
196       saved_string_obstack_end = 0;
197     }
198   else
199     {
200       int length;
201 
202       string = demand_copy_C_string (&length);
203       /* FIXME: We should probably find some other temporary storage
204 	 for string, rather than leaking memory if someone else
205 	 happens to use the notes obstack.  */
206       saved_string_obstack_end = notes.next_free;
207       SKIP_WHITESPACE ();
208       if (*input_line_pointer == ',')
209 	input_line_pointer++;
210       else
211 	{
212 	  as_warn (_(".stab%c: missing comma"), what);
213 	  ignore_rest_of_line ();
214 	  return;
215 	}
216     }
217 
218   if (get_absolute_expression_and_terminator (&longint) != ',')
219     {
220       as_warn (_(".stab%c: missing comma"), what);
221       ignore_rest_of_line ();
222       return;
223     }
224   type = longint;
225 
226   if (get_absolute_expression_and_terminator (&longint) != ',')
227     {
228       as_warn (_(".stab%c: missing comma"), what);
229       ignore_rest_of_line ();
230       return;
231     }
232   other = longint;
233 
234   desc = get_absolute_expression ();
235 
236   if ((desc > 0xffff) || (desc < -0x8000))
237     /* This could happen for example with a source file with a huge
238        number of lines.  The only cure is to use a different debug
239        format, probably DWARF.  */
240     as_warn (_(".stab%c: description field '%x' too big, try a different debug format"),
241 	     what, desc);
242 
243   if (what == 's' || what == 'n')
244     {
245       if (*input_line_pointer != ',')
246 	{
247 	  as_warn (_(".stab%c: missing comma"), what);
248 	  ignore_rest_of_line ();
249 	  return;
250 	}
251       input_line_pointer++;
252       SKIP_WHITESPACE ();
253     }
254 
255 #ifdef TC_PPC
256 #ifdef OBJ_ELF
257   /* Solaris on PowerPC has decided that .stabd can take 4 arguments, so if we were
258      given 4 arguments, make it a .stabn */
259   else if (what == 'd')
260     {
261       char *save_location = input_line_pointer;
262 
263       SKIP_WHITESPACE ();
264       if (*input_line_pointer == ',')
265 	{
266 	  input_line_pointer++;
267 	  what = 'n';
268 	}
269       else
270 	input_line_pointer = save_location;
271     }
272 #endif /* OBJ_ELF */
273 #endif /* TC_PPC */
274 
275 #ifndef NO_LISTING
276   if (listing)
277     {
278       switch (type)
279 	{
280 	case N_SLINE:
281 	  listing_source_line ((unsigned int) desc);
282 	  break;
283 	case N_SO:
284 	case N_SOL:
285 	  listing_source_file (string);
286 	  break;
287 	}
288     }
289 #endif /* ! NO_LISTING */
290 
291   /* We have now gathered the type, other, and desc information.  For
292      .stabs or .stabn, input_line_pointer is now pointing at the
293      value.  */
294 
295   if (SEPARATE_STAB_SECTIONS)
296     /* Output the stab information in a separate section.  This is used
297        at least for COFF and ELF.  */
298     {
299       segT saved_seg = now_seg;
300       subsegT saved_subseg = now_subseg;
301       fragS *saved_frag = frag_now;
302       valueT dot;
303       segT seg;
304       unsigned int stroff;
305       char *p;
306 
307       static segT cached_sec;
308       static char *cached_secname;
309 
310       dot = frag_now_fix ();
311 
312 #ifdef md_flush_pending_output
313       md_flush_pending_output ();
314 #endif
315 
316       if (cached_secname && !strcmp (cached_secname, stab_secname))
317 	{
318 	  seg = cached_sec;
319 	  subseg_set (seg, 0);
320 	}
321       else
322 	{
323 	  seg = subseg_new (stab_secname, 0);
324 	  if (cached_secname)
325 	    free (cached_secname);
326 	  cached_secname = xstrdup (stab_secname);
327 	  cached_sec = seg;
328 	}
329 
330       if (! seg_info (seg)->hadone)
331 	{
332 	  bfd_set_section_flags (stdoutput, seg,
333 				 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
334 #ifdef INIT_STAB_SECTION
335 	  INIT_STAB_SECTION (seg);
336 #endif
337 	  seg_info (seg)->hadone = 1;
338 	}
339 
340       stroff = get_stab_string_offset (string, stabstr_secname);
341       if (what == 's')
342 	{
343 	  /* Release the string, if nobody else has used the obstack.  */
344 	  if (saved_string_obstack_end == notes.next_free)
345 	    obstack_free (&notes, string);
346 	}
347 
348       /* At least for now, stabs in a special stab section are always
349 	 output as 12 byte blocks of information.  */
350       p = frag_more (8);
351       md_number_to_chars (p, (valueT) stroff, 4);
352       md_number_to_chars (p + 4, (valueT) type, 1);
353       md_number_to_chars (p + 5, (valueT) other, 1);
354       md_number_to_chars (p + 6, (valueT) desc, 2);
355 
356       if (what == 's' || what == 'n')
357 	{
358 	  /* Pick up the value from the input line.  */
359 	  cons (4);
360 	  input_line_pointer--;
361 	}
362       else
363 	{
364 	  symbolS *symbol;
365 	  expressionS exp;
366 
367 	  /* Arrange for a value representing the current location.  */
368 	  symbol = symbol_temp_new (saved_seg, dot, saved_frag);
369 
370 	  exp.X_op = O_symbol;
371 	  exp.X_add_symbol = symbol;
372 	  exp.X_add_number = 0;
373 
374 	  emit_expr (&exp, 4);
375 	}
376 
377 #ifdef OBJ_PROCESS_STAB
378       OBJ_PROCESS_STAB (seg, what, string, type, other, desc);
379 #endif
380 
381       subseg_set (saved_seg, saved_subseg);
382     }
383   else
384     {
385 #ifdef OBJ_PROCESS_STAB
386       OBJ_PROCESS_STAB (0, what, string, type, other, desc);
387 #else
388       abort ();
389 #endif
390     }
391 
392   demand_empty_rest_of_line ();
393 }
394 
395 /* Regular stab directive.  */
396 
397 void
398 s_stab (int what)
399 {
400   s_stab_generic (what, STAB_SECTION_NAME, STAB_STRING_SECTION_NAME);
401 }
402 
403 /* "Extended stabs", used in Solaris only now.  */
404 
405 void
406 s_xstab (int what)
407 {
408   int length;
409   char *stab_secname, *stabstr_secname;
410   static char *saved_secname, *saved_strsecname;
411 
412   /* @@ MEMORY LEAK: This allocates a copy of the string, but in most
413      cases it will be the same string, so we could release the storage
414      back to the obstack it came from.  */
415   stab_secname = demand_copy_C_string (&length);
416   SKIP_WHITESPACE ();
417   if (*input_line_pointer == ',')
418     input_line_pointer++;
419   else
420     {
421       as_bad (_("comma missing in .xstabs"));
422       ignore_rest_of_line ();
423       return;
424     }
425 
426   /* To get the name of the stab string section, simply add "str" to
427      the stab section name.  */
428   if (saved_secname == 0 || strcmp (saved_secname, stab_secname))
429     {
430       stabstr_secname = (char *) xmalloc (strlen (stab_secname) + 4);
431       strcpy (stabstr_secname, stab_secname);
432       strcat (stabstr_secname, "str");
433       if (saved_secname)
434 	{
435 	  free (saved_secname);
436 	  free (saved_strsecname);
437 	}
438       saved_secname = stab_secname;
439       saved_strsecname = stabstr_secname;
440     }
441   s_stab_generic (what, saved_secname, saved_strsecname);
442 }
443 
444 #ifdef S_SET_DESC
445 
446 /* Frob invented at RMS' request. Set the n_desc of a symbol.  */
447 
448 void
449 s_desc (ignore)
450      int ignore ATTRIBUTE_UNUSED;
451 {
452   char *name;
453   char c;
454   char *p;
455   symbolS *symbolP;
456   int temp;
457 
458   name = input_line_pointer;
459   c = get_symbol_end ();
460   p = input_line_pointer;
461   *p = c;
462   SKIP_WHITESPACE ();
463   if (*input_line_pointer != ',')
464     {
465       *p = 0;
466       as_bad (_("expected comma after \"%s\""), name);
467       *p = c;
468       ignore_rest_of_line ();
469     }
470   else
471     {
472       input_line_pointer++;
473       temp = get_absolute_expression ();
474       *p = 0;
475       symbolP = symbol_find_or_make (name);
476       *p = c;
477       S_SET_DESC (symbolP, temp);
478     }
479   demand_empty_rest_of_line ();
480 }				/* s_desc() */
481 
482 #endif /* defined (S_SET_DESC) */
483 
484 /* Generate stabs debugging information to denote the main source file.  */
485 
486 void
487 stabs_generate_asm_file (void)
488 {
489   char *file;
490   unsigned int lineno;
491 
492   as_where (&file, &lineno);
493   if (use_gnu_debug_info_extensions)
494     {
495       char *dir, *dir2;
496 
497       dir = getpwd ();
498       dir2 = alloca (strlen (dir) + 2);
499       sprintf (dir2, "%s%s", dir, "/");
500       generate_asm_file (N_SO, dir2);
501     }
502   generate_asm_file (N_SO, file);
503 }
504 
505 /* Generate stabs debugging information to denote the source file.
506    TYPE is one of N_SO, N_SOL.  */
507 
508 static void
509 generate_asm_file (int type, char *file)
510 {
511   static char *last_file;
512   static int label_count;
513   char *hold;
514   char sym[30];
515   char *buf;
516   char *tmp = file;
517   char *endp = file + strlen (file);
518   char *bufp;
519 
520   if (last_file != NULL
521       && strcmp (last_file, file) == 0)
522     return;
523 
524   /* Rather than try to do this in some efficient fashion, we just
525      generate a string and then parse it again.  That lets us use the
526      existing stabs hook, which expect to see a string, rather than
527      inventing new ones.  */
528   hold = input_line_pointer;
529 
530   sprintf (sym, "%sF%d", FAKE_LABEL_NAME, label_count);
531   ++label_count;
532 
533   /* Allocate enough space for the file name (possibly extended with
534      doubled up backslashes), the symbol name, and the other characters
535      that make up a stabs file directive.  */
536   bufp = buf = xmalloc (2 * strlen (file) + strlen (sym) + 12);
537 
538   *bufp++ = '"';
539 
540   while (tmp < endp)
541     {
542       char *bslash = strchr (tmp, '\\');
543       size_t len = (bslash) ? (size_t) (bslash - tmp + 1) : strlen (tmp);
544 
545       /* Double all backslashes, since demand_copy_C_string (used by
546 	 s_stab to extract the part in quotes) will try to replace them as
547 	 escape sequences.  backslash may appear in a filespec.  */
548       strncpy (bufp, tmp, len);
549 
550       tmp += len;
551       bufp += len;
552 
553       if (bslash != NULL)
554 	*bufp++ = '\\';
555     }
556 
557   sprintf (bufp, "\",%d,0,0,%s\n", type, sym);
558 
559   input_line_pointer = buf;
560   s_stab ('s');
561   colon (sym);
562 
563   if (last_file != NULL)
564     free (last_file);
565   last_file = xstrdup (file);
566 
567   free (buf);
568 
569   input_line_pointer = hold;
570 }
571 
572 /* Generate stabs debugging information for the current line.  This is
573    used to produce debugging information for an assembler file.  */
574 
575 void
576 stabs_generate_asm_lineno (void)
577 {
578   static int label_count;
579   char *hold;
580   char *file;
581   unsigned int lineno;
582   char *buf;
583   char sym[30];
584   /* Remember the last file/line and avoid duplicates.  */
585   static unsigned int prev_lineno = -1;
586   static char *prev_file = NULL;
587 
588   /* Rather than try to do this in some efficient fashion, we just
589      generate a string and then parse it again.  That lets us use the
590      existing stabs hook, which expect to see a string, rather than
591      inventing new ones.  */
592 
593   hold = input_line_pointer;
594 
595   as_where (&file, &lineno);
596 
597   /* Don't emit sequences of stabs for the same line.  */
598   if (prev_file == NULL)
599     {
600       /* First time thru.  */
601       prev_file = xstrdup (file);
602       prev_lineno = lineno;
603     }
604   else if (lineno == prev_lineno
605 	   && strcmp (file, prev_file) == 0)
606     {
607       /* Same file/line as last time.  */
608       return;
609     }
610   else
611     {
612       /* Remember file/line for next time.  */
613       prev_lineno = lineno;
614       if (strcmp (file, prev_file) != 0)
615 	{
616 	  free (prev_file);
617 	  prev_file = xstrdup (file);
618 	}
619     }
620 
621   /* Let the world know that we are in the middle of generating a
622      piece of stabs line debugging information.  */
623   outputting_stabs_line_debug = 1;
624 
625   generate_asm_file (N_SOL, file);
626 
627   sprintf (sym, "%sL%d", FAKE_LABEL_NAME, label_count);
628   ++label_count;
629 
630   if (in_dot_func_p)
631     {
632       buf = (char *) alloca (100 + strlen (current_function_label));
633       sprintf (buf, "%d,0,%d,%s-%s\n", N_SLINE, lineno,
634 	       sym, current_function_label);
635     }
636   else
637     {
638       buf = (char *) alloca (100);
639       sprintf (buf, "%d,0,%d,%s\n", N_SLINE, lineno, sym);
640     }
641   input_line_pointer = buf;
642   s_stab ('n');
643   colon (sym);
644 
645   input_line_pointer = hold;
646   outputting_stabs_line_debug = 0;
647 }
648 
649 /* Emit a function stab.
650    All assembler functions are assumed to have return type `void'.  */
651 
652 void
653 stabs_generate_asm_func (const char *funcname, const char *startlabname)
654 {
655   static int void_emitted_p;
656   char *hold = input_line_pointer;
657   char *buf;
658   char *file;
659   unsigned int lineno;
660 
661   if (! void_emitted_p)
662     {
663       input_line_pointer = "\"void:t1=1\",128,0,0,0";
664       s_stab ('s');
665       void_emitted_p = 1;
666     }
667 
668   as_where (&file, &lineno);
669   asprintf (&buf, "\"%s:F1\",%d,0,%d,%s",
670 	    funcname, N_FUN, lineno + 1, startlabname);
671   input_line_pointer = buf;
672   s_stab ('s');
673   free (buf);
674 
675   input_line_pointer = hold;
676   current_function_label = xstrdup (startlabname);
677   in_dot_func_p = 1;
678 }
679 
680 /* Emit a stab to record the end of a function.  */
681 
682 void
683 stabs_generate_asm_endfunc (const char *funcname ATTRIBUTE_UNUSED,
684 			    const char *startlabname)
685 {
686   static int label_count;
687   char *hold = input_line_pointer;
688   char *buf;
689   char sym[30];
690 
691   sprintf (sym, "%sendfunc%d", FAKE_LABEL_NAME, label_count);
692   ++label_count;
693   colon (sym);
694 
695   asprintf (&buf, "\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname);
696   input_line_pointer = buf;
697   s_stab ('s');
698   free (buf);
699 
700   input_line_pointer = hold;
701   in_dot_func_p = 0;
702   current_function_label = NULL;
703 }
704