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. */
48 #include "coff/ecoff.h"
51 /* If strerror is just a macro, we want to use the one from libiberty
52 since it will handle undefined values. */
54 extern char *strerror ();
57 extern struct tm *localtime ();
61 extern char *getenv ();
74 /* Global variables. */
76 /* The name used to invoke the program. */
79 /* The version number. */
80 extern char *program_version;
82 /* Local variables. */
84 /* Whether to print out debugging information (currently just controls
85 whether it prints the linker command if there is one). */
88 /* The symbol table. */
89 static asymbol **symbols;
91 /* A section we create in the output file to hold pointers to where
92 the sections of the input file end up. We will put a pointer to
93 this section in the NLM header. These is an entry for each input
94 section. The format is
95 null terminated section name
96 zeroes to adjust to 4 byte boundary
97 4 byte section data file pointer
99 We don't need a version number. The way we find this information
100 is by finding a stamp in the NLM header information. If we need to
101 change the format of this information, we can simply change the
103 static asection *secsec;
105 /* A temporary file name to be unlinked on exit. Actually, for most
106 errors, we leave it around. It's not clear whether that is helpful
108 static char *unlink_on_exit;
110 /* The list of long options. */
111 static struct option long_options[] =
113 { "debug", no_argument, 0, 'd' },
114 { "header-file", required_argument, 0, 'T' },
115 { "help", no_argument, 0, 'h' },
116 { "input-target", required_argument, 0, 'I' },
117 { "input-format", required_argument, 0, 'I' }, /* Obsolete */
118 { "linker", required_argument, 0, 'l' },
119 { "output-target", required_argument, 0, 'O' },
120 { "output-format", required_argument, 0, 'O' }, /* Obsolete */
121 { "version", no_argument, 0, 'V' },
122 { NULL, no_argument, 0, 0 }
125 /* Local routines. */
127 static void show_help PARAMS ((void));
128 static void show_usage PARAMS ((FILE *, int));
129 static const char *select_output_format PARAMS ((enum bfd_architecture,
130 unsigned long, boolean));
131 static void setup_sections PARAMS ((bfd *, asection *, PTR));
132 static void copy_sections PARAMS ((bfd *, asection *, PTR));
133 static void mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
136 static void default_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
139 static char *link_inputs PARAMS ((struct string_list *, char *));
140 static const char *choose_temp_base_try PARAMS ((const char *,
142 static void choose_temp_base PARAMS ((void));
143 static int pexecute PARAMS ((char *, char *[]));
146 static void i386_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
152 static void alpha_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
157 #ifdef NLMCONV_POWERPC
158 static void powerpc_build_stubs PARAMS ((bfd *, bfd *, asymbol ***, long *));
159 static void powerpc_resolve_stubs PARAMS ((bfd *, bfd *));
160 static void powerpc_mangle_relocs PARAMS ((bfd *, asection *, arelent ***,
165 /* The main routine. */
173 char *input_file = NULL;
174 const char *input_format = NULL;
175 const char *output_format = NULL;
176 const char *header_file = NULL;
178 Nlm_Internal_Fixed_Header fixed_hdr_struct;
179 Nlm_Internal_Variable_Header var_hdr_struct;
180 Nlm_Internal_Version_Header version_hdr_struct;
181 Nlm_Internal_Copyright_Header copyright_hdr_struct;
182 Nlm_Internal_Extended_Header extended_hdr_struct;
185 asymbol **newsyms, **outsyms;
186 long symcount, newsymalloc, newsymcount;
188 asection *text_sec, *bss_sec, *data_sec;
193 char inlead, outlead;
194 boolean gotstart, gotexit, gotcheck;
196 FILE *custom_data, *help_data, *message_data, *rpc_data, *shared_data;
197 size_t custom_size, help_size, message_size, module_size, rpc_size;
198 asection *custom_section, *help_section, *message_section, *module_section;
199 asection *rpc_section, *shared_section;
201 size_t shared_offset, shared_size;
202 Nlm_Internal_Fixed_Header sharedhdr;
207 program_name = argv[0];
208 xmalloc_set_program_name (program_name);
212 while ((opt = getopt_long (argc, argv, "dhI:l:O:T:V", long_options,
225 input_format = optarg;
231 output_format = optarg;
234 header_file = optarg;
237 printf ("GNU %s version %s\n", program_name, program_version);
243 show_usage (stderr, 1);
248 /* The input and output files may be named on the command line. */
252 input_file = argv[optind];
256 output_file = argv[optind];
259 show_usage (stderr, 1);
260 if (strcmp (input_file, output_file) == 0)
263 "%s: input and output files must be different\n",
270 /* Initialize the header information to default values. */
271 fixed_hdr = &fixed_hdr_struct;
272 memset ((PTR) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
273 var_hdr = &var_hdr_struct;
274 memset ((PTR) &var_hdr_struct, 0, sizeof var_hdr_struct);
275 version_hdr = &version_hdr_struct;
276 memset ((PTR) &version_hdr_struct, 0, sizeof version_hdr_struct);
277 copyright_hdr = ©right_hdr_struct;
278 memset ((PTR) ©right_hdr_struct, 0, sizeof copyright_hdr_struct);
279 extended_hdr = &extended_hdr_struct;
280 memset ((PTR) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
281 check_procedure = NULL;
284 exit_procedure = "_Stop";
285 export_symbols = NULL;
289 import_symbols = NULL;
292 sharelib_file = NULL;
293 start_procedure = "_Prelude";
299 /* Parse the header file (if there is one). */
300 if (header_file != NULL)
302 if (! nlmlex_file (header_file)
304 || parse_errors != 0)
308 if (input_files != NULL)
310 if (input_file != NULL)
313 "%s: input file named both on command line and with INPUT\n",
317 if (input_files->next == NULL)
318 input_file = input_files->string;
320 input_file = link_inputs (input_files, ld_arg);
322 else if (input_file == NULL)
324 fprintf (stderr, "%s: no input file\n", program_name);
325 show_usage (stderr, 1);
328 inbfd = bfd_openr (input_file, input_format);
330 bfd_fatal (input_file);
332 if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
334 bfd_nonfatal (input_file);
335 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
337 list_matching_formats (matching);
343 if (output_format == NULL)
344 output_format = select_output_format (bfd_get_arch (inbfd),
345 bfd_get_mach (inbfd),
346 inbfd->xvec->byteorder_big_p);
348 assert (output_format != NULL);
350 /* Use the output file named on the command line if it exists.
351 Otherwise use the file named in the OUTPUT statement. */
352 if (output_file == NULL)
354 fprintf (stderr, "%s: no name for output file\n",
356 show_usage (stderr, 1);
359 outbfd = bfd_openw (output_file, output_format);
361 bfd_fatal (output_file);
362 if (! bfd_set_format (outbfd, bfd_object))
363 bfd_fatal (output_file);
365 assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
367 if (bfd_arch_get_compatible (inbfd, outbfd) == NULL)
369 "%s: warning:input and output formats are not compatible\n",
372 /* Move the values read from the command file into outbfd. */
373 *nlm_fixed_header (outbfd) = fixed_hdr_struct;
374 *nlm_variable_header (outbfd) = var_hdr_struct;
375 *nlm_version_header (outbfd) = version_hdr_struct;
376 *nlm_copyright_header (outbfd) = copyright_hdr_struct;
377 *nlm_extended_header (outbfd) = extended_hdr_struct;
379 /* Start copying the input BFD to the output BFD. */
380 if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
381 bfd_fatal (bfd_get_filename (outbfd));
383 symsize = bfd_get_symtab_upper_bound (inbfd);
385 bfd_fatal (input_file);
386 symbols = (asymbol **) xmalloc (symsize);
387 symcount = bfd_canonicalize_symtab (inbfd, symbols);
389 bfd_fatal (input_file);
391 /* Make sure we have a .bss section. */
392 bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
395 bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
397 || ! bfd_set_section_flags (outbfd, bss_sec, SEC_ALLOC)
398 || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
399 bfd_fatal ("make .bss section");
402 /* We store the original section names in the .nlmsections section,
403 so that programs which understand it can resurrect the original
404 sections from the NLM. We will put a pointer to .nlmsections in
405 the NLM header area. */
406 secsec = bfd_make_section (outbfd, ".nlmsections");
408 bfd_fatal ("make .nlmsections section");
409 if (! bfd_set_section_flags (outbfd, secsec, SEC_HAS_CONTENTS))
410 bfd_fatal ("set .nlmsections flags");
412 #ifdef NLMCONV_POWERPC
413 /* For PowerPC NetWare we need to build stubs for calls to undefined
414 symbols. Because each stub requires an entry in the TOC section
415 which must be at the same location as other entries in the TOC
416 section, we must do this before determining where the TOC section
417 goes in setup_sections. */
418 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
419 powerpc_build_stubs (inbfd, outbfd, &symbols, &symcount);
422 /* Set up the sections. */
423 bfd_map_over_sections (inbfd, setup_sections, (PTR) outbfd);
425 text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
427 /* The .bss section immediately follows the .data section. */
428 data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
429 if (data_sec != NULL)
433 vma = bfd_get_section_size_before_reloc (data_sec);
434 align = 1 << bss_sec->alignment_power;
435 add = ((vma + align - 1) &~ (align - 1)) - vma;
437 if (! bfd_set_section_vma (outbfd, bss_sec, vma))
438 bfd_fatal ("set .bss vma");
441 bfd_size_type data_size;
443 data_size = bfd_get_section_size_before_reloc (data_sec);
444 if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
445 bfd_fatal ("set .data size");
449 /* Adjust symbol information. */
450 inlead = bfd_get_symbol_leading_char (inbfd);
451 outlead = bfd_get_symbol_leading_char (outbfd);
456 newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
459 for (i = 0; i < symcount; i++)
461 register asymbol *sym;
465 /* Add or remove a leading underscore. */
466 if (inlead != outlead)
470 if (bfd_asymbol_name (sym)[0] == inlead)
478 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
480 strcpy (new + 1, bfd_asymbol_name (sym) + 1);
489 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
491 strcpy (new + 1, bfd_asymbol_name (sym));
496 /* NLM's have an uninitialized data section, but they do not
497 have a common section in the Unix sense. Move all common
498 symbols into the .bss section, and mark them as exported. */
499 if (bfd_is_com_section (bfd_get_section (sym)))
503 sym->section = bss_sec;
505 sym->value = bss_sec->_raw_size;
506 bss_sec->_raw_size += size;
507 align = 1 << bss_sec->alignment_power;
508 bss_sec->_raw_size = (bss_sec->_raw_size + align - 1) &~ (align - 1);
509 sym->flags |= BSF_EXPORT | BSF_GLOBAL;
511 else if (bfd_get_section (sym)->output_section != NULL)
513 /* Move the symbol into the output section. */
514 sym->value += bfd_get_section (sym)->output_offset;
515 sym->section = bfd_get_section (sym)->output_section;
516 /* This is no longer a section symbol. */
517 sym->flags &=~ BSF_SECTION_SYM;
520 /* Force _edata and _end to be defined. This would normally be
521 done by the linker, but the manipulation of the common
522 symbols will confuse it. */
523 if ((sym->flags & BSF_DEBUGGING) == 0
524 && bfd_asymbol_name (sym)[0] == '_'
525 && bfd_is_und_section (bfd_get_section (sym)))
527 if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
529 sym->section = bss_sec;
532 if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
534 sym->section = bss_sec;
538 #ifdef NLMCONV_POWERPC
539 /* For PowerPC NetWare, we define __GOT0. This is the start
540 of the .got section. */
541 if (bfd_get_arch (inbfd) == bfd_arch_powerpc
542 && strcmp (bfd_asymbol_name (sym), "__GOT0") == 0)
546 got_sec = bfd_get_section_by_name (inbfd, ".got");
547 assert (got_sec != (asection *) NULL);
548 sym->value = got_sec->output_offset;
549 sym->section = got_sec->output_section;
554 /* If this is a global symbol, check the export list. */
555 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
557 register struct string_list *l;
560 /* Unfortunately, a symbol can appear multiple times on the
561 export list, with and without prefixes. */
563 for (l = export_symbols; l != NULL; l = l->next)
565 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
571 zbase = strchr (l->string, '@');
573 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
575 /* We must add a symbol with this prefix. */
576 if (newsymcount >= newsymalloc)
579 newsyms = ((asymbol **)
580 xrealloc ((PTR) newsyms,
582 * sizeof (asymbol *))));
584 newsyms[newsymcount] =
585 (asymbol *) xmalloc (sizeof (asymbol));
586 *newsyms[newsymcount] = *sym;
587 newsyms[newsymcount]->name = l->string;
594 /* The unmodified symbol is actually not exported at
596 sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
597 sym->flags |= BSF_LOCAL;
601 /* If it's an undefined symbol, see if it's on the import list.
602 Change the prefix if necessary. */
603 if (bfd_is_und_section (bfd_get_section (sym)))
605 register struct string_list *l;
607 for (l = import_symbols; l != NULL; l = l->next)
609 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
615 zbase = strchr (l->string, '@');
617 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
619 sym->name = l->string;
626 "%s: warning: symbol %s imported but not in import list\n",
627 program_name, bfd_asymbol_name (sym));
630 /* See if it's one of the special named symbols. */
631 if ((sym->flags & BSF_DEBUGGING) == 0)
635 /* FIXME: If these symbols are not in the .text section, we
636 add the .text section size to the value. This may not be
637 correct for all targets. I'm not sure how this should
638 really be handled. */
639 if (strcmp (bfd_asymbol_name (sym), start_procedure) == 0)
641 val = bfd_asymbol_value (sym);
642 if (bfd_get_section (sym) == data_sec
643 && text_sec != (asection *) NULL)
644 val += bfd_section_size (outbfd, text_sec);
645 if (! bfd_set_start_address (outbfd, val))
646 bfd_fatal ("set start address");
649 if (strcmp (bfd_asymbol_name (sym), exit_procedure) == 0)
651 val = bfd_asymbol_value (sym);
652 if (bfd_get_section (sym) == data_sec
653 && text_sec != (asection *) NULL)
654 val += bfd_section_size (outbfd, text_sec);
655 nlm_fixed_header (outbfd)->exitProcedureOffset = val;
658 if (check_procedure != NULL
659 && strcmp (bfd_asymbol_name (sym), check_procedure) == 0)
661 val = bfd_asymbol_value (sym);
662 if (bfd_get_section (sym) == data_sec
663 && text_sec != (asection *) NULL)
664 val += bfd_section_size (outbfd, text_sec);
665 nlm_fixed_header (outbfd)->checkUnloadProcedureOffset = val;
673 endsym->value = bfd_get_section_size_before_reloc (bss_sec);
675 /* FIXME: If any relocs referring to _end use inplace addends,
676 then I think they need to be updated. This is handled by
677 i386_mangle_relocs. Is it needed for any other object
681 if (newsymcount == 0)
685 outsyms = (asymbol **) xmalloc ((symcount + newsymcount + 1)
686 * sizeof (asymbol *));
687 memcpy (outsyms, symbols, symcount * sizeof (asymbol *));
688 memcpy (outsyms + symcount, newsyms, newsymcount * sizeof (asymbol *));
689 outsyms[symcount + newsymcount] = NULL;
692 bfd_set_symtab (outbfd, outsyms, symcount + newsymcount);
695 fprintf (stderr, "%s: warning: START procedure %s not defined\n",
696 program_name, start_procedure);
698 fprintf (stderr, "%s: warning: EXIT procedure %s not defined\n",
699 program_name, exit_procedure);
700 if (check_procedure != NULL
702 fprintf (stderr, "%s: warning: CHECK procedure %s not defined\n",
703 program_name, check_procedure);
705 /* Add additional sections required for the header information. */
706 if (custom_file != NULL)
708 custom_data = fopen (custom_file, "r");
709 if (custom_data == NULL
710 || fstat (fileno (custom_data), &st) < 0)
712 fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
718 custom_size = st.st_size;
719 custom_section = bfd_make_section (outbfd, ".nlmcustom");
720 if (custom_section == NULL
721 || ! bfd_set_section_size (outbfd, custom_section, custom_size)
722 || ! bfd_set_section_flags (outbfd, custom_section,
724 bfd_fatal ("custom section");
727 if (help_file != NULL)
729 help_data = fopen (help_file, "r");
730 if (help_data == NULL
731 || fstat (fileno (help_data), &st) < 0)
733 fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
739 help_size = st.st_size;
740 help_section = bfd_make_section (outbfd, ".nlmhelp");
741 if (help_section == NULL
742 || ! bfd_set_section_size (outbfd, help_section, help_size)
743 || ! bfd_set_section_flags (outbfd, help_section,
745 bfd_fatal ("help section");
746 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
749 if (message_file != NULL)
751 message_data = fopen (message_file, "r");
752 if (message_data == NULL
753 || fstat (fileno (message_data), &st) < 0)
755 fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
761 message_size = st.st_size;
762 message_section = bfd_make_section (outbfd, ".nlmmessages");
763 if (message_section == NULL
764 || ! bfd_set_section_size (outbfd, message_section, message_size)
765 || ! bfd_set_section_flags (outbfd, message_section,
767 bfd_fatal ("message section");
768 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
773 struct string_list *l;
776 for (l = modules; l != NULL; l = l->next)
777 module_size += strlen (l->string) + 1;
778 module_section = bfd_make_section (outbfd, ".nlmmodules");
779 if (module_section == NULL
780 || ! bfd_set_section_size (outbfd, module_section, module_size)
781 || ! bfd_set_section_flags (outbfd, module_section,
783 bfd_fatal ("module section");
785 if (rpc_file != NULL)
787 rpc_data = fopen (rpc_file, "r");
789 || fstat (fileno (rpc_data), &st) < 0)
791 fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
797 rpc_size = st.st_size;
798 rpc_section = bfd_make_section (outbfd, ".nlmrpc");
799 if (rpc_section == NULL
800 || ! bfd_set_section_size (outbfd, rpc_section, rpc_size)
801 || ! bfd_set_section_flags (outbfd, rpc_section,
803 bfd_fatal ("rpc section");
804 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
807 if (sharelib_file != NULL)
809 sharedbfd = bfd_openr (sharelib_file, output_format);
810 if (sharedbfd == NULL
811 || ! bfd_check_format (sharedbfd, bfd_object))
813 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
814 bfd_errmsg (bfd_get_error ()));
815 sharelib_file = NULL;
819 sharedhdr = *nlm_fixed_header (sharedbfd);
820 bfd_close (sharedbfd);
821 shared_data = fopen (sharelib_file, "r");
822 if (shared_data == NULL
823 || (fstat (fileno (shared_data), &st) < 0))
825 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
827 sharelib_file = NULL;
831 /* If we were clever, we could just copy out the
832 sections of the shared library which we actually
833 need. However, we would have to figure out the sizes
834 of the external and public information, and that can
835 not be done without reading through them. */
836 if (sharedhdr.uninitializedDataSize > 0)
838 /* There is no place to record this information. */
840 "%s:%s: warning: shared libraries can not have uninitialized data\n",
841 program_name, sharelib_file);
843 shared_offset = st.st_size;
844 if (shared_offset > sharedhdr.codeImageOffset)
845 shared_offset = sharedhdr.codeImageOffset;
846 if (shared_offset > sharedhdr.dataImageOffset)
847 shared_offset = sharedhdr.dataImageOffset;
848 if (shared_offset > sharedhdr.relocationFixupOffset)
849 shared_offset = sharedhdr.relocationFixupOffset;
850 if (shared_offset > sharedhdr.externalReferencesOffset)
851 shared_offset = sharedhdr.externalReferencesOffset;
852 if (shared_offset > sharedhdr.publicsOffset)
853 shared_offset = sharedhdr.publicsOffset;
854 shared_size = st.st_size - shared_offset;
855 shared_section = bfd_make_section (outbfd, ".nlmshared");
856 if (shared_section == NULL
857 || ! bfd_set_section_size (outbfd, shared_section,
859 || ! bfd_set_section_flags (outbfd, shared_section,
861 bfd_fatal ("shared section");
862 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
867 /* Check whether a version was given. */
868 if (strncmp (version_hdr->stamp, "VeRsIoN#", 8) != 0)
869 fprintf (stderr, "%s: warning: No version number given\n",
872 /* At least for now, always create an extended header, because that
873 is what NLMLINK does. */
874 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
876 strncpy (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx", 8);
878 /* If the date was not given, force it in. */
879 if (nlm_version_header (outbfd)->month == 0
880 && nlm_version_header (outbfd)->day == 0
881 && nlm_version_header (outbfd)->year == 0)
887 ptm = localtime (&now);
888 nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
889 nlm_version_header (outbfd)->day = ptm->tm_mday;
890 nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
891 strncpy (version_hdr->stamp, "VeRsIoN#", 8);
894 #ifdef NLMCONV_POWERPC
895 /* Resolve the stubs we build for PowerPC NetWare. */
896 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
897 powerpc_resolve_stubs (inbfd, outbfd);
900 /* Copy over the sections. */
901 bfd_map_over_sections (inbfd, copy_sections, (PTR) outbfd);
903 /* Finish up the header information. */
904 if (custom_file != NULL)
908 data = xmalloc (custom_size);
909 if (fread (data, 1, custom_size, custom_data) != custom_size)
910 fprintf (stderr, "%s:%s: read: %s\n", program_name, custom_file,
914 if (! bfd_set_section_contents (outbfd, custom_section, data,
915 (file_ptr) 0, custom_size))
916 bfd_fatal ("custom section");
917 nlm_fixed_header (outbfd)->customDataOffset =
918 custom_section->filepos;
919 nlm_fixed_header (outbfd)->customDataSize = custom_size;
925 /* As a special hack, the backend recognizes a debugInfoOffset
926 of -1 to mean that it should not output any debugging
927 information. This can not be handling by fiddling with the
928 symbol table because exported symbols appear in both the
929 export information and the debugging information. */
930 nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
932 if (map_file != NULL)
934 "%s: warning: MAP and FULLMAP are not supported; try ld -M\n",
936 if (help_file != NULL)
940 data = xmalloc (help_size);
941 if (fread (data, 1, help_size, help_data) != help_size)
942 fprintf (stderr, "%s:%s: read: %s\n", program_name, help_file,
946 if (! bfd_set_section_contents (outbfd, help_section, data,
947 (file_ptr) 0, help_size))
948 bfd_fatal ("help section");
949 nlm_extended_header (outbfd)->helpFileOffset =
950 help_section->filepos;
951 nlm_extended_header (outbfd)->helpFileLength = help_size;
955 if (message_file != NULL)
959 data = xmalloc (message_size);
960 if (fread (data, 1, message_size, message_data) != message_size)
961 fprintf (stderr, "%s:%s: read: %s\n", program_name, message_file,
965 if (! bfd_set_section_contents (outbfd, message_section, data,
966 (file_ptr) 0, message_size))
967 bfd_fatal ("message section");
968 nlm_extended_header (outbfd)->messageFileOffset =
969 message_section->filepos;
970 nlm_extended_header (outbfd)->messageFileLength = message_size;
972 /* FIXME: Are these offsets correct on all platforms? Are
973 they 32 bits on all platforms? What endianness? */
974 nlm_extended_header (outbfd)->languageID =
975 bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
976 nlm_extended_header (outbfd)->messageCount =
977 bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
985 struct string_list *l;
988 data = xmalloc (module_size);
990 set = (unsigned char *) data;
991 for (l = modules; l != NULL; l = l->next)
993 *set = strlen (l->string);
994 strncpy (set + 1, l->string, *set);
998 if (! bfd_set_section_contents (outbfd, module_section, data,
999 (file_ptr) 0, module_size))
1000 bfd_fatal ("module section");
1001 nlm_fixed_header (outbfd)->moduleDependencyOffset =
1002 module_section->filepos;
1003 nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
1005 if (rpc_file != NULL)
1009 data = xmalloc (rpc_size);
1010 if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
1011 fprintf (stderr, "%s:%s: read: %s\n", program_name, rpc_file,
1015 if (! bfd_set_section_contents (outbfd, rpc_section, data,
1016 (file_ptr) 0, rpc_size))
1017 bfd_fatal ("rpc section");
1018 nlm_extended_header (outbfd)->RPCDataOffset =
1019 rpc_section->filepos;
1020 nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
1024 if (sharelib_file != NULL)
1028 data = xmalloc (shared_size);
1029 if (fseek (shared_data, shared_offset, SEEK_SET) != 0
1030 || fread (data, 1, shared_size, shared_data) != shared_size)
1031 fprintf (stderr, "%s:%s: read: %s\n", program_name, sharelib_file,
1035 if (! bfd_set_section_contents (outbfd, shared_section, data,
1036 (file_ptr) 0, shared_size))
1037 bfd_fatal ("shared section");
1039 nlm_extended_header (outbfd)->sharedCodeOffset =
1040 sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
1041 nlm_extended_header (outbfd)->sharedCodeLength =
1042 sharedhdr.codeImageSize;
1043 nlm_extended_header (outbfd)->sharedDataOffset =
1044 sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
1045 nlm_extended_header (outbfd)->sharedDataLength =
1046 sharedhdr.dataImageSize;
1047 nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
1048 (sharedhdr.relocationFixupOffset
1050 + shared_section->filepos);
1051 nlm_extended_header (outbfd)->sharedRelocationFixupCount =
1052 sharedhdr.numberOfRelocationFixups;
1053 nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
1054 (sharedhdr.externalReferencesOffset
1056 + shared_section->filepos);
1057 nlm_extended_header (outbfd)->sharedExternalReferenceCount =
1058 sharedhdr.numberOfExternalReferences;
1059 nlm_extended_header (outbfd)->sharedPublicsOffset =
1060 sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
1061 nlm_extended_header (outbfd)->sharedPublicsCount =
1062 sharedhdr.numberOfPublics;
1063 nlm_extended_header (outbfd)->sharedDebugRecordOffset =
1064 sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
1065 nlm_extended_header (outbfd)->sharedDebugRecordCount =
1066 sharedhdr.numberOfDebugRecords;
1067 nlm_extended_header (outbfd)->SharedInitializationOffset =
1068 sharedhdr.codeStartOffset;
1069 nlm_extended_header (outbfd)->SharedExitProcedureOffset =
1070 sharedhdr.exitProcedureOffset;
1073 len = strlen (output_file);
1074 if (len > NLM_MODULE_NAME_SIZE - 2)
1075 len = NLM_MODULE_NAME_SIZE - 2;
1076 nlm_fixed_header (outbfd)->moduleName[0] = len;
1078 strncpy (nlm_fixed_header (outbfd)->moduleName + 1, output_file,
1079 NLM_MODULE_NAME_SIZE - 2);
1080 nlm_fixed_header (outbfd)->moduleName[NLM_MODULE_NAME_SIZE - 1] = '\0';
1081 for (modname = nlm_fixed_header (outbfd)->moduleName;
1084 if (islower (*modname))
1085 *modname = toupper (*modname);
1087 strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
1088 NLM_OLD_THREAD_NAME_LENGTH);
1090 nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
1091 nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
1093 if (! bfd_close (outbfd))
1094 bfd_fatal (output_file);
1095 if (! bfd_close (inbfd))
1096 bfd_fatal (input_file);
1098 if (unlink_on_exit != NULL)
1099 unlink (unlink_on_exit);
1104 /* Display a help message and exit. */
1109 printf ("%s: Convert an object file into a NetWare Loadable Module\n",
1111 show_usage (stdout, 0);
1114 /* Show a usage message and exit. */
1117 show_usage (file, status)
1122 Usage: %s [-dhV] [-I bfdname] [-O bfdname] [-T header-file] [-l linker]\n\
1123 [--input-target=bfdname] [--output-target=bfdname]\n\
1124 [--header-file=file] [--linker=linker] [--debug]\n\
1125 [--help] [--version]\n\
1126 [in-file [out-file]]\n",
1131 /* Select the output format based on the input architecture, machine,
1132 and endianness. This chooses the appropriate NLM target. */
1135 select_output_format (arch, mach, bigendian)
1136 enum bfd_architecture arch;
1144 return "nlm32-i386";
1146 #ifdef NLMCONV_SPARC
1147 case bfd_arch_sparc:
1148 return "nlm32-sparc";
1150 #ifdef NLMCONV_ALPHA
1151 case bfd_arch_alpha:
1152 return "nlm32-alpha";
1154 #ifdef NLMCONV_POWERPC
1155 case bfd_arch_powerpc:
1156 return "nlm32-powerpc";
1159 fprintf (stderr, "%s: support not compiled in for %s\n",
1160 program_name, bfd_printable_arch_mach (arch, mach));
1162 /* Avoid warning. */
1168 /* The BFD sections are copied in two passes. This function selects
1169 the output section for each input section, and sets up the section
1173 setup_sections (inbfd, insec, data_ptr)
1178 bfd *outbfd = (bfd *) data_ptr;
1180 const char *outname;
1183 bfd_size_type align;
1185 bfd_size_type secsecsize;
1187 f = bfd_get_section_flags (inbfd, insec);
1189 outname = NLM_CODE_NAME;
1190 else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
1191 outname = NLM_INITIALIZED_DATA_NAME;
1192 else if (f & SEC_ALLOC)
1193 outname = NLM_UNINITIALIZED_DATA_NAME;
1195 outname = bfd_section_name (inbfd, insec);
1197 outsec = bfd_get_section_by_name (outbfd, outname);
1200 outsec = bfd_make_section (outbfd, outname);
1202 bfd_fatal ("make section");
1205 insec->output_section = outsec;
1207 offset = bfd_section_size (outbfd, outsec);
1208 align = 1 << bfd_section_alignment (inbfd, insec);
1209 add = ((offset + align - 1) &~ (align - 1)) - offset;
1210 insec->output_offset = offset + add;
1212 if (! bfd_set_section_size (outbfd, outsec,
1213 (bfd_section_size (outbfd, outsec)
1214 + bfd_section_size (inbfd, insec)
1216 bfd_fatal ("set section size");
1218 if ((bfd_section_alignment (inbfd, insec)
1219 > bfd_section_alignment (outbfd, outsec))
1220 && ! bfd_set_section_alignment (outbfd, outsec,
1221 bfd_section_alignment (inbfd, insec)))
1222 bfd_fatal ("set section alignment");
1224 if (! bfd_set_section_flags (outbfd, outsec, f))
1225 bfd_fatal ("set section flags");
1227 bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
1229 /* For each input section we allocate space for an entry in
1231 secsecsize = bfd_section_size (outbfd, secsec);
1232 secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
1233 secsecsize = (secsecsize + 3) &~ 3;
1235 if (! bfd_set_section_size (outbfd, secsec, secsecsize))
1236 bfd_fatal ("set .nlmsections size");
1239 /* Copy the section contents. */
1242 copy_sections (inbfd, insec, data_ptr)
1247 static bfd_size_type secsecoff = 0;
1248 bfd *outbfd = (bfd *) data_ptr;
1257 inname = bfd_section_name (inbfd, insec);
1259 outsec = insec->output_section;
1260 assert (outsec != NULL);
1262 size = bfd_get_section_size_before_reloc (insec);
1264 /* FIXME: Why are these necessary? */
1265 insec->_cooked_size = insec->_raw_size;
1266 insec->reloc_done = true;
1268 if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
1272 contents = xmalloc (size);
1273 if (! bfd_get_section_contents (inbfd, insec, contents,
1274 (file_ptr) 0, size))
1275 bfd_fatal (bfd_get_filename (inbfd));
1278 reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
1280 bfd_fatal (bfd_get_filename (inbfd));
1281 if (reloc_size != 0)
1286 relocs = (arelent **) xmalloc (reloc_size);
1287 reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
1288 if (reloc_count < 0)
1289 bfd_fatal (bfd_get_filename (inbfd));
1290 mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
1293 /* FIXME: refers to internal BFD fields. */
1294 if (outsec->orelocation != (arelent **) NULL)
1296 bfd_size_type total_count;
1299 total_count = reloc_count + outsec->reloc_count;
1300 combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
1301 memcpy (combined, outsec->orelocation,
1302 outsec->reloc_count * sizeof (arelent *));
1303 memcpy (combined + outsec->reloc_count, relocs,
1304 (size_t) (reloc_count * sizeof (arelent *)));
1305 free (outsec->orelocation);
1306 reloc_count = total_count;
1310 bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
1313 if (contents != NULL)
1315 if (! bfd_set_section_contents (outbfd, outsec, contents,
1316 insec->output_offset, size))
1317 bfd_fatal (bfd_get_filename (outbfd));
1321 /* Add this section to .nlmsections. */
1322 if (! bfd_set_section_contents (outbfd, secsec, (PTR) inname, secsecoff,
1323 strlen (inname) + 1))
1324 bfd_fatal ("set .nlmsection contents");
1325 secsecoff += strlen (inname) + 1;
1327 add = ((secsecoff + 3) &~ 3) - secsecoff;
1330 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1331 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
1332 bfd_fatal ("set .nlmsection contents");
1336 if (contents != NULL)
1337 bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
1339 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1340 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1341 bfd_fatal ("set .nlmsection contents");
1344 bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
1345 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1346 bfd_fatal ("set .nlmsection contents");
1350 /* Some, perhaps all, NetWare targets require changing the relocs used
1351 by the input formats. */
1354 mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1358 arelent ***relocs_ptr;
1359 long *reloc_count_ptr;
1361 bfd_size_type contents_size;
1363 switch (bfd_get_arch (outbfd))
1367 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1368 contents, contents_size);
1371 #ifdef NLMCONV_ALPHA
1372 case bfd_arch_alpha:
1373 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1374 contents, contents_size);
1377 #ifdef NLMCONV_POWERPC
1378 case bfd_arch_powerpc:
1379 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1380 contents, contents_size);
1384 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1385 contents, contents_size);
1390 /* By default all we need to do for relocs is change the address by
1391 the output_offset. */
1395 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1399 arelent ***relocs_ptr;
1400 long *reloc_count_ptr;
1402 bfd_size_type contents_size;
1404 if (insec->output_offset != 0)
1407 register arelent **relocs;
1410 reloc_count = *reloc_count_ptr;
1411 relocs = *relocs_ptr;
1412 for (i = 0; i < reloc_count; i++, relocs++)
1413 (*relocs)->address += insec->output_offset;
1419 /* NetWare on the i386 supports a restricted set of relocs, which are
1420 different from those used on other i386 targets. This routine
1421 converts the relocs. It is, obviously, very target dependent. At
1422 the moment, the nlm32-i386 backend performs similar translations;
1423 however, it is more reliable and efficient to do them here. */
1425 static reloc_howto_type nlm_i386_pcrel_howto =
1426 HOWTO (1, /* type */
1428 2, /* size (0 = byte, 1 = short, 2 = long) */
1430 true, /* pc_relative */
1432 complain_overflow_signed, /* complain_on_overflow */
1433 0, /* special_function */
1434 "DISP32", /* name */
1435 true, /* partial_inplace */
1436 0xffffffff, /* src_mask */
1437 0xffffffff, /* dst_mask */
1438 true); /* pcrel_offset */
1441 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1445 arelent ***relocs_ptr;
1446 long *reloc_count_ptr;
1448 bfd_size_type contents_size;
1450 long reloc_count, i;
1453 reloc_count = *reloc_count_ptr;
1454 relocs = *relocs_ptr;
1455 for (i = 0; i < reloc_count; i++)
1459 bfd_size_type address;
1463 sym = *rel->sym_ptr_ptr;
1465 /* We're moving the relocs from the input section to the output
1466 section, so we must adjust the address accordingly. */
1467 address = rel->address;
1468 rel->address += insec->output_offset;
1470 /* Note that no serious harm will ensue if we fail to change a
1471 reloc. The backend will fail when writing out the reloc. */
1473 /* Make sure this reloc is within the data we have. We use only
1474 4 byte relocs here, so we insist on having 4 bytes. */
1475 if (address + 4 > contents_size)
1478 /* A PC relative reloc entirely within a single section is
1479 completely unnecessary. This can be generated by ld -r. */
1480 if (sym == insec->symbol
1481 && rel->howto != NULL
1482 && rel->howto->pc_relative
1483 && ! rel->howto->pcrel_offset)
1487 memmove (relocs, relocs + 1,
1488 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1492 /* Get the amount the relocation will add in. */
1493 addend = rel->addend + sym->value;
1495 /* NetWare doesn't support PC relative relocs against defined
1496 symbols, so we have to eliminate them by doing the relocation
1497 now. We can only do this if the reloc is within a single
1499 if (rel->howto != NULL
1500 && rel->howto->pc_relative
1501 && bfd_get_section (sym) == insec->output_section)
1505 if (rel->howto->pcrel_offset)
1508 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1510 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1514 memmove (relocs, relocs + 1,
1515 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1519 /* NetWare doesn't support reloc addends, so we get rid of them
1520 here by simply adding them into the object data. We handle
1521 the symbol value, if any, the same way. */
1523 && rel->howto != NULL
1524 && rel->howto->rightshift == 0
1525 && rel->howto->size == 2
1526 && rel->howto->bitsize == 32
1527 && rel->howto->bitpos == 0
1528 && rel->howto->src_mask == 0xffffffff
1529 && rel->howto->dst_mask == 0xffffffff)
1533 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1535 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1537 /* Adjust the reloc for the changes we just made. */
1539 if (! bfd_is_und_section (bfd_get_section (sym)))
1540 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
1543 /* NetWare uses a reloc with pcrel_offset set. We adjust
1544 pc_relative relocs accordingly. We are going to change the
1545 howto field, so we can only do this if the current one is
1546 compatible. We should check that special_function is NULL
1547 here, but at the moment coff-i386 uses a special_function
1548 which does not affect what we are doing here. */
1549 if (rel->howto != NULL
1550 && rel->howto->pc_relative
1551 && ! rel->howto->pcrel_offset
1552 && rel->howto->rightshift == 0
1553 && rel->howto->size == 2
1554 && rel->howto->bitsize == 32
1555 && rel->howto->bitpos == 0
1556 && rel->howto->src_mask == 0xffffffff
1557 && rel->howto->dst_mask == 0xffffffff)
1561 /* When pcrel_offset is not set, it means that the negative
1562 of the address of the memory location is stored in the
1563 memory location. We must add it back in. */
1564 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1566 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1568 /* We must change to a new howto. */
1569 rel->howto = &nlm_i386_pcrel_howto;
1574 #endif /* NLMCONV_I386 */
1576 #ifdef NLMCONV_ALPHA
1578 /* On the Alpha the first reloc for every section must be a special
1579 relocs which hold the GP address. Also, the first reloc in the
1580 file must be a special reloc which holds the address of the .lita
1583 static reloc_howto_type nlm32_alpha_nw_howto =
1584 HOWTO (ALPHA_R_NW_RELOC, /* type */
1586 0, /* size (0 = byte, 1 = short, 2 = long) */
1588 false, /* pc_relative */
1590 complain_overflow_dont, /* complain_on_overflow */
1591 0, /* special_function */
1592 "NW_RELOC", /* name */
1593 false, /* partial_inplace */
1596 false); /* pcrel_offset */
1600 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1604 register arelent ***relocs_ptr;
1605 long *reloc_count_ptr;
1607 bfd_size_type contents_size;
1609 long old_reloc_count;
1610 arelent **old_relocs;
1611 register arelent **relocs;
1613 old_reloc_count = *reloc_count_ptr;
1614 old_relocs = *relocs_ptr;
1615 relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
1616 *relocs_ptr = relocs;
1618 if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
1621 asection *lita_section;
1623 inbfd = insec->owner;
1624 lita_section = bfd_get_section_by_name (inbfd, _LITA);
1625 if (lita_section != (asection *) NULL)
1627 nlm_alpha_backend_data (outbfd)->lita_address =
1628 bfd_get_section_vma (inbfd, lita_section);
1629 nlm_alpha_backend_data (outbfd)->lita_size =
1630 bfd_section_size (inbfd, lita_section);
1634 /* Avoid outputting this reloc again. */
1635 nlm_alpha_backend_data (outbfd)->lita_address = 4;
1638 *relocs = (arelent *) xmalloc (sizeof (arelent));
1639 (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1640 (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
1641 (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
1642 (*relocs)->howto = &nlm32_alpha_nw_howto;
1644 ++(*reloc_count_ptr);
1647 /* Get the GP value from bfd. */
1648 if (nlm_alpha_backend_data (outbfd)->gp == 0)
1649 nlm_alpha_backend_data (outbfd)->gp =
1650 bfd_ecoff_get_gp_value (insec->owner);
1652 *relocs = (arelent *) xmalloc (sizeof (arelent));
1653 (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1654 (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
1655 (*relocs)->addend = 0;
1656 (*relocs)->howto = &nlm32_alpha_nw_howto;
1658 ++(*reloc_count_ptr);
1660 memcpy ((PTR) relocs, (PTR) old_relocs,
1661 (size_t) old_reloc_count * sizeof (arelent *));
1662 relocs[old_reloc_count] = (arelent *) NULL;
1666 if (insec->output_offset != 0)
1668 register bfd_size_type i;
1670 for (i = 0; i < old_reloc_count; i++, relocs++)
1671 (*relocs)->address += insec->output_offset;
1675 #endif /* NLMCONV_ALPHA */
1677 #ifdef NLMCONV_POWERPC
1679 /* We keep a linked list of stubs which we must build. Because BFD
1680 requires us to know the sizes of all sections before we can set the
1681 contents of any, we must figure out which stubs we want to build
1682 before we can actually build any of them. */
1686 /* Next stub in linked list. */
1687 struct powerpc_stub *next;
1689 /* Symbol whose value is the start of the stub. This is a symbol
1690 whose name begins with `.'. */
1693 /* Symbol we are going to create a reloc against. This is a symbol
1694 with the same name as START but without the leading `.'. */
1697 /* The TOC index for this stub. This is the index into the TOC
1698 section at which the reloc is created. */
1699 unsigned int toc_index;
1702 /* The linked list of stubs. */
1704 static struct powerpc_stub *powerpc_stubs;
1706 /* This is what a stub looks like. The first instruction will get
1707 adjusted with the correct TOC index. */
1709 static unsigned long powerpc_stub_insns[] =
1711 0x81820000, /* lwz r12,0(r2) */
1712 0x90410014, /* stw r2,20(r1) */
1713 0x800c0000, /* lwz r0,0(r12) */
1714 0x804c0004, /* lwz r2,r(r12) */
1715 0x7c0903a6, /* mtctr r0 */
1716 0x4e800420, /* bctr */
1717 0, /* Traceback table. */
1722 #define POWERPC_STUB_INSN_COUNT \
1723 (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
1725 #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
1727 /* Each stub uses a four byte TOC entry. */
1728 #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
1730 /* The original size of the .got section. */
1731 static bfd_size_type powerpc_initial_got_size;
1733 /* Look for all undefined symbols beginning with `.', and prepare to
1734 build a stub for each one. */
1737 powerpc_build_stubs (inbfd, outbfd, symbols_ptr, symcount_ptr)
1740 asymbol ***symbols_ptr;
1745 unsigned int got_base;
1750 /* Make a section to hold stubs. We don't set SEC_HAS_CONTENTS for
1751 the section to prevent copy_sections from reading from it. */
1752 stub_sec = bfd_make_section (inbfd, ".stubs");
1753 if (stub_sec == (asection *) NULL
1754 || ! bfd_set_section_flags (inbfd, stub_sec,
1759 || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
1760 bfd_fatal (".stubs");
1762 /* Get the TOC section, which is named .got. */
1763 got_sec = bfd_get_section_by_name (inbfd, ".got");
1764 if (got_sec == (asection *) NULL)
1766 got_sec = bfd_make_section (inbfd, ".got");
1767 if (got_sec == (asection *) NULL
1768 || ! bfd_set_section_flags (inbfd, got_sec,
1773 | SEC_HAS_CONTENTS))
1774 || ! bfd_set_section_alignment (inbfd, got_sec, 2))
1778 powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
1779 got_base = powerpc_initial_got_size;
1780 got_base = (got_base + 3) &~ 3;
1784 symcount = *symcount_ptr;
1785 for (i = 0; i < symcount; i++)
1790 struct powerpc_stub *item;
1792 sym = (*symbols_ptr)[i];
1794 /* We must make a stub for every undefined symbol whose name
1796 if (bfd_asymbol_name (sym)[0] != '.'
1797 || ! bfd_is_und_section (bfd_get_section (sym)))
1800 /* Make a new undefined symbol with the same name but without
1802 newsym = (asymbol *) xmalloc (sizeof (asymbol));
1804 newname = (char *) xmalloc (strlen (bfd_asymbol_name (sym)));
1805 strcpy (newname, bfd_asymbol_name (sym) + 1);
1806 newsym->name = newname;
1808 /* Define the `.' symbol to be in the stub section. */
1809 sym->section = stub_sec;
1810 sym->value = stubcount * POWERPC_STUB_SIZE;
1811 /* We set the BSF_DYNAMIC flag here so that we can check it when
1812 we are mangling relocs. FIXME: This is a hack. */
1813 sym->flags = BSF_LOCAL | BSF_DYNAMIC;
1815 /* Add this stub to the linked list. */
1816 item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
1818 item->reloc = newsym;
1819 item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
1821 item->next = powerpc_stubs;
1822 powerpc_stubs = item;
1830 struct powerpc_stub *l;
1832 /* Add the new symbols we just created to the symbol table. */
1833 *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
1834 ((symcount + stubcount)
1835 * sizeof (asymbol)));
1836 *symcount_ptr += stubcount;
1837 s = &(*symbols_ptr)[symcount];
1838 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1841 /* Set the size of the .stubs section and increase the size of
1842 the .got section. */
1843 if (! bfd_set_section_size (inbfd, stub_sec,
1844 stubcount * POWERPC_STUB_SIZE)
1845 || ! bfd_set_section_size (inbfd, got_sec,
1848 * POWERPC_STUB_TOC_ENTRY_SIZE))))
1849 bfd_fatal ("stub section sizes");
1852 /* PowerPC NetWare requires a custom header. We create it here.
1853 The first word is the header version number, currently 1. The
1854 second word is the timestamp of the input file. Unfortunately,
1855 they do not conform to the emergent standard for custom headers.
1856 We must fake the version number and timestamp in the offset and
1858 memcpy (nlm_custom_header (outbfd)->stamp, "CuStHeAd", 8);
1859 nlm_custom_header (outbfd)->hdrLength = 0;
1860 /* Put version number in dataOffset field. */
1861 nlm_custom_header (outbfd)->dataOffset = 1;
1862 /* Put timestamp in length field. */
1866 if (stat (bfd_get_filename (inbfd), &s) < 0)
1868 nlm_custom_header (outbfd)->dataLength = s.st_mtime;
1870 /* No data stamp. */
1871 memset (nlm_custom_header (outbfd)->dataStamp, 0,
1872 sizeof (nlm_custom_header (outbfd)->dataStamp));
1875 /* Resolve all the stubs for PowerPC NetWare. We fill in the contents
1876 of the output section, and create new relocs in the TOC. */
1879 powerpc_resolve_stubs (inbfd, outbfd)
1883 bfd_byte buf[POWERPC_STUB_SIZE];
1885 unsigned int stubcount;
1889 struct powerpc_stub *l;
1891 if (powerpc_stubs == (struct powerpc_stub *) NULL)
1894 for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
1895 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
1897 got_sec = bfd_get_section_by_name (inbfd, ".got");
1898 assert (got_sec != (asection *) NULL);
1899 assert (got_sec->output_section->orelocation == (arelent **) NULL);
1902 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1904 relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
1907 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1911 /* Adjust the first instruction to use the right TOC index. */
1912 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
1914 /* Write this stub out. */
1915 if (! bfd_set_section_contents (outbfd,
1916 bfd_get_section (l->start),
1920 bfd_fatal ("writing stub");
1922 /* Create a new reloc for the TOC entry. */
1923 reloc = (arelent *) xmalloc (sizeof (arelent));
1924 reloc->sym_ptr_ptr = &l->reloc;
1925 reloc->address = l->toc_index + got_sec->output_offset;
1927 reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
1932 bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
1935 /* Adjust relocation entries for PowerPC NetWare. We do not output
1936 TOC relocations. The object code already contains the offset from
1937 the TOC pointer. When the function is called, the TOC register,
1938 r2, will be set to the correct TOC value, so there is no need for
1939 any further reloc. */
1943 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr, contents,
1947 register arelent ***relocs_ptr;
1948 long *reloc_count_ptr;
1950 bfd_size_type contents_size;
1952 const reloc_howto_type *toc_howto;
1954 register arelent **relocs;
1957 toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
1958 if (toc_howto == (reloc_howto_type *) NULL)
1961 /* If this is the .got section, clear out all the contents beyond
1962 the initial size. We must do this here because copy_sections is
1963 going to write out whatever we return in the contents field. */
1964 if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
1965 memset (contents + powerpc_initial_got_size, 0,
1966 (bfd_get_section_size_after_reloc (insec)
1967 - powerpc_initial_got_size));
1969 reloc_count = *reloc_count_ptr;
1970 relocs = *relocs_ptr;
1971 for (i = 0; i < reloc_count; i++)
1978 sym = *rel->sym_ptr_ptr;
1980 /* We must be able to resolve all PC relative relocs at this
1981 point. If we get a branch to an undefined symbol we build a
1982 stub, since NetWare will resolve undefined symbols into a
1983 pointer to a function descriptor. */
1984 if (rel->howto->pc_relative)
1986 /* This check for whether a symbol is in the same section as
1987 the reloc will be wrong if there is a PC relative reloc
1988 between two sections both of which were placed in the
1989 same output section. This should not happen. */
1990 if (bfd_get_section (sym) != insec->output_section)
1991 fprintf (stderr, "%s: unresolved PC relative reloc against %s\n",
1992 program_name, bfd_asymbol_name (sym));
1997 assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
1998 val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
1999 val = ((val &~ rel->howto->dst_mask)
2000 | (((val & rel->howto->src_mask)
2001 + (sym->value - rel->address)
2003 & rel->howto->dst_mask));
2004 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
2006 /* If this reloc is against an stubbed symbol and the
2009 then we replace the next instruction with
2011 This reloads the TOC pointer after a stub call. */
2012 if (bfd_asymbol_name (sym)[0] == '.'
2013 && (sym->flags & BSF_DYNAMIC) != 0
2014 && (bfd_get_32 (outbfd,
2015 (bfd_byte *) contents + rel->address + 4)
2016 == 0x4ffffb82)) /* cror 31,31,31 */
2017 bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
2018 (bfd_byte *) contents + rel->address + 4);
2022 memmove (relocs, relocs + 1,
2023 (size_t) ((reloc_count - 1) * sizeof (arelent *)));
2028 /* When considering a TOC reloc, we do not want to include the
2029 symbol value. The symbol will be start of the TOC section
2030 (which is named .got). We do want to include the addend. */
2031 if (rel->howto == toc_howto)
2034 symvalue = sym->value;
2036 /* If this is a relocation against a symbol with a value, or
2037 there is a reloc addend, we need to update the addend in the
2039 if (symvalue + rel->addend != 0)
2043 switch (rel->howto->size)
2046 val = bfd_get_16 (outbfd,
2047 (bfd_byte *) contents + rel->address);
2048 val = ((val &~ rel->howto->dst_mask)
2049 | (((val & rel->howto->src_mask)
2052 & rel->howto->dst_mask));
2053 if ((bfd_signed_vma) val < - 0x8000
2054 || (bfd_signed_vma) val >= 0x8000)
2056 "%s: overflow when adjusting relocation against %s\n",
2057 program_name, bfd_asymbol_name (sym));
2058 bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
2062 val = bfd_get_32 (outbfd,
2063 (bfd_byte *) contents + rel->address);
2064 val = ((val &~ rel->howto->dst_mask)
2065 | (((val & rel->howto->src_mask)
2068 & rel->howto->dst_mask));
2069 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
2076 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
2080 /* Now that we have incorporated the addend, remove any TOC
2082 if (rel->howto == toc_howto)
2086 memmove (relocs, relocs + 1,
2087 (size_t) ((reloc_count - i) * sizeof (arelent *)));
2091 rel->address += insec->output_offset;
2095 #endif /* NLMCONV_POWERPC */
2097 /* Name of linker. */
2099 #define LD_NAME "ld"
2102 /* Temporary file name base. */
2103 static char *temp_filename;
2105 /* The user has specified several input files. Invoke the linker to
2106 link them all together, and convert and delete the resulting output
2110 link_inputs (inputs, ld)
2111 struct string_list *inputs;
2115 struct string_list *q;
2122 for (q = inputs; q != NULL; q = q->next)
2125 argv = (char **) alloca (c + 5);
2132 /* Find the linker to invoke based on how nlmconv was run. */
2133 p = program_name + strlen (program_name);
2134 while (p != program_name)
2138 ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
2139 memcpy (ld, program_name, p - program_name);
2140 strcpy (ld + (p - program_name), LD_NAME);
2149 ld = (char *) LD_NAME;
2151 choose_temp_base ();
2153 unlink_on_exit = xmalloc (strlen (temp_filename) + 3);
2154 sprintf (unlink_on_exit, "%s.O", temp_filename);
2157 argv[1] = (char *) "-r";
2158 argv[2] = (char *) "-o";
2159 argv[3] = unlink_on_exit;
2161 for (q = inputs; q != NULL; q = q->next, i++)
2162 argv[i] = q->string;
2167 for (i = 0; argv[i] != NULL; i++)
2168 fprintf (stderr, " %s", argv[i]);
2169 fprintf (stderr, "\n");
2172 pid = pexecute (ld, argv);
2174 if (waitpid (pid, &status, 0) < 0)
2177 unlink (unlink_on_exit);
2183 fprintf (stderr, "%s: Execution of %s failed\n", program_name, ld);
2184 unlink (unlink_on_exit);
2188 return unlink_on_exit;
2191 /* Choose a temporary file name. Stolen from gcc.c. */
2194 choose_temp_base_try (try, base)
2202 else if (try == NULL)
2204 else if (access (try, R_OK | W_OK) != 0)
2214 const char *base = NULL;
2217 base = choose_temp_base_try (getenv ("TMPDIR"), base);
2218 base = choose_temp_base_try (getenv ("TMP"), base);
2219 base = choose_temp_base_try (getenv ("TEMP"), base);
2222 base = choose_temp_base_try (P_tmpdir, base);
2225 base = choose_temp_base_try ("/usr/tmp", base);
2226 base = choose_temp_base_try ("/tmp", base);
2228 /* If all else fails, use the current directory! */
2232 len = strlen (base);
2233 temp_filename = xmalloc (len + sizeof("/ccXXXXXX") + 1);
2234 strcpy (temp_filename, base);
2235 if (len > 0 && temp_filename[len-1] != '/')
2236 temp_filename[len++] = '/';
2237 strcpy (temp_filename + len, "ccXXXXXX");
2239 mktemp (temp_filename);
2240 if (*temp_filename == '\0')
2244 /* Execute a job. Stolen from gcc.c. */
2250 pexecute (program, argv)
2258 scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 10);
2259 rf = scmd + strlen(program) + 2 + el;
2260 sprintf (scmd, "%s.exe @%s.gp", program, temp_filename);
2261 argfile = fopen (rf, "w");
2263 pfatal_with_name (rf);
2265 for (i=1; argv[i]; i++)
2268 for (cp = argv[i]; *cp; cp++)
2270 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
2271 fputc ('\\', argfile);
2272 fputc (*cp, argfile);
2274 fputc ('\n', argfile);
2285 return MIN_FATAL_STATUS << 8;
2291 #else /* not __MSDOS__ */
2294 pexecute (program, argv)
2299 int retries, sleep_interval;
2301 /* Fork a subprocess; wait and retry if it fails. */
2303 for (retries = 0; retries < 4; retries++)
2308 sleep (sleep_interval);
2309 sleep_interval *= 2;
2325 /* Exec the program. */
2326 execvp (program, argv);
2333 /* Return child's process number. */
2338 #endif /* not __MSDOS__ */
2342 pexecute (program, argv)
2346 return spawnvp (1, program, argv);
2348 #endif /* not OS2 */