00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <alloca.h>
00049 #include <stdio.h>
00050 #include <stdlib.h>
00051 #include <unistd.h>
00052 #include "basic.h"
00053 #include "string_utils.h"
00054 #include "objects.h"
00055 #include "option_names.h"
00056 #include "options.h"
00057 #include "option_seen.h"
00058 #include "opt_actions.h"
00059 #include "get_options.h"
00060 #include "errors.h"
00061 #include "lang_defs.h"
00062 #include "phases.h"
00063 #include "file_names.h"
00064 #include "file_utils.h"
00065 #include "pathscale_defs.h"
00066 #include "run.h"
00067
00068 string_list_t *objects;
00069 string_list_t *lib_objects;
00070 static string_list_t *cxx_prelinker_objects;
00071 static string_list_t *ar_objects;
00072 static string_list_t *library_dirs;
00073
00074 static int check_for_whirl(char *name);
00075
00076 void
00077 init_objects (void)
00078 {
00079 objects = init_string_list();
00080 lib_objects = init_string_list();
00081 cxx_prelinker_objects = init_string_list();
00082 ar_objects = init_string_list();
00083 library_dirs = init_string_list();
00084 }
00085
00086
00087
00088
00089
00090
00091 void
00092 find_full_path_of_gcc_file (char* const gcc_name, char* const comp_name,
00093 char* full_path, int full_path_len) {
00094 int n = 3, i = 0;
00095 FILE *path_file;
00096 char *p;
00097 char **argv;
00098 buffer_t buf;
00099 char* tmp_name;
00100
00101 if (abi == ABI_N32) {
00102 n++;
00103 }
00104 argv = (char **) alloca(n*sizeof(char*));
00105 argv[i++] = gcc_name;
00106 if (abi == ABI_N32) {
00107 argv[i++] = "-m32";
00108 }
00109 sprintf(buf, "-print-file-name=%s", comp_name);
00110 argv[i++] = buf;
00111 argv[i++] = NULL;
00112
00113 tmp_name = create_temp_file_name ("gc");
00114 run_simple_program (argv[0], argv, tmp_name);
00115
00116
00117 path_file = fopen(tmp_name, "r");
00118 if (path_file == NULL) {
00119 internal_error("couldn't open %s tmp file", comp_name);
00120 return;
00121 }
00122 if (fgets (full_path, full_path_len, path_file) == NULL) {
00123 internal_error("couldn't read %s tmp file", comp_name);
00124 }
00125 fclose(path_file);
00126 if (full_path[0] != '/') {
00127 internal_error("%s path not found", comp_name);
00128 }
00129
00130 p = full_path + strlen(full_path) - 1;
00131 while (p >= full_path && (*p == '\r' || *p == '\n')) {
00132 *p = '\0';
00133 }
00134 }
00135
00136
00137 char *
00138 find_obj_path (char *objname)
00139 {
00140 buffer_t buf;
00141 sprintf (buf, "%s/%s", get_phase_dir(P_be), objname);
00142 if (file_exists(buf)) {
00143 return string_copy(buf);
00144 }
00145
00146 sprintf (buf, "%s/%s", get_phase_dir(P_library), objname);
00147 if (file_exists(buf)) {
00148 return string_copy(buf);
00149 }
00150
00151 sprintf (buf, "%s/%s", get_phase_dir(P_alt_library), objname);
00152 if (file_exists(buf)) {
00153 return string_copy(buf);
00154 }
00155
00156
00157 sprintf(buf, "%s/%s", get_phase_dir(P_startup), objname);
00158 return string_copy(buf);
00159 }
00160
00161
00162 char*
00163 find_crt_path (char *crtname)
00164 {
00165 string_item_t *p;
00166 buffer_t buf;
00167 phases_t ld_phase;
00168 #ifndef TARG_SL
00169
00170
00171
00172 ld_phase = determine_ld_phase (FALSE);
00173 if (ld_phase == P_ldplus || ld_phase == P_ld) {
00174
00175
00176
00177 find_full_path_of_gcc_file (get_full_phase_name(ld_phase),
00178 crtname, buf, sizeof(buf));
00179 return string_copy (buf);
00180 }
00181 #endif
00182 for (p = library_dirs->head; p != NULL; p = p->next) {
00183 sprintf(buf, "%s/%s", p->name, crtname);
00184 if (file_exists(buf)) {
00185 return string_copy(buf);
00186 }
00187 }
00188
00189 if (option_was_seen(O_nostdlib)) {
00190 error("crt files not found in any -L directories:");
00191 for (p = library_dirs->head; p != NULL; p = p->next) {
00192 fprintf(stderr, "\t%s/%s\n", p->name, crtname);
00193 }
00194 return crtname;
00195 }
00196 #ifndef TARG_SL
00197 sprintf (buf, "%s/%s", get_phase_dir(P_be), crtname);
00198 if (file_exists(buf)) { return string_copy(buf); }
00199
00200 sprintf (buf, "%s/%s", get_phase_dir(P_library), crtname);
00201 if (file_exists(buf)) { return string_copy(buf); }
00202
00203 sprintf (buf, "%s/%s", get_phase_dir(P_alt_library), crtname);
00204 if (file_exists(buf)) { return string_copy(buf); }
00205 #endif
00206 if (option_was_seen(O_L)) {
00207 error("crt files not found in any -L directories:");
00208 for (p = library_dirs->head; p != NULL; p = p->next) {
00209 fprintf(stderr, "\t%s/%s\n", p->name, crtname);
00210 }
00211
00212 return crtname;
00213 }
00214
00215 sprintf(buf, "%s/%s", get_phase_dir(P_startup), crtname);
00216 return string_copy(buf);
00217 }
00218
00219 static void
00220 init_given_crt_path (char *crtname, char *prog_name, char *tmp_name)
00221 {
00222 char* p;
00223 char buf[1024];
00224
00225 p = find_crt_path (crtname);
00226 strncpy (buf, p, sizeof(buf)-1);
00227 buf[sizeof(buf)-1] = '\0';
00228
00229
00230 p = drop_path (&buf[0]);
00231 *p = '\0';
00232 if (debug) fprintf(stderr, "%s found in %s\n", crtname, &buf[0]);
00233 add_library_dir (&buf[0]);
00234 }
00235
00236
00237
00238
00239 void init_stdc_plus_plus_path (void)
00240 {
00241 #ifndef TARG_SL
00242 phases_t ld_phase = determine_ld_phase (FALSE);
00243 if (ld_phase != P_ldplus || ld_phase != P_ld) {
00244
00245
00246
00247
00248 } else {
00249 char buf[1024];
00250 char* p;
00251 find_full_path_of_gcc_file (get_full_phase_name (ld_phase),
00252 "libstdc++.so", &buf[0], sizeof(buf));
00253 p = drop_path (&buf[0]);
00254 *p = '\0';
00255 if (debug) fprintf(stderr, "libstdc++.so found in %s\n", &buf[0]);
00256 add_library_dir (&buf[0]);
00257 }
00258 #else
00259 char *tmp_name = create_temp_file_name("sl");
00260 char *slcc_name = concat_strings(get_phase_dir(P_ld), "/slcc");
00261 init_given_crt_path ("libstdc++.a", slcc_name, tmp_name);
00262 #endif
00263 }
00264
00265
00266
00267 void
00268 init_crt_paths (void)
00269 {
00270
00271
00272
00273
00274
00275 #ifndef TARG_SL
00276 char *tmp_name = create_temp_file_name("gc");
00277 char *gcc_name = get_full_phase_name(P_ld);
00278 init_given_crt_path ("crtbegin.o", gcc_name, tmp_name);
00279 init_given_crt_path ("crt1.o", gcc_name, tmp_name);
00280 #else
00281
00282
00283
00284
00285 char *crt_names[2] = {"crtbegin.o", "crt1.o"};
00286
00287 for (int i = 0; i < 2; i++) {
00288 char *crt = crt_names[i];
00289 char* path = find_crt_path(crt);
00290 if (path[0] != '/') {
00291 internal_error("%s path not found", crt);
00292 }
00293 else {
00294 char *p = drop_path (path);
00295 *p = '\0';
00296 add_library_dir (path);
00297 }
00298 }
00299 #endif
00300 }
00301
00302
00303 boolean
00304 is_object_option (int flag)
00305 {
00306 switch (flag) {
00307 case O_object:
00308 case O_l:
00309 case O_all:
00310 case O__whole_archive:
00311 case O__no_whole_archive:
00312 case O_WlC:
00313 return TRUE;
00314 default:
00315 return FALSE;
00316 }
00317 }
00318
00319 struct prof_lib
00320 {
00321 const char *name;
00322 int always;
00323 };
00324
00325 static struct prof_lib prof_libs[] = {
00326
00327 { "BrokenLocale", 0 },
00328 { "anl", 0 },
00329 { "c", 0 },
00330 { "crypt", 0 },
00331 { "dl", 0 },
00332 { "m", 0 },
00333 { "nsl", 0 },
00334 { "pthread", 0 },
00335 { "resolv", 0 },
00336 { "rpcsvc", 0 },
00337 { "rt", 0 },
00338 { "util", 0 },
00339
00340 { "instr", 1 },
00341 { "msgi", 1 },
00342 { "mv", 1 },
00343 { "fortran", 1 },
00344
00345 { NULL, 0 },
00346 };
00347
00348 int prof_lib_exists(const char *lib)
00349 {
00350 char *path;
00351 int exists;
00352 asprintf(&path, "%s/lib%s_p.a", abi == ABI_N32 ? "/usr/lib" : "/usr/lib64",
00353 lib);
00354 exists = access(path, R_OK) == 0;
00355 free(path);
00356 return exists;
00357 }
00358
00359 void add_library(string_list_t *list, const char *lib)
00360 {
00361 struct prof_lib *l;
00362 if (option_was_seen(O_profile)) {
00363 for (l = prof_libs; l->name; l++) {
00364 if (strcmp(l->name, lib) != 0)
00365 continue;
00366 if (!l->always && !prof_lib_exists(lib))
00367 continue;
00368 lib = concat_strings(lib, "_p");
00369 }
00370 }
00371
00372 add_string(list, concat_strings("-l", lib));
00373 }
00374
00375
00376
00377 void
00378 add_object (int flag, char *arg)
00379 {
00380 phases_t ld_phase = determine_ld_phase (ipa == TRUE);
00381
00382
00383 switch (flag) {
00384 case O_l:
00385
00386 if (strcmp(arg, "m") == 0
00387 #ifndef VENDOR_OSP
00388 || strcmp(arg, "mpath") == 0
00389 #endif
00390 ) {
00391
00392
00393 if (xpg_flag && invoked_lang == L_f77) {
00394 add_library(lib_objects, "mv");
00395 add_library(lib_objects, "m");
00396 } else {
00397 #ifndef TARG_SL
00398 add_library(objects, "mv");
00399 #endif
00400 add_library(objects, "m");
00401 }
00402 #ifndef TARG_SL
00403 if (invoked_lang == L_CC) {
00404 add_library(cxx_prelinker_objects, "mv");
00405 add_library(cxx_prelinker_objects, "m");
00406 }
00407 #endif
00408 #ifdef TARG_X8664
00409 extern boolean link_with_mathlib;
00410
00411
00412
00413 link_with_mathlib = 1;
00414 #endif
00415 }
00416
00417
00418 if (xpg_flag && invoked_lang == L_f77) {
00419 add_library(lib_objects, arg);
00420 } else {
00421 add_library(objects, arg);
00422 }
00423 if (invoked_lang == L_CC) {
00424 add_library(cxx_prelinker_objects, arg);
00425 }
00426 break;
00427 case O_object:
00428 if (dashdash_flag && arg[0] == '-') {
00429 add_string(objects,"--");
00430 dashdash_flag = 1;
00431 }
00432 if (strncmp(arg, "-l", 2) == 0)
00433 add_object(O_l, arg + 2);
00434 else
00435 add_string(objects, arg);
00436 if (invoked_lang == L_CC) {
00437 add_string(cxx_prelinker_objects, arg);
00438 }
00439
00440 break;
00441 case O_WlC:
00442 if (ld_phase == P_ld || ld_phase == P_ldplus) {
00443 #ifdef TARG_SL
00444
00445 add_string(objects, arg);
00446 #else
00447 add_string(objects, concat_strings("-Wl,", arg));
00448 #endif
00449 } else {
00450
00451
00452
00453
00454 char* dup_str, *next_arg;
00455 dup_str = string_copy (arg);
00456 next_arg = strtok (dup_str, ",");
00457 do {
00458 add_string (objects, next_arg);
00459 } while (next_arg = strtok (NULL, ","));
00460 }
00461 break;
00462 case O__whole_archive:
00463 if (ld_phase == P_ld || ld_phase == P_ldplus) {
00464 add_string(objects, "-Wl,-whole-archive");
00465 } else {
00466 add_string(objects, "-whole-archive");
00467 }
00468 break;
00469 case O__no_whole_archive:
00470 if (ld_phase == P_ld || ld_phase == P_ldplus) {
00471 add_string(objects, "-Wl,-no-whole-archive");
00472 } else {
00473 add_string(objects, "-no-whole-archive");
00474 }
00475 break;
00476 default:
00477 internal_error("add_object called with not-an-object");
00478 }
00479 }
00480
00481
00482 void
00483 add_ar_objects (char *arg)
00484 {
00485 add_string(ar_objects, arg);
00486 }
00487
00488
00489 void
00490 append_objects_to_list (string_list_t *list)
00491 {
00492
00493 if (ipa != TRUE) {
00494 int has_ipa_obj = FALSE;
00495 string_item_t *p;
00496 for (p = objects->head; p != NULL; p = p->next) {
00497 char *filename = p->name;
00498 if (check_for_whirl(filename) == TRUE) {
00499 error("IPA-created object %s not allowed without -ipa", filename);
00500 has_ipa_obj = TRUE;
00501 }
00502 }
00503 if (has_ipa_obj == TRUE)
00504 do_exit(1);
00505 }
00506 append_string_lists (list, objects);
00507 if (xpg_flag && invoked_lang == L_f77) {
00508 append_string_lists (list, lib_objects);
00509 }
00510 }
00511
00512
00513 void
00514 append_cxx_prelinker_objects_to_list (string_list_t *list)
00515 {
00516 append_string_lists (list, cxx_prelinker_objects);
00517 }
00518
00519 void
00520 append_ar_objects_to_list(string_list_t *list)
00521 {
00522 append_string_lists (list, ar_objects);
00523 }
00524
00525 void
00526 append_libraries_to_list (string_list_t *list)
00527 {
00528 string_item_t *p;
00529 for (p = library_dirs->head; p != NULL; p = p->next) {
00530 add_string(list, concat_strings("-L", p->name));
00531 }
00532 }
00533
00534 void
00535 dump_objects (void)
00536 {
00537 printf("objects: ");
00538 print_string_list (stdout, objects);
00539 }
00540
00541 void
00542 add_library_dir (char *path)
00543 {
00544 add_string(library_dirs, path);
00545 }
00546
00547 string_list_t *
00548 get_library_dirs(void)
00549 {
00550 return library_dirs;
00551 }
00552
00553 void
00554 add_library_options (void)
00555 {
00556 int flag;
00557 buffer_t mbuf;
00558 buffer_t rbuf;
00559 char *suffix = NULL;
00560 char *mips_lib = NULL;
00561 char *proc_lib = NULL;
00562 char *lib = NULL;
00563
00564
00565
00566
00567
00568
00569 switch (abi) {
00570 #ifdef TARG_MIPS
00571 #ifndef TARG_SL
00572 case ABI_N32:
00573 case ABI_I32:
00574 append_phase_dir(P_library, "32");
00575 append_phase_dir(P_startup, "32");
00576 break;
00577 case ABI_64:
00578 append_phase_dir(P_library, "64");
00579 append_phase_dir(P_startup, "64");
00580 break;
00581 #else
00582 case ABI_N32:
00583 case ABI_64:
00584 break;
00585 #endif
00586 #else
00587 case ABI_N32:
00588 case ABI_64:
00589 break;
00590 #endif
00591 case ABI_I64:
00592 case ABI_W64:
00593 break;
00594 case ABI_IA32:
00595 break;
00596 default:
00597 internal_error("no abi set? (%d)", abi);
00598 }
00599 }
00600
00601
00602
00603 boolean
00604 is_maybe_linker_option (int flag)
00605 {
00606
00607
00608
00609
00610
00611 switch (flag) {
00612 default:
00613 break;
00614 }
00615 return FALSE;
00616 }
00617
00618
00619 void
00620 add_maybe_linker_option (int flag)
00621 {
00622
00623
00624
00625
00626
00627
00628 switch (flag) {
00629 default:
00630 break;
00631 }
00632 }
00633
00634
00635
00636 void
00637 finalize_maybe_linker_options (boolean is_linker)
00638 {
00639 string_item_t *p;
00640
00641 if (is_linker) {
00642
00643 for (p = objects->head; p != NULL; p = p->next) {
00644 if (p->name[0] == ',') {
00645
00646 char *new_str = string_copy(&(p->name[1]));
00647 p->name = new_str;
00648 }
00649 }
00650 } else {
00651 string_item_t *prev = NULL;
00652 int deleted = FALSE;
00653 for (p = objects->head; p != NULL; p = p->next) {
00654
00655 if (p->name[0] == ',') {
00656
00657 char *str = p->name;
00658
00659
00660
00661
00662
00663
00664
00665 if (prev == NULL) {
00666 objects->head = p->next;
00667 } else {
00668 prev->next = p->next;
00669 }
00670 deleted = TRUE;
00671 } else {
00672 prev = p;
00673 }
00674 }
00675
00676
00677 if (deleted) {
00678 string_item_t *tail = NULL;
00679 for (p = objects->head; p != NULL; p = p->next) {
00680 tail = p;
00681 }
00682 objects->tail = tail;
00683 }
00684 }
00685 }
00686
00687
00688 #if defined(TARG_NVISA)
00689
00690 static int check_for_whirl (char *name) { return FALSE; }
00691 #else
00692
00693
00694
00695
00696 #include <elf.h>
00697 #include <fcntl.h>
00698 #include <unistd.h>
00699 #include <sys/stat.h>
00700 #include <stdint.h>
00701
00702
00703 #define ET_SGI_IR (ET_LOPROC + 0)
00704 static int
00705 check_for_whirl(char *name)
00706 {
00707 int fd = -1;
00708 char *raw_bits = NULL;
00709 int size,bufsize;
00710 Elf32_Ehdr *p_ehdr = NULL;
00711 struct stat statb;
00712 int test;
00713
00714 fd = open(name, O_RDONLY, 0755);
00715 if (fd < 0)
00716 return FALSE;
00717
00718 if ((test = fstat(fd, &statb) != 0)) {
00719 close(fd);
00720 return FALSE;
00721 }
00722
00723 if (statb.st_size < sizeof(Elf64_Ehdr)) {
00724 close(fd);
00725 return FALSE;
00726 }
00727
00728 bufsize = sizeof(Elf64_Ehdr);
00729
00730 raw_bits = (char *)alloca(bufsize*4);
00731
00732 size = read(fd, raw_bits, bufsize);
00733
00734
00735
00736
00737 p_ehdr = (Elf32_Ehdr *)raw_bits;
00738 if (p_ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
00739 p_ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
00740 p_ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
00741 p_ehdr->e_ident[EI_MAG3] != ELFMAG3) {
00742 close(fd);
00743 return(FALSE);
00744 }
00745
00746 if(p_ehdr->e_ident[EI_CLASS] == ELFCLASS32){
00747 Elf32_Ehdr *p32_ehdr = (Elf32_Ehdr *)raw_bits;
00748 if (p32_ehdr->e_type == ET_SGI_IR) {
00749 close(fd);
00750 return TRUE;
00751 }
00752 }
00753 else {
00754 Elf64_Ehdr *p64_ehdr = (Elf64_Ehdr *)raw_bits;
00755 if (p64_ehdr->e_type == ET_SGI_IR) {
00756 close(fd);
00757 return TRUE;
00758 }
00759 }
00760
00761 close(fd);
00762 return FALSE;
00763
00764 }
00765 #endif //TARG_NVISA