]>
Commit | Line | Data |
---|---|---|
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, | |
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 | ||
40 | void | |
139633c3 NA |
41 | ctf_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. */ | |
81 | ctf_id_t | |
139633c3 | 82 | ctf_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. */ | |
143 | const char * | |
139633c3 | 144 | ctf_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. */ | |
160 | typedef 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 | ||
168 | static void | |
169 | ctf_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. */ | |
179 | static int | |
139633c3 NA |
180 | ctf_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 | ||
226 | static int | |
139633c3 | 227 | ctf_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 | ||
273 | int | |
139633c3 | 274 | ctf_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 NA |
282 | static ctf_dict_t * |
283 | ctf_create_per_cu (ctf_dict_t *fp, const char *filename, const char *cuname) | |
eabb7154 | 284 | { |
139633c3 | 285 | ctf_dict_t *cu_fp; |
49ea9b45 | 286 | const char *ctf_name = NULL; |
eabb7154 NA |
287 | char *dynname = NULL; |
288 | ||
49ea9b45 NA |
289 | /* First, check the mapping table and translate the per-CU name we use |
290 | accordingly. We check both the input filename and the CU name. Only if | |
291 | neither are set do we fall back to the input filename as the per-CU | |
292 | dictionary name. We prefer the filename because this is easier for likely | |
293 | callers to determine. */ | |
294 | ||
5f54462c | 295 | if (fp->ctf_link_in_cu_mapping) |
49ea9b45 | 296 | { |
5f54462c NA |
297 | if (((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping, |
298 | filename)) == NULL) && | |
299 | ((ctf_name = ctf_dynhash_lookup (fp->ctf_link_in_cu_mapping, | |
300 | cuname)) == NULL)) | |
49ea9b45 NA |
301 | ctf_name = filename; |
302 | } | |
303 | ||
304 | if (ctf_name == NULL) | |
305 | ctf_name = filename; | |
306 | ||
307 | if ((cu_fp = ctf_dynhash_lookup (fp->ctf_link_outputs, ctf_name)) == NULL) | |
eabb7154 NA |
308 | { |
309 | int err; | |
310 | ||
311 | if ((cu_fp = ctf_create (&err)) == NULL) | |
312 | { | |
926c9e76 NA |
313 | ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive for " |
314 | "CU %s from input file %s"), | |
315 | cuname, filename); | |
eabb7154 NA |
316 | ctf_set_errno (fp, err); |
317 | return NULL; | |
318 | } | |
319 | ||
49ea9b45 | 320 | if ((dynname = strdup (ctf_name)) == NULL) |
eabb7154 NA |
321 | goto oom; |
322 | if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, cu_fp) < 0) | |
323 | goto oom; | |
324 | ||
1fa7a0c2 | 325 | ctf_import_unref (cu_fp, fp); |
eabb7154 NA |
326 | ctf_cuname_set (cu_fp, cuname); |
327 | ctf_parent_name_set (cu_fp, _CTF_SECTION); | |
328 | } | |
329 | return cu_fp; | |
330 | ||
331 | oom: | |
332 | free (dynname); | |
139633c3 | 333 | ctf_dict_close (cu_fp); |
eabb7154 NA |
334 | ctf_set_errno (fp, ENOMEM); |
335 | return NULL; | |
336 | } | |
337 | ||
49ea9b45 | 338 | /* Add a mapping directing that the CU named FROM should have its |
139633c3 | 339 | conflicting/non-duplicate types (depending on link mode) go into a dict |
5f54462c | 340 | named TO. Many FROMs can share a TO. |
49ea9b45 | 341 | |
139633c3 | 342 | We forcibly add a dict named TO in every case, even though it may well |
49ea9b45 | 343 | wind up empty, because clients that use this facility usually expect to find |
139633c3 | 344 | every TO dict present, even if empty, and malfunction otherwise. */ |
49ea9b45 NA |
345 | |
346 | int | |
139633c3 | 347 | ctf_link_add_cu_mapping (ctf_dict_t *fp, const char *from, const char *to) |
49ea9b45 NA |
348 | { |
349 | int err; | |
5f54462c NA |
350 | char *f = NULL, *t = NULL; |
351 | ctf_dynhash_t *one_out; | |
352 | ||
353 | if (fp->ctf_link_in_cu_mapping == NULL) | |
354 | fp->ctf_link_in_cu_mapping = ctf_dynhash_create (ctf_hash_string, | |
355 | ctf_hash_eq_string, free, | |
356 | free); | |
357 | if (fp->ctf_link_in_cu_mapping == NULL) | |
358 | goto oom; | |
49ea9b45 | 359 | |
5f54462c NA |
360 | if (fp->ctf_link_out_cu_mapping == NULL) |
361 | fp->ctf_link_out_cu_mapping = ctf_dynhash_create (ctf_hash_string, | |
362 | ctf_hash_eq_string, free, | |
363 | (ctf_hash_free_fun) | |
364 | ctf_dynhash_destroy); | |
365 | if (fp->ctf_link_out_cu_mapping == NULL) | |
366 | goto oom; | |
49ea9b45 | 367 | |
5f54462c NA |
368 | f = strdup (from); |
369 | t = strdup (to); | |
370 | if (!f || !t) | |
371 | goto oom; | |
49ea9b45 | 372 | |
5f54462c NA |
373 | /* Track both in a list from FROM to TO and in a list from TO to a list of |
374 | FROM. The former is used to create TUs with the mapped-to name at need: | |
375 | the latter is used in deduplicating links to pull in all input CUs | |
376 | corresponding to a single output CU. */ | |
377 | ||
378 | if ((err = ctf_dynhash_insert (fp->ctf_link_in_cu_mapping, f, t)) < 0) | |
379 | { | |
380 | ctf_set_errno (fp, err); | |
381 | goto oom_noerrno; | |
382 | } | |
49ea9b45 | 383 | |
5f54462c | 384 | /* f and t are now owned by the in_cu_mapping: reallocate them. */ |
49ea9b45 NA |
385 | f = strdup (from); |
386 | t = strdup (to); | |
387 | if (!f || !t) | |
388 | goto oom; | |
389 | ||
5f54462c NA |
390 | if ((one_out = ctf_dynhash_lookup (fp->ctf_link_out_cu_mapping, t)) == NULL) |
391 | { | |
392 | if ((one_out = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string, | |
393 | free, NULL)) == NULL) | |
394 | goto oom; | |
395 | if ((err = ctf_dynhash_insert (fp->ctf_link_out_cu_mapping, | |
396 | t, one_out)) < 0) | |
397 | { | |
398 | ctf_dynhash_destroy (one_out); | |
399 | ctf_set_errno (fp, err); | |
400 | goto oom_noerrno; | |
401 | } | |
402 | } | |
403 | else | |
404 | free (t); | |
49ea9b45 | 405 | |
5f54462c | 406 | if (ctf_dynhash_insert (one_out, f, NULL) < 0) |
49ea9b45 NA |
407 | { |
408 | ctf_set_errno (fp, err); | |
409 | goto oom_noerrno; | |
410 | } | |
411 | ||
412 | return 0; | |
413 | ||
414 | oom: | |
415 | ctf_set_errno (fp, errno); | |
416 | oom_noerrno: | |
417 | free (f); | |
418 | free (t); | |
419 | return -1; | |
420 | } | |
421 | ||
422 | /* Set a function which is called to transform the names of archive members. | |
423 | This is useful for applying regular transformations to many names, where | |
424 | ctf_link_add_cu_mapping applies arbitrarily irregular changes to single | |
425 | names. The member name changer is applied at ctf_link_write time, so it | |
426 | cannot conflate multiple CUs into one the way ctf_link_add_cu_mapping can. | |
427 | The changer function accepts a name and should return a new | |
428 | dynamically-allocated name, or NULL if the name should be left unchanged. */ | |
429 | void | |
139633c3 | 430 | ctf_link_set_memb_name_changer (ctf_dict_t *fp, |
49ea9b45 NA |
431 | ctf_link_memb_name_changer_f *changer, |
432 | void *arg) | |
433 | { | |
434 | fp->ctf_link_memb_name_changer = changer; | |
435 | fp->ctf_link_memb_name_changer_arg = arg; | |
436 | } | |
437 | ||
72c83edd NA |
438 | typedef struct ctf_link_in_member_cb_arg |
439 | { | |
8d2229ad | 440 | /* The shared output dictionary. */ |
139633c3 | 441 | ctf_dict_t *out_fp; |
8d2229ad NA |
442 | |
443 | /* The filename of the input file, and an fp to each dictionary in that file | |
444 | in turn. */ | |
445 | const char *in_file_name; | |
139633c3 | 446 | ctf_dict_t *in_fp; |
8d2229ad NA |
447 | |
448 | /* The CU name of the dict being processed. */ | |
72c83edd | 449 | const char *cu_name; |
72c83edd | 450 | int in_input_cu_file; |
8d2229ad NA |
451 | |
452 | /* The parent dictionary in the input, and whether it's been processed yet. | |
453 | Not needed by ctf_link_one_type / ctf_link_one_variable, only by higher | |
454 | layers. */ | |
139633c3 | 455 | ctf_dict_t *in_fp_parent; |
8d2229ad NA |
456 | int done_parent; |
457 | ||
458 | /* If true, this is the CU-mapped portion of a deduplicating link: no child | |
459 | dictionaries should be created. */ | |
460 | int cu_mapped; | |
72c83edd NA |
461 | } ctf_link_in_member_cb_arg_t; |
462 | ||
463 | /* Link one type into the link. We rely on ctf_add_type() to detect | |
464 | duplicates. This is not terribly reliable yet (unnmamed types will be | |
465 | mindlessly duplicated), but will improve shortly. */ | |
466 | ||
467 | static int | |
468 | ctf_link_one_type (ctf_id_t type, int isroot _libctf_unused_, void *arg_) | |
469 | { | |
470 | ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_; | |
139633c3 | 471 | ctf_dict_t *per_cu_out_fp; |
72c83edd NA |
472 | int err; |
473 | ||
8d2229ad | 474 | if (arg->in_fp->ctf_link_flags != CTF_LINK_SHARE_UNCONFLICTED) |
72c83edd | 475 | { |
926c9e76 NA |
476 | ctf_err_warn (arg->out_fp, 0, ECTF_NOTYET, |
477 | _("share-duplicated mode not yet implemented")); | |
72c83edd NA |
478 | return ctf_set_errno (arg->out_fp, ECTF_NOTYET); |
479 | } | |
480 | ||
481 | /* Simply call ctf_add_type: if it reports a conflict and we're adding to the | |
482 | main CTF file, add to the per-CU archive member instead, creating it if | |
483 | necessary. If we got this type from a per-CU archive member, add it | |
484 | straight back to the corresponding member in the output. */ | |
485 | ||
486 | if (!arg->in_input_cu_file) | |
487 | { | |
488 | if (ctf_add_type (arg->out_fp, arg->in_fp, type) != CTF_ERR) | |
489 | return 0; | |
490 | ||
491 | err = ctf_errno (arg->out_fp); | |
492 | if (err != ECTF_CONFLICT) | |
493 | { | |
791915db | 494 | if (err != ECTF_NONREPRESENTABLE) |
926c9e76 NA |
495 | ctf_err_warn (arg->out_fp, 1, 0, |
496 | _("cannot link type %lx from input file %s, CU %s " | |
497 | "into output link"), type, arg->cu_name, | |
498 | arg->in_file_name); | |
791915db NA |
499 | /* We must ignore this problem or we end up losing future types, then |
500 | trying to link the variables in, then exploding. Better to link as | |
8d2229ad | 501 | much as possible. */ |
791915db | 502 | return 0; |
72c83edd NA |
503 | } |
504 | ctf_set_errno (arg->out_fp, 0); | |
505 | } | |
506 | ||
8d2229ad | 507 | if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->in_file_name, |
eabb7154 NA |
508 | arg->cu_name)) == NULL) |
509 | return -1; /* Errno is set for us. */ | |
72c83edd NA |
510 | |
511 | if (ctf_add_type (per_cu_out_fp, arg->in_fp, type) != CTF_ERR) | |
512 | return 0; | |
513 | ||
514 | err = ctf_errno (per_cu_out_fp); | |
791915db | 515 | if (err != ECTF_NONREPRESENTABLE) |
926c9e76 NA |
516 | ctf_err_warn (arg->out_fp, 1, 0, |
517 | _("cannot link type %lx from input file %s, CU %s " | |
518 | "into output per-CU CTF archive member %s: %s: skipped"), | |
519 | type, ctf_link_input_name (arg->in_fp), arg->in_file_name, | |
520 | ctf_link_input_name (per_cu_out_fp), ctf_errmsg (err)); | |
72c83edd NA |
521 | if (err == ECTF_CONFLICT) |
522 | /* Conflicts are possible at this stage only if a non-ld user has combined | |
523 | multiple TUs into a single output dictionary. Even in this case we do not | |
524 | want to stop the link or propagate the error. */ | |
525 | ctf_set_errno (arg->out_fp, 0); | |
526 | ||
527 | return 0; /* As above: do not lose types. */ | |
528 | } | |
529 | ||
6dd2819f NA |
530 | /* Set a function which is used to filter out unwanted variables from the link. */ |
531 | int | |
139633c3 | 532 | ctf_link_set_variable_filter (ctf_dict_t *fp, ctf_link_variable_filter_f *filter, |
6dd2819f NA |
533 | void *arg) |
534 | { | |
535 | fp->ctf_link_variable_filter = filter; | |
536 | fp->ctf_link_variable_filter_arg = arg; | |
537 | return 0; | |
538 | } | |
539 | ||
139633c3 | 540 | /* Check if we can safely add a variable with the given type to this dict. */ |
eabb7154 NA |
541 | |
542 | static int | |
139633c3 | 543 | check_variable (const char *name, ctf_dict_t *fp, ctf_id_t type, |
eabb7154 NA |
544 | ctf_dvdef_t **out_dvd) |
545 | { | |
546 | ctf_dvdef_t *dvd; | |
547 | ||
548 | dvd = ctf_dynhash_lookup (fp->ctf_dvhash, name); | |
549 | *out_dvd = dvd; | |
550 | if (!dvd) | |
551 | return 1; | |
552 | ||
553 | if (dvd->dvd_type != type) | |
554 | { | |
555 | /* Variable here. Wrong type: cannot add. Just skip it, because there is | |
8d2229ad NA |
556 | no way to express this in CTF. Don't even warn: this case is too |
557 | common. (This might be the parent, in which case we'll try adding in | |
558 | the child first, and only then give up.) */ | |
eabb7154 NA |
559 | ctf_dprintf ("Inexpressible duplicate variable %s skipped.\n", name); |
560 | } | |
561 | ||
562 | return 0; /* Already exists. */ | |
563 | } | |
564 | ||
565 | /* Link one variable in. */ | |
566 | ||
567 | static int | |
568 | ctf_link_one_variable (const char *name, ctf_id_t type, void *arg_) | |
569 | { | |
570 | ctf_link_in_member_cb_arg_t *arg = (ctf_link_in_member_cb_arg_t *) arg_; | |
139633c3 | 571 | ctf_dict_t *per_cu_out_fp; |
eabb7154 | 572 | ctf_id_t dst_type = 0; |
1136c379 | 573 | ctf_dict_t *insert_fp; |
eabb7154 NA |
574 | ctf_dvdef_t *dvd; |
575 | ||
6dd2819f NA |
576 | /* See if this variable is filtered out. */ |
577 | ||
578 | if (arg->out_fp->ctf_link_variable_filter) | |
579 | { | |
580 | void *farg = arg->out_fp->ctf_link_variable_filter_arg; | |
581 | if (arg->out_fp->ctf_link_variable_filter (arg->in_fp, name, type, farg)) | |
582 | return 0; | |
583 | } | |
584 | ||
eabb7154 | 585 | /* In unconflicted link mode, if this type is mapped to a type in the parent |
139633c3 | 586 | dict, we want to try to add to that first: if it reports a duplicate, |
eabb7154 NA |
587 | or if the type is in a child already, add straight to the child. */ |
588 | ||
1136c379 | 589 | insert_fp = arg->out_fp; |
eabb7154 | 590 | |
1136c379 | 591 | dst_type = ctf_type_mapping (arg->in_fp, type, &insert_fp); |
eabb7154 NA |
592 | if (dst_type != 0) |
593 | { | |
1136c379 | 594 | if (insert_fp == arg->out_fp) |
eabb7154 | 595 | { |
1136c379 | 596 | if (check_variable (name, insert_fp, dst_type, &dvd)) |
eabb7154 NA |
597 | { |
598 | /* No variable here: we can add it. */ | |
1136c379 NA |
599 | if (ctf_add_variable (insert_fp, name, dst_type) < 0) |
600 | return (ctf_set_errno (arg->out_fp, ctf_errno (insert_fp))); | |
eabb7154 NA |
601 | return 0; |
602 | } | |
603 | ||
604 | /* Already present? Nothing to do. */ | |
19d4b1ad | 605 | if (dvd && dvd->dvd_type == dst_type) |
eabb7154 NA |
606 | return 0; |
607 | } | |
608 | } | |
609 | ||
610 | /* Can't add to the parent due to a name clash, or because it references a | |
611 | type only present in the child. Try adding to the child, creating if need | |
8d2229ad NA |
612 | be. If we can't do that, skip it. Don't add to a child if we're doing a |
613 | CU-mapped link, since that has only one output. */ | |
614 | ||
615 | if (arg->cu_mapped) | |
616 | { | |
617 | ctf_dprintf ("Variable %s in input file %s depends on a type %lx hidden " | |
618 | "due to conflicts: skipped.\n", name, arg->in_file_name, | |
619 | type); | |
620 | return 0; | |
621 | } | |
eabb7154 | 622 | |
8d2229ad | 623 | if ((per_cu_out_fp = ctf_create_per_cu (arg->out_fp, arg->in_file_name, |
eabb7154 NA |
624 | arg->cu_name)) == NULL) |
625 | return -1; /* Errno is set for us. */ | |
626 | ||
1136c379 | 627 | /* If the type was not found, check for it in the child too. */ |
eabb7154 NA |
628 | if (dst_type == 0) |
629 | { | |
1136c379 NA |
630 | insert_fp = per_cu_out_fp; |
631 | dst_type = ctf_type_mapping (arg->in_fp, type, &insert_fp); | |
eabb7154 NA |
632 | |
633 | if (dst_type == 0) | |
634 | { | |
926c9e76 NA |
635 | ctf_err_warn (arg->out_fp, 1, 0, |
636 | _("type %lx for variable %s in input file %s " | |
637 | "not found: skipped"), type, name, | |
8d2229ad | 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 | 655 | static int |
139633c3 | 656 | ctf_link_one_input_archive_member (ctf_dict_t *in_fp, const char *name, void *arg_) |
72c83edd NA |
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. */ |
700 | static void | |
701 | empty_link_type_mapping (void *key _libctf_unused_, void *value, | |
702 | void *arg _libctf_unused_) | |
703 | { | |
139633c3 | 704 | ctf_dict_t *fp = (ctf_dict_t *) value; |
886453cb NA |
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. */ | |
714 | static ssize_t | |
139633c3 | 715 | ctf_link_lazy_open (ctf_dict_t *fp, ctf_link_input_t *input) |
8d2229ad NA |
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 | |
926c9e76 NA |
730 | ctf_err_warn (fp, 0, ECTF_NEEDSBFD, _("cannot open %s lazily"), |
731 | input->clin_filename); | |
8d2229ad NA |
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 | ||
926c9e76 NA |
744 | ctf_err_warn (fp, 0, err, _("opening CTF %s failed"), |
745 | input->clin_filename); | |
8d2229ad NA |
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. */ | |
757 | static void | |
758 | ctf_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. */ |
768 | static void | |
769 | ctf_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; | |
ae41200b NA |
788 | if ((arg->in_fp_parent = ctf_dict_open (input->clin_arc, |
789 | NULL, &err)) == NULL) | |
72c83edd NA |
790 | if (err != ECTF_ARNNAME) |
791 | { | |
926c9e76 NA |
792 | ctf_err_warn (arg->out_fp, 1, 0, |
793 | _("cannot open main archive member in input file %s " | |
794 | "in the link: skipping: %s"), arg->in_file_name, | |
795 | ctf_errmsg (err)); | |
8d2229ad | 796 | goto out; |
72c83edd NA |
797 | } |
798 | ||
8d2229ad | 799 | if (ctf_link_one_input_archive_member (arg->in_fp_parent, |
72c83edd NA |
800 | _CTF_SECTION, arg) < 0) |
801 | { | |
139633c3 | 802 | ctf_dict_close (arg->in_fp_parent); |
8d2229ad | 803 | goto out; |
72c83edd | 804 | } |
8d2229ad NA |
805 | arg->done_parent = 1; |
806 | if (ctf_archive_iter (input->clin_arc, ctf_link_one_input_archive_member, | |
807 | arg) < 0) | |
926c9e76 NA |
808 | ctf_err_warn (arg->out_fp, 0, 0, _("cannot traverse archive in input file " |
809 | "%s: link cannot continue"), | |
810 | arg->in_file_name); | |
72c83edd NA |
811 | else |
812 | { | |
813 | /* The only error indication to the caller is the errno: so ensure that it | |
814 | is zero if there was no actual error from the caller. */ | |
815 | ctf_set_errno (arg->out_fp, 0); | |
816 | } | |
139633c3 | 817 | ctf_dict_close (arg->in_fp_parent); |
886453cb | 818 | |
8d2229ad NA |
819 | out: |
820 | ctf_link_close_one_input_archive (key, value, NULL); | |
72c83edd NA |
821 | } |
822 | ||
662df3c3 NA |
823 | typedef struct link_sort_inputs_cb_arg |
824 | { | |
825 | int is_cu_mapped; | |
139633c3 | 826 | ctf_dict_t *fp; |
662df3c3 NA |
827 | } link_sort_inputs_cb_arg_t; |
828 | ||
829 | /* Sort the inputs by N (the link order). For CU-mapped links, this is a | |
830 | mapping of input to output name, not a mapping of input name to input | |
831 | ctf_link_input_t: compensate accordingly. */ | |
832 | static int | |
833 | ctf_link_sort_inputs (const ctf_next_hkv_t *one, const ctf_next_hkv_t *two, | |
834 | void *arg) | |
835 | { | |
836 | ctf_link_input_t *input_1; | |
837 | ctf_link_input_t *input_2; | |
838 | link_sort_inputs_cb_arg_t *cu_mapped = (link_sort_inputs_cb_arg_t *) arg; | |
839 | ||
840 | if (!cu_mapped || !cu_mapped->is_cu_mapped) | |
841 | { | |
842 | input_1 = (ctf_link_input_t *) one->hkv_value; | |
843 | input_2 = (ctf_link_input_t *) two->hkv_value; | |
844 | } | |
845 | else | |
846 | { | |
847 | const char *name_1 = (const char *) one->hkv_key; | |
848 | const char *name_2 = (const char *) two->hkv_key; | |
849 | ||
850 | input_1 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_1); | |
851 | input_2 = ctf_dynhash_lookup (cu_mapped->fp->ctf_link_inputs, name_2); | |
852 | ||
853 | /* There is no guarantee that CU-mappings actually have corresponding | |
854 | inputs: the relative ordering in that case is unimportant. */ | |
855 | if (!input_1) | |
856 | return -1; | |
857 | if (!input_2) | |
858 | return 1; | |
859 | } | |
860 | ||
861 | if (input_1->n < input_2->n) | |
862 | return -1; | |
863 | else if (input_1->n > input_2->n) | |
864 | return 1; | |
865 | else | |
866 | return 0; | |
867 | } | |
868 | ||
869 | /* Count the number of input dicts in the ctf_link_inputs, or that subset of the | |
870 | ctf_link_inputs given by CU_NAMES if set. Return the number of input dicts, | |
871 | and optionally the name and ctf_link_input_t of the single input archive if | |
872 | only one exists (no matter how many dicts it contains). */ | |
873 | static ssize_t | |
139633c3 | 874 | ctf_link_deduplicating_count_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names, |
662df3c3 NA |
875 | ctf_link_input_t **only_one_input) |
876 | { | |
877 | ctf_dynhash_t *inputs = fp->ctf_link_inputs; | |
878 | ctf_next_t *i = NULL; | |
879 | void *name, *input; | |
880 | ctf_link_input_t *one_input = NULL; | |
881 | const char *one_name = NULL; | |
882 | ssize_t count = 0, narcs = 0; | |
883 | int err; | |
884 | ||
885 | if (cu_names) | |
886 | inputs = cu_names; | |
887 | ||
888 | while ((err = ctf_dynhash_next (inputs, &i, &name, &input)) == 0) | |
889 | { | |
890 | ssize_t one_count; | |
891 | ||
892 | one_name = (const char *) name; | |
893 | /* If we are processing CU names, get the real input. */ | |
894 | if (cu_names) | |
895 | one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name); | |
896 | else | |
897 | one_input = (ctf_link_input_t *) input; | |
898 | ||
899 | if (!one_input) | |
900 | continue; | |
901 | ||
902 | one_count = ctf_link_lazy_open (fp, one_input); | |
903 | ||
904 | if (one_count < 0) | |
905 | { | |
906 | ctf_next_destroy (i); | |
907 | return -1; /* errno is set for us. */ | |
908 | } | |
909 | ||
910 | count += one_count; | |
911 | narcs++; | |
912 | } | |
913 | if (err != ECTF_NEXT_END) | |
914 | { | |
926c9e76 NA |
915 | ctf_err_warn (fp, 0, err, _("iteration error counting deduplicating " |
916 | "CTF link inputs")); | |
662df3c3 NA |
917 | ctf_set_errno (fp, err); |
918 | return -1; | |
919 | } | |
920 | ||
921 | if (!count) | |
922 | return 0; | |
923 | ||
924 | if (narcs == 1) | |
925 | { | |
926 | if (only_one_input) | |
927 | *only_one_input = one_input; | |
928 | } | |
929 | else if (only_one_input) | |
930 | *only_one_input = NULL; | |
931 | ||
932 | return count; | |
933 | } | |
934 | ||
935 | /* Allocate and populate an inputs array big enough for a given set of inputs: | |
936 | either a specific set of CU names (those from that set found in the | |
937 | ctf_link_inputs), or the entire ctf_link_inputs (if cu_names is not set). | |
938 | The number of inputs (from ctf_link_deduplicating_count_inputs, above) is | |
939 | passed in NINPUTS: an array of uint32_t containing parent pointers | |
940 | (corresponding to those members of the inputs that have parents) is allocated | |
941 | and returned in PARENTS. | |
942 | ||
943 | The inputs are *archives*, not files: the archive can have multiple members | |
944 | if it is the result of a previous incremental link. We want to add every one | |
945 | in turn, including the shared parent. (The dedup machinery knows that a type | |
946 | used by a single dictionary and its parent should not be shared in | |
947 | CTF_LINK_SHARE_DUPLICATED mode.) | |
948 | ||
949 | If no inputs exist that correspond to these CUs, return NULL with the errno | |
950 | set to ECTF_NOCTFDATA. */ | |
139633c3 NA |
951 | static ctf_dict_t ** |
952 | ctf_link_deduplicating_open_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names, | |
662df3c3 NA |
953 | ssize_t ninputs, uint32_t **parents) |
954 | { | |
955 | ctf_dynhash_t *inputs = fp->ctf_link_inputs; | |
956 | ctf_next_t *i = NULL; | |
957 | void *name, *input; | |
958 | link_sort_inputs_cb_arg_t sort_arg; | |
139633c3 NA |
959 | ctf_dict_t **dedup_inputs = NULL; |
960 | ctf_dict_t **walk; | |
662df3c3 NA |
961 | uint32_t *parents_ = NULL; |
962 | int err; | |
963 | ||
964 | if (cu_names) | |
965 | inputs = cu_names; | |
966 | ||
139633c3 | 967 | if ((dedup_inputs = calloc (ninputs, sizeof (ctf_dict_t *))) == NULL) |
662df3c3 NA |
968 | goto oom; |
969 | ||
970 | if ((parents_ = calloc (ninputs, sizeof (uint32_t))) == NULL) | |
971 | goto oom; | |
972 | ||
973 | walk = dedup_inputs; | |
974 | ||
975 | /* Counting done: push every input into the array, in the order they were | |
976 | passed to ctf_link_add_ctf (and ultimately ld). */ | |
977 | ||
978 | sort_arg.is_cu_mapped = (cu_names != NULL); | |
979 | sort_arg.fp = fp; | |
980 | ||
981 | while ((err = ctf_dynhash_next_sorted (inputs, &i, &name, &input, | |
982 | ctf_link_sort_inputs, &sort_arg)) == 0) | |
983 | { | |
984 | const char *one_name = (const char *) name; | |
985 | ctf_link_input_t *one_input; | |
139633c3 NA |
986 | ctf_dict_t *one_fp; |
987 | ctf_dict_t *parent_fp = NULL; | |
662df3c3 NA |
988 | uint32_t parent_i; |
989 | ctf_next_t *j = NULL; | |
990 | ||
991 | /* If we are processing CU names, get the real input. All the inputs | |
992 | will have been opened, if they contained any CTF at all. */ | |
993 | if (cu_names) | |
994 | one_input = ctf_dynhash_lookup (fp->ctf_link_inputs, one_name); | |
995 | else | |
996 | one_input = (ctf_link_input_t *) input; | |
997 | ||
998 | if (!one_input || (!one_input->clin_arc && !one_input->clin_fp)) | |
999 | continue; | |
1000 | ||
1001 | /* Short-circuit: if clin_fp is set, just use it. */ | |
1002 | if (one_input->clin_fp) | |
1003 | { | |
1004 | parents_[walk - dedup_inputs] = walk - dedup_inputs; | |
1005 | *walk = one_input->clin_fp; | |
1006 | walk++; | |
1007 | continue; | |
1008 | } | |
1009 | ||
1010 | /* Get and insert the parent archive (if any), if this archive has | |
1011 | multiple members. We assume, as elsewhere, that the parent is named | |
1012 | _CTF_SECTION. */ | |
1013 | ||
ae41200b NA |
1014 | if ((parent_fp = ctf_dict_open (one_input->clin_arc, _CTF_SECTION, |
1015 | &err)) == NULL) | |
662df3c3 NA |
1016 | { |
1017 | if (err != ECTF_NOMEMBNAM) | |
1018 | { | |
1019 | ctf_next_destroy (i); | |
1020 | ctf_set_errno (fp, err); | |
1021 | goto err; | |
1022 | } | |
1023 | } | |
1024 | else | |
1025 | { | |
1026 | *walk = parent_fp; | |
1027 | parent_i = walk - dedup_inputs; | |
1028 | walk++; | |
1029 | } | |
1030 | ||
1031 | /* We disregard the input archive name: either it is the parent (which we | |
1032 | already have), or we want to put everything into one TU sharing the | |
1033 | cuname anyway (if this is a CU-mapped link), or this is the final phase | |
1034 | of a relink with CU-mapping off (i.e. ld -r) in which case the cuname | |
1035 | is correctly set regardless. */ | |
1036 | while ((one_fp = ctf_archive_next (one_input->clin_arc, &j, NULL, | |
1037 | 1, &err)) != NULL) | |
1038 | { | |
1039 | if (one_fp->ctf_flags & LCTF_CHILD) | |
1040 | { | |
1041 | /* The contents of the parents array for elements not | |
1042 | corresponding to children is undefined. If there is no parent | |
1043 | (itself a sign of a likely linker bug or corrupt input), we set | |
1044 | it to itself. */ | |
1045 | ||
1046 | ctf_import (one_fp, parent_fp); | |
1047 | if (parent_fp) | |
1048 | parents_[walk - dedup_inputs] = parent_i; | |
1049 | else | |
1050 | parents_[walk - dedup_inputs] = walk - dedup_inputs; | |
1051 | } | |
1052 | *walk = one_fp; | |
1053 | walk++; | |
1054 | } | |
1055 | if (err != ECTF_NEXT_END) | |
1056 | { | |
1057 | ctf_next_destroy (i); | |
1058 | goto iterr; | |
1059 | } | |
1060 | } | |
1061 | if (err != ECTF_NEXT_END) | |
1062 | goto iterr; | |
1063 | ||
1064 | *parents = parents_; | |
1065 | ||
1066 | return dedup_inputs; | |
1067 | ||
1068 | oom: | |
1069 | err = ENOMEM; | |
1070 | ||
1071 | iterr: | |
1072 | ctf_set_errno (fp, err); | |
1073 | ||
1074 | err: | |
1075 | free (dedup_inputs); | |
1076 | free (parents_); | |
926c9e76 NA |
1077 | ctf_err_warn (fp, 0, 0, _("error in deduplicating CTF link " |
1078 | "input allocation")); | |
662df3c3 NA |
1079 | return NULL; |
1080 | } | |
1081 | ||
1082 | /* Close INPUTS that have already been linked, first the passed array, and then | |
1083 | that subset of the ctf_link_inputs archives they came from cited by the | |
1084 | CU_NAMES. If CU_NAMES is not specified, close all the ctf_link_inputs in one | |
1085 | go, leaving it empty. */ | |
1086 | static int | |
139633c3 NA |
1087 | ctf_link_deduplicating_close_inputs (ctf_dict_t *fp, ctf_dynhash_t *cu_names, |
1088 | ctf_dict_t **inputs, ssize_t ninputs) | |
662df3c3 NA |
1089 | { |
1090 | ctf_next_t *it = NULL; | |
1091 | void *name; | |
1092 | int err; | |
1093 | ssize_t i; | |
1094 | ||
1095 | /* This is the inverse of ctf_link_deduplicating_open_inputs: so first, close | |
1096 | all the individual input dicts, opened by the archive iterator. */ | |
1097 | for (i = 0; i < ninputs; i++) | |
139633c3 | 1098 | ctf_dict_close (inputs[i]); |
662df3c3 NA |
1099 | |
1100 | /* Now close the archives they are part of. */ | |
1101 | if (cu_names) | |
1102 | { | |
1103 | while ((err = ctf_dynhash_next (cu_names, &it, &name, NULL)) == 0) | |
1104 | { | |
1105 | /* Remove the input from the linker inputs, if it exists, which also | |
1106 | closes it. */ | |
1107 | ||
1108 | ctf_dynhash_remove (fp->ctf_link_inputs, (const char *) name); | |
1109 | } | |
1110 | if (err != ECTF_NEXT_END) | |
1111 | { | |
926c9e76 NA |
1112 | ctf_err_warn (fp, 0, err, _("iteration error in deduplicating link " |
1113 | "input freeing")); | |
662df3c3 NA |
1114 | ctf_set_errno (fp, err); |
1115 | } | |
1116 | } | |
1117 | else | |
1118 | ctf_dynhash_empty (fp->ctf_link_inputs); | |
1119 | ||
1120 | return 0; | |
1121 | } | |
1122 | ||
1123 | /* Do a deduplicating link of all variables in the inputs. */ | |
1124 | static int | |
139633c3 | 1125 | ctf_link_deduplicating_variables (ctf_dict_t *fp, ctf_dict_t **inputs, |
662df3c3 NA |
1126 | size_t ninputs, int cu_mapped) |
1127 | { | |
1128 | ctf_link_in_member_cb_arg_t arg; | |
1129 | size_t i; | |
1130 | ||
1131 | arg.cu_mapped = cu_mapped; | |
1132 | arg.out_fp = fp; | |
1133 | arg.in_input_cu_file = 0; | |
1134 | ||
1135 | for (i = 0; i < ninputs; i++) | |
1136 | { | |
1137 | arg.in_fp = inputs[i]; | |
1138 | if (ctf_cuname (inputs[i]) != NULL) | |
1139 | arg.in_file_name = ctf_cuname (inputs[i]); | |
1140 | else | |
1141 | arg.in_file_name = "unnamed-CU"; | |
1142 | arg.cu_name = arg.in_file_name; | |
1143 | if (ctf_variable_iter (arg.in_fp, ctf_link_one_variable, &arg) < 0) | |
1144 | return ctf_set_errno (fp, ctf_errno (arg.in_fp)); | |
1145 | ||
1146 | /* Outputs > 0 are per-CU. */ | |
1147 | arg.in_input_cu_file = 1; | |
1148 | } | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1136c379 NA |
1152 | /* Check for symbol conflicts during linking. Three possibilities: already |
1153 | exists, conflicting, or nonexistent. We don't have a dvd structure we can | |
1154 | use as a flag like check_variable does, so we use a tristate return | |
1155 | value instead: -1: conflicting; 1: nonexistent: 0: already exists. */ | |
1156 | ||
1157 | static int | |
1158 | check_sym (ctf_dict_t *fp, const char *name, ctf_id_t type, int functions) | |
1159 | { | |
1160 | ctf_dynhash_t *thishash = functions ? fp->ctf_funchash : fp->ctf_objthash; | |
1161 | ctf_dynhash_t *thathash = functions ? fp->ctf_objthash : fp->ctf_funchash; | |
1162 | void *value; | |
1163 | ||
1164 | /* Wrong type (function when object is wanted, etc). */ | |
1165 | if (ctf_dynhash_lookup_kv (thathash, name, NULL, NULL)) | |
1166 | return -1; | |
1167 | ||
1168 | /* Not present at all yet. */ | |
1169 | if (!ctf_dynhash_lookup_kv (thishash, name, NULL, &value)) | |
1170 | return 1; | |
1171 | ||
1172 | /* Already present. */ | |
1173 | if ((ctf_id_t) (uintptr_t) value == type) | |
1174 | return 0; | |
1175 | ||
1176 | /* Wrong type. */ | |
1177 | return -1; | |
1178 | } | |
1179 | ||
1180 | /* Do a deduplicating link of one symtypetab (function info or data object) in | |
1181 | one input dict. */ | |
1182 | ||
1183 | static int | |
1184 | ctf_link_deduplicating_one_symtypetab (ctf_dict_t *fp, ctf_dict_t *input, | |
1185 | int cu_mapped, int functions) | |
1186 | { | |
1187 | ctf_next_t *it = NULL; | |
1188 | const char *name; | |
1189 | ctf_id_t type; | |
1190 | const char *in_file_name; | |
1191 | ||
1192 | if (ctf_cuname (input) != NULL) | |
1193 | in_file_name = ctf_cuname (input); | |
1194 | else | |
1195 | in_file_name = "unnamed-CU"; | |
1196 | ||
1197 | while ((type = ctf_symbol_next (input, &it, &name, functions)) != CTF_ERR) | |
1198 | { | |
1199 | ctf_id_t dst_type; | |
1200 | ctf_dict_t *per_cu_out_fp; | |
1201 | ctf_dict_t *insert_fp = fp; | |
1202 | int sym; | |
1203 | ||
1204 | /* Look in the parent first. */ | |
1205 | ||
1206 | dst_type = ctf_type_mapping (input, type, &insert_fp); | |
1207 | if (dst_type != 0) | |
1208 | { | |
1209 | if (insert_fp == fp) | |
1210 | { | |
1211 | sym = check_sym (fp, name, dst_type, functions); | |
1212 | ||
1213 | /* Already present: next symbol. */ | |
1214 | if (sym == 0) | |
1215 | continue; | |
1216 | /* Not present: add it. */ | |
1217 | else if (sym > 0) | |
1218 | { | |
1219 | if (ctf_add_funcobjt_sym (fp, functions, | |
1220 | name, dst_type) < 0) | |
1221 | return -1; /* errno is set for us. */ | |
1222 | continue; | |
1223 | } | |
1224 | } | |
1225 | } | |
1226 | ||
1227 | /* Can't add to the parent due to a name clash (most unlikely), or because | |
1228 | it references a type only present in the child. Try adding to the | |
1229 | child, creating if need be. If we can't do that, skip it. Don't add | |
1230 | to a child if we're doing a CU-mapped link, since that has only one | |
1231 | output. */ | |
1232 | if (cu_mapped) | |
1233 | { | |
1234 | ctf_dprintf ("Symbol %s in input file %s depends on a type %lx " | |
1235 | "hidden due to conflicts: skipped.\n", name, | |
1236 | in_file_name, type); | |
1237 | continue; | |
1238 | } | |
1239 | ||
1240 | if ((per_cu_out_fp = ctf_create_per_cu (fp, in_file_name, | |
1241 | in_file_name)) == NULL) | |
1242 | return -1; /* errno is set for us. */ | |
1243 | ||
1244 | /* If the type was not found, check for it in the child too. */ | |
1245 | if (dst_type == 0) | |
1246 | { | |
1247 | insert_fp = per_cu_out_fp; | |
1248 | dst_type = ctf_type_mapping (input, type, &insert_fp); | |
1249 | ||
1250 | if (dst_type == 0) | |
1251 | { | |
1252 | ctf_err_warn (fp, 1, 0, | |
1253 | _("type %lx for symbol %s in input file %s " | |
1254 | "not found: skipped"), type, name, in_file_name); | |
1255 | continue; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | sym = check_sym (per_cu_out_fp, name, dst_type, functions); | |
1260 | ||
1261 | /* Already present: next symbol. */ | |
1262 | if (sym == 0) | |
1263 | continue; | |
1264 | /* Not present: add it. */ | |
1265 | else if (sym > 0) | |
1266 | { | |
1267 | if (ctf_add_funcobjt_sym (per_cu_out_fp, functions, | |
1268 | name, dst_type) < 0) | |
1269 | return -1; /* errno is set for us. */ | |
1270 | } | |
1271 | else | |
1272 | { | |
1273 | /* Perhaps this should be an assertion failure. */ | |
1274 | ctf_err_warn (fp, 0, ECTF_DUPLICATE, | |
1275 | _("symbol %s in input file %s found conflicting " | |
1276 | "even when trying in per-CU dict."), name, | |
1277 | in_file_name); | |
1278 | return (ctf_set_errno (fp, ECTF_DUPLICATE)); | |
1279 | } | |
1280 | } | |
1281 | if (ctf_errno (input) != ECTF_NEXT_END) | |
1282 | { | |
1283 | ctf_set_errno (fp, ctf_errno (input)); | |
1284 | ctf_err_warn (fp, 0, ctf_errno (input), | |
1285 | functions ? _("iterating over function symbols") : | |
1286 | _("iterating over data symbols")); | |
1287 | return -1; | |
1288 | } | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | /* Do a deduplicating link of the function info and data objects | |
1294 | in the inputs. */ | |
1295 | static int | |
1296 | ctf_link_deduplicating_syms (ctf_dict_t *fp, ctf_dict_t **inputs, | |
1297 | size_t ninputs, int cu_mapped) | |
1298 | { | |
1299 | size_t i; | |
1300 | ||
1301 | for (i = 0; i < ninputs; i++) | |
1302 | { | |
1303 | if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i], | |
1304 | cu_mapped, 0) < 0) | |
1305 | return -1; /* errno is set for us. */ | |
1306 | ||
1307 | if (ctf_link_deduplicating_one_symtypetab (fp, inputs[i], | |
1308 | cu_mapped, 1) < 0) | |
1309 | return -1; /* errno is set for us. */ | |
1310 | } | |
1311 | ||
1312 | return 0; | |
1313 | } | |
1314 | ||
662df3c3 NA |
1315 | /* Do the per-CU part of a deduplicating link. */ |
1316 | static int | |
139633c3 | 1317 | ctf_link_deduplicating_per_cu (ctf_dict_t *fp) |
662df3c3 NA |
1318 | { |
1319 | ctf_next_t *i = NULL; | |
1320 | int err; | |
1321 | void *out_cu; | |
1322 | void *in_cus; | |
1323 | ||
1324 | /* Links with a per-CU mapping in force get a first pass of deduplication, | |
1325 | dedupping the inputs for a given CU mapping into the output for that | |
1326 | mapping. The outputs from this process get fed back into the final pass | |
1327 | that is carried out even for non-CU links. */ | |
1328 | ||
1329 | while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &out_cu, | |
1330 | &in_cus)) == 0) | |
1331 | { | |
1332 | const char *out_name = (const char *) out_cu; | |
1333 | ctf_dynhash_t *in = (ctf_dynhash_t *) in_cus; | |
139633c3 NA |
1334 | ctf_dict_t *out = NULL; |
1335 | ctf_dict_t **inputs; | |
1336 | ctf_dict_t **outputs; | |
662df3c3 NA |
1337 | ctf_archive_t *in_arc; |
1338 | ssize_t ninputs; | |
1339 | ctf_link_input_t *only_input; | |
1340 | uint32_t noutputs; | |
1341 | uint32_t *parents; | |
1342 | ||
1343 | if ((ninputs = ctf_link_deduplicating_count_inputs (fp, in, | |
1344 | &only_input)) == -1) | |
1345 | goto err_open_inputs; | |
1346 | ||
1347 | /* CU mapping with no inputs? Skip. */ | |
1348 | if (ninputs == 0) | |
1349 | continue; | |
1350 | ||
1351 | if (labs ((long int) ninputs) > 0xfffffffe) | |
1352 | { | |
926c9e76 NA |
1353 | ctf_err_warn (fp, 0, EFBIG, _("too many inputs in deduplicating " |
1354 | "link: %li"), (long int) ninputs); | |
1355 | ctf_set_errno (fp, EFBIG); | |
662df3c3 NA |
1356 | goto err_open_inputs; |
1357 | } | |
1358 | ||
1359 | /* Short-circuit: a cu-mapped link with only one input archive with | |
1360 | unconflicting contents is a do-nothing, and we can just leave the input | |
1361 | in place: we do have to change the cuname, though, so we unwrap it, | |
1362 | change the cuname, then stuff it back in the linker input again, via | |
1363 | the clin_fp short-circuit member. ctf_link_deduplicating_open_inputs | |
1364 | will spot this member and jam it straight into the next link phase, | |
1365 | ignoring the corresponding archive. */ | |
1366 | if (only_input && ninputs == 1) | |
1367 | { | |
1368 | ctf_next_t *ai = NULL; | |
1369 | int err; | |
1370 | ||
1371 | /* We can abuse an archive iterator to get the only member cheaply, no | |
1372 | matter what its name. */ | |
1373 | only_input->clin_fp = ctf_archive_next (only_input->clin_arc, | |
1374 | &ai, NULL, 0, &err); | |
1375 | if (!only_input->clin_fp) | |
1376 | { | |
926c9e76 NA |
1377 | ctf_err_warn (fp, 0, err, _("cannot open archive %s in " |
1378 | "CU-mapped CTF link"), | |
1379 | only_input->clin_filename); | |
662df3c3 NA |
1380 | ctf_set_errno (fp, err); |
1381 | goto err_open_inputs; | |
1382 | } | |
1383 | ctf_next_destroy (ai); | |
1384 | ||
1385 | if (strcmp (only_input->clin_filename, out_name) != 0) | |
1386 | { | |
1387 | /* Renaming. We need to add a new input, then null out the | |
1388 | clin_arc and clin_fp of the old one to stop it being | |
1389 | auto-closed on removal. The new input needs its cuname changed | |
1390 | to out_name, which is doable only because the cuname is a | |
1391 | dynamic property which can be changed even in readonly | |
1392 | dicts. */ | |
1393 | ||
1394 | ctf_cuname_set (only_input->clin_fp, out_name); | |
1395 | if (ctf_link_add_ctf_internal (fp, only_input->clin_arc, | |
1396 | only_input->clin_fp, | |
1397 | out_name) < 0) | |
1398 | { | |
926c9e76 NA |
1399 | ctf_err_warn (fp, 0, 0, _("cannot add intermediate files " |
1400 | "to link")); | |
662df3c3 NA |
1401 | goto err_open_inputs; |
1402 | } | |
1403 | only_input->clin_arc = NULL; | |
1404 | only_input->clin_fp = NULL; | |
1405 | ctf_dynhash_remove (fp->ctf_link_inputs, | |
1406 | only_input->clin_filename); | |
1407 | } | |
1408 | continue; | |
1409 | } | |
1410 | ||
1411 | /* This is a real CU many-to-one mapping: we must dedup the inputs into | |
1412 | a new output to be used in the final link phase. */ | |
1413 | ||
1414 | if ((inputs = ctf_link_deduplicating_open_inputs (fp, in, ninputs, | |
1415 | &parents)) == NULL) | |
1416 | { | |
1417 | ctf_next_destroy (i); | |
1418 | goto err_inputs; | |
1419 | } | |
1420 | ||
1421 | if ((out = ctf_create (&err)) == NULL) | |
1422 | { | |
926c9e76 NA |
1423 | ctf_err_warn (fp, 0, err, _("cannot create per-CU CTF archive " |
1424 | "for %s"), | |
1425 | out_name); | |
662df3c3 NA |
1426 | ctf_set_errno (fp, err); |
1427 | goto err_inputs; | |
1428 | } | |
1429 | ||
1430 | /* Share the atoms table to reduce memory usage. */ | |
1431 | out->ctf_dedup_atoms = fp->ctf_dedup_atoms_alloc; | |
1432 | ||
1433 | /* No ctf_imports at this stage: this per-CU dictionary has no parents. | |
1434 | Parent/child deduplication happens in the link's final pass. However, | |
1435 | the cuname *is* important, as it is propagated into the final | |
1436 | dictionary. */ | |
1437 | ctf_cuname_set (out, out_name); | |
1438 | ||
1439 | if (ctf_dedup (out, inputs, ninputs, parents, 1) < 0) | |
1440 | { | |
926c9e76 NA |
1441 | ctf_set_errno (fp, ctf_errno (out)); |
1442 | ctf_err_warn (fp, 0, 0, _("CU-mapped deduplication failed for %s"), | |
1443 | out_name); | |
662df3c3 NA |
1444 | goto err_inputs; |
1445 | } | |
1446 | ||
1447 | if ((outputs = ctf_dedup_emit (out, inputs, ninputs, parents, | |
1448 | &noutputs, 1)) == NULL) | |
1449 | { | |
926c9e76 NA |
1450 | ctf_set_errno (fp, ctf_errno (out)); |
1451 | ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link type emission " | |
1452 | "failed for %s"), out_name); | |
662df3c3 NA |
1453 | goto err_inputs; |
1454 | } | |
1455 | if (!ctf_assert (fp, noutputs == 1)) | |
1456 | goto err_inputs_outputs; | |
1457 | ||
1458 | if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION) | |
1459 | && ctf_link_deduplicating_variables (out, inputs, ninputs, 1) < 0) | |
1460 | { | |
926c9e76 NA |
1461 | ctf_set_errno (fp, ctf_errno (out)); |
1462 | ctf_err_warn (fp, 0, 0, _("CU-mapped deduplicating link variable " | |
1463 | "emission failed for %s"), out_name); | |
662df3c3 NA |
1464 | goto err_inputs_outputs; |
1465 | } | |
1466 | ||
1136c379 NA |
1467 | /* For now, we omit symbol section linking for CU-mapped links, until it |
1468 | is clear how to unify the symbol table across such links. (Perhaps we | |
1469 | should emit an unconditionally indexed symtab, like the compiler | |
1470 | does.) */ | |
1471 | ||
662df3c3 NA |
1472 | if (ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs) < 0) |
1473 | { | |
1474 | free (inputs); | |
1475 | free (parents); | |
1476 | goto err_outputs; | |
1477 | } | |
1478 | free (inputs); | |
1479 | free (parents); | |
1480 | ||
1481 | /* Splice any errors or warnings created during this link back into the | |
1482 | dict that the caller knows about. */ | |
1483 | ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings); | |
1484 | ||
1485 | /* This output now becomes an input to the next link phase, with a name | |
1486 | equal to the CU name. We have to wrap it in an archive wrapper | |
1487 | first. */ | |
1488 | ||
1489 | if ((in_arc = ctf_new_archive_internal (0, 0, NULL, outputs[0], NULL, | |
1490 | NULL, &err)) == NULL) | |
1491 | { | |
1492 | ctf_set_errno (fp, err); | |
1493 | goto err_outputs; | |
1494 | } | |
1495 | ||
1496 | if (ctf_link_add_ctf_internal (fp, in_arc, NULL, | |
1497 | ctf_cuname (outputs[0])) < 0) | |
1498 | { | |
926c9e76 | 1499 | ctf_err_warn (fp, 0, 0, _("cannot add intermediate files to link")); |
662df3c3 NA |
1500 | goto err_outputs; |
1501 | } | |
1502 | ||
139633c3 | 1503 | ctf_dict_close (out); |
662df3c3 NA |
1504 | free (outputs); |
1505 | continue; | |
1506 | ||
1507 | err_inputs_outputs: | |
1508 | ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings); | |
139633c3 | 1509 | ctf_dict_close (outputs[0]); |
662df3c3 NA |
1510 | free (outputs); |
1511 | err_inputs: | |
1512 | ctf_link_deduplicating_close_inputs (fp, in, inputs, ninputs); | |
139633c3 | 1513 | ctf_dict_close (out); |
662df3c3 NA |
1514 | free (inputs); |
1515 | free (parents); | |
1516 | err_open_inputs: | |
1517 | ctf_next_destroy (i); | |
1518 | return -1; | |
1519 | ||
1520 | err_outputs: | |
1521 | ctf_list_splice (&fp->ctf_errs_warnings, &outputs[0]->ctf_errs_warnings); | |
139633c3 | 1522 | ctf_dict_close (outputs[0]); |
662df3c3 NA |
1523 | free (outputs); |
1524 | ctf_next_destroy (i); | |
1525 | return -1; /* Errno is set for us. */ | |
1526 | } | |
1527 | if (err != ECTF_NEXT_END) | |
1528 | { | |
926c9e76 NA |
1529 | ctf_err_warn (fp, 0, err, _("iteration error in CU-mapped deduplicating " |
1530 | "link")); | |
662df3c3 NA |
1531 | return ctf_set_errno (fp, err); |
1532 | } | |
1533 | ||
1534 | return 0; | |
1535 | } | |
1536 | ||
1537 | /* Do a deduplicating link using the ctf-dedup machinery. */ | |
1538 | static void | |
139633c3 | 1539 | ctf_link_deduplicating (ctf_dict_t *fp) |
662df3c3 NA |
1540 | { |
1541 | size_t i; | |
139633c3 | 1542 | ctf_dict_t **inputs, **outputs = NULL; |
662df3c3 NA |
1543 | ssize_t ninputs; |
1544 | uint32_t noutputs; | |
1545 | uint32_t *parents; | |
1546 | ||
1547 | if (ctf_dedup_atoms_init (fp) < 0) | |
1548 | { | |
926c9e76 | 1549 | ctf_err_warn (fp, 0, 0, _("allocating CTF dedup atoms table")); |
662df3c3 NA |
1550 | return; /* Errno is set for us. */ |
1551 | } | |
1552 | ||
1553 | if (fp->ctf_link_out_cu_mapping | |
1554 | && (ctf_link_deduplicating_per_cu (fp) < 0)) | |
1555 | return; /* Errno is set for us. */ | |
1556 | ||
1557 | if ((ninputs = ctf_link_deduplicating_count_inputs (fp, NULL, NULL)) < 0) | |
1558 | return; /* Errno is set for us. */ | |
1559 | ||
1560 | if ((inputs = ctf_link_deduplicating_open_inputs (fp, NULL, ninputs, | |
1561 | &parents)) == NULL) | |
1562 | return; /* Errno is set for us. */ | |
1563 | ||
1564 | if (ninputs == 1 && ctf_cuname (inputs[0]) != NULL) | |
1565 | ctf_cuname_set (fp, ctf_cuname (inputs[0])); | |
1566 | ||
1567 | if (ctf_dedup (fp, inputs, ninputs, parents, 0) < 0) | |
1568 | { | |
926c9e76 NA |
1569 | ctf_err_warn (fp, 0, 0, _("deduplication failed for %s"), |
1570 | ctf_link_input_name (fp)); | |
662df3c3 NA |
1571 | goto err; |
1572 | } | |
1573 | ||
1574 | if ((outputs = ctf_dedup_emit (fp, inputs, ninputs, parents, &noutputs, | |
1575 | 0)) == NULL) | |
1576 | { | |
926c9e76 NA |
1577 | ctf_err_warn (fp, 0, 0, _("deduplicating link type emission failed " |
1578 | "for %s"), ctf_link_input_name (fp)); | |
662df3c3 NA |
1579 | goto err; |
1580 | } | |
1581 | ||
1582 | if (!ctf_assert (fp, outputs[0] == fp)) | |
1583 | goto err; | |
1584 | ||
1585 | for (i = 0; i < noutputs; i++) | |
1586 | { | |
1587 | char *dynname; | |
1588 | ||
1589 | /* We already have access to this one. Close the duplicate. */ | |
1590 | if (i == 0) | |
1591 | { | |
139633c3 | 1592 | ctf_dict_close (outputs[0]); |
662df3c3 NA |
1593 | continue; |
1594 | } | |
1595 | ||
1596 | if ((dynname = strdup (ctf_cuname (outputs[i]))) == NULL) | |
1597 | goto oom_one_output; | |
1598 | ||
1599 | if (ctf_dynhash_insert (fp->ctf_link_outputs, dynname, outputs[i]) < 0) | |
1600 | goto oom_one_output; | |
1601 | ||
1602 | continue; | |
1603 | ||
1604 | oom_one_output: | |
662df3c3 | 1605 | ctf_set_errno (fp, ENOMEM); |
926c9e76 | 1606 | ctf_err_warn (fp, 0, 0, _("out of memory allocating link outputs")); |
662df3c3 NA |
1607 | free (dynname); |
1608 | ||
1609 | for (; i < noutputs; i++) | |
139633c3 | 1610 | ctf_dict_close (outputs[i]); |
662df3c3 NA |
1611 | goto err; |
1612 | } | |
1613 | ||
1614 | if (!(fp->ctf_link_flags & CTF_LINK_OMIT_VARIABLES_SECTION) | |
1615 | && ctf_link_deduplicating_variables (fp, inputs, ninputs, 0) < 0) | |
1616 | { | |
926c9e76 NA |
1617 | ctf_err_warn (fp, 0, 0, _("deduplicating link variable emission failed for " |
1618 | "%s"), ctf_link_input_name (fp)); | |
662df3c3 | 1619 | for (i = 1; i < noutputs; i++) |
139633c3 | 1620 | ctf_dict_close (outputs[i]); |
662df3c3 NA |
1621 | goto err; |
1622 | } | |
1623 | ||
1136c379 NA |
1624 | if (ctf_link_deduplicating_syms (fp, inputs, ninputs, 0) < 0) |
1625 | { | |
1626 | ctf_err_warn (fp, 0, 0, _("deduplicating link symbol emission failed for " | |
1627 | "%s"), ctf_link_input_name (fp)); | |
1628 | for (i = 1; i < noutputs; i++) | |
1629 | ctf_dict_close (outputs[i]); | |
1630 | goto err; | |
1631 | } | |
1632 | ||
662df3c3 NA |
1633 | /* Now close all the inputs, including per-CU intermediates. */ |
1634 | ||
1635 | if (ctf_link_deduplicating_close_inputs (fp, NULL, inputs, ninputs) < 0) | |
1636 | return; /* errno is set for us. */ | |
1637 | ||
1638 | ninputs = 0; /* Prevent double-close. */ | |
1639 | ctf_set_errno (fp, 0); | |
1640 | ||
1641 | /* Fall through. */ | |
1642 | ||
1643 | err: | |
1644 | for (i = 0; i < (size_t) ninputs; i++) | |
139633c3 | 1645 | ctf_dict_close (inputs[i]); |
662df3c3 NA |
1646 | free (inputs); |
1647 | free (parents); | |
1648 | free (outputs); | |
1649 | return; | |
1650 | } | |
1651 | ||
72c83edd | 1652 | /* Merge types and variable sections in all files added to the link |
8d2229ad | 1653 | together. All the added files are closed. */ |
72c83edd | 1654 | int |
139633c3 | 1655 | ctf_link (ctf_dict_t *fp, int flags) |
72c83edd NA |
1656 | { |
1657 | ctf_link_in_member_cb_arg_t arg; | |
5f54462c NA |
1658 | ctf_next_t *i = NULL; |
1659 | int err; | |
72c83edd NA |
1660 | |
1661 | memset (&arg, 0, sizeof (struct ctf_link_in_member_cb_arg)); | |
1662 | arg.out_fp = fp; | |
8d2229ad | 1663 | fp->ctf_link_flags = flags; |
72c83edd NA |
1664 | |
1665 | if (fp->ctf_link_inputs == NULL) | |
1666 | return 0; /* Nothing to do. */ | |
1667 | ||
1668 | if (fp->ctf_link_outputs == NULL) | |
1669 | fp->ctf_link_outputs = ctf_dynhash_create (ctf_hash_string, | |
1670 | ctf_hash_eq_string, free, | |
8d2229ad | 1671 | (ctf_hash_free_fun) |
139633c3 | 1672 | ctf_dict_close); |
72c83edd NA |
1673 | |
1674 | if (fp->ctf_link_outputs == NULL) | |
1675 | return ctf_set_errno (fp, ENOMEM); | |
1676 | ||
5f54462c NA |
1677 | /* Create empty CUs if requested. We do not currently claim that multiple |
1678 | links in succession with CTF_LINK_EMPTY_CU_MAPPINGS set in some calls and | |
1679 | not set in others will do anything especially sensible. */ | |
1680 | ||
1681 | if (fp->ctf_link_out_cu_mapping && (flags & CTF_LINK_EMPTY_CU_MAPPINGS)) | |
1682 | { | |
1683 | void *v; | |
1684 | ||
1685 | while ((err = ctf_dynhash_next (fp->ctf_link_out_cu_mapping, &i, &v, | |
1686 | NULL)) == 0) | |
1687 | { | |
1688 | const char *to = (const char *) v; | |
1689 | if (ctf_create_per_cu (fp, to, to) == NULL) | |
1690 | { | |
1691 | ctf_next_destroy (i); | |
1692 | return -1; /* Errno is set for us. */ | |
1693 | } | |
1694 | } | |
1695 | if (err != ECTF_NEXT_END) | |
1696 | { | |
926c9e76 | 1697 | ctf_err_warn (fp, 1, err, _("iteration error creating empty CUs")); |
5f54462c NA |
1698 | ctf_set_errno (fp, err); |
1699 | return -1; | |
1700 | } | |
1701 | } | |
1702 | ||
662df3c3 NA |
1703 | if ((flags & CTF_LINK_NONDEDUP) || (getenv ("LD_NO_CTF_DEDUP"))) |
1704 | ctf_dynhash_iter (fp->ctf_link_inputs, ctf_link_one_input_archive, | |
1705 | &arg); | |
1706 | else | |
1707 | ctf_link_deduplicating (fp); | |
72c83edd | 1708 | |
8d2229ad NA |
1709 | /* Discard the now-unnecessary mapping table data from all the outputs. */ |
1710 | if (fp->ctf_link_type_mapping) | |
1711 | ctf_dynhash_empty (fp->ctf_link_type_mapping); | |
1712 | ctf_dynhash_iter (fp->ctf_link_outputs, empty_link_type_mapping, NULL); | |
1713 | ||
1714 | if ((ctf_errno (fp) != 0) && (ctf_errno (fp) != ECTF_NOCTFDATA)) | |
72c83edd NA |
1715 | return -1; |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | typedef struct ctf_link_out_string_cb_arg | |
1720 | { | |
1721 | const char *str; | |
1722 | uint32_t offset; | |
1723 | int err; | |
1724 | } ctf_link_out_string_cb_arg_t; | |
1725 | ||
1726 | /* Intern a string in the string table of an output per-CU CTF file. */ | |
1727 | static void | |
1728 | ctf_link_intern_extern_string (void *key _libctf_unused_, void *value, | |
1729 | void *arg_) | |
1730 | { | |
139633c3 | 1731 | ctf_dict_t *fp = (ctf_dict_t *) value; |
72c83edd NA |
1732 | ctf_link_out_string_cb_arg_t *arg = (ctf_link_out_string_cb_arg_t *) arg_; |
1733 | ||
1734 | fp->ctf_flags |= LCTF_DIRTY; | |
676c3ecb | 1735 | if (!ctf_str_add_external (fp, arg->str, arg->offset)) |
72c83edd NA |
1736 | arg->err = ENOMEM; |
1737 | } | |
1738 | ||
1739 | /* Repeatedly call ADD_STRING to acquire strings from the external string table, | |
1740 | adding them to the atoms table for this CU and all subsidiary CUs. | |
1741 | ||
1742 | If ctf_link() is also called, it must be called first if you want the new CTF | |
1743 | files ctf_link() can create to get their strings dedupped against the ELF | |
1744 | strtab properly. */ | |
1745 | int | |
139633c3 | 1746 | ctf_link_add_strtab (ctf_dict_t *fp, ctf_link_strtab_string_f *add_string, |
72c83edd NA |
1747 | void *arg) |
1748 | { | |
1749 | const char *str; | |
1750 | uint32_t offset; | |
1751 | int err = 0; | |
1752 | ||
1753 | while ((str = add_string (&offset, arg)) != NULL) | |
1754 | { | |
1755 | ctf_link_out_string_cb_arg_t iter_arg = { str, offset, 0 }; | |
1756 | ||
1757 | fp->ctf_flags |= LCTF_DIRTY; | |
676c3ecb | 1758 | if (!ctf_str_add_external (fp, str, offset)) |
72c83edd NA |
1759 | err = ENOMEM; |
1760 | ||
1761 | ctf_dynhash_iter (fp->ctf_link_outputs, ctf_link_intern_extern_string, | |
1762 | &iter_arg); | |
1763 | if (iter_arg.err) | |
1764 | err = iter_arg.err; | |
1765 | } | |
1766 | ||
1136c379 NA |
1767 | if (err) |
1768 | ctf_set_errno (fp, err); | |
1769 | ||
72c83edd NA |
1770 | return -err; |
1771 | } | |
1772 | ||
1136c379 NA |
1773 | /* Inform the ctf-link machinery of a new symbol in the target symbol table |
1774 | (which must be some symtab that is not usually stripped, and which | |
1775 | is in agreement with ctf_bfdopen_ctfsect). May be called either before or | |
1776 | after ctf_link_add_strtab. */ | |
72c83edd | 1777 | int |
3d16b64e NA |
1778 | ctf_link_add_linker_symbol (ctf_dict_t *fp, ctf_link_sym_t *sym) |
1779 | { | |
1136c379 NA |
1780 | ctf_in_flight_dynsym_t *cid; |
1781 | ||
1782 | /* Cheat a little: if there is already an ENOMEM error code recorded against | |
1783 | this dict, we shouldn't even try to add symbols because there will be no | |
1784 | memory to do so: probably we failed to add some previous symbol. This | |
1785 | makes out-of-memory exits 'sticky' across calls to this function, so the | |
1786 | caller doesn't need to worry about error conditions. */ | |
1787 | ||
1788 | if (ctf_errno (fp) == ENOMEM) | |
1789 | return -ENOMEM; /* errno is set for us. */ | |
1790 | ||
1791 | if (ctf_symtab_skippable (sym)) | |
1792 | return 0; | |
1793 | ||
1794 | if (sym->st_type != STT_OBJECT && sym->st_type != STT_FUNC) | |
1795 | return 0; | |
1796 | ||
1797 | /* Add the symbol to the in-flight list. */ | |
1798 | ||
1799 | if ((cid = malloc (sizeof (ctf_in_flight_dynsym_t))) == NULL) | |
1800 | goto oom; | |
1801 | ||
1802 | cid->cid_sym = *sym; | |
1803 | ctf_list_append (&fp->ctf_in_flight_dynsyms, cid); | |
1804 | ||
3d16b64e | 1805 | return 0; |
1136c379 NA |
1806 | |
1807 | oom: | |
1808 | ctf_dynhash_destroy (fp->ctf_dynsyms); | |
1809 | fp->ctf_dynsyms = NULL; | |
1810 | ctf_set_errno (fp, ENOMEM); | |
1811 | return -ENOMEM; | |
3d16b64e | 1812 | } |
1136c379 NA |
1813 | |
1814 | /* Impose an ordering on symbols. The ordering takes effect immediately, but | |
1815 | since the ordering info does not include type IDs, lookups may return nothing | |
1816 | until such IDs are added by calls to ctf_add_*_sym. Must be called after | |
1817 | ctf_link_add_strtab and ctf_link_add_linker_symbol. */ | |
3d16b64e NA |
1818 | int |
1819 | ctf_link_shuffle_syms (ctf_dict_t *fp) | |
72c83edd | 1820 | { |
1136c379 NA |
1821 | ctf_in_flight_dynsym_t *did, *nid; |
1822 | ctf_next_t *i = NULL; | |
1823 | int err = ENOMEM; | |
1824 | void *name_, *sym_; | |
1825 | ||
1826 | if (!fp->ctf_dynsyms) | |
1827 | { | |
1828 | fp->ctf_dynsyms = ctf_dynhash_create (ctf_hash_string, | |
1829 | ctf_hash_eq_string, | |
1830 | NULL, free); | |
1831 | if (!fp->ctf_dynsyms) | |
1832 | { | |
1833 | ctf_set_errno (fp, ENOMEM); | |
1834 | return -ENOMEM; | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | /* Add all the symbols, excluding only those we already know are prohibited | |
1839 | from appearing in symtypetabs. */ | |
1840 | ||
1841 | for (did = ctf_list_next (&fp->ctf_in_flight_dynsyms); did != NULL; did = nid) | |
1842 | { | |
1843 | ctf_link_sym_t *new_sym; | |
1844 | ||
1845 | nid = ctf_list_next (did); | |
1846 | ctf_list_delete (&fp->ctf_in_flight_dynsyms, did); | |
1847 | ||
1848 | /* We might get a name or an external strtab offset. The strtab offset is | |
1849 | guaranteed resolvable at this point, so turn it into a string. */ | |
1850 | ||
1851 | if (did->cid_sym.st_name == NULL) | |
1852 | { | |
1853 | uint32_t off = CTF_SET_STID (did->cid_sym.st_nameidx, CTF_STRTAB_1); | |
1854 | ||
1855 | did->cid_sym.st_name = ctf_strraw (fp, off); | |
1856 | did->cid_sym.st_nameidx_set = 0; | |
1857 | if (!ctf_assert (fp, did->cid_sym.st_name != NULL)) | |
1858 | return -ECTF_INTERNAL; /* errno is set for us. */ | |
1859 | } | |
1860 | ||
1861 | /* The symbol might have turned out to be nameless, so we have to recheck | |
1862 | for skippability here. */ | |
1863 | if (!ctf_symtab_skippable (&did->cid_sym)) | |
1864 | { | |
1865 | ctf_dprintf ("symbol name from linker: %s\n", did->cid_sym.st_name); | |
1866 | ||
1867 | if ((new_sym = malloc (sizeof (ctf_link_sym_t))) == NULL) | |
1868 | goto local_oom; | |
1869 | ||
1870 | memcpy (new_sym, &did->cid_sym, sizeof (ctf_link_sym_t)); | |
1871 | if (ctf_dynhash_cinsert (fp->ctf_dynsyms, new_sym->st_name, new_sym) < 0) | |
1872 | goto local_oom; | |
1873 | ||
1874 | if (fp->ctf_dynsymmax < new_sym->st_symidx) | |
1875 | fp->ctf_dynsymmax = new_sym->st_symidx; | |
1876 | } | |
1877 | ||
1878 | free (did); | |
1879 | continue; | |
1880 | ||
1881 | local_oom: | |
1882 | free (did); | |
1883 | free (new_sym); | |
1884 | goto err; | |
1885 | } | |
1886 | ||
1887 | /* Construct a mapping from shndx to the symbol info. */ | |
1888 | free (fp->ctf_dynsymidx); | |
1889 | if ((fp->ctf_dynsymidx = calloc (fp->ctf_dynsymmax + 1, | |
1890 | sizeof (ctf_link_sym_t *))) == NULL) | |
1891 | goto err; | |
1892 | ||
1893 | while ((err = ctf_dynhash_next (fp->ctf_dynsyms, &i, &name_, &sym_)) == 0) | |
1894 | { | |
1895 | const char *name = (const char *) name; | |
1896 | ctf_link_sym_t *symp = (ctf_link_sym_t *) sym_; | |
1897 | ||
1898 | if (!ctf_assert (fp, symp->st_symidx <= fp->ctf_dynsymmax)) | |
1899 | { | |
1900 | ctf_next_destroy (i); | |
1901 | err = ctf_errno (fp); | |
1902 | goto err; | |
1903 | } | |
1904 | fp->ctf_dynsymidx[symp->st_symidx] = symp; | |
1905 | } | |
1906 | if (err != ECTF_NEXT_END) | |
1907 | { | |
1908 | ctf_err_warn (fp, 0, err, _("error iterating over shuffled symbols")); | |
1909 | goto err; | |
1910 | } | |
72c83edd | 1911 | return 0; |
1136c379 NA |
1912 | |
1913 | err: | |
1914 | /* Leave the in-flight symbols around: they'll be freed at | |
1915 | dict close time regardless. */ | |
1916 | ctf_dynhash_destroy (fp->ctf_dynsyms); | |
1917 | fp->ctf_dynsyms = NULL; | |
1918 | free (fp->ctf_dynsymidx); | |
1919 | fp->ctf_dynsymidx = NULL; | |
1920 | fp->ctf_dynsymmax = 0; | |
1921 | ctf_set_errno (fp, err); | |
1922 | return -err; | |
72c83edd NA |
1923 | } |
1924 | ||
1925 | typedef struct ctf_name_list_accum_cb_arg | |
1926 | { | |
1927 | char **names; | |
139633c3 NA |
1928 | ctf_dict_t *fp; |
1929 | ctf_dict_t **files; | |
72c83edd | 1930 | size_t i; |
49ea9b45 NA |
1931 | char **dynames; |
1932 | size_t ndynames; | |
72c83edd NA |
1933 | } ctf_name_list_accum_cb_arg_t; |
1934 | ||
676c3ecb | 1935 | /* Accumulate the names and a count of the names in the link output hash. */ |
72c83edd NA |
1936 | static void |
1937 | ctf_accumulate_archive_names (void *key, void *value, void *arg_) | |
1938 | { | |
1939 | const char *name = (const char *) key; | |
139633c3 | 1940 | ctf_dict_t *fp = (ctf_dict_t *) value; |
72c83edd | 1941 | char **names; |
139633c3 | 1942 | ctf_dict_t **files; |
72c83edd | 1943 | ctf_name_list_accum_cb_arg_t *arg = (ctf_name_list_accum_cb_arg_t *) arg_; |
72c83edd NA |
1944 | |
1945 | if ((names = realloc (arg->names, sizeof (char *) * ++(arg->i))) == NULL) | |
1946 | { | |
1947 | (arg->i)--; | |
1948 | ctf_set_errno (arg->fp, ENOMEM); | |
1949 | return; | |
1950 | } | |
1951 | ||
139633c3 | 1952 | if ((files = realloc (arg->files, sizeof (ctf_dict_t *) * arg->i)) == NULL) |
72c83edd NA |
1953 | { |
1954 | (arg->i)--; | |
1955 | ctf_set_errno (arg->fp, ENOMEM); | |
1956 | return; | |
1957 | } | |
49ea9b45 NA |
1958 | |
1959 | /* Allow the caller to get in and modify the name at the last minute. If the | |
1960 | caller *does* modify the name, we have to stash away the new name the | |
1961 | caller returned so we can free it later on. (The original name is the key | |
1962 | of the ctf_link_outputs hash and is freed by the dynhash machinery.) */ | |
1963 | ||
1964 | if (fp->ctf_link_memb_name_changer) | |
1965 | { | |
1966 | char **dynames; | |
1967 | char *dyname; | |
1968 | void *nc_arg = fp->ctf_link_memb_name_changer_arg; | |
1969 | ||
1970 | dyname = fp->ctf_link_memb_name_changer (fp, name, nc_arg); | |
1971 | ||
1972 | if (dyname != NULL) | |
1973 | { | |
1974 | if ((dynames = realloc (arg->dynames, | |
1975 | sizeof (char *) * ++(arg->ndynames))) == NULL) | |
1976 | { | |
1977 | (arg->ndynames)--; | |
1978 | ctf_set_errno (arg->fp, ENOMEM); | |
1979 | return; | |
1980 | } | |
1981 | arg->dynames = dynames; | |
1982 | name = (const char *) dyname; | |
1983 | } | |
1984 | } | |
1985 | ||
72c83edd NA |
1986 | arg->names = names; |
1987 | arg->names[(arg->i) - 1] = (char *) name; | |
1988 | arg->files = files; | |
1989 | arg->files[(arg->i) - 1] = fp; | |
1990 | } | |
1991 | ||
49ea9b45 NA |
1992 | /* Change the name of the parent CTF section, if the name transformer has got to |
1993 | it. */ | |
1994 | static void | |
1995 | ctf_change_parent_name (void *key _libctf_unused_, void *value, void *arg) | |
1996 | { | |
139633c3 | 1997 | ctf_dict_t *fp = (ctf_dict_t *) value; |
49ea9b45 NA |
1998 | const char *name = (const char *) arg; |
1999 | ||
2000 | ctf_parent_name_set (fp, name); | |
2001 | } | |
2002 | ||
72c83edd NA |
2003 | /* Write out a CTF archive (if there are per-CU CTF files) or a CTF file |
2004 | (otherwise) into a new dynamically-allocated string, and return it. | |
2005 | Members with sizes above THRESHOLD are compressed. */ | |
2006 | unsigned char * | |
139633c3 | 2007 | ctf_link_write (ctf_dict_t *fp, size_t *size, size_t threshold) |
72c83edd NA |
2008 | { |
2009 | ctf_name_list_accum_cb_arg_t arg; | |
2010 | char **names; | |
49ea9b45 | 2011 | char *transformed_name = NULL; |
139633c3 | 2012 | ctf_dict_t **files; |
72c83edd NA |
2013 | FILE *f = NULL; |
2014 | int err; | |
2015 | long fsize; | |
2016 | const char *errloc; | |
2017 | unsigned char *buf = NULL; | |
2018 | ||
2019 | memset (&arg, 0, sizeof (ctf_name_list_accum_cb_arg_t)); | |
2020 | arg.fp = fp; | |
2021 | ||
72c83edd NA |
2022 | if (fp->ctf_link_outputs) |
2023 | { | |
2024 | ctf_dynhash_iter (fp->ctf_link_outputs, ctf_accumulate_archive_names, &arg); | |
2025 | if (ctf_errno (fp) < 0) | |
2026 | { | |
2027 | errloc = "hash creation"; | |
2028 | goto err; | |
2029 | } | |
2030 | } | |
2031 | ||
139633c3 | 2032 | /* No extra outputs? Just write a simple ctf_dict_t. */ |
72c83edd NA |
2033 | if (arg.i == 0) |
2034 | return ctf_write_mem (fp, size, threshold); | |
2035 | ||
2036 | /* Writing an archive. Stick ourselves (the shared repository, parent of all | |
2037 | other archives) on the front of it with the default name. */ | |
2038 | if ((names = realloc (arg.names, sizeof (char *) * (arg.i + 1))) == NULL) | |
2039 | { | |
2040 | errloc = "name reallocation"; | |
2041 | goto err_no; | |
2042 | } | |
2043 | arg.names = names; | |
2044 | memmove (&(arg.names[1]), arg.names, sizeof (char *) * (arg.i)); | |
49ea9b45 | 2045 | |
72c83edd | 2046 | arg.names[0] = (char *) _CTF_SECTION; |
49ea9b45 NA |
2047 | if (fp->ctf_link_memb_name_changer) |
2048 | { | |
2049 | void *nc_arg = fp->ctf_link_memb_name_changer_arg; | |
2050 | ||
2051 | transformed_name = fp->ctf_link_memb_name_changer (fp, _CTF_SECTION, | |
2052 | nc_arg); | |
2053 | ||
2054 | if (transformed_name != NULL) | |
2055 | { | |
2056 | arg.names[0] = transformed_name; | |
2057 | ctf_dynhash_iter (fp->ctf_link_outputs, ctf_change_parent_name, | |
2058 | transformed_name); | |
2059 | } | |
2060 | } | |
72c83edd NA |
2061 | |
2062 | if ((files = realloc (arg.files, | |
139633c3 | 2063 | sizeof (struct ctf_dict *) * (arg.i + 1))) == NULL) |
72c83edd | 2064 | { |
139633c3 | 2065 | errloc = "ctf_dict reallocation"; |
72c83edd NA |
2066 | goto err_no; |
2067 | } | |
2068 | arg.files = files; | |
139633c3 | 2069 | memmove (&(arg.files[1]), arg.files, sizeof (ctf_dict_t *) * (arg.i)); |
72c83edd NA |
2070 | arg.files[0] = fp; |
2071 | ||
2072 | if ((f = tmpfile ()) == NULL) | |
2073 | { | |
2074 | errloc = "tempfile creation"; | |
2075 | goto err_no; | |
2076 | } | |
2077 | ||
2078 | if ((err = ctf_arc_write_fd (fileno (f), arg.files, arg.i + 1, | |
2079 | (const char **) arg.names, | |
2080 | threshold)) < 0) | |
2081 | { | |
2082 | errloc = "archive writing"; | |
2083 | ctf_set_errno (fp, err); | |
2084 | goto err; | |
2085 | } | |
2086 | ||
2087 | if (fseek (f, 0, SEEK_END) < 0) | |
2088 | { | |
2089 | errloc = "seeking to end"; | |
2090 | goto err_no; | |
2091 | } | |
2092 | ||
2093 | if ((fsize = ftell (f)) < 0) | |
2094 | { | |
2095 | errloc = "filesize determination"; | |
2096 | goto err_no; | |
2097 | } | |
2098 | ||
2099 | if (fseek (f, 0, SEEK_SET) < 0) | |
2100 | { | |
2101 | errloc = "filepos resetting"; | |
2102 | goto err_no; | |
2103 | } | |
2104 | ||
2105 | if ((buf = malloc (fsize)) == NULL) | |
2106 | { | |
2107 | errloc = "CTF archive buffer allocation"; | |
2108 | goto err_no; | |
2109 | } | |
2110 | ||
2111 | while (!feof (f) && fread (buf, fsize, 1, f) == 0) | |
2112 | if (ferror (f)) | |
2113 | { | |
2114 | errloc = "reading archive from temporary file"; | |
2115 | goto err_no; | |
2116 | } | |
2117 | ||
2118 | *size = fsize; | |
2119 | free (arg.names); | |
2120 | free (arg.files); | |
49ea9b45 NA |
2121 | free (transformed_name); |
2122 | if (arg.ndynames) | |
2123 | { | |
2124 | size_t i; | |
2125 | for (i = 0; i < arg.ndynames; i++) | |
2126 | free (arg.dynames[i]); | |
2127 | free (arg.dynames); | |
2128 | } | |
e3f17159 | 2129 | fclose (f); |
72c83edd NA |
2130 | return buf; |
2131 | ||
2132 | err_no: | |
2133 | ctf_set_errno (fp, errno); | |
2134 | err: | |
2135 | free (buf); | |
2136 | if (f) | |
2137 | fclose (f); | |
2138 | free (arg.names); | |
2139 | free (arg.files); | |
49ea9b45 NA |
2140 | free (transformed_name); |
2141 | if (arg.ndynames) | |
2142 | { | |
2143 | size_t i; | |
2144 | for (i = 0; i < arg.ndynames; i++) | |
2145 | free (arg.dynames[i]); | |
2146 | free (arg.dynames); | |
2147 | } | |
926c9e76 NA |
2148 | ctf_err_warn (fp, 0, 0, _("cannot write archive in link: %s failure"), |
2149 | errloc); | |
72c83edd NA |
2150 | return NULL; |
2151 | } |