1 /* nlmconv.c -- NLM conversion program
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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.
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.
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. */
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. */
33 #include <sys/types.h>
39 #include "libiberty.h"
42 /* Internal BFD NLM header. */
46 /* Needed for Alpha support. */
48 #include "coff/ecoff.h"
50 /* If strerror is just a macro, we want to use the one from libiberty
51 since it will handle undefined values. */
53 extern char *strerror ();
56 extern struct tm *localtime ();
60 extern char *getenv ();
73 /* Global variables. */
75 /* The name used to invoke the program. */
78 /* The version number. */
79 extern char *program_version;
81 /* Local variables. */
83 /* Whether to print out debugging information (currently just controls
84 whether it prints the linker command if there is one). */
87 /* The symbol table. */
88 static asymbol **symbols;
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
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
102 static asection *secsec;
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
107 static char *unlink_on_exit;
109 /* The list of long options. */
110 static struct option long_options[] =
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 }
124 /* Local routines. */
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 ***,
135 static void i386_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
138 static void alpha_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
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 ***,
146 static void default_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
149 static char *link_inputs PARAMS ((struct string_list *, char *));
150 static const char *choose_temp_base_try PARAMS ((const char *,
152 static void choose_temp_base PARAMS ((void));
153 static int pexecute PARAMS ((char *, char *[]));
155 /* The main routine. */
163 char *input_file = NULL;
164 const char *input_format = NULL;
165 const char *output_format = NULL;
166 const char *header_file = 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;
175 asymbol **newsyms, **outsyms;
176 long symcount, newsymalloc, newsymcount;
178 asection *text_sec, *bss_sec, *data_sec;
183 char inlead, outlead;
184 boolean gotstart, gotexit, gotcheck;
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;
191 size_t shared_offset, shared_size;
192 Nlm_Internal_Fixed_Header sharedhdr;
197 program_name = argv[0];
198 xmalloc_set_program_name (program_name);
202 while ((opt = getopt_long (argc, argv, "dhI:l:O:T:V", long_options,
215 input_format = optarg;
221 output_format = optarg;
224 header_file = optarg;
227 printf ("GNU %s version %s\n", program_name, program_version);
233 show_usage (stderr, 1);
238 /* The input and output files may be named on the command line. */
242 input_file = argv[optind];
246 output_file = argv[optind];
249 show_usage (stderr, 1);
250 if (strcmp (input_file, output_file) == 0)
253 "%s: input and output files must be different\n",
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 = ©right_hdr_struct;
268 memset ((PTR) ©right_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;
274 exit_procedure = "_Stop";
275 export_symbols = NULL;
279 import_symbols = NULL;
282 sharelib_file = NULL;
283 start_procedure = "_Prelude";
289 /* Parse the header file (if there is one). */
290 if (header_file != NULL)
292 if (! nlmlex_file (header_file)
294 || parse_errors != 0)
298 if (input_files != NULL)
300 if (input_file != NULL)
303 "%s: input file named both on command line and with INPUT\n",
307 if (input_files->next == NULL)
308 input_file = input_files->string;
310 input_file = link_inputs (input_files, ld_arg);
312 else if (input_file == NULL)
314 fprintf (stderr, "%s: no input file\n", program_name);
315 show_usage (stderr, 1);
318 inbfd = bfd_openr (input_file, input_format);
320 bfd_fatal (input_file);
322 if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
324 bfd_nonfatal (input_file);
325 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
327 list_matching_formats (matching);
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);
338 assert (output_format != NULL);
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)
344 fprintf (stderr, "%s: no name for output file\n",
346 show_usage (stderr, 1);
349 outbfd = bfd_openw (output_file, output_format);
351 bfd_fatal (output_file);
352 if (! bfd_set_format (outbfd, bfd_object))
353 bfd_fatal (output_file);
355 assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
357 if (bfd_arch_get_compatible (inbfd, outbfd) == NULL)
359 "%s: warning:input and output formats are not compatible\n",
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;
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));
373 symsize = bfd_get_symtab_upper_bound (inbfd);
375 bfd_fatal (input_file);
376 symbols = (asymbol **) xmalloc (symsize);
377 symcount = bfd_canonicalize_symtab (inbfd, symbols);
379 bfd_fatal (input_file);
381 /* Make sure we have a .bss section. */
382 bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
385 bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
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");
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");
398 bfd_fatal ("make .nlmsections section");
399 if (! bfd_set_section_flags (outbfd, secsec, SEC_HAS_CONTENTS))
400 bfd_fatal ("set .nlmsections flags");
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);
410 /* Set up the sections. */
411 bfd_map_over_sections (inbfd, setup_sections, (PTR) outbfd);
413 text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
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)
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;
425 if (! bfd_set_section_vma (outbfd, bss_sec, vma))
426 bfd_fatal ("set .bss vma");
429 bfd_size_type data_size;
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");
437 /* Adjust symbol information. */
438 inlead = bfd_get_symbol_leading_char (inbfd);
439 outlead = bfd_get_symbol_leading_char (outbfd);
444 newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
447 for (i = 0; i < symcount; i++)
449 register asymbol *sym;
453 /* Add or remove a leading underscore. */
454 if (inlead != outlead)
458 if (bfd_asymbol_name (sym)[0] == inlead)
466 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
468 strcpy (new + 1, bfd_asymbol_name (sym) + 1);
477 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
479 strcpy (new + 1, bfd_asymbol_name (sym));
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)))
491 sym->section = bss_sec;
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;
499 else if (bfd_get_section (sym)->output_section != NULL)
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;
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)
515 if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
517 sym->section = bss_sec;
520 if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
522 sym->section = bss_sec;
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)
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;
540 /* If this is a global symbol, check the export list. */
541 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
543 register struct string_list *l;
546 /* Unfortunately, a symbol can appear multiple times on the
547 export list, with and without prefixes. */
549 for (l = export_symbols; l != NULL; l = l->next)
551 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
557 zbase = strchr (l->string, '@');
559 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
561 /* We must add a symbol with this prefix. */
562 if (newsymcount >= newsymalloc)
565 newsyms = ((asymbol **)
566 xrealloc ((PTR) newsyms,
568 * sizeof (asymbol *))));
570 newsyms[newsymcount] =
571 (asymbol *) xmalloc (sizeof (asymbol));
572 *newsyms[newsymcount] = *sym;
573 newsyms[newsymcount]->name = l->string;
580 /* The unmodified symbol is actually not exported at
582 sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
583 sym->flags |= BSF_LOCAL;
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)
591 register struct string_list *l;
593 for (l = import_symbols; l != NULL; l = l->next)
595 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
601 zbase = strchr (l->string, '@');
603 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
605 sym->name = l->string;
612 "%s: warning: symbol %s imported but not in import list\n",
613 program_name, bfd_asymbol_name (sym));
616 /* See if it's one of the special named symbols. */
617 if ((sym->flags & BSF_DEBUGGING) == 0)
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)
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");
635 if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
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;
644 if (check_procedure != NULL
645 && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
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;
659 endsym->value = bfd_get_section_size_before_reloc (bss_sec);
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
667 if (newsymcount == 0)
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;
678 bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
681 fprintf (stderr, "%s: warning: START procedure %s not defined\n",
682 program_name, start_procedure);
684 fprintf (stderr, "%s: warning: EXIT procedure %s not defined\n",
685 program_name, exit_procedure);
686 if (check_procedure != NULL
688 fprintf (stderr, "%s: warning: CHECK procedure %s not defined\n",
689 program_name, check_procedure);
691 /* Add additional sections required for the header information. */
692 if (custom_file != NULL)
694 custom_data = fopen (custom_file, "r");
695 if (custom_data == NULL
696 || fstat (fileno (custom_data), &st) < 0)
698 fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
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,
710 bfd_fatal ("custom section");
713 if (help_file != NULL)
715 help_data = fopen (help_file, "r");
716 if (help_data == NULL
717 || fstat (fileno (help_data), &st) < 0)
719 fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
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,
731 bfd_fatal ("help section");
732 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
735 if (message_file != NULL)
737 message_data = fopen (message_file, "r");
738 if (message_data == NULL
739 || fstat (fileno (message_data), &st) < 0)
741 fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
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,
753 bfd_fatal ("message section");
754 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
759 struct string_list *l;
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,
769 bfd_fatal ("module section");
771 if (rpc_file != NULL)
773 rpc_data = fopen (rpc_file, "r");
775 || fstat (fileno (rpc_data), &st) < 0)
777 fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
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,
789 bfd_fatal ("rpc section");
790 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
793 if (sharelib_file != NULL)
795 sharedbfd = bfd_openr (sharelib_file, output_format);
796 if (sharedbfd == NULL
797 || ! bfd_check_format (sharedbfd, bfd_object))
799 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
800 bfd_errmsg (bfd_get_error ()));
801 sharelib_file = NULL;
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))
811 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
813 sharelib_file = NULL;
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)
824 /* There is no place to record this information. */
826 "%s:%s: warning: shared libraries can not have uninitialized data\n",
827 program_name, sharelib_file);
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,
845 || ! bfd_set_section_flags (outbfd, shared_section,
847 bfd_fatal ("shared section");
848 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
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",
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);
862 strncpy (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx", 8);
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)
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);
880 /* Resolve the stubs we build for PowerPC NetWare. */
881 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
882 powerpc_resolve_stubs (inbfd, outbfd);
884 /* Copy over the sections. */
885 bfd_map_over_sections (inbfd, copy_sections, (PTR) outbfd);
887 /* Finish up the header information. */
888 if (custom_file != NULL)
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,
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;
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;
916 if (map_file != NULL)
918 "%s: warning: MAP and FULLMAP are not supported; try ld -M\n",
920 if (help_file != NULL)
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,
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;
939 if (message_file != NULL)
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,
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;
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);
969 struct string_list *l;
972 data = xmalloc (module_size);
974 set = (unsigned char *) data;
975 for (l = modules; l != NULL; l = l->next)
977 *set = strlen (l->string);
978 strncpy (set + 1, l->string, *set);
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;
989 if (rpc_file != NULL)
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,
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;
1008 if (sharelib_file != NULL)
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,
1019 if (! bfd_set_section_contents (outbfd, shared_section, data,
1020 (file_ptr) 0, shared_size))
1021 bfd_fatal ("shared section");
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
1034 + shared_section->filepos);
1035 nlm_extended_header (outbfd)->sharedRelocationFixupCount =
1036 sharedhdr.numberOfRelocationFixups;
1037 nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
1038 (sharedhdr.externalReferencesOffset
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;
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;
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;
1068 if (islower (*modname))
1069 *modname = toupper (*modname);
1071 strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
1072 NLM_OLD_THREAD_NAME_LENGTH);
1074 nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
1075 nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
1077 if (! bfd_close (outbfd))
1078 bfd_fatal (output_file);
1079 if (! bfd_close (inbfd))
1080 bfd_fatal (input_file);
1082 if (unlink_on_exit != NULL)
1083 unlink (unlink_on_exit);
1088 /* Display a help message and exit. */
1093 printf ("%s: Convert an object file into a NetWare Loadable Module\n",
1095 show_usage (stdout, 0);
1098 /* Show a usage message and exit. */
1101 show_usage (file, status)
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",
1115 /* Select the output format based on the input architecture, machine,
1116 and endianness. This chooses the appropriate NLM target. */
1119 select_output_format (arch, mach, bigendian)
1120 enum bfd_architecture arch;
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";
1135 fprintf (stderr, "%s: no default NLM format for %s\n",
1136 program_name, bfd_printable_arch_mach (arch, mach));
1138 /* Avoid warning. */
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
1149 setup_sections (inbfd, insec, data_ptr)
1154 bfd *outbfd = (bfd *) data_ptr;
1156 const char *outname;
1159 bfd_size_type align;
1161 bfd_size_type secsecsize;
1163 f = bfd_get_section_flags (inbfd, insec);
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;
1171 outname = bfd_section_name (inbfd, insec);
1173 outsec = bfd_get_section_by_name (outbfd, outname);
1176 outsec = bfd_make_section (outbfd, outname);
1178 bfd_fatal ("make section");
1181 insec->output_section = outsec;
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;
1188 if (! bfd_set_section_size (outbfd, outsec,
1189 (bfd_section_size (outbfd, outsec)
1190 + bfd_section_size (inbfd, insec)
1192 bfd_fatal ("set section size");
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");
1200 if (! bfd_set_section_flags (outbfd, outsec, f))
1201 bfd_fatal ("set section flags");
1203 bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
1205 /* For each input section we allocate space for an entry in
1207 secsecsize = bfd_section_size (outbfd, secsec);
1208 secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
1209 secsecsize = (secsecsize + 3) &~ 3;
1211 if (! bfd_set_section_size (outbfd, secsec, secsecsize))
1212 bfd_fatal ("set .nlmsections size");
1215 /* Copy the section contents. */
1218 copy_sections (inbfd, insec, data_ptr)
1223 static bfd_size_type secsecoff = 0;
1224 bfd *outbfd = (bfd *) data_ptr;
1233 inname = bfd_section_name (inbfd, insec);
1235 outsec = insec->output_section;
1236 assert (outsec != NULL);
1238 size = bfd_get_section_size_before_reloc (insec);
1240 /* FIXME: Why are these necessary? */
1241 insec->_cooked_size = insec->_raw_size;
1242 insec->reloc_done = true;
1244 if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
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));
1254 reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
1256 bfd_fatal (bfd_get_filename (inbfd));
1257 if (reloc_size != 0)
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,
1269 /* FIXME: refers to internal BFD fields. */
1270 if (outsec->orelocation != (arelent **) NULL)
1272 bfd_size_type total_count;
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;
1286 bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
1289 if (contents != NULL)
1291 if (! bfd_set_section_contents (outbfd, outsec, contents,
1292 insec->output_offset, size))
1293 bfd_fatal (bfd_get_filename (outbfd));
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;
1303 add = ((secsecoff + 3) &~ 3) - secsecoff;
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");
1312 if (contents != NULL)
1313 bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
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");
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");
1326 /* Some, perhaps all, NetWare targets require changing the relocs used
1327 by the input formats. */
1330 mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1334 arelent ***relocs_ptr;
1335 long *reloc_count_ptr;
1337 bfd_size_type contents_size;
1339 switch (bfd_get_arch (outbfd))
1342 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1343 contents, contents_size);
1345 case bfd_arch_alpha:
1346 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1347 contents, contents_size);
1349 case bfd_arch_powerpc:
1350 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1351 contents, contents_size);
1354 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1355 contents, contents_size);
1360 /* By default all we need to do for relocs is change the address by
1361 the output_offset. */
1365 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1369 arelent ***relocs_ptr;
1370 long *reloc_count_ptr;
1372 bfd_size_type contents_size;
1374 if (insec->output_offset != 0)
1377 register arelent **relocs;
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;
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. */
1393 static reloc_howto_type nlm_i386_pcrel_howto =
1394 HOWTO (1, /* type */
1396 2, /* size (0 = byte, 1 = short, 2 = long) */
1398 true, /* pc_relative */
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 */
1409 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1413 arelent ***relocs_ptr;
1414 long *reloc_count_ptr;
1416 bfd_size_type contents_size;
1418 long reloc_count, i;
1421 reloc_count = *reloc_count_ptr;
1422 relocs = *relocs_ptr;
1423 for (i = 0; i < reloc_count; i++)
1427 bfd_size_type address;
1431 sym = *rel->sym_ptr_ptr;
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;
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. */
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)
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)
1455 memmove (relocs, relocs + 1,
1456 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1460 /* Get the amount the relocation will add in. */
1461 addend = rel->addend + sym->value;
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
1467 if (rel->howto != NULL
1468 && rel->howto->pc_relative
1469 && bfd_get_section (sym) == insec->output_section)
1473 if (rel->howto->pcrel_offset)
1476 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1478 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1482 memmove (relocs, relocs + 1,
1483 (size_t) ((reloc_count - i) * sizeof (arelent *)));
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. */
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)
1501 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1503 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1505 /* Adjust the reloc for the changes we just made. */
1507 if (bfd_get_section (sym) != &bfd_und_section)
1508 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
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)
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);
1534 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1536 /* We must change to a new howto. */
1537 rel->howto = &nlm_i386_pcrel_howto;
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
1547 static reloc_howto_type nlm32_alpha_nw_howto =
1548 HOWTO (ALPHA_R_NW_RELOC, /* type */
1550 0, /* size (0 = byte, 1 = short, 2 = long) */
1552 false, /* pc_relative */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 0, /* special_function */
1556 "NW_RELOC", /* name */
1557 false, /* partial_inplace */
1560 false); /* pcrel_offset */
1564 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1568 register arelent ***relocs_ptr;
1569 long *reloc_count_ptr;
1571 bfd_size_type contents_size;
1573 long old_reloc_count;
1574 arelent **old_relocs;
1575 register arelent **relocs;
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;
1582 if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
1585 asection *lita_section;
1587 inbfd = insec->owner;
1588 lita_section = bfd_get_section_by_name (inbfd, _LITA);
1589 if (lita_section != (asection *) NULL)
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);
1598 /* Avoid outputting this reloc again. */
1599 nlm_alpha_backend_data (outbfd)->lita_address = 4;
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;
1608 ++(*reloc_count_ptr);
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);
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;
1622 ++(*reloc_count_ptr);
1624 memcpy ((PTR) relocs, (PTR) old_relocs,
1625 (size_t) old_reloc_count * sizeof (arelent *));
1626 relocs[old_reloc_count] = (arelent *) NULL;
1630 if (insec->output_offset != 0)
1632 register bfd_size_type i;
1634 for (i = 0; i < old_reloc_count; i++, relocs++)
1635 (*relocs)->address += insec->output_offset;
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. */
1646 /* Next stub in linked list. */
1647 struct powerpc_stub *next;
1649 /* Symbol whose value is the start of the stub. This is a symbol
1650 whose name begins with `.'. */
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 `.'. */
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;
1662 /* The linked list of stubs. */
1664 static struct powerpc_stub *powerpc_stubs;
1666 /* This is what a stub looks like. The first instruction will get
1667 adjusted with the correct TOC index. */
1669 static unsigned long powerpc_stub_insns[] =
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. */
1682 #define POWERPC_STUB_INSN_COUNT \
1683 (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
1685 #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
1687 /* Each stub uses a four byte TOC entry. */
1688 #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
1690 /* The original size of the .got section. */
1691 static bfd_size_type powerpc_initial_got_size;
1693 /* Look for all undefined symbols beginning with `.', and prepare to
1694 build a stub for each one. */
1697 powerpc_build_stubs (inbfd, outbfd, symbols_ptr, symcount_ptr)
1700 asymbol ***symbols_ptr;
1705 unsigned int got_base;
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,
1719 || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
1720 bfd_fatal (".stubs");
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)
1726 got_sec = bfd_make_section (inbfd, ".got");
1727 if (got_sec == (asection *) NULL
1728 || ! bfd_set_section_flags (inbfd, got_sec,
1733 | SEC_HAS_CONTENTS))
1734 || ! bfd_set_section_alignment (inbfd, got_sec, 2))
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;
1744 symcount = *symcount_ptr;
1745 for (i = 0; i < symcount; i++)
1750 struct powerpc_stub *item;
1752 sym = (*symbols_ptr)[i];
1754 /* We must make a stub for every undefined symbol whose name
1756 if (bfd_asymbol_name (sym)[0] != '.'
1757 || bfd_get_section (sym) != &bfd_und_section)
1760 /* Make a new undefined symbol with the same name but without
1762 newsym = (asymbol *) xmalloc (sizeof (asymbol));
1764 newname = (char *) xmalloc (strlen (bfd_asymbol_name (sym)));
1765 strcpy (newname, bfd_asymbol_name (sym) + 1);
1766 newsym->name = newname;
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;
1775 /* Add this stub to the linked list. */
1776 item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
1778 item->reloc = newsym;
1779 item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
1781 item->next = powerpc_stubs;
1782 powerpc_stubs = item;
1790 struct powerpc_stub *l;
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)
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,
1808 * POWERPC_STUB_TOC_ENTRY_SIZE))))
1809 bfd_fatal ("stub section sizes");
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
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. */
1826 if (stat (bfd_get_filename (inbfd), &s) < 0)
1828 nlm_custom_header (outbfd)->dataLength = s.st_mtime;
1830 /* No data stamp. */
1831 memset (nlm_custom_header (outbfd)->dataStamp, 0,
1832 sizeof (nlm_custom_header (outbfd)->dataStamp));
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. */
1839 powerpc_resolve_stubs (inbfd, outbfd)
1843 bfd_byte buf[POWERPC_STUB_SIZE];
1845 unsigned int stubcount;
1849 struct powerpc_stub *l;
1851 if (powerpc_stubs == (struct powerpc_stub *) NULL)
1854 for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
1855 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
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);
1862 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1864 relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
1867 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
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);
1874 /* Write this stub out. */
1875 if (! bfd_set_section_contents (outbfd,
1876 bfd_get_section (l->start),
1880 bfd_fatal ("writing stub");
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;
1887 reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
1892 bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
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. */
1903 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1907 register arelent ***relocs_ptr;
1908 long *reloc_count_ptr;
1910 bfd_size_type contents_size;
1912 const reloc_howto_type *toc_howto;
1914 register arelent **relocs;
1917 toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
1918 if (toc_howto == (reloc_howto_type *) NULL)
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));
1929 reloc_count = *reloc_count_ptr;
1930 relocs = *relocs_ptr;
1931 for (i = 0; i < reloc_count; i++)
1938 sym = *rel->sym_ptr_ptr;
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)
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));
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)
1963 & rel->howto->dst_mask));
1964 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
1966 /* If this reloc is against an stubbed symbol and the
1969 then we replace the next instruction with
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);
1982 memmove (relocs, relocs + 1,
1983 (size_t) ((reloc_count - 1) * sizeof (arelent *)));
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)
1994 symvalue = sym->value;
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
1999 if (symvalue + rel->addend != 0)
2003 switch (rel->howto->size)
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)
2012 & rel->howto->dst_mask));
2013 if ((bfd_signed_vma) val < - 0x8000
2014 || (bfd_signed_vma) val >= 0x8000)
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);
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)
2028 & rel->howto->dst_mask));
2029 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
2036 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
2040 /* Now that we have incorporated the addend, remove any TOC
2042 if (rel->howto == toc_howto)
2046 memmove (relocs, relocs + 1,
2047 (size_t) ((reloc_count - i) * sizeof (arelent *)));
2051 rel->address += insec->output_offset;
2055 /* Name of linker. */
2057 #define LD_NAME "ld"
2060 /* Temporary file name base. */
2061 static char *temp_filename;
2063 /* The user has specified several input files. Invoke the linker to
2064 link them all together, and convert and delete the resulting output
2068 link_inputs (inputs, ld)
2069 struct string_list *inputs;
2073 struct string_list *q;
2080 for (q = inputs; q != NULL; q = q->next)
2083 argv = (char **) alloca (c + 5);
2090 /* Find the linker to invoke based on how nlmconv was run. */
2091 p = program_name + strlen (program_name);
2092 while (p != program_name)
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);
2107 ld = (char *) LD_NAME;
2109 choose_temp_base ();
2111 unlink_on_exit = xmalloc (strlen (temp_filename) + 3);
2112 sprintf (unlink_on_exit, "%s.O", temp_filename);
2115 argv[1] = (char *) "-r";
2116 argv[2] = (char *) "-o";
2117 argv[3] = unlink_on_exit;
2119 for (q = inputs; q != NULL; q = q->next, i++)
2120 argv[i] = q->string;
2125 for (i = 0; argv[i] != NULL; i++)
2126 fprintf (stderr, " %s", argv[i]);
2127 fprintf (stderr, "\n");
2130 pid = pexecute (ld, argv);
2132 if (waitpid (pid, &status, 0) < 0)
2135 unlink (unlink_on_exit);
2141 fprintf (stderr, "%s: Execution of %s failed\n", program_name, ld);
2142 unlink (unlink_on_exit);
2146 return unlink_on_exit;
2149 /* Choose a temporary file name. Stolen from gcc.c. */
2152 choose_temp_base_try (try, base)
2160 else if (try == NULL)
2162 else if (access (try, R_OK | W_OK) != 0)
2172 const char *base = NULL;
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);
2180 base = choose_temp_base_try (P_tmpdir, base);
2183 base = choose_temp_base_try ("/usr/tmp", base);
2184 base = choose_temp_base_try ("/tmp", base);
2186 /* If all else fails, use the current directory! */
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");
2197 mktemp (temp_filename);
2198 if (*temp_filename == '\0')
2202 /* Execute a job. Stolen from gcc.c. */
2208 pexecute (program, argv)
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");
2221 pfatal_with_name (rf);
2223 for (i=1; argv[i]; i++)
2226 for (cp = argv[i]; *cp; cp++)
2228 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
2229 fputc ('\\', argfile);
2230 fputc (*cp, argfile);
2232 fputc ('\n', argfile);
2243 return MIN_FATAL_STATUS << 8;
2249 #else /* not __MSDOS__ */
2252 pexecute (program, argv)
2257 int retries, sleep_interval;
2259 /* Fork a subprocess; wait and retry if it fails. */
2261 for (retries = 0; retries < 4; retries++)
2266 sleep (sleep_interval);
2267 sleep_interval *= 2;
2283 /* Exec the program. */
2284 execvp (program, argv);
2291 /* Return child's process number. */
2296 #endif /* not __MSDOS__ */
2300 pexecute (program, argv)
2304 return spawnvp (1, program, argv);
2306 #endif /* not OS2 */