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