gdbint_5.html   [plain text]


<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from /mnt/apple/gdb/source/gdb.apple/source/gdb/gdb/doc/gdbint.texinfo on 23 November 1999 -->

<TITLE>GDB Internals - Symbol Handling</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="gdbint_1.html">first</A>, <A HREF="gdbint_4.html">previous</A>, <A HREF="gdbint_6.html">next</A>, <A HREF="gdbint_16.html">last</A> section, <A HREF="gdbint_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC19" HREF="gdbint_toc.html#TOC19">Symbol Handling</A></H1>

<P>
Symbols are a key part of GDB's operation.  Symbols include variables,
functions, and types.

</P>


<H2><A NAME="SEC20" HREF="gdbint_toc.html#TOC20">Symbol Reading</A></H2>

<P>
GDB reads symbols from "symbol files".  The usual symbol file is the
file containing the program which GDB is debugging.  GDB can be directed
to use a different file for symbols (with the <CODE>symbol-file</CODE>
command), and it can also read more symbols via the "add-file" and
"load" commands, or while reading symbols from shared libraries.

</P>
<P>
Symbol files are initially opened by code in <TT>`symfile.c'</TT> using the
BFD library.  BFD identifies the type of the file by examining its
header.  <CODE>symfile_init</CODE> then uses this identification to locate a
set of symbol-reading functions.

</P>
<P>
Symbol reading modules identify themselves to GDB by calling
<CODE>add_symtab_fns</CODE> during their module initialization.  The argument
to <CODE>add_symtab_fns</CODE> is a <CODE>struct sym_fns</CODE> which contains the
name (or name prefix) of the symbol format, the length of the prefix,
and pointers to four functions.  These functions are called at various
times to process symbol-files whose identification matches the specified
prefix.

</P>
<P>
The functions supplied by each module are:

</P>
<DL COMPACT>

<DT><CODE><VAR>xyz</VAR>_symfile_init(struct sym_fns *sf)</CODE>
<DD>
Called from <CODE>symbol_file_add</CODE> when we are about to read a new
symbol file.  This function should clean up any internal state (possibly
resulting from half-read previous files, for example) and prepare to
read a new symbol file. Note that the symbol file which we are reading
might be a new "main" symbol file, or might be a secondary symbol file
whose symbols are being added to the existing symbol table.

The argument to <CODE><VAR>xyz</VAR>_symfile_init</CODE> is a newly allocated
<CODE>struct sym_fns</CODE> whose <CODE>bfd</CODE> field contains the BFD for the
new symbol file being read.  Its <CODE>private</CODE> field has been zeroed,
and can be modified as desired.  Typically, a struct of private
information will be <CODE>malloc</CODE>'d, and a pointer to it will be placed
in the <CODE>private</CODE> field.

There is no result from <CODE><VAR>xyz</VAR>_symfile_init</CODE>, but it can call
<CODE>error</CODE> if it detects an unavoidable problem.

<DT><CODE><VAR>xyz</VAR>_new_init()</CODE>
<DD>
Called from <CODE>symbol_file_add</CODE> when discarding existing symbols.
This function need only handle the symbol-reading module's internal
state; the symbol table data structures visible to the rest of GDB will
be discarded by <CODE>symbol_file_add</CODE>.  It has no arguments and no
result.  It may be called after <CODE><VAR>xyz</VAR>_symfile_init</CODE>, if a new
symbol table is being read, or may be called alone if all symbols are
simply being discarded.

<DT><CODE><VAR>xyz</VAR>_symfile_read(struct sym_fns *sf, CORE_ADDR addr, int mainline)</CODE>
<DD>
Called from <CODE>symbol_file_add</CODE> to actually read the symbols from a
symbol-file into a set of psymtabs or symtabs.

<CODE>sf</CODE> points to the struct sym_fns originally passed to
<CODE><VAR>xyz</VAR>_sym_init</CODE> for possible initialization.  <CODE>addr</CODE> is
the offset between the file's specified start address and its true
address in memory.  <CODE>mainline</CODE> is 1 if this is the main symbol
table being read, and 0 if a secondary symbol file (e.g. shared library
or dynamically loaded file) is being read.</DL>

<P>
In addition, if a symbol-reading module creates psymtabs when
<VAR>xyz</VAR>_symfile_read is called, these psymtabs will contain a pointer
to a function <CODE><VAR>xyz</VAR>_psymtab_to_symtab</CODE>, which can be called
from any point in the GDB symbol-handling code.

</P>
<DL COMPACT>

<DT><CODE><VAR>xyz</VAR>_psymtab_to_symtab (struct partial_symtab *pst)</CODE>
<DD>
Called from <CODE>psymtab_to_symtab</CODE> (or the PSYMTAB_TO_SYMTAB macro) if
the psymtab has not already been read in and had its <CODE>pst-&#62;symtab</CODE>
pointer set.  The argument is the psymtab to be fleshed-out into a
symtab.  Upon return, pst-&#62;readin should have been set to 1, and
pst-&#62;symtab should contain a pointer to the new corresponding symtab, or
zero if there were no symbols in that part of the symbol file.
</DL>



<H2><A NAME="SEC21" HREF="gdbint_toc.html#TOC21">Partial Symbol Tables</A></H2>

<P>
GDB has three types of symbol tables.

</P>

<UL>

<LI>full symbol tables (symtabs).  These contain the main information

about symbols and addresses.

<LI>partial symbol tables (psymtabs).  These contain enough

information to know when to read the corresponding part of the full
symbol table.

<LI>minimal symbol tables (msymtabs).  These contain information

gleaned from non-debugging symbols.

</UL>

<P>
This section describes partial symbol tables.

</P>
<P>
A psymtab is constructed by doing a very quick pass over an executable
file's debugging information.  Small amounts of information are
extracted -- enough to identify which parts of the symbol table will
need to be re-read and fully digested later, when the user needs the
information.  The speed of this pass causes GDB to start up very
quickly.  Later, as the detailed rereading occurs, it occurs in small
pieces, at various times, and the delay therefrom is mostly invisible to
the user.

</P>
<P>
The symbols that show up in a file's psymtab should be, roughly, those
visible to the debugger's user when the program is not running code from
that file.  These include external symbols and types, static symbols and
types, and enum values declared at file scope.

</P>
<P>
The psymtab also contains the range of instruction addresses that the
full symbol table would represent.

</P>
<P>
The idea is that there are only two ways for the user (or much of the
code in the debugger) to reference a symbol:

</P>

<UL>

<LI>by its address

(e.g. execution stops at some address which is inside a function in this
file).  The address will be noticed to be in the range of this psymtab,
and the full symtab will be read in.  <CODE>find_pc_function</CODE>,
<CODE>find_pc_line</CODE>, and other <CODE>find_pc_...</CODE> functions handle
this.

<LI>by its name

(e.g. the user asks to print a variable, or set a breakpoint on a
function).  Global names and file-scope names will be found in the
psymtab, which will cause the symtab to be pulled in.  Local names will
have to be qualified by a global name, or a file-scope name, in which
case we will have already read in the symtab as we evaluated the
qualifier.  Or, a local symbol can be referenced when we are "in" a
local scope, in which case the first case applies.  <CODE>lookup_symbol</CODE>
does most of the work here.

</UL>

<P>
The only reason that psymtabs exist is to cause a symtab to be read in
at the right moment.  Any symbol that can be elided from a psymtab,
while still causing that to happen, should not appear in it.  Since
psymtabs don't have the idea of scope, you can't put local symbols in
them anyway.  Psymtabs don't have the idea of the type of a symbol,
either, so types need not appear, unless they will be referenced by
name.

</P>
<P>
It is a bug for GDB to behave one way when only a psymtab has been read,
and another way if the corresponding symtab has been read in.  Such bugs
are typically caused by a psymtab that does not contain all the visible
symbols, or which has the wrong instruction address ranges.

</P>
<P>
The psymtab for a particular section of a symbol-file (objfile) could be
thrown away after the symtab has been read in.  The symtab should always
be searched before the psymtab, so the psymtab will never be used (in a
bug-free environment).  Currently, psymtabs are allocated on an obstack,
and all the psymbols themselves are allocated in a pair of large arrays
on an obstack, so there is little to be gained by trying to free them
unless you want to do a lot more work.

</P>


<H2><A NAME="SEC22" HREF="gdbint_toc.html#TOC22">Types</A></H2>

<P>
Fundamental Types (e.g., FT_VOID, FT_BOOLEAN).

</P>
<P>
These are the fundamental types that GDB uses internally.  Fundamental
types from the various debugging formats (stabs, ELF, etc) are mapped
into one of these.  They are basically a union of all fundamental types
that gdb knows about for all the languages that GDB knows about.

</P>
<P>
Type Codes (e.g., TYPE_CODE_PTR, TYPE_CODE_ARRAY).

</P>
<P>
Each time GDB builds an internal type, it marks it with one of these
types.  The type may be a fundamental type, such as TYPE_CODE_INT, or a
derived type, such as TYPE_CODE_PTR which is a pointer to another type.
Typically, several FT_* types map to one TYPE_CODE_* type, and are
distinguished by other members of the type struct, such as whether the
type is signed or unsigned, and how many bits it uses.

</P>
<P>
Builtin Types (e.g., builtin_type_void, builtin_type_char).

</P>
<P>
These are instances of type structs that roughly correspond to
fundamental types and are created as global types for GDB to use for
various ugly historical reasons.  We eventually want to eliminate these.
Note for example that builtin_type_int initialized in gdbtypes.c is
basically the same as a TYPE_CODE_INT type that is initialized in
c-lang.c for an FT_INTEGER fundamental type.  The difference is that the
builtin_type is not associated with any particular objfile, and only one
instance exists, while c-lang.c builds as many TYPE_CODE_INT types as
needed, with each one associated with some particular objfile.

</P>


<H2><A NAME="SEC23" HREF="gdbint_toc.html#TOC23">Object File Formats</A></H2>



<H3><A NAME="SEC24" HREF="gdbint_toc.html#TOC24">a.out</A></H3>

<P>
The <TT>`a.out'</TT> format is the original file format for Unix.  It
consists of three sections: text, data, and bss, which are for program
code, initialized data, and uninitialized data, respectively.

