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