1 /* NLM (NetWare Loadable Module) executable support for BFD.
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 Written by Fred Fish @ Cygnus Support, using ELF support as the
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <string.h> /* For strrchr and friends */
29 /* The functions in this file do not use the names they appear to use.
30 This file is actually compiled multiple times, once for each size
31 of NLM target we are using. At each size we use a different name,
32 constructed by the macro nlmNAME. For example, the function which
33 is named nlm_symbol_type below is actually named nlm32_symbol_type
34 in the final executable. */
36 #define Nlm_External_Fixed_Header NlmNAME(External_Fixed_Header)
37 #define Nlm_External_Version_Header NlmNAME(External_Version_Header)
38 #define Nlm_External_Copyright_Header NlmNAME(External_Copyright_Header)
39 #define Nlm_External_Extended_Header NlmNAME(External_Extended_Header)
40 #define Nlm_External_Custom_Header NlmNAME(External_Custom_Header)
42 #define nlm_symbol_type nlmNAME(symbol_type)
43 #define nlm_get_symtab_upper_bound nlmNAME(get_symtab_upper_bound)
44 #define nlm_get_symtab nlmNAME(get_symtab)
45 #define nlm_make_empty_symbol nlmNAME(make_empty_symbol)
46 #define nlm_print_symbol nlmNAME(print_symbol)
47 #define nlm_get_symbol_info nlmNAME(get_symbol_info)
48 #define nlm_get_reloc_upper_bound nlmNAME(get_reloc_upper_bound)
49 #define nlm_canonicalize_reloc nlmNAME(canonicalize_reloc)
50 #define nlm_object_p nlmNAME(object_p)
51 #define nlm_set_section_contents nlmNAME(set_section_contents)
52 #define nlm_write_object_contents nlmNAME(write_object_contents)
54 #define nlm_swap_fixed_header_in(abfd,src,dst) \
55 (nlm_swap_fixed_header_in_func(abfd))(abfd,src,dst)
56 #define nlm_swap_fixed_header_out(abfd,src,dst) \
57 (nlm_swap_fixed_header_out_func(abfd))(abfd,src,dst)
59 /* Forward declarations of static functions */
61 static boolean add_bfd_section
62 PARAMS ((bfd *, char *, file_ptr, bfd_size_type, flagword));
63 static boolean nlm_swap_variable_header_in
65 static boolean nlm_swap_variable_header_out
67 static boolean find_nonzero
68 PARAMS ((PTR, size_t));
69 static boolean nlm_swap_auxiliary_headers_in
71 static boolean nlm_swap_auxiliary_headers_out
73 static boolean nlm_slurp_symbol_table
75 static boolean nlm_slurp_reloc_fixups
77 static boolean nlm_compute_section_file_positions
79 static int nlm_external_reloc_compare
80 PARAMS ((const void *, const void *));
82 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
83 can be handled by explicitly specifying 32 bits or "the long type". */
85 #define put_word bfd_h_put_64
86 #define get_word bfd_h_get_64
89 #define put_word bfd_h_put_32
90 #define get_word bfd_h_get_32
94 DEFUN (nlm_object_p, (abfd), bfd * abfd)
96 struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd);
97 boolean (*backend_object_p) PARAMS ((bfd *));
99 Nlm_Internal_Fixed_Header *i_fxdhdrp;
100 const char *signature;
101 enum bfd_architecture arch;
103 /* Some NLM formats have a prefix before the standard NLM fixed
105 backend_object_p = nlm_backend_object_p_func (abfd);
106 if (backend_object_p)
108 if (! (*backend_object_p) (abfd))
109 goto got_wrong_format_error;
112 /* Read in the fixed length portion of the NLM header in external format. */
114 x_fxdhdr = (PTR) malloc (nlm_fixed_header_size (abfd));
115 if (x_fxdhdr == NULL)
117 bfd_set_error (bfd_error_no_memory);
121 if (bfd_read ((PTR) x_fxdhdr, nlm_fixed_header_size (abfd), 1, abfd) !=
122 nlm_fixed_header_size (abfd))
123 goto got_wrong_format_error;
125 /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
126 the tdata pointer in the bfd. */
128 nlm_tdata (abfd) = (struct nlm_obj_tdata *)
129 bfd_zalloc (abfd, sizeof (struct nlm_obj_tdata));
130 if (nlm_tdata (abfd) == NULL)
132 bfd_set_error (bfd_error_no_memory);
136 i_fxdhdrp = nlm_fixed_header (abfd);
137 nlm_swap_fixed_header_in (abfd, x_fxdhdr, i_fxdhdrp);
139 /* Check to see if we have an NLM file for this backend by matching
140 the NLM signature. */
142 signature = nlm_signature (abfd);
143 if (signature != NULL
144 && *signature != '\0'
145 && strncmp ((char *) i_fxdhdrp->signature, signature,
146 NLM_SIGNATURE_SIZE) != 0)
147 goto got_wrong_format_error;
149 /* There's no supported way to discover the endianess of an NLM, so test for
150 a sane version number after doing byte swapping appropriate for this
151 XVEC. (Hack alert!) */
153 if (i_fxdhdrp->version > 0xFFFF)
154 goto got_wrong_format_error;
156 /* There's no supported way to check for 32 bit versus 64 bit addresses,
157 so ignore this distinction for now. (FIXME) */
159 /* FIXME: Any return(NULL) exits below here will leak memory (tdata).
160 And a memory leak also means we lost the real tdata info we wanted
161 to save, because it was in the leaked memory. */
163 /* Swap in the rest of the fixed length header. */
165 if (!nlm_swap_variable_header_in (abfd)
166 || !nlm_swap_auxiliary_headers_in (abfd)
167 || !add_bfd_section (abfd, NLM_CODE_NAME,
168 i_fxdhdrp -> codeImageOffset,
169 i_fxdhdrp -> codeImageSize,
170 (SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
172 || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
173 i_fxdhdrp -> dataImageOffset,
174 i_fxdhdrp -> dataImageSize,
175 (SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
177 || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
179 i_fxdhdrp -> uninitializedDataSize,
181 goto got_wrong_format_error;
183 if (nlm_fixed_header (abfd)->numberOfRelocationFixups != 0
184 || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
185 abfd->flags |= HAS_RELOC;
186 if (nlm_fixed_header (abfd)->numberOfPublics != 0
187 || nlm_fixed_header (abfd)->numberOfDebugRecords != 0
188 || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
189 abfd->flags |= HAS_SYMS;
191 arch = nlm_architecture (abfd);
192 if (arch != bfd_arch_unknown)
193 bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
195 if (x_fxdhdr != NULL)
197 return (abfd -> xvec);
199 got_wrong_format_error:
200 bfd_set_error (bfd_error_wrong_format);
202 nlm_tdata (abfd) = preserved_tdata;
203 if (x_fxdhdr != NULL)
208 /* Add a section to the bfd. */
211 DEFUN (add_bfd_section, (abfd, name, offset, size, flags),
215 bfd_size_type size AND
220 newsect = bfd_make_section (abfd, name);
225 newsect -> vma = 0; /* NLM's are relocatable. */
226 newsect -> _raw_size = size;
227 newsect -> filepos = offset;
228 newsect -> flags = flags;
229 newsect -> alignment_power = bfd_log2 (0); /* FIXME */
233 /* Read and swap in the variable length header. All the fields must
234 exist in the NLM, and must exist in the order they are read here. */
237 DEFUN (nlm_swap_variable_header_in, (abfd),
240 unsigned char temp [NLM_TARGET_LONG_SIZE];
242 /* Read the description length and text members. */
244 if (bfd_read ((PTR) &nlm_variable_header (abfd) -> descriptionLength,
245 sizeof (nlm_variable_header (abfd) -> descriptionLength),
247 sizeof (nlm_variable_header (abfd) -> descriptionLength))
249 bfd_set_error (bfd_error_system_call);
252 if (bfd_read ((PTR) nlm_variable_header (abfd) -> descriptionText,
253 nlm_variable_header (abfd) -> descriptionLength + 1,
255 nlm_variable_header (abfd) -> descriptionLength + 1)
257 bfd_set_error (bfd_error_system_call);
261 /* Read and convert the stackSize field. */
263 if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
265 bfd_set_error (bfd_error_system_call);
268 nlm_variable_header (abfd) -> stackSize = get_word (abfd, (bfd_byte *) temp);
270 /* Read and convert the reserved field. */
272 if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
274 bfd_set_error (bfd_error_system_call);
277 nlm_variable_header (abfd) -> reserved = get_word (abfd, (bfd_byte *) temp);
279 /* Read the oldThreadName field. This field is a fixed length string. */
281 if (bfd_read ((PTR) nlm_variable_header (abfd) -> oldThreadName,
282 sizeof (nlm_variable_header (abfd) -> oldThreadName),
284 sizeof (nlm_variable_header (abfd) -> oldThreadName))
286 bfd_set_error (bfd_error_system_call);
290 /* Read the screen name length and text members. */
292 if (bfd_read ((PTR) &nlm_variable_header (abfd) -> screenNameLength,
293 sizeof (nlm_variable_header (abfd) -> screenNameLength),
295 sizeof (nlm_variable_header (abfd) -> screenNameLength))
297 bfd_set_error (bfd_error_system_call);
300 if (bfd_read ((PTR) nlm_variable_header (abfd) -> screenName,
301 nlm_variable_header (abfd) -> screenNameLength + 1,
303 nlm_variable_header (abfd) -> screenNameLength + 1)
305 bfd_set_error (bfd_error_system_call);
309 /* Read the thread name length and text members. */
311 if (bfd_read ((PTR) &nlm_variable_header (abfd) -> threadNameLength,
312 sizeof (nlm_variable_header (abfd) -> threadNameLength),
314 sizeof (nlm_variable_header (abfd) -> threadNameLength))
316 bfd_set_error (bfd_error_system_call);
319 if (bfd_read ((PTR) nlm_variable_header (abfd) -> threadName,
320 nlm_variable_header (abfd) -> threadNameLength + 1,
322 nlm_variable_header (abfd) -> threadNameLength + 1)
324 bfd_set_error (bfd_error_system_call);
330 /* Swap and write out the variable length header. All the fields must
331 exist in the NLM, and must exist in this order. */
334 DEFUN (nlm_swap_variable_header_out, (abfd),
337 unsigned char temp [NLM_TARGET_LONG_SIZE];
339 /* Write the description length and text members. */
341 if (bfd_write ((PTR) &nlm_variable_header (abfd) -> descriptionLength,
342 sizeof (nlm_variable_header (abfd) -> descriptionLength),
344 sizeof (nlm_variable_header (abfd) -> descriptionLength))
346 bfd_set_error (bfd_error_system_call);
349 if (bfd_write ((PTR) nlm_variable_header (abfd) -> descriptionText,
350 nlm_variable_header (abfd) -> descriptionLength + 1,
352 nlm_variable_header (abfd) -> descriptionLength + 1)
354 bfd_set_error (bfd_error_system_call);
358 /* Convert and write the stackSize field. */
360 put_word (abfd, (bfd_vma) nlm_variable_header (abfd) -> stackSize,
362 if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
364 bfd_set_error (bfd_error_system_call);
368 /* Convert and write the reserved field. */
370 put_word (abfd, (bfd_vma) nlm_variable_header (abfd) -> reserved,
372 if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
374 bfd_set_error (bfd_error_system_call);
378 /* Write the oldThreadName field. This field is a fixed length string. */
380 if (bfd_write ((PTR) nlm_variable_header (abfd) -> oldThreadName,
381 sizeof (nlm_variable_header (abfd) -> oldThreadName),
383 sizeof (nlm_variable_header (abfd) -> oldThreadName))
385 bfd_set_error (bfd_error_system_call);
389 /* Write the screen name length and text members. */
391 if (bfd_write ((PTR) &nlm_variable_header (abfd) -> screenNameLength,
392 sizeof (nlm_variable_header (abfd) -> screenNameLength),
394 sizeof (nlm_variable_header (abfd) -> screenNameLength))
396 bfd_set_error (bfd_error_system_call);
399 if (bfd_write ((PTR) nlm_variable_header (abfd) -> screenName,
400 nlm_variable_header (abfd) -> screenNameLength + 1,
402 nlm_variable_header (abfd) -> screenNameLength + 1)
404 bfd_set_error (bfd_error_system_call);
408 /* Write the thread name length and text members. */
410 if (bfd_write ((PTR) &nlm_variable_header (abfd) -> threadNameLength,
411 sizeof (nlm_variable_header (abfd) -> threadNameLength),
413 sizeof (nlm_variable_header (abfd) -> threadNameLength))
415 bfd_set_error (bfd_error_system_call);
418 if (bfd_write ((PTR) nlm_variable_header (abfd) -> threadName,
419 nlm_variable_header (abfd) -> threadNameLength + 1,
421 nlm_variable_header (abfd) -> threadNameLength + 1)
423 bfd_set_error (bfd_error_system_call);
429 /* Read and swap in the contents of all the auxiliary headers. Because of
430 the braindead design, we have to do strcmps on strings of indeterminate
431 length to figure out what each auxiliary header is. Even worse, we have
432 no way of knowing how many auxiliary headers there are or where the end
433 of the auxiliary headers are, except by finding something that doesn't
434 look like a known auxiliary header. This means that the first new type
435 of auxiliary header added will break all existing tools that don't
439 DEFUN (nlm_swap_auxiliary_headers_in, (abfd),
447 position = bfd_tell (abfd);
448 if (bfd_read ((PTR) tempstr, sizeof (tempstr), 1, abfd) !=
451 bfd_set_error (bfd_error_system_call);
454 if (bfd_seek (abfd, position, SEEK_SET) == -1)
456 bfd_set_error (bfd_error_system_call);
459 if (strncmp (tempstr, "VeRsIoN#", 8) == 0)
461 Nlm_External_Version_Header thdr;
462 if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
464 bfd_set_error (bfd_error_system_call);
467 memcpy (nlm_version_header (abfd) -> stamp, thdr.stamp,
468 sizeof (thdr.stamp));
469 nlm_version_header (abfd) -> majorVersion =
470 get_word (abfd, (bfd_byte *) thdr.majorVersion);
471 nlm_version_header (abfd) -> minorVersion =
472 get_word (abfd, (bfd_byte *) thdr.minorVersion);
473 nlm_version_header (abfd) -> revision =
474 get_word (abfd, (bfd_byte *) thdr.revision);
475 nlm_version_header (abfd) -> year =
476 get_word (abfd, (bfd_byte *) thdr.year);
477 nlm_version_header (abfd) -> month =
478 get_word (abfd, (bfd_byte *) thdr.month);
479 nlm_version_header (abfd) -> day =
480 get_word (abfd, (bfd_byte *) thdr.day);
482 else if (strncmp (tempstr, "MeSsAgEs", 8) == 0)
484 Nlm_External_Extended_Header thdr;
485 if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
487 bfd_set_error (bfd_error_system_call);
490 memcpy (nlm_extended_header (abfd) -> stamp, thdr.stamp,
491 sizeof (thdr.stamp));
492 nlm_extended_header (abfd) -> languageID =
493 get_word (abfd, (bfd_byte *) thdr.languageID);
494 nlm_extended_header (abfd) -> messageFileOffset =
495 get_word (abfd, (bfd_byte *) thdr.messageFileOffset);
496 nlm_extended_header (abfd) -> messageFileLength =
497 get_word (abfd, (bfd_byte *) thdr.messageFileLength);
498 nlm_extended_header (abfd) -> messageCount =
499 get_word (abfd, (bfd_byte *) thdr.messageCount);
500 nlm_extended_header (abfd) -> helpFileOffset =
501 get_word (abfd, (bfd_byte *) thdr.helpFileOffset);
502 nlm_extended_header (abfd) -> helpFileLength =
503 get_word (abfd, (bfd_byte *) thdr.helpFileLength);
504 nlm_extended_header (abfd) -> RPCDataOffset =
505 get_word (abfd, (bfd_byte *) thdr.RPCDataOffset);
506 nlm_extended_header (abfd) -> RPCDataLength =
507 get_word (abfd, (bfd_byte *) thdr.RPCDataLength);
508 nlm_extended_header (abfd) -> sharedCodeOffset =
509 get_word (abfd, (bfd_byte *) thdr.sharedCodeOffset);
510 nlm_extended_header (abfd) -> sharedCodeLength =
511 get_word (abfd, (bfd_byte *) thdr.sharedCodeLength);
512 nlm_extended_header (abfd) -> sharedDataOffset =
513 get_word (abfd, (bfd_byte *) thdr.sharedDataOffset);
514 nlm_extended_header (abfd) -> sharedDataLength =
515 get_word (abfd, (bfd_byte *) thdr.sharedDataLength);
516 nlm_extended_header (abfd) -> sharedRelocationFixupOffset =
517 get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupOffset);
518 nlm_extended_header (abfd) -> sharedRelocationFixupCount =
519 get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupCount);
520 nlm_extended_header (abfd) -> sharedExternalReferenceOffset =
521 get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceOffset);
522 nlm_extended_header (abfd) -> sharedExternalReferenceCount =
523 get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceCount);
524 nlm_extended_header (abfd) -> sharedPublicsOffset =
525 get_word (abfd, (bfd_byte *) thdr.sharedPublicsOffset);
526 nlm_extended_header (abfd) -> sharedPublicsCount =
527 get_word (abfd, (bfd_byte *) thdr.sharedPublicsCount);
528 nlm_extended_header (abfd) -> sharedDebugRecordOffset =
529 get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordOffset);
530 nlm_extended_header (abfd) -> sharedDebugRecordCount =
531 get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordCount);
532 nlm_extended_header (abfd) -> SharedInitializationOffset =
533 get_word (abfd, (bfd_byte *) thdr.sharedInitializationOffset);
534 nlm_extended_header (abfd) -> SharedExitProcedureOffset =
535 get_word (abfd, (bfd_byte *) thdr.SharedExitProcedureOffset);
536 nlm_extended_header (abfd) -> productID =
537 get_word (abfd, (bfd_byte *) thdr.productID);
538 nlm_extended_header (abfd) -> reserved0 =
539 get_word (abfd, (bfd_byte *) thdr.reserved0);
540 nlm_extended_header (abfd) -> reserved1 =
541 get_word (abfd, (bfd_byte *) thdr.reserved1);
542 nlm_extended_header (abfd) -> reserved2 =
543 get_word (abfd, (bfd_byte *) thdr.reserved2);
544 nlm_extended_header (abfd) -> reserved3 =
545 get_word (abfd, (bfd_byte *) thdr.reserved3);
546 nlm_extended_header (abfd) -> reserved4 =
547 get_word (abfd, (bfd_byte *) thdr.reserved4);
548 nlm_extended_header (abfd) -> reserved5 =
549 get_word (abfd, (bfd_byte *) thdr.reserved5);
551 else if (strncmp (tempstr, "CuStHeAd", 8) == 0)
553 Nlm_External_Custom_Header thdr;
554 if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
556 bfd_set_error (bfd_error_system_call);
559 memcpy (nlm_custom_header (abfd) -> stamp, thdr.stamp,
560 sizeof (thdr.stamp));
561 nlm_custom_header (abfd) -> dataLength =
562 get_word (abfd, (bfd_byte *) thdr.dataLength);
563 nlm_custom_header (abfd) -> debugRecOffset =
564 get_word (abfd, (bfd_byte *) thdr.debugRecOffset);
565 nlm_custom_header (abfd) -> debugRecLength =
566 get_word (abfd, (bfd_byte *) thdr.debugRecLength);
568 else if (strncmp (tempstr, "CoPyRiGhT=", 10) == 0)
570 if (bfd_read ((PTR) nlm_copyright_header (abfd)->stamp,
571 sizeof (nlm_copyright_header (abfd)->stamp),
573 != sizeof (nlm_copyright_header (abfd)->stamp))
575 bfd_set_error (bfd_error_system_call);
578 if (bfd_read ((PTR) &(nlm_copyright_header (abfd)
579 ->copyrightMessageLength),
582 bfd_set_error (bfd_error_system_call);
585 /* The copyright message is a variable length string. */
586 if (bfd_read ((PTR) nlm_copyright_header (abfd) -> copyrightMessage,
587 nlm_copyright_header (abfd) -> copyrightMessageLength + 1,
589 nlm_copyright_header (abfd) -> copyrightMessageLength + 1)
591 bfd_set_error (bfd_error_system_call);
603 /* Return whether there is a non-zero byte in a memory block. */
606 find_nonzero (buf, size)
610 char *p = (char *) buf;
618 /* Swap out the contents of the auxiliary headers. We create those
619 auxiliary headers which have been set non-zero. We do not require
620 the caller to set up the stamp fields. */
623 nlm_swap_auxiliary_headers_out (abfd)
626 /* Write out the version header if there is one. */
627 if (find_nonzero ((PTR) nlm_version_header (abfd),
628 sizeof (Nlm_Internal_Version_Header)))
630 Nlm_External_Version_Header thdr;
632 memcpy (thdr.stamp, "VeRsIoN#", 8);
633 put_word (abfd, (bfd_vma) nlm_version_header (abfd) -> majorVersion,
634 (bfd_byte *) thdr.majorVersion);
635 put_word (abfd, (bfd_vma) nlm_version_header (abfd) -> minorVersion,
636 (bfd_byte *) thdr.minorVersion);
637 put_word (abfd, (bfd_vma) nlm_version_header (abfd) -> revision,
638 (bfd_byte *) thdr.revision);
639 put_word (abfd, (bfd_vma) nlm_version_header (abfd) -> year,
640 (bfd_byte *) thdr.year);
641 put_word (abfd, (bfd_vma) nlm_version_header (abfd) -> month,
642 (bfd_byte *) thdr.month);
643 put_word (abfd, (bfd_vma) nlm_version_header (abfd) -> day,
644 (bfd_byte *) thdr.day);
645 if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
647 bfd_set_error (bfd_error_system_call);
652 /* Write out the extended header if there is one. */
653 if (find_nonzero ((PTR) nlm_extended_header (abfd),
654 sizeof (Nlm_Internal_Extended_Header)))
656 Nlm_External_Extended_Header thdr;
658 memcpy (thdr.stamp, "MeSsAgEs", 8);
660 (bfd_vma) nlm_extended_header (abfd) -> languageID,
661 (bfd_byte *) thdr.languageID);
663 (bfd_vma) nlm_extended_header (abfd) -> messageFileOffset,
664 (bfd_byte *) thdr.messageFileOffset);
666 (bfd_vma) nlm_extended_header (abfd) -> messageFileLength,
667 (bfd_byte *) thdr.messageFileLength);
669 (bfd_vma) nlm_extended_header (abfd) -> messageCount,
670 (bfd_byte *) thdr.messageCount);
672 (bfd_vma) nlm_extended_header (abfd) -> helpFileOffset,
673 (bfd_byte *) thdr.helpFileOffset);
675 (bfd_vma) nlm_extended_header (abfd) -> helpFileLength,
676 (bfd_byte *) thdr.helpFileLength);
678 (bfd_vma) nlm_extended_header (abfd) -> RPCDataOffset,
679 (bfd_byte *) thdr.RPCDataOffset);
681 (bfd_vma) nlm_extended_header (abfd) -> RPCDataLength,
682 (bfd_byte *) thdr.RPCDataLength);
684 (bfd_vma) nlm_extended_header (abfd) -> sharedCodeOffset,
685 (bfd_byte *) thdr.sharedCodeOffset);
687 (bfd_vma) nlm_extended_header (abfd) -> sharedCodeLength,
688 (bfd_byte *) thdr.sharedCodeLength);
690 (bfd_vma) nlm_extended_header (abfd) -> sharedDataOffset,
691 (bfd_byte *) thdr.sharedDataOffset);
693 (bfd_vma) nlm_extended_header (abfd) -> sharedDataLength,
694 (bfd_byte *) thdr.sharedDataLength);
696 (bfd_vma) nlm_extended_header (abfd) -> sharedRelocationFixupOffset,
697 (bfd_byte *) thdr.sharedRelocationFixupOffset);
699 (bfd_vma) nlm_extended_header (abfd) -> sharedRelocationFixupCount,
700 (bfd_byte *) thdr.sharedRelocationFixupCount);
702 (bfd_vma) nlm_extended_header (abfd) -> sharedExternalReferenceOffset,
703 (bfd_byte *) thdr.sharedExternalReferenceOffset);
705 (bfd_vma) nlm_extended_header (abfd) -> sharedExternalReferenceCount,
706 (bfd_byte *) thdr.sharedExternalReferenceCount);
708 (bfd_vma) nlm_extended_header (abfd) -> sharedPublicsOffset,
709 (bfd_byte *) thdr.sharedPublicsOffset);
711 (bfd_vma) nlm_extended_header (abfd) -> sharedPublicsCount,
712 (bfd_byte *) thdr.sharedPublicsCount);
714 (bfd_vma) nlm_extended_header (abfd) -> sharedDebugRecordOffset,
715 (bfd_byte *) thdr.sharedDebugRecordOffset);
717 (bfd_vma) nlm_extended_header (abfd) -> sharedDebugRecordCount,
718 (bfd_byte *) thdr.sharedDebugRecordCount);
720 (bfd_vma) nlm_extended_header (abfd) -> SharedInitializationOffset,
721 (bfd_byte *) thdr.sharedInitializationOffset);
723 (bfd_vma) nlm_extended_header (abfd) -> SharedExitProcedureOffset,
724 (bfd_byte *) thdr.SharedExitProcedureOffset);
726 (bfd_vma) nlm_extended_header (abfd) -> productID,
727 (bfd_byte *) thdr.productID);
729 (bfd_vma) nlm_extended_header (abfd) -> reserved0,
730 (bfd_byte *) thdr.reserved0);
732 (bfd_vma) nlm_extended_header (abfd) -> reserved1,
733 (bfd_byte *) thdr.reserved1);
735 (bfd_vma) nlm_extended_header (abfd) -> reserved2,
736 (bfd_byte *) thdr.reserved2);
738 (bfd_vma) nlm_extended_header (abfd) -> reserved3,
739 (bfd_byte *) thdr.reserved3);
741 (bfd_vma) nlm_extended_header (abfd) -> reserved4,
742 (bfd_byte *) thdr.reserved4);
744 (bfd_vma) nlm_extended_header (abfd) -> reserved5,
745 (bfd_byte *) thdr.reserved5);
746 if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
748 bfd_set_error (bfd_error_system_call);
753 /* Write out the custom header if there is one. */
754 if (find_nonzero ((PTR) nlm_custom_header (abfd),
755 sizeof (Nlm_Internal_Custom_Header)))
757 Nlm_External_Custom_Header thdr;
759 /* Right now we assume the custom header is always the suggested
760 format for alternate debugging records. */
761 BFD_ASSERT (nlm_custom_header (abfd) -> dataLength == 8);
763 memcpy (thdr.stamp, "CuStHeAd", 8);
764 put_word (abfd, (bfd_vma) nlm_custom_header (abfd) -> dataLength,
765 (bfd_byte *) thdr.dataLength);
766 put_word (abfd, (bfd_vma) nlm_custom_header (abfd) -> debugRecOffset,
767 (bfd_byte *) thdr.debugRecOffset);
768 put_word (abfd, (bfd_vma) nlm_custom_header (abfd) -> debugRecLength,
769 (bfd_byte *) thdr.debugRecLength);
770 if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
772 bfd_set_error (bfd_error_system_call);
777 /* Write out the copyright header if there is one. */
778 if (find_nonzero ((PTR) nlm_copyright_header (abfd),
779 sizeof (Nlm_Internal_Copyright_Header)))
781 Nlm_External_Copyright_Header thdr;
783 memcpy (thdr.stamp, "CoPyRiGhT=", 10);
784 if (bfd_write ((PTR) thdr.stamp, sizeof (thdr.stamp), 1, abfd)
785 != sizeof (thdr.stamp))
787 bfd_set_error (bfd_error_system_call);
790 thdr.copyrightMessageLength[0] =
791 nlm_copyright_header (abfd)->copyrightMessageLength;
792 if (bfd_write ((PTR) thdr.copyrightMessageLength, 1, 1, abfd) != 1)
794 bfd_set_error (bfd_error_system_call);
797 /* The copyright message is a variable length string. */
798 if (bfd_write ((PTR) nlm_copyright_header (abfd) -> copyrightMessage,
799 nlm_copyright_header (abfd) -> copyrightMessageLength + 1,
801 nlm_copyright_header (abfd) -> copyrightMessageLength + 1)
803 bfd_set_error (bfd_error_system_call);
811 /* We read the NLM's public symbols and use it to generate a bfd symbol
812 table (hey, it's better than nothing) on a one-for-one basis. Thus
813 use the number of public symbols as the number of bfd symbols we will
814 have once we actually get around to reading them in.
816 Return the number of bytes required to hold the symtab vector, based on
817 the count plus 1, since we will NULL terminate the vector allocated based
821 DEFUN (nlm_get_symtab_upper_bound, (abfd), bfd * abfd)
823 Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form */
824 unsigned int symcount;
825 unsigned int symtab_size = 0;
827 i_fxdhdrp = nlm_fixed_header (abfd);
828 symcount = (i_fxdhdrp -> numberOfPublics
829 + i_fxdhdrp -> numberOfDebugRecords
830 + i_fxdhdrp -> numberOfExternalReferences);
831 symtab_size = (symcount + 1) * (sizeof (asymbol));
832 return (symtab_size);
835 /* Note that bfd_get_symcount is guaranteed to be zero if slurping the
836 symbol table fails. */
839 nlm_get_symtab (abfd, alocation)
843 nlm_symbol_type *symbase;
844 bfd_size_type counter = 0;
846 if (nlm_slurp_symbol_table (abfd) == false)
848 symbase = nlm_get_symbols (abfd);
849 while (counter < bfd_get_symcount (abfd))
851 *alocation++ = &symbase->symbol;
855 *alocation = (asymbol *) NULL;
856 return bfd_get_symcount (abfd);
859 /* Make an NLM symbol. There is nothing special to do here. */
862 nlm_make_empty_symbol (abfd)
865 nlm_symbol_type *new;
867 new = (nlm_symbol_type *) bfd_zalloc (abfd, sizeof (nlm_symbol_type));
869 new->symbol.the_bfd = abfd;
873 /* Get symbol information. */
876 nlm_get_symbol_info (ignore_abfd, symbol, ret)
881 bfd_symbol_info (symbol, ret);
884 /* Print symbol information. */
887 nlm_print_symbol (abfd, afile, symbol, how)
891 bfd_print_symbol_type how;
893 FILE *file = (FILE *) afile;
897 case bfd_print_symbol_name:
898 case bfd_print_symbol_more:
900 fprintf (file,"%s", symbol->name);
902 case bfd_print_symbol_all:
903 bfd_print_symbol_vandf ((PTR) file, symbol);
904 fprintf (file, " %-5s", symbol->section->name);
906 fprintf (file," %s", symbol->name);
911 /* Slurp in nlm symbol table.
913 In the external (in-file) form, NLM export records are variable length,
914 with the following form:
916 1 byte length of the symbol name (N)
917 N bytes the symbol name
918 4 bytes the symbol offset from start of it's section
920 We also read in the debugging symbols and import records. Import
921 records are treated as undefined symbols. As we read the import
922 records we also read in the associated reloc information, which is
923 attached to the symbol.
925 The bfd symbols are copied to SYMPTRS.
927 When we return, the bfd symcount is either zero or contains the correct
932 nlm_slurp_symbol_table (abfd)
935 Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form */
936 bfd_size_type totsymcount; /* Number of NLM symbols */
937 bfd_size_type symcount; /* Counter of NLM symbols */
938 nlm_symbol_type *sym; /* Pointer to current bfd symbol */
939 unsigned char symlength; /* Symbol length read into here */
940 unsigned char symtype; /* Type of debugging symbol */
941 bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* Symbol offsets read into here */
942 boolean (*read_import_func) PARAMS ((bfd *, nlm_symbol_type *));
943 boolean (*set_public_section_func) PARAMS ((bfd *, nlm_symbol_type *));
945 if (nlm_get_symbols (abfd) != NULL)
948 /* Read each raw NLM symbol, using the information to create a canonical bfd
951 Note that we allocate the initial bfd canonical symbol buffer based on a
952 one-to-one mapping of the NLM symbols to canonical symbols. We actually
953 use all the NLM symbols, so there will be no space left over at the end.
954 When we have all the symbols, we build the caller's pointer vector. */
956 abfd -> symcount = 0;
957 i_fxdhdrp = nlm_fixed_header (abfd);
958 totsymcount = (i_fxdhdrp -> numberOfPublics
959 + i_fxdhdrp -> numberOfDebugRecords
960 + i_fxdhdrp -> numberOfExternalReferences);
961 if (totsymcount == 0)
966 if (bfd_seek (abfd, i_fxdhdrp -> publicsOffset, SEEK_SET) == -1)
968 bfd_set_error (bfd_error_system_call);
972 sym = ((nlm_symbol_type *)
973 bfd_zalloc (abfd, totsymcount * sizeof (nlm_symbol_type)));
976 bfd_set_error (bfd_error_no_memory);
979 nlm_set_symbols (abfd, sym);
981 /* We use the bfd's symcount directly as the control count, so that early
982 termination of the loop leaves the symcount correct for the symbols that
985 set_public_section_func = nlm_set_public_section_func (abfd);
986 symcount = i_fxdhdrp -> numberOfPublics;
987 while (abfd -> symcount < symcount)
989 if (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd)
990 != sizeof (symlength))
992 bfd_set_error (bfd_error_system_call);
995 sym -> symbol.the_bfd = abfd;
996 sym -> symbol.name = bfd_alloc (abfd, symlength + 1);
997 if (!sym -> symbol.name)
999 bfd_set_error (bfd_error_no_memory);
1002 if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd)
1005 bfd_set_error (bfd_error_system_call);
1008 /* Cast away const. */
1009 ((char *) (sym -> symbol.name))[symlength] = '\0';
1010 if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
1012 bfd_set_error (bfd_error_system_call);
1015 sym -> symbol.flags = BSF_GLOBAL | BSF_EXPORT;
1016 sym -> symbol.value = get_word (abfd, temp);
1017 if (set_public_section_func)
1019 /* Most backends can use the code below, but unfortunately
1020 some use a different scheme. */
1021 if ((*set_public_section_func) (abfd, sym) == false)
1026 if (sym -> symbol.value & NLM_HIBIT)
1028 sym -> symbol.value &= ~NLM_HIBIT;
1029 sym -> symbol.flags |= BSF_FUNCTION;
1030 sym -> symbol.section =
1031 bfd_get_section_by_name (abfd, NLM_CODE_NAME);
1035 sym -> symbol.section =
1036 bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
1044 /* Read the debugging records. */
1046 if (i_fxdhdrp -> numberOfDebugRecords > 0)
1048 if (bfd_seek (abfd, i_fxdhdrp -> debugInfoOffset, SEEK_SET) == -1)
1050 bfd_set_error (bfd_error_system_call);
1054 symcount += i_fxdhdrp -> numberOfDebugRecords;
1055 while (abfd -> symcount < symcount)
1057 if ((bfd_read ((PTR) &symtype, sizeof (symtype), 1, abfd)
1058 != sizeof (symtype))
1059 || bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)
1060 || (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd)
1061 != sizeof (symlength)))
1063 bfd_set_error (bfd_error_system_call);
1066 sym -> symbol.the_bfd = abfd;
1067 sym -> symbol.name = bfd_alloc (abfd, symlength + 1);
1068 if (!sym -> symbol.name)
1070 bfd_set_error (bfd_error_no_memory);
1073 if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd)
1076 bfd_set_error (bfd_error_system_call);
1079 /* Cast away const. */
1080 ((char *) (sym -> symbol.name))[symlength] = '\0';
1081 sym -> symbol.flags = BSF_LOCAL;
1082 sym -> symbol.value = get_word (abfd, temp);
1085 sym -> symbol.section =
1086 bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
1088 else if (symtype == 1)
1090 sym -> symbol.flags |= BSF_FUNCTION;
1091 sym -> symbol.section =
1092 bfd_get_section_by_name (abfd, NLM_CODE_NAME);
1096 sym -> symbol.section = &bfd_abs_section;
1104 /* Read in the import records. We can only do this if we know how
1105 to read relocs for this target. */
1107 read_import_func = nlm_read_import_func (abfd);
1108 if (read_import_func != NULL)
1110 if (bfd_seek (abfd, i_fxdhdrp -> externalReferencesOffset, SEEK_SET)
1113 bfd_set_error (bfd_error_system_call);
1117 symcount += i_fxdhdrp -> numberOfExternalReferences;
1118 while (abfd -> symcount < symcount)
1120 if ((*read_import_func) (abfd, sym) == false)
1130 /* Get the relocs for an NLM file. There are two types of relocs.
1131 Imports are relocs against symbols defined in other NLM files. We
1132 treat these as relocs against global symbols. Relocation fixups
1133 are internal relocs.
1135 The actual format used to store the relocs is machine specific. */
1137 /* Read in the relocation fixup information. This is stored in
1138 nlm_relocation_fixups, an array of arelent structures, and
1139 nlm_relocation_fixup_secs, an array of section pointers. The
1140 section pointers are needed because the relocs are not sorted by
1144 nlm_slurp_reloc_fixups (abfd)
1147 boolean (*read_func) PARAMS ((bfd *, nlm_symbol_type *, asection **,
1149 bfd_size_type count;
1153 if (nlm_relocation_fixups (abfd) != NULL)
1155 read_func = nlm_read_reloc_func (abfd);
1156 if (read_func == NULL)
1159 if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
1162 bfd_set_error (bfd_error_system_call);
1166 count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1167 rels = (arelent *) bfd_alloc (abfd, count * sizeof (arelent));
1168 secs = (asection **) bfd_alloc (abfd, count * sizeof (asection *));
1169 if (rels == NULL || secs == NULL)
1171 bfd_set_error (bfd_error_no_memory);
1174 nlm_relocation_fixups (abfd) = rels;
1175 nlm_relocation_fixup_secs (abfd) = secs;
1177 /* We have to read piece by piece, because we don't know how large
1178 the machine specific reloc information is. */
1179 while (count-- != 0)
1181 if ((*read_func) (abfd, (nlm_symbol_type *) NULL, secs, rels) == false)
1183 nlm_relocation_fixups (abfd) = NULL;
1184 nlm_relocation_fixup_secs (abfd) = NULL;
1194 /* Get the number of relocs. This really just returns an upper bound,
1195 since it does not attempt to distinguish them based on the section.
1196 That will be handled when they are actually read. */
1199 nlm_get_reloc_upper_bound (abfd, sec)
1203 nlm_symbol_type *syms;
1204 bfd_size_type count;
1207 /* If we don't know how to read relocs, just return 0. */
1208 if (nlm_read_reloc_func (abfd) == NULL)
1210 /* Make sure we have either the code or the data section. */
1211 if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
1214 syms = nlm_get_symbols (abfd);
1217 if (nlm_slurp_symbol_table (abfd) == false)
1219 syms = nlm_get_symbols (abfd);
1222 ret = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1224 count = bfd_get_symcount (abfd);
1225 while (count-- != 0)
1231 return (ret + 1) * sizeof (arelent *);
1234 /* Get the relocs themselves. */
1237 nlm_canonicalize_reloc (abfd, sec, relptr, symbols)
1245 bfd_size_type count, i;
1248 /* Get the relocation fixups. */
1249 rels = nlm_relocation_fixups (abfd);
1252 if (nlm_slurp_reloc_fixups (abfd) == false)
1254 rels = nlm_relocation_fixups (abfd);
1258 secs = nlm_relocation_fixup_secs (abfd);
1261 count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1262 for (i = 0; i < count; i++, rels++, secs++)
1271 /* Get the import symbols. */
1272 count = bfd_get_symcount (abfd);
1273 for (i = 0; i < count; i++, symbols++)
1278 if (bfd_asymbol_flavour (sym) == bfd_target_nlm_flavour)
1280 nlm_symbol_type *nlm_sym;
1283 nlm_sym = (nlm_symbol_type *) sym;
1284 for (j = 0; j < nlm_sym->rcnt; j++)
1286 if (nlm_sym->relocs[j].section == sec)
1288 *relptr = &nlm_sym->relocs[j].reloc;
1289 (*relptr)->sym_ptr_ptr = symbols;
1302 /* Compute the section file positions for an NLM file. All variable
1303 length data in the file headers must be set before this function is
1304 called. If the variable length data is changed later, the
1305 resulting object file will be incorrect. Unfortunately, there is
1306 no way to check this.
1308 This routine also sets the Size and Offset fields in the fixed
1311 It also looks over the symbols and moves any common symbols into
1312 the .bss section; NLM has no way to represent a common symbol.
1313 This approach means that either the symbols must already have been
1314 set at this point, or there must be no common symbols. We need to
1315 move the symbols at this point so that mangle_relocs can see the
1319 nlm_compute_section_file_positions (abfd)
1324 bfd_vma text, data, bss;
1325 bfd_vma text_low, data_low;
1326 int text_align, data_align, other_align;
1327 file_ptr text_ptr, data_ptr, other_ptr;
1329 asymbol **sym_ptr_ptr;
1331 if (abfd->output_has_begun == true)
1334 /* Make sure we have a section to hold uninitialized data. */
1335 bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
1336 if (bss_sec == NULL)
1338 if (! add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
1339 (file_ptr) 0, (bfd_size_type) 0,
1342 bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
1345 abfd->output_has_begun = true;
1347 /* The fixed header. */
1348 sofar = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
1350 /* The variable header. */
1351 sofar += (sizeof (nlm_variable_header (abfd)->descriptionLength)
1352 + nlm_variable_header (abfd) -> descriptionLength + 1
1353 + NLM_TARGET_LONG_SIZE /* stackSize */
1354 + NLM_TARGET_LONG_SIZE /* reserved */
1355 + sizeof (nlm_variable_header (abfd) -> oldThreadName)
1356 + sizeof (nlm_variable_header (abfd) -> screenNameLength)
1357 + nlm_variable_header (abfd) -> screenNameLength + 1
1358 + sizeof (nlm_variable_header (abfd) -> threadNameLength)
1359 + nlm_variable_header (abfd) -> threadNameLength + 1);
1361 /* The auxiliary headers. */
1362 if (find_nonzero ((PTR) nlm_version_header (abfd),
1363 sizeof (Nlm_Internal_Version_Header)))
1364 sofar += sizeof (Nlm_External_Version_Header);
1365 if (find_nonzero ((PTR) nlm_extended_header (abfd),
1366 sizeof (Nlm_Internal_Extended_Header)))
1367 sofar += sizeof (Nlm_External_Extended_Header);
1368 if (find_nonzero ((PTR) nlm_custom_header (abfd),
1369 sizeof (Nlm_Internal_Custom_Header)))
1370 sofar += sizeof (Nlm_External_Custom_Header);
1371 if (find_nonzero ((PTR) nlm_copyright_header (abfd),
1372 sizeof (Nlm_Internal_Copyright_Header)))
1373 sofar += (sizeof (Nlm_External_Copyright_Header)
1374 + nlm_copyright_header (abfd) -> copyrightMessageLength + 1);
1376 /* Compute the section file positions in two passes. First get the
1377 sizes of the text and data sections, and then set the file
1378 positions. This code aligns the sections in the file using the
1379 same alignment restrictions that apply to the sections in memory;
1380 this may not be necessary. */
1382 text_low = (bfd_vma) -1;
1385 data_low = (bfd_vma) -1;
1389 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
1393 sec->_raw_size = BFD_ALIGN (sec->_raw_size, 1 << sec->alignment_power);
1395 f = bfd_get_section_flags (abfd, sec);
1398 text += sec->_raw_size;
1399 if (bfd_get_section_vma (abfd, sec) < text_low)
1400 text_low = bfd_get_section_vma (abfd, sec);
1401 if (sec->alignment_power > text_align)
1402 text_align = sec->alignment_power;
1404 else if (f & SEC_DATA)
1406 data += sec->_raw_size;
1407 if (bfd_get_section_vma (abfd, sec) < data_low)
1408 data_low = bfd_get_section_vma (abfd, sec);
1409 if (sec->alignment_power > data_align)
1410 data_align = sec->alignment_power;
1412 else if (f & SEC_HAS_CONTENTS)
1414 if (sec->alignment_power > other_align)
1415 other_align = sec->alignment_power;
1417 else if (f & SEC_ALLOC)
1418 bss += sec->_raw_size;
1421 nlm_set_text_low (abfd, text_low);
1422 nlm_set_data_low (abfd, data_low);
1424 if (nlm_no_uninitialized_data (abfd))
1426 /* This NetWare format does not use uninitialized data. We must
1427 increase the size of the data section. We will never wind up
1428 writing those file locations, so they will remain zero. */
1433 text_ptr = BFD_ALIGN (sofar, 1 << text_align);
1434 data_ptr = BFD_ALIGN (text_ptr + text, 1 << data_align);
1435 other_ptr = BFD_ALIGN (data_ptr + data, 1 << other_align);
1437 /* Fill in some fields in the header for which we now have the
1439 nlm_fixed_header (abfd)->codeImageOffset = text_ptr;
1440 nlm_fixed_header (abfd)->codeImageSize = text;
1441 nlm_fixed_header (abfd)->dataImageOffset = data_ptr;
1442 nlm_fixed_header (abfd)->dataImageSize = data;
1443 nlm_fixed_header (abfd)->uninitializedDataSize = bss;
1445 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
1449 f = bfd_get_section_flags (abfd, sec);
1453 sec->filepos = text_ptr;
1454 text_ptr += sec->_raw_size;
1456 else if (f & SEC_DATA)
1458 sec->filepos = data_ptr;
1459 data_ptr += sec->_raw_size;
1461 else if (f & SEC_HAS_CONTENTS)
1463 sec->filepos = other_ptr;
1464 other_ptr += sec->_raw_size;
1468 nlm_fixed_header (abfd)->relocationFixupOffset = other_ptr;
1470 /* Move all common symbols into the .bss section. */
1472 sym_ptr_ptr = bfd_get_outsymbols (abfd);
1473 if (sym_ptr_ptr != NULL)
1478 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1480 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1487 if (! bfd_is_com_section (bfd_get_section (sym)))
1490 /* Put the common symbol in the .bss section, and increase
1491 the size of the .bss section by the size of the common
1492 symbol (which is the old value of the symbol). */
1493 sym->section = bss_sec;
1495 sym->value = bss_sec->_raw_size + add;
1497 add = BFD_ALIGN (add, 1 << bss_sec->alignment_power);
1501 if (nlm_no_uninitialized_data (abfd))
1503 /* We could handle this case, but so far it hasn't been
1507 nlm_fixed_header (abfd)->uninitializedDataSize += add;
1508 bss_sec->_raw_size += add;
1515 /* Set the contents of a section. To do this we need to know where
1516 the section is going to be located in the output file. That means
1517 that the sizes of all the sections must be set, and all the
1518 variable size header information must be known. */
1521 nlm_set_section_contents (abfd, section, location, offset, count)
1526 bfd_size_type count;
1528 if (abfd->output_has_begun == false
1529 && nlm_compute_section_file_positions (abfd) == false)
1535 /* i386 NetWare has a very restricted set of relocs. In order for
1536 objcopy to work, the NLM i386 backend needs a chance to rework
1537 the section contents so that its set of relocs will work. If all
1538 the relocs are already acceptable, this will not do anything. */
1539 if (section->reloc_count != 0)
1541 boolean (*mangle_relocs_func) PARAMS ((bfd *, asection *, PTR,
1542 bfd_vma, bfd_size_type));
1544 mangle_relocs_func = nlm_mangle_relocs_func (abfd);
1545 if (mangle_relocs_func != NULL)
1547 if (! (*mangle_relocs_func) (abfd, section, location,
1548 (bfd_vma) offset, count))
1553 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
1554 || bfd_write (location, 1, count, abfd) != count)
1556 bfd_set_error (bfd_error_system_call);
1563 /* We need to sort a list of relocs associated with sections when we
1564 write out the external relocs. */
1567 nlm_external_reloc_compare (p1, p2)
1571 const struct reloc_and_sec *r1 = (const struct reloc_and_sec *) p1;
1572 const struct reloc_and_sec *r2 = (const struct reloc_and_sec *) p2;
1575 cmp = strcmp ((*r1->rel->sym_ptr_ptr)->name,
1576 (*r2->rel->sym_ptr_ptr)->name);
1580 /* We sort by address within symbol to make the sort more stable and
1581 increase the chances that different hosts will generate bit for
1582 bit equivalent results. */
1583 return (int) (r1->rel->address - r2->rel->address);
1586 /* Write out an NLM file. We write out the information in this order:
1592 other sections (custom data, messages, help, shared NLM, RPC,
1593 module dependencies)
1595 external references (imports)
1596 public symbols (exports)
1598 This is similar to the order used by the NetWare tools; the
1599 difference is that NetWare puts the sections other than code, data
1600 and custom data at the end of the NLM. It is convenient for us to
1601 know where the sections are going to be before worrying about the
1602 size of the other information.
1604 By the time this function is called, all the section data should
1605 have been output using set_section_contents. Note that custom
1606 data, the message file, the help file, the shared NLM file, the RPC
1607 data, and the module dependencies are all considered to be
1608 sections; the caller is responsible for filling in the offset and
1609 length fields in the NLM headers. The relocation fixups and
1610 imports are both obtained from the list of relocs attached to each
1611 section. The exports and debugging records are obtained from the
1612 list of outsymbols. */
1615 nlm_write_object_contents (abfd)
1619 boolean (*write_import_func) PARAMS ((bfd *, asection *, arelent *));
1620 bfd_size_type external_reloc_count, internal_reloc_count, i, c;
1621 struct reloc_and_sec *external_relocs;
1622 asymbol **sym_ptr_ptr;
1624 boolean (*write_prefix_func) PARAMS ((bfd *));
1625 unsigned char *fixed_header = NULL;
1627 fixed_header = (unsigned char *) malloc (nlm_fixed_header_size (abfd));
1628 if (fixed_header == NULL)
1630 bfd_set_error (bfd_error_no_memory);
1634 if (abfd->output_has_begun == false
1635 && nlm_compute_section_file_positions (abfd) == false)
1638 /* Write out the variable length headers. */
1640 nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd),
1643 bfd_set_error (bfd_error_system_call);
1646 if (nlm_swap_variable_header_out (abfd) == false
1647 || nlm_swap_auxiliary_headers_out (abfd) == false)
1649 bfd_set_error (bfd_error_system_call);
1653 /* A weak check on whether the section file positions were
1655 if (bfd_tell (abfd) > nlm_fixed_header (abfd)->codeImageOffset)
1657 bfd_set_error (bfd_error_invalid_operation);
1661 /* Advance to the relocs. */
1662 if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
1665 bfd_set_error (bfd_error_system_call);
1669 /* The format of the relocation entries is dependent upon the
1670 particular target. We use an external routine to write the reloc
1672 write_import_func = nlm_write_import_func (abfd);
1674 /* Write out the internal relocation fixups. While we're looping
1675 over the relocs, we also count the external relocs, which is
1676 needed when they are written out below. */
1677 internal_reloc_count = 0;
1678 external_reloc_count = 0;
1679 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
1681 arelent **rel_ptr_ptr, **rel_end;
1683 if (sec->reloc_count == 0)
1686 /* We can only represent relocs within a code or data
1687 section. We ignore them for a debugging section. */
1688 if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
1691 /* We need to know how to write out imports */
1692 if (write_import_func == NULL)
1694 bfd_set_error (bfd_error_invalid_operation);
1698 rel_ptr_ptr = sec->orelocation;
1699 rel_end = rel_ptr_ptr + sec->reloc_count;
1700 for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
1706 sym = *rel->sym_ptr_ptr;
1708 if (bfd_get_section (sym) != &bfd_und_section)
1710 ++internal_reloc_count;
1711 if ((*write_import_func) (abfd, sec, rel) == false)
1715 ++external_reloc_count;
1718 nlm_fixed_header (abfd)->numberOfRelocationFixups = internal_reloc_count;
1720 /* Write out the imports (relocs against external symbols). These
1721 are output as a symbol name followed by all the relocs for that
1722 symbol, so we must first gather together all the relocs against
1723 external symbols and sort them. */
1725 (struct reloc_and_sec *) bfd_alloc (abfd,
1726 (external_reloc_count
1727 * sizeof (struct reloc_and_sec)));
1728 if (external_relocs == (struct reloc_and_sec *) NULL)
1730 bfd_set_error (bfd_error_no_memory);
1734 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
1736 arelent **rel_ptr_ptr, **rel_end;
1738 if (sec->reloc_count == 0)
1741 rel_ptr_ptr = sec->orelocation;
1742 rel_end = rel_ptr_ptr + sec->reloc_count;
1743 for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
1749 sym = *rel->sym_ptr_ptr;
1751 if (bfd_get_section (sym) != &bfd_und_section)
1754 external_relocs[i].rel = rel;
1755 external_relocs[i].sec = sec;
1760 BFD_ASSERT (i == external_reloc_count);
1762 /* Sort the external relocs by name. */
1763 qsort ((PTR) external_relocs, (size_t) external_reloc_count,
1764 sizeof (struct reloc_and_sec), nlm_external_reloc_compare);
1766 /* Write out the external relocs. */
1767 nlm_fixed_header (abfd)->externalReferencesOffset = bfd_tell (abfd);
1770 while (i < external_reloc_count)
1774 bfd_size_type j, cnt;
1778 rel = external_relocs[i].rel;
1779 sym = *rel->sym_ptr_ptr;
1783 (j < external_reloc_count
1784 && *external_relocs[j].rel->sym_ptr_ptr == sym);
1788 if ((*nlm_write_external_func (abfd)) (abfd, cnt, sym,
1789 &external_relocs[i])
1796 nlm_fixed_header (abfd)->numberOfExternalReferences = c;
1798 /* Write out the public symbols (exports). */
1799 sym_ptr_ptr = bfd_get_outsymbols (abfd);
1800 if (sym_ptr_ptr != (asymbol **) NULL)
1802 bfd_vma (*get_public_offset_func) PARAMS ((bfd *, asymbol *));
1803 boolean (*write_export_func) PARAMS ((bfd*, asymbol *, bfd_vma));
1807 nlm_fixed_header (abfd)->publicsOffset = bfd_tell (abfd);
1808 get_public_offset_func = nlm_get_public_offset_func (abfd);
1809 write_export_func = nlm_write_export_func (abfd);
1811 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1812 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1817 bfd_byte temp[NLM_TARGET_LONG_SIZE];
1821 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) == 0
1822 || bfd_get_section (sym) == &bfd_und_section)
1827 if (get_public_offset_func)
1829 /* Most backends can use the code below, but
1830 unfortunately some use a different scheme. */
1831 offset = (*get_public_offset_func) (abfd, sym);
1835 offset = bfd_asymbol_value (sym);
1837 if (sec->flags & SEC_CODE)
1839 offset -= nlm_get_text_low (abfd);
1840 offset |= NLM_HIBIT;
1842 else if (sec->flags & (SEC_DATA | SEC_ALLOC))
1844 /* SEC_ALLOC is for the .bss section. */
1845 offset -= nlm_get_data_low (abfd);
1849 /* We can't handle an exported symbol that is not in
1850 the code or data segment. */
1851 bfd_set_error (bfd_error_invalid_operation);
1856 if (write_export_func)
1858 if ((*write_export_func) (abfd, sym, offset) == false)
1863 len = strlen (sym->name);
1864 if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd)
1865 != sizeof (bfd_byte))
1866 || bfd_write (sym->name, len, 1, abfd) != len)
1868 bfd_set_error (bfd_error_system_call);
1872 put_word (abfd, offset, temp);
1873 if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
1875 bfd_set_error (bfd_error_system_call);
1880 nlm_fixed_header (abfd)->numberOfPublics = c;
1882 /* Write out the debugging records. The NLM conversion program
1883 wants to be able to inhibit this, so as a special hack if
1884 debugInfoOffset is set to -1 we don't write any debugging
1885 information. This can not be handled by fiddling with the
1886 symbol table, because exported symbols appear in both the
1887 exported symbol list and the debugging information. */
1888 if (nlm_fixed_header (abfd)->debugInfoOffset == (file_ptr) -1)
1890 nlm_fixed_header (abfd)->debugInfoOffset = 0;
1891 nlm_fixed_header (abfd)->numberOfDebugRecords = 0;
1895 nlm_fixed_header (abfd)->debugInfoOffset = bfd_tell (abfd);
1897 sym_ptr_ptr = bfd_get_outsymbols (abfd);
1898 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1899 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1904 bfd_byte temp[NLM_TARGET_LONG_SIZE];
1908 /* The NLM notion of a debugging symbol is actually what
1909 BFD calls a local or global symbol. What BFD calls a
1910 debugging symbol NLM does not understand at all. */
1911 if ((sym->flags & (BSF_LOCAL | BSF_GLOBAL | BSF_EXPORT)) == 0
1912 || (sym->flags & BSF_DEBUGGING) != 0
1913 || bfd_get_section (sym) == &bfd_und_section)
1918 offset = bfd_asymbol_value (sym);
1920 if (sec->flags & SEC_CODE)
1922 offset -= nlm_get_text_low (abfd);
1925 else if (sec->flags & (SEC_DATA | SEC_ALLOC))
1927 /* SEC_ALLOC is for the .bss section. */
1928 offset -= nlm_get_data_low (abfd);
1934 /* The type is 0 for data, 1 for code, 2 for absolute. */
1935 if (bfd_write (&type, sizeof (bfd_byte), 1, abfd)
1936 != sizeof (bfd_byte))
1938 bfd_set_error (bfd_error_system_call);
1942 put_word (abfd, offset, temp);
1943 if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
1945 bfd_set_error (bfd_error_system_call);
1949 len = strlen (sym->name);
1950 if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd)
1951 != sizeof (bfd_byte))
1952 || bfd_write (sym->name, len, 1, abfd) != len)
1954 bfd_set_error (bfd_error_system_call);
1958 nlm_fixed_header (abfd)->numberOfDebugRecords = c;
1962 /* NLMLINK fills in offset values even if there is no data, so we do
1964 last = bfd_tell (abfd);
1965 if (nlm_fixed_header (abfd)->codeImageOffset == 0)
1966 nlm_fixed_header (abfd)->codeImageOffset = last;
1967 if (nlm_fixed_header (abfd)->dataImageOffset == 0)
1968 nlm_fixed_header (abfd)->dataImageOffset = last;
1969 if (nlm_fixed_header (abfd)->customDataOffset == 0)
1970 nlm_fixed_header (abfd)->customDataOffset = last;
1971 if (nlm_fixed_header (abfd)->moduleDependencyOffset == 0)
1972 nlm_fixed_header (abfd)->moduleDependencyOffset = last;
1973 if (nlm_fixed_header (abfd)->relocationFixupOffset == 0)
1974 nlm_fixed_header (abfd)->relocationFixupOffset = last;
1975 if (nlm_fixed_header (abfd)->externalReferencesOffset == 0)
1976 nlm_fixed_header (abfd)->externalReferencesOffset = last;
1977 if (nlm_fixed_header (abfd)->publicsOffset == 0)
1978 nlm_fixed_header (abfd)->publicsOffset = last;
1979 if (nlm_fixed_header (abfd)->debugInfoOffset == 0)
1980 nlm_fixed_header (abfd)->debugInfoOffset = last;
1982 /* At this point everything has been written out except the fixed
1984 memcpy (nlm_fixed_header (abfd)->signature, nlm_signature (abfd),
1985 NLM_SIGNATURE_SIZE);
1986 nlm_fixed_header (abfd)->version = NLM_HEADER_VERSION;
1987 nlm_fixed_header (abfd)->codeStartOffset =
1988 (bfd_get_start_address (abfd)
1989 - nlm_get_text_low (abfd));
1991 /* We have no convenient way for the caller to pass in the exit
1992 procedure or the check unload procedure, so the caller must set
1993 the values in the header to the values of the symbols. */
1994 nlm_fixed_header (abfd)->exitProcedureOffset -= nlm_get_text_low (abfd);
1995 if (nlm_fixed_header (abfd)->checkUnloadProcedureOffset != 0)
1996 nlm_fixed_header (abfd)->checkUnloadProcedureOffset -=
1997 nlm_get_text_low (abfd);
1999 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
2002 write_prefix_func = nlm_write_prefix_func (abfd);
2003 if (write_prefix_func)
2005 if ((*write_prefix_func) (abfd) == false)
2009 BFD_ASSERT (bfd_tell (abfd) == nlm_optional_prefix_size (abfd));
2011 nlm_swap_fixed_header_out (abfd, nlm_fixed_header (abfd), fixed_header);
2012 if (bfd_write (fixed_header, nlm_fixed_header_size (abfd), 1, abfd)
2013 != nlm_fixed_header_size (abfd))
2015 bfd_set_error (bfd_error_system_call);
2019 if (fixed_header != NULL)
2020 free (fixed_header);
2024 if (fixed_header != NULL)
2025 free (fixed_header);