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