-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrstabs.c
2271 lines (1819 loc) · 55.2 KB
/
wrstabs.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
/* wrstabs.c -- Output stabs debugging information
Copyright (C) 1996-2015 Free Software Foundation, Inc.
Written by Ian Lance Taylor <ian@cygnus.com>.
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 contains code which writes out stabs debugging
information. */
#include "sysdep.h"
#include <assert.h>
#include "bfd.h"
#include "libiberty.h"
#include "filenames.h"
#include "safe-ctype.h"
#include "bucomm.h"
#include "debug.h"
#include "budbg.h"
#include "aout/aout64.h"
#include "aout/stab_gnu.h"
/* The size of a stabs symbol. This presumes 32 bit values. */
#define STAB_SYMBOL_SIZE (12)
/* An entry in a string hash table. */
struct string_hash_entry
{
struct bfd_hash_entry root;
/* Next string in this table. */
struct string_hash_entry *next;
/* Index in string table. */
long index;
/* Size of type if this is a typedef. */
unsigned int size;
};
/* A string hash table. */
struct string_hash_table
{
struct bfd_hash_table table;
};
/* The type stack. Each element on the stack is a string. */
struct stab_type_stack
{
/* The next element on the stack. */
struct stab_type_stack *next;
/* This element as a string. */
char *string;
/* The type index of this element. */
long index;
/* The size of the type. */
unsigned int size;
/* Whether type string defines a new type. */
bfd_boolean definition;
/* String defining struct fields. */
char *fields;
/* NULL terminated array of strings defining base classes for a
class. */
char **baseclasses;
/* String defining class methods. */
char *methods;
/* String defining vtable pointer for a class. */
char *vtable;
};
/* This structure is used to keep track of type indices for tagged
types. */
struct stab_tag
{
/* The type index. */
long index;
/* The tag name. */
const char *tag;
/* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
type is defined. */
enum debug_type_kind kind;
/* The size of the struct. */
unsigned int size;
};
/* We remember various sorts of type indices. They are not related,
but, for convenience, we keep all the information in this
structure. */
struct stab_type_cache
{
/* The void type index. */
long void_type;
/* Signed integer type indices, indexed by size - 1. */
long signed_integer_types[8];
/* Unsigned integer type indices, indexed by size - 1. */
long unsigned_integer_types[8];
/* Floating point types, indexed by size - 1. */
long float_types[16];
/* Pointers to types, indexed by the type index. */
long *pointer_types;
size_t pointer_types_alloc;
/* Functions returning types, indexed by the type index. */
long *function_types;
size_t function_types_alloc;
/* References to types, indexed by the type index. */
long *reference_types;
size_t reference_types_alloc;
/* Struct/union/class type indices, indexed by the struct id. */
struct stab_tag *struct_types;
size_t struct_types_alloc;
};
/* This is the handle passed through debug_write. */
struct stab_write_handle
{
/* The BFD. */
bfd *abfd;
/* This buffer holds the symbols. */
bfd_byte *symbols;
size_t symbols_size;
size_t symbols_alloc;
/* This is a list of hash table entries for the strings. */
struct string_hash_entry *strings;
/* The last string hash table entry. */
struct string_hash_entry *last_string;
/* The size of the strings. */
size_t strings_size;
/* This hash table eliminates duplicate strings. */
struct string_hash_table strhash;
/* The type stack. */
struct stab_type_stack *type_stack;
/* The next type index. */
long type_index;
/* The type cache. */
struct stab_type_cache type_cache;
/* A mapping from typedef names to type indices. */
struct string_hash_table typedef_hash;
/* If this is not -1, it is the offset to the most recent N_SO
symbol, and the value of that symbol needs to be set. */
long so_offset;
/* If this is not -1, it is the offset to the most recent N_FUN
symbol, and the value of that symbol needs to be set. */
long fun_offset;
/* The last text section address seen. */
bfd_vma last_text_address;
/* The block nesting depth. */
unsigned int nesting;
/* The function address. */
bfd_vma fnaddr;
/* A pending LBRAC symbol. */
bfd_vma pending_lbrac;
/* The current line number file name. */
const char *lineno_filename;
};
static struct bfd_hash_entry *string_hash_newfunc
(struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
static bfd_boolean stab_write_symbol
(struct stab_write_handle *, int, int, bfd_vma, const char *);
static bfd_boolean stab_push_string
(struct stab_write_handle *, const char *, long, bfd_boolean, unsigned int);
static bfd_boolean stab_push_defined_type
(struct stab_write_handle *, long, unsigned int);
static char *stab_pop_type (struct stab_write_handle *);
static bfd_boolean stab_modify_type
(struct stab_write_handle *, int, unsigned int, long **, size_t *);
static long stab_get_struct_index
(struct stab_write_handle *, const char *, unsigned int,
enum debug_type_kind, unsigned int *);
static bfd_boolean stab_class_method_var
(struct stab_write_handle *, const char *, enum debug_visibility,
bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
static bfd_boolean stab_start_compilation_unit (void *, const char *);
static bfd_boolean stab_start_source (void *, const char *);
static bfd_boolean stab_empty_type (void *);
static bfd_boolean stab_void_type (void *);
static bfd_boolean stab_int_type (void *, unsigned int, bfd_boolean);
static bfd_boolean stab_float_type (void *, unsigned int);
static bfd_boolean stab_complex_type (void *, unsigned int);
static bfd_boolean stab_bool_type (void *, unsigned int);
static bfd_boolean stab_enum_type
(void *, const char *, const char **, bfd_signed_vma *);
static bfd_boolean stab_pointer_type (void *);
static bfd_boolean stab_function_type (void *, int, bfd_boolean);
static bfd_boolean stab_reference_type (void *);
static bfd_boolean stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
static bfd_boolean stab_array_type
(void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
static bfd_boolean stab_set_type (void *, bfd_boolean);
static bfd_boolean stab_offset_type (void *);
static bfd_boolean stab_method_type (void *, bfd_boolean, int, bfd_boolean);
static bfd_boolean stab_const_type (void *);
static bfd_boolean stab_volatile_type (void *);
static bfd_boolean stab_start_struct_type
(void *, const char *, unsigned int, bfd_boolean, unsigned int);
static bfd_boolean stab_struct_field
(void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
static bfd_boolean stab_end_struct_type (void *);
static bfd_boolean stab_start_class_type
(void *, const char *, unsigned int, bfd_boolean, unsigned int,
bfd_boolean, bfd_boolean);
static bfd_boolean stab_class_static_member
(void *, const char *, const char *, enum debug_visibility);
static bfd_boolean stab_class_baseclass
(void *, bfd_vma, bfd_boolean, enum debug_visibility);
static bfd_boolean stab_class_start_method (void *, const char *);
static bfd_boolean stab_class_method_variant
(void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
bfd_vma, bfd_boolean);
static bfd_boolean stab_class_static_method_variant
(void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
static bfd_boolean stab_class_end_method (void *);
static bfd_boolean stab_end_class_type (void *);
static bfd_boolean stab_typedef_type (void *, const char *);
static bfd_boolean stab_tag_type
(void *, const char *, unsigned int, enum debug_type_kind);
static bfd_boolean stab_typdef (void *, const char *);
static bfd_boolean stab_tag (void *, const char *);
static bfd_boolean stab_int_constant (void *, const char *, bfd_vma);
static bfd_boolean stab_float_constant (void *, const char *, double);
static bfd_boolean stab_typed_constant (void *, const char *, bfd_vma);
static bfd_boolean stab_variable
(void *, const char *, enum debug_var_kind, bfd_vma);
static bfd_boolean stab_start_function (void *, const char *, bfd_boolean);
static bfd_boolean stab_function_parameter
(void *, const char *, enum debug_parm_kind, bfd_vma);
static bfd_boolean stab_start_block (void *, bfd_vma);
static bfd_boolean stab_end_block (void *, bfd_vma);
static bfd_boolean stab_end_function (void *);
static bfd_boolean stab_lineno (void *, const char *, unsigned long, bfd_vma);
static const struct debug_write_fns stab_fns =
{
stab_start_compilation_unit,
stab_start_source,
stab_empty_type,
stab_void_type,
stab_int_type,
stab_float_type,
stab_complex_type,
stab_bool_type,
stab_enum_type,
stab_pointer_type,
stab_function_type,
stab_reference_type,
stab_range_type,
stab_array_type,
stab_set_type,
stab_offset_type,
stab_method_type,
stab_const_type,
stab_volatile_type,
stab_start_struct_type,
stab_struct_field,
stab_end_struct_type,
stab_start_class_type,
stab_class_static_member,
stab_class_baseclass,
stab_class_start_method,
stab_class_method_variant,
stab_class_static_method_variant,
stab_class_end_method,
stab_end_class_type,
stab_typedef_type,
stab_tag_type,
stab_typdef,
stab_tag,
stab_int_constant,
stab_float_constant,
stab_typed_constant,
stab_variable,
stab_start_function,
stab_function_parameter,
stab_start_block,
stab_end_block,
stab_end_function,
stab_lineno
};
/* Routine to create an entry in a string hash table. */
static struct bfd_hash_entry *
string_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table, const char *string)
{
struct string_hash_entry *ret = (struct string_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == (struct string_hash_entry *) NULL)
ret = ((struct string_hash_entry *)
bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
if (ret == (struct string_hash_entry *) NULL)
return NULL;
/* Call the allocation method of the superclass. */
ret = ((struct string_hash_entry *)
bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
if (ret)
{
/* Initialize the local fields. */
ret->next = NULL;
ret->index = -1;
ret->size = 0;
}
return (struct bfd_hash_entry *) ret;
}
/* Look up an entry in a string hash table. */
#define string_hash_lookup(t, string, create, copy) \
((struct string_hash_entry *) \
bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
/* Add a symbol to the stabs debugging information we are building. */
static bfd_boolean
stab_write_symbol (struct stab_write_handle *info, int type, int desc,
bfd_vma value, const char *string)
{
bfd_size_type strx;
bfd_byte sym[STAB_SYMBOL_SIZE];
if (string == NULL)
strx = 0;
else
{
struct string_hash_entry *h;
h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
if (h == NULL)
{
non_fatal (_("string_hash_lookup failed: %s"),
bfd_errmsg (bfd_get_error ()));
return FALSE;
}
if (h->index != -1)
strx = h->index;
else
{
strx = info->strings_size;
h->index = strx;
if (info->last_string == NULL)
info->strings = h;
else
info->last_string->next = h;
info->last_string = h;
info->strings_size += strlen (string) + 1;
}
}
/* This presumes 32 bit values. */
bfd_put_32 (info->abfd, strx, sym);
bfd_put_8 (info->abfd, type, sym + 4);
bfd_put_8 (info->abfd, 0, sym + 5);
bfd_put_16 (info->abfd, desc, sym + 6);
bfd_put_32 (info->abfd, value, sym + 8);
if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
{
info->symbols_alloc *= 2;
info->symbols = (bfd_byte *) xrealloc (info->symbols,
info->symbols_alloc);
}
memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
info->symbols_size += STAB_SYMBOL_SIZE;
return TRUE;
}
/* Push a string on to the type stack. */
static bfd_boolean
stab_push_string (struct stab_write_handle *info, const char *string,
long tindex, bfd_boolean definition, unsigned int size)
{
struct stab_type_stack *s;
s = (struct stab_type_stack *) xmalloc (sizeof *s);
s->string = xstrdup (string);
s->index = tindex;
s->definition = definition;
s->size = size;
s->fields = NULL;
s->baseclasses = NULL;
s->methods = NULL;
s->vtable = NULL;
s->next = info->type_stack;
info->type_stack = s;
return TRUE;
}
/* Push a type index which has already been defined. */
static bfd_boolean
stab_push_defined_type (struct stab_write_handle *info, long tindex,
unsigned int size)
{
char buf[20];
sprintf (buf, "%ld", tindex);
return stab_push_string (info, buf, tindex, FALSE, size);
}
/* Pop a type off the type stack. The caller is responsible for
freeing the string. */
static char *
stab_pop_type (struct stab_write_handle *info)
{
struct stab_type_stack *s;
char *ret;
s = info->type_stack;
assert (s != NULL);
info->type_stack = s->next;
ret = s->string;
free (s);
return ret;
}
/* The general routine to write out stabs in sections debugging
information. This accumulates the stabs symbols and the strings in
two obstacks. We can't easily write out the information as we go
along, because we need to know the section sizes before we can
write out the section contents. ABFD is the BFD and DHANDLE is the
handle for the debugging information. This sets *PSYMS to point to
the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
strings, and *PSTRINGSIZE to the size of the strings. */
bfd_boolean
write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
bfd_byte **psyms,
bfd_size_type *psymsize,
bfd_byte **pstrings,
bfd_size_type *pstringsize)
{
struct stab_write_handle info;
struct string_hash_entry *h;
bfd_byte *p;
info.abfd = abfd;
info.symbols_size = 0;
info.symbols_alloc = 500;
info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
info.strings = NULL;
info.last_string = NULL;
/* Reserve 1 byte for a null byte. */
info.strings_size = 1;
if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
sizeof (struct string_hash_entry))
|| !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
sizeof (struct string_hash_entry)))
{
non_fatal ("bfd_hash_table_init_failed: %s",
bfd_errmsg (bfd_get_error ()));
return FALSE;
}
info.type_stack = NULL;
info.type_index = 1;
memset (&info.type_cache, 0, sizeof info.type_cache);
info.so_offset = -1;
info.fun_offset = -1;
info.last_text_address = 0;
info.nesting = 0;
info.fnaddr = 0;
info.pending_lbrac = (bfd_vma) -1;
/* The initial symbol holds the string size. */
if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
return FALSE;
/* Output an initial N_SO symbol. */
info.so_offset = info.symbols_size;
if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
return FALSE;
if (! debug_write (dhandle, &stab_fns, (void *) &info))
return FALSE;
assert (info.pending_lbrac == (bfd_vma) -1);
/* Output a trailing N_SO. */
if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
(const char *) NULL))
return FALSE;
/* Put the string size in the initial symbol. */
bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
*psyms = info.symbols;
*psymsize = info.symbols_size;
*pstringsize = info.strings_size;
*pstrings = (bfd_byte *) xmalloc (info.strings_size);
p = *pstrings;
*p++ = '\0';
for (h = info.strings; h != NULL; h = h->next)
{
strcpy ((char *) p, h->root.string);
p += strlen ((char *) p) + 1;
}
return TRUE;
}
/* Start writing out information for a compilation unit. */
static bfd_boolean
stab_start_compilation_unit (void *p, const char *filename)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
/* We would normally output an N_SO symbol here. However, that
would force us to reset all of our type information. I think we
will be better off just outputting an N_SOL symbol, and not
worrying about splitting information between files. */
info->lineno_filename = filename;
return stab_write_symbol (info, N_SOL, 0, 0, filename);
}
/* Start writing out information for a particular source file. */
static bfd_boolean
stab_start_source (void *p, const char *filename)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
/* FIXME: The symbol's value is supposed to be the text section
address. However, we would have to fill it in later, and gdb
doesn't care, so we don't bother with it. */
info->lineno_filename = filename;
return stab_write_symbol (info, N_SOL, 0, 0, filename);
}
/* Push an empty type. This shouldn't normally happen. We just use a
void type. */
static bfd_boolean
stab_empty_type (void *p)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
/* We don't call stab_void_type if the type is not yet defined,
because that might screw up the typedef. */
if (info->type_cache.void_type != 0)
return stab_push_defined_type (info, info->type_cache.void_type, 0);
else
{
long tindex;
char buf[40];
tindex = info->type_index;
++info->type_index;
sprintf (buf, "%ld=%ld", tindex, tindex);
return stab_push_string (info, buf, tindex, FALSE, 0);
}
}
/* Push a void type. */
static bfd_boolean
stab_void_type (void *p)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
if (info->type_cache.void_type != 0)
return stab_push_defined_type (info, info->type_cache.void_type, 0);
else
{
long tindex;
char buf[40];
tindex = info->type_index;
++info->type_index;
info->type_cache.void_type = tindex;
sprintf (buf, "%ld=%ld", tindex, tindex);
return stab_push_string (info, buf, tindex, TRUE, 0);
}
}
/* Push an integer type. */
static bfd_boolean
stab_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
long *cache;
if (size <= 0 || (size > sizeof (long) && size != 8))
{
non_fatal (_("stab_int_type: bad size %u"), size);
return FALSE;
}
if (unsignedp)
cache = info->type_cache.signed_integer_types;
else
cache = info->type_cache.unsigned_integer_types;
if (cache[size - 1] != 0)
return stab_push_defined_type (info, cache[size - 1], size);
else
{
long tindex;
char buf[100];
tindex = info->type_index;
++info->type_index;
cache[size - 1] = tindex;
sprintf (buf, "%ld=r%ld;", tindex, tindex);
if (unsignedp)
{
strcat (buf, "0;");
if (size < sizeof (long))
sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
else if (size == sizeof (long))
strcat (buf, "-1;");
else if (size == 8)
strcat (buf, "01777777777777777777777;");
else
abort ();
}
else
{
if (size <= sizeof (long))
sprintf (buf + strlen (buf), "%ld;%ld;",
(long) - ((unsigned long) 1 << (size * 8 - 1)),
(long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
else if (size == 8)
strcat (buf, "01000000000000000000000;0777777777777777777777;");
else
abort ();
}
return stab_push_string (info, buf, tindex, TRUE, size);
}
}
/* Push a floating point type. */
static bfd_boolean
stab_float_type (void *p, unsigned int size)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
if (size > 0
&& size - 1 < (sizeof info->type_cache.float_types
/ sizeof info->type_cache.float_types[0])
&& info->type_cache.float_types[size - 1] != 0)
return stab_push_defined_type (info,
info->type_cache.float_types[size - 1],
size);
else
{
long tindex;
char *int_type;
char buf[50];
/* Floats are defined as a subrange of int. */
if (! stab_int_type (info, 4, FALSE))
return FALSE;
int_type = stab_pop_type (info);
tindex = info->type_index;
++info->type_index;
if (size > 0
&& size - 1 < (sizeof info->type_cache.float_types
/ sizeof info->type_cache.float_types[0]))
info->type_cache.float_types[size - 1] = tindex;
sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
free (int_type);
return stab_push_string (info, buf, tindex, TRUE, size);
}
}
/* Push a complex type. */
static bfd_boolean
stab_complex_type (void *p, unsigned int size)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
char buf[50];
long tindex;
tindex = info->type_index;
++info->type_index;
sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
return stab_push_string (info, buf, tindex, TRUE, size * 2);
}
/* Push a bfd_boolean type. We use an XCOFF predefined type, since gdb
always recognizes them. */
static bfd_boolean
stab_bool_type (void *p, unsigned int size)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
long tindex;
switch (size)
{
case 1:
tindex = -21;
break;
case 2:
tindex = -22;
break;
default:
case 4:
tindex = -16;
break;
case 8:
tindex = -33;
break;
}
return stab_push_defined_type (info, tindex, size);
}
/* Push an enum type. */
static bfd_boolean
stab_enum_type (void *p, const char *tag, const char **names,
bfd_signed_vma *vals)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
size_t len;
const char **pn;
char *buf;
long tindex = 0;
bfd_signed_vma *pv;
if (names == NULL)
{
assert (tag != NULL);
buf = (char *) xmalloc (10 + strlen (tag));
sprintf (buf, "xe%s:", tag);
/* FIXME: The size is just a guess. */
if (! stab_push_string (info, buf, 0, FALSE, 4))
return FALSE;
free (buf);
return TRUE;
}
len = 10;
if (tag != NULL)
len += strlen (tag);
for (pn = names; *pn != NULL; pn++)
len += strlen (*pn) + 20;
buf = (char *) xmalloc (len);
if (tag == NULL)
strcpy (buf, "e");
else
{
tindex = info->type_index;
++info->type_index;
sprintf (buf, "%s:T%ld=e", tag, tindex);
}
for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
strcat (buf, ";");
if (tag == NULL)
{
/* FIXME: The size is just a guess. */
if (! stab_push_string (info, buf, 0, FALSE, 4))
return FALSE;
}
else
{
/* FIXME: The size is just a guess. */
if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
|| ! stab_push_defined_type (info, tindex, 4))
return FALSE;
}
free (buf);
return TRUE;
}
/* Push a modification of the top type on the stack. Cache the
results in CACHE and CACHE_ALLOC. */
static bfd_boolean
stab_modify_type (struct stab_write_handle *info, int mod,
unsigned int size, long **cache, size_t *cache_alloc)
{
long targindex;
long tindex;
char *s, *buf;
assert (info->type_stack != NULL);
targindex = info->type_stack->index;
if (targindex <= 0
|| cache == NULL)
{
bfd_boolean definition;
/* Either the target type has no index, or we aren't caching
this modifier. Either way we have no way of recording the
new type, so we don't bother to define one. */
definition = info->type_stack->definition;
s = stab_pop_type (info);
buf = (char *) xmalloc (strlen (s) + 2);
sprintf (buf, "%c%s", mod, s);
free (s);
if (! stab_push_string (info, buf, 0, definition, size))
return FALSE;
free (buf);
}
else
{
if ((size_t) targindex >= *cache_alloc)
{
size_t alloc;
alloc = *cache_alloc;
if (alloc == 0)
alloc = 10;
while ((size_t) targindex >= alloc)
alloc *= 2;
*cache = (long *) xrealloc (*cache, alloc * sizeof (long));
memset (*cache + *cache_alloc, 0,
(alloc - *cache_alloc) * sizeof (long));
*cache_alloc = alloc;
}
tindex = (*cache)[targindex];
if (tindex != 0 && ! info->type_stack->definition)
{
/* We have already defined a modification of this type, and
the entry on the type stack is not a definition, so we
can safely discard it (we may have a definition on the
stack, even if we already defined a modification, if it
is a struct which we did not define at the time it was
referenced). */
free (stab_pop_type (info));
if (! stab_push_defined_type (info, tindex, size))
return FALSE;
}
else
{
tindex = info->type_index;
++info->type_index;
s = stab_pop_type (info);
buf = (char *) xmalloc (strlen (s) + 20);
sprintf (buf, "%ld=%c%s", tindex, mod, s);
free (s);
(*cache)[targindex] = tindex;
if (! stab_push_string (info, buf, tindex, TRUE, size))
return FALSE;
free (buf);
}
}
return TRUE;
}
/* Push a pointer type. */
static bfd_boolean
stab_pointer_type (void *p)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
/* FIXME: The size should depend upon the architecture. */
return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
&info->type_cache.pointer_types_alloc);
}
/* Push a function type. */
static bfd_boolean
stab_function_type (void *p, int argcount,
bfd_boolean varargs ATTRIBUTE_UNUSED)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
int i;
/* We have no way to represent the argument types, so we just
discard them. However, if they define new types, we must output
them. We do this by producing empty typedefs. */
for (i = 0; i < argcount; i++)
{
if (! info->type_stack->definition)
free (stab_pop_type (info));
else
{
char *s, *buf;
s = stab_pop_type (info);
buf = (char *) xmalloc (strlen (s) + 3);
sprintf (buf, ":t%s", s);
free (s);
if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
return FALSE;
free (buf);
}
}
return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
&info->type_cache.function_types_alloc);
}
/* Push a reference type. */
static bfd_boolean
stab_reference_type (void *p)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
/* FIXME: The size should depend upon the architecture. */
return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
&info->type_cache.reference_types_alloc);
}
/* Push a range type. */
static bfd_boolean
stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
{
struct stab_write_handle *info = (struct stab_write_handle *) p;
bfd_boolean definition;
unsigned int size;
char *s, *buf;
definition = info->type_stack->definition;
size = info->type_stack->size;
s = stab_pop_type (info);
buf = (char *) xmalloc (strlen (s) + 100);