1 /* Read dbx symbol tables and convert to internal format, for GDB.
2 Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* This module provides three functions: dbx_symfile_init,
20 which initializes to read a symbol file; dbx_new_init, which
21 discards existing cached information when all symbols are being
22 discarded; and dbx_symfile_read, which reads a symbol table
25 dbx_symfile_read only does the minimum work necessary for letting the
26 user "name" things symbolically; it does not read the entire symtab.
27 Instead, it reads the external and static symbols and puts them in partial
28 symbol tables. When more extensive information is requested of a
29 file, the corresponding partial symbol table is mutated into a full
30 fledged symbol table by going back and reading the symbols
31 for real. dbx_psymtab_to_symtab() is the function that does this */
34 #include "event-top.h"
35 #include "gdbsupport/gdb_obstack.h"
38 #include "breakpoint.h"
42 #include "filenames.h"
44 #include "buildsym-legacy.h"
45 #include "stabsread.h"
46 #include "gdb-stabs.h"
48 #include "complaints.h"
50 #include "cp-support.h"
54 #include "aout/aout64.h"
55 #include "aout/stab_gnu.h"
57 /* Required for the following registry. */
58 #include "gdb-stabs.h"
64 /* Local function prototypes. */
66 static void dbx_symfile_init (struct objfile
*);
68 static void dbx_new_init (struct objfile
*);
70 static void dbx_symfile_read (struct objfile
*, symfile_add_flags
);
72 static void dbx_symfile_finish (struct objfile
*);
77 explicit_lookup_type (int real_filenum
, int index
)
79 struct header_file
*f
= &HEADER_FILES (dbxread_objfile
)[real_filenum
];
81 if (index
>= f
->length
)
84 f
->vector
= (struct type
**)
85 xrealloc (f
->vector
, f
->length
* sizeof (struct type
*));
86 memset (&f
->vector
[f
->length
/ 2],
87 '\0', f
->length
* sizeof (struct type
*) / 2);
89 return &f
->vector
[index
];
93 /* Scan and build partial symbols for a symbol file.
94 We have been initialized by a call to dbx_symfile_init, which
95 put all the relevant info into a "struct dbx_symfile_info",
96 hung off the objfile structure. */
99 dbx_symfile_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
101 read_stabs_symtab (objfile
, symfile_flags
);
104 /* Initialize anything that needs initializing when a completely new
105 symbol file is specified (not just adding some symbols from another
106 file, e.g. a shared library). */
109 dbx_new_init (struct objfile
*ignore
)
111 stabsread_new_init ();
112 init_header_files ();
116 /* dbx_symfile_init ()
117 is the dbx-specific initialization routine for reading symbols.
118 It is passed a struct objfile which contains, among other things,
119 the BFD for the file whose symbols are being read, and a slot for a pointer
120 to "private data" which we fill with goodies.
122 We read the string table into malloc'd space and stash a pointer to it.
124 Since BFD doesn't know how to read debug symbols in a format-independent
125 way (and may never do so...), we have to do it ourselves. We will never
126 be called unless this is an a.out (or very similar) file.
127 FIXME, there should be a cleaner peephole into the BFD environment here. */
129 #define DBX_STRINGTAB_SIZE_SIZE sizeof(long) /* FIXME */
132 dbx_symfile_init (struct objfile
*objfile
)
135 bfd
*sym_bfd
= objfile
->obfd
.get ();
136 const char *name
= bfd_get_filename (sym_bfd
);
138 unsigned char size_temp
[DBX_STRINGTAB_SIZE_SIZE
];
140 /* Allocate struct to keep track of the symfile. */
141 dbx_objfile_data_key
.emplace (objfile
);
143 DBX_TEXT_SECTION (objfile
) = bfd_get_section_by_name (sym_bfd
, ".text");
144 DBX_DATA_SECTION (objfile
) = bfd_get_section_by_name (sym_bfd
, ".data");
145 DBX_BSS_SECTION (objfile
) = bfd_get_section_by_name (sym_bfd
, ".bss");
147 /* FIXME POKING INSIDE BFD DATA STRUCTURES. */
148 #define STRING_TABLE_OFFSET (sym_bfd->origin + obj_str_filepos (sym_bfd))
149 #define SYMBOL_TABLE_OFFSET (sym_bfd->origin + obj_sym_filepos (sym_bfd))
151 /* FIXME POKING INSIDE BFD DATA STRUCTURES. */
153 text_sect
= bfd_get_section_by_name (sym_bfd
, ".text");
155 error (_("Can't find .text section in symbol file"));
156 DBX_TEXT_ADDR (objfile
) = bfd_section_vma (text_sect
);
157 DBX_TEXT_SIZE (objfile
) = bfd_section_size (text_sect
);
159 DBX_SYMBOL_SIZE (objfile
) = obj_symbol_entry_size (sym_bfd
);
160 DBX_SYMCOUNT (objfile
) = bfd_get_symcount (sym_bfd
);
161 DBX_SYMTAB_OFFSET (objfile
) = SYMBOL_TABLE_OFFSET
;
163 /* Read the string table and stash it away in the objfile_obstack.
164 When we blow away the objfile the string table goes away as well.
165 Note that gdb used to use the results of attempting to malloc the
166 string table, based on the size it read, as a form of sanity check
167 for botched byte swapping, on the theory that a byte swapped string
168 table size would be so totally bogus that the malloc would fail. Now
169 that we put in on the objfile_obstack, we can't do this since gdb gets
170 a fatal error (out of virtual memory) if the size is bogus. We can
171 however at least check to see if the size is less than the size of
172 the size field itself, or larger than the size of the entire file.
173 Note that all valid string tables have a size greater than zero, since
174 the bytes used to hold the size are included in the count. */
176 if (STRING_TABLE_OFFSET
== 0)
178 /* It appears that with the existing bfd code, STRING_TABLE_OFFSET
179 will never be zero, even when there is no string table. This
180 would appear to be a bug in bfd. */
181 DBX_STRINGTAB_SIZE (objfile
) = 0;
182 DBX_STRINGTAB (objfile
) = NULL
;
186 val
= bfd_seek (sym_bfd
, STRING_TABLE_OFFSET
, SEEK_SET
);
188 perror_with_name (name
);
190 memset (size_temp
, 0, sizeof (size_temp
));
191 val
= bfd_read (size_temp
, sizeof (size_temp
), sym_bfd
);
194 perror_with_name (name
);
198 /* With the existing bfd code, STRING_TABLE_OFFSET will be set to
199 EOF if there is no string table, and attempting to read the size
200 from EOF will read zero bytes. */
201 DBX_STRINGTAB_SIZE (objfile
) = 0;
202 DBX_STRINGTAB (objfile
) = NULL
;
206 /* Read some data that would appear to be the string table size.
207 If there really is a string table, then it is probably the right
208 size. Byteswap if necessary and validate the size. Note that
209 the minimum is DBX_STRINGTAB_SIZE_SIZE. If we just read some
210 random data that happened to be at STRING_TABLE_OFFSET, because
211 bfd can't tell us there is no string table, the sanity checks may
212 or may not catch this. */
213 DBX_STRINGTAB_SIZE (objfile
) = bfd_h_get_32 (sym_bfd
, size_temp
);
215 if (DBX_STRINGTAB_SIZE (objfile
) < sizeof (size_temp
)
216 || DBX_STRINGTAB_SIZE (objfile
) > bfd_get_size (sym_bfd
))
217 error (_("ridiculous string table size (%d bytes)."),
218 DBX_STRINGTAB_SIZE (objfile
));
220 DBX_STRINGTAB (objfile
) =
221 (char *) obstack_alloc (&objfile
->objfile_obstack
,
222 DBX_STRINGTAB_SIZE (objfile
));
223 OBJSTAT (objfile
, sz_strtab
+= DBX_STRINGTAB_SIZE (objfile
));
225 /* Now read in the string table in one big gulp. */
227 val
= bfd_seek (sym_bfd
, STRING_TABLE_OFFSET
, SEEK_SET
);
229 perror_with_name (name
);
230 val
= bfd_read (DBX_STRINGTAB (objfile
),
231 DBX_STRINGTAB_SIZE (objfile
),
233 if (val
!= DBX_STRINGTAB_SIZE (objfile
))
234 perror_with_name (name
);
239 /* Perform any local cleanups required when we are done with a particular
240 objfile. I.E, we are in the process of discarding all symbol information
241 for an objfile, freeing up all memory held for it, and unlinking the
242 objfile struct from the global list of known objfiles. */
245 dbx_symfile_finish (struct objfile
*objfile
)
247 free_header_files ();
255 /* Scan and build partial symbols for a file with special sections for stabs
256 and stabstrings. The file has already been processed to get its minimal
257 symbols, and any other symbols that might be necessary to resolve GSYMs.
259 This routine is the equivalent of dbx_symfile_init and dbx_symfile_read
262 OBJFILE is the object file we are reading symbols from.
263 ADDR is the address relative to which the symbols are (e.g. the base address
264 of the text segment).
265 STAB_NAME is the name of the section that contains the stabs.
266 STABSTR_NAME is the name of the section that contains the stab strings.
268 This routine is mostly copied from dbx_symfile_init and
272 stabsect_build_psymtabs (struct objfile
*objfile
, char *stab_name
,
273 char *stabstr_name
, char *text_name
)
276 bfd
*sym_bfd
= objfile
->obfd
.get ();
277 const char *name
= bfd_get_filename (sym_bfd
);
279 asection
*stabstrsect
;
282 stabsect
= bfd_get_section_by_name (sym_bfd
, stab_name
);
283 stabstrsect
= bfd_get_section_by_name (sym_bfd
, stabstr_name
);
289 error (_("stabsect_build_psymtabs: Found stabs (%s), "
290 "but not string section (%s)"),
291 stab_name
, stabstr_name
);
293 dbx_objfile_data_key
.emplace (objfile
);
295 text_sect
= bfd_get_section_by_name (sym_bfd
, text_name
);
297 error (_("Can't find %s section in symbol file"), text_name
);
298 DBX_TEXT_ADDR (objfile
) = bfd_section_vma (text_sect
);
299 DBX_TEXT_SIZE (objfile
) = bfd_section_size (text_sect
);
301 DBX_SYMBOL_SIZE (objfile
) = sizeof (struct external_nlist
);
302 DBX_SYMCOUNT (objfile
) = bfd_section_size (stabsect
)
303 / DBX_SYMBOL_SIZE (objfile
);
304 DBX_STRINGTAB_SIZE (objfile
) = bfd_section_size (stabstrsect
);
305 DBX_SYMTAB_OFFSET (objfile
) = stabsect
->filepos
; /* XXX - FIXME: POKING
309 if (DBX_STRINGTAB_SIZE (objfile
) > bfd_get_size (sym_bfd
))
310 error (_("ridiculous string table size: %d bytes"),
311 DBX_STRINGTAB_SIZE (objfile
));
312 DBX_STRINGTAB (objfile
) = (char *)
313 obstack_alloc (&objfile
->objfile_obstack
,
314 DBX_STRINGTAB_SIZE (objfile
) + 1);
315 OBJSTAT (objfile
, sz_strtab
+= DBX_STRINGTAB_SIZE (objfile
) + 1);
317 /* Now read in the string table in one big gulp. */
319 val
= bfd_get_section_contents (sym_bfd
, /* bfd */
320 stabstrsect
, /* bfd section */
321 DBX_STRINGTAB (objfile
), /* input buffer */
322 0, /* offset into section */
323 DBX_STRINGTAB_SIZE (objfile
)); /* amount to
327 perror_with_name (name
);
329 stabsread_new_init ();
330 free_header_files ();
331 init_header_files ();
333 /* Now, do an incremental load. */
335 dbx_objfile_data_key
.get (objfile
)->ctx
.processing_acc_compilation
= 1;
336 dbx_symfile_read (objfile
, 0);
339 static const struct sym_fns aout_sym_fns
=
341 dbx_new_init
, /* init anything gbl to entire symtab */
342 dbx_symfile_init
, /* read initial info, setup for sym_read() */
343 dbx_symfile_read
, /* read a symbol file into symtab */
344 dbx_symfile_finish
, /* finished with file, cleanup */
345 default_symfile_offsets
, /* parse user's offsets to internal form */
346 default_symfile_segments
, /* Get segment information from a file. */
348 default_symfile_relocate
, /* Relocate a debug section. */
349 NULL
, /* sym_probe_fns */
352 void _initialize_dbxread ();
354 _initialize_dbxread ()
356 add_symtab_fns (bfd_target_aout_flavour
, &aout_sym_fns
);