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