</P>
<P>
The <TT>`a.out'</TT> format is so simple that it doesn't have any reserved
place for debugging information.  (Hey, the original Unix hackers used
<TT>`adb'</TT>, which is a machine-language debugger.)  The only debugging
format for <TT>`a.out'</TT> is stabs, which is encoded as a set of normal
symbols with distinctive attributes.

</P>
<P>
The basic <TT>`a.out'</TT> reader is in <TT>`dbxread.c'</TT>.

</P>


<H3><A NAME="SEC25" HREF="gdbint_toc.html#TOC25">COFF</A></H3>

<P>
The COFF format was introduced with System V Release 3 (SVR3) Unix.
COFF files may have multiple sections, each prefixed by a header.  The
number of sections is limited.

</P>
<P>
The COFF specification includes support for debugging.  Although this
was a step forward, the debugging information was woefully limited.  For
instance, it was not possible to represent code that came from an
included file.

</P>
<P>
The COFF reader is in <TT>`coffread.c'</TT>.

</P>


<H3><A NAME="SEC26" HREF="gdbint_toc.html#TOC26">ECOFF</A></H3>

<P>
ECOFF is an extended COFF originally introduced for Mips and Alpha
workstations.

</P>
<P>
The basic ECOFF reader is in <TT>`mipsread.c'</TT>.

