2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
27 /* Type tracking machinery. */
29 /* Record the correspondence between a source and ctf_add_type()-added
30 destination type: both types are translated into parent type IDs if need be,
31 so they relate to the actual dictionary they are in. Outside controlled
32 circumstances (like linking) it is probably not useful to do more than
33 compare these pointers, since there is nothing stopping the user closing the
34 source dict whenever they want to.
36 Our OOM handling here is just to not do anything, because this is called deep
37 enough in the call stack that doing anything useful is painfully difficult:
38 the worst consequence if we do OOM is a bit of type duplication anyway. */
41 ctf_add_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type,
42 ctf_dict_t *dst_fp, ctf_id_t dst_type)
44 if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent)
45 src_fp = src_fp->ctf_parent;
47 src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
49 if (LCTF_TYPE_ISPARENT (dst_fp, dst_type) && dst_fp->ctf_parent)
50 dst_fp = dst_fp->ctf_parent;
52 dst_type = LCTF_TYPE_TO_INDEX(dst_fp, dst_type);
54 if (dst_fp->ctf_link_type_mapping == NULL)
56 ctf_hash_fun f = ctf_hash_type_key;
57 ctf_hash_eq_fun e = ctf_hash_eq_type_key;
59 if ((dst_fp->ctf_link_type_mapping = ctf_dynhash_create (f, e, free,
64 ctf_link_type_key_t *key;
65 key = calloc (1, sizeof (struct ctf_link_type_key));
69 key->cltk_fp = src_fp;
70 key->cltk_idx = src_type;
72 /* No OOM checking needed, because if this doesn't work the worst we'll do is
73 add a few more duplicate types (which will probably run out of memory
75 ctf_dynhash_insert (dst_fp->ctf_link_type_mapping, key,
76 (void *) (uintptr_t) dst_type);
79 /* Look up a type mapping: return 0 if none. The DST_FP is modified to point to
80 the parent if need be. The ID returned is from the dst_fp's perspective. */
82 ctf_type_mapping (ctf_dict_t *src_fp, ctf_id_t src_type, ctf_dict_t **dst_fp)
84 ctf_link_type_key_t key;
85 ctf_dict_t *target_fp = *dst_fp;
86 ctf_id_t dst_type = 0;
88 if (LCTF_TYPE_ISPARENT (src_fp, src_type) && src_fp->ctf_parent)
89 src_fp = src_fp->ctf_parent;
91 src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
93 key.cltk_idx = src_type;
95 if (target_fp->ctf_link_type_mapping)
96 dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping,
101 dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
102 target_fp->ctf_parent != NULL);
107 if (target_fp->ctf_parent)
108 target_fp = target_fp->ctf_parent;
112 if (target_fp->ctf_link_type_mapping)
113 dst_type = (uintptr_t) ctf_dynhash_lookup (target_fp->ctf_link_type_mapping,
117 dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
118 target_fp->ctf_parent != NULL);
126 CTF linking consists of adding CTF archives full of content to be merged into
127 this one to the current file (which must be writable) by calling
128 ctf_link_add_ctf(). Once this is done, a call to ctf_link() will merge the
129 type tables together, generating new CTF files as needed, with this one as a
130 parent, to contain types from the inputs which conflict.
131 ctf_link_add_strtab() takes a callback which provides string/offset pairs to
132 be added to the external symbol table and deduplicated from all CTF string
133 tables in the output link; ctf_link_shuffle_syms() takes a callback which
134 provides symtab entries in ascending order, and shuffles the function and
135 data sections to match; and ctf_link_write() emits a CTF file (if there are
136 no conflicts requiring per-compilation-unit sub-CTF files) or CTF archives
137 (otherwise) and returns it, suitable for addition in the .ctf section of the
140 /* Return the name of the compilation unit this CTF dict or its parent applies
141 to, or a non-null string otherwise: prefer the parent. Used in debugging
142 output. Sometimes used for outputs too. */
144 ctf_link_input_name (ctf_dict_t *fp)
146 if (fp->ctf_parent && fp->ctf_parent->ctf_cuname)
147 return fp->ctf_parent->ctf_cuname;
148 else if (fp->ctf_cuname)
149 return fp->ctf_cuname;
154 /* The linker inputs look like this. clin_fp is used for short-circuited
155 CU-mapped links that can entirely avoid the first link phase in some
156 situations in favour of just passing on the contained ctf_dict_t: it is
157 always the sole ctf_dict_t inside the corresponding clin_arc. If set, it
158 gets assigned directly to the final link inputs and freed from there, so it
159 never gets explicitly freed in the ctf_link_input. */
160 typedef struct ctf_link_input
162 const char *clin_filename;
163 ctf_archive_t *clin_arc;
169 ctf_link_input_close (void *input)
171 ctf_link_input_t *i = (ctf_link_input_t *) input;
173 ctf_arc_close (i->clin_arc);
177 /* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
178 in the middle of an ongoing link. */
180 ctf_link_add_ctf_internal (ctf_dict_t *fp, ctf_archive_t *ctf,
181 ctf_dict_t *fp_input, const char *name)
183 ctf_link_input_t *input = NULL;
184 char *dupname = NULL;
186 if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
189 if ((dupname = strdup (name)) == NULL)
192 input->clin_arc = ctf;
193 input->clin_fp = fp_input;
194 input->clin_filename = dupname;
195 input->n = ctf_dynhash_elements (fp->ctf_link_inputs);
197 if (ctf_dynhash_insert (fp->ctf_link_inputs, dupname, input) < 0)
204 return ctf_set_errno (fp, ENOMEM);
207 /* Add a file, memory buffer, or unopened file (by name) to a link.
209 You can call this with:
211 CTF and NAME: link the passed ctf_archive_t, with the given NAME.
212 NAME alone: open NAME as a CTF file when needed.
213 BUF and NAME: open the BUF (of length N) as CTF, with the given NAME. (Not
216 Passed in CTF args are owned by the dictionary and will be freed by it.
217 The BUF arg is *not* owned by the dictionary, and the user should not free
218 its referent until the link is done.
220 The order of calls to this function influences the order of types in the
221 final link output, but otherwise is not important.
223 Private for now, but may in time become public once support for BUF is
227 ctf_link_add (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name,
228 void *buf _libctf_unused_, size_t n _libctf_unused_)
231 return (ctf_set_errno (fp, ECTF_NOTYET));
233 if (!((ctf && name && !buf)
234 || (name && !buf && !ctf)
235 || (buf && name && !ctf)))
236 return (ctf_set_errno (fp, EINVAL));
238 /* We can only lazily open files if libctf.so is in use rather than
239 libctf-nobfd.so. This is a little tricky: in shared libraries, we can use
240 a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
241 must distinguish between the two libraries explicitly. */
244 if (!buf && !ctf && name && !ctf_open)
245 return (ctf_set_errno (fp, ECTF_NEEDSBFD));
247 if (!buf && !ctf && name)
248 return (ctf_set_errno (fp, ECTF_NEEDSBFD));
251 if (fp->ctf_link_outputs)
252 return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
253 if (fp->ctf_link_inputs == NULL)
254 fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
255 ctf_hash_eq_string, free,
256 ctf_link_input_close);
258 if (fp->ctf_link_inputs == NULL)
259 return (ctf_set_errno (fp, ENOMEM));
261 return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
264 /* Add an opened CTF archive or unopened file (by name) to a link.
265 If CTF is NULL and NAME is non-null, an unopened file is meant:
266 otherwise, the specified archive is assumed to have the given NAME.
268 Passed in CTF args are owned by the dictionary and will be freed by it.
270 The order of calls to this function influences the order of types in the
271 final link output, but otherwise is not important. */
274 ctf_link_add_ctf (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name)
276 return ctf_link_add (fp, ctf, name, NULL, 0);
279 /* Return a per-CU output CTF dictionary suitable for the given CU, creating and
280 interning it if need be. */
283 ctf_create_per_cu (ctf_dict_t *fp, const char *filename, const char *cuname)
286 const char *ctf_name = NULL;
287 char *dynname = NULL;
289 /* First, check the mapping table and translate the per-CU name we use
290 accordingly. We check both the input filename and the CU name. Only if
291 neither are set do we fall back to the input filename as the per-CU
292 dictionary name. We prefer the filename because this is easier for likely
293 callers to determine. */
295 if (fp->ctf_link_in_cu_mapping)
297 if (((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
298 filename)) == NULL) &&
299 ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
304 if (ctf_name == NULL)
307 if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL)
311 if ((cu_fp = ctf_create (&err)) == NULL)
313 ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive for "
314 "CU %s from input file %s"),
316 ctf_set_errno (fp, err);
320 if ((dynname = strdup (ctf_name)) == NULL)
322 if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
325 ctf_import_unref (cu_fp, fp);
326 ctf_cuname_set (cu_fp, cuname);
327 ctf_parent_name_set (cu_fp, _CTF_SECTION);
333 ctf_dict_close (cu_fp);
334 ctf_set_errno (fp, ENOMEM);
338 /* Add a mapping directing that the CU named FROM should have its
339 conflicting/non-duplicate types (depending on link mode) go into a dict
340 named TO. Many FROMs can share a TO.
342 We forcibly add a dict named TO in every case, even though it may well
343 wind up empty, because clients that use this facility usually expect to find
344 every TO dict present, even if empty, and malfunction otherwise. */
347 ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to)
350 char *f = NULL, *t = NULL;
351 ctf_dynhash_t *one_out;
353 if (fp->ctf_link_in_cu_mapping == NULL)
354 fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string,
355 ctf_hash_eq_string, free,
357 if (fp->ctf_link_in_cu_mapping == NULL)
360 if (fp->ctf_link_out_cu_mapping == NULL)
361 fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string,
362 ctf_hash_eq_string, free,
364 ctf_dynhash_destroy);
365 if (fp->ctf_link_out_cu_mapping == NULL)
373 /* Track both in a list from FROM to TO and in a list from TO to a list of
374 FROM. The former is used to create TUs with the mapped-to name at need:
375 the latter is used in deduplicating links to pull in all input CUs
376 corresponding to a single output CU. */
378 if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0)
380 ctf_set_errno (fp, err);
384 /* f and t are now owned by the in_cu_mapping: reallocate them. */
390 if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL)
392 if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
393 free, NULL)) == NULL)
395 if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping,
398 ctf_dynhash_destroy (one_out);
399 ctf_set_errno (fp, err);
406 if (ctf_dynhash_insert (one_out, f, NULL) < 0)
408 ctf_set_errno (fp, err);
415 ctf_set_errno (fp, errno);
422 /* Set a function which is called to transform the names of archive members.
423 This is useful for applying regular transformations to many names, where
424 ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
425 names. The member name changer is applied at ctf_link_write time, so it
426 cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
427 The changer function accepts a name and should return a new
428 dynamically-allocated name, or NULL if the name should be left unchanged. */
430 ctf_link_set_memb_name_changer (ctf_dict_t *fp,
431 ctf_link_memb_name_changer_f *changer,
434 fp->ctf_link_memb_name_changer = changer;
435 fp->ctf_link_memb_name_changer_arg = arg;
438 typedef struct ctf_link_in_member_cb_arg
440 /* The shared output dictionary. */
443 /* The filename of the input file, and an fp to each dictionary in that file
445 const char *in_file_name;
448 /* The CU name of the dict being processed. */
450 int in_input_cu_file;
452 /* The parent dictionary in the input, and whether it's been processed yet.
453 Not needed by ctf_link_one_type / ctf_link_one_variable, only by higher
455 ctf_dict_t *in_fp_parent;
458 /* If true, this is the CU-mapped portion of a deduplicating link: no child
459 dictionaries should be created. */
461 } ctf_link_in_member_cb_arg_t;
463 /* Link one type into the link. We rely on ctf_add_type() to detect
464 duplicates. This is not terribly reliable yet (unnmamed types will be
465 mindlessly duplicated), but will improve shortly. */
468 ctf_link_one_type (ctf_id_t type, int isroot _libctf_unused_, void *arg_)
470 ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
471 ctf_dict_t *per_cu_out_fp;
474 if (arg->in_fp->ctf_link_flags != CTF_LINK_SHARE_UNCONFLICTED)
476 ctf_err_warn (arg->out_fp, 0, ECTF_NOTYET,
477 _("share-duplicated mode not yet implemented"));
478 return ctf_set_errno (arg->out_fp, ECTF_NOTYET);
481 /* Simply call ctf_add_type: if it reports a conflict and we're adding to the
482 main CTF file, add to the per-CU archive member instead, creating it if
483 necessary. If we got this type from a per-CU archive member, add it
484 straight back to the corresponding member in the output. */
486 if (!arg->in_input_cu_file)
488 if (ctf_add_type (arg->out_fp, arg->in_fp, type) != CTF_ERR)
491 err = ctf_errno (arg->out_fp);
492 if (err != ECTF_CONFLICT)
494 if (err != ECTF_NONREPRESENTABLE)
495 ctf_err_warn (arg->out_fp, 1, 0,
496 _("cannot link type %lx from input file %s, CU %s "
497 "into output link"), type, arg->cu_name,
499 /* We must ignore this problem or we end up losing future types, then
500 trying to link the variables in, then exploding. Better to link as
504 ctf_set_errno (arg->out_fp, 0);
507 if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->in_file_name,
508 arg->cu_name)) == NULL)
509 return -1; /* Errno is set for us. */
511 if (ctf_add_type (per_cu_out_fp, arg->in_fp, type) != CTF_ERR)
514 err = ctf_errno (per_cu_out_fp);
515 if (err != ECTF_NONREPRESENTABLE)
516 ctf_err_warn (arg->out_fp, 1, 0,
517 _("cannot link type %lx from input file %s, CU %s "
518 "into output per-CU CTF archive member %s: %s: skipped"),
519 type, ctf_link_input_name (arg->in_fp), arg->in_file_name,
520 ctf_link_input_name (per_cu_out_fp), ctf_errmsg (err));
521 if (err == ECTF_CONFLICT)
522 /* Conflicts are possible at this stage only if a non-ld user has combined
523 multiple TUs into a single output dictionary. Even in this case we do not
524 want to stop the link or propagate the error. */
525 ctf_set_errno (arg->out_fp, 0);
527 return 0; /* As above: do not lose types. */
530 /* Set a function which is used to filter out unwanted variables from the link. */
532 ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter,
535 fp->ctf_link_variable_filter = filter;
536 fp->ctf_link_variable_filter_arg = arg;
540 /* Check if we can safely add a variable with the given type to this dict. */
543 check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type,
544 ctf_dvdef_t **out_dvd)
548 dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
553 if (dvd->dvd_type != type)
555 /* Variable here. Wrong type: cannot add. Just skip it, because there is
556 no way to express this in CTF. Don't even warn: this case is too
557 common. (This might be the parent, in which case we'll try adding in
558 the child first, and only then give up.) */
559 ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
562 return 0; /* Already exists. */
565 /* Link one variable in. */
568 ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_)
570 ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
571 ctf_dict_t *per_cu_out_fp;
572 ctf_id_t dst_type = 0;
573 ctf_dict_t *insert_fp;
576 /* See if this variable is filtered out. */
578 if (arg->out_fp->ctf_link_variable_filter)
580 void *farg = arg->out_fp->ctf_link_variable_filter_arg;
581 if (arg->out_fp->ctf_link_variable_filter (arg->in_fp, name, type, farg))
585 /* In unconflicted link mode, if this type is mapped to a type in the parent
586 dict, we want to try to add to that first: if it reports a duplicate,
587 or if the type is in a child already, add straight to the child. */
589 insert_fp = arg->out_fp;
591 dst_type = ctf_type_mapping (arg->in_fp, type, &insert_fp);
594 if (insert_fp == arg->out_fp)
596 if (check_variable (name, insert_fp, dst_type, &dvd))
598 /* No variable here: we can add it. */
599 if (ctf_add_variable (insert_fp, name, dst_type) < 0)
600 return (ctf_set_errno (arg->out_fp, ctf_errno (insert_fp)));
604 /* Already present? Nothing to do. */
605 if (dvd && dvd->dvd_type == dst_type)
610 /* Can't add to the parent due to a name clash, or because it references a
611 type only present in the child. Try adding to the child, creating if need
612 be. If we can't do that, skip it. Don't add to a child if we're doing a
613 CU-mapped link, since that has only one output. */
617 ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
618 "due to conflicts: skipped.\n", name, arg->in_file_name,
623 if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->in_file_name,
624 arg->cu_name)) == NULL)
625 return -1; /* Errno is set for us. */
627 /* If the type was not found, check for it in the child too. */
630 insert_fp = per_cu_out_fp;
631 dst_type = ctf_type_mapping (arg->in_fp, type, &insert_fp);
635 ctf_err_warn (arg->out_fp, 1, 0,
636 _("type %lx for variable %s in input file %s "
637 "not found: skipped"), type, name,
639 /* Do not terminate the link: just skip the variable. */
644 if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
645 if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
646 return (ctf_set_errno (arg->out_fp, ctf_errno (per_cu_out_fp)));
650 /* Merge every type (and optionally, variable) in this archive member into the
651 link, so we can relink things that have already had ld run on them. We use
652 the archive member name, sans any leading '.ctf.', as the CU name for
653 ambiguous types if there is one and it's not the default: otherwise, we use
654 the name of the input file. */
656 ctf_link_one_input_archive_member (ctf_dict_t *in_fp, const char *name, void *arg_)
658 ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
661 if (strcmp (name, _CTF_SECTION) == 0)
663 /* This file is the default member of this archive, and has already been
664 explicitly processed.
666 In the default sharing mode of CTF_LINK_SHARE_UNCONFLICTED, it does no
667 harm to rescan an existing shared repo again: all the types will just
668 end up in the same place. But in CTF_LINK_SHARE_DUPLICATED mode, this
669 causes the system to erroneously conclude that all types are duplicated
670 and should be shared, even if they are not. */
672 if (arg->done_parent)
677 /* Get ambiguous types from our parent. */
678 ctf_import (in_fp, arg->in_fp_parent);
679 arg->in_input_cu_file = 1;
683 if (strncmp (arg->cu_name, ".ctf.", strlen (".ctf.")) == 0)
684 arg->cu_name += strlen (".ctf.");
687 if ((err = ctf_type_iter_all (in_fp, ctf_link_one_type, arg)) > -1)
688 if (!(in_fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION))
689 err = ctf_variable_iter (in_fp, ctf_link_one_variable, arg);
691 arg->in_input_cu_file = 0;
694 return -1; /* Errno is set for us. */
699 /* Dump the unnecessary link type mapping after one input file is processed. */
701 empty_link_type_mapping (void *key _libctf_unused_, void *value,
702 void *arg _libctf_unused_)
704 ctf_dict_t *fp = (ctf_dict_t *) value;
706 if (fp->ctf_link_type_mapping)
707 ctf_dynhash_empty (fp->ctf_link_type_mapping);
710 /* Lazily open a CTF archive for linking, if not already open.
712 Returns the number of files contained within the opened archive (0 for none),
713 or -1 on error, as usual. */
715 ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input)
721 return ctf_archive_count (input->clin_arc);
726 /* See ctf_link_add_ctf. */
727 #if defined (PIC) || !NOBFD
728 input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
730 ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"),
731 input->clin_filename);
732 ctf_set_errno (fp, ECTF_NEEDSBFD);
736 /* Having no CTF sections is not an error. We just don't need to do
739 if (!input->clin_arc)
741 if (err == ECTF_NOCTFDATA)
744 ctf_err_warn (fp, 0, err, _("opening CTF %s failed"),
745 input->clin_filename);
746 ctf_set_errno (fp, err);
750 if ((count = ctf_archive_count (input->clin_arc)) == 0)
751 ctf_arc_close (input->clin_arc);
753 return (ssize_t) count;
756 /* Close an input, as a ctf_dynhash_iter iterator. */
758 ctf_link_close_one_input_archive (void *key _libctf_unused_, void *value,
759 void *arg _libctf_unused_)
761 ctf_link_input_t *input = (ctf_link_input_t *) value;
763 ctf_arc_close (input->clin_arc);
764 input->clin_arc = NULL;
767 /* Link one input file's types into the output file. */
769 ctf_link_one_input_archive (void *key, void *value, void *arg_)
771 const char *file_name = (const char *) key;
772 ctf_link_input_t *input = (ctf_link_input_t *)value;
773 ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_;
776 if (!input->clin_arc)
778 err = ctf_link_lazy_open (arg->out_fp, input);
779 if (err == 0) /* Just no CTF. */
783 return; /* errno is set for us. */
786 arg->in_file_name = file_name;
787 arg->done_parent = 0;
788 if ((arg->in_fp_parent = ctf_dict_open (input->clin_arc,
789 NULL, &err)) == NULL)
790 if (err != ECTF_ARNNAME)
792 ctf_err_warn (arg->out_fp, 1, 0,
793 _("cannot open main archive member in input file %s "
794 "in the link: skipping: %s"), arg->in_file_name,
799 if (ctf_link_one_input_archive_member (arg->in_fp_parent,
800 _CTF_SECTION, arg) < 0)
802 ctf_dict_close (arg->in_fp_parent);
805 arg->done_parent = 1;
806 if (ctf_archive_iter (input->clin_arc, ctf_link_one_input_archive_member,
808 ctf_err_warn (arg->out_fp, 0, 0, _("cannot traverse archive in input file "
809 "%s: link cannot continue"),
813 /* The only error indication to the caller is the errno: so ensure that it
814 is zero if there was no actual error from the caller. */
815 ctf_set_errno (arg->out_fp, 0);
817 ctf_dict_close (arg->in_fp_parent);
820 ctf_link_close_one_input_archive (key, value, NULL);
823 typedef struct link_sort_inputs_cb_arg
827 } link_sort_inputs_cb_arg_t;
829 /* Sort the inputs by N (the link order). For CU-mapped links, this is a
830 mapping of input to output name, not a mapping of input name to input
831 ctf_link_input_t: compensate accordingly. */
833 ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
836 ctf_link_input_t *input_1;
837 ctf_link_input_t *input_2;
838 link_sort_inputs_cb_arg_t *cu_mapped = (link_sort_inputs_cb_arg_t *) arg;
840 if (!cu_mapped || !cu_mapped->is_cu_mapped)
842 input_1 = (ctf_link_input_t *) one->hkv_value;
843 input_2 = (ctf_link_input_t *) two->hkv_value;
847 const char *name_1 = (const char *) one->hkv_key;
848 const char *name_2 = (const char *) two->hkv_key;
850 input_1 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_1);
851 input_2 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_2);
853 /* There is no guarantee that CU-mappings actually have corresponding
854 inputs: the relative ordering in that case is unimportant. */
861 if (input_1->n < input_2->n)
863 else if (input_1->n > input_2->n)
869 /* Count the number of input dicts in the ctf_link_inputs, or that subset of the
870 ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts,
871 and optionally the name and ctf_link_input_t of the single input archive if
872 only one exists (no matter how many dicts it contains). */
874 ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
875 ctf_link_input_t **only_one_input)
877 ctf_dynhash_t *inputs = fp->ctf_link_inputs;
878 ctf_next_t *i = NULL;
880 ctf_link_input_t *one_input = NULL;
881 const char *one_name = NULL;
882 ssize_t count = 0, narcs = 0;
888 while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0)
892 one_name = (const char *) name;
893 /* If we are processing CU names, get the real input. */
895 one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
897 one_input = (ctf_link_input_t *) input;
902 one_count = ctf_link_lazy_open (fp, one_input);
906 ctf_next_destroy (i);
907 return -1; /* errno is set for us. */
913 if (err != ECTF_NEXT_END)
915 ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating "
917 ctf_set_errno (fp, err);
927 *only_one_input = one_input;
929 else if (only_one_input)
930 *only_one_input = NULL;
935 /* Allocate and populate an inputs array big enough for a given set of inputs:
936 either a specific set of CU names (those from that set found in the
937 ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set).
938 The number of inputs (from ctf_link_deduplicating_count_inputs, above) is
939 passed in NINPUTS: an array of uint32_t containing parent pointers
940 (corresponding to those members of the inputs that have parents) is allocated
941 and returned in PARENTS.
943 The inputs are *archives*, not files: the archive can have multiple members
944 if it is the result of a previous incremental link. We want to add every one
945 in turn, including the shared parent. (The dedup machinery knows that a type
946 used by a single dictionary and its parent should not be shared in
947 CTF_LINK_SHARE_DUPLICATED mode.)
949 If no inputs exist that correspond to these CUs, return NULL with the errno
950 set to ECTF_NOCTFDATA. */
952 ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
953 ssize_t ninputs, uint32_t **parents)
955 ctf_dynhash_t *inputs = fp->ctf_link_inputs;
956 ctf_next_t *i = NULL;
958 link_sort_inputs_cb_arg_t sort_arg;
959 ctf_dict_t **dedup_inputs = NULL;
961 uint32_t *parents_ = NULL;
967 if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL)
970 if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL)
975 /* Counting done: push every input into the array, in the order they were
976 passed to ctf_link_add_ctf (and ultimately ld). */
978 sort_arg.is_cu_mapped = (cu_names != NULL);
981 while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input,
982 ctf_link_sort_inputs, &sort_arg)) == 0)
984 const char *one_name = (const char *) name;
985 ctf_link_input_t *one_input;
987 ctf_dict_t *parent_fp = NULL;
989 ctf_next_t *j = NULL;
991 /* If we are processing CU names, get the real input. All the inputs
992 will have been opened, if they contained any CTF at all. */
994 one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
996 one_input = (ctf_link_input_t *) input;
998 if (!one_input || (!one_input->clin_arc && !one_input->clin_fp))
1001 /* Short-circuit: if clin_fp is set, just use it. */
1002 if (one_input->clin_fp)
1004 parents_[walk - dedup_inputs] = walk - dedup_inputs;
1005 *walk = one_input->clin_fp;
1010 /* Get and insert the parent archive (if any), if this archive has
1011 multiple members. We assume, as elsewhere, that the parent is named
1014 if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION,
1017 if (err != ECTF_NOMEMBNAM)
1019 ctf_next_destroy (i);
1020 ctf_set_errno (fp, err);
1027 parent_i = walk - dedup_inputs;
1031 /* We disregard the input archive name: either it is the parent (which we
1032 already have), or we want to put everything into one TU sharing the
1033 cuname anyway (if this is a CU-mapped link), or this is the final phase
1034 of a relink with CU-mapping off (i.e. ld -r) in which case the cuname
1035 is correctly set regardless. */
1036 while ((one_fp = ctf_archive_next (one_input->clin_arc, &j, NULL,
1039 if (one_fp->ctf_flags & LCTF_CHILD)
1041 /* The contents of the parents array for elements not
1042 corresponding to children is undefined. If there is no parent
1043 (itself a sign of a likely linker bug or corrupt input), we set
1046 ctf_import (one_fp, parent_fp);
1048 parents_[walk - dedup_inputs] = parent_i;
1050 parents_[walk - dedup_inputs] = walk - dedup_inputs;
1055 if (err != ECTF_NEXT_END)
1057 ctf_next_destroy (i);
1061 if (err != ECTF_NEXT_END)
1064 *parents = parents_;
1066 return dedup_inputs;
1072 ctf_set_errno (fp, err);
1075 free (dedup_inputs);
1077 ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link "
1078 "input allocation"));
1082 /* Close INPUTS that have already been linked, first the passed array, and then
1083 that subset of the ctf_link_inputs archives they came from cited by the
1084 CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one
1085 go, leaving it empty. */
1087 ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
1088 ctf_dict_t **inputs, ssize_t ninputs)
1090 ctf_next_t *it = NULL;
1095 /* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close
1096 all the individual input dicts, opened by the archive iterator. */
1097 for (i = 0; i < ninputs; i++)
1098 ctf_dict_close (inputs[i]);
1100 /* Now close the archives they are part of. */
1103 while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0)
1105 /* Remove the input from the linker inputs, if it exists, which also
1108 ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name);
1110 if (err != ECTF_NEXT_END)
1112 ctf_err_warn (fp, 0, err, _("iteration error in deduplicating link "
1114 ctf_set_errno (fp, err);
1118 ctf_dynhash_empty (fp->ctf_link_inputs);
1123 /* Do a deduplicating link of all variables in the inputs. */
1125 ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs,
1126 size_t ninputs, int cu_mapped)
1128 ctf_link_in_member_cb_arg_t arg;
1131 arg.cu_mapped = cu_mapped;
1133 arg.in_input_cu_file = 0;
1135 for (i = 0; i < ninputs; i++)
1137 arg.in_fp = inputs[i];
1138 if (ctf_cuname (inputs[i]) != NULL)
1139 arg.in_file_name = ctf_cuname (inputs[i]);
1141 arg.in_file_name = "unnamed-CU";
1142 arg.cu_name = arg.in_file_name;
1143 if (ctf_variable_iter (arg.in_fp, ctf_link_one_variable, &arg) < 0)
1144 return ctf_set_errno (fp, ctf_errno (arg.in_fp));
1146 /* Outputs > 0 are per-CU. */
1147 arg.in_input_cu_file = 1;
1152 /* Check for symbol conflicts during linking. Three possibilities: already
1153 exists, conflicting, or nonexistent. We don't have a dvd structure we can
1154 use as a flag like check_variable does, so we use a tristate return
1155 value instead: -1: conflicting; 1: nonexistent: 0: already exists. */
1158 check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions)
1160 ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash;
1161 ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash;
1164 /* Wrong type (function when object is wanted, etc). */
1165 if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL))
1168 /* Not present at all yet. */
1169 if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value))
1172 /* Already present. */
1173 if ((ctf_id_t) (uintptr_t) value == type)
1180 /* Do a deduplicating link of one symtypetab (function info or data object) in
1184 ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input,
1185 int cu_mapped, int functions)
1187 ctf_next_t *it = NULL;
1190 const char *in_file_name;
1192 if (ctf_cuname (input) != NULL)
1193 in_file_name = ctf_cuname (input);
1195 in_file_name = "unnamed-CU";
1197 while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR)
1200 ctf_dict_t *per_cu_out_fp;
1201 ctf_dict_t *insert_fp = fp;
1204 /* Look in the parent first. */
1206 dst_type = ctf_type_mapping (input, type, &insert_fp);
1209 if (insert_fp == fp)
1211 sym = check_sym (fp, name, dst_type, functions);
1213 /* Already present: next symbol. */
1216 /* Not present: add it. */
1219 if (ctf_add_funcobjt_sym (fp, functions,
1220 name, dst_type) < 0)
1221 return -1; /* errno is set for us. */
1227 /* Can't add to the parent due to a name clash (most unlikely), or because
1228 it references a type only present in the child. Try adding to the
1229 child, creating if need be. If we can't do that, skip it. Don't add
1230 to a child if we're doing a CU-mapped link, since that has only one
1234 ctf_dprintf ("Symbol %s in input file %s depends on a type %lx "
1235 "hidden due to conflicts: skipped.\n", name,
1236 in_file_name, type);
1240 if ((per_cu_out_fp = ctf_create_per_cu (fp, in_file_name,
1241 in_file_name)) == NULL)
1242 return -1; /* errno is set for us. */
1244 /* If the type was not found, check for it in the child too. */
1247 insert_fp = per_cu_out_fp;
1248 dst_type = ctf_type_mapping (input, type, &insert_fp);
1252 ctf_err_warn (fp, 1, 0,
1253 _("type %lx for symbol %s in input file %s "
1254 "not found: skipped"), type, name, in_file_name);
1259 sym = check_sym (per_cu_out_fp, name, dst_type, functions);
1261 /* Already present: next symbol. */
1264 /* Not present: add it. */
1267 if (ctf_add_funcobjt_sym (per_cu_out_fp, functions,
1268 name, dst_type) < 0)
1269 return -1; /* errno is set for us. */
1273 /* Perhaps this should be an assertion failure. */
1274 ctf_err_warn (fp, 0, ECTF_DUPLICATE,
1275 _("symbol %s in input file %s found conflicting "
1276 "even when trying in per-CU dict."), name,
1278 return (ctf_set_errno (fp, ECTF_DUPLICATE));
1281 if (ctf_errno (input) != ECTF_NEXT_END)
1283 ctf_set_errno (fp, ctf_errno (input));
1284 ctf_err_warn (fp, 0, ctf_errno (input),
1285 functions ? _("iterating over function symbols") :
1286 _("iterating over data symbols"));
1293 /* Do a deduplicating link of the function info and data objects
1296 ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs,
1297 size_t ninputs, int cu_mapped)
1301 for (i = 0; i < ninputs; i++)
1303 if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
1305 return -1; /* errno is set for us. */
1307 if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
1309 return -1; /* errno is set for us. */
1315 /* Do the per-CU part of a deduplicating link. */
1317 ctf_link_deduplicating_per_cu (ctf_dict_t *fp)
1319 ctf_next_t *i = NULL;
1324 /* Links with a per-CU mapping in force get a first pass of deduplication,
1325 dedupping the inputs for a given CU mapping into the output for that
1326 mapping. The outputs from this process get fed back into the final pass
1327 that is carried out even for non-CU links. */
1329 while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &out_cu,
1332 const char *out_name = (const char *) out_cu;
1333 ctf_dynhash_t *in = (ctf_dynhash_t *) in_cus;
1334 ctf_dict_t *out = NULL;
1335 ctf_dict_t **inputs;
1336 ctf_dict_t **outputs;
1337 ctf_archive_t *in_arc;
1339 ctf_link_input_t *only_input;
1343 if ((ninputs = ctf_link_deduplicating_count_inputs (fp, in,
1344 &only_input)) == -1)
1345 goto err_open_inputs;
1347 /* CU mapping with no inputs? Skip. */
1351 if (labs ((long int) ninputs) > 0xfffffffe)
1353 ctf_err_warn (fp, 0, EFBIG, _("too many inputs in deduplicating "
1354 "link: %li"), (long int) ninputs);
1355 ctf_set_errno (fp, EFBIG);
1356 goto err_open_inputs;
1359 /* Short-circuit: a cu-mapped link with only one input archive with
1360 unconflicting contents is a do-nothing, and we can just leave the input
1361 in place: we do have to change the cuname, though, so we unwrap it,
1362 change the cuname, then stuff it back in the linker input again, via
1363 the clin_fp short-circuit member. ctf_link_deduplicating_open_inputs
1364 will spot this member and jam it straight into the next link phase,
1365 ignoring the corresponding archive. */
1366 if (only_input && ninputs == 1)
1368 ctf_next_t *ai = NULL;
1371 /* We can abuse an archive iterator to get the only member cheaply, no
1372 matter what its name. */
1373 only_input->clin_fp = ctf_archive_next (only_input->clin_arc,
1374 &ai, NULL, 0, &err);
1375 if (!only_input->clin_fp)
1377 ctf_err_warn (fp, 0, err, _("cannot open archive %s in "
1378 "CU-mapped CTF link"),
1379 only_input->clin_filename);
1380 ctf_set_errno (fp, err);
1381 goto err_open_inputs;
1383 ctf_next_destroy (ai);
1385 if (strcmp (only_input->clin_filename, out_name) != 0)
1387 /* Renaming. We need to add a new input, then null out the
1388 clin_arc and clin_fp of the old one to stop it being
1389 auto-closed on removal. The new input needs its cuname changed
1390 to out_name, which is doable only because the cuname is a
1391 dynamic property which can be changed even in readonly
1394 ctf_cuname_set (only_input->clin_fp, out_name);
1395 if (ctf_link_add_ctf_internal (fp, only_input->clin_arc,
1396 only_input->clin_fp,
1399 ctf_err_warn (fp, 0, 0, _("cannot add intermediate files "
1401 goto err_open_inputs;
1403 only_input->clin_arc = NULL;
1404 only_input->clin_fp = NULL;
1405 ctf_dynhash_remove (fp->ctf_link_inputs,
1406 only_input->clin_filename);
1411 /* This is a real CU many-to-one mapping: we must dedup the inputs into
1412 a new output to be used in the final link phase. */
1414 if ((inputs = ctf_link_deduplicating_open_inputs (fp, in, ninputs,
1417 ctf_next_destroy (i);
1421 if ((out = ctf_create (&err)) == NULL)
1423 ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive "
1426 ctf_set_errno (fp, err);
1430 /* Share the atoms table to reduce memory usage. */
1431 out->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
1433 /* No ctf_imports at this stage: this per-CU dictionary has no parents.
1434 Parent/child deduplication happens in the link's final pass. However,
1435 the cuname *is* important, as it is propagated into the final
1437 ctf_cuname_set (out, out_name);
1439 if (ctf_dedup (out, inputs, ninputs, parents, 1) < 0)
1441 ctf_set_errno (fp, ctf_errno (out));
1442 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplication failed for %s"),
1447 if ((outputs = ctf_dedup_emit (out, inputs, ninputs, parents,
1448 &noutputs, 1)) == NULL)
1450 ctf_set_errno (fp, ctf_errno (out));
1451 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link type emission "
1452 "failed for %s"), out_name);
1455 if (!ctf_assert (fp, noutputs == 1))
1456 goto err_inputs_outputs;
1458 if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1459 && ctf_link_deduplicating_variables (out, inputs, ninputs, 1) < 0)
1461 ctf_set_errno (fp, ctf_errno (out));
1462 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link variable "
1463 "emission failed for %s"), out_name);
1464 goto err_inputs_outputs;
1467 /* For now, we omit symbol section linking for CU-mapped links, until it
1468 is clear how to unify the symbol table across such links. (Perhaps we
1469 should emit an unconditionally indexed symtab, like the compiler
1472 if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0)
1481 /* Splice any errors or warnings created during this link back into the
1482 dict that the caller knows about. */
1483 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1485 /* This output now becomes an input to the next link phase, with a name
1486 equal to the CU name. We have to wrap it in an archive wrapper
1489 if ((in_arc = ctf_new_archive_internal (0, 0, NULL, outputs[0], NULL,
1490 NULL, &err)) == NULL)
1492 ctf_set_errno (fp, err);
1496 if (ctf_link_add_ctf_internal (fp, in_arc, NULL,
1497 ctf_cuname (outputs[0])) < 0)
1499 ctf_err_warn (fp, 0, 0, _("cannot add intermediate files to link"));
1503 ctf_dict_close (out);
1508 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1509 ctf_dict_close (outputs[0]);
1512 ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs);
1513 ctf_dict_close (out);
1517 ctf_next_destroy (i);
1521 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1522 ctf_dict_close (outputs[0]);
1524 ctf_next_destroy (i);
1525 return -1; /* Errno is set for us. */
1527 if (err != ECTF_NEXT_END)
1529 ctf_err_warn (fp, 0, err, _("iteration error in CU-mapped deduplicating "
1531 return ctf_set_errno (fp, err);
1537 /* Do a deduplicating link using the ctf-dedup machinery. */
1539 ctf_link_deduplicating (ctf_dict_t *fp)
1542 ctf_dict_t **inputs, **outputs = NULL;
1547 if (ctf_dedup_atoms_init (fp) < 0)
1549 ctf_err_warn (fp, 0, 0, _("allocating CTF dedup atoms table"));
1550 return; /* Errno is set for us. */
1553 if (fp->ctf_link_out_cu_mapping
1554 && (ctf_link_deduplicating_per_cu (fp) < 0))
1555 return; /* Errno is set for us. */
1557 if ((ninputs = ctf_link_deduplicating_count_inputs (fp, NULL, NULL)) < 0)
1558 return; /* Errno is set for us. */
1560 if ((inputs = ctf_link_deduplicating_open_inputs (fp, NULL, ninputs,
1562 return; /* Errno is set for us. */
1564 if (ninputs == 1 && ctf_cuname (inputs[0]) != NULL)
1565 ctf_cuname_set (fp, ctf_cuname (inputs[0]));
1567 if (ctf_dedup (fp, inputs, ninputs, parents, 0) < 0)
1569 ctf_err_warn (fp, 0, 0, _("deduplication failed for %s"),
1570 ctf_link_input_name (fp));
1574 if ((outputs = ctf_dedup_emit (fp, inputs, ninputs, parents, &noutputs,
1577 ctf_err_warn (fp, 0, 0, _("deduplicating link type emission failed "
1578 "for %s"), ctf_link_input_name (fp));
1582 if (!ctf_assert (fp, outputs[0] == fp))
1585 for (i = 0; i < noutputs; i++)
1589 /* We already have access to this one. Close the duplicate. */
1592 ctf_dict_close (outputs[0]);
1596 if ((dynname = strdup (ctf_cuname (outputs[i]))) == NULL)
1597 goto oom_one_output;
1599 if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, outputs[i]) < 0)
1600 goto oom_one_output;
1605 ctf_set_errno (fp, ENOMEM);
1606 ctf_err_warn (fp, 0, 0, _("out of memory allocating link outputs"));
1609 for (; i < noutputs; i++)
1610 ctf_dict_close (outputs[i]);
1614 if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1615 && ctf_link_deduplicating_variables (fp, inputs, ninputs, 0) < 0)
1617 ctf_err_warn (fp, 0, 0, _("deduplicating link variable emission failed for "
1618 "%s"), ctf_link_input_name (fp));
1619 goto err_clean_outputs;
1622 if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0)
1624 ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for "
1625 "%s"), ctf_link_input_name (fp));
1626 goto err_clean_outputs;
1629 /* Now close all the inputs, including per-CU intermediates. */
1631 if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0)
1632 return; /* errno is set for us. */
1634 ninputs = 0; /* Prevent double-close. */
1635 ctf_set_errno (fp, 0);
1640 for (i = 0; i < (size_t) ninputs; i++)
1641 ctf_dict_close (inputs[i]);
1648 for (i = 1; i < noutputs; i++)
1650 ctf_dynhash_remove (fp->ctf_link_outputs, ctf_cuname (outputs[i]));
1651 ctf_dict_close (outputs[i]);
1656 /* Merge types and variable sections in all files added to the link
1657 together. All the added files are closed. */
1659 ctf_link (ctf_dict_t *fp, int flags)
1661 ctf_link_in_member_cb_arg_t arg;
1662 ctf_next_t *i = NULL;
1665 memset (&arg, 0, sizeof (struct ctf_link_in_member_cb_arg));
1667 fp->ctf_link_flags = flags;
1669 if (fp->ctf_link_inputs == NULL)
1670 return 0; /* Nothing to do. */
1672 if (fp->ctf_link_outputs == NULL)
1673 fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
1674 ctf_hash_eq_string, free,
1678 if (fp->ctf_link_outputs == NULL)
1679 return ctf_set_errno (fp, ENOMEM);
1681 /* Create empty CUs if requested. We do not currently claim that multiple
1682 links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and
1683 not set in others will do anything especially sensible. */
1685 if (fp->ctf_link_out_cu_mapping && (flags & CTF_LINK_EMPTY_CU_MAPPINGS))
1689 while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &v,
1692 const char *to = (const char *) v;
1693 if (ctf_create_per_cu (fp, to, to) == NULL)
1695 ctf_next_destroy (i);
1696 return -1; /* Errno is set for us. */
1699 if (err != ECTF_NEXT_END)
1701 ctf_err_warn (fp, 1, err, _("iteration error creating empty CUs"));
1702 ctf_set_errno (fp, err);
1707 if ((flags & CTF_LINK_NONDEDUP) || (getenv ("LD_NO_CTF_DEDUP")))
1708 ctf_dynhash_iter (fp->ctf_link_inputs, ctf_link_one_input_archive,
1711 ctf_link_deduplicating (fp);
1713 /* Discard the now-unnecessary mapping table data from all the outputs. */
1714 if (fp->ctf_link_type_mapping)
1715 ctf_dynhash_empty (fp->ctf_link_type_mapping);
1716 ctf_dynhash_iter (fp->ctf_link_outputs, empty_link_type_mapping, NULL);
1718 if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA))
1723 typedef struct ctf_link_out_string_cb_arg
1728 } ctf_link_out_string_cb_arg_t;
1730 /* Intern a string in the string table of an output per-CU CTF file. */
1732 ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
1735 ctf_dict_t *fp = (ctf_dict_t *) value;
1736 ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
1738 fp->ctf_flags |= LCTF_DIRTY;
1739 if (!ctf_str_add_external (fp, arg->str, arg->offset))
1743 /* Repeatedly call ADD_STRING to acquire strings from the external string table,
1744 adding them to the atoms table for this CU and all subsidiary CUs.
1746 If ctf_link() is also called, it must be called first if you want the new CTF
1747 files ctf_link() can create to get their strings dedupped against the ELF
1750 ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string,
1757 while ((str = add_string (&offset, arg)) != NULL)
1759 ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
1761 fp->ctf_flags |= LCTF_DIRTY;
1762 if (!ctf_str_add_external (fp, str, offset))
1765 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
1772 ctf_set_errno (fp, err);
1777 /* Inform the ctf-link machinery of a new symbol in the target symbol table
1778 (which must be some symtab that is not usually stripped, and which
1779 is in agreement with ctf_bfdopen_ctfsect). May be called either before or
1780 after ctf_link_add_strtab. */
1782 ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym)
1784 ctf_in_flight_dynsym_t *cid;
1786 /* Cheat a little: if there is already an ENOMEM error code recorded against
1787 this dict, we shouldn't even try to add symbols because there will be no
1788 memory to do so: probably we failed to add some previous symbol. This
1789 makes out-of-memory exits 'sticky' across calls to this function, so the
1790 caller doesn't need to worry about error conditions. */
1792 if (ctf_errno (fp) == ENOMEM)
1793 return -ENOMEM; /* errno is set for us. */
1795 if (ctf_symtab_skippable (sym))
1798 if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC)
1801 /* Add the symbol to the in-flight list. */
1803 if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL)
1806 cid->cid_sym = *sym;
1807 ctf_list_append (&fp->ctf_in_flight_dynsyms, cid);
1812 ctf_dynhash_destroy (fp->ctf_dynsyms);
1813 fp->ctf_dynsyms = NULL;
1814 ctf_set_errno (fp, ENOMEM);
1818 /* Impose an ordering on symbols. The ordering takes effect immediately, but
1819 since the ordering info does not include type IDs, lookups may return nothing
1820 until such IDs are added by calls to ctf_add_*_sym. Must be called after
1821 ctf_link_add_strtab and ctf_link_add_linker_symbol. */
1823 ctf_link_shuffle_syms (ctf_dict_t *fp)
1825 ctf_in_flight_dynsym_t *did, *nid;
1826 ctf_next_t *i = NULL;
1830 if (!fp->ctf_dynsyms)
1832 fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string,
1835 if (!fp->ctf_dynsyms)
1837 ctf_set_errno (fp, ENOMEM);
1842 /* Add all the symbols, excluding only those we already know are prohibited
1843 from appearing in symtypetabs. */
1845 for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid)
1847 ctf_link_sym_t *new_sym;
1849 nid = ctf_list_next (did);
1850 ctf_list_delete (&fp->ctf_in_flight_dynsyms, did);
1852 /* We might get a name or an external strtab offset. The strtab offset is
1853 guaranteed resolvable at this point, so turn it into a string. */
1855 if (did->cid_sym.st_name == NULL)
1857 uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1);
1859 did->cid_sym.st_name = ctf_strraw (fp, off);
1860 did->cid_sym.st_nameidx_set = 0;
1861 if (!ctf_assert (fp, did->cid_sym.st_name != NULL))
1862 return -ECTF_INTERNAL; /* errno is set for us. */
1865 /* The symbol might have turned out to be nameless, so we have to recheck
1866 for skippability here. */
1867 if (!ctf_symtab_skippable (&did->cid_sym))
1869 ctf_dprintf ("symbol name from linker: %s\n", did->cid_sym.st_name);
1871 if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL)
1874 memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t));
1875 if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0)
1878 if (fp->ctf_dynsymmax < new_sym->st_symidx)
1879 fp->ctf_dynsymmax = new_sym->st_symidx;
1891 /* Construct a mapping from shndx to the symbol info. */
1892 free (fp->ctf_dynsymidx);
1893 if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1,
1894 sizeof (ctf_link_sym_t *))) == NULL)
1897 while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0)
1899 const char *name = (const char *) name;
1900 ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_;
1902 if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax))
1904 ctf_next_destroy (i);
1905 err = ctf_errno (fp);
1908 fp->ctf_dynsymidx[symp->st_symidx] = symp;
1910 if (err != ECTF_NEXT_END)
1912 ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols"));
1918 /* Leave the in-flight symbols around: they'll be freed at
1919 dict close time regardless. */
1920 ctf_dynhash_destroy (fp->ctf_dynsyms);
1921 fp->ctf_dynsyms = NULL;
1922 free (fp->ctf_dynsymidx);
1923 fp->ctf_dynsymidx = NULL;
1924 fp->ctf_dynsymmax = 0;
1925 ctf_set_errno (fp, err);
1929 typedef struct ctf_name_list_accum_cb_arg
1937 } ctf_name_list_accum_cb_arg_t;
1939 /* Accumulate the names and a count of the names in the link output hash. */
1941 ctf_accumulate_archive_names (void *key, void *value, void *arg_)
1943 const char *name = (const char *) key;
1944 ctf_dict_t *fp = (ctf_dict_t *) value;
1947 ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
1949 if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
1952 ctf_set_errno (arg->fp, ENOMEM);
1956 if ((files = realloc (arg->files, sizeof (ctf_dict_t *) * arg->i)) == NULL)
1959 ctf_set_errno (arg->fp, ENOMEM);
1963 /* Allow the caller to get in and modify the name at the last minute. If the
1964 caller *does* modify the name, we have to stash away the new name the
1965 caller returned so we can free it later on. (The original name is the key
1966 of the ctf_link_outputs hash and is freed by the dynhash machinery.) */
1968 if (fp->ctf_link_memb_name_changer)
1972 void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1974 dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
1978 if ((dynames = realloc (arg->dynames,
1979 sizeof (char *) * ++(arg->ndynames))) == NULL)
1982 ctf_set_errno (arg->fp, ENOMEM);
1985 arg->dynames = dynames;
1986 name = (const char *) dyname;
1991 arg->names[(arg->i) - 1] = (char *) name;
1993 arg->files[(arg->i) - 1] = fp;
1996 /* Change the name of the parent CTF section, if the name transformer has got to
1999 ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
2001 ctf_dict_t *fp = (ctf_dict_t *) value;
2002 const char *name = (const char *) arg;
2004 ctf_parent_name_set (fp, name);
2007 /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
2008 (otherwise) into a new dynamically-allocated string, and return it.
2009 Members with sizes above THRESHOLD are compressed. */
2011 ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
2013 ctf_name_list_accum_cb_arg_t arg;
2015 char *transformed_name = NULL;
2021 unsigned char *buf = NULL;
2023 memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
2026 if (fp->ctf_link_outputs)
2028 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
2029 if (ctf_errno (fp) < 0)
2031 errloc = "hash creation";
2036 /* No extra outputs? Just write a simple ctf_dict_t. */
2038 return ctf_write_mem (fp, size, threshold);
2040 /* Writing an archive. Stick ourselves (the shared repository, parent of all
2041 other archives) on the front of it with the default name. */
2042 if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
2044 errloc = "name reallocation";
2048 memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
2050 arg.names[0] = (char *) _CTF_SECTION;
2051 if (fp->ctf_link_memb_name_changer)
2053 void *nc_arg = fp->ctf_link_memb_name_changer_arg;
2055 transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
2058 if (transformed_name != NULL)
2060 arg.names[0] = transformed_name;
2061 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
2066 if ((files = realloc (arg.files,
2067 sizeof (struct ctf_dict *) * (arg.i + 1))) == NULL)
2069 errloc = "ctf_dict reallocation";
2073 memmove (&(arg.files[1]), arg.files, sizeof (ctf_dict_t *) * (arg.i));
2076 if ((f = tmpfile ()) == NULL)
2078 errloc = "tempfile creation";
2082 if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
2083 (const char **) arg.names,
2086 errloc = "archive writing";
2087 ctf_set_errno (fp, err);
2091 if (fseek (f, 0, SEEK_END) < 0)
2093 errloc = "seeking to end";
2097 if ((fsize = ftell (f)) < 0)
2099 errloc = "filesize determination";
2103 if (fseek (f, 0, SEEK_SET) < 0)
2105 errloc = "filepos resetting";
2109 if ((buf = malloc (fsize)) == NULL)
2111 errloc = "CTF archive buffer allocation";
2115 while (!feof (f) && fread (buf, fsize, 1, f) == 0)
2118 errloc = "reading archive from temporary file";
2125 free (transformed_name);
2129 for (i = 0; i < arg.ndynames; i++)
2130 free (arg.dynames[i]);
2137 ctf_set_errno (fp, errno);
2144 free (transformed_name);
2148 for (i = 0; i < arg.ndynames; i++)
2149 free (arg.dynames[i]);
2152 ctf_err_warn (fp, 0, 0, _("cannot write archive in link: %s failure"),