xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/buildsym.h (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /* Build symbol tables in GDB's internal format.
2    Copyright (C) 1986-2019 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
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.
10 
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.
15 
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/>.  */
18 
19 #if !defined (BUILDSYM_H)
20 #define BUILDSYM_H 1
21 
22 struct objfile;
23 struct symbol;
24 struct addrmap;
25 struct compunit_symtab;
26 enum language;
27 
28 /* This module provides definitions used for creating and adding to
29    the symbol table.  These routines are called from various symbol-
30    file-reading routines.
31 
32    They originated in dbxread.c of gdb-4.2, and were split out to
33    make xcoffread.c more maintainable by sharing code.  */
34 
35 struct block;
36 struct pending_block;
37 
38 struct dynamic_prop;
39 
40 /* The list of sub-source-files within the current individual
41    compilation.  Each file gets its own symtab with its own linetable
42    and associated info, but they all share one blockvector.  */
43 
44 struct subfile
45 {
46   struct subfile *next;
47   /* Space for this is malloc'd.  */
48   char *name;
49   /* Space for this is malloc'd.  */
50   struct linetable *line_vector;
51   int line_vector_length;
52   /* The "containing" compunit.  */
53   struct buildsym_compunit *buildsym_compunit;
54   enum language language;
55   struct symtab *symtab;
56 };
57 
58 /* Record the symbols defined for each context in a list.  We don't
59    create a struct block for the context until we know how long to
60    make it.  */
61 
62 #define PENDINGSIZE 100
63 
64 struct pending
65   {
66     struct pending *next;
67     int nsyms;
68     struct symbol *symbol[PENDINGSIZE];
69   };
70 
71 /* Stack representing unclosed lexical contexts (that will become
72    blocks, eventually).  */
73 
74 struct context_stack
75   {
76     /* Outer locals at the time we entered */
77 
78     struct pending *locals;
79 
80     /* Pending using directives at the time we entered.  */
81 
82     struct using_direct *local_using_directives;
83 
84     /* Pointer into blocklist as of entry */
85 
86     struct pending_block *old_blocks;
87 
88     /* Name of function, if any, defining context */
89 
90     struct symbol *name;
91 
92     /* Expression that computes the frame base of the lexically enclosing
93        function, if any.  NULL otherwise.  */
94 
95     struct dynamic_prop *static_link;
96 
97     /* PC where this context starts */
98 
99     CORE_ADDR start_addr;
100 
101     /* Temp slot for exception handling.  */
102 
103     CORE_ADDR end_addr;
104 
105     /* For error-checking matching push/pop */
106 
107     int depth;
108 
109   };
110 
111 /* Buildsym's counterpart to struct compunit_symtab.  */
112 
113 struct buildsym_compunit
114 {
115   /* Start recording information about a primary source file (IOW, not an
116      included source file).
117      COMP_DIR is the directory in which the compilation unit was compiled
118      (or NULL if not known).  */
119 
120   buildsym_compunit (struct objfile *objfile_, const char *name,
121 		     const char *comp_dir_, enum language language_,
122 		     CORE_ADDR last_addr);
123 
124   /* Reopen an existing compunit_symtab so that additional symbols can
125      be added to it.  Arguments are as for the main constructor.  CUST
126      is the expandable compunit_symtab to be reopened.  */
127 
128   buildsym_compunit (struct objfile *objfile_, const char *name,
129 		     const char *comp_dir_, enum language language_,
130 		     CORE_ADDR last_addr, struct compunit_symtab *cust)
131     : m_objfile (objfile_),
132       m_last_source_file (name == nullptr ? nullptr : xstrdup (name)),
133       m_comp_dir (comp_dir_ == nullptr ? nullptr : xstrdup (comp_dir_)),
134       m_compunit_symtab (cust),
135       m_language (language_),
136       m_last_source_start_addr (last_addr)
137   {
138   }
139 
140   ~buildsym_compunit ();
141 
142   DISABLE_COPY_AND_ASSIGN (buildsym_compunit);
143 
144   void set_last_source_file (const char *name)
145   {
146     char *new_name = name == NULL ? NULL : xstrdup (name);
147     m_last_source_file.reset (new_name);
148   }
149 
150   const char *get_last_source_file ()
151   {
152     return m_last_source_file.get ();
153   }
154 
155   struct macro_table *get_macro_table ();
156 
157   struct macro_table *release_macros ()
158   {
159     struct macro_table *result = m_pending_macros;
160     m_pending_macros = nullptr;
161     return result;
162   }
163 
164   /* This function is called to discard any pending blocks.  */
165 
166   void free_pending_blocks ()
167   {
168     m_pending_block_obstack.clear ();
169     m_pending_blocks = nullptr;
170   }
171 
172   struct block *finish_block (struct symbol *symbol,
173 			      struct pending_block *old_blocks,
174 			      const struct dynamic_prop *static_link,
175 			      CORE_ADDR start, CORE_ADDR end);
176 
177   void record_block_range (struct block *block,
178 			   CORE_ADDR start, CORE_ADDR end_inclusive);
179 
180   void start_subfile (const char *name);
181 
182   void patch_subfile_names (struct subfile *subfile, const char *name);
183 
184   void push_subfile ();
185 
186   const char *pop_subfile ();
187 
188   void record_line (struct subfile *subfile, int line, CORE_ADDR pc);
189 
190   struct compunit_symtab *get_compunit_symtab ()
191   {
192     return m_compunit_symtab;
193   }
194 
195   void set_last_source_start_addr (CORE_ADDR addr)
196   {
197     m_last_source_start_addr = addr;
198   }
199 
200   CORE_ADDR get_last_source_start_addr ()
201   {
202     return m_last_source_start_addr;
203   }
204 
205   struct using_direct **get_local_using_directives ()
206   {
207     return &m_local_using_directives;
208   }
209 
210   void set_local_using_directives (struct using_direct *new_local)
211   {
212     m_local_using_directives = new_local;
213   }
214 
215   struct using_direct **get_global_using_directives ()
216   {
217     return &m_global_using_directives;
218   }
219 
220   bool outermost_context_p () const
221   {
222     return m_context_stack.empty ();
223   }
224 
225   struct context_stack *get_current_context_stack ()
226   {
227     if (m_context_stack.empty ())
228       return nullptr;
229     return &m_context_stack.back ();
230   }
231 
232   int get_context_stack_depth () const
233   {
234     return m_context_stack.size ();
235   }
236 
237   struct subfile *get_current_subfile ()
238   {
239     return m_current_subfile;
240   }
241 
242   struct pending **get_local_symbols ()
243   {
244     return &m_local_symbols;
245   }
246 
247   struct pending **get_file_symbols ()
248   {
249     return &m_file_symbols;
250   }
251 
252   struct pending **get_global_symbols ()
253   {
254     return &m_global_symbols;
255   }
256 
257   void record_debugformat (const char *format)
258   {
259     m_debugformat = format;
260   }
261 
262   void record_producer (const char *producer)
263   {
264     m_producer = producer;
265   }
266 
267   struct context_stack *push_context (int desc, CORE_ADDR valu);
268 
269   struct context_stack pop_context ();
270 
271   struct block *end_symtab_get_static_block (CORE_ADDR end_addr,
272 					     int expandable, int required);
273 
274   struct compunit_symtab *end_symtab_from_static_block
275       (struct block *static_block, int section, int expandable);
276 
277   struct compunit_symtab *end_symtab (CORE_ADDR end_addr, int section);
278 
279   struct compunit_symtab *end_expandable_symtab (CORE_ADDR end_addr,
280 						 int section);
281 
282   void augment_type_symtab ();
283 
284 private:
285 
286   void record_pending_block (struct block *block, struct pending_block *opblock);
287 
288   struct block *finish_block_internal (struct symbol *symbol,
289 				       struct pending **listhead,
290 				       struct pending_block *old_blocks,
291 				       const struct dynamic_prop *static_link,
292 				       CORE_ADDR start, CORE_ADDR end,
293 				       int is_global, int expandable);
294 
295   struct blockvector *make_blockvector ();
296 
297   void watch_main_source_file_lossage ();
298 
299   struct compunit_symtab *end_symtab_with_blockvector
300       (struct block *static_block, int section, int expandable);
301 
302   /* The objfile we're reading debug info from.  */
303   struct objfile *m_objfile;
304 
305   /* List of subfiles (source files).
306      Files are added to the front of the list.
307      This is important mostly for the language determination hacks we use,
308      which iterate over previously added files.  */
309   struct subfile *m_subfiles = nullptr;
310 
311   /* The subfile of the main source file.  */
312   struct subfile *m_main_subfile = nullptr;
313 
314   /* Name of source file whose symbol data we are now processing.  This
315      comes from a symbol of type N_SO for stabs.  For DWARF it comes
316      from the DW_AT_name attribute of a DW_TAG_compile_unit DIE.  */
317   gdb::unique_xmalloc_ptr<char> m_last_source_file;
318 
319   /* E.g., DW_AT_comp_dir if DWARF.  Space for this is malloc'd.  */
320   gdb::unique_xmalloc_ptr<char> m_comp_dir;
321 
322   /* Space for this is not malloc'd, and is assumed to have at least
323      the same lifetime as objfile.  */
324   const char *m_producer = nullptr;
325 
326   /* Space for this is not malloc'd, and is assumed to have at least
327      the same lifetime as objfile.  */
328   const char *m_debugformat = nullptr;
329 
330   /* The compunit we are building.  */
331   struct compunit_symtab *m_compunit_symtab = nullptr;
332 
333   /* Language of this compunit_symtab.  */
334   enum language m_language;
335 
336   /* The macro table for the compilation unit whose symbols we're
337      currently reading.  */
338   struct macro_table *m_pending_macros = nullptr;
339 
340   /* True if symtab has line number info.  This prevents an otherwise
341      empty symtab from being tossed.  */
342   bool m_have_line_numbers = false;
343 
344   /* Core address of start of text of current source file.  This too
345      comes from the N_SO symbol.  For Dwarf it typically comes from the
346      DW_AT_low_pc attribute of a DW_TAG_compile_unit DIE.  */
347   CORE_ADDR m_last_source_start_addr;
348 
349   /* Stack of subfile names.  */
350   std::vector<const char *> m_subfile_stack;
351 
352   /* The "using" directives local to lexical context.  */
353   struct using_direct *m_local_using_directives = nullptr;
354 
355   /* Global "using" directives.  */
356   struct using_direct *m_global_using_directives = nullptr;
357 
358   /* The stack of contexts that are pushed by push_context and popped
359      by pop_context.  */
360   std::vector<struct context_stack> m_context_stack;
361 
362   struct subfile *m_current_subfile = nullptr;
363 
364   /* The mutable address map for the compilation unit whose symbols
365      we're currently reading.  The symtabs' shared blockvector will
366      point to a fixed copy of this.  */
367   struct addrmap *m_pending_addrmap = nullptr;
368 
369   /* The obstack on which we allocate pending_addrmap.
370      If pending_addrmap is NULL, this is uninitialized; otherwise, it is
371      initialized (and holds pending_addrmap).  */
372   auto_obstack m_pending_addrmap_obstack;
373 
374   /* True if we recorded any ranges in the addrmap that are different
375      from those in the blockvector already.  We set this to false when
376      we start processing a symfile, and if it's still false at the
377      end, then we just toss the addrmap.  */
378   bool m_pending_addrmap_interesting = false;
379 
380   /* An obstack used for allocating pending blocks.  */
381   auto_obstack m_pending_block_obstack;
382 
383   /* Pointer to the head of a linked list of symbol blocks which have
384      already been finalized (lexical contexts already closed) and which
385      are just waiting to be built into a blockvector when finalizing the
386      associated symtab.  */
387   struct pending_block *m_pending_blocks = nullptr;
388 
389   /* Pending static symbols and types at the top level.  */
390   struct pending *m_file_symbols = nullptr;
391 
392   /* Pending global functions and variables.  */
393   struct pending *m_global_symbols = nullptr;
394 
395   /* Pending symbols that are local to the lexical context.  */
396   struct pending *m_local_symbols = nullptr;
397 };
398 
399 
400 
401 extern void add_symbol_to_list (struct symbol *symbol,
402 				struct pending **listhead);
403 
404 extern struct symbol *find_symbol_in_list (struct pending *list,
405 					   char *name, int length);
406 
407 #endif /* defined (BUILDSYM_H) */
408