1 /* nlmconv.c -- NLM conversion program
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Written by Ian Lance Taylor <ian@cygnus.com>.
23 This program can be used to convert any appropriate object file
24 into a NetWare Loadable Module (an NLM). It will accept a linker
25 specification file which is identical to that accepted by the
26 NetWare linker, NLMLINK. */
28 /* AIX requires this to be the first thing in the file. */
36 #include "libiberty.h"
38 #include "safe-ctype.h"
47 /* Internal BFD NLM header. */
53 #include "coff/ecoff.h"
56 /* If strerror is just a macro, we want to use the one from libiberty
57 since it will handle undefined values. */
59 extern char *strerror (int);
62 extern struct tm *localtime ();
75 /* Global variables. */
77 /* The name used to invoke the program. */
80 /* Local variables. */
82 /* Whether to print out debugging information (currently just controls
83 whether it prints the linker command if there is one). */
86 /* The symbol table. */
87 static asymbol **symbols;
89 /* A section we create in the output file to hold pointers to where
90 the sections of the input file end up. We will put a pointer to
91 this section in the NLM header. These is an entry for each input
92 section. The format is
93 null terminated section name
94 zeroes to adjust to 4 byte boundary
95 4 byte section data file pointer
97 We don't need a version number. The way we find this information
98 is by finding a stamp in the NLM header information. If we need to
99 change the format of this information, we can simply change the
101 static asection *secsec;
103 /* A temporary file name to be unlinked on exit. Actually, for most
104 errors, we leave it around. It's not clear whether that is helpful
106 static char *unlink_on_exit;
108 /* The list of long options. */
109 static struct option long_options[] =
111 { "debug", no_argument, 0, 'd' },
112 { "header-file", required_argument, 0, 'T' },
113 { "help", no_argument, 0, 'h' },
114 { "input-target", required_argument, 0, 'I' },
115 { "input-format", required_argument, 0, 'I' }, /* Obsolete */
116 { "linker", required_argument, 0, 'l' },
117 { "output-target", required_argument, 0, 'O' },
118 { "output-format", required_argument, 0, 'O' }, /* Obsolete */
119 { "version", no_argument, 0, 'V' },
120 { NULL, no_argument, 0, 0 }
123 /* Local routines. */
125 int main (int, char **);
127 static void show_usage (FILE *, int);
128 static const char *select_output_format
129 (enum bfd_architecture, unsigned long, bfd_boolean);
130 static void setup_sections (bfd *, asection *, void *);
131 static void copy_sections (bfd *, asection *, void *);
132 static void mangle_relocs
133 (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
134 static void default_mangle_relocs
135 (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
136 static char *link_inputs (struct string_list *, char *, char *);
139 static void i386_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
143 static void alpha_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
146 #ifdef NLMCONV_POWERPC
147 static void powerpc_build_stubs (bfd *, bfd *, asymbol ***, long *);
148 static void powerpc_resolve_stubs (bfd *, bfd *);
149 static void powerpc_mangle_relocs (bfd *, asection *, arelent ***, long *, char *, bfd_size_type);
152 /* The main routine. */
155 main (int argc, char **argv)
158 char *input_file = NULL;
159 const char *input_format = NULL;
160 const char *output_format = NULL;
161 const char *header_file = NULL;
163 Nlm_Internal_Fixed_Header fixed_hdr_struct;
164 Nlm_Internal_Variable_Header var_hdr_struct;
165 Nlm_Internal_Version_Header version_hdr_struct;
166 Nlm_Internal_Copyright_Header copyright_hdr_struct;
167 Nlm_Internal_Extended_Header extended_hdr_struct;
170 asymbol **newsyms, **outsyms;
171 long symcount, newsymalloc, newsymcount;
173 asection *text_sec, *bss_sec, *data_sec;
178 char inlead, outlead;
179 bfd_boolean gotstart, gotexit, gotcheck;
181 FILE *custom_data = NULL;
182 FILE *help_data = NULL;
183 FILE *message_data = NULL;
184 FILE *rpc_data = NULL;
185 FILE *shared_data = NULL;
186 size_t custom_size = 0;
187 size_t help_size = 0;
188 size_t message_size = 0;
189 size_t module_size = 0;
191 asection *custom_section = NULL;
192 asection *help_section = NULL;
193 asection *message_section = NULL;
194 asection *module_section = NULL;
195 asection *rpc_section = NULL;
196 asection *shared_section = NULL;
198 size_t shared_offset = 0;
199 size_t shared_size = 0;
200 Nlm_Internal_Fixed_Header sharedhdr;
205 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
206 setlocale (LC_MESSAGES, "");
208 #if defined (HAVE_SETLOCALE)
209 setlocale (LC_CTYPE, "");
211 bindtextdomain (PACKAGE, LOCALEDIR);
212 textdomain (PACKAGE);
214 program_name = argv[0];
215 xmalloc_set_program_name (program_name);
218 set_default_bfd_target ();
220 while ((opt = getopt_long (argc, argv, "dHhI:l:O:T:Vv", long_options,
231 show_usage (stdout, 0);
234 input_format = optarg;
240 output_format = optarg;
243 header_file = optarg;
247 print_version ("nlmconv");
252 show_usage (stderr, 1);
257 /* The input and output files may be named on the command line. */
261 input_file = argv[optind];
265 output_file = argv[optind];
268 show_usage (stderr, 1);
269 if (strcmp (input_file, output_file) == 0)
271 fatal (_("input and output files must be different"));
276 /* Initialize the header information to default values. */
277 fixed_hdr = &fixed_hdr_struct;
278 memset ((void *) &fixed_hdr_struct, 0, sizeof fixed_hdr_struct);
279 var_hdr = &var_hdr_struct;
280 memset ((void *) &var_hdr_struct, 0, sizeof var_hdr_struct);
281 version_hdr = &version_hdr_struct;
282 memset ((void *) &version_hdr_struct, 0, sizeof version_hdr_struct);
283 copyright_hdr = ©right_hdr_struct;
284 memset ((void *) ©right_hdr_struct, 0, sizeof copyright_hdr_struct);
285 extended_hdr = &extended_hdr_struct;
286 memset ((void *) &extended_hdr_struct, 0, sizeof extended_hdr_struct);
287 check_procedure = NULL;
290 exit_procedure = "_Stop";
291 export_symbols = NULL;
295 import_symbols = NULL;
298 sharelib_file = NULL;
299 start_procedure = "_Prelude";
305 /* Parse the header file (if there is one). */
306 if (header_file != NULL)
308 if (! nlmlex_file (header_file)
310 || parse_errors != 0)
314 if (input_files != NULL)
316 if (input_file != NULL)
318 fatal (_("input file named both on command line and with INPUT"));
320 if (input_files->next == NULL)
321 input_file = input_files->string;
323 input_file = link_inputs (input_files, ld_arg, map_file);
325 else if (input_file == NULL)
327 non_fatal (_("no input file"));
328 show_usage (stderr, 1);
331 inbfd = bfd_openr (input_file, input_format);
333 bfd_fatal (input_file);
335 if (! bfd_check_format_matches (inbfd, bfd_object, &matching))
337 bfd_nonfatal (input_file);
338 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
340 list_matching_formats (matching);
346 if (output_format == NULL)
347 output_format = select_output_format (bfd_get_arch (inbfd),
348 bfd_get_mach (inbfd),
349 bfd_big_endian (inbfd));
351 assert (output_format != NULL);
353 /* Use the output file named on the command line if it exists.
354 Otherwise use the file named in the OUTPUT statement. */
355 if (output_file == NULL)
357 non_fatal (_("no name for output file"));
358 show_usage (stderr, 1);
361 outbfd = bfd_openw (output_file, output_format);
363 bfd_fatal (output_file);
364 if (! bfd_set_format (outbfd, bfd_object))
365 bfd_fatal (output_file);
367 assert (bfd_get_flavour (outbfd) == bfd_target_nlm_flavour);
369 /* XXX: Should we accept the unknown bfd format here ? */
370 if (bfd_arch_get_compatible (inbfd, outbfd, TRUE) == NULL)
371 non_fatal (_("warning: input and output formats are not compatible"));
373 /* Move the values read from the command file into outbfd. */
374 *nlm_fixed_header (outbfd) = fixed_hdr_struct;
375 *nlm_variable_header (outbfd) = var_hdr_struct;
376 *nlm_version_header (outbfd) = version_hdr_struct;
377 *nlm_copyright_header (outbfd) = copyright_hdr_struct;
378 *nlm_extended_header (outbfd) = extended_hdr_struct;
380 /* Start copying the input BFD to the output BFD. */
381 if (! bfd_set_file_flags (outbfd, bfd_get_file_flags (inbfd)))
382 bfd_fatal (bfd_get_filename (outbfd));
384 symsize = bfd_get_symtab_upper_bound (inbfd);
386 bfd_fatal (input_file);
387 symbols = (asymbol **) xmalloc (symsize);
388 symcount = bfd_canonicalize_symtab (inbfd, symbols);
390 bfd_fatal (input_file);
392 /* Make sure we have a .bss section. */
393 bss_sec = bfd_get_section_by_name (outbfd, NLM_UNINITIALIZED_DATA_NAME);
396 bss_sec = bfd_make_section (outbfd, NLM_UNINITIALIZED_DATA_NAME);
398 || ! bfd_set_section_flags (outbfd, bss_sec, SEC_ALLOC)
399 || ! bfd_set_section_alignment (outbfd, bss_sec, 1))
400 bfd_fatal (_("make .bss section"));
403 /* We store the original section names in the .nlmsections section,
404 so that programs which understand it can resurrect the original
405 sections from the NLM. We will put a pointer to .nlmsections in
406 the NLM header area. */
407 secsec = bfd_make_section (outbfd, ".nlmsections");
409 bfd_fatal (_("make .nlmsections section"));
410 if (! bfd_set_section_flags (outbfd, secsec, SEC_HAS_CONTENTS))
411 bfd_fatal (_("set .nlmsections flags"));
413 #ifdef NLMCONV_POWERPC
414 /* For PowerPC NetWare we need to build stubs for calls to undefined
415 symbols. Because each stub requires an entry in the TOC section
416 which must be at the same location as other entries in the TOC
417 section, we must do this before determining where the TOC section
418 goes in setup_sections. */
419 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
420 powerpc_build_stubs (inbfd, outbfd, &symbols, &symcount);
423 /* Set up the sections. */
424 bfd_map_over_sections (inbfd, setup_sections, (void *) outbfd);
426 text_sec = bfd_get_section_by_name (outbfd, NLM_CODE_NAME);
428 /* The .bss section immediately follows the .data section. */
429 data_sec = bfd_get_section_by_name (outbfd, NLM_INITIALIZED_DATA_NAME);
430 if (data_sec != NULL)
434 vma = bfd_get_section_size (data_sec);
435 align = 1 << bss_sec->alignment_power;
436 add = ((vma + align - 1) &~ (align - 1)) - vma;
438 if (! bfd_set_section_vma (outbfd, bss_sec, vma))
439 bfd_fatal (_("set .bss vma"));
442 bfd_size_type data_size;
444 data_size = bfd_get_section_size (data_sec);
445 if (! bfd_set_section_size (outbfd, data_sec, data_size + add))
446 bfd_fatal (_("set .data size"));
450 /* Adjust symbol information. */
451 inlead = bfd_get_symbol_leading_char (inbfd);
452 outlead = bfd_get_symbol_leading_char (outbfd);
457 newsyms = (asymbol **) xmalloc (newsymalloc * sizeof (asymbol *));
460 for (i = 0; i < symcount; i++)
466 /* Add or remove a leading underscore. */
467 if (inlead != outlead)
471 if (bfd_asymbol_name (sym)[0] == inlead)
479 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 1);
481 strcpy (new + 1, bfd_asymbol_name (sym) + 1);
490 new = xmalloc (strlen (bfd_asymbol_name (sym)) + 2);
492 strcpy (new + 1, bfd_asymbol_name (sym));
497 /* NLM's have an uninitialized data section, but they do not
498 have a common section in the Unix sense. Move all common
499 symbols into the .bss section, and mark them as exported. */
500 if (bfd_is_com_section (bfd_get_section (sym)))
502 bfd_vma size = sym->value;
504 sym->section = bss_sec;
505 sym->value = bfd_get_section_size (bss_sec);
507 align = 1 << bss_sec->alignment_power;
508 size = (size + align - 1) & ~(align - 1);
509 bfd_set_section_size (outbfd, bss_sec, size);
510 sym->flags |= BSF_EXPORT | BSF_GLOBAL;
512 else if (bfd_get_section (sym)->output_section != NULL)
514 /* Move the symbol into the output section. */
515 sym->value += bfd_get_section (sym)->output_offset;
516 sym->section = bfd_get_section (sym)->output_section;
517 /* This is no longer a section symbol. */
518 sym->flags &=~ BSF_SECTION_SYM;
521 /* Force _edata and _end to be defined. This would normally be
522 done by the linker, but the manipulation of the common
523 symbols will confuse it. */
524 if ((sym->flags & BSF_DEBUGGING) == 0
525 && bfd_asymbol_name (sym)[0] == '_'
526 && bfd_is_und_section (bfd_get_section (sym)))
528 if (strcmp (bfd_asymbol_name (sym), "_edata") == 0)
530 sym->section = bss_sec;
533 if (strcmp (bfd_asymbol_name (sym), "_end") == 0)
535 sym->section = bss_sec;
539 #ifdef NLMCONV_POWERPC
540 /* For PowerPC NetWare, we define __GOT0. This is the start
541 of the .got section. */
542 if (bfd_get_arch (inbfd) == bfd_arch_powerpc
543 && strcmp (bfd_asymbol_name (sym), "__GOT0") == 0)
547 got_sec = bfd_get_section_by_name (inbfd, ".got");
548 assert (got_sec != (asection *) NULL);
549 sym->value = got_sec->output_offset;
550 sym->section = got_sec->output_section;
555 /* If this is a global symbol, check the export list. */
556 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) != 0)
558 struct string_list *l;
561 /* Unfortunately, a symbol can appear multiple times on the
562 export list, with and without prefixes. */
564 for (l = export_symbols; l != NULL; l = l->next)
566 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
572 zbase = strchr (l->string, '@');
574 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
576 /* We must add a symbol with this prefix. */
577 if (newsymcount >= newsymalloc)
580 newsyms = ((asymbol **)
581 xrealloc ((void *) newsyms,
583 * sizeof (asymbol *))));
585 newsyms[newsymcount] =
586 (asymbol *) xmalloc (sizeof (asymbol));
587 *newsyms[newsymcount] = *sym;
588 newsyms[newsymcount]->name = l->string;
595 /* The unmodified symbol is actually not exported at
597 sym->flags &=~ (BSF_GLOBAL | BSF_EXPORT);
598 sym->flags |= BSF_LOCAL;
602 /* If it's an undefined symbol, see if it's on the import list.
603 Change the prefix if necessary. */
604 if (bfd_is_und_section (bfd_get_section (sym)))
606 struct string_list *l;
608 for (l = import_symbols; l != NULL; l = l->next)
610 if (strcmp (l->string, bfd_asymbol_name (sym)) == 0)
616 zbase = strchr (l->string, '@');
618 && strcmp (zbase + 1, bfd_asymbol_name (sym)) == 0)
620 sym->name = l->string;
626 non_fatal (_("warning: symbol %s imported but not in import list"),
627 bfd_asymbol_name (sym));
630 /* See if it's one of the special named symbols. */
631 if ((sym->flags & BSF_DEBUGGING) == 0)
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 (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 non_fatal (_("warning: START procedure %s not defined"), start_procedure);
697 non_fatal (_("warning: EXIT procedure %s not defined"), exit_procedure);
698 if (check_procedure != NULL && ! gotcheck)
699 non_fatal (_("warning: CHECK procedure %s not defined"), check_procedure);
701 /* Add additional sections required for the header information. */
702 if (custom_file != NULL)
704 custom_data = fopen (custom_file, "r");
705 if (custom_data == NULL
706 || fstat (fileno (custom_data), &st) < 0)
708 fprintf (stderr, "%s:%s: %s\n", program_name, custom_file,
714 custom_size = st.st_size;
715 custom_section = bfd_make_section (outbfd, ".nlmcustom");
716 if (custom_section == NULL
717 || ! bfd_set_section_size (outbfd, custom_section, custom_size)
718 || ! bfd_set_section_flags (outbfd, custom_section,
720 bfd_fatal (_("custom section"));
723 if (help_file != NULL)
725 help_data = fopen (help_file, "r");
726 if (help_data == NULL
727 || fstat (fileno (help_data), &st) < 0)
729 fprintf (stderr, "%s:%s: %s\n", program_name, help_file,
735 help_size = st.st_size;
736 help_section = bfd_make_section (outbfd, ".nlmhelp");
737 if (help_section == NULL
738 || ! bfd_set_section_size (outbfd, help_section, help_size)
739 || ! bfd_set_section_flags (outbfd, help_section,
741 bfd_fatal (_("help section"));
742 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
745 if (message_file != NULL)
747 message_data = fopen (message_file, "r");
748 if (message_data == NULL
749 || fstat (fileno (message_data), &st) < 0)
751 fprintf (stderr, "%s:%s: %s\n", program_name, message_file,
757 message_size = st.st_size;
758 message_section = bfd_make_section (outbfd, ".nlmmessages");
759 if (message_section == NULL
760 || ! bfd_set_section_size (outbfd, message_section, message_size)
761 || ! bfd_set_section_flags (outbfd, message_section,
763 bfd_fatal (_("message section"));
764 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
769 struct string_list *l;
772 for (l = modules; l != NULL; l = l->next)
773 module_size += strlen (l->string) + 1;
774 module_section = bfd_make_section (outbfd, ".nlmmodules");
775 if (module_section == NULL
776 || ! bfd_set_section_size (outbfd, module_section, module_size)
777 || ! bfd_set_section_flags (outbfd, module_section,
779 bfd_fatal (_("module section"));
781 if (rpc_file != NULL)
783 rpc_data = fopen (rpc_file, "r");
785 || fstat (fileno (rpc_data), &st) < 0)
787 fprintf (stderr, "%s:%s: %s\n", program_name, rpc_file,
793 rpc_size = st.st_size;
794 rpc_section = bfd_make_section (outbfd, ".nlmrpc");
795 if (rpc_section == NULL
796 || ! bfd_set_section_size (outbfd, rpc_section, rpc_size)
797 || ! bfd_set_section_flags (outbfd, rpc_section,
799 bfd_fatal (_("rpc section"));
800 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
803 if (sharelib_file != NULL)
805 sharedbfd = bfd_openr (sharelib_file, output_format);
806 if (sharedbfd == NULL
807 || ! bfd_check_format (sharedbfd, bfd_object))
809 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
810 bfd_errmsg (bfd_get_error ()));
811 sharelib_file = NULL;
815 sharedhdr = *nlm_fixed_header (sharedbfd);
816 bfd_close (sharedbfd);
817 shared_data = fopen (sharelib_file, "r");
818 if (shared_data == NULL
819 || (fstat (fileno (shared_data), &st) < 0))
821 fprintf (stderr, "%s:%s: %s\n", program_name, sharelib_file,
823 sharelib_file = NULL;
827 /* If we were clever, we could just copy out the
828 sections of the shared library which we actually
829 need. However, we would have to figure out the sizes
830 of the external and public information, and that can
831 not be done without reading through them. */
832 if (sharedhdr.uninitializedDataSize > 0)
834 /* There is no place to record this information. */
835 non_fatal (_("%s: warning: shared libraries can not have uninitialized data"),
838 shared_offset = st.st_size;
839 if (shared_offset > (size_t) sharedhdr.codeImageOffset)
840 shared_offset = sharedhdr.codeImageOffset;
841 if (shared_offset > (size_t) sharedhdr.dataImageOffset)
842 shared_offset = sharedhdr.dataImageOffset;
843 if (shared_offset > (size_t) sharedhdr.relocationFixupOffset)
844 shared_offset = sharedhdr.relocationFixupOffset;
845 if (shared_offset > (size_t) sharedhdr.externalReferencesOffset)
846 shared_offset = sharedhdr.externalReferencesOffset;
847 if (shared_offset > (size_t) sharedhdr.publicsOffset)
848 shared_offset = sharedhdr.publicsOffset;
849 shared_size = st.st_size - shared_offset;
850 shared_section = bfd_make_section (outbfd, ".nlmshared");
851 if (shared_section == NULL
852 || ! bfd_set_section_size (outbfd, shared_section,
854 || ! bfd_set_section_flags (outbfd, shared_section,
856 bfd_fatal (_("shared section"));
857 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
862 /* Check whether a version was given. */
863 if (strncmp (version_hdr->stamp, "VeRsIoN#", 8) != 0)
864 non_fatal (_("warning: No version number given"));
866 /* At least for now, always create an extended header, because that
867 is what NLMLINK does. */
868 strncpy (nlm_extended_header (outbfd)->stamp, "MeSsAgEs", 8);
870 strncpy (nlm_cygnus_ext_header (outbfd)->stamp, "CyGnUsEx", 8);
872 /* If the date was not given, force it in. */
873 if (nlm_version_header (outbfd)->month == 0
874 && nlm_version_header (outbfd)->day == 0
875 && nlm_version_header (outbfd)->year == 0)
881 ptm = localtime (&now);
882 nlm_version_header (outbfd)->month = ptm->tm_mon + 1;
883 nlm_version_header (outbfd)->day = ptm->tm_mday;
884 nlm_version_header (outbfd)->year = ptm->tm_year + 1900;
885 strncpy (version_hdr->stamp, "VeRsIoN#", 8);
888 #ifdef NLMCONV_POWERPC
889 /* Resolve the stubs we build for PowerPC NetWare. */
890 if (bfd_get_arch (inbfd) == bfd_arch_powerpc)
891 powerpc_resolve_stubs (inbfd, outbfd);
894 /* Copy over the sections. */
895 bfd_map_over_sections (inbfd, copy_sections, (void *) outbfd);
897 /* Finish up the header information. */
898 if (custom_file != NULL)
902 data = xmalloc (custom_size);
903 if (fread (data, 1, custom_size, custom_data) != custom_size)
904 non_fatal (_("%s: read: %s"), custom_file, strerror (errno));
907 if (! bfd_set_section_contents (outbfd, custom_section, data,
908 (file_ptr) 0, custom_size))
909 bfd_fatal (_("custom section"));
910 nlm_fixed_header (outbfd)->customDataOffset =
911 custom_section->filepos;
912 nlm_fixed_header (outbfd)->customDataSize = custom_size;
918 /* As a special hack, the backend recognizes a debugInfoOffset
919 of -1 to mean that it should not output any debugging
920 information. This can not be handling by fiddling with the
921 symbol table because exported symbols appear in both the
922 export information and the debugging information. */
923 nlm_fixed_header (outbfd)->debugInfoOffset = (file_ptr) -1;
926 non_fatal (_("warning: FULLMAP is not supported; try ld -M"));
927 if (help_file != NULL)
931 data = xmalloc (help_size);
932 if (fread (data, 1, help_size, help_data) != help_size)
933 non_fatal (_("%s: read: %s"), help_file, strerror (errno));
936 if (! bfd_set_section_contents (outbfd, help_section, data,
937 (file_ptr) 0, help_size))
938 bfd_fatal (_("help section"));
939 nlm_extended_header (outbfd)->helpFileOffset =
940 help_section->filepos;
941 nlm_extended_header (outbfd)->helpFileLength = help_size;
945 if (message_file != NULL)
949 data = xmalloc (message_size);
950 if (fread (data, 1, message_size, message_data) != message_size)
951 non_fatal (_("%s: read: %s"), message_file, strerror (errno));
954 if (! bfd_set_section_contents (outbfd, message_section, data,
955 (file_ptr) 0, message_size))
956 bfd_fatal (_("message section"));
957 nlm_extended_header (outbfd)->messageFileOffset =
958 message_section->filepos;
959 nlm_extended_header (outbfd)->messageFileLength = message_size;
961 /* FIXME: Are these offsets correct on all platforms? Are
962 they 32 bits on all platforms? What endianness? */
963 nlm_extended_header (outbfd)->languageID =
964 bfd_h_get_32 (outbfd, (bfd_byte *) data + 106);
965 nlm_extended_header (outbfd)->messageCount =
966 bfd_h_get_32 (outbfd, (bfd_byte *) data + 110);
974 struct string_list *l;
977 data = xmalloc (module_size);
979 set = (unsigned char *) data;
980 for (l = modules; l != NULL; l = l->next)
982 *set = strlen (l->string);
983 strncpy (set + 1, l->string, *set);
987 if (! bfd_set_section_contents (outbfd, module_section, data,
988 (file_ptr) 0, module_size))
989 bfd_fatal (_("module section"));
990 nlm_fixed_header (outbfd)->moduleDependencyOffset =
991 module_section->filepos;
992 nlm_fixed_header (outbfd)->numberOfModuleDependencies = c;
994 if (rpc_file != NULL)
998 data = xmalloc (rpc_size);
999 if (fread (data, 1, rpc_size, rpc_data) != rpc_size)
1000 non_fatal (_("%s: read: %s"), rpc_file, strerror (errno));
1003 if (! bfd_set_section_contents (outbfd, rpc_section, data,
1004 (file_ptr) 0, rpc_size))
1005 bfd_fatal (_("rpc section"));
1006 nlm_extended_header (outbfd)->RPCDataOffset =
1007 rpc_section->filepos;
1008 nlm_extended_header (outbfd)->RPCDataLength = rpc_size;
1012 if (sharelib_file != NULL)
1016 data = xmalloc (shared_size);
1017 if (fseek (shared_data, shared_offset, SEEK_SET) != 0
1018 || fread (data, 1, shared_size, shared_data) != shared_size)
1019 non_fatal (_("%s: read: %s"), sharelib_file, strerror (errno));
1022 if (! bfd_set_section_contents (outbfd, shared_section, data,
1023 (file_ptr) 0, shared_size))
1024 bfd_fatal (_("shared section"));
1026 nlm_extended_header (outbfd)->sharedCodeOffset =
1027 sharedhdr.codeImageOffset - shared_offset + shared_section->filepos;
1028 nlm_extended_header (outbfd)->sharedCodeLength =
1029 sharedhdr.codeImageSize;
1030 nlm_extended_header (outbfd)->sharedDataOffset =
1031 sharedhdr.dataImageOffset - shared_offset + shared_section->filepos;
1032 nlm_extended_header (outbfd)->sharedDataLength =
1033 sharedhdr.dataImageSize;
1034 nlm_extended_header (outbfd)->sharedRelocationFixupOffset =
1035 (sharedhdr.relocationFixupOffset
1037 + shared_section->filepos);
1038 nlm_extended_header (outbfd)->sharedRelocationFixupCount =
1039 sharedhdr.numberOfRelocationFixups;
1040 nlm_extended_header (outbfd)->sharedExternalReferenceOffset =
1041 (sharedhdr.externalReferencesOffset
1043 + shared_section->filepos);
1044 nlm_extended_header (outbfd)->sharedExternalReferenceCount =
1045 sharedhdr.numberOfExternalReferences;
1046 nlm_extended_header (outbfd)->sharedPublicsOffset =
1047 sharedhdr.publicsOffset - shared_offset + shared_section->filepos;
1048 nlm_extended_header (outbfd)->sharedPublicsCount =
1049 sharedhdr.numberOfPublics;
1050 nlm_extended_header (outbfd)->sharedDebugRecordOffset =
1051 sharedhdr.debugInfoOffset - shared_offset + shared_section->filepos;
1052 nlm_extended_header (outbfd)->sharedDebugRecordCount =
1053 sharedhdr.numberOfDebugRecords;
1054 nlm_extended_header (outbfd)->SharedInitializationOffset =
1055 sharedhdr.codeStartOffset;
1056 nlm_extended_header (outbfd)->SharedExitProcedureOffset =
1057 sharedhdr.exitProcedureOffset;
1060 len = strlen (output_file);
1061 if (len > NLM_MODULE_NAME_SIZE - 2)
1062 len = NLM_MODULE_NAME_SIZE - 2;
1063 nlm_fixed_header (outbfd)->moduleName[0] = len;
1065 strncpy (nlm_fixed_header (outbfd)->moduleName + 1, output_file,
1066 NLM_MODULE_NAME_SIZE - 2);
1067 nlm_fixed_header (outbfd)->moduleName[NLM_MODULE_NAME_SIZE - 1] = '\0';
1068 for (modname = nlm_fixed_header (outbfd)->moduleName;
1071 *modname = TOUPPER (*modname);
1073 strncpy (nlm_variable_header (outbfd)->oldThreadName, " LONG",
1074 NLM_OLD_THREAD_NAME_LENGTH);
1076 nlm_cygnus_ext_header (outbfd)->offset = secsec->filepos;
1077 nlm_cygnus_ext_header (outbfd)->length = bfd_section_size (outbfd, secsec);
1079 if (! bfd_close (outbfd))
1080 bfd_fatal (output_file);
1081 if (! bfd_close (inbfd))
1082 bfd_fatal (input_file);
1084 if (unlink_on_exit != NULL)
1085 unlink (unlink_on_exit);
1091 /* Show a usage message and exit. */
1094 show_usage (FILE *file, int status)
1096 fprintf (file, _("Usage: %s [option(s)] [in-file [out-file]]\n"), program_name);
1097 fprintf (file, _(" Convert an object file into a NetWare Loadable Module\n"));
1098 fprintf (file, _(" The options are:\n\
1099 -I --input-target=<bfdname> Set the input binary file format\n\
1100 -O --output-target=<bfdname> Set the output binary file format\n\
1101 -T --header-file=<file> Read <file> for NLM header information\n\
1102 -l --linker=<linker> Use <linker> for any linking\n\
1103 -d --debug Display on stderr the linker command line\n\
1104 -h --help Display this information\n\
1105 -v --version Display the program's version\n\
1108 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1112 /* Select the output format based on the input architecture, machine,
1113 and endianness. This chooses the appropriate NLM target. */
1116 select_output_format (enum bfd_architecture arch, unsigned long mach,
1117 bfd_boolean bigendian ATTRIBUTE_UNUSED)
1123 return "nlm32-i386";
1125 #ifdef NLMCONV_SPARC
1126 case bfd_arch_sparc:
1127 return "nlm32-sparc";
1129 #ifdef NLMCONV_ALPHA
1130 case bfd_arch_alpha:
1131 return "nlm32-alpha";
1133 #ifdef NLMCONV_POWERPC
1134 case bfd_arch_powerpc:
1135 return "nlm32-powerpc";
1138 fatal (_("support not compiled in for %s"),
1139 bfd_printable_arch_mach (arch, mach));
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 (bfd *inbfd ATTRIBUTE_UNUSED, asection *insec, void *data_ptr)
1151 bfd *outbfd = (bfd *) data_ptr;
1153 const char *outname;
1156 bfd_size_type align;
1158 bfd_size_type secsecsize;
1160 f = bfd_get_section_flags (inbfd, insec);
1162 outname = NLM_CODE_NAME;
1163 else if ((f & SEC_LOAD) && (f & SEC_HAS_CONTENTS))
1164 outname = NLM_INITIALIZED_DATA_NAME;
1165 else if (f & SEC_ALLOC)
1166 outname = NLM_UNINITIALIZED_DATA_NAME;
1168 outname = bfd_section_name (inbfd, insec);
1170 outsec = bfd_get_section_by_name (outbfd, outname);
1173 outsec = bfd_make_section (outbfd, outname);
1175 bfd_fatal (_("make section"));
1178 insec->output_section = outsec;
1180 offset = bfd_section_size (outbfd, outsec);
1181 align = 1 << bfd_section_alignment (inbfd, insec);
1182 add = ((offset + align - 1) &~ (align - 1)) - offset;
1183 insec->output_offset = offset + add;
1185 if (! bfd_set_section_size (outbfd, outsec,
1186 (bfd_section_size (outbfd, outsec)
1187 + bfd_section_size (inbfd, insec)
1189 bfd_fatal (_("set section size"));
1191 if ((bfd_section_alignment (inbfd, insec)
1192 > bfd_section_alignment (outbfd, outsec))
1193 && ! bfd_set_section_alignment (outbfd, outsec,
1194 bfd_section_alignment (inbfd, insec)))
1195 bfd_fatal (_("set section alignment"));
1197 if (! bfd_set_section_flags (outbfd, outsec,
1198 f | bfd_get_section_flags (outbfd, outsec)))
1199 bfd_fatal (_("set section flags"));
1201 bfd_set_reloc (outbfd, outsec, (arelent **) NULL, 0);
1203 /* For each input section we allocate space for an entry in
1205 secsecsize = bfd_section_size (outbfd, secsec);
1206 secsecsize += strlen (bfd_section_name (inbfd, insec)) + 1;
1207 secsecsize = (secsecsize + 3) &~ 3;
1209 if (! bfd_set_section_size (outbfd, secsec, secsecsize))
1210 bfd_fatal (_("set .nlmsections size"));
1213 /* Copy the section contents. */
1216 copy_sections (bfd *inbfd, asection *insec, void *data_ptr)
1218 static bfd_size_type secsecoff = 0;
1219 bfd *outbfd = (bfd *) data_ptr;
1228 inname = bfd_section_name (inbfd, insec);
1230 outsec = insec->output_section;
1231 assert (outsec != NULL);
1233 size = bfd_get_section_size (insec);
1235 if ((bfd_get_section_flags (inbfd, insec) & SEC_HAS_CONTENTS) == 0)
1239 contents = xmalloc (size);
1240 if (! bfd_get_section_contents (inbfd, insec, contents,
1241 (file_ptr) 0, size))
1242 bfd_fatal (bfd_get_filename (inbfd));
1245 reloc_size = bfd_get_reloc_upper_bound (inbfd, insec);
1247 bfd_fatal (bfd_get_filename (inbfd));
1248 if (reloc_size != 0)
1253 relocs = (arelent **) xmalloc (reloc_size);
1254 reloc_count = bfd_canonicalize_reloc (inbfd, insec, relocs, symbols);
1255 if (reloc_count < 0)
1256 bfd_fatal (bfd_get_filename (inbfd));
1257 mangle_relocs (outbfd, insec, &relocs, &reloc_count, (char *) contents,
1260 /* FIXME: refers to internal BFD fields. */
1261 if (outsec->orelocation != (arelent **) NULL)
1263 bfd_size_type total_count;
1266 total_count = reloc_count + outsec->reloc_count;
1267 combined = (arelent **) xmalloc (total_count * sizeof (arelent *));
1268 memcpy (combined, outsec->orelocation,
1269 outsec->reloc_count * sizeof (arelent *));
1270 memcpy (combined + outsec->reloc_count, relocs,
1271 (size_t) (reloc_count * sizeof (arelent *)));
1272 free (outsec->orelocation);
1273 reloc_count = total_count;
1277 bfd_set_reloc (outbfd, outsec, relocs, reloc_count);
1280 if (contents != NULL)
1282 if (! bfd_set_section_contents (outbfd, outsec, contents,
1283 insec->output_offset, size))
1284 bfd_fatal (bfd_get_filename (outbfd));
1288 /* Add this section to .nlmsections. */
1289 if (! bfd_set_section_contents (outbfd, secsec, (void *) inname, secsecoff,
1290 strlen (inname) + 1))
1291 bfd_fatal (_("set .nlmsection contents"));
1292 secsecoff += strlen (inname) + 1;
1294 add = ((secsecoff + 3) &~ 3) - secsecoff;
1297 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1298 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, add))
1299 bfd_fatal (_("set .nlmsection contents"));
1303 if (contents != NULL)
1304 bfd_h_put_32 (outbfd, (bfd_vma) outsec->filepos, buf);
1306 bfd_h_put_32 (outbfd, (bfd_vma) 0, buf);
1307 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1308 bfd_fatal (_("set .nlmsection contents"));
1311 bfd_h_put_32 (outbfd, (bfd_vma) size, buf);
1312 if (! bfd_set_section_contents (outbfd, secsec, buf, secsecoff, 4))
1313 bfd_fatal (_("set .nlmsection contents"));
1317 /* Some, perhaps all, NetWare targets require changing the relocs used
1318 by the input formats. */
1321 mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
1322 long *reloc_count_ptr, char *contents,
1323 bfd_size_type contents_size)
1325 switch (bfd_get_arch (outbfd))
1329 i386_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1330 contents, contents_size);
1333 #ifdef NLMCONV_ALPHA
1334 case bfd_arch_alpha:
1335 alpha_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1336 contents, contents_size);
1339 #ifdef NLMCONV_POWERPC
1340 case bfd_arch_powerpc:
1341 powerpc_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1342 contents, contents_size);
1346 default_mangle_relocs (outbfd, insec, relocs_ptr, reloc_count_ptr,
1347 contents, contents_size);
1352 /* By default all we need to do for relocs is change the address by
1353 the output_offset. */
1356 default_mangle_relocs (bfd *outbfd ATTRIBUTE_UNUSED, asection *insec,
1357 arelent ***relocs_ptr, long *reloc_count_ptr,
1358 char *contents ATTRIBUTE_UNUSED,
1359 bfd_size_type contents_size ATTRIBUTE_UNUSED)
1361 if (insec->output_offset != 0)
1367 reloc_count = *reloc_count_ptr;
1368 relocs = *relocs_ptr;
1369 for (i = 0; i < reloc_count; i++, relocs++)
1370 (*relocs)->address += insec->output_offset;
1376 /* NetWare on the i386 supports a restricted set of relocs, which are
1377 different from those used on other i386 targets. This routine
1378 converts the relocs. It is, obviously, very target dependent. At
1379 the moment, the nlm32-i386 backend performs similar translations;
1380 however, it is more reliable and efficient to do them here. */
1382 static reloc_howto_type nlm_i386_pcrel_howto =
1383 HOWTO (1, /* type */
1385 2, /* size (0 = byte, 1 = short, 2 = long) */
1387 TRUE, /* pc_relative */
1389 complain_overflow_signed, /* complain_on_overflow */
1390 0, /* special_function */
1391 "DISP32", /* name */
1392 TRUE, /* partial_inplace */
1393 0xffffffff, /* src_mask */
1394 0xffffffff, /* dst_mask */
1395 TRUE); /* pcrel_offset */
1398 i386_mangle_relocs (bfd *outbfd, asection *insec, arelent ***relocs_ptr,
1399 long *reloc_count_ptr, char *contents,
1400 bfd_size_type contents_size)
1402 long reloc_count, i;
1405 reloc_count = *reloc_count_ptr;
1406 relocs = *relocs_ptr;
1407 for (i = 0; i < reloc_count; i++)
1411 bfd_size_type address;
1415 sym = *rel->sym_ptr_ptr;
1417 /* We're moving the relocs from the input section to the output
1418 section, so we must adjust the address accordingly. */
1419 address = rel->address;
1420 rel->address += insec->output_offset;
1422 /* Note that no serious harm will ensue if we fail to change a
1423 reloc. The backend will fail when writing out the reloc. */
1425 /* Make sure this reloc is within the data we have. We use only
1426 4 byte relocs here, so we insist on having 4 bytes. */
1427 if (address + 4 > contents_size)
1430 /* A PC relative reloc entirely within a single section is
1431 completely unnecessary. This can be generated by ld -r. */
1432 if (sym == insec->symbol
1433 && rel->howto != NULL
1434 && rel->howto->pc_relative
1435 && ! rel->howto->pcrel_offset)
1439 memmove (relocs, relocs + 1,
1440 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1444 /* Get the amount the relocation will add in. */
1445 addend = rel->addend + sym->value;
1447 /* NetWare doesn't support PC relative relocs against defined
1448 symbols, so we have to eliminate them by doing the relocation
1449 now. We can only do this if the reloc is within a single
1451 if (rel->howto != NULL
1452 && rel->howto->pc_relative
1453 && bfd_get_section (sym) == insec->output_section)
1457 if (rel->howto->pcrel_offset)
1460 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1462 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1466 memmove (relocs, relocs + 1,
1467 (size_t) ((reloc_count - i) * sizeof (arelent *)));
1471 /* NetWare doesn't support reloc addends, so we get rid of them
1472 here by simply adding them into the object data. We handle
1473 the symbol value, if any, the same way. */
1475 && rel->howto != NULL
1476 && rel->howto->rightshift == 0
1477 && rel->howto->size == 2
1478 && rel->howto->bitsize == 32
1479 && rel->howto->bitpos == 0
1480 && rel->howto->src_mask == 0xffffffff
1481 && rel->howto->dst_mask == 0xffffffff)
1485 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1487 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1489 /* Adjust the reloc for the changes we just made. */
1491 if (! bfd_is_und_section (bfd_get_section (sym)))
1492 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
1495 /* NetWare uses a reloc with pcrel_offset set. We adjust
1496 pc_relative relocs accordingly. We are going to change the
1497 howto field, so we can only do this if the current one is
1498 compatible. We should check that special_function is NULL
1499 here, but at the moment coff-i386 uses a special_function
1500 which does not affect what we are doing here. */
1501 if (rel->howto != NULL
1502 && rel->howto->pc_relative
1503 && ! rel->howto->pcrel_offset
1504 && rel->howto->rightshift == 0
1505 && rel->howto->size == 2
1506 && rel->howto->bitsize == 32
1507 && rel->howto->bitpos == 0
1508 && rel->howto->src_mask == 0xffffffff
1509 && rel->howto->dst_mask == 0xffffffff)
1513 /* When pcrel_offset is not set, it means that the negative
1514 of the address of the memory location is stored in the
1515 memory location. We must add it back in. */
1516 val = bfd_get_32 (outbfd, (bfd_byte *) contents + address);
1518 bfd_put_32 (outbfd, val, (bfd_byte *) contents + address);
1520 /* We must change to a new howto. */
1521 rel->howto = &nlm_i386_pcrel_howto;
1526 #endif /* NLMCONV_I386 */
1528 #ifdef NLMCONV_ALPHA
1530 /* On the Alpha the first reloc for every section must be a special
1531 relocs which hold the GP address. Also, the first reloc in the
1532 file must be a special reloc which holds the address of the .lita
1535 static reloc_howto_type nlm32_alpha_nw_howto =
1536 HOWTO (ALPHA_R_NW_RELOC, /* type */
1538 0, /* size (0 = byte, 1 = short, 2 = long) */
1540 FALSE, /* pc_relative */
1542 complain_overflow_dont, /* complain_on_overflow */
1543 0, /* special_function */
1544 "NW_RELOC", /* name */
1545 FALSE, /* partial_inplace */
1548 FALSE); /* pcrel_offset */
1551 alpha_mangle_relocs (bfd *outbfd, asection *insec,
1552 arelent ***relocs_ptr, long *reloc_count_ptr,
1553 char *contents ATTRIBUTE_UNUSED,
1554 bfd_size_type contents_size ATTRIBUTE_UNUSED)
1556 long old_reloc_count;
1557 arelent **old_relocs;
1560 old_reloc_count = *reloc_count_ptr;
1561 old_relocs = *relocs_ptr;
1562 relocs = (arelent **) xmalloc ((old_reloc_count + 3) * sizeof (arelent *));
1563 *relocs_ptr = relocs;
1565 if (nlm_alpha_backend_data (outbfd)->lita_address == 0)
1568 asection *lita_section;
1570 inbfd = insec->owner;
1571 lita_section = bfd_get_section_by_name (inbfd, _LITA);
1572 if (lita_section != (asection *) NULL)
1574 nlm_alpha_backend_data (outbfd)->lita_address =
1575 bfd_get_section_vma (inbfd, lita_section);
1576 nlm_alpha_backend_data (outbfd)->lita_size =
1577 bfd_section_size (inbfd, lita_section);
1581 /* Avoid outputting this reloc again. */
1582 nlm_alpha_backend_data (outbfd)->lita_address = 4;
1585 *relocs = (arelent *) xmalloc (sizeof (arelent));
1586 (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1587 (*relocs)->address = nlm_alpha_backend_data (outbfd)->lita_address;
1588 (*relocs)->addend = nlm_alpha_backend_data (outbfd)->lita_size + 1;
1589 (*relocs)->howto = &nlm32_alpha_nw_howto;
1591 ++(*reloc_count_ptr);
1594 /* Get the GP value from bfd. */
1595 if (nlm_alpha_backend_data (outbfd)->gp == 0)
1596 nlm_alpha_backend_data (outbfd)->gp =
1597 bfd_ecoff_get_gp_value (insec->owner);
1599 *relocs = (arelent *) xmalloc (sizeof (arelent));
1600 (*relocs)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1601 (*relocs)->address = nlm_alpha_backend_data (outbfd)->gp;
1602 (*relocs)->addend = 0;
1603 (*relocs)->howto = &nlm32_alpha_nw_howto;
1605 ++(*reloc_count_ptr);
1607 memcpy (relocs, old_relocs, (size_t) old_reloc_count * sizeof (arelent *));
1608 relocs[old_reloc_count] = (arelent *) NULL;
1612 if (insec->output_offset != 0)
1616 for (i = 0; i < (bfd_size_type) old_reloc_count; i++, relocs++)
1617 (*relocs)->address += insec->output_offset;
1621 #endif /* NLMCONV_ALPHA */
1623 #ifdef NLMCONV_POWERPC
1625 /* We keep a linked list of stubs which we must build. Because BFD
1626 requires us to know the sizes of all sections before we can set the
1627 contents of any, we must figure out which stubs we want to build
1628 before we can actually build any of them. */
1632 /* Next stub in linked list. */
1633 struct powerpc_stub *next;
1635 /* Symbol whose value is the start of the stub. This is a symbol
1636 whose name begins with `.'. */
1639 /* Symbol we are going to create a reloc against. This is a symbol
1640 with the same name as START but without the leading `.'. */
1643 /* The TOC index for this stub. This is the index into the TOC
1644 section at which the reloc is created. */
1645 unsigned int toc_index;
1648 /* The linked list of stubs. */
1650 static struct powerpc_stub *powerpc_stubs;
1652 /* This is what a stub looks like. The first instruction will get
1653 adjusted with the correct TOC index. */
1655 static unsigned long powerpc_stub_insns[] =
1657 0x81820000, /* lwz r12,0(r2) */
1658 0x90410014, /* stw r2,20(r1) */
1659 0x800c0000, /* lwz r0,0(r12) */
1660 0x804c0004, /* lwz r2,r(r12) */
1661 0x7c0903a6, /* mtctr r0 */
1662 0x4e800420, /* bctr */
1663 0, /* Traceback table. */
1668 #define POWERPC_STUB_INSN_COUNT \
1669 (sizeof powerpc_stub_insns / sizeof powerpc_stub_insns[0])
1671 #define POWERPC_STUB_SIZE (4 * POWERPC_STUB_INSN_COUNT)
1673 /* Each stub uses a four byte TOC entry. */
1674 #define POWERPC_STUB_TOC_ENTRY_SIZE (4)
1676 /* The original size of the .got section. */
1677 static bfd_size_type powerpc_initial_got_size;
1679 /* Look for all undefined symbols beginning with `.', and prepare to
1680 build a stub for each one. */
1683 powerpc_build_stubs (bfd *inbfd, bfd *outbfd ATTRIBUTE_UNUSED,
1684 asymbol ***symbols_ptr, long *symcount_ptr)
1688 unsigned int got_base;
1693 /* Make a section to hold stubs. We don't set SEC_HAS_CONTENTS for
1694 the section to prevent copy_sections from reading from it. */
1695 stub_sec = bfd_make_section (inbfd, ".stubs");
1696 if (stub_sec == (asection *) NULL
1697 || ! bfd_set_section_flags (inbfd, stub_sec,
1702 || ! bfd_set_section_alignment (inbfd, stub_sec, 2))
1703 bfd_fatal (".stubs");
1705 /* Get the TOC section, which is named .got. */
1706 got_sec = bfd_get_section_by_name (inbfd, ".got");
1707 if (got_sec == (asection *) NULL)
1709 got_sec = bfd_make_section (inbfd, ".got");
1710 if (got_sec == (asection *) NULL
1711 || ! bfd_set_section_flags (inbfd, got_sec,
1716 | SEC_HAS_CONTENTS))
1717 || ! bfd_set_section_alignment (inbfd, got_sec, 2))
1721 powerpc_initial_got_size = bfd_section_size (inbfd, got_sec);
1722 got_base = powerpc_initial_got_size;
1723 got_base = (got_base + 3) &~ 3;
1727 symcount = *symcount_ptr;
1728 for (i = 0; i < symcount; i++)
1733 struct powerpc_stub *item;
1735 sym = (*symbols_ptr)[i];
1737 /* We must make a stub for every undefined symbol whose name
1739 if (bfd_asymbol_name (sym)[0] != '.'
1740 || ! bfd_is_und_section (bfd_get_section (sym)))
1743 /* Make a new undefined symbol with the same name but without
1745 newsym = (asymbol *) xmalloc (sizeof (asymbol));
1747 newname = (char *) xmalloc (strlen (bfd_asymbol_name (sym)));
1748 strcpy (newname, bfd_asymbol_name (sym) + 1);
1749 newsym->name = newname;
1751 /* Define the `.' symbol to be in the stub section. */
1752 sym->section = stub_sec;
1753 sym->value = stubcount * POWERPC_STUB_SIZE;
1754 /* We set the BSF_DYNAMIC flag here so that we can check it when
1755 we are mangling relocs. FIXME: This is a hack. */
1756 sym->flags = BSF_LOCAL | BSF_DYNAMIC;
1758 /* Add this stub to the linked list. */
1759 item = (struct powerpc_stub *) xmalloc (sizeof (struct powerpc_stub));
1761 item->reloc = newsym;
1762 item->toc_index = got_base + stubcount * POWERPC_STUB_TOC_ENTRY_SIZE;
1764 item->next = powerpc_stubs;
1765 powerpc_stubs = item;
1773 struct powerpc_stub *l;
1775 /* Add the new symbols we just created to the symbol table. */
1776 *symbols_ptr = (asymbol **) xrealloc ((char *) *symbols_ptr,
1777 ((symcount + stubcount)
1778 * sizeof (asymbol)));
1779 *symcount_ptr += stubcount;
1780 s = &(*symbols_ptr)[symcount];
1781 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1784 /* Set the size of the .stubs section and increase the size of
1785 the .got section. */
1786 if (! bfd_set_section_size (inbfd, stub_sec,
1787 stubcount * POWERPC_STUB_SIZE)
1788 || ! bfd_set_section_size (inbfd, got_sec,
1791 * POWERPC_STUB_TOC_ENTRY_SIZE))))
1792 bfd_fatal (_("stub section sizes"));
1796 /* Resolve all the stubs for PowerPC NetWare. We fill in the contents
1797 of the output section, and create new relocs in the TOC. */
1800 powerpc_resolve_stubs (bfd *inbfd, bfd *outbfd)
1802 bfd_byte buf[POWERPC_STUB_SIZE];
1804 unsigned int stubcount;
1808 struct powerpc_stub *l;
1810 if (powerpc_stubs == (struct powerpc_stub *) NULL)
1813 for (i = 0; i < POWERPC_STUB_INSN_COUNT; i++)
1814 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[i], buf + i * 4);
1816 got_sec = bfd_get_section_by_name (inbfd, ".got");
1817 assert (got_sec != (asection *) NULL);
1818 assert (got_sec->output_section->orelocation == (arelent **) NULL);
1821 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1823 relocs = (arelent **) xmalloc (stubcount * sizeof (arelent *));
1826 for (l = powerpc_stubs; l != (struct powerpc_stub *) NULL; l = l->next)
1830 /* Adjust the first instruction to use the right TOC index. */
1831 bfd_put_32 (outbfd, (bfd_vma) powerpc_stub_insns[0] + l->toc_index, buf);
1833 /* Write this stub out. */
1834 if (! bfd_set_section_contents (outbfd,
1835 bfd_get_section (l->start),
1839 bfd_fatal (_("writing stub"));
1841 /* Create a new reloc for the TOC entry. */
1842 reloc = (arelent *) xmalloc (sizeof (arelent));
1843 reloc->sym_ptr_ptr = &l->reloc;
1844 reloc->address = l->toc_index + got_sec->output_offset;
1846 reloc->howto = bfd_reloc_type_lookup (inbfd, BFD_RELOC_32);
1851 bfd_set_reloc (outbfd, got_sec->output_section, relocs, stubcount);
1854 /* Adjust relocation entries for PowerPC NetWare. We do not output
1855 TOC relocations. The object code already contains the offset from
1856 the TOC pointer. When the function is called, the TOC register,
1857 r2, will be set to the correct TOC value, so there is no need for
1858 any further reloc. */
1861 powerpc_mangle_relocs (bfd *outbfd, asection *insec,
1862 arelent ***relocs_ptr,
1863 long *reloc_count_ptr, char *contents,
1864 bfd_size_type contents_size ATTRIBUTE_UNUSED)
1866 reloc_howto_type *toc_howto;
1871 toc_howto = bfd_reloc_type_lookup (insec->owner, BFD_RELOC_PPC_TOC16);
1872 if (toc_howto == (reloc_howto_type *) NULL)
1875 /* If this is the .got section, clear out all the contents beyond
1876 the initial size. We must do this here because copy_sections is
1877 going to write out whatever we return in the contents field. */
1878 if (strcmp (bfd_get_section_name (insec->owner, insec), ".got") == 0)
1879 memset (contents + powerpc_initial_got_size, 0,
1880 (size_t) (bfd_get_section_size (insec) - powerpc_initial_got_size));
1882 reloc_count = *reloc_count_ptr;
1883 relocs = *relocs_ptr;
1884 for (i = 0; i < reloc_count; i++)
1891 sym = *rel->sym_ptr_ptr;
1893 /* Convert any relocs against the .bss section into relocs
1894 against the .data section. */
1895 if (strcmp (bfd_get_section_name (outbfd, bfd_get_section (sym)),
1896 NLM_UNINITIALIZED_DATA_NAME) == 0)
1900 datasec = bfd_get_section_by_name (outbfd,
1901 NLM_INITIALIZED_DATA_NAME);
1902 if (datasec != NULL)
1904 rel->addend += (bfd_get_section_vma (outbfd,
1905 bfd_get_section (sym))
1907 rel->sym_ptr_ptr = datasec->symbol_ptr_ptr;
1908 sym = *rel->sym_ptr_ptr;
1912 /* We must be able to resolve all PC relative relocs at this
1913 point. If we get a branch to an undefined symbol we build a
1914 stub, since NetWare will resolve undefined symbols into a
1915 pointer to a function descriptor. */
1916 if (rel->howto->pc_relative)
1918 /* This check for whether a symbol is in the same section as
1919 the reloc will be wrong if there is a PC relative reloc
1920 between two sections both of which were placed in the
1921 same output section. This should not happen. */
1922 if (bfd_get_section (sym) != insec->output_section)
1923 non_fatal (_("unresolved PC relative reloc against %s"),
1924 bfd_asymbol_name (sym));
1929 assert (rel->howto->size == 2 && rel->howto->pcrel_offset);
1930 val = bfd_get_32 (outbfd, (bfd_byte *) contents + rel->address);
1931 val = ((val &~ rel->howto->dst_mask)
1932 | (((val & rel->howto->src_mask)
1933 + (sym->value - rel->address)
1935 & rel->howto->dst_mask));
1936 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
1938 /* If this reloc is against an stubbed symbol and the
1941 then we replace the next instruction with
1943 This reloads the TOC pointer after a stub call. */
1944 if (bfd_asymbol_name (sym)[0] == '.'
1945 && (sym->flags & BSF_DYNAMIC) != 0
1946 && (bfd_get_32 (outbfd,
1947 (bfd_byte *) contents + rel->address + 4)
1948 == 0x4ffffb82)) /* cror 31,31,31 */
1949 bfd_put_32 (outbfd, (bfd_vma) 0x80410014, /* lwz r2,20(r1) */
1950 (bfd_byte *) contents + rel->address + 4);
1954 memmove (relocs, relocs + 1,
1955 (size_t) ((reloc_count - 1) * sizeof (arelent *)));
1960 /* When considering a TOC reloc, we do not want to include the
1961 symbol value. The symbol will be start of the TOC section
1962 (which is named .got). We do want to include the addend. */
1963 if (rel->howto == toc_howto)
1966 sym_value = sym->value;
1968 /* If this is a relocation against a symbol with a value, or
1969 there is a reloc addend, we need to update the addend in the
1971 if (sym_value + rel->addend != 0)
1975 switch (rel->howto->size)
1978 val = bfd_get_16 (outbfd,
1979 (bfd_byte *) contents + rel->address);
1980 val = ((val &~ rel->howto->dst_mask)
1981 | (((val & rel->howto->src_mask)
1984 & rel->howto->dst_mask));
1985 if ((bfd_signed_vma) val < - 0x8000
1986 || (bfd_signed_vma) val >= 0x8000)
1987 non_fatal (_("overflow when adjusting relocation against %s"),
1988 bfd_asymbol_name (sym));
1989 bfd_put_16 (outbfd, val, (bfd_byte *) contents + rel->address);
1993 val = bfd_get_32 (outbfd,
1994 (bfd_byte *) contents + rel->address);
1995 val = ((val &~ rel->howto->dst_mask)
1996 | (((val & rel->howto->src_mask)
1999 & rel->howto->dst_mask));
2000 bfd_put_32 (outbfd, val, (bfd_byte *) contents + rel->address);
2007 if (! bfd_is_und_section (bfd_get_section (sym)))
2008 rel->sym_ptr_ptr = bfd_get_section (sym)->symbol_ptr_ptr;
2012 /* Now that we have incorporated the addend, remove any TOC
2014 if (rel->howto == toc_howto)
2018 memmove (relocs, relocs + 1,
2019 (size_t) ((reloc_count - i) * sizeof (arelent *)));
2023 rel->address += insec->output_offset;
2027 #endif /* NLMCONV_POWERPC */
2029 /* Name of linker. */
2031 #define LD_NAME "ld"
2034 /* The user has specified several input files. Invoke the linker to
2035 link them all together, and convert and delete the resulting output
2039 link_inputs (struct string_list *inputs, char *ld, char * map_file)
2042 struct string_list *q;
2051 for (q = inputs; q != NULL; q = q->next)
2054 argv = (char **) alloca ((c + 7) * sizeof (char *));
2061 /* Find the linker to invoke based on how nlmconv was run. */
2062 p = program_name + strlen (program_name);
2063 while (p != program_name)
2067 ld = (char *) xmalloc (p - program_name + strlen (LD_NAME) + 1);
2068 memcpy (ld, program_name, p - program_name);
2069 strcpy (ld + (p - program_name), LD_NAME);
2078 ld = (char *) LD_NAME;
2080 unlink_on_exit = make_temp_file (".O");
2083 argv[1] = (char *) "-Ur";
2084 argv[2] = (char *) "-o";
2085 argv[3] = unlink_on_exit;
2086 /* If we have been given the name of a mapfile and that
2087 name is not 'stderr' then pass it on to the linker. */
2090 && strcmp (map_file, "stderr") == 0)
2092 argv[4] = (char *) "-Map";
2099 for (q = inputs; q != NULL; q = q->next, i++)
2100 argv[i] = q->string;
2105 for (i = 0; argv[i] != NULL; i++)
2106 fprintf (stderr, " %s", argv[i]);
2107 fprintf (stderr, "\n");
2110 pid = pexecute (ld, argv, program_name, (char *) NULL, &errfmt, &errarg,
2111 PEXECUTE_SEARCH | PEXECUTE_ONE);
2114 fprintf (stderr, _("%s: execution of %s failed: "), program_name, ld);
2115 fprintf (stderr, errfmt, errarg);
2116 unlink (unlink_on_exit);
2120 if (pwait (pid, &status, 0) < 0)
2123 unlink (unlink_on_exit);
2129 non_fatal (_("Execution of %s failed"), ld);
2130 unlink (unlink_on_exit);
2134 return unlink_on_exit;