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)
41 #define Nlm_External_Cygnus_Section_Header \
42 NlmNAME(External_Cygnus_Section_Header)
44 #define nlm_symbol_type nlmNAME(symbol_type)
45 #define nlm_get_symtab_upper_bound nlmNAME(get_symtab_upper_bound)
46 #define nlm_get_symtab nlmNAME(get_symtab)
47 #define nlm_make_empty_symbol nlmNAME(make_empty_symbol)
48 #define nlm_print_symbol nlmNAME(print_symbol)
49 #define nlm_get_symbol_info nlmNAME(get_symbol_info)
50 #define nlm_get_reloc_upper_bound nlmNAME(get_reloc_upper_bound)
51 #define nlm_canonicalize_reloc nlmNAME(canonicalize_reloc)
52 #define nlm_object_p nlmNAME(object_p)
53 #define nlm_set_section_contents nlmNAME(set_section_contents)
54 #define nlm_write_object_contents nlmNAME(write_object_contents)
56 #define nlm_swap_fixed_header_in(abfd,src,dst) \
57 (nlm_swap_fixed_header_in_func(abfd))(abfd,src,dst)
58 #define nlm_swap_fixed_header_out(abfd,src,dst) \
59 (nlm_swap_fixed_header_out_func(abfd))(abfd,src,dst)
61 /* Forward declarations of static functions */
63 static boolean add_bfd_section
64 PARAMS ((bfd *, char *, file_ptr, bfd_size_type, flagword));
65 static boolean nlm_swap_variable_header_in
67 static boolean nlm_swap_variable_header_out
69 static boolean find_nonzero
70 PARAMS ((PTR, size_t));
71 static boolean nlm_swap_auxiliary_headers_in
73 static boolean nlm_swap_auxiliary_headers_out
75 static boolean nlm_slurp_symbol_table
77 static boolean nlm_slurp_reloc_fixups
79 static boolean nlm_compute_section_file_positions
81 static int nlm_external_reloc_compare
82 PARAMS ((const void *, const void *));
84 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
85 can be handled by explicitly specifying 32 bits or "the long type". */
87 #define put_word bfd_h_put_64
88 #define get_word bfd_h_get_64
91 #define put_word bfd_h_put_32
92 #define get_word bfd_h_get_32
99 struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd);
100 boolean (*backend_object_p) PARAMS ((bfd *));
102 Nlm_Internal_Fixed_Header *i_fxdhdrp;
103 struct nlm_obj_tdata *new_tdata = NULL;
104 const char *signature;
105 enum bfd_architecture arch;
107 /* Some NLM formats have a prefix before the standard NLM fixed
109 backend_object_p = nlm_backend_object_p_func (abfd);
110 if (backend_object_p)
112 if (!(*backend_object_p) (abfd))
113 goto got_wrong_format_error;
116 /* Read in the fixed length portion of the NLM header in external format. */
118 x_fxdhdr = (PTR) malloc (nlm_fixed_header_size (abfd));
119 if (x_fxdhdr == NULL)
121 bfd_set_error (bfd_error_no_memory);
125 if (bfd_read ((PTR) x_fxdhdr, nlm_fixed_header_size (abfd), 1, abfd) !=
126 nlm_fixed_header_size (abfd))
128 if (bfd_get_error () != bfd_error_system_call)
129 goto got_wrong_format_error;
134 /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
135 the tdata pointer in the bfd. */
137 new_tdata = ((struct nlm_obj_tdata *)
138 bfd_zalloc (abfd, sizeof (struct nlm_obj_tdata)));
139 if (new_tdata == NULL)
141 bfd_set_error (bfd_error_no_memory);
145 nlm_tdata (abfd) = new_tdata;
147 i_fxdhdrp = nlm_fixed_header (abfd);
148 nlm_swap_fixed_header_in (abfd, x_fxdhdr, i_fxdhdrp);
152 /* Check to see if we have an NLM file for this backend by matching
153 the NLM signature. */
155 signature = nlm_signature (abfd);
156 if (signature != NULL
157 && *signature != '\0'
158 && strncmp ((char *) i_fxdhdrp->signature, signature,
159 NLM_SIGNATURE_SIZE) != 0)
160 goto got_wrong_format_error;
162 /* There's no supported way to discover the endianess of an NLM, so test for
163 a sane version number after doing byte swapping appropriate for this
164 XVEC. (Hack alert!) */
166 if (i_fxdhdrp->version > 0xFFFF)
167 goto got_wrong_format_error;
169 /* There's no supported way to check for 32 bit versus 64 bit addresses,
170 so ignore this distinction for now. (FIXME) */
172 /* Swap in the rest of the required header. */
173 if (!nlm_swap_variable_header_in (abfd))
175 if (bfd_get_error () != bfd_error_system_call)
176 goto got_wrong_format_error;
181 /* Add the sections supplied by all NLM's, and then read in the
182 auxiliary headers. Reading the auxiliary headers may create
183 additional sections described in the cygnus_sections header.
184 From this point on we assume that we have an NLM, and do not
185 treat errors as indicating the wrong format. */
187 if (!add_bfd_section (abfd, NLM_CODE_NAME,
188 i_fxdhdrp->codeImageOffset,
189 i_fxdhdrp->codeImageSize,
190 (SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
192 || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
193 i_fxdhdrp->dataImageOffset,
194 i_fxdhdrp->dataImageSize,
195 (SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
197 || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
199 i_fxdhdrp->uninitializedDataSize,
203 if (!nlm_swap_auxiliary_headers_in (abfd))
206 if (nlm_fixed_header (abfd)->numberOfRelocationFixups != 0
207 || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
208 abfd->flags |= HAS_RELOC;
209 if (nlm_fixed_header (abfd)->numberOfPublics != 0
210 || nlm_fixed_header (abfd)->numberOfDebugRecords != 0
211 || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
212 abfd->flags |= HAS_SYMS;
214 arch = nlm_architecture (abfd);
215 if (arch != bfd_arch_unknown)
216 bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
220 got_wrong_format_error:
221 bfd_set_error (bfd_error_wrong_format);
223 nlm_tdata (abfd) = preserved_tdata;
224 if (new_tdata != NULL)
225 bfd_release (abfd, new_tdata);
226 if (x_fxdhdr != NULL)
231 /* Add a section to the bfd. */
234 add_bfd_section (abfd, name, offset, size, flags)
243 newsect = bfd_make_section (abfd, name);
248 newsect->vma = 0; /* NLM's are relocatable. */
249 newsect->_raw_size = size;
250 newsect->filepos = offset;
251 newsect->flags = flags;
252 newsect->alignment_power = bfd_log2 (0); /* FIXME */
256 /* Read and swap in the variable length header. All the fields must
257 exist in the NLM, and must exist in the order they are read here. */
260 nlm_swap_variable_header_in (abfd)
263 unsigned char temp[NLM_TARGET_LONG_SIZE];
265 /* Read the description length and text members. */
267 if (bfd_read ((PTR) & nlm_variable_header (abfd)->descriptionLength,
268 sizeof (nlm_variable_header (abfd)->descriptionLength),
270 sizeof (nlm_variable_header (abfd)->descriptionLength))
272 if (bfd_read ((PTR) nlm_variable_header (abfd)->descriptionText,
273 nlm_variable_header (abfd)->descriptionLength + 1,
275 nlm_variable_header (abfd)->descriptionLength + 1)
278 /* Read and convert the stackSize field. */
280 if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
282 nlm_variable_header (abfd)->stackSize = get_word (abfd, (bfd_byte *) temp);
284 /* Read and convert the reserved field. */
286 if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
288 nlm_variable_header (abfd)->reserved = get_word (abfd, (bfd_byte *) temp);
290 /* Read the oldThreadName field. This field is a fixed length string. */
292 if (bfd_read ((PTR) nlm_variable_header (abfd)->oldThreadName,
293 sizeof (nlm_variable_header (abfd)->oldThreadName),
295 sizeof (nlm_variable_header (abfd)->oldThreadName))
298 /* Read the screen name length and text members. */
300 if (bfd_read ((PTR) & nlm_variable_header (abfd)->screenNameLength,
301 sizeof (nlm_variable_header (abfd)->screenNameLength),
303 sizeof (nlm_variable_header (abfd)->screenNameLength))
305 if (bfd_read ((PTR) nlm_variable_header (abfd)->screenName,
306 nlm_variable_header (abfd)->screenNameLength + 1,
308 nlm_variable_header (abfd)->screenNameLength + 1)
311 /* Read the thread name length and text members. */
313 if (bfd_read ((PTR) & nlm_variable_header (abfd)->threadNameLength,
314 sizeof (nlm_variable_header (abfd)->threadNameLength),
316 sizeof (nlm_variable_header (abfd)->threadNameLength))
318 if (bfd_read ((PTR) nlm_variable_header (abfd)->threadName,
319 nlm_variable_header (abfd)->threadNameLength + 1,
321 nlm_variable_header (abfd)->threadNameLength + 1)
326 /* Swap and write out the variable length header. All the fields must
327 exist in the NLM, and must exist in this order. */
330 nlm_swap_variable_header_out (abfd)
333 unsigned char temp[NLM_TARGET_LONG_SIZE];
335 /* Write the description length and text members. */
337 if (bfd_write ((PTR) & nlm_variable_header (abfd)->descriptionLength,
338 sizeof (nlm_variable_header (abfd)->descriptionLength),
340 sizeof (nlm_variable_header (abfd)->descriptionLength))
342 if (bfd_write ((PTR) nlm_variable_header (abfd)->descriptionText,
343 nlm_variable_header (abfd)->descriptionLength + 1,
345 nlm_variable_header (abfd)->descriptionLength + 1)
348 /* Convert and write the stackSize field. */
350 put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->stackSize,
352 if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
355 /* Convert and write the reserved field. */
357 put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->reserved,
359 if (bfd_write ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
362 /* Write the oldThreadName field. This field is a fixed length string. */
364 if (bfd_write ((PTR) nlm_variable_header (abfd)->oldThreadName,
365 sizeof (nlm_variable_header (abfd)->oldThreadName),
367 sizeof (nlm_variable_header (abfd)->oldThreadName))
370 /* Write the screen name length and text members. */
372 if (bfd_write ((PTR) & nlm_variable_header (abfd)->screenNameLength,
373 sizeof (nlm_variable_header (abfd)->screenNameLength),
375 sizeof (nlm_variable_header (abfd)->screenNameLength))
377 if (bfd_write ((PTR) nlm_variable_header (abfd)->screenName,
378 nlm_variable_header (abfd)->screenNameLength + 1,
380 nlm_variable_header (abfd)->screenNameLength + 1)
383 /* Write the thread name length and text members. */
385 if (bfd_write ((PTR) & nlm_variable_header (abfd)->threadNameLength,
386 sizeof (nlm_variable_header (abfd)->threadNameLength),
388 sizeof (nlm_variable_header (abfd)->threadNameLength))
390 if (bfd_write ((PTR) nlm_variable_header (abfd)->threadName,
391 nlm_variable_header (abfd)->threadNameLength + 1,
393 nlm_variable_header (abfd)->threadNameLength + 1)
398 /* Read and swap in the contents of all the auxiliary headers. Because of
399 the braindead design, we have to do strcmps on strings of indeterminate
400 length to figure out what each auxiliary header is. Even worse, we have
401 no way of knowing how many auxiliary headers there are or where the end
402 of the auxiliary headers are, except by finding something that doesn't
403 look like a known auxiliary header. This means that the first new type
404 of auxiliary header added will break all existing tools that don't
408 nlm_swap_auxiliary_headers_in (abfd)
416 position = bfd_tell (abfd);
417 if (bfd_read ((PTR) tempstr, sizeof (tempstr), 1, abfd) !=
420 if (bfd_seek (abfd, position, SEEK_SET) == -1)
422 if (strncmp (tempstr, "VeRsIoN#", 8) == 0)
424 Nlm_External_Version_Header thdr;
425 if (bfd_read ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
427 memcpy (nlm_version_header (abfd)->stamp, thdr.stamp,
428 sizeof (thdr.stamp));
429 nlm_version_header (abfd)->majorVersion =
430 get_word (abfd, (bfd_byte *) thdr.majorVersion);
431 nlm_version_header (abfd)->minorVersion =
432 get_word (abfd, (bfd_byte *) thdr.minorVersion);
433 nlm_version_header (abfd)->revision =
434 get_word (abfd, (bfd_byte *) thdr.revision);
435 nlm_version_header (abfd)->year =
436 get_word (abfd, (bfd_byte *) thdr.year);
437 nlm_version_header (abfd)->month =
438 get_word (abfd, (bfd_byte *) thdr.month);
439 nlm_version_header (abfd)->day =
440 get_word (abfd, (bfd_byte *) thdr.day);
442 else if (strncmp (tempstr, "MeSsAgEs", 8) == 0)
444 Nlm_External_Extended_Header thdr;
445 if (bfd_read ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
447 memcpy (nlm_extended_header (abfd)->stamp, thdr.stamp,
448 sizeof (thdr.stamp));
449 nlm_extended_header (abfd)->languageID =
450 get_word (abfd, (bfd_byte *) thdr.languageID);
451 nlm_extended_header (abfd)->messageFileOffset =
452 get_word (abfd, (bfd_byte *) thdr.messageFileOffset);
453 nlm_extended_header (abfd)->messageFileLength =
454 get_word (abfd, (bfd_byte *) thdr.messageFileLength);
455 nlm_extended_header (abfd)->messageCount =
456 get_word (abfd, (bfd_byte *) thdr.messageCount);
457 nlm_extended_header (abfd)->helpFileOffset =
458 get_word (abfd, (bfd_byte *) thdr.helpFileOffset);
459 nlm_extended_header (abfd)->helpFileLength =
460 get_word (abfd, (bfd_byte *) thdr.helpFileLength);
461 nlm_extended_header (abfd)->RPCDataOffset =
462 get_word (abfd, (bfd_byte *) thdr.RPCDataOffset);
463 nlm_extended_header (abfd)->RPCDataLength =
464 get_word (abfd, (bfd_byte *) thdr.RPCDataLength);
465 nlm_extended_header (abfd)->sharedCodeOffset =
466 get_word (abfd, (bfd_byte *) thdr.sharedCodeOffset);
467 nlm_extended_header (abfd)->sharedCodeLength =
468 get_word (abfd, (bfd_byte *) thdr.sharedCodeLength);
469 nlm_extended_header (abfd)->sharedDataOffset =
470 get_word (abfd, (bfd_byte *) thdr.sharedDataOffset);
471 nlm_extended_header (abfd)->sharedDataLength =
472 get_word (abfd, (bfd_byte *) thdr.sharedDataLength);
473 nlm_extended_header (abfd)->sharedRelocationFixupOffset =
474 get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupOffset);
475 nlm_extended_header (abfd)->sharedRelocationFixupCount =
476 get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupCount);
477 nlm_extended_header (abfd)->sharedExternalReferenceOffset =
478 get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceOffset);
479 nlm_extended_header (abfd)->sharedExternalReferenceCount =
480 get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceCount);
481 nlm_extended_header (abfd)->sharedPublicsOffset =
482 get_word (abfd, (bfd_byte *) thdr.sharedPublicsOffset);
483 nlm_extended_header (abfd)->sharedPublicsCount =
484 get_word (abfd, (bfd_byte *) thdr.sharedPublicsCount);
485 nlm_extended_header (abfd)->sharedDebugRecordOffset =
486 get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordOffset);
487 nlm_extended_header (abfd)->sharedDebugRecordCount =
488 get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordCount);
489 nlm_extended_header (abfd)->SharedInitializationOffset =
490 get_word (abfd, (bfd_byte *) thdr.sharedInitializationOffset);
491 nlm_extended_header (abfd)->SharedExitProcedureOffset =
492 get_word (abfd, (bfd_byte *) thdr.SharedExitProcedureOffset);
493 nlm_extended_header (abfd)->productID =
494 get_word (abfd, (bfd_byte *) thdr.productID);
495 nlm_extended_header (abfd)->reserved0 =
496 get_word (abfd, (bfd_byte *) thdr.reserved0);
497 nlm_extended_header (abfd)->reserved1 =
498 get_word (abfd, (bfd_byte *) thdr.reserved1);
499 nlm_extended_header (abfd)->reserved2 =
500 get_word (abfd, (bfd_byte *) thdr.reserved2);
501 nlm_extended_header (abfd)->reserved3 =
502 get_word (abfd, (bfd_byte *) thdr.reserved3);
503 nlm_extended_header (abfd)->reserved4 =
504 get_word (abfd, (bfd_byte *) thdr.reserved4);
505 nlm_extended_header (abfd)->reserved5 =
506 get_word (abfd, (bfd_byte *) thdr.reserved5);
508 else if (strncmp (tempstr, "CuStHeAd", 8) == 0)
510 Nlm_External_Custom_Header thdr;
511 if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
513 memcpy (nlm_custom_header (abfd)->stamp, thdr.stamp,
514 sizeof (thdr.stamp));
515 nlm_custom_header (abfd)->dataLength =
516 get_word (abfd, (bfd_byte *) thdr.dataLength);
517 nlm_custom_header (abfd)->data =
518 bfd_alloc (abfd, nlm_custom_header (abfd)->dataLength);
519 if (nlm_custom_header (abfd)->data == NULL)
521 if (bfd_read (nlm_custom_header (abfd)->data, 1,
522 nlm_custom_header (abfd)->dataLength, abfd)
523 != nlm_custom_header (abfd)->dataLength)
526 else if (strncmp (tempstr, "CoPyRiGhT=", 10) == 0)
528 if (bfd_read ((PTR) nlm_copyright_header (abfd)->stamp,
529 sizeof (nlm_copyright_header (abfd)->stamp),
531 != sizeof (nlm_copyright_header (abfd)->stamp))
533 if (bfd_read ((PTR) & (nlm_copyright_header (abfd)
534 ->copyrightMessageLength),
537 /* The copyright message is a variable length string. */
538 if (bfd_read ((PTR) nlm_copyright_header (abfd)->copyrightMessage,
539 nlm_copyright_header (abfd)->copyrightMessageLength + 1,
541 nlm_copyright_header (abfd)->copyrightMessageLength + 1)
544 else if (strncmp (tempstr, "CyGnUsSeCs", 10) == 0)
546 Nlm_External_Cygnus_Section_Header thdr;
552 if (bfd_read ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
554 memcpy (nlm_cygnus_section_header (abfd)->stamp, thdr.stamp,
555 sizeof (thdr.stamp));
556 nlm_cygnus_section_header (abfd)->offset =
557 get_word (abfd, (bfd_byte *) thdr.offset);
558 len = get_word (abfd, (bfd_byte *) thdr.length);
559 nlm_cygnus_section_header (abfd)->length = len;
561 /* This data this header points to provides a list of the
562 sections which were in the original object file which was
563 converted to become an NLM. We locate those sections and
564 add them to the BFD. Note that this is likely to create
565 a second .text, .data and .bss section; retrieving the
566 sections by name will get the actual NLM sections, which
567 is what we want to happen. The sections from the
568 original file, which may be subsets of the NLM section,
569 can only be found using bfd_map_over_sections. */
571 contents = (bfd_byte *) bfd_alloc (abfd, len);
572 if (contents == (bfd_byte *) NULL)
574 bfd_set_error (bfd_error_no_memory);
577 pos = bfd_tell (abfd);
578 if (bfd_seek (abfd, nlm_cygnus_section_header (abfd)->offset,
580 || bfd_read (contents, len, 1, abfd) != len)
592 /* The format of this information is
593 null terminated section name
594 zeroes to adjust to 4 byte boundary
595 4 byte section data file pointer
600 l = strlen (name) + 1;
603 filepos = bfd_h_get_32 (abfd, p);
605 size = bfd_h_get_32 (abfd, p);
608 newsec = bfd_make_section_anyway (abfd, name);
609 if (newsec == (asection *) NULL)
611 newsec->_raw_size = size;
614 newsec->filepos = filepos;
615 newsec->flags |= SEC_HAS_CONTENTS;
619 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
630 /* Return whether there is a non-zero byte in a memory block. */
633 find_nonzero (buf, size)
637 char *p = (char *) buf;
645 /* Swap out the contents of the auxiliary headers. We create those
646 auxiliary headers which have been set non-zero. We do not require
647 the caller to set up the stamp fields. */
650 nlm_swap_auxiliary_headers_out (abfd)
653 /* Write out the version header if there is one. */
654 if (find_nonzero ((PTR) nlm_version_header (abfd),
655 sizeof (Nlm_Internal_Version_Header)))
657 Nlm_External_Version_Header thdr;
659 memcpy (thdr.stamp, "VeRsIoN#", 8);
660 put_word (abfd, (bfd_vma) nlm_version_header (abfd)->majorVersion,
661 (bfd_byte *) thdr.majorVersion);
662 put_word (abfd, (bfd_vma) nlm_version_header (abfd)->minorVersion,
663 (bfd_byte *) thdr.minorVersion);
664 put_word (abfd, (bfd_vma) nlm_version_header (abfd)->revision,
665 (bfd_byte *) thdr.revision);
666 put_word (abfd, (bfd_vma) nlm_version_header (abfd)->year,
667 (bfd_byte *) thdr.year);
668 put_word (abfd, (bfd_vma) nlm_version_header (abfd)->month,
669 (bfd_byte *) thdr.month);
670 put_word (abfd, (bfd_vma) nlm_version_header (abfd)->day,
671 (bfd_byte *) thdr.day);
672 if (bfd_write ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
676 /* Write out the extended header if there is one. */
677 if (find_nonzero ((PTR) nlm_extended_header (abfd),
678 sizeof (Nlm_Internal_Extended_Header)))
680 Nlm_External_Extended_Header thdr;
682 memcpy (thdr.stamp, "MeSsAgEs", 8);
684 (bfd_vma) nlm_extended_header (abfd)->languageID,
685 (bfd_byte *) thdr.languageID);
687 (bfd_vma) nlm_extended_header (abfd)->messageFileOffset,
688 (bfd_byte *) thdr.messageFileOffset);
690 (bfd_vma) nlm_extended_header (abfd)->messageFileLength,
691 (bfd_byte *) thdr.messageFileLength);
693 (bfd_vma) nlm_extended_header (abfd)->messageCount,
694 (bfd_byte *) thdr.messageCount);
696 (bfd_vma) nlm_extended_header (abfd)->helpFileOffset,
697 (bfd_byte *) thdr.helpFileOffset);
699 (bfd_vma) nlm_extended_header (abfd)->helpFileLength,
700 (bfd_byte *) thdr.helpFileLength);
702 (bfd_vma) nlm_extended_header (abfd)->RPCDataOffset,
703 (bfd_byte *) thdr.RPCDataOffset);
705 (bfd_vma) nlm_extended_header (abfd)->RPCDataLength,
706 (bfd_byte *) thdr.RPCDataLength);
708 (bfd_vma) nlm_extended_header (abfd)->sharedCodeOffset,
709 (bfd_byte *) thdr.sharedCodeOffset);
711 (bfd_vma) nlm_extended_header (abfd)->sharedCodeLength,
712 (bfd_byte *) thdr.sharedCodeLength);
714 (bfd_vma) nlm_extended_header (abfd)->sharedDataOffset,
715 (bfd_byte *) thdr.sharedDataOffset);
717 (bfd_vma) nlm_extended_header (abfd)->sharedDataLength,
718 (bfd_byte *) thdr.sharedDataLength);
720 (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupOffset,
721 (bfd_byte *) thdr.sharedRelocationFixupOffset);
723 (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupCount,
724 (bfd_byte *) thdr.sharedRelocationFixupCount);
726 (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceOffset,
727 (bfd_byte *) thdr.sharedExternalReferenceOffset);
729 (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceCount,
730 (bfd_byte *) thdr.sharedExternalReferenceCount);
732 (bfd_vma) nlm_extended_header (abfd)->sharedPublicsOffset,
733 (bfd_byte *) thdr.sharedPublicsOffset);
735 (bfd_vma) nlm_extended_header (abfd)->sharedPublicsCount,
736 (bfd_byte *) thdr.sharedPublicsCount);
738 (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordOffset,
739 (bfd_byte *) thdr.sharedDebugRecordOffset);
741 (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordCount,
742 (bfd_byte *) thdr.sharedDebugRecordCount);
744 (bfd_vma) nlm_extended_header (abfd)->SharedInitializationOffset,
745 (bfd_byte *) thdr.sharedInitializationOffset);
747 (bfd_vma) nlm_extended_header (abfd)->SharedExitProcedureOffset,
748 (bfd_byte *) thdr.SharedExitProcedureOffset);
750 (bfd_vma) nlm_extended_header (abfd)->productID,
751 (bfd_byte *) thdr.productID);
753 (bfd_vma) nlm_extended_header (abfd)->reserved0,
754 (bfd_byte *) thdr.reserved0);
756 (bfd_vma) nlm_extended_header (abfd)->reserved1,
757 (bfd_byte *) thdr.reserved1);
759 (bfd_vma) nlm_extended_header (abfd)->reserved2,
760 (bfd_byte *) thdr.reserved2);
762 (bfd_vma) nlm_extended_header (abfd)->reserved3,
763 (bfd_byte *) thdr.reserved3);
765 (bfd_vma) nlm_extended_header (abfd)->reserved4,
766 (bfd_byte *) thdr.reserved4);
768 (bfd_vma) nlm_extended_header (abfd)->reserved5,
769 (bfd_byte *) thdr.reserved5);
770 if (bfd_write ((PTR) & thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
774 /* Write out the custom header if there is one. */
775 if (find_nonzero ((PTR) nlm_custom_header (abfd),
776 sizeof (Nlm_Internal_Custom_Header)))
778 Nlm_External_Custom_Header thdr;
780 /* Right now we assume the custom header is always the suggested
781 format for alternate debugging records. */
782 BFD_ASSERT (nlm_custom_header (abfd)->dataLength == 8);
784 memcpy (thdr.stamp, "CuStHeAd", 8);
785 put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataLength,
786 (bfd_byte *) thdr.dataLength);
787 if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
789 if (bfd_write (nlm_custom_header (abfd)->data, 1,
790 nlm_custom_header (abfd)->dataLength, abfd)
791 != nlm_custom_header (abfd)->dataLength)
795 /* Write out the copyright header if there is one. */
796 if (find_nonzero ((PTR) nlm_copyright_header (abfd),
797 sizeof (Nlm_Internal_Copyright_Header)))
799 Nlm_External_Copyright_Header thdr;
801 memcpy (thdr.stamp, "CoPyRiGhT=", 10);
802 if (bfd_write ((PTR) thdr.stamp, sizeof (thdr.stamp), 1, abfd)
803 != sizeof (thdr.stamp))
805 thdr.copyrightMessageLength[0] =
806 nlm_copyright_header (abfd)->copyrightMessageLength;
807 if (bfd_write ((PTR) thdr.copyrightMessageLength, 1, 1, abfd) != 1)
809 /* The copyright message is a variable length string. */
810 if (bfd_write ((PTR) nlm_copyright_header (abfd)->copyrightMessage,
811 nlm_copyright_header (abfd)->copyrightMessageLength + 1,
813 nlm_copyright_header (abfd)->copyrightMessageLength + 1)
817 /* Write out the Cygnus debugging header if there is one. */
818 if (find_nonzero ((PTR) nlm_cygnus_section_header (abfd),
819 sizeof (Nlm_Internal_Cygnus_Section_Header)))
821 Nlm_External_Cygnus_Section_Header thdr;
823 memcpy (thdr.stamp, "CyGnUsSeCs", 10);
824 put_word (abfd, (bfd_vma) nlm_cygnus_section_header (abfd)->offset,
825 (bfd_byte *) thdr.offset);
826 put_word (abfd, (bfd_vma) nlm_cygnus_section_header (abfd)->length,
827 (bfd_byte *) thdr.length);
828 if (bfd_write ((PTR) &thdr, sizeof (thdr), 1, abfd) != sizeof (thdr))
835 /* We read the NLM's public symbols and use it to generate a bfd symbol
836 table (hey, it's better than nothing) on a one-for-one basis. Thus
837 use the number of public symbols as the number of bfd symbols we will
838 have once we actually get around to reading them in.
840 Return the number of bytes required to hold the symtab vector, based on
841 the count plus 1, since we will NULL terminate the vector allocated based
845 nlm_get_symtab_upper_bound (abfd)
848 Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form */
850 long symtab_size = 0;
852 i_fxdhdrp = nlm_fixed_header (abfd);
853 symcount = (i_fxdhdrp->numberOfPublics
854 + i_fxdhdrp->numberOfDebugRecords
855 + i_fxdhdrp->numberOfExternalReferences);
856 symtab_size = (symcount + 1) * (sizeof (asymbol));
857 return (symtab_size);
860 /* Note that bfd_get_symcount is guaranteed to be zero if slurping the
861 symbol table fails. */
864 nlm_get_symtab (abfd, alocation)
868 nlm_symbol_type *symbase;
869 bfd_size_type counter = 0;
871 if (nlm_slurp_symbol_table (abfd) == false)
873 symbase = nlm_get_symbols (abfd);
874 while (counter < bfd_get_symcount (abfd))
876 *alocation++ = &symbase->symbol;
880 *alocation = (asymbol *) NULL;
881 return bfd_get_symcount (abfd);
884 /* Make an NLM symbol. There is nothing special to do here. */
887 nlm_make_empty_symbol (abfd)
890 nlm_symbol_type *new;
892 new = (nlm_symbol_type *) bfd_zalloc (abfd, sizeof (nlm_symbol_type));
894 new->symbol.the_bfd = abfd;
898 /* Get symbol information. */
901 nlm_get_symbol_info (ignore_abfd, symbol, ret)
906 bfd_symbol_info (symbol, ret);
909 /* Print symbol information. */
912 nlm_print_symbol (abfd, afile, symbol, how)
916 bfd_print_symbol_type how;
918 FILE *file = (FILE *) afile;
922 case bfd_print_symbol_name:
923 case bfd_print_symbol_more:
925 fprintf (file, "%s", symbol->name);
927 case bfd_print_symbol_all:
928 bfd_print_symbol_vandf ((PTR) file, symbol);
929 fprintf (file, " %-5s", symbol->section->name);
931 fprintf (file, " %s", symbol->name);
936 /* Slurp in nlm symbol table.
938 In the external (in-file) form, NLM export records are variable length,
939 with the following form:
941 1 byte length of the symbol name (N)
942 N bytes the symbol name
943 4 bytes the symbol offset from start of it's section
945 We also read in the debugging symbols and import records. Import
946 records are treated as undefined symbols. As we read the import
947 records we also read in the associated reloc information, which is
948 attached to the symbol.
950 The bfd symbols are copied to SYMPTRS.
952 When we return, the bfd symcount is either zero or contains the correct
957 nlm_slurp_symbol_table (abfd)
960 Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form */
961 bfd_size_type totsymcount; /* Number of NLM symbols */
962 bfd_size_type symcount; /* Counter of NLM symbols */
963 nlm_symbol_type *sym; /* Pointer to current bfd symbol */
964 unsigned char symlength; /* Symbol length read into here */
965 unsigned char symtype; /* Type of debugging symbol */
966 bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* Symbol offsets read into here */
967 boolean (*read_import_func) PARAMS ((bfd *, nlm_symbol_type *));
968 boolean (*set_public_section_func) PARAMS ((bfd *, nlm_symbol_type *));
970 if (nlm_get_symbols (abfd) != NULL)
973 /* Read each raw NLM symbol, using the information to create a canonical bfd
976 Note that we allocate the initial bfd canonical symbol buffer based on a
977 one-to-one mapping of the NLM symbols to canonical symbols. We actually
978 use all the NLM symbols, so there will be no space left over at the end.
979 When we have all the symbols, we build the caller's pointer vector. */
982 i_fxdhdrp = nlm_fixed_header (abfd);
983 totsymcount = (i_fxdhdrp->numberOfPublics
984 + i_fxdhdrp->numberOfDebugRecords
985 + i_fxdhdrp->numberOfExternalReferences);
986 if (totsymcount == 0)
991 if (bfd_seek (abfd, i_fxdhdrp->publicsOffset, SEEK_SET) == -1)
994 sym = ((nlm_symbol_type *)
995 bfd_zalloc (abfd, totsymcount * sizeof (nlm_symbol_type)));
998 bfd_set_error (bfd_error_no_memory);
1001 nlm_set_symbols (abfd, sym);
1003 /* We use the bfd's symcount directly as the control count, so that early
1004 termination of the loop leaves the symcount correct for the symbols that
1007 set_public_section_func = nlm_set_public_section_func (abfd);
1008 symcount = i_fxdhdrp->numberOfPublics;
1009 while (abfd->symcount < symcount)
1011 if (bfd_read ((PTR) & symlength, sizeof (symlength), 1, abfd)
1012 != sizeof (symlength))
1014 sym->symbol.the_bfd = abfd;
1015 sym->symbol.name = bfd_alloc (abfd, symlength + 1);
1016 if (!sym->symbol.name)
1018 bfd_set_error (bfd_error_no_memory);
1021 if (bfd_read ((PTR) sym->symbol.name, symlength, 1, abfd)
1024 /* Cast away const. */
1025 ((char *) (sym->symbol.name))[symlength] = '\0';
1026 if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
1028 sym->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
1029 sym->symbol.value = get_word (abfd, temp);
1030 if (set_public_section_func)
1032 /* Most backends can use the code below, but unfortunately
1033 some use a different scheme. */
1034 if ((*set_public_section_func) (abfd, sym) == false)
1039 if (sym->symbol.value & NLM_HIBIT)
1041 sym->symbol.value &= ~NLM_HIBIT;
1042 sym->symbol.flags |= BSF_FUNCTION;
1043 sym->symbol.section =
1044 bfd_get_section_by_name (abfd, NLM_CODE_NAME);
1048 sym->symbol.section =
1049 bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
1057 /* Read the debugging records. */
1059 if (i_fxdhdrp->numberOfDebugRecords > 0)
1061 if (bfd_seek (abfd, i_fxdhdrp->debugInfoOffset, SEEK_SET) == -1)
1064 symcount += i_fxdhdrp->numberOfDebugRecords;
1065 while (abfd->symcount < symcount)
1067 if ((bfd_read ((PTR) & symtype, sizeof (symtype), 1, abfd)
1068 != sizeof (symtype))
1069 || bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp)
1070 || (bfd_read ((PTR) & symlength, sizeof (symlength), 1, abfd)
1071 != sizeof (symlength)))
1073 sym->symbol.the_bfd = abfd;
1074 sym->symbol.name = bfd_alloc (abfd, symlength + 1);
1075 if (!sym->symbol.name)
1077 bfd_set_error (bfd_error_no_memory);
1080 if (bfd_read ((PTR) sym->symbol.name, symlength, 1, abfd)
1083 /* Cast away const. */
1084 ((char *) (sym->symbol.name))[symlength] = '\0';
1085 sym->symbol.flags = BSF_LOCAL;
1086 sym->symbol.value = get_word (abfd, temp);
1089 sym->symbol.section =
1090 bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
1092 else if (symtype == 1)
1094 sym->symbol.flags |= BSF_FUNCTION;
1095 sym->symbol.section =
1096 bfd_get_section_by_name (abfd, NLM_CODE_NAME);
1100 sym->symbol.section = &bfd_abs_section;
1108 /* Read in the import records. We can only do this if we know how
1109 to read relocs for this target. */
1111 read_import_func = nlm_read_import_func (abfd);
1112 if (read_import_func != NULL)
1114 if (bfd_seek (abfd, i_fxdhdrp->externalReferencesOffset, SEEK_SET)
1118 symcount += i_fxdhdrp->numberOfExternalReferences;
1119 while (abfd->symcount < symcount)
1121 if ((*read_import_func) (abfd, sym) == false)
1131 /* Get the relocs for an NLM file. There are two types of relocs.
1132 Imports are relocs against symbols defined in other NLM files. We
1133 treat these as relocs against global symbols. Relocation fixups
1134 are internal relocs.
1136 The actual format used to store the relocs is machine specific. */
1138 /* Read in the relocation fixup information. This is stored in
1139 nlm_relocation_fixups, an array of arelent structures, and
1140 nlm_relocation_fixup_secs, an array of section pointers. The
1141 section pointers are needed because the relocs are not sorted by
1145 nlm_slurp_reloc_fixups (abfd)
1148 boolean (*read_func) PARAMS ((bfd *, nlm_symbol_type *, asection **,
1150 bfd_size_type count;
1154 if (nlm_relocation_fixups (abfd) != NULL)
1156 read_func = nlm_read_reloc_func (abfd);
1157 if (read_func == NULL)
1160 if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
1164 count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1165 rels = (arelent *) bfd_alloc (abfd, count * sizeof (arelent));
1166 secs = (asection **) bfd_alloc (abfd, count * sizeof (asection *));
1167 if ((rels == NULL || secs == NULL) && count != 0)
1169 bfd_set_error (bfd_error_no_memory);
1172 nlm_relocation_fixups (abfd) = rels;
1173 nlm_relocation_fixup_secs (abfd) = secs;
1175 /* We have to read piece by piece, because we don't know how large
1176 the machine specific reloc information is. */
1177 while (count-- != 0)
1179 if ((*read_func) (abfd, (nlm_symbol_type *) NULL, secs, rels) == false)
1181 nlm_relocation_fixups (abfd) = NULL;
1182 nlm_relocation_fixup_secs (abfd) = NULL;
1192 /* Get the number of relocs. This really just returns an upper bound,
1193 since it does not attempt to distinguish them based on the section.
1194 That will be handled when they are actually read. */
1197 nlm_get_reloc_upper_bound (abfd, sec)
1201 nlm_symbol_type *syms;
1202 bfd_size_type count;
1205 /* If we don't know how to read relocs, just return 0. */
1206 if (nlm_read_reloc_func (abfd) == NULL)
1208 /* Make sure we have either the code or the data section. */
1209 if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
1212 syms = nlm_get_symbols (abfd);
1215 if (nlm_slurp_symbol_table (abfd) == false)
1217 syms = nlm_get_symbols (abfd);
1220 ret = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1222 count = bfd_get_symcount (abfd);
1223 while (count-- != 0)
1229 return (ret + 1) * sizeof (arelent *);
1232 /* Get the relocs themselves. */
1235 nlm_canonicalize_reloc (abfd, sec, relptr, symbols)
1243 bfd_size_type count, i;
1246 /* Get the relocation fixups. */
1247 rels = nlm_relocation_fixups (abfd);
1250 if (nlm_slurp_reloc_fixups (abfd) == false)
1252 rels = nlm_relocation_fixups (abfd);
1254 secs = nlm_relocation_fixup_secs (abfd);
1257 count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1258 for (i = 0; i < count; i++, rels++, secs++)
1267 /* Get the import symbols. */
1268 count = bfd_get_symcount (abfd);
1269 for (i = 0; i < count; i++, symbols++)
1274 if (bfd_asymbol_flavour (sym) == bfd_target_nlm_flavour)
1276 nlm_symbol_type *nlm_sym;
1279 nlm_sym = (nlm_symbol_type *) sym;
1280 for (j = 0; j < nlm_sym->rcnt; j++)
1282 if (nlm_sym->relocs[j].section == sec)
1284 *relptr = &nlm_sym->relocs[j].reloc;
1285 (*relptr)->sym_ptr_ptr = symbols;
1298 /* Compute the section file positions for an NLM file. All variable
1299 length data in the file headers must be set before this function is
1300 called. If the variable length data is changed later, the
1301 resulting object file will be incorrect. Unfortunately, there is
1302 no way to check this.
1304 This routine also sets the Size and Offset fields in the fixed
1307 It also looks over the symbols and moves any common symbols into
1308 the .bss section; NLM has no way to represent a common symbol.
1309 This approach means that either the symbols must already have been
1310 set at this point, or there must be no common symbols. We need to
1311 move the symbols at this point so that mangle_relocs can see the
1315 nlm_compute_section_file_positions (abfd)
1320 bfd_vma text, data, bss;
1321 bfd_vma text_low, data_low;
1322 int text_align, data_align, other_align;
1323 file_ptr text_ptr, data_ptr, other_ptr;
1325 asymbol **sym_ptr_ptr;
1327 if (abfd->output_has_begun == true)
1330 /* Make sure we have a section to hold uninitialized data. */
1331 bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
1332 if (bss_sec == NULL)
1334 if (!add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
1335 (file_ptr) 0, (bfd_size_type) 0,
1338 bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
1341 abfd->output_has_begun = true;
1343 /* The fixed header. */
1344 sofar = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
1346 /* The variable header. */
1347 sofar += (sizeof (nlm_variable_header (abfd)->descriptionLength)
1348 + nlm_variable_header (abfd)->descriptionLength + 1
1349 + NLM_TARGET_LONG_SIZE /* stackSize */
1350 + NLM_TARGET_LONG_SIZE /* reserved */
1351 + sizeof (nlm_variable_header (abfd)->oldThreadName)
1352 + sizeof (nlm_variable_header (abfd)->screenNameLength)
1353 + nlm_variable_header (abfd)->screenNameLength + 1
1354 + sizeof (nlm_variable_header (abfd)->threadNameLength)
1355 + nlm_variable_header (abfd)->threadNameLength + 1);
1357 /* The auxiliary headers. */
1358 if (find_nonzero ((PTR) nlm_version_header (abfd),
1359 sizeof (Nlm_Internal_Version_Header)))
1360 sofar += sizeof (Nlm_External_Version_Header);
1361 if (find_nonzero ((PTR) nlm_extended_header (abfd),
1362 sizeof (Nlm_Internal_Extended_Header)))
1363 sofar += sizeof (Nlm_External_Extended_Header);
1364 if (find_nonzero ((PTR) nlm_custom_header (abfd),
1365 sizeof (Nlm_Internal_Custom_Header)))
1366 sofar += (sizeof (Nlm_External_Custom_Header)
1367 + nlm_custom_header (abfd)->dataLength);
1368 if (find_nonzero ((PTR) nlm_copyright_header (abfd),
1369 sizeof (Nlm_Internal_Copyright_Header)))
1370 sofar += (sizeof (Nlm_External_Copyright_Header)
1371 + nlm_copyright_header (abfd)->copyrightMessageLength + 1);
1372 if (find_nonzero ((PTR) nlm_cygnus_section_header (abfd),
1373 sizeof (Nlm_Internal_Cygnus_Section_Header)))
1374 sofar += sizeof (Nlm_External_Cygnus_Section_Header);
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)
1560 /* We need to sort a list of relocs associated with sections when we
1561 write out the external relocs. */
1564 nlm_external_reloc_compare (p1, p2)
1568 const struct reloc_and_sec *r1 = (const struct reloc_and_sec *) p1;
1569 const struct reloc_and_sec *r2 = (const struct reloc_and_sec *) p2;
1572 cmp = strcmp ((*r1->rel->sym_ptr_ptr)->name,
1573 (*r2->rel->sym_ptr_ptr)->name);
1577 /* We sort by address within symbol to make the sort more stable and
1578 increase the chances that different hosts will generate bit for
1579 bit equivalent results. */
1580 return (int) (r1->rel->address - r2->rel->address);
1583 /* Write out an NLM file. We write out the information in this order:
1589 other sections (custom data, messages, help, shared NLM, RPC,
1590 module dependencies)
1592 external references (imports)
1593 public symbols (exports)
1595 This is similar to the order used by the NetWare tools; the
1596 difference is that NetWare puts the sections other than code, data
1597 and custom data at the end of the NLM. It is convenient for us to
1598 know where the sections are going to be before worrying about the
1599 size of the other information.
1601 By the time this function is called, all the section data should
1602 have been output using set_section_contents. Note that custom
1603 data, the message file, the help file, the shared NLM file, the RPC
1604 data, and the module dependencies are all considered to be
1605 sections; the caller is responsible for filling in the offset and
1606 length fields in the NLM headers. The relocation fixups and
1607 imports are both obtained from the list of relocs attached to each
1608 section. The exports and debugging records are obtained from the
1609 list of outsymbols. */
1612 nlm_write_object_contents (abfd)
1616 boolean (*write_import_func) PARAMS ((bfd *, asection *, arelent *));
1617 bfd_size_type external_reloc_count, internal_reloc_count, i, c;
1618 struct reloc_and_sec *external_relocs;
1619 asymbol **sym_ptr_ptr;
1621 boolean (*write_prefix_func) PARAMS ((bfd *));
1622 unsigned char *fixed_header = NULL;
1624 fixed_header = (unsigned char *) malloc (nlm_fixed_header_size (abfd));
1625 if (fixed_header == NULL)
1627 bfd_set_error (bfd_error_no_memory);
1631 if (abfd->output_has_begun == false
1632 && nlm_compute_section_file_positions (abfd) == false)
1635 /* Write out the variable length headers. */
1637 nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd),
1640 if (nlm_swap_variable_header_out (abfd) == false
1641 || nlm_swap_auxiliary_headers_out (abfd) == false)
1643 bfd_set_error (bfd_error_system_call);
1647 /* A weak check on whether the section file positions were
1649 if (bfd_tell (abfd) > nlm_fixed_header (abfd)->codeImageOffset)
1651 bfd_set_error (bfd_error_invalid_operation);
1655 /* Advance to the relocs. */
1656 if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
1660 /* The format of the relocation entries is dependent upon the
1661 particular target. We use an external routine to write the reloc
1663 write_import_func = nlm_write_import_func (abfd);
1665 /* Write out the internal relocation fixups. While we're looping
1666 over the relocs, we also count the external relocs, which is
1667 needed when they are written out below. */
1668 internal_reloc_count = 0;
1669 external_reloc_count = 0;
1670 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
1672 arelent **rel_ptr_ptr, **rel_end;
1674 if (sec->reloc_count == 0)
1677 /* We can only represent relocs within a code or data
1678 section. We ignore them for a debugging section. */
1679 if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
1682 /* We need to know how to write out imports */
1683 if (write_import_func == NULL)
1685 bfd_set_error (bfd_error_invalid_operation);
1689 rel_ptr_ptr = sec->orelocation;
1690 rel_end = rel_ptr_ptr + sec->reloc_count;
1691 for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
1697 sym = *rel->sym_ptr_ptr;
1699 if (bfd_get_section (sym) != &bfd_und_section)
1701 ++internal_reloc_count;
1702 if ((*write_import_func) (abfd, sec, rel) == false)
1706 ++external_reloc_count;
1709 nlm_fixed_header (abfd)->numberOfRelocationFixups = internal_reloc_count;
1711 /* Write out the imports (relocs against external symbols). These
1712 are output as a symbol name followed by all the relocs for that
1713 symbol, so we must first gather together all the relocs against
1714 external symbols and sort them. */
1716 (struct reloc_and_sec *) bfd_alloc (abfd,
1717 (external_reloc_count
1718 * sizeof (struct reloc_and_sec)));
1719 if (external_relocs == (struct reloc_and_sec *) NULL)
1721 bfd_set_error (bfd_error_no_memory);
1725 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
1727 arelent **rel_ptr_ptr, **rel_end;
1729 if (sec->reloc_count == 0)
1732 rel_ptr_ptr = sec->orelocation;
1733 rel_end = rel_ptr_ptr + sec->reloc_count;
1734 for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
1740 sym = *rel->sym_ptr_ptr;
1742 if (bfd_get_section (sym) != &bfd_und_section)
1745 external_relocs[i].rel = rel;
1746 external_relocs[i].sec = sec;
1751 BFD_ASSERT (i == external_reloc_count);
1753 /* Sort the external relocs by name. */
1754 qsort ((PTR) external_relocs, (size_t) external_reloc_count,
1755 sizeof (struct reloc_and_sec), nlm_external_reloc_compare);
1757 /* Write out the external relocs. */
1758 nlm_fixed_header (abfd)->externalReferencesOffset = bfd_tell (abfd);
1761 while (i < external_reloc_count)
1765 bfd_size_type j, cnt;
1769 rel = external_relocs[i].rel;
1770 sym = *rel->sym_ptr_ptr;
1774 (j < external_reloc_count
1775 && *external_relocs[j].rel->sym_ptr_ptr == sym);
1779 if ((*nlm_write_external_func (abfd)) (abfd, cnt, sym,
1780 &external_relocs[i])
1787 nlm_fixed_header (abfd)->numberOfExternalReferences = c;
1789 /* Write out the public symbols (exports). */
1790 sym_ptr_ptr = bfd_get_outsymbols (abfd);
1791 if (sym_ptr_ptr != (asymbol **) NULL)
1793 bfd_vma (*get_public_offset_func) PARAMS ((bfd *, asymbol *));
1794 boolean (*write_export_func) PARAMS ((bfd *, asymbol *, bfd_vma));
1798 nlm_fixed_header (abfd)->publicsOffset = bfd_tell (abfd);
1799 get_public_offset_func = nlm_get_public_offset_func (abfd);
1800 write_export_func = nlm_write_export_func (abfd);
1802 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1803 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1808 bfd_byte temp[NLM_TARGET_LONG_SIZE];
1812 if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) == 0
1813 || bfd_get_section (sym) == &bfd_und_section)
1818 if (get_public_offset_func)
1820 /* Most backends can use the code below, but
1821 unfortunately some use a different scheme. */
1822 offset = (*get_public_offset_func) (abfd, sym);
1826 offset = bfd_asymbol_value (sym);
1828 if (sec->flags & SEC_CODE)
1830 offset -= nlm_get_text_low (abfd);
1831 offset |= NLM_HIBIT;
1833 else if (sec->flags & (SEC_DATA | SEC_ALLOC))
1835 /* SEC_ALLOC is for the .bss section. */
1836 offset -= nlm_get_data_low (abfd);
1840 /* We can't handle an exported symbol that is not in
1841 the code or data segment. */
1842 bfd_set_error (bfd_error_invalid_operation);
1847 if (write_export_func)
1849 if ((*write_export_func) (abfd, sym, offset) == false)
1854 len = strlen (sym->name);
1855 if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd)
1856 != sizeof (bfd_byte))
1857 || bfd_write (sym->name, len, 1, abfd) != len)
1860 put_word (abfd, offset, temp);
1861 if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
1865 nlm_fixed_header (abfd)->numberOfPublics = c;
1867 /* Write out the debugging records. The NLM conversion program
1868 wants to be able to inhibit this, so as a special hack if
1869 debugInfoOffset is set to -1 we don't write any debugging
1870 information. This can not be handled by fiddling with the
1871 symbol table, because exported symbols appear in both the
1872 exported symbol list and the debugging information. */
1873 if (nlm_fixed_header (abfd)->debugInfoOffset == (file_ptr) - 1)
1875 nlm_fixed_header (abfd)->debugInfoOffset = 0;
1876 nlm_fixed_header (abfd)->numberOfDebugRecords = 0;
1880 nlm_fixed_header (abfd)->debugInfoOffset = bfd_tell (abfd);
1882 sym_ptr_ptr = bfd_get_outsymbols (abfd);
1883 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1884 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1889 bfd_byte temp[NLM_TARGET_LONG_SIZE];
1893 /* The NLM notion of a debugging symbol is actually what
1894 BFD calls a local or global symbol. What BFD calls a
1895 debugging symbol NLM does not understand at all. */
1896 if ((sym->flags & (BSF_LOCAL | BSF_GLOBAL | BSF_EXPORT)) == 0
1897 || (sym->flags & BSF_DEBUGGING) != 0
1898 || bfd_get_section (sym) == &bfd_und_section)
1903 offset = bfd_asymbol_value (sym);
1905 if (sec->flags & SEC_CODE)
1907 offset -= nlm_get_text_low (abfd);
1910 else if (sec->flags & (SEC_DATA | SEC_ALLOC))
1912 /* SEC_ALLOC is for the .bss section. */
1913 offset -= nlm_get_data_low (abfd);
1919 /* The type is 0 for data, 1 for code, 2 for absolute. */
1920 if (bfd_write (&type, sizeof (bfd_byte), 1, abfd)
1921 != sizeof (bfd_byte))
1924 put_word (abfd, offset, temp);
1925 if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
1928 len = strlen (sym->name);
1929 if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd)
1930 != sizeof (bfd_byte))
1931 || bfd_write (sym->name, len, 1, abfd) != len)
1934 nlm_fixed_header (abfd)->numberOfDebugRecords = c;
1938 /* NLMLINK fills in offset values even if there is no data, so we do
1940 last = bfd_tell (abfd);
1941 if (nlm_fixed_header (abfd)->codeImageOffset == 0)
1942 nlm_fixed_header (abfd)->codeImageOffset = last;
1943 if (nlm_fixed_header (abfd)->dataImageOffset == 0)
1944 nlm_fixed_header (abfd)->dataImageOffset = last;
1945 if (nlm_fixed_header (abfd)->customDataOffset == 0)
1946 nlm_fixed_header (abfd)->customDataOffset = last;
1947 if (nlm_fixed_header (abfd)->moduleDependencyOffset == 0)
1948 nlm_fixed_header (abfd)->moduleDependencyOffset = last;
1949 if (nlm_fixed_header (abfd)->relocationFixupOffset == 0)
1950 nlm_fixed_header (abfd)->relocationFixupOffset = last;
1951 if (nlm_fixed_header (abfd)->externalReferencesOffset == 0)
1952 nlm_fixed_header (abfd)->externalReferencesOffset = last;
1953 if (nlm_fixed_header (abfd)->publicsOffset == 0)
1954 nlm_fixed_header (abfd)->publicsOffset = last;
1955 if (nlm_fixed_header (abfd)->debugInfoOffset == 0)
1956 nlm_fixed_header (abfd)->debugInfoOffset = last;
1958 /* At this point everything has been written out except the fixed
1960 memcpy (nlm_fixed_header (abfd)->signature, nlm_signature (abfd),
1961 NLM_SIGNATURE_SIZE);
1962 nlm_fixed_header (abfd)->version = NLM_HEADER_VERSION;
1963 nlm_fixed_header (abfd)->codeStartOffset =
1964 (bfd_get_start_address (abfd)
1965 - nlm_get_text_low (abfd));
1967 /* We have no convenient way for the caller to pass in the exit
1968 procedure or the check unload procedure, so the caller must set
1969 the values in the header to the values of the symbols. */
1970 nlm_fixed_header (abfd)->exitProcedureOffset -= nlm_get_text_low (abfd);
1971 if (nlm_fixed_header (abfd)->checkUnloadProcedureOffset != 0)
1972 nlm_fixed_header (abfd)->checkUnloadProcedureOffset -=
1973 nlm_get_text_low (abfd);
1975 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
1978 write_prefix_func = nlm_write_prefix_func (abfd);
1979 if (write_prefix_func)
1981 if ((*write_prefix_func) (abfd) == false)
1985 BFD_ASSERT (bfd_tell (abfd) == nlm_optional_prefix_size (abfd));
1987 nlm_swap_fixed_header_out (abfd, nlm_fixed_header (abfd), fixed_header);
1988 if (bfd_write (fixed_header, nlm_fixed_header_size (abfd), 1, abfd)
1989 != nlm_fixed_header_size (abfd))
1992 if (fixed_header != NULL)
1993 free (fixed_header);
1997 if (fixed_header != NULL)
1998 free (fixed_header);