]>
Commit | Line | Data |
---|---|---|
5a52c5f8 ILT |
1 | /* wrstabs.c -- Output stabs debugging information |
2 | Copyright (C) 1996 Free Software Foundation, Inc. | |
3 | Written by Ian Lance Taylor <[email protected]>. | |
4 | ||
5 | This file is part of GNU Binutils. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | /* This file contains code which writes out stabs debugging | |
23 | information. */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <ctype.h> | |
27 | #include <assert.h> | |
28 | ||
29 | #include "bfd.h" | |
30 | #include "bucomm.h" | |
31 | #include "libiberty.h" | |
32 | #include "debug.h" | |
33 | #include "budbg.h" | |
34 | ||
35 | /* Meaningless definition needs by aout64.h. FIXME. */ | |
36 | #define BYTES_IN_WORD 4 | |
37 | ||
38 | #include "aout/aout64.h" | |
39 | #include "aout/stab_gnu.h" | |
40 | ||
41 | /* The size of a stabs symbol. This presumes 32 bit values. */ | |
42 | ||
43 | #define STAB_SYMBOL_SIZE (12) | |
44 | ||
45 | /* An entry in a string hash table. */ | |
46 | ||
47 | struct string_hash_entry | |
48 | { | |
49 | struct bfd_hash_entry root; | |
50 | /* Index in string table. */ | |
51 | long index; | |
52 | /* Size of type if this is a typedef. */ | |
53 | unsigned int size; | |
54 | }; | |
55 | ||
56 | /* A string hash table. */ | |
57 | ||
58 | struct string_hash_table | |
59 | { | |
60 | struct bfd_hash_table table; | |
61 | }; | |
62 | ||
63 | /* The type stack. Each element on the stack is a string. */ | |
64 | ||
65 | struct stab_type_stack | |
66 | { | |
67 | /* The next element on the stack. */ | |
68 | struct stab_type_stack *next; | |
69 | /* This element as a string. */ | |
70 | char *string; | |
71 | /* The type index of this element. */ | |
72 | long index; | |
73 | /* The size of the type. */ | |
74 | unsigned int size; | |
75 | /* Whether type string defines a new type. */ | |
76 | boolean definition; | |
77 | /* String defining struct fields. */ | |
78 | char *fields; | |
79 | /* NULL terminated array of strings defining base classes for a | |
80 | class. */ | |
81 | char **baseclasses; | |
82 | /* String defining class methods. */ | |
83 | char *methods; | |
84 | /* String defining vtable pointer for a class. */ | |
85 | char *vtable; | |
86 | }; | |
87 | ||
88 | /* This structure is used to keep track of type indices for tagged | |
89 | types. */ | |
90 | ||
91 | struct stab_tag | |
92 | { | |
93 | /* The type index. */ | |
94 | long index; | |
95 | /* The tag name. */ | |
96 | const char *tag; | |
97 | /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the | |
98 | type is defined. */ | |
99 | enum debug_type_kind kind; | |
100 | /* The size of the struct. */ | |
101 | unsigned int size; | |
102 | }; | |
103 | ||
104 | /* We remember various sorts of type indices. They are not related, | |
105 | but, for convenience, we keep all the information in this | |
106 | structure. */ | |
107 | ||
108 | struct stab_type_cache | |
109 | { | |
110 | /* The void type index. */ | |
111 | long void_type; | |
112 | /* Signed integer type indices, indexed by size - 1. */ | |
113 | long signed_integer_types[8]; | |
114 | /* Unsigned integer type indices, indexed by size - 1. */ | |
115 | long unsigned_integer_types[8]; | |
116 | /* Floating point types, indexed by size - 1. */ | |
117 | long float_types[16]; | |
118 | /* Pointers to types, indexed by the type index. */ | |
119 | long *pointer_types; | |
120 | size_t pointer_types_alloc; | |
121 | /* Functions returning types, indexed by the type index. */ | |
122 | long *function_types; | |
123 | size_t function_types_alloc; | |
124 | /* References to types, indexed by the type index. */ | |
125 | long *reference_types; | |
126 | size_t reference_types_alloc; | |
127 | /* Struct/union/class type indices, indexed by the struct id. */ | |
128 | struct stab_tag *struct_types; | |
129 | size_t struct_types_alloc; | |
130 | }; | |
131 | ||
132 | /* This is the handle passed through debug_write. */ | |
133 | ||
134 | struct stab_write_handle | |
135 | { | |
136 | /* The BFD. */ | |
137 | bfd *abfd; | |
138 | /* This buffer holds the symbols. */ | |
139 | bfd_byte *symbols; | |
140 | size_t symbols_size; | |
141 | size_t symbols_alloc; | |
142 | /* This buffer holds the strings. */ | |
143 | bfd_byte *strings; | |
144 | size_t strings_size; | |
145 | size_t strings_alloc; | |
146 | /* This hash table eliminates duplicate strings. */ | |
147 | struct string_hash_table strhash; | |
148 | /* The type stack. */ | |
149 | struct stab_type_stack *type_stack; | |
150 | /* The next type index. */ | |
151 | long type_index; | |
152 | /* The type cache. */ | |
153 | struct stab_type_cache type_cache; | |
154 | /* A mapping from typedef names to type indices. */ | |
155 | struct string_hash_table typedef_hash; | |
156 | /* If this is not -1, it is the offset to the most recent N_SO | |
157 | symbol, and the value of that symbol needs to be set. */ | |
158 | long so_offset; | |
159 | /* If this is not -1, it is the offset to the most recent N_FUN | |
160 | symbol, and the value of that symbol needs to be set. */ | |
161 | long fun_offset; | |
162 | /* The last text section address seen. */ | |
163 | bfd_vma last_text_address; | |
164 | /* The block nesting depth. */ | |
165 | unsigned int nesting; | |
166 | /* The function address. */ | |
167 | bfd_vma fnaddr; | |
168 | /* A pending LBRAC symbol. */ | |
169 | bfd_vma pending_lbrac; | |
170 | /* The current line number file name. */ | |
171 | const char *lineno_filename; | |
172 | }; | |
173 | ||
174 | static struct bfd_hash_entry *string_hash_newfunc | |
175 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
176 | static boolean stab_write_symbol | |
177 | PARAMS ((struct stab_write_handle *, int, int, bfd_vma, const char *)); | |
178 | static boolean stab_push_string | |
179 | PARAMS ((struct stab_write_handle *, const char *, long, boolean, | |
180 | unsigned int)); | |
181 | static boolean stab_push_defined_type | |
182 | PARAMS ((struct stab_write_handle *, long, unsigned int)); | |
183 | static char *stab_pop_type PARAMS ((struct stab_write_handle *)); | |
184 | static boolean stab_modify_type | |
185 | PARAMS ((struct stab_write_handle *, int, unsigned int, long **, size_t *)); | |
186 | static long stab_get_struct_index | |
187 | PARAMS ((struct stab_write_handle *, const char *, unsigned int, | |
188 | enum debug_type_kind, unsigned int *)); | |
189 | static boolean stab_class_method_var | |
190 | PARAMS ((struct stab_write_handle *, const char *, enum debug_visibility, | |
191 | boolean, boolean, boolean, bfd_vma, boolean)); | |
192 | ||
193 | static boolean stab_start_compilation_unit PARAMS ((PTR, const char *)); | |
194 | static boolean stab_start_source PARAMS ((PTR, const char *)); | |
195 | static boolean stab_empty_type PARAMS ((PTR)); | |
196 | static boolean stab_void_type PARAMS ((PTR)); | |
197 | static boolean stab_int_type PARAMS ((PTR, unsigned int, boolean)); | |
198 | static boolean stab_float_type PARAMS ((PTR, unsigned int)); | |
199 | static boolean stab_complex_type PARAMS ((PTR, unsigned int)); | |
200 | static boolean stab_bool_type PARAMS ((PTR, unsigned int)); | |
201 | static boolean stab_enum_type | |
202 | PARAMS ((PTR, const char *, const char **, bfd_signed_vma *)); | |
203 | static boolean stab_pointer_type PARAMS ((PTR)); | |
204 | static boolean stab_function_type PARAMS ((PTR, int, boolean)); | |
205 | static boolean stab_reference_type PARAMS ((PTR)); | |
206 | static boolean stab_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma)); | |
207 | static boolean stab_array_type | |
208 | PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean)); | |
209 | static boolean stab_set_type PARAMS ((PTR, boolean)); | |
210 | static boolean stab_offset_type PARAMS ((PTR)); | |
211 | static boolean stab_method_type PARAMS ((PTR, boolean, int, boolean)); | |
212 | static boolean stab_const_type PARAMS ((PTR)); | |
213 | static boolean stab_volatile_type PARAMS ((PTR)); | |
214 | static boolean stab_start_struct_type | |
215 | PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int)); | |
216 | static boolean stab_struct_field | |
217 | PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility)); | |
218 | static boolean stab_end_struct_type PARAMS ((PTR)); | |
219 | static boolean stab_start_class_type | |
220 | PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int, boolean, | |
221 | boolean)); | |
222 | static boolean stab_class_static_member | |
223 | PARAMS ((PTR, const char *, const char *, enum debug_visibility)); | |
224 | static boolean stab_class_baseclass | |
225 | PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility)); | |
226 | static boolean stab_class_start_method PARAMS ((PTR, const char *)); | |
227 | static boolean stab_class_method_variant | |
228 | PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean, | |
229 | bfd_vma, boolean)); | |
230 | static boolean stab_class_static_method_variant | |
231 | PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean)); | |
232 | static boolean stab_class_end_method PARAMS ((PTR)); | |
233 | static boolean stab_end_class_type PARAMS ((PTR)); | |
234 | static boolean stab_typedef_type PARAMS ((PTR, const char *)); | |
235 | static boolean stab_tag_type | |
236 | PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind)); | |
237 | static boolean stab_typdef PARAMS ((PTR, const char *)); | |
238 | static boolean stab_tag PARAMS ((PTR, const char *)); | |
239 | static boolean stab_int_constant PARAMS ((PTR, const char *, bfd_vma)); | |
240 | static boolean stab_float_constant PARAMS ((PTR, const char *, double)); | |
241 | static boolean stab_typed_constant PARAMS ((PTR, const char *, bfd_vma)); | |
242 | static boolean stab_variable | |
243 | PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma)); | |
244 | static boolean stab_start_function PARAMS ((PTR, const char *, boolean)); | |
245 | static boolean stab_function_parameter | |
246 | PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma)); | |
247 | static boolean stab_start_block PARAMS ((PTR, bfd_vma)); | |
248 | static boolean stab_end_block PARAMS ((PTR, bfd_vma)); | |
249 | static boolean stab_end_function PARAMS ((PTR)); | |
250 | static boolean stab_lineno | |
251 | PARAMS ((PTR, const char *, unsigned long, bfd_vma)); | |
252 | ||
253 | static const struct debug_write_fns stab_fns = | |
254 | { | |
255 | stab_start_compilation_unit, | |
256 | stab_start_source, | |
257 | stab_empty_type, | |
258 | stab_void_type, | |
259 | stab_int_type, | |
260 | stab_float_type, | |
261 | stab_complex_type, | |
262 | stab_bool_type, | |
263 | stab_enum_type, | |
264 | stab_pointer_type, | |
265 | stab_function_type, | |
266 | stab_reference_type, | |
267 | stab_range_type, | |
268 | stab_array_type, | |
269 | stab_set_type, | |
270 | stab_offset_type, | |
271 | stab_method_type, | |
272 | stab_const_type, | |
273 | stab_volatile_type, | |
274 | stab_start_struct_type, | |
275 | stab_struct_field, | |
276 | stab_end_struct_type, | |
277 | stab_start_class_type, | |
278 | stab_class_static_member, | |
279 | stab_class_baseclass, | |
280 | stab_class_start_method, | |
281 | stab_class_method_variant, | |
282 | stab_class_static_method_variant, | |
283 | stab_class_end_method, | |
284 | stab_end_class_type, | |
285 | stab_typedef_type, | |
286 | stab_tag_type, | |
287 | stab_typdef, | |
288 | stab_tag, | |
289 | stab_int_constant, | |
290 | stab_float_constant, | |
291 | stab_typed_constant, | |
292 | stab_variable, | |
293 | stab_start_function, | |
294 | stab_function_parameter, | |
295 | stab_start_block, | |
296 | stab_end_block, | |
297 | stab_end_function, | |
298 | stab_lineno | |
299 | }; | |
300 | \f | |
301 | /* Routine to create an entry in a string hash table. */ | |
302 | ||
303 | static struct bfd_hash_entry * | |
304 | string_hash_newfunc (entry, table, string) | |
305 | struct bfd_hash_entry *entry; | |
306 | struct bfd_hash_table *table; | |
307 | const char *string; | |
308 | { | |
309 | struct string_hash_entry *ret = (struct string_hash_entry *) entry; | |
310 | ||
311 | /* Allocate the structure if it has not already been allocated by a | |
312 | subclass. */ | |
313 | if (ret == (struct string_hash_entry *) NULL) | |
314 | ret = ((struct string_hash_entry *) | |
315 | bfd_hash_allocate (table, sizeof (struct string_hash_entry))); | |
316 | if (ret == (struct string_hash_entry *) NULL) | |
317 | return NULL; | |
318 | ||
319 | /* Call the allocation method of the superclass. */ | |
320 | ret = ((struct string_hash_entry *) | |
321 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); | |
322 | ||
323 | if (ret) | |
324 | { | |
325 | /* Initialize the local fields. */ | |
326 | ret->index = -1; | |
327 | ret->size = 0; | |
328 | } | |
329 | ||
330 | return (struct bfd_hash_entry *) ret; | |
331 | } | |
332 | ||
333 | /* Look up an entry in a string hash table. */ | |
334 | ||
335 | #define string_hash_lookup(t, string, create, copy) \ | |
336 | ((struct string_hash_entry *) \ | |
337 | bfd_hash_lookup (&(t)->table, (string), (create), (copy))) | |
338 | ||
339 | /* Add a symbol to the stabs debugging information we are building. */ | |
340 | ||
341 | static boolean | |
342 | stab_write_symbol (info, type, desc, value, string) | |
343 | struct stab_write_handle *info; | |
344 | int type; | |
345 | int desc; | |
346 | bfd_vma value; | |
347 | const char *string; | |
348 | { | |
349 | bfd_size_type strx; | |
350 | bfd_byte sym[STAB_SYMBOL_SIZE]; | |
351 | ||
352 | if (string == NULL) | |
353 | strx = 0; | |
354 | else | |
355 | { | |
356 | struct string_hash_entry *h; | |
357 | ||
358 | h = string_hash_lookup (&info->strhash, string, true, false); | |
359 | if (h == NULL) | |
360 | { | |
361 | fprintf (stderr, "string_hash_lookup failed: %s\n", | |
362 | bfd_errmsg (bfd_get_error ())); | |
363 | return false; | |
364 | } | |
365 | if (h->index != -1) | |
366 | strx = h->index; | |
367 | else | |
368 | { | |
369 | size_t len; | |
370 | ||
371 | strx = info->strings_size; | |
372 | h->index = strx; | |
373 | ||
374 | len = strlen (string); | |
375 | while (info->strings_size + len + 1 > info->strings_alloc) | |
376 | { | |
377 | info->strings_alloc *= 2; | |
378 | info->strings = (bfd_byte *) xrealloc (info->strings, | |
379 | info->strings_alloc); | |
380 | } | |
381 | strcpy (info->strings + info->strings_size, string); | |
382 | info->strings_size += len + 1; | |
383 | } | |
384 | } | |
385 | ||
386 | /* This presumes 32 bit values. */ | |
387 | bfd_put_32 (info->abfd, strx, sym); | |
388 | bfd_put_8 (info->abfd, type, sym + 4); | |
389 | bfd_put_8 (info->abfd, 0, sym + 5); | |
390 | bfd_put_16 (info->abfd, desc, sym + 6); | |
391 | bfd_put_32 (info->abfd, value, sym + 8); | |
392 | ||
393 | if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc) | |
394 | { | |
395 | info->symbols_alloc *= 2; | |
396 | info->symbols = (bfd_byte *) xrealloc (info->symbols, | |
397 | info->symbols_alloc); | |
398 | } | |
399 | ||
400 | memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE); | |
401 | ||
402 | info->symbols_size += STAB_SYMBOL_SIZE; | |
403 | ||
404 | return true; | |
405 | } | |
406 | ||
407 | /* Push a string on to the type stack. */ | |
408 | ||
409 | static boolean | |
410 | stab_push_string (info, string, index, definition, size) | |
411 | struct stab_write_handle *info; | |
412 | const char *string; | |
413 | long index; | |
414 | boolean definition; | |
415 | unsigned int size; | |
416 | { | |
417 | struct stab_type_stack *s; | |
418 | ||
419 | s = (struct stab_type_stack *) xmalloc (sizeof *s); | |
420 | s->string = xstrdup (string); | |
421 | s->index = index; | |
422 | s->definition = definition; | |
423 | s->size = size; | |
424 | ||
425 | s->fields = NULL; | |
426 | s->baseclasses = NULL; | |
427 | s->methods = NULL; | |
428 | s->vtable = NULL; | |
429 | ||
430 | s->next = info->type_stack; | |
431 | info->type_stack = s; | |
432 | ||
433 | return true; | |
434 | } | |
435 | ||
436 | /* Push a type index which has already been defined. */ | |
437 | ||
438 | static boolean | |
439 | stab_push_defined_type (info, index, size) | |
440 | struct stab_write_handle *info; | |
441 | long index; | |
442 | unsigned int size; | |
443 | { | |
444 | char buf[20]; | |
445 | ||
446 | sprintf (buf, "%ld", index); | |
447 | return stab_push_string (info, buf, index, false, size); | |
448 | } | |
449 | ||
450 | /* Pop a type off the type stack. The caller is responsible for | |
451 | freeing the string. */ | |
452 | ||
453 | static char * | |
454 | stab_pop_type (info) | |
455 | struct stab_write_handle *info; | |
456 | { | |
457 | struct stab_type_stack *s; | |
458 | char *ret; | |
459 | ||
460 | s = info->type_stack; | |
461 | assert (s != NULL); | |
462 | ||
463 | info->type_stack = s->next; | |
464 | ||
465 | ret = s->string; | |
466 | ||
467 | free (s); | |
468 | ||
469 | return ret; | |
470 | } | |
471 | \f | |
472 | /* The general routine to write out stabs in sections debugging | |
473 | information. This accumulates the stabs symbols and the strings in | |
474 | two obstacks. We can't easily write out the information as we go | |
475 | along, because we need to know the section sizes before we can | |
476 | write out the section contents. ABFD is the BFD and DHANDLE is the | |
477 | handle for the debugging information. This sets *PSYMS to point to | |
478 | the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the | |
479 | strings, and *PSTRINGSIZE to the size of the strings. */ | |
480 | ||
481 | boolean | |
482 | write_stabs_in_sections_debugging_info (abfd, dhandle, psyms, psymsize, | |
483 | pstrings, pstringsize) | |
484 | bfd *abfd; | |
485 | PTR dhandle; | |
486 | bfd_byte **psyms; | |
487 | bfd_size_type *psymsize; | |
488 | bfd_byte **pstrings; | |
489 | bfd_size_type *pstringsize; | |
490 | { | |
491 | struct stab_write_handle info; | |
492 | ||
493 | info.abfd = abfd; | |
494 | ||
495 | info.symbols_size = 0; | |
496 | info.symbols_alloc = 500; | |
497 | info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc); | |
498 | ||
499 | info.strings_size = 1; | |
500 | info.strings_alloc = 500; | |
501 | info.strings = (bfd_byte *) xmalloc (info.strings_alloc); | |
502 | info.strings[0] = '\0'; | |
503 | ||
504 | if (! bfd_hash_table_init (&info.strhash.table, string_hash_newfunc) | |
505 | || ! bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc)) | |
506 | { | |
507 | fprintf (stderr, "bfd_hash_table_init_failed: %s\n", | |
508 | bfd_errmsg (bfd_get_error ())); | |
509 | return false; | |
510 | } | |
511 | ||
512 | info.type_stack = NULL; | |
513 | info.type_index = 1; | |
514 | memset (&info.type_cache, 0, sizeof info.type_cache); | |
515 | info.so_offset = -1; | |
516 | info.fun_offset = -1; | |
517 | info.last_text_address = 0; | |
518 | info.nesting = 0; | |
519 | info.fnaddr = 0; | |
520 | info.pending_lbrac = (bfd_vma) -1; | |
521 | ||
522 | /* The initial symbol holds the string size. */ | |
523 | if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL)) | |
524 | return false; | |
525 | ||
526 | /* Output an initial N_SO symbol. */ | |
527 | info.so_offset = info.symbols_size; | |
528 | if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd))) | |
529 | return false; | |
530 | ||
531 | if (! debug_write (dhandle, &stab_fns, (PTR) &info)) | |
532 | return false; | |
533 | ||
534 | assert (info.pending_lbrac == (bfd_vma) -1); | |
535 | ||
536 | /* Output a trailing N_SO. */ | |
537 | if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address, | |
538 | (const char *) NULL)) | |
539 | return false; | |
540 | ||
541 | /* Put the string size in the initial symbol. */ | |
542 | bfd_put_32 (abfd, info.strings_size, info.symbols + 8); | |
543 | ||
544 | *psyms = info.symbols; | |
545 | *psymsize = info.symbols_size; | |
546 | ||
547 | *pstrings = info.strings; | |
548 | *pstringsize = info.strings_size; | |
549 | ||
550 | return true; | |
551 | } | |
552 | ||
553 | /* Start writing out information for a compilation unit. */ | |
554 | ||
555 | static boolean | |
556 | stab_start_compilation_unit (p, filename) | |
557 | PTR p; | |
558 | const char *filename; | |
559 | { | |
560 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
561 | ||
562 | /* We would normally output an N_SO symbol here. However, that | |
563 | would force us to reset all of our type information. I think we | |
564 | will be better off just outputting an N_SOL symbol, and not | |
565 | worrying about splitting information between files. */ | |
566 | ||
567 | info->lineno_filename = filename; | |
568 | ||
569 | return stab_write_symbol (info, N_SOL, 0, 0, filename); | |
570 | } | |
571 | ||
572 | /* Start writing out information for a particular source file. */ | |
573 | ||
574 | static boolean | |
575 | stab_start_source (p, filename) | |
576 | PTR p; | |
577 | const char *filename; | |
578 | { | |
579 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
580 | ||
581 | /* FIXME: The symbol's value is supposed to be the text section | |
582 | address. However, we would have to fill it in later, and gdb | |
583 | doesn't care, so we don't bother with it. */ | |
584 | ||
585 | info->lineno_filename = filename; | |
586 | ||
587 | return stab_write_symbol (info, N_SOL, 0, 0, filename); | |
588 | } | |
589 | ||
590 | /* Push an empty type. This shouldn't normally happen. We just use a | |
591 | void type. */ | |
592 | ||
593 | static boolean | |
594 | stab_empty_type (p) | |
595 | PTR p; | |
596 | { | |
597 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
598 | ||
599 | /* We don't call stab_void_type if the type is not yet defined, | |
600 | because that might screw up the typedef. */ | |
601 | ||
602 | if (info->type_cache.void_type != 0) | |
603 | return stab_push_defined_type (info, info->type_cache.void_type, 0); | |
604 | else | |
605 | { | |
606 | long index; | |
607 | char buf[40]; | |
608 | ||
609 | index = info->type_index; | |
610 | ++info->type_index; | |
611 | ||
612 | sprintf (buf, "%ld=%ld", index, index); | |
613 | ||
614 | return stab_push_string (info, buf, index, false, 0); | |
615 | } | |
616 | } | |
617 | ||
618 | /* Push a void type. */ | |
619 | ||
620 | static boolean | |
621 | stab_void_type (p) | |
622 | PTR p; | |
623 | { | |
624 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
625 | ||
626 | if (info->type_cache.void_type != 0) | |
627 | return stab_push_defined_type (info, info->type_cache.void_type, 0); | |
628 | else | |
629 | { | |
630 | long index; | |
631 | char buf[40]; | |
632 | ||
633 | index = info->type_index; | |
634 | ++info->type_index; | |
635 | ||
636 | info->type_cache.void_type = index; | |
637 | ||
638 | sprintf (buf, "%ld=%ld", index, index); | |
639 | ||
640 | return stab_push_string (info, buf, index, true, 0); | |
641 | } | |
642 | } | |
643 | ||
644 | /* Push an integer type. */ | |
645 | ||
646 | static boolean | |
647 | stab_int_type (p, size, unsignedp) | |
648 | PTR p; | |
649 | unsigned int size; | |
650 | boolean unsignedp; | |
651 | { | |
652 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
653 | long *cache; | |
654 | ||
655 | if (size <= 0 || (size > sizeof (long) && size != 8)) | |
656 | { | |
657 | fprintf (stderr, "stab_int_type: bad size %u\n", size); | |
658 | return false; | |
659 | } | |
660 | ||
661 | if (unsignedp) | |
662 | cache = info->type_cache.signed_integer_types; | |
663 | else | |
664 | cache = info->type_cache.unsigned_integer_types; | |
665 | ||
666 | if (cache[size - 1] != 0) | |
667 | return stab_push_defined_type (info, cache[size - 1], size); | |
668 | else | |
669 | { | |
670 | long index; | |
671 | char buf[100]; | |
672 | ||
673 | index = info->type_index; | |
674 | ++info->type_index; | |
675 | ||
676 | cache[size - 1] = index; | |
677 | ||
678 | sprintf (buf, "%ld=r%ld;", index, index); | |
679 | if (unsignedp) | |
680 | { | |
681 | strcat (buf, "0;"); | |
682 | if (size < sizeof (long)) | |
683 | sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1); | |
684 | else if (size == sizeof (long)) | |
685 | strcat (buf, "-1;"); | |
686 | else if (size == 8) | |
687 | strcat (buf, "01777777777777777777777;"); | |
688 | else | |
689 | abort (); | |
690 | } | |
691 | else | |
692 | { | |
693 | if (size <= sizeof (long)) | |
694 | sprintf (buf + strlen (buf), "%ld;%ld;", | |
695 | (long) - ((unsigned long) 1 << (size * 8 - 1)), | |
696 | (long) (((unsigned long) 1 << (size * 8 - 1)) - 1)); | |
697 | else if (size == 8) | |
698 | strcat (buf, "01000000000000000000000;0777777777777777777777;"); | |
699 | else | |
700 | abort (); | |
701 | } | |
702 | ||
703 | return stab_push_string (info, buf, index, true, size); | |
704 | } | |
705 | } | |
706 | ||
707 | /* Push a floating point type. */ | |
708 | ||
709 | static boolean | |
710 | stab_float_type (p, size) | |
711 | PTR p; | |
712 | unsigned int size; | |
713 | { | |
714 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
715 | ||
716 | if (size > 0 | |
717 | && size - 1 < (sizeof info->type_cache.float_types | |
718 | / sizeof info->type_cache.float_types[0]) | |
719 | && info->type_cache.float_types[size - 1] != 0) | |
720 | return stab_push_defined_type (info, | |
721 | info->type_cache.float_types[size - 1], | |
722 | size); | |
723 | else | |
724 | { | |
725 | long index; | |
726 | char *int_type; | |
727 | char buf[50]; | |
728 | ||
729 | /* Floats are defined as a subrange of int. */ | |
730 | if (! stab_int_type (info, 4, false)) | |
731 | return false; | |
732 | int_type = stab_pop_type (info); | |
733 | ||
734 | index = info->type_index; | |
735 | ++info->type_index; | |
736 | ||
737 | if (size > 0 | |
738 | && size - 1 < (sizeof info->type_cache.float_types | |
739 | / sizeof info->type_cache.float_types[0])) | |
740 | info->type_cache.float_types[size - 1] = index; | |
741 | ||
742 | sprintf (buf, "%ld=r%s;%u;0;", index, int_type, size); | |
743 | ||
744 | free (int_type); | |
745 | ||
746 | return stab_push_string (info, buf, index, true, size); | |
747 | } | |
748 | } | |
749 | ||
750 | /* Push a complex type. */ | |
751 | ||
752 | static boolean | |
753 | stab_complex_type (p, size) | |
754 | PTR p; | |
755 | unsigned int size; | |
756 | { | |
757 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
758 | char buf[50]; | |
759 | long index; | |
760 | ||
761 | index = info->type_index; | |
762 | ++info->type_index; | |
763 | ||
764 | sprintf (buf, "%ld=r%ld;%u;0;", index, index, size); | |
765 | ||
766 | return stab_push_string (info, buf, index, true, size * 2); | |
767 | } | |
768 | ||
769 | /* Push a boolean type. We use an XCOFF predefined type, since gdb | |
770 | always recognizes them. */ | |
771 | ||
772 | static boolean | |
773 | stab_bool_type (p, size) | |
774 | PTR p; | |
775 | unsigned int size; | |
776 | { | |
777 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
778 | long index; | |
779 | ||
780 | switch (size) | |
781 | { | |
782 | case 1: | |
783 | index = -21; | |
784 | break; | |
785 | ||
786 | case 2: | |
787 | index = -22; | |
788 | break; | |
789 | ||
790 | default: | |
791 | case 4: | |
792 | index = -16; | |
793 | break; | |
794 | ||
795 | case 8: | |
796 | index = -33; | |
797 | break; | |
798 | } | |
799 | ||
800 | return stab_push_defined_type (info, index, size); | |
801 | } | |
802 | ||
803 | /* Push an enum type. */ | |
804 | ||
805 | static boolean | |
806 | stab_enum_type (p, tag, names, vals) | |
807 | PTR p; | |
808 | const char *tag; | |
809 | const char **names; | |
810 | bfd_signed_vma *vals; | |
811 | { | |
812 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
813 | size_t len; | |
814 | const char **pn; | |
815 | char *buf; | |
816 | long index = 0; | |
817 | bfd_signed_vma *pv; | |
818 | ||
819 | if (names == NULL) | |
820 | { | |
821 | assert (tag != NULL); | |
822 | ||
823 | buf = (char *) xmalloc (10 + strlen (tag)); | |
824 | sprintf (buf, "xe%s:", tag); | |
825 | /* FIXME: The size is just a guess. */ | |
826 | if (! stab_push_string (info, buf, 0, false, 4)) | |
827 | return false; | |
828 | free (buf); | |
829 | return true; | |
830 | } | |
831 | ||
832 | len = 10; | |
833 | if (tag != NULL) | |
834 | len += strlen (tag); | |
835 | for (pn = names; *pn != NULL; pn++) | |
836 | len += strlen (*pn) + 20; | |
837 | ||
838 | if (tag == NULL) | |
839 | strcpy (buf, "e"); | |
840 | else | |
841 | { | |
842 | index = info->type_index; | |
843 | ++info->type_index; | |
844 | sprintf (buf, "%s:T%ld=e", tag, index); | |
845 | } | |
846 | ||
847 | buf = (char *) xmalloc (len); | |
848 | for (pn = names, pv = vals; *pn != NULL; pn++, pv++) | |
849 | sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv); | |
850 | strcat (buf, ";"); | |
851 | ||
852 | if (tag == NULL) | |
853 | { | |
854 | /* FIXME: The size is just a guess. */ | |
855 | if (! stab_push_string (info, buf, 0, false, 4)) | |
856 | return false; | |
857 | } | |
858 | else | |
859 | { | |
860 | /* FIXME: The size is just a guess. */ | |
861 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf) | |
862 | || ! stab_push_defined_type (info, index, 4)) | |
863 | return false; | |
864 | } | |
865 | ||
866 | free (buf); | |
867 | ||
868 | return true; | |
869 | } | |
870 | ||
871 | /* Push a modification of the top type on the stack. Cache the | |
872 | results in CACHE and CACHE_ALLOC. */ | |
873 | ||
874 | static boolean | |
875 | stab_modify_type (info, mod, size, cache, cache_alloc) | |
876 | struct stab_write_handle *info; | |
877 | int mod; | |
878 | unsigned int size; | |
879 | long **cache; | |
880 | size_t *cache_alloc; | |
881 | { | |
882 | long targindex; | |
883 | long index; | |
884 | char *s, *buf; | |
885 | ||
886 | assert (info->type_stack != NULL); | |
887 | targindex = info->type_stack->index; | |
888 | ||
889 | if (targindex <= 0 | |
890 | || cache == NULL) | |
891 | { | |
892 | boolean definition; | |
893 | ||
894 | /* Either the target type has no index, or we aren't caching | |
895 | this modifier. Either way we have no way of recording the | |
896 | new type, so we don't bother to define one. */ | |
897 | definition = info->type_stack->definition; | |
898 | s = stab_pop_type (info); | |
899 | buf = (char *) xmalloc (strlen (s) + 2); | |
900 | sprintf (buf, "%c%s", mod, s); | |
901 | free (s); | |
902 | if (! stab_push_string (info, buf, 0, definition, size)) | |
903 | return false; | |
904 | free (buf); | |
905 | } | |
906 | else | |
907 | { | |
908 | if (targindex >= *cache_alloc) | |
909 | { | |
910 | size_t alloc; | |
911 | ||
912 | alloc = *cache_alloc; | |
913 | if (alloc == 0) | |
914 | alloc = 10; | |
915 | while (targindex >= alloc) | |
916 | alloc *= 2; | |
917 | *cache = (long *) xrealloc (*cache, alloc * sizeof (long)); | |
918 | memset (*cache + *cache_alloc, 0, | |
919 | (alloc - *cache_alloc) * sizeof (long)); | |
920 | *cache_alloc = alloc; | |
921 | } | |
922 | ||
923 | index = (*cache)[targindex]; | |
924 | if (index != 0) | |
925 | { | |
926 | /* If we have already defined a modification of this type, | |
927 | then the entry on the type stack can not be a definition, | |
928 | so we can safely discard it. */ | |
929 | assert (! info->type_stack->definition); | |
930 | free (stab_pop_type (info)); | |
931 | if (! stab_push_defined_type (info, index, size)) | |
932 | return false; | |
933 | } | |
934 | else | |
935 | { | |
936 | index = info->type_index; | |
937 | ++info->type_index; | |
938 | ||
939 | s = stab_pop_type (info); | |
940 | buf = (char *) xmalloc (strlen (s) + 20); | |
941 | sprintf (buf, "%ld=%c%s", index, mod, s); | |
942 | free (s); | |
943 | ||
944 | (*cache)[targindex] = index; | |
945 | ||
946 | if (! stab_push_string (info, buf, index, true, size)) | |
947 | return false; | |
948 | ||
949 | free (buf); | |
950 | } | |
951 | } | |
952 | ||
953 | return true; | |
954 | } | |
955 | ||
956 | /* Push a pointer type. */ | |
957 | ||
958 | static boolean | |
959 | stab_pointer_type (p) | |
960 | PTR p; | |
961 | { | |
962 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
963 | ||
964 | /* FIXME: The size should depend upon the architecture. */ | |
965 | return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types, | |
966 | &info->type_cache.pointer_types_alloc); | |
967 | } | |
968 | ||
969 | /* Push a function type. */ | |
970 | ||
971 | static boolean | |
972 | stab_function_type (p, argcount, varargs) | |
973 | PTR p; | |
974 | int argcount; | |
975 | boolean varargs; | |
976 | { | |
977 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
978 | int i; | |
979 | ||
980 | /* We have no way to represent the argument types, so we just | |
981 | discard them. However, if they define new types, we must output | |
982 | them. We do this by producing meaningless typedefs. */ | |
983 | for (i = 0; i < argcount; i++) | |
984 | { | |
985 | if (! info->type_stack->definition) | |
986 | free (stab_pop_type (info)); | |
987 | else | |
988 | { | |
989 | char *s, *buf; | |
990 | long index; | |
991 | ||
992 | s = stab_pop_type (info); | |
993 | ||
994 | buf = (char *) xmalloc (strlen (s) + 40); | |
995 | index = info->type_index; | |
996 | ++info->type_index; | |
997 | sprintf (buf, "__fake_type_%ld:t%s", index, s); | |
998 | ||
999 | free (s); | |
1000 | ||
1001 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)) | |
1002 | return false; | |
1003 | ||
1004 | free (buf); | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | return stab_modify_type (info, 'f', 0, &info->type_cache.function_types, | |
1009 | &info->type_cache.function_types_alloc); | |
1010 | } | |
1011 | ||
1012 | /* Push a reference type. */ | |
1013 | ||
1014 | static boolean | |
1015 | stab_reference_type (p) | |
1016 | PTR p; | |
1017 | { | |
1018 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1019 | ||
1020 | /* FIXME: The size should depend upon the architecture. */ | |
1021 | return stab_modify_type (info, '&', 4, &info->type_cache.reference_types, | |
1022 | &info->type_cache.reference_types_alloc); | |
1023 | } | |
1024 | ||
1025 | /* Push a range type. */ | |
1026 | ||
1027 | static boolean | |
1028 | stab_range_type (p, low, high) | |
1029 | PTR p; | |
1030 | bfd_signed_vma low; | |
1031 | bfd_signed_vma high; | |
1032 | { | |
1033 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1034 | boolean definition; | |
1035 | unsigned int size; | |
1036 | char *s, *buf; | |
1037 | ||
1038 | definition = info->type_stack->definition; | |
1039 | size = info->type_stack->size; | |
1040 | ||
1041 | s = stab_pop_type (info); | |
1042 | buf = (char *) xmalloc (strlen (s) + 100); | |
1043 | sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high); | |
1044 | free (s); | |
1045 | ||
1046 | if (! stab_push_string (info, buf, 0, definition, size)) | |
1047 | return false; | |
1048 | ||
1049 | free (buf); | |
1050 | ||
1051 | return true; | |
1052 | } | |
1053 | ||
1054 | /* Push an array type. */ | |
1055 | ||
1056 | static boolean | |
1057 | stab_array_type (p, low, high, stringp) | |
1058 | PTR p; | |
1059 | bfd_signed_vma low; | |
1060 | bfd_signed_vma high; | |
1061 | boolean stringp; | |
1062 | { | |
1063 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1064 | boolean definition; | |
1065 | unsigned int element_size; | |
1066 | char *range, *element, *buf; | |
1067 | long index; | |
1068 | unsigned int size; | |
1069 | ||
1070 | definition = info->type_stack->definition; | |
1071 | range = stab_pop_type (info); | |
1072 | ||
1073 | definition = definition || info->type_stack->definition; | |
1074 | element_size = info->type_stack->size; | |
1075 | element = stab_pop_type (info); | |
1076 | ||
1077 | buf = (char *) xmalloc (strlen (range) + strlen (element) + 100); | |
1078 | ||
1079 | if (! stringp) | |
1080 | { | |
1081 | index = 0; | |
1082 | *buf = '\0'; | |
1083 | } | |
1084 | else | |
1085 | { | |
1086 | /* We need to define a type in order to include the string | |
1087 | attribute. */ | |
1088 | index = info->type_index; | |
1089 | ++info->type_index; | |
1090 | definition = true; | |
1091 | sprintf (buf, "%ld=@S;", index); | |
1092 | } | |
1093 | ||
1094 | sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s", range, low, high, element); | |
1095 | free (range); | |
1096 | free (element); | |
1097 | ||
1098 | if (high <= low) | |
1099 | size = 0; | |
1100 | else | |
1101 | size = element_size * ((high - low) + 1); | |
1102 | if (! stab_push_string (info, buf, index, definition, size)) | |
1103 | return false; | |
1104 | ||
1105 | free (buf); | |
1106 | ||
1107 | return true; | |
1108 | } | |
1109 | ||
1110 | /* Push a set type. */ | |
1111 | ||
1112 | static boolean | |
1113 | stab_set_type (p, bitstringp) | |
1114 | PTR p; | |
1115 | boolean bitstringp; | |
1116 | { | |
1117 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1118 | boolean definition; | |
1119 | char *s, *buf; | |
1120 | long index; | |
1121 | ||
1122 | definition = info->type_stack->definition; | |
1123 | ||
1124 | s = stab_pop_type (info); | |
1125 | buf = (char *) xmalloc (strlen (s) + 30); | |
1126 | ||
1127 | if (! bitstringp) | |
1128 | { | |
1129 | *buf = '\0'; | |
1130 | index = 0; | |
1131 | } | |
1132 | else | |
1133 | { | |
1134 | /* We need to define a type in order to include the string | |
1135 | attribute. */ | |
1136 | index = info->type_index; | |
1137 | ++info->type_index; | |
1138 | definition = true; | |
1139 | sprintf (buf, "%ld=@S;", index); | |
1140 | } | |
1141 | ||
1142 | sprintf (buf + strlen (buf), "S%s", s); | |
1143 | free (s); | |
1144 | ||
1145 | if (! stab_push_string (info, buf, index, definition, 0)) | |
1146 | return false; | |
1147 | ||
1148 | free (buf); | |
1149 | ||
1150 | return true; | |
1151 | } | |
1152 | ||
1153 | /* Push an offset type. */ | |
1154 | ||
1155 | static boolean | |
1156 | stab_offset_type (p) | |
1157 | PTR p; | |
1158 | { | |
1159 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1160 | boolean definition; | |
1161 | char *target, *base, *buf; | |
1162 | ||
1163 | definition = info->type_stack->definition; | |
1164 | target = stab_pop_type (info); | |
1165 | ||
1166 | definition = definition || info->type_stack->definition; | |
1167 | base = stab_pop_type (info); | |
1168 | ||
1169 | buf = (char *) xmalloc (strlen (target) + strlen (base) + 3); | |
1170 | sprintf (buf, "@%s,%s", base, target); | |
1171 | free (base); | |
1172 | free (target); | |
1173 | ||
1174 | if (! stab_push_string (info, buf, 0, definition, 0)) | |
1175 | return false; | |
1176 | ||
1177 | free (buf); | |
1178 | ||
1179 | return true; | |
1180 | } | |
1181 | ||
1182 | /* Push a method type. */ | |
1183 | ||
1184 | static boolean | |
1185 | stab_method_type (p, domainp, argcount, varargs) | |
1186 | PTR p; | |
1187 | boolean domainp; | |
1188 | int argcount; | |
1189 | boolean varargs; | |
1190 | { | |
1191 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1192 | boolean definition; | |
1193 | char *domain, *return_type, *buf; | |
1194 | char **args; | |
1195 | int i; | |
1196 | size_t len; | |
1197 | ||
1198 | /* We don't bother with stub method types, because that would | |
1199 | require a mangler for C++ argument types. This will waste space | |
1200 | in the debugging output. */ | |
1201 | ||
1202 | /* We need a domain. I'm not sure DOMAINP can ever be false, | |
1203 | anyhow. */ | |
1204 | if (! domainp) | |
1205 | { | |
1206 | if (! stab_empty_type (p)) | |
1207 | return false; | |
1208 | } | |
1209 | ||
1210 | definition = info->type_stack->definition; | |
1211 | domain = stab_pop_type (info); | |
1212 | ||
1213 | /* A non-varargs function is indicated by making the last parameter | |
1214 | type be void. */ | |
1215 | ||
1216 | if (argcount < 0) | |
1217 | { | |
1218 | args = NULL; | |
1219 | argcount = 0; | |
1220 | } | |
1221 | else if (argcount == 0) | |
1222 | { | |
1223 | if (varargs) | |
1224 | args = NULL; | |
1225 | else | |
1226 | { | |
1227 | args = (char **) xmalloc (1 * sizeof (*args)); | |
1228 | if (! stab_empty_type (p)) | |
1229 | return false; | |
1230 | definition = definition || info->type_stack->definition; | |
1231 | args[0] = stab_pop_type (info); | |
1232 | argcount = 1; | |
1233 | } | |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | args = (char **) xmalloc ((argcount + 1) * sizeof (*args)); | |
1238 | for (i = argcount - 1; i >= 0; i--) | |
1239 | { | |
1240 | definition = definition || info->type_stack->definition; | |
1241 | args[i] = stab_pop_type (info); | |
1242 | } | |
1243 | if (! varargs) | |
1244 | { | |
1245 | if (! stab_empty_type (p)) | |
1246 | return false; | |
1247 | definition = definition || info->type_stack->definition; | |
1248 | args[argcount] = stab_pop_type (info); | |
1249 | ++argcount; | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | definition = definition || info->type_stack->definition; | |
1254 | return_type = stab_pop_type (info); | |
1255 | ||
1256 | len = strlen (domain) + strlen (return_type) + 10; | |
1257 | for (i = 0; i < argcount; i++) | |
1258 | len += strlen (args[i]); | |
1259 | ||
1260 | buf = (char *) xmalloc (len); | |
1261 | ||
1262 | sprintf (buf, "#%s,%s", domain, return_type); | |
1263 | free (domain); | |
1264 | free (return_type); | |
1265 | for (i = 0; i < argcount; i++) | |
1266 | { | |
1267 | strcat (buf, ","); | |
1268 | strcat (buf, args[i]); | |
1269 | free (args[i]); | |
1270 | } | |
1271 | strcat (buf, ";"); | |
1272 | ||
1273 | if (args != NULL) | |
1274 | free (args); | |
1275 | ||
1276 | if (! stab_push_string (info, buf, 0, definition, 0)) | |
1277 | return false; | |
1278 | ||
1279 | free (buf); | |
1280 | ||
1281 | return true; | |
1282 | } | |
1283 | ||
1284 | /* Push a const version of a type. */ | |
1285 | ||
1286 | static boolean | |
1287 | stab_const_type (p) | |
1288 | PTR p; | |
1289 | { | |
1290 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1291 | ||
1292 | return stab_modify_type (info, 'k', info->type_stack->size, | |
1293 | (long **) NULL, (size_t *) NULL); | |
1294 | } | |
1295 | ||
1296 | /* Push a volatile version of a type. */ | |
1297 | ||
1298 | static boolean | |
1299 | stab_volatile_type (p) | |
1300 | PTR p; | |
1301 | { | |
1302 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1303 | ||
1304 | return stab_modify_type (info, 'B', info->type_stack->size, | |
1305 | (long **) NULL, (size_t *) NULL); | |
1306 | } | |
1307 | ||
1308 | /* Get the type index to use for a struct/union/class ID. This should | |
1309 | return -1 if it fails. */ | |
1310 | ||
1311 | static long | |
1312 | stab_get_struct_index (info, tag, id, kind, psize) | |
1313 | struct stab_write_handle *info; | |
1314 | const char *tag; | |
1315 | unsigned int id; | |
1316 | enum debug_type_kind kind; | |
1317 | unsigned int *psize; | |
1318 | { | |
1319 | if (id >= info->type_cache.struct_types_alloc) | |
1320 | { | |
1321 | size_t alloc; | |
1322 | ||
1323 | alloc = info->type_cache.struct_types_alloc; | |
1324 | if (alloc == 0) | |
1325 | alloc = 10; | |
1326 | while (id >= alloc) | |
1327 | alloc *= 2; | |
1328 | info->type_cache.struct_types = | |
1329 | (struct stab_tag *) xrealloc (info->type_cache.struct_types, | |
1330 | alloc * sizeof (struct stab_tag)); | |
1331 | memset ((info->type_cache.struct_types | |
1332 | + info->type_cache.struct_types_alloc), | |
1333 | 0, | |
1334 | ((alloc - info->type_cache.struct_types_alloc) | |
1335 | * sizeof (struct stab_tag))); | |
1336 | info->type_cache.struct_types_alloc = alloc; | |
1337 | } | |
1338 | ||
1339 | if (info->type_cache.struct_types[id].index == 0) | |
1340 | { | |
1341 | info->type_cache.struct_types[id].index = info->type_index; | |
1342 | ++info->type_index; | |
1343 | info->type_cache.struct_types[id].tag = tag; | |
1344 | info->type_cache.struct_types[id].kind = kind; | |
1345 | } | |
1346 | ||
1347 | if (kind == DEBUG_KIND_ILLEGAL) | |
1348 | { | |
1349 | /* This is a definition of the struct. */ | |
1350 | info->type_cache.struct_types[id].kind = kind; | |
1351 | info->type_cache.struct_types[id].size = *psize; | |
1352 | } | |
1353 | else | |
1354 | *psize = info->type_cache.struct_types[id].size; | |
1355 | ||
1356 | return info->type_cache.struct_types[id].index; | |
1357 | } | |
1358 | ||
1359 | /* Start outputting a struct. We ignore the tag, and handle it in | |
1360 | stab_tag. */ | |
1361 | ||
1362 | /*ARGSUSED*/ | |
1363 | static boolean | |
1364 | stab_start_struct_type (p, tag, id, structp, size) | |
1365 | PTR p; | |
1366 | const char *tag; | |
1367 | unsigned int id; | |
1368 | boolean structp; | |
1369 | unsigned int size; | |
1370 | { | |
1371 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1372 | long index; | |
1373 | boolean definition; | |
1374 | char *buf; | |
1375 | ||
1376 | buf = (char *) xmalloc (40); | |
1377 | ||
1378 | if (id == 0) | |
1379 | { | |
1380 | index = 0; | |
1381 | *buf = '\0'; | |
1382 | definition = false; | |
1383 | } | |
1384 | else | |
1385 | { | |
1386 | index = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL, | |
1387 | &size); | |
1388 | if (index < 0) | |
1389 | return false; | |
1390 | sprintf (buf, "%ld=", index); | |
1391 | definition = true; | |
1392 | } | |
1393 | ||
1394 | sprintf (buf + strlen (buf), "%c%u", | |
1395 | structp ? 's' : 'u', | |
1396 | size); | |
1397 | ||
1398 | if (! stab_push_string (info, buf, index, definition, size)) | |
1399 | return false; | |
1400 | ||
1401 | info->type_stack->fields = (char *) xmalloc (1); | |
1402 | info->type_stack->fields[0] = '\0'; | |
1403 | ||
1404 | return true; | |
1405 | } | |
1406 | ||
1407 | /* Add a field to a struct. */ | |
1408 | ||
1409 | static boolean | |
1410 | stab_struct_field (p, name, bitpos, bitsize, visibility) | |
1411 | PTR p; | |
1412 | const char *name; | |
1413 | bfd_vma bitpos; | |
1414 | bfd_vma bitsize; | |
1415 | enum debug_visibility visibility; | |
1416 | { | |
1417 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1418 | boolean definition; | |
1419 | unsigned int size; | |
1420 | char *s, *n; | |
1421 | const char *vis; | |
1422 | ||
1423 | definition = info->type_stack->definition; | |
1424 | size = info->type_stack->size; | |
1425 | s = stab_pop_type (info); | |
1426 | ||
1427 | /* Add this field to the end of the current struct fields, which is | |
1428 | currently on the top of the stack. */ | |
1429 | ||
1430 | assert (info->type_stack->fields != NULL); | |
1431 | n = (char *) xmalloc (strlen (info->type_stack->fields) | |
1432 | + strlen (name) | |
1433 | + strlen (s) | |
1434 | + 50); | |
1435 | ||
1436 | switch (visibility) | |
1437 | { | |
1438 | default: | |
1439 | abort (); | |
1440 | ||
1441 | case DEBUG_VISIBILITY_PUBLIC: | |
1442 | vis = ""; | |
1443 | break; | |
1444 | ||
1445 | case DEBUG_VISIBILITY_PRIVATE: | |
1446 | vis = "/0"; | |
1447 | break; | |
1448 | ||
1449 | case DEBUG_VISIBILITY_PROTECTED: | |
1450 | vis = "/1"; | |
1451 | break; | |
1452 | } | |
1453 | ||
1454 | if (bitsize == 0) | |
1455 | { | |
1456 | bitsize = size * 8; | |
1457 | if (bitsize == 0) | |
1458 | fprintf (stderr, | |
1459 | "%s: warning: unknown size for field `%s' in struct\n", | |
1460 | bfd_get_filename (info->abfd), name); | |
1461 | } | |
1462 | ||
1463 | sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s, | |
1464 | (long) bitpos, (long) bitsize); | |
1465 | ||
1466 | free (info->type_stack->fields); | |
1467 | info->type_stack->fields = n; | |
1468 | ||
1469 | if (definition) | |
1470 | info->type_stack->definition = true; | |
1471 | ||
1472 | return true; | |
1473 | } | |
1474 | ||
1475 | /* Finish up a struct. */ | |
1476 | ||
1477 | static boolean | |
1478 | stab_end_struct_type (p) | |
1479 | PTR p; | |
1480 | { | |
1481 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1482 | boolean definition; | |
1483 | long index; | |
1484 | unsigned int size; | |
1485 | char *fields, *first, *buf; | |
1486 | ||
1487 | assert (info->type_stack != NULL && info->type_stack->fields != NULL); | |
1488 | ||
1489 | definition = info->type_stack->definition; | |
1490 | index = info->type_stack->index; | |
1491 | size = info->type_stack->size; | |
1492 | fields = info->type_stack->fields; | |
1493 | first = stab_pop_type (info); | |
1494 | ||
1495 | buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2); | |
1496 | sprintf (buf, "%s%s;", first, fields); | |
1497 | free (first); | |
1498 | free (fields); | |
1499 | ||
1500 | if (! stab_push_string (info, buf, index, definition, size)) | |
1501 | return false; | |
1502 | ||
1503 | free (buf); | |
1504 | ||
1505 | return true; | |
1506 | } | |
1507 | ||
1508 | /* Start outputting a class. */ | |
1509 | ||
1510 | static boolean | |
1511 | stab_start_class_type (p, tag, id, structp, size, vptr, ownvptr) | |
1512 | PTR p; | |
1513 | const char *tag; | |
1514 | unsigned int id; | |
1515 | boolean structp; | |
1516 | unsigned int size; | |
1517 | boolean vptr; | |
1518 | boolean ownvptr; | |
1519 | { | |
1520 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1521 | boolean definition; | |
1522 | char *vstring; | |
1523 | ||
1524 | if (! vptr || ownvptr) | |
1525 | { | |
1526 | definition = false; | |
1527 | vstring = NULL; | |
1528 | } | |
1529 | else | |
1530 | { | |
1531 | definition = info->type_stack->definition; | |
1532 | vstring = stab_pop_type (info); | |
1533 | } | |
1534 | ||
1535 | if (! stab_start_struct_type (p, tag, id, structp, size)) | |
1536 | return false; | |
1537 | ||
1538 | if (vptr) | |
1539 | { | |
1540 | char *vtable; | |
1541 | ||
1542 | if (ownvptr) | |
1543 | { | |
1544 | assert (info->type_stack->index > 0); | |
1545 | vtable = (char *) xmalloc (20); | |
1546 | sprintf (vtable, "~%%%ld", info->type_stack->index); | |
1547 | } | |
1548 | else | |
1549 | { | |
1550 | vtable = (char *) xmalloc (strlen (vstring) + 3); | |
1551 | sprintf (vtable, "~%%%s", vstring); | |
1552 | free (vstring); | |
1553 | } | |
1554 | ||
1555 | info->type_stack->vtable = vtable; | |
1556 | } | |
1557 | ||
1558 | if (definition) | |
1559 | info->type_stack->definition = true; | |
1560 | ||
1561 | return true; | |
1562 | } | |
1563 | ||
1564 | /* Add a static member to the class on the type stack. */ | |
1565 | ||
1566 | static boolean | |
1567 | stab_class_static_member (p, name, physname, visibility) | |
1568 | PTR p; | |
1569 | const char *name; | |
1570 | const char *physname; | |
1571 | enum debug_visibility visibility; | |
1572 | { | |
1573 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1574 | boolean definition; | |
1575 | char *s, *n; | |
1576 | const char *vis; | |
1577 | ||
1578 | definition = info->type_stack->definition; | |
1579 | s = stab_pop_type (info); | |
1580 | ||
1581 | /* Add this field to the end of the current struct fields, which is | |
1582 | currently on the top of the stack. */ | |
1583 | ||
1584 | assert (info->type_stack->fields != NULL); | |
1585 | n = (char *) xmalloc (strlen (info->type_stack->fields) | |
1586 | + strlen (name) | |
1587 | + strlen (s) | |
1588 | + strlen (physname) | |
1589 | + 10); | |
1590 | ||
1591 | switch (visibility) | |
1592 | { | |
1593 | default: | |
1594 | abort (); | |
1595 | ||
1596 | case DEBUG_VISIBILITY_PUBLIC: | |
1597 | vis = ""; | |
1598 | break; | |
1599 | ||
1600 | case DEBUG_VISIBILITY_PRIVATE: | |
1601 | vis = "/0"; | |
1602 | break; | |
1603 | ||
1604 | case DEBUG_VISIBILITY_PROTECTED: | |
1605 | vis = "/1"; | |
1606 | break; | |
1607 | } | |
1608 | ||
1609 | sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s, | |
1610 | physname); | |
1611 | ||
1612 | free (info->type_stack->fields); | |
1613 | info->type_stack->fields = n; | |
1614 | ||
1615 | if (definition) | |
1616 | info->type_stack->definition = true; | |
1617 | ||
1618 | return true; | |
1619 | } | |
1620 | ||
1621 | /* Add a base class to the class on the type stack. */ | |
1622 | ||
1623 | static boolean | |
1624 | stab_class_baseclass (p, bitpos, virtual, visibility) | |
1625 | PTR p; | |
1626 | bfd_vma bitpos; | |
1627 | boolean virtual; | |
1628 | enum debug_visibility visibility; | |
1629 | { | |
1630 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1631 | boolean definition; | |
1632 | char *s; | |
1633 | char *buf; | |
1634 | unsigned int c; | |
1635 | char **baseclasses; | |
1636 | ||
1637 | definition = info->type_stack->definition; | |
1638 | s = stab_pop_type (info); | |
1639 | ||
1640 | /* Build the base class specifier. */ | |
1641 | ||
1642 | buf = (char *) xmalloc (strlen (s) + 25); | |
1643 | buf[0] = virtual ? '1' : '0'; | |
1644 | switch (visibility) | |
1645 | { | |
1646 | default: | |
1647 | abort (); | |
1648 | ||
1649 | case DEBUG_VISIBILITY_PRIVATE: | |
1650 | buf[1] = '0'; | |
1651 | break; | |
1652 | ||
1653 | case DEBUG_VISIBILITY_PROTECTED: | |
1654 | buf[1] = '1'; | |
1655 | break; | |
1656 | ||
1657 | case DEBUG_VISIBILITY_PUBLIC: | |
1658 | buf[1] = '2'; | |
1659 | break; | |
1660 | } | |
1661 | ||
1662 | sprintf (buf + 2, "%ld,%s;", (long) bitpos, s); | |
1663 | free (s); | |
1664 | ||
1665 | /* Add the new baseclass to the existing ones. */ | |
1666 | ||
1667 | assert (info->type_stack != NULL && info->type_stack->fields != NULL); | |
1668 | ||
1669 | if (info->type_stack->baseclasses == NULL) | |
1670 | c = 0; | |
1671 | else | |
1672 | { | |
1673 | c = 0; | |
1674 | while (info->type_stack->baseclasses[c] != NULL) | |
1675 | ++c; | |
1676 | } | |
1677 | ||
1678 | baseclasses = (char **) xrealloc (info->type_stack->baseclasses, | |
1679 | (c + 2) * sizeof (*baseclasses)); | |
1680 | baseclasses[c] = buf; | |
1681 | baseclasses[c + 1] = NULL; | |
1682 | ||
1683 | info->type_stack->baseclasses = baseclasses; | |
1684 | ||
1685 | if (definition) | |
1686 | info->type_stack->definition = true; | |
1687 | ||
1688 | return true; | |
1689 | } | |
1690 | ||
1691 | /* Start adding a method to the class on the type stack. */ | |
1692 | ||
1693 | static boolean | |
1694 | stab_class_start_method (p, name) | |
1695 | PTR p; | |
1696 | const char *name; | |
1697 | { | |
1698 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1699 | char *m; | |
1700 | ||
1701 | assert (info->type_stack != NULL && info->type_stack->fields != NULL); | |
1702 | ||
1703 | if (info->type_stack->methods == NULL) | |
1704 | { | |
1705 | m = (char *) xmalloc (strlen (name) + 3); | |
1706 | *m = '\0'; | |
1707 | } | |
1708 | else | |
1709 | { | |
1710 | m = (char *) xrealloc (info->type_stack->methods, | |
1711 | (strlen (info->type_stack->methods) | |
1712 | + strlen (name) | |
1713 | + 4)); | |
1714 | } | |
1715 | ||
1716 | sprintf (m + strlen (m), "%s::", name); | |
1717 | ||
1718 | info->type_stack->methods = m; | |
1719 | ||
1720 | return true; | |
1721 | } | |
1722 | ||
1723 | /* Add a variant, either static or not, to the current method. */ | |
1724 | ||
1725 | static boolean | |
1726 | stab_class_method_var (info, physname, visibility, staticp, constp, volatilep, | |
1727 | voffset, contextp) | |
1728 | struct stab_write_handle *info; | |
1729 | const char *physname; | |
1730 | enum debug_visibility visibility; | |
1731 | boolean staticp; | |
1732 | boolean constp; | |
1733 | boolean volatilep; | |
1734 | bfd_vma voffset; | |
1735 | boolean contextp; | |
1736 | { | |
1737 | boolean definition; | |
1738 | char *type; | |
1739 | char *context = NULL; | |
1740 | char visc, qualc, typec; | |
1741 | ||
1742 | definition = info->type_stack->definition; | |
1743 | type = stab_pop_type (info); | |
1744 | ||
1745 | if (contextp) | |
1746 | { | |
1747 | definition = definition || info->type_stack->definition; | |
1748 | context = stab_pop_type (info); | |
1749 | } | |
1750 | ||
1751 | assert (info->type_stack != NULL && info->type_stack->methods != NULL); | |
1752 | ||
1753 | switch (visibility) | |
1754 | { | |
1755 | default: | |
1756 | abort (); | |
1757 | ||
1758 | case DEBUG_VISIBILITY_PRIVATE: | |
1759 | visc = '0'; | |
1760 | break; | |
1761 | ||
1762 | case DEBUG_VISIBILITY_PROTECTED: | |
1763 | visc = '1'; | |
1764 | break; | |
1765 | ||
1766 | case DEBUG_VISIBILITY_PUBLIC: | |
1767 | visc = '2'; | |
1768 | break; | |
1769 | } | |
1770 | ||
1771 | if (constp) | |
1772 | { | |
1773 | if (volatilep) | |
1774 | qualc = 'D'; | |
1775 | else | |
1776 | qualc = 'B'; | |
1777 | } | |
1778 | else | |
1779 | { | |
1780 | if (volatilep) | |
1781 | qualc = 'C'; | |
1782 | else | |
1783 | qualc = 'A'; | |
1784 | } | |
1785 | ||
1786 | if (staticp) | |
1787 | typec = '?'; | |
1788 | else if (! contextp) | |
1789 | typec = '.'; | |
1790 | else | |
1791 | typec = '*'; | |
1792 | ||
1793 | info->type_stack->methods = | |
1794 | (char *) xrealloc (info->type_stack->methods, | |
1795 | (strlen (info->type_stack->methods) | |
1796 | + strlen (type) | |
1797 | + strlen (physname) | |
1798 | + (contextp ? strlen (context) : 0) | |
1799 | + 40)); | |
1800 | ||
1801 | sprintf (info->type_stack->methods + strlen (info->type_stack->methods), | |
1802 | "%s:%s;%c%c%c", type, physname, visc, qualc, typec); | |
1803 | free (type); | |
1804 | ||
1805 | if (contextp) | |
1806 | { | |
1807 | sprintf (info->type_stack->methods + strlen (info->type_stack->methods), | |
1808 | "%ld;%s;", (long) voffset, context); | |
1809 | free (context); | |
1810 | } | |
1811 | ||
1812 | if (definition) | |
1813 | info->type_stack->definition = true; | |
1814 | ||
1815 | return true; | |
1816 | } | |
1817 | ||
1818 | /* Add a variant to the current method. */ | |
1819 | ||
1820 | static boolean | |
1821 | stab_class_method_variant (p, physname, visibility, constp, volatilep, | |
1822 | voffset, contextp) | |
1823 | PTR p; | |
1824 | const char *physname; | |
1825 | enum debug_visibility visibility; | |
1826 | boolean constp; | |
1827 | boolean volatilep; | |
1828 | bfd_vma voffset; | |
1829 | boolean contextp; | |
1830 | { | |
1831 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1832 | ||
1833 | return stab_class_method_var (info, physname, visibility, false, constp, | |
1834 | volatilep, voffset, contextp); | |
1835 | } | |
1836 | ||
1837 | /* Add a static variant to the current method. */ | |
1838 | ||
1839 | static boolean | |
1840 | stab_class_static_method_variant (p, physname, visibility, constp, volatilep) | |
1841 | PTR p; | |
1842 | const char *physname; | |
1843 | enum debug_visibility visibility; | |
1844 | boolean constp; | |
1845 | boolean volatilep; | |
1846 | { | |
1847 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1848 | ||
1849 | return stab_class_method_var (info, physname, visibility, true, constp, | |
1850 | volatilep, 0, false); | |
1851 | } | |
1852 | ||
1853 | /* Finish up a method. */ | |
1854 | ||
1855 | static boolean | |
1856 | stab_class_end_method (p) | |
1857 | PTR p; | |
1858 | { | |
1859 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1860 | ||
1861 | assert (info->type_stack != NULL && info->type_stack->methods != NULL); | |
1862 | ||
1863 | /* We allocated enough room on info->type_stack->methods to add the | |
1864 | trailing semicolon. */ | |
1865 | strcat (info->type_stack->methods, ";"); | |
1866 | ||
1867 | return true; | |
1868 | } | |
1869 | ||
1870 | /* Finish up a class. */ | |
1871 | ||
1872 | static boolean | |
1873 | stab_end_class_type (p) | |
1874 | PTR p; | |
1875 | { | |
1876 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1877 | size_t len; | |
1878 | unsigned int i; | |
1879 | char *buf; | |
1880 | ||
1881 | assert (info->type_stack != NULL && info->type_stack->fields != NULL); | |
1882 | ||
1883 | /* Work out the size we need to allocate for the class definition. */ | |
1884 | ||
1885 | len = (strlen (info->type_stack->string) | |
1886 | + strlen (info->type_stack->fields) | |
1887 | + 10); | |
1888 | if (info->type_stack->baseclasses != NULL) | |
1889 | { | |
1890 | len += 20; | |
1891 | for (i = 0; info->type_stack->baseclasses[i] != NULL; i++) | |
1892 | len += strlen (info->type_stack->baseclasses[i]); | |
1893 | } | |
1894 | if (info->type_stack->methods != NULL) | |
1895 | len += strlen (info->type_stack->methods); | |
1896 | if (info->type_stack->vtable != NULL) | |
1897 | len += strlen (info->type_stack->vtable); | |
1898 | ||
1899 | /* Build the class definition. */ | |
1900 | ||
1901 | buf = (char *) xmalloc (len); | |
1902 | ||
1903 | strcpy (buf, info->type_stack->string); | |
1904 | ||
1905 | if (info->type_stack->baseclasses != NULL) | |
1906 | { | |
1907 | sprintf (buf + strlen (buf), "!%u,", i); | |
1908 | for (i = 0; info->type_stack->baseclasses[i] != NULL; i++) | |
1909 | { | |
1910 | strcat (buf, info->type_stack->baseclasses[i]); | |
1911 | free (info->type_stack->baseclasses[i]); | |
1912 | } | |
1913 | free (info->type_stack->baseclasses); | |
1914 | info->type_stack->baseclasses = NULL; | |
1915 | } | |
1916 | ||
1917 | strcat (buf, info->type_stack->fields); | |
1918 | free (info->type_stack->fields); | |
1919 | info->type_stack->fields = NULL; | |
1920 | ||
1921 | if (info->type_stack->methods != NULL) | |
1922 | { | |
1923 | strcat (buf, info->type_stack->methods); | |
1924 | free (info->type_stack->methods); | |
1925 | info->type_stack->methods = NULL; | |
1926 | } | |
1927 | ||
1928 | strcat (buf, ";"); | |
1929 | ||
1930 | if (info->type_stack->vtable != NULL) | |
1931 | { | |
1932 | strcat (buf, info->type_stack->vtable); | |
1933 | free (info->type_stack->vtable); | |
1934 | info->type_stack->vtable = NULL; | |
1935 | } | |
1936 | ||
1937 | /* Replace the string on the top of the stack with the complete | |
1938 | class definition. */ | |
1939 | free (info->type_stack->string); | |
1940 | info->type_stack->string = buf; | |
1941 | ||
1942 | return true; | |
1943 | } | |
1944 | ||
1945 | /* Push a typedef which was previously defined. */ | |
1946 | ||
1947 | static boolean | |
1948 | stab_typedef_type (p, name) | |
1949 | PTR p; | |
1950 | const char *name; | |
1951 | { | |
1952 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1953 | struct string_hash_entry *h; | |
1954 | ||
1955 | h = string_hash_lookup (&info->typedef_hash, name, false, false); | |
1956 | assert (h != NULL && h->index > 0); | |
1957 | ||
1958 | return stab_push_defined_type (info, h->index, h->size); | |
1959 | } | |
1960 | ||
1961 | /* Push a struct, union or class tag. */ | |
1962 | ||
1963 | static boolean | |
1964 | stab_tag_type (p, name, id, kind) | |
1965 | PTR p; | |
1966 | const char *name; | |
1967 | unsigned int id; | |
1968 | enum debug_type_kind kind; | |
1969 | { | |
1970 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1971 | long index; | |
1972 | unsigned int size; | |
1973 | ||
1974 | index = stab_get_struct_index (info, name, id, kind, &size); | |
1975 | if (index < 0) | |
1976 | return false; | |
1977 | ||
1978 | return stab_push_defined_type (info, index, size); | |
1979 | } | |
1980 | ||
1981 | /* Define a typedef. */ | |
1982 | ||
1983 | static boolean | |
1984 | stab_typdef (p, name) | |
1985 | PTR p; | |
1986 | const char *name; | |
1987 | { | |
1988 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
1989 | long index; | |
1990 | unsigned int size; | |
1991 | char *s, *buf; | |
1992 | struct string_hash_entry *h; | |
1993 | ||
1994 | index = info->type_stack->index; | |
1995 | size = info->type_stack->size; | |
1996 | s = stab_pop_type (info); | |
1997 | ||
1998 | buf = (char *) xmalloc (strlen (name) + strlen (s) + 20); | |
1999 | ||
2000 | if (index > 0) | |
2001 | sprintf (buf, "%s:t%s", name, s); | |
2002 | else | |
2003 | { | |
2004 | index = info->type_index; | |
2005 | ++info->type_index; | |
2006 | sprintf (buf, "%s:t%ld=%s", name, index, s); | |
2007 | } | |
2008 | ||
2009 | free (s); | |
2010 | ||
2011 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)) | |
2012 | return false; | |
2013 | ||
2014 | free (buf); | |
2015 | ||
2016 | h = string_hash_lookup (&info->typedef_hash, name, true, false); | |
2017 | if (h == NULL) | |
2018 | { | |
2019 | fprintf (stderr, "string_hash_lookup failed: %s\n", | |
2020 | bfd_errmsg (bfd_get_error ())); | |
2021 | return false; | |
2022 | } | |
2023 | ||
2024 | /* I don't think we care about redefinitions. */ | |
2025 | ||
2026 | h->index = index; | |
2027 | h->size = size; | |
2028 | ||
2029 | return true; | |
2030 | } | |
2031 | ||
2032 | /* Define a tag. */ | |
2033 | ||
2034 | static boolean | |
2035 | stab_tag (p, tag) | |
2036 | PTR p; | |
2037 | const char *tag; | |
2038 | { | |
2039 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2040 | char *s, *buf; | |
2041 | ||
2042 | s = stab_pop_type (info); | |
2043 | ||
2044 | buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3); | |
2045 | ||
2046 | sprintf (buf, "%s:T%s", tag, s); | |
2047 | free (s); | |
2048 | ||
2049 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)) | |
2050 | return false; | |
2051 | ||
2052 | free (buf); | |
2053 | ||
2054 | return true; | |
2055 | } | |
2056 | ||
2057 | /* Define an integer constant. */ | |
2058 | ||
2059 | static boolean | |
2060 | stab_int_constant (p, name, val) | |
2061 | PTR p; | |
2062 | const char *name; | |
2063 | bfd_vma val; | |
2064 | { | |
2065 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2066 | char *buf; | |
2067 | ||
2068 | buf = (char *) xmalloc (strlen (name) + 20); | |
2069 | sprintf (buf, "%s:c=i%ld", name, (long) val); | |
2070 | ||
2071 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)) | |
2072 | return false; | |
2073 | ||
2074 | free (buf); | |
2075 | ||
2076 | return true; | |
2077 | } | |
2078 | ||
2079 | /* Define a floating point constant. */ | |
2080 | ||
2081 | static boolean | |
2082 | stab_float_constant (p, name, val) | |
2083 | PTR p; | |
2084 | const char *name; | |
2085 | double val; | |
2086 | { | |
2087 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2088 | char *buf; | |
2089 | ||
2090 | buf = (char *) xmalloc (strlen (name) + 20); | |
2091 | sprintf (buf, "%s:c=f%g", name, val); | |
2092 | ||
2093 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)) | |
2094 | return false; | |
2095 | ||
2096 | free (buf); | |
2097 | ||
2098 | return true; | |
2099 | } | |
2100 | ||
2101 | /* Define a typed constant. */ | |
2102 | ||
2103 | static boolean | |
2104 | stab_typed_constant (p, name, val) | |
2105 | PTR p; | |
2106 | const char *name; | |
2107 | bfd_vma val; | |
2108 | { | |
2109 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2110 | char *s, *buf; | |
2111 | ||
2112 | s = stab_pop_type (info); | |
2113 | ||
2114 | buf = (char *) xmalloc (strlen (name) + strlen (s) + 20); | |
2115 | sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val); | |
2116 | free (s); | |
2117 | ||
2118 | if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)) | |
2119 | return false; | |
2120 | ||
2121 | free (buf); | |
2122 | ||
2123 | return true; | |
2124 | } | |
2125 | ||
2126 | /* Record a variable. */ | |
2127 | ||
2128 | static boolean | |
2129 | stab_variable (p, name, kind, val) | |
2130 | PTR p; | |
2131 | const char *name; | |
2132 | enum debug_var_kind kind; | |
2133 | bfd_vma val; | |
2134 | { | |
2135 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2136 | char *s, *buf; | |
2137 | int stab_type; | |
2138 | const char *kindstr; | |
2139 | ||
2140 | s = stab_pop_type (info); | |
2141 | ||
2142 | switch (kind) | |
2143 | { | |
2144 | default: | |
2145 | abort (); | |
2146 | ||
2147 | case DEBUG_GLOBAL: | |
2148 | stab_type = N_GSYM; | |
2149 | kindstr = "G"; | |
2150 | break; | |
2151 | ||
2152 | case DEBUG_STATIC: | |
2153 | stab_type = N_STSYM; | |
2154 | kindstr = "S"; | |
2155 | break; | |
2156 | ||
2157 | case DEBUG_LOCAL_STATIC: | |
2158 | stab_type = N_STSYM; | |
2159 | kindstr = "V"; | |
2160 | break; | |
2161 | ||
2162 | case DEBUG_LOCAL: | |
2163 | stab_type = N_LSYM; | |
2164 | kindstr = ""; | |
2165 | ||
2166 | /* Make sure that this is a type reference or definition. */ | |
2167 | if (! isdigit (*s)) | |
2168 | { | |
2169 | char *n; | |
2170 | long index; | |
2171 | ||
2172 | index = info->type_index; | |
2173 | ++info->type_index; | |
2174 | n = (char *) xmalloc (strlen (s) + 20); | |
2175 | sprintf (n, "%ld=%s", index, s); | |
2176 | free (s); | |
2177 | s = n; | |
2178 | } | |
2179 | break; | |
2180 | ||
2181 | case DEBUG_REGISTER: | |
2182 | stab_type = N_LSYM; | |
2183 | kindstr = "r"; | |
2184 | break; | |
2185 | } | |
2186 | ||
2187 | buf = (char *) xmalloc (strlen (name) + strlen (s) + 3); | |
2188 | sprintf (buf, "%s:%s%s", name, kindstr, s); | |
2189 | free (s); | |
2190 | ||
2191 | if (! stab_write_symbol (info, stab_type, 0, val, buf)) | |
2192 | return false; | |
2193 | ||
2194 | free (buf); | |
2195 | ||
2196 | return true; | |
2197 | } | |
2198 | ||
2199 | /* Start outputting a function. */ | |
2200 | ||
2201 | static boolean | |
2202 | stab_start_function (p, name, globalp) | |
2203 | PTR p; | |
2204 | const char *name; | |
2205 | boolean globalp; | |
2206 | { | |
2207 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2208 | char *rettype, *buf; | |
2209 | ||
2210 | assert (info->nesting == 0 && info->fun_offset == -1); | |
2211 | ||
2212 | rettype = stab_pop_type (info); | |
2213 | ||
2214 | buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3); | |
2215 | sprintf (buf, "%s:%c%s", name, | |
2216 | globalp ? 'F' : 'f', | |
2217 | rettype); | |
2218 | ||
2219 | /* We don't know the value now, so we set it in start_block. */ | |
2220 | info->fun_offset = info->symbols_size; | |
2221 | ||
2222 | if (! stab_write_symbol (info, N_FUN, 0, 0, buf)) | |
2223 | return false; | |
2224 | ||
2225 | free (buf); | |
2226 | ||
2227 | return true; | |
2228 | } | |
2229 | ||
2230 | /* Output a function parameter. */ | |
2231 | ||
2232 | static boolean | |
2233 | stab_function_parameter (p, name, kind, val) | |
2234 | PTR p; | |
2235 | const char *name; | |
2236 | enum debug_parm_kind kind; | |
2237 | bfd_vma val; | |
2238 | { | |
2239 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2240 | char *s, *buf; | |
2241 | int stab_type; | |
2242 | char kindc; | |
2243 | ||
2244 | s = stab_pop_type (info); | |
2245 | ||
2246 | switch (kind) | |
2247 | { | |
2248 | default: | |
2249 | abort (); | |
2250 | ||
2251 | case DEBUG_PARM_STACK: | |
2252 | stab_type = N_PSYM; | |
2253 | kindc = 'p'; | |
2254 | break; | |
2255 | ||
2256 | case DEBUG_PARM_REG: | |
2257 | stab_type = N_RSYM; | |
2258 | kindc = 'P'; | |
2259 | break; | |
2260 | ||
2261 | case DEBUG_PARM_REFERENCE: | |
2262 | stab_type = N_PSYM; | |
2263 | kindc = 'v'; | |
2264 | break; | |
2265 | ||
2266 | case DEBUG_PARM_REF_REG: | |
2267 | stab_type = N_RSYM; | |
2268 | kindc = 'a'; | |
2269 | break; | |
2270 | } | |
2271 | ||
2272 | buf = (char *) xmalloc (strlen (name) + strlen (s) + 3); | |
2273 | sprintf (buf, "%s:%c%s", name, kindc, s); | |
2274 | free (s); | |
2275 | ||
2276 | if (! stab_write_symbol (info, stab_type, 0, val, buf)) | |
2277 | return false; | |
2278 | ||
2279 | free (buf); | |
2280 | ||
2281 | return true; | |
2282 | } | |
2283 | ||
2284 | /* Start a block. */ | |
2285 | ||
2286 | static boolean | |
2287 | stab_start_block (p, addr) | |
2288 | PTR p; | |
2289 | bfd_vma addr; | |
2290 | { | |
2291 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2292 | ||
2293 | /* Fill in any slots which have been waiting for the first known | |
2294 | text address. */ | |
2295 | ||
2296 | if (info->so_offset != -1) | |
2297 | { | |
2298 | bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8); | |
2299 | info->so_offset = -1; | |
2300 | } | |
2301 | ||
2302 | if (info->fun_offset != -1) | |
2303 | { | |
2304 | bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8); | |
2305 | info->fun_offset = -1; | |
2306 | } | |
2307 | ||
2308 | ++info->nesting; | |
2309 | ||
2310 | /* We will be called with a top level block surrounding the | |
2311 | function, but stabs information does not output that block, so we | |
2312 | ignore it. */ | |
2313 | ||
2314 | if (info->nesting == 1) | |
2315 | { | |
2316 | info->fnaddr = addr; | |
2317 | return true; | |
2318 | } | |
2319 | ||
2320 | /* We have to output the LBRAC symbol after any variables which are | |
2321 | declared inside the block. We postpone the LBRAC until the next | |
2322 | start_block or end_block. */ | |
2323 | ||
2324 | /* If we have postponed an LBRAC, output it now. */ | |
2325 | if (info->pending_lbrac != (bfd_vma) -1) | |
2326 | { | |
2327 | if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac, | |
2328 | (const char *) NULL)) | |
2329 | return false; | |
2330 | } | |
2331 | ||
2332 | /* Remember the address and output it later. */ | |
2333 | ||
2334 | info->pending_lbrac = addr - info->fnaddr; | |
2335 | ||
2336 | return true; | |
2337 | } | |
2338 | ||
2339 | /* End a block. */ | |
2340 | ||
2341 | static boolean | |
2342 | stab_end_block (p, addr) | |
2343 | PTR p; | |
2344 | bfd_vma addr; | |
2345 | { | |
2346 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2347 | ||
2348 | if (addr > info->last_text_address) | |
2349 | info->last_text_address = addr; | |
2350 | ||
2351 | /* If we have postponed an LBRAC, output it now. */ | |
2352 | if (info->pending_lbrac != (bfd_vma) -1) | |
2353 | { | |
2354 | if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac, | |
2355 | (const char *) NULL)) | |
2356 | return false; | |
2357 | info->pending_lbrac = (bfd_vma) -1; | |
2358 | } | |
2359 | ||
2360 | assert (info->nesting > 0); | |
2361 | ||
2362 | --info->nesting; | |
2363 | ||
2364 | /* We ignore the outermost block. */ | |
2365 | if (info->nesting == 0) | |
2366 | return true; | |
2367 | ||
2368 | return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr, | |
2369 | (const char *) NULL); | |
2370 | } | |
2371 | ||
2372 | /* End a function. */ | |
2373 | ||
2374 | /*ARGSUSED*/ | |
2375 | static boolean | |
2376 | stab_end_function (p) | |
2377 | PTR p; | |
2378 | { | |
2379 | return true; | |
2380 | } | |
2381 | ||
2382 | /* Output a line number. */ | |
2383 | ||
2384 | static boolean | |
2385 | stab_lineno (p, file, lineno, addr) | |
2386 | PTR p; | |
2387 | const char *file; | |
2388 | unsigned long lineno; | |
2389 | bfd_vma addr; | |
2390 | { | |
2391 | struct stab_write_handle *info = (struct stab_write_handle *) p; | |
2392 | ||
2393 | assert (info->lineno_filename != NULL); | |
2394 | ||
2395 | if (addr > info->last_text_address) | |
2396 | info->last_text_address = addr; | |
2397 | ||
2398 | if (strcmp (file, info->lineno_filename) != 0) | |
2399 | { | |
2400 | if (! stab_write_symbol (info, N_SOL, 0, addr, file)) | |
2401 | return false; | |
2402 | info->lineno_filename = file; | |
2403 | } | |
2404 | ||
2405 | return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr, | |
2406 | (const char *) NULL); | |
2407 | } |