</P>


<H3><A NAME="SEC27" HREF="gdbint_toc.html#TOC27">XCOFF</A></H3>

<P>
The IBM RS/6000 running AIX uses an object file format called XCOFF.
The COFF sections, symbols, and line numbers are used, but debugging
symbols are dbx-style stabs whose strings are located in the
<SAMP>`.debug'</SAMP> section (rather than the string table).  For more
information, see See section `Top' in <CITE>The Stabs Debugging Format</CITE>.

</P>
<P>
The shared library scheme has a clean interface for figuring out what
shared libraries are in use, but the catch is that everything which
refers to addresses (symbol tables and breakpoints at least) needs to be
relocated for both shared libraries and the main executable.  At least
using the standard mechanism this can only be done once the program has
been run (or the core file has been read).

</P>


<H3><A NAME="SEC28" HREF="gdbint_toc.html#TOC28">PE</A></H3>

<P>
Windows 95 and NT use the PE (Portable Executable) format for their
executables.  PE is basically COFF with additional headers.

</P>
<P>
While BFD includes special PE support, GDB needs only the basic
COFF reader.

</P>


<H3><A NAME="SEC29" HREF="gdbint_toc.html#TOC29">ELF</A></H3>

<P>
The ELF format came with System V Release 4 (SVR4) Unix.  ELF is similar
to COFF in being organized into a number of sections, but it removes
many of COFF's limitations.

