home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / ld / ldfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  7.2 KB  |  362 lines

  1.  
  2. /* Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GLD, the Gnu Linker.
  5.  
  6. GLD is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GLD is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GLD; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  ldfile.c
  22.  
  23.  look after all the file stuff
  24.  
  25.  */
  26.  
  27. #include "bfd.h"
  28. #include "sysdep.h"
  29.  
  30. #include "ldmisc.h"
  31. #include "ldlang.h"
  32. #include "ldfile.h"
  33. #include <ctype.h>
  34. /* EXPORT */
  35. char *ldfile_input_filename;
  36. CONST char * ldfile_output_machine_name ="";
  37. unsigned long ldfile_output_machine;
  38. enum bfd_architecture ldfile_output_architecture;
  39. boolean had_script;
  40.  
  41. /* IMPORT */
  42.  
  43. extern boolean option_v;
  44.  
  45.  
  46. #ifdef VMS
  47. char *slash = "";
  48. #else
  49. char *slash = "/";
  50. #endif
  51.  
  52.  
  53.  
  54.  
  55. /* LOACL */
  56. typedef struct search_dirs 
  57. {
  58.   char *name;
  59.   struct search_dirs *next;
  60. } search_dirs_type;
  61.  
  62. static search_dirs_type *search_head;
  63. static search_dirs_type **search_tail_ptr = &search_head;
  64.  
  65. typedef struct search_arch 
  66. {
  67.   char *name; 
  68.   struct search_arch *next;
  69. } search_arch_type;
  70.  
  71. static search_arch_type *search_arch_head;
  72. static search_arch_type **search_arch_tail_ptr = &search_arch_head;
  73.  
  74.  
  75.  
  76. void
  77. ldfile_add_library_path(name)
  78. char *name;
  79. {
  80.   search_dirs_type *new =
  81.     (search_dirs_type *)ldmalloc((bfd_size_type)(sizeof(search_dirs_type)));
  82.   new->name = name;
  83.   new->next = (search_dirs_type*)NULL;
  84.   *search_tail_ptr = new;
  85.   search_tail_ptr = &new->next;
  86. }
  87.  
  88.  
  89. static bfd*
  90. cached_bfd_openr(attempt,entry)
  91. char *attempt;
  92. lang_input_statement_type  *entry;
  93. {
  94.   entry->the_bfd = bfd_openr(attempt, entry->target);
  95.   if (option_v == true ) {
  96.     info("attempt to open %s %s\n", attempt,
  97.         (entry->the_bfd == (bfd *)NULL) ? "failed" : "succeeded" );
  98.   }
  99.   return entry->the_bfd;
  100. }
  101.  
  102. static bfd *
  103. open_a(arch, entry, lib, suffix)
  104. char *arch;
  105. lang_input_statement_type *entry;
  106. char *lib;
  107. char *suffix;
  108. {
  109.   bfd*desc;
  110.   search_dirs_type *search ;
  111.   for (search = search_head;
  112.        search != (search_dirs_type *)NULL;
  113.        search = search->next) 
  114.     {
  115.       char buffer[1000];
  116.       char *string;
  117.       if (entry->is_archive == true) {
  118.     sprintf(buffer,
  119.         "%s%s%s%s%s%s",
  120.         search->name,
  121.         slash,
  122.         lib,
  123.         entry->filename, arch, suffix);
  124.       }
  125.       else {
  126.     if (entry->filename[0] == '/' || entry->filename[0] == '.') {
  127.       strcpy(buffer, entry->filename);
  128.     } else {
  129.       sprintf(buffer,"%s%s%s",search->name, slash, entry->filename);
  130.     } 
  131.       }
  132.       string = buystring(buffer);      
  133.       desc = cached_bfd_openr (string, entry);
  134.       if (desc)
  135.     {
  136.       entry->filename = string;
  137.       entry->search_dirs_flag = false;
  138.       entry->the_bfd =  desc;
  139.       return desc;
  140.     }
  141.       free(string);
  142.     }
  143.   return (bfd *)NULL;
  144. }
  145.  
  146. /* Open the input file specified by 'entry', and return a descriptor.
  147.    The open file is remembered; if the same file is opened twice in a row,
  148.    a new open is not actually done.  */
  149.  
  150. void
  151. ldfile_open_file (entry)
  152. lang_input_statement_type *entry;
  153. {
  154.  
  155.   if (entry->superfile)
  156.     ldfile_open_file (entry->superfile);
  157.  
  158.   if (entry->search_dirs_flag)
  159.     {
  160.       search_arch_type *arch;
  161.       /* Try to open <filename><suffix> or lib<filename><suffix>.a */
  162.   
  163.       for (arch = search_arch_head;
  164.        arch != (search_arch_type *)NULL;
  165.        arch = arch->next) {
  166.     if (open_a(arch->name,entry,"lib",".a") != (bfd *)NULL) {
  167.       return;
  168.     }
  169. #ifdef VMS
  170.     if (open_a(arch->name,entry,":lib",".a") != (bfd *)NULL) {
  171.       return;
  172.     }
  173. #endif
  174.  
  175.       }
  176.  
  177.  
  178.     }
  179.   else {
  180.     entry->the_bfd = cached_bfd_openr (entry->filename, entry);
  181.  
  182.   }
  183.   if (!entry->the_bfd)  einfo("%F%P: Can't open %s\n", entry->filename);
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. static FILE *
  193. try_open(name, exten)
  194. char *name;
  195. char *exten;
  196. {
  197.   FILE *result;
  198.   char buff[1000];
  199.   result = fopen(name, "r");
  200.   if (option_v == true) {
  201.     if (result == (FILE *)NULL) {
  202.       info("can't find ");
  203.     }
  204.     info("%s\n",name);
  205.     
  206.     return result;
  207.   }
  208.   sprintf(buff, "%s%s", name, exten);
  209.   result = fopen(buff, "r");
  210.  
  211.   if (option_v == true) {
  212.     if (result == (FILE *)NULL) {
  213.       info("can't find ");
  214.     }
  215.     info("%s\n", buff);
  216.   }
  217.   return result;
  218. }
  219. static FILE *
  220. find_a_name(name, extend)
  221. char *name;
  222. char *extend;
  223. {
  224.   search_dirs_type *search;
  225.   FILE *result;
  226.   char buffer[1000];
  227.   /* First try raw name */
  228.   result = try_open(name,"");
  229.   if (result == (FILE *)NULL) {
  230.     /* Try now prefixes */
  231.     for (search = search_head;
  232.      search != (search_dirs_type *)NULL;
  233.      search = search->next) {
  234.       sprintf(buffer,"%s/%s", search->name, name);
  235.       result = try_open(buffer, extend);
  236.       if (result)break;
  237.     }
  238.   }
  239.   return result;
  240. }
  241.  
  242. void ldfile_open_command_file(name)
  243. char *name;
  244. {
  245.    FILE *ldlex_input_stack;
  246.   ldlex_input_stack = find_a_name(name, ".ld");
  247.  
  248.   if (ldlex_input_stack == (FILE *)NULL) {
  249.     einfo("%P%F cannot open load script file %s\n",name);
  250.   }
  251.   lex_push_file(ldlex_input_stack, name);
  252.   
  253.   ldfile_input_filename = name;
  254.   had_script = true;
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261. #ifdef GNU960
  262. static
  263. char *
  264. gnu960_map_archname( name )
  265. char *name;
  266. {
  267.   struct tabentry { char *cmd_switch; char *arch; };
  268.   static struct tabentry arch_tab[] = {
  269.     "",   "",
  270.     "KA", "ka",
  271.     "KB", "kb",
  272.     "KC", "mc",    /* Synonym for MC */
  273.     "MC", "mc",
  274.     "CA", "ca",
  275.     "SA", "ka",    /* Functionally equivalent to KA */
  276.     "SB", "kb",    /* Functionally equivalent to KB */
  277.     NULL, ""
  278.   };
  279.   struct tabentry *tp;
  280.   
  281.  
  282.   for ( tp = arch_tab; tp->cmd_switch != NULL; tp++ ){
  283.     if ( !strcmp(name,tp->cmd_switch) ){
  284.       break;
  285.     }
  286.   }
  287.  
  288.   if ( tp->cmd_switch == NULL ){
  289.     einfo("%P%F: unknown architecture: %s\n",name);
  290.   }
  291.   return tp->arch;
  292. }
  293.  
  294.  
  295.  
  296. void
  297. ldfile_add_arch(name)
  298. char *name;
  299. {
  300.   search_arch_type *new =
  301.     (search_arch_type *)ldmalloc((bfd_size_type)(sizeof(search_arch_type)));
  302.  
  303.  
  304.   if (*name != '\0') {
  305.     if (ldfile_output_machine_name[0] != '\0') {
  306.       einfo("%P%F: target architecture respecified\n");
  307.       return;
  308.     }
  309.     ldfile_output_machine_name = name;
  310.   }
  311.  
  312.   new->next = (search_arch_type*)NULL;
  313.   new->name = gnu960_map_archname( name );
  314.   *search_arch_tail_ptr = new;
  315.   search_arch_tail_ptr = &new->next;
  316.  
  317. }
  318.  
  319. #else    /* not GNU960 */
  320.  
  321.  
  322. void
  323. DEFUN(ldfile_add_arch,(in_name),
  324.       CONST char * in_name)
  325. {
  326.   char *name = buystring(in_name);
  327.   search_arch_type *new =
  328.     (search_arch_type *)ldmalloc((bfd_size_type)(sizeof(search_arch_type)));
  329.  
  330.   ldfile_output_machine_name = in_name;
  331.  
  332.   new->name = name;
  333.   new->next = (search_arch_type*)NULL;
  334.   while (*name) {
  335.     if (isupper(*name)) *name = tolower(*name);
  336.     name++;
  337.   }
  338.   *search_arch_tail_ptr = new;
  339.   search_arch_tail_ptr = &new->next;
  340.  
  341. }
  342. #endif
  343.  
  344. /* Set the output architecture */
  345. void
  346. DEFUN(ldfile_set_output_arch,(string),
  347. CONST char *string)
  348. {
  349.  
  350.  
  351.   bfd_arch_info_type *arch = bfd_scan_arch(string);
  352.  
  353.   if (arch) {
  354.     ldfile_output_architecture = arch->arch;
  355.     ldfile_output_machine = arch->mach;
  356.     ldfile_output_machine_name = arch->printable_name;
  357.   }
  358.   else {
  359.     einfo("%P%F: Can't represent machine `%s'\n", string);
  360.   }
  361. }
  362.