]> git.ipfire.org Git - thirdparty/binutils-gdb.git/log
thirdparty/binutils-gdb.git
7 weeks agolibctf: fix a debugging typo
Nick Alcock [Tue, 2 Apr 2024 15:09:11 +0000 (16:09 +0100)] 
libctf: fix a debugging typo

libctf/

* ctf-lookup.c (ctf_symidx_sort): Fix a debugging typo.

7 weeks agolibctf: make ctf_lookup of symbols by name work in more cases
Nick Alcock [Tue, 2 Apr 2024 15:06:50 +0000 (16:06 +0100)] 
libctf: make ctf_lookup of symbols by name work in more cases

In particular, we don't need a symbol table if we're looking up a
symbol by name and that type of symbol has an indexed symtypetab,
since in that case we get the name from the symtypetab index, not
from the symbol table.

This lets you do symbol lookups in unlinked object files and unlinked
dicts written out via libctf's writeout functions.

libctf/

* ctf-lookup.c (ctf_lookup_by_sym_or_name): Allow lookups
by index even when there is no symtab.

7 weeks agolibctf: improve handling of type dumping errors
Nick Alcock [Tue, 2 Apr 2024 12:48:13 +0000 (13:48 +0100)] 
libctf: improve handling of type dumping errors

When dumping a type fails with an error, we want to emit a warning noting
this: a warning because it's not fatal and we can continue.  But warnings
don't automatically print out the ctf_errno (because not all cases causing
warnings set the errno at all), so we must do it at warning-emission time or
lose track of what's gone wrong.

libctf/

* ctf-dump.c (ctf_dump_format_type): Dump the underlying error on
type dump failure.

7 weeks agolibctf: fix tiny dumping error
Nick Alcock [Tue, 2 Apr 2024 12:46:53 +0000 (13:46 +0100)] 
libctf: fix tiny dumping error

Without this, you might get things like this in the output:

    Flags: 0xa (CTF_F_NEWFUNCINFO, , CTF_F_DYNSTR)

Note the spurious comma.

libctf/
* ctf-dump.c (ctf_dump_header): Fix comma emission.

7 weeks agolibctf: make ctf_serialize() actually serialize
Nick Alcock [Tue, 26 Mar 2024 13:04:20 +0000 (13:04 +0000)] 
libctf: make ctf_serialize() actually serialize

ctf_serialize() evolved from the old ctf_update(), which mutated the
in-memory CTF dict to make all the dynamic in-memory types into static,
unchanging written-to-the-dict types (by deserializing and reserializing
it): back in the days when you could only do type lookups on static types,
this meant you could see all the types you added recently, at the small,
small cost of making it impossible to change those older types ever again
and inducing an amortized O(n^2) cost if you actually wanted to add
references to types you added at arbitrary times to later types.

It also reset things so that ctf_discard() would throw away only types you
added after the most recent ctf_update() call.

Some time ago this was all changed so that you could look up dynamic types
just as easily as static types: ctf_update() changed so that only its
visible side-effect of affecting ctf_discard() remained: the old
ctf_update() was renamed to ctf_serialize(), made internal to libctf, and
called from the various functions that wrote files out.

... but it was still working by serializing and deserializing the entire
dict, swapping out its guts with the newly-serialized copy in an invasive
and horrible fashion that coupled ctf_serialize() to almost every field in
the ctf_dict_t.  This is totally useless, and fixing it is easy: just rip
all that code out and have ctf_serialize return a serialized representation,
and let everything use that directly.  This simplifies most of its callers
significantly.

(It also points up another bug: ctf_gzwrite() failed to call ctf_serialize()
at all, so it would only ever work for a dict you just ctf_write_mem()ed
yourself, just for its invisible side-effect of serializing the dict!)

This lets us simplify away a bunch of internal-only open-side functionality
for overriding the syn_ext_strtab and some just-added functionality for
forcing in an existing atoms table, without loss of functionality, and lets
us lift the restriction on reserializing a dict that was ctf_open()ed rather
than being ctf_create()d: it's now perfectly OK to open a dict, modify it
(except for adding members to existing structs, unions, or enums, which
fails with -ECTF_RDONLY), and write it out again, just as one would expect.

libctf/

* ctf-serialize.c (ctf_symtypetab_sect_sizes): Fix typos.
(ctf_type_sect_size): Add static type sizes too.
(ctf_serialize): Return the new dict rather than updating the
existing dict.  No longer fail for dicts with static types;
copy them onto the start of the new types table.
(ctf_gzwrite): Actually serialize before gzwriting.
(ctf_write_mem): Improve forced (test-mode) endian-flipping:
flip dicts even if they are too small to be compressed.
Improve confusing variable naming.
* ctf-archive.c (arc_write_one_ctf): Don't bother to call
ctf_serialize: both the functions we call do so.
* ctf-string.c (ctf_str_create_atoms): Drop serializing case
(atoms arg).
* ctf-open.c (ctf_simple_open): Call ctf_bufopen directly.
(ctf_simple_open_internal): Delete.
(ctf_bufopen_internal): Delete/rename to ctf_bufopen: no
longer bother with syn_ext_strtab or forced atoms table,
serialization no longer needs them.
* ctf-create.c (ctf_create): Call ctf_bufopen directly.
* ctf-impl.h (ctf_str_create_atoms): Drop atoms arg.
(ctf_simple_open_internal): Delete.
(ctf_bufopen_internal): Likewise.
(ctf_serialize): Adjust.
* testsuite/libctf-lookup/add-to-opened.c: Adjust now that
this is supposed to work.

7 weeks agolibctf: rethink strtab writeout
Nick Alcock [Mon, 25 Mar 2024 19:07:43 +0000 (19:07 +0000)] 
libctf: rethink strtab writeout

This commit finally adjusts strtab writeout so that repeated writeouts, or
writeouts of a dict that was read in earlier, only sorts the portion of the
strtab that was newly added.

There are three intertwined changes here:

 - pull the contents of strtabs from newly ctf_bufopened dicts into the
   atoms table, so that future additions will reuse the existing offset etc
   rather than adding new identical strings
 - allow the internal ctf_bufopen done by serialization to contribute its
   existing atoms table, so that existing atoms can be used for the
   remainder of the open process (like name table construction): this atoms
   table currente gets thrown away in the mass reassignment done later in
   ctf_serialize in any case, but it needs to be there during the open.
 - rewrite ctf_str_write_strtab so that a) it uses iterators rather than
   ctf_*_iter, reducing pointless structures which serve no other purpose
   than to implement ordinary variable scope, but more clunkily, and b)
   retains the existing strtab on the front of the new one, with its sort
   retained, rather than resorting, so all existing already-written strtab
   offsets remain valid across the call.

This latter change finally permits repeated serializations, and
reserializations of ctf_open()ed dicts, to work, but for now we keep the
code that prevents that because serialization is about to change again in a
way that will make it more obvious that doing such things is safe, and we
can take it out then.

