RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdb / progspace.h
blobd426dfaabf97a5a41822ee3bcc00ec8e9ff0f67e
1 /* Program and address space management, for GDB, the GNU debugger.
3 Copyright (C) 2009-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #ifndef PROGSPACE_H
22 #define PROGSPACE_H
24 #include "target.h"
25 #include "gdb_bfd.h"
26 #include "registry.h"
27 #include "solist.h"
28 #include "gdbsupport/safe-iterator.h"
29 #include "gdbsupport/intrusive_list.h"
30 #include "gdbsupport/refcounted-object.h"
31 #include "gdbsupport/gdb_ref_ptr.h"
32 #include <vector>
34 struct target_ops;
35 struct bfd;
36 struct objfile;
37 struct inferior;
38 struct exec;
39 struct address_space;
40 struct program_space;
41 struct solib;
43 /* An address space. It is used for comparing if
44 pspaces/inferior/threads see the same address space and for
45 associating caches to each address space. */
46 struct address_space : public refcounted_object
48 /* Create a new address space object, and add it to the list. */
49 address_space ();
50 DISABLE_COPY_AND_ASSIGN (address_space);
52 /* Returns the integer address space id of this address space. */
53 int num () const
55 return m_num;
58 /* Per aspace data-pointers required by other GDB modules. */
59 registry<address_space> registry_fields;
61 private:
62 int m_num;
65 using address_space_ref_ptr
66 = gdb::ref_ptr<address_space,
67 refcounted_object_delete_ref_policy<address_space>>;
69 /* Create a new address space. */
71 static inline address_space_ref_ptr
72 new_address_space ()
74 return address_space_ref_ptr::new_reference (new address_space);
77 /* A program space represents a symbolic view of an address space.
78 Roughly speaking, it holds all the data associated with a
79 non-running-yet program (main executable, main symbols), and when
80 an inferior is running and is bound to it, includes the list of its
81 mapped in shared libraries.
83 In the traditional debugging scenario, there's a 1-1 correspondence
84 among program spaces, inferiors and address spaces, like so:
86 pspace1 (prog1) <--> inf1(pid1) <--> aspace1
88 In the case of debugging more than one traditional unix process or
89 program, we still have:
91 |-----------------+------------+---------|
92 | pspace1 (prog1) | inf1(pid1) | aspace1 |
93 |----------------------------------------|
94 | pspace2 (prog1) | no inf yet | aspace2 |
95 |-----------------+------------+---------|
96 | pspace3 (prog2) | inf2(pid2) | aspace3 |
97 |-----------------+------------+---------|
99 In the former example, if inf1 forks (and GDB stays attached to
100 both processes), the new child will have its own program and
101 address spaces. Like so:
103 |-----------------+------------+---------|
104 | pspace1 (prog1) | inf1(pid1) | aspace1 |
105 |-----------------+------------+---------|
106 | pspace2 (prog1) | inf2(pid2) | aspace2 |
107 |-----------------+------------+---------|
109 However, had inf1 from the latter case vforked instead, it would
110 share the program and address spaces with its parent, until it
111 execs or exits, like so:
113 |-----------------+------------+---------|
114 | pspace1 (prog1) | inf1(pid1) | aspace1 |
115 | | inf2(pid2) | |
116 |-----------------+------------+---------|
118 When the vfork child execs, it is finally given new program and
119 address spaces.
121 |-----------------+------------+---------|
122 | pspace1 (prog1) | inf1(pid1) | aspace1 |
123 |-----------------+------------+---------|
124 | pspace2 (prog1) | inf2(pid2) | aspace2 |
125 |-----------------+------------+---------|
127 There are targets where the OS (if any) doesn't provide memory
128 management or VM protection, where all inferiors share the same
129 address space --- e.g. uClinux. GDB models this by having all
130 inferiors share the same address space, but, giving each its own
131 program space, like so:
133 |-----------------+------------+---------|
134 | pspace1 (prog1) | inf1(pid1) | |
135 |-----------------+------------+ |
136 | pspace2 (prog1) | inf2(pid2) | aspace1 |
137 |-----------------+------------+ |
138 | pspace3 (prog2) | inf3(pid3) | |
139 |-----------------+------------+---------|
141 The address space sharing matters for run control and breakpoints
142 management. E.g., did we just hit a known breakpoint that we need
143 to step over? Is this breakpoint a duplicate of this other one, or
144 do I need to insert a trap?
146 Then, there are targets where all symbols look the same for all
147 inferiors, although each has its own address space, as e.g.,
148 Ericsson DICOS. In such case, the model is:
150 |---------+------------+---------|
151 | | inf1(pid1) | aspace1 |
152 | +------------+---------|
153 | pspace | inf2(pid2) | aspace2 |
154 | +------------+---------|
155 | | inf3(pid3) | aspace3 |
156 |---------+------------+---------|
158 Note however, that the DICOS debug API takes care of making GDB
159 believe that breakpoints are "global". That is, although each
160 process does have its own private copy of data symbols (just like a
161 bunch of forks), to the breakpoints module, all processes share a
162 single address space, so all breakpoints set at the same address
163 are duplicates of each other, even breakpoints set in the data
164 space (e.g., call dummy breakpoints placed on stack). This allows
165 a simplification in the spaces implementation: we avoid caring for
166 a many-many links between address and program spaces. Either
167 there's a single address space bound to the program space
168 (traditional unix/uClinux), or, in the DICOS case, the address
169 space bound to the program space is mostly ignored. */
171 /* The program space structure. */
173 struct program_space
175 /* Constructs a new empty program space, binds it to ASPACE, and
176 adds it to the program space list. */
177 explicit program_space (address_space_ref_ptr aspace);
179 /* Releases a program space, and all its contents (shared libraries,
180 objfiles, and any other references to the program space in other
181 modules). It is an internal error to call this when the program
182 space is the current program space, since there should always be
183 a program space. */
184 ~program_space ();
186 using objfiles_iterator
187 = reference_to_pointer_iterator<intrusive_list<objfile>::iterator>;
188 using objfiles_range = iterator_range<objfiles_iterator>;
190 /* Return an iterable object that can be used to iterate over all
191 objfiles. The basic use is in a foreach, like:
193 for (objfile *objf : pspace->objfiles ()) { ... } */
194 objfiles_range objfiles ()
196 return objfiles_range (objfiles_iterator (m_objfiles_list.begin ()));
199 using objfiles_safe_range = basic_safe_range<objfiles_range>;
201 /* An iterable object that can be used to iterate over all objfiles.
202 The basic use is in a foreach, like:
204 for (objfile *objf : pspace->objfiles_safe ()) { ... }
206 This variant uses a basic_safe_iterator so that objfiles can be
207 deleted during iteration. */
208 objfiles_safe_range objfiles_safe ()
210 return objfiles_safe_range
211 (objfiles_range (objfiles_iterator (m_objfiles_list.begin ())));
214 /* Add OBJFILE to the list of objfiles, putting it just before
215 BEFORE. If BEFORE is nullptr, it will go at the end of the
216 list. */
217 void add_objfile (std::unique_ptr<objfile> &&objfile,
218 struct objfile *before);
220 /* Remove OBJFILE from the list of objfiles. */
221 void remove_objfile (struct objfile *objfile);
223 /* Return true if there is more than one object file loaded; false
224 otherwise. */
225 bool multi_objfile_p () const;
227 /* Free all the objfiles associated with this program space. */
228 void free_all_objfiles ();
230 /* Return the objfile containing ADDRESS, or nullptr if the address
231 is outside all objfiles in this progspace. */
232 struct objfile *objfile_for_address (CORE_ADDR address);
234 /* Return the list of all the solibs in this program space. */
235 owning_intrusive_list<solib> &solibs ()
236 { return so_list; }
238 /* Similar to `bfd_get_filename (exec_bfd ())` but in original form given
239 by user, without symbolic links and pathname resolved. It is not nullptr
240 iff `exec_bfd ()` is not nullptr. */
241 const char *exec_filename () const
242 { return m_exec_filename.get (); }
244 void set_exec_filename (gdb::unique_xmalloc_ptr<char> filename)
245 { m_exec_filename = std::move (filename); }
247 /* Close and clear exec_bfd. If we end up with no target sections
248 to read memory from, this unpushes the exec_ops target. */
249 void exec_close ();
251 /* Return the exec BFD for this program space. */
252 bfd *exec_bfd () const
253 { return ebfd.get (); }
255 /* Set the exec BFD for this program space to ABFD. */
256 void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
258 ebfd = std::move (abfd);
261 bfd *core_bfd () const
262 { return cbfd.get (); }
264 /* Reset saved solib data at the start of an solib event. This lets
265 us properly collect the data when calling solib_add, so it can then
266 later be printed. */
267 void clear_solib_cache ();
269 /* Returns true iff there's no inferior bound to this program
270 space. */
271 bool empty ();
273 /* Remove all target sections owned by OWNER. */
274 void remove_target_sections (target_section_owner owner);
276 /* Add the sections array defined by SECTIONS to the
277 current set of target sections. */
278 void add_target_sections (target_section_owner owner,
279 const std::vector<target_section> &sections);
281 /* Add the sections of OBJFILE to the current set of target
282 sections. They are given OBJFILE as the "owner". */
283 void add_target_sections (struct objfile *objfile);
285 /* Clear all target sections from M_TARGET_SECTIONS table. */
286 void clear_target_sections ()
288 m_target_sections.clear ();
291 /* Return a reference to the M_TARGET_SECTIONS table. */
292 std::vector<target_section> &target_sections ()
294 return m_target_sections;
297 /* Unique ID number. */
298 int num = 0;
300 /* The main executable loaded into this program space. This is
301 managed by the exec target. */
303 /* The BFD handle for the main executable. */
304 gdb_bfd_ref_ptr ebfd;
305 /* The last-modified time, from when the exec was brought in. */
306 long ebfd_mtime = 0;
308 /* Binary file diddling handle for the core file. */
309 gdb_bfd_ref_ptr cbfd;
311 /* The address space attached to this program space. More than one
312 program space may be bound to the same address space. In the
313 traditional unix-like debugging scenario, this will usually
314 match the address space bound to the inferior, and is mostly
315 used by the breakpoints module for address matches. If the
316 target shares a program space for all inferiors and breakpoints
317 are global, then this field is ignored (we don't currently
318 support inferiors sharing a program space if the target doesn't
319 make breakpoints global). */
320 address_space_ref_ptr aspace;
322 /* True if this program space's section offsets don't yet represent
323 the final offsets of the "live" address space (that is, the
324 section addresses still require the relocation offsets to be
325 applied, and hence we can't trust the section addresses for
326 anything that pokes at live memory). E.g., for qOffsets
327 targets, or for PIE executables, until we connect and ask the
328 target for the final relocation offsets, the symbols we've used
329 to set breakpoints point at the wrong addresses. */
330 int executing_startup = 0;
332 /* True if no breakpoints should be inserted in this program
333 space. */
334 int breakpoints_not_allowed = 0;
336 /* The object file that the main symbol table was loaded from
337 (e.g. the argument to the "symbol-file" or "file" command). */
338 struct objfile *symfile_object_file = NULL;
340 /* List of shared objects mapped into this space. Managed by
341 solib.c. */
342 owning_intrusive_list<solib> so_list;
344 /* Number of calls to solib_add. */
345 unsigned int solib_add_generation = 0;
347 /* When an solib is added, it is also added to this vector. This
348 is so we can properly report solib changes to the user. */
349 std::vector<solib *> added_solibs;
351 /* When an solib is removed, its name is added to this vector.
352 This is so we can properly report solib changes to the user. */
353 std::vector<std::string> deleted_solibs;
355 /* Per pspace data-pointers required by other GDB modules. */
356 registry<program_space> registry_fields;
358 private:
359 /* All known objfiles are kept in a linked list. */
360 owning_intrusive_list<objfile> m_objfiles_list;
362 /* The set of target sections matching the sections mapped into
363 this program space. Managed by both exec_ops and solib.c. */
364 std::vector<target_section> m_target_sections;
366 /* See `exec_filename`. */
367 gdb::unique_xmalloc_ptr<char> m_exec_filename;
370 /* The list of all program spaces. There's always at least one. */
371 extern std::vector<struct program_space *>program_spaces;
373 /* The current program space. This is always non-null. */
374 extern struct program_space *current_program_space;
376 /* Initialize progspace-related global state. */
377 extern void initialize_progspace ();
379 /* Copies program space SRC to DEST. Copies the main executable file,
380 and the main symbol file. Returns DEST. */
381 extern struct program_space *clone_program_space (struct program_space *dest,
382 struct program_space *src);
384 /* Sets PSPACE as the current program space. This is usually used
385 instead of set_current_space_and_thread when the current
386 thread/inferior is not important for the operations that follow.
387 E.g., when accessing the raw symbol tables. If memory access is
388 required, then you should use switch_to_program_space_and_thread.
389 Otherwise, it is the caller's responsibility to make sure that the
390 currently selected inferior/thread matches the selected program
391 space. */
392 extern void set_current_program_space (struct program_space *pspace);
394 /* Save/restore the current program space. */
396 class scoped_restore_current_program_space
398 public:
399 scoped_restore_current_program_space ()
400 : m_saved_pspace (current_program_space)
403 ~scoped_restore_current_program_space ()
404 { set_current_program_space (m_saved_pspace); }
406 DISABLE_COPY_AND_ASSIGN (scoped_restore_current_program_space);
408 private:
409 program_space *m_saved_pspace;
412 /* Maybe create a new address space object, and add it to the list, or
413 return a pointer to an existing address space, in case inferiors
414 share an address space. */
415 extern address_space_ref_ptr maybe_new_address_space ();
417 /* Update all program spaces matching to address spaces. The user may
418 have created several program spaces, and loaded executables into
419 them before connecting to the target interface that will create the
420 inferiors. All that happens before GDB has a chance to know if the
421 inferiors will share an address space or not. Call this after
422 having connected to the target interface and having fetched the
423 target description, to fixup the program/address spaces
424 mappings. */
425 extern void update_address_spaces (void);
427 #endif