Remove sanitization of PowerPC NetWare.
[deliverable/binutils-gdb.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 "libiberty.h"
40 #include "sysdep.h"
41 #include "bucomm.h"
42 /* Internal BFD NLM header. */
43 #include "libnlm.h"
44 #include "nlmconv.h"
45
46 /* Needed for Alpha support. */
47 #include "coff/sym.h"
48 #include "coff/ecoff.h"
49
50 /* If strerror is just a macro, we want to use the one from libiberty
51 since it will handle undefined values. */
52 #undef strerror
53 extern char *strerror ();
54
55 #ifndef localtime
56 extern struct tm *localtime ();
57 #endif
58
59 #ifndef getenv
60 extern char *getenv ();
61 #endif
62
63 #ifndef SEEK_SET
64 #define SEEK_SET 0
65 #endif
66
67 #ifndef R_OK
68 #define R_OK 4
69 #define W_OK 2
70 #define X_OK 1
71 #endif
72 \f
73 /* Global variables. */
74
75 /* The name used to invoke the program. */
76 char *program_name;
77
78 /* The version number. */
79 extern char *program_version;
80
81 /* Local variables. */
82
83 /* Whether to print out debugging information (currently just controls
84 whether it prints the linker command if there is one). */
85 static int debug;
86
87 /* The symbol table. */
88 static asymbol **symbols;
89
90 /* A section we create in the output file to hold pointers to where
91 the sections of the input file end up. We will put a pointer to
92 this section in the NLM header. These is an entry for each input
93 section. The format is
94 null terminated section name
95 zeroes to adjust to 4 byte boundary
96 4 byte section data file pointer
97 4 byte section size
98 We don't need a version number. The way we find this information
99 is by finding a stamp in the NLM header information. If we need to
100 change the format of this information, we can simply change the
101 stamp. */
102 static asection *secsec;
103
104 /* A temporary file name to be unlinked on exit. Actually, for most
105 errors, we leave it around. It's not clear whether that is helpful
106 or not. */
107 static char *unlink_on_exit;
108
109 /* The list of long options. */
110 static struct option long_options[] =
111 {
112 { "debug", no_argument, 0, 'd' },
113 { "header-file", required_argument, 0, 'T' },
114 { "help", no_argument, 0, 'h' },
115 { "input-target", required_argument, 0, 'I' },
116 { "input-format", required_argument, 0, 'I' }, /* Obsolete */
117 { "linker", required_argument, 0, 'l' },
118 { "output-target", required_argument, 0, 'O' },
119 { "output-format", required_argument, 0, 'O' }, /* Obsolete */
120 { "version", no_argument, 0, 'V' },
121 { NULL, no_argument, 0, 0 }
122 };
123
124 /* Local routines. */
125
126 static void show_help PARAMS ((void));
127 static void show_usage PARAMS ((FILE *, int));
128 static const char *select_output_format PARAMS ((enum bfd_architecture,
129 unsigned long, boolean));
130 static void setup_sections PARAMS ((bfd *, asection *, PTR));
131 static void copy_sections PARAMS ((bfd *, asection *, PTR));
132 static void mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
133 long *, char *,
134 bfd_size_type));
135 static void i386_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
136 long *, char *,
137 bfd_size_type));
138 static void alpha_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
139 long *, char *,
140 bfd_size_type));
141 static void powerpc_build_stubs PARAMS ((bfd *, bfd *, asymbol ***, long *));
142 static void powerpc_resolve_stubs PARAMS ((bfd *, bfd *));
143 static void powerpc_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
144 long *, char *,
145 bfd_size_type));
146 static void default_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
147 long *, char *,
148 bfd_size_type));
149 static char *link_inputs PARAMS ((struct string_list *, char *));
150 static const char *choose_temp_base_try PARAMS ((const char *,
151 const char *));
152 static void choose_temp_base PARAMS ((void));
153 static int pexecute PARAMS ((char *, char *[]));
154 \f
155 /* The main routine. */
156
157 int
158 main (argc, argv)
159 int argc;
160 char **argv;
161 {
162 int opt;
163 char *input_file = NULL;
164 const char *input_format = NULL;
165 const char *output_format = NULL;
166 const char *header_file = NULL;
167 char *ld_arg = NULL;
168 Nlm_Internal_Fixed_Header fixed_hdr_struct;
169 Nlm_Internal_Variable_Header var_hdr_struct;
170 Nlm_Internal_Version_Header version_hdr_struct;
171 Nlm_Internal_Copyright_Header copyright_hdr_struct;
172 Nlm_Internal_Extended_Header extended_hdr_struct;
173 bfd *inbfd;
174 bfd *outbfd;
175 asymbol **newsyms, **outsyms;
176 long symcount, newsymalloc, newsymcount;
177 long symsize;
178 asection *text_sec, *bss_sec, *data_sec;
179 bfd_vma vma;
180 bfd_size_type align;
181 asymbol *endsym;
182 long i;
183 char inlead, outlead;
184 boolean gotstart, gotexit, gotcheck;
185 struct stat st;
186 FILE *custom_data, *help_data, *message_data, *rpc_data, *shared_data;
187 size_t custom_size, help_size, message_size, module_size, rpc_size;
188 asection *custom_section, *help_section, *message_section, *module_section;
189 asection *rpc_section, *shared_section;
190 bfd *sharedbfd;
191 size_t shared_offset, shared_size;
192 Nlm_Internal_Fixed_Header sharedhdr;
193 int len;
194 char *modname;
195 char **matching;
196
197 program_name = argv[0];
198 xmalloc_set_program_name (program_name);
199
200 bfd_init ();
201
202 while ((opt = getopt_long (argc, argv, "dhI:l:O:T:V", long_options,
203 (int *) NULL))
204 != EOF)
205 {
206 switch (opt)
207 {
208 case 'd':
209 debug = 1;
210 break;
211 case 'h':
212 show_help ();
213 /*NOTREACHED*/
214 case 'I':
215 input_format = optarg;
216 break;
217 case 'l':
218 ld_arg = optarg;
219 break;
220 case 'O':
221 output_format = optarg;
222 break;
223 case 'T':
224 header_file = optarg;
225 break;
226 case 'V':
227 printf ("GNU %s version %s\n", program_name, program_version);
228 exit (0);
229 /*NOTREACHED*/
230 case 0:
231 break;
232 default:
233 show_usage (stderr, 1);
234 /*NOTREACHED*/
235 }
236 }
237
238 /* The input and output files may be named on the command line. */
239 output_file = NULL;
240 if (optind < argc)
241 {
242 input_file = argv[optind];
243 ++optind;
244 if (optind < argc)
245 {
246 output_file = argv[optind];
247 ++optind;
248 if (optind < argc)
249 show_usage (stderr, 1);
250 if (strcmp (input_file, output_file) == 0)
251 {
252 fprintf (stderr,
253 "%s: input and output files must be different\n",
254 program_name);
255 exit (1);
256 }
257 }
258 }
259
260 /* Initialize the header information to default values. */
261 fixed_hdr = &fixed_hdr_struct;
262 memset ((PTR) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
263 var_hdr = &var_hdr_struct;
264 memset ((PTR) &var_hdr_struct, 0, sizeof var_hdr_struct);
265 version_hdr = &version_hdr_struct;
266 memset ((PTR) &version_hdr_struct, 0, sizeof version_hdr_struct);
267 copyright_hdr = &copyright_hdr_struct;
268 memset ((PTR) &copyright_hdr_struct, 0, sizeof copyright_hdr_struct);
269 extended_hdr = &extended_hdr_struct;
270 memset ((PTR) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
271 check_procedure = NULL;
272 custom_file = NULL;
273 debug_info = false;
274 exit_procedure = "_Stop";
275 export_symbols = NULL;
276 map_file = NULL;
277 full_map = false;
278 help_file = NULL;
279 import_symbols = NULL;
280 message_file = NULL;
281 modules = NULL;
282 sharelib_file = NULL;
283 start_procedure = "_Prelude";
284 verbose = false;
285 rpc_file = NULL;
286
287 parse_errors = 0;
288
289 /* Parse the header file (if there is one). */
290 if (header_file != NULL)
291 {
292 if (! nlmlex_file (header_file)
293 || yyparse () != 0
294 || parse_errors != 0)
295 exit (1);
296 }
297
298 if (input_files != NULL)
299 {
300 if (input_file != NULL)
301 {
302 fprintf (stderr,
303 "%s: input file named both on command line and with INPUT\n",
304 program_name);
305 exit (1);
306 }
307 if (input_files->next == NULL)
308 input_file = input_files->string;
309 else
310 input_file = link_inputs (input_files, ld_arg);
311 }
312 else if (input_file == NULL)
313 {
314 fprintf (stderr, "%s: no input file\n", program_name);
315 show_usage (stderr, 1);
316 }
317
318 inbfd = bfd_openr (input_file, input_format);
319 if (inbfd == NULL)
320 bfd_fatal (input_file);
321
322 if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
323 {
324 bfd_nonfatal (input_file);
325 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
326 {
327 list_matching_formats (matching);
328 free (matching);
329 }
330 exit (1);
331 }
332
333 if (output_format == NULL)
334 output_format = select_output_format (bfd_get_arch (inbfd),
335 bfd_get_mach (inbfd),
336 inbfd->xvec->byteorder_big_p);
337
338 assert (output_format != NULL);
339
340 /* Use the output file named on the command line if it exists.
341 Otherwise use the file named in the OUTPUT statement. */
342 if (output_file == NULL)
343 {
344 fprintf (stderr, "%s: no name for output file\n",
345 program_name);
346 show_usage (stderr, 1);
347 }
348
349 outbfd = bfd_openw (output_file, output_format);
350 if (outbfd == NULL)
351 bfd_fatal (output_file);
352 if (! bfd_set_format (outbfd, bfd_object))
353 bfd_fatal (output_file);
354
355 assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
356
357 if (bfd_arch_get_compatible (inbfd, outbfd) == NULL)
358 fprintf (stderr,
359 "%s: warning:input and output formats are not compatible\n",
360 program_name);
361
362 /* Move the values read from the command file into outbfd. */
363 *nlm_fixed_header (outbfd) = fixed_hdr_struct;
364 *nlm_variable_header (outbfd) = var_hdr_struct;
365 *nlm_version_header (outbfd) = version_hdr_struct;
366 *nlm_copyright_header (outbfd) = copyright_hdr_struct;
367 *nlm_extended_header (outbfd) = extended_hdr_struct;
368
369 /* Start copying the input BFD to the output BFD. */
370 if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
371 bfd_fatal (bfd_get_filename (outbfd));
372
373 symsize = bfd_get_symtab_upper_bound (inbfd);
374 if (symsize < 0)
375 bfd_fatal (input_file);
376 symbols = (asymbol **) xmalloc (symsize);
377 symcount = bfd_canonicalize_symtab (inbfd, symbols);
378 if (symcount < 0)
379 bfd_fatal (input_file);
380
381 /* Make sure we have a .bss section. */
382 bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
383 if (bss_sec == NULL)
384 {
385 bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
386 if (bss_sec == NULL
387 || ! bfd_set_section_flags (outbfd, bss_sec, SEC_ALLOC)
388 || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
389 bfd_fatal ("make .bss section");
390 }
391
392 /* We store the original section names in the .nlmsections section,
393 so that programs which understand it can resurrect the original
394 sections from the NLM. We will put a pointer to .nlmsections in
395 the NLM header area. */
396 secsec = bfd_make_section (outbfd, ".nlmsections");
397 if (secsec == NULL)
398 bfd_fatal ("make .nlmsections section");
399 if (! bfd_set_section_flags (outbfd, secsec, SEC_HAS_CONTENTS))
400 bfd_fatal ("set .nlmsections flags");
401
402 /* For PowerPC NetWare we need to build stubs for calls to undefined
403 symbols. Because each stub requires an entry in the TOC section
404 which must be at the same location as other entries in the TOC
405 section, we must do this before determining where the TOC section
406 goes in setup_sections. */
407 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
408 powerpc_build_stubs (inbfd, outbfd, &symbols, &symcount);
409
410 /* Set up the sections. */
411 bfd_map_over_sections (inbfd, setup_sections, (PTR) outbfd);
412
413 text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
414
415 /* The .bss section immediately follows the .data section. */
416 data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
417 if (data_sec != NULL)
418 {
419 bfd_size_type add;
420
421 vma = bfd_get_section_size_before_reloc (data_sec);
422 align = 1 << bss_sec->alignment_power;
423 add = ((vma + align - 1) &~ (align - 1)) - vma;
424 vma += add;
425 if (! bfd_set_section_vma (outbfd, bss_sec, vma))
426 bfd_fatal ("set .bss vma");
427 if (add != 0)
428 {
429 bfd_size_type data_size;
430
431 data_size = bfd_get_section_size_before_reloc (data_sec);
432 if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
433 bfd_fatal ("set .data size");
434 }
435 }
436
437 /* Adjust symbol information. */
438 inlead = bfd_get_symbol_leading_char (inbfd);
439 outlead = bfd_get_symbol_leading_char (outbfd);
440 gotstart = false;
441 gotexit = false;
442 gotcheck = false;
443 newsymalloc = 10;
444 newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
445 newsymcount = 0;
446 endsym = NULL;
447 for (i = 0; i < symcount; i++)
448 {
449 register asymbol *sym;
450
451 sym = symbols[i];
452
453 /* Add or remove a leading underscore. */
454 if (inlead != outlead)
455 {
456 if (inlead != '\0')
457 {
458 if (bfd_asymbol_name (sym)[0] == inlead)
459 {
460 if (outlead == '\0')
461 ++sym->name;
462 else
463 {
464 char *new;
465
466 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
467 new[0] = outlead;
468 strcpy (new + 1, bfd_asymbol_name (sym) + 1);
469 sym->name = new;
470 }
471 }
472 }
473 else
474 {
475 char *new;
476
477 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
478 new[0] = outlead;
479 strcpy (new + 1, bfd_asymbol_name (sym));
480 sym->name = new;
481 }
482 }
483
484 /* NLM's have an uninitialized data section, but they do not
485 have a common section in the Unix sense. Move all common
486 symbols into the .bss section, and mark them as exported. */
487 if (bfd_is_com_section (bfd_get_section (sym)))
488 {
489 bfd_vma size;
490
491 sym->section = bss_sec;
492 size = sym->value;
493 sym->value = bss_sec->_raw_size;
494 bss_sec->_raw_size += size;
495 align = 1 << bss_sec->alignment_power;
496 bss_sec->_raw_size = (bss_sec->_raw_size + align - 1) &~ (align - 1);
497 sym->flags |= BSF_EXPORT | BSF_GLOBAL;
498 }
499 else if (bfd_get_section (sym)->output_section != NULL)
500 {
501 /* Move the symbol into the output section. */
502 sym->value += bfd_get_section (sym)->output_offset;
503 sym->section = bfd_get_section (sym)->output_section;
504 /* This is no longer a section symbol. */
505 sym->flags &=~ BSF_SECTION_SYM;
506 }
507
508 /* Force _edata and _end to be defined. This would normally be
509 done by the linker, but the manipulation of the common
510 symbols will confuse it. */
511 if ((sym->flags & BSF_DEBUGGING) == 0
512 && bfd_asymbol_name (sym)[0] == '_'
513 && bfd_get_section (sym) == &bfd_und_section)
514 {
515 if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
516 {
517 sym->section = bss_sec;
518 sym->value = 0;
519 }
520 if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
521 {
522 sym->section = bss_sec;
523 endsym = sym;
524 }
525
526 /* For PowerPC NetWare, we define __GOT0. This is the start
527 of the .got section. */
528 if (bfd_get_arch (inbfd) == bfd_arch_powerpc
529 && strcmp (bfd_asymbol_name (sym), "__GOT0") == 0)
530 {
531 asection *got_sec;
532
533 got_sec = bfd_get_section_by_name (inbfd, ".got");
534 assert (got_sec != (asection *) NULL);
535 sym->value = got_sec->output_offset;
536 sym->section = got_sec->output_section;
537 }
538 }
539
540 /* If this is a global symbol, check the export list. */
541 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
542 {
543 register struct string_list *l;
544 int found_simple;
545
546 /* Unfortunately, a symbol can appear multiple times on the
547 export list, with and without prefixes. */
548 found_simple = 0;
549 for (l = export_symbols; l != NULL; l = l->next)
550 {
551 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
552 found_simple = 1;
553 else
554 {
555 char *zbase;
556
557 zbase = strchr (l->string, '@');
558 if (zbase != NULL
559 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
560 {
561 /* We must add a symbol with this prefix. */
562 if (newsymcount >= newsymalloc)
563 {
564 newsymalloc += 10;
565 newsyms = ((asymbol **)
566 xrealloc ((PTR) newsyms,
567 (newsymalloc
568 * sizeof (asymbol *))));
569 }
570 newsyms[newsymcount] =
571 (asymbol *) xmalloc (sizeof (asymbol));
572 *newsyms[newsymcount] = *sym;
573 newsyms[newsymcount]->name = l->string;
574 ++newsymcount;
575 }
576 }
577 }
578 if (! found_simple)
579 {
580 /* The unmodified symbol is actually not exported at
581 all. */
582 sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
583 sym->flags |= BSF_LOCAL;
584 }
585 }
586
587 /* If it's an undefined symbol, see if it's on the import list.
588 Change the prefix if necessary. */
589 if (bfd_get_section (sym) == &bfd_und_section)
590 {
591 register struct string_list *l;
592
593 for (l = import_symbols; l != NULL; l = l->next)
594 {
595 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
596 break;
597 else
598 {
599 char *zbase;
600
601 zbase = strchr (l->string, '@');
602 if (zbase != NULL
603 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
604 {
605 sym->name = l->string;
606 break;
607 }
608 }
609 }
610 if (l == NULL)
611 fprintf (stderr,
612 "%s: warning: symbol %s imported but not in import list\n",
613 program_name, bfd_asymbol_name (sym));
614 }
615
616 /* See if it's one of the special named symbols. */
617 if ((sym->flags & BSF_DEBUGGING) == 0)
618 {
619 bfd_vma val;
620
621 /* FIXME: If these symbols are not in the .text section, we
622 add the .text section size to the value. This may not be
623 correct for all targets. I'm not sure how this should
624 really be handled. */
625 if (strcmp (bfd_asymbol_name (sym), start_procedure) == 0)
626 {
627 val = bfd_asymbol_value (sym);
628 if (bfd_get_section (sym) == data_sec
629 && text_sec != (asection *) NULL)
630 val += bfd_section_size (outbfd, text_sec);
631 if (! bfd_set_start_address (outbfd, val))
632 bfd_fatal ("set start address");
633 gotstart = true;
634 }
635 if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
636 {
637 val = bfd_asymbol_value (sym);
638 if (bfd_get_section (sym) == data_sec
639 && text_sec != (asection *) NULL)
640 val += bfd_section_size (outbfd, text_sec);
641 nlm_fixed_header (outbfd)->exitProcedureOffset = val;
642 gotexit = true;
643 }
644 if (check_procedure != NULL
645 && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
646 {
647 val = bfd_asymbol_value (sym);
648 if (bfd_get_section (sym) == data_sec
649 && text_sec != (asection *) NULL)
650 val += bfd_section_size (outbfd, text_sec);
651 nlm_fixed_header (outbfd)->checkUnloadProcedureOffset = val;
652 gotcheck = true;
653 }
654 }
655 }
656
657 if (endsym != NULL)
658 {
659 endsym->value = bfd_get_section_size_before_reloc (bss_sec);
660
661 /* FIXME: If any relocs referring to _end use inplace addends,
662 then I think they need to be updated. This is handled by
663 i386_mangle_relocs. Is it needed for any other object
664 formats? */
665 }
666
667 if (newsymcount == 0)
668 outsyms = symbols;
669 else
670 {
671 outsyms = (asymbol **) xmalloc ((symcount + newsymcount + 1)
672 * sizeof (asymbol *));
673 memcpy (outsyms, symbols, symcount * sizeof (asymbol *));
674 memcpy (outsyms + symcount, newsyms, newsymcount * sizeof (asymbol *));
675 outsyms[symcount + newsymcount] = NULL;
676 }
677
678 bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
679
680 if (! gotstart)
681 fprintf (stderr, "%s: warning: START procedure %s not defined\n",
682 program_name, start_procedure);
683 if (! gotexit)
684 fprintf (stderr, "%s: warning: EXIT procedure %s not defined\n",
685 program_name, exit_procedure);
686 if (check_procedure != NULL
687 && ! gotcheck)
688 fprintf (stderr, "%s: warning: CHECK procedure %s not defined\n",
689 program_name, check_procedure);
690
691 /* Add additional sections required for the header information. */
692 if (custom_file != NULL)
693 {
694 custom_data = fopen (custom_file, "r");
695 if (custom_data == NULL
696 || fstat (fileno (custom_data), &st) < 0)
697 {
698 fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
699 strerror (errno));
700 custom_file = NULL;
701 }
702 else
703 {
704 custom_size = st.st_size;
705 custom_section = bfd_make_section (outbfd, ".nlmcustom");
706 if (custom_section == NULL
707 || ! bfd_set_section_size (outbfd, custom_section, custom_size)
708 || ! bfd_set_section_flags (outbfd, custom_section,
709 SEC_HAS_CONTENTS))
710 bfd_fatal ("custom section");
711 }
712 }
713 if (help_file != NULL)
714 {
715 help_data = fopen (help_file, "r");
716 if (help_data == NULL
717 || fstat (fileno (help_data), &st) < 0)
718 {
719 fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
720 strerror (errno));
721 help_file = NULL;
722 }
723 else
724 {
725 help_size = st.st_size;
726 help_section = bfd_make_section (outbfd, ".nlmhelp");
727 if (help_section == NULL
728 || ! bfd_set_section_size (outbfd, help_section, help_size)
729 || ! bfd_set_section_flags (outbfd, help_section,
730 SEC_HAS_CONTENTS))
731 bfd_fatal ("help section");
732 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
733 }
734 }
735 if (message_file != NULL)
736 {
737 message_data = fopen (message_file, "r");
738 if (message_data == NULL
739 || fstat (fileno (message_data), &st) < 0)
740 {
741 fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
742 strerror (errno));
743 message_file = NULL;
744 }
745 else
746 {
747 message_size = st.st_size;
748 message_section = bfd_make_section (outbfd, ".nlmmessages");
749 if (message_section == NULL
750 || ! bfd_set_section_size (outbfd, message_section, message_size)
751 || ! bfd_set_section_flags (outbfd, message_section,
752 SEC_HAS_CONTENTS))
753 bfd_fatal ("message section");
754 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
755 }
756 }
757 if (modules != NULL)
758 {
759 struct string_list *l;
760
761 module_size = 0;
762 for (l = modules; l != NULL; l = l->next)
763 module_size += strlen (l->string) + 1;
764 module_section = bfd_make_section (outbfd, ".nlmmodules");
765 if (module_section == NULL
766 || ! bfd_set_section_size (outbfd, module_section, module_size)
767 || ! bfd_set_section_flags (outbfd, module_section,
768 SEC_HAS_CONTENTS))
769 bfd_fatal ("module section");
770 }
771 if (rpc_file != NULL)
772 {
773 rpc_data = fopen (rpc_file, "r");
774 if (rpc_data == NULL
775 || fstat (fileno (rpc_data), &st) < 0)
776 {
777 fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
778 strerror (errno));
779 rpc_file = NULL;
780 }
781 else
782 {
783 rpc_size = st.st_size;
784 rpc_section = bfd_make_section (outbfd, ".nlmrpc");
785 if (rpc_section == NULL
786 || ! bfd_set_section_size (outbfd, rpc_section, rpc_size)
787 || ! bfd_set_section_flags (outbfd, rpc_section,
788 SEC_HAS_CONTENTS))
789 bfd_fatal ("rpc section");
790 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
791 }
792 }
793 if (sharelib_file != NULL)
794 {
795 sharedbfd = bfd_openr (sharelib_file, output_format);
796 if (sharedbfd == NULL
797 || ! bfd_check_format (sharedbfd, bfd_object))
798 {
799 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
800 bfd_errmsg (bfd_get_error ()));
801 sharelib_file = NULL;
802 }
803 else
804 {
805 sharedhdr = *nlm_fixed_header (sharedbfd);
806 bfd_close (sharedbfd);
807 shared_data = fopen (sharelib_file, "r");
808 if (shared_data == NULL
809 || (fstat (fileno (shared_data), &st) < 0))
810 {
811 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
812 strerror (errno));
813 sharelib_file = NULL;
814 }
815 else
816 {
817 /* If we were clever, we could just copy out the
818 sections of the shared library which we actually
819 need. However, we would have to figure out the sizes
820 of the external and public information, and that can
821 not be done without reading through them. */
822 if (sharedhdr.uninitializedDataSize > 0)
823 {
824 /* There is no place to record this information. */
825 fprintf (stderr,
826 "%s:%s: warning: shared libraries can not have uninitialized data\n",
827 program_name, sharelib_file);
828 }
829 shared_offset = st.st_size;
830 if (shared_offset > sharedhdr.codeImageOffset)
831 shared_offset = sharedhdr.codeImageOffset;
832 if (shared_offset > sharedhdr.dataImageOffset)
833 shared_offset = sharedhdr.dataImageOffset;
834 if (shared_offset > sharedhdr.relocationFixupOffset)
835 shared_offset = sharedhdr.relocationFixupOffset;
836 if (shared_offset > sharedhdr.externalReferencesOffset)
837 shared_offset = sharedhdr.externalReferencesOffset;
838 if (shared_offset > sharedhdr.publicsOffset)
839 shared_offset = sharedhdr.publicsOffset;
840 shared_size = st.st_size - shared_offset;
841 shared_section = bfd_make_section (outbfd, ".nlmshared");
842 if (shared_section == NULL
843 || ! bfd_set_section_size (outbfd, shared_section,
844 shared_size)
845 || ! bfd_set_section_flags (outbfd, shared_section,
846 SEC_HAS_CONTENTS))
847 bfd_fatal ("shared section");
848 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
849 }
850 }
851 }
852
853 /* Check whether a version was given. */
854 if (strncmp (version_hdr->stamp, "VeRsIoN#", 8) != 0)
855 fprintf (stderr, "%s: warning: No version number given\n",
856 program_name);
857
858 /* At least for now, always create an extended header, because that
859 is what NLMLINK does. */
860 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
861
862 strncpy (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx", 8);
863
864 /* If the date was not given, force it in. */
865 if (nlm_version_header (outbfd)->month == 0
866 && nlm_version_header (outbfd)->day == 0
867 && nlm_version_header (outbfd)->year == 0)
868 {
869 time_t now;
870 struct tm *ptm;
871
872 time (&now);
873 ptm = localtime (&now);
874 nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
875 nlm_version_header (outbfd)->day = ptm->tm_mday;
876 nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
877 strncpy (version_hdr->stamp, "VeRsIoN#", 8);
878 }
879
880 /* Resolve the stubs we build for PowerPC NetWare. */
881 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
882 powerpc_resolve_stubs (inbfd, outbfd);
883
884 /* Copy over the sections. */
885 bfd_map_over_sections (inbfd, copy_sections, (PTR) outbfd);
886
887 /* Finish up the header information. */
888 if (custom_file != NULL)
889 {
890 PTR data;
891
892 data = xmalloc (custom_size);
893 if (fread (data, 1, custom_size, custom_data) != custom_size)
894 fprintf (stderr, "%s:%s: read: %s\n", program_name, custom_file,
895 strerror (errno));
896 else
897 {
898 if (! bfd_set_section_contents (outbfd, custom_section, data,
899 (file_ptr) 0, custom_size))
900 bfd_fatal ("custom section");
901 nlm_fixed_header (outbfd)->customDataOffset =
902 custom_section->filepos;
903 nlm_fixed_header (outbfd)->customDataSize = custom_size;
904 }
905 free (data);
906 }
907 if (! debug_info)
908 {
909 /* As a special hack, the backend recognizes a debugInfoOffset
910 of -1 to mean that it should not output any debugging
911 information. This can not be handling by fiddling with the
912 symbol table because exported symbols appear in both the
913 export information and the debugging information. */
914 nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
915 }
916 if (map_file != NULL)
917 fprintf (stderr,
918 "%s: warning: MAP and FULLMAP are not supported; try ld -M\n",
919 program_name);
920 if (help_file != NULL)
921 {
922 PTR data;
923
924 data = xmalloc (help_size);
925 if (fread (data, 1, help_size, help_data) != help_size)
926 fprintf (stderr, "%s:%s: read: %s\n", program_name, help_file,
927 strerror (errno));
928 else
929 {
930 if (! bfd_set_section_contents (outbfd, help_section, data,
931 (file_ptr) 0, help_size))
932 bfd_fatal ("help section");
933 nlm_extended_header (outbfd)->helpFileOffset =
934 help_section->filepos;
935 nlm_extended_header (outbfd)->helpFileLength = help_size;
936 }
937 free (data);
938 }
939 if (message_file != NULL)
940 {
941 PTR data;
942
943 data = xmalloc (message_size);
944 if (fread (data, 1, message_size, message_data) != message_size)
945 fprintf (stderr, "%s:%s: read: %s\n", program_name, message_file,
946 strerror (errno));
947 else
948 {
949 if (! bfd_set_section_contents (outbfd, message_section, data,
950 (file_ptr) 0, message_size))
951 bfd_fatal ("message section");
952 nlm_extended_header (outbfd)->messageFileOffset =
953 message_section->filepos;
954 nlm_extended_header (outbfd)->messageFileLength = message_size;
955
956 /* FIXME: Are these offsets correct on all platforms? Are
957 they 32 bits on all platforms? What endianness? */
958 nlm_extended_header (outbfd)->languageID =
959 bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
960 nlm_extended_header (outbfd)->messageCount =
961 bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
962 }
963 free (data);
964 }
965 if (modules != NULL)
966 {
967 PTR data;
968 unsigned char *set;
969 struct string_list *l;
970 bfd_size_type c;
971
972 data = xmalloc (module_size);
973 c = 0;
974 set = (unsigned char *) data;
975 for (l = modules; l != NULL; l = l->next)
976 {
977 *set = strlen (l->string);
978 strncpy (set + 1, l->string, *set);
979 set += *set + 1;
980 ++c;
981 }
982 if (! bfd_set_section_contents (outbfd, module_section, data,
983 (file_ptr) 0, module_size))
984 bfd_fatal ("module section");
985 nlm_fixed_header (outbfd)->moduleDependencyOffset =
986 module_section->filepos;
987 nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
988 }
989 if (rpc_file != NULL)
990 {
991 PTR data;
992
993 data = xmalloc (rpc_size);
994 if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
995 fprintf (stderr, "%s:%s: read: %s\n", program_name, rpc_file,
996 strerror (errno));
997 else
998 {
999 if (! bfd_set_section_contents (outbfd, rpc_section, data,
1000 (file_ptr) 0, rpc_size))
1001 bfd_fatal ("rpc section");
1002 nlm_extended_header (outbfd)->RPCDataOffset =
1003 rpc_section->filepos;
1004 nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
1005 }
1006 free (data);
1007 }
1008 if (sharelib_file != NULL)
1009 {
1010 PTR data;
1011
1012 data = xmalloc (shared_size);
1013 if (fseek (shared_data, shared_offset, SEEK_SET) != 0
1014 || fread (data, 1, shared_size, shared_data) != shared_size)
1015 fprintf (stderr, "%s:%s: read: %s\n", program_name, sharelib_file,
1016 strerror (errno));
1017 else
1018 {
1019 if (! bfd_set_section_contents (outbfd, shared_section, data,
1020 (file_ptr) 0, shared_size))
1021 bfd_fatal ("shared section");
1022 }
1023 nlm_extended_header (outbfd)->sharedCodeOffset =
1024 sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
1025 nlm_extended_header (outbfd)->sharedCodeLength =
1026 sharedhdr.codeImageSize;
1027 nlm_extended_header (outbfd)->sharedDataOffset =
1028 sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
1029 nlm_extended_header (outbfd)->sharedDataLength =
1030 sharedhdr.dataImageSize;
1031 nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
1032 (sharedhdr.relocationFixupOffset
1033 - shared_offset
1034 + shared_section->filepos);
1035 nlm_extended_header (outbfd)->sharedRelocationFixupCount =
1036 sharedhdr.numberOfRelocationFixups;
1037 nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
1038 (sharedhdr.externalReferencesOffset
1039 - shared_offset
1040 + shared_section->filepos);
1041 nlm_extended_header (outbfd)->sharedExternalReferenceCount =
1042 sharedhdr.numberOfExternalReferences;
1043 nlm_extended_header (outbfd)->sharedPublicsOffset =
1044 sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
1045 nlm_extended_header (outbfd)->sharedPublicsCount =
1046 sharedhdr.numberOfPublics;
1047 nlm_extended_header (outbfd)->sharedDebugRecordOffset =
1048 sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
1049 nlm_extended_header (outbfd)->sharedDebugRecordCount =
1050 sharedhdr.numberOfDebugRecords;
1051 nlm_extended_header (outbfd)->SharedInitializationOffset =
1052 sharedhdr.codeStartOffset;
1053 nlm_extended_header (outbfd)->SharedExitProcedureOffset =
1054 sharedhdr.exitProcedureOffset;
1055 free (data);
1056 }
1057 len = strlen (output_file);
1058 if (len > NLM_MODULE_NAME_SIZE - 2)
1059 len = NLM_MODULE_NAME_SIZE - 2;
1060 nlm_fixed_header (outbfd)->moduleName[0] = len;
1061
1062 strncpy (nlm_fixed_header (outbfd)->moduleName + 1, output_file,
1063 NLM_MODULE_NAME_SIZE - 2);
1064 nlm_fixed_header (outbfd)->moduleName[NLM_MODULE_NAME_SIZE - 1] = '\0';
1065 for (modname = nlm_fixed_header (outbfd)->moduleName;
1066 *modname != '\0';
1067 modname++)
1068 if (islower (*modname))
1069 *modname = toupper (*modname);
1070
1071 strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
1072 NLM_OLD_THREAD_NAME_LENGTH);
1073
1074 nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
1075 nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
1076
1077 if (! bfd_close (outbfd))
1078 bfd_fatal (output_file);
1079 if (! bfd_close (inbfd))
1080 bfd_fatal (input_file);
1081
1082 if (unlink_on_exit != NULL)
1083 unlink (unlink_on_exit);
1084
1085 return 0;
1086 }
1087 \f
1088 /* Display a help message and exit. */
1089
1090 static void
1091 show_help ()
1092 {
1093 printf ("%s: Convert an object file into a NetWare Loadable Module\n",
1094 program_name);
1095 show_usage (stdout, 0);
1096 }
1097
1098 /* Show a usage message and exit. */
1099
1100 static void
1101 show_usage (file, status)
1102 FILE *file;
1103 int status;
1104 {
1105 fprintf (file, "\
1106 Usage: %s [-dhV] [-I bfdname] [-O bfdname] [-T header-file] [-l linker]\n\
1107 [--input-target=bfdname] [--output-target=bfdname]\n\
1108 [--header-file=file] [--linker=linker] [--debug]\n\
1109 [--help] [--version]\n\
1110 [in-file [out-file]]\n",
1111 program_name);
1112 exit (status);
1113 }
1114 \f
1115 /* Select the output format based on the input architecture, machine,
1116 and endianness. This chooses the appropriate NLM target. */
1117
1118 static const char *
1119 select_output_format (arch, mach, bigendian)
1120 enum bfd_architecture arch;
1121 unsigned long mach;
1122 boolean bigendian;
1123 {
1124 switch (arch)
1125 {
1126 case bfd_arch_i386:
1127 return "nlm32-i386";
1128 case bfd_arch_sparc:
1129 return "nlm32-sparc";
1130 case bfd_arch_alpha:
1131 return "nlm32-alpha";
1132 case bfd_arch_powerpc:
1133 return "nlm32-powerpc";
1134 default:
1135 fprintf (stderr, "%s: no default NLM format for %s\n",
1136 program_name, bfd_printable_arch_mach (arch, mach));
1137 exit (1);
1138 /* Avoid warning. */
1139 return NULL;
1140 }
1141 /*NOTREACHED*/
1142 }
1143 \f
1144 /* The BFD sections are copied in two passes. This function selects
1145 the output section for each input section, and sets up the section
1146 name, size, etc. */
1147
1148 static void
1149 setup_sections (inbfd, insec, data_ptr)
1150 bfd *inbfd;
1151 asection *insec;
1152 PTR data_ptr;
1153 {
1154 bfd *outbfd = (bfd *) data_ptr;
1155 flagword f;
1156 const char *outname;
1157 asection *outsec;
1158 bfd_vma offset;
1159 bfd_size_type align;
1160 bfd_size_type add;
1161 bfd_size_type secsecsize;
1162
1163 f = bfd_get_section_flags (inbfd, insec);
1164 if (f & SEC_CODE)
1165 outname = NLM_CODE_NAME;
1166 else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
1167 outname = NLM_INITIALIZED_DATA_NAME;
1168 else if (f & SEC_ALLOC)
1169 outname = NLM_UNINITIALIZED_DATA_NAME;
1170 else
1171 outname = bfd_section_name (inbfd, insec);
1172
1173 outsec = bfd_get_section_by_name (outbfd, outname);
1174 if (outsec == NULL)
1175 {
1176 outsec = bfd_make_section (outbfd, outname);
1177 if (outsec == NULL)
1178 bfd_fatal ("make section");
1179 }
1180
1181 insec->output_section = outsec;
1182
1183 offset = bfd_section_size (outbfd, outsec);
1184 align = 1 << bfd_section_alignment (inbfd, insec);
1185 add = ((offset + align - 1) &~ (align - 1)) - offset;
1186 insec->output_offset = offset + add;
1187
1188 if (! bfd_set_section_size (outbfd, outsec,
1189 (bfd_section_size (outbfd, outsec)
1190 + bfd_section_size (inbfd, insec)
1191 + add)))
1192 bfd_fatal ("set section size");
1193
1194 if ((bfd_section_alignment (inbfd, insec)
1195 > bfd_section_alignment (outbfd, outsec))
1196 && ! bfd_set_section_alignment (outbfd, outsec,
1197 bfd_section_alignment (inbfd, insec)))
1198 bfd_fatal ("set section alignment");
1199
1200 if (! bfd_set_section_flags (outbfd, outsec, f))
1201 bfd_fatal ("set section flags");
1202
1203 bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
1204
1205 /* For each input section we allocate space for an entry in
1206 .nlmsections. */
1207 secsecsize = bfd_section_size (outbfd, secsec);
1208 secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
1209 secsecsize = (secsecsize + 3) &~ 3;
1210 secsecsize += 8;
1211 if (! bfd_set_section_size (outbfd, secsec, secsecsize))
1212 bfd_fatal ("set .nlmsections size");
1213 }
1214
1215 /* Copy the section contents. */
1216
1217 static void
1218 copy_sections (inbfd, insec, data_ptr)
1219 bfd *inbfd;
1220 asection *insec;
1221 PTR data_ptr;
1222 {
1223 static bfd_size_type secsecoff = 0;
1224 bfd *outbfd = (bfd *) data_ptr;
1225 const char *inname;
1226 asection *outsec;
1227 bfd_size_type size;
1228 PTR contents;
1229 long reloc_size;
1230 bfd_byte buf[4];
1231 bfd_size_type add;
1232
1233 inname = bfd_section_name (inbfd, insec);
1234
1235 outsec = insec->output_section;
1236 assert (outsec != NULL);
1237
1238 size = bfd_get_section_size_before_reloc (insec);
1239
1240 /* FIXME: Why are these necessary? */
1241 insec->_cooked_size = insec->_raw_size;
1242 insec->reloc_done = true;
1243
1244 if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
1245 contents = NULL;
1246 else
1247 {
1248 contents = xmalloc (size);
1249 if (! bfd_get_section_contents (inbfd, insec, contents,
1250 (file_ptr) 0, size))
1251 bfd_fatal (bfd_get_filename (inbfd));
1252 }
1253
1254 reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
1255 if (reloc_size < 0)
1256 bfd_fatal (bfd_get_filename (inbfd));
1257 if (reloc_size != 0)
1258 {
1259 arelent **relocs;
1260 long reloc_count;
1261
1262 relocs = (arelent **) xmalloc (reloc_size);
1263 reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
1264 if (reloc_count < 0)
1265 bfd_fatal (bfd_get_filename (inbfd));
1266 mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
1267 size);
1268
1269 /* FIXME: refers to internal BFD fields. */
1270 if (outsec->orelocation != (arelent **) NULL)
1271 {
1272 bfd_size_type total_count;
1273 arelent **combined;
1274
1275 total_count = reloc_count + outsec->reloc_count;
1276 combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
1277 memcpy (combined, outsec->orelocation,
1278 outsec->reloc_count * sizeof (arelent *));
1279 memcpy (combined + outsec->reloc_count, relocs,
1280 (size_t) (reloc_count * sizeof (arelent *)));
1281 free (outsec->orelocation);
1282 reloc_count = total_count;
1283 relocs = combined;
1284 }
1285
1286 bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
1287 }
1288
1289 if (contents != NULL)
1290 {
1291 if (! bfd_set_section_contents (outbfd, outsec, contents,
1292 insec->output_offset, size))
1293 bfd_fatal (bfd_get_filename (outbfd));
1294 free (contents);
1295 }
1296
1297 /* Add this section to .nlmsections. */
1298 if (! bfd_set_section_contents (outbfd, secsec, (PTR) inname, secsecoff,
1299 strlen (inname) + 1))
1300 bfd_fatal ("set .nlmsection contents");
1301 secsecoff += strlen (inname) + 1;
1302
1303 add = ((secsecoff + 3) &~ 3) - secsecoff;
1304 if (add != 0)
1305 {
1306 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1307 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
1308 bfd_fatal ("set .nlmsection contents");
1309 secsecoff += add;
1310 }
1311
1312 if (contents != NULL)
1313 bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
1314 else
1315 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1316 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1317 bfd_fatal ("set .nlmsection contents");
1318 secsecoff += 4;
1319
1320 bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
1321 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1322 bfd_fatal ("set .nlmsection contents");
1323 secsecoff += 4;
1324 }
1325
1326 /* Some, perhaps all, NetWare targets require changing the relocs used
1327 by the input formats. */
1328
1329 static void
1330 mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1331 contents_size)
1332 bfd *outbfd;
1333 asection *insec;
1334 arelent ***relocs_ptr;
1335 long *reloc_count_ptr;
1336 char *contents;
1337 bfd_size_type contents_size;
1338 {
1339 switch (bfd_get_arch (outbfd))
1340 {
1341 case bfd_arch_i386:
1342 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1343 contents, contents_size);
1344 break;
1345 case bfd_arch_alpha:
1346 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1347 contents, contents_size);
1348 break;
1349 case bfd_arch_powerpc:
1350 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1351 contents, contents_size);
1352 break;
1353 default:
1354 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1355 contents, contents_size);
1356 break;
1357 }
1358 }
1359
1360 /* By default all we need to do for relocs is change the address by
1361 the output_offset. */
1362
1363 /*ARGSUSED*/
1364 static void
1365 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1366 contents_size)
1367 bfd *outbfd;
1368 asection *insec;
1369 arelent ***relocs_ptr;
1370 long *reloc_count_ptr;
1371 char *contents;
1372 bfd_size_type contents_size;
1373 {
1374 if (insec->output_offset != 0)
1375 {
1376 long reloc_count;
1377 register arelent **relocs;
1378 register long i;
1379
1380 reloc_count = *reloc_count_ptr;
1381 relocs = *relocs_ptr;
1382 for (i = 0; i < reloc_count; i++, relocs++)
1383 (*relocs)->address += insec->output_offset;
1384 }
1385 }
1386
1387 /* NetWare on the i386 supports a restricted set of relocs, which are
1388 different from those used on other i386 targets. This routine
1389 converts the relocs. It is, obviously, very target dependent. At
1390 the moment, the nlm32-i386 backend performs similar translations;
1391 however, it is more reliable and efficient to do them here. */
1392
1393 static reloc_howto_type nlm_i386_pcrel_howto =
1394 HOWTO (1, /* type */
1395 0, /* rightshift */
1396 2, /* size (0 = byte, 1 = short, 2 = long) */
1397 32, /* bitsize */
1398 true, /* pc_relative */
1399 0, /* bitpos */
1400 complain_overflow_signed, /* complain_on_overflow */
1401 0, /* special_function */
1402 "DISP32", /* name */
1403 true, /* partial_inplace */
1404 0xffffffff, /* src_mask */
1405 0xffffffff, /* dst_mask */
1406 true); /* pcrel_offset */
1407
1408 static void
1409 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1410 contents_size)
1411 bfd *outbfd;
1412 asection *insec;
1413 arelent ***relocs_ptr;
1414 long *reloc_count_ptr;
1415 char *contents;
1416 bfd_size_type contents_size;
1417 {
1418 long reloc_count, i;
1419 arelent **relocs;
1420
1421 reloc_count = *reloc_count_ptr;
1422 relocs = *relocs_ptr;
1423 for (i = 0; i < reloc_count; i++)
1424 {
1425 arelent *rel;
1426 asymbol *sym;
1427 bfd_size_type address;
1428 bfd_vma addend;
1429
1430 rel = *relocs++;
1431 sym = *rel->sym_ptr_ptr;
1432
1433 /* We're moving the relocs from the input section to the output
1434 section, so we must adjust the address accordingly. */
1435 address = rel->address;
1436 rel->address += insec->output_offset;
1437
1438 /* Note that no serious harm will ensue if we fail to change a
1439 reloc. The backend will fail when writing out the reloc. */
1440
1441 /* Make sure this reloc is within the data we have. We use only
1442 4 byte relocs here, so we insist on having 4 bytes. */
1443 if (address + 4 > contents_size)
1444 continue;
1445
1446 /* A PC relative reloc entirely within a single section is
1447 completely unnecessary. This can be generated by ld -r. */
1448 if (sym == insec->symbol
1449 && rel->howto != NULL
1450 && rel->howto->pc_relative
1451 && ! rel->howto->pcrel_offset)
1452 {
1453 --*reloc_count_ptr;
1454 --relocs;
1455 memmove (relocs, relocs + 1,
1456 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1457 continue;
1458 }
1459
1460 /* Get the amount the relocation will add in. */
1461 addend = rel->addend + sym->value;
1462
1463 /* NetWare doesn't support PC relative relocs against defined
1464 symbols, so we have to eliminate them by doing the relocation
1465 now. We can only do this if the reloc is within a single
1466 section. */
1467 if (rel->howto != NULL
1468 && rel->howto->pc_relative
1469 && bfd_get_section (sym) == insec->output_section)
1470 {
1471 bfd_vma val;
1472
1473 if (rel->howto->pcrel_offset)
1474 addend -= address;
1475
1476 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1477 val += addend;
1478 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1479
1480 --*reloc_count_ptr;
1481 --relocs;
1482 memmove (relocs, relocs + 1,
1483 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1484 continue;
1485 }
1486
1487 /* NetWare doesn't support reloc addends, so we get rid of them
1488 here by simply adding them into the object data. We handle
1489 the symbol value, if any, the same way. */
1490 if (addend != 0
1491 && rel->howto != NULL
1492 && rel->howto->rightshift == 0
1493 && rel->howto->size == 2
1494 && rel->howto->bitsize == 32
1495 && rel->howto->bitpos == 0
1496 && rel->howto->src_mask == 0xffffffff
1497 && rel->howto->dst_mask == 0xffffffff)
1498 {
1499 bfd_vma val;
1500
1501 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1502 val += addend;
1503 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1504
1505 /* Adjust the reloc for the changes we just made. */
1506 rel->addend = 0;
1507 if (bfd_get_section (sym) != &bfd_und_section)
1508 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
1509 }
1510
1511 /* NetWare uses a reloc with pcrel_offset set. We adjust
1512 pc_relative relocs accordingly. We are going to change the
1513 howto field, so we can only do this if the current one is
1514 compatible. We should check that special_function is NULL
1515 here, but at the moment coff-i386 uses a special_function
1516 which does not affect what we are doing here. */
1517 if (rel->howto != NULL
1518 && rel->howto->pc_relative
1519 && ! rel->howto->pcrel_offset
1520 && rel->howto->rightshift == 0
1521 && rel->howto->size == 2
1522 && rel->howto->bitsize == 32
1523 && rel->howto->bitpos == 0
1524 && rel->howto->src_mask == 0xffffffff
1525 && rel->howto->dst_mask == 0xffffffff)
1526 {
1527 bfd_vma val;
1528
1529 /* When pcrel_offset is not set, it means that the negative
1530 of the address of the memory location is stored in the
1531 memory location. We must add it back in. */
1532 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1533 val += address;
1534 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1535
1536 /* We must change to a new howto. */
1537 rel->howto = &nlm_i386_pcrel_howto;
1538 }
1539 }
1540 }
1541
1542 /* On the Alpha the first reloc for every section must be a special
1543 relocs which hold the GP address. Also, the first reloc in the
1544 file must be a special reloc which holds the address of the .lita
1545 section. */
1546
1547 static reloc_howto_type nlm32_alpha_nw_howto =
1548 HOWTO (ALPHA_R_NW_RELOC, /* type */
1549 0, /* rightshift */
1550 0, /* size (0 = byte, 1 = short, 2 = long) */
1551 0, /* bitsize */
1552 false, /* pc_relative */
1553 0, /* bitpos */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 0, /* special_function */
1556 "NW_RELOC", /* name */
1557 false, /* partial_inplace */
1558 0, /* src_mask */
1559 0, /* dst_mask */
1560 false); /* pcrel_offset */
1561
1562 /*ARGSUSED*/
1563 static void
1564 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1565 contents_size)
1566 bfd *outbfd;
1567 asection *insec;
1568 register arelent ***relocs_ptr;
1569 long *reloc_count_ptr;
1570 char *contents;
1571 bfd_size_type contents_size;
1572 {
1573 long old_reloc_count;
1574 arelent **old_relocs;
1575 register arelent **relocs;
1576
1577 old_reloc_count = *reloc_count_ptr;
1578 old_relocs = *relocs_ptr;
1579 relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
1580 *relocs_ptr = relocs;
1581
1582 if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
1583 {
1584 bfd *inbfd;
1585 asection *lita_section;
1586
1587 inbfd = insec->owner;
1588 lita_section = bfd_get_section_by_name (inbfd, _LITA);
1589 if (lita_section != (asection *) NULL)
1590 {
1591 nlm_alpha_backend_data (outbfd)->lita_address =
1592 bfd_get_section_vma (inbfd, lita_section);
1593 nlm_alpha_backend_data (outbfd)->lita_size =
1594 bfd_section_size (inbfd, lita_section);
1595 }
1596 else
1597 {
1598 /* Avoid outputting this reloc again. */
1599 nlm_alpha_backend_data (outbfd)->lita_address = 4;
1600 }
1601
1602 *relocs = (arelent *) xmalloc (sizeof (arelent));
1603 (*relocs)->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1604 (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
1605 (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
1606 (*relocs)->howto = &nlm32_alpha_nw_howto;
1607 ++relocs;
1608 ++(*reloc_count_ptr);
1609 }
1610
1611 /* Get the GP value from bfd. */
1612 if (nlm_alpha_backend_data (outbfd)->gp == 0)
1613 nlm_alpha_backend_data (outbfd)->gp =
1614 bfd_ecoff_get_gp_value (insec->owner);
1615
1616 *relocs = (arelent *) xmalloc (sizeof (arelent));
1617 (*relocs)->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1618 (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
1619 (*relocs)->addend = 0;
1620 (*relocs)->howto = &nlm32_alpha_nw_howto;
1621 ++relocs;
1622 ++(*reloc_count_ptr);
1623
1624 memcpy ((PTR) relocs, (PTR) old_relocs,
1625 (size_t) old_reloc_count * sizeof (arelent *));
1626 relocs[old_reloc_count] = (arelent *) NULL;
1627
1628 free (old_relocs);
1629
1630 if (insec->output_offset != 0)
1631 {
1632 register bfd_size_type i;
1633
1634 for (i = 0; i < old_reloc_count; i++, relocs++)
1635 (*relocs)->address += insec->output_offset;
1636 }
1637 }
1638
1639 /* We keep a linked list of stubs which we must build. Because BFD
1640 requires us to know the sizes of all sections before we can set the
1641 contents of any, we must figure out which stubs we want to build
1642 before we can actually build any of them. */
1643
1644 struct powerpc_stub
1645 {
1646 /* Next stub in linked list. */
1647 struct powerpc_stub *next;
1648
1649 /* Symbol whose value is the start of the stub. This is a symbol
1650 whose name begins with `.'. */
1651 asymbol *start;
1652
1653 /* Symbol we are going to create a reloc against. This is a symbol
1654 with the same name as START but without the leading `.'. */
1655 asymbol *reloc;
1656
1657 /* The TOC index for this stub. This is the index into the TOC
1658 section at which the reloc is created. */
1659 unsigned int toc_index;
1660 };
1661
1662 /* The linked list of stubs. */
1663
1664 static struct powerpc_stub *powerpc_stubs;
1665
1666 /* This is what a stub looks like. The first instruction will get
1667 adjusted with the correct TOC index. */
1668
1669 static unsigned long powerpc_stub_insns[] =
1670 {
1671 0x81820000, /* lwz r12,0(r2) */
1672 0x90410014, /* stw r2,20(r1) */
1673 0x800c0000, /* lwz r0,0(r12) */
1674 0x804c0004, /* lwz r2,r(r12) */
1675 0x7c0903a6, /* mtctr r0 */
1676 0x4e800420, /* bctr */
1677 0, /* Traceback table. */
1678 0xc8000,
1679 0
1680 };
1681
1682 #define POWERPC_STUB_INSN_COUNT \
1683 (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
1684
1685 #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
1686
1687 /* Each stub uses a four byte TOC entry. */
1688 #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
1689
1690 /* The original size of the .got section. */
1691 static bfd_size_type powerpc_initial_got_size;
1692
1693 /* Look for all undefined symbols beginning with `.', and prepare to
1694 build a stub for each one. */
1695
1696 static void
1697 powerpc_build_stubs (inbfd, outbfd, symbols_ptr, symcount_ptr)
1698 bfd *inbfd;
1699 bfd *outbfd;
1700 asymbol ***symbols_ptr;
1701 long *symcount_ptr;
1702 {
1703 asection *stub_sec;
1704 asection *got_sec;
1705 unsigned int got_base;
1706 long i;
1707 long symcount;
1708 long stubcount;
1709
1710 /* Make a section to hold stubs. We don't set SEC_HAS_CONTENTS for
1711 the section to prevent copy_sections from reading from it. */
1712 stub_sec = bfd_make_section (inbfd, ".stubs");
1713 if (stub_sec == (asection *) NULL
1714 || ! bfd_set_section_flags (inbfd, stub_sec,
1715 (SEC_CODE
1716 | SEC_RELOC
1717 | SEC_ALLOC
1718 | SEC_LOAD))
1719 || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
1720 bfd_fatal (".stubs");
1721
1722 /* Get the TOC section, which is named .got. */
1723 got_sec = bfd_get_section_by_name (inbfd, ".got");
1724 if (got_sec == (asection *) NULL)
1725 {
1726 got_sec = bfd_make_section (inbfd, ".got");
1727 if (got_sec == (asection *) NULL
1728 || ! bfd_set_section_flags (inbfd, got_sec,
1729 (SEC_DATA
1730 | SEC_RELOC
1731 | SEC_ALLOC
1732 | SEC_LOAD
1733 | SEC_HAS_CONTENTS))
1734 || ! bfd_set_section_alignment (inbfd, got_sec, 2))
1735 bfd_fatal (".got");
1736 }
1737
1738 powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
1739 got_base = powerpc_initial_got_size;
1740 got_base = (got_base + 3) &~ 3;
1741
1742 stubcount = 0;
1743
1744 symcount = *symcount_ptr;
1745 for (i = 0; i < symcount; i++)
1746 {
1747 asymbol *sym;
1748 asymbol *newsym;
1749 char *newname;
1750 struct powerpc_stub *item;
1751
1752 sym = (*symbols_ptr)[i];
1753
1754 /* We must make a stub for every undefined symbol whose name
1755 starts with '.'. */
1756 if (bfd_asymbol_name (sym)[0] != '.'
1757 || bfd_get_section (sym) != &bfd_und_section)
1758 continue;
1759
1760 /* Make a new undefined symbol with the same name but without
1761 the leading `.'. */
1762 newsym = (asymbol *) xmalloc (sizeof (asymbol));
1763 *newsym = *sym;
1764 newname = (char *) xmalloc (strlen (bfd_asymbol_name (sym)));
1765 strcpy (newname, bfd_asymbol_name (sym) + 1);
1766 newsym->name = newname;
1767
1768 /* Define the `.' symbol to be in the stub section. */
1769 sym->section = stub_sec;
1770 sym->value = stubcount * POWERPC_STUB_SIZE;
1771 /* We set the BSF_DYNAMIC flag here so that we can check it when
1772 we are mangling relocs. FIXME: This is a hack. */
1773 sym->flags = BSF_LOCAL | BSF_DYNAMIC;
1774
1775 /* Add this stub to the linked list. */
1776 item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
1777 item->start = sym;
1778 item->reloc = newsym;
1779 item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
1780
1781 item->next = powerpc_stubs;
1782 powerpc_stubs = item;
1783
1784 ++stubcount;
1785 }
1786
1787 if (stubcount > 0)
1788 {
1789 asymbol **s;
1790 struct powerpc_stub *l;
1791
1792 /* Add the new symbols we just created to the symbol table. */
1793 *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
1794 ((symcount + stubcount)
1795 * sizeof (asymbol)));
1796 *symcount_ptr += stubcount;
1797 s = &(*symbols_ptr)[symcount];
1798 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1799 *s++ = l->reloc;
1800
1801 /* Set the size of the .stubs section and increase the size of
1802 the .got section. */
1803 if (! bfd_set_section_size (inbfd, stub_sec,
1804 stubcount * POWERPC_STUB_SIZE)
1805 || ! bfd_set_section_size (inbfd, got_sec,
1806 (got_base
1807 + (stubcount
1808 * POWERPC_STUB_TOC_ENTRY_SIZE))))
1809 bfd_fatal ("stub section sizes");
1810 }
1811
1812 /* PowerPC NetWare requires a custom header. We create it here.
1813 The first word is the header version number, currently 1. The
1814 second word is the timestamp of the input file. Unfortunately,
1815 they do not conform to the emergent standard for custom headers.
1816 We must fake the version number and timestamp in the offset and
1817 length fields. */
1818 memcpy (nlm_custom_header (outbfd)->stamp, "CuStHeAd", 8);
1819 nlm_custom_header (outbfd)->hdrLength = 0;
1820 /* Put version number in dataOffset field. */
1821 nlm_custom_header (outbfd)->dataOffset = 1;
1822 /* Put timestamp in length field. */
1823 {
1824 struct stat s;
1825
1826 if (stat (bfd_get_filename (inbfd), &s) < 0)
1827 s.st_mtime = 0;
1828 nlm_custom_header (outbfd)->dataLength = s.st_mtime;
1829 }
1830 /* No data stamp. */
1831 memset (nlm_custom_header (outbfd)->dataStamp, 0,
1832 sizeof (nlm_custom_header (outbfd)->dataStamp));
1833 }
1834
1835 /* Resolve all the stubs for PowerPC NetWare. We fill in the contents
1836 of the output section, and create new relocs in the TOC. */
1837
1838 static void
1839 powerpc_resolve_stubs (inbfd, outbfd)
1840 bfd *inbfd;
1841 bfd *outbfd;
1842 {
1843 bfd_byte buf[POWERPC_STUB_SIZE];
1844 unsigned int i;
1845 unsigned int stubcount;
1846 arelent **relocs;
1847 asection *got_sec;
1848 arelent **r;
1849 struct powerpc_stub *l;
1850
1851 if (powerpc_stubs == (struct powerpc_stub *) NULL)
1852 return;
1853
1854 for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
1855 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
1856
1857 got_sec = bfd_get_section_by_name (inbfd, ".got");
1858 assert (got_sec != (asection *) NULL);
1859 assert (got_sec->output_section->orelocation == (arelent **) NULL);
1860
1861 stubcount = 0;
1862 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1863 ++stubcount;
1864 relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
1865
1866 r = relocs;
1867 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1868 {
1869 arelent *reloc;
1870
1871 /* Adjust the first instruction to use the right TOC index. */
1872 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
1873
1874 /* Write this stub out. */
1875 if (! bfd_set_section_contents (outbfd,
1876 bfd_get_section (l->start),
1877 buf,
1878 l->start->value,
1879 POWERPC_STUB_SIZE))
1880 bfd_fatal ("writing stub");
1881
1882 /* Create a new reloc for the TOC entry. */
1883 reloc = (arelent *) xmalloc (sizeof (arelent));
1884 reloc->sym_ptr_ptr = &l->reloc;
1885 reloc->address = l->toc_index + got_sec->output_offset;
1886 reloc->addend = 0;
1887 reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
1888
1889 *r++ = reloc;
1890 }
1891
1892 bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
1893 }
1894
1895 /* Adjust relocation entries for PowerPC NetWare. We do not output
1896 TOC relocations. The object code already contains the offset from
1897 the TOC pointer. When the function is called, the TOC register,
1898 r2, will be set to the correct TOC value, so there is no need for
1899 any further reloc. */
1900
1901 /*ARGSUSED*/
1902 static void
1903 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1904 contents_size)
1905 bfd *outbfd;
1906 asection *insec;
1907 register arelent ***relocs_ptr;
1908 long *reloc_count_ptr;
1909 char *contents;
1910 bfd_size_type contents_size;
1911 {
1912 const reloc_howto_type *toc_howto;
1913 long reloc_count;
1914 register arelent **relocs;
1915 register long i;
1916
1917 toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
1918 if (toc_howto == (reloc_howto_type *) NULL)
1919 abort ();
1920
1921 /* If this is the .got section, clear out all the contents beyond
1922 the initial size. We must do this here because copy_sections is
1923 going to write out whatever we return in the contents field. */
1924 if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
1925 memset (contents + powerpc_initial_got_size, 0,
1926 (bfd_get_section_size_after_reloc (insec)
1927 - powerpc_initial_got_size));
1928
1929 reloc_count = *reloc_count_ptr;
1930 relocs = *relocs_ptr;
1931 for (i = 0; i < reloc_count; i++)
1932 {
1933 arelent *rel;
1934 asymbol *sym;
1935 bfd_vma symvalue;
1936
1937 rel = *relocs++;
1938 sym = *rel->sym_ptr_ptr;
1939
1940 /* We must be able to resolve all PC relative relocs at this
1941 point. If we get a branch to an undefined symbol we build a
1942 stub, since NetWare will resolve undefined symbols into a
1943 pointer to a function descriptor. */
1944 if (rel->howto->pc_relative)
1945 {
1946 /* This check for whether a symbol is in the same section as
1947 the reloc will be wrong if there is a PC relative reloc
1948 between two sections both of which were placed in the
1949 same output section. This should not happen. */
1950 if (bfd_get_section (sym) != insec->output_section)
1951 fprintf (stderr, "%s: unresolved PC relative reloc against %s\n",
1952 program_name, bfd_asymbol_name (sym));
1953 else
1954 {
1955 bfd_vma val;
1956
1957 assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
1958 val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
1959 val = ((val &~ rel->howto->dst_mask)
1960 | (((val & rel->howto->src_mask)
1961 + (sym->value - rel->address)
1962 + rel->addend)
1963 & rel->howto->dst_mask));
1964 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
1965
1966 /* If this reloc is against an stubbed symbol and the
1967 next instruction is
1968 cror 31,31,31
1969 then we replace the next instruction with
1970 lwz r2,20(r1)
1971 This reloads the TOC pointer after a stub call. */
1972 if (bfd_asymbol_name (sym)[0] == '.'
1973 && (sym->flags & BSF_DYNAMIC) != 0
1974 && (bfd_get_32 (outbfd,
1975 (bfd_byte *) contents + rel->address + 4)
1976 == 0x4ffffb82)) /* cror 31,31,31 */
1977 bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
1978 (bfd_byte *) contents + rel->address + 4);
1979
1980 --*reloc_count_ptr;
1981 --relocs;
1982 memmove (relocs, relocs + 1,
1983 (size_t) ((reloc_count - 1) * sizeof (arelent *)));
1984 continue;
1985 }
1986 }
1987
1988 /* When considering a TOC reloc, we do not want to include the
1989 symbol value. The symbol will be start of the TOC section
1990 (which is named .got). We do want to include the addend. */
1991 if (rel->howto == toc_howto)
1992 symvalue = 0;
1993 else
1994 symvalue = sym->value;
1995
1996 /* If this is a relocation against a symbol with a value, or
1997 there is a reloc addend, we need to update the addend in the
1998 object file. */
1999 if (symvalue + rel->addend != 0)
2000 {
2001 bfd_vma val;
2002
2003 switch (rel->howto->size)
2004 {
2005 case 1:
2006 val = bfd_get_16 (outbfd,
2007 (bfd_byte *) contents + rel->address);
2008 val = ((val &~ rel->howto->dst_mask)
2009 | (((val & rel->howto->src_mask)
2010 + symvalue
2011 + rel->addend)
2012 & rel->howto->dst_mask));
2013 if ((bfd_signed_vma) val < - 0x8000
2014 || (bfd_signed_vma) val >= 0x8000)
2015 fprintf (stderr,
2016 "%s: overflow when adjusting relocation against %s\n",
2017 program_name, bfd_asymbol_name (sym));
2018 bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
2019 break;
2020
2021 case 2:
2022 val = bfd_get_32 (outbfd,
2023 (bfd_byte *) contents + rel->address);
2024 val = ((val &~ rel->howto->dst_mask)
2025 | (((val & rel->howto->src_mask)
2026 + symvalue
2027 + rel->addend)
2028 & rel->howto->dst_mask));
2029 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
2030 break;
2031
2032 default:
2033 abort ();
2034 }
2035
2036 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
2037 rel->addend = 0;
2038 }
2039
2040 /* Now that we have incorporated the addend, remove any TOC
2041 relocs. */
2042 if (rel->howto == toc_howto)
2043 {
2044 --*reloc_count_ptr;
2045 --relocs;
2046 memmove (relocs, relocs + 1,
2047 (size_t) ((reloc_count - i) * sizeof (arelent *)));
2048 continue;
2049 }
2050
2051 rel->address += insec->output_offset;
2052 }
2053 }
2054 \f
2055 /* Name of linker. */
2056 #ifndef LD_NAME
2057 #define LD_NAME "ld"
2058 #endif
2059
2060 /* Temporary file name base. */
2061 static char *temp_filename;
2062
2063 /* The user has specified several input files. Invoke the linker to
2064 link them all together, and convert and delete the resulting output
2065 file. */
2066
2067 static char *
2068 link_inputs (inputs, ld)
2069 struct string_list *inputs;
2070 char *ld;
2071 {
2072 size_t c;
2073 struct string_list *q;
2074 char **argv;
2075 size_t i;
2076 int pid;
2077 int status;
2078
2079 c = 0;
2080 for (q = inputs; q != NULL; q = q->next)
2081 ++c;
2082
2083 argv = (char **) alloca (c + 5);
2084
2085 #ifndef __MSDOS__
2086 if (ld == NULL)
2087 {
2088 char *p;
2089
2090 /* Find the linker to invoke based on how nlmconv was run. */
2091 p = program_name + strlen (program_name);
2092 while (p != program_name)
2093 {
2094 if (p[-1] == '/')
2095 {
2096 ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
2097 memcpy (ld, program_name, p - program_name);
2098 strcpy (ld + (p - program_name), LD_NAME);
2099 break;
2100 }
2101 --p;
2102 }
2103 }
2104 #endif
2105
2106 if (ld == NULL)
2107 ld = (char *) LD_NAME;
2108
2109 choose_temp_base ();
2110
2111 unlink_on_exit = xmalloc (strlen (temp_filename) + 3);
2112 sprintf (unlink_on_exit, "%s.O", temp_filename);
2113
2114 argv[0] = ld;
2115 argv[1] = (char *) "-r";
2116 argv[2] = (char *) "-o";
2117 argv[3] = unlink_on_exit;
2118 i = 4;
2119 for (q = inputs; q != NULL; q = q->next, i++)
2120 argv[i] = q->string;
2121 argv[i] = NULL;
2122
2123 if (debug)
2124 {
2125 for (i = 0; argv[i] != NULL; i++)
2126 fprintf (stderr, " %s", argv[i]);
2127 fprintf (stderr, "\n");
2128 }
2129
2130 pid = pexecute (ld, argv);
2131
2132 if (waitpid (pid, &status, 0) < 0)
2133 {
2134 perror ("waitpid");
2135 unlink (unlink_on_exit);
2136 exit (1);
2137 }
2138
2139 if (status != 0)
2140 {
2141 fprintf (stderr, "%s: Execution of %s failed\n", program_name, ld);
2142 unlink (unlink_on_exit);
2143 exit (1);
2144 }
2145
2146 return unlink_on_exit;
2147 }
2148
2149 /* Choose a temporary file name. Stolen from gcc.c. */
2150
2151 static const char *
2152 choose_temp_base_try (try, base)
2153 const char *try;
2154 const char *base;
2155 {
2156 const char *rv;
2157
2158 if (base)
2159 rv = base;
2160 else if (try == NULL)
2161 rv = NULL;
2162 else if (access (try, R_OK | W_OK) != 0)
2163 rv = NULL;
2164 else
2165 rv = try;
2166 return rv;
2167 }
2168
2169 static void
2170 choose_temp_base ()
2171 {
2172 const char *base = NULL;
2173 int len;
2174
2175 base = choose_temp_base_try (getenv ("TMPDIR"), base);
2176 base = choose_temp_base_try (getenv ("TMP"), base);
2177 base = choose_temp_base_try (getenv ("TEMP"), base);
2178
2179 #ifdef P_tmpdir
2180 base = choose_temp_base_try (P_tmpdir, base);
2181 #endif
2182
2183 base = choose_temp_base_try ("/usr/tmp", base);
2184 base = choose_temp_base_try ("/tmp", base);
2185
2186 /* If all else fails, use the current directory! */
2187 if (base == NULL)
2188 base = "./";
2189
2190 len = strlen (base);
2191 temp_filename = xmalloc (len + sizeof("/ccXXXXXX") + 1);
2192 strcpy (temp_filename, base);
2193 if (len > 0 && temp_filename[len-1] != '/')
2194 temp_filename[len++] = '/';
2195 strcpy (temp_filename + len, "ccXXXXXX");
2196
2197 mktemp (temp_filename);
2198 if (*temp_filename == '\0')
2199 abort ();
2200 }
2201
2202 /* Execute a job. Stolen from gcc.c. */
2203
2204 #ifndef OS2
2205 #ifdef __MSDOS__
2206
2207 static int
2208 pexecute (program, argv)
2209 char *program;
2210 char *argv[];
2211 {
2212 char *scmd, *rf;
2213 FILE *argfile;
2214 int i;
2215
2216 scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 10);
2217 rf = scmd + strlen(program) + 2 + el;
2218 sprintf (scmd, "%s.exe @%s.gp", program, temp_filename);
2219 argfile = fopen (rf, "w");
2220 if (argfile == 0)
2221 pfatal_with_name (rf);
2222
2223 for (i=1; argv[i]; i++)
2224 {
2225 char *cp;
2226 for (cp = argv[i]; *cp; cp++)
2227 {
2228 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
2229 fputc ('\\', argfile);
2230 fputc (*cp, argfile);
2231 }
2232 fputc ('\n', argfile);
2233 }
2234 fclose (argfile);
2235
2236 i = system (scmd);
2237
2238 remove (rf);
2239
2240 if (i == -1)
2241 {
2242 perror (program);
2243 return MIN_FATAL_STATUS << 8;
2244 }
2245
2246 return i << 8;
2247 }
2248
2249 #else /* not __MSDOS__ */
2250
2251 static int
2252 pexecute (program, argv)
2253 char *program;
2254 char *argv[];
2255 {
2256 int pid;
2257 int retries, sleep_interval;
2258
2259 /* Fork a subprocess; wait and retry if it fails. */
2260 sleep_interval = 1;
2261 for (retries = 0; retries < 4; retries++)
2262 {
2263 pid = vfork ();
2264 if (pid >= 0)
2265 break;
2266 sleep (sleep_interval);
2267 sleep_interval *= 2;
2268 }
2269
2270 switch (pid)
2271 {
2272 case -1:
2273 #ifdef vfork
2274 perror ("fork");
2275 #else
2276 perror ("vfork");
2277 #endif
2278 exit (1);
2279 /* NOTREACHED */
2280 return 0;
2281
2282 case 0: /* child */
2283 /* Exec the program. */
2284 execvp (program, argv);
2285 perror (program);
2286 exit (1);
2287 /* NOTREACHED */
2288 return 0;
2289
2290 default:
2291 /* Return child's process number. */
2292 return pid;
2293 }
2294 }
2295
2296 #endif /* not __MSDOS__ */
2297 #else /* not OS2 */
2298
2299 static int
2300 pexecute (program, argv)
2301 char *program;
2302 char *argv[];
2303 {
2304 return spawnvp (1, program, argv);
2305 }
2306 #endif /* not OS2 */
This page took 0.108703 seconds and 5 git commands to generate.