From 72c83edd92ef15a19ed0c033e25bb5006ee3bdd8 Mon Sep 17 00:00:00 2001 From: Nick Alcock Date: Sat, 13 Jul 2019 21:06:55 +0100 Subject: libctf: add the ctf_link machinery This is the start of work on the core of the linking mechanism for CTF sections. This commit handles the type and string sections. The linker calls these functions in sequence: ctf_link_add_ctf: to add each CTF section in the input in turn to a newly-created ctf_file_t (which will appear in the output, and which itself will become the shared parent that contains types that all TUs have in common (in all link modes) and all types that do not have conflicting definitions between types (by default). Input files that are themselves products of ld -r are supported, though this is not heavily tested yet. ctf_link: called once all input files are added to merge the types in all the input containers into the output container, eliminating duplicates. ctf_link_add_strtab: called once the ELF string table is finalized and all its offsets are known, this calls a callback provided by the linker which returns the string content and offset of every string in the ELF strtab in turn: all these strings which appear in the input CTF strtab are eliminated from it in favour of the ELF strtab: equally, any strings that only appear in the input strtab will reappear in the internal CTF strtab of the output. ctf_link_shuffle_syms (not yet implemented): called once the ELF symtab is finalized, this calls a callback provided by the linker which returns information on every symbol in turn as a ctf_link_sym_t. This is then used to shuffle the function info and data object sections in the CTF section into symbol table order, eliminating the index sections which map those sections to symbol names before that point. Currently just returns ECTF_NOTYET. ctf_link_write: Returns a buffer containing either a serialized ctf_file_t (if there are no types with conflicting definitions in the object files in the link) or a ctf_archive_t containing a large ctf_file_t (the common types) and a bunch of small ones named after individual CUs in which conflicting types are found (containing the conflicting types, and all types that reference them). A threshold size above which compression takes place is passed as one parameter. (Currently, only gzip compression is supported, but I hope to add lzma as well.) Lifetime rules for this are simple: don't close the input CTF files until you've called ctf_link for the last time. We do not assume that symbols or strings passed in by the callback outlast the call to ctf_link_add_strtab or ctf_link_shuffle_syms. Right now, the duplicate elimination mechanism is the one already present as part of the ctf_add_type function, and is not particularly good: it misses numerous actual duplicates, and the conflicting-types detection hardly ever reports that types conflict, even when they do (one of them just tends to get silently dropped): it is also very slow. This will all be fixed in the next few weeks, but the fix hardly touches any of this code, and the linker does work without it, just not as well as it otherwise might. (And when no CTF section is present, there is no effect on performance, of course. So only people using a trunk GCC with not-yet-committed patches will even notice. By the time it gets upstream, things should be better.) v3: Fix error handling. v4: check for strdup failure. v5: fix tabdamage. include/ * ctf-api.h (struct ctf_link_sym): New, a symbol in flight to the libctf linking machinery. (CTF_LINK_SHARE_UNCONFLICTED): New. (CTF_LINK_SHARE_DUPLICATED): New. (ECTF_LINKADDEDLATE): New, replacing ECTF_UNUSED. (ECTF_NOTYET): New, a 'not yet implemented' message. (ctf_link_add_ctf): New, add an input file's CTF to the link. (ctf_link): New, merge the type and string sections. (ctf_link_strtab_string_f): New, callback for feeding strtab info. (ctf_link_iter_symbol_f): New, callback for feeding symtab info. (ctf_link_add_strtab): New, tell the CTF linker about the ELF strtab's strings. (ctf_link_shuffle_syms): New, ask the CTF linker to shuffle its symbols into symtab order. (ctf_link_write): New, ask the CTF linker to write the CTF out. libctf/ * ctf-link.c: New file, linking of the string and type sections. * Makefile.am (libctf_a_SOURCES): Add it. * Makefile.in: Regenerate. * ctf-impl.h (ctf_file_t): New fields ctf_link_inputs, ctf_link_outputs. * ctf-create.c (ctf_update): Update accordingly. * ctf-open.c (ctf_file_close): Likewise. * ctf-error.c (_ctf_errlist): Updated with new errors. --- include/ChangeLog | 18 ++++++++++++++++++ include/ctf-api.h | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/ChangeLog b/include/ChangeLog index ddc5667..6980ec2 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,5 +1,23 @@ 2019-07-13 Nick Alcock + * ctf-api.h (struct ctf_link_sym): New, a symbol in flight to the + libctf linking machinery. + (CTF_LINK_SHARE_UNCONFLICTED): New. + (CTF_LINK_SHARE_DUPLICATED): New. + (ECTF_LINKADDEDLATE): New, replacing ECTF_UNUSED. + (ECTF_NOTYET): New, a 'not yet implemented' message. + (ctf_link_add_ctf): New, add an input file's CTF to the link. + (ctf_link): New, merge the type and string sections. + (ctf_link_strtab_string_f): New, callback for feeding strtab info. + (ctf_link_iter_symbol_f): New, callback for feeding symtab info. + (ctf_link_add_strtab): New, tell the CTF linker about the ELF + strtab's strings. + (ctf_link_shuffle_syms): New, ask the CTF linker to shuffle its + symbols into symtab order. + (ctf_link_write): New, ask the CTF linker to write the CTF out. + +2019-07-13 Nick Alcock + * ctf-api.h (ctf_arc_write_fd): New. (ctf_write_mem): Likewise. (ctf_gzwrite): Spacing fix. diff --git a/include/ctf-api.h b/include/ctf-api.h index 2bee08b..e4c6f9f 100644 --- a/include/ctf-api.h +++ b/include/ctf-api.h @@ -65,6 +65,28 @@ typedef struct ctf_sect size_t cts_entsize; /* Size of each section entry (symtab only). */ } ctf_sect_t; +/* A minimal symbol extracted from a linker's internal symbol table + representation. */ + +typedef struct ctf_link_sym +{ + /* The st_name will not be accessed outside the call to + ctf_link_shuffle_syms(). */ + + const char *st_name; + uint32_t st_shndx; + uint32_t st_type; + uint32_t st_value; +} ctf_link_sym_t; + +/* Indication of how to share types when linking. */ + +/* Share all types thare are not in conflict. The default. */ +#define CTF_LINK_SHARE_UNCONFLICTED 0x0 + +/* Share only types that are used by multiple inputs. Not implemented yet. */ +#define CTF_LINK_SHARE_DUPLICATED 0x1 + /* Symbolic names for CTF sections. */ typedef enum ctf_sect_names @@ -145,7 +167,7 @@ enum ECTF_NOSYMTAB, /* Symbol table data is not available. */ ECTF_NOPARENT, /* Parent CTF container is not available. */ ECTF_DMODEL, /* Data model mismatch. */ - ECTF_UNUSED, /* Unused error. */ + ECTF_LINKADDEDLATE, /* File added to link too late. */ ECTF_ZALLOC, /* Failed to allocate (de)compression buffer. */ ECTF_DECOMPRESS, /* Failed to decompress CTF data. */ ECTF_STRTAB, /* String table for this string is missing. */ @@ -180,7 +202,8 @@ enum ECTF_ARNNAME, /* Name not found in CTF archive. */ ECTF_SLICEOVERFLOW, /* Overflow of type bitness or offset in slice. */ ECTF_DUMPSECTUNKNOWN, /* Unknown section number in dump. */ - ECTF_DUMPSECTCHANGED /* Section changed in middle of dump. */ + ECTF_DUMPSECTCHANGED, /* Section changed in middle of dump. */ + ECTF_NOTYET /* Feature not yet implemented. */ }; /* The CTF data model is inferred to be the caller's data model or the data @@ -385,6 +408,18 @@ extern int ctf_gzwrite (ctf_file_t *fp, gzFile fd); extern int ctf_compress_write (ctf_file_t * fp, int fd); extern unsigned char *ctf_write_mem (ctf_file_t *, size_t *, size_t threshold); +extern int ctf_link_add_ctf (ctf_file_t *, ctf_archive_t *, const char *); +extern int ctf_link (ctf_file_t *, int share_mode); +typedef const char *ctf_link_strtab_string_f (uint32_t *offset, void *arg); +extern int ctf_link_add_strtab (ctf_file_t *, ctf_link_strtab_string_f *, + void *); +typedef ctf_link_sym_t *ctf_link_iter_symbol_f (ctf_link_sym_t *dest, + void *arg); +extern int ctf_link_shuffle_syms (ctf_file_t *, ctf_link_iter_symbol_f *, + void *); +extern unsigned char *ctf_link_write (ctf_file_t *, size_t *size, + size_t threshold); + extern void ctf_setdebug (int debug); extern int ctf_getdebug (void); -- cgit v1.1