-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebug.c
3371 lines (2794 loc) · 85.1 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* debug.c -- Handle generic debugging information.
Copyright (C) 1995-2019 Free Software Foundation, Inc.
Written by Ian Lance Taylor <[email protected]>.
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
/* This file implements a generic debugging format. We may eventually
have readers which convert different formats into this generic
format, and writers which write it out. The initial impetus for
this was writing a converter from stabs to HP IEEE-695 debugging
format. */
#include "sysdep.h"
#include <assert.h>
#include "bfd.h"
#include "libiberty.h"
#include "filenames.h"
#include "debug.h"
/* Global information we keep for debugging. A pointer to this
structure is the debugging handle passed to all the routines. */
struct debug_handle
{
/* A linked list of compilation units. */
struct debug_unit *units;
/* The current compilation unit. */
struct debug_unit *current_unit;
/* The current source file. */
struct debug_file *current_file;
/* The current function. */
struct debug_function *current_function;
/* The current block. */
struct debug_block *current_block;
/* The current line number information for the current unit. */
struct debug_lineno *current_lineno;
/* Mark. This is used by debug_write. */
unsigned int mark;
/* A struct/class ID used by debug_write. */
unsigned int class_id;
/* The base for class_id for this call to debug_write. */
unsigned int base_id;
/* The current line number in debug_write. */
struct debug_lineno *current_write_lineno;
unsigned int current_write_lineno_index;
/* A list of classes which have assigned ID's during debug_write.
This is linked through the next_id field of debug_class_type. */
struct debug_class_id *id_list;
/* A list used to avoid recursion during debug_type_samep. */
struct debug_type_compare_list *compare_list;
};
/* Information we keep for a single compilation unit. */
struct debug_unit
{
/* The next compilation unit. */
struct debug_unit *next;
/* A list of files included in this compilation unit. The first
file is always the main one, and that is where the main file name
is stored. */
struct debug_file *files;
/* Line number information for this compilation unit. This is not
stored by function, because assembler code may have line number
information without function information. */
struct debug_lineno *linenos;
};
/* Information kept for a single source file. */
struct debug_file
{
/* The next source file in this compilation unit. */
struct debug_file *next;
/* The name of the source file. */
const char *filename;
/* Global functions, variables, types, etc. */
struct debug_namespace *globals;
};
/* A type. */
struct debug_type_s
{
/* Kind of type. */
enum debug_type_kind kind;
/* Size of type (0 if not known). */
unsigned int size;
/* Type which is a pointer to this type. */
debug_type pointer;
/* Tagged union with additional information about the type. */
union
{
/* DEBUG_KIND_INDIRECT. */
struct debug_indirect_type *kindirect;
/* DEBUG_KIND_INT. */
/* Whether the integer is unsigned. */
bfd_boolean kint;
/* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
DEBUG_KIND_UNION_CLASS. */
struct debug_class_type *kclass;
/* DEBUG_KIND_ENUM. */
struct debug_enum_type *kenum;
/* DEBUG_KIND_POINTER. */
struct debug_type_s *kpointer;
/* DEBUG_KIND_FUNCTION. */
struct debug_function_type *kfunction;
/* DEBUG_KIND_REFERENCE. */
struct debug_type_s *kreference;
/* DEBUG_KIND_RANGE. */
struct debug_range_type *krange;
/* DEBUG_KIND_ARRAY. */
struct debug_array_type *karray;
/* DEBUG_KIND_SET. */
struct debug_set_type *kset;
/* DEBUG_KIND_OFFSET. */
struct debug_offset_type *koffset;
/* DEBUG_KIND_METHOD. */
struct debug_method_type *kmethod;
/* DEBUG_KIND_CONST. */
struct debug_type_s *kconst;
/* DEBUG_KIND_VOLATILE. */
struct debug_type_s *kvolatile;
/* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED. */
struct debug_named_type *knamed;
} u;
};
/* Information kept for an indirect type. */
struct debug_indirect_type
{
/* Slot where the final type will appear. */
debug_type *slot;
/* Tag. */
const char *tag;
};
/* Information kept for a struct, union, or class. */
struct debug_class_type
{
/* NULL terminated array of fields. */
debug_field *fields;
/* A mark field which indicates whether the struct has already been
printed. */
unsigned int mark;
/* This is used to uniquely identify unnamed structs when printing. */
unsigned int id;
/* The remaining fields are only used for DEBUG_KIND_CLASS and
DEBUG_KIND_UNION_CLASS. */
/* NULL terminated array of base classes. */
debug_baseclass *baseclasses;
/* NULL terminated array of methods. */
debug_method *methods;
/* The type of the class providing the virtual function table for
this class. This may point to the type itself. */
debug_type vptrbase;
};
/* Information kept for an enum. */
struct debug_enum_type
{
/* NULL terminated array of names. */
const char **names;
/* Array of corresponding values. */
bfd_signed_vma *values;
};
/* Information kept for a function. FIXME: We should be able to
record the parameter types. */
struct debug_function_type
{
/* Return type. */
debug_type return_type;
/* NULL terminated array of argument types. */
debug_type *arg_types;
/* Whether the function takes a variable number of arguments. */
bfd_boolean varargs;
};
/* Information kept for a range. */
struct debug_range_type
{
/* Range base type. */
debug_type type;
/* Lower bound. */
bfd_signed_vma lower;
/* Upper bound. */
bfd_signed_vma upper;
};
/* Information kept for an array. */
struct debug_array_type
{
/* Element type. */
debug_type element_type;
/* Range type. */
debug_type range_type;
/* Lower bound. */
bfd_signed_vma lower;
/* Upper bound. */
bfd_signed_vma upper;
/* Whether this array is really a string. */
bfd_boolean stringp;
};
/* Information kept for a set. */
struct debug_set_type
{
/* Base type. */
debug_type type;
/* Whether this set is really a bitstring. */
bfd_boolean bitstringp;
};
/* Information kept for an offset type (a based pointer). */
struct debug_offset_type
{
/* The type the pointer is an offset from. */
debug_type base_type;
/* The type the pointer points to. */
debug_type target_type;
};
/* Information kept for a method type. */
struct debug_method_type
{
/* The return type. */
debug_type return_type;
/* The object type which this method is for. */
debug_type domain_type;
/* A NULL terminated array of argument types. */
debug_type *arg_types;
/* Whether the method takes a variable number of arguments. */
bfd_boolean varargs;
};
/* Information kept for a named type. */
struct debug_named_type
{
/* Name. */
struct debug_name *name;
/* Real type. */
debug_type type;
};
/* A field in a struct or union. */
struct debug_field_s
{
/* Name of the field. */
const char *name;
/* Type of the field. */
struct debug_type_s *type;
/* Visibility of the field. */
enum debug_visibility visibility;
/* Whether this is a static member. */
bfd_boolean static_member;
union
{
/* If static_member is false. */
struct
{
/* Bit position of the field in the struct. */
unsigned int bitpos;
/* Size of the field in bits. */
unsigned int bitsize;
} f;
/* If static_member is true. */
struct
{
const char *physname;
} s;
} u;
};
/* A base class for an object. */
struct debug_baseclass_s
{
/* Type of the base class. */
struct debug_type_s *type;
/* Bit position of the base class in the object. */
unsigned int bitpos;
/* Whether the base class is virtual. */
bfd_boolean is_virtual;
/* Visibility of the base class. */
enum debug_visibility visibility;
};
/* A method of an object. */
struct debug_method_s
{
/* The name of the method. */
const char *name;
/* A NULL terminated array of different types of variants. */
struct debug_method_variant_s **variants;
};
/* The variants of a method function of an object. These indicate
which method to run. */
struct debug_method_variant_s
{
/* The physical name of the function. */
const char *physname;
/* The type of the function. */
struct debug_type_s *type;
/* The visibility of the function. */
enum debug_visibility visibility;
/* Whether the function is const. */
bfd_boolean constp;
/* Whether the function is volatile. */
bfd_boolean volatilep;
/* The offset to the function in the virtual function table. */
bfd_vma voffset;
/* If voffset is VOFFSET_STATIC_METHOD, this is a static method. */
#define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
/* Context of a virtual method function. */
struct debug_type_s *context;
};
/* A variable. This is the information we keep for a variable object.
This has no name; a name is associated with a variable in a
debug_name structure. */
struct debug_variable
{
/* Kind of variable. */
enum debug_var_kind kind;
/* Type. */
debug_type type;
/* Value. The interpretation of the value depends upon kind. */
bfd_vma val;
};
/* A function. This has no name; a name is associated with a function
in a debug_name structure. */
struct debug_function
{
/* Return type. */
debug_type return_type;
/* Parameter information. */
struct debug_parameter *parameters;
/* Block information. The first structure on the list is the main
block of the function, and describes function local variables. */
struct debug_block *blocks;
};
/* A function parameter. */
struct debug_parameter
{
/* Next parameter. */
struct debug_parameter *next;
/* Name. */
const char *name;
/* Type. */
debug_type type;
/* Kind. */
enum debug_parm_kind kind;
/* Value (meaning depends upon kind). */
bfd_vma val;
};
/* A typed constant. */
struct debug_typed_constant
{
/* Type. */
debug_type type;
/* Value. FIXME: We may eventually need to support non-integral
values. */
bfd_vma val;
};
/* Information about a block within a function. */
struct debug_block
{
/* Next block with the same parent. */
struct debug_block *next;
/* Parent block. */
struct debug_block *parent;
/* List of child blocks. */
struct debug_block *children;
/* Start address of the block. */
bfd_vma start;
/* End address of the block. */
bfd_vma end;
/* Local variables. */
struct debug_namespace *locals;
};
/* Line number information we keep for a compilation unit. FIXME:
This structure is easy to create, but can be very space
inefficient. */
struct debug_lineno
{
/* More line number information for this block. */
struct debug_lineno *next;
/* Source file. */
struct debug_file *file;
/* Line numbers, terminated by a -1 or the end of the array. */
#define DEBUG_LINENO_COUNT 10
unsigned long linenos[DEBUG_LINENO_COUNT];
/* Addresses for the line numbers. */
bfd_vma addrs[DEBUG_LINENO_COUNT];
};
/* A namespace. This is a mapping from names to objects. FIXME: This
should be implemented as a hash table. */
struct debug_namespace
{
/* List of items in this namespace. */
struct debug_name *list;
/* Pointer to where the next item in this namespace should go. */
struct debug_name **tail;
};
/* Kinds of objects that appear in a namespace. */
enum debug_object_kind
{
/* A type. */
DEBUG_OBJECT_TYPE,
/* A tagged type (really a different sort of namespace). */
DEBUG_OBJECT_TAG,
/* A variable. */
DEBUG_OBJECT_VARIABLE,
/* A function. */
DEBUG_OBJECT_FUNCTION,
/* An integer constant. */
DEBUG_OBJECT_INT_CONSTANT,
/* A floating point constant. */
DEBUG_OBJECT_FLOAT_CONSTANT,
/* A typed constant. */
DEBUG_OBJECT_TYPED_CONSTANT
};
/* Linkage of an object that appears in a namespace. */
enum debug_object_linkage
{
/* Local variable. */
DEBUG_LINKAGE_AUTOMATIC,
/* Static--either file static or function static, depending upon the
namespace is. */
DEBUG_LINKAGE_STATIC,
/* Global. */
DEBUG_LINKAGE_GLOBAL,
/* No linkage. */
DEBUG_LINKAGE_NONE
};
/* A name in a namespace. */
struct debug_name
{
/* Next name in this namespace. */
struct debug_name *next;
/* Name. */
const char *name;
/* Mark. This is used by debug_write. */
unsigned int mark;
/* Kind of object. */
enum debug_object_kind kind;
/* Linkage of object. */
enum debug_object_linkage linkage;
/* Tagged union with additional information about the object. */
union
{
/* DEBUG_OBJECT_TYPE. */
struct debug_type_s *type;
/* DEBUG_OBJECT_TAG. */
struct debug_type_s *tag;
/* DEBUG_OBJECT_VARIABLE. */
struct debug_variable *variable;
/* DEBUG_OBJECT_FUNCTION. */
struct debug_function *function;
/* DEBUG_OBJECT_INT_CONSTANT. */
bfd_vma int_constant;
/* DEBUG_OBJECT_FLOAT_CONSTANT. */
double float_constant;
/* DEBUG_OBJECT_TYPED_CONSTANT. */
struct debug_typed_constant *typed_constant;
} u;
};
/* During debug_write, a linked list of these structures is used to
keep track of ID numbers that have been assigned to classes. */
struct debug_class_id
{
/* Next ID number. */
struct debug_class_id *next;
/* The type with the ID. */
struct debug_type_s *type;
/* The tag; NULL if no tag. */
const char *tag;
};
/* During debug_type_samep, a linked list of these structures is kept
on the stack to avoid infinite recursion. */
struct debug_type_compare_list
{
/* Next type on list. */
struct debug_type_compare_list *next;
/* The types we are comparing. */
struct debug_type_s *t1;
struct debug_type_s *t2;
};
/* During debug_get_real_type, a linked list of these structures is
kept on the stack to avoid infinite recursion. */
struct debug_type_real_list
{
/* Next type on list. */
struct debug_type_real_list *next;
/* The type we are checking. */
struct debug_type_s *t;
};
/* Local functions. */
static void debug_error (const char *);
static struct debug_name *debug_add_to_namespace
(struct debug_handle *, struct debug_namespace **, const char *,
enum debug_object_kind, enum debug_object_linkage);
static struct debug_name *debug_add_to_current_namespace
(struct debug_handle *, const char *, enum debug_object_kind,
enum debug_object_linkage);
static struct debug_type_s *debug_make_type
(struct debug_handle *, enum debug_type_kind, unsigned int);
static struct debug_type_s *debug_get_real_type
(void *, debug_type, struct debug_type_real_list *);
static bfd_boolean debug_write_name
(struct debug_handle *, const struct debug_write_fns *, void *,
struct debug_name *);
static bfd_boolean debug_write_type
(struct debug_handle *, const struct debug_write_fns *, void *,
struct debug_type_s *, struct debug_name *);
static bfd_boolean debug_write_class_type
(struct debug_handle *, const struct debug_write_fns *, void *,
struct debug_type_s *, const char *);
static bfd_boolean debug_write_function
(struct debug_handle *, const struct debug_write_fns *, void *,
const char *, enum debug_object_linkage, struct debug_function *);
static bfd_boolean debug_write_block
(struct debug_handle *, const struct debug_write_fns *, void *,
struct debug_block *);
static bfd_boolean debug_write_linenos
(struct debug_handle *, const struct debug_write_fns *, void *, bfd_vma);
static bfd_boolean debug_set_class_id
(struct debug_handle *, const char *, struct debug_type_s *);
static bfd_boolean debug_type_samep
(struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
static bfd_boolean debug_class_type_samep
(struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
/* Issue an error message. */
static void
debug_error (const char *message)
{
fprintf (stderr, "%s\n", message);
}
/* Add an object to a namespace. */
static struct debug_name *
debug_add_to_namespace (struct debug_handle *info ATTRIBUTE_UNUSED,
struct debug_namespace **nsp, const char *name,
enum debug_object_kind kind,
enum debug_object_linkage linkage)
{
struct debug_name *n;
struct debug_namespace *ns;
n = (struct debug_name *) xmalloc (sizeof *n);
memset (n, 0, sizeof *n);
n->name = name;
n->kind = kind;
n->linkage = linkage;
ns = *nsp;
if (ns == NULL)
{
ns = (struct debug_namespace *) xmalloc (sizeof *ns);
memset (ns, 0, sizeof *ns);
ns->tail = &ns->list;
*nsp = ns;
}
*ns->tail = n;
ns->tail = &n->next;
return n;
}
/* Add an object to the current namespace. */
static struct debug_name *
debug_add_to_current_namespace (struct debug_handle *info, const char *name,
enum debug_object_kind kind,
enum debug_object_linkage linkage)
{
struct debug_namespace **nsp;
if (info->current_unit == NULL
|| info->current_file == NULL)
{
debug_error (_("debug_add_to_current_namespace: no current file"));
return NULL;
}
if (info->current_block != NULL)
nsp = &info->current_block->locals;
else
nsp = &info->current_file->globals;
return debug_add_to_namespace (info, nsp, name, kind, linkage);
}
/* Return a handle for debugging information. */
void *
debug_init (void)
{
struct debug_handle *ret;
ret = (struct debug_handle *) xmalloc (sizeof *ret);
memset (ret, 0, sizeof *ret);
return (void *) ret;
}
/* Set the source filename. This implicitly starts a new compilation
unit. */
bfd_boolean
debug_set_filename (void *handle, const char *name)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_file *nfile;
struct debug_unit *nunit;
if (name == NULL)
name = "";
nfile = (struct debug_file *) xmalloc (sizeof *nfile);
memset (nfile, 0, sizeof *nfile);
nfile->filename = name;
nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
memset (nunit, 0, sizeof *nunit);
nunit->files = nfile;
info->current_file = nfile;
if (info->current_unit != NULL)
info->current_unit->next = nunit;
else
{
assert (info->units == NULL);
info->units = nunit;
}
info->current_unit = nunit;
info->current_function = NULL;
info->current_block = NULL;
info->current_lineno = NULL;
return TRUE;
}
/* Change source files to the given file name. This is used for
include files in a single compilation unit. */
bfd_boolean
debug_start_source (void *handle, const char *name)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_file *f, **pf;
if (name == NULL)
name = "";
if (info->current_unit == NULL)
{
debug_error (_("debug_start_source: no debug_set_filename call"));
return FALSE;
}
for (f = info->current_unit->files; f != NULL; f = f->next)
{
if (filename_cmp (f->filename, name) == 0)
{
info->current_file = f;
return TRUE;
}
}
f = (struct debug_file *) xmalloc (sizeof *f);
memset (f, 0, sizeof *f);
f->filename = name;
for (pf = &info->current_file->next;
*pf != NULL;
pf = &(*pf)->next)
;
*pf = f;
info->current_file = f;
return TRUE;
}
/* Record a function definition. This implicitly starts a function
block. The debug_type argument is the type of the return value.
The boolean indicates whether the function is globally visible.
The bfd_vma is the address of the start of the function. Currently
the parameter types are specified by calls to
debug_record_parameter. FIXME: There is no way to specify nested
functions. */
bfd_boolean
debug_record_function (void *handle, const char *name,
debug_type return_type, bfd_boolean global,
bfd_vma addr)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_function *f;
struct debug_block *b;
struct debug_name *n;
if (name == NULL)
name = "";
if (return_type == NULL)
return FALSE;
if (info->current_unit == NULL)
{
debug_error (_("debug_record_function: no debug_set_filename call"));
return FALSE;
}
f = (struct debug_function *) xmalloc (sizeof *f);
memset (f, 0, sizeof *f);
f->return_type = return_type;
b = (struct debug_block *) xmalloc (sizeof *b);
memset (b, 0, sizeof *b);
b->start = addr;
b->end = (bfd_vma) -1;
f->blocks = b;
info->current_function = f;
info->current_block = b;
/* FIXME: If we could handle nested functions, this would be the
place: we would want to use a different namespace. */
n = debug_add_to_namespace (info,
&info->current_file->globals,
name,
DEBUG_OBJECT_FUNCTION,
(global
? DEBUG_LINKAGE_GLOBAL
: DEBUG_LINKAGE_STATIC));
if (n == NULL)
return FALSE;
n->u.function = f;
return TRUE;
}
/* Record a parameter for the current function. */
bfd_boolean
debug_record_parameter (void *handle, const char *name, debug_type type,
enum debug_parm_kind kind, bfd_vma val)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_parameter *p, **pp;
if (name == NULL || type == NULL)
return FALSE;
if (info->current_unit == NULL
|| info->current_function == NULL)
{
debug_error (_("debug_record_parameter: no current function"));
return FALSE;
}
p = (struct debug_parameter *) xmalloc (sizeof *p);
memset (p, 0, sizeof *p);
p->name = name;
p->type = type;
p->kind = kind;
p->val = val;
for (pp = &info->current_function->parameters;
*pp != NULL;
pp = &(*pp)->next)
;
*pp = p;
return TRUE;
}
/* End a function. FIXME: This should handle function nesting. */
bfd_boolean
debug_end_function (void *handle, bfd_vma addr)
{
struct debug_handle *info = (struct debug_handle *) handle;
if (info->current_unit == NULL
|| info->current_block == NULL
|| info->current_function == NULL)
{
debug_error (_("debug_end_function: no current function"));
return FALSE;
}
if (info->current_block->parent != NULL)
{
debug_error (_("debug_end_function: some blocks were not closed"));
return FALSE;
}
info->current_block->end = addr;
info->current_function = NULL;
info->current_block = NULL;
return TRUE;
}
/* Start a block in a function. All local information will be
recorded in this block, until the matching call to debug_end_block.
debug_start_block and debug_end_block may be nested. The bfd_vma
argument is the address at which this block starts. */
bfd_boolean
debug_start_block (void *handle, bfd_vma addr)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_block *b, **pb;
/* We must always have a current block: debug_record_function sets
one up. */
if (info->current_unit == NULL
|| info->current_block == NULL)
{
debug_error (_("debug_start_block: no current block"));
return FALSE;
}
b = (struct debug_block *) xmalloc (sizeof *b);
memset (b, 0, sizeof *b);
b->parent = info->current_block;
b->start = addr;
b->end = (bfd_vma) -1;
/* This new block is a child of the current block. */
for (pb = &info->current_block->children;
*pb != NULL;
pb = &(*pb)->next)
;
*pb = b;
info->current_block = b;
return TRUE;
}
/* Finish a block in a function. This matches the call to
debug_start_block. The argument is the address at which this block
ends. */
bfd_boolean
debug_end_block (void *handle, bfd_vma addr)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_block *parent;
if (info->current_unit == NULL
|| info->current_block == NULL)
{
debug_error (_("debug_end_block: no current block"));
return FALSE;
}
parent = info->current_block->parent;
if (parent == NULL)
{
debug_error (_("debug_end_block: attempt to close top level block"));
return FALSE;
}
info->current_block->end = addr;
info->current_block = parent;
return TRUE;
}
/* Associate a line number in the current source file and function
with a given address. */
bfd_boolean
debug_record_line (void *handle, unsigned long lineno, bfd_vma addr)
{
struct debug_handle *info = (struct debug_handle *) handle;
struct debug_lineno *l;
unsigned int i;
if (info->current_unit == NULL)
{
debug_error (_("debug_record_line: no current unit"));
return FALSE;
}
l = info->current_lineno;
if (l != NULL && l->file == info->current_file)
{
for (i = 0; i < DEBUG_LINENO_COUNT; i++)
{
if (l->linenos[i] == (unsigned long) -1)
{
l->linenos[i] = lineno;
l->addrs[i] = addr;
return TRUE;
}
}
}
/* If we get here, then either 1) there is no current_lineno
structure, which means this is the first line number in this
compilation unit, 2) the current_lineno structure is for a
different file, or 3) the current_lineno structure is full.
Regardless, we want to allocate a new debug_lineno structure, put
it in the right place, and make it the new current_lineno
structure. */
l = (struct debug_lineno *) xmalloc (sizeof *l);
memset (l, 0, sizeof *l);
l->file = info->current_file;
l->linenos[0] = lineno;
l->addrs[0] = addr;
for (i = 1; i < DEBUG_LINENO_COUNT; i++)
l->linenos[i] = (unsigned long) -1;
if (info->current_lineno != NULL)
info->current_lineno->next = l;