(There are also some smaller changes like moving the purge of the refs table
into ctf_str_write_strtab(), since that's where the changes happen that
invalidate it, rather than doing it in ctf_serialize().  We also prohibit
something that has never worked, opening a dict and then reporting symbols
to it via ctf_link_add_strtab() et al: you must do that to newly-created
dicts which have had stuff ctf_link()ed into them.  This is very unlikely
ever to be a problem in practice: linkers just don't do that sort of thing.)

libctf/

* ctf-create.c (ctf_create): Add (temporary) atoms arg.
* ctf-impl.h (struct ctf_dict.ctf_dynstrtab): New.
(ctf_str_create_atoms): Adjust.
(ctf_str_write_strtab): Likewise.
(ctf_simple_open_internal): Likewise.
* ctf-open.c (ctf_simple_open_internal): Add atoms arg.
(ctf_bufopen): Likewise.
(ctf_bufopen_internal): Initialize just enough of an
atoms table: pre-init from the atoms arg if supplied.
(ctf_simple_open): Adjust.
* ctf-serialize.c (ctf_serialize): Constify the strtab.
Move ref list purging into ctf_str_write_strtab.
Initialize the new dict with the old dict's atoms table.
Accept the new strtab from ctf_str_write_strtab.
Adjust for addition of ctf_dynstrtab.
* ctf-string.c (ctf_strraw_explicit): Improve comments.
(ctf_str_create_atoms): Prepopulate from an existing atoms table,
or alternatively pull in all strings from the strtab and turn
them into atoms.
(ctf_str_free_atoms): Free the dynstrtab and its strtab.
(struct ctf_strtab_write_state): Remove.
(ctf_str_count_strtab): Fold this...
(ctf_str_populate_sorttab): ... and this...
(ctf_str_write_strtab): ... into this.  Prepend existing strings
to the strtab rather than resorting them (and wrecking their
offsets).  Keep the dynstrtab updated.  Update refs for all
atoms with refs, whether or not they are strings newly added
to the strtab.

7 weeks agolibctf: replace 'pending refs' abstraction
Nick Alcock [Mon, 25 Mar 2024 16:39:02 +0000 (16:39 +0000)] 
libctf: replace 'pending refs' abstraction

A few years ago we introduced a 'pending refs' abstraction to fix one
problem: serializing a dict, then changing it would tend to corrupt the dict
because the strtab sort we do on strtab writeout (to improve compression
efficiency) would modify the offset of any strings that sorted
lexicographically earlier in the strtab: so we added a new restriction that
all strings are added only at serialization time, and maintained a set of
'pending' refs that were added earlier, whose offsets we could update (like
other refs) at writeout time.

This was in hindsight seriously problematic for maintenance (because
serialization has to traverse all strings in all datatypes in the entire
dict), and has become impossible to sustain now that we can read in existing
dicts, modify them, and reserialize them again.  We really don't want to
have to dig through the entire dict we jut read in just in order to dig out
all its strtab offsets, then *change* it, just for the sake of a sort that
adds a frankly trivial amount of compression efficiency.

Sorting *is* still worthwhile -- but it sacrifices very little to only sort
newly-added portions of the strtab, reusing older portions as necessary.
As a first stage in this, discard the whole "pending refs" abstraction and
replace it with "movable" refs, which are exactly like all other refs
(addresses containing the strtab offset of some string, which are updated
wiht the final strtab offset on serialization) except that we track them in
a reverse dict so that we can move the refs around (which we do whenever we
realloc() a buffer containing a bunch of structure members or something when
we add members to the structure).

libctf/

* ctf-create.c (ctf_add_enumerator): Call ctf_str_move_refs; add
        a movable ref.
(ctf_add_member_offset): Likewise.
* ctf-util.c (ctf_realloc): Delete.
* ctf-serialize.c (ctf_serialize): No longer use it.  Adjust to
new fields.
* ctf-string.c (ctf_str_purge_atom_refs): Purge movable refs.
(ctf_str_free_atom): Free freeable atoms' strings.
(ctf_str_create_atoms): Create the movable refs dynhash if needed.
(ctf_str_free_atoms): Destroy it.
(CTF_STR_MOVABLE): Switch (back) from ints to flags (see previous
reversion).  Add new flag.
(aref_create):  New, populate movable refs if need be.
(ctf_str_add_ref_internal): Switch back to flags, update refs
directly for nonprovisional strings (with already-known fixed offsets);
create refs via aref_create.  Allocate strings only if not within an
mmapped strtab.
(ctf_str_add_movable_ref): New.
(ctf_str_add): Adjust to CTF_STR_* reintroduction.
(ctf_str_add_external): LIkewise.
(ctf_str_move_refs): New, move refs via ctf_str_movable_refs
backpointer.
(ctf_str_purge_refs): Drop ctf_str_num_refs.
(ctf_str_update_refs): Fix indentation.
* ctf-impl.h (struct ctf_str_atom_movable): New.
(struct ctf_dict.ctf_str_num_refs): Drop.
(struct ctf_dict.ctf_str_movable_refs): New.
(ctf_str_add_movable_ref): Declare.
(ctf_str_move_refs): Likewise.
(ctf_realloc): Drop.

7 weeks agoRevert "libctf: do not corrupt strings across ctf_serialize"
Nick Alcock [Tue, 30 Jan 2024 13:40:56 +0000 (13:40 +0000)] 
Revert "libctf: do not corrupt strings across ctf_serialize"

This reverts commit 986e9e3aa03f854bedacef7fac38fe8f009a416c.

(We do not revert the testcase -- it remains valid -- but we are
taking a different, less complex and more robust approach.)

This also deletes the pending refs abstraction without (yet)
replacing it, so some tests will fail for a commit or two.

7 weeks agolibctf: rename ctf_dict.ctf_{symtab,strtab}
Nick Alcock [Fri, 5 Jan 2024 12:17:27 +0000 (12:17 +0000)] 
libctf: rename ctf_dict.ctf_{symtab,strtab}

These two fields are constantly confusing because CTF dicts contain both
a symtypetab and strtab, but these fields are not that: they are the
symtab and strtab from the ELF file.  We have enough string tables now
(internal, external, synthetic external, dynamic) that we need to at
least name them better than this to avoid getting totally confused.
Rename them to ctf_ext_symtab and ctf_ext_strtab.

libctf/

* ctf-dump.c (ctf_dump_objts): Rename ctf_symtab -> ctf_ext_symtab.
* ctf-impl.h (struct ctf_dict.ctf_symtab): Rename to...
(struct ctf_dict.ctf_ext_strtab): ... this.
(struct ctf_dict.ctf_strtab): Rename to...
(struct ctf_dict.ctf_ext_strtab): ... this.
* ctf-lookup.c (ctf_lookup_symbol_name): Adapt.
(ctf_lookup_symbol_idx): Adapt.
(ctf_lookup_by_sym_or_name): Adapt.
* ctf-open.c (ctf_bufopen_internal): Adapt.
(ctf_dict_close): Adapt.
(ctf_getsymsect): Adapt.
(ctf_getstrsect): Adapt.
(ctf_symsect_endianness): Adapt.

7 weeks agolibctf: fix a comment typo
Nick Alcock [Fri, 5 Jan 2024 11:12:37 +0000 (11:12 +0000)] 
libctf: fix a comment typo

ctf_update has been called ctf_serialize for years now.

libctf/

* ctf-impl.h: Fix comment typo.

7 weeks agolibctf: delete LCTF_DIRTY
Nick Alcock [Fri, 5 Jan 2024 11:09:01 +0000 (11:09 +0000)] 
libctf: delete LCTF_DIRTY

This flag was meant as an optimization to avoid reserializing dicts
unnecessarily.  It was critically necessary back when serialization was
done by ctf_update() and you had to call that every time you wanted any
new modifications to the type table to be usable by other types, but
that has been unnecessary for years now, and serialization is only done
once when writing out, which one would naturally assume would always
serialize the dict.  Worse, it never really worked: it only tracked
newly-added types, not things like added symbols which might equally
well require reserialization, and it gets in the way of an upcoming
change.  Delete entirely.

libctf/

* ctf-create.c (ctf_create): Drop LCTF_DIRTY.
(ctf_discard): Likewise.
(ctf_rollback): Likewise.
(ctf_add_generic): Likewise.
(ctf_set_array): Likewise.
(ctf_add_enumerator): Likewise.
(ctf_add_member_offset): Likewise.
(ctf_add_variable_forced): Likewise.
* ctf-link.c (ctf_link_intern_extern_string): Likewise.
(ctf_link_add_strtab): Likewise.
* ctf-serialize.c (ctf_serialize): Likewise.
* ctf-impl.h (LCTF_DIRTY): Likewise.
(LCTF_LINKING): Renumber.

7 weeks agolibctf: fix a comment
Nick Alcock [Tue, 19 Dec 2023 17:14:35 +0000 (17:14 +0000)] 
libctf: fix a comment

A mistaken "not" in ctf_err_warn made it seem like we only extracted
error messages if this was not an error.

libctf/

* ctf-subr.c (ctf_err_warn): Fix comment.

7 weeks agolibctf: support addition of types to dicts read via ctf_open()
Nick Alcock [Tue, 19 Dec 2023 16:58:19 +0000 (16:58 +0000)] 
libctf: support addition of types to dicts read via ctf_open()

libctf has long declared deserialized dictionaries (out of files or ELF
sections or memory buffers or whatever) to be read-only: back in the
furthest prehistory this was not the case, in that you could add a
few sorts of type to such dicts, but attempting to do so often caused
horrible memory corruption, so I banned the lot.

But it turns out real consumers want it (notably DTrace, which
synthesises pointers to types that don't have them and adds them to the
ctf_open()ed dicts if it needs them). Let's bring it back again, but
without the memory corruption and without the massive code duplication
required in days of yore to distinguish between static and dynamic
types: the representation of both types has been identical for a few
years, with the only difference being that types as a whole are stored in
a big buffer for types read in via ctf_open and per-type hashtables for
newly-added types.

So we discard the internally-visible concept of "readonly dictionaries"
in favour of declaring the *range of types* that were already present
when the dict was read in to be read-only: you can't modify them (say,
by adding members to them if they're structs, or calling ctf_set_array
on them), but you can add more types and point to them.  (The API
remains the same, with calls sometimes returning ECTF_RDONLY, but now
they do so less often.)

This is a fairly invasive change, mostly because code written since the
ban was introduced didn't take the possibility of a static/dynamic split
into account.  Some of these irregularities were hard to define as
anything but bugs.

Notably:

 - The symbol handling was assuming that symbols only needed to be
   looked for in dynamic hashtabs or static linker-laid-out indexed/
   nonindexed layouts, but now we want to check both in case people
   added more symbols to a dict they opened.

 - The code that handles type additions wasn't checking to see if types
   with the same name existed *at all* (so you could do
   ctf_add_typedef (fp, "foo", bar) repeatedly without error).  This
   seems reasonable for types you just added, but we probably *do* want
   to ban addition of types with names that override names we already
   used in the ctf_open()ed portion, since that would probably corrupt
   existing type relationships.  (Doing things this way also avoids
   causing new errors for any existing code that was doing this sort of
   thing.)

 - ctf_lookup_variable entirely failed to work for variables just added
   by ctf_add_variable: you had to write the dict out and read it back
   in again before they appeared.

 - The symbol handling remembered what symbols you looked up but didn't
   remember their types, so you could look up an object symbol and then
   find it popping up when you asked for function symbols, which seems
   less than ideal.  Since we had to rejig things enough to be able to
   distinguish function and object symbols internally anyway (in order
   to give suitable errors if you try to add a symbol with a name that
   already existed in the ctf_open()ed dict), this bug suddenly became
   more visible and was easily fixed.

We do not (yet) support writing out dicts that have been previously read
in via ctf_open() or other deserializer (you can look things up in them,
but not write them out a second time).  This never worked, so there is
no incompatibility; if it is needed at a later date, the serializer is a
little bit closer to having it work now (the only table we don't deal
with is the types table, and that's because the upcoming CTFv4 changes
are likely to make major changes to the way that table is represented
internally, so adding more code that depends on its current form seems
like a bad idea).

There is a new testcase that tests much of this, in particular that
modification of existing types is still banned and that you can add new
ones and chase them without error.

libctf/

* ctf-impl.h (struct ctf_dict.ctf_symhash): Split into...
(ctf_dict.ctf_symhash_func): ... this and...
(ctf_dict.ctf_symhash_objt): ... this.
(ctf_dict.ctf_stypes): New, counts static types.
(LCTF_INDEX_TO_TYPEPTR): Use it instead of CTF_RDWR.
(LCTF_RDWR): Deleted.
(LCTF_DIRTY): Renumbered.
(LCTF_LINKING): Likewise.
(ctf_lookup_variable_here): New.
(ctf_lookup_by_sym_or_name): Likewise.
(ctf_symbol_next_static): Likewise.
(ctf_add_variable_forced): Likewise.
(ctf_add_funcobjt_sym_forced): Likewise.
(ctf_simple_open_internal): Adjust.
(ctf_bufopen_internal): Likewise.
* ctf-create.c (ctf_grow_ptrtab): Adjust a lot to start with.
(ctf_create): Migrate a bunch of initializations into bufopen.
Force recreation of name tables.  Do not forcibly override the
model, let ctf_bufopen do it.
(ctf_static_type): New.
(ctf_update): Drop LCTF_RDWR check.
(ctf_dynamic_type): Likewise.
(ctf_add_function): Likewise.
(ctf_add_type_internal): Likewise.
(ctf_rollback): Check ctf_stypes, not LCTF_RDWR.
(ctf_set_array): Likewise.
(ctf_add_struct_sized): Likewise.
(ctf_add_union_sized): Likewise.
(ctf_add_enum): Likewise.
(ctf_add_enumerator): Likewise (only on the target dict).
(ctf_add_member_offset): Likewise.
(ctf_add_generic): Drop LCTF_RDWR check.  Ban addition of types
with colliding names.
(ctf_add_forward): Note safety under the new rules.
(ctf_add_variable): Split all but the existence check into...
(ctf_add_variable_forced): ... this new function.
(ctf_add_funcobjt_sym): Likewise...
(ctf_add_funcobjt_sym_forced): ... for this new function.
* ctf-link.c (ctf_link_add_linker_symbol): Ban calling on dicts
with any stypes.
(ctf_link_add_strtab): Likewise.
(ctf_link_shuffle_syms): Likewise.
(ctf_link_intern_extern_string): Note pre-existing prohibition.
* ctf-lookup.c (ctf_lookup_by_id): Drop LCTF_RDWR check.
(ctf_lookup_variable): Split out looking in a dict but not
its parent into...
(ctf_lookup_variable_here): ... this new function.
(ctf_lookup_symbol_idx): Track whether looking up a function or
object: cache them separately.
(ctf_symbol_next): Split out looking in non-dynamic symtypetab
entries to...
(ctf_symbol_next_static): ... this new function.  Don't get confused
by the simultaneous presence of static and dynamic symtypetab entries.
(ctf_try_lookup_indexed):  Don't waste time looking up symbols by
index before there can be any idea how symbols are numbered.
(ctf_lookup_by_sym_or_name): Distinguish between function and
data object lookups.  Drop LCTF_RDWR.
(ctf_lookup_by_symbol): Adjust.
(ctf_lookup_by_symbol_name): Likewise.
* ctf-open.c (init_types): Rename to...
(init_static_types): ... this.  Drop LCTF_RDWR.  Populate ctf_stypes.
(ctf_simple_open): Drop writable arg.
(ctf_simple_open_internal): Likewise.
(ctf_bufopen): Likewise.
(ctf_bufopen_internal): Populate fields only used for writable dicts.
Drop LCTF_RDWR.
(ctf_dict_close): Cater for symhash cache split.
* ctf-serialize.c (ctf_serialize): Use ctf_stypes, not LCTF_RDWR.
* ctf-types.c (ctf_variable_next): Drop LCTF_RDWR.
* testsuite/libctf-lookup/add-to-opened*: New test.

