In some cases, the objfile owns the per-bfd object. This is yet
another object that can sometimes be destroyed before the registry is
destroyed, possibly reslting in a use-after-free. Also, I noticed
that the condition for deleting the object is not the same as the
condition used to create it -- so it could possibly result in a memory
leak in some situations. This patch fixes the problem by introducing
a new unique_ptr that holds this object when necessary.
This changes objfile to use an auto_obstack. This helps prevent
use-after-free bugs, because it ensures that anything allocated on the
objfile obstack will live past the point at which the registry object
is destroyed.
This changes struct objfile to use a gdb_bfd_ref_ptr. In addition to
removing some manual memory management, this fixes a use-after-free
that was introduced by the registry rewrite series. The issue there
was that, in some cases, registry shutdown could refer to memory that
had already been freed. This help fix the bug by delaying the
destruction of the BFD reference (and thus the per-bfd object) until
after the registry has been shut down.
This rewrites registry.h, removing all the macros and replacing it
with relatively ordinary template classes. The result is less code
than the previous setup. It replaces large macros with a relatively
straightforward C++ class, and now manages its own cleanup.
The existing type-safe "key" class is replaced with the equivalent
template class. This approach ended up requiring relatively few
changes to the users of the registry code in gdb -- code using the key
system just required a small change to the key's declaration.
All existing users of the old C-like API are now converted to use the
type-safe API. This mostly involved changing explicit deletion
functions to be an operator() in a deleter class.
The old "save/free" two-phase process is removed, and replaced with a
single "free" phase. No existing code used both phases.
The old "free" callbacks took a parameter for the enclosing container
object. However, this wasn't truly needed and is removed here as
well.
From: Ulrich Weigand <ulrich.weigand@de.ibm.com>
build_objfile_section_table () creates four synthetic sections per
objfile, which are collected by update_section_map () and passed to
std::sort (). When there are a lot of objfiles, for example, when
debugging JITs, the presence of these sections slows down the sorting
significantly.
The output of update_section_map () is used by find_pc_section (),
which can never return any of these sections: their size is 0, so they
cannot be accepted by bsearch_cmp ().
Filter them (and all the other empty sections) out in
insert_section_p (), which is used only by update_section_map ().
A while back, I changed objfiles to be held via a shared_ptr. The
idea at the time was that this was a step toward writing to the index
cache in the background, and this would let gdb keep a reference alive
to do so. However, since then we've rewritten the DWARF reader, and
the new index can do this without requiring a shared pointer -- in
fact there are patches pending to implement this.
This patch switches objfile management to unique_ptr, which makes more
sense now.
Regression tested on x86-64 Fedora 34.
A rather straightforward patch to change an instance of callback +
void pointer to gdb::function_view, allowing pasing lambdas that
capture, and eliminating the need for the untyped pointer.
Change-Id: I73ed644e7849945265a2c763f79f5456695b0037
Replace with calls to blockvector::blocks, and the appropriate method
call on the returned array_view.
Change-Id: I04d1f39603e4d4c21c96822421431d9a029d8ddd
I'm trying to switch these functions to use std::string instead of char
arrays, as much as possible. Some callers benefit from it (can avoid
doing a copy of the result), while others suffer (have to make one more
copy).
Change-Id: Iced49b8ee2f189744c5072a3b217aab5af17a993
Same idea as previous patch, but for symtab::objfile. I find
it clearer without this wrapper, as it shows that the objfile is
common to all symtabs of a given compunit. Otherwise, you could think
that each symtab (of a given compunit) can have a specific objfile.
Change-Id: Ifc0dbc7ec31a06eefa2787c921196949d5a6fcc6
Add a getter and a setter for a symtab's linetable. Remove the
corresponding macro and adjust all callers.
Change-Id: I159183fc0ccd8e18ab937b3c2f09ef2244ec6e9c
Add a getter and a setter for a compunit_symtab's block line section. Remove
the corresponding macro and adjust all callers.
Change-Id: I3eb1a323388ad55eae8bfa45f5bc4a08dc3df455
Add a getter and a setter for a compunit_symtab's blockvector. Remove
the corresponding macro and adjust all callers.
Change-Id: I99484c6619dcbbea7c5d89c72aa660316ca62f64
Make compunit_filetabs, used to iterate a compunit_symtab's filetabs, a
method of compunit_symtab. The name filetabs conflicts with the current
name of the field. Rename the field to m_filetabs, since at this point
nothing outside of compunit_symtab uses it, so we should treat it as
private (even though it's not actually private). Rename the
last_filetab field to m_last_filetab as well (it's only used on
compunit_symtab::add_filetab).
Adjust the COMPUNIT_FILETABS macro to keep its current behavior of
returning the first filetab.
Change-Id: I537b553a44451c52d24b18ee1bfa47e23747cfc3
This commit brings all the changes made by running gdb/copyright.py
as per GDB's Start of New Year Procedure.
For the avoidance of doubt, all changes in this commits were
performed by the script.
A bug was filed against the incorrect underlying type setting for
an enumeration type, which was caused by a copy and paste error.
This patch fixes the problem by setting it by calling objfile_int_type,
which was originally dwarf2_per_objfile::int_type, with ctf_type_size bits.
Also add error checking on ctf_func_type_info call.
Convert these three macros to methods of obj_section. The problem fixed
by the following patch is caused by an out of bound access of the
objfile::section_offsets vector. Since this is deep in macros, we don't
get a clear backtrace and it's difficult to debug. Changing that to
methods means we can step in them and break on them.
Because their implementation requires knowing about struct objfile, move
struct obj_section below struct objfile in objfiles.h.
The obj_section_offset was used in one place as an lvalue to set
offsets, in machoread.c. Replace that with a set_offset method.
Add the objfile::section_offset and objfile::set_section_offset methods
to improve encapsulation (reduce other objects poking into struct
objfile's internals).
gdb/ChangeLog:
* objfiles.h (struct obj_section): Move down.
<offset, set_offset, addr, endaddr>: New.
(obj_section_offset, obj_section_addr, obj_section_endaddr),
replace all users to use obj_section methods.
(struct objfile) <section_offset, set_section_offset>: New.
Change-Id: I97e8fcae93ab2353fbdadcb4a5ec10d7949a7334
I noticed it was unused. I think that makes sense, as it shows that
objfile_per_bfd_storage is not specific to one objfile (it can be shared
by multiple objfiles that have the same bfd).
There is one thing I wonder though, maybe I'm missing something. If
the BFD doesn't require relocation, get_objfile_bfd_data stores the
newly allocated object in objfiles_bfd_data, so we can assume that
objfiles_bfd_data is the owner of the object. When the bfd's refcount
drops to 0, the corresponding objfile_per_bfd_storage object in
objfiles_bfd_data is deleted.
But if the BFD requires relocation, get_objfile_bfd_data returns a newly
allocated object that isn't kept anywhere else (and isn't shared). So
the objfile becomes the owner of the objfile_per_bfd_storage object. In
objfile::~objfile, we have this:
if (obfd)
gdb_bfd_unref (obfd);
else
delete per_bfd;
I'm thinking that obfd could be non-nullptr, and it could require
relocation. In that case, it would never be freed. Anyway, that's not
really connected to this patch.
gdb/ChangeLog:
* objfiles.c (get_objfile_bfd_data): Remove objfile parameter,
adjust callers.
Change-Id: Ifa3158074ea6b42686780ba09d0c964b0cf14cf1
Since partial_symtab is supposed to be objfile-independent (since series
[1]), I think it would make sense for partial_symtab to not take an
objfile as a parameter in its constructor.
This patch replaces that parameter with an objfile_per_bfd_storage
parameter.
The objfile is used for two things:
- to get the objfile_name, for debug messages. We can get that name
from the bfd instead.
- to intern the partial symtab filename. Even though it goes through
an objfile method, the request is actually forwarded to the
underlying objfile_per_bfd_storage. So we can ask the new
objfile_per_bfd_storage instead.
In order to get a reference to the BFD from the objfile_per_bfd_storage,
the BFD is saved in the objfile_per_bfd_storage object.
[1] https://sourceware.org/pipermail/gdb-patches/2021-February/176625.html
gdb/ChangeLog:
* psympriv.h (struct partial_symtab) <partial_symtab>: Change
objfile parameter for objfile_per_bfd_storage, adjust callers.
(struct standard_psymtab) <standard_psymtab>: Likewise.
(struct legacy_psymtab) <legacy_psymtab>: Likewise.
* psymtab.c (partial_symtab::partial_symtab): Likewise.
* ctfread.c (struct ctf_psymtab): Likewise.
* dwarf2/read.h (struct dwarf2_psymtab): Likewise.
* dwarf2/read.c (struct dwarf2_include_psymtab): Likewise.
(dwarf2_create_include_psymtab): Likewise.
* objfiles.h (struct objfile_per_bfd_storage)
<objfile_per_bfd_storage>: Add bfd parameter, adjust callers.
<get_bfd>: New method.
<m_bfd>: New field.
* objfiles.c (get_objfile_bfd_data): Adjust.
Change-Id: I2ed3ab5d2e6f27d034bd4dc26ae2fae7b0b8a2b9
This changes objfile::qf to be a forward_list, and then updates all
the uses to iterate over the list. Note that there is still only ever
a single element in the list; this is handled by clearing the list
whenever an object is added.
gdb/ChangeLog
2021-03-20 Tom Tromey <tom@tromey.com>
* dwarf2/read.c (dwarf2_build_psymtabs): Update.
* symfile.c (syms_from_objfile_1, reread_symbols): Update.
* symfile-debug.c (objfile::has_partial_symbols)
(objfile::find_last_source_symtab)
(objfile::forget_cached_source_info)
(objfile::map_symtabs_matching_filename, objfile::lookup_symbol)
(objfile::print_stats, objfile::dump)
(objfile::expand_symtabs_for_function)
(objfile::expand_all_symtabs)
(objfile::expand_symtabs_with_fullname)
(objfile::map_matching_symbols)
(objfile::expand_symtabs_matching)
(objfile::find_pc_sect_compunit_symtab)
(objfile::map_symbol_filenames)
(objfile::find_compunit_symtab_by_address)
(objfile::lookup_global_symbol_language)
(objfile::require_partial_symbols): Update.
* psymtab.c (maintenance_print_psymbols)
(maintenance_info_psymtabs, maintenance_check_psymtabs): Update.
* objfiles.h (struct objfile) <qf>: Now a forward_list.
* objfiles.c (objfile_relocate1): Update.
* elfread.c (elf_symfile_read): Update.
This changes objfile so that it doesn't construct a psymtab_storage
object until the psymtab functions are installed. It also applies a
similar treatment to reread_symbols.
gdb/ChangeLog
2021-03-20 Tom Tromey <tom@tromey.com>
* symfile.c (syms_from_objfile_1): Call reset_psymtabs.
(reread_symbols): Move reset_psymtabs call later.
* objfiles.c (objfile::objfile): Don't initialize
partial_symtabs.
objfile::psymbol_map is used to implement a Rust feature. It is
currently specific to partial symbols -- it isn't used by the DWARF
indices.
This patch moves it out of objfile and into psymbol_functions, adding
a new method to quick_symbol_functions to handle the clearing case.
This is needed because the map holds unrelocated addresses.
gdb/ChangeLog
2021-03-20 Tom Tromey <tom@tromey.com>
* quick-symbol.h (struct quick_symbol_functions)
<relocated>: New method.
* psymtab.h (struct psymbol_functions) <relocated>: New
method.
<fill_psymbol_map>: Declare method.
<m_psymbol_map>: New member.
* psymtab.c (psymbol_functions::fill_psymbol_map): Rename.
(psymbol_functions::find_compunit_symtab_by_address): Update.
* objfiles.h (reset_psymtabs): Don't clear psymbol_map.
(struct objfile) <psymbol_map>: Remove.
* objfiles.c (objfile_relocate1): Update.
This introduces wrappers for each function in quick_symbol_functions.
The wrappers are methods on objfile, and are defined in
symfile-debug.c, so that they can use the symfile_debug variable.
Places that call the quick functions are all updated to call these new
wrapper methods.
gdb/ChangeLog
2021-03-20 Tom Tromey <tom@tromey.com>
* symtab.c (iterate_over_symtabs, expand_symtab_containing_pc)
(lookup_symbol_via_quick_fns, find_quick_global_symbol_language)
(basic_lookup_transparent_type_quick)
(find_pc_sect_compunit_symtab, find_symbol_at_address)
(find_line_symtab, global_symbol_searcher::expand_symtabs):
Update.
* symmisc.c (print_objfile_statistics, dump_objfile)
(maintenance_expand_symtabs): Update.
* symfile.c (symbol_file_add_with_addrs)
(expand_symtabs_matching, map_symbol_filenames): Update.
* symfile-debug.c (objfile::has_partial_symbols)
(objfile::find_last_source_symtab)
(objfile::forget_cached_source_info)
(objfile::map_symtabs_matching_filename, objfile::lookup_symbol)
(objfile::print_stats, objfile::dump)
(objfile::expand_symtabs_for_function)
(objfile::expand_all_symtabs)
(objfile::expand_symtabs_with_fullname)
(objfile::map_matching_symbols)
(objfile::expand_symtabs_matching)
(objfile::find_pc_sect_compunit_symtab)
(objfile::map_symbol_filenames)
(objfile::find_compunit_symtab_by_address)
(objfile::lookup_global_symbol_language): New methods.
(debug_sym_quick_functions): Remove.
(debug_sym_fns, install_symfile_debug_logging): Update.
* source.c (forget_cached_source_info_for_objfile)
(select_source_symtab): Update.
* objfiles.h (struct objfile): Add methods corresponding to
quick_symbol_functions.
* objfiles.c (objfile::has_partial_symbols): Move to
symfile-debug.c.
* linespec.c (iterate_over_all_matching_symtabs): Update.
* cp-support.c (add_symbol_overload_list_qualified): Update.
* ada-lang.c (add_nonlocal_symbols): Update.
This changes objfile_has_partial_symbols to be a method on objfile.
There are some other functions that could benefit from this sort of
change, but this was the only one that was relevant to this series.
gdb/ChangeLog
2021-03-20 Tom Tromey <tom@tromey.com>
* symfile.c (read_symbols): Update.
* objfiles.h (struct objfile) <has_partial_symbols>: New method.
(objfile_has_partial_symbols): Don't declare.
* objfiles.c (objfile::has_partial_symbols): Rename from
objfile_has_partial_symbols.
(objfile_has_symbols, have_partial_symbols): Update.
* elfread.c (elf_symfile_read): Update.
* dwarf2/read.c (dwarf2_has_info): Update.
* coffread.c (coff_symfile_read): Update.
Delete two more symbol/section related macros. This time it's
SYMBOL_SECTION and MSYMBOL_SECTION.
As with general_symbol_info::m_name it is not currently possible to
make general_symbol_info::m_section private as general_symbol_info
must remain a POD type.
But other than failing to make the new m_section private, this change
does what you'd expect, adds a get and set member function and updates
all users to use the new functions instead of the previous wrapper
macros.
There should be no user visible change after this commit.
gdb/ChangeLog:
* coff-pe-read.c (add_pe_forwarded_sym): Make use of section_index
and set_section_index member functions where appropriate.
* coffread.c (coff_symtab_read): Likewise.
(process_coff_symbol): Likewise.
* ctfread.c (set_symbol_address): Likewise.
* dwarf2/read.c (add_partial_symbol): Likewise.
(var_decode_location): Likewise.
* language.c: Likewise.
* minsyms.c (minimal_symbol_reader::record_full): Likewise.
(compact_minimal_symbols): Likewise.
(minimal_symbol_upper_bound): Likewise.
* objfiles.c (relocate_one_symbol): Likewise.
* psympriv.h (partial_symbol::obj_section): Likewise.
(partial_symbol::address): Likewise.
* psymtab.c (partial_symtab::add_psymbol): Likewise.
* stabsread.c (scan_file_globals): Likewise.
* symmisc.c (dump_msymbols): Likewise.
* symtab.c (general_symbol_info::obj_section): Likewise.
(fixup_section): Likewise.
(get_msymbol_address): Likewise.
* symtab.h (general_symbol_info::section): Rename to...
(general_symbol_info::m_section): ...this.
(general_symbol_info::set_section_index): New member function.
(general_symbol_info::section_index): Likewise.
(SYMBOL_SECTION): Delete.
(MSYMBOL_VALUE_ADDRESS): Make use of section_index and
set_section_index member functions where appropriate.
(MSYMBOL_SECTION): Delete.
(symbol::symbol): Update to initialize 'm_section'.
* xcoffread.c (read_xcoff_symtab): Make use of set_section_index.
(process_xcoff_symbol): Likewise.
This commits the result of running gdb/copyright.py as per our Start
of New Year procedure...
gdb/ChangeLog
Update copyright year range in copyright header of all GDB files.
This changes build_objfile_section_table to avoid
bfd_map_over_sections, in favor of iteration.
gdb/ChangeLog
2020-09-19 Tom Tromey <tom@tromey.com>
* objfiles.c (add_to_objfile_sections): Rename from
add_to_objfile_sections_full.
(add_to_objfile_sections): Remove.
(build_objfile_section_table): Use foreach.
This changes objfiles to be managed using a shared_ptr. shared_ptr is
chosen because it enables the use of objfiles in background threads.
The simplest way to do this was to introduce a new iterator that will
return the underlying objfile, rather than a shared_ptr. (I also
tried changing the rest of gdb to use shared_ptr, but this was quite
large; and to using intrusive reference counting, but this also was
tricky.)
gdb/ChangeLog
2019-12-12 Tom Tromey <tom@tromey.com>
* progspace.h (objfile_list): New typedef.
(class unwrapping_objfile_iterator)
(struct unwrapping_objfile_range): Newl
(struct program_space) <objfiles_range>: Change type.
<objfiles>: Change return type.
<add_objfile>: Change type of "objfile" parameter.
<objfiles_list>: Now a list of shared_ptr.
* progspace.c (program_space::add_objfile): Change type of
"objfile". Update.
(program_space::remove_objfile): Update.
* objfiles.h (struct objfile) <~objfile>: Make public.
* objfiles.c (objfile::make): Update.
(objfile::unlink): Don't call delete.
Change-Id: I6fb7fbf06efb7cb7474c525908365863eae27eb3
This changes free_all_objfiles to be a method on program_space, in
line with the other changes to treat program_space as a container for
objfiles.
gdb/ChangeLog
2019-12-12 Tom Tromey <tom@tromey.com>
* symfile.c (symbol_file_clear): Update.
* progspace.h (struct program_space) <free_all_objfiles>: Declare
method.
* progspace.c (program_space::free_all_objfiles): New method.
* objfiles.h (free_all_objfiles): Don't declare.
* objfiles.c (free_all_objfiles): Move to program_space.
Change-Id: I908b549d2981b6005f7ca181fc0e6d24fc8b7b6f
This introduces a new method, program_space::remove_objfile, and
changes the objfile destructor not to unlink an objfile from the
program space's list.
This is cleaner because, like the previous patch, it treats the
program space more like a container for objfiles. Also, this makes it
possible to keep an objfile alive even though it has been unlinked
from the program space's list, which is important for processing in a
worker thread.
gdb/ChangeLog
2019-12-12 Tom Tromey <tom@tromey.com>
* progspace.h (struct program_space) <remove_objfile>: Declare.
* progspace.c (program_space::remove_objfile): New method.
* objfiles.c (unlink_objfile): Remove.
(objfile::unlink): Call remove_objfile.
(objfile): Don't call unlink_objfile.
Change-Id: I22f768827723dce21886fae9b3664532c8349e68
This introduces a new method, program_space::add_objfile, that adds an
objfile to the program space's list of objfiles. It also changes the
obfile's constructor so that linking an objfile into this list is not
done here.
The former is an improvement because it makes more sense to treat the
program space as a container holding objfiles -- so manipulation of
the list belongs there.
The latter is not strictly needed, but seemed better both because it
is removing a global side effect from a constructor, and for symmetry
reasons, as a subsequent patch will remove unlinking from the
destructor.
gdb/ChangeLog
2019-12-12 Tom Tromey <tom@tromey.com>
* progspace.h (struct program_space) <add_objfile>: Declare
method.
* progspace.c (program_space::add_objfile): New method.
* objfiles.c (~objfile): Don't unlink objfile.
(put_objfile_before): Remove.
(add_separate_debug_objfile): Don't call put_objfile_before.
(objfile::make): Call add_objfile. Set new_objfiles_available on
the per-program-space data.
Change-Id: I93e8525dda631cb89dcc2046a5c51c7c9f34ccfd
The idea behind this is that, in the long run, some code will need to
be able to hold onto an objfile after it is unlinked from the program
space. In particular, this is needed for some functionality to be
moved to worker threads -- otherwise the objfile can be deleted while
still in use.
So, this makes ~objfile private, replacing it with an "unlink" method,
making it more obvious which operation is intended at the calling
points.
gdb/ChangeLog
2019-12-12 Tom Tromey <tom@tromey.com>
* symfile.c (syms_from_objfile_1): Use objfile_up.
(syms_from_objfile_1, remove_symbol_file_command): Call unlink
method.
(reread_symbols): Use objfile_up.
* solib.c (update_solib_list, reload_shared_libraries_1): Call
unlink method.
* objfiles.h (struct objfile) <~objfile>: Now private.
<unlink>: New method.
(struct objfile_deleter): New.
(objfile_up): New typedef.
* objfiles.c (objfile::unlink): New method.
(free_objfile_separate_debug, free_all_objfiles)
(objfile_purge_solibs): Use it.
* jit.c (jit_unregister_code): Remove.
(jit_inferior_exit_hook, jit_event_handler): Call unlink on
objfile.
* compile/compile-object-run.c (do_module_cleanup): Call unlink on
objfile.
* compile/compile-object-load.c (compile_object_load): Use
objfile_up.
Change-Id: I934bee70b26b8b24e1735828fb1e60fe8a05714f