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