7 weeks agolibctf: fix name lookup in dicts containing base-type bitfields
Nick Alcock [Mon, 18 Dec 2023 18:27:44 +0000 (18:27 +0000)] 
libctf: fix name lookup in dicts containing base-type bitfields

The intent of the name lookup code was for lookups to yield non-bitfield
basic types except if none existed with a given name, and only then
return bitfield types with that name.  Unfortunately, the code as
written only does this if the base type has a type ID higher than all
bitfield types, which is most unlikely (the opposite is almost always
the case).

Adjust it so that what ends up in the name table is the highest-width
zero-offset type with a given name, if any such exist, and failing that
the first type with that name we see, no matter its offset.  (We don't
define *which* bitfield type you get, after all, so we might as well
just stuff in the first we find.)

Reported by Stephen Brennan <stephen.brennan@oracle.com>.

libctf/

* ctf-open.c (init_types): Modify to allow some lookups during open;
detect bitfield name reuse and prefer less bitfieldy types.
* testsuite/libctf-writable/libctf-bitfield-name-lookup.*: New test.

7 weeks agolibctf: remove static/dynamic name lookup distinction
Nick Alcock [Mon, 18 Dec 2023 17:47:48 +0000 (17:47 +0000)] 
libctf: remove static/dynamic name lookup distinction

libctf internally maintains a set of hash tables for type name lookups,
one for each valid C type namespace (struct, union, enum, and everything
else).

Or, rather, it maintains *two* sets of hash tables: one, a ctf_hash *,
is meant for lookups in ctf_(buf)open()ed dicts with fixed content; the
other, a ctf_dynhash *, is meant for lookups in ctf_create()d dicts.

This distinction was somewhat valuable in the far pre-binutils past when
two different hashtable implementations were used (one expanding, the
other fixed-size), but those days are long gone: the hash table
implementations are almost identical, both wrappers around the libiberty
hashtab. The ctf_dynhash has many more capabilities than the ctf_hash
(iteration, deletion, etc etc) and has no downsides other than starting
at a fixed, arbitrary small size.

That limitation is easy to lift (via a new ctf_dynhash_create_sized()),
following which we can throw away nearly all the ctf_hash
implementation, and all the code to choose between readable and writable
hashtabs; the few convenience functions that are still useful (for
insertion of name -> type mappings) can also be generalized a bit so
that the extra string verification they do is potentially available to
other string lookups as well.

(libctf still has two hashtable implementations, ctf_dynhash, above,
and ctf_dynset, which is a key-only hashtab that can avoid a great many
malloc()s, used for high-volume applications in the deduplicator.)

libctf/

* ctf-create.c (ctf_create): Eliminate ctn_writable.
(ctf_dtd_insert): Likewise.
(ctf_dtd_delete): Likewise.
(ctf_rollback): Likewise.
(ctf_name_table): Eliminate ctf_names_t.
* ctf-hash.c (ctf_dynhash_create): Comment update.
        Reimplement in terms of...
(ctf_dynhash_create_sized): ... this new function.
(ctf_hash_create): Remove.
(ctf_hash_size): Remove.
(ctf_hash_define_type): Remove.
(ctf_hash_destroy): Remove.
(ctf_hash_lookup_type): Rename to...
(ctf_dynhash_lookup_type): ... this.
(ctf_hash_insert_type): Rename to...
(ctf_dynhash_insert_type): ... this, moving validation to...
* ctf-string.c (ctf_strptr_validate): ... this new function.
* ctf-impl.h (struct ctf_names): Extirpate.
(struct ctf_lookup.ctl_hash): Now a ctf_dynhash_t.
(struct ctf_dict): All ctf_names_t fields are now ctf_dynhash_t.
(ctf_name_table): Now returns a ctf_dynhash_t.
(ctf_lookup_by_rawhash): Remove.
(ctf_hash_create): Likewise.
(ctf_hash_insert_type): Likewise.
(ctf_hash_define_type): Likewise.
(ctf_hash_lookup_type): Likewise.
(ctf_hash_size): Likewise.
(ctf_hash_destroy): Likewise.
(ctf_dynhash_create_sized): New.
(ctf_dynhash_insert_type): New.
(ctf_dynhash_lookup_type): New.
(ctf_strptr_validate): New.
* ctf-lookup.c (ctf_lookup_by_name_internal): Adapt.
* ctf-open.c (init_types): Adapt.
(ctf_set_ctl_hashes): Adapt.
(ctf_dict_close): Adapt.
* ctf-serialize.c (ctf_serialize): Adapt.
* ctf-types.c (ctf_lookup_by_rawhash): Remove.

7 weeks agolibctf: don't leak the symbol name in the name->type cache
Nick Alcock [Wed, 13 Dec 2023 12:24:57 +0000 (12:24 +0000)] 
libctf: don't leak the symbol name in the name->type cache

This cache replaced a cache of symbol index->ctf_id_t. That cache was
just an array, so it could get away with just being free()d, but the
ctfi_symnamedicts cache that replaced it is a full dynhash with a
dynamically-allocated string as the key.  As such, it needs freeing with
ctf_dynhash_destroy(), not just free(), or we leak parts of the
underlying hashtab, and all the keys.

libctf/ChangeLog:

* ctf-archive.c (ctf_arc_flush_caches): Fix leak.

7 weeks agobinutils, objdump: Add --ctf-parent-section
Nick Alcock [Mon, 2 Oct 2023 14:37:17 +0000 (15:37 +0100)] 
binutils, objdump: Add --ctf-parent-section

This lets you examine CTF where the parent and child dicts are in entirely
different sections, rather than in a CTF archive with members with different
names.  The linker doesn't emit ELF objects structured like this, but some
third-party linkers may; it's also useful for objcopy-constructed files
in some cases.

(This is what the objdump --ctf-parent option used to do before commit
80b56fad5c99a8c9 in 2021.  The new semantics of that option are much more
useful, but that doesn't mean the old ones are never useful at all, so let's
bring them back.)

(I was specifically driven to add this by DTrace's obscure "ctypes" and
"dtypes" options, which dump its internal, dynamically-generated dicts out
to files for debugging purposes: there are two, one the parent of the other.
Since they're in two separate files rather than a CTF archive and we have no
tools that paste files together into archives, objdump wouldn't show them --
and even pasting them together into an ELF executable with objcopy didn't
help, since objdump had no options that could be used to look in specific
sections for the parent dict.  With --ctf-parent-section, this sort of
obscure use case becomes possible again.  You'll never need it for the
output of the normal linker.)

binutils/

* doc/ctf.options.texi: Add --ctf-parent-section=.
* objdump.c (dump_ctf): Implement it.
(dump_bfd): Likewise.
(main): Likewise.

7 weeks agogdb: Document qIsAddressTagged packet
Gustavo Romero [Thu, 18 Apr 2024 20:10:39 +0000 (20:10 +0000)] 
gdb: Document qIsAddressTagged packet

This commit documents the qIsAddressTagged packet.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Reviewed-by: Eli Zaretskii <eliz@gnu.org>
Approved-By: Eli Zaretskii <eliz@gnu.org>
7 weeks agogdb/testsuite: Add unit tests for qIsAddressTagged packet
Gustavo Romero [Thu, 18 Apr 2024 20:10:38 +0000 (20:10 +0000)] 
gdb/testsuite: Add unit tests for qIsAddressTagged packet

Add unit tests for testing qIsAddressTagged packet request creation and
reply checks.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agogdb: Add qIsAddressTagged packet
Gustavo Romero [Thu, 18 Apr 2024 20:10:37 +0000 (20:10 +0000)] 
gdb: Add qIsAddressTagged packet

This commit adds a new packet, qIsAddressTagged, allowing GDB remote
targets to use it to query the stub if a given address is tagged.

Currently, the memory tagging address check is done via a read query,
where the contents of /proc/<PID>/smaps is read and the flags are
inspected for memory tagging-related flags that indicate the address is
in a memory tagged region.

This is not ideal, for example, for QEMU stub and other cases, such as
on bare-metal, where there is no notion of an OS file like 'smaps.'
Hence, the introduction of qIsAddressTagged packet allows checking
if an address is tagged in an agnostic way.

The is_address_tagged target hook in remote.c attempts to use the
qIsAddressTagged packet first for checking if an address is tagged and
if the stub does not support such a packet (reply is empty) it falls
back to using the current mechanism that reads the contents of
/proc/<PID>/smaps via vFile requests.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agogdb: Introduce is_address_tagged target hook
Gustavo Romero [Thu, 18 Apr 2024 20:10:36 +0000 (20:10 +0000)] 
gdb: Introduce is_address_tagged target hook

This commit introduces a new target hook, target_is_address_tagged,
which is used instead of the gdbarch_tagged_address_p gdbarch hook in
the upper layer (printcmd.c).

This change enables easy specialization of memory tagging address
check per target in the future. As target_is_address_tagged continues
to utilize the gdbarch_tagged_address_p hook, there is no change in
behavior for all the targets that use the new target hook (i.e., the
remote.c, aarch64-linux-nat.c, and corelow.c targets).

Just the gdbarch_tagged_address_p signature is changed for convenience,
since target_is_address_tagged takes the address to be checked as a
CORE_ADDR type.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agogdb: Use passed gdbarch instead of calling current_inferior
Gustavo Romero [Thu, 18 Apr 2024 20:10:35 +0000 (20:10 +0000)] 
gdb: Use passed gdbarch instead of calling current_inferior

In do_examine function, use passed gdbarch when checking if an address
is tagged instead of calling current_inferior()->arch() to make the code
more localized and help modularity by not calling a current_* function,
which disguises the use of a global state/variable. There is no change
in the code behavior.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Suggested-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agogdb: aarch64: Remove MTE address checking from memtag_matches_p
Gustavo Romero [Thu, 18 Apr 2024 20:10:34 +0000 (20:10 +0000)] 
gdb: aarch64: Remove MTE address checking from memtag_matches_p

This commit removes aarch64_linux_tagged_address_p from
aarch64_linux_memtag_matches_p. aarch64_linux_tagged_address_p checks if
an address is tagged (MTE) or not.

The check is redundant because aarch64_linux_memtag_matches_p is always
called from the upper layers (i.e. from printcmd.c via gdbarch hook
gdbarch_memtag_matches_p) after either gdbarch_tagged_address_p (that
already points to aarch64_linux_tagged_address_p) has been called or
after should_validate_memtags (that calls gdbarch_tagged_address_p at
the end) has been called, so the address is already checked. Hence:

a) in print_command_1, gdbarch_memtag_matches_p is called only after
should_validate_memtags is called, which checks the address at its end;

b) in memory_tag_check_command, gdbarch_memtag_matches_p is called only
after gdbarch_tagged_address_p is called directly.

Also, because after this change the address checking only happens at the
upper layer it now allows the address checking to be specialized easily
per target, via a target hook.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agogdb: aarch64: Move MTE address check out of set_memtag
Gustavo Romero [Thu, 18 Apr 2024 20:10:33 +0000 (20:10 +0000)] 
gdb: aarch64: Move MTE address check out of set_memtag

