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