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