Mercurial > hg > CbC > CbC_gcc
comparison lto-plugin/lto-plugin.c @ 63:b7f97abdc517 gcc-4.6-20100522
update gcc from gcc-4.5.0 to gcc-4.6
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 24 May 2010 12:47:05 +0900 |
parents | 77e2b8dfacca |
children | f6334be47118 |
comparison
equal
deleted
inserted
replaced
56:3c8a44c06a95 | 63:b7f97abdc517 |
---|---|
138 LDPV_PROTECTED, | 138 LDPV_PROTECTED, |
139 LDPV_INTERNAL, | 139 LDPV_INTERNAL, |
140 LDPV_HIDDEN | 140 LDPV_HIDDEN |
141 }; | 141 }; |
142 | 142 |
143 entry->name = strdup (p); | 143 entry->name = xstrdup (p); |
144 while (*p) | 144 while (*p) |
145 p++; | 145 p++; |
146 p++; | 146 p++; |
147 | 147 |
148 entry->version = NULL; | 148 entry->version = NULL; |
153 p++; | 153 p++; |
154 | 154 |
155 if (strlen (entry->comdat_key) == 0) | 155 if (strlen (entry->comdat_key) == 0) |
156 entry->comdat_key = NULL; | 156 entry->comdat_key = NULL; |
157 else | 157 else |
158 entry->comdat_key = strdup (entry->comdat_key); | 158 entry->comdat_key = xstrdup (entry->comdat_key); |
159 | 159 |
160 t = *p; | 160 t = *p; |
161 check (t <= 4, LDPL_FATAL, "invalid symbol kind found"); | 161 check (t <= 4, LDPL_FATAL, "invalid symbol kind found"); |
162 entry->def = translate_kind[t]; | 162 entry->def = translate_kind[t]; |
163 p++; | 163 p++; |
231 int n = 0; | 231 int n = 0; |
232 | 232 |
233 while (data < end) | 233 while (data < end) |
234 { | 234 { |
235 n++; | 235 n++; |
236 syms = realloc (syms, n * sizeof (struct ld_plugin_symbol)); | 236 syms = xrealloc (syms, n * sizeof (struct ld_plugin_symbol)); |
237 check (syms, LDPL_FATAL, "could not allocate memory"); | 237 check (syms, LDPL_FATAL, "could not allocate memory"); |
238 slots = realloc (slots, n * sizeof (uint32_t)); | 238 slots = xrealloc (slots, n * sizeof (uint32_t)); |
239 check (slots, LDPL_FATAL, "could not allocate memory"); | 239 check (slots, LDPL_FATAL, "could not allocate memory"); |
240 data = parse_table_entry (data, &syms[n - 1], &slots[n - 1]); | 240 data = parse_table_entry (data, &syms[n - 1], &slots[n - 1]); |
241 } | 241 } |
242 | 242 |
243 out->nsyms = n; | 243 out->nsyms = n; |
292 num_claimed_files = 0; | 292 num_claimed_files = 0; |
293 | 293 |
294 if (arguments_file_name) | 294 if (arguments_file_name) |
295 free (arguments_file_name); | 295 free (arguments_file_name); |
296 arguments_file_name = NULL; | 296 arguments_file_name = NULL; |
297 | |
298 if (resolution_file) | |
299 { | |
300 free (resolution_file); | |
301 resolution_file = NULL; | |
302 } | |
303 } | 297 } |
304 | 298 |
305 /* Writes the relocations to disk. */ | 299 /* Writes the relocations to disk. */ |
306 | 300 |
307 static void | 301 static void |
308 write_resolution (void) | 302 write_resolution (void) |
309 { | 303 { |
310 unsigned int i; | 304 unsigned int i; |
311 FILE *f; | 305 FILE *f; |
312 | 306 |
307 check (resolution_file, LDPL_FATAL, "resolution file not specified"); | |
313 f = fopen (resolution_file, "w"); | 308 f = fopen (resolution_file, "w"); |
314 check (f, LDPL_FATAL, "could not open file"); | 309 check (f, LDPL_FATAL, "could not open file"); |
315 | 310 |
316 fprintf (f, "%d\n", num_claimed_files); | 311 fprintf (f, "%d\n", num_claimed_files); |
317 | 312 |
320 struct plugin_file_info *info = &claimed_files[i]; | 315 struct plugin_file_info *info = &claimed_files[i]; |
321 struct plugin_symtab *symtab = &info->symtab; | 316 struct plugin_symtab *symtab = &info->symtab; |
322 struct ld_plugin_symbol *syms = symtab->syms; | 317 struct ld_plugin_symbol *syms = symtab->syms; |
323 unsigned j; | 318 unsigned j; |
324 | 319 |
325 assert (syms); | |
326 get_symbols (info->handle, symtab->nsyms, syms); | 320 get_symbols (info->handle, symtab->nsyms, syms); |
327 | 321 |
328 fprintf (f, "%s %d\n", info->name, info->symtab.nsyms); | 322 fprintf (f, "%s %d\n", info->name, info->symtab.nsyms); |
329 | 323 |
330 for (j = 0; j < info->symtab.nsyms; j++) | 324 for (j = 0; j < info->symtab.nsyms; j++) |
341 stdout. */ | 335 stdout. */ |
342 | 336 |
343 static void | 337 static void |
344 add_output_files (FILE *f) | 338 add_output_files (FILE *f) |
345 { | 339 { |
346 char fname[1000]; /* FIXME: Remove this restriction. */ | |
347 | |
348 for (;;) | 340 for (;;) |
349 { | 341 { |
342 const unsigned piece = 32; | |
343 char *buf, *s = xmalloc (piece); | |
350 size_t len; | 344 size_t len; |
351 char *s = fgets (fname, sizeof (fname), f); | 345 |
352 if (!s) | 346 buf = s; |
347 cont: | |
348 if (!fgets (buf, piece, f)) | |
353 break; | 349 break; |
354 | |
355 len = strlen (s); | 350 len = strlen (s); |
356 check (s[len - 1] == '\n', LDPL_FATAL, "file name too long"); | 351 if (s[len - 1] != '\n') |
352 { | |
353 s = xrealloc (s, len + piece); | |
354 buf = s + len; | |
355 goto cont; | |
356 } | |
357 s[len - 1] = '\0'; | 357 s[len - 1] = '\0'; |
358 | 358 |
359 num_output_files++; | 359 num_output_files++; |
360 output_files = realloc (output_files, num_output_files * sizeof (char *)); | 360 output_files |
361 output_files[num_output_files - 1] = strdup (s); | 361 = xrealloc (output_files, num_output_files * sizeof (char *)); |
362 output_files[num_output_files - 1] = s; | |
362 add_input_file (output_files[num_output_files - 1]); | 363 add_input_file (output_files[num_output_files - 1]); |
363 } | 364 } |
364 } | 365 } |
365 | 366 |
366 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the | 367 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the |
367 argument list. */ | 368 argument list. */ |
368 | 369 |
369 static void | 370 static void |
370 exec_lto_wrapper (char *argv[]) | 371 exec_lto_wrapper (char *argv[]) |
371 { | 372 { |
372 int t; | 373 int t, i; |
373 int status; | 374 int status; |
374 char *at_args; | 375 char *at_args; |
375 FILE *args; | 376 FILE *args; |
376 FILE *wrapper_output; | 377 FILE *wrapper_output; |
377 char *new_argv[3]; | 378 char *new_argv[3]; |
392 check (t == 0, LDPL_FATAL, "could not close arguments file"); | 393 check (t == 0, LDPL_FATAL, "could not close arguments file"); |
393 | 394 |
394 at_args = concat ("@", arguments_file_name, NULL); | 395 at_args = concat ("@", arguments_file_name, NULL); |
395 check (at_args, LDPL_FATAL, "could not allocate"); | 396 check (at_args, LDPL_FATAL, "could not allocate"); |
396 | 397 |
398 for (i = 1; argv[i]; i++) | |
399 { | |
400 char *a = argv[i]; | |
401 if (a[0] == '-' && a[1] == 'v' && a[2] == '\0') | |
402 { | |
403 for (i = 0; argv[i]; i++) | |
404 fprintf (stderr, "%s ", argv[i]); | |
405 fprintf (stderr, "\n"); | |
406 break; | |
407 } | |
408 } | |
409 | |
397 new_argv[0] = argv[0]; | 410 new_argv[0] = argv[0]; |
398 new_argv[1] = at_args; | 411 new_argv[1] = at_args; |
399 new_argv[2] = NULL; | 412 new_argv[2] = NULL; |
400 | 413 |
401 if (debug) | 414 if (debug) |
402 { | 415 { |
403 int i; | |
404 for (i = 0; new_argv[i]; i++) | 416 for (i = 0; new_argv[i]; i++) |
405 fprintf (stderr, "%s ", new_argv[i]); | 417 fprintf (stderr, "%s ", new_argv[i]); |
406 fprintf (stderr, "\n"); | 418 fprintf (stderr, "\n"); |
407 } | 419 } |
408 | 420 |
447 | 459 |
448 static enum ld_plugin_status | 460 static enum ld_plugin_status |
449 all_symbols_read_handler (void) | 461 all_symbols_read_handler (void) |
450 { | 462 { |
451 unsigned i; | 463 unsigned i; |
452 unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 2 + 1; | 464 unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 1; |
453 char **lto_argv; | 465 char **lto_argv; |
454 const char **lto_arg_ptr; | 466 const char **lto_arg_ptr; |
455 if (num_claimed_files == 0) | 467 if (num_claimed_files == 0) |
456 return LDPS_OK; | 468 return LDPS_OK; |
457 | 469 |
459 { | 471 { |
460 use_original_files (); | 472 use_original_files (); |
461 return LDPS_OK; | 473 return LDPS_OK; |
462 } | 474 } |
463 | 475 |
464 lto_argv = (char **) calloc (sizeof (char *), num_lto_args); | 476 lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args); |
465 lto_arg_ptr = (const char **) lto_argv; | 477 lto_arg_ptr = (const char **) lto_argv; |
466 assert (lto_wrapper_argv); | 478 assert (lto_wrapper_argv); |
467 | 479 |
468 resolution_file = make_temp_file (""); | |
469 | |
470 write_resolution (); | 480 write_resolution (); |
471 | 481 |
472 free_1 (); | 482 free_1 (); |
473 | 483 |
474 for (i = 0; i < lto_wrapper_num_args; i++) | 484 for (i = 0; i < lto_wrapper_num_args; i++) |
475 *lto_arg_ptr++ = lto_wrapper_argv[i]; | 485 *lto_arg_ptr++ = lto_wrapper_argv[i]; |
476 | |
477 *lto_arg_ptr++ = "-fresolution"; | |
478 *lto_arg_ptr++ = resolution_file; | |
479 | 486 |
480 for (i = 0; i < num_claimed_files; i++) | 487 for (i = 0; i < num_claimed_files; i++) |
481 { | 488 { |
482 struct plugin_file_info *info = &claimed_files[i]; | 489 struct plugin_file_info *info = &claimed_files[i]; |
483 | 490 |
511 /* Remove temporary files at the end of the link. */ | 518 /* Remove temporary files at the end of the link. */ |
512 | 519 |
513 static enum ld_plugin_status | 520 static enum ld_plugin_status |
514 cleanup_handler (void) | 521 cleanup_handler (void) |
515 { | 522 { |
523 unsigned int i; | |
516 int t; | 524 int t; |
517 | 525 |
518 if (debug) | 526 if (debug) |
519 return LDPS_OK; | 527 return LDPS_OK; |
520 | 528 |
522 { | 530 { |
523 t = unlink (arguments_file_name); | 531 t = unlink (arguments_file_name); |
524 check (t == 0, LDPL_FATAL, "could not unlink arguments file"); | 532 check (t == 0, LDPL_FATAL, "could not unlink arguments file"); |
525 } | 533 } |
526 | 534 |
527 if (resolution_file) | 535 for (i = 0; i < num_output_files; i++) |
528 { | 536 { |
529 t = unlink (resolution_file); | 537 t = unlink (output_files[i]); |
530 check (t == 0, LDPL_FATAL, "could not unlink resolution file"); | 538 check (t == 0, LDPL_FATAL, "could not unlink output file"); |
531 } | 539 } |
532 | 540 |
533 free_2 (); | 541 free_2 (); |
534 return LDPS_OK; | 542 return LDPS_OK; |
535 } | 543 } |
571 check (elf != NULL, LDPL_FATAL, "could not find archive member"); | 579 check (elf != NULL, LDPL_FATAL, "could not find archive member"); |
572 elf_end (archive); | 580 elf_end (archive); |
573 } | 581 } |
574 else | 582 else |
575 { | 583 { |
576 lto_file.name = strdup (file->name); | 584 lto_file.name = xstrdup (file->name); |
577 elf = elf_begin (file->fd, ELF_C_READ, NULL); | 585 elf = elf_begin (file->fd, ELF_C_READ, NULL); |
578 } | 586 } |
579 lto_file.handle = file->handle; | 587 lto_file.handle = file->handle; |
580 | 588 |
581 *claimed = 0; | 589 *claimed = 0; |
594 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols"); | 602 check (status == LDPS_OK, LDPL_FATAL, "could not add symbols"); |
595 | 603 |
596 *claimed = 1; | 604 *claimed = 1; |
597 num_claimed_files++; | 605 num_claimed_files++; |
598 claimed_files = | 606 claimed_files = |
599 realloc (claimed_files, | 607 xrealloc (claimed_files, |
600 num_claimed_files * sizeof (struct plugin_file_info)); | 608 num_claimed_files * sizeof (struct plugin_file_info)); |
601 claimed_files[num_claimed_files - 1] = lto_file; | 609 claimed_files[num_claimed_files - 1] = lto_file; |
602 | 610 |
603 goto cleanup; | 611 goto cleanup; |
604 | 612 |
605 err: | 613 err: |
622 else if (strcmp (option, "-nop") == 0) | 630 else if (strcmp (option, "-nop") == 0) |
623 nop = 1; | 631 nop = 1; |
624 else if (!strncmp (option, "-pass-through=", strlen("-pass-through="))) | 632 else if (!strncmp (option, "-pass-through=", strlen("-pass-through="))) |
625 { | 633 { |
626 num_pass_through_items++; | 634 num_pass_through_items++; |
627 pass_through_items = realloc (pass_through_items, | 635 pass_through_items = xrealloc (pass_through_items, |
628 num_pass_through_items * sizeof (char *)); | 636 num_pass_through_items * sizeof (char *)); |
629 pass_through_items[num_pass_through_items - 1] = | 637 pass_through_items[num_pass_through_items - 1] = |
630 strdup (option + strlen ("-pass-through=")); | 638 xstrdup (option + strlen ("-pass-through=")); |
631 } | 639 } |
632 else | 640 else |
633 { | 641 { |
634 int size; | 642 int size; |
643 char *opt = xstrdup (option); | |
635 lto_wrapper_num_args += 1; | 644 lto_wrapper_num_args += 1; |
636 size = lto_wrapper_num_args * sizeof (char *); | 645 size = lto_wrapper_num_args * sizeof (char *); |
637 lto_wrapper_argv = (char **) realloc (lto_wrapper_argv, size); | 646 lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size); |
638 lto_wrapper_argv[lto_wrapper_num_args - 1] = strdup(option); | 647 lto_wrapper_argv[lto_wrapper_num_args - 1] = opt; |
648 if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0) | |
649 resolution_file = opt + sizeof ("-fresolution=") - 1; | |
639 } | 650 } |
640 } | 651 } |
641 | 652 |
642 /* Called by gold after loading the plugin. TV is the transfer vector. */ | 653 /* Called by gold after loading the plugin. TV is the transfer vector. */ |
643 | 654 |