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