diff options
author | Nick Alcock <nick.alcock@oracle.com> | 2021-01-16 16:49:29 +0000 |
---|---|---|
committer | Nick Alcock <nick.alcock@oracle.com> | 2021-02-04 16:01:53 +0000 |
commit | 35a01a045442f6860abba7246f215adefc9dfa5b (patch) | |
tree | cb878c9733152a0a4d82e27daa655d1ebd6c89f9 /libctf/ctf-impl.h | |
parent | 1a2f1b54a520b28891910846c8671e1c4bcaf348 (diff) | |
download | gdb-35a01a045442f6860abba7246f215adefc9dfa5b.zip gdb-35a01a045442f6860abba7246f215adefc9dfa5b.tar.gz gdb-35a01a045442f6860abba7246f215adefc9dfa5b.tar.bz2 |
libctf, ld: fix symtypetab and var section population under ld -r
The variable section in a CTF dict is meant to contain the types of
variables that do not appear in the symbol table (mostly file-scope
static declarations). We implement this by having the compiler emit
all potential data symbols into both sections, then delete those
symbols from the variable section that correspond to data symbols the
linker has reported.
Unfortunately, the check for this in ctf_serialize is wrong: rather than
checking the set of linker-reported symbols, we check the set of names
in the data object symtypetab section: if the linker has reported no
symbols at all (usually if ld -r has been run, or if a non-linker
program that does not use symbol tables is calling ctf_link) this will
include every single symbol, emptying the variable section completely.
Worse, when ld -r is in use, we want to force writeout of every
symtypetab entry on the inputs, in an indexed section, whether or not
the linker has reported them, since this isn't a final link yet and the
symbol table is not finalized (and may grow more symbols than the linker
has yet reported). But the check for this is flawed too: we were
relying on ctf_link_shuffle_syms not having been called if no symbols
exist, but that function is *always* called by ld even when ld -r is in
use: ctf_link_add_linker_symbol is the one that's not called when there
are no symbols.
We clearly need to rethink this. Using the emptiness of the set of
reported symbols as a test for ld -r is just ugly: the linker already
knows if ld -r is underway and can just tell us. So add a new linker
flag CTF_LINK_NO_FILTER_REPORTED_SYMS that is set to stop the linker
filtering the symbols in the symtypetab sections using the set that the
linker has reported: use the presence or absence of this flag to
determine whether to emit unindexed symtabs: we only remove entries from
the variable section when filtering symbols, and we only remove them if
they are in the reported symbol set, fixing the case where no symbols
are reported by the linker at all.
(The negative sense of the new CTF_LINK flag is intentional: the common
case, both for ld and for simple tools that want to do a ctf_link with
no ELF symbol table in sight, is probably to filter out symbols that no
linker has reported: i.e., for the simple tools, all of them.)
There's another wrinkle, though. It is quite possible for a non-linker
to add symbols to a dict via ctf_add_*_sym and then write it out via the
ctf_write APIs: perhaps it's preparing a dict for a later linker
invocation. Right now this would not lead to anything terribly
meaningful happening: ctf_serialize just assumes it was called via
ctf_link if symbols are present. So add an (internal-to-libctf) flag
that indicates that a writeout is happening via ctf_link_write, and set
it there (propagating it to child dicts as needed). ctf_serialize can
then spot when it is not being called by a linker, and arrange to always
write out an indexed, sorted symtypetab for fastest possible future
symbol lookup by name in that case. (The writeouts done by ld -r are
unsorted, because the only thing likely to use those symtabs is the
linker, which doesn't benefit from symtypetab sorting.)
Tests added for all three linking cases (ld -r, ld -shared, ld), with a
bit of testsuite framework enhancement to stop it unconditionally
linking the CTF to be checked by the lookup program with -shared, so
tests can now examine CTF linked with -r or indeed with no flags at all,
though the output filename is still foo.so even in this case.
Another test added for the non-linker case that endeavours to determine
whether the symtypetab is sorted by examining the order of entries
returned from ctf_symbol_next: nobody outside libctf should rely on
this ordering, but this test is not outside libctf :)
include/ChangeLog
2021-01-26 Nick Alcock <nick.alcock@oracle.com>
* ctf-api.h (CTF_LINK_NO_FILTER_REPORTED_SYMS): New.
ld/ChangeLog
2021-01-26 Nick Alcock <nick.alcock@oracle.com>
* ldlang.c (lang_merge_ctf): Set CTF_LINK_NO_FILTER_REPORTED_SYMS
when appropriate.
libctf/ChangeLog
2021-01-27 Nick Alcock <nick.alcock@oracle.com>
* ctf-impl.c (_libctf_nonnull_): Add parameters.
(LCTF_LINKING): New flag.
(ctf_dict_t) <ctf_link_flags>: Mention it.
* ctf-link.c (ctf_link): Keep LCTF_LINKING set across call.
(ctf_write): Likewise, including in child dictionaries.
(ctf_link_shuffle_syms): Make sure ctf_dynsyms is NULL if there
are no reported symbols.
* ctf-create.c (symtypetab_delete_nonstatic_vars): Make sure
the variable has been reported as a symbol by the linker.
(symtypetab_skippable): Mention relationship between SYMFP and the
flags.
(symtypetab_density): Adjust nonnullity. Exit early if no symbols
were reported and force-indexing is off (i.e., we are doing a
final link).
(ctf_serialize): Handle the !LCTF_LINKING case by writing out an
indexed, sorted symtypetab (and allow SYMFP to be NULL in this
case). Turn sorting off if this is a non-final link. Only delete
nonstatic vars if we are filtering symbols and the linker has
reported some.
* testsuite/libctf-regression/nonstatic-var-section-ld-r*:
New test of variable and symtypetab section population when
ld -r is used.
* testsuite/libctf-regression/nonstatic-var-section-ld-executable.lk:
Likewise, when ld of an executable is used.
* testsuite/libctf-regression/nonstatic-var-section-ld.lk:
Likewise, when ld -shared alone is used.
* testsuite/libctf-regression/nonstatic-var-section-ld*.c:
Lookup programs for the above.
* testsuite/libctf-writable/symtypetab-nonlinker-writeout.*: New
test, testing survival of symbols across ctf_write paths.
* testsuite/lib/ctf-lib.exp (run_lookup_test): New option,
nonshared, suppressing linking of the SOURCE with -shared.
Diffstat (limited to 'libctf/ctf-impl.h')
-rw-r--r-- | libctf/ctf-impl.h | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h index d9972e6..0a508eb 100644 --- a/libctf/ctf-impl.h +++ b/libctf/ctf-impl.h @@ -70,7 +70,7 @@ extern "C" #define _libctf_unlikely_(x) __builtin_expect ((x), 0) #define _libctf_unused_ __attribute__ ((__unused__)) #define _libctf_malloc_ __attribute__((__malloc__)) -#define _libctf_nonnull_ __attribute__((__nonnull__)) +#define _libctf_nonnull_(params) __attribute__((__nonnull__ params)) #else @@ -78,7 +78,7 @@ extern "C" #define _libctf_unlikely_(x) (x) #define _libctf_unused_ #define _libctf_malloc_ -#define _libctf_nonnull_ +#define _libctf_nonnull_(params) #define __extension__ #endif @@ -469,7 +469,8 @@ struct ctf_dict individual value members are shared with ctf_link_in_cu_mapping. */ ctf_dynhash_t *ctf_link_out_cu_mapping; - /* CTF linker flags. */ + /* CTF linker flags. Set on the parent output dict (the one passed to + ctf_link). Only respected when LCTF_LINKING set in ctf_flags. */ int ctf_link_flags; /* Allow the caller to change the name of link archive members. */ @@ -595,6 +596,7 @@ struct ctf_next #define LCTF_CHILD 0x0001 /* CTF dict is a child. */ #define LCTF_RDWR 0x0002 /* CTF dict is writable. */ #define LCTF_DIRTY 0x0004 /* CTF dict has been modified. */ +#define LCTF_LINKING 0x0008 /* CTF link is underway: respect ctf_link_flags. */ extern ctf_names_t *ctf_name_table (ctf_dict_t *, int); extern const ctf_type_t *ctf_lookup_by_id (ctf_dict_t **, ctf_id_t); |