2 Copyright (C) 2019-2024 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
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
);
87 free (i
->clin_filename
);
91 /* Like ctf_link_add_ctf, below, but with no error-checking, so it can be called
92 in the middle of an ongoing link. */
94 ctf_link_add_ctf_internal (ctf_dict_t
*fp
, ctf_archive_t
*ctf
,
95 ctf_dict_t
*fp_input
, const char *name
)
98 ctf_link_input_t
*input
;
99 char *filename
, *keyname
;
101 /* Existing: return it, or (if a different dict with the same name
102 is already there) make up a new unique name. Always use the actual name
103 for the filename, because that needs to be ctf_open()ed. */
105 if ((input
= ctf_dynhash_lookup (fp
->ctf_link_inputs
, name
)) != NULL
)
107 if ((fp_input
!= NULL
&& (input
->clin_fp
== fp_input
))
108 || (ctf
!= NULL
&& (input
->clin_arc
== ctf
)))
113 if ((filename
= strdup (name
)) == NULL
)
116 if ((input
= calloc (1, sizeof (ctf_link_input_t
))) == NULL
)
119 input
->clin_arc
= ctf
;
120 input
->clin_fp
= fp_input
;
121 input
->clin_filename
= filename
;
122 input
->n
= ctf_dynhash_elements (fp
->ctf_link_inputs
);
126 if (asprintf (&keyname
, "%s#%li", name
, (long int)
127 ctf_dynhash_elements (fp
->ctf_link_inputs
)) < 0)
130 else if ((keyname
= strdup (name
)) == NULL
)
133 if (ctf_dynhash_insert (fp
->ctf_link_inputs
, keyname
, input
) < 0)
145 return ctf_set_errno (fp
, ENOMEM
);
148 /* Add a file, memory buffer, or unopened file (by name) to a link.
150 You can call this with:
152 CTF and NAME: link the passed ctf_archive_t, with the given NAME.
153 NAME alone: open NAME as a CTF file when needed.
154 BUF and NAME: open the BUF (of length N) as CTF, with the given NAME. (Not
157 Passed in CTF args are owned by the dictionary and will be freed by it.
158 The BUF arg is *not* owned by the dictionary, and the user should not free
159 its referent until the link is done.
161 The order of calls to this function influences the order of types in the
162 final link output, but otherwise is not important.
164 Repeated additions of the same NAME have no effect; repeated additions of
165 different dicts with the same NAME add all the dicts with unique NAMEs
168 Private for now, but may in time become public once support for BUF is
172 ctf_link_add (ctf_dict_t
*fp
, ctf_archive_t
*ctf
, const char *name
,
173 void *buf _libctf_unused_
, size_t n _libctf_unused_
)
176 return (ctf_set_errno (fp
, ECTF_NOTYET
));
178 if (!((ctf
&& name
&& !buf
)
179 || (name
&& !buf
&& !ctf
)
180 || (buf
&& name
&& !ctf
)))
181 return (ctf_set_errno (fp
, EINVAL
));
183 /* We can only lazily open files if libctf.so is in use rather than
184 libctf-nobfd.so. This is a little tricky: in shared libraries, we can use
185 a weak symbol so that -lctf -lctf-nobfd works, but in static libraries we
186 must distinguish between the two libraries explicitly. */
189 if (!buf
&& !ctf
&& name
&& !ctf_open
)
190 return (ctf_set_errno (fp
, ECTF_NEEDSBFD
));
192 if (!buf
&& !ctf
&& name
)
193 return (ctf_set_errno (fp
, ECTF_NEEDSBFD
));
196 if (fp
->ctf_link_outputs
)
197 return (ctf_set_errno (fp
, ECTF_LINKADDEDLATE
));
198 if (fp
->ctf_link_inputs
== NULL
)
199 fp
->ctf_link_inputs
= ctf_dynhash_create (ctf_hash_string
,
200 ctf_hash_eq_string
, free
,
201 ctf_link_input_close
);
203 if (fp
->ctf_link_inputs
== NULL
)
204 return (ctf_set_errno (fp
, ENOMEM
));
206 return ctf_link_add_ctf_internal (fp
, ctf
, NULL
, name
);
209 /* Add an opened CTF archive or unopened file (by name) to a link.
210 If CTF is NULL and NAME is non-null, an unopened file is meant:
211 otherwise, the specified archive is assumed to have the given NAME.
213 Passed in CTF args are owned by the dictionary and will be freed by it.
215 The order of calls to this function influences the order of types in the
216 final link output, but otherwise is not important. */
219 ctf_link_add_ctf (ctf_dict_t
*fp
, ctf_archive_t
*ctf
, const char *name
)
221 return ctf_link_add (fp
, ctf
, name
, NULL
, 0);
224 /* Lazily open a CTF archive for linking, if not already open.
226 Returns the number of files contained within the opened archive (0 for none),
227 or -1 on error, as usual. */
229 ctf_link_lazy_open (ctf_dict_t
*fp
, ctf_link_input_t
*input
)
235 return ctf_archive_count (input
->clin_arc
);
240 /* See ctf_link_add_ctf. */
241 #if defined (PIC) || !NOBFD
242 input
->clin_arc
= ctf_open (input
->clin_filename
, NULL
, &err
);
244 ctf_err_warn (fp
, 0, ECTF_NEEDSBFD
, _("cannot open %s lazily"),
245 input
->clin_filename
);
246 return ctf_set_errno (fp
, ECTF_NEEDSBFD
);
249 /* Having no CTF sections is not an error. We just don't need to do
252 if (!input
->clin_arc
)
254 if (err
== ECTF_NOCTFDATA
)
257 ctf_err_warn (fp
, 0, err
, _("opening CTF %s failed"),
258 input
->clin_filename
);
259 return ctf_set_errno (fp
, err
);
262 if ((count
= ctf_archive_count (input
->clin_arc
)) == 0)
263 ctf_arc_close (input
->clin_arc
);
265 return (ssize_t
) count
;
268 /* Find a non-clashing unique name for a per-CU output dict, to prevent distinct
269 members corresponding to inputs with identical cunames from overwriting each
270 other. The name should be something like NAME. */
273 ctf_new_per_cu_name (ctf_dict_t
*fp
, const char *name
)
278 if ((dynname
= strdup (name
)) == NULL
)
281 while ((ctf_dynhash_lookup (fp
->ctf_link_outputs
, dynname
)) != NULL
)
284 if (asprintf (&dynname
, "%s#%li", name
, i
++) < 0)
291 /* Return a per-CU output CTF dictionary suitable for the given INPUT or CU,
292 creating and interning it if need be. */
295 ctf_create_per_cu (ctf_dict_t
*fp
, ctf_dict_t
*input
, const char *cu_name
)
298 const char *ctf_name
= NULL
;
299 char *dynname
= NULL
;
301 /* Already has a per-CU mapping? Just return it. */
303 if (input
&& input
->ctf_link_in_out
)
304 return input
->ctf_link_in_out
;
306 /* Check the mapping table and translate the per-CU name we use
310 cu_name
= ctf_unnamed_cuname (input
);
312 if (fp
->ctf_link_in_cu_mapping
)
314 if ((ctf_name
= ctf_dynhash_lookup (fp
->ctf_link_in_cu_mapping
,
319 if (ctf_name
== NULL
)
322 /* Look up the per-CU dict. If we don't know of one, or it is for a different input
323 CU which just happens to have the same name, create a new one. If we are creating
324 a dict with no input specified, anything will do. */
326 if ((cu_fp
= ctf_dynhash_lookup (fp
->ctf_link_outputs
, ctf_name
)) == NULL
327 || (input
&& cu_fp
->ctf_link_in_out
!= fp
))
331 if ((cu_fp
= ctf_create (&err
)) == NULL
)
333 ctf_set_errno (fp
, err
);
334 ctf_err_warn (fp
, 0, 0, _("cannot create per-CU CTF archive for "
335 "input CU %s"), cu_name
);
339 ctf_import_unref (cu_fp
, fp
);
341 if ((dynname
= ctf_new_per_cu_name (fp
, ctf_name
)) == NULL
)
344 ctf_cuname_set (cu_fp
, cu_name
);
346 ctf_parent_name_set (cu_fp
, _CTF_SECTION
);
347 cu_fp
->ctf_link_in_out
= fp
;
348 fp
->ctf_link_in_out
= cu_fp
;
350 if (ctf_dynhash_insert (fp
->ctf_link_outputs
, dynname
, cu_fp
) < 0)
357 ctf_dict_close (cu_fp
);
358 ctf_set_errno (fp
, ENOMEM
);
362 /* Add a mapping directing that the CU named FROM should have its
363 conflicting/non-duplicate types (depending on link mode) go into a dict
364 named TO. Many FROMs can share a TO, but adding the same FROM with
365 a different TO will replace the old mapping.
367 We forcibly add a dict named TO in every case, even though it may well
368 wind up empty, because clients that use this facility usually expect to find
369 every TO dict present, even if empty, and malfunction otherwise. */
372 ctf_link_add_cu_mapping (ctf_dict_t
*fp
, const char *from
, const char *to
)
375 char *f
= NULL
, *t
= NULL
, *existing
;
376 ctf_dynhash_t
*one_out
;
378 /* Mappings cannot be set up if per-CU output dicts already exist. */
379 if (fp
->ctf_link_outputs
&& ctf_dynhash_elements (fp
->ctf_link_outputs
) != 0)
380 return (ctf_set_errno (fp
, ECTF_LINKADDEDLATE
));
382 if (fp
->ctf_link_in_cu_mapping
== NULL
)
383 fp
->ctf_link_in_cu_mapping
= ctf_dynhash_create (ctf_hash_string
,
384 ctf_hash_eq_string
, free
,
386 if (fp
->ctf_link_in_cu_mapping
== NULL
)
389 if (fp
->ctf_link_out_cu_mapping
== NULL
)
390 fp
->ctf_link_out_cu_mapping
= ctf_dynhash_create (ctf_hash_string
,
391 ctf_hash_eq_string
, free
,
393 ctf_dynhash_destroy
);
394 if (fp
->ctf_link_out_cu_mapping
== NULL
)
397 /* If this FROM already exists, remove the mapping from both the FROM->TO
398 and the TO->FROM lists: the user wants to change it. */
400 if ((existing
= ctf_dynhash_lookup (fp
->ctf_link_in_cu_mapping
, from
)) != NULL
)
402 one_out
= ctf_dynhash_lookup (fp
->ctf_link_out_cu_mapping
, existing
);
403 if (!ctf_assert (fp
, one_out
))
404 return -1; /* errno is set for us. */
406 ctf_dynhash_remove (one_out
, from
);
407 ctf_dynhash_remove (fp
->ctf_link_in_cu_mapping
, from
);
415 /* Track both in a list from FROM to TO and in a list from TO to a list of
416 FROM. The former is used to create TUs with the mapped-to name at need:
417 the latter is used in deduplicating links to pull in all input CUs
418 corresponding to a single output CU. */
420 if ((err
= ctf_dynhash_insert (fp
->ctf_link_in_cu_mapping
, f
, t
)) < 0)
422 ctf_set_errno (fp
, err
);
426 /* f and t are now owned by the in_cu_mapping: reallocate them. */
432 if ((one_out
= ctf_dynhash_lookup (fp
->ctf_link_out_cu_mapping
, t
)) == NULL
)
434 if ((one_out
= ctf_dynhash_create (ctf_hash_string
, ctf_hash_eq_string
,
435 free
, NULL
)) == NULL
)
437 if ((err
= ctf_dynhash_insert (fp
->ctf_link_out_cu_mapping
,
440 ctf_dynhash_destroy (one_out
);
441 ctf_set_errno (fp
, err
);
451 if (ctf_dynhash_insert (one_out
, f
, NULL
) < 0)
453 ctf_set_errno (fp
, err
);
460 ctf_set_errno (fp
, errno
);
467 /* Set a function which is called to transform the names of archive members.
468 This is useful for applying regular transformations to many names, where
469 ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
470 names. The member name changer is applied at ctf_link_write time, so it
471 cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
472 The changer function accepts a name and should return a new
473 dynamically-allocated name, or NULL if the name should be left unchanged. */
475 ctf_link_set_memb_name_changer (ctf_dict_t
*fp
,
476 ctf_link_memb_name_changer_f
*changer
,
479 fp
->ctf_link_memb_name_changer
= changer
;
480 fp
->ctf_link_memb_name_changer_arg
= arg
;
483 /* Set a function which is used to filter out unwanted variables from the link. */
485 ctf_link_set_variable_filter (ctf_dict_t
*fp
, ctf_link_variable_filter_f
*filter
,
488 fp
->ctf_link_variable_filter
= filter
;
489 fp
->ctf_link_variable_filter_arg
= arg
;
493 /* Check if we can safely add a variable with the given type to this dict. */
496 check_variable (const char *name
, ctf_dict_t
*fp
, ctf_id_t type
,
497 ctf_dvdef_t
**out_dvd
)
501 dvd
= ctf_dynhash_lookup (fp
->ctf_dvhash
, name
);
506 if (dvd
->dvd_type
!= type
)
508 /* Variable here. Wrong type: cannot add. Just skip it, because there is
509 no way to express this in CTF. Don't even warn: this case is too
510 common. (This might be the parent, in which case we'll try adding in
511 the child first, and only then give up.) */
512 ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name
);
515 return 0; /* Already exists. */
518 /* Link one variable named NAME of type TYPE found in IN_FP into FP. */
521 ctf_link_one_variable (ctf_dict_t
*fp
, ctf_dict_t
*in_fp
, const char *name
,
522 ctf_id_t type
, int cu_mapped
)
524 ctf_dict_t
*per_cu_out_fp
;
525 ctf_id_t dst_type
= 0;
528 /* See if this variable is filtered out. */
530 if (fp
->ctf_link_variable_filter
)
532 void *farg
= fp
->ctf_link_variable_filter_arg
;
533 if (fp
->ctf_link_variable_filter (in_fp
, name
, type
, farg
))
537 /* If this type is mapped to a type in the parent dict, we want to try to add
538 to that first: if it reports a duplicate, or if the type is in a child
539 already, add straight to the child. */
541 if ((dst_type
= ctf_dedup_type_mapping (fp
, in_fp
, type
)) == CTF_ERR
)
542 return -1; /* errno is set for us. */
546 if (!ctf_assert (fp
, ctf_type_isparent (fp
, dst_type
)))
547 return -1; /* errno is set for us. */
549 if (check_variable (name
, fp
, dst_type
, &dvd
))
551 /* No variable here: we can add it. */
552 if (ctf_add_variable (fp
, name
, dst_type
) < 0)
553 return -1; /* errno is set for us. */
557 /* Already present? Nothing to do. */
558 if (dvd
&& dvd
->dvd_type
== dst_type
)
562 /* Can't add to the parent due to a name clash, or because it references a
563 type only present in the child. Try adding to the child, creating if need
564 be. If we can't do that, skip it. Don't add to a child if we're doing a
565 CU-mapped link, since that has only one output. */
569 ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
570 "due to conflicts: skipped.\n", name
,
571 ctf_unnamed_cuname (in_fp
), type
);
575 if ((per_cu_out_fp
= ctf_create_per_cu (fp
, in_fp
, NULL
)) == NULL
)
576 return -1; /* errno is set for us. */
578 /* If the type was not found, check for it in the child too. */
581 if ((dst_type
= ctf_dedup_type_mapping (per_cu_out_fp
,
582 in_fp
, type
)) == CTF_ERR
)
583 return -1; /* errno is set for us. */
587 ctf_err_warn (fp
, 1, 0, _("type %lx for variable %s in input file %s "
588 "not found: skipped"), type
, name
,
589 ctf_unnamed_cuname (in_fp
));
590 /* Do not terminate the link: just skip the variable. */
595 if (check_variable (name
, per_cu_out_fp
, dst_type
, &dvd
))
596 if (ctf_add_variable (per_cu_out_fp
, name
, dst_type
) < 0)
597 return (ctf_set_errno (fp
, ctf_errno (per_cu_out_fp
)));
601 typedef struct link_sort_inputs_cb_arg
605 } link_sort_inputs_cb_arg_t
;
607 /* Sort the inputs by N (the link order). For CU-mapped links, this is a
608 mapping of input to output name, not a mapping of input name to input
609 ctf_link_input_t: compensate accordingly. */
611 ctf_link_sort_inputs (const ctf_next_hkv_t
*one
, const ctf_next_hkv_t
*two
,
614 ctf_link_input_t
*input_1
;
615 ctf_link_input_t
*input_2
;
616 link_sort_inputs_cb_arg_t
*cu_mapped
= (link_sort_inputs_cb_arg_t
*) arg
;
618 if (!cu_mapped
|| !cu_mapped
->is_cu_mapped
)
620 input_1
= (ctf_link_input_t
*) one
->hkv_value
;
621 input_2
= (ctf_link_input_t
*) two
->hkv_value
;
625 const char *name_1
= (const char *) one
->hkv_key
;
626 const char *name_2
= (const char *) two
->hkv_key
;
628 input_1
= ctf_dynhash_lookup (cu_mapped
->fp
->ctf_link_inputs
, name_1
);
629 input_2
= ctf_dynhash_lookup (cu_mapped
->fp
->ctf_link_inputs
, name_2
);
631 /* There is no guarantee that CU-mappings actually have corresponding
632 inputs: the relative ordering in that case is unimportant. */
639 if (input_1
->n
< input_2
->n
)
641 else if (input_1
->n
> input_2
->n
)
647 /* Count the number of input dicts in the ctf_link_inputs, or that subset of the
648 ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts,
649 and optionally the name and ctf_link_input_t of the single input archive if
650 only one exists (no matter how many dicts it contains). */
652 ctf_link_deduplicating_count_inputs (ctf_dict_t
*fp
, ctf_dynhash_t
*cu_names
,
653 ctf_link_input_t
**only_one_input
)
655 ctf_dynhash_t
*inputs
= fp
->ctf_link_inputs
;
656 ctf_next_t
*i
= NULL
;
658 ctf_link_input_t
*one_input
= NULL
;
659 const char *one_name
= NULL
;
660 ssize_t count
= 0, narcs
= 0;
666 while ((err
= ctf_dynhash_next (inputs
, &i
, &name
, &input
)) == 0)
670 one_name
= (const char *) name
;
671 /* If we are processing CU names, get the real input. */
673 one_input
= ctf_dynhash_lookup (fp
->ctf_link_inputs
, one_name
);
675 one_input
= (ctf_link_input_t
*) input
;
680 one_count
= ctf_link_lazy_open (fp
, one_input
);
684 ctf_next_destroy (i
);
685 return -1; /* errno is set for us. */
691 if (err
!= ECTF_NEXT_END
)
693 ctf_err_warn (fp
, 0, err
, _("iteration error counting deduplicating "
695 return ctf_set_errno (fp
, err
);
704 *only_one_input
= one_input
;
706 else if (only_one_input
)
707 *only_one_input
= NULL
;
712 /* Allocate and populate an inputs array big enough for a given set of inputs:
713 either a specific set of CU names (those from that set found in the
714 ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set).
715 The number of inputs (from ctf_link_deduplicating_count_inputs, above) is
716 passed in NINPUTS: an array of uint32_t containing parent pointers
717 (corresponding to those members of the inputs that have parents) is allocated
718 and returned in PARENTS.
720 The inputs are *archives*, not files: the archive can have multiple members
721 if it is the result of a previous incremental link. We want to add every one
722 in turn, including the shared parent. (The dedup machinery knows that a type
723 used by a single dictionary and its parent should not be shared in
724 CTF_LINK_SHARE_DUPLICATED mode.)
726 If no inputs exist that correspond to these CUs, return NULL with the errno
727 set to ECTF_NOCTFDATA. */
729 ctf_link_deduplicating_open_inputs (ctf_dict_t
*fp
, ctf_dynhash_t
*cu_names
,
730 ssize_t ninputs
, uint32_t **parents
)
732 ctf_dynhash_t
*inputs
= fp
->ctf_link_inputs
;
733 ctf_next_t
*i
= NULL
;
735 link_sort_inputs_cb_arg_t sort_arg
;
736 ctf_dict_t
**dedup_inputs
= NULL
;
738 uint32_t *parents_
= NULL
;
744 if ((dedup_inputs
= calloc (ninputs
, sizeof (ctf_dict_t
*))) == NULL
)
747 if ((parents_
= calloc (ninputs
, sizeof (uint32_t))) == NULL
)
752 /* Counting done: push every input into the array, in the order they were
753 passed to ctf_link_add_ctf (and ultimately ld). */
755 sort_arg
.is_cu_mapped
= (cu_names
!= NULL
);
758 while ((err
= ctf_dynhash_next_sorted (inputs
, &i
, &name
, &input
,
759 ctf_link_sort_inputs
, &sort_arg
)) == 0)
761 const char *one_name
= (const char *) name
;
762 ctf_link_input_t
*one_input
;
764 ctf_dict_t
*parent_fp
= NULL
;
765 uint32_t parent_i
= 0;
766 ctf_next_t
*j
= NULL
;
768 /* If we are processing CU names, get the real input. All the inputs
769 will have been opened, if they contained any CTF at all. */
771 one_input
= ctf_dynhash_lookup (fp
->ctf_link_inputs
, one_name
);
773 one_input
= (ctf_link_input_t
*) input
;
775 if (!one_input
|| (!one_input
->clin_arc
&& !one_input
->clin_fp
))
778 /* Short-circuit: if clin_fp is set, just use it. */
779 if (one_input
->clin_fp
)
781 parents_
[walk
- dedup_inputs
] = walk
- dedup_inputs
;
782 *walk
= one_input
->clin_fp
;
787 /* Get and insert the parent archive (if any), if this archive has
788 multiple members. We assume, as elsewhere, that the parent is named
791 if ((parent_fp
= ctf_dict_open (one_input
->clin_arc
, _CTF_SECTION
,
794 if (err
!= ECTF_NOMEMBNAM
)
796 ctf_next_destroy (i
);
797 ctf_set_errno (fp
, err
);
804 parent_i
= walk
- dedup_inputs
;
808 /* We disregard the input archive name: either it is the parent (which we
809 already have), or we want to put everything into one TU sharing the
810 cuname anyway (if this is a CU-mapped link), or this is the final phase
811 of a relink with CU-mapping off (i.e. ld -r) in which case the cuname
812 is correctly set regardless. */
813 while ((one_fp
= ctf_archive_next (one_input
->clin_arc
, &j
, NULL
,
816 if (one_fp
->ctf_flags
& LCTF_CHILD
)
818 /* The contents of the parents array for elements not
819 corresponding to children is undefined. If there is no parent
820 (itself a sign of a likely linker bug or corrupt input), we set
823 ctf_import (one_fp
, parent_fp
);
825 parents_
[walk
- dedup_inputs
] = parent_i
;
827 parents_
[walk
- dedup_inputs
] = walk
- dedup_inputs
;
832 if (err
!= ECTF_NEXT_END
)
834 ctf_next_destroy (i
);
838 if (err
!= ECTF_NEXT_END
)
849 ctf_set_errno (fp
, err
);
854 ctf_err_warn (fp
, 0, 0, _("error in deduplicating CTF link "
855 "input allocation"));
859 /* Close INPUTS that have already been linked, first the passed array, and then
860 that subset of the ctf_link_inputs archives they came from cited by the
861 CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one
862 go, leaving it empty. */
864 ctf_link_deduplicating_close_inputs (ctf_dict_t
*fp
, ctf_dynhash_t
*cu_names
,
865 ctf_dict_t
**inputs
, ssize_t ninputs
)
867 ctf_next_t
*it
= NULL
;
872 /* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close
873 all the individual input dicts, opened by the archive iterator. */
874 for (i
= 0; i
< ninputs
; i
++)
875 ctf_dict_close (inputs
[i
]);
877 /* Now close the archives they are part of. */
880 while ((err
= ctf_dynhash_next (cu_names
, &it
, &name
, NULL
)) == 0)
882 /* Remove the input from the linker inputs, if it exists, which also
885 ctf_dynhash_remove (fp
->ctf_link_inputs
, (const char *) name
);
887 if (err
!= ECTF_NEXT_END
)
889 ctf_set_errno (fp
, err
);
890 ctf_err_warn (fp
, 0, 0, _("iteration error in deduplicating link "
895 ctf_dynhash_empty (fp
->ctf_link_inputs
);
900 /* Do a deduplicating link of all variables in the inputs.
902 Also, if we are not omitting the variable section, integrate all symbols from
903 the symtypetabs into the variable section too. (Duplication with the
904 symtypetab section in the output will be eliminated at serialization time.) */
907 ctf_link_deduplicating_variables (ctf_dict_t
*fp
, ctf_dict_t
**inputs
,
908 size_t ninputs
, int cu_mapped
)
912 for (i
= 0; i
< ninputs
; i
++)
914 ctf_next_t
*it
= NULL
;
918 /* First the variables on the inputs. */
920 while ((type
= ctf_variable_next (inputs
[i
], &it
, &name
)) != CTF_ERR
)
922 if (ctf_link_one_variable (fp
, inputs
[i
], name
, type
, cu_mapped
) < 0)
924 ctf_next_destroy (it
);
925 return -1; /* errno is set for us. */
928 if (ctf_errno (inputs
[i
]) != ECTF_NEXT_END
)
929 return ctf_set_errno (fp
, ctf_errno (inputs
[i
]));
931 /* Next the symbols. We integrate data symbols even though the compiler
932 is currently doing the same, to allow the compiler to stop in
935 while ((type
= ctf_symbol_next (inputs
[i
], &it
, &name
, 0)) != CTF_ERR
)
937 if (ctf_link_one_variable (fp
, inputs
[i
], name
, type
, 1) < 0)
939 ctf_next_destroy (it
);
940 return -1; /* errno is set for us. */
943 if (ctf_errno (inputs
[i
]) != ECTF_NEXT_END
)
944 return ctf_set_errno (fp
, ctf_errno (inputs
[i
]));
946 /* Finally the function symbols. */
948 while ((type
= ctf_symbol_next (inputs
[i
], &it
, &name
, 1)) != CTF_ERR
)
950 if (ctf_link_one_variable (fp
, inputs
[i
], name
, type
, 1) < 0)
952 ctf_next_destroy (it
);
953 return -1; /* errno is set for us. */
956 if (ctf_errno (inputs
[i
]) != ECTF_NEXT_END
)
957 return ctf_set_errno (fp
, ctf_errno (inputs
[i
]));
962 /* Check for symbol conflicts during linking. Three possibilities: already
963 exists, conflicting, or nonexistent. We don't have a dvd structure we can
964 use as a flag like check_variable does, so we use a tristate return
965 value instead: -1: conflicting; 1: nonexistent: 0: already exists. */
968 check_sym (ctf_dict_t
*fp
, const char *name
, ctf_id_t type
, int functions
)
970 ctf_dynhash_t
*thishash
= functions
? fp
->ctf_funchash
: fp
->ctf_objthash
;
971 ctf_dynhash_t
*thathash
= functions
? fp
->ctf_objthash
: fp
->ctf_funchash
;
974 /* Wrong type (function when object is wanted, etc). */
975 if (ctf_dynhash_lookup_kv (thathash
, name
, NULL
, NULL
))
978 /* Not present at all yet. */
979 if (!ctf_dynhash_lookup_kv (thishash
, name
, NULL
, &value
))
982 /* Already present. */
983 if ((ctf_id_t
) (uintptr_t) value
== type
)
990 /* Do a deduplicating link of one symtypetab (function info or data object) in
994 ctf_link_deduplicating_one_symtypetab (ctf_dict_t
*fp
, ctf_dict_t
*input
,
995 int cu_mapped
, int functions
)
997 ctf_next_t
*it
= NULL
;
1001 while ((type
= ctf_symbol_next (input
, &it
, &name
, functions
)) != CTF_ERR
)
1004 ctf_dict_t
*per_cu_out_fp
;
1007 /* Look in the parent first. */
1009 if ((dst_type
= ctf_dedup_type_mapping (fp
, input
, type
)) == CTF_ERR
)
1010 return -1; /* errno is set for us. */
1014 if (!ctf_assert (fp
, ctf_type_isparent (fp
, dst_type
)))
1015 return -1; /* errno is set for us. */
1017 sym
= check_sym (fp
, name
, dst_type
, functions
);
1019 /* Already present: next symbol. */
1022 /* Not present: add it. */
1025 if (ctf_add_funcobjt_sym (fp
, functions
,
1026 name
, dst_type
) < 0)
1027 return -1; /* errno is set for us. */
1032 /* Can't add to the parent due to a name clash (most unlikely), or because
1033 it references a type only present in the child. Try adding to the
1034 child, creating if need be. If we can't do that, skip it. Don't add
1035 to a child if we're doing a CU-mapped link, since that has only one
1039 ctf_dprintf ("Symbol %s in input file %s depends on a type %lx "
1040 "hidden due to conflicts: skipped.\n", name
,
1041 ctf_unnamed_cuname (input
), type
);
1045 if ((per_cu_out_fp
= ctf_create_per_cu (fp
, input
, NULL
)) == NULL
)
1046 return -1; /* errno is set for us. */
1048 /* If the type was not found, check for it in the child too. */
1051 if ((dst_type
= ctf_dedup_type_mapping (per_cu_out_fp
,
1052 input
, type
)) == CTF_ERR
)
1053 return -1; /* errno is set for us. */
1057 ctf_err_warn (fp
, 1, 0,
1058 _("type %lx for symbol %s in input file %s "
1059 "not found: skipped"), type
, name
,
1060 ctf_unnamed_cuname (input
));
1065 sym
= check_sym (per_cu_out_fp
, name
, dst_type
, functions
);
1067 /* Already present: next symbol. */
1070 /* Not present: add it. */
1073 if (ctf_add_funcobjt_sym (per_cu_out_fp
, functions
,
1074 name
, dst_type
) < 0)
1075 return -1; /* errno is set for us. */
1079 /* Perhaps this should be an assertion failure. */
1080 ctf_err_warn (fp
, 0, ECTF_DUPLICATE
,
1081 _("symbol %s in input file %s found conflicting "
1082 "even when trying in per-CU dict."), name
,
1083 ctf_unnamed_cuname (input
));
1084 return (ctf_set_errno (fp
, ECTF_DUPLICATE
));
1087 if (ctf_errno (input
) != ECTF_NEXT_END
)
1089 ctf_set_errno (fp
, ctf_errno (input
));
1090 ctf_err_warn (fp
, 0, 0, functions
?
1091 _("iterating over function symbols") :
1092 _("iterating over data symbols"));
1099 /* Do a deduplicating link of the function info and data objects
1102 ctf_link_deduplicating_syms (ctf_dict_t
*fp
, ctf_dict_t
**inputs
,
1103 size_t ninputs
, int cu_mapped
)
1107 for (i
= 0; i
< ninputs
; i
++)
1109 if (ctf_link_deduplicating_one_symtypetab (fp
, inputs
[i
],
1111 return -1; /* errno is set for us. */
1113 if (ctf_link_deduplicating_one_symtypetab (fp
, inputs
[i
],
1115 return -1; /* errno is set for us. */
1121 /* Do the per-CU part of a deduplicating link. */
1123 ctf_link_deduplicating_per_cu (ctf_dict_t
*fp
)
1125 ctf_next_t
*i
= NULL
;
1130 /* Links with a per-CU mapping in force get a first pass of deduplication,
1131 dedupping the inputs for a given CU mapping into the output for that
1132 mapping. The outputs from this process get fed back into the final pass
1133 that is carried out even for non-CU links. */
1135 while ((err
= ctf_dynhash_next (fp
->ctf_link_out_cu_mapping
, &i
, &out_cu
,
1138 const char *out_name
= (const char *) out_cu
;
1139 ctf_dynhash_t
*in
= (ctf_dynhash_t
*) in_cus
;
1140 ctf_dict_t
*out
= NULL
;
1141 ctf_dict_t
**inputs
;
1142 ctf_dict_t
**outputs
;
1143 ctf_archive_t
*in_arc
;
1145 ctf_link_input_t
*only_input
;
1149 if ((ninputs
= ctf_link_deduplicating_count_inputs (fp
, in
,
1150 &only_input
)) == -1)
1151 goto err_open_inputs
;
1153 /* CU mapping with no inputs? Skip. */
1157 if (labs ((long int) ninputs
) > 0xfffffffe)
1159 ctf_set_errno (fp
, EFBIG
);
1160 ctf_err_warn (fp
, 0, 0, _("too many inputs in deduplicating "
1161 "link: %li"), (long int) ninputs
);
1162 goto err_open_inputs
;
1165 /* Short-circuit: a cu-mapped link with only one input archive with
1166 unconflicting contents is a do-nothing, and we can just leave the input
1167 in place: we do have to change the cuname, though, so we unwrap it,
1168 change the cuname, then stuff it back in the linker input again, via
1169 the clin_fp short-circuit member. ctf_link_deduplicating_open_inputs
1170 will spot this member and jam it straight into the next link phase,
1171 ignoring the corresponding archive. */
1172 if (only_input
&& ninputs
== 1)
1174 ctf_next_t
*ai
= NULL
;
1177 /* We can abuse an archive iterator to get the only member cheaply, no
1178 matter what its name. */
1179 only_input
->clin_fp
= ctf_archive_next (only_input
->clin_arc
,
1180 &ai
, NULL
, 0, &err
);
1181 if (!only_input
->clin_fp
)
1183 ctf_set_errno (fp
, err
);
1184 ctf_err_warn (fp
, 0, 0, _("cannot open archive %s in "
1185 "CU-mapped CTF link"),
1186 only_input
->clin_filename
);
1187 goto err_open_inputs
;
1189 ctf_next_destroy (ai
);
1191 if (strcmp (only_input
->clin_filename
, out_name
) != 0)
1193 /* Renaming. We need to add a new input, then null out the
1194 clin_arc and clin_fp of the old one to stop it being
1195 auto-closed on removal. The new input needs its cuname changed
1196 to out_name, which is doable only because the cuname is a
1197 dynamic property which can be changed even in readonly
1200 ctf_cuname_set (only_input
->clin_fp
, out_name
);
1201 if (ctf_link_add_ctf_internal (fp
, only_input
->clin_arc
,
1202 only_input
->clin_fp
,
1205 ctf_err_warn (fp
, 0, 0, _("cannot add intermediate files "
1207 goto err_open_inputs
;
1209 only_input
->clin_arc
= NULL
;
1210 only_input
->clin_fp
= NULL
;
1211 ctf_dynhash_remove (fp
->ctf_link_inputs
,
1212 only_input
->clin_filename
);
1217 /* This is a real CU many-to-one mapping: we must dedup the inputs into
1218 a new output to be used in the final link phase. */
1220 if ((inputs
= ctf_link_deduplicating_open_inputs (fp
, in
, ninputs
,
1223 ctf_next_destroy (i
);
1227 if ((out
= ctf_create (&err
)) == NULL
)
1229 ctf_err_warn (fp
, 0, err
, _("cannot create per-CU CTF archive "
1232 ctf_set_errno (fp
, err
);
1236 /* Share the atoms table to reduce memory usage. */
1237 out
->ctf_dedup_atoms
= fp
->ctf_dedup_atoms_alloc
;
1239 /* No ctf_imports at this stage: this per-CU dictionary has no parents.
1240 Parent/child deduplication happens in the link's final pass. However,
1241 the cuname *is* important, as it is propagated into the final
1243 ctf_cuname_set (out
, out_name
);
1245 if (ctf_dedup (out
, inputs
, ninputs
, parents
, 1) < 0)
1247 ctf_set_errno (fp
, ctf_errno (out
));
1248 ctf_err_warn (fp
, 0, 0, _("CU-mapped deduplication failed for %s"),
1253 if ((outputs
= ctf_dedup_emit (out
, inputs
, ninputs
, parents
,
1254 &noutputs
, 1)) == NULL
)
1256 ctf_set_errno (fp
, ctf_errno (out
));
1257 ctf_err_warn (fp
, 0, 0, _("CU-mapped deduplicating link type emission "
1258 "failed for %s"), out_name
);
1261 if (!ctf_assert (fp
, noutputs
== 1))
1264 for (j
= 1; j
< noutputs
; j
++)
1265 ctf_dict_close (outputs
[j
]);
1266 goto err_inputs_outputs
;
1269 if (!(fp
->ctf_link_flags
& CTF_LINK_OMIT_VARIABLES_SECTION
)
1270 && ctf_link_deduplicating_variables (out
, inputs
, ninputs
, 1) < 0)
1272 ctf_set_errno (fp
, ctf_errno (out
));
1273 ctf_err_warn (fp
, 0, 0, _("CU-mapped deduplicating link variable "
1274 "emission failed for %s"), out_name
);
1275 goto err_inputs_outputs
;
1278 ctf_dedup_fini (out
, outputs
, noutputs
);
1280 /* For now, we omit symbol section linking for CU-mapped links, until it
1281 is clear how to unify the symbol table across such links. (Perhaps we
1282 should emit an unconditionally indexed symtab, like the compiler
1285 if (ctf_link_deduplicating_close_inputs (fp
, in
, inputs
, ninputs
) < 0)
1294 /* Splice any errors or warnings created during this link back into the
1295 dict that the caller knows about. */
1296 ctf_list_splice (&fp
->ctf_errs_warnings
, &outputs
[0]->ctf_errs_warnings
);
1298 /* This output now becomes an input to the next link phase, with a name
1299 equal to the CU name. We have to wrap it in an archive wrapper
1302 if ((in_arc
= ctf_new_archive_internal (0, 0, NULL
, outputs
[0], NULL
,
1303 NULL
, &err
)) == NULL
)
1305 ctf_set_errno (fp
, err
);
1309 if (ctf_link_add_ctf_internal (fp
, in_arc
, NULL
,
1310 ctf_cuname (outputs
[0])) < 0)
1312 ctf_err_warn (fp
, 0, 0, _("cannot add intermediate files to link"));
1316 ctf_dict_close (out
);
1321 ctf_list_splice (&fp
->ctf_errs_warnings
, &outputs
[0]->ctf_errs_warnings
);
1322 ctf_dict_close (outputs
[0]);
1325 ctf_link_deduplicating_close_inputs (fp
, in
, inputs
, ninputs
);
1326 ctf_dict_close (out
);
1330 ctf_next_destroy (i
);
1334 ctf_list_splice (&fp
->ctf_errs_warnings
, &outputs
[0]->ctf_errs_warnings
);
1335 ctf_dict_close (outputs
[0]);
1337 ctf_next_destroy (i
);
1338 return -1; /* Errno is set for us. */
1340 if (err
!= ECTF_NEXT_END
)
1342 ctf_err_warn (fp
, 0, err
, _("iteration error in CU-mapped deduplicating "
1344 return ctf_set_errno (fp
, err
);
1350 /* Empty all the ctf_link_outputs. */
1352 ctf_link_empty_outputs (ctf_dict_t
*fp
)
1354 ctf_next_t
*i
= NULL
;
1358 ctf_dynhash_empty (fp
->ctf_link_outputs
);
1360 while ((err
= ctf_dynhash_next (fp
->ctf_link_inputs
, &i
, NULL
, &v
)) == 0)
1362 ctf_dict_t
*in
= (ctf_dict_t
*) v
;
1363 in
->ctf_link_in_out
= NULL
;
1365 if (err
!= ECTF_NEXT_END
)
1367 fp
->ctf_flags
&= ~LCTF_LINKING
;
1368 ctf_err_warn (fp
, 1, err
, _("iteration error removing old outputs"));
1369 return ctf_set_errno (fp
, err
);
1374 /* Do a deduplicating link using the ctf-dedup machinery. */
1376 ctf_link_deduplicating (ctf_dict_t
*fp
)
1379 ctf_dict_t
**inputs
, **outputs
= NULL
;
1384 if (ctf_dedup_atoms_init (fp
) < 0)
1386 ctf_err_warn (fp
, 0, 0, _("allocating CTF dedup atoms table"));
1387 return; /* Errno is set for us. */
1390 if (fp
->ctf_link_out_cu_mapping
1391 && (ctf_link_deduplicating_per_cu (fp
) < 0))
1392 return; /* Errno is set for us. */
1394 if ((ninputs
= ctf_link_deduplicating_count_inputs (fp
, NULL
, NULL
)) < 0)
1395 return; /* Errno is set for us. */
1397 if ((inputs
= ctf_link_deduplicating_open_inputs (fp
, NULL
, ninputs
,
1399 return; /* Errno is set for us. */
1401 if (ninputs
== 1 && ctf_cuname (inputs
[0]) != NULL
)
1402 ctf_cuname_set (fp
, ctf_cuname (inputs
[0]));
1404 if (ctf_dedup (fp
, inputs
, ninputs
, parents
, 0) < 0)
1406 ctf_err_warn (fp
, 0, 0, _("deduplication failed for %s"),
1407 ctf_link_input_name (fp
));
1411 if ((outputs
= ctf_dedup_emit (fp
, inputs
, ninputs
, parents
, &noutputs
,
1414 ctf_err_warn (fp
, 0, 0, _("deduplicating link type emission failed "
1415 "for %s"), ctf_link_input_name (fp
));
1419 if (!ctf_assert (fp
, outputs
[0] == fp
))
1421 for (i
= 1; i
< noutputs
; i
++)
1422 ctf_dict_close (outputs
[i
]);
1426 for (i
= 0; i
< noutputs
; i
++)
1430 /* We already have access to this one. Close the duplicate. */
1433 ctf_dict_close (outputs
[0]);
1437 if ((dynname
= ctf_new_per_cu_name (fp
, ctf_cuname (outputs
[i
]))) == NULL
)
1438 goto oom_one_output
;
1440 if (ctf_dynhash_insert (fp
->ctf_link_outputs
, dynname
, outputs
[i
]) < 0)
1441 goto oom_one_output
;
1446 ctf_set_errno (fp
, ENOMEM
);
1447 ctf_err_warn (fp
, 0, 0, _("out of memory allocating link outputs"));
1450 for (; i
< noutputs
; i
++)
1451 ctf_dict_close (outputs
[i
]);
1455 if (!(fp
->ctf_link_flags
& CTF_LINK_OMIT_VARIABLES_SECTION
)
1456 && ctf_link_deduplicating_variables (fp
, inputs
, ninputs
, 0) < 0)
1458 ctf_err_warn (fp
, 0, 0, _("deduplicating link variable emission failed for "
1459 "%s"), ctf_link_input_name (fp
));
1460 goto err_clean_outputs
;
1463 if (ctf_link_deduplicating_syms (fp
, inputs
, ninputs
, 0) < 0)
1465 ctf_err_warn (fp
, 0, 0, _("deduplicating link symbol emission failed for "
1466 "%s"), ctf_link_input_name (fp
));
1467 goto err_clean_outputs
;
1470 ctf_dedup_fini (fp
, outputs
, noutputs
);
1472 /* Now close all the inputs, including per-CU intermediates. */
1474 if (ctf_link_deduplicating_close_inputs (fp
, NULL
, inputs
, ninputs
) < 0)
1475 return; /* errno is set for us. */
1477 ninputs
= 0; /* Prevent double-close. */
1478 ctf_set_errno (fp
, 0);
1483 for (i
= 0; i
< (size_t) ninputs
; i
++)
1484 ctf_dict_close (inputs
[i
]);
1491 ctf_link_empty_outputs (fp
);
1495 /* Merge types and variable sections in all dicts added to the link together.
1496 The result of any previous link is discarded. */
1498 ctf_link (ctf_dict_t
*fp
, int flags
)
1502 fp
->ctf_link_flags
= flags
;
1504 if (fp
->ctf_link_inputs
== NULL
)
1505 return 0; /* Nothing to do. */
1507 if (fp
->ctf_link_outputs
!= NULL
)
1508 ctf_link_empty_outputs (fp
);
1510 fp
->ctf_link_outputs
= ctf_dynhash_create (ctf_hash_string
,
1511 ctf_hash_eq_string
, free
,
1515 if (fp
->ctf_link_outputs
== NULL
)
1516 return ctf_set_errno (fp
, ENOMEM
);
1518 fp
->ctf_flags
|= LCTF_LINKING
;
1519 ctf_link_deduplicating (fp
);
1520 fp
->ctf_flags
&= ~LCTF_LINKING
;
1522 if ((ctf_errno (fp
) != 0) && (ctf_errno (fp
) != ECTF_NOCTFDATA
))
1525 /* Create empty CUs if requested. We do not currently claim that multiple
1526 links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and
1527 not set in others will do anything especially sensible. */
1529 if (fp
->ctf_link_out_cu_mapping
&& (flags
& CTF_LINK_EMPTY_CU_MAPPINGS
))
1531 ctf_next_t
*i
= NULL
;
1534 while ((err
= ctf_dynhash_next (fp
->ctf_link_out_cu_mapping
, &i
, &k
,
1537 const char *to
= (const char *) k
;
1538 if (ctf_create_per_cu (fp
, NULL
, to
) == NULL
)
1540 fp
->ctf_flags
&= ~LCTF_LINKING
;
1541 ctf_next_destroy (i
);
1542 return -1; /* Errno is set for us. */
1545 if (err
!= ECTF_NEXT_END
)
1547 fp
->ctf_flags
&= ~LCTF_LINKING
;
1548 ctf_err_warn (fp
, 1, err
, _("iteration error creating empty CUs"));
1549 return ctf_set_errno (fp
, err
);
1556 typedef struct ctf_link_out_string_cb_arg
1561 } ctf_link_out_string_cb_arg_t
;
1563 /* Intern a string in the string table of an output per-CU CTF file. */
1565 ctf_link_intern_extern_string (void *key _libctf_unused_
, void *value
,
1568 ctf_dict_t
*fp
= (ctf_dict_t
*) value
;
1569 ctf_link_out_string_cb_arg_t
*arg
= (ctf_link_out_string_cb_arg_t
*) arg_
;
1571 if (!ctf_str_add_external (fp
, arg
->str
, arg
->offset
))
1575 /* Repeatedly call ADD_STRING to acquire strings from the external string table,
1576 adding them to the atoms table for this CU and all subsidiary CUs.
1578 Must be called on a dict that has not yet been serialized.
1580 If ctf_link is also called, it must be called first if you want the new CTF
1581 files ctf_link can create to get their strings dedupped against the ELF
1584 ctf_link_add_strtab (ctf_dict_t
*fp
, ctf_link_strtab_string_f
*add_string
,
1591 if (fp
->ctf_stypes
> 0)
1592 return ctf_set_errno (fp
, ECTF_RDONLY
);
1594 while ((str
= add_string (&offset
, arg
)) != NULL
)
1596 ctf_link_out_string_cb_arg_t iter_arg
= { str
, offset
, 0 };
1598 if (!ctf_str_add_external (fp
, str
, offset
))
1601 ctf_dynhash_iter (fp
->ctf_link_outputs
, ctf_link_intern_extern_string
,
1608 ctf_set_errno (fp
, err
);
1613 /* Inform the ctf-link machinery of a new symbol in the target symbol table
1614 (which must be some symtab that is not usually stripped, and which
1615 is in agreement with ctf_bfdopen_ctfsect). May be called either before or
1616 after ctf_link_add_strtab. As with that function, must be called on a dict which
1617 has not yet been serialized. */
1619 ctf_link_add_linker_symbol (ctf_dict_t
*fp
, ctf_link_sym_t
*sym
)
1621 ctf_in_flight_dynsym_t
*cid
;
1623 /* Cheat a little: if there is already an ENOMEM error code recorded against
1624 this dict, we shouldn't even try to add symbols because there will be no
1625 memory to do so: probably we failed to add some previous symbol. This
1626 makes out-of-memory exits 'sticky' across calls to this function, so the
1627 caller doesn't need to worry about error conditions. */
1629 if (ctf_errno (fp
) == ENOMEM
)
1630 return -ENOMEM
; /* errno is set for us. */
1632 if (fp
->ctf_stypes
> 0)
1633 return ctf_set_errno (fp
, ECTF_RDONLY
);
1635 if (ctf_symtab_skippable (sym
))
1638 if (sym
->st_type
!= STT_OBJECT
&& sym
->st_type
!= STT_FUNC
)
1641 /* Add the symbol to the in-flight list. */
1643 if ((cid
= malloc (sizeof (ctf_in_flight_dynsym_t
))) == NULL
)
1646 cid
->cid_sym
= *sym
;
1647 ctf_list_append (&fp
->ctf_in_flight_dynsyms
, cid
);
1652 ctf_dynhash_destroy (fp
->ctf_dynsyms
);
1653 fp
->ctf_dynsyms
= NULL
;
1654 ctf_set_errno (fp
, ENOMEM
);
1658 /* Impose an ordering on symbols. The ordering takes effect immediately, but
1659 since the ordering info does not include type IDs, lookups may return nothing
1660 until such IDs are added by calls to ctf_add_*_sym. Must be called after
1661 ctf_link_add_strtab and ctf_link_add_linker_symbol. */
1663 ctf_link_shuffle_syms (ctf_dict_t
*fp
)
1665 ctf_in_flight_dynsym_t
*did
, *nid
;
1666 ctf_next_t
*i
= NULL
;
1670 if (fp
->ctf_stypes
> 0)
1671 return ctf_set_errno (fp
, ECTF_RDONLY
);
1673 if (!fp
->ctf_dynsyms
)
1675 fp
->ctf_dynsyms
= ctf_dynhash_create (ctf_hash_string
,
1678 if (!fp
->ctf_dynsyms
)
1680 ctf_set_errno (fp
, ENOMEM
);
1685 /* Add all the symbols, excluding only those we already know are prohibited
1686 from appearing in symtypetabs. */
1688 for (did
= ctf_list_next (&fp
->ctf_in_flight_dynsyms
); did
!= NULL
; did
= nid
)
1690 ctf_link_sym_t
*new_sym
;
1692 nid
= ctf_list_next (did
);
1693 ctf_list_delete (&fp
->ctf_in_flight_dynsyms
, did
);
1695 /* We might get a name or an external strtab offset. The strtab offset is
1696 guaranteed resolvable at this point, so turn it into a string. */
1698 if (did
->cid_sym
.st_name
== NULL
)
1700 uint32_t off
= CTF_SET_STID (did
->cid_sym
.st_nameidx
, CTF_STRTAB_1
);
1702 did
->cid_sym
.st_name
= ctf_strraw (fp
, off
);
1703 did
->cid_sym
.st_nameidx_set
= 0;
1704 if (!ctf_assert (fp
, did
->cid_sym
.st_name
!= NULL
))
1705 return -ECTF_INTERNAL
; /* errno is set for us. */
1708 /* The symbol might have turned out to be nameless, so we have to recheck
1709 for skippability here. */
1710 if (!ctf_symtab_skippable (&did
->cid_sym
))
1712 ctf_dprintf ("symbol from linker: %s (%x)\n", did
->cid_sym
.st_name
,
1713 did
->cid_sym
.st_symidx
);
1715 if ((new_sym
= malloc (sizeof (ctf_link_sym_t
))) == NULL
)
1718 memcpy (new_sym
, &did
->cid_sym
, sizeof (ctf_link_sym_t
));
1719 if (ctf_dynhash_cinsert (fp
->ctf_dynsyms
, new_sym
->st_name
, new_sym
) < 0)
1722 if (fp
->ctf_dynsymmax
< new_sym
->st_symidx
)
1723 fp
->ctf_dynsymmax
= new_sym
->st_symidx
;
1735 /* If no symbols are reported, unwind what we have done and return. This
1736 makes it a bit easier for the serializer to tell that no symbols have been
1737 reported and that it should look elsewhere for reported symbols. */
1738 if (!ctf_dynhash_elements (fp
->ctf_dynsyms
))
1740 ctf_dprintf ("No symbols: not a final link.\n");
1741 ctf_dynhash_destroy (fp
->ctf_dynsyms
);
1742 fp
->ctf_dynsyms
= NULL
;
1746 /* Construct a mapping from shndx to the symbol info. */
1747 free (fp
->ctf_dynsymidx
);
1748 if ((fp
->ctf_dynsymidx
= calloc (fp
->ctf_dynsymmax
+ 1,
1749 sizeof (ctf_link_sym_t
*))) == NULL
)
1752 while ((err
= ctf_dynhash_next (fp
->ctf_dynsyms
, &i
, &name_
, &sym_
)) == 0)
1754 const char *name
= (const char *) name
;
1755 ctf_link_sym_t
*symp
= (ctf_link_sym_t
*) sym_
;
1757 if (!ctf_assert (fp
, symp
->st_symidx
<= fp
->ctf_dynsymmax
))
1759 ctf_next_destroy (i
);
1760 err
= ctf_errno (fp
);
1763 fp
->ctf_dynsymidx
[symp
->st_symidx
] = symp
;
1765 if (err
!= ECTF_NEXT_END
)
1767 ctf_err_warn (fp
, 0, err
, _("error iterating over shuffled symbols"));
1773 /* Leave the in-flight symbols around: they'll be freed at
1774 dict close time regardless. */
1775 ctf_dynhash_destroy (fp
->ctf_dynsyms
);
1776 fp
->ctf_dynsyms
= NULL
;
1777 free (fp
->ctf_dynsymidx
);
1778 fp
->ctf_dynsymidx
= NULL
;
1779 fp
->ctf_dynsymmax
= 0;
1780 ctf_set_errno (fp
, err
);
1784 typedef struct ctf_name_list_accum_cb_arg
1792 } ctf_name_list_accum_cb_arg_t
;
1794 /* Accumulate the names and a count of the names in the link output hash. */
1796 ctf_accumulate_archive_names (void *key
, void *value
, void *arg_
)
1798 const char *name
= (const char *) key
;
1799 ctf_dict_t
*fp
= (ctf_dict_t
*) value
;
1802 ctf_name_list_accum_cb_arg_t
*arg
= (ctf_name_list_accum_cb_arg_t
*) arg_
;
1804 if ((names
= realloc (arg
->names
, sizeof (char *) * ++(arg
->i
))) == NULL
)
1807 ctf_set_errno (arg
->fp
, ENOMEM
);
1811 if ((files
= realloc (arg
->files
, sizeof (ctf_dict_t
*) * arg
->i
)) == NULL
)
1814 ctf_set_errno (arg
->fp
, ENOMEM
);
1818 /* Allow the caller to get in and modify the name at the last minute. If the
1819 caller *does* modify the name, we have to stash away the new name the
1820 caller returned so we can free it later on. (The original name is the key
1821 of the ctf_link_outputs hash and is freed by the dynhash machinery.) */
1823 if (fp
->ctf_link_memb_name_changer
)
1827 void *nc_arg
= fp
->ctf_link_memb_name_changer_arg
;
1829 dyname
= fp
->ctf_link_memb_name_changer (fp
, name
, nc_arg
);
1833 if ((dynames
= realloc (arg
->dynames
,
1834 sizeof (char *) * ++(arg
->ndynames
))) == NULL
)
1837 ctf_set_errno (arg
->fp
, ENOMEM
);
1840 arg
->dynames
= dynames
;
1841 name
= (const char *) dyname
;
1846 arg
->names
[(arg
->i
) - 1] = (char *) name
;
1848 arg
->files
[(arg
->i
) - 1] = fp
;
1851 /* Change the name of the parent CTF section, if the name transformer has got to
1854 ctf_change_parent_name (void *key _libctf_unused_
, void *value
, void *arg
)
1856 ctf_dict_t
*fp
= (ctf_dict_t
*) value
;
1857 const char *name
= (const char *) arg
;
1859 ctf_parent_name_set (fp
, name
);
1862 /* Warn if we may suffer information loss because the CTF input files are too
1863 old. Usually we provide complete backward compatibility, but compiler
1864 changes etc which never hit a release may have a flag in the header that
1865 simply prevents those changes from being used. */
1867 ctf_link_warn_outdated_inputs (ctf_dict_t
*fp
)
1869 ctf_next_t
*i
= NULL
;
1874 while ((err
= ctf_dynhash_next (fp
->ctf_link_inputs
, &i
, &name_
, &input_
)) == 0)
1876 const char *name
= (const char *) name_
;
1877 ctf_link_input_t
*input
= (ctf_link_input_t
*) input_
;
1878 ctf_next_t
*j
= NULL
;
1882 /* We only care about CTF archives by this point: lazy-opened archives
1883 have always been opened by this point, and short-circuited entries have
1884 a matching corresponding archive member. Entries with NULL clin_arc can
1885 exist, and constitute old entries renamed via a name changer: the
1886 renamed entries exist elsewhere in the list, so we can just skip
1889 if (!input
->clin_arc
)
1892 /* All entries in the archive will necessarily contain the same
1893 CTF_F_NEWFUNCINFO flag, so we only need to check the first. We don't
1894 even need to do that if we can't open it for any reason at all: the
1895 link will fail later on regardless, since an input can't be opened. */
1897 ifp
= ctf_archive_next (input
->clin_arc
, &j
, NULL
, 0, &err
);
1900 ctf_next_destroy (j
);
1902 if (!(ifp
->ctf_header
->cth_flags
& CTF_F_NEWFUNCINFO
)
1903 && (ifp
->ctf_header
->cth_varoff
- ifp
->ctf_header
->cth_funcoff
) > 0)
1904 ctf_err_warn (fp
, 1, 0, _("linker input %s has CTF func info but uses "
1905 "an old, unreleased func info format: "
1906 "this func info section will be dropped."),
1909 if (err
!= ECTF_NEXT_END
)
1910 ctf_err_warn (fp
, 0, err
, _("error checking for outdated inputs"));
1913 /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
1914 (otherwise) into a new dynamically-allocated string, and return it.
1915 Members with sizes above THRESHOLD are compressed. */
1917 ctf_link_write (ctf_dict_t
*fp
, size_t *size
, size_t threshold
)
1919 ctf_name_list_accum_cb_arg_t arg
;
1921 char *transformed_name
= NULL
;
1928 unsigned char *buf
= NULL
;
1930 memset (&arg
, 0, sizeof (ctf_name_list_accum_cb_arg_t
));
1932 fp
->ctf_flags
|= LCTF_LINKING
;
1934 ctf_link_warn_outdated_inputs (fp
);
1936 if (fp
->ctf_link_outputs
)
1938 ctf_dynhash_iter (fp
->ctf_link_outputs
, ctf_accumulate_archive_names
, &arg
);
1939 if (ctf_errno (fp
) < 0)
1941 errloc
= "hash creation";
1946 /* No extra outputs? Just write a simple ctf_dict_t. */
1949 unsigned char *ret
= ctf_write_mem (fp
, size
, threshold
);
1950 fp
->ctf_flags
&= ~LCTF_LINKING
;
1954 /* Writing an archive. Stick ourselves (the shared repository, parent of all
1955 other archives) on the front of it with the default name. */
1956 if ((names
= realloc (arg
.names
, sizeof (char *) * (arg
.i
+ 1))) == NULL
)
1958 errloc
= "name reallocation";
1962 memmove (&(arg
.names
[1]), arg
.names
, sizeof (char *) * (arg
.i
));
1964 arg
.names
[0] = (char *) _CTF_SECTION
;
1965 if (fp
->ctf_link_memb_name_changer
)
1967 void *nc_arg
= fp
->ctf_link_memb_name_changer_arg
;
1969 transformed_name
= fp
->ctf_link_memb_name_changer (fp
, _CTF_SECTION
,
1972 if (transformed_name
!= NULL
)
1974 arg
.names
[0] = transformed_name
;
1975 ctf_dynhash_iter (fp
->ctf_link_outputs
, ctf_change_parent_name
,
1980 /* Propagate the link flags to all the dicts in this link. */
1981 for (i
= 0; i
< arg
.i
; i
++)
1983 arg
.files
[i
]->ctf_link_flags
= fp
->ctf_link_flags
;
1984 arg
.files
[i
]->ctf_flags
|= LCTF_LINKING
;
1987 if ((files
= realloc (arg
.files
,
1988 sizeof (struct ctf_dict
*) * (arg
.i
+ 1))) == NULL
)
1990 errloc
= "ctf_dict reallocation";
1994 memmove (&(arg
.files
[1]), arg
.files
, sizeof (ctf_dict_t
*) * (arg
.i
));
1997 if ((f
= tmpfile ()) == NULL
)
1999 errloc
= "tempfile creation";
2003 if ((err
= ctf_arc_write_fd (fileno (f
), arg
.files
, arg
.i
+ 1,
2004 (const char **) arg
.names
,
2007 errloc
= "archive writing";
2008 ctf_set_errno (fp
, err
);
2012 if (fseek (f
, 0, SEEK_END
) < 0)
2014 errloc
= "seeking to end";
2018 if ((fsize
= ftell (f
)) < 0)
2020 errloc
= "filesize determination";
2024 if (fseek (f
, 0, SEEK_SET
) < 0)
2026 errloc
= "filepos resetting";
2030 if ((buf
= malloc (fsize
)) == NULL
)
2032 errloc
= "CTF archive buffer allocation";
2036 while (!feof (f
) && fread (buf
, fsize
, 1, f
) == 0)
2039 errloc
= "reading archive from temporary file";
2046 free (transformed_name
);
2050 for (i
= 0; i
< arg
.ndynames
; i
++)
2051 free (arg
.dynames
[i
]);
2058 ctf_set_errno (fp
, errno
);
2060 /* Turn off the is-linking flag on all the dicts in this link. */
2061 for (i
= 0; i
< arg
.i
; i
++)
2062 arg
.files
[i
]->ctf_flags
&= ~LCTF_LINKING
;
2069 free (transformed_name
);
2073 for (i
= 0; i
< arg
.ndynames
; i
++)
2074 free (arg
.dynames
[i
]);
2077 ctf_err_warn (fp
, 0, 0, _("cannot write archive in link: %s failure"),