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 /* CTF linking consists of adding CTF archives full of content to be merged into
28 this one to the current file (which must be writable) by calling
29 ctf_link_add_ctf. Once this is done, a call to ctf_link will merge the type
30 tables together, generating new CTF files as needed, with this one as a
31 parent, to contain types from the inputs which conflict. ctf_link_add_strtab
32 takes a callback which provides string/offset pairs to be added to the
33 external symbol table and deduplicated from all CTF string tables in the
34 output link; ctf_link_shuffle_syms takes a callback which provides symtab
35 entries in ascending order, and shuffles the function and data sections to
36 match; and ctf_link_write emits a CTF file (if there are no conflicts
37 requiring per-compilation-unit sub-CTF files) or CTF archives (otherwise) and
38 returns it, suitable for addition in the .ctf section of the output. */
40 /* Return the name of the compilation unit this CTF dict or its parent applies
41 to, or a non-null string otherwise: prefer the parent. Used in debugging
42 output. Sometimes used for outputs too. */
44 ctf_link_input_name (ctf_dict_t *fp)
46 if (fp->ctf_parent && fp->ctf_parent->ctf_cuname)
47 return fp->ctf_parent->ctf_cuname;
48 else if (fp->ctf_cuname)
49 return fp->ctf_cuname;
54 /* Return the cuname of a dict, or the string "unnamed-CU" if none. */
57 ctf_unnamed_cuname (ctf_dict_t *fp)
59 const char *cuname = ctf_cuname (fp);
62 cuname = "unnamed-CU";
67 /* The linker inputs look like this. clin_fp is used for short-circuited
68 CU-mapped links that can entirely avoid the first link phase in some
69 situations in favour of just passing on the contained ctf_dict_t: it is
70 always the sole ctf_dict_t inside the corresponding clin_arc. If set, it
71 gets assigned directly to the final link inputs and freed from there, so it
72 never gets explicitly freed in the ctf_link_input. */
73 typedef struct ctf_link_input
75 const char *clin_filename;
76 ctf_archive_t *clin_arc;
82 ctf_link_input_close (void *input)
84 ctf_link_input_t *i = (ctf_link_input_t *) input;
86 ctf_arc_close (i->clin_arc);
90 /* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
91 in the middle of an ongoing link. */
93 ctf_link_add_ctf_internal (ctf_dict_t *fp, ctf_archive_t *ctf,
94 ctf_dict_t *fp_input, const char *name)
96 ctf_link_input_t *input = NULL;
99 if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
102 if ((dupname = strdup (name)) == NULL)
105 input->clin_arc = ctf;
106 input->clin_fp = fp_input;
107 input->clin_filename = dupname;
108 input->n = ctf_dynhash_elements (fp->ctf_link_inputs);
110 if (ctf_dynhash_insert (fp->ctf_link_inputs, dupname, input) < 0)
117 return ctf_set_errno (fp, ENOMEM);
120 /* Add a file, memory buffer, or unopened file (by name) to a link.
122 You can call this with:
124 CTF and NAME: link the passed ctf_archive_t, with the given NAME.
125 NAME alone: open NAME as a CTF file when needed.
126 BUF and NAME: open the BUF (of length N) as CTF, with the given NAME. (Not
129 Passed in CTF args are owned by the dictionary and will be freed by it.
130 The BUF arg is *not* owned by the dictionary, and the user should not free
131 its referent until the link is done.
133 The order of calls to this function influences the order of types in the
134 final link output, but otherwise is not important.
136 Private for now, but may in time become public once support for BUF is
140 ctf_link_add (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name,
141 void *buf _libctf_unused_, size_t n _libctf_unused_)
144 return (ctf_set_errno (fp, ECTF_NOTYET));
146 if (!((ctf && name && !buf)
147 || (name && !buf && !ctf)
148 || (buf && name && !ctf)))
149 return (ctf_set_errno (fp, EINVAL));
151 /* We can only lazily open files if libctf.so is in use rather than
152 libctf-nobfd.so. This is a little tricky: in shared libraries, we can use
153 a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
154 must distinguish between the two libraries explicitly. */
157 if (!buf && !ctf && name && !ctf_open)
158 return (ctf_set_errno (fp, ECTF_NEEDSBFD));
160 if (!buf && !ctf && name)
161 return (ctf_set_errno (fp, ECTF_NEEDSBFD));
164 if (fp->ctf_link_outputs)
165 return (ctf_set_errno (fp, ECTF_LINKADDEDLATE));
166 if (fp->ctf_link_inputs == NULL)
167 fp->ctf_link_inputs = ctf_dynhash_create (ctf_hash_string,
168 ctf_hash_eq_string, free,
169 ctf_link_input_close);
171 if (fp->ctf_link_inputs == NULL)
172 return (ctf_set_errno (fp, ENOMEM));
174 return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
177 /* Add an opened CTF archive or unopened file (by name) to a link.
178 If CTF is NULL and NAME is non-null, an unopened file is meant:
179 otherwise, the specified archive is assumed to have the given NAME.
181 Passed in CTF args are owned by the dictionary and will be freed by it.
183 The order of calls to this function influences the order of types in the
184 final link output, but otherwise is not important. */
187 ctf_link_add_ctf (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name)
189 return ctf_link_add (fp, ctf, name, NULL, 0);
192 /* Return a per-CU output CTF dictionary suitable for the given CU, creating and
193 interning it if need be. */
195 _libctf_nonnull_((1,2))
197 ctf_create_per_cu (ctf_dict_t *fp, const char *cu_name)
200 const char *ctf_name = NULL;
201 char *dynname = NULL;
203 /* First, check the mapping table and translate the per-CU name we use
206 if (fp->ctf_link_in_cu_mapping)
208 if ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
213 if (ctf_name == NULL)
216 if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL)
220 if ((cu_fp = ctf_create (&err)) == NULL)
222 ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive for "
223 "input CU %s"), cu_name);
224 ctf_set_errno (fp, err);
228 if ((dynname = strdup (ctf_name)) == NULL)
230 if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
233 ctf_import_unref (cu_fp, fp);
234 ctf_cuname_set (cu_fp, cu_name);
235 ctf_parent_name_set (cu_fp, _CTF_SECTION);
241 ctf_dict_close (cu_fp);
242 ctf_set_errno (fp, ENOMEM);
246 /* Add a mapping directing that the CU named FROM should have its
247 conflicting/non-duplicate types (depending on link mode) go into a dict
248 named TO. Many FROMs can share a TO.
250 We forcibly add a dict named TO in every case, even though it may well
251 wind up empty, because clients that use this facility usually expect to find
252 every TO dict present, even if empty, and malfunction otherwise. */
255 ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to)
258 char *f = NULL, *t = NULL;
259 ctf_dynhash_t *one_out;
261 if (fp->ctf_link_in_cu_mapping == NULL)
262 fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string,
263 ctf_hash_eq_string, free,
265 if (fp->ctf_link_in_cu_mapping == NULL)
268 if (fp->ctf_link_out_cu_mapping == NULL)
269 fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string,
270 ctf_hash_eq_string, free,
272 ctf_dynhash_destroy);
273 if (fp->ctf_link_out_cu_mapping == NULL)
281 /* Track both in a list from FROM to TO and in a list from TO to a list of
282 FROM. The former is used to create TUs with the mapped-to name at need:
283 the latter is used in deduplicating links to pull in all input CUs
284 corresponding to a single output CU. */
286 if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0)
288 ctf_set_errno (fp, err);
292 /* f and t are now owned by the in_cu_mapping: reallocate them. */
298 if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL)
300 if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
301 free, NULL)) == NULL)
303 if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping,
306 ctf_dynhash_destroy (one_out);
307 ctf_set_errno (fp, err);
314 if (ctf_dynhash_insert (one_out, f, NULL) < 0)
316 ctf_set_errno (fp, err);
323 ctf_set_errno (fp, errno);
330 /* Set a function which is called to transform the names of archive members.
331 This is useful for applying regular transformations to many names, where
332 ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
333 names. The member name changer is applied at ctf_link_write time, so it
334 cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
335 The changer function accepts a name and should return a new
336 dynamically-allocated name, or NULL if the name should be left unchanged. */
338 ctf_link_set_memb_name_changer (ctf_dict_t *fp,
339 ctf_link_memb_name_changer_f *changer,
342 fp->ctf_link_memb_name_changer = changer;
343 fp->ctf_link_memb_name_changer_arg = arg;
346 /* Set a function which is used to filter out unwanted variables from the link. */
348 ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter,
351 fp->ctf_link_variable_filter = filter;
352 fp->ctf_link_variable_filter_arg = arg;
356 /* Check if we can safely add a variable with the given type to this dict. */
359 check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type,
360 ctf_dvdef_t **out_dvd)
364 dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
369 if (dvd->dvd_type != type)
371 /* Variable here. Wrong type: cannot add. Just skip it, because there is
372 no way to express this in CTF. Don't even warn: this case is too
373 common. (This might be the parent, in which case we'll try adding in
374 the child first, and only then give up.) */
375 ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
378 return 0; /* Already exists. */
381 /* Link one variable named NAME of type TYPE found in IN_FP into FP. */
384 ctf_link_one_variable (ctf_dict_t *fp, ctf_dict_t *in_fp, const char *name,
385 ctf_id_t type, int cu_mapped)
387 ctf_dict_t *per_cu_out_fp;
388 ctf_id_t dst_type = 0;
391 /* See if this variable is filtered out. */
393 if (fp->ctf_link_variable_filter)
395 void *farg = fp->ctf_link_variable_filter_arg;
396 if (fp->ctf_link_variable_filter (in_fp, name, type, farg))
400 /* If this type is mapped to a type in the parent dict, we want to try to add
401 to that first: if it reports a duplicate, or if the type is in a child
402 already, add straight to the child. */
404 if ((dst_type = ctf_dedup_type_mapping (fp, in_fp, type)) == CTF_ERR)
405 return -1; /* errno is set for us. */
409 if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
410 return -1; /* errno is set for us. */
412 if (check_variable (name, fp, dst_type, &dvd))
414 /* No variable here: we can add it. */
415 if (ctf_add_variable (fp, name, dst_type) < 0)
416 return -1; /* errno is set for us. */
420 /* Already present? Nothing to do. */
421 if (dvd && dvd->dvd_type == dst_type)
425 /* Can't add to the parent due to a name clash, or because it references a
426 type only present in the child. Try adding to the child, creating if need
427 be. If we can't do that, skip it. Don't add to a child if we're doing a
428 CU-mapped link, since that has only one output. */
432 ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
433 "due to conflicts: skipped.\n", name,
434 ctf_unnamed_cuname (in_fp), type);
438 if ((per_cu_out_fp = ctf_create_per_cu (fp, ctf_unnamed_cuname (in_fp))) == NULL)
439 return -1; /* errno is set for us. */
441 /* If the type was not found, check for it in the child too. */
444 if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
445 in_fp, type)) == CTF_ERR)
446 return -1; /* errno is set for us. */
450 ctf_err_warn (fp, 1, 0, _("type %lx for variable %s in input file %s "
451 "not found: skipped"), type, name,
452 ctf_unnamed_cuname (in_fp));
453 /* Do not terminate the link: just skip the variable. */
458 if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
459 if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
460 return (ctf_set_errno (fp, ctf_errno (per_cu_out_fp)));
464 /* Lazily open a CTF archive for linking, if not already open.
466 Returns the number of files contained within the opened archive (0 for none),
467 or -1 on error, as usual. */
469 ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input)
475 return ctf_archive_count (input->clin_arc);
480 /* See ctf_link_add_ctf. */
481 #if defined (PIC) || !NOBFD
482 input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
484 ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"),
485 input->clin_filename);
486 ctf_set_errno (fp, ECTF_NEEDSBFD);
490 /* Having no CTF sections is not an error. We just don't need to do
493 if (!input->clin_arc)
495 if (err == ECTF_NOCTFDATA)
498 ctf_err_warn (fp, 0, err, _("opening CTF %s failed"),
499 input->clin_filename);
500 ctf_set_errno (fp, err);
504 if ((count = ctf_archive_count (input->clin_arc)) == 0)
505 ctf_arc_close (input->clin_arc);
507 return (ssize_t) count;
510 typedef struct link_sort_inputs_cb_arg
514 } link_sort_inputs_cb_arg_t;
516 /* Sort the inputs by N (the link order). For CU-mapped links, this is a
517 mapping of input to output name, not a mapping of input name to input
518 ctf_link_input_t: compensate accordingly. */
520 ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
523 ctf_link_input_t *input_1;
524 ctf_link_input_t *input_2;
525 link_sort_inputs_cb_arg_t *cu_mapped = (link_sort_inputs_cb_arg_t *) arg;
527 if (!cu_mapped || !cu_mapped->is_cu_mapped)
529 input_1 = (ctf_link_input_t *) one->hkv_value;
530 input_2 = (ctf_link_input_t *) two->hkv_value;
534 const char *name_1 = (const char *) one->hkv_key;
535 const char *name_2 = (const char *) two->hkv_key;
537 input_1 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_1);
538 input_2 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_2);
540 /* There is no guarantee that CU-mappings actually have corresponding
541 inputs: the relative ordering in that case is unimportant. */
548 if (input_1->n < input_2->n)
550 else if (input_1->n > input_2->n)
556 /* Count the number of input dicts in the ctf_link_inputs, or that subset of the
557 ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts,
558 and optionally the name and ctf_link_input_t of the single input archive if
559 only one exists (no matter how many dicts it contains). */
561 ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
562 ctf_link_input_t **only_one_input)
564 ctf_dynhash_t *inputs = fp->ctf_link_inputs;
565 ctf_next_t *i = NULL;
567 ctf_link_input_t *one_input = NULL;
568 const char *one_name = NULL;
569 ssize_t count = 0, narcs = 0;
575 while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0)
579 one_name = (const char *) name;
580 /* If we are processing CU names, get the real input. */
582 one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
584 one_input = (ctf_link_input_t *) input;
589 one_count = ctf_link_lazy_open (fp, one_input);
593 ctf_next_destroy (i);
594 return -1; /* errno is set for us. */
600 if (err != ECTF_NEXT_END)
602 ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating "
604 ctf_set_errno (fp, err);
614 *only_one_input = one_input;
616 else if (only_one_input)
617 *only_one_input = NULL;
622 /* Allocate and populate an inputs array big enough for a given set of inputs:
623 either a specific set of CU names (those from that set found in the
624 ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set).
625 The number of inputs (from ctf_link_deduplicating_count_inputs, above) is
626 passed in NINPUTS: an array of uint32_t containing parent pointers
627 (corresponding to those members of the inputs that have parents) is allocated
628 and returned in PARENTS.
630 The inputs are *archives*, not files: the archive can have multiple members
631 if it is the result of a previous incremental link. We want to add every one
632 in turn, including the shared parent. (The dedup machinery knows that a type
633 used by a single dictionary and its parent should not be shared in
634 CTF_LINK_SHARE_DUPLICATED mode.)
636 If no inputs exist that correspond to these CUs, return NULL with the errno
637 set to ECTF_NOCTFDATA. */
639 ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
640 ssize_t ninputs, uint32_t **parents)
642 ctf_dynhash_t *inputs = fp->ctf_link_inputs;
643 ctf_next_t *i = NULL;
645 link_sort_inputs_cb_arg_t sort_arg;
646 ctf_dict_t **dedup_inputs = NULL;
648 uint32_t *parents_ = NULL;
654 if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL)
657 if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL)
662 /* Counting done: push every input into the array, in the order they were
663 passed to ctf_link_add_ctf (and ultimately ld). */
665 sort_arg.is_cu_mapped = (cu_names != NULL);
668 while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input,
669 ctf_link_sort_inputs, &sort_arg)) == 0)
671 const char *one_name = (const char *) name;
672 ctf_link_input_t *one_input;
674 ctf_dict_t *parent_fp = NULL;
676 ctf_next_t *j = NULL;
678 /* If we are processing CU names, get the real input. All the inputs
679 will have been opened, if they contained any CTF at all. */
681 one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
683 one_input = (ctf_link_input_t *) input;
685 if (!one_input || (!one_input->clin_arc && !one_input->clin_fp))
688 /* Short-circuit: if clin_fp is set, just use it. */
689 if (one_input->clin_fp)
691 parents_[walk - dedup_inputs] = walk - dedup_inputs;
692 *walk = one_input->clin_fp;
697 /* Get and insert the parent archive (if any), if this archive has
698 multiple members. We assume, as elsewhere, that the parent is named
701 if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION,
704 if (err != ECTF_NOMEMBNAM)
706 ctf_next_destroy (i);
707 ctf_set_errno (fp, err);
714 parent_i = walk - dedup_inputs;
718 /* We disregard the input archive name: either it is the parent (which we
719 already have), or we want to put everything into one TU sharing the
720 cuname anyway (if this is a CU-mapped link), or this is the final phase
721 of a relink with CU-mapping off (i.e. ld -r) in which case the cuname
722 is correctly set regardless. */
723 while ((one_fp = ctf_archive_next (one_input->clin_arc, &j, NULL,
726 if (one_fp->ctf_flags & LCTF_CHILD)
728 /* The contents of the parents array for elements not
729 corresponding to children is undefined. If there is no parent
730 (itself a sign of a likely linker bug or corrupt input), we set
733 ctf_import (one_fp, parent_fp);
735 parents_[walk - dedup_inputs] = parent_i;
737 parents_[walk - dedup_inputs] = walk - dedup_inputs;
742 if (err != ECTF_NEXT_END)
744 ctf_next_destroy (i);
748 if (err != ECTF_NEXT_END)
759 ctf_set_errno (fp, err);
764 ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link "
765 "input allocation"));
769 /* Close INPUTS that have already been linked, first the passed array, and then
770 that subset of the ctf_link_inputs archives they came from cited by the
771 CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one
772 go, leaving it empty. */
774 ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
775 ctf_dict_t **inputs, ssize_t ninputs)
777 ctf_next_t *it = NULL;
782 /* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close
783 all the individual input dicts, opened by the archive iterator. */
784 for (i = 0; i < ninputs; i++)
785 ctf_dict_close (inputs[i]);
787 /* Now close the archives they are part of. */
790 while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0)
792 /* Remove the input from the linker inputs, if it exists, which also
795 ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name);
797 if (err != ECTF_NEXT_END)
799 ctf_err_warn (fp, 0, err, _("iteration error in deduplicating link "
801 ctf_set_errno (fp, err);
805 ctf_dynhash_empty (fp->ctf_link_inputs);
810 /* Do a deduplicating link of all variables in the inputs. */
812 ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs,
813 size_t ninputs, int cu_mapped)
817 for (i = 0; i < ninputs; i++)
819 ctf_next_t *it = NULL;
823 while ((type = ctf_variable_next (inputs[i], &it, &name)) != CTF_ERR)
825 if (ctf_link_one_variable (fp, inputs[i], name, type, cu_mapped) < 0)
827 ctf_next_destroy (it);
828 return -1; /* errno is set for us. */
831 if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
832 return ctf_set_errno (fp, ctf_errno (inputs[i]));
837 /* Check for symbol conflicts during linking. Three possibilities: already
838 exists, conflicting, or nonexistent. We don't have a dvd structure we can
839 use as a flag like check_variable does, so we use a tristate return
840 value instead: -1: conflicting; 1: nonexistent: 0: already exists. */
843 check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions)
845 ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash;
846 ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash;
849 /* Wrong type (function when object is wanted, etc). */
850 if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL))
853 /* Not present at all yet. */
854 if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value))
857 /* Already present. */
858 if ((ctf_id_t) (uintptr_t) value == type)
865 /* Do a deduplicating link of one symtypetab (function info or data object) in
869 ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input,
870 int cu_mapped, int functions)
872 ctf_next_t *it = NULL;
876 while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR)
879 ctf_dict_t *per_cu_out_fp;
882 /* Look in the parent first. */
884 if ((dst_type = ctf_dedup_type_mapping (fp, input, type)) == CTF_ERR)
885 return -1; /* errno is set for us. */
889 if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
890 return -1; /* errno is set for us. */
892 sym = check_sym (fp, name, dst_type, functions);
894 /* Already present: next symbol. */
897 /* Not present: add it. */
900 if (ctf_add_funcobjt_sym (fp, functions,
902 return -1; /* errno is set for us. */
907 /* Can't add to the parent due to a name clash (most unlikely), or because
908 it references a type only present in the child. Try adding to the
909 child, creating if need be. If we can't do that, skip it. Don't add
910 to a child if we're doing a CU-mapped link, since that has only one
914 ctf_dprintf ("Symbol %s in input file %s depends on a type %lx "
915 "hidden due to conflicts: skipped.\n", name,
916 ctf_unnamed_cuname (input), type);
920 if ((per_cu_out_fp = ctf_create_per_cu (fp, ctf_unnamed_cuname (input))) == NULL)
921 return -1; /* errno is set for us. */
923 /* If the type was not found, check for it in the child too. */
926 if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
927 input, type)) == CTF_ERR)
928 return -1; /* errno is set for us. */
932 ctf_err_warn (fp, 1, 0,
933 _("type %lx for symbol %s in input file %s "
934 "not found: skipped"), type, name,
935 ctf_unnamed_cuname (input));
940 sym = check_sym (per_cu_out_fp, name, dst_type, functions);
942 /* Already present: next symbol. */
945 /* Not present: add it. */
948 if (ctf_add_funcobjt_sym (per_cu_out_fp, functions,
950 return -1; /* errno is set for us. */
954 /* Perhaps this should be an assertion failure. */
955 ctf_err_warn (fp, 0, ECTF_DUPLICATE,
956 _("symbol %s in input file %s found conflicting "
957 "even when trying in per-CU dict."), name,
958 ctf_unnamed_cuname (input));
959 return (ctf_set_errno (fp, ECTF_DUPLICATE));
962 if (ctf_errno (input) != ECTF_NEXT_END)
964 ctf_set_errno (fp, ctf_errno (input));
965 ctf_err_warn (fp, 0, ctf_errno (input),
966 functions ? _("iterating over function symbols") :
967 _("iterating over data symbols"));
974 /* Do a deduplicating link of the function info and data objects
977 ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs,
978 size_t ninputs, int cu_mapped)
982 for (i = 0; i < ninputs; i++)
984 if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
986 return -1; /* errno is set for us. */
988 if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
990 return -1; /* errno is set for us. */
996 /* Do the per-CU part of a deduplicating link. */
998 ctf_link_deduplicating_per_cu (ctf_dict_t *fp)
1000 ctf_next_t *i = NULL;
1005 /* Links with a per-CU mapping in force get a first pass of deduplication,
1006 dedupping the inputs for a given CU mapping into the output for that
1007 mapping. The outputs from this process get fed back into the final pass
1008 that is carried out even for non-CU links. */
1010 while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &out_cu,
1013 const char *out_name = (const char *) out_cu;
1014 ctf_dynhash_t *in = (ctf_dynhash_t *) in_cus;
1015 ctf_dict_t *out = NULL;
1016 ctf_dict_t **inputs;
1017 ctf_dict_t **outputs;
1018 ctf_archive_t *in_arc;
1020 ctf_link_input_t *only_input;
1024 if ((ninputs = ctf_link_deduplicating_count_inputs (fp, in,
1025 &only_input)) == -1)
1026 goto err_open_inputs;
1028 /* CU mapping with no inputs? Skip. */
1032 if (labs ((long int) ninputs) > 0xfffffffe)
1034 ctf_err_warn (fp, 0, EFBIG, _("too many inputs in deduplicating "
1035 "link: %li"), (long int) ninputs);
1036 ctf_set_errno (fp, EFBIG);
1037 goto err_open_inputs;
1040 /* Short-circuit: a cu-mapped link with only one input archive with
1041 unconflicting contents is a do-nothing, and we can just leave the input
1042 in place: we do have to change the cuname, though, so we unwrap it,
1043 change the cuname, then stuff it back in the linker input again, via
1044 the clin_fp short-circuit member. ctf_link_deduplicating_open_inputs
1045 will spot this member and jam it straight into the next link phase,
1046 ignoring the corresponding archive. */
1047 if (only_input && ninputs == 1)
1049 ctf_next_t *ai = NULL;
1052 /* We can abuse an archive iterator to get the only member cheaply, no
1053 matter what its name. */
1054 only_input->clin_fp = ctf_archive_next (only_input->clin_arc,
1055 &ai, NULL, 0, &err);
1056 if (!only_input->clin_fp)
1058 ctf_err_warn (fp, 0, err, _("cannot open archive %s in "
1059 "CU-mapped CTF link"),
1060 only_input->clin_filename);
1061 ctf_set_errno (fp, err);
1062 goto err_open_inputs;
1064 ctf_next_destroy (ai);
1066 if (strcmp (only_input->clin_filename, out_name) != 0)
1068 /* Renaming. We need to add a new input, then null out the
1069 clin_arc and clin_fp of the old one to stop it being
1070 auto-closed on removal. The new input needs its cuname changed
1071 to out_name, which is doable only because the cuname is a
1072 dynamic property which can be changed even in readonly
1075 ctf_cuname_set (only_input->clin_fp, out_name);
1076 if (ctf_link_add_ctf_internal (fp, only_input->clin_arc,
1077 only_input->clin_fp,
1080 ctf_err_warn (fp, 0, 0, _("cannot add intermediate files "
1082 goto err_open_inputs;
1084 only_input->clin_arc = NULL;
1085 only_input->clin_fp = NULL;
1086 ctf_dynhash_remove (fp->ctf_link_inputs,
1087 only_input->clin_filename);
1092 /* This is a real CU many-to-one mapping: we must dedup the inputs into
1093 a new output to be used in the final link phase. */
1095 if ((inputs = ctf_link_deduplicating_open_inputs (fp, in, ninputs,
1098 ctf_next_destroy (i);
1102 if ((out = ctf_create (&err)) == NULL)
1104 ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive "
1107 ctf_set_errno (fp, err);
1111 /* Share the atoms table to reduce memory usage. */
1112 out->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
1114 /* No ctf_imports at this stage: this per-CU dictionary has no parents.
1115 Parent/child deduplication happens in the link's final pass. However,
1116 the cuname *is* important, as it is propagated into the final
1118 ctf_cuname_set (out, out_name);
1120 if (ctf_dedup (out, inputs, ninputs, parents, 1) < 0)
1122 ctf_set_errno (fp, ctf_errno (out));
1123 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplication failed for %s"),
1128 if ((outputs = ctf_dedup_emit (out, inputs, ninputs, parents,
1129 &noutputs, 1)) == NULL)
1131 ctf_set_errno (fp, ctf_errno (out));
1132 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link type emission "
1133 "failed for %s"), out_name);
1136 if (!ctf_assert (fp, noutputs == 1))
1139 for (j = 1; j < noutputs; j++)
1140 ctf_dict_close (outputs[j]);
1141 goto err_inputs_outputs;
1144 if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1145 && ctf_link_deduplicating_variables (out, inputs, ninputs, 1) < 0)
1147 ctf_set_errno (fp, ctf_errno (out));
1148 ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link variable "
1149 "emission failed for %s"), out_name);
1150 goto err_inputs_outputs;
1153 ctf_dedup_fini (out, outputs, noutputs);
1155 /* For now, we omit symbol section linking for CU-mapped links, until it
1156 is clear how to unify the symbol table across such links. (Perhaps we
1157 should emit an unconditionally indexed symtab, like the compiler
1160 if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0)
1169 /* Splice any errors or warnings created during this link back into the
1170 dict that the caller knows about. */
1171 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1173 /* This output now becomes an input to the next link phase, with a name
1174 equal to the CU name. We have to wrap it in an archive wrapper
1177 if ((in_arc = ctf_new_archive_internal (0, 0, NULL, outputs[0], NULL,
1178 NULL, &err)) == NULL)
1180 ctf_set_errno (fp, err);
1184 if (ctf_link_add_ctf_internal (fp, in_arc, NULL,
1185 ctf_cuname (outputs[0])) < 0)
1187 ctf_err_warn (fp, 0, 0, _("cannot add intermediate files to link"));
1191 ctf_dict_close (out);
1196 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1197 ctf_dict_close (outputs[0]);
1200 ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs);
1201 ctf_dict_close (out);
1205 ctf_next_destroy (i);
1209 ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1210 ctf_dict_close (outputs[0]);
1212 ctf_next_destroy (i);
1213 return -1; /* Errno is set for us. */
1215 if (err != ECTF_NEXT_END)
1217 ctf_err_warn (fp, 0, err, _("iteration error in CU-mapped deduplicating "
1219 return ctf_set_errno (fp, err);
1225 /* Do a deduplicating link using the ctf-dedup machinery. */
1227 ctf_link_deduplicating (ctf_dict_t *fp)
1230 ctf_dict_t **inputs, **outputs = NULL;
1235 if (ctf_dedup_atoms_init (fp) < 0)
1237 ctf_err_warn (fp, 0, 0, _("allocating CTF dedup atoms table"));
1238 return; /* Errno is set for us. */
1241 if (fp->ctf_link_out_cu_mapping
1242 && (ctf_link_deduplicating_per_cu (fp) < 0))
1243 return; /* Errno is set for us. */
1245 if ((ninputs = ctf_link_deduplicating_count_inputs (fp, NULL, NULL)) < 0)
1246 return; /* Errno is set for us. */
1248 if ((inputs = ctf_link_deduplicating_open_inputs (fp, NULL, ninputs,
1250 return; /* Errno is set for us. */
1252 if (ninputs == 1 && ctf_cuname (inputs[0]) != NULL)
1253 ctf_cuname_set (fp, ctf_cuname (inputs[0]));
1255 if (ctf_dedup (fp, inputs, ninputs, parents, 0) < 0)
1257 ctf_err_warn (fp, 0, 0, _("deduplication failed for %s"),
1258 ctf_link_input_name (fp));
1262 if ((outputs = ctf_dedup_emit (fp, inputs, ninputs, parents, &noutputs,
1265 ctf_err_warn (fp, 0, 0, _("deduplicating link type emission failed "
1266 "for %s"), ctf_link_input_name (fp));
1270 if (!ctf_assert (fp, outputs[0] == fp))
1272 for (i = 1; i < noutputs; i++)
1273 ctf_dict_close (outputs[i]);
1277 for (i = 0; i < noutputs; i++)
1281 /* We already have access to this one. Close the duplicate. */
1284 ctf_dict_close (outputs[0]);
1288 if ((dynname = strdup (ctf_cuname (outputs[i]))) == NULL)
1289 goto oom_one_output;
1291 if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, outputs[i]) < 0)
1292 goto oom_one_output;
1297 ctf_set_errno (fp, ENOMEM);
1298 ctf_err_warn (fp, 0, 0, _("out of memory allocating link outputs"));
1301 for (; i < noutputs; i++)
1302 ctf_dict_close (outputs[i]);
1306 if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1307 && ctf_link_deduplicating_variables (fp, inputs, ninputs, 0) < 0)
1309 ctf_err_warn (fp, 0, 0, _("deduplicating link variable emission failed for "
1310 "%s"), ctf_link_input_name (fp));
1311 goto err_clean_outputs;
1314 if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0)
1316 ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for "
1317 "%s"), ctf_link_input_name (fp));
1318 goto err_clean_outputs;
1321 ctf_dedup_fini (fp, outputs, noutputs);
1323 /* Now close all the inputs, including per-CU intermediates. */
1325 if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0)
1326 return; /* errno is set for us. */
1328 ninputs = 0; /* Prevent double-close. */
1329 ctf_set_errno (fp, 0);
1334 for (i = 0; i < (size_t) ninputs; i++)
1335 ctf_dict_close (inputs[i]);
1342 for (i = 1; i < noutputs; i++)
1344 ctf_dynhash_remove (fp->ctf_link_outputs, ctf_cuname (outputs[i]));
1345 ctf_dict_close (outputs[i]);
1350 /* Merge types and variable sections in all dicts added to the link together.
1351 All the added dicts are closed. */
1353 ctf_link (ctf_dict_t *fp, int flags)
1355 ctf_next_t *i = NULL;
1358 fp->ctf_link_flags = flags;
1360 if (fp->ctf_link_inputs == NULL)
1361 return 0; /* Nothing to do. */
1363 if (fp->ctf_link_outputs == NULL)
1364 fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
1365 ctf_hash_eq_string, free,
1369 if (fp->ctf_link_outputs == NULL)
1370 return ctf_set_errno (fp, ENOMEM);
1372 /* Create empty CUs if requested. We do not currently claim that multiple
1373 links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and
1374 not set in others will do anything especially sensible. */
1376 fp->ctf_flags |= LCTF_LINKING;
1377 if (fp->ctf_link_out_cu_mapping && (flags & CTF_LINK_EMPTY_CU_MAPPINGS))
1381 while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &v,
1384 const char *to = (const char *) v;
1385 if (ctf_create_per_cu (fp, to) == NULL)
1387 fp->ctf_flags &= ~LCTF_LINKING;
1388 ctf_next_destroy (i);
1389 return -1; /* Errno is set for us. */
1392 if (err != ECTF_NEXT_END)
1394 fp->ctf_flags &= ~LCTF_LINKING;
1395 ctf_err_warn (fp, 1, err, _("iteration error creating empty CUs"));
1396 ctf_set_errno (fp, err);
1401 ctf_link_deduplicating (fp);
1403 fp->ctf_flags &= ~LCTF_LINKING;
1404 if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA))
1409 typedef struct ctf_link_out_string_cb_arg
1414 } ctf_link_out_string_cb_arg_t;
1416 /* Intern a string in the string table of an output per-CU CTF file. */
1418 ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
1421 ctf_dict_t *fp = (ctf_dict_t *) value;
1422 ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
1424 fp->ctf_flags |= LCTF_DIRTY;
1425 if (!ctf_str_add_external (fp, arg->str, arg->offset))
1429 /* Repeatedly call ADD_STRING to acquire strings from the external string table,
1430 adding them to the atoms table for this CU and all subsidiary CUs.
1432 If ctf_link is also called, it must be called first if you want the new CTF
1433 files ctf_link can create to get their strings dedupped against the ELF
1436 ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string,
1443 while ((str = add_string (&offset, arg)) != NULL)
1445 ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
1447 fp->ctf_flags |= LCTF_DIRTY;
1448 if (!ctf_str_add_external (fp, str, offset))
1451 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
1458 ctf_set_errno (fp, err);
1463 /* Inform the ctf-link machinery of a new symbol in the target symbol table
1464 (which must be some symtab that is not usually stripped, and which
1465 is in agreement with ctf_bfdopen_ctfsect). May be called either before or
1466 after ctf_link_add_strtab. */
1468 ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym)
1470 ctf_in_flight_dynsym_t *cid;
1472 /* Cheat a little: if there is already an ENOMEM error code recorded against
1473 this dict, we shouldn't even try to add symbols because there will be no
1474 memory to do so: probably we failed to add some previous symbol. This
1475 makes out-of-memory exits 'sticky' across calls to this function, so the
1476 caller doesn't need to worry about error conditions. */
1478 if (ctf_errno (fp) == ENOMEM)
1479 return -ENOMEM; /* errno is set for us. */
1481 if (ctf_symtab_skippable (sym))
1484 if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC)
1487 /* Add the symbol to the in-flight list. */
1489 if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL)
1492 cid->cid_sym = *sym;
1493 ctf_list_append (&fp->ctf_in_flight_dynsyms, cid);
1498 ctf_dynhash_destroy (fp->ctf_dynsyms);
1499 fp->ctf_dynsyms = NULL;
1500 ctf_set_errno (fp, ENOMEM);
1504 /* Impose an ordering on symbols. The ordering takes effect immediately, but
1505 since the ordering info does not include type IDs, lookups may return nothing
1506 until such IDs are added by calls to ctf_add_*_sym. Must be called after
1507 ctf_link_add_strtab and ctf_link_add_linker_symbol. */
1509 ctf_link_shuffle_syms (ctf_dict_t *fp)
1511 ctf_in_flight_dynsym_t *did, *nid;
1512 ctf_next_t *i = NULL;
1516 if (!fp->ctf_dynsyms)
1518 fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string,
1521 if (!fp->ctf_dynsyms)
1523 ctf_set_errno (fp, ENOMEM);
1528 /* Add all the symbols, excluding only those we already know are prohibited
1529 from appearing in symtypetabs. */
1531 for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid)
1533 ctf_link_sym_t *new_sym;
1535 nid = ctf_list_next (did);
1536 ctf_list_delete (&fp->ctf_in_flight_dynsyms, did);
1538 /* We might get a name or an external strtab offset. The strtab offset is
1539 guaranteed resolvable at this point, so turn it into a string. */
1541 if (did->cid_sym.st_name == NULL)
1543 uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1);
1545 did->cid_sym.st_name = ctf_strraw (fp, off);
1546 did->cid_sym.st_nameidx_set = 0;
1547 if (!ctf_assert (fp, did->cid_sym.st_name != NULL))
1548 return -ECTF_INTERNAL; /* errno is set for us. */
1551 /* The symbol might have turned out to be nameless, so we have to recheck
1552 for skippability here. */
1553 if (!ctf_symtab_skippable (&did->cid_sym))
1555 ctf_dprintf ("symbol name from linker: %s\n", did->cid_sym.st_name);
1557 if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL)
1560 memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t));
1561 if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0)
1564 if (fp->ctf_dynsymmax < new_sym->st_symidx)
1565 fp->ctf_dynsymmax = new_sym->st_symidx;
1577 /* If no symbols are reported, unwind what we have done and return. This
1578 makes it a bit easier for the serializer to tell that no symbols have been
1579 reported and that it should look elsewhere for reported symbols. */
1580 if (!ctf_dynhash_elements (fp->ctf_dynsyms))
1582 ctf_dprintf ("No symbols: not a final link.\n");
1583 ctf_dynhash_destroy (fp->ctf_dynsyms);
1584 fp->ctf_dynsyms = NULL;
1588 /* Construct a mapping from shndx to the symbol info. */
1589 free (fp->ctf_dynsymidx);
1590 if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1,
1591 sizeof (ctf_link_sym_t *))) == NULL)
1594 while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0)
1596 const char *name = (const char *) name;
1597 ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_;
1599 if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax))
1601 ctf_next_destroy (i);
1602 err = ctf_errno (fp);
1605 fp->ctf_dynsymidx[symp->st_symidx] = symp;
1607 if (err != ECTF_NEXT_END)
1609 ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols"));
1615 /* Leave the in-flight symbols around: they'll be freed at
1616 dict close time regardless. */
1617 ctf_dynhash_destroy (fp->ctf_dynsyms);
1618 fp->ctf_dynsyms = NULL;
1619 free (fp->ctf_dynsymidx);
1620 fp->ctf_dynsymidx = NULL;
1621 fp->ctf_dynsymmax = 0;
1622 ctf_set_errno (fp, err);
1626 typedef struct ctf_name_list_accum_cb_arg
1634 } ctf_name_list_accum_cb_arg_t;
1636 /* Accumulate the names and a count of the names in the link output hash. */
1638 ctf_accumulate_archive_names (void *key, void *value, void *arg_)
1640 const char *name = (const char *) key;
1641 ctf_dict_t *fp = (ctf_dict_t *) value;
1644 ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
1646 if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
1649 ctf_set_errno (arg->fp, ENOMEM);
1653 if ((files = realloc (arg->files, sizeof (ctf_dict_t *) * arg->i)) == NULL)
1656 ctf_set_errno (arg->fp, ENOMEM);
1660 /* Allow the caller to get in and modify the name at the last minute. If the
1661 caller *does* modify the name, we have to stash away the new name the
1662 caller returned so we can free it later on. (The original name is the key
1663 of the ctf_link_outputs hash and is freed by the dynhash machinery.) */
1665 if (fp->ctf_link_memb_name_changer)
1669 void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1671 dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
1675 if ((dynames = realloc (arg->dynames,
1676 sizeof (char *) * ++(arg->ndynames))) == NULL)
1679 ctf_set_errno (arg->fp, ENOMEM);
1682 arg->dynames = dynames;
1683 name = (const char *) dyname;
1688 arg->names[(arg->i) - 1] = (char *) name;
1690 arg->files[(arg->i) - 1] = fp;
1693 /* Change the name of the parent CTF section, if the name transformer has got to
1696 ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
1698 ctf_dict_t *fp = (ctf_dict_t *) value;
1699 const char *name = (const char *) arg;
1701 ctf_parent_name_set (fp, name);
1704 /* Warn if we may suffer information loss because the CTF input files are too
1705 old. Usually we provide complete backward compatibility, but compiler
1706 changes etc which never hit a release may have a flag in the header that
1707 simply prevents those changes from being used. */
1709 ctf_link_warn_outdated_inputs (ctf_dict_t *fp)
1711 ctf_next_t *i = NULL;
1716 while ((err = ctf_dynhash_next (fp->ctf_link_inputs, &i, &name_, &ifp_)) == 0)
1718 const char *name = (const char *) name_;
1719 ctf_dict_t *ifp = (ctf_dict_t *) ifp_;
1721 if (!(ifp->ctf_header->cth_flags & CTF_F_NEWFUNCINFO)
1722 && (ifp->ctf_header->cth_varoff - ifp->ctf_header->cth_funcoff) > 0)
1723 ctf_err_warn (ifp, 1, 0, _("linker input %s has CTF func info but uses "
1724 "an old, unreleased func info format: "
1725 "this func info section will be dropped."),
1728 if (err != ECTF_NEXT_END)
1729 ctf_err_warn (fp, 0, err, _("error checking for outdated inputs"));
1732 /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
1733 (otherwise) into a new dynamically-allocated string, and return it.
1734 Members with sizes above THRESHOLD are compressed. */
1736 ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
1738 ctf_name_list_accum_cb_arg_t arg;
1740 char *transformed_name = NULL;
1747 unsigned char *buf = NULL;
1749 memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
1751 fp->ctf_flags |= LCTF_LINKING;
1753 ctf_link_warn_outdated_inputs (fp);
1755 if (fp->ctf_link_outputs)
1757 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
1758 if (ctf_errno (fp) < 0)
1760 errloc = "hash creation";
1765 /* No extra outputs? Just write a simple ctf_dict_t. */
1768 unsigned char *ret = ctf_write_mem (fp, size, threshold);
1769 fp->ctf_flags &= ~LCTF_LINKING;
1773 /* Writing an archive. Stick ourselves (the shared repository, parent of all
1774 other archives) on the front of it with the default name. */
1775 if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
1777 errloc = "name reallocation";
1781 memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
1783 arg.names[0] = (char *) _CTF_SECTION;
1784 if (fp->ctf_link_memb_name_changer)
1786 void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1788 transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
1791 if (transformed_name != NULL)
1793 arg.names[0] = transformed_name;
1794 ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
1799 /* Propagate the link flags to all the dicts in this link. */
1800 for (i = 0; i < arg.i; i++)
1802 arg.files[i]->ctf_link_flags = fp->ctf_link_flags;
1803 arg.files[i]->ctf_flags |= LCTF_LINKING;
1806 if ((files = realloc (arg.files,
1807 sizeof (struct ctf_dict *) * (arg.i + 1))) == NULL)
1809 errloc = "ctf_dict reallocation";
1813 memmove (&(arg.files[1]), arg.files, sizeof (ctf_dict_t *) * (arg.i));
1816 if ((f = tmpfile ()) == NULL)
1818 errloc = "tempfile creation";
1822 if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
1823 (const char **) arg.names,
1826 errloc = "archive writing";
1827 ctf_set_errno (fp, err);
1831 if (fseek (f, 0, SEEK_END) < 0)
1833 errloc = "seeking to end";
1837 if ((fsize = ftell (f)) < 0)
1839 errloc = "filesize determination";
1843 if (fseek (f, 0, SEEK_SET) < 0)
1845 errloc = "filepos resetting";
1849 if ((buf = malloc (fsize)) == NULL)
1851 errloc = "CTF archive buffer allocation";
1855 while (!feof (f) && fread (buf, fsize, 1, f) == 0)
1858 errloc = "reading archive from temporary file";
1865 free (transformed_name);
1869 for (i = 0; i < arg.ndynames; i++)
1870 free (arg.dynames[i]);
1877 ctf_set_errno (fp, errno);
1879 /* Turn off the is-linking flag on all the dicts in this link. */
1880 for (i = 0; i < arg.i; i++)
1881 arg.files[i]->ctf_flags &= ~LCTF_LINKING;
1888 free (transformed_name);
1892 for (i = 0; i < arg.ndynames; i++)
1893 free (arg.dynames[i]);
1896 ctf_err_warn (fp, 0, 0, _("cannot write archive in link: %s failure"),