This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffread.c
2164 lines (1833 loc) · 62.1 KB
/
coffread.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
/* Read coff symbol tables and convert to internal format, for GDB.
Copyright (C) 1987-2024 Free Software Foundation, Inc.
Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
This file is part of GDB.
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, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "symtab.h"
#include "gdbtypes.h"
#include "demangle.h"
#include "breakpoint.h"
#include "bfd.h"
#include "gdbsupport/gdb_obstack.h"
#include <ctype.h>
#include "coff/internal.h"
#include "libcoff.h"
#include "objfiles.h"
#include "buildsym-legacy.h"
#include "stabsread.h"
#include "complaints.h"
#include "target.h"
#include "block.h"
#include "dictionary.h"
#include "dwarf2/public.h"
#include "coff-pe-read.h"
/* The objfile we are currently reading. */
static struct objfile *coffread_objfile;
struct coff_symfile_info
{
file_ptr min_lineno_offset = 0; /* Where in file lowest line#s are. */
file_ptr max_lineno_offset = 0; /* 1+last byte of line#s in file. */
CORE_ADDR textaddr = 0; /* Addr of .text section. */
unsigned int textsize = 0; /* Size of .text section. */
std::vector<asection *> *stabsects; /* .stab sections. */
asection *stabstrsect = nullptr; /* Section pointer for .stab section. */
char *stabstrdata = nullptr;
};
/* Key for COFF-associated data. */
static const registry<objfile>::key<coff_symfile_info> coff_objfile_data_key;
/* Translate an external name string into a user-visible name. */
#define EXTERNAL_NAME(string, abfd) \
(*string != '\0' && *string == bfd_get_symbol_leading_char (abfd) \
? string + 1 : string)
/* To be an sdb debug type, type must have at least a basic or primary
derived type. Using this rather than checking against T_NULL is
said to prevent core dumps if we try to operate on Michael Bloom
dbx-in-coff file. */
#define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
/* Core address of start and end of text of current source file.
This comes from a ".text" symbol where x_nlinno > 0. */
static CORE_ADDR current_source_start_addr;
static CORE_ADDR current_source_end_addr;
/* The addresses of the symbol table stream and number of symbols
of the object file we are reading (as copied into core). */
static bfd *nlist_bfd_global;
static int nlist_nsyms_global;
/* Pointers to scratch storage, used for reading raw symbols and
auxents. */
static char *temp_sym;
static char *temp_aux;
/* Local variables that hold the shift and mask values for the
COFF file that we are currently reading. These come back to us
from BFD, and are referenced by their macro names, as well as
internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
macros from include/coff/internal.h . */
static unsigned local_n_btmask;
static unsigned local_n_btshft;
static unsigned local_n_tmask;
static unsigned local_n_tshift;
#define N_BTMASK local_n_btmask
#define N_BTSHFT local_n_btshft
#define N_TMASK local_n_tmask
#define N_TSHIFT local_n_tshift
/* Local variables that hold the sizes in the file of various COFF
structures. (We only need to know this to read them from the file
-- BFD will then translate the data in them, into `internal_xxx'
structs in the right byte order, alignment, etc.) */
static unsigned local_linesz;
static unsigned local_symesz;
static unsigned local_auxesz;
/* This is set if this is a PE format file. */
static int pe_file;
/* Chain of typedefs of pointers to empty struct/union types.
They are chained thru the SYMBOL_VALUE_CHAIN. */
static struct symbol *opaque_type_chain[HASHSIZE];
/* Simplified internal version of coff symbol table information. */
struct coff_symbol
{
char *c_name;
int c_symnum; /* Symbol number of this entry. */
int c_naux; /* 0 if syment only, 1 if syment +
auxent, etc. */
CORE_ADDR c_value;
int c_sclass;
int c_secnum;
unsigned int c_type;
};
/* Vector of types defined so far, indexed by their type numbers. */
static struct type **type_vector;
/* Number of elements allocated for type_vector currently. */
static int type_vector_length;
/* Initial size of type vector. Is realloc'd larger if needed, and
realloc'd down to the size actually used, when completed. */
#define INITIAL_TYPE_VECTOR_LENGTH 160
static char *linetab = NULL;
static file_ptr linetab_offset;
static file_ptr linetab_size;
static char *stringtab = NULL;
static long stringtab_length = 0;
extern void stabsread_clear_cache (void);
static struct type *coff_read_struct_type (int, int, int,
struct objfile *);
static struct type *decode_base_type (struct coff_symbol *,
unsigned int,
union internal_auxent *,
struct objfile *);
static struct type *decode_type (struct coff_symbol *, unsigned int,
union internal_auxent *,
struct objfile *);
static struct type *decode_function_type (struct coff_symbol *,
unsigned int,
union internal_auxent *,
struct objfile *);
static struct type *coff_read_enum_type (int, int, int,
struct objfile *);
static struct symbol *process_coff_symbol (struct coff_symbol *,
union internal_auxent *,
struct objfile *);
static void patch_opaque_types (struct symtab *);
static void enter_linenos (file_ptr, int, int, struct objfile *);
static int init_lineno (bfd *, file_ptr, file_ptr, gdb::unique_xmalloc_ptr<char> *);
static char *getsymname (struct internal_syment *);
static const char *coff_getfilename (union internal_auxent *);
static int init_stringtab (bfd *, file_ptr, gdb::unique_xmalloc_ptr<char> *);
static void read_one_sym (struct coff_symbol *,
struct internal_syment *,
union internal_auxent *);
static void coff_symtab_read (minimal_symbol_reader &,
file_ptr, unsigned int, struct objfile *);
/* We are called once per section from coff_symfile_read. We
need to examine each section we are passed, check to see
if it is something we are interested in processing, and
if so, stash away some access information for the section.
FIXME: The section names should not be hardwired strings (what
should they be? I don't think most object file formats have enough
section flags to specify what kind of debug section it is
-kingdon). */
static void
coff_locate_sections (bfd *abfd, asection *sectp, void *csip)
{
struct coff_symfile_info *csi;
const char *name;
csi = (struct coff_symfile_info *) csip;
name = bfd_section_name (sectp);
if (strcmp (name, ".text") == 0)
{
csi->textaddr = bfd_section_vma (sectp);
csi->textsize += bfd_section_size (sectp);
}
else if (startswith (name, ".text"))
{
csi->textsize += bfd_section_size (sectp);
}
else if (strcmp (name, ".stabstr") == 0)
{
csi->stabstrsect = sectp;
}
else if (startswith (name, ".stab"))
{
const char *s;
/* We can have multiple .stab sections if linked with
--split-by-reloc. */
for (s = name + sizeof ".stab" - 1; *s != '\0'; s++)
if (!isdigit (*s))
break;
if (*s == '\0')
csi->stabsects->push_back (sectp);
}
}
/* Return the section_offsets* that CS points to. */
static int cs_to_section (struct coff_symbol *, struct objfile *);
struct coff_find_targ_sec_arg
{
int targ_index;
asection **resultp;
};
static void
find_targ_sec (bfd *abfd, asection *sect, void *obj)
{
struct coff_find_targ_sec_arg *args = (struct coff_find_targ_sec_arg *) obj;
if (sect->target_index == args->targ_index)
*args->resultp = sect;
}
/* Return the bfd_section that CS points to. */
static struct bfd_section*
cs_to_bfd_section (struct coff_symbol *cs, struct objfile *objfile)
{
asection *sect = NULL;
struct coff_find_targ_sec_arg args;
args.targ_index = cs->c_secnum;
args.resultp = §
bfd_map_over_sections (objfile->obfd.get (), find_targ_sec, &args);
return sect;
}
/* Return the section number (SECT_OFF_*) that CS points to. */
static int
cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
{
asection *sect = cs_to_bfd_section (cs, objfile);
if (sect == NULL)
return SECT_OFF_TEXT (objfile);
return gdb_bfd_section_index (objfile->obfd.get (), sect);
}
/* Return the address of the section of a COFF symbol. */
static CORE_ADDR cs_section_address (struct coff_symbol *, bfd *);
static CORE_ADDR
cs_section_address (struct coff_symbol *cs, bfd *abfd)
{
asection *sect = NULL;
struct coff_find_targ_sec_arg args;
CORE_ADDR addr = 0;
args.targ_index = cs->c_secnum;
args.resultp = §
bfd_map_over_sections (abfd, find_targ_sec, &args);
if (sect != NULL)
addr = bfd_section_vma (sect);
return addr;
}
/* Look up a coff type-number index. Return the address of the slot
where the type for that index is stored.
The type-number is in INDEX.
This can be used for finding the type associated with that index
or for associating a new type with the index. */
static struct type **
coff_lookup_type (int index)
{
if (index >= type_vector_length)
{
int old_vector_length = type_vector_length;
type_vector_length *= 2;
if (index /* is still */ >= type_vector_length)
type_vector_length = index * 2;
type_vector = (struct type **)
xrealloc ((char *) type_vector,
type_vector_length * sizeof (struct type *));
memset (&type_vector[old_vector_length], 0,
(type_vector_length - old_vector_length) * sizeof (struct type *));
}
return &type_vector[index];
}
/* Make sure there is a type allocated for type number index
and return the type object.
This can create an empty (zeroed) type object. */
static struct type *
coff_alloc_type (int index)
{
struct type **type_addr = coff_lookup_type (index);
struct type *type = *type_addr;
/* If we are referring to a type not known at all yet,
allocate an empty type for it.
We will fill it in later if we find out how. */
if (type == NULL)
{
type = type_allocator (coffread_objfile, language_c).new_type ();
*type_addr = type;
}
return type;
}
/* Start a new symtab for a new source file.
This is called when a COFF ".file" symbol is seen;
it indicates the start of data for one original source file. */
static void
coff_start_compunit_symtab (struct objfile *objfile, const char *name)
{
within_function = 0;
start_compunit_symtab (objfile,
name,
/* We never know the directory name for COFF. */
NULL,
/* The start address is irrelevant, since we call
set_last_source_start_addr in coff_end_compunit_symtab. */
0,
/* Let buildsym.c deduce the language for this symtab. */
language_unknown);
record_debugformat ("COFF");
}
/* Save the vital information from when starting to read a file,
for use when closing off the current file.
NAME is the file name the symbols came from, START_ADDR is the
first text address for the file, and SIZE is the number of bytes of
text. */
static void
complete_symtab (const char *name, CORE_ADDR start_addr, unsigned int size)
{
set_last_source_file (name);
current_source_start_addr = start_addr;
current_source_end_addr = start_addr + size;
}
/* Finish the symbol definitions for one main source file, close off
all the lexical contexts for that file (creating struct block's for
them), then make the struct symtab for that file and put it in the
list of all such. */
static void
coff_end_compunit_symtab (struct objfile *objfile)
{
set_last_source_start_addr (current_source_start_addr);
end_compunit_symtab (current_source_end_addr);
/* Reinitialize for beginning of new file. */
set_last_source_file (NULL);
}
/* The linker sometimes generates some non-function symbols inside
functions referencing variables imported from another DLL.
Return nonzero if the given symbol corresponds to one of them. */
static int
is_import_fixup_symbol (struct coff_symbol *cs,
enum minimal_symbol_type type)
{
/* The following is a bit of a heuristic using the characteristics
of these fixup symbols, but should work well in practice... */
int i;
/* Must be a non-static text symbol. */
if (type != mst_text)
return 0;
/* Must be a non-function symbol. */
if (ISFCN (cs->c_type))
return 0;
/* The name must start with "__fu<digits>__". */
if (!startswith (cs->c_name, "__fu"))
return 0;
if (! isdigit (cs->c_name[4]))
return 0;
for (i = 5; cs->c_name[i] != '\0' && isdigit (cs->c_name[i]); i++)
/* Nothing, just incrementing index past all digits. */;
if (cs->c_name[i] != '_' || cs->c_name[i + 1] != '_')
return 0;
return 1;
}
static struct minimal_symbol *
record_minimal_symbol (minimal_symbol_reader &reader,
struct coff_symbol *cs, unrelocated_addr address,
enum minimal_symbol_type type, int section,
struct objfile *objfile)
{
/* We don't want TDESC entry points in the minimal symbol table. */
if (cs->c_name[0] == '@')
return NULL;
if (is_import_fixup_symbol (cs, type))
{
/* Because the value of these symbols is within a function code
range, these symbols interfere with the symbol-from-address
reverse lookup; this manifests itself in backtraces, or any
other commands that prints symbolic addresses. Just pretend
these symbols do not exist. */
return NULL;
}
return reader.record_full (cs->c_name, true, address, type, section);
}
/* coff_symfile_init ()
is the coff-specific initialization routine for reading symbols.
It is passed a struct objfile which contains, among other things,
the BFD for the file whose symbols are being read, and a slot for
a pointer to "private data" which we fill with cookies and other
treats for coff_symfile_read ().
We will only be called if this is a COFF or COFF-like file. BFD
handles figuring out the format of the file, and code in symtab.c
uses BFD's determination to vector to us.
The ultimate result is a new symtab (or, FIXME, eventually a
psymtab). */
static void
coff_symfile_init (struct objfile *objfile)
{
/* Allocate struct to keep track of the symfile. */
coff_objfile_data_key.emplace (objfile);
}
/* This function is called for every section; it finds the outer
limits of the line table (minimum and maximum file offset) so that
the mainline code can read the whole thing for efficiency. */
static void
find_linenos (bfd *abfd, struct bfd_section *asect, void *vpinfo)
{
struct coff_symfile_info *info;
int size, count;
file_ptr offset, maxoff;
/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
count = asect->lineno_count;
/* End of warning. */
if (count == 0)
return;
size = count * local_linesz;
info = (struct coff_symfile_info *) vpinfo;
/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
offset = asect->line_filepos;
/* End of warning. */
if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
info->min_lineno_offset = offset;
maxoff = offset + size;
if (maxoff > info->max_lineno_offset)
info->max_lineno_offset = maxoff;
}
/* A helper function for coff_symfile_read that reads minimal
symbols. It may also read other forms of symbol as well. */
static void
coff_read_minsyms (file_ptr symtab_offset, unsigned int nsyms,
struct objfile *objfile)
{
/* If minimal symbols were already read, and if we know we aren't
going to read any other kind of symbol here, then we can just
return. */
if (objfile->per_bfd->minsyms_read && pe_file && nsyms == 0)
return;
minimal_symbol_reader reader (objfile);
if (pe_file && nsyms == 0)
{
/* We've got no debugging symbols, but it's a portable
executable, so try to read the export table. */
read_pe_exported_syms (reader, objfile);
}
else
{
/* Now that the executable file is positioned at symbol table,
process it and define symbols accordingly. */
coff_symtab_read (reader, symtab_offset, nsyms, objfile);
}
/* Install any minimal symbols that have been collected as the
current minimal symbols for this objfile. */
reader.install ();
if (pe_file)
{
for (minimal_symbol *msym : objfile->msymbols ())
{
const char *name = msym->linkage_name ();
/* If the minimal symbols whose name are prefixed by "__imp_"
or "_imp_", get rid of the prefix, and search the minimal
symbol in OBJFILE. Note that 'maintenance print msymbols'
shows that type of these "_imp_XXXX" symbols is mst_data. */
if (msym->type () == mst_data)
{
const char *name1 = NULL;
if (startswith (name, "_imp_"))
name1 = name + 5;
else if (startswith (name, "__imp_"))
name1 = name + 6;
if (name1 != NULL)
{
int lead
= bfd_get_symbol_leading_char (objfile->obfd.get ());
struct bound_minimal_symbol found;
if (lead != '\0' && *name1 == lead)
name1 += 1;
found = lookup_minimal_symbol (name1, NULL, objfile);
/* If found, there are symbols named "_imp_foo" and "foo"
respectively in OBJFILE. Set the type of symbol "foo"
as 'mst_solib_trampoline'. */
if (found.minsym != NULL
&& found.minsym->type () == mst_text)
found.minsym->set_type (mst_solib_trampoline);
}
}
}
}
}
/* The BFD for this file -- only good while we're actively reading
symbols into a psymtab or a symtab. */
static bfd *symfile_bfd;
/* Read a symbol file, after initialization by coff_symfile_init. */
static void
coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
{
struct coff_symfile_info *info;
bfd *abfd = objfile->obfd.get ();
coff_data_type *cdata = coff_data (abfd);
const char *filename = bfd_get_filename (abfd);
int val;
unsigned int num_symbols;
file_ptr symtab_offset;
file_ptr stringtab_offset;
unsigned int stabstrsize;
info = coff_objfile_data_key.get (objfile);
symfile_bfd = abfd; /* Kludge for swap routines. */
std::vector<asection *> stabsects;
scoped_restore restore_stabsects
= make_scoped_restore (&info->stabsects, &stabsects);
/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
num_symbols = bfd_get_symcount (abfd); /* How many syms */
symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
stringtab_offset = symtab_offset + /* String table file offset */
num_symbols * cdata->local_symesz;
/* Set a few file-statics that give us specific information about
the particular COFF file format we're reading. */
local_n_btmask = cdata->local_n_btmask;
local_n_btshft = cdata->local_n_btshft;
local_n_tmask = cdata->local_n_tmask;
local_n_tshift = cdata->local_n_tshift;
local_linesz = cdata->local_linesz;
local_symesz = cdata->local_symesz;
local_auxesz = cdata->local_auxesz;
/* Allocate space for raw symbol and aux entries, based on their
space requirements as reported by BFD. */
gdb::def_vector<char> temp_storage (cdata->local_symesz
+ cdata->local_auxesz);
temp_sym = temp_storage.data ();
temp_aux = temp_sym + cdata->local_symesz;
/* We need to know whether this is a PE file, because in PE files,
unlike standard COFF files, symbol values are stored as offsets
from the section address, rather than as absolute addresses.
FIXME: We should use BFD to read the symbol table, and thus avoid
this problem. */
pe_file =
startswith (bfd_get_target (objfile->obfd.get ()), "pe")
|| startswith (bfd_get_target (objfile->obfd.get ()), "epoc-pe");
/* End of warning. */
info->min_lineno_offset = 0;
info->max_lineno_offset = 0;
/* Only read line number information if we have symbols.
On Windows NT, some of the system's DLL's have sections with
PointerToLinenumbers fields that are non-zero, but point at
random places within the image file. (In the case I found,
KERNEL32.DLL's .text section has a line number info pointer that
points into the middle of the string `lib\\i386\kernel32.dll'.)
However, these DLL's also have no symbols. The line number
tables are meaningless without symbols. And in fact, GDB never
uses the line number information unless there are symbols. So we
can avoid spurious error messages (and maybe run a little
faster!) by not even reading the line number table unless we have
symbols. */
scoped_restore restore_linetab = make_scoped_restore (&linetab);
gdb::unique_xmalloc_ptr<char> linetab_storage;
if (num_symbols > 0)
{
/* Read the line number table, all at once. */
bfd_map_over_sections (abfd, find_linenos, (void *) info);
val = init_lineno (abfd, info->min_lineno_offset,
info->max_lineno_offset - info->min_lineno_offset,
&linetab_storage);
if (val < 0)
error (_("\"%s\": error reading line numbers."), filename);
}
/* Now read the string table, all at once. */
scoped_restore restore_stringtab = make_scoped_restore (&stringtab);
gdb::unique_xmalloc_ptr<char> stringtab_storage;
val = init_stringtab (abfd, stringtab_offset, &stringtab_storage);
if (val < 0)
error (_("\"%s\": can't get string table"), filename);
coff_read_minsyms (symtab_offset, num_symbols, objfile);
if (!(objfile->flags & OBJF_READNEVER))
bfd_map_over_sections (abfd, coff_locate_sections, (void *) info);
if (!info->stabsects->empty())
{
if (!info->stabstrsect)
{
error (_("The debugging information in `%s' is corrupted.\nThe "
"file has a `.stabs' section, but no `.stabstr' section."),
filename);
}
/* FIXME: dubious. Why can't we use something normal like
bfd_get_section_contents? */
stabstrsize = bfd_section_size (info->stabstrsect);
coffstab_build_psymtabs (objfile,
info->textaddr, info->textsize,
*info->stabsects,
info->stabstrsect->filepos, stabstrsize);
}
if (dwarf2_initialize_objfile (objfile))
{
/* Nothing. */
}
/* Try to add separate debug file if no symbols table found. */
else if (!objfile->has_partial_symbols ()
&& objfile->separate_debug_objfile == NULL
&& objfile->separate_debug_objfile_backlink == NULL)
{
if (objfile->find_and_add_separate_symbol_file (symfile_flags))
gdb_assert (objfile->separate_debug_objfile != nullptr);
}
}
static void
coff_new_init (struct objfile *ignore)
{
}
/* Perform any local cleanups required when we are done with a
particular objfile. I.E, we are in the process of discarding all
symbol information for an objfile, freeing up all memory held for
it, and unlinking the objfile struct from the global list of known
objfiles. */
static void
coff_symfile_finish (struct objfile *objfile)
{
/* Let stabs reader clean up. */
stabsread_clear_cache ();
}
/* Given pointers to a symbol table in coff style exec file,
analyze them and create struct symtab's describing the symbols.
NSYMS is the number of symbols in the symbol table.
We read them one at a time using read_one_sym (). */
static void
coff_symtab_read (minimal_symbol_reader &reader,
file_ptr symtab_offset, unsigned int nsyms,
struct objfile *objfile)
{
struct gdbarch *gdbarch = objfile->arch ();
struct context_stack *newobj = nullptr;
struct coff_symbol coff_symbol;
struct coff_symbol *cs = &coff_symbol;
static struct internal_syment main_sym;
static union internal_auxent main_aux;
struct coff_symbol fcn_cs_saved;
static struct internal_syment fcn_sym_saved;
static union internal_auxent fcn_aux_saved;
/* A .file is open. */
int in_source_file = 0;
int next_file_symnum = -1;
/* Name of the current file. */
const char *filestring = "";
int depth = 0;
int fcn_first_line = 0;
CORE_ADDR fcn_first_line_addr = 0;
int fcn_last_line = 0;
int fcn_start_addr = 0;
long fcn_line_ptr = 0;
int val;
CORE_ADDR tmpaddr;
struct minimal_symbol *msym;
scoped_free_pendings free_pending;
/* Position to read the symbol table. */
val = bfd_seek (objfile->obfd.get (), symtab_offset, 0);
if (val < 0)
perror_with_name (objfile_name (objfile));
coffread_objfile = objfile;
nlist_bfd_global = objfile->obfd.get ();
nlist_nsyms_global = nsyms;
set_last_source_file (NULL);
memset (opaque_type_chain, 0, sizeof opaque_type_chain);
if (type_vector) /* Get rid of previous one. */
xfree (type_vector);
type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
type_vector = XCNEWVEC (struct type *, type_vector_length);
coff_start_compunit_symtab (objfile, "");
symnum = 0;
while (symnum < nsyms)
{
QUIT; /* Make this command interruptable. */
read_one_sym (cs, &main_sym, &main_aux);
if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
{
if (get_last_source_file ())
coff_end_compunit_symtab (objfile);
coff_start_compunit_symtab (objfile, "_globals_");
/* coff_start_compunit_symtab will set the language of this symtab to
language_unknown, since such a ``file name'' is not
recognized. Override that with the minimal language to
allow printing values in this symtab. */
get_current_subfile ()->language = language_minimal;
complete_symtab ("_globals_", 0, 0);
/* Done with all files, everything from here on out is
globals. */
}
/* Special case for file with type declarations only, no
text. */
if (!get_last_source_file () && SDB_TYPE (cs->c_type)
&& cs->c_secnum == N_DEBUG)
complete_symtab (filestring, 0, 0);
/* Typedefs should not be treated as symbol definitions. */
if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
{
/* Record all functions -- external and static -- in
minsyms. */
int section = cs_to_section (cs, objfile);
tmpaddr = cs->c_value;
/* Don't record unresolved symbols. */
if (!(cs->c_secnum <= 0 && cs->c_value == 0))
record_minimal_symbol (reader, cs,
unrelocated_addr (tmpaddr),
mst_text, section, objfile);
fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
fcn_start_addr = tmpaddr;
fcn_cs_saved = *cs;
fcn_sym_saved = main_sym;
fcn_aux_saved = main_aux;
continue;
}
switch (cs->c_sclass)
{
case C_EFCN:
case C_EXTDEF:
case C_ULABEL:
case C_USTATIC:
case C_LINE:
case C_ALIAS:
case C_HIDDEN:
complaint (_("Bad n_sclass for symbol %s"),
cs->c_name);
break;
case C_FILE:
/* c_value field contains symnum of next .file entry in
table or symnum of first global after last .file. */
next_file_symnum = cs->c_value;
if (cs->c_naux > 0)
filestring = coff_getfilename (&main_aux);
else
filestring = "";
/* Complete symbol table for last object file
containing debugging information. */
if (get_last_source_file ())
{
coff_end_compunit_symtab (objfile);
coff_start_compunit_symtab (objfile, filestring);
}
in_source_file = 1;
break;
/* C_LABEL is used for labels and static functions.
Including it here allows gdb to see static functions when
no debug info is available. */
case C_LABEL:
/* However, labels within a function can make weird
backtraces, so filter them out (from phdm@macqel.be). */
if (within_function)
break;
[[fallthrough]];
case C_STAT:
case C_THUMBLABEL:
case C_THUMBSTAT:
case C_THUMBSTATFUNC:
if (cs->c_name[0] == '.')
{
if (strcmp (cs->c_name, ".text") == 0)
{
/* FIXME: don't wire in ".text" as section name or
symbol name! */
/* Check for in_source_file deals with case of a
file with debugging symbols followed by a later
file with no symbols. */
if (in_source_file)
complete_symtab (filestring,
(cs->c_value
+ objfile->text_section_offset ()),
main_aux.x_scn.x_scnlen);
in_source_file = 0;
}
/* Flush rest of '.' symbols. */
break;
}
else if (!SDB_TYPE (cs->c_type)
&& cs->c_name[0] == 'L'
&& (startswith (cs->c_name, "LI%")
|| startswith (cs->c_name, "LF%")
|| startswith (cs->c_name, "LC%")
|| startswith (cs->c_name, "LP%")
|| startswith (cs->c_name, "LPB%")
|| startswith (cs->c_name, "LBB%")
|| startswith (cs->c_name, "LBE%")
|| startswith (cs->c_name, "LPBX%")))
/* At least on a 3b1, gcc generates swbeg and string labels
that look like this. Ignore them. */
break;
/* For static symbols that don't start with '.'... */
[[fallthrough]];
case C_THUMBEXT:
case C_THUMBEXTFUNC:
case C_EXT:
{
/* Record it in the minimal symbols regardless of
SDB_TYPE. This parallels what we do for other debug
formats, and probably is needed to make
print_address_symbolic work right without the (now
gone) "set fast-symbolic-addr off" kludge. */
enum minimal_symbol_type ms_type;
int sec;
CORE_ADDR offset = 0;
if (cs->c_secnum == N_UNDEF)
{
/* This is a common symbol. We used to rely on
the target to tell us whether it knows where
the symbol has been relocated to, but none of
the target implementations actually provided
that operation. So we just ignore the symbol,
the same way we would do if we had a target-side
symbol lookup which returned no match. */
break;
}
else if (cs->c_secnum == N_ABS)
{
/* Use the correct minimal symbol type (and don't
relocate) for absolute values. */
ms_type = mst_abs;
sec = cs_to_section (cs, objfile);
tmpaddr = cs->c_value;
}
else
{
asection *bfd_section = cs_to_bfd_section (cs, objfile);
sec = cs_to_section (cs, objfile);
tmpaddr = cs->c_value;
/* Statics in a PE file also get relocated. */
if (cs->c_sclass == C_EXT
|| cs->c_sclass == C_THUMBEXTFUNC
|| cs->c_sclass == C_THUMBEXT
|| (pe_file && (cs->c_sclass == C_STAT)))
offset = objfile->section_offsets[sec];
if (bfd_section->flags & SEC_CODE)
{
ms_type =
cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
|| cs->c_sclass == C_THUMBEXT ?
mst_text : mst_file_text;
tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
}
else if (bfd_section->flags & SEC_ALLOC
&& bfd_section->flags & SEC_LOAD)
{
ms_type =
cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
? mst_data : mst_file_data;
}
else if (bfd_section->flags & SEC_ALLOC)
{