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