</P>
<P>
The basic ELF reader is in <TT>`elfread.c'</TT>.

</P>


<H3><A NAME="SEC30" HREF="gdbint_toc.html#TOC30">SOM</A></H3>

<P>
SOM is HP's object file and debug format (not to be confused with IBM's
SOM, which is a cross-language ABI).

</P>
<P>
The SOM reader is in <TT>`hpread.c'</TT>.

</P>


<H3><A NAME="SEC31" HREF="gdbint_toc.html#TOC31">Other File Formats</A></H3>

<P>
Other file formats that have been supported by GDB include Netware
Loadable Modules (<TT>`nlmread.c'</TT>.

</P>


<H2><A NAME="SEC32" HREF="gdbint_toc.html#TOC32">Debugging File Formats</A></H2>

<P>
This section describes characteristics of debugging information that
are independent of the object file format.

</P>


<H3><A NAME="SEC33" HREF="gdbint_toc.html#TOC33">stabs</A></H3>

<P>
<CODE>stabs</CODE> started out as special symbols within the <CODE>a.out</CODE>
format.  Since then, it has been encapsulated into other file
formats, such as COFF and ELF.

</P>
<P>
While <TT>`dbxread.c'</TT> does some of the basic stab processing,
including for encapsulated versions, <TT>`stabsread.c'</TT> does
the real work.

</P>


<H3><A NAME="SEC34" HREF="gdbint_toc.html#TOC34">COFF</A></H3>

<P>
The basic COFF definition includes debugging information.  The level
of support is minimal and non-extensible, and is not often used.

</P>


<H3><A NAME="SEC35" HREF="gdbint_toc.html#TOC35">Mips debug (Third Eye)</A></H3>

<P>
ECOFF includes a definition of a special debug format.

</P>
<P>
The file <TT>`mdebugread.c'</TT> implements reading for this format.

</P>


<H3><A NAME="SEC36" HREF="gdbint_toc.html#TOC36">DWARF 1</A></H3>

<P>
DWARF 1 is a debugging format that was originally designed to be
used with ELF in SVR4 systems.

</P>

<P>
The DWARF 1 reader is in <TT>`dwarfread.c'</TT>.

</P>


<H3><A NAME="SEC37" HREF="gdbint_toc.html#TOC37">DWARF 2</A></H3>

<P>
DWARF 2 is an improved but incompatible version of DWARF 1.

</P>
<P>
The DWARF 2 reader is in <TT>`dwarf2read.c'</TT>.

</P>


<H3><A NAME="SEC38" HREF="gdbint_toc.html#TOC38">SOM</A></H3>

<P>
Like COFF, the SOM definition includes debugging information.

</P>


<H2><A NAME="SEC39" HREF="gdbint_toc.html#TOC39">Adding a New Symbol Reader to GDB</A></H2>

<P>
If you are using an existing object file format (a.out, COFF, ELF, etc),
there is probably little to be done.

</P>
<P>
If you need to add a new object file format, you must first add it to
BFD.  This is beyond the scope of this document.

</P>
<P>
You must then arrange for the BFD code to provide access to the
debugging symbols.  Generally GDB will have to call swapping routines
from BFD and a few other BFD internal routines to locate the debugging
information.  As much as possible, GDB should not depend on the BFD
internal data structures.

</P>
<P>
For some targets (e.g., COFF), there is a special transfer vector used
to call swapping routines, since the external data structures on various
platforms have different sizes and layouts.  Specialized routines that
will only ever be implemented by one object file format may be called
directly.  This interface should be described in a file
<TT>`bfd/libxyz.h'</TT>, which is included by GDB.

</P>

<P><HR><P>
Go to the <A HREF="gdbint_1.html">first</A>, <A HREF="gdbint_4.html">previous</A>, <A HREF="gdbint_6.html">next</A>, <A HREF="gdbint_16.html">last</A> section, <A HREF="gdbint_toc.html">table of contents</A>.
</BODY>
</HTML>