Remove check in parse_set_allocation_tag_input as it is redundant:
currently the check happens at the end of parse_set_allocation_tag_input
and also in set_memtag (called after parse_set_allocation_tag_input).

After it, move MTE address check out of set_memtag and add this check to
the upper layer, before set_memtag is called.

This is a preparation for using a target hook instead of a gdbarch hook
on MTE address checks.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
7 weeks agogdb: aarch64: Remove MTE address checking from get_memtag
Gustavo Romero [Thu, 18 Apr 2024 20:10:32 +0000 (20:10 +0000)] 
gdb: aarch64: Remove MTE address checking from get_memtag

This commit removes aarch64_linux_tagged_address_p from
aarch64_linux_get_memtag. aarch64_linux_tagged_address_p checks if an
address is tagged (MTE) or not.

The check is redundant because aarch64_linux_get_memtag is always called
from the upper layers (i.e. from printcmd.c via gdbarch hook
gdbarch_get_memtag) after either gdbarch_tagged_address_p (that already
points to aarch64_linux_tagged_address_p) has been called or
after should_validate_memtags (that calls gdbarch_tagged_address_p at
the end) has been called, so the address is already checked. Hence:

a) in print_command_1, aarch64_linux_get_memtag (via gdbarch_get_memtag
hook) is called but only after should_validate_memtags, which calls
gdbarch_tagged_address_p;

b) in do_examine, aarch64_linux_get_memtag is also called only after
gdbarch_tagged_address_p is directly called;

c) in memory_tag_check_command, gdbarch_get_memtag is called -- tags
matching or not -- after the initial check via direct call to
gdbarch_tagged_address_p;

d) in memory_tag_print_tag_command, address is checked directly via
gdbarch_tagged_address_p before gdbarch_get_memtag is called.

Also, because after this change the address checking only happens at the
upper layer it now allows the address checking to be specialized easily
per target, via a target hook.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agoRe: elf: Strip unreferenced weak undefined symbols
Alan Modra [Fri, 19 Apr 2024 07:05:30 +0000 (16:35 +0930)] 
Re: elf: Strip unreferenced weak undefined symbols

PR ld/31652
* elflink.c (_bfd_elf_link_output_relocs): Don't segfault
on NULL rel_hash.

7 weeks agoelf: Strip unreferenced weak undefined symbols
H.J. Lu [Thu, 18 Apr 2024 12:28:56 +0000 (05:28 -0700)] 
elf: Strip unreferenced weak undefined symbols

Linker will resolve an undefined symbol only if it is referenced by
relocation.  Unreferenced weak undefined symbols serve no purpose.
Weak undefined symbols appear in the dynamic symbol table only when they
are referenced by dynamic relocation.  Mark symbols with relocation and
strip undefined weak symbols if they don't have relocation and aren't
in the dynamic symbol table.

bfd/

PR ld/31652
* elf-bfd.h (elf_link_hash_entry): Add has_reloc.
* elf-vxworks.c (elf_vxworks_emit_relocs): Set has_reloc.
* elflink.c (_bfd_elf_link_output_relocs): Likewise.
(elf_link_output_extsym): Strip undefined weak symbols if they
don't have relocation and aren't in the dynamic symbol table.

ld/

PR ld/31652
* testsuite/ld-elf/elf.exp: Run undefweak tests.
* testsuite/ld-elf/undefweak-1.rd: New file.
* testsuite/ld-elf/undefweak-1a.s: Likewise.
* testsuite/ld-elf/undefweak-1b.s: Likewise.
* testsuite/ld-x86-64/weakundef-1.nd: Likewise.
* testsuite/ld-x86-64/weakundef-1a.s: Likewise.
* testsuite/ld-x86-64/weakundef-1b.s: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run undefweak tests.

7 weeks agomemory leak in bfd/dwarf2.c
Alan Modra [Thu, 18 Apr 2024 23:40:25 +0000 (09:10 +0930)] 
memory leak in bfd/dwarf2.c

* dwarf2.c (_bfd_dwarf2_cleanup_debug_info): Free
dwarf_addr_buffer and dwarf_str_offsets_buffer.

7 weeks agommix disassemble memory leak
Alan Modra [Thu, 18 Apr 2024 23:34:16 +0000 (09:04 +0930)] 
mmix disassemble memory leak

It's a once-off and of no consequence, but fix it anyway.  The mmix
caonoicalize_syms array is an array of pointers.  Freeing it won't
lose symbol names.

* mmix-dis.c (initialize_mmix_dis_info): Free syms.

7 weeks agoAutomatic date update in version.in
GDB Administrator [Fri, 19 Apr 2024 00:00:11 +0000 (00:00 +0000)] 
Automatic date update in version.in

7 weeks ago[gdb/testsuite] Use find_gnatmake instead of gdb_find_gnatmake
Tom de Vries [Thu, 18 Apr 2024 15:02:13 +0000 (17:02 +0200)] 
[gdb/testsuite] Use find_gnatmake instead of gdb_find_gnatmake

On SLE-11, with an older dejagnu version, I ran into:
...
Running gdb.ada/mi_prot.exp ...
UNRESOLVED: gdb.ada/mi_prot.exp: \
  testcase aborted due to invalid command name: gdb_find_gnatmake
ERROR: tcl error sourcing gdb.ada/mi_prot.exp.
ERROR: invalid command name "gdb_find_gnatmake"
    while executing
"::gdb_tcl_unknown gdb_find_gnatmake"
    ("uplevel" body line 1)
    invoked from within
"uplevel 1 ::gdb_tcl_unknown $args"
    (procedure "::unknown" line 5)
    invoked from within
"gdb_find_gnatmake"
    (procedure "gnatmake_version_at_least" line 2)
    invoked from within
...

