]> Git Repo - binutils.git/blob - libctf/ctf-link.c
include, libctf, ld: extend variable section to contain functions too
[binutils.git] / libctf / ctf-link.c
1 /* CTF linking.
2    Copyright (C) 2019-2022 Free Software Foundation, Inc.
3
4    This file is part of libctf.
5
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
9    version.
10
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.
15
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/>.  */
19
20 #include <ctf-impl.h>
21 #include <string.h>
22
23 #if defined (PIC)
24 #pragma weak ctf_open
25 #endif
26
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.  */
39
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.  */
43 const char *
44 ctf_link_input_name (ctf_dict_t *fp)
45 {
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;
50   else
51     return "(unnamed)";
52 }
53
54 /* Return the cuname of a dict, or the string "unnamed-CU" if none.  */
55
56 static const char *
57 ctf_unnamed_cuname (ctf_dict_t *fp)
58 {
59   const char *cuname = ctf_cuname (fp);
60
61   if (!cuname)
62     cuname = "unnamed-CU";
63
64   return cuname;
65 }
66
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
74 {
75   const char *clin_filename;
76   ctf_archive_t *clin_arc;
77   ctf_dict_t *clin_fp;
78   int n;
79 } ctf_link_input_t;
80
81 static void
82 ctf_link_input_close (void *input)
83 {
84   ctf_link_input_t *i = (ctf_link_input_t *) input;
85   if (i->clin_arc)
86     ctf_arc_close (i->clin_arc);
87   free (i);
88 }
89
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.  */
92 static int
93 ctf_link_add_ctf_internal (ctf_dict_t *fp, ctf_archive_t *ctf,
94                            ctf_dict_t *fp_input, const char *name)
95 {
96   ctf_link_input_t *input = NULL;
97   char *dupname = NULL;
98
99   if ((input = calloc (1, sizeof (ctf_link_input_t))) == NULL)
100     goto oom;
101
102   if ((dupname = strdup (name)) == NULL)
103     goto oom;
104
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);
109
110   if (ctf_dynhash_insert (fp->ctf_link_inputs, dupname, input) < 0)
111     goto oom;
112
113   return 0;
114  oom:
115   free (input);
116   free (dupname);
117   return ctf_set_errno (fp, ENOMEM);
118 }
119
120 /* Add a file, memory buffer, or unopened file (by name) to a link.
121
122    You can call this with:
123
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
127     yet implemented.)
128
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.
132
133     The order of calls to this function influences the order of types in the
134     final link output, but otherwise is not important.
135
136     Private for now, but may in time become public once support for BUF is
137     implemented.  */
138
139 static int
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_)
142 {
143   if (buf)
144     return (ctf_set_errno (fp, ECTF_NOTYET));
145
146   if (!((ctf && name && !buf)
147         || (name && !buf && !ctf)
148         || (buf && name && !ctf)))
149     return (ctf_set_errno (fp, EINVAL));
150
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.  */
155
156 #if defined (PIC)
157   if (!buf && !ctf && name && !ctf_open)
158     return (ctf_set_errno (fp, ECTF_NEEDSBFD));
159 #elif NOBFD
160   if (!buf && !ctf && name)
161     return (ctf_set_errno (fp, ECTF_NEEDSBFD));
162 #endif
163
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);
170
171   if (fp->ctf_link_inputs == NULL)
172     return (ctf_set_errno (fp, ENOMEM));
173
174   return ctf_link_add_ctf_internal (fp, ctf, NULL, name);
175 }
176
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.
180
181     Passed in CTF args are owned by the dictionary and will be freed by it.
182
183     The order of calls to this function influences the order of types in the
184     final link output, but otherwise is not important.  */
185
186 int
187 ctf_link_add_ctf (ctf_dict_t *fp, ctf_archive_t *ctf, const char *name)
188 {
189   return ctf_link_add (fp, ctf, name, NULL, 0);
190 }
191
192 /* Lazily open a CTF archive for linking, if not already open.
193
194    Returns the number of files contained within the opened archive (0 for none),
195    or -1 on error, as usual.  */
196 static ssize_t
197 ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input)
198 {
199   size_t count;
200   int err;
201
202   if (input->clin_arc)
203     return ctf_archive_count (input->clin_arc);
204
205   if (input->clin_fp)
206     return 1;
207
208   /* See ctf_link_add_ctf.  */
209 #if defined (PIC) || !NOBFD
210   input->clin_arc = ctf_open (input->clin_filename, NULL, &err);
211 #else
212   ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"),
213                 input->clin_filename);
214   ctf_set_errno (fp, ECTF_NEEDSBFD);
215   return -1;
216 #endif
217
218   /* Having no CTF sections is not an error.  We just don't need to do
219      anything.  */
220
221   if (!input->clin_arc)
222     {
223       if (err == ECTF_NOCTFDATA)
224         return 0;
225
226       ctf_err_warn (fp, 0, err, _("opening CTF %s failed"),
227                     input->clin_filename);
228       ctf_set_errno (fp, err);
229       return -1;
230     }
231
232   if ((count = ctf_archive_count (input->clin_arc)) == 0)
233     ctf_arc_close (input->clin_arc);
234
235   return (ssize_t) count;
236 }
237
238 /* Return a per-CU output CTF dictionary suitable for the given CU, creating and
239    interning it if need be.  */
240
241 _libctf_nonnull_((1,2))
242 static ctf_dict_t *
243 ctf_create_per_cu (ctf_dict_t *fp, const char *cu_name)
244 {
245   ctf_dict_t *cu_fp;
246   const char *ctf_name = NULL;
247   char *dynname = NULL;
248
249   /* First, check the mapping table and translate the per-CU name we use
250      accordingly.  */
251
252   if (fp->ctf_link_in_cu_mapping)
253     {
254       if ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping,
255                                           cu_name)) == NULL)
256         ctf_name = cu_name;
257     }
258
259   if (ctf_name == NULL)
260     ctf_name = cu_name;
261
262   if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL)
263     {
264       int err;
265
266       if ((cu_fp = ctf_create (&err)) == NULL)
267         {
268           ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive for "
269                                       "input CU %s"), cu_name);
270           ctf_set_errno (fp, err);
271           return NULL;
272         }
273
274       if ((dynname = strdup (ctf_name)) == NULL)
275         goto oom;
276       if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0)
277         goto oom;
278
279       ctf_import_unref (cu_fp, fp);
280       ctf_cuname_set (cu_fp, cu_name);
281       ctf_parent_name_set (cu_fp, _CTF_SECTION);
282     }
283   return cu_fp;
284
285  oom:
286   free (dynname);
287   ctf_dict_close (cu_fp);
288   ctf_set_errno (fp, ENOMEM);
289   return NULL;
290 }
291
292 /* Add a mapping directing that the CU named FROM should have its
293    conflicting/non-duplicate types (depending on link mode) go into a dict
294    named TO.  Many FROMs can share a TO.
295
296    We forcibly add a dict named TO in every case, even though it may well
297    wind up empty, because clients that use this facility usually expect to find
298    every TO dict present, even if empty, and malfunction otherwise.  */
299
300 int
301 ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to)
302 {
303   int err;
304   char *f = NULL, *t = NULL;
305   ctf_dynhash_t *one_out;
306
307   if (fp->ctf_link_in_cu_mapping == NULL)
308     fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string,
309                                                      ctf_hash_eq_string, free,
310                                                      free);
311   if (fp->ctf_link_in_cu_mapping == NULL)
312     goto oom;
313
314   if (fp->ctf_link_out_cu_mapping == NULL)
315     fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string,
316                                                       ctf_hash_eq_string, free,
317                                                       (ctf_hash_free_fun)
318                                                       ctf_dynhash_destroy);
319   if (fp->ctf_link_out_cu_mapping == NULL)
320     goto oom;
321
322   f = strdup (from);
323   t = strdup (to);
324   if (!f || !t)
325     goto oom;
326
327   /* Track both in a list from FROM to TO and in a list from TO to a list of
328      FROM.  The former is used to create TUs with the mapped-to name at need:
329      the latter is used in deduplicating links to pull in all input CUs
330      corresponding to a single output CU.  */
331
332   if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0)
333     {
334       ctf_set_errno (fp, err);
335       goto oom_noerrno;
336     }
337
338   /* f and t are now owned by the in_cu_mapping: reallocate them.  */
339   f = strdup (from);
340   t = strdup (to);
341   if (!f || !t)
342     goto oom;
343
344   if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL)
345     {
346       if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
347                                          free, NULL)) == NULL)
348         goto oom;
349       if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping,
350                                      t, one_out)) < 0)
351         {
352           ctf_dynhash_destroy (one_out);
353           ctf_set_errno (fp, err);
354           goto oom_noerrno;
355         }
356     }
357   else
358     free (t);
359
360   if (ctf_dynhash_insert (one_out, f, NULL) < 0)
361     {
362       ctf_set_errno (fp, err);
363       goto oom_noerrno;
364     }
365
366   return 0;
367
368  oom:
369   ctf_set_errno (fp, errno);
370  oom_noerrno:
371   free (f);
372   free (t);
373   return -1;
374 }
375
376 /* Set a function which is called to transform the names of archive members.
377    This is useful for applying regular transformations to many names, where
378    ctf_link_add_cu_mapping applies arbitrarily irregular changes to single
379    names.  The member name changer is applied at ctf_link_write time, so it
380    cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can.
381    The changer function accepts a name and should return a new
382    dynamically-allocated name, or NULL if the name should be left unchanged.  */
383 void
384 ctf_link_set_memb_name_changer (ctf_dict_t *fp,
385                                 ctf_link_memb_name_changer_f *changer,
386                                 void *arg)
387 {
388   fp->ctf_link_memb_name_changer = changer;
389   fp->ctf_link_memb_name_changer_arg = arg;
390 }
391
392 /* Set a function which is used to filter out unwanted variables from the link.  */
393 int
394 ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter,
395                               void *arg)
396 {
397   fp->ctf_link_variable_filter = filter;
398   fp->ctf_link_variable_filter_arg = arg;
399   return 0;
400 }
401
402 /* Check if we can safely add a variable with the given type to this dict.  */
403
404 static int
405 check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type,
406                 ctf_dvdef_t **out_dvd)
407 {
408   ctf_dvdef_t *dvd;
409
410   dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name);
411   *out_dvd = dvd;
412   if (!dvd)
413     return 1;
414
415   if (dvd->dvd_type != type)
416     {
417       /* Variable here.  Wrong type: cannot add.  Just skip it, because there is
418          no way to express this in CTF.  Don't even warn: this case is too
419          common.  (This might be the parent, in which case we'll try adding in
420          the child first, and only then give up.)  */
421       ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name);
422     }
423
424   return 0;                                   /* Already exists.  */
425 }
426
427 /* Link one variable named NAME of type TYPE found in IN_FP into FP.  */
428
429 static int
430 ctf_link_one_variable (ctf_dict_t *fp, ctf_dict_t *in_fp, const char *name,
431                        ctf_id_t type, int cu_mapped)
432 {
433   ctf_dict_t *per_cu_out_fp;
434   ctf_id_t dst_type = 0;
435   ctf_dvdef_t *dvd;
436
437   /* See if this variable is filtered out.  */
438
439   if (fp->ctf_link_variable_filter)
440     {
441       void *farg = fp->ctf_link_variable_filter_arg;
442       if (fp->ctf_link_variable_filter (in_fp, name, type, farg))
443         return 0;
444     }
445
446   /* If this type is mapped to a type in the parent dict, we want to try to add
447      to that first: if it reports a duplicate, or if the type is in a child
448      already, add straight to the child.  */
449
450   if ((dst_type = ctf_dedup_type_mapping (fp, in_fp, type)) == CTF_ERR)
451     return -1;                                  /* errno is set for us.  */
452
453   if (dst_type != 0)
454     {
455       if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
456         return -1;                              /* errno is set for us.  */
457
458       if (check_variable (name, fp, dst_type, &dvd))
459         {
460           /* No variable here: we can add it.  */
461           if (ctf_add_variable (fp, name, dst_type) < 0)
462             return -1;                          /* errno is set for us.  */
463           return 0;
464         }
465
466       /* Already present?  Nothing to do.  */
467       if (dvd && dvd->dvd_type == dst_type)
468         return 0;
469     }
470
471   /* Can't add to the parent due to a name clash, or because it references a
472      type only present in the child.  Try adding to the child, creating if need
473      be.  If we can't do that, skip it.  Don't add to a child if we're doing a
474      CU-mapped link, since that has only one output.  */
475
476   if (cu_mapped)
477     {
478       ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden "
479                    "due to conflicts: skipped.\n", name,
480                    ctf_unnamed_cuname (in_fp), type);
481       return 0;
482     }
483
484   if ((per_cu_out_fp = ctf_create_per_cu (fp, ctf_unnamed_cuname (in_fp))) == NULL)
485     return -1;                                  /* errno is set for us.  */
486
487   /* If the type was not found, check for it in the child too.  */
488   if (dst_type == 0)
489     {
490       if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
491                                               in_fp, type)) == CTF_ERR)
492         return -1;                              /* errno is set for us.   */
493
494       if (dst_type == 0)
495         {
496           ctf_err_warn (fp, 1, 0, _("type %lx for variable %s in input file %s "
497                                     "not found: skipped"), type, name,
498                         ctf_unnamed_cuname (in_fp));
499           /* Do not terminate the link: just skip the variable.  */
500           return 0;
501         }
502     }
503
504   if (check_variable (name, per_cu_out_fp, dst_type, &dvd))
505     if (ctf_add_variable (per_cu_out_fp, name, dst_type) < 0)
506       return (ctf_set_errno (fp, ctf_errno (per_cu_out_fp)));
507   return 0;
508 }
509
510 typedef struct link_sort_inputs_cb_arg
511 {
512   int is_cu_mapped;
513   ctf_dict_t *fp;
514 } link_sort_inputs_cb_arg_t;
515
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.  */
519 static int
520 ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two,
521                       void *arg)
522 {
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;
526
527   if (!cu_mapped || !cu_mapped->is_cu_mapped)
528     {
529       input_1 = (ctf_link_input_t *) one->hkv_value;
530       input_2 = (ctf_link_input_t *) two->hkv_value;
531     }
532   else
533     {
534       const char *name_1 = (const char *) one->hkv_key;
535       const char *name_2 = (const char *) two->hkv_key;
536
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);
539
540       /* There is no guarantee that CU-mappings actually have corresponding
541          inputs: the relative ordering in that case is unimportant.  */
542       if (!input_1)
543         return -1;
544       if (!input_2)
545         return 1;
546     }
547
548   if (input_1->n < input_2->n)
549     return -1;
550   else if (input_1->n > input_2->n)
551     return 1;
552   else
553     return 0;
554 }
555
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).  */
560 static ssize_t
561 ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
562                                      ctf_link_input_t **only_one_input)
563 {
564   ctf_dynhash_t *inputs = fp->ctf_link_inputs;
565   ctf_next_t *i = NULL;
566   void *name, *input;
567   ctf_link_input_t *one_input = NULL;
568   const char *one_name = NULL;
569   ssize_t count = 0, narcs = 0;
570   int err;
571
572   if (cu_names)
573     inputs = cu_names;
574
575   while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0)
576     {
577       ssize_t one_count;
578
579       one_name = (const char *) name;
580       /* If we are processing CU names, get the real input.  */
581       if (cu_names)
582         one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
583       else
584         one_input = (ctf_link_input_t *) input;
585
586       if (!one_input)
587         continue;
588
589       one_count = ctf_link_lazy_open (fp, one_input);
590
591       if (one_count < 0)
592         {
593           ctf_next_destroy (i);
594           return -1;                            /* errno is set for us.  */
595         }
596
597       count += one_count;
598       narcs++;
599     }
600   if (err != ECTF_NEXT_END)
601     {
602       ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating "
603                                   "CTF link inputs"));
604       ctf_set_errno (fp, err);
605       return -1;
606     }
607
608   if (!count)
609     return 0;
610
611   if (narcs == 1)
612     {
613       if (only_one_input)
614         *only_one_input = one_input;
615     }
616   else if (only_one_input)
617     *only_one_input = NULL;
618
619   return count;
620 }
621
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.
629
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.)
635
636    If no inputs exist that correspond to these CUs, return NULL with the errno
637    set to ECTF_NOCTFDATA.  */
638 static ctf_dict_t **
639 ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
640                                     ssize_t ninputs, uint32_t **parents)
641 {
642   ctf_dynhash_t *inputs = fp->ctf_link_inputs;
643   ctf_next_t *i = NULL;
644   void *name, *input;
645   link_sort_inputs_cb_arg_t sort_arg;
646   ctf_dict_t **dedup_inputs = NULL;
647   ctf_dict_t **walk;
648   uint32_t *parents_ = NULL;
649   int err;
650
651   if (cu_names)
652     inputs = cu_names;
653
654   if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL)
655     goto oom;
656
657   if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL)
658     goto oom;
659
660   walk = dedup_inputs;
661
662   /* Counting done: push every input into the array, in the order they were
663      passed to ctf_link_add_ctf (and ultimately ld).  */
664
665   sort_arg.is_cu_mapped = (cu_names != NULL);
666   sort_arg.fp = fp;
667
668   while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input,
669                                          ctf_link_sort_inputs, &sort_arg)) == 0)
670     {
671       const char *one_name = (const char *) name;
672       ctf_link_input_t *one_input;
673       ctf_dict_t *one_fp;
674       ctf_dict_t *parent_fp = NULL;
675       uint32_t parent_i;
676       ctf_next_t *j = NULL;
677
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.  */
680       if (cu_names)
681         one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name);
682       else
683         one_input = (ctf_link_input_t *) input;
684
685       if (!one_input || (!one_input->clin_arc && !one_input->clin_fp))
686         continue;
687
688       /* Short-circuit: if clin_fp is set, just use it.   */
689       if (one_input->clin_fp)
690         {
691           parents_[walk - dedup_inputs] = walk - dedup_inputs;
692           *walk = one_input->clin_fp;
693           walk++;
694           continue;
695         }
696
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
699          _CTF_SECTION.  */
700
701       if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION,
702                                       &err)) == NULL)
703         {
704           if (err != ECTF_NOMEMBNAM)
705             {
706               ctf_next_destroy (i);
707               ctf_set_errno (fp, err);
708               goto err;
709             }
710         }
711       else
712         {
713           *walk = parent_fp;
714           parent_i = walk - dedup_inputs;
715           walk++;
716         }
717
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,
724                                          1, &err)) != NULL)
725         {
726           if (one_fp->ctf_flags & LCTF_CHILD)
727             {
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
731                  it to itself.  */
732
733               ctf_import (one_fp, parent_fp);
734               if (parent_fp)
735                 parents_[walk - dedup_inputs] = parent_i;
736               else
737                 parents_[walk - dedup_inputs] = walk - dedup_inputs;
738             }
739           *walk = one_fp;
740           walk++;
741         }
742       if (err != ECTF_NEXT_END)
743         {
744           ctf_next_destroy (i);
745           goto iterr;
746         }
747     }
748   if (err != ECTF_NEXT_END)
749     goto iterr;
750
751   *parents = parents_;
752
753   return dedup_inputs;
754
755  oom:
756   err = ENOMEM;
757
758  iterr:
759   ctf_set_errno (fp, err);
760
761  err:
762   free (dedup_inputs);
763   free (parents_);
764   ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link "
765                             "input allocation"));
766   return NULL;
767 }
768
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.  */
773 static int
774 ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names,
775                                      ctf_dict_t **inputs, ssize_t ninputs)
776 {
777   ctf_next_t *it = NULL;
778   void *name;
779   int err;
780   ssize_t i;
781
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]);
786
787   /* Now close the archives they are part of.  */
788   if (cu_names)
789     {
790       while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0)
791         {
792           /* Remove the input from the linker inputs, if it exists, which also
793              closes it.  */
794
795           ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name);
796         }
797       if (err != ECTF_NEXT_END)
798         {
799           ctf_err_warn (fp, 0, err, _("iteration error in deduplicating link "
800                                       "input freeing"));
801           ctf_set_errno (fp, err);
802         }
803     }
804   else
805     ctf_dynhash_empty (fp->ctf_link_inputs);
806
807   return 0;
808 }
809
810 /* Do a deduplicating link of all variables in the inputs.
811
812    Also, if we are not omitting the variable section, integrate all symbols from
813    the symtypetabs into the variable section too.  (Duplication with the
814    symtypetab section in the output will be eliminated at serialization time.)  */
815
816 static int
817 ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs,
818                                   size_t ninputs, int cu_mapped)
819 {
820   size_t i;
821
822   for (i = 0; i < ninputs; i++)
823     {
824       ctf_next_t *it = NULL;
825       ctf_id_t type;
826       const char *name;
827
828       /* First the variables on the inputs.  */
829
830       while ((type = ctf_variable_next (inputs[i], &it, &name)) != CTF_ERR)
831         {
832           if (ctf_link_one_variable (fp, inputs[i], name, type, cu_mapped) < 0)
833             {
834               ctf_next_destroy (it);
835               return -1;                        /* errno is set for us.  */
836             }
837         }
838       if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
839         return ctf_set_errno (fp, ctf_errno (inputs[i]));
840
841       /* Next the symbols.  We integrate data symbols even though the compiler
842          is currently doing the same, to allow the compiler to stop in
843          future.  */
844
845       while ((type = ctf_symbol_next (inputs[i], &it, &name, 0)) != CTF_ERR)
846         {
847           if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
848             {
849               ctf_next_destroy (it);
850               return -1;                        /* errno is set for us.  */
851             }
852         }
853       if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
854         return ctf_set_errno (fp, ctf_errno (inputs[i]));
855
856       /* Finally the function symbols.  */
857
858       while ((type = ctf_symbol_next (inputs[i], &it, &name, 1)) != CTF_ERR)
859         {
860           if (ctf_link_one_variable (fp, inputs[i], name, type, 1) < 0)
861             {
862               ctf_next_destroy (it);
863               return -1;                        /* errno is set for us.  */
864             }
865         }
866       if (ctf_errno (inputs[i]) != ECTF_NEXT_END)
867         return ctf_set_errno (fp, ctf_errno (inputs[i]));
868     }
869   return 0;
870 }
871
872 /* Check for symbol conflicts during linking.  Three possibilities: already
873    exists, conflicting, or nonexistent.  We don't have a dvd structure we can
874    use as a flag like check_variable does, so we use a tristate return
875    value instead: -1: conflicting; 1: nonexistent: 0: already exists.  */
876
877 static int
878 check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions)
879 {
880   ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash;
881   ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash;
882   void *value;
883
884   /* Wrong type (function when object is wanted, etc).  */
885   if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL))
886     return -1;
887
888   /* Not present at all yet.  */
889   if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value))
890     return 1;
891
892   /* Already present.  */
893   if ((ctf_id_t) (uintptr_t) value == type)
894     return 0;
895
896   /* Wrong type.  */
897   return -1;
898 }
899
900 /* Do a deduplicating link of one symtypetab (function info or data object) in
901    one input dict.  */
902
903 static int
904 ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input,
905                                        int cu_mapped, int functions)
906 {
907   ctf_next_t *it = NULL;
908   const char *name;
909   ctf_id_t type;
910
911   while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR)
912     {
913       ctf_id_t dst_type;
914       ctf_dict_t *per_cu_out_fp;
915       int sym;
916
917       /* Look in the parent first.  */
918
919       if ((dst_type = ctf_dedup_type_mapping (fp, input, type)) == CTF_ERR)
920         return -1;                              /* errno is set for us.  */
921
922       if (dst_type != 0)
923         {
924           if (!ctf_assert (fp, ctf_type_isparent (fp, dst_type)))
925             return -1;                          /* errno is set for us.  */
926
927           sym = check_sym (fp, name, dst_type, functions);
928
929           /* Already present: next symbol.  */
930           if (sym == 0)
931             continue;
932           /* Not present: add it.  */
933           else if (sym > 0)
934             {
935               if (ctf_add_funcobjt_sym (fp, functions,
936                                         name, dst_type) < 0)
937                 return -1;                      /* errno is set for us.  */
938               continue;
939             }
940         }
941
942       /* Can't add to the parent due to a name clash (most unlikely), or because
943          it references a type only present in the child.  Try adding to the
944          child, creating if need be.  If we can't do that, skip it.  Don't add
945          to a child if we're doing a CU-mapped link, since that has only one
946          output.  */
947       if (cu_mapped)
948         {
949           ctf_dprintf ("Symbol %s in input file %s depends on a type %lx "
950                        "hidden due to conflicts: skipped.\n", name,
951                        ctf_unnamed_cuname (input), type);
952           continue;
953         }
954
955       if ((per_cu_out_fp = ctf_create_per_cu (fp, ctf_unnamed_cuname (input))) == NULL)
956         return -1;                              /* errno is set for us.  */
957
958       /* If the type was not found, check for it in the child too.  */
959       if (dst_type == 0)
960         {
961           if ((dst_type = ctf_dedup_type_mapping (per_cu_out_fp,
962                                                   input, type)) == CTF_ERR)
963             return -1;                          /* errno is set for us.  */
964
965           if (dst_type == 0)
966             {
967               ctf_err_warn (fp, 1, 0,
968                             _("type %lx for symbol %s in input file %s "
969                               "not found: skipped"), type, name,
970                             ctf_unnamed_cuname (input));
971               continue;
972             }
973         }
974
975       sym = check_sym (per_cu_out_fp, name, dst_type, functions);
976
977       /* Already present: next symbol.  */
978       if (sym == 0)
979         continue;
980       /* Not present: add it.  */
981       else if (sym > 0)
982         {
983           if (ctf_add_funcobjt_sym (per_cu_out_fp, functions,
984                                     name, dst_type) < 0)
985             return -1;                          /* errno is set for us.  */
986         }
987       else
988         {
989           /* Perhaps this should be an assertion failure.  */
990           ctf_err_warn (fp, 0, ECTF_DUPLICATE,
991                         _("symbol %s in input file %s found conflicting "
992                           "even when trying in per-CU dict."), name,
993                         ctf_unnamed_cuname (input));
994           return (ctf_set_errno (fp, ECTF_DUPLICATE));
995         }
996     }
997   if (ctf_errno (input) != ECTF_NEXT_END)
998     {
999       ctf_set_errno (fp, ctf_errno (input));
1000       ctf_err_warn (fp, 0, ctf_errno (input),
1001                     functions ? _("iterating over function symbols") :
1002                     _("iterating over data symbols"));
1003       return -1;
1004     }
1005
1006   return 0;
1007 }
1008
1009 /* Do a deduplicating link of the function info and data objects
1010    in the inputs.  */
1011 static int
1012 ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs,
1013                              size_t ninputs, int cu_mapped)
1014 {
1015   size_t i;
1016
1017   for (i = 0; i < ninputs; i++)
1018     {
1019       if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
1020                                                  cu_mapped, 0) < 0)
1021         return -1;                              /* errno is set for us.  */
1022
1023       if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i],
1024                                                  cu_mapped, 1) < 0)
1025         return -1;                              /* errno is set for us.  */
1026     }
1027
1028   return 0;
1029 }
1030
1031 /* Do the per-CU part of a deduplicating link.  */
1032 static int
1033 ctf_link_deduplicating_per_cu (ctf_dict_t *fp)
1034 {
1035   ctf_next_t *i = NULL;
1036   int err;
1037   void *out_cu;
1038   void *in_cus;
1039
1040   /* Links with a per-CU mapping in force get a first pass of deduplication,
1041      dedupping the inputs for a given CU mapping into the output for that
1042      mapping.  The outputs from this process get fed back into the final pass
1043      that is carried out even for non-CU links.  */
1044
1045   while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &out_cu,
1046                                   &in_cus)) == 0)
1047     {
1048       const char *out_name = (const char *) out_cu;
1049       ctf_dynhash_t *in = (ctf_dynhash_t *) in_cus;
1050       ctf_dict_t *out = NULL;
1051       ctf_dict_t **inputs;
1052       ctf_dict_t **outputs;
1053       ctf_archive_t *in_arc;
1054       ssize_t ninputs;
1055       ctf_link_input_t *only_input;
1056       uint32_t noutputs;
1057       uint32_t *parents;
1058
1059       if ((ninputs = ctf_link_deduplicating_count_inputs (fp, in,
1060                                                           &only_input)) == -1)
1061         goto err_open_inputs;
1062
1063       /* CU mapping with no inputs?  Skip.  */
1064       if (ninputs == 0)
1065         continue;
1066
1067       if (labs ((long int) ninputs) > 0xfffffffe)
1068         {
1069           ctf_err_warn (fp, 0, EFBIG, _("too many inputs in deduplicating "
1070                                         "link: %li"), (long int) ninputs);
1071           ctf_set_errno (fp, EFBIG);
1072           goto err_open_inputs;
1073         }
1074
1075       /* Short-circuit: a cu-mapped link with only one input archive with
1076          unconflicting contents is a do-nothing, and we can just leave the input
1077          in place: we do have to change the cuname, though, so we unwrap it,
1078          change the cuname, then stuff it back in the linker input again, via
1079          the clin_fp short-circuit member.  ctf_link_deduplicating_open_inputs
1080          will spot this member and jam it straight into the next link phase,
1081          ignoring the corresponding archive.  */
1082       if (only_input && ninputs == 1)
1083         {
1084           ctf_next_t *ai = NULL;
1085           int err;
1086
1087           /* We can abuse an archive iterator to get the only member cheaply, no
1088              matter what its name.  */
1089           only_input->clin_fp = ctf_archive_next (only_input->clin_arc,
1090                                                   &ai, NULL, 0, &err);
1091           if (!only_input->clin_fp)
1092             {
1093               ctf_err_warn (fp, 0, err, _("cannot open archive %s in "
1094                                           "CU-mapped CTF link"),
1095                             only_input->clin_filename);
1096               ctf_set_errno (fp, err);
1097               goto err_open_inputs;
1098             }
1099           ctf_next_destroy (ai);
1100
1101           if (strcmp (only_input->clin_filename, out_name) != 0)
1102             {
1103               /* Renaming. We need to add a new input, then null out the
1104                  clin_arc and clin_fp of the old one to stop it being
1105                  auto-closed on removal.  The new input needs its cuname changed
1106                  to out_name, which is doable only because the cuname is a
1107                  dynamic property which can be changed even in readonly
1108                  dicts. */
1109
1110               ctf_cuname_set (only_input->clin_fp, out_name);
1111               if (ctf_link_add_ctf_internal (fp, only_input->clin_arc,
1112                                              only_input->clin_fp,
1113                                              out_name) < 0)
1114                 {
1115                   ctf_err_warn (fp, 0, 0, _("cannot add intermediate files "
1116                                             "to link"));
1117                   goto err_open_inputs;
1118                 }
1119               only_input->clin_arc = NULL;
1120               only_input->clin_fp = NULL;
1121               ctf_dynhash_remove (fp->ctf_link_inputs,
1122                                   only_input->clin_filename);
1123             }
1124           continue;
1125         }
1126
1127       /* This is a real CU many-to-one mapping: we must dedup the inputs into
1128          a new output to be used in the final link phase.  */
1129
1130       if ((inputs = ctf_link_deduplicating_open_inputs (fp, in, ninputs,
1131                                                         &parents)) == NULL)
1132         {
1133           ctf_next_destroy (i);
1134           goto err_inputs;
1135         }
1136
1137       if ((out = ctf_create (&err)) == NULL)
1138         {
1139           ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive "
1140                                       "for %s"),
1141                         out_name);
1142           ctf_set_errno (fp, err);
1143           goto err_inputs;
1144         }
1145
1146       /* Share the atoms table to reduce memory usage.  */
1147       out->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc;
1148
1149       /* No ctf_imports at this stage: this per-CU dictionary has no parents.
1150          Parent/child deduplication happens in the link's final pass.  However,
1151          the cuname *is* important, as it is propagated into the final
1152          dictionary.  */
1153       ctf_cuname_set (out, out_name);
1154
1155       if (ctf_dedup (out, inputs, ninputs, parents, 1) < 0)
1156         {
1157           ctf_set_errno (fp, ctf_errno (out));
1158           ctf_err_warn (fp, 0, 0, _("CU-mapped deduplication failed for %s"),
1159                         out_name);
1160           goto err_inputs;
1161         }
1162
1163       if ((outputs = ctf_dedup_emit (out, inputs, ninputs, parents,
1164                                      &noutputs, 1)) == NULL)
1165         {
1166           ctf_set_errno (fp, ctf_errno (out));
1167           ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link type emission "
1168                                      "failed for %s"), out_name);
1169           goto err_inputs;
1170         }
1171       if (!ctf_assert (fp, noutputs == 1))
1172         {
1173           size_t j;
1174           for (j = 1; j < noutputs; j++)
1175             ctf_dict_close (outputs[j]);
1176           goto err_inputs_outputs;
1177         }
1178
1179       if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1180           && ctf_link_deduplicating_variables (out, inputs, ninputs, 1) < 0)
1181         {
1182           ctf_set_errno (fp, ctf_errno (out));
1183           ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link variable "
1184                                     "emission failed for %s"), out_name);
1185           goto err_inputs_outputs;
1186         }
1187
1188       ctf_dedup_fini (out, outputs, noutputs);
1189
1190       /* For now, we omit symbol section linking for CU-mapped links, until it
1191          is clear how to unify the symbol table across such links.  (Perhaps we
1192          should emit an unconditionally indexed symtab, like the compiler
1193          does.)  */
1194
1195       if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0)
1196         {
1197           free (inputs);
1198           free (parents);
1199           goto err_outputs;
1200         }
1201       free (inputs);
1202       free (parents);
1203
1204       /* Splice any errors or warnings created during this link back into the
1205          dict that the caller knows about.  */
1206       ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1207
1208       /* This output now becomes an input to the next link phase, with a name
1209          equal to the CU name.  We have to wrap it in an archive wrapper
1210          first.  */
1211
1212       if ((in_arc = ctf_new_archive_internal (0, 0, NULL, outputs[0], NULL,
1213                                               NULL, &err)) == NULL)
1214         {
1215           ctf_set_errno (fp, err);
1216           goto err_outputs;
1217         }
1218
1219       if (ctf_link_add_ctf_internal (fp, in_arc, NULL,
1220                                      ctf_cuname (outputs[0])) < 0)
1221         {
1222           ctf_err_warn (fp, 0, 0, _("cannot add intermediate files to link"));
1223           goto err_outputs;
1224         }
1225
1226       ctf_dict_close (out);
1227       free (outputs);
1228       continue;
1229
1230     err_inputs_outputs:
1231       ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1232       ctf_dict_close (outputs[0]);
1233       free (outputs);
1234     err_inputs:
1235       ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs);
1236       ctf_dict_close (out);
1237       free (inputs);
1238       free (parents);
1239     err_open_inputs:
1240       ctf_next_destroy (i);
1241       return -1;
1242
1243     err_outputs:
1244       ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings);
1245       ctf_dict_close (outputs[0]);
1246       free (outputs);
1247       ctf_next_destroy (i);
1248       return -1;                                /* Errno is set for us.  */
1249     }
1250   if (err != ECTF_NEXT_END)
1251     {
1252       ctf_err_warn (fp, 0, err, _("iteration error in CU-mapped deduplicating "
1253                                   "link"));
1254       return ctf_set_errno (fp, err);
1255     }
1256
1257   return 0;
1258 }
1259
1260 /* Do a deduplicating link using the ctf-dedup machinery.  */
1261 static void
1262 ctf_link_deduplicating (ctf_dict_t *fp)
1263 {
1264   size_t i;
1265   ctf_dict_t **inputs, **outputs = NULL;
1266   ssize_t ninputs;
1267   uint32_t noutputs;
1268   uint32_t *parents;
1269
1270   if (ctf_dedup_atoms_init (fp) < 0)
1271     {
1272       ctf_err_warn (fp, 0, 0, _("allocating CTF dedup atoms table"));
1273       return;                                   /* Errno is set for us.  */
1274     }
1275
1276   if (fp->ctf_link_out_cu_mapping
1277       && (ctf_link_deduplicating_per_cu (fp) < 0))
1278     return;                                     /* Errno is set for us.  */
1279
1280   if ((ninputs = ctf_link_deduplicating_count_inputs (fp, NULL, NULL)) < 0)
1281     return;                                     /* Errno is set for us.  */
1282
1283   if ((inputs = ctf_link_deduplicating_open_inputs (fp, NULL, ninputs,
1284                                                     &parents)) == NULL)
1285     return;                                     /* Errno is set for us.  */
1286
1287   if (ninputs == 1 && ctf_cuname (inputs[0]) != NULL)
1288     ctf_cuname_set (fp, ctf_cuname (inputs[0]));
1289
1290   if (ctf_dedup (fp, inputs, ninputs, parents, 0) < 0)
1291     {
1292       ctf_err_warn (fp, 0, 0, _("deduplication failed for %s"),
1293                     ctf_link_input_name (fp));
1294       goto err;
1295     }
1296
1297   if ((outputs = ctf_dedup_emit (fp, inputs, ninputs, parents, &noutputs,
1298                                  0)) == NULL)
1299     {
1300       ctf_err_warn (fp, 0, 0, _("deduplicating link type emission failed "
1301                                 "for %s"), ctf_link_input_name (fp));
1302       goto err;
1303     }
1304
1305   if (!ctf_assert (fp, outputs[0] == fp))
1306     {
1307       for (i = 1; i < noutputs; i++)
1308         ctf_dict_close (outputs[i]);
1309       goto err;
1310     }
1311
1312   for (i = 0; i < noutputs; i++)
1313     {
1314       char *dynname;
1315
1316       /* We already have access to this one.  Close the duplicate.  */
1317       if (i == 0)
1318         {
1319           ctf_dict_close (outputs[0]);
1320           continue;
1321         }
1322
1323       if ((dynname = strdup (ctf_cuname (outputs[i]))) == NULL)
1324         goto oom_one_output;
1325
1326       if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, outputs[i]) < 0)
1327         goto oom_one_output;
1328
1329       continue;
1330
1331     oom_one_output:
1332       ctf_set_errno (fp, ENOMEM);
1333       ctf_err_warn (fp, 0, 0, _("out of memory allocating link outputs"));
1334       free (dynname);
1335
1336       for (; i < noutputs; i++)
1337         ctf_dict_close (outputs[i]);
1338       goto err;
1339     }
1340
1341   if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION)
1342       && ctf_link_deduplicating_variables (fp, inputs, ninputs, 0) < 0)
1343     {
1344       ctf_err_warn (fp, 0, 0, _("deduplicating link variable emission failed for "
1345                                 "%s"), ctf_link_input_name (fp));
1346       goto err_clean_outputs;
1347     }
1348
1349   if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0)
1350     {
1351       ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for "
1352                                 "%s"), ctf_link_input_name (fp));
1353       goto err_clean_outputs;
1354     }
1355
1356   ctf_dedup_fini (fp, outputs, noutputs);
1357
1358   /* Now close all the inputs, including per-CU intermediates.  */
1359
1360   if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0)
1361     return;                                     /* errno is set for us.  */
1362
1363   ninputs = 0;                                  /* Prevent double-close.  */
1364   ctf_set_errno (fp, 0);
1365
1366   /* Fall through.  */
1367
1368  err:
1369   for (i = 0; i < (size_t) ninputs; i++)
1370     ctf_dict_close (inputs[i]);
1371   free (inputs);
1372   free (parents);
1373   free (outputs);
1374   return;
1375
1376  err_clean_outputs:
1377   for (i = 1; i < noutputs; i++)
1378     {
1379       ctf_dynhash_remove (fp->ctf_link_outputs, ctf_cuname (outputs[i]));
1380       ctf_dict_close (outputs[i]);
1381     }
1382   goto err;
1383 }
1384
1385 /* Merge types and variable sections in all dicts added to the link together.
1386    All the added dicts are closed.  */
1387 int
1388 ctf_link (ctf_dict_t *fp, int flags)
1389 {
1390   ctf_next_t *i = NULL;
1391   int err;
1392
1393   fp->ctf_link_flags = flags;
1394
1395   if (fp->ctf_link_inputs == NULL)
1396     return 0;                                   /* Nothing to do. */
1397
1398   if (fp->ctf_link_outputs == NULL)
1399     fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string,
1400                                                ctf_hash_eq_string, free,
1401                                                (ctf_hash_free_fun)
1402                                                ctf_dict_close);
1403
1404   if (fp->ctf_link_outputs == NULL)
1405     return ctf_set_errno (fp, ENOMEM);
1406
1407   /* Create empty CUs if requested.  We do not currently claim that multiple
1408      links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and
1409      not set in others will do anything especially sensible.  */
1410
1411   fp->ctf_flags |= LCTF_LINKING;
1412   if (fp->ctf_link_out_cu_mapping && (flags & CTF_LINK_EMPTY_CU_MAPPINGS))
1413     {
1414       void *v;
1415
1416       while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &v,
1417                                       NULL)) == 0)
1418         {
1419           const char *to = (const char *) v;
1420           if (ctf_create_per_cu (fp, to) == NULL)
1421             {
1422               fp->ctf_flags &= ~LCTF_LINKING;
1423               ctf_next_destroy (i);
1424               return -1;                        /* Errno is set for us.  */
1425             }
1426         }
1427       if (err != ECTF_NEXT_END)
1428         {
1429           fp->ctf_flags &= ~LCTF_LINKING;
1430           ctf_err_warn (fp, 1, err, _("iteration error creating empty CUs"));
1431           ctf_set_errno (fp, err);
1432           return -1;
1433         }
1434     }
1435
1436   ctf_link_deduplicating (fp);
1437
1438   fp->ctf_flags &= ~LCTF_LINKING;
1439   if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA))
1440     return -1;
1441   return 0;
1442 }
1443
1444 typedef struct ctf_link_out_string_cb_arg
1445 {
1446   const char *str;
1447   uint32_t offset;
1448   int err;
1449 } ctf_link_out_string_cb_arg_t;
1450
1451 /* Intern a string in the string table of an output per-CU CTF file.  */
1452 static void
1453 ctf_link_intern_extern_string (void *key _libctf_unused_, void *value,
1454                                void *arg_)
1455 {
1456   ctf_dict_t *fp = (ctf_dict_t *) value;
1457   ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_;
1458
1459   fp->ctf_flags |= LCTF_DIRTY;
1460   if (!ctf_str_add_external (fp, arg->str, arg->offset))
1461     arg->err = ENOMEM;
1462 }
1463
1464 /* Repeatedly call ADD_STRING to acquire strings from the external string table,
1465    adding them to the atoms table for this CU and all subsidiary CUs.
1466
1467    If ctf_link is also called, it must be called first if you want the new CTF
1468    files ctf_link can create to get their strings dedupped against the ELF
1469    strtab properly.  */
1470 int
1471 ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string,
1472                      void *arg)
1473 {
1474   const char *str;
1475   uint32_t offset;
1476   int err = 0;
1477
1478   while ((str = add_string (&offset, arg)) != NULL)
1479     {
1480       ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 };
1481
1482       fp->ctf_flags |= LCTF_DIRTY;
1483       if (!ctf_str_add_external (fp, str, offset))
1484         err = ENOMEM;
1485
1486       ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string,
1487                         &iter_arg);
1488       if (iter_arg.err)
1489         err = iter_arg.err;
1490     }
1491
1492   if (err)
1493     ctf_set_errno (fp, err);
1494
1495   return -err;
1496 }
1497
1498 /* Inform the ctf-link machinery of a new symbol in the target symbol table
1499    (which must be some symtab that is not usually stripped, and which
1500    is in agreement with ctf_bfdopen_ctfsect).  May be called either before or
1501    after ctf_link_add_strtab.  */
1502 int
1503 ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym)
1504 {
1505   ctf_in_flight_dynsym_t *cid;
1506
1507   /* Cheat a little: if there is already an ENOMEM error code recorded against
1508      this dict, we shouldn't even try to add symbols because there will be no
1509      memory to do so: probably we failed to add some previous symbol.  This
1510      makes out-of-memory exits 'sticky' across calls to this function, so the
1511      caller doesn't need to worry about error conditions.  */
1512
1513   if (ctf_errno (fp) == ENOMEM)
1514     return -ENOMEM;                             /* errno is set for us.  */
1515
1516   if (ctf_symtab_skippable (sym))
1517     return 0;
1518
1519   if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC)
1520     return 0;
1521
1522   /* Add the symbol to the in-flight list.  */
1523
1524   if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL)
1525     goto oom;
1526
1527   cid->cid_sym = *sym;
1528   ctf_list_append (&fp->ctf_in_flight_dynsyms, cid);
1529
1530   return 0;
1531
1532  oom:
1533   ctf_dynhash_destroy (fp->ctf_dynsyms);
1534   fp->ctf_dynsyms = NULL;
1535   ctf_set_errno (fp, ENOMEM);
1536   return -ENOMEM;
1537 }
1538
1539 /* Impose an ordering on symbols.  The ordering takes effect immediately, but
1540    since the ordering info does not include type IDs, lookups may return nothing
1541    until such IDs are added by calls to ctf_add_*_sym.  Must be called after
1542    ctf_link_add_strtab and ctf_link_add_linker_symbol.  */
1543 int
1544 ctf_link_shuffle_syms (ctf_dict_t *fp)
1545 {
1546   ctf_in_flight_dynsym_t *did, *nid;
1547   ctf_next_t *i = NULL;
1548   int err = ENOMEM;
1549   void *name_, *sym_;
1550
1551   if (!fp->ctf_dynsyms)
1552     {
1553       fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string,
1554                                             ctf_hash_eq_string,
1555                                             NULL, free);
1556       if (!fp->ctf_dynsyms)
1557         {
1558           ctf_set_errno (fp, ENOMEM);
1559           return -ENOMEM;
1560         }
1561     }
1562
1563   /* Add all the symbols, excluding only those we already know are prohibited
1564      from appearing in symtypetabs.  */
1565
1566   for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid)
1567     {
1568       ctf_link_sym_t *new_sym;
1569
1570       nid = ctf_list_next (did);
1571       ctf_list_delete (&fp->ctf_in_flight_dynsyms, did);
1572
1573       /* We might get a name or an external strtab offset.  The strtab offset is
1574          guaranteed resolvable at this point, so turn it into a string.  */
1575
1576       if (did->cid_sym.st_name == NULL)
1577         {
1578           uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1);
1579
1580           did->cid_sym.st_name = ctf_strraw (fp, off);
1581           did->cid_sym.st_nameidx_set = 0;
1582           if (!ctf_assert (fp, did->cid_sym.st_name != NULL))
1583             return -ECTF_INTERNAL;              /* errno is set for us.  */
1584         }
1585
1586       /* The symbol might have turned out to be nameless, so we have to recheck
1587          for skippability here.  */
1588       if (!ctf_symtab_skippable (&did->cid_sym))
1589         {
1590           ctf_dprintf ("symbol from linker: %s (%x)\n", did->cid_sym.st_name,
1591                        did->cid_sym.st_symidx);
1592
1593           if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL)
1594             goto local_oom;
1595
1596           memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t));
1597           if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0)
1598             goto local_oom;
1599
1600           if (fp->ctf_dynsymmax < new_sym->st_symidx)
1601             fp->ctf_dynsymmax = new_sym->st_symidx;
1602         }
1603
1604       free (did);
1605       continue;
1606
1607     local_oom:
1608       free (did);
1609       free (new_sym);
1610       goto err;
1611     }
1612
1613   /* If no symbols are reported, unwind what we have done and return.  This
1614      makes it a bit easier for the serializer to tell that no symbols have been
1615      reported and that it should look elsewhere for reported symbols.  */
1616   if (!ctf_dynhash_elements (fp->ctf_dynsyms))
1617     {
1618       ctf_dprintf ("No symbols: not a final link.\n");
1619       ctf_dynhash_destroy (fp->ctf_dynsyms);
1620       fp->ctf_dynsyms = NULL;
1621       return 0;
1622     }
1623
1624   /* Construct a mapping from shndx to the symbol info.  */
1625   free (fp->ctf_dynsymidx);
1626   if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1,
1627                                    sizeof (ctf_link_sym_t *))) == NULL)
1628     goto err;
1629
1630   while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0)
1631     {
1632       const char *name = (const char *) name;
1633       ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_;
1634
1635       if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax))
1636         {
1637           ctf_next_destroy (i);
1638           err = ctf_errno (fp);
1639           goto err;
1640         }
1641       fp->ctf_dynsymidx[symp->st_symidx] = symp;
1642     }
1643   if (err != ECTF_NEXT_END)
1644     {
1645       ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols"));
1646       goto err;
1647     }
1648   return 0;
1649
1650  err:
1651   /* Leave the in-flight symbols around: they'll be freed at
1652      dict close time regardless.  */
1653   ctf_dynhash_destroy (fp->ctf_dynsyms);
1654   fp->ctf_dynsyms = NULL;
1655   free (fp->ctf_dynsymidx);
1656   fp->ctf_dynsymidx = NULL;
1657   fp->ctf_dynsymmax = 0;
1658   ctf_set_errno (fp, err);
1659   return -err;
1660 }
1661
1662 typedef struct ctf_name_list_accum_cb_arg
1663 {
1664   char **names;
1665   ctf_dict_t *fp;
1666   ctf_dict_t **files;
1667   size_t i;
1668   char **dynames;
1669   size_t ndynames;
1670 } ctf_name_list_accum_cb_arg_t;
1671
1672 /* Accumulate the names and a count of the names in the link output hash.  */
1673 static void
1674 ctf_accumulate_archive_names (void *key, void *value, void *arg_)
1675 {
1676   const char *name = (const char *) key;
1677   ctf_dict_t *fp = (ctf_dict_t *) value;
1678   char **names;
1679   ctf_dict_t **files;
1680   ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_;
1681
1682   if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL)
1683     {
1684       (arg->i)--;
1685       ctf_set_errno (arg->fp, ENOMEM);
1686       return;
1687     }
1688
1689   if ((files = realloc (arg->files, sizeof (ctf_dict_t *) * arg->i)) == NULL)
1690     {
1691       (arg->i)--;
1692       ctf_set_errno (arg->fp, ENOMEM);
1693       return;
1694     }
1695
1696   /* Allow the caller to get in and modify the name at the last minute.  If the
1697      caller *does* modify the name, we have to stash away the new name the
1698      caller returned so we can free it later on.  (The original name is the key
1699      of the ctf_link_outputs hash and is freed by the dynhash machinery.)  */
1700
1701   if (fp->ctf_link_memb_name_changer)
1702     {
1703       char **dynames;
1704       char *dyname;
1705       void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1706
1707       dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg);
1708
1709       if (dyname != NULL)
1710         {
1711           if ((dynames = realloc (arg->dynames,
1712                                   sizeof (char *) * ++(arg->ndynames))) == NULL)
1713             {
1714               (arg->ndynames)--;
1715               ctf_set_errno (arg->fp, ENOMEM);
1716               return;
1717             }
1718             arg->dynames = dynames;
1719             name = (const char *) dyname;
1720         }
1721     }
1722
1723   arg->names = names;
1724   arg->names[(arg->i) - 1] = (char *) name;
1725   arg->files = files;
1726   arg->files[(arg->i) - 1] = fp;
1727 }
1728
1729 /* Change the name of the parent CTF section, if the name transformer has got to
1730    it.  */
1731 static void
1732 ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg)
1733 {
1734   ctf_dict_t *fp = (ctf_dict_t *) value;
1735   const char *name = (const char *) arg;
1736
1737   ctf_parent_name_set (fp, name);
1738 }
1739
1740 /* Warn if we may suffer information loss because the CTF input files are too
1741    old.  Usually we provide complete backward compatibility, but compiler
1742    changes etc which never hit a release may have a flag in the header that
1743    simply prevents those changes from being used.  */
1744 static void
1745 ctf_link_warn_outdated_inputs (ctf_dict_t *fp)
1746 {
1747   ctf_next_t *i = NULL;
1748   void *name_;
1749   void *ifp_;
1750   int err;
1751
1752   while ((err = ctf_dynhash_next (fp->ctf_link_inputs, &i, &name_, &ifp_)) == 0)
1753     {
1754       const char *name = (const char *) name_;
1755       ctf_dict_t *ifp = (ctf_dict_t *) ifp_;
1756
1757       if (!(ifp->ctf_header->cth_flags & CTF_F_NEWFUNCINFO)
1758           && (ifp->ctf_header->cth_varoff - ifp->ctf_header->cth_funcoff) > 0)
1759         ctf_err_warn (ifp, 1, 0, _("linker input %s has CTF func info but uses "
1760                                    "an old, unreleased func info format: "
1761                                    "this func info section will be dropped."),
1762                       name);
1763     }
1764   if (err != ECTF_NEXT_END)
1765     ctf_err_warn (fp, 0, err, _("error checking for outdated inputs"));
1766 }
1767
1768 /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file
1769    (otherwise) into a new dynamically-allocated string, and return it.
1770    Members with sizes above THRESHOLD are compressed.  */
1771 unsigned char *
1772 ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold)
1773 {
1774   ctf_name_list_accum_cb_arg_t arg;
1775   char **names;
1776   char *transformed_name = NULL;
1777   ctf_dict_t **files;
1778   FILE *f = NULL;
1779   size_t i;
1780   int err;
1781   long fsize;
1782   const char *errloc;
1783   unsigned char *buf = NULL;
1784
1785   memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t));
1786   arg.fp = fp;
1787   fp->ctf_flags |= LCTF_LINKING;
1788
1789   ctf_link_warn_outdated_inputs (fp);
1790
1791   if (fp->ctf_link_outputs)
1792     {
1793       ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg);
1794       if (ctf_errno (fp) < 0)
1795         {
1796           errloc = "hash creation";
1797           goto err;
1798         }
1799     }
1800
1801   /* No extra outputs? Just write a simple ctf_dict_t.  */
1802   if (arg.i == 0)
1803     {
1804       unsigned char *ret = ctf_write_mem (fp, size, threshold);
1805       fp->ctf_flags &= ~LCTF_LINKING;
1806       return ret;
1807     }
1808
1809   /* Writing an archive.  Stick ourselves (the shared repository, parent of all
1810      other archives) on the front of it with the default name.  */
1811   if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL)
1812     {
1813       errloc = "name reallocation";
1814       goto err_no;
1815     }
1816   arg.names = names;
1817   memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i));
1818
1819   arg.names[0] = (char *) _CTF_SECTION;
1820   if (fp->ctf_link_memb_name_changer)
1821     {
1822       void *nc_arg = fp->ctf_link_memb_name_changer_arg;
1823
1824       transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION,
1825                                                          nc_arg);
1826
1827       if (transformed_name != NULL)
1828         {
1829           arg.names[0] = transformed_name;
1830           ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name,
1831                             transformed_name);
1832         }
1833     }
1834
1835   /* Propagate the link flags to all the dicts in this link.  */
1836   for (i = 0; i < arg.i; i++)
1837     {
1838       arg.files[i]->ctf_link_flags = fp->ctf_link_flags;
1839       arg.files[i]->ctf_flags |= LCTF_LINKING;
1840     }
1841
1842   if ((files = realloc (arg.files,
1843                         sizeof (struct ctf_dict *) * (arg.i + 1))) == NULL)
1844     {
1845       errloc = "ctf_dict reallocation";
1846       goto err_no;
1847     }
1848   arg.files = files;
1849   memmove (&(arg.files[1]), arg.files, sizeof (ctf_dict_t *) * (arg.i));
1850   arg.files[0] = fp;
1851
1852   if ((f = tmpfile ()) == NULL)
1853     {
1854       errloc = "tempfile creation";
1855       goto err_no;
1856     }
1857
1858   if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1,
1859                                (const char **) arg.names,
1860                                threshold)) < 0)
1861     {
1862       errloc = "archive writing";
1863       ctf_set_errno (fp, err);
1864       goto err;
1865     }
1866
1867   if (fseek (f, 0, SEEK_END) < 0)
1868     {
1869       errloc = "seeking to end";
1870       goto err_no;
1871     }
1872
1873   if ((fsize = ftell (f)) < 0)
1874     {
1875       errloc = "filesize determination";
1876       goto err_no;
1877     }
1878
1879   if (fseek (f, 0, SEEK_SET) < 0)
1880     {
1881       errloc = "filepos resetting";
1882       goto err_no;
1883     }
1884
1885   if ((buf = malloc (fsize)) == NULL)
1886     {
1887       errloc = "CTF archive buffer allocation";
1888       goto err_no;
1889     }
1890
1891   while (!feof (f) && fread (buf, fsize, 1, f) == 0)
1892     if (ferror (f))
1893       {
1894         errloc = "reading archive from temporary file";
1895         goto err_no;
1896       }
1897
1898   *size = fsize;
1899   free (arg.names);
1900   free (arg.files);
1901   free (transformed_name);
1902   if (arg.ndynames)
1903     {
1904       size_t i;
1905       for (i = 0; i < arg.ndynames; i++)
1906         free (arg.dynames[i]);
1907       free (arg.dynames);
1908     }
1909   fclose (f);
1910   return buf;
1911
1912  err_no:
1913   ctf_set_errno (fp, errno);
1914
1915   /* Turn off the is-linking flag on all the dicts in this link.  */
1916   for (i = 0; i < arg.i; i++)
1917     arg.files[i]->ctf_flags &= ~LCTF_LINKING;
1918  err:
1919   free (buf);
1920   if (f)
1921     fclose (f);
1922   free (arg.names);
1923   free (arg.files);
1924   free (transformed_name);
1925   if (arg.ndynames)
1926     {
1927       size_t i;
1928       for (i = 0; i < arg.ndynames; i++)
1929         free (arg.dynames[i]);
1930       free (arg.dynames);
1931     }
1932   ctf_err_warn (fp, 0, 0, _("cannot write archive in link: %s failure"),
1933                 errloc);
1934   return NULL;
1935 }
This page took 0.138684 seconds and 4 git commands to generate.