libctf: link: BTF support

This is in two parts, one new API function and one change.

New API:
+int ctf_link_output_is_btf (ctf_dict_t *);

Changed API:
unsigned char *ctf_link_write (ctf_dict_t *, size_t *size,
-			      size_t threshold);
+			      size_t threshold, int *is_btf);

The idea here is that callers can call ctf_link_output_is_btf on a
ctf_link()ed (deduplicated) dict to tell whether a link will yield
BTF-compatible output before actually generating that output, so
they can e.g. decide whether to avoid trying to compress the dict
if they know it would be BTF otherwise (since compressing a dict
renders it non-BTF-compatible).

ctf_link_write() gains an optional is_btf output parameter that
reports whether the dict that was finally generated is actually BTF
after all, perhaps because the caller didn't call
ctf_link_output_is_btf or wants to be robust against possible future
changes that may add other reasons why a written-out dict can't be BTF
at the last minute.

These are simple wrappers around already-existing machinery earlier in
this series.
This commit is contained in:
Nick Alcock
2025-04-25 18:22:07 +01:00
parent 343de78445
commit 9ea8bea7f0
4 changed files with 67 additions and 11 deletions

View File

@@ -1117,7 +1117,10 @@ extern int ctf_write_suppress_kind (ctf_dict_t *fp, int kind, int prohibited);
object files into a single .ctf section which is an archive possibly
containing members containing types whose names collide across multiple
compilation units, but they are usable by other programs as well and are not
private to the linker. */
private to the linker.
They should be called in the order they appear below, though some are
optional. */
/* Add a CTF archive to the link with a given NAME (usually the name of the
containing object file). The dict added to is usually a new dict created
@@ -1136,7 +1139,7 @@ extern int ctf_link_add_ctf (ctf_dict_t *, ctf_archive_t *, const char *name);
extern int ctf_link (ctf_dict_t *, int flags);
/* Symtab linker handling, called after ctf_link to set up the symbol type
information used by ctf_*_lookup_symbol. */
information used by ctf_*_lookup_symbol. Optional. */
/* Add strings to the link from the ELF string table, repeatedly calling
ADD_STRING to add each string and its corresponding offset in turn. */
@@ -1153,19 +1156,28 @@ extern int ctf_link_add_strtab (ctf_dict_t *,
extern int ctf_link_add_linker_symbol (ctf_dict_t *, ctf_link_sym_t *);
/* Impose an ordering on symbols, as defined by the strtab and symbol
added by earlier calls to the above two functions. */
added by earlier calls to the above two functions. Optional. */
extern int ctf_link_shuffle_syms (ctf_dict_t *);
/* Determine the file format of the dict that will be written out after the
calls above is compatible with pure BTF or would require CTF. (Other things
may nonetheless require CTF, in particular, compression.) */
extern int ctf_link_output_is_btf (ctf_dict_t *);
/* Return the serialized form of this ctf_linked dict as a new
dynamically-allocated string, compressed if size over THRESHOLD.
May be a CTF dict or a CTF archive (this library mostly papers over the
differences so you can open both the same way, treat both as ctf_archive_t
and so on). */
and so on).
If IS_BTF is set on return, the output is BTF-compatible and can be stored
in a .BTF section. */
extern unsigned char *ctf_link_write (ctf_dict_t *, size_t *size,
size_t threshold);
size_t threshold, int *is_btf);
/* Specialist linker functions. These functions are not used by ld, but can be
used by other programs making use of the linker machinery for other purposes