Proc gdb_find_gnatmake is actually a backup for find_gnatmake:
...
if {[info procs find_gnatmake] == ""} {
    rename gdb_find_gnatmake find_gnatmake
...
so gnatmake_version_at_least should use find_gnatmake instead.

For a recent dejagnu with find_gnatmake, gdb_find_gnatmake is kept, and we
don't run into this error.

For an older dejagnu without find_gnatmake, gdb_find_gnatmake is renamed to
find_gnatmake, and we do run into the error.

It's confusing that we're using the gdb_ prefix for gdb_find_gnatmake, it
seems something legitimate to use.  Maybe we should use future_ or gdb_future_
prefix instead to make this more clear, but I've left that alone for now.

Fix this by:
- triggering the same error with a recent dejagnu by removing
  gdb_find_gnatmake unless used (and likewise for other procs in future.exp),
  and
- using find_gnatmake in gnatmake_version_at_least.

Tested on x86_64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
PR testsuite/31647
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31647

7 weeks ago[gdb/testsuite] Use allocator_may_return_null=1 in two test-cases
Tom de Vries [Thu, 18 Apr 2024 15:01:09 +0000 (17:01 +0200)] 
[gdb/testsuite] Use allocator_may_return_null=1 in two test-cases

Simon reported [1] that recent commit 06e967dbc9b ("[gdb/python] Throw
MemoryError in inferior.read_memory if malloc fails") introduced
AddressSanitizer allocation-size-too-big errors in the two test-cases
affected by this commit.

Fix this by suppressing the error in the two test-cases using
allocator_may_return_null=1.

Tested on aarch64-linux.

Approved-By: Tom Tromey <tom@tromey.com>
[1] https://sourceware.org/pipermail/gdb-patches/2024-April/208171.html

7 weeks agogdbsupport: constify some return values in print-utils.{h,cc}
Simon Marchi [Wed, 17 Apr 2024 20:55:28 +0000 (16:55 -0400)] 
gdbsupport: constify some return values in print-utils.{h,cc}

There is no reason the callers of these functions need to change the
returned string, so change the `char *` return types to `const char *`.

Update a few callers to also use `const char *`.

Change-Id: I94adff574d5e1b326e8cc688cf1817a15b408b96
Approved-By: Tom Tromey <tom@tromey.com>
7 weeks agoHPPA64 linker: Do not force the generation of DT_FLAGS for Linux targets.
Nick Clifton [Thu, 18 Apr 2024 12:24:42 +0000 (13:24 +0100)] 
HPPA64 linker: Do not force the generation of DT_FLAGS for Linux targets.

  PR 30743

7 weeks agoAdd DW_TAG_compile_unit DIE to Dummy CUs
Will Hawkins [Thu, 18 Apr 2024 11:28:52 +0000 (13:28 +0200)] 
Add DW_TAG_compile_unit DIE to Dummy CUs

Dummy CUs help detect errors and are very helpful. However, the DWARF
spec seems to indicate the CUs need a DW_TAG_compile_unit in addition to
the header. This patch adds that.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31650

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
Approved-By: Tom de Vries <tdevries@suse.de>
Tested-By: Tom de Vries <tdevries@suse.de>
7 weeks agoRe: Fix address violations when reading corrupt VMS records
Alan Modra [Wed, 17 Apr 2024 12:58:23 +0000 (22:28 +0930)] 
Re: Fix address violations when reading corrupt VMS records

Fixes error reports about the length of EEOM records produced by gas.

PR 21618
* vms-alpha.c (evax_bfd_print_emh): Don't read subtyp if short
record.  Consolidate error messages.
(evax_bfd_print_eeom): Allow length 10 record.

7 weeks agoalpha_vms_get_section_contents vs. fuzzed files
Alan Modra [Wed, 17 Apr 2024 08:59:19 +0000 (18:29 +0930)] 
alpha_vms_get_section_contents vs. fuzzed files

This patch is in response to an oss-fuzz report regarding
use-of-uninitialized-value in bfd_is_section_compressed_info from
section contents provided by alpha_vms_get_section_contents.  That
hole is covered by using bfd_zalloc rather than bfd_alloc.

The rest of the patch is mostly a tidy.  In a function returning
section contents, I tend to prefer a test on the section properties
over a test on file properties.  That's why I've changed the file
flags test to one on section filepos and flags before calling
_bfd_generic_get_section_contents.  Also, fuzzed objects can easily
have sections with file backing in relocatable objects, or sections
without file backing in images.  Possible confusion is avoided by
testing each section.

Note that we are always going to run into out-of-memory with fuzzed
alpha-vms object files due to sections with contents via ETIR records.
eg. ETIR__C_STO_IMMR stores a number of bytes repeatedly, with a
32-bit repeat count.  So section contents can be very large from a
relatively small file.  I'm inclined to think that an out-of-memory
error is fine for such files.

* vms-alpha.c (alpha_vms_get_section_contents): Handle sections
with non-zero filepos or without SEC_HAS_CONTENTS via
_bfd_generic_get_section_contents.  Zero memory allocated for
sections filled by ETIR records.

7 weeks agoTidy objdump opb expressions
Alan Modra [Wed, 17 Apr 2024 08:46:55 +0000 (18:16 +0930)] 
Tidy objdump opb expressions

I don't think any of these can overflow, but since all of the
expressions I'm editing here are inside a while loop with condition
addr_offset < stop_offset, this change makes it more obvious that they
can't overflow.

* objdump.c (disassemble_bytes): Calculate octet expressions
involving both addr_offset and stop_offset by first
subtracting addr_offset from stop_offset.

7 weeks agoAutomatic date update in version.in
GDB Administrator [Thu, 18 Apr 2024 00:00:32 +0000 (00:00 +0000)] 
Automatic date update in version.in

7 weeks agoRemove a copy from c-exp.y:parse_number
Tom Tromey [Fri, 12 Apr 2024 02:34:17 +0000 (20:34 -0600)] 
Remove a copy from c-exp.y:parse_number

parse_number copies its input string, but there is no need to do this.
This patch removes the copy.

Regression tested on x86-64 Fedora 38.

Reviewed-by: Keith Seitz <keiths@redhat.com>
7 weeks agogdb/Windows: Fix detach while running
Pedro Alves [Wed, 17 Apr 2024 17:57:01 +0000 (18:57 +0100)] 
gdb/Windows: Fix detach while running

While testing a WIP Cygwin GDB that supports non-stop, I noticed that
gdb.threads/attach-non-stop.exp exposes that this:

 (gdb) attach PID&
 ...
 (gdb) detach

... hangs.

And it turns out that it hangs in all-stop as well.  This commits
fixes that.

After "attach &", the target is set running, we've called
ContinueDebugEvent and the process_thread thread is waiting for
WaitForDebugEvent events.  It is the equivalent of "attach; c&".

In windows_nat_target::detach, the first thing we do is
unconditionally call windows_continue (for ContinueDebugEvent), which
blocks in do_synchronously, until the process_thread sees an event out
of WaitForDebugEvent.  Unless the inferior happens to run into a
breakpoint, etc., then this hangs indefinitely.

If we've already called ContinueDebugEvent earlier, then we shouldn't
be calling it again in ::detach.

Still in windows_nat_target::detach, we have an interesting issue that
ends up being the bulk of the patch -- only the process_thread thread
can call DebugActiveProcessStop, but if it is blocked in
WaitForDebugEvent, we need to somehow force it to break out of it.
The only way to do that, is to force the inferior to do something that
causes WaitForDebugEvent to return some event.

This patch uses CreateRemoteThread to do it, which results in
WaitForDebugEvent reporting CREATE_THREAD_DEBUG_EVENT.  We then
terminate the injected thread before it has a chance to run any
userspace code.

Note that Win32 functions like DebugBreakProcess and
GenerateConsoleCtrlEvent would also inject a new thread in the
inferior.  I first used DebugBreakProcess, but that is actually more
complicated to use, because we'd have to be sure to consume the
breakpoint event before detaching, otherwise the inferior would likely
die due a breakpoint exception being raised with no debugger around to
intercept it.

See the new break_out_process_thread method.

So the fix has two parts:

 - Keep track of whether we've called ContinueDebugEvent and the
   process_thread thread is waiting for events, or whether
   WaitForDebugEvent already returned an event.

 - In windows_nat_target::detach, if the process_thread thread is
   waiting for events, unblock out of its WaitForDebugEvent, before
   proceeding with the actual detach.

New test included.  Passes cleanly on GNU/Linux native and gdbserver,
and also passes cleanly on Cygwin and MinGW, with the fix.  Before the
fix, it would hang and fail with a timeout.

Tested-By: Hannes Domani <ssbssa@yahoo.de>
Reviewed-By: Tom Tromey <tom@tromey.com>
Change-Id: Ifb91c58c08af1a9bcbafecedc93dfce001040905

7 weeks agogdb+gdbserver/Linux: Remove USE_SIGTRAP_SIGINFO fallback
Pedro Alves [Wed, 17 Apr 2024 17:20:35 +0000 (18:20 +0100)] 
gdb+gdbserver/Linux: Remove USE_SIGTRAP_SIGINFO fallback

It's been over 9 years (since commit faf09f0119da) since Linux GDB and
GDBserver started relying on SIGTRAP si_code to tell whether a
breakpoint triggered, which is important for non-stop mode.  When that
then-new code was added, I had left the then-old code as fallback, in
case some architectured still needed it.  Given AFAIK there haven't
been complaints since, this commit finally removes the fallback code,
along with USE_SIGTRAP_SIGINFO.

Change-Id: I140a5333a9fe70e90dbd186aca1f081549b2e63d

7 weeks agoUse section name in DWARF error message
Tom Tromey [Wed, 17 Apr 2024 13:42:28 +0000 (07:42 -0600)] 
Use section name in DWARF error message

A bug points out that a certain error message in read_str_index uses a
hard-coded section name.  This patch changes it to use
dwarf2_section_info::get_name instead, like the other errors in the
function.

No test because it didn't seem worthwhile.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31639
Approved-By: Simon Marchi <simon.marchi@efficios.com>
7 weeks agogdbsupport, gdbserver, gdb: use -Wno-vla-cxx-extension
Simon Marchi [Fri, 12 Apr 2024 17:51:54 +0000 (13:51 -0400)] 
gdbsupport, gdbserver, gdb: use -Wno-vla-cxx-extension

When building with clang 18, I see:

      CXX    aarch64-linux-tdep.o
    /home/smarchi/src/binutils-gdb/gdb/aarch64-linux-tdep.c:1299:26: error: variable length arrays in C++ are a Clang extension [-Werror,-Wvla-cxx-extension]
     1299 |       gdb_byte za_zeroed[za_bytes];
          |                          ^~~~~~~~
    /home/smarchi/src/binutils-gdb/gdb/aarch64-linux-tdep.c:1299:26: note: read of non-const variable 'za_bytes' is not allowed in a constant expression
    /home/smarchi/src/binutils-gdb/gdb/aarch64-linux-tdep.c:1282:10: note: declared here
     1282 |   size_t za_bytes = std::pow (sve_vl_from_vg (svg), 2);
          |          ^

Since we are using VLAs right now, that warning doesn't make sense for
us.  add `-Wno-vla-cxx-extension` to the list of warning flags we try to
enable.  If we ever choose to disallow VLAs, we can remove that flag.

Change-Id: Ie41feafc50c343f6e75333d4f836ce32fbeb6d8c

7 weeks agoFix include guard typo
Matt Wozniski [Tue, 16 Apr 2024 21:20:20 +0000 (17:20 -0400)] 
Fix include guard typo

Signed-off-by: Matt Wozniski <godlygeek@gmail.com>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31645
Approved-By: Tom Tromey <tom@tromey.com>
7 weeks agogdb/record: minor clean, remove some unneeded arguments
Andrew Burgess [Mon, 15 Apr 2024 13:02:15 +0000 (14:02 +0100)] 
gdb/record: minor clean, remove some unneeded arguments

I spotted that the two functions:

  record_full_open_1
  record_full_core_open_1

both took two arguments, neither of which are used.

I stumbled onto this while reviewing how filename_completer is used.
The 'record full restore' command uses filename_completer and invokes
the cmd_record_full_restore function.

The cmd_record_full_restore function calls core_file_command and then
record_full_open, which then calls one of the above functions.

As 'record full restore' takes a filename, this is passed to
cmd_record_full_restore, which forwards the filename to both
core_file_command and record_full_open.  However, record_full_open
never actually uses the filename that is passed in.

The record_full_open function is also used for 'target record-full'.

I propose that record_full_open should no longer expect to see any
user supplied arguments passed in (it doesn't use any).  In fact, I've
added a check that if we do get any user supplied arguments we'll
throw an error.

Now that we know record_full_open isn't being passed any user
arguments we can stop passing the arguments to record_full_open_1 and
record_full_core_open_1, this will make no user visible difference as
these arguments were not used.

It is possible that a user was previously doing:

  (gdb) target record-full blah blah blah

And this previously would work fine, the 'blah blah blah' was
ignored.  Now this will give an error.  Other than this case there
should be no user visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
7 weeks agogdb/record: add an assert in cmd_record_start
Andrew Burgess [Mon, 15 Apr 2024 13:10:05 +0000 (14:10 +0100)] 
gdb/record: add an assert in cmd_record_start

The 'record' command is both a prefix command AND an alias for 'target
record-full'.  As it is a prefix command, if a user types:

  (gdb) record blah

Then GDB will look for, and try to invoke the 'blah' sub-command.
This will either succeed (if blah is found) or throw an error (if blah
is not found).

As such, the only way to invoke the 'record' command is like:

  (gdb) record

It is impossible to pass arguments to the 'record' command.  As we
know this is true then we can assert this in cmd_record_start.

I added this assert because initially I was going to try forwarding
ARGS from cmd_record_start to the 'target record-full' command, but
then I realised passing arguments to 'record' was impossible.

There should be no user visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
7 weeks agogdb/record: remove unnecessary use of filename_completer
Andrew Burgess [Mon, 15 Apr 2024 12:48:34 +0000 (13:48 +0100)] 
gdb/record: remove unnecessary use of filename_completer

Spotted that the 'record' command has its completer set to
filename_completer.  The problem is that 'record' is a prefix command,
as such, its completer is hard-coded to complete on sub-commands.  The
attempt to use filename_completer is irrelevant.

The 'record' command is itself a command though, that is, a user can
do this:

  (gdb) record

which is really just an alias for:

  (gdb) target record-full

Nowhere does cmd_record_start (which is called when 'record' is used)
expect a filename, and 'target record-full' doesn't expect a filename
either.

So lets just drop the line which sets filename_completer as the
completer for 'record'.

Approved-By: Tom Tromey <tom@tromey.com>
7 weeks ago[gdb/testsuite] Require DW_LNE_end_sequence
Tom de Vries [Wed, 17 Apr 2024 10:55:00 +0000 (12:55 +0200)] 
[gdb/testsuite] Require DW_LNE_end_sequence

The dwarf standard requires that every line number program sequence ends
with a DW_LNE_end_sequence instruction.

Enforce this in the dwarf assembler for the last sequence in a line number
program (we have no means to enforce this for earlier sequences), and fix a
few test-case that don't have it.

Tested on aarch64-linux.

PR testsuite/31618
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31618

7 weeks ago[gdb/testsuite] Fix end_sequence addresses
Tom de Vries [Wed, 17 Apr 2024 10:55:00 +0000 (12:55 +0200)] 
[gdb/testsuite] Fix end_sequence addresses

I noticed in test-case gdb.reverse/map-to-same-line.exp, that the end of main:
...
00000000004102c4 <end_of_sequence>:
  4102c4:       52800000        mov     w0, #0x0                        // #0
  4102c8:       9100c3ff        add     sp, sp, #0x30
  4102cc:       d65f03c0        ret
...
is not described by the line table:
...
File name                    Line number    Starting address    View    Stmt
  ...
map-to-same-line.c                    54            0x4102ac               x
map-to-same-line.c                     -            0x4102c4
...

Fix this by ending the line table at $main_end.

Likewise in a few other test-cases, found using:
...
$ find gdb/testsuite/ -type f \
  | xargs grep -B1 DW_LNE_end_sequence \
  | grep set_address \
  | egrep -v "_end|_len"
...

Tested on aarch64-linux.

7 weeks ago[gdb/testsuite] Require address update for DW_LNS_copy
Tom de Vries [Wed, 17 Apr 2024 10:55:00 +0000 (12:55 +0200)] 
[gdb/testsuite] Require address update for DW_LNS_copy

No address update before a DW_LNS_copy might mean an incorrect dwarf
assembly test-case.

Try to catch such incorrect dwarf assembly test-cases by:
- requiring an explicit address update for each DW_LNS_copy, and
- handling the cases where an update is indeed not needed, by adding
  "DW_LNS_advance_pc 0".

Tested on aarch64-linux.

7 weeks ago[gdb/testsuite] Require address update for DW_LNE_end_sequence
Tom de Vries [Wed, 17 Apr 2024 10:55:00 +0000 (12:55 +0200)] 
[gdb/testsuite] Require address update for DW_LNE_end_sequence

With test-case gdb.dwarf2/dw2-epilogue-begin.exp, we have an end_sequence
entry with the same address as the line entry before it:
...
File name                    Line number    Starting address    View    Stmt

dw2-epilogue-begin.c                  44            0x4101e8               x
dw2-epilogue-begin.c                  47            0x4101ec               x
dw2-epilogue-begin.c                   -            0x4101ec
...
and consequently the line entry is removed by gdb:
...
INDEX  LINE   REL-ADDRESS        UNREL-ADDRESS      IS-STMT PRO EPI
0      20     0x00000000004101a8 0x00000000004101a8 Y       Y   Y
1      27     0x00000000004101b0 0x00000000004101b0 Y
2      32     0x00000000004101b8 0x00000000004101b8 Y       Y
3      34     0x00000000004101c0 0x00000000004101c0 Y           Y
4      35     0x00000000004101c8 0x00000000004101c8 Y
5      40     0x00000000004101d4 0x00000000004101d4 Y       Y
6      44     0x00000000004101e8 0x00000000004101e8 Y
7      END    0x00000000004101ec 0x00000000004101ec Y
...

This is a common mistake in dwarf assembly test-cases.

Fix this by:
- requiring an address update for each DW_LNE_end_sequence, and
- fixing the test-cases where that triggers an error.

I also encountered the error in test-case gdb.dwarf2/dw2-bad-elf.exp, and in
this case I worked around it using "DW_LNS_advance_pc 0".

Tested on aarch64-linux.

PR testsuite/31592
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31592

7 weeks agoFix max-depth test case for AIX.
Aditya Vidyadhar Kamath [Wed, 10 Apr 2024 05:52:05 +0000 (00:52 -0500)] 
Fix max-depth test case for AIX.

In AIX, if in the main program the global variables are unused then the linker optimises
these variables and the dwarf will not have proper address to the same. Hence we cannot access these
variables.

This patch is a fix to the same so that all the test case of max-depth can passs in AIX as well.

7 weeks agoaarch64: Remove asserts from operand qualifier decoders [PR31595]
Victor Do Nascimento [Tue, 16 Apr 2024 10:49:15 +0000 (11:49 +0100)] 
aarch64: Remove asserts from operand qualifier decoders [PR31595]

Given that the disassembler should never abort when decoding
(potentially random) data, assertion statements in the
`get_*reg_qualifier_from_value' function family prove problematic.

Consider the random 32-bit word W, encoded in a data segment and
encountered on execution of `objdump -D <obj_name>'.

If:

  (W & ~opcode_mask) == valid instruction

Then before `print_insn_aarch64_word' has a chance to report the
instruction as potentially undefined, an attempt will be made to have
the qualifiers for the instruction's register operands (if any)
decoded.  If the relevant bits do not map onto a valid qualifier for
the matched instruction-like word, an abort will be triggered and the
execution of objdump aborted.

As this scenario is perfectly feasible and, in light of the fact that
objdump must successfully decode all sections of a given object file,
it is not appropriate to assert in this family of functions.

Therefore, we add a new pseudo-qualifier `AARCH64_OPND_QLF_ERR' for
handling invalid qualifier-associated values and re-purpose the
assertion conditions in qualifier-retrieving functions to be the
predicate guarding the returning of the calculated qualifier type.
If the predicate fails, we return this new qualifier and allow the
caller to handle the error as appropriate.

As these functions are called either from within
`aarch64_extract_operand' or `do_special_decoding', both of which are
expected to return non-zero values, it suffices that callers return
zero upon encountering `AARCH64_OPND_QLF_ERR'.

Ar present the error presented in the hypothetical scenario has been
encountered in `get_sreg_qualifier_from_value', but the change is made
to the whole family to keep the interface consistent.

Bug: https://sourceware.org/PR31595

7 weeks ago[gdb/testsuite] Fix gdbserver pid in gdb.server/server-kill-python.exp
Tom de Vries [Wed, 17 Apr 2024 09:45:02 +0000 (11:45 +0200)] 
[gdb/testsuite] Fix gdbserver pid in gdb.server/server-kill-python.exp

The commit ed32754a8c7 ("[gdb/testsuite] Fix gdb.server/multi-ui-errors.exp for
remote target") intended to addresss the problem that this command:
...
set gdbserver_pid [exp_pid -i $server_spawn_id]
...
does not return the pid of the gdbserver for remote target, but rather the one
of the ssh client session.

To fix this, it added another way of getting the gdbserver_pid.

For the trivial case of non-remote target, the PID found by either method
should be identical, but if we compare those by adding
"puts [exec ps -p $gdbserver_pid]" we get:
...
  PID TTY          TIME CMD
31711 pts/8    00:00:00 gdbserver
  PID TTY          TIME CMD
31718 pts/8    00:00:00 server-kill-pyt
...

The problem is that while the gdbserver PID is supposed to be read from the
result of "gdb.execute ('p server_pid')" in the python script, instead it's
taken from:
...
Process server-kill-python created; pid = 31718^M
...

Fix this by moving the printing of the gdbserver PID out of the python script.

Also double-check the two methods against each other, in the cases that they
should match.

Tested on x86_64-linux.

PR testsuite/31633
https://sourceware.org/bugzilla/show_bug.cgi?id=31633

7 weeks ago[gdb/testsuite] Simplify gdb.server/server-kill-python.exp
Tom de Vries [Wed, 17 Apr 2024 09:45:02 +0000 (11:45 +0200)] 
[gdb/testsuite] Simplify gdb.server/server-kill-python.exp

In test-case gdb.server/server-kill-python.exp we have:
...
if {[gdb_spawn_with_cmdline_opts \
         "-quiet -iex \"set height 0\" -iex \"set width 0\" -ex \"source $host_file1\""] != 0} {
    fail "spawn"
    return
}
...

I reproduced the problem by reverting the fix at the commit adding both the
fix and the test-case, and the reproduced the same problem using:
...
(gdb) source $host_file1
...
so there doesn't seem to be a specific need to source the python file using
"-ex".

Simplify the test-case by sourcing the python file using send_gdb.

This also allow us to simplify the python script.

Tested on x86_64-linux.

7 weeks agoAdd W table for USER_MSR under MAP4.
Hu, Lin1 [Wed, 17 Apr 2024 05:57:50 +0000 (13:57 +0800)] 
Add W table for USER_MSR under MAP4.

opcodes/ChangeLog:

* i386-dis-evex-mod.h: Modify MOD_EVEX_MAP4_F8_P1,
MOD_EVEX_MAP4_F8_P3.
* i386-dis-evex-w.h (EVEX_W_MAP4_F8_P1_M_1): New.
(EVEX_W_MAP4_F8_P3_M_1): Ditto.
* i386-dis.c (vex_w_table): Add EVEX_W_MAP4_F8_P1_M_1,
EVEX_W_MAP4_F8_P3_M_1.
* i386-opc.tbl: Remove redundant '|'.

7 weeks agoelf: Skip the archive if the symbol isn't referenced
H.J. Lu [Tue, 16 Apr 2024 22:48:21 +0000 (15:48 -0700)] 
elf: Skip the archive if the symbol isn't referenced

Also skip the archive if the symbol isn't referenced by a regular object.

bfd/

PR ld/31644
* elflink.c (elf_link_add_archive_symbols): Also skip the archive
if the symbol isn't referenced by a regular object.

ld/

PR ld/31644
* testsuite/ld-plugin/lto.exp: Run PR ld/31644 tests.
* testsuite/ld-plugin/pr31644a.c: New test.
* testsuite/ld-plugin/pr31644b.c: Likewise.
* testsuite/ld-plugin/pr31644c.c: Likewise.

7 weeks agoAutomatic date update in version.in
GDB Administrator [Wed, 17 Apr 2024 00:00:23 +0000 (00:00 +0000)] 
Automatic date update in version.in

7 weeks agoARC e_flags vs. objcopy
Alan Modra [Tue, 16 Apr 2024 22:36:05 +0000 (08:06 +0930)] 
ARC e_flags vs. objcopy

While the patch that Nick reverted in commit 3f6a060c7543 was in the
source, "FAIL: objcopy executable (pr25662)" was seen on ARC.  The
failure was triggered by the .ARC.attributes section being removed by
the linker script.  When a file lacking this section is copied by
objcopy, e_flags from the input is copied to the output (in this case
the value 0x406), but arc_elf_final_write_processing then logical-ors
in 0x300 when Tag_ARC_ABI_osver is not found.

* elf32-arc.c (arc_elf_final_write_processing): Don't ignore
existing e_flags for objcopy.

7 weeks agolibctf warnings
Alan Modra [Mon, 8 Apr 2024 23:23:35 +0000 (08:53 +0930)] 
libctf warnings

Seen with every compiler I have if using -fno-inline:
home/alan/src/binutils-gdb/libctf/ctf-create.c: In function ‘ctf_add_encoded’:
/home/alan/src/binutils-gdb/libctf/ctf-create.c:555:3: warning: ‘encoding’ may be used uninitialized [-Wmaybe-uninitialized]
  555 |   memcpy (dtd->dtd_vlen, &encoding, sizeof (encoding));

Seen with gcc-4.9 and probably others at lower optimisation levels:
home/alan/src/binutils-gdb/libctf/ctf-serialize.c: In function 'symtypetab_density':
/home/alan/src/binutils-gdb/libctf/ctf-serialize.c:211:18: warning: 'sym' may be used uninitialized in this function [-Wmaybe-uninitialized]
    if (*max < sym->st_symidx)

Seen with gcc-4.5 and probably others at lower optimisation levels:
/home/alan/src/binutils-gdb/libctf/ctf-types.c:1649:21: warning: 'tp' may be used uninitialized in this function
/home/alan/src/binutils-gdb/libctf/ctf-link.c:765:16: warning: 'parent_i' may be used uninitialized in this function

Also with gcc-4.5:
In file included from /home/alan/src/binutils-gdb/libctf/ctf-endian.h:25:0,
                 from /home/alan/src/binutils-gdb/libctf/ctf-archive.c:24:
/home/alan/src/binutils-gdb/libctf/swap.h:70:0: warning: "_Static_assert" redefined
/usr/include/sys/cdefs.h:568:0: note: this is the location of the previous definition

* swap.h (_Static_assert): Don't define if already defined.
* ctf-serialize.c (symtypetab_density): Merge two
CTF_SYMTYPETAB_FORCE_INDEXED blocks.
* ctf-create.c (ctf_add_encoded): Avoid "encoding" may be used
uninitialized warning.
* ctf-link.c (ctf_link_deduplicating_open_inputs): Avoid
"parent_i" may be used uninitialized warning.
* ctf-types.c (ctf_type_rvisit): Avoid "tp" may be used
uninitialized warning.

7 weeks agoAvoid cache race in bfd_check_format_matches
Tom Tromey [Sat, 23 Mar 2024 21:19:20 +0000 (15:19 -0600)] 
Avoid cache race in bfd_check_format_matches

Running the gdb test suite with the thread sanitizer enabled shows a
race when bfd_check_format_matches and bfd_cache_close_all are called
simultaneously on different threads.

This patch fixes this race by having bfd_check_format_matches
temporarily remove the BFD from the file descriptor cache -- leaving
it open while format-checking proceeds.

In this setup, the BFD client is responsible for closing the BFD again
on the "checking" thread, should that be desired.  gdb does this by
calling bfd_cache_close in the relevant worker thread.

An earlier version of this patch omitted the "possibly_cached" helper
function.  However, this ran into crashes in the binutils test suite
involving the archive-checking abort in bfd_cache_lookup_worker.  I do
not understand the purpose of this check, so I've simply had the new
function work around it.  I couldn't find any comments explaining this
situation, either.  I suspect that there may still be races related to
this case, but I don't think I have access to the platforms where gdb
deals with archives.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31264

7 weeks agoThread-safety improvements for bfd_check_format_matches
Tom Tromey [Tue, 13 Feb 2024 01:06:56 +0000 (18:06 -0700)] 
Thread-safety improvements for bfd_check_format_matches

A gdb bug found that bfd_check_format_matches has some data races when
called from multiple threads.

In particular, it changes the BFD error handler, which is a global.
It also has a local static variable ("in_check_format") that is used
for recursion detection.  And, finally, it may emit warnings to the
per-xvec warning array, which is a global.

This patch removes all the races here.

The first part of patch is to change _bfd_error_handler to directly
handle the needs of bfd_check_format_matches.  This way, the error
handler does not need to be changed.

This change lets us use the new per-thread global
(error_handler_messages, replacing error_handler_bfd) to also remove
the need for in_check_format -- a single variable suffices.

Finally, the global per-xvec array is replaced with a new type that
holds the error messages.  The outermost such type is stack-allocated
in bfd_check_format_matches.

I tested this using the binutils test suite.  I also built gdb with
thread sanitizer and ran the test case that was noted as failing.
Finally, Alan sent me the test file that caused the addition of the
xvec warning code in the first place, and I confirmed that "nm-new"
has the same behavior on this file both before and after this patch.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31264
Co-Authored-By: Alan Modra <amodra@gmail.com>
7 weeks ago[gdb/testsuite] Add gdb.dwarf2/backward-spec-inter-cu.exp
Tom de Vries [Sun, 24 Sep 2023 10:30:10 +0000 (12:30 +0200)] 
[gdb/testsuite] Add gdb.dwarf2/backward-spec-inter-cu.exp

Add another regression test for PR symtab/30846.

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30846

7 weeks ago[gdb/testsuite] Add gdb.dwarf2/forward-spec-inter-cu.exp
Tom de Vries [Sun, 24 Sep 2023 10:33:59 +0000 (12:33 +0200)] 
[gdb/testsuite] Add gdb.dwarf2/forward-spec-inter-cu.exp

Add a regression test for PR symtab/30846.

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30846

7 weeks agoCorrectly handle DIE parent computations
Tom Tromey [Sat, 13 Jan 2024 01:29:52 +0000 (18:29 -0700)] 
Correctly handle DIE parent computations

Tom de Vries pointed out that the combination of sharding,
multi-threading, and per-CU "racing" means that sometimes a cross-CU
DIE reference might not be correctly resolved.  However, it's
important to handle this correctly, due to some unfortunate aspects of
DWARF.

This patch implements this by arranging to preserve each worker's DIE
map through the end of index finalization.  The extra data is
discarded when finalization is done.  This approach also allows the
parent name resolution to be sharded, by integrating it into the
existing entry finalization loop.

In an earlier review, I remarked that addrmap couldn't be used here.
However, I was mistaken.  A *mutable* addrmap cannot be used, as those
are based on splay trees and restructure the tree even during lookups
(and thus aren't thread-safe).  A fixed addrmap, on the other hand, is
just a vector and is thread-safe.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30846

7 weeks agoIntroduce class parent_map for DIE range map
Tom Tromey [Fri, 12 Jan 2024 03:07:06 +0000 (20:07 -0700)] 
Introduce class parent_map for DIE range map

This changes the DIE range map from a raw addrmap to a custom class.
A new type is used to represent the ranges, in an attempt to gain a
little type safety as well.

Note that the new code includes a map-of-maps type.  This is not used
yet, but will be used in the next patch.

Co-Authored-By: Tom de Vries <tdevries@suse.de>
7 weeks agoAdd move operators for addrmap
Tom Tromey [Sun, 14 Jan 2024 18:51:38 +0000 (11:51 -0700)] 
Add move operators for addrmap

A subsequent patch needs to move an addrmap.  This patch adds the
necessary support.  It also changes addrmap_fixed to take a 'const'
addrmap_mutable.  This is fine according to the contract of
addrmap_mutable; but it did require a compensating const_cast in the
implementation.

7 weeks agoChange handling of DW_TAG_enumeration_type in DWARF scanner
Tom Tromey [Sat, 13 Jan 2024 01:01:00 +0000 (18:01 -0700)] 
Change handling of DW_TAG_enumeration_type in DWARF scanner

Currently the DWARF scanner will enter enumeration constants into the
same namespace as the DW_TAG_enumeration_type itself.  This is the
right thing to do, but the implementation may result in strange
entries being added to the addrmap that maps DIE ranges to entries.

This came up when debugging an earlier version of this series; and
while I don't think this should impact the current series, it seems
better to clean this up anyway.

In the new code, rather than pass the "wrong" scope down through
recursive calls to the scanner, the correct scope is always passed,
and then the parent handling is done when creating the enumerator
entry.

7 weeks ago[gdb/symtab] Refactor condition in scan_attributes
Tom de Vries [Mon, 11 Dec 2023 14:41:26 +0000 (15:41 +0100)] 
[gdb/symtab] Refactor condition in scan_attributes

In scan_attributes there's code:
...
  if (new_reader->cu == reader->cu
      && new_info_ptr > watermark_ptr
      && *parent_entry == nullptr)
    ...
  else if (*parent_entry == nullptr)
    ...
...
that uses the "*parent_entry == nullptr" condition twice.

Make this somewhat more readable by factoring out the condition:
...
  if (*parent_entry == nullptr)
    {
      if (new_reader->cu == reader->cu
  && new_info_ptr > watermark_ptr)
...
      else
...
    }
...

This also allows us to factor out "form_addr (origin_offset, origin_is_dwz)".

Tested on x86_64-linux.

7 weeks agoFix test for sections with different VMA<->LMA relationships so that it only applies...
Nick Clifton [Tue, 16 Apr 2024 16:54:13 +0000 (17:54 +0100)] 
Fix test for sections with different VMA<->LMA relationships so that it only applies to allocated sections, and only sections in the same segment are checked.

  PR 31450

7 weeks agogdb/make-target-delegates.py: don't handle "void" in parse_argtypes
Simon Marchi [Tue, 16 Apr 2024 15:52:24 +0000 (11:52 -0400)] 
gdb/make-target-delegates.py: don't handle "void" in parse_argtypes

I suppose this was needed when we had `void` in declarations of methods
with no parameters.  If so, we no longer need it.  There are no changes
in the generated file.

Change-Id: I0a2b398408aa129634e2d73097a038f7f80db4b4
Approved-By: John Baldwin <jhb@FreeBSD.org>
7 weeks agoRemove excess whitespace from doc strings of some commands
Eli Zaretskii [Tue, 16 Apr 2024 16:13:39 +0000 (19:13 +0300)] 
Remove excess whitespace from doc strings of some commands

I've noticed that doc strings of some commands, like "set cwd"
and  "set inferior-tty", have some excess whitespace, which
makes them display with unexpected indentation, at least in a
Windows command prompt window.  This patch fixes that.

* gdb/linux-nat.c (_initialize_linux_nat):
* gdb/riscv-tdep.c (riscv_insn):
* gdb/top.c (quit_force):
* gdb/infcmd.c (_initialize_infcmd): Remove excess whitespace.

7 weeks agoRemove accidental commit of an experimental change
Nick Clifton [Tue, 16 Apr 2024 14:06:05 +0000 (15:06 +0100)] 
Remove accidental commit of an experimental change

7 weeks ago[gdb/python] Throw MemoryError in inferior.read_memory if malloc fails
Tom de Vries [Tue, 16 Apr 2024 13:53:47 +0000 (15:53 +0200)] 
[gdb/python] Throw MemoryError in inferior.read_memory if malloc fails

PR python/31631 reports a gdb internal error when doing:
...
(gdb) python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff)
utils.c:709: internal-error: virtual memory exhausted.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
...

Fix this by throwing a python MemoryError, such that we have instead:
...
(gdb) python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff)
Python Exception <class 'MemoryError'>:
Error occurred in Python.
(gdb)
...

Likewise for DAP.

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31631

7 weeks agox86: Fix a memory leak in md_assemble
H.J. Lu [Tue, 9 Apr 2024 23:01:16 +0000 (16:01 -0700)] 
x86: Fix a memory leak in md_assemble

Fix a memory leak in md_assemble where copy may be cleared and may be
the same as copy:

      if (copy && !mnem_suffix)
        {
          line = copy;
          copy = NULL;
  no_match:

* config/tc-i386.c (md_assemble): Properly free the xstrdup
memory.

7 weeks agogas: Free unused memory in scfi_ops_cleanup
H.J. Lu [Tue, 9 Apr 2024 23:04:49 +0000 (16:04 -0700)] 
gas: Free unused memory in scfi_ops_cleanup

* scfi.c (scfi_ops_cleanup): Free op->op_data and head.

7 weeks agoSimplify readelf's RELR relocation display.
Fangrui Song [Tue, 16 Apr 2024 12:17:35 +0000 (13:17 +0100)] 
Simplify readelf's RELR relocation display.

7 weeks agoGas Doc: Update example of how .altmacro affects the interpretation of macro arguments.
Nick Clifton [Tue, 16 Apr 2024 11:06:08 +0000 (12:06 +0100)] 
Gas Doc: Update example of how .altmacro affects the interpretation of macro arguments.

  PR 31255

7 weeks agoRemove debug printout from 9dd918142787246ea7ed53494d9cbc6b51486133
Simon Cook [Tue, 16 Apr 2024 10:54:08 +0000 (11:54 +0100)] 
Remove debug printout from 9dd918142787246ea7ed53494d9cbc6b51486133

7 weeks agoAutomatic date update in version.in
GDB Administrator [Tue, 16 Apr 2024 00:00:13 +0000 (00:00 +0000)] 
Automatic date update in version.in

7 weeks agoFix crash in gdb_rl_callback_handler
Tom Tromey [Mon, 15 Apr 2024 18:52:05 +0000 (12:52 -0600)] 
Fix crash in gdb_rl_callback_handler

commit bdcd50f9 ("Strip trailing newlines from input string")
introduced a crash in eof-exit.exp.  This patch fixes the problem by
adding a NULL check in the appropriate spot.

Regression tested on x86-64 Fedora 38.  I'm checking this in.

7 weeks agoRemove 'copy_names' parameter from add_using_directive
Tom Tromey [Wed, 10 Apr 2024 17:48:13 +0000 (11:48 -0600)] 
Remove 'copy_names' parameter from add_using_directive

I noticed that add_using_directive's 'copy_names' parameter is only
used by a single caller.  This patch removes the parameter and changes
that caller to copy the names itself.  I chose to use intern here
since I suspect the names may well be repeated in a given objfile.

Approved-By: John Baldwin <jhb@FreeBSD.org>
7 weeks agogdb: Add Felix Willgerodt as the x86 architecture maintainer
John Baldwin [Mon, 15 Apr 2024 17:39:46 +0000 (10:39 -0700)] 
gdb: Add Felix Willgerodt as the x86 architecture maintainer

This includes both the i386 and x86-64 architectures.

7 weeks agoRemove dependency upon shlwapi library when building BFD for Windows/MinGW environments.
Nick Clifton [Mon, 15 Apr 2024 15:42:15 +0000 (16:42 +0100)] 
Remove dependency upon shlwapi library when building BFD for Windows/MinGW environments.

  PR 31527

7 weeks agoChange printf attribute to fix clang build
Tom Tromey [Mon, 15 Apr 2024 15:33:21 +0000 (09:33 -0600)] 
Change printf attribute to fix clang build

commit e8cd90f0 ("Rewrite gdb_bfd_error_handler") broke the clang
build.

The problem here is that print_error_callback isn't marked as being
printf-like, but it calls string_file::vprintf, triggering:

../../binutils-gdb/gdb/gdb_bfd.c:1202:18: error: format string is not a string literal [-Werror,-Wformat-nonliteral]

This patch applies the attribute to this function.

It also removes the attribute from gdb_bfd_error_handler, because that
function is no longer really printf-like.

7 weeks agoWhen mapping sections to segments ensure that we do not add sections whose VMA->LMA...
Vijay Shankar [Mon, 15 Apr 2024 15:27:21 +0000 (16:27 +0100)] 
When mapping sections to segments ensure that we do not add sections whose VMA->LMA relationship does not match the relationship of earlier sections in the segment.

  PR 31540

7 weeks agoAvoid complaint warning on mingw
Tom Tromey [Tue, 9 Apr 2024 14:54:58 +0000 (08:54 -0600)] 
Avoid complaint warning on mingw

The mingw build currently issues a warning:

./../../src/gdb/utils.h:378:56: warning: ignoring attributes on template argument 'void(const char*, va_list)' {aka 'void(const char*, char*)'} [-Wignored-attributes]

This patch fixes the problem as suggested by Simon:

    https://sourceware.org/pipermail/gdb-patches/2024-April/207908.html

...that is, by changing the warning interceptor to a class with a
single 'warn' method.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
7 weeks agoStrip trailing newlines from input string
Tom Tromey [Thu, 4 Apr 2024 14:40:38 +0000 (08:40 -0600)] 
Strip trailing newlines from input string

A co-worker noticed a strange situation where "target remote" would
fail due to a trailing newline in the address part of the command.
Eventually he tracked this down to the fact that he was pasting the
command into the terminal, and due to bracketed paste mode, the
newline was being preserved by readline.

It seems to me that we basically never want a trailing newline on a
gdb command, so this patch removes it when handling the readline
result.

Co-Authored-By: Kévin Le Gouguec <legouguec@adacore.com>
Approved-By: Luis Machado <luis.machado@arm.com>
Tested-By: Luis Machado <luis.machado@arm.com>
7 weeks agogprofng: Fix dvi documentation build rule
Christophe Lyon [Wed, 10 Apr 2024 09:58:42 +0000 (09:58 +0000)] 
gprofng: Fix dvi documentation build rule

This  patch fixes 'install-dvi'.

7 weeks agosim: riscv: Fix confusion with c.jal vs. c.addiw
Bernd Edlinger [Mon, 15 Apr 2024 03:52:19 +0000 (05:52 +0200)] 
sim: riscv: Fix confusion with c.jal vs. c.addiw

There was apparently a confusion which cpu model uses
compressed JAL and which ADDIW.  Fixed that in execute_c,
case MATCH_C_JAL | MATCH_C_ADDIW.

Fixes 3224e32fb84f ("sim: riscv: Add support for compressed integer instructions")

Approved-By: Andrew Burgess <aburgess@redhat.com>
7 weeks agosim: riscv: Make stack 16-byte aligned
Bernd Edlinger [Wed, 10 Apr 2024 11:34:21 +0000 (13:34 +0200)] 
sim: riscv: Make stack 16-byte aligned

Various gcc test cases fail due to the stack
alignment of 16 bytes is expected by gcc,
causing issues mostly with vararg functions,
e.g.

FAIL: gcc.c-torture/execute/nest-align-1.c   -O0  execution test
FAIL: gcc.c-torture/execute/nest-stdar-1.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-12.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-15.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-16.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-17.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-20.c   -O0  execution test
FAIL: gcc.c-torture/execute/va-arg-26.c   -O0  execution test
...

Approved-By: Andrew Burgess <aburgess@redhat.com>
7 weeks agosim: riscv: Fix PC at gdb breakpoints
Bernd Edlinger [Fri, 12 Apr 2024 06:55:11 +0000 (08:55 +0200)] 
sim: riscv: Fix PC at gdb breakpoints

The uncompressed EBREAK instruction does not work
correctly this way, and the comment saying that
GDB expects us to step over EBREAK is just wrong.
The PC was always 4 bytes too high, which skips one
instruction at break and step over commands, and
causes complete chaos.  The compressed EBREAK was
already implemented correctly.

Tested by using gdb's "target sim" and single-stepping.

Approved-By: Andrew Burgess <aburgess@redhat.com>
8 weeks agoLoongArch: ld:Report an error when seeing an unrecognized relocation
Lulu Cai [Thu, 21 Mar 2024 07:16:05 +0000 (15:16 +0800)] 
LoongArch: ld:Report an error when seeing an unrecognized relocation

If we generate an object file using an assembler with the new
relocations added, and then linking those files with an older
linker, the link will still complete and the linked file will
be generated.
In this case we should report an error instead of continuing
the linking process.

8 weeks agoAutomatic date update in version.in
GDB Administrator [Mon, 15 Apr 2024 00:00:11 +0000 (00:00 +0000)] 
Automatic date update in version.in

8 weeks agoAutomatic date update in version.in
GDB Administrator [Sun, 14 Apr 2024 00:00:07 +0000 (00:00 +0000)] 
Automatic date update in version.in

8 weeks agoAutomatic date update in version.in
GDB Administrator [Sat, 13 Apr 2024 00:00:09 +0000 (00:00 +0000)] 
Automatic date update in version.in

8 weeks agoFix setting watchpoints when current thread is running
Pedro Alves [Tue, 2 May 2023 14:04:28 +0000 (15:04 +0100)] 
Fix setting watchpoints when current thread is running

Currently, when the current thread is running, you can print global
variables.  However, if you try to set a watchpoint on the same
globals, GDB errors out, complaining that the selected thread is
running.  Like so:

 (gdb) c&
 Continuing.
 (gdb) p global
 $1 = 1098377287
 (gdb) watch global
 Selected thread is running.

This patch makes setting the watchpoint work.  You'll now get:

 (gdb) c&
 Continuing.
 (gdb) [New Thread 0x7ffff7d6e640 (LWP 434993)]
 [New Thread 0x7ffff756d640 (LWP 434994)]
 p global
 $1 = 88168
 (gdb) watch global
 Hardware watchpoint 2: global
 (gdb) [Switching to Thread 0x7ffff7d6e640 (LWP 434993)]

 Thread 2 "function0" hit Hardware watchpoint 2: global

 Old value = 185420
 New value = 185423
 int_return () at threads.c:39
 39      }

The problem is that update_watchpoint calls get_selected_frame
unconditionally.  We can skip it if the watchpoint expression is only
watching globals.

This adds a testcase that exercises both all-stop and non-stop, and
also software and hardware watchpoints.  It is kfailed for software
watchpoints, as those require another fix not handled by this patch
(the sw watchpoint doesn't fire because GDB doesn't force the
running-free thread to switch to single-stepping).

Change-Id: I68ca948541aea3edd4f70741f272f543187abe40

8 weeks agoNew testcase gdb.threads/leader-exit-attach.exp (PR threads/8153)
Pedro Alves [Mon, 25 Mar 2024 15:17:02 +0000 (15:17 +0000)] 
New testcase gdb.threads/leader-exit-attach.exp (PR threads/8153)

Add a new testcase for exercising attaching to a process after its
main thread has exited.

This is not possible on Linux, the kernel does not allow attaching to
a zombie task, so the test is kfailed there.  It is possible however
on Windows at least, and was the scenario addressed by the Windows
backend fix in
https://sourceware.org/legacy-ml/gdb-patches/2003-12/msg00479.html,
nowadays PR threads/8153, back in 2003.

Passes cleanly on Cygwin.
KFAILed on GNU/Linux native and gdbserver.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=8153
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31554
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31555
Change-Id: Ib554f92f68c965bb4603cdf2aadb55ca45ded53b

8 weeks agoCygwin/testsuite: Avoid infinite hang
Pedro Alves [Tue, 5 Mar 2024 16:03:15 +0000 (16:03 +0000)] 
Cygwin/testsuite: Avoid infinite hang

On Cygwin, the gdb.base/fork-no-detach-follow-child-dlopen.exp
testcase hits a sequence of cascading FAILs:

 (gdb) run
 Starting program: ..../gdb.base/fork-no-detach-follow-child-dlopen/fork-no-detach-follow-child-dlopen
 [New Thread 12672.0x318c]
 [New Thread 12672.0x2844]
 [New Thread 12672.0x714]
 FAIL: gdb.base/fork-no-detach-follow-child-dlopen.exp: runto: run to add (timeout)
 frame
 FAIL: gdb.base/fork-no-detach-follow-child-dlopen.exp: frame (timeout)
 list
 FAIL: gdb.base/fork-no-detach-follow-child-dlopen.exp: list (timeout)

And the test program never makes progress.

... and at this point, Cygwin is completely stuck.  I can't run any
other Cygwin program.

However, if we run the test program outside DejaGnu, we see something
different:

  (gdb) b add
  Function "add" not defined.
  Make breakpoint pending on future shared library load? (y or [n]) y
  Breakpoint 1 (add) pending.
  (gdb) r
  Starting program: ..../gdb.base/fork-no-detach-follow-child-dlopen/fork-no-detach-follow-child-dlopen
  [New Thread 10968.0x834]
  [New Thread 10968.0x29a4]
  [New Thread 10968.0x16b8]
  [New Thread 10968.0xf9c]
  [Switching to Thread 10968.0x16b8]

  Thread 4 "sig" hit Breakpoint 1.2, pending_signals::add (pack=..., this=0x7ffa1e748a40 <sigq>) at /usr/src/debug/cygwin-3.4.9-1/winsup/cygwin/sigproc.cc:1304
  1304      se = sigs + pack.si.si_signo;
  (gdb)

Ah, the test wanted to run to a global "add" function, but managed to
stop at an internal Cygwin method called "add".  And stopping there
deadlocks everything Cygwin in the system.  (I believe some
cygwin1.dll mechanisms use cross-process synchronization or
communication, we're probably blocking something like that.)

Fix this by using "break -q".  The tests FAIL because we don't support
follow-fork for Cygwin, but at least we no longer deadlock the
machine.

Approved-by: Kevin Buettner <kevinb@redhat.com>
Change-Id: I7181d8481c2ae1024b0d73e3bb194f9a4f0a7eb9