Remove support for the beos file format
[binutils-gdb.git] / binutils / debug.c
blobfadd65922d66159f423e3b3f4a07b3ab81f7f0b9
1 /* debug.c -- Handle generic debugging information.
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GNU Binutils.
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 3 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
23 /* This file implements a generic debugging format. We may eventually
24 have readers which convert different formats into this generic
25 format, and writers which write it out. The initial impetus for
26 this was writing a converter from stabs to HP IEEE-695 debugging
27 format. */
29 #include "sysdep.h"
30 #include <assert.h>
31 #include "bfd.h"
32 #include "libiberty.h"
33 #include "filenames.h"
34 #include "bucomm.h"
35 #include "debug.h"
37 /* Global information we keep for debugging. A pointer to this
38 structure is the debugging handle passed to all the routines. */
40 struct debug_handle
42 /* The bfd where we objalloc memory. */
43 bfd *abfd;
44 /* A linked list of compilation units. */
45 struct debug_unit *units;
46 /* The current compilation unit. */
47 struct debug_unit *current_unit;
48 /* The current source file. */
49 struct debug_file *current_file;
50 /* The current function. */
51 struct debug_function *current_function;
52 /* The current block. */
53 struct debug_block *current_block;
54 /* The current line number information for the current unit. */
55 struct debug_lineno *current_lineno;
56 /* Mark. This is used by debug_write. */
57 unsigned int mark;
58 /* A struct/class ID used by debug_write. */
59 unsigned int class_id;
60 /* The base for class_id for this call to debug_write. */
61 unsigned int base_id;
62 /* The current line number in debug_write. */
63 struct debug_lineno *current_write_lineno;
64 unsigned int current_write_lineno_index;
65 /* A list of classes which have assigned ID's during debug_write.
66 This is linked through the next_id field of debug_class_type. */
67 struct debug_class_id *id_list;
68 /* A list used to avoid recursion during debug_type_samep. */
69 struct debug_type_compare_list *compare_list;
72 /* Information we keep for a single compilation unit. */
74 struct debug_unit
76 /* The next compilation unit. */
77 struct debug_unit *next;
78 /* A list of files included in this compilation unit. The first
79 file is always the main one, and that is where the main file name
80 is stored. */
81 struct debug_file *files;
82 /* Line number information for this compilation unit. This is not
83 stored by function, because assembler code may have line number
84 information without function information. */
85 struct debug_lineno *linenos;
88 /* Information kept for a single source file. */
90 struct debug_file
92 /* The next source file in this compilation unit. */
93 struct debug_file *next;
94 /* The name of the source file. */
95 const char *filename;
96 /* Global functions, variables, types, etc. */
97 struct debug_namespace *globals;
100 /* A type. */
102 struct debug_type_s
104 /* Kind of type. */
105 enum debug_type_kind kind;
106 /* Size of type (0 if not known). */
107 unsigned int size;
108 /* Used by debug_write to stop DEBUG_KIND_INDIRECT infinite recursion. */
109 unsigned int mark;
110 /* Type which is a pointer to this type. */
111 debug_type pointer;
112 /* Tagged union with additional information about the type. */
113 union
115 /* DEBUG_KIND_INDIRECT. */
116 struct debug_indirect_type *kindirect;
117 /* DEBUG_KIND_INT. */
118 /* Whether the integer is unsigned. */
119 bool kint;
120 /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
121 DEBUG_KIND_UNION_CLASS. */
122 struct debug_class_type *kclass;
123 /* DEBUG_KIND_ENUM. */
124 struct debug_enum_type *kenum;
125 /* DEBUG_KIND_POINTER. */
126 struct debug_type_s *kpointer;
127 /* DEBUG_KIND_FUNCTION. */
128 struct debug_function_type *kfunction;
129 /* DEBUG_KIND_REFERENCE. */
130 struct debug_type_s *kreference;
131 /* DEBUG_KIND_RANGE. */
132 struct debug_range_type *krange;
133 /* DEBUG_KIND_ARRAY. */
134 struct debug_array_type *karray;
135 /* DEBUG_KIND_SET. */
136 struct debug_set_type *kset;
137 /* DEBUG_KIND_OFFSET. */
138 struct debug_offset_type *koffset;
139 /* DEBUG_KIND_METHOD. */
140 struct debug_method_type *kmethod;
141 /* DEBUG_KIND_CONST. */
142 struct debug_type_s *kconst;
143 /* DEBUG_KIND_VOLATILE. */
144 struct debug_type_s *kvolatile;
145 /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED. */
146 struct debug_named_type *knamed;
147 } u;
150 /* Information kept for an indirect type. */
152 struct debug_indirect_type
154 /* Slot where the final type will appear. */
155 debug_type *slot;
156 /* Tag. */
157 const char *tag;
160 /* Information kept for a struct, union, or class. */
162 struct debug_class_type
164 /* NULL terminated array of fields. */
165 debug_field *fields;
166 /* A mark field which indicates whether the struct has already been
167 printed. */
168 unsigned int mark;
169 /* This is used to uniquely identify unnamed structs when printing. */
170 unsigned int id;
171 /* The remaining fields are only used for DEBUG_KIND_CLASS and
172 DEBUG_KIND_UNION_CLASS. */
173 /* NULL terminated array of base classes. */
174 debug_baseclass *baseclasses;
175 /* NULL terminated array of methods. */
176 debug_method *methods;
177 /* The type of the class providing the virtual function table for
178 this class. This may point to the type itself. */
179 debug_type vptrbase;
182 /* Information kept for an enum. */
184 struct debug_enum_type
186 /* NULL terminated array of names. */
187 const char **names;
188 /* Array of corresponding values. */
189 bfd_signed_vma *values;
192 /* Information kept for a function. FIXME: We should be able to
193 record the parameter types. */
195 struct debug_function_type
197 /* Return type. */
198 debug_type return_type;
199 /* NULL terminated array of argument types. */
200 debug_type *arg_types;
201 /* Whether the function takes a variable number of arguments. */
202 bool varargs;
205 /* Information kept for a range. */
207 struct debug_range_type
209 /* Range base type. */
210 debug_type type;
211 /* Lower bound. */
212 bfd_signed_vma lower;
213 /* Upper bound. */
214 bfd_signed_vma upper;
217 /* Information kept for an array. */
219 struct debug_array_type
221 /* Element type. */
222 debug_type element_type;
223 /* Range type. */
224 debug_type range_type;
225 /* Lower bound. */
226 bfd_signed_vma lower;
227 /* Upper bound. */
228 bfd_signed_vma upper;
229 /* Whether this array is really a string. */
230 bool stringp;
233 /* Information kept for a set. */
235 struct debug_set_type
237 /* Base type. */
238 debug_type type;
239 /* Whether this set is really a bitstring. */
240 bool bitstringp;
243 /* Information kept for an offset type (a based pointer). */
245 struct debug_offset_type
247 /* The type the pointer is an offset from. */
248 debug_type base_type;
249 /* The type the pointer points to. */
250 debug_type target_type;
253 /* Information kept for a method type. */
255 struct debug_method_type
257 /* The return type. */
258 debug_type return_type;
259 /* The object type which this method is for. */
260 debug_type domain_type;
261 /* A NULL terminated array of argument types. */
262 debug_type *arg_types;
263 /* Whether the method takes a variable number of arguments. */
264 bool varargs;
267 /* Information kept for a named type. */
269 struct debug_named_type
271 /* Name. */
272 struct debug_name *name;
273 /* Real type. */
274 debug_type type;
277 /* A field in a struct or union. */
279 struct debug_field_s
281 /* Name of the field. */
282 const char *name;
283 /* Type of the field. */
284 struct debug_type_s *type;
285 /* Visibility of the field. */
286 enum debug_visibility visibility;
287 /* Whether this is a static member. */
288 bool static_member;
289 union
291 /* If static_member is false. */
292 struct
294 /* Bit position of the field in the struct. */
295 unsigned int bitpos;
296 /* Size of the field in bits. */
297 unsigned int bitsize;
298 } f;
299 /* If static_member is true. */
300 struct
302 const char *physname;
303 } s;
304 } u;
307 /* A base class for an object. */
309 struct debug_baseclass_s
311 /* Type of the base class. */
312 struct debug_type_s *type;
313 /* Bit position of the base class in the object. */
314 unsigned int bitpos;
315 /* Whether the base class is virtual. */
316 bool is_virtual;
317 /* Visibility of the base class. */
318 enum debug_visibility visibility;
321 /* A method of an object. */
323 struct debug_method_s
325 /* The name of the method. */
326 const char *name;
327 /* A NULL terminated array of different types of variants. */
328 struct debug_method_variant_s **variants;
331 /* The variants of a method function of an object. These indicate
332 which method to run. */
334 struct debug_method_variant_s
336 /* The physical name of the function. */
337 const char *physname;
338 /* The type of the function. */
339 struct debug_type_s *type;
340 /* The visibility of the function. */
341 enum debug_visibility visibility;
342 /* Whether the function is const. */
343 bool constp;
344 /* Whether the function is volatile. */
345 bool volatilep;
346 /* The offset to the function in the virtual function table. */
347 bfd_vma voffset;
348 /* If voffset is VOFFSET_STATIC_METHOD, this is a static method. */
349 #define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
350 /* Context of a virtual method function. */
351 struct debug_type_s *context;
354 /* A variable. This is the information we keep for a variable object.
355 This has no name; a name is associated with a variable in a
356 debug_name structure. */
358 struct debug_variable
360 /* Kind of variable. */
361 enum debug_var_kind kind;
362 /* Type. */
363 debug_type type;
364 /* Value. The interpretation of the value depends upon kind. */
365 bfd_vma val;
368 /* A function. This has no name; a name is associated with a function
369 in a debug_name structure. */
371 struct debug_function
373 /* Return type. */
374 debug_type return_type;
375 /* Parameter information. */
376 struct debug_parameter *parameters;
377 /* Block information. The first structure on the list is the main
378 block of the function, and describes function local variables. */
379 struct debug_block *blocks;
382 /* A function parameter. */
384 struct debug_parameter
386 /* Next parameter. */
387 struct debug_parameter *next;
388 /* Name. */
389 const char *name;
390 /* Type. */
391 debug_type type;
392 /* Kind. */
393 enum debug_parm_kind kind;
394 /* Value (meaning depends upon kind). */
395 bfd_vma val;
398 /* A typed constant. */
400 struct debug_typed_constant
402 /* Type. */
403 debug_type type;
404 /* Value. FIXME: We may eventually need to support non-integral
405 values. */
406 bfd_vma val;
409 /* Information about a block within a function. */
411 struct debug_block
413 /* Next block with the same parent. */
414 struct debug_block *next;
415 /* Parent block. */
416 struct debug_block *parent;
417 /* List of child blocks. */
418 struct debug_block *children;
419 /* Start address of the block. */
420 bfd_vma start;
421 /* End address of the block. */
422 bfd_vma end;
423 /* Local variables. */
424 struct debug_namespace *locals;
427 /* Line number information we keep for a compilation unit. FIXME:
428 This structure is easy to create, but can be very space
429 inefficient. */
431 struct debug_lineno
433 /* More line number information for this block. */
434 struct debug_lineno *next;
435 /* Source file. */
436 struct debug_file *file;
437 /* Line numbers, terminated by a -1 or the end of the array. */
438 #define DEBUG_LINENO_COUNT 10
439 unsigned long linenos[DEBUG_LINENO_COUNT];
440 /* Addresses for the line numbers. */
441 bfd_vma addrs[DEBUG_LINENO_COUNT];
444 /* A namespace. This is a mapping from names to objects. FIXME: This
445 should be implemented as a hash table. */
447 struct debug_namespace
449 /* List of items in this namespace. */
450 struct debug_name *list;
451 /* Pointer to where the next item in this namespace should go. */
452 struct debug_name **tail;
455 /* Kinds of objects that appear in a namespace. */
457 enum debug_object_kind
459 /* A type. */
460 DEBUG_OBJECT_TYPE,
461 /* A tagged type (really a different sort of namespace). */
462 DEBUG_OBJECT_TAG,
463 /* A variable. */
464 DEBUG_OBJECT_VARIABLE,
465 /* A function. */
466 DEBUG_OBJECT_FUNCTION,
467 /* An integer constant. */
468 DEBUG_OBJECT_INT_CONSTANT,
469 /* A floating point constant. */
470 DEBUG_OBJECT_FLOAT_CONSTANT,
471 /* A typed constant. */
472 DEBUG_OBJECT_TYPED_CONSTANT
475 /* Linkage of an object that appears in a namespace. */
477 enum debug_object_linkage
479 /* Local variable. */
480 DEBUG_LINKAGE_AUTOMATIC,
481 /* Static--either file static or function static, depending upon the
482 namespace is. */
483 DEBUG_LINKAGE_STATIC,
484 /* Global. */
485 DEBUG_LINKAGE_GLOBAL,
486 /* No linkage. */
487 DEBUG_LINKAGE_NONE
490 /* A name in a namespace. */
492 struct debug_name
494 /* Next name in this namespace. */
495 struct debug_name *next;
496 /* Name. */
497 const char *name;
498 /* Mark. This is used by debug_write. */
499 unsigned int mark;
500 /* Kind of object. */
501 enum debug_object_kind kind;
502 /* Linkage of object. */
503 enum debug_object_linkage linkage;
504 /* Tagged union with additional information about the object. */
505 union
507 /* DEBUG_OBJECT_TYPE. */
508 struct debug_type_s *type;
509 /* DEBUG_OBJECT_TAG. */
510 struct debug_type_s *tag;
511 /* DEBUG_OBJECT_VARIABLE. */
512 struct debug_variable *variable;
513 /* DEBUG_OBJECT_FUNCTION. */
514 struct debug_function *function;
515 /* DEBUG_OBJECT_INT_CONSTANT. */
516 bfd_vma int_constant;
517 /* DEBUG_OBJECT_FLOAT_CONSTANT. */
518 double float_constant;
519 /* DEBUG_OBJECT_TYPED_CONSTANT. */
520 struct debug_typed_constant *typed_constant;
521 } u;
524 /* During debug_write, a linked list of these structures is used to
525 keep track of ID numbers that have been assigned to classes. */
527 struct debug_class_id
529 /* Next ID number. */
530 struct debug_class_id *next;
531 /* The type with the ID. */
532 struct debug_type_s *type;
533 /* The tag; NULL if no tag. */
534 const char *tag;
537 /* During debug_type_samep, a linked list of these structures is kept
538 on the stack to avoid infinite recursion. */
540 struct debug_type_compare_list
542 /* Next type on list. */
543 struct debug_type_compare_list *next;
544 /* The types we are comparing. */
545 struct debug_type_s *t1;
546 struct debug_type_s *t2;
549 /* During debug_get_real_type, a linked list of these structures is
550 kept on the stack to avoid infinite recursion. */
552 struct debug_type_real_list
554 /* Next type on list. */
555 struct debug_type_real_list *next;
556 /* The type we are checking. */
557 struct debug_type_s *t;
560 /* Local functions. */
562 static void debug_error (const char *);
563 static struct debug_name *debug_add_to_namespace
564 (struct debug_handle *, struct debug_namespace **, const char *,
565 enum debug_object_kind, enum debug_object_linkage);
566 static struct debug_name *debug_add_to_current_namespace
567 (struct debug_handle *, const char *, enum debug_object_kind,
568 enum debug_object_linkage);
569 static struct debug_type_s *debug_make_type
570 (struct debug_handle *, enum debug_type_kind, unsigned int);
571 static struct debug_type_s *debug_get_real_type
572 (void *, debug_type, struct debug_type_real_list *);
573 static bool debug_write_name
574 (struct debug_handle *, const struct debug_write_fns *, void *,
575 struct debug_name *);
576 static bool debug_write_type
577 (struct debug_handle *, const struct debug_write_fns *, void *,
578 struct debug_type_s *, struct debug_name *);
579 static bool debug_write_class_type
580 (struct debug_handle *, const struct debug_write_fns *, void *,
581 struct debug_type_s *, const char *);
582 static bool debug_write_function
583 (struct debug_handle *, const struct debug_write_fns *, void *,
584 const char *, enum debug_object_linkage, struct debug_function *);
585 static bool debug_write_block
586 (struct debug_handle *, const struct debug_write_fns *, void *,
587 struct debug_block *);
588 static bool debug_write_linenos
589 (struct debug_handle *, const struct debug_write_fns *, void *, bfd_vma);
590 static bool debug_set_class_id
591 (struct debug_handle *, const char *, struct debug_type_s *);
592 static bool debug_type_samep
593 (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
594 static bool debug_class_type_samep
595 (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
597 /* Issue an error message. */
599 static void
600 debug_error (const char *message)
602 fprintf (stderr, "%s\n", message);
605 /* Add an object to a namespace. */
607 static struct debug_name *
608 debug_add_to_namespace (struct debug_handle *info,
609 struct debug_namespace **nsp, const char *name,
610 enum debug_object_kind kind,
611 enum debug_object_linkage linkage)
613 struct debug_name *n;
614 struct debug_namespace *ns;
616 n = debug_xzalloc (info, sizeof (*n));
618 n->name = name;
619 n->kind = kind;
620 n->linkage = linkage;
622 ns = *nsp;
623 if (ns == NULL)
625 ns = debug_xzalloc (info, sizeof (*ns));
627 ns->tail = &ns->list;
629 *nsp = ns;
632 *ns->tail = n;
633 ns->tail = &n->next;
635 return n;
638 /* Add an object to the current namespace. */
640 static struct debug_name *
641 debug_add_to_current_namespace (struct debug_handle *info, const char *name,
642 enum debug_object_kind kind,
643 enum debug_object_linkage linkage)
645 struct debug_namespace **nsp;
647 if (info->current_unit == NULL
648 || info->current_file == NULL)
650 debug_error (_("debug_add_to_current_namespace: no current file"));
651 return NULL;
654 if (info->current_block != NULL)
655 nsp = &info->current_block->locals;
656 else
657 nsp = &info->current_file->globals;
659 return debug_add_to_namespace (info, nsp, name, kind, linkage);
662 /* Return a handle for debugging information. */
664 void *
665 debug_init (bfd *abfd)
667 struct debug_handle *ret;
669 ret = bfd_xalloc (abfd, sizeof (*ret));
670 memset (ret, 0, sizeof (*ret));
671 ret->abfd = abfd;
672 return ret;
675 void *
676 debug_xalloc (void *handle, size_t size)
678 struct debug_handle *info = (struct debug_handle *) handle;
679 return bfd_xalloc (info->abfd, size);
682 void *
683 debug_xzalloc (void *handle, size_t size)
685 struct debug_handle *info = (struct debug_handle *) handle;
686 void *mem = bfd_xalloc (info->abfd, size);
687 memset (mem, 0, size);
688 return mem;
691 /* Set the source filename. This implicitly starts a new compilation
692 unit. */
694 bool
695 debug_set_filename (void *handle, const char *name)
697 struct debug_handle *info = (struct debug_handle *) handle;
698 struct debug_file *nfile;
699 struct debug_unit *nunit;
701 if (name == NULL)
702 name = "";
704 nfile = debug_xzalloc (info, sizeof (*nfile));
706 nfile->filename = name;
708 nunit = debug_xzalloc (info, sizeof (*nunit));
710 nunit->files = nfile;
711 info->current_file = nfile;
713 if (info->current_unit != NULL)
714 info->current_unit->next = nunit;
715 else
717 assert (info->units == NULL);
718 info->units = nunit;
721 info->current_unit = nunit;
723 info->current_function = NULL;
724 info->current_block = NULL;
725 info->current_lineno = NULL;
727 return true;
730 /* Change source files to the given file name. This is used for
731 include files in a single compilation unit. */
733 bool
734 debug_start_source (void *handle, const char *name)
736 struct debug_handle *info = (struct debug_handle *) handle;
737 struct debug_file *f, **pf;
739 if (name == NULL)
740 name = "";
742 if (info->current_unit == NULL)
744 debug_error (_("debug_start_source: no debug_set_filename call"));
745 return false;
748 for (f = info->current_unit->files; f != NULL; f = f->next)
750 if (filename_cmp (f->filename, name) == 0)
752 info->current_file = f;
753 return true;
757 f = debug_xzalloc (info, sizeof (*f));
758 f->filename = name;
760 for (pf = &info->current_file->next;
761 *pf != NULL;
762 pf = &(*pf)->next)
764 *pf = f;
766 info->current_file = f;
768 return true;
771 /* Record a function definition. This implicitly starts a function
772 block. The debug_type argument is the type of the return value.
773 The boolean indicates whether the function is globally visible.
774 The bfd_vma is the address of the start of the function. Currently
775 the parameter types are specified by calls to
776 debug_record_parameter. FIXME: There is no way to specify nested
777 functions. */
779 bool
780 debug_record_function (void *handle, const char *name,
781 debug_type return_type, bool global,
782 bfd_vma addr)
784 struct debug_handle *info = (struct debug_handle *) handle;
785 struct debug_function *f;
786 struct debug_block *b;
787 struct debug_name *n;
789 if (name == NULL)
790 name = "";
791 if (return_type == NULL)
792 return false;
794 if (info->current_unit == NULL)
796 debug_error (_("debug_record_function: no debug_set_filename call"));
797 return false;
800 f = debug_xzalloc (info, sizeof (*f));
802 f->return_type = return_type;
804 b = debug_xzalloc (info, sizeof (*b));
806 b->start = addr;
807 b->end = (bfd_vma) -1;
809 f->blocks = b;
811 info->current_function = f;
812 info->current_block = b;
814 /* FIXME: If we could handle nested functions, this would be the
815 place: we would want to use a different namespace. */
816 n = debug_add_to_namespace (info,
817 &info->current_file->globals,
818 name,
819 DEBUG_OBJECT_FUNCTION,
820 (global
821 ? DEBUG_LINKAGE_GLOBAL
822 : DEBUG_LINKAGE_STATIC));
823 if (n == NULL)
824 return false;
826 n->u.function = f;
828 return true;
831 /* Record a parameter for the current function. */
833 bool
834 debug_record_parameter (void *handle, const char *name, debug_type type,
835 enum debug_parm_kind kind, bfd_vma val)
837 struct debug_handle *info = (struct debug_handle *) handle;
838 struct debug_parameter *p, **pp;
840 if (name == NULL || type == NULL)
841 return false;
843 if (info->current_unit == NULL
844 || info->current_function == NULL)
846 debug_error (_("debug_record_parameter: no current function"));
847 return false;
850 p = debug_xzalloc (info, sizeof (*p));
852 p->name = name;
853 p->type = type;
854 p->kind = kind;
855 p->val = val;
857 for (pp = &info->current_function->parameters;
858 *pp != NULL;
859 pp = &(*pp)->next)
861 *pp = p;
863 return true;
866 /* End a function. FIXME: This should handle function nesting. */
868 bool
869 debug_end_function (void *handle, bfd_vma addr)
871 struct debug_handle *info = (struct debug_handle *) handle;
873 if (info->current_unit == NULL
874 || info->current_block == NULL
875 || info->current_function == NULL)
877 debug_error (_("debug_end_function: no current function"));
878 return false;
881 if (info->current_block->parent != NULL)
883 debug_error (_("debug_end_function: some blocks were not closed"));
884 return false;
887 info->current_block->end = addr;
889 info->current_function = NULL;
890 info->current_block = NULL;
892 return true;
895 /* Start a block in a function. All local information will be
896 recorded in this block, until the matching call to debug_end_block.
897 debug_start_block and debug_end_block may be nested. The bfd_vma
898 argument is the address at which this block starts. */
900 bool
901 debug_start_block (void *handle, bfd_vma addr)
903 struct debug_handle *info = (struct debug_handle *) handle;
904 struct debug_block *b, **pb;
906 /* We must always have a current block: debug_record_function sets
907 one up. */
908 if (info->current_unit == NULL
909 || info->current_block == NULL)
911 debug_error (_("debug_start_block: no current block"));
912 return false;
915 b = debug_xzalloc (info, sizeof (*b));
917 b->parent = info->current_block;
918 b->start = addr;
919 b->end = (bfd_vma) -1;
921 /* This new block is a child of the current block. */
922 for (pb = &info->current_block->children;
923 *pb != NULL;
924 pb = &(*pb)->next)
926 *pb = b;
928 info->current_block = b;
930 return true;
933 /* Finish a block in a function. This matches the call to
934 debug_start_block. The argument is the address at which this block
935 ends. */
937 bool
938 debug_end_block (void *handle, bfd_vma addr)
940 struct debug_handle *info = (struct debug_handle *) handle;
941 struct debug_block *parent;
943 if (info->current_unit == NULL
944 || info->current_block == NULL)
946 debug_error (_("debug_end_block: no current block"));
947 return false;
950 parent = info->current_block->parent;
951 if (parent == NULL)
953 debug_error (_("debug_end_block: attempt to close top level block"));
954 return false;
957 info->current_block->end = addr;
959 info->current_block = parent;
961 return true;
964 /* Associate a line number in the current source file and function
965 with a given address. */
967 bool
968 debug_record_line (void *handle, unsigned long lineno, bfd_vma addr)
970 struct debug_handle *info = (struct debug_handle *) handle;
971 struct debug_lineno *l;
972 unsigned int i;
974 if (info->current_unit == NULL)
976 debug_error (_("debug_record_line: no current unit"));
977 return false;
980 l = info->current_lineno;
981 if (l != NULL && l->file == info->current_file)
983 for (i = 0; i < DEBUG_LINENO_COUNT; i++)
985 if (l->linenos[i] == (unsigned long) -1)
987 l->linenos[i] = lineno;
988 l->addrs[i] = addr;
989 return true;
994 /* If we get here, then either 1) there is no current_lineno
995 structure, which means this is the first line number in this
996 compilation unit, 2) the current_lineno structure is for a
997 different file, or 3) the current_lineno structure is full.
998 Regardless, we want to allocate a new debug_lineno structure, put
999 it in the right place, and make it the new current_lineno
1000 structure. */
1002 l = debug_xzalloc (info, sizeof (*l));
1004 l->file = info->current_file;
1005 l->linenos[0] = lineno;
1006 l->addrs[0] = addr;
1007 for (i = 1; i < DEBUG_LINENO_COUNT; i++)
1008 l->linenos[i] = (unsigned long) -1;
1010 if (info->current_lineno != NULL)
1011 info->current_lineno->next = l;
1012 else
1013 info->current_unit->linenos = l;
1015 info->current_lineno = l;
1017 return true;
1020 /* Start a named common block. This is a block of variables that may
1021 move in memory. */
1023 bool
1024 debug_start_common_block (void *handle ATTRIBUTE_UNUSED,
1025 const char *name ATTRIBUTE_UNUSED)
1027 /* FIXME */
1028 debug_error (_("debug_start_common_block: not implemented"));
1029 return false;
1032 /* End a named common block. */
1034 bool
1035 debug_end_common_block (void *handle ATTRIBUTE_UNUSED,
1036 const char *name ATTRIBUTE_UNUSED)
1038 /* FIXME */
1039 debug_error (_("debug_end_common_block: not implemented"));
1040 return false;
1043 /* Record a named integer constant. */
1045 bool
1046 debug_record_int_const (void *handle, const char *name, bfd_vma val)
1048 struct debug_handle *info = (struct debug_handle *) handle;
1049 struct debug_name *n;
1051 if (name == NULL)
1052 return false;
1054 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
1055 DEBUG_LINKAGE_NONE);
1056 if (n == NULL)
1057 return false;
1059 n->u.int_constant = val;
1061 return true;
1064 /* Record a named floating point constant. */
1066 bool
1067 debug_record_float_const (void *handle, const char *name, double val)
1069 struct debug_handle *info = (struct debug_handle *) handle;
1070 struct debug_name *n;
1072 if (name == NULL)
1073 return false;
1075 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
1076 DEBUG_LINKAGE_NONE);
1077 if (n == NULL)
1078 return false;
1080 n->u.float_constant = val;
1082 return true;
1085 /* Record a typed constant with an integral value. */
1087 bool
1088 debug_record_typed_const (void *handle, const char *name, debug_type type,
1089 bfd_vma val)
1091 struct debug_handle *info = (struct debug_handle *) handle;
1092 struct debug_name *n;
1093 struct debug_typed_constant *tc;
1095 if (name == NULL || type == NULL)
1096 return false;
1098 n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
1099 DEBUG_LINKAGE_NONE);
1100 if (n == NULL)
1101 return false;
1103 tc = debug_xzalloc (info, sizeof (*tc));
1105 tc->type = type;
1106 tc->val = val;
1108 n->u.typed_constant = tc;
1110 return true;
1113 /* Record a label. */
1115 bool
1116 debug_record_label (void *handle ATTRIBUTE_UNUSED,
1117 const char *name ATTRIBUTE_UNUSED,
1118 debug_type type ATTRIBUTE_UNUSED,
1119 bfd_vma addr ATTRIBUTE_UNUSED)
1121 /* FIXME. */
1122 debug_error (_("debug_record_label: not implemented"));
1123 return false;
1126 /* Record a variable. */
1128 bool
1129 debug_record_variable (void *handle, const char *name, debug_type type,
1130 enum debug_var_kind kind, bfd_vma val)
1132 struct debug_handle *info = (struct debug_handle *) handle;
1133 struct debug_namespace **nsp;
1134 enum debug_object_linkage linkage;
1135 struct debug_name *n;
1136 struct debug_variable *v;
1138 if (name == NULL || type == NULL)
1139 return false;
1141 if (info->current_unit == NULL
1142 || info->current_file == NULL)
1144 debug_error (_("debug_record_variable: no current file"));
1145 return false;
1148 if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
1150 nsp = &info->current_file->globals;
1151 if (kind == DEBUG_GLOBAL)
1152 linkage = DEBUG_LINKAGE_GLOBAL;
1153 else
1154 linkage = DEBUG_LINKAGE_STATIC;
1156 else
1158 if (info->current_block == NULL)
1159 nsp = &info->current_file->globals;
1160 else
1161 nsp = &info->current_block->locals;
1162 linkage = DEBUG_LINKAGE_AUTOMATIC;
1165 n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
1166 if (n == NULL)
1167 return false;
1169 v = debug_xzalloc (info, sizeof (*v));
1171 v->kind = kind;
1172 v->type = type;
1173 v->val = val;
1175 n->u.variable = v;
1177 return true;
1180 /* Make a type with a given kind and size. */
1182 static struct debug_type_s *
1183 debug_make_type (struct debug_handle *info,
1184 enum debug_type_kind kind, unsigned int size)
1186 struct debug_type_s *t;
1188 t = debug_xzalloc (info, sizeof (*t));
1190 t->kind = kind;
1191 t->size = size;
1193 return t;
1196 /* Make an indirect type which may be used as a placeholder for a type
1197 which is referenced before it is defined. */
1199 debug_type
1200 debug_make_indirect_type (void *handle, debug_type *slot, const char *tag)
1202 struct debug_handle *info = (struct debug_handle *) handle;
1203 struct debug_type_s *t;
1204 struct debug_indirect_type *i;
1206 t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
1207 if (t == NULL)
1208 return DEBUG_TYPE_NULL;
1210 i = debug_xzalloc (info, sizeof (*i));
1212 i->slot = slot;
1213 i->tag = tag;
1215 t->u.kindirect = i;
1217 return t;
1220 /* Make a void type. There is only one of these. */
1222 debug_type
1223 debug_make_void_type (void *handle)
1225 struct debug_handle *info = (struct debug_handle *) handle;
1227 return debug_make_type (info, DEBUG_KIND_VOID, 0);
1230 /* Make an integer type of a given size. The boolean argument is true
1231 if the integer is unsigned. */
1233 debug_type
1234 debug_make_int_type (void *handle, unsigned int size, bool unsignedp)
1236 struct debug_handle *info = (struct debug_handle *) handle;
1237 struct debug_type_s *t;
1239 t = debug_make_type (info, DEBUG_KIND_INT, size);
1240 if (t == NULL)
1241 return DEBUG_TYPE_NULL;
1243 t->u.kint = unsignedp;
1245 return t;
1248 /* Make a floating point type of a given size. FIXME: On some
1249 platforms, like an Alpha, you probably need to be able to specify
1250 the format. */
1252 debug_type
1253 debug_make_float_type (void *handle, unsigned int size)
1255 struct debug_handle *info = (struct debug_handle *) handle;
1257 return debug_make_type (info, DEBUG_KIND_FLOAT, size);
1260 /* Make a boolean type of a given size. */
1262 debug_type
1263 debug_make_bool_type (void *handle, unsigned int size)
1265 struct debug_handle *info = (struct debug_handle *) handle;
1267 return debug_make_type (info, DEBUG_KIND_BOOL, size);
1270 /* Make a complex type of a given size. */
1272 debug_type
1273 debug_make_complex_type (void *handle, unsigned int size)
1275 struct debug_handle *info = (struct debug_handle *) handle;
1277 return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
1280 /* Make a structure type. The second argument is true for a struct,
1281 false for a union. The third argument is the size of the struct.
1282 The fourth argument is a NULL terminated array of fields. */
1284 debug_type
1285 debug_make_struct_type (void *handle, bool structp, bfd_vma size,
1286 debug_field *fields)
1288 struct debug_handle *info = (struct debug_handle *) handle;
1289 struct debug_type_s *t;
1290 struct debug_class_type *c;
1292 t = debug_make_type (info,
1293 structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
1294 size);
1295 if (t == NULL)
1296 return DEBUG_TYPE_NULL;
1298 c = debug_xzalloc (info, sizeof (*c));
1300 c->fields = fields;
1302 t->u.kclass = c;
1304 return t;
1307 /* Make an object type. The first three arguments after the handle
1308 are the same as for debug_make_struct_type. The next arguments are
1309 a NULL terminated array of base classes, a NULL terminated array of
1310 methods, the type of the object holding the virtual function table
1311 if it is not this object, and a boolean which is true if this
1312 object has its own virtual function table. */
1314 debug_type
1315 debug_make_object_type (void *handle, bool structp, bfd_vma size,
1316 debug_field *fields, debug_baseclass *baseclasses,
1317 debug_method *methods, debug_type vptrbase,
1318 bool ownvptr)
1320 struct debug_handle *info = (struct debug_handle *) handle;
1321 struct debug_type_s *t;
1322 struct debug_class_type *c;
1324 t = debug_make_type (info,
1325 structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
1326 size);
1327 if (t == NULL)
1328 return DEBUG_TYPE_NULL;
1330 c = debug_xzalloc (info, sizeof (*c));
1332 c->fields = fields;
1333 c->baseclasses = baseclasses;
1334 c->methods = methods;
1335 if (ownvptr)
1336 c->vptrbase = t;
1337 else
1338 c->vptrbase = vptrbase;
1340 t->u.kclass = c;
1342 return t;
1345 /* Make an enumeration type. The arguments are a null terminated
1346 array of strings, and an array of corresponding values. */
1348 debug_type
1349 debug_make_enum_type (void *handle, const char **names,
1350 bfd_signed_vma *values)
1352 struct debug_handle *info = (struct debug_handle *) handle;
1353 struct debug_type_s *t;
1354 struct debug_enum_type *e;
1356 t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
1357 if (t == NULL)
1358 return DEBUG_TYPE_NULL;
1360 e = debug_xzalloc (info, sizeof (*e));
1362 e->names = names;
1363 e->values = values;
1365 t->u.kenum = e;
1367 return t;
1370 /* Make a pointer to a given type. */
1372 debug_type
1373 debug_make_pointer_type (void *handle, debug_type type)
1375 struct debug_handle *info = (struct debug_handle *) handle;
1376 struct debug_type_s *t;
1378 if (type == NULL)
1379 return DEBUG_TYPE_NULL;
1381 if (type->pointer != DEBUG_TYPE_NULL)
1382 return type->pointer;
1384 t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
1385 if (t == NULL)
1386 return DEBUG_TYPE_NULL;
1388 t->u.kpointer = type;
1390 type->pointer = t;
1392 return t;
1395 /* Make a function returning a given type. FIXME: We should be able
1396 to record the parameter types. */
1398 debug_type
1399 debug_make_function_type (void *handle, debug_type type,
1400 debug_type *arg_types, bool varargs)
1402 struct debug_handle *info = (struct debug_handle *) handle;
1403 struct debug_type_s *t;
1404 struct debug_function_type *f;
1406 if (type == NULL)
1407 return DEBUG_TYPE_NULL;
1409 t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
1410 if (t == NULL)
1411 return DEBUG_TYPE_NULL;
1413 f = debug_xzalloc (info, sizeof (*f));
1415 f->return_type = type;
1416 f->arg_types = arg_types;
1417 f->varargs = varargs;
1419 t->u.kfunction = f;
1421 return t;
1424 /* Make a reference to a given type. */
1426 debug_type
1427 debug_make_reference_type (void *handle, debug_type type)
1429 struct debug_handle *info = (struct debug_handle *) handle;
1430 struct debug_type_s *t;
1432 if (type == NULL)
1433 return DEBUG_TYPE_NULL;
1435 t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
1436 if (t == NULL)
1437 return DEBUG_TYPE_NULL;
1439 t->u.kreference = type;
1441 return t;
1444 /* Make a range of a given type from a lower to an upper bound. */
1446 debug_type
1447 debug_make_range_type (void *handle, debug_type type, bfd_signed_vma lower,
1448 bfd_signed_vma upper)
1450 struct debug_handle *info = (struct debug_handle *) handle;
1451 struct debug_type_s *t;
1452 struct debug_range_type *r;
1454 if (type == NULL)
1455 return DEBUG_TYPE_NULL;
1457 t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
1458 if (t == NULL)
1459 return DEBUG_TYPE_NULL;
1461 r = debug_xzalloc (info, sizeof (*r));
1463 r->type = type;
1464 r->lower = lower;
1465 r->upper = upper;
1467 t->u.krange = r;
1469 return t;
1472 /* Make an array type. The second argument is the type of an element
1473 of the array. The third argument is the type of a range of the
1474 array. The fourth and fifth argument are the lower and upper
1475 bounds, respectively. The sixth argument is true if this array is
1476 actually a string, as in C. */
1478 debug_type
1479 debug_make_array_type (void *handle, debug_type element_type,
1480 debug_type range_type, bfd_signed_vma lower,
1481 bfd_signed_vma upper, bool stringp)
1483 struct debug_handle *info = (struct debug_handle *) handle;
1484 struct debug_type_s *t;
1485 struct debug_array_type *a;
1487 if (element_type == NULL || range_type == NULL)
1488 return DEBUG_TYPE_NULL;
1490 t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
1491 if (t == NULL)
1492 return DEBUG_TYPE_NULL;
1494 a = debug_xzalloc (info, sizeof (*a));
1496 a->element_type = element_type;
1497 a->range_type = range_type;
1498 a->lower = lower;
1499 a->upper = upper;
1500 a->stringp = stringp;
1502 t->u.karray = a;
1504 return t;
1507 /* Make a set of a given type. For example, a Pascal set type. The
1508 boolean argument is true if this set is actually a bitstring, as in
1509 CHILL. */
1511 debug_type
1512 debug_make_set_type (void *handle, debug_type type, bool bitstringp)
1514 struct debug_handle *info = (struct debug_handle *) handle;
1515 struct debug_type_s *t;
1516 struct debug_set_type *s;
1518 if (type == NULL)
1519 return DEBUG_TYPE_NULL;
1521 t = debug_make_type (info, DEBUG_KIND_SET, 0);
1522 if (t == NULL)
1523 return DEBUG_TYPE_NULL;
1525 s = debug_xzalloc (info, sizeof (*s));
1527 s->type = type;
1528 s->bitstringp = bitstringp;
1530 t->u.kset = s;
1532 return t;
1535 /* Make a type for a pointer which is relative to an object. The
1536 second argument is the type of the object to which the pointer is
1537 relative. The third argument is the type that the pointer points
1538 to. */
1540 debug_type
1541 debug_make_offset_type (void *handle, debug_type base_type,
1542 debug_type target_type)
1544 struct debug_handle *info = (struct debug_handle *) handle;
1545 struct debug_type_s *t;
1546 struct debug_offset_type *o;
1548 if (base_type == NULL || target_type == NULL)
1549 return DEBUG_TYPE_NULL;
1551 t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
1552 if (t == NULL)
1553 return DEBUG_TYPE_NULL;
1555 o = debug_xzalloc (info, sizeof (*o));
1557 o->base_type = base_type;
1558 o->target_type = target_type;
1560 t->u.koffset = o;
1562 return t;
1565 /* Make a type for a method function. The second argument is the
1566 return type, the third argument is the domain, and the fourth
1567 argument is a NULL terminated array of argument types. */
1569 debug_type
1570 debug_make_method_type (void *handle, debug_type return_type,
1571 debug_type domain_type, debug_type *arg_types,
1572 bool varargs)
1574 struct debug_handle *info = (struct debug_handle *) handle;
1575 struct debug_type_s *t;
1576 struct debug_method_type *m;
1578 if (return_type == NULL)
1579 return DEBUG_TYPE_NULL;
1581 t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
1582 if (t == NULL)
1583 return DEBUG_TYPE_NULL;
1585 m = debug_xzalloc (info, sizeof (*m));
1587 m->return_type = return_type;
1588 m->domain_type = domain_type;
1589 m->arg_types = arg_types;
1590 m->varargs = varargs;
1592 t->u.kmethod = m;
1594 return t;
1597 /* Make a const qualified version of a given type. */
1599 debug_type
1600 debug_make_const_type (void *handle, debug_type type)
1602 struct debug_handle *info = (struct debug_handle *) handle;
1603 struct debug_type_s *t;
1605 if (type == NULL)
1606 return DEBUG_TYPE_NULL;
1608 t = debug_make_type (info, DEBUG_KIND_CONST, 0);
1609 if (t == NULL)
1610 return DEBUG_TYPE_NULL;
1612 t->u.kconst = type;
1614 return t;
1617 /* Make a volatile qualified version of a given type. */
1619 debug_type
1620 debug_make_volatile_type (void *handle, debug_type type)
1622 struct debug_handle *info = (struct debug_handle *) handle;
1623 struct debug_type_s *t;
1625 if (type == NULL)
1626 return DEBUG_TYPE_NULL;
1628 t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
1629 if (t == NULL)
1630 return DEBUG_TYPE_NULL;
1632 t->u.kvolatile = type;
1634 return t;
1637 /* Make an undefined tagged type. For example, a struct which has
1638 been mentioned, but not defined. */
1640 debug_type
1641 debug_make_undefined_tagged_type (void *handle, const char *name,
1642 enum debug_type_kind kind)
1644 struct debug_handle *info = (struct debug_handle *) handle;
1645 struct debug_type_s *t;
1647 if (name == NULL)
1648 return DEBUG_TYPE_NULL;
1650 switch (kind)
1652 case DEBUG_KIND_STRUCT:
1653 case DEBUG_KIND_UNION:
1654 case DEBUG_KIND_CLASS:
1655 case DEBUG_KIND_UNION_CLASS:
1656 case DEBUG_KIND_ENUM:
1657 break;
1659 default:
1660 debug_error (_("debug_make_undefined_type: unsupported kind"));
1661 return DEBUG_TYPE_NULL;
1664 t = debug_make_type (info, kind, 0);
1665 if (t == NULL)
1666 return DEBUG_TYPE_NULL;
1668 return debug_tag_type (handle, name, t);
1671 /* Make a base class for an object. The second argument is the base
1672 class type. The third argument is the bit position of this base
1673 class in the object (always 0 unless doing multiple inheritance).
1674 The fourth argument is whether this is a virtual class. The fifth
1675 argument is the visibility of the base class. */
1677 debug_baseclass
1678 debug_make_baseclass (void *handle, debug_type type,
1679 bfd_vma bitpos, bool is_virtual,
1680 enum debug_visibility visibility)
1682 struct debug_handle *info = (struct debug_handle *) handle;
1683 struct debug_baseclass_s *b;
1685 b = debug_xzalloc (info, sizeof (*b));
1687 b->type = type;
1688 b->bitpos = bitpos;
1689 b->is_virtual = is_virtual;
1690 b->visibility = visibility;
1692 return b;
1695 /* Make a field for a struct. The second argument is the name. The
1696 third argument is the type of the field. The fourth argument is
1697 the bit position of the field. The fifth argument is the size of
1698 the field (it may be zero). The sixth argument is the visibility
1699 of the field. */
1701 debug_field
1702 debug_make_field (void *handle, const char *name,
1703 debug_type type, bfd_vma bitpos, bfd_vma bitsize,
1704 enum debug_visibility visibility)
1706 struct debug_handle *info = (struct debug_handle *) handle;
1707 struct debug_field_s *f;
1709 f = debug_xzalloc (info, sizeof (*f));
1711 f->name = name;
1712 f->type = type;
1713 f->static_member = false;
1714 f->u.f.bitpos = bitpos;
1715 f->u.f.bitsize = bitsize;
1716 f->visibility = visibility;
1718 return f;
1721 /* Make a static member of an object. The second argument is the
1722 name. The third argument is the type of the member. The fourth
1723 argument is the physical name of the member (i.e., the name as a
1724 global variable). The fifth argument is the visibility of the
1725 member. */
1727 debug_field
1728 debug_make_static_member (void *handle, const char *name,
1729 debug_type type, const char *physname,
1730 enum debug_visibility visibility)
1732 struct debug_handle *info = (struct debug_handle *) handle;
1733 struct debug_field_s *f;
1735 f = debug_xzalloc (info, sizeof (*f));
1737 f->name = name;
1738 f->type = type;
1739 f->static_member = true;
1740 f->u.s.physname = physname;
1741 f->visibility = visibility;
1743 return f;
1746 /* Make a method. The second argument is the name, and the third
1747 argument is a NULL terminated array of method variants. */
1749 debug_method
1750 debug_make_method (void *handle, const char *name,
1751 debug_method_variant *variants)
1753 struct debug_handle *info = (struct debug_handle *) handle;
1754 struct debug_method_s *m;
1756 m = debug_xzalloc (info, sizeof (*m));
1758 m->name = name;
1759 m->variants = variants;
1761 return m;
1764 /* Make a method argument. The second argument is the real name of
1765 the function. The third argument is the type of the function. The
1766 fourth argument is the visibility. The fifth argument is whether
1767 this is a const function. The sixth argument is whether this is a
1768 volatile function. The seventh argument is the offset in the
1769 virtual function table, if any. The eighth argument is the virtual
1770 function context. FIXME: Are the const and volatile arguments
1771 necessary? Could we just use debug_make_const_type? */
1773 debug_method_variant
1774 debug_make_method_variant (void *handle,
1775 const char *physname, debug_type type,
1776 enum debug_visibility visibility,
1777 bool constp, bool volatilep,
1778 bfd_vma voffset, debug_type context)
1780 struct debug_handle *info = (struct debug_handle *) handle;
1781 struct debug_method_variant_s *m;
1783 m = debug_xzalloc (info, sizeof (*m));
1785 m->physname = physname;
1786 m->type = type;
1787 m->visibility = visibility;
1788 m->constp = constp;
1789 m->volatilep = volatilep;
1790 m->voffset = voffset;
1791 m->context = context;
1793 return m;
1796 /* Make a static method argument. The arguments are the same as for
1797 debug_make_method_variant, except that the last two are omitted
1798 since a static method can not also be virtual. */
1800 debug_method_variant
1801 debug_make_static_method_variant (void *handle,
1802 const char *physname, debug_type type,
1803 enum debug_visibility visibility,
1804 bool constp, bool volatilep)
1806 struct debug_handle *info = (struct debug_handle *) handle;
1807 struct debug_method_variant_s *m;
1809 m = debug_xzalloc (info, sizeof (*m));
1811 m->physname = physname;
1812 m->type = type;
1813 m->visibility = visibility;
1814 m->constp = constp;
1815 m->volatilep = volatilep;
1816 m->voffset = VOFFSET_STATIC_METHOD;
1818 return m;
1821 /* Name a type. */
1823 debug_type
1824 debug_name_type (void *handle, const char *name, debug_type type)
1826 struct debug_handle *info = (struct debug_handle *) handle;
1827 struct debug_type_s *t;
1828 struct debug_named_type *n;
1829 struct debug_name *nm;
1831 if (name == NULL || type == NULL)
1832 return DEBUG_TYPE_NULL;
1834 if (info->current_unit == NULL
1835 || info->current_file == NULL)
1837 debug_error (_("debug_name_type: no current file"));
1838 return DEBUG_TYPE_NULL;
1841 t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
1842 if (t == NULL)
1843 return DEBUG_TYPE_NULL;
1845 n = debug_xzalloc (info, sizeof (*n));
1847 n->type = type;
1849 t->u.knamed = n;
1851 /* We always add the name to the global namespace. This is probably
1852 wrong in some cases, but it seems to be right for stabs. FIXME. */
1854 nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1855 DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
1856 if (nm == NULL)
1857 return DEBUG_TYPE_NULL;
1859 nm->u.type = t;
1861 n->name = nm;
1863 return t;
1866 /* Tag a type. */
1868 debug_type
1869 debug_tag_type (void *handle, const char *name, debug_type type)
1871 struct debug_handle *info = (struct debug_handle *) handle;
1872 struct debug_type_s *t;
1873 struct debug_named_type *n;
1874 struct debug_name *nm;
1876 if (name == NULL || type == NULL)
1877 return DEBUG_TYPE_NULL;
1879 if (info->current_file == NULL)
1881 debug_error (_("debug_tag_type: no current file"));
1882 return DEBUG_TYPE_NULL;
1885 if (type->kind == DEBUG_KIND_TAGGED)
1887 if (strcmp (type->u.knamed->name->name, name) == 0)
1888 return type;
1889 debug_error (_("debug_tag_type: extra tag attempted"));
1890 return DEBUG_TYPE_NULL;
1893 t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
1894 if (t == NULL)
1895 return DEBUG_TYPE_NULL;
1897 n = debug_xzalloc (info, sizeof (*n));
1899 n->type = type;
1901 t->u.knamed = n;
1903 /* We keep a global namespace of tags for each compilation unit. I
1904 don't know if that is the right thing to do. */
1906 nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1907 DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
1908 if (nm == NULL)
1909 return DEBUG_TYPE_NULL;
1911 nm->u.tag = t;
1913 n->name = nm;
1915 return t;
1918 /* Record the size of a given type. */
1920 bool
1921 debug_record_type_size (void *handle ATTRIBUTE_UNUSED, debug_type type,
1922 unsigned int size)
1924 if (type->size != 0 && type->size != size)
1925 fprintf (stderr, _("Warning: changing type size from %d to %d\n"),
1926 type->size, size);
1928 type->size = size;
1930 return true;
1933 /* Find a named type. */
1935 debug_type
1936 debug_find_named_type (void *handle, const char *name)
1938 struct debug_handle *info = (struct debug_handle *) handle;
1939 struct debug_block *b;
1940 struct debug_file *f;
1942 /* We only search the current compilation unit. I don't know if
1943 this is right or not. */
1945 if (info->current_unit == NULL)
1947 debug_error (_("debug_find_named_type: no current compilation unit"));
1948 return DEBUG_TYPE_NULL;
1951 for (b = info->current_block; b != NULL; b = b->parent)
1953 if (b->locals != NULL)
1955 struct debug_name *n;
1957 for (n = b->locals->list; n != NULL; n = n->next)
1959 if (n->kind == DEBUG_OBJECT_TYPE
1960 && n->name[0] == name[0]
1961 && strcmp (n->name, name) == 0)
1962 return n->u.type;
1967 for (f = info->current_unit->files; f != NULL; f = f->next)
1969 if (f->globals != NULL)
1971 struct debug_name *n;
1973 for (n = f->globals->list; n != NULL; n = n->next)
1975 if (n->kind == DEBUG_OBJECT_TYPE
1976 && n->name[0] == name[0]
1977 && strcmp (n->name, name) == 0)
1978 return n->u.type;
1983 return DEBUG_TYPE_NULL;
1986 /* Find a tagged type. */
1988 debug_type
1989 debug_find_tagged_type (void *handle, const char *name,
1990 enum debug_type_kind kind)
1992 struct debug_handle *info = (struct debug_handle *) handle;
1993 struct debug_unit *u;
1995 /* We search the globals of all the compilation units. I don't know
1996 if this is correct or not. It would be easy to change. */
1998 for (u = info->units; u != NULL; u = u->next)
2000 struct debug_file *f;
2002 for (f = u->files; f != NULL; f = f->next)
2004 struct debug_name *n;
2006 if (f->globals != NULL)
2008 for (n = f->globals->list; n != NULL; n = n->next)
2010 if (n->kind == DEBUG_OBJECT_TAG
2011 && (kind == DEBUG_KIND_ILLEGAL
2012 || n->u.tag->kind == kind)
2013 && n->name[0] == name[0]
2014 && strcmp (n->name, name) == 0)
2015 return n->u.tag;
2021 return DEBUG_TYPE_NULL;
2024 /* Get a base type. We build a linked list on the stack to avoid
2025 crashing if the type is defined circularly. */
2027 static struct debug_type_s *
2028 debug_get_real_type (void *handle, debug_type type,
2029 struct debug_type_real_list *list)
2031 struct debug_type_real_list *l;
2032 struct debug_type_real_list rl;
2034 switch (type->kind)
2036 default:
2037 return type;
2039 case DEBUG_KIND_INDIRECT:
2040 case DEBUG_KIND_NAMED:
2041 case DEBUG_KIND_TAGGED:
2042 break;
2045 for (l = list; l != NULL; l = l->next)
2047 if (l->t == type || l == l->next)
2049 fprintf (stderr,
2050 _("debug_get_real_type: circular debug information for %s\n"),
2051 debug_get_type_name (handle, type));
2052 return NULL;
2056 rl.next = list;
2057 rl.t = type;
2059 switch (type->kind)
2061 /* The default case is just here to avoid warnings. */
2062 default:
2063 case DEBUG_KIND_INDIRECT:
2064 /* A valid non-self-referencing indirect type. */
2065 if (*type->u.kindirect->slot != NULL
2066 && *type->u.kindirect->slot != type)
2067 return debug_get_real_type (handle, *type->u.kindirect->slot, &rl);
2068 return type;
2069 case DEBUG_KIND_NAMED:
2070 case DEBUG_KIND_TAGGED:
2071 return debug_get_real_type (handle, type->u.knamed->type, &rl);
2073 /*NOTREACHED*/
2076 /* Get the kind of a type. */
2078 enum debug_type_kind
2079 debug_get_type_kind (void *handle, debug_type type)
2081 if (type == NULL)
2082 return DEBUG_KIND_ILLEGAL;
2083 type = debug_get_real_type (handle, type, NULL);
2084 if (type == NULL)
2085 return DEBUG_KIND_ILLEGAL;
2086 return type->kind;
2089 /* Get the name of a type. */
2091 const char *
2092 debug_get_type_name (void *handle, debug_type type)
2094 if (type->kind == DEBUG_KIND_INDIRECT)
2096 /* A valid non-self-referencing indirect type. */
2097 if (*type->u.kindirect->slot != NULL
2098 && *type->u.kindirect->slot != type)
2099 return debug_get_type_name (handle, *type->u.kindirect->slot);
2100 return type->u.kindirect->tag;
2102 if (type->kind == DEBUG_KIND_NAMED
2103 || type->kind == DEBUG_KIND_TAGGED)
2104 return type->u.knamed->name->name;
2105 return NULL;
2108 /* Get the size of a type. */
2110 bfd_vma
2111 debug_get_type_size (void *handle, debug_type type)
2113 if (type == NULL)
2114 return 0;
2116 /* We don't call debug_get_real_type, because somebody might have
2117 called debug_record_type_size on a named or indirect type. */
2119 if (type->size != 0)
2120 return type->size;
2122 switch (type->kind)
2124 default:
2125 return 0;
2126 case DEBUG_KIND_INDIRECT:
2127 /* A valid non-self-referencing indirect type. */
2128 if (*type->u.kindirect->slot != NULL
2129 && *type->u.kindirect->slot != type)
2130 return debug_get_type_size (handle, *type->u.kindirect->slot);
2131 return 0;
2132 case DEBUG_KIND_NAMED:
2133 case DEBUG_KIND_TAGGED:
2134 return debug_get_type_size (handle, type->u.knamed->type);
2136 /*NOTREACHED*/
2139 /* Get the return type of a function or method type. */
2141 debug_type
2142 debug_get_return_type (void *handle, debug_type type)
2144 if (type == NULL)
2145 return DEBUG_TYPE_NULL;
2147 type = debug_get_real_type (handle, type, NULL);
2148 if (type == NULL)
2149 return DEBUG_TYPE_NULL;
2151 switch (type->kind)
2153 default:
2154 return DEBUG_TYPE_NULL;
2155 case DEBUG_KIND_FUNCTION:
2156 return type->u.kfunction->return_type;
2157 case DEBUG_KIND_METHOD:
2158 return type->u.kmethod->return_type;
2160 /*NOTREACHED*/
2163 /* Get the parameter types of a function or method type (except that
2164 we don't currently store the parameter types of a function). */
2166 const debug_type *
2167 debug_get_parameter_types (void *handle, debug_type type,
2168 bool *pvarargs)
2170 if (type == NULL)
2171 return NULL;
2173 type = debug_get_real_type (handle, type, NULL);
2174 if (type == NULL)
2175 return NULL;
2177 switch (type->kind)
2179 default:
2180 return NULL;
2181 case DEBUG_KIND_FUNCTION:
2182 *pvarargs = type->u.kfunction->varargs;
2183 return type->u.kfunction->arg_types;
2184 case DEBUG_KIND_METHOD:
2185 *pvarargs = type->u.kmethod->varargs;
2186 return type->u.kmethod->arg_types;
2188 /*NOTREACHED*/
2191 /* Get the target type of a type. */
2193 debug_type
2194 debug_get_target_type (void *handle, debug_type type)
2196 if (type == NULL)
2197 return NULL;
2199 type = debug_get_real_type (handle, type, NULL);
2200 if (type == NULL)
2201 return NULL;
2203 switch (type->kind)
2205 default:
2206 return NULL;
2207 case DEBUG_KIND_POINTER:
2208 return type->u.kpointer;
2209 case DEBUG_KIND_REFERENCE:
2210 return type->u.kreference;
2211 case DEBUG_KIND_CONST:
2212 return type->u.kconst;
2213 case DEBUG_KIND_VOLATILE:
2214 return type->u.kvolatile;
2216 /*NOTREACHED*/
2219 /* Get the NULL terminated array of fields for a struct, union, or
2220 class. */
2222 const debug_field *
2223 debug_get_fields (void *handle, debug_type type)
2225 if (type == NULL)
2226 return NULL;
2228 type = debug_get_real_type (handle, type, NULL);
2229 if (type == NULL)
2230 return NULL;
2232 switch (type->kind)
2234 default:
2235 return NULL;
2236 case DEBUG_KIND_STRUCT:
2237 case DEBUG_KIND_UNION:
2238 case DEBUG_KIND_CLASS:
2239 case DEBUG_KIND_UNION_CLASS:
2240 return type->u.kclass->fields;
2242 /*NOTREACHED*/
2245 /* Get the type of a field. */
2247 debug_type
2248 debug_get_field_type (void *handle ATTRIBUTE_UNUSED, debug_field field)
2250 if (field == NULL)
2251 return NULL;
2252 return field->type;
2255 /* Get the name of a field. */
2257 const char *
2258 debug_get_field_name (void *handle ATTRIBUTE_UNUSED, debug_field field)
2260 if (field == NULL)
2261 return NULL;
2262 return field->name;
2265 /* Get the bit position of a field. */
2267 bfd_vma
2268 debug_get_field_bitpos (void *handle ATTRIBUTE_UNUSED, debug_field field)
2270 if (field == NULL || field->static_member)
2271 return (bfd_vma) -1;
2272 return field->u.f.bitpos;
2275 /* Get the bit size of a field. */
2277 bfd_vma
2278 debug_get_field_bitsize (void *handle ATTRIBUTE_UNUSED, debug_field field)
2280 if (field == NULL || field->static_member)
2281 return (bfd_vma) -1;
2282 return field->u.f.bitsize;
2285 /* Get the visibility of a field. */
2287 enum debug_visibility
2288 debug_get_field_visibility (void *handle ATTRIBUTE_UNUSED, debug_field field)
2290 if (field == NULL)
2291 return DEBUG_VISIBILITY_IGNORE;
2292 return field->visibility;
2295 /* Get the physical name of a field. */
2297 const char *
2298 debug_get_field_physname (void *handle ATTRIBUTE_UNUSED, debug_field field)
2300 if (field == NULL || ! field->static_member)
2301 return NULL;
2302 return field->u.s.physname;
2305 /* Write out the debugging information. This is given a handle to
2306 debugging information, and a set of function pointers to call. */
2308 bool
2309 debug_write (void *handle, const struct debug_write_fns *fns, void *fhandle)
2311 struct debug_handle *info = (struct debug_handle *) handle;
2312 struct debug_unit *u;
2314 /* We use a mark to tell whether we have already written out a
2315 particular name. We use an integer, so that we don't have to
2316 clear the mark fields if we happen to write out the same
2317 information more than once. */
2318 ++info->mark;
2320 /* The base_id field holds an ID value which will never be used, so
2321 that we can tell whether we have assigned an ID during this call
2322 to debug_write. */
2323 info->base_id = info->class_id;
2325 /* We keep a linked list of classes for which was have assigned ID's
2326 during this call to debug_write. */
2327 info->id_list = NULL;
2329 for (u = info->units; u != NULL; u = u->next)
2331 struct debug_file *f;
2332 bool first_file;
2334 info->current_write_lineno = u->linenos;
2335 info->current_write_lineno_index = 0;
2337 if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2338 return false;
2340 first_file = true;
2341 for (f = u->files; f != NULL; f = f->next)
2343 struct debug_name *n;
2345 if (first_file)
2346 first_file = false;
2347 else if (! (*fns->start_source) (fhandle, f->filename))
2348 return false;
2350 if (f->globals != NULL)
2351 for (n = f->globals->list; n != NULL; n = n->next)
2352 if (! debug_write_name (info, fns, fhandle, n))
2353 return false;
2356 /* Output any line number information which hasn't already been
2357 handled. */
2358 if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
2359 return false;
2362 return true;
2365 /* Write out an element in a namespace. */
2367 static bool
2368 debug_write_name (struct debug_handle *info,
2369 const struct debug_write_fns *fns, void *fhandle,
2370 struct debug_name *n)
2372 switch (n->kind)
2374 case DEBUG_OBJECT_TYPE:
2375 if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2376 || ! (*fns->typdef) (fhandle, n->name))
2377 return false;
2378 return true;
2379 case DEBUG_OBJECT_TAG:
2380 if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2381 return false;
2382 return (*fns->tag) (fhandle, n->name);
2383 case DEBUG_OBJECT_VARIABLE:
2384 if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2385 (struct debug_name *) NULL))
2386 return false;
2387 return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2388 n->u.variable->val);
2389 case DEBUG_OBJECT_FUNCTION:
2390 return debug_write_function (info, fns, fhandle, n->name,
2391 n->linkage, n->u.function);
2392 case DEBUG_OBJECT_INT_CONSTANT:
2393 return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2394 case DEBUG_OBJECT_FLOAT_CONSTANT:
2395 return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2396 case DEBUG_OBJECT_TYPED_CONSTANT:
2397 if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2398 (struct debug_name *) NULL))
2399 return false;
2400 return (*fns->typed_constant) (fhandle, n->name,
2401 n->u.typed_constant->val);
2402 default:
2403 abort ();
2404 return false;
2406 /*NOTREACHED*/
2409 /* Write out a type. If the type is DEBUG_KIND_NAMED or
2410 DEBUG_KIND_TAGGED, then the name argument is the name for which we
2411 are about to call typedef or tag. If the type is anything else,
2412 then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2413 points to this one. */
2415 static bool
2416 debug_write_type (struct debug_handle *info,
2417 const struct debug_write_fns *fns, void *fhandle,
2418 struct debug_type_s *type, struct debug_name *name)
2420 unsigned int i;
2421 int is;
2422 const char *tag = NULL;
2424 if (type == DEBUG_TYPE_NULL)
2425 return (*fns->empty_type) (fhandle);
2427 /* Mark the type so that we don't define a type in terms of itself. */
2428 type->mark = info->mark;
2430 /* If we have a name for this type, just output it. We only output
2431 typedef names after they have been defined. We output type tags
2432 whenever we are not actually defining them. */
2433 if ((type->kind == DEBUG_KIND_NAMED
2434 || type->kind == DEBUG_KIND_TAGGED)
2435 && (type->u.knamed->name->mark == info->mark
2436 || (type->kind == DEBUG_KIND_TAGGED
2437 && type->u.knamed->name != name)))
2439 if (type->kind == DEBUG_KIND_NAMED)
2440 return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2441 else
2443 struct debug_type_s *real;
2444 unsigned int id;
2446 real = debug_get_real_type ((void *) info, type, NULL);
2447 if (real == NULL)
2448 return (*fns->empty_type) (fhandle);
2449 id = 0;
2450 if ((real->kind == DEBUG_KIND_STRUCT
2451 || real->kind == DEBUG_KIND_UNION
2452 || real->kind == DEBUG_KIND_CLASS
2453 || real->kind == DEBUG_KIND_UNION_CLASS)
2454 && real->u.kclass != NULL)
2456 if (real->u.kclass->id <= info->base_id)
2458 if (! debug_set_class_id (info,
2459 type->u.knamed->name->name,
2460 real))
2461 return false;
2463 id = real->u.kclass->id;
2466 return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
2467 real->kind);
2471 /* Mark the name after we have already looked for a known name, so
2472 that we don't just define a type in terms of itself. We need to
2473 mark the name here so that a struct containing a pointer to
2474 itself will work. */
2475 if (name != NULL)
2476 name->mark = info->mark;
2478 if (name != NULL
2479 && type->kind != DEBUG_KIND_NAMED
2480 && type->kind != DEBUG_KIND_TAGGED)
2482 assert (name->kind == DEBUG_OBJECT_TAG);
2483 tag = name->name;
2486 switch (type->kind)
2488 case DEBUG_KIND_ILLEGAL:
2489 debug_error (_("debug_write_type: illegal type encountered"));
2490 return false;
2491 case DEBUG_KIND_INDIRECT:
2492 /* Prevent infinite recursion. */
2493 if (*type->u.kindirect->slot != DEBUG_TYPE_NULL
2494 && (*type->u.kindirect->slot)->mark == info->mark)
2495 return (*fns->empty_type) (fhandle);
2496 return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2497 name);
2498 case DEBUG_KIND_VOID:
2499 return (*fns->void_type) (fhandle);
2500 case DEBUG_KIND_INT:
2501 return (*fns->int_type) (fhandle, type->size, type->u.kint);
2502 case DEBUG_KIND_FLOAT:
2503 return (*fns->float_type) (fhandle, type->size);
2504 case DEBUG_KIND_COMPLEX:
2505 return (*fns->complex_type) (fhandle, type->size);
2506 case DEBUG_KIND_BOOL:
2507 return (*fns->bool_type) (fhandle, type->size);
2508 case DEBUG_KIND_STRUCT:
2509 case DEBUG_KIND_UNION:
2510 if (type->u.kclass != NULL)
2512 if (type->u.kclass->id <= info->base_id)
2514 if (! debug_set_class_id (info, tag, type))
2515 return false;
2518 if (info->mark == type->u.kclass->mark)
2520 /* We are currently outputting this struct, or we have
2521 already output it. I don't know if this can happen,
2522 but it can happen for a class. */
2523 assert (type->u.kclass->id > info->base_id);
2524 return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2525 type->kind);
2527 type->u.kclass->mark = info->mark;
2530 if (! (*fns->start_struct_type) (fhandle, tag,
2531 (type->u.kclass != NULL
2532 ? type->u.kclass->id
2533 : 0),
2534 type->kind == DEBUG_KIND_STRUCT,
2535 type->size))
2536 return false;
2537 if (type->u.kclass != NULL
2538 && type->u.kclass->fields != NULL)
2540 for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2542 struct debug_field_s *f;
2544 f = type->u.kclass->fields[i];
2545 if (! debug_write_type (info, fns, fhandle, f->type,
2546 (struct debug_name *) NULL)
2547 || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2548 f->u.f.bitsize, f->visibility))
2549 return false;
2552 return (*fns->end_struct_type) (fhandle);
2553 case DEBUG_KIND_CLASS:
2554 case DEBUG_KIND_UNION_CLASS:
2555 return debug_write_class_type (info, fns, fhandle, type, tag);
2556 case DEBUG_KIND_ENUM:
2557 if (type->u.kenum == NULL)
2558 return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2559 (bfd_signed_vma *) NULL);
2560 return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2561 type->u.kenum->values);
2562 case DEBUG_KIND_POINTER:
2563 if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2564 (struct debug_name *) NULL))
2565 return false;
2566 return (*fns->pointer_type) (fhandle);
2567 case DEBUG_KIND_FUNCTION:
2568 if (! debug_write_type (info, fns, fhandle,
2569 type->u.kfunction->return_type,
2570 (struct debug_name *) NULL))
2571 return false;
2572 if (type->u.kfunction->arg_types == NULL)
2573 is = -1;
2574 else
2576 for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2577 if (! debug_write_type (info, fns, fhandle,
2578 type->u.kfunction->arg_types[is],
2579 (struct debug_name *) NULL))
2580 return false;
2582 return (*fns->function_type) (fhandle, is,
2583 type->u.kfunction->varargs);
2584 case DEBUG_KIND_REFERENCE:
2585 if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2586 (struct debug_name *) NULL))
2587 return false;
2588 return (*fns->reference_type) (fhandle);
2589 case DEBUG_KIND_RANGE:
2590 if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2591 (struct debug_name *) NULL))
2592 return false;
2593 return (*fns->range_type) (fhandle, type->u.krange->lower,
2594 type->u.krange->upper);
2595 case DEBUG_KIND_ARRAY:
2596 if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2597 (struct debug_name *) NULL)
2598 || ! debug_write_type (info, fns, fhandle,
2599 type->u.karray->range_type,
2600 (struct debug_name *) NULL))
2601 return false;
2602 return (*fns->array_type) (fhandle, type->u.karray->lower,
2603 type->u.karray->upper,
2604 type->u.karray->stringp);
2605 case DEBUG_KIND_SET:
2606 if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2607 (struct debug_name *) NULL))
2608 return false;
2609 return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2610 case DEBUG_KIND_OFFSET:
2611 if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2612 (struct debug_name *) NULL)
2613 || ! debug_write_type (info, fns, fhandle,
2614 type->u.koffset->target_type,
2615 (struct debug_name *) NULL))
2616 return false;
2617 return (*fns->offset_type) (fhandle);
2618 case DEBUG_KIND_METHOD:
2619 if (! debug_write_type (info, fns, fhandle,
2620 type->u.kmethod->return_type,
2621 (struct debug_name *) NULL))
2622 return false;
2623 if (type->u.kmethod->arg_types == NULL)
2624 is = -1;
2625 else
2627 for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2628 if (! debug_write_type (info, fns, fhandle,
2629 type->u.kmethod->arg_types[is],
2630 (struct debug_name *) NULL))
2631 return false;
2633 if (type->u.kmethod->domain_type != NULL)
2635 if (! debug_write_type (info, fns, fhandle,
2636 type->u.kmethod->domain_type,
2637 (struct debug_name *) NULL))
2638 return false;
2640 return (*fns->method_type) (fhandle,
2641 type->u.kmethod->domain_type != NULL,
2643 type->u.kmethod->varargs);
2644 case DEBUG_KIND_CONST:
2645 if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2646 (struct debug_name *) NULL))
2647 return false;
2648 return (*fns->const_type) (fhandle);
2649 case DEBUG_KIND_VOLATILE:
2650 if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2651 (struct debug_name *) NULL))
2652 return false;
2653 return (*fns->volatile_type) (fhandle);
2654 case DEBUG_KIND_NAMED:
2655 return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2656 (struct debug_name *) NULL);
2657 case DEBUG_KIND_TAGGED:
2658 return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2659 type->u.knamed->name);
2660 default:
2661 abort ();
2662 return false;
2666 /* Write out a class type. */
2668 static bool
2669 debug_write_class_type (struct debug_handle *info,
2670 const struct debug_write_fns *fns, void *fhandle,
2671 struct debug_type_s *type, const char *tag)
2673 unsigned int i;
2674 unsigned int id;
2675 struct debug_type_s *vptrbase;
2677 if (type->u.kclass == NULL)
2679 id = 0;
2680 vptrbase = NULL;
2682 else
2684 if (type->u.kclass->id <= info->base_id)
2686 if (! debug_set_class_id (info, tag, type))
2687 return false;
2690 if (info->mark == type->u.kclass->mark)
2692 /* We are currently outputting this class, or we have
2693 already output it. This can happen when there are
2694 methods for an anonymous class. */
2695 assert (type->u.kclass->id > info->base_id);
2696 return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2697 type->kind);
2699 type->u.kclass->mark = info->mark;
2700 id = type->u.kclass->id;
2702 vptrbase = type->u.kclass->vptrbase;
2703 if (vptrbase != NULL && vptrbase != type)
2705 if (! debug_write_type (info, fns, fhandle, vptrbase,
2706 (struct debug_name *) NULL))
2707 return false;
2711 if (! (*fns->start_class_type) (fhandle, tag, id,
2712 type->kind == DEBUG_KIND_CLASS,
2713 type->size,
2714 vptrbase != NULL,
2715 vptrbase == type))
2716 return false;
2718 if (type->u.kclass != NULL)
2720 if (type->u.kclass->fields != NULL)
2722 for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2724 struct debug_field_s *f;
2726 f = type->u.kclass->fields[i];
2727 if (! debug_write_type (info, fns, fhandle, f->type,
2728 (struct debug_name *) NULL))
2729 return false;
2730 if (f->static_member)
2732 if (! (*fns->class_static_member) (fhandle, f->name,
2733 f->u.s.physname,
2734 f->visibility))
2735 return false;
2737 else
2739 if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2740 f->u.f.bitsize, f->visibility))
2741 return false;
2746 if (type->u.kclass->baseclasses != NULL)
2748 for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2750 struct debug_baseclass_s *b;
2752 b = type->u.kclass->baseclasses[i];
2753 if (! debug_write_type (info, fns, fhandle, b->type,
2754 (struct debug_name *) NULL))
2755 return false;
2756 if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->is_virtual,
2757 b->visibility))
2758 return false;
2762 if (type->u.kclass->methods != NULL)
2764 for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2766 struct debug_method_s *m;
2767 unsigned int j;
2769 m = type->u.kclass->methods[i];
2770 if (! (*fns->class_start_method) (fhandle, m->name))
2771 return false;
2772 for (j = 0; m->variants[j] != NULL; j++)
2774 struct debug_method_variant_s *v;
2776 v = m->variants[j];
2777 if (v->context != NULL)
2779 if (! debug_write_type (info, fns, fhandle, v->context,
2780 (struct debug_name *) NULL))
2781 return false;
2783 if (! debug_write_type (info, fns, fhandle, v->type,
2784 (struct debug_name *) NULL))
2785 return false;
2786 if (v->voffset != VOFFSET_STATIC_METHOD)
2788 if (! (*fns->class_method_variant) (fhandle, v->physname,
2789 v->visibility,
2790 v->constp,
2791 v->volatilep,
2792 v->voffset,
2793 v->context != NULL))
2794 return false;
2796 else
2798 if (! (*fns->class_static_method_variant) (fhandle,
2799 v->physname,
2800 v->visibility,
2801 v->constp,
2802 v->volatilep))
2803 return false;
2806 if (! (*fns->class_end_method) (fhandle))
2807 return false;
2812 return (*fns->end_class_type) (fhandle);
2815 /* Write out information for a function. */
2817 static bool
2818 debug_write_function (struct debug_handle *info,
2819 const struct debug_write_fns *fns, void *fhandle,
2820 const char *name, enum debug_object_linkage linkage,
2821 struct debug_function *function)
2823 struct debug_parameter *p;
2824 struct debug_block *b;
2826 if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
2827 return false;
2829 if (! debug_write_type (info, fns, fhandle, function->return_type,
2830 (struct debug_name *) NULL))
2831 return false;
2833 if (! (*fns->start_function) (fhandle, name,
2834 linkage == DEBUG_LINKAGE_GLOBAL))
2835 return false;
2837 for (p = function->parameters; p != NULL; p = p->next)
2839 if (! debug_write_type (info, fns, fhandle, p->type,
2840 (struct debug_name *) NULL)
2841 || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2842 return false;
2845 for (b = function->blocks; b != NULL; b = b->next)
2847 if (! debug_write_block (info, fns, fhandle, b))
2848 return false;
2851 return (*fns->end_function) (fhandle);
2854 /* Write out information for a block. */
2856 static bool
2857 debug_write_block (struct debug_handle *info,
2858 const struct debug_write_fns *fns, void *fhandle,
2859 struct debug_block *block)
2861 struct debug_name *n;
2862 struct debug_block *b;
2864 if (! debug_write_linenos (info, fns, fhandle, block->start))
2865 return false;
2867 /* I can't see any point to writing out a block with no local
2868 variables, so we don't bother, except for the top level block. */
2869 if (block->locals != NULL || block->parent == NULL)
2871 if (! (*fns->start_block) (fhandle, block->start))
2872 return false;
2875 if (block->locals != NULL)
2877 for (n = block->locals->list; n != NULL; n = n->next)
2879 if (! debug_write_name (info, fns, fhandle, n))
2880 return false;
2884 for (b = block->children; b != NULL; b = b->next)
2886 if (! debug_write_block (info, fns, fhandle, b))
2887 return false;
2890 if (! debug_write_linenos (info, fns, fhandle, block->end))
2891 return false;
2893 if (block->locals != NULL || block->parent == NULL)
2895 if (! (*fns->end_block) (fhandle, block->end))
2896 return false;
2899 return true;
2902 /* Write out line number information up to ADDRESS. */
2904 static bool
2905 debug_write_linenos (struct debug_handle *info,
2906 const struct debug_write_fns *fns, void *fhandle,
2907 bfd_vma address)
2909 while (info->current_write_lineno != NULL)
2911 struct debug_lineno *l;
2913 l = info->current_write_lineno;
2915 while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
2917 if (l->linenos[info->current_write_lineno_index]
2918 == (unsigned long) -1)
2919 break;
2921 if (l->addrs[info->current_write_lineno_index] >= address)
2922 return true;
2924 if (! (*fns->lineno) (fhandle, l->file->filename,
2925 l->linenos[info->current_write_lineno_index],
2926 l->addrs[info->current_write_lineno_index]))
2927 return false;
2929 ++info->current_write_lineno_index;
2932 info->current_write_lineno = l->next;
2933 info->current_write_lineno_index = 0;
2936 return true;
2939 /* Get the ID number for a class. If during the same call to
2940 debug_write we find a struct with the same definition with the same
2941 name, we use the same ID. This type of things happens because the
2942 same struct will be defined by multiple compilation units. */
2944 static bool
2945 debug_set_class_id (struct debug_handle *info, const char *tag,
2946 struct debug_type_s *type)
2948 struct debug_class_type *c;
2949 struct debug_class_id *l;
2951 assert (type->kind == DEBUG_KIND_STRUCT
2952 || type->kind == DEBUG_KIND_UNION
2953 || type->kind == DEBUG_KIND_CLASS
2954 || type->kind == DEBUG_KIND_UNION_CLASS);
2956 c = type->u.kclass;
2958 if (c->id > info->base_id)
2959 return true;
2961 for (l = info->id_list; l != NULL; l = l->next)
2963 if (l->type->kind != type->kind)
2964 continue;
2966 if (tag == NULL)
2968 if (l->tag != NULL)
2969 continue;
2971 else
2973 if (l->tag == NULL
2974 || l->tag[0] != tag[0]
2975 || strcmp (l->tag, tag) != 0)
2976 continue;
2979 if (debug_type_samep (info, l->type, type))
2981 c->id = l->type->u.kclass->id;
2982 return true;
2986 /* There are no identical types. Use a new ID, and add it to the
2987 list. */
2988 ++info->class_id;
2989 c->id = info->class_id;
2991 l = debug_xzalloc (info, sizeof (*l));
2993 l->type = type;
2994 l->tag = tag;
2996 l->next = info->id_list;
2997 info->id_list = l;
2999 return true;
3002 /* See if two types are the same. At this point, we don't care about
3003 tags and the like. */
3005 static bool
3006 debug_type_samep (struct debug_handle *info, struct debug_type_s *t1,
3007 struct debug_type_s *t2)
3009 struct debug_type_compare_list *l;
3010 struct debug_type_compare_list top;
3011 bool ret;
3013 if (t1 == NULL)
3014 return t2 == NULL;
3015 if (t2 == NULL)
3016 return false;
3018 while (t1->kind == DEBUG_KIND_INDIRECT)
3020 t1 = *t1->u.kindirect->slot;
3021 if (t1 == NULL)
3022 return false;
3024 while (t2->kind == DEBUG_KIND_INDIRECT)
3026 t2 = *t2->u.kindirect->slot;
3027 if (t2 == NULL)
3028 return false;
3031 if (t1 == t2)
3032 return true;
3034 /* As a special case, permit a typedef to match a tag, since C++
3035 debugging output will sometimes add a typedef where C debugging
3036 output will not. */
3037 if (t1->kind == DEBUG_KIND_NAMED
3038 && t2->kind == DEBUG_KIND_TAGGED)
3039 return debug_type_samep (info, t1->u.knamed->type, t2);
3040 else if (t1->kind == DEBUG_KIND_TAGGED
3041 && t2->kind == DEBUG_KIND_NAMED)
3042 return debug_type_samep (info, t1, t2->u.knamed->type);
3044 if (t1->kind != t2->kind
3045 || t1->size != t2->size)
3046 return false;
3048 /* Get rid of the trivial cases first. */
3049 switch (t1->kind)
3051 default:
3052 break;
3053 case DEBUG_KIND_VOID:
3054 case DEBUG_KIND_FLOAT:
3055 case DEBUG_KIND_COMPLEX:
3056 case DEBUG_KIND_BOOL:
3057 return true;
3058 case DEBUG_KIND_INT:
3059 return t1->u.kint == t2->u.kint;
3062 /* We have to avoid an infinite recursion. We do this by keeping a
3063 list of types which we are comparing. We just keep the list on
3064 the stack. If we encounter a pair of types we are currently
3065 comparing, we just assume that they are equal. */
3066 for (l = info->compare_list; l != NULL; l = l->next)
3068 if (l->t1 == t1 && l->t2 == t2)
3069 return true;
3072 top.t1 = t1;
3073 top.t2 = t2;
3074 top.next = info->compare_list;
3075 info->compare_list = &top;
3077 switch (t1->kind)
3079 default:
3080 abort ();
3081 ret = false;
3082 break;
3084 case DEBUG_KIND_STRUCT:
3085 case DEBUG_KIND_UNION:
3086 case DEBUG_KIND_CLASS:
3087 case DEBUG_KIND_UNION_CLASS:
3088 if (t1->u.kclass == NULL)
3089 ret = t2->u.kclass == NULL;
3090 else if (t2->u.kclass == NULL)
3091 ret = false;
3092 else if (t1->u.kclass->id > info->base_id
3093 && t1->u.kclass->id == t2->u.kclass->id)
3094 ret = true;
3095 else
3096 ret = debug_class_type_samep (info, t1, t2);
3097 break;
3099 case DEBUG_KIND_ENUM:
3100 if (t1->u.kenum == NULL)
3101 ret = t2->u.kenum == NULL;
3102 else if (t2->u.kenum == NULL)
3103 ret = false;
3104 else
3106 const char **pn1, **pn2;
3107 bfd_signed_vma *pv1, *pv2;
3109 pn1 = t1->u.kenum->names;
3110 pn2 = t2->u.kenum->names;
3111 pv1 = t1->u.kenum->values;
3112 pv2 = t2->u.kenum->values;
3113 while (*pn1 != NULL && *pn2 != NULL)
3115 if (**pn1 != **pn2
3116 || *pv1 != *pv2
3117 || strcmp (*pn1, *pn2) != 0)
3118 break;
3119 ++pn1;
3120 ++pn2;
3121 ++pv1;
3122 ++pv2;
3124 ret = *pn1 == NULL && *pn2 == NULL;
3126 break;
3128 case DEBUG_KIND_POINTER:
3129 ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
3130 break;
3132 case DEBUG_KIND_FUNCTION:
3133 if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
3134 || ! debug_type_samep (info, t1->u.kfunction->return_type,
3135 t2->u.kfunction->return_type)
3136 || ((t1->u.kfunction->arg_types == NULL)
3137 != (t2->u.kfunction->arg_types == NULL)))
3138 ret = false;
3139 else if (t1->u.kfunction->arg_types == NULL)
3140 ret = true;
3141 else
3143 struct debug_type_s **a1, **a2;
3145 a1 = t1->u.kfunction->arg_types;
3146 a2 = t2->u.kfunction->arg_types;
3147 while (*a1 != NULL && *a2 != NULL)
3149 if (! debug_type_samep (info, *a1, *a2))
3150 break;
3151 ++a1;
3152 ++a2;
3154 ret = *a1 == NULL && *a2 == NULL;
3156 break;
3158 case DEBUG_KIND_REFERENCE:
3159 ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
3160 break;
3162 case DEBUG_KIND_RANGE:
3163 ret = (t1->u.krange->lower == t2->u.krange->lower
3164 && t1->u.krange->upper == t2->u.krange->upper
3165 && debug_type_samep (info, t1->u.krange->type,
3166 t2->u.krange->type));
3167 break;
3169 case DEBUG_KIND_ARRAY:
3170 ret = (t1->u.karray->lower == t2->u.karray->lower
3171 && t1->u.karray->upper == t2->u.karray->upper
3172 && t1->u.karray->stringp == t2->u.karray->stringp
3173 && debug_type_samep (info, t1->u.karray->element_type,
3174 t2->u.karray->element_type));
3175 break;
3177 case DEBUG_KIND_SET:
3178 ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
3179 && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
3180 break;
3182 case DEBUG_KIND_OFFSET:
3183 ret = (debug_type_samep (info, t1->u.koffset->base_type,
3184 t2->u.koffset->base_type)
3185 && debug_type_samep (info, t1->u.koffset->target_type,
3186 t2->u.koffset->target_type));
3187 break;
3189 case DEBUG_KIND_METHOD:
3190 if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
3191 || ! debug_type_samep (info, t1->u.kmethod->return_type,
3192 t2->u.kmethod->return_type)
3193 || ! debug_type_samep (info, t1->u.kmethod->domain_type,
3194 t2->u.kmethod->domain_type)
3195 || ((t1->u.kmethod->arg_types == NULL)
3196 != (t2->u.kmethod->arg_types == NULL)))
3197 ret = false;
3198 else if (t1->u.kmethod->arg_types == NULL)
3199 ret = true;
3200 else
3202 struct debug_type_s **a1, **a2;
3204 a1 = t1->u.kmethod->arg_types;
3205 a2 = t2->u.kmethod->arg_types;
3206 while (*a1 != NULL && *a2 != NULL)
3208 if (! debug_type_samep (info, *a1, *a2))
3209 break;
3210 ++a1;
3211 ++a2;
3213 ret = *a1 == NULL && *a2 == NULL;
3215 break;
3217 case DEBUG_KIND_CONST:
3218 ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
3219 break;
3221 case DEBUG_KIND_VOLATILE:
3222 ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
3223 break;
3225 case DEBUG_KIND_NAMED:
3226 case DEBUG_KIND_TAGGED:
3227 ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
3228 && debug_type_samep (info, t1->u.knamed->type,
3229 t2->u.knamed->type));
3230 break;
3233 info->compare_list = top.next;
3235 return ret;
3238 /* See if two classes are the same. This is a subroutine of
3239 debug_type_samep. */
3241 static bool
3242 debug_class_type_samep (struct debug_handle *info, struct debug_type_s *t1,
3243 struct debug_type_s *t2)
3245 struct debug_class_type *c1, *c2;
3247 c1 = t1->u.kclass;
3248 c2 = t2->u.kclass;
3250 if ((c1->fields == NULL) != (c2->fields == NULL)
3251 || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
3252 || (c1->methods == NULL) != (c2->methods == NULL)
3253 || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
3254 return false;
3256 if (c1->fields != NULL)
3258 struct debug_field_s **pf1, **pf2;
3260 for (pf1 = c1->fields, pf2 = c2->fields;
3261 *pf1 != NULL && *pf2 != NULL;
3262 pf1++, pf2++)
3264 struct debug_field_s *f1, *f2;
3266 f1 = *pf1;
3267 f2 = *pf2;
3268 if (f1->name[0] != f2->name[0]
3269 || f1->visibility != f2->visibility
3270 || f1->static_member != f2->static_member)
3271 return false;
3272 if (f1->static_member)
3274 if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
3275 return false;
3277 else
3279 if (f1->u.f.bitpos != f2->u.f.bitpos
3280 || f1->u.f.bitsize != f2->u.f.bitsize)
3281 return false;
3283 /* We do the checks which require function calls last. We
3284 don't require that the types of fields have the same
3285 names, since that sometimes fails in the presence of
3286 typedefs and we really don't care. */
3287 if (strcmp (f1->name, f2->name) != 0
3288 || f1->type == NULL
3289 || f2->type == NULL
3290 || ! debug_type_samep (info,
3291 debug_get_real_type ((void *) info,
3292 f1->type, NULL),
3293 debug_get_real_type ((void *) info,
3294 f2->type, NULL)))
3295 return false;
3297 if (*pf1 != NULL || *pf2 != NULL)
3298 return false;
3301 if (c1->vptrbase != NULL)
3303 if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
3304 return false;
3307 if (c1->baseclasses != NULL)
3309 struct debug_baseclass_s **pb1, **pb2;
3311 for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
3312 *pb1 != NULL && *pb2 != NULL;
3313 ++pb1, ++pb2)
3315 struct debug_baseclass_s *b1, *b2;
3317 b1 = *pb1;
3318 b2 = *pb2;
3319 if (b1->bitpos != b2->bitpos
3320 || b1->is_virtual != b2->is_virtual
3321 || b1->visibility != b2->visibility
3322 || ! debug_type_samep (info, b1->type, b2->type))
3323 return false;
3325 if (*pb1 != NULL || *pb2 != NULL)
3326 return false;
3329 if (c1->methods != NULL)
3331 struct debug_method_s **pm1, **pm2;
3333 for (pm1 = c1->methods, pm2 = c2->methods;
3334 *pm1 != NULL && *pm2 != NULL;
3335 ++pm1, ++pm2)
3337 struct debug_method_s *m1, *m2;
3339 m1 = *pm1;
3340 m2 = *pm2;
3341 if (m1->name[0] != m2->name[0]
3342 || strcmp (m1->name, m2->name) != 0
3343 || (m1->variants == NULL) != (m2->variants == NULL))
3344 return false;
3345 if (m1->variants != NULL)
3347 struct debug_method_variant_s **pv1, **pv2;
3349 for (pv1 = m1->variants, pv2 = m2->variants;
3350 *pv1 != NULL && *pv2 != NULL;
3351 ++pv1, ++pv2)
3353 struct debug_method_variant_s *v1, *v2;
3355 v1 = *pv1;
3356 v2 = *pv2;
3357 if (v1->physname[0] != v2->physname[0]
3358 || v1->visibility != v2->visibility
3359 || v1->constp != v2->constp
3360 || v1->volatilep != v2->volatilep
3361 || v1->voffset != v2->voffset
3362 || (v1->context == NULL) != (v2->context == NULL)
3363 || strcmp (v1->physname, v2->physname) != 0
3364 || ! debug_type_samep (info, v1->type, v2->type))
3365 return false;
3366 if (v1->context != NULL)
3368 if (! debug_type_samep (info, v1->context,
3369 v2->context))
3370 return false;
3373 if (*pv1 != NULL || *pv2 != NULL)
3374 return false;
3377 if (*pm1 != NULL || *pm2 != NULL)
3378 return false;
3381 return true;