* nlmconv.c (setup_sections): Make sure that we align the
[external/binutils.git] / binutils / nlmconv.c
1 /* nlmconv.c -- NLM conversion program
2    Copyright (C) 1993 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
6 This program 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 2 of the License, or
9 (at your option) any later version.
10
11 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* Written by Ian Lance Taylor <ian@cygnus.com>.
21
22    This program can be used to convert any appropriate object file
23    into a NetWare Loadable Module (an NLM).  It will accept a linker
24    specification file which is identical to that accepted by the
25    NetWare linker, NLMLINK, except that the INPUT command, normally
26    used to give a list of object files to link together, is not used.
27    This program will convert only a single object file.  */
28
29 #include <ansidecl.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/file.h>
36 #include <assert.h>
37 #include <getopt.h>
38 #include <bfd.h>
39 #include "sysdep.h"
40 #include "bucomm.h"
41 /* Internal BFD NLM header.  */
42 #include "libnlm.h"
43 #include "nlmconv.h"
44
45 /* Needed for Alpha support.  */
46 #include "coff/sym.h"
47 #include "coff/ecoff.h"
48
49 /* If strerror is just a macro, we want to use the one from libiberty
50    since it will handle undefined values.  */
51 #undef strerror
52 extern char *strerror ();
53
54 #ifndef localtime
55 extern struct tm *localtime ();
56 #endif
57
58 #ifndef getenv
59 extern char *getenv ();
60 #endif
61
62 #ifndef SEEK_SET
63 #define SEEK_SET 0
64 #endif
65
66 #ifndef R_OK
67 #define R_OK 4
68 #define W_OK 2
69 #define X_OK 1
70 #endif
71 \f
72 /* Global variables.  */
73
74 /* The name used to invoke the program.  */
75 char *program_name;
76
77 /* The version number.  */
78 extern char *program_version;
79
80 /* Local variables.  */
81
82 /* Whether to print out debugging information (currently just controls
83    whether it prints the linker command if there is one).  */
84 static int debug;
85
86 /* The symbol table.  */
87 static asymbol **symbols;
88
89 /* A temporary file name to be unlinked on exit.  Actually, for most
90    errors, we leave it around.  It's not clear whether that is helpful
91    or not.  */
92 static char *unlink_on_exit;
93
94 /* The list of long options.  */
95 static struct option long_options[] =
96 {
97   { "debug", no_argument, 0, 'd' },
98   { "header-file", required_argument, 0, 'T' },
99   { "help", no_argument, 0, 'h' },
100   { "input-target", required_argument, 0, 'I' },
101   { "input-format", required_argument, 0, 'I' }, /* Obsolete */
102   { "linker", required_argument, 0, 'l' },
103   { "output-target", required_argument, 0, 'O' },
104   { "output-format", required_argument, 0, 'O' }, /* Obsolete */
105   { "version", no_argument, 0, 'V' },
106   { NULL, no_argument, 0, 0 }
107 };
108
109 /* Local routines.  */
110
111 static void show_help PARAMS ((void));
112 static void show_usage PARAMS ((FILE *, int));
113 static const char *select_output_format PARAMS ((enum bfd_architecture,
114                                                  unsigned long, boolean));
115 static void setup_sections PARAMS ((bfd *, asection *, PTR));
116 static void copy_sections PARAMS ((bfd *, asection *, PTR));
117 static void mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
118                                    bfd_size_type *, char *,
119                                    bfd_size_type));
120 static void i386_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
121                                         bfd_size_type *, char *,
122                                         bfd_size_type));
123 static void alpha_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
124                                          bfd_size_type *, char *,
125                                          bfd_size_type));
126 static void default_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
127                                            bfd_size_type *, char *,
128                                            bfd_size_type));
129 static char *link_inputs PARAMS ((struct string_list *, char *));
130 static const char *choose_temp_base_try PARAMS ((const char *,
131                                                  const char *));
132 static void choose_temp_base PARAMS ((void));
133 static int pexecute PARAMS ((char *, char *[]));
134 \f
135 /* The main routine.  */
136
137 int
138 main (argc, argv)
139      int argc;
140      char **argv;
141 {
142   int opt;
143   char *input_file = NULL;
144   const char *input_format = NULL;
145   const char *output_format = NULL;
146   const char *header_file = NULL;
147   char *ld_arg = NULL;
148   Nlm_Internal_Fixed_Header fixed_hdr_struct;
149   Nlm_Internal_Variable_Header var_hdr_struct;
150   Nlm_Internal_Version_Header version_hdr_struct;
151   Nlm_Internal_Copyright_Header copyright_hdr_struct;
152   Nlm_Internal_Extended_Header extended_hdr_struct;
153   bfd *inbfd;
154   bfd *outbfd;
155   asymbol **newsyms, **outsyms;
156   unsigned int symcount, newsymalloc, newsymcount;
157   asection *bss_sec, *data_sec;
158   bfd_vma vma;
159   bfd_size_type align;
160   asymbol *endsym;
161   unsigned int i;
162   char inlead, outlead;
163   boolean gotstart, gotexit, gotcheck;
164   struct stat st;
165   FILE *custom_data, *help_data, *message_data, *rpc_data, *shared_data;
166   size_t custom_size, help_size, message_size, module_size, rpc_size;
167   asection *custom_section, *help_section, *message_section, *module_section;
168   asection *rpc_section, *shared_section;
169   bfd *sharedbfd;
170   size_t shared_offset, shared_size;
171   Nlm_Internal_Fixed_Header sharedhdr;
172   int len;
173   char *modname;
174
175   program_name = argv[0];
176
177   bfd_init ();
178
179   while ((opt = getopt_long (argc, argv, "dhI:l:O:T:V", long_options,
180                              (int *) NULL))
181          != EOF)
182     {
183       switch (opt)
184         {
185         case 'd':
186           debug = 1;
187           break;
188         case 'h':
189           show_help ();
190           /*NOTREACHED*/
191         case 'I':
192           input_format = optarg;
193           break;
194         case 'l':
195           ld_arg = optarg;
196           break;
197         case 'O':
198           output_format = optarg;
199           break;
200         case 'T':
201           header_file = optarg;
202           break;
203         case 'V':
204           printf ("GNU %s version %s\n", program_name, program_version);
205           exit (0);
206           /*NOTREACHED*/
207         case 0:
208           break;
209         default:
210           show_usage (stderr, 1);
211           /*NOTREACHED*/
212         }
213     }
214
215   /* The input and output files may be named on the command line.  */
216   output_file = NULL;
217   if (optind < argc)
218     {
219       input_file = argv[optind];
220       ++optind;
221       if (optind < argc)
222         {
223           output_file = argv[optind];
224           ++optind;
225           if (optind < argc)
226             show_usage (stderr, 1);
227           if (strcmp (input_file, output_file) == 0)
228             {
229               fprintf (stderr,
230                        "%s: input and output files must be different\n",
231                        program_name);
232               exit (1);
233             }
234         }
235     }
236
237   /* Initialize the header information to default values.  */
238   fixed_hdr = &fixed_hdr_struct;
239   memset ((PTR) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
240   var_hdr = &var_hdr_struct;
241   memset ((PTR) &var_hdr_struct, 0, sizeof var_hdr_struct);
242   version_hdr = &version_hdr_struct;
243   memset ((PTR) &version_hdr_struct, 0, sizeof version_hdr_struct);
244   copyright_hdr = &copyright_hdr_struct;
245   memset ((PTR) &copyright_hdr_struct, 0, sizeof copyright_hdr_struct);
246   extended_hdr = &extended_hdr_struct;
247   memset ((PTR) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
248   check_procedure = NULL;
249   custom_file = NULL;
250   debug_info = false;
251   exit_procedure = "_Stop";
252   export_symbols = NULL;
253   map_file = NULL;
254   full_map = false;
255   help_file = NULL;
256   import_symbols = NULL;
257   message_file = NULL;
258   modules = NULL;
259   sharelib_file = NULL;
260   start_procedure = "_Prelude";
261   verbose = false;
262   rpc_file = NULL;
263
264   parse_errors = 0;
265
266   /* Parse the header file (if there is one).  */
267   if (header_file != NULL)
268     {
269       if (! nlmlex_file (header_file)
270           || yyparse () != 0
271           || parse_errors != 0)
272         exit (1);
273     }
274
275   if (input_files != NULL)
276     {
277       if (input_file != NULL)
278         {
279           fprintf (stderr,
280                    "%s: input file named both on command line and with INPUT\n",
281                    program_name);
282           exit (1);
283         }
284       if (input_files->next == NULL)
285         input_file = input_files->string;
286       else
287         input_file = link_inputs (input_files, ld_arg);
288     }
289   else if (input_file == NULL)
290     {
291       fprintf (stderr, "%s: no input file\n", program_name);
292       show_usage (stderr, 1);
293     }
294
295   inbfd = bfd_openr (input_file, input_format);
296   if (inbfd == NULL)
297     bfd_fatal (input_file);
298
299   if (! bfd_check_format (inbfd, bfd_object))
300     bfd_fatal (input_file);
301
302   if (output_format == NULL)
303     output_format = select_output_format (bfd_get_arch (inbfd),
304                                           bfd_get_mach (inbfd),
305                                           inbfd->xvec->byteorder_big_p);
306
307   assert (output_format != NULL);
308
309   /* Use the output file named on the command line if it exists.
310      Otherwise use the file named in the OUTPUT statement.  */
311   if (output_file == NULL)
312     {
313       fprintf (stderr, "%s: no name for output file\n",
314                program_name);
315       show_usage (stderr, 1);
316     }
317
318   outbfd = bfd_openw (output_file, output_format);
319   if (outbfd == NULL)
320     bfd_fatal (output_file);
321   if (! bfd_set_format (outbfd, bfd_object))
322     bfd_fatal (output_file);
323
324   assert (outbfd->xvec->flavour == bfd_target_nlm_flavour);
325
326   if (bfd_arch_get_compatible (inbfd, outbfd) == NULL)
327     fprintf (stderr,
328              "%s: warning:input and output formats are not compatible\n",
329              program_name);
330
331   /* Move the values read from the command file into outbfd.  */
332   *nlm_fixed_header (outbfd) = fixed_hdr_struct;
333   *nlm_variable_header (outbfd) = var_hdr_struct;
334   *nlm_version_header (outbfd) = version_hdr_struct;
335   *nlm_copyright_header (outbfd) = copyright_hdr_struct;
336   *nlm_extended_header (outbfd) = extended_hdr_struct;
337
338   /* Start copying the input BFD to the output BFD.  */
339   if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
340     bfd_fatal (bfd_get_filename (outbfd));
341
342   symbols = (asymbol **) xmalloc (get_symtab_upper_bound (inbfd));
343   symcount = bfd_canonicalize_symtab (inbfd, symbols);
344
345   /* Make sure we have a .bss section.  */
346   bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
347   if (bss_sec == NULL)
348     {
349       bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
350       if (bss_sec == NULL
351           || ! bfd_set_section_flags (outbfd, bss_sec, SEC_ALLOC)
352           || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
353         bfd_fatal ("make .bss section");
354     }
355
356   /* Set up the sections.  */
357   bfd_map_over_sections (inbfd, setup_sections, (PTR) outbfd);
358
359   /* The .bss section immediately follows the .data section.  */
360   data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
361   if (data_sec != NULL)
362     {
363       bfd_size_type add;
364
365       vma = bfd_get_section_size_before_reloc (data_sec);
366       align = 1 << bss_sec->alignment_power;
367       add = ((vma + align - 1) &~ (align - 1)) - vma;
368       vma += add;
369       if (! bfd_set_section_vma (outbfd, bss_sec, vma))
370         bfd_fatal ("set .bss vma");
371       if (add != 0)
372         {
373           bfd_size_type data_size;
374
375           data_size = bfd_get_section_size_before_reloc (data_sec);
376           if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
377             bfd_fatal ("set .data size");
378         }
379     }
380
381   /* Adjust symbol information.  */
382   inlead = bfd_get_symbol_leading_char (inbfd);
383   outlead = bfd_get_symbol_leading_char (outbfd);
384   gotstart = false;
385   gotexit = false;
386   gotcheck = false;
387   newsymalloc = 10;
388   newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
389   newsymcount = 0;
390   endsym = NULL;
391   for (i = 0; i < symcount; i++)
392     {
393       register asymbol *sym;
394
395       sym = symbols[i];
396
397       /* Add or remove a leading underscore.  */
398       if (inlead != outlead)
399         {
400           if (inlead != '\0')
401             {
402               if (bfd_asymbol_name (sym)[0] == inlead)
403                 {
404                   if (outlead == '\0')
405                     ++sym->name;
406                   else
407                     {
408                       char *new;
409
410                       new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
411                       new[0] = outlead;
412                       strcpy (new + 1, bfd_asymbol_name (sym) + 1);
413                       sym->name = new;
414                     }
415                 }
416             }
417           else
418             {
419               char *new;
420
421               new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
422               new[0] = outlead;
423               strcpy (new + 1, bfd_asymbol_name (sym));
424               sym->name = new;
425             }
426         }
427
428       /* NLM's have an uninitialized data section, but they do not
429          have a common section in the Unix sense.  Move all common
430          symbols into the .bss section, and mark them as exported.  */
431       if (bfd_is_com_section (bfd_get_section (sym)))
432         {
433           bfd_vma size;
434
435           sym->section = bss_sec;
436           size = sym->value;
437           sym->value = bss_sec->_raw_size;
438           bss_sec->_raw_size += size;
439           align = 1 << bss_sec->alignment_power;
440           bss_sec->_raw_size = (bss_sec->_raw_size + align - 1) &~ (align - 1);
441           sym->flags |= BSF_EXPORT | BSF_GLOBAL;
442         }
443       else if (bfd_get_section (sym)->output_section != NULL)
444         {
445           /* Move the symbol into the output section.  */
446           sym->value += bfd_get_section (sym)->output_offset;
447           sym->section = bfd_get_section (sym)->output_section;
448           /* This is no longer a section symbol.  */
449           sym->flags &=~ BSF_SECTION_SYM;
450         }
451
452       /* Force _edata and _end to be defined.  This would normally be
453          done by the linker, but the manipulation of the common
454          symbols will confuse it.  */
455       if (bfd_asymbol_name (sym)[0] == '_'
456           && bfd_get_section (sym) == &bfd_und_section)
457         {
458           if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
459             {
460               sym->section = bss_sec;
461               sym->value = 0;
462             }
463           if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
464             {
465               sym->section = bss_sec;
466               endsym = sym;
467             }
468         }
469
470       /* If this is a global symbol, check the export list.  */
471       if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
472         {
473           register struct string_list *l;
474           int found_simple;
475
476           /* Unfortunately, a symbol can appear multiple times on the
477              export list, with and without prefixes.  */
478           found_simple = 0;
479           for (l = export_symbols; l != NULL; l = l->next)
480             {
481               if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
482                 found_simple = 1;
483               else
484                 {
485                   char *zbase;
486
487                   zbase = strchr (l->string, '@');
488                   if (zbase != NULL
489                       && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
490                     {
491                       /* We must add a symbol with this prefix.  */
492                       if (newsymcount >= newsymalloc)
493                         {
494                           newsymalloc += 10;
495                           newsyms = ((asymbol **)
496                                      xrealloc (newsyms,
497                                                (newsymalloc
498                                                 * sizeof (asymbol *))));
499                         }
500                       newsyms[newsymcount] =
501                         (asymbol *) xmalloc (sizeof (asymbol));
502                       *newsyms[newsymcount] = *sym;
503                       newsyms[newsymcount]->name = l->string;
504                       ++newsymcount;
505                     }
506                 }
507             }
508           if (! found_simple)
509             {
510               /* The unmodified symbol is actually not exported at
511                  all.  */
512               sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
513               sym->flags |= BSF_LOCAL;
514             }
515         }
516
517       /* If it's an undefined symbol, see if it's on the import list.
518          Change the prefix if necessary.  */
519       if (bfd_get_section (sym) == &bfd_und_section)
520         {
521           register struct string_list *l;
522
523           for (l = import_symbols; l != NULL; l = l->next)
524             {
525               if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
526                 break;
527               else
528                 {
529                   char *zbase;
530
531                   zbase = strchr (l->string, '@');
532                   if (zbase != NULL
533                       && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
534                     {
535                       sym->name = l->string;
536                       break;
537                     }
538                 }
539             }
540           if (l == NULL)
541             fprintf (stderr,
542                      "%s: warning: symbol %s imported but not in import list\n",
543                      program_name, bfd_asymbol_name (sym));
544         }
545         
546       /* See if it's one of the special named symbols.  */
547       if (strcmp (bfd_asymbol_name (sym), start_procedure) == 0)
548         {
549           if (! bfd_set_start_address (outbfd, bfd_asymbol_value (sym)))
550             bfd_fatal ("set start address");
551           gotstart = true;
552         }
553       if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
554         {
555           nlm_fixed_header (outbfd)->exitProcedureOffset =
556             bfd_asymbol_value (sym);
557           gotexit = true;
558         }
559       if (check_procedure != NULL
560           && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
561         {
562           nlm_fixed_header (outbfd)->checkUnloadProcedureOffset =
563             bfd_asymbol_value (sym);
564           gotcheck = true;
565         }
566     }
567
568   if (endsym != NULL)
569     endsym->value = bfd_get_section_size_before_reloc (bss_sec);
570
571   if (newsymcount == 0)
572     outsyms = symbols;
573   else
574     {
575       outsyms = (asymbol **) xmalloc ((symcount + newsymcount + 1)
576                                       * sizeof (asymbol *));
577       memcpy (outsyms, symbols, symcount * sizeof (asymbol *));
578       memcpy (outsyms + symcount, newsyms, newsymcount * sizeof (asymbol *));
579       outsyms[symcount + newsymcount] = NULL;
580     }
581
582   bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
583     
584   if (! gotstart)
585     fprintf (stderr, "%s: warning: START procedure %s not defined\n",
586              program_name, start_procedure);
587   if (! gotexit)
588     fprintf (stderr, "%s: warning: EXIT procedure %s not defined\n",
589              program_name, exit_procedure);
590   if (check_procedure != NULL
591       && ! gotcheck)
592     fprintf (stderr, "%s: warning: CHECK procedure %s not defined\n",
593              program_name, check_procedure);
594
595   /* Add additional sections required for the header information.  */
596   if (custom_file != NULL)
597     {
598       custom_data = fopen (custom_file, "r");
599       if (custom_data == NULL
600           || fstat (fileno (custom_data), &st) < 0)
601         {
602           fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
603                    strerror (errno));
604           custom_file = NULL;
605         }
606       else
607         {
608           custom_size = st.st_size;
609           custom_section = bfd_make_section (outbfd, ".nlmcustom");
610           if (custom_section == NULL
611               || ! bfd_set_section_size (outbfd, custom_section, custom_size)
612               || ! bfd_set_section_flags (outbfd, custom_section,
613                                           SEC_HAS_CONTENTS))
614             bfd_fatal ("custom section");
615         }
616     }
617   if (help_file != NULL)
618     {
619       help_data = fopen (help_file, "r");
620       if (help_data == NULL
621           || fstat (fileno (help_data), &st) < 0)
622         {
623           fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
624                    strerror (errno));
625           help_file = NULL;
626         }
627       else
628         {
629           help_size = st.st_size;
630           help_section = bfd_make_section (outbfd, ".nlmhelp");
631           if (help_section == NULL
632               || ! bfd_set_section_size (outbfd, help_section, help_size)
633               || ! bfd_set_section_flags (outbfd, help_section,
634                                           SEC_HAS_CONTENTS))
635             bfd_fatal ("help section");
636           strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
637         }
638     }
639   if (message_file != NULL)
640     {
641       message_data = fopen (message_file, "r");
642       if (message_data == NULL
643           || fstat (fileno (message_data), &st) < 0)
644         {
645           fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
646                    strerror (errno));
647           message_file = NULL;
648         }
649       else
650         {
651           message_size = st.st_size;
652           message_section = bfd_make_section (outbfd, ".nlmmessages");
653           if (message_section == NULL
654               || ! bfd_set_section_size (outbfd, message_section, message_size)
655               || ! bfd_set_section_flags (outbfd, message_section,
656                                           SEC_HAS_CONTENTS))
657             bfd_fatal ("message section");
658           strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
659         }
660     }
661   if (modules != NULL)
662     {
663       struct string_list *l;
664
665       module_size = 0;
666       for (l = modules; l != NULL; l = l->next)
667         module_size += strlen (l->string) + 1;
668       module_section = bfd_make_section (outbfd, ".nlmmodules");
669       if (module_section == NULL
670           || ! bfd_set_section_size (outbfd, module_section, module_size)
671           || ! bfd_set_section_flags (outbfd, module_section,
672                                       SEC_HAS_CONTENTS))
673         bfd_fatal ("module section");
674     }
675   if (rpc_file != NULL)
676     {
677       rpc_data = fopen (rpc_file, "r");
678       if (rpc_data == NULL
679           || fstat (fileno (rpc_data), &st) < 0)
680         {
681           fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
682                    strerror (errno));
683           rpc_file = NULL;
684         }
685       else
686         {
687           rpc_size = st.st_size;
688           rpc_section = bfd_make_section (outbfd, ".nlmrpc");
689           if (rpc_section == NULL
690               || ! bfd_set_section_size (outbfd, rpc_section, rpc_size)
691               || ! bfd_set_section_flags (outbfd, rpc_section,
692                                           SEC_HAS_CONTENTS))
693             bfd_fatal ("rpc section");
694           strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
695         }
696     }
697   if (sharelib_file != NULL)
698     {
699       sharedbfd = bfd_openr (sharelib_file, output_format);
700       if (sharedbfd == NULL
701           || ! bfd_check_format (sharedbfd, bfd_object))
702         {
703           fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
704                    bfd_errmsg (bfd_error));
705           sharelib_file = NULL;
706         }
707       else
708         {
709           sharedhdr = *nlm_fixed_header (sharedbfd);
710           bfd_close (sharedbfd);
711           shared_data = fopen (sharelib_file, "r");
712           if (shared_data == NULL
713               || (fstat (fileno (shared_data), &st) < 0))
714             {
715               fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
716                        strerror (errno));
717               sharelib_file = NULL;
718             }
719           else
720             {
721               /* If we were clever, we could just copy out the
722                  sections of the shared library which we actually
723                  need.  However, we would have to figure out the sizes
724                  of the external and public information, and that can
725                  not be done without reading through them.  */
726               shared_offset = st.st_size;
727               if (shared_offset > sharedhdr.codeImageOffset)
728                 shared_offset = sharedhdr.codeImageOffset;
729               if (shared_offset > sharedhdr.dataImageOffset)
730                 shared_offset = sharedhdr.dataImageOffset;
731               if (shared_offset > sharedhdr.relocationFixupOffset)
732                 shared_offset = sharedhdr.relocationFixupOffset;
733               if (shared_offset > sharedhdr.externalReferencesOffset)
734                 shared_offset = sharedhdr.externalReferencesOffset;
735               if (shared_offset > sharedhdr.publicsOffset)
736                 shared_offset = sharedhdr.publicsOffset;
737               shared_size = st.st_size - shared_offset;
738               shared_section = bfd_make_section (outbfd, ".nlmshared");
739               if (shared_section == NULL
740                   || ! bfd_set_section_size (outbfd, shared_section,
741                                              shared_size)
742                   || ! bfd_set_section_flags (outbfd, shared_section,
743                                               SEC_HAS_CONTENTS))
744                 bfd_fatal ("shared section");
745               strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
746             }
747         }
748     }
749
750   /* Check whether a version was given.  */
751   if (strncmp (version_hdr->stamp, "VeRsIoN#", 8) != 0)
752     fprintf (stderr, "%s: warning: No version number given\n",
753              program_name);
754
755   /* At least for now, always create an extended header, because that
756      is what NLMLINK does.  */
757   strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
758
759   /* If the date was not given, force it in.  */
760   if (nlm_version_header (outbfd)->month == 0
761       && nlm_version_header (outbfd)->day == 0
762       && nlm_version_header (outbfd)->year == 0)
763     {
764       time_t now;
765       struct tm *ptm;
766
767       time (&now);
768       ptm = localtime (&now);
769       nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
770       nlm_version_header (outbfd)->day = ptm->tm_mday;
771       nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
772       strncpy (version_hdr->stamp, "VeRsIoN#", 8);
773     }
774
775   /* Copy over the sections.  */
776   bfd_map_over_sections (inbfd, copy_sections, (PTR) outbfd);
777
778   /* Finish up the header information.  */
779   if (custom_file != NULL)
780     {
781       PTR data;
782
783       data = xmalloc (custom_size);
784       if (fread (data, 1, custom_size, custom_data) != custom_size)
785         fprintf (stderr, "%s:%s: read: %s\n", program_name, custom_file,
786                  strerror (errno));
787       else
788         {
789           if (! bfd_set_section_contents (outbfd, custom_section, data,
790                                           (file_ptr) 0, custom_size))
791             bfd_fatal ("custom section");
792           nlm_fixed_header (outbfd)->customDataOffset =
793             custom_section->filepos;
794           nlm_fixed_header (outbfd)->customDataSize = custom_size;
795         }
796       free (data);
797     }
798   if (! debug_info)
799     {
800       /* As a special hack, the backend recognizes a debugInfoOffset
801          of -1 to mean that it should not output any debugging
802          information.  This can not be handling by fiddling with the
803          symbol table because exported symbols appear in both the
804          export information and the debugging information.  */
805       nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
806     }
807   if (map_file != NULL)
808     fprintf (stderr,
809              "%s: warning: MAP and FULLMAP are not supported; try ld -M\n",
810              program_name);
811   if (help_file != NULL)
812     {
813       PTR data;
814
815       data = xmalloc (help_size);
816       if (fread (data, 1, help_size, help_data) != help_size)
817         fprintf (stderr, "%s:%s: read: %s\n", program_name, help_file,
818                  strerror (errno));
819       else
820         {
821           if (! bfd_set_section_contents (outbfd, help_section, data,
822                                           (file_ptr) 0, help_size))
823             bfd_fatal ("help section");
824           nlm_extended_header (outbfd)->helpFileOffset =
825             help_section->filepos;
826           nlm_extended_header (outbfd)->helpFileLength = help_size;
827         }
828       free (data);
829     }
830   if (message_file != NULL)
831     {
832       PTR data;
833
834       data = xmalloc (message_size);
835       if (fread (data, 1, message_size, message_data) != message_size)
836         fprintf (stderr, "%s:%s: read: %s\n", program_name, message_file,
837                  strerror (errno));
838       else
839         {
840           if (! bfd_set_section_contents (outbfd, message_section, data,
841                                           (file_ptr) 0, message_size))
842             bfd_fatal ("message section");
843           nlm_extended_header (outbfd)->messageFileOffset =
844             message_section->filepos;
845           nlm_extended_header (outbfd)->messageFileLength = message_size;
846
847           /* FIXME: Are these offsets correct on all platforms?  Are
848              they 32 bits on all platforms?  What endianness?  */
849           nlm_extended_header (outbfd)->languageID =
850             bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
851           nlm_extended_header (outbfd)->messageCount =
852             bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
853         }
854       free (data);
855     }
856   if (modules != NULL)
857     {
858       PTR data;
859       unsigned char *set;
860       struct string_list *l;
861       bfd_size_type c;
862
863       data = xmalloc (module_size);
864       c = 0;
865       set = (unsigned char *) data;
866       for (l = modules; l != NULL; l = l->next)
867         {
868           *set = strlen (l->string);
869           strncpy (set + 1, l->string, *set);
870           set += *set + 1;
871           ++c;
872         }
873       if (! bfd_set_section_contents (outbfd, module_section, data,
874                                       (file_ptr) 0, module_size))
875         bfd_fatal ("module section");
876       nlm_fixed_header (outbfd)->moduleDependencyOffset =
877         module_section->filepos;
878       nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
879     }
880   if (rpc_file != NULL)
881     {
882       PTR data;
883
884       data = xmalloc (rpc_size);
885       if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
886         fprintf (stderr, "%s:%s: read: %s\n", program_name, rpc_file,
887                  strerror (errno));
888       else
889         {
890           if (! bfd_set_section_contents (outbfd, rpc_section, data,
891                                           (file_ptr) 0, rpc_size))
892             bfd_fatal ("rpc section");
893           nlm_extended_header (outbfd)->RPCDataOffset =
894             rpc_section->filepos;
895           nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
896         }
897       free (data);
898     }
899   if (sharelib_file != NULL)
900     {
901       PTR data;
902
903       data = xmalloc (shared_size);
904       if (fseek (shared_data, shared_offset, SEEK_SET) != 0
905           || fread (data, 1, shared_size, shared_data) != shared_size)
906         fprintf (stderr, "%s:%s: read: %s\n", program_name, sharelib_file,
907                  strerror (errno));
908       else
909         {
910           if (! bfd_set_section_contents (outbfd, shared_section, data,
911                                           (file_ptr) 0, shared_size))
912             bfd_fatal ("shared section");
913         }
914       nlm_extended_header (outbfd)->sharedCodeOffset =
915         sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
916       nlm_extended_header (outbfd)->sharedCodeLength =
917         sharedhdr.codeImageSize;
918       nlm_extended_header (outbfd)->sharedDataOffset =
919         sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
920       nlm_extended_header (outbfd)->sharedDataLength =
921         sharedhdr.dataImageSize;
922       nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
923         (sharedhdr.relocationFixupOffset
924          - shared_offset
925          + shared_section->filepos);
926       nlm_extended_header (outbfd)->sharedRelocationFixupCount =
927         sharedhdr.numberOfRelocationFixups;
928       nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
929         (sharedhdr.externalReferencesOffset
930          - shared_offset
931          + shared_section->filepos);
932       nlm_extended_header (outbfd)->sharedExternalReferenceCount =
933         sharedhdr.numberOfExternalReferences;
934       nlm_extended_header (outbfd)->sharedPublicsOffset =
935         sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
936       nlm_extended_header (outbfd)->sharedPublicsCount =
937         sharedhdr.numberOfPublics;
938       nlm_extended_header (outbfd)->sharedDebugRecordOffset =
939         sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
940       nlm_extended_header (outbfd)->sharedDebugRecordCount =
941         sharedhdr.numberOfDebugRecords;
942       nlm_extended_header (outbfd)->SharedInitializationOffset =
943         sharedhdr.codeStartOffset;
944       nlm_extended_header (outbfd)->SharedExitProcedureOffset =
945         sharedhdr.exitProcedureOffset;
946       free (data);
947     }
948   len = strlen (output_file);
949   if (len > NLM_MODULE_NAME_SIZE - 2)
950     len = NLM_MODULE_NAME_SIZE - 2;
951   nlm_fixed_header (outbfd)->moduleName[0] = len;
952
953   strncpy (nlm_fixed_header (outbfd)->moduleName + 1, output_file,
954            NLM_MODULE_NAME_SIZE - 2);
955   nlm_fixed_header (outbfd)->moduleName[NLM_MODULE_NAME_SIZE - 1] = '\0';
956   for (modname = nlm_fixed_header (outbfd)->moduleName;
957        *modname != '\0';
958        modname++)
959     if (islower (*modname))
960       *modname = toupper (*modname);
961
962   strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
963            NLM_OLD_THREAD_NAME_LENGTH);
964
965   if (! bfd_close (outbfd))
966     bfd_fatal (output_file);
967   if (! bfd_close (inbfd))
968     bfd_fatal (input_file);
969
970   if (unlink_on_exit != NULL)
971     unlink (unlink_on_exit);
972
973   return 0;
974 }
975 \f
976 /* Display a help message and exit.  */
977
978 static void
979 show_help ()
980 {
981   printf ("%s: Convert an object file into a NetWare Loadable Module\n",
982           program_name);
983   show_usage (stdout, 0);
984 }
985
986 /* Show a usage message and exit.  */
987
988 static void
989 show_usage (file, status)
990      FILE *file;
991      int status;
992 {
993   fprintf (file, "\
994 Usage: %s [-dhV] [-I bfdname] [-O bfdname] [-T header-file] [-l linker]\n\
995        [--input-target=bfdname] [--output-target=bfdname]\n\
996        [--header-file=file] [--linker=linker] [--debug]\n\
997        [--help] [--version]\n\
998        [in-file [out-file]]\n",
999            program_name);
1000   exit (status);
1001 }
1002 \f
1003 /* Select the output format based on the input architecture, machine,
1004    and endianness.  This chooses the appropriate NLM target.  */
1005
1006 static const char *
1007 select_output_format (arch, mach, bigendian)
1008      enum bfd_architecture arch;
1009      unsigned long mach;
1010      boolean bigendian;
1011 {
1012   switch (arch)
1013     {
1014     case bfd_arch_i386:
1015       return "nlm32-i386";
1016     case bfd_arch_sparc:
1017       return "nlm32-sparc";
1018     case bfd_arch_alpha:
1019       return "nlm32-alpha";
1020     default:
1021       fprintf (stderr, "%s: no default NLM format for %s\n",
1022                program_name, bfd_printable_arch_mach (arch, mach));
1023       exit (1);
1024       /* Avoid warning.  */
1025       return NULL;
1026     }
1027   /*NOTREACHED*/
1028 }
1029 \f
1030 /* The BFD sections are copied in two passes.  This function selects
1031    the output section for each input section, and sets up the section
1032    name, size, etc.  */
1033
1034 static void
1035 setup_sections (inbfd, insec, data_ptr)
1036      bfd *inbfd;
1037      asection *insec;
1038      PTR data_ptr;
1039 {
1040   bfd *outbfd = (bfd *) data_ptr;
1041   flagword f;
1042   const char *outname;
1043   asection *outsec;
1044   bfd_vma offset;
1045   bfd_size_type align;
1046   bfd_size_type add;
1047
1048   /* FIXME: We don't want to copy the .reginfo section of an ECOFF
1049      file.  However, I don't have a good way to describe this section.
1050      We do want to copy the section when using objcopy.  */
1051   if (bfd_get_flavour (inbfd) == bfd_target_ecoff_flavour
1052       && strcmp (bfd_section_name (inbfd, insec), ".reginfo") == 0)
1053     return;
1054
1055   f = bfd_get_section_flags (inbfd, insec);
1056   if (f & SEC_CODE)
1057     outname = NLM_CODE_NAME;
1058   else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
1059     outname = NLM_INITIALIZED_DATA_NAME;
1060   else if (f & SEC_ALLOC)
1061     outname = NLM_UNINITIALIZED_DATA_NAME;
1062   else
1063     outname = bfd_section_name (inbfd, insec);
1064
1065   outsec = bfd_get_section_by_name (outbfd, outname);
1066   if (outsec == NULL)
1067     {
1068       outsec = bfd_make_section (outbfd, outname);
1069       if (outsec == NULL)
1070         bfd_fatal ("make section");
1071     }
1072
1073   insec->output_section = outsec;
1074
1075   offset = bfd_section_size (outbfd, outsec);
1076   align = 1 << bfd_section_alignment (inbfd, insec);
1077   add = ((offset + align - 1) &~ (align - 1)) - offset;
1078   insec->output_offset = offset + add;
1079
1080   if (! bfd_set_section_size (outbfd, outsec,
1081                               (bfd_section_size (outbfd, outsec)
1082                                + bfd_section_size (inbfd, insec)
1083                                + add)))
1084     bfd_fatal ("set section size");
1085
1086   if ((bfd_section_alignment (inbfd, insec)
1087        > bfd_section_alignment (outbfd, outsec))
1088       && ! bfd_set_section_alignment (outbfd, outsec,
1089                                       bfd_section_alignment (inbfd, insec)))
1090     bfd_fatal ("set section alignment");
1091
1092   if (! bfd_set_section_flags (outbfd, outsec, f))
1093     bfd_fatal ("set section flags");
1094
1095   bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
1096 }
1097
1098 /* Copy the section contents.  */
1099
1100 static void
1101 copy_sections (inbfd, insec, data_ptr)
1102      bfd *inbfd;
1103      asection *insec;
1104      PTR data_ptr;
1105 {
1106   bfd *outbfd = (bfd *) data_ptr;
1107   asection *outsec;
1108   bfd_size_type size;
1109   PTR contents;
1110   bfd_size_type reloc_size;
1111
1112   /* FIXME: We don't want to copy the .reginfo section of an ECOFF
1113      file.  However, I don't have a good way to describe this section.
1114      We do want to copy the section when using objcopy.  */
1115   if (bfd_get_flavour (inbfd) == bfd_target_ecoff_flavour
1116       && strcmp (bfd_section_name (inbfd, insec), ".reginfo") == 0)
1117     return;
1118
1119   outsec = insec->output_section;
1120   assert (outsec != NULL);
1121
1122   size = bfd_get_section_size_before_reloc (insec);
1123   if (size == 0)
1124     return;
1125
1126   /* FIXME: Why are these necessary?  */
1127   insec->_cooked_size = insec->_raw_size;
1128   insec->reloc_done = true;
1129
1130   if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
1131     contents = NULL;
1132   else
1133     {
1134       contents = xmalloc (size);
1135       if (! bfd_get_section_contents (inbfd, insec, contents,
1136                                       (file_ptr) 0, size))
1137         bfd_fatal (bfd_get_filename (inbfd));
1138     }
1139
1140   reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
1141   if (reloc_size != 0)
1142     {
1143       arelent **relocs;
1144       bfd_size_type reloc_count;
1145
1146       relocs = (arelent **) xmalloc (reloc_size);
1147       reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
1148       mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
1149                      size);
1150
1151       /* FIXME: refers to internal BFD fields.  */
1152       if (outsec->orelocation != (arelent **) NULL)
1153         {
1154           bfd_size_type total_count;
1155           arelent **combined;
1156
1157           total_count = reloc_count + outsec->reloc_count;
1158           combined = (arelent **) xmalloc (total_count * sizeof (arelent));
1159           memcpy (combined, outsec->orelocation,
1160                   outsec->reloc_count * sizeof (arelent));
1161           memcpy (combined + outsec->reloc_count, relocs,
1162                   (size_t) (reloc_count * sizeof (arelent)));
1163           free (outsec->orelocation);
1164           reloc_count = total_count;
1165           relocs = combined;
1166         }
1167
1168       bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
1169     }
1170
1171   if (contents != NULL)
1172     {
1173       if (! bfd_set_section_contents (outbfd, outsec, contents,
1174                                       insec->output_offset, size))
1175         bfd_fatal (bfd_get_filename (outbfd));
1176       free (contents);
1177     }
1178 }
1179
1180 /* Some, perhaps all, NetWare targets require changing the relocs used
1181    by the input formats.  */
1182
1183 static void
1184 mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1185                contents_size)
1186      bfd *outbfd;
1187      asection *insec;
1188      arelent ***relocs_ptr;
1189      bfd_size_type *reloc_count_ptr;
1190      char *contents;
1191      bfd_size_type contents_size;
1192 {
1193   switch (bfd_get_arch (outbfd))
1194     {
1195     case bfd_arch_i386:
1196       i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1197                           contents, contents_size);
1198       break;
1199     case bfd_arch_alpha:
1200       alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1201                            contents, contents_size);
1202       break;
1203     default:
1204       default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1205                              contents, contents_size);
1206       break;
1207     }
1208 }
1209
1210 /* By default all we need to do for relocs is change the address by
1211    the output_offset.  */
1212
1213 /*ARGSUSED*/
1214 static void
1215 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1216                        contents_size)
1217      bfd *outbfd;
1218      asection *insec;
1219      arelent ***relocs_ptr;
1220      bfd_size_type *reloc_count_ptr;
1221      char *contents;
1222      bfd_size_type contents_size;
1223 {
1224   if (insec->output_offset != 0)
1225     {
1226       bfd_size_type reloc_count;
1227       register arelent **relocs;
1228       register bfd_size_type i;
1229
1230       reloc_count = *reloc_count_ptr;
1231       relocs = *relocs_ptr;
1232       for (i = 0; i < reloc_count; i++, relocs++)
1233         (*relocs)->address += insec->output_offset;
1234     }
1235 }
1236
1237 /* NetWare on the i386 supports a restricted set of relocs, which are
1238    different from those used on other i386 targets.  This routine
1239    converts the relocs.  It is, obviously, very target dependent.  At
1240    the moment, the nlm32-i386 backend performs similar translations;
1241    however, it is more reliable and efficient to do them here.  */
1242
1243 static reloc_howto_type nlm_i386_pcrel_howto =
1244   HOWTO (1,                     /* type */
1245          0,                     /* rightshift */
1246          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1247          32,                    /* bitsize */
1248          true,                  /* pc_relative */
1249          0,                     /* bitpos */
1250          complain_overflow_signed, /* complain_on_overflow */
1251          0,                     /* special_function */
1252          "DISP32",              /* name */
1253          true,                  /* partial_inplace */
1254          0xffffffff,            /* src_mask */
1255          0xffffffff,            /* dst_mask */
1256          true);                 /* pcrel_offset */
1257
1258 static void
1259 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1260                     contents_size)
1261      bfd *outbfd;
1262      asection *insec;
1263      arelent ***relocs_ptr;
1264      bfd_size_type *reloc_count_ptr;
1265      char *contents;
1266      bfd_size_type contents_size;
1267 {
1268   bfd_size_type reloc_count, i;
1269   arelent **relocs;
1270
1271   reloc_count = *reloc_count_ptr;
1272   relocs = *relocs_ptr;
1273   for (i = 0; i < reloc_count; i++)
1274     {
1275       arelent *rel;
1276       asymbol *sym;
1277       bfd_size_type address;
1278       bfd_vma addend;
1279
1280       rel = *relocs++;
1281       sym = *rel->sym_ptr_ptr;
1282
1283       /* We're moving the relocs from the input section to the output
1284          section, so we must adjust the address accordingly.  */
1285       address = rel->address;
1286       rel->address += insec->output_offset;
1287
1288       /* Note that no serious harm will ensue if we fail to change a
1289          reloc.  The backend will fail when writing out the reloc.  */
1290
1291       /* Make sure this reloc is within the data we have.  We use only
1292          4 byte relocs here, so we insist on having 4 bytes.  */
1293       if (address + 4 > contents_size)
1294         continue;
1295
1296       /* A PC relative reloc entirely within a single section is
1297          completely unnecessary.  This can be generated by ld -r.  */
1298       if (sym == insec->symbol
1299           && rel->howto != NULL
1300           && rel->howto->pc_relative
1301           && ! rel->howto->pcrel_offset)
1302         {
1303           --*reloc_count_ptr;
1304           --relocs;
1305           memmove (relocs, relocs + 1,
1306                    (size_t) ((reloc_count - i) * sizeof (arelent *)));
1307           continue;
1308         }
1309
1310       /* Get the amount the relocation will add in.  */
1311       addend = rel->addend + sym->value;
1312
1313       /* NetWare doesn't support PC relative relocs against defined
1314          symbols, so we have to eliminate them by doing the relocation
1315          now.  We can only do this if the reloc is within a single
1316          section.  */
1317       if (rel->howto != NULL
1318           && rel->howto->pc_relative
1319           && bfd_get_section (sym) == insec->output_section)
1320         {
1321           bfd_vma val;
1322
1323           if (rel->howto->pcrel_offset)
1324             addend -= address;
1325
1326           val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1327           val += addend;
1328           bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1329
1330           --*reloc_count_ptr;
1331           --relocs;
1332           memmove (relocs, relocs + 1,
1333                    (size_t) ((reloc_count - i) * sizeof (arelent *)));
1334           continue;
1335         }
1336
1337       /* NetWare doesn't support reloc addends, so we get rid of them
1338          here by simply adding them into the object data.  We handle
1339          the symbol value, if any, the same way.  */
1340       if (addend != 0
1341           && rel->howto != NULL
1342           && rel->howto->rightshift == 0
1343           && rel->howto->size == 2
1344           && rel->howto->bitsize == 32
1345           && rel->howto->bitpos == 0
1346           && rel->howto->src_mask == 0xffffffff
1347           && rel->howto->dst_mask == 0xffffffff)
1348         {
1349           bfd_vma val;
1350
1351           val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1352           val += addend;
1353           bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1354
1355           /* Adjust the reloc for the changes we just made.  */
1356           rel->addend = 0;
1357           if (bfd_get_section (sym) != &bfd_und_section)
1358             rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
1359         }
1360
1361       /* NetWare uses a reloc with pcrel_offset set.  We adjust
1362          pc_relative relocs accordingly.  We are going to change the
1363          howto field, so we can only do this if the current one is
1364          compatible.  We should check that special_function is NULL
1365          here, but at the moment coff-i386 uses a special_function
1366          which does not affect what we are doing here.  */
1367       if (rel->howto != NULL
1368           && rel->howto->pc_relative
1369           && ! rel->howto->pcrel_offset
1370           && rel->howto->rightshift == 0
1371           && rel->howto->size == 2
1372           && rel->howto->bitsize == 32
1373           && rel->howto->bitpos == 0
1374           && rel->howto->src_mask == 0xffffffff
1375           && rel->howto->dst_mask == 0xffffffff)
1376         {
1377           bfd_vma val;
1378
1379           /* When pcrel_offset is not set, it means that the negative
1380              of the address of the memory location is stored in the
1381              memory location.  We must add it back in.  */
1382           val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1383           val += address;
1384           bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1385
1386           /* We must change to a new howto.  */
1387           rel->howto = &nlm_i386_pcrel_howto;
1388         }
1389     }
1390 }
1391
1392 /* On the Alpha the first reloc for every section must be a special
1393    relocs which hold the GP address.  Also, the first reloc in the
1394    file must be a special reloc which holds the address of the .lita
1395    section.  */
1396
1397 static reloc_howto_type nlm32_alpha_nw_howto =
1398   HOWTO (ALPHA_R_NW_RELOC,      /* type */
1399          0,                     /* rightshift */
1400          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1401          0,                     /* bitsize */
1402          false,                 /* pc_relative */
1403          0,                     /* bitpos */
1404          complain_overflow_dont, /* complain_on_overflow */
1405          0,                     /* special_function */
1406          "NW_RELOC",            /* name */
1407          false,                 /* partial_inplace */
1408          0,                     /* src_mask */
1409          0,                     /* dst_mask */
1410          false);                /* pcrel_offset */
1411
1412 /*ARGSUSED*/
1413 static void
1414 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1415                      contents_size)
1416      bfd *outbfd;
1417      asection *insec;
1418      register arelent ***relocs_ptr;
1419      bfd_size_type *reloc_count_ptr;
1420      char *contents;
1421      bfd_size_type contents_size;
1422 {
1423   bfd_size_type old_reloc_count;
1424   arelent **old_relocs;
1425   register arelent **relocs;
1426
1427   old_reloc_count = *reloc_count_ptr;
1428   old_relocs = *relocs_ptr;
1429   relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
1430   *relocs_ptr = relocs;
1431
1432   if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
1433     {
1434       bfd *inbfd;
1435       asection *lita_section;
1436
1437       inbfd = insec->owner;
1438       lita_section = bfd_get_section_by_name (inbfd, _LITA);
1439       if (lita_section != (asection *) NULL)
1440         {
1441           nlm_alpha_backend_data (outbfd)->lita_address =
1442             bfd_get_section_vma (inbfd, lita_section);
1443           nlm_alpha_backend_data (outbfd)->lita_size =
1444             bfd_section_size (inbfd, lita_section);
1445         }
1446       else
1447         {
1448           /* Avoid outputting this reloc again.  */
1449           nlm_alpha_backend_data (outbfd)->lita_address = 4;
1450         }
1451
1452       *relocs = (arelent *) xmalloc (sizeof (arelent));
1453       (*relocs)->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1454       (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
1455       (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
1456       (*relocs)->howto = &nlm32_alpha_nw_howto;
1457       ++relocs;
1458       ++(*reloc_count_ptr);
1459     }
1460
1461   /* Get the GP value from bfd.  It is in the .reginfo section.  */
1462   if (nlm_alpha_backend_data (outbfd)->gp == 0)
1463     {
1464       bfd *inbfd;
1465       asection *reginfo_sec;
1466       struct ecoff_reginfo sreginfo;
1467
1468       inbfd = insec->owner;
1469       assert (bfd_get_flavour (inbfd) == bfd_target_ecoff_flavour);
1470       reginfo_sec = bfd_get_section_by_name (inbfd, REGINFO);
1471       if (reginfo_sec != (asection *) NULL
1472           && bfd_get_section_contents (inbfd, reginfo_sec,
1473                                        (PTR) &sreginfo, (file_ptr) 0,
1474                                        sizeof sreginfo) != false)
1475         nlm_alpha_backend_data (outbfd)->gp = sreginfo.gp_value;
1476     }
1477
1478   *relocs = (arelent *) xmalloc (sizeof (arelent));
1479   (*relocs)->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1480   (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
1481   (*relocs)->addend = 0;
1482   (*relocs)->howto = &nlm32_alpha_nw_howto;
1483   ++relocs;
1484   ++(*reloc_count_ptr);
1485
1486   memcpy ((PTR) relocs, (PTR) old_relocs,
1487           (size_t) old_reloc_count * sizeof (arelent *));
1488   relocs[old_reloc_count] = (arelent *) NULL;
1489
1490   free (old_relocs);
1491
1492   if (insec->output_offset != 0)
1493     {
1494       register bfd_size_type i;
1495
1496       for (i = 0; i < old_reloc_count; i++, relocs++)
1497         (*relocs)->address += insec->output_offset;
1498     }
1499 }
1500 \f
1501 /* Name of linker.  */
1502 #ifndef LD_NAME
1503 #define LD_NAME "ld"
1504 #endif
1505
1506 /* Temporary file name base.  */
1507 static char *temp_filename;
1508
1509 /* The user has specified several input files.  Invoke the linker to
1510    link them all together, and convert and delete the resulting output
1511    file.  */
1512
1513 static char *
1514 link_inputs (inputs, ld)
1515      struct string_list *inputs;
1516      char *ld;
1517 {
1518   size_t c;
1519   struct string_list *q;
1520   char **argv;
1521   size_t i;
1522   int pid;
1523   int status;
1524
1525   c = 0;
1526   for (q = inputs; q != NULL; q = q->next)
1527     ++c;
1528
1529   argv = (char **) alloca (c + 5);
1530
1531 #ifndef __MSDOS__
1532   if (ld == NULL)
1533     {
1534       char *p;
1535
1536       /* Find the linker to invoke based on how nlmconv was run.  */
1537       p = program_name + strlen (program_name);
1538       while (p != program_name)
1539         {
1540           if (p[-1] == '/')
1541             {
1542               ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
1543               memcpy (ld, program_name, p - program_name);
1544               strcpy (ld + (p - program_name), LD_NAME);
1545               break;
1546             }
1547           --p;
1548         }
1549     }
1550 #endif
1551
1552   if (ld == NULL)
1553     ld = (char *) LD_NAME;
1554
1555   choose_temp_base ();
1556
1557   unlink_on_exit = xmalloc (strlen (temp_filename) + 3);
1558   sprintf (unlink_on_exit, "%s.O", temp_filename);
1559
1560   argv[0] = ld;
1561   argv[1] = (char *) "-r";
1562   argv[2] = (char *) "-o";
1563   argv[3] = unlink_on_exit;
1564   i = 4;
1565   for (q = inputs; q != NULL; q = q->next, i++)
1566     argv[i] = q->string;
1567   argv[i] = NULL;
1568
1569   if (debug)
1570     {
1571       for (i = 0; argv[i] != NULL; i++)
1572         fprintf (stderr, " %s", argv[i]);
1573       fprintf (stderr, "\n");
1574     }
1575
1576   pid = pexecute (ld, argv);
1577
1578   if (waitpid (pid, &status, 0) < 0)
1579     {
1580       perror ("waitpid");
1581       unlink (unlink_on_exit);
1582       exit (1);
1583     }
1584
1585   if (status != 0)
1586     {
1587       fprintf (stderr, "%s: Execution of %s failed\n", program_name, ld);
1588       unlink (unlink_on_exit);
1589       exit (1);
1590     }
1591
1592   return unlink_on_exit;
1593 }
1594
1595 /* Choose a temporary file name.  Stolen from gcc.c.  */
1596
1597 static const char *
1598 choose_temp_base_try (try, base)
1599      const char *try;
1600      const char *base;
1601 {
1602   const char *rv;
1603
1604   if (base)
1605     rv = base;
1606   else if (try == NULL)
1607     rv = NULL;
1608   else if (access (try, R_OK | W_OK) != 0)
1609     rv = NULL;
1610   else
1611     rv = try;
1612   return rv;
1613 }
1614
1615 static void
1616 choose_temp_base ()
1617 {
1618   const char *base = NULL;
1619   int len;
1620
1621   base = choose_temp_base_try (getenv ("TMPDIR"), base);
1622   base = choose_temp_base_try (getenv ("TMP"), base);
1623   base = choose_temp_base_try (getenv ("TEMP"), base);
1624
1625 #ifdef P_tmpdir
1626   base = choose_temp_base_try (P_tmpdir, base);
1627 #endif
1628
1629   base = choose_temp_base_try ("/usr/tmp", base);
1630   base = choose_temp_base_try ("/tmp", base);
1631
1632   /* If all else fails, use the current directory! */  
1633   if (base == NULL)
1634     base = "./";
1635
1636   len = strlen (base);
1637   temp_filename = xmalloc (len + sizeof("/ccXXXXXX") + 1);
1638   strcpy (temp_filename, base);
1639   if (len > 0 && temp_filename[len-1] != '/')
1640     temp_filename[len++] = '/';
1641   strcpy (temp_filename + len, "ccXXXXXX");
1642
1643   mktemp (temp_filename);
1644   if (*temp_filename == '\0')
1645     abort ();
1646 }
1647
1648 /* Execute a job.  Stolen from gcc.c.  */
1649
1650 #ifndef OS2
1651 #ifdef __MSDOS__
1652
1653 static int
1654 pexecute (program, argv)
1655      char *program;
1656      char *argv[];
1657 {
1658   char *scmd, *rf;
1659   FILE *argfile;
1660   int i;
1661
1662   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 10);
1663   rf = scmd + strlen(program) + 2 + el;
1664   sprintf (scmd, "%s.exe @%s.gp", program, temp_filename);
1665   argfile = fopen (rf, "w");
1666   if (argfile == 0)
1667     pfatal_with_name (rf);
1668
1669   for (i=1; argv[i]; i++)
1670     {
1671       char *cp;
1672       for (cp = argv[i]; *cp; cp++)
1673         {
1674           if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
1675             fputc ('\\', argfile);
1676           fputc (*cp, argfile);
1677         }
1678       fputc ('\n', argfile);
1679     }
1680   fclose (argfile);
1681
1682   i = system (scmd);
1683
1684   remove (rf);
1685   
1686   if (i == -1)
1687     {
1688       perror (program);
1689       return MIN_FATAL_STATUS << 8;
1690     }
1691
1692   return i << 8;
1693 }
1694
1695 #else /* not __MSDOS__ */
1696
1697 static int
1698 pexecute (program, argv)
1699      char *program;
1700      char *argv[];
1701 {
1702   int pid;
1703   int retries, sleep_interval;
1704
1705   /* Fork a subprocess; wait and retry if it fails.  */
1706   sleep_interval = 1;
1707   for (retries = 0; retries < 4; retries++)
1708     {
1709       pid = vfork ();
1710       if (pid >= 0)
1711         break;
1712       sleep (sleep_interval);
1713       sleep_interval *= 2;
1714     }
1715
1716   switch (pid)
1717     {
1718     case -1:
1719 #ifdef vfork
1720       perror ("fork");
1721 #else
1722       perror ("vfork");
1723 #endif
1724       exit (1);
1725       /* NOTREACHED */
1726       return 0;
1727
1728     case 0: /* child */
1729       /* Exec the program.  */
1730       execvp (program, argv);
1731       perror (program);
1732       exit (1);
1733       /* NOTREACHED */
1734       return 0;
1735
1736     default:
1737       /* Return child's process number.  */
1738       return pid;
1739     }
1740 }
1741
1742 #endif /* not __MSDOS__ */
1743 #else /* not OS2 */
1744
1745 static int
1746 pexecute (program, argv)
1747      char *program;
1748      char *argv[];
1749 {
1750   return spawnvp (1, program, argv);
1751 }
1752 #endif /* not OS2 */