* doc/c-xtensa.texi (Xtensa Automatic Alignment): Remove statements
[binutils.git] / binutils / nlmconv.c
blob9932ba29b71da447bcbc0c598e05a720c595c2e6
1 /* nlmconv.c -- NLM conversion program
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Written by Ian Lance Taylor <ian@cygnus.com>.
23 This program can be used to convert any appropriate object file
24 into a NetWare Loadable Module (an NLM). It will accept a linker
25 specification file which is identical to that accepted by the
26 NetWare linker, NLMLINK. */
28 /* AIX requires this to be the first thing in the file. */
29 #ifndef __GNUC__
30 # ifdef _AIX
31 #pragma alloca
32 #endif
33 #endif
35 #include "sysdep.h"
36 #include "bfd.h"
37 #include "libiberty.h"
38 #include "safe-ctype.h"
40 #include "ansidecl.h"
41 #include <time.h>
42 #include <sys/stat.h>
43 #include <sys/file.h>
44 #include <assert.h>
45 #include "getopt.h"
47 /* Internal BFD NLM header. */
48 #include "libnlm.h"
49 #include "nlmconv.h"
51 #ifdef NLMCONV_ALPHA
52 #include "coff/sym.h"
53 #include "coff/ecoff.h"
54 #endif
56 #include "bucomm.h"
58 /* If strerror is just a macro, we want to use the one from libiberty
59 since it will handle undefined values. */
60 #undef strerror
61 extern char *strerror (int);
63 #ifndef SEEK_SET
64 #define SEEK_SET 0
65 #endif
67 #ifndef R_OK
68 #define R_OK 4
69 #define W_OK 2
70 #define X_OK 1
71 #endif
73 /* Global variables. */
75 /* The name used to invoke the program. */
76 char *program_name;
78 /* Local variables. */
80 /* Whether to print out debugging information (currently just controls
81 whether it prints the linker command if there is one). */
82 static int debug;
84 /* The symbol table. */
85 static asymbol **symbols;
87 /* A section we create in the output file to hold pointers to where
88 the sections of the input file end up. We will put a pointer to
89 this section in the NLM header. These is an entry for each input
90 section. The format is
91 null terminated section name
92 zeroes to adjust to 4 byte boundary
93 4 byte section data file pointer
94 4 byte section size
95 We don't need a version number. The way we find this information
96 is by finding a stamp in the NLM header information. If we need to
97 change the format of this information, we can simply change the
98 stamp. */
99 static asection *secsec;
101 /* A temporary file name to be unlinked on exit. Actually, for most
102 errors, we leave it around. It's not clear whether that is helpful
103 or not. */
104 static char *unlink_on_exit;
106 /* The list of long options. */
107 static struct option long_options[] =
109 { "debug", no_argument, 0, 'd' },
110 { "header-file", required_argument, 0, 'T' },
111 { "help", no_argument, 0, 'h' },
112 { "input-target", required_argument, 0, 'I' },
113 { "input-format", required_argument, 0, 'I' }, /* Obsolete */
114 { "linker", required_argument, 0, 'l' },
115 { "output-target", required_argument, 0, 'O' },
116 { "output-format", required_argument, 0, 'O' }, /* Obsolete */
117 { "version", no_argument, 0, 'V' },
118 { NULL, no_argument, 0, 0 }
121 /* Local routines. */
123 int main (int, char **);
125 static void show_usage (FILE *, int);
126 static const char *select_output_format
127 (enum bfd_architecture, unsigned long, bfd_boolean);
128 static void setup_sections (bfd *, asection *, void *);
129 static void copy_sections (bfd *, asection *, void *);
130 static void mangle_relocs
131 (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
132 static void default_mangle_relocs
133 (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
134 static char *link_inputs (struct string_list *, char *, char *);
136 #ifdef NLMCONV_I386
137 static void i386_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
138 #endif
140 #ifdef NLMCONV_ALPHA
141 static void alpha_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
142 #endif
144 #ifdef NLMCONV_POWERPC
145 static void powerpc_build_stubs (bfd *, bfd *, asymbol ***, long *);
146 static void powerpc_resolve_stubs (bfd *, bfd *);
147 static void powerpc_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
148 #endif
150 /* The main routine. */
153 main (int argc, char **argv)
155 int opt;
156 char *input_file = NULL;
157 const char *input_format = NULL;
158 const char *output_format = NULL;
159 const char *header_file = NULL;
160 char *ld_arg = NULL;
161 Nlm_Internal_Fixed_Header fixed_hdr_struct;
162 Nlm_Internal_Variable_Header var_hdr_struct;
163 Nlm_Internal_Version_Header version_hdr_struct;
164 Nlm_Internal_Copyright_Header copyright_hdr_struct;
165 Nlm_Internal_Extended_Header extended_hdr_struct;
166 bfd *inbfd;
167 bfd *outbfd;
168 asymbol **newsyms, **outsyms;
169 long symcount, newsymalloc, newsymcount;
170 long symsize;
171 asection *text_sec, *bss_sec, *data_sec;
172 bfd_vma vma;
173 bfd_size_type align;
174 asymbol *endsym;
175 long i;
176 char inlead, outlead;
177 bfd_boolean gotstart, gotexit, gotcheck;
178 struct stat st;
179 FILE *custom_data = NULL;
180 FILE *help_data = NULL;
181 FILE *message_data = NULL;
182 FILE *rpc_data = NULL;
183 FILE *shared_data = NULL;
184 size_t custom_size = 0;
185 size_t help_size = 0;
186 size_t message_size = 0;
187 size_t module_size = 0;
188 size_t rpc_size = 0;
189 asection *custom_section = NULL;
190 asection *help_section = NULL;
191 asection *message_section = NULL;
192 asection *module_section = NULL;
193 asection *rpc_section = NULL;
194 asection *shared_section = NULL;
195 bfd *sharedbfd;
196 size_t shared_offset = 0;
197 size_t shared_size = 0;
198 static Nlm_Internal_Fixed_Header sharedhdr;
199 int len;
200 char *modname;
201 char **matching;
203 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
204 setlocale (LC_MESSAGES, "");
205 #endif
206 #if defined (HAVE_SETLOCALE)
207 setlocale (LC_CTYPE, "");
208 #endif
209 bindtextdomain (PACKAGE, LOCALEDIR);
210 textdomain (PACKAGE);
212 program_name = argv[0];
213 xmalloc_set_program_name (program_name);
215 expandargv (&argc, &argv);
217 bfd_init ();
218 set_default_bfd_target ();
220 while ((opt = getopt_long (argc, argv, "dHhI:l:O:T:Vv", long_options,
221 (int *) NULL))
222 != EOF)
224 switch (opt)
226 case 'd':
227 debug = 1;
228 break;
229 case 'H':
230 case 'h':
231 show_usage (stdout, 0);
232 break;
233 case 'I':
234 input_format = optarg;
235 break;
236 case 'l':
237 ld_arg = optarg;
238 break;
239 case 'O':
240 output_format = optarg;
241 break;
242 case 'T':
243 header_file = optarg;
244 break;
245 case 'v':
246 case 'V':
247 print_version ("nlmconv");
248 break;
249 case 0:
250 break;
251 default:
252 show_usage (stderr, 1);
253 break;
257 /* The input and output files may be named on the command line. */
258 output_file = NULL;
259 if (optind < argc)
261 input_file = argv[optind];
262 ++optind;
263 if (optind < argc)
265 output_file = argv[optind];
266 ++optind;
267 if (optind < argc)
268 show_usage (stderr, 1);
269 if (strcmp (input_file, output_file) == 0)
271 fatal (_("input and output files must be different"));
276 /* Initialize the header information to default values. */
277 fixed_hdr = &fixed_hdr_struct;
278 memset ((void *) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
279 var_hdr = &var_hdr_struct;
280 memset ((void *) &var_hdr_struct, 0, sizeof var_hdr_struct);
281 version_hdr = &version_hdr_struct;
282 memset ((void *) &version_hdr_struct, 0, sizeof version_hdr_struct);
283 copyright_hdr = &copyright_hdr_struct;
284 memset ((void *) &copyright_hdr_struct, 0, sizeof copyright_hdr_struct);
285 extended_hdr = &extended_hdr_struct;
286 memset ((void *) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
287 check_procedure = NULL;
288 custom_file = NULL;
289 debug_info = FALSE;
290 exit_procedure = "_Stop";
291 export_symbols = NULL;
292 map_file = NULL;
293 full_map = FALSE;
294 help_file = NULL;
295 import_symbols = NULL;
296 message_file = NULL;
297 modules = NULL;
298 sharelib_file = NULL;
299 start_procedure = "_Prelude";
300 verbose = FALSE;
301 rpc_file = NULL;
303 parse_errors = 0;
305 /* Parse the header file (if there is one). */
306 if (header_file != NULL)
308 if (! nlmlex_file (header_file)
309 || yyparse () != 0
310 || parse_errors != 0)
311 exit (1);
314 if (input_files != NULL)
316 if (input_file != NULL)
318 fatal (_("input file named both on command line and with INPUT"));
320 if (input_files->next == NULL)
321 input_file = input_files->string;
322 else
323 input_file = link_inputs (input_files, ld_arg, map_file);
325 else if (input_file == NULL)
327 non_fatal (_("no input file"));
328 show_usage (stderr, 1);
331 inbfd = bfd_openr (input_file, input_format);
332 if (inbfd == NULL)
333 bfd_fatal (input_file);
335 if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
337 bfd_nonfatal (input_file);
338 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
340 list_matching_formats (matching);
341 free (matching);
343 exit (1);
346 if (output_format == NULL)
347 output_format = select_output_format (bfd_get_arch (inbfd),
348 bfd_get_mach (inbfd),
349 bfd_big_endian (inbfd));
351 assert (output_format != NULL);
353 /* Use the output file named on the command line if it exists.
354 Otherwise use the file named in the OUTPUT statement. */
355 if (output_file == NULL)
357 non_fatal (_("no name for output file"));
358 show_usage (stderr, 1);
361 outbfd = bfd_openw (output_file, output_format);
362 if (outbfd == NULL)
363 bfd_fatal (output_file);
364 if (! bfd_set_format (outbfd, bfd_object))
365 bfd_fatal (output_file);
367 assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
369 /* XXX: Should we accept the unknown bfd format here ? */
370 if (bfd_arch_get_compatible (inbfd, outbfd, TRUE) == NULL)
371 non_fatal (_("warning: input and output formats are not compatible"));
373 /* Move the values read from the command file into outbfd. */
374 *nlm_fixed_header (outbfd) = fixed_hdr_struct;
375 *nlm_variable_header (outbfd) = var_hdr_struct;
376 *nlm_version_header (outbfd) = version_hdr_struct;
377 *nlm_copyright_header (outbfd) = copyright_hdr_struct;
378 *nlm_extended_header (outbfd) = extended_hdr_struct;
380 /* Start copying the input BFD to the output BFD. */
381 if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
382 bfd_fatal (bfd_get_filename (outbfd));
384 symsize = bfd_get_symtab_upper_bound (inbfd);
385 if (symsize < 0)
386 bfd_fatal (input_file);
387 symbols = (asymbol **) xmalloc (symsize);
388 symcount = bfd_canonicalize_symtab (inbfd, symbols);
389 if (symcount < 0)
390 bfd_fatal (input_file);
392 /* Make sure we have a .bss section. */
393 bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
394 if (bss_sec == NULL)
396 bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
397 if (bss_sec == NULL
398 || ! bfd_set_section_flags (outbfd, bss_sec, SEC_ALLOC)
399 || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
400 bfd_fatal (_("make .bss section"));
403 /* We store the original section names in the .nlmsections section,
404 so that programs which understand it can resurrect the original
405 sections from the NLM. We will put a pointer to .nlmsections in
406 the NLM header area. */
407 secsec = bfd_make_section (outbfd, ".nlmsections");
408 if (secsec == NULL)
409 bfd_fatal (_("make .nlmsections section"));
410 if (! bfd_set_section_flags (outbfd, secsec, SEC_HAS_CONTENTS))
411 bfd_fatal (_("set .nlmsections flags"));
413 #ifdef NLMCONV_POWERPC
414 /* For PowerPC NetWare we need to build stubs for calls to undefined
415 symbols. Because each stub requires an entry in the TOC section
416 which must be at the same location as other entries in the TOC
417 section, we must do this before determining where the TOC section
418 goes in setup_sections. */
419 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
420 powerpc_build_stubs (inbfd, outbfd, &symbols, &symcount);
421 #endif
423 /* Set up the sections. */
424 bfd_map_over_sections (inbfd, setup_sections, (void *) outbfd);
426 text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
428 /* The .bss section immediately follows the .data section. */
429 data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
430 if (data_sec != NULL)
432 bfd_size_type add;
434 vma = bfd_get_section_size (data_sec);
435 align = 1 << bss_sec->alignment_power;
436 add = ((vma + align - 1) &~ (align - 1)) - vma;
437 vma += add;
438 if (! bfd_set_section_vma (outbfd, bss_sec, vma))
439 bfd_fatal (_("set .bss vma"));
440 if (add != 0)
442 bfd_size_type data_size;
444 data_size = bfd_get_section_size (data_sec);
445 if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
446 bfd_fatal (_("set .data size"));
450 /* Adjust symbol information. */
451 inlead = bfd_get_symbol_leading_char (inbfd);
452 outlead = bfd_get_symbol_leading_char (outbfd);
453 gotstart = FALSE;
454 gotexit = FALSE;
455 gotcheck = FALSE;
456 newsymalloc = 10;
457 newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
458 newsymcount = 0;
459 endsym = NULL;
460 for (i = 0; i < symcount; i++)
462 asymbol *sym;
464 sym = symbols[i];
466 /* Add or remove a leading underscore. */
467 if (inlead != outlead)
469 if (inlead != '\0')
471 if (bfd_asymbol_name (sym)[0] == inlead)
473 if (outlead == '\0')
474 ++sym->name;
475 else
477 char *new;
479 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
480 new[0] = outlead;
481 strcpy (new + 1, bfd_asymbol_name (sym) + 1);
482 sym->name = new;
486 else
488 char *new;
490 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
491 new[0] = outlead;
492 strcpy (new + 1, bfd_asymbol_name (sym));
493 sym->name = new;
497 /* NLM's have an uninitialized data section, but they do not
498 have a common section in the Unix sense. Move all common
499 symbols into the .bss section, and mark them as exported. */
500 if (bfd_is_com_section (bfd_get_section (sym)))
502 bfd_vma size = sym->value;
504 sym->section = bss_sec;
505 sym->value = bfd_get_section_size (bss_sec);
506 size += sym->value;
507 align = 1 << bss_sec->alignment_power;
508 size = (size + align - 1) & ~(align - 1);
509 bfd_set_section_size (outbfd, bss_sec, size);
510 sym->flags |= BSF_EXPORT | BSF_GLOBAL;
512 else if (bfd_get_section (sym)->output_section != NULL)
514 /* Move the symbol into the output section. */
515 sym->value += bfd_get_section (sym)->output_offset;
516 sym->section = bfd_get_section (sym)->output_section;
517 /* This is no longer a section symbol. */
518 sym->flags &=~ BSF_SECTION_SYM;
521 /* Force _edata and _end to be defined. This would normally be
522 done by the linker, but the manipulation of the common
523 symbols will confuse it. */
524 if ((sym->flags & BSF_DEBUGGING) == 0
525 && bfd_asymbol_name (sym)[0] == '_'
526 && bfd_is_und_section (bfd_get_section (sym)))
528 if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
530 sym->section = bss_sec;
531 sym->value = 0;
533 if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
535 sym->section = bss_sec;
536 endsym = sym;
539 #ifdef NLMCONV_POWERPC
540 /* For PowerPC NetWare, we define __GOT0. This is the start
541 of the .got section. */
542 if (bfd_get_arch (inbfd) == bfd_arch_powerpc
543 && strcmp (bfd_asymbol_name (sym), "__GOT0") == 0)
545 asection *got_sec;
547 got_sec = bfd_get_section_by_name (inbfd, ".got");
548 assert (got_sec != (asection *) NULL);
549 sym->value = got_sec->output_offset;
550 sym->section = got_sec->output_section;
552 #endif
555 /* If this is a global symbol, check the export list. */
556 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
558 struct string_list *l;
559 int found_simple;
561 /* Unfortunately, a symbol can appear multiple times on the
562 export list, with and without prefixes. */
563 found_simple = 0;
564 for (l = export_symbols; l != NULL; l = l->next)
566 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
567 found_simple = 1;
568 else
570 char *zbase;
572 zbase = strchr (l->string, '@');
573 if (zbase != NULL
574 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
576 /* We must add a symbol with this prefix. */
577 if (newsymcount >= newsymalloc)
579 newsymalloc += 10;
580 newsyms = ((asymbol **)
581 xrealloc ((void *) newsyms,
582 (newsymalloc
583 * sizeof (asymbol *))));
585 newsyms[newsymcount] =
586 (asymbol *) xmalloc (sizeof (asymbol));
587 *newsyms[newsymcount] = *sym;
588 newsyms[newsymcount]->name = l->string;
589 ++newsymcount;
593 if (! found_simple)
595 /* The unmodified symbol is actually not exported at
596 all. */
597 sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
598 sym->flags |= BSF_LOCAL;
602 /* If it's an undefined symbol, see if it's on the import list.
603 Change the prefix if necessary. */
604 if (bfd_is_und_section (bfd_get_section (sym)))
606 struct string_list *l;
608 for (l = import_symbols; l != NULL; l = l->next)
610 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
611 break;
612 else
614 char *zbase;
616 zbase = strchr (l->string, '@');
617 if (zbase != NULL
618 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
620 sym->name = l->string;
621 break;
625 if (l == NULL)
626 non_fatal (_("warning: symbol %s imported but not in import list"),
627 bfd_asymbol_name (sym));
630 /* See if it's one of the special named symbols. */
631 if ((sym->flags & BSF_DEBUGGING) == 0)
633 bfd_vma val;
635 /* FIXME: If these symbols are not in the .text section, we
636 add the .text section size to the value. This may not be
637 correct for all targets. I'm not sure how this should
638 really be handled. */
639 if (strcmp (bfd_asymbol_name (sym), start_procedure) == 0)
641 val = bfd_asymbol_value (sym);
642 if (bfd_get_section (sym) == data_sec
643 && text_sec != (asection *) NULL)
644 val += bfd_section_size (outbfd, text_sec);
645 if (! bfd_set_start_address (outbfd, val))
646 bfd_fatal (_("set start address"));
647 gotstart = TRUE;
649 if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
651 val = bfd_asymbol_value (sym);
652 if (bfd_get_section (sym) == data_sec
653 && text_sec != (asection *) NULL)
654 val += bfd_section_size (outbfd, text_sec);
655 nlm_fixed_header (outbfd)->exitProcedureOffset = val;
656 gotexit = TRUE;
658 if (check_procedure != NULL
659 && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
661 val = bfd_asymbol_value (sym);
662 if (bfd_get_section (sym) == data_sec
663 && text_sec != (asection *) NULL)
664 val += bfd_section_size (outbfd, text_sec);
665 nlm_fixed_header (outbfd)->checkUnloadProcedureOffset = val;
666 gotcheck = TRUE;
671 if (endsym != NULL)
673 endsym->value = bfd_get_section_size (bss_sec);
675 /* FIXME: If any relocs referring to _end use inplace addends,
676 then I think they need to be updated. This is handled by
677 i386_mangle_relocs. Is it needed for any other object
678 formats? */
681 if (newsymcount == 0)
682 outsyms = symbols;
683 else
685 outsyms = (asymbol **) xmalloc ((symcount + newsymcount + 1)
686 * sizeof (asymbol *));
687 memcpy (outsyms, symbols, symcount * sizeof (asymbol *));
688 memcpy (outsyms + symcount, newsyms, newsymcount * sizeof (asymbol *));
689 outsyms[symcount + newsymcount] = NULL;
692 bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
694 if (! gotstart)
695 non_fatal (_("warning: START procedure %s not defined"), start_procedure);
696 if (! gotexit)
697 non_fatal (_("warning: EXIT procedure %s not defined"), exit_procedure);
698 if (check_procedure != NULL && ! gotcheck)
699 non_fatal (_("warning: CHECK procedure %s not defined"), check_procedure);
701 /* Add additional sections required for the header information. */
702 if (custom_file != NULL)
704 custom_data = fopen (custom_file, "r");
705 if (custom_data == NULL
706 || fstat (fileno (custom_data), &st) < 0)
708 fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
709 strerror (errno));
710 custom_file = NULL;
712 else
714 custom_size = st.st_size;
715 custom_section = bfd_make_section (outbfd, ".nlmcustom");
716 if (custom_section == NULL
717 || ! bfd_set_section_size (outbfd, custom_section, custom_size)
718 || ! bfd_set_section_flags (outbfd, custom_section,
719 SEC_HAS_CONTENTS))
720 bfd_fatal (_("custom section"));
723 if (help_file != NULL)
725 help_data = fopen (help_file, "r");
726 if (help_data == NULL
727 || fstat (fileno (help_data), &st) < 0)
729 fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
730 strerror (errno));
731 help_file = NULL;
733 else
735 help_size = st.st_size;
736 help_section = bfd_make_section (outbfd, ".nlmhelp");
737 if (help_section == NULL
738 || ! bfd_set_section_size (outbfd, help_section, help_size)
739 || ! bfd_set_section_flags (outbfd, help_section,
740 SEC_HAS_CONTENTS))
741 bfd_fatal (_("help section"));
742 LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
745 if (message_file != NULL)
747 message_data = fopen (message_file, "r");
748 if (message_data == NULL
749 || fstat (fileno (message_data), &st) < 0)
751 fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
752 strerror (errno));
753 message_file = NULL;
755 else
757 message_size = st.st_size;
758 message_section = bfd_make_section (outbfd, ".nlmmessages");
759 if (message_section == NULL
760 || ! bfd_set_section_size (outbfd, message_section, message_size)
761 || ! bfd_set_section_flags (outbfd, message_section,
762 SEC_HAS_CONTENTS))
763 bfd_fatal (_("message section"));
764 LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
767 if (modules != NULL)
769 struct string_list *l;
771 module_size = 0;
772 for (l = modules; l != NULL; l = l->next)
773 module_size += strlen (l->string) + 1;
774 module_section = bfd_make_section (outbfd, ".nlmmodules");
775 if (module_section == NULL
776 || ! bfd_set_section_size (outbfd, module_section, module_size)
777 || ! bfd_set_section_flags (outbfd, module_section,
778 SEC_HAS_CONTENTS))
779 bfd_fatal (_("module section"));
781 if (rpc_file != NULL)
783 rpc_data = fopen (rpc_file, "r");
784 if (rpc_data == NULL
785 || fstat (fileno (rpc_data), &st) < 0)
787 fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
788 strerror (errno));
789 rpc_file = NULL;
791 else
793 rpc_size = st.st_size;
794 rpc_section = bfd_make_section (outbfd, ".nlmrpc");
795 if (rpc_section == NULL
796 || ! bfd_set_section_size (outbfd, rpc_section, rpc_size)
797 || ! bfd_set_section_flags (outbfd, rpc_section,
798 SEC_HAS_CONTENTS))
799 bfd_fatal (_("rpc section"));
800 LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
803 if (sharelib_file != NULL)
805 sharedbfd = bfd_openr (sharelib_file, output_format);
806 if (sharedbfd == NULL
807 || ! bfd_check_format (sharedbfd, bfd_object))
809 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
810 bfd_errmsg (bfd_get_error ()));
811 sharelib_file = NULL;
813 else
815 sharedhdr = *nlm_fixed_header (sharedbfd);
816 bfd_close (sharedbfd);
817 shared_data = fopen (sharelib_file, "r");
818 if (shared_data == NULL
819 || (fstat (fileno (shared_data), &st) < 0))
821 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
822 strerror (errno));
823 sharelib_file = NULL;
825 else
827 /* If we were clever, we could just copy out the
828 sections of the shared library which we actually
829 need. However, we would have to figure out the sizes
830 of the external and public information, and that can
831 not be done without reading through them. */
832 if (sharedhdr.uninitializedDataSize > 0)
834 /* There is no place to record this information. */
835 non_fatal (_("%s: warning: shared libraries can not have uninitialized data"),
836 sharelib_file);
838 shared_offset = st.st_size;
839 if (shared_offset > (size_t) sharedhdr.codeImageOffset)
840 shared_offset = sharedhdr.codeImageOffset;
841 if (shared_offset > (size_t) sharedhdr.dataImageOffset)
842 shared_offset = sharedhdr.dataImageOffset;
843 if (shared_offset > (size_t) sharedhdr.relocationFixupOffset)
844 shared_offset = sharedhdr.relocationFixupOffset;
845 if (shared_offset > (size_t) sharedhdr.externalReferencesOffset)
846 shared_offset = sharedhdr.externalReferencesOffset;
847 if (shared_offset > (size_t) sharedhdr.publicsOffset)
848 shared_offset = sharedhdr.publicsOffset;
849 shared_size = st.st_size - shared_offset;
850 shared_section = bfd_make_section (outbfd, ".nlmshared");
851 if (shared_section == NULL
852 || ! bfd_set_section_size (outbfd, shared_section,
853 shared_size)
854 || ! bfd_set_section_flags (outbfd, shared_section,
855 SEC_HAS_CONTENTS))
856 bfd_fatal (_("shared section"));
857 LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
862 /* Check whether a version was given. */
863 if (!CONST_STRNEQ (version_hdr->stamp, "VeRsIoN#"))
864 non_fatal (_("warning: No version number given"));
866 /* At least for now, always create an extended header, because that
867 is what NLMLINK does. */
868 LITMEMCPY (nlm_extended_header (outbfd)->stamp, "MeSsAgEs");
870 LITMEMCPY (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx");
872 /* If the date was not given, force it in. */
873 if (nlm_version_header (outbfd)->month == 0
874 && nlm_version_header (outbfd)->day == 0
875 && nlm_version_header (outbfd)->year == 0)
877 time_t now;
878 struct tm *ptm;
880 time (&now);
881 ptm = localtime (&now);
882 nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
883 nlm_version_header (outbfd)->day = ptm->tm_mday;
884 nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
885 LITMEMCPY (version_hdr->stamp, "VeRsIoN#");
888 #ifdef NLMCONV_POWERPC
889 /* Resolve the stubs we build for PowerPC NetWare. */
890 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
891 powerpc_resolve_stubs (inbfd, outbfd);
892 #endif
894 /* Copy over the sections. */
895 bfd_map_over_sections (inbfd, copy_sections, (void *) outbfd);
897 /* Finish up the header information. */
898 if (custom_file != NULL)
900 void *data;
902 data = xmalloc (custom_size);
903 if (fread (data, 1, custom_size, custom_data) != custom_size)
904 non_fatal (_("%s: read: %s"), custom_file, strerror (errno));
905 else
907 if (! bfd_set_section_contents (outbfd, custom_section, data,
908 (file_ptr) 0, custom_size))
909 bfd_fatal (_("custom section"));
910 nlm_fixed_header (outbfd)->customDataOffset =
911 custom_section->filepos;
912 nlm_fixed_header (outbfd)->customDataSize = custom_size;
914 free (data);
916 if (! debug_info)
918 /* As a special hack, the backend recognizes a debugInfoOffset
919 of -1 to mean that it should not output any debugging
920 information. This can not be handling by fiddling with the
921 symbol table because exported symbols appear in both the
922 export information and the debugging information. */
923 nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
925 if (full_map)
926 non_fatal (_("warning: FULLMAP is not supported; try ld -M"));
927 if (help_file != NULL)
929 void *data;
931 data = xmalloc (help_size);
932 if (fread (data, 1, help_size, help_data) != help_size)
933 non_fatal (_("%s: read: %s"), help_file, strerror (errno));
934 else
936 if (! bfd_set_section_contents (outbfd, help_section, data,
937 (file_ptr) 0, help_size))
938 bfd_fatal (_("help section"));
939 nlm_extended_header (outbfd)->helpFileOffset =
940 help_section->filepos;
941 nlm_extended_header (outbfd)->helpFileLength = help_size;
943 free (data);
945 if (message_file != NULL)
947 void *data;
949 data = xmalloc (message_size);
950 if (fread (data, 1, message_size, message_data) != message_size)
951 non_fatal (_("%s: read: %s"), message_file, strerror (errno));
952 else
954 if (! bfd_set_section_contents (outbfd, message_section, data,
955 (file_ptr) 0, message_size))
956 bfd_fatal (_("message section"));
957 nlm_extended_header (outbfd)->messageFileOffset =
958 message_section->filepos;
959 nlm_extended_header (outbfd)->messageFileLength = message_size;
961 /* FIXME: Are these offsets correct on all platforms? Are
962 they 32 bits on all platforms? What endianness? */
963 nlm_extended_header (outbfd)->languageID =
964 bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
965 nlm_extended_header (outbfd)->messageCount =
966 bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
968 free (data);
970 if (modules != NULL)
972 void *data;
973 unsigned char *set;
974 struct string_list *l;
975 bfd_size_type c;
977 data = xmalloc (module_size);
978 c = 0;
979 set = (unsigned char *) data;
980 for (l = modules; l != NULL; l = l->next)
982 *set = strlen (l->string);
983 strncpy ((char *) set + 1, l->string, *set);
984 set += *set + 1;
985 ++c;
987 if (! bfd_set_section_contents (outbfd, module_section, data,
988 (file_ptr) 0, module_size))
989 bfd_fatal (_("module section"));
990 nlm_fixed_header (outbfd)->moduleDependencyOffset =
991 module_section->filepos;
992 nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
994 if (rpc_file != NULL)
996 void *data;
998 data = xmalloc (rpc_size);
999 if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
1000 non_fatal (_("%s: read: %s"), rpc_file, strerror (errno));
1001 else
1003 if (! bfd_set_section_contents (outbfd, rpc_section, data,
1004 (file_ptr) 0, rpc_size))
1005 bfd_fatal (_("rpc section"));
1006 nlm_extended_header (outbfd)->RPCDataOffset =
1007 rpc_section->filepos;
1008 nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
1010 free (data);
1012 if (sharelib_file != NULL)
1014 void *data;
1016 data = xmalloc (shared_size);
1017 if (fseek (shared_data, shared_offset, SEEK_SET) != 0
1018 || fread (data, 1, shared_size, shared_data) != shared_size)
1019 non_fatal (_("%s: read: %s"), sharelib_file, strerror (errno));
1020 else
1022 if (! bfd_set_section_contents (outbfd, shared_section, data,
1023 (file_ptr) 0, shared_size))
1024 bfd_fatal (_("shared section"));
1026 nlm_extended_header (outbfd)->sharedCodeOffset =
1027 sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
1028 nlm_extended_header (outbfd)->sharedCodeLength =
1029 sharedhdr.codeImageSize;
1030 nlm_extended_header (outbfd)->sharedDataOffset =
1031 sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
1032 nlm_extended_header (outbfd)->sharedDataLength =
1033 sharedhdr.dataImageSize;
1034 nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
1035 (sharedhdr.relocationFixupOffset
1036 - shared_offset
1037 + shared_section->filepos);
1038 nlm_extended_header (outbfd)->sharedRelocationFixupCount =
1039 sharedhdr.numberOfRelocationFixups;
1040 nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
1041 (sharedhdr.externalReferencesOffset
1042 - shared_offset
1043 + shared_section->filepos);
1044 nlm_extended_header (outbfd)->sharedExternalReferenceCount =
1045 sharedhdr.numberOfExternalReferences;
1046 nlm_extended_header (outbfd)->sharedPublicsOffset =
1047 sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
1048 nlm_extended_header (outbfd)->sharedPublicsCount =
1049 sharedhdr.numberOfPublics;
1050 nlm_extended_header (outbfd)->sharedDebugRecordOffset =
1051 sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
1052 nlm_extended_header (outbfd)->sharedDebugRecordCount =
1053 sharedhdr.numberOfDebugRecords;
1054 nlm_extended_header (outbfd)->SharedInitializationOffset =
1055 sharedhdr.codeStartOffset;
1056 nlm_extended_header (outbfd)->SharedExitProcedureOffset =
1057 sharedhdr.exitProcedureOffset;
1058 free (data);
1062 const int max_len = NLM_MODULE_NAME_SIZE - 2;
1063 const char * filename = lbasename (output_file);
1065 len = strlen (filename);
1066 if (len > max_len)
1067 len = max_len;
1068 nlm_fixed_header (outbfd)->moduleName[0] = len;
1070 strncpy (nlm_fixed_header (outbfd)->moduleName + 1, filename, max_len);
1071 nlm_fixed_header (outbfd)->moduleName[max_len + 1] = '\0';
1073 for (modname = nlm_fixed_header (outbfd)->moduleName;
1074 *modname != '\0';
1075 modname++)
1076 *modname = TOUPPER (*modname);
1079 strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
1080 NLM_OLD_THREAD_NAME_LENGTH);
1082 nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
1083 nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
1085 if (! bfd_close (outbfd))
1086 bfd_fatal (output_file);
1087 if (! bfd_close (inbfd))
1088 bfd_fatal (input_file);
1090 if (unlink_on_exit != NULL)
1091 unlink (unlink_on_exit);
1093 return 0;
1097 /* Show a usage message and exit. */
1099 static void
1100 show_usage (FILE *file, int status)
1102 fprintf (file, _("Usage: %s [option(s)] [in-file [out-file]]\n"), program_name);
1103 fprintf (file, _(" Convert an object file into a NetWare Loadable Module\n"));
1104 fprintf (file, _(" The options are:\n\
1105 -I --input-target=<bfdname> Set the input binary file format\n\
1106 -O --output-target=<bfdname> Set the output binary file format\n\
1107 -T --header-file=<file> Read <file> for NLM header information\n\
1108 -l --linker=<linker> Use <linker> for any linking\n\
1109 -d --debug Display on stderr the linker command line\n\
1110 @<file> Read options from <file>.\n\
1111 -h --help Display this information\n\
1112 -v --version Display the program's version\n\
1113 "));
1114 if (REPORT_BUGS_TO[0] && status == 0)
1115 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1116 exit (status);
1119 /* Select the output format based on the input architecture, machine,
1120 and endianness. This chooses the appropriate NLM target. */
1122 static const char *
1123 select_output_format (enum bfd_architecture arch, unsigned long mach,
1124 bfd_boolean bigendian ATTRIBUTE_UNUSED)
1126 switch (arch)
1128 #ifdef NLMCONV_I386
1129 case bfd_arch_i386:
1130 return "nlm32-i386";
1131 #endif
1132 #ifdef NLMCONV_SPARC
1133 case bfd_arch_sparc:
1134 return "nlm32-sparc";
1135 #endif
1136 #ifdef NLMCONV_ALPHA
1137 case bfd_arch_alpha:
1138 return "nlm32-alpha";
1139 #endif
1140 #ifdef NLMCONV_POWERPC
1141 case bfd_arch_powerpc:
1142 return "nlm32-powerpc";
1143 #endif
1144 default:
1145 fatal (_("support not compiled in for %s"),
1146 bfd_printable_arch_mach (arch, mach));
1148 /*NOTREACHED*/
1151 /* The BFD sections are copied in two passes. This function selects
1152 the output section for each input section, and sets up the section
1153 name, size, etc. */
1155 static void
1156 setup_sections (bfd *inbfd ATTRIBUTE_UNUSED, asection *insec, void *data_ptr)
1158 bfd *outbfd = (bfd *) data_ptr;
1159 flagword f;
1160 const char *outname;
1161 asection *outsec;
1162 bfd_vma offset;
1163 bfd_size_type align;
1164 bfd_size_type add;
1165 bfd_size_type secsecsize;
1167 f = bfd_get_section_flags (inbfd, insec);
1168 if (f & SEC_CODE)
1169 outname = NLM_CODE_NAME;
1170 else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
1171 outname = NLM_INITIALIZED_DATA_NAME;
1172 else if (f & SEC_ALLOC)
1173 outname = NLM_UNINITIALIZED_DATA_NAME;
1174 else
1175 outname = bfd_section_name (inbfd, insec);
1177 outsec = bfd_get_section_by_name (outbfd, outname);
1178 if (outsec == NULL)
1180 outsec = bfd_make_section (outbfd, outname);
1181 if (outsec == NULL)
1182 bfd_fatal (_("make section"));
1185 insec->output_section = outsec;
1187 offset = bfd_section_size (outbfd, outsec);
1188 align = 1 << bfd_section_alignment (inbfd, insec);
1189 add = ((offset + align - 1) &~ (align - 1)) - offset;
1190 insec->output_offset = offset + add;
1192 if (! bfd_set_section_size (outbfd, outsec,
1193 (bfd_section_size (outbfd, outsec)
1194 + bfd_section_size (inbfd, insec)
1195 + add)))
1196 bfd_fatal (_("set section size"));
1198 if ((bfd_section_alignment (inbfd, insec)
1199 > bfd_section_alignment (outbfd, outsec))
1200 && ! bfd_set_section_alignment (outbfd, outsec,
1201 bfd_section_alignment (inbfd, insec)))
1202 bfd_fatal (_("set section alignment"));
1204 if (! bfd_set_section_flags (outbfd, outsec,
1205 f | bfd_get_section_flags (outbfd, outsec)))
1206 bfd_fatal (_("set section flags"));
1208 bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
1210 /* For each input section we allocate space for an entry in
1211 .nlmsections. */
1212 secsecsize = bfd_section_size (outbfd, secsec);
1213 secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
1214 secsecsize = (secsecsize + 3) &~ 3;
1215 secsecsize += 8;
1216 if (! bfd_set_section_size (outbfd, secsec, secsecsize))
1217 bfd_fatal (_("set .nlmsections size"));
1220 /* Copy the section contents. */
1222 static void
1223 copy_sections (bfd *inbfd, asection *insec, void *data_ptr)
1225 static bfd_size_type secsecoff = 0;
1226 bfd *outbfd = (bfd *) data_ptr;
1227 const char *inname;
1228 asection *outsec;
1229 bfd_size_type size;
1230 void *contents;
1231 long reloc_size;
1232 bfd_byte buf[4];
1233 bfd_size_type add;
1235 inname = bfd_section_name (inbfd, insec);
1237 outsec = insec->output_section;
1238 assert (outsec != NULL);
1240 size = bfd_get_section_size (insec);
1242 if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
1243 contents = NULL;
1244 else
1246 contents = xmalloc (size);
1247 if (! bfd_get_section_contents (inbfd, insec, contents,
1248 (file_ptr) 0, size))
1249 bfd_fatal (bfd_get_filename (inbfd));
1252 reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
1253 if (reloc_size < 0)
1254 bfd_fatal (bfd_get_filename (inbfd));
1255 if (reloc_size != 0)
1257 arelent **relocs;
1258 long reloc_count;
1260 relocs = (arelent **) xmalloc (reloc_size);
1261 reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
1262 if (reloc_count < 0)
1263 bfd_fatal (bfd_get_filename (inbfd));
1264 mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
1265 size);
1267 /* FIXME: refers to internal BFD fields. */
1268 if (outsec->orelocation != (arelent **) NULL)
1270 bfd_size_type total_count;
1271 arelent **combined;
1273 total_count = reloc_count + outsec->reloc_count;
1274 combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
1275 memcpy (combined, outsec->orelocation,
1276 outsec->reloc_count * sizeof (arelent *));
1277 memcpy (combined + outsec->reloc_count, relocs,
1278 (size_t) (reloc_count * sizeof (arelent *)));
1279 free (outsec->orelocation);
1280 reloc_count = total_count;
1281 relocs = combined;
1284 bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
1287 if (contents != NULL)
1289 if (! bfd_set_section_contents (outbfd, outsec, contents,
1290 insec->output_offset, size))
1291 bfd_fatal (bfd_get_filename (outbfd));
1292 free (contents);
1295 /* Add this section to .nlmsections. */
1296 if (! bfd_set_section_contents (outbfd, secsec, (void *) inname, secsecoff,
1297 strlen (inname) + 1))
1298 bfd_fatal (_("set .nlmsection contents"));
1299 secsecoff += strlen (inname) + 1;
1301 add = ((secsecoff + 3) &~ 3) - secsecoff;
1302 if (add != 0)
1304 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1305 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
1306 bfd_fatal (_("set .nlmsection contents"));
1307 secsecoff += add;
1310 if (contents != NULL)
1311 bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
1312 else
1313 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1314 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1315 bfd_fatal (_("set .nlmsection contents"));
1316 secsecoff += 4;
1318 bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
1319 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1320 bfd_fatal (_("set .nlmsection contents"));
1321 secsecoff += 4;
1324 /* Some, perhaps all, NetWare targets require changing the relocs used
1325 by the input formats. */
1327 static void
1328 mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
1329 long *reloc_count_ptr, char *contents,
1330 bfd_size_type contents_size)
1332 switch (bfd_get_arch (outbfd))
1334 #ifdef NLMCONV_I386
1335 case bfd_arch_i386:
1336 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1337 contents, contents_size);
1338 break;
1339 #endif
1340 #ifdef NLMCONV_ALPHA
1341 case bfd_arch_alpha:
1342 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1343 contents, contents_size);
1344 break;
1345 #endif
1346 #ifdef NLMCONV_POWERPC
1347 case bfd_arch_powerpc:
1348 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1349 contents, contents_size);
1350 break;
1351 #endif
1352 default:
1353 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1354 contents, contents_size);
1355 break;
1359 /* By default all we need to do for relocs is change the address by
1360 the output_offset. */
1362 static void
1363 default_mangle_relocs (bfd *outbfd ATTRIBUTE_UNUSED, asection *insec,
1364 arelent ***relocs_ptr, long *reloc_count_ptr,
1365 char *contents ATTRIBUTE_UNUSED,
1366 bfd_size_type contents_size ATTRIBUTE_UNUSED)
1368 if (insec->output_offset != 0)
1370 long reloc_count;
1371 arelent **relocs;
1372 long i;
1374 reloc_count = *reloc_count_ptr;
1375 relocs = *relocs_ptr;
1376 for (i = 0; i < reloc_count; i++, relocs++)
1377 (*relocs)->address += insec->output_offset;
1381 #ifdef NLMCONV_I386
1383 /* NetWare on the i386 supports a restricted set of relocs, which are
1384 different from those used on other i386 targets. This routine
1385 converts the relocs. It is, obviously, very target dependent. At
1386 the moment, the nlm32-i386 backend performs similar translations;
1387 however, it is more reliable and efficient to do them here. */
1389 static reloc_howto_type nlm_i386_pcrel_howto =
1390 HOWTO (1, /* type */
1391 0, /* rightshift */
1392 2, /* size (0 = byte, 1 = short, 2 = long) */
1393 32, /* bitsize */
1394 TRUE, /* pc_relative */
1395 0, /* bitpos */
1396 complain_overflow_signed, /* complain_on_overflow */
1397 0, /* special_function */
1398 "DISP32", /* name */
1399 TRUE, /* partial_inplace */
1400 0xffffffff, /* src_mask */
1401 0xffffffff, /* dst_mask */
1402 TRUE); /* pcrel_offset */
1404 static void
1405 i386_mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
1406 long *reloc_count_ptr, char *contents,
1407 bfd_size_type contents_size)
1409 long reloc_count, i;
1410 arelent **relocs;
1412 reloc_count = *reloc_count_ptr;
1413 relocs = *relocs_ptr;
1414 for (i = 0; i < reloc_count; i++)
1416 arelent *rel;
1417 asymbol *sym;
1418 bfd_size_type address;
1419 bfd_vma addend;
1421 rel = *relocs++;
1422 sym = *rel->sym_ptr_ptr;
1424 /* We're moving the relocs from the input section to the output
1425 section, so we must adjust the address accordingly. */
1426 address = rel->address;
1427 rel->address += insec->output_offset;
1429 /* Note that no serious harm will ensue if we fail to change a
1430 reloc. The backend will fail when writing out the reloc. */
1432 /* Make sure this reloc is within the data we have. We use only
1433 4 byte relocs here, so we insist on having 4 bytes. */
1434 if (address + 4 > contents_size)
1435 continue;
1437 /* A PC relative reloc entirely within a single section is
1438 completely unnecessary. This can be generated by ld -r. */
1439 if (sym == insec->symbol
1440 && rel->howto != NULL
1441 && rel->howto->pc_relative
1442 && ! rel->howto->pcrel_offset)
1444 --*reloc_count_ptr;
1445 --relocs;
1446 memmove (relocs, relocs + 1,
1447 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1448 continue;
1451 /* Get the amount the relocation will add in. */
1452 addend = rel->addend + sym->value;
1454 /* NetWare doesn't support PC relative relocs against defined
1455 symbols, so we have to eliminate them by doing the relocation
1456 now. We can only do this if the reloc is within a single
1457 section. */
1458 if (rel->howto != NULL
1459 && rel->howto->pc_relative
1460 && bfd_get_section (sym) == insec->output_section)
1462 bfd_vma val;
1464 if (rel->howto->pcrel_offset)
1465 addend -= address;
1467 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1468 val += addend;
1469 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1471 --*reloc_count_ptr;
1472 --relocs;
1473 memmove (relocs, relocs + 1,
1474 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1475 continue;
1478 /* NetWare doesn't support reloc addends, so we get rid of them
1479 here by simply adding them into the object data. We handle
1480 the symbol value, if any, the same way. */
1481 if (addend != 0
1482 && rel->howto != NULL
1483 && rel->howto->rightshift == 0
1484 && rel->howto->size == 2
1485 && rel->howto->bitsize == 32
1486 && rel->howto->bitpos == 0
1487 && rel->howto->src_mask == 0xffffffff
1488 && rel->howto->dst_mask == 0xffffffff)
1490 bfd_vma val;
1492 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1493 val += addend;
1494 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1496 /* Adjust the reloc for the changes we just made. */
1497 rel->addend = 0;
1498 if (! bfd_is_und_section (bfd_get_section (sym)))
1499 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
1502 /* NetWare uses a reloc with pcrel_offset set. We adjust
1503 pc_relative relocs accordingly. We are going to change the
1504 howto field, so we can only do this if the current one is
1505 compatible. We should check that special_function is NULL
1506 here, but at the moment coff-i386 uses a special_function
1507 which does not affect what we are doing here. */
1508 if (rel->howto != NULL
1509 && rel->howto->pc_relative
1510 && ! rel->howto->pcrel_offset
1511 && rel->howto->rightshift == 0
1512 && rel->howto->size == 2
1513 && rel->howto->bitsize == 32
1514 && rel->howto->bitpos == 0
1515 && rel->howto->src_mask == 0xffffffff
1516 && rel->howto->dst_mask == 0xffffffff)
1518 bfd_vma val;
1520 /* When pcrel_offset is not set, it means that the negative
1521 of the address of the memory location is stored in the
1522 memory location. We must add it back in. */
1523 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1524 val += address;
1525 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1527 /* We must change to a new howto. */
1528 rel->howto = &nlm_i386_pcrel_howto;
1533 #endif /* NLMCONV_I386 */
1535 #ifdef NLMCONV_ALPHA
1537 /* On the Alpha the first reloc for every section must be a special
1538 relocs which hold the GP address. Also, the first reloc in the
1539 file must be a special reloc which holds the address of the .lita
1540 section. */
1542 static reloc_howto_type nlm32_alpha_nw_howto =
1543 HOWTO (ALPHA_R_NW_RELOC, /* type */
1544 0, /* rightshift */
1545 0, /* size (0 = byte, 1 = short, 2 = long) */
1546 0, /* bitsize */
1547 FALSE, /* pc_relative */
1548 0, /* bitpos */
1549 complain_overflow_dont, /* complain_on_overflow */
1550 0, /* special_function */
1551 "NW_RELOC", /* name */
1552 FALSE, /* partial_inplace */
1553 0, /* src_mask */
1554 0, /* dst_mask */
1555 FALSE); /* pcrel_offset */
1557 static void
1558 alpha_mangle_relocs (bfd *outbfd, asection *insec,
1559 arelent ***relocs_ptr, long *reloc_count_ptr,
1560 char *contents ATTRIBUTE_UNUSED,
1561 bfd_size_type contents_size ATTRIBUTE_UNUSED)
1563 long old_reloc_count;
1564 arelent **old_relocs;
1565 arelent **relocs;
1567 old_reloc_count = *reloc_count_ptr;
1568 old_relocs = *relocs_ptr;
1569 relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
1570 *relocs_ptr = relocs;
1572 if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
1574 bfd *inbfd;
1575 asection *lita_section;
1577 inbfd = insec->owner;
1578 lita_section = bfd_get_section_by_name (inbfd, _LITA);
1579 if (lita_section != (asection *) NULL)
1581 nlm_alpha_backend_data (outbfd)->lita_address =
1582 bfd_get_section_vma (inbfd, lita_section);
1583 nlm_alpha_backend_data (outbfd)->lita_size =
1584 bfd_section_size (inbfd, lita_section);
1586 else
1588 /* Avoid outputting this reloc again. */
1589 nlm_alpha_backend_data (outbfd)->lita_address = 4;
1592 *relocs = (arelent *) xmalloc (sizeof (arelent));
1593 (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1594 (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
1595 (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
1596 (*relocs)->howto = &nlm32_alpha_nw_howto;
1597 ++relocs;
1598 ++(*reloc_count_ptr);
1601 /* Get the GP value from bfd. */
1602 if (nlm_alpha_backend_data (outbfd)->gp == 0)
1603 nlm_alpha_backend_data (outbfd)->gp =
1604 bfd_ecoff_get_gp_value (insec->owner);
1606 *relocs = (arelent *) xmalloc (sizeof (arelent));
1607 (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1608 (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
1609 (*relocs)->addend = 0;
1610 (*relocs)->howto = &nlm32_alpha_nw_howto;
1611 ++relocs;
1612 ++(*reloc_count_ptr);
1614 memcpy (relocs, old_relocs, (size_t) old_reloc_count * sizeof (arelent *));
1615 relocs[old_reloc_count] = (arelent *) NULL;
1617 free (old_relocs);
1619 if (insec->output_offset != 0)
1621 bfd_size_type i;
1623 for (i = 0; i < (bfd_size_type) old_reloc_count; i++, relocs++)
1624 (*relocs)->address += insec->output_offset;
1628 #endif /* NLMCONV_ALPHA */
1630 #ifdef NLMCONV_POWERPC
1632 /* We keep a linked list of stubs which we must build. Because BFD
1633 requires us to know the sizes of all sections before we can set the
1634 contents of any, we must figure out which stubs we want to build
1635 before we can actually build any of them. */
1637 struct powerpc_stub
1639 /* Next stub in linked list. */
1640 struct powerpc_stub *next;
1642 /* Symbol whose value is the start of the stub. This is a symbol
1643 whose name begins with `.'. */
1644 asymbol *start;
1646 /* Symbol we are going to create a reloc against. This is a symbol
1647 with the same name as START but without the leading `.'. */
1648 asymbol *reloc;
1650 /* The TOC index for this stub. This is the index into the TOC
1651 section at which the reloc is created. */
1652 unsigned int toc_index;
1655 /* The linked list of stubs. */
1657 static struct powerpc_stub *powerpc_stubs;
1659 /* This is what a stub looks like. The first instruction will get
1660 adjusted with the correct TOC index. */
1662 static unsigned long powerpc_stub_insns[] =
1664 0x81820000, /* lwz r12,0(r2) */
1665 0x90410014, /* stw r2,20(r1) */
1666 0x800c0000, /* lwz r0,0(r12) */
1667 0x804c0004, /* lwz r2,r(r12) */
1668 0x7c0903a6, /* mtctr r0 */
1669 0x4e800420, /* bctr */
1670 0, /* Traceback table. */
1671 0xc8000,
1675 #define POWERPC_STUB_INSN_COUNT \
1676 (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
1678 #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
1680 /* Each stub uses a four byte TOC entry. */
1681 #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
1683 /* The original size of the .got section. */
1684 static bfd_size_type powerpc_initial_got_size;
1686 /* Look for all undefined symbols beginning with `.', and prepare to
1687 build a stub for each one. */
1689 static void
1690 powerpc_build_stubs (bfd *inbfd, bfd *outbfd ATTRIBUTE_UNUSED,
1691 asymbol ***symbols_ptr, long *symcount_ptr)
1693 asection *stub_sec;
1694 asection *got_sec;
1695 unsigned int got_base;
1696 long i;
1697 long symcount;
1698 long stubcount;
1700 /* Make a section to hold stubs. We don't set SEC_HAS_CONTENTS for
1701 the section to prevent copy_sections from reading from it. */
1702 stub_sec = bfd_make_section (inbfd, ".stubs");
1703 if (stub_sec == (asection *) NULL
1704 || ! bfd_set_section_flags (inbfd, stub_sec,
1705 (SEC_CODE
1706 | SEC_RELOC
1707 | SEC_ALLOC
1708 | SEC_LOAD))
1709 || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
1710 bfd_fatal (".stubs");
1712 /* Get the TOC section, which is named .got. */
1713 got_sec = bfd_get_section_by_name (inbfd, ".got");
1714 if (got_sec == (asection *) NULL)
1716 got_sec = bfd_make_section (inbfd, ".got");
1717 if (got_sec == (asection *) NULL
1718 || ! bfd_set_section_flags (inbfd, got_sec,
1719 (SEC_DATA
1720 | SEC_RELOC
1721 | SEC_ALLOC
1722 | SEC_LOAD
1723 | SEC_HAS_CONTENTS))
1724 || ! bfd_set_section_alignment (inbfd, got_sec, 2))
1725 bfd_fatal (".got");
1728 powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
1729 got_base = powerpc_initial_got_size;
1730 got_base = (got_base + 3) &~ 3;
1732 stubcount = 0;
1734 symcount = *symcount_ptr;
1735 for (i = 0; i < symcount; i++)
1737 asymbol *sym;
1738 asymbol *newsym;
1739 char *newname;
1740 struct powerpc_stub *item;
1742 sym = (*symbols_ptr)[i];
1744 /* We must make a stub for every undefined symbol whose name
1745 starts with '.'. */
1746 if (bfd_asymbol_name (sym)[0] != '.'
1747 || ! bfd_is_und_section (bfd_get_section (sym)))
1748 continue;
1750 /* Make a new undefined symbol with the same name but without
1751 the leading `.'. */
1752 newsym = xmalloc (sizeof (asymbol));
1753 *newsym = *sym;
1754 newname = xmalloc (strlen (bfd_asymbol_name (sym)));
1755 strcpy (newname, bfd_asymbol_name (sym) + 1);
1756 newsym->name = newname;
1758 /* Define the `.' symbol to be in the stub section. */
1759 sym->section = stub_sec;
1760 sym->value = stubcount * POWERPC_STUB_SIZE;
1761 /* We set the BSF_DYNAMIC flag here so that we can check it when
1762 we are mangling relocs. FIXME: This is a hack. */
1763 sym->flags = BSF_LOCAL | BSF_DYNAMIC;
1765 /* Add this stub to the linked list. */
1766 item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
1767 item->start = sym;
1768 item->reloc = newsym;
1769 item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
1771 item->next = powerpc_stubs;
1772 powerpc_stubs = item;
1774 ++stubcount;
1777 if (stubcount > 0)
1779 asymbol **s;
1780 struct powerpc_stub *l;
1782 /* Add the new symbols we just created to the symbol table. */
1783 *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
1784 ((symcount + stubcount)
1785 * sizeof (asymbol)));
1786 *symcount_ptr += stubcount;
1787 s = &(*symbols_ptr)[symcount];
1788 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1789 *s++ = l->reloc;
1791 /* Set the size of the .stubs section and increase the size of
1792 the .got section. */
1793 if (! bfd_set_section_size (inbfd, stub_sec,
1794 stubcount * POWERPC_STUB_SIZE)
1795 || ! bfd_set_section_size (inbfd, got_sec,
1796 (got_base
1797 + (stubcount
1798 * POWERPC_STUB_TOC_ENTRY_SIZE))))
1799 bfd_fatal (_("stub section sizes"));
1803 /* Resolve all the stubs for PowerPC NetWare. We fill in the contents
1804 of the output section, and create new relocs in the TOC. */
1806 static void
1807 powerpc_resolve_stubs (bfd *inbfd, bfd *outbfd)
1809 bfd_byte buf[POWERPC_STUB_SIZE];
1810 unsigned int i;
1811 unsigned int stubcount;
1812 arelent **relocs;
1813 asection *got_sec;
1814 arelent **r;
1815 struct powerpc_stub *l;
1817 if (powerpc_stubs == (struct powerpc_stub *) NULL)
1818 return;
1820 for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
1821 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
1823 got_sec = bfd_get_section_by_name (inbfd, ".got");
1824 assert (got_sec != (asection *) NULL);
1825 assert (got_sec->output_section->orelocation == (arelent **) NULL);
1827 stubcount = 0;
1828 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1829 ++stubcount;
1830 relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
1832 r = relocs;
1833 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1835 arelent *reloc;
1837 /* Adjust the first instruction to use the right TOC index. */
1838 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
1840 /* Write this stub out. */
1841 if (! bfd_set_section_contents (outbfd,
1842 bfd_get_section (l->start),
1843 buf,
1844 l->start->value,
1845 POWERPC_STUB_SIZE))
1846 bfd_fatal (_("writing stub"));
1848 /* Create a new reloc for the TOC entry. */
1849 reloc = (arelent *) xmalloc (sizeof (arelent));
1850 reloc->sym_ptr_ptr = &l->reloc;
1851 reloc->address = l->toc_index + got_sec->output_offset;
1852 reloc->addend = 0;
1853 reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
1855 *r++ = reloc;
1858 bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
1861 /* Adjust relocation entries for PowerPC NetWare. We do not output
1862 TOC relocations. The object code already contains the offset from
1863 the TOC pointer. When the function is called, the TOC register,
1864 r2, will be set to the correct TOC value, so there is no need for
1865 any further reloc. */
1867 static void
1868 powerpc_mangle_relocs (bfd *outbfd, asection *insec,
1869 arelent ***relocs_ptr,
1870 long *reloc_count_ptr, char *contents,
1871 bfd_size_type contents_size ATTRIBUTE_UNUSED)
1873 reloc_howto_type *toc_howto;
1874 long reloc_count;
1875 arelent **relocs;
1876 long i;
1878 toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
1879 if (toc_howto == (reloc_howto_type *) NULL)
1880 abort ();
1882 /* If this is the .got section, clear out all the contents beyond
1883 the initial size. We must do this here because copy_sections is
1884 going to write out whatever we return in the contents field. */
1885 if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
1886 memset (contents + powerpc_initial_got_size, 0,
1887 (size_t) (bfd_get_section_size (insec) - powerpc_initial_got_size));
1889 reloc_count = *reloc_count_ptr;
1890 relocs = *relocs_ptr;
1891 for (i = 0; i < reloc_count; i++)
1893 arelent *rel;
1894 asymbol *sym;
1895 bfd_vma sym_value;
1897 rel = *relocs++;
1898 sym = *rel->sym_ptr_ptr;
1900 /* Convert any relocs against the .bss section into relocs
1901 against the .data section. */
1902 if (strcmp (bfd_get_section_name (outbfd, bfd_get_section (sym)),
1903 NLM_UNINITIALIZED_DATA_NAME) == 0)
1905 asection *datasec;
1907 datasec = bfd_get_section_by_name (outbfd,
1908 NLM_INITIALIZED_DATA_NAME);
1909 if (datasec != NULL)
1911 rel->addend += (bfd_get_section_vma (outbfd,
1912 bfd_get_section (sym))
1913 + sym->value);
1914 rel->sym_ptr_ptr = datasec->symbol_ptr_ptr;
1915 sym = *rel->sym_ptr_ptr;
1919 /* We must be able to resolve all PC relative relocs at this
1920 point. If we get a branch to an undefined symbol we build a
1921 stub, since NetWare will resolve undefined symbols into a
1922 pointer to a function descriptor. */
1923 if (rel->howto->pc_relative)
1925 /* This check for whether a symbol is in the same section as
1926 the reloc will be wrong if there is a PC relative reloc
1927 between two sections both of which were placed in the
1928 same output section. This should not happen. */
1929 if (bfd_get_section (sym) != insec->output_section)
1930 non_fatal (_("unresolved PC relative reloc against %s"),
1931 bfd_asymbol_name (sym));
1932 else
1934 bfd_vma val;
1936 assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
1937 val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
1938 val = ((val &~ rel->howto->dst_mask)
1939 | (((val & rel->howto->src_mask)
1940 + (sym->value - rel->address)
1941 + rel->addend)
1942 & rel->howto->dst_mask));
1943 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
1945 /* If this reloc is against an stubbed symbol and the
1946 next instruction is
1947 cror 31,31,31
1948 then we replace the next instruction with
1949 lwz r2,20(r1)
1950 This reloads the TOC pointer after a stub call. */
1951 if (bfd_asymbol_name (sym)[0] == '.'
1952 && (sym->flags & BSF_DYNAMIC) != 0
1953 && (bfd_get_32 (outbfd,
1954 (bfd_byte *) contents + rel->address + 4)
1955 == 0x4ffffb82)) /* cror 31,31,31 */
1956 bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
1957 (bfd_byte *) contents + rel->address + 4);
1959 --*reloc_count_ptr;
1960 --relocs;
1961 memmove (relocs, relocs + 1,
1962 (size_t) ((reloc_count - 1) * sizeof (arelent *)));
1963 continue;
1967 /* When considering a TOC reloc, we do not want to include the
1968 symbol value. The symbol will be start of the TOC section
1969 (which is named .got). We do want to include the addend. */
1970 if (rel->howto == toc_howto)
1971 sym_value = 0;
1972 else
1973 sym_value = sym->value;
1975 /* If this is a relocation against a symbol with a value, or
1976 there is a reloc addend, we need to update the addend in the
1977 object file. */
1978 if (sym_value + rel->addend != 0)
1980 bfd_vma val;
1982 switch (rel->howto->size)
1984 case 1:
1985 val = bfd_get_16 (outbfd,
1986 (bfd_byte *) contents + rel->address);
1987 val = ((val &~ rel->howto->dst_mask)
1988 | (((val & rel->howto->src_mask)
1989 + sym_value
1990 + rel->addend)
1991 & rel->howto->dst_mask));
1992 if ((bfd_signed_vma) val < - 0x8000
1993 || (bfd_signed_vma) val >= 0x8000)
1994 non_fatal (_("overflow when adjusting relocation against %s"),
1995 bfd_asymbol_name (sym));
1996 bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
1997 break;
1999 case 2:
2000 val = bfd_get_32 (outbfd,
2001 (bfd_byte *) contents + rel->address);
2002 val = ((val &~ rel->howto->dst_mask)
2003 | (((val & rel->howto->src_mask)
2004 + sym_value
2005 + rel->addend)
2006 & rel->howto->dst_mask));
2007 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
2008 break;
2010 default:
2011 abort ();
2014 if (! bfd_is_und_section (bfd_get_section (sym)))
2015 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
2016 rel->addend = 0;
2019 /* Now that we have incorporated the addend, remove any TOC
2020 relocs. */
2021 if (rel->howto == toc_howto)
2023 --*reloc_count_ptr;
2024 --relocs;
2025 memmove (relocs, relocs + 1,
2026 (size_t) ((reloc_count - i) * sizeof (arelent *)));
2027 continue;
2030 rel->address += insec->output_offset;
2034 #endif /* NLMCONV_POWERPC */
2036 /* Name of linker. */
2037 #ifndef LD_NAME
2038 #define LD_NAME "ld"
2039 #endif
2041 /* The user has specified several input files. Invoke the linker to
2042 link them all together, and convert and delete the resulting output
2043 file. */
2045 static char *
2046 link_inputs (struct string_list *inputs, char *ld, char * map_file)
2048 size_t c;
2049 struct string_list *q;
2050 char **argv;
2051 size_t i;
2052 int pid;
2053 int status;
2054 char *errfmt;
2055 char *errarg;
2057 c = 0;
2058 for (q = inputs; q != NULL; q = q->next)
2059 ++c;
2061 argv = (char **) alloca ((c + 7) * sizeof (char *));
2063 #ifndef __MSDOS__
2064 if (ld == NULL)
2066 char *p;
2068 /* Find the linker to invoke based on how nlmconv was run. */
2069 p = program_name + strlen (program_name);
2070 while (p != program_name)
2072 if (p[-1] == '/')
2074 ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
2075 memcpy (ld, program_name, p - program_name);
2076 strcpy (ld + (p - program_name), LD_NAME);
2077 break;
2079 --p;
2082 #endif
2084 if (ld == NULL)
2085 ld = (char *) LD_NAME;
2087 unlink_on_exit = make_temp_file (".O");
2089 argv[0] = ld;
2090 argv[1] = (char *) "-Ur";
2091 argv[2] = (char *) "-o";
2092 argv[3] = unlink_on_exit;
2093 /* If we have been given the name of a mapfile and that
2094 name is not 'stderr' then pass it on to the linker. */
2095 if (map_file
2096 && * map_file
2097 && strcmp (map_file, "stderr") == 0)
2099 argv[4] = (char *) "-Map";
2100 argv[5] = map_file;
2101 i = 6;
2103 else
2104 i = 4;
2106 for (q = inputs; q != NULL; q = q->next, i++)
2107 argv[i] = q->string;
2108 argv[i] = NULL;
2110 if (debug)
2112 for (i = 0; argv[i] != NULL; i++)
2113 fprintf (stderr, " %s", argv[i]);
2114 fprintf (stderr, "\n");
2117 pid = pexecute (ld, argv, program_name, (char *) NULL, &errfmt, &errarg,
2118 PEXECUTE_SEARCH | PEXECUTE_ONE);
2119 if (pid == -1)
2121 fprintf (stderr, _("%s: execution of %s failed: "), program_name, ld);
2122 fprintf (stderr, errfmt, errarg);
2123 unlink (unlink_on_exit);
2124 exit (1);
2127 if (pwait (pid, &status, 0) < 0)
2129 perror ("pwait");
2130 unlink (unlink_on_exit);
2131 exit (1);
2134 if (status != 0)
2136 non_fatal (_("Execution of %s failed"), ld);
2137 unlink (unlink_on_exit);
2138 exit (1);
2141 return unlink_on_exit;