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
97 struct nlm_obj_tdata
*preserved_tdata
= nlm_tdata (abfd
);
98 boolean (*backend_object_p
) PARAMS ((bfd
*));
100 Nlm_Internal_Fixed_Header
*i_fxdhdrp
;
101 const char *signature
;
102 enum bfd_architecture arch
;
104 /* Some NLM formats have a prefix before the standard NLM fixed
106 backend_object_p
= nlm_backend_object_p_func (abfd
);
107 if (backend_object_p
)
109 if (!(*backend_object_p
) (abfd
))
110 goto got_wrong_format_error
;
113 /* Read in the fixed length portion of the NLM header in external format. */
115 x_fxdhdr
= (PTR
) malloc (nlm_fixed_header_size (abfd
));
116 if (x_fxdhdr
== NULL
)
118 bfd_set_error (bfd_error_no_memory
);
122 if (bfd_read ((PTR
) x_fxdhdr
, nlm_fixed_header_size (abfd
), 1, abfd
) !=
123 nlm_fixed_header_size (abfd
))
124 goto got_wrong_format_error
;
126 /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
127 the tdata pointer in the bfd. */
129 nlm_tdata (abfd
) = (struct nlm_obj_tdata
*)
130 bfd_zalloc (abfd
, sizeof (struct nlm_obj_tdata
));
131 if (nlm_tdata (abfd
) == NULL
)
133 bfd_set_error (bfd_error_no_memory
);
137 i_fxdhdrp
= nlm_fixed_header (abfd
);
138 nlm_swap_fixed_header_in (abfd
, x_fxdhdr
, i_fxdhdrp
);
140 /* Check to see if we have an NLM file for this backend by matching
141 the NLM signature. */
143 signature
= nlm_signature (abfd
);
144 if (signature
!= NULL
145 && *signature
!= '\0'
146 && strncmp ((char *) i_fxdhdrp
->signature
, signature
,
147 NLM_SIGNATURE_SIZE
) != 0)
148 goto got_wrong_format_error
;
150 /* There's no supported way to discover the endianess of an NLM, so test for
151 a sane version number after doing byte swapping appropriate for this
152 XVEC. (Hack alert!) */
154 if (i_fxdhdrp
->version
> 0xFFFF)
155 goto got_wrong_format_error
;
157 /* There's no supported way to check for 32 bit versus 64 bit addresses,
158 so ignore this distinction for now. (FIXME) */
160 /* FIXME: Any return(NULL) exits below here will leak memory (tdata).
161 And a memory leak also means we lost the real tdata info we wanted
162 to save, because it was in the leaked memory. */
164 /* Swap in the rest of the fixed length header. */
166 if (!nlm_swap_variable_header_in (abfd
)
167 || !nlm_swap_auxiliary_headers_in (abfd
)
168 || !add_bfd_section (abfd
, NLM_CODE_NAME
,
169 i_fxdhdrp
->codeImageOffset
,
170 i_fxdhdrp
->codeImageSize
,
171 (SEC_CODE
| SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
173 || !add_bfd_section (abfd
, NLM_INITIALIZED_DATA_NAME
,
174 i_fxdhdrp
->dataImageOffset
,
175 i_fxdhdrp
->dataImageSize
,
176 (SEC_DATA
| SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
178 || !add_bfd_section (abfd
, NLM_UNINITIALIZED_DATA_NAME
,
180 i_fxdhdrp
->uninitializedDataSize
,
182 goto got_wrong_format_error
;
184 if (nlm_fixed_header (abfd
)->numberOfRelocationFixups
!= 0
185 || nlm_fixed_header (abfd
)->numberOfExternalReferences
!= 0)
186 abfd
->flags
|= HAS_RELOC
;
187 if (nlm_fixed_header (abfd
)->numberOfPublics
!= 0
188 || nlm_fixed_header (abfd
)->numberOfDebugRecords
!= 0
189 || nlm_fixed_header (abfd
)->numberOfExternalReferences
!= 0)
190 abfd
->flags
|= HAS_SYMS
;
192 arch
= nlm_architecture (abfd
);
193 if (arch
!= bfd_arch_unknown
)
194 bfd_default_set_arch_mach (abfd
, arch
, (unsigned long) 0);
196 if (x_fxdhdr
!= NULL
)
200 got_wrong_format_error
:
201 bfd_set_error (bfd_error_wrong_format
);
203 nlm_tdata (abfd
) = preserved_tdata
;
204 if (x_fxdhdr
!= NULL
)
209 /* Add a section to the bfd. */
212 add_bfd_section (abfd
, name
, offset
, size
, flags
)
221 newsect
= bfd_make_section (abfd
, name
);
226 newsect
->vma
= 0; /* NLM's are relocatable. */
227 newsect
->_raw_size
= size
;
228 newsect
->filepos
= offset
;
229 newsect
->flags
= flags
;
230 newsect
->alignment_power
= bfd_log2 (0); /* FIXME */
234 /* Read and swap in the variable length header. All the fields must
235 exist in the NLM, and must exist in the order they are read here. */
238 nlm_swap_variable_header_in (abfd
)
241 unsigned char temp
[NLM_TARGET_LONG_SIZE
];
243 /* Read the description length and text members. */
245 if (bfd_read ((PTR
) & nlm_variable_header (abfd
)->descriptionLength
,
246 sizeof (nlm_variable_header (abfd
)->descriptionLength
),
248 sizeof (nlm_variable_header (abfd
)->descriptionLength
))
250 bfd_set_error (bfd_error_system_call
);
253 if (bfd_read ((PTR
) nlm_variable_header (abfd
)->descriptionText
,
254 nlm_variable_header (abfd
)->descriptionLength
+ 1,
256 nlm_variable_header (abfd
)->descriptionLength
+ 1)
258 bfd_set_error (bfd_error_system_call
);
262 /* Read and convert the stackSize field. */
264 if (bfd_read ((PTR
) temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
266 bfd_set_error (bfd_error_system_call
);
269 nlm_variable_header (abfd
)->stackSize
= get_word (abfd
, (bfd_byte
*) temp
);
271 /* Read and convert the reserved field. */
273 if (bfd_read ((PTR
) temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
275 bfd_set_error (bfd_error_system_call
);
278 nlm_variable_header (abfd
)->reserved
= get_word (abfd
, (bfd_byte
*) temp
);
280 /* Read the oldThreadName field. This field is a fixed length string. */
282 if (bfd_read ((PTR
) nlm_variable_header (abfd
)->oldThreadName
,
283 sizeof (nlm_variable_header (abfd
)->oldThreadName
),
285 sizeof (nlm_variable_header (abfd
)->oldThreadName
))
287 bfd_set_error (bfd_error_system_call
);
291 /* Read the screen name length and text members. */
293 if (bfd_read ((PTR
) & nlm_variable_header (abfd
)->screenNameLength
,
294 sizeof (nlm_variable_header (abfd
)->screenNameLength
),
296 sizeof (nlm_variable_header (abfd
)->screenNameLength
))
298 bfd_set_error (bfd_error_system_call
);
301 if (bfd_read ((PTR
) nlm_variable_header (abfd
)->screenName
,
302 nlm_variable_header (abfd
)->screenNameLength
+ 1,
304 nlm_variable_header (abfd
)->screenNameLength
+ 1)
306 bfd_set_error (bfd_error_system_call
);
310 /* Read the thread name length and text members. */
312 if (bfd_read ((PTR
) & nlm_variable_header (abfd
)->threadNameLength
,
313 sizeof (nlm_variable_header (abfd
)->threadNameLength
),
315 sizeof (nlm_variable_header (abfd
)->threadNameLength
))
317 bfd_set_error (bfd_error_system_call
);
320 if (bfd_read ((PTR
) nlm_variable_header (abfd
)->threadName
,
321 nlm_variable_header (abfd
)->threadNameLength
+ 1,
323 nlm_variable_header (abfd
)->threadNameLength
+ 1)
325 bfd_set_error (bfd_error_system_call
);
331 /* Swap and write out the variable length header. All the fields must
332 exist in the NLM, and must exist in this order. */
335 nlm_swap_variable_header_out (abfd
)
338 unsigned char temp
[NLM_TARGET_LONG_SIZE
];
340 /* Write the description length and text members. */
342 if (bfd_write ((PTR
) & nlm_variable_header (abfd
)->descriptionLength
,
343 sizeof (nlm_variable_header (abfd
)->descriptionLength
),
345 sizeof (nlm_variable_header (abfd
)->descriptionLength
))
347 bfd_set_error (bfd_error_system_call
);
350 if (bfd_write ((PTR
) nlm_variable_header (abfd
)->descriptionText
,
351 nlm_variable_header (abfd
)->descriptionLength
+ 1,
353 nlm_variable_header (abfd
)->descriptionLength
+ 1)
355 bfd_set_error (bfd_error_system_call
);
359 /* Convert and write the stackSize field. */
361 put_word (abfd
, (bfd_vma
) nlm_variable_header (abfd
)->stackSize
,
363 if (bfd_write ((PTR
) temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
365 bfd_set_error (bfd_error_system_call
);
369 /* Convert and write the reserved field. */
371 put_word (abfd
, (bfd_vma
) nlm_variable_header (abfd
)->reserved
,
373 if (bfd_write ((PTR
) temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
375 bfd_set_error (bfd_error_system_call
);
379 /* Write the oldThreadName field. This field is a fixed length string. */
381 if (bfd_write ((PTR
) nlm_variable_header (abfd
)->oldThreadName
,
382 sizeof (nlm_variable_header (abfd
)->oldThreadName
),
384 sizeof (nlm_variable_header (abfd
)->oldThreadName
))
386 bfd_set_error (bfd_error_system_call
);
390 /* Write the screen name length and text members. */
392 if (bfd_write ((PTR
) & nlm_variable_header (abfd
)->screenNameLength
,
393 sizeof (nlm_variable_header (abfd
)->screenNameLength
),
395 sizeof (nlm_variable_header (abfd
)->screenNameLength
))
397 bfd_set_error (bfd_error_system_call
);
400 if (bfd_write ((PTR
) nlm_variable_header (abfd
)->screenName
,
401 nlm_variable_header (abfd
)->screenNameLength
+ 1,
403 nlm_variable_header (abfd
)->screenNameLength
+ 1)
405 bfd_set_error (bfd_error_system_call
);
409 /* Write the thread name length and text members. */
411 if (bfd_write ((PTR
) & nlm_variable_header (abfd
)->threadNameLength
,
412 sizeof (nlm_variable_header (abfd
)->threadNameLength
),
414 sizeof (nlm_variable_header (abfd
)->threadNameLength
))
416 bfd_set_error (bfd_error_system_call
);
419 if (bfd_write ((PTR
) nlm_variable_header (abfd
)->threadName
,
420 nlm_variable_header (abfd
)->threadNameLength
+ 1,
422 nlm_variable_header (abfd
)->threadNameLength
+ 1)
424 bfd_set_error (bfd_error_system_call
);
430 /* Read and swap in the contents of all the auxiliary headers. Because of
431 the braindead design, we have to do strcmps on strings of indeterminate
432 length to figure out what each auxiliary header is. Even worse, we have
433 no way of knowing how many auxiliary headers there are or where the end
434 of the auxiliary headers are, except by finding something that doesn't
435 look like a known auxiliary header. This means that the first new type
436 of auxiliary header added will break all existing tools that don't
440 nlm_swap_auxiliary_headers_in (abfd
)
448 position
= bfd_tell (abfd
);
449 if (bfd_read ((PTR
) tempstr
, sizeof (tempstr
), 1, abfd
) !=
452 bfd_set_error (bfd_error_system_call
);
455 if (bfd_seek (abfd
, position
, SEEK_SET
) == -1)
457 bfd_set_error (bfd_error_system_call
);
460 if (strncmp (tempstr
, "VeRsIoN#", 8) == 0)
462 Nlm_External_Version_Header thdr
;
463 if (bfd_read ((PTR
) & thdr
, sizeof (thdr
), 1, abfd
) != sizeof (thdr
))
465 bfd_set_error (bfd_error_system_call
);
468 memcpy (nlm_version_header (abfd
)->stamp
, thdr
.stamp
,
469 sizeof (thdr
.stamp
));
470 nlm_version_header (abfd
)->majorVersion
=
471 get_word (abfd
, (bfd_byte
*) thdr
.majorVersion
);
472 nlm_version_header (abfd
)->minorVersion
=
473 get_word (abfd
, (bfd_byte
*) thdr
.minorVersion
);
474 nlm_version_header (abfd
)->revision
=
475 get_word (abfd
, (bfd_byte
*) thdr
.revision
);
476 nlm_version_header (abfd
)->year
=
477 get_word (abfd
, (bfd_byte
*) thdr
.year
);
478 nlm_version_header (abfd
)->month
=
479 get_word (abfd
, (bfd_byte
*) thdr
.month
);
480 nlm_version_header (abfd
)->day
=
481 get_word (abfd
, (bfd_byte
*) thdr
.day
);
483 else if (strncmp (tempstr
, "MeSsAgEs", 8) == 0)
485 Nlm_External_Extended_Header thdr
;
486 if (bfd_read ((PTR
) & thdr
, sizeof (thdr
), 1, abfd
) != sizeof (thdr
))
488 bfd_set_error (bfd_error_system_call
);
491 memcpy (nlm_extended_header (abfd
)->stamp
, thdr
.stamp
,
492 sizeof (thdr
.stamp
));
493 nlm_extended_header (abfd
)->languageID
=
494 get_word (abfd
, (bfd_byte
*) thdr
.languageID
);
495 nlm_extended_header (abfd
)->messageFileOffset
=
496 get_word (abfd
, (bfd_byte
*) thdr
.messageFileOffset
);
497 nlm_extended_header (abfd
)->messageFileLength
=
498 get_word (abfd
, (bfd_byte
*) thdr
.messageFileLength
);
499 nlm_extended_header (abfd
)->messageCount
=
500 get_word (abfd
, (bfd_byte
*) thdr
.messageCount
);
501 nlm_extended_header (abfd
)->helpFileOffset
=
502 get_word (abfd
, (bfd_byte
*) thdr
.helpFileOffset
);
503 nlm_extended_header (abfd
)->helpFileLength
=
504 get_word (abfd
, (bfd_byte
*) thdr
.helpFileLength
);
505 nlm_extended_header (abfd
)->RPCDataOffset
=
506 get_word (abfd
, (bfd_byte
*) thdr
.RPCDataOffset
);
507 nlm_extended_header (abfd
)->RPCDataLength
=
508 get_word (abfd
, (bfd_byte
*) thdr
.RPCDataLength
);
509 nlm_extended_header (abfd
)->sharedCodeOffset
=
510 get_word (abfd
, (bfd_byte
*) thdr
.sharedCodeOffset
);
511 nlm_extended_header (abfd
)->sharedCodeLength
=
512 get_word (abfd
, (bfd_byte
*) thdr
.sharedCodeLength
);
513 nlm_extended_header (abfd
)->sharedDataOffset
=
514 get_word (abfd
, (bfd_byte
*) thdr
.sharedDataOffset
);
515 nlm_extended_header (abfd
)->sharedDataLength
=
516 get_word (abfd
, (bfd_byte
*) thdr
.sharedDataLength
);
517 nlm_extended_header (abfd
)->sharedRelocationFixupOffset
=
518 get_word (abfd
, (bfd_byte
*) thdr
.sharedRelocationFixupOffset
);
519 nlm_extended_header (abfd
)->sharedRelocationFixupCount
=
520 get_word (abfd
, (bfd_byte
*) thdr
.sharedRelocationFixupCount
);
521 nlm_extended_header (abfd
)->sharedExternalReferenceOffset
=
522 get_word (abfd
, (bfd_byte
*) thdr
.sharedExternalReferenceOffset
);
523 nlm_extended_header (abfd
)->sharedExternalReferenceCount
=
524 get_word (abfd
, (bfd_byte
*) thdr
.sharedExternalReferenceCount
);
525 nlm_extended_header (abfd
)->sharedPublicsOffset
=
526 get_word (abfd
, (bfd_byte
*) thdr
.sharedPublicsOffset
);
527 nlm_extended_header (abfd
)->sharedPublicsCount
=
528 get_word (abfd
, (bfd_byte
*) thdr
.sharedPublicsCount
);
529 nlm_extended_header (abfd
)->sharedDebugRecordOffset
=
530 get_word (abfd
, (bfd_byte
*) thdr
.sharedDebugRecordOffset
);
531 nlm_extended_header (abfd
)->sharedDebugRecordCount
=
532 get_word (abfd
, (bfd_byte
*) thdr
.sharedDebugRecordCount
);
533 nlm_extended_header (abfd
)->SharedInitializationOffset
=
534 get_word (abfd
, (bfd_byte
*) thdr
.sharedInitializationOffset
);
535 nlm_extended_header (abfd
)->SharedExitProcedureOffset
=
536 get_word (abfd
, (bfd_byte
*) thdr
.SharedExitProcedureOffset
);
537 nlm_extended_header (abfd
)->productID
=
538 get_word (abfd
, (bfd_byte
*) thdr
.productID
);
539 nlm_extended_header (abfd
)->reserved0
=
540 get_word (abfd
, (bfd_byte
*) thdr
.reserved0
);
541 nlm_extended_header (abfd
)->reserved1
=
542 get_word (abfd
, (bfd_byte
*) thdr
.reserved1
);
543 nlm_extended_header (abfd
)->reserved2
=
544 get_word (abfd
, (bfd_byte
*) thdr
.reserved2
);
545 nlm_extended_header (abfd
)->reserved3
=
546 get_word (abfd
, (bfd_byte
*) thdr
.reserved3
);
547 nlm_extended_header (abfd
)->reserved4
=
548 get_word (abfd
, (bfd_byte
*) thdr
.reserved4
);
549 nlm_extended_header (abfd
)->reserved5
=
550 get_word (abfd
, (bfd_byte
*) thdr
.reserved5
);
552 else if (strncmp (tempstr
, "CuStHeAd", 8) == 0)
554 Nlm_External_Custom_Header thdr
;
555 if (bfd_read ((PTR
) & thdr
, sizeof (thdr
), 1, abfd
) != sizeof (thdr
))
557 bfd_set_error (bfd_error_system_call
);
560 memcpy (nlm_custom_header (abfd
)->stamp
, thdr
.stamp
,
561 sizeof (thdr
.stamp
));
562 nlm_custom_header (abfd
)->dataLength
=
563 get_word (abfd
, (bfd_byte
*) thdr
.dataLength
);
564 nlm_custom_header (abfd
)->debugRecOffset
=
565 get_word (abfd
, (bfd_byte
*) thdr
.debugRecOffset
);
566 nlm_custom_header (abfd
)->debugRecLength
=
567 get_word (abfd
, (bfd_byte
*) thdr
.debugRecLength
);
569 else if (strncmp (tempstr
, "CoPyRiGhT=", 10) == 0)
571 if (bfd_read ((PTR
) nlm_copyright_header (abfd
)->stamp
,
572 sizeof (nlm_copyright_header (abfd
)->stamp
),
574 != sizeof (nlm_copyright_header (abfd
)->stamp
))
576 bfd_set_error (bfd_error_system_call
);
579 if (bfd_read ((PTR
) & (nlm_copyright_header (abfd
)
580 ->copyrightMessageLength
),
583 bfd_set_error (bfd_error_system_call
);
586 /* The copyright message is a variable length string. */
587 if (bfd_read ((PTR
) nlm_copyright_header (abfd
)->copyrightMessage
,
588 nlm_copyright_header (abfd
)->copyrightMessageLength
+ 1,
590 nlm_copyright_header (abfd
)->copyrightMessageLength
+ 1)
592 bfd_set_error (bfd_error_system_call
);
604 /* Return whether there is a non-zero byte in a memory block. */
607 find_nonzero (buf
, size
)
611 char *p
= (char *) buf
;
619 /* Swap out the contents of the auxiliary headers. We create those
620 auxiliary headers which have been set non-zero. We do not require
621 the caller to set up the stamp fields. */
624 nlm_swap_auxiliary_headers_out (abfd
)
627 /* Write out the version header if there is one. */
628 if (find_nonzero ((PTR
) nlm_version_header (abfd
),
629 sizeof (Nlm_Internal_Version_Header
)))
631 Nlm_External_Version_Header thdr
;
633 memcpy (thdr
.stamp
, "VeRsIoN#", 8);
634 put_word (abfd
, (bfd_vma
) nlm_version_header (abfd
)->majorVersion
,
635 (bfd_byte
*) thdr
.majorVersion
);
636 put_word (abfd
, (bfd_vma
) nlm_version_header (abfd
)->minorVersion
,
637 (bfd_byte
*) thdr
.minorVersion
);
638 put_word (abfd
, (bfd_vma
) nlm_version_header (abfd
)->revision
,
639 (bfd_byte
*) thdr
.revision
);
640 put_word (abfd
, (bfd_vma
) nlm_version_header (abfd
)->year
,
641 (bfd_byte
*) thdr
.year
);
642 put_word (abfd
, (bfd_vma
) nlm_version_header (abfd
)->month
,
643 (bfd_byte
*) thdr
.month
);
644 put_word (abfd
, (bfd_vma
) nlm_version_header (abfd
)->day
,
645 (bfd_byte
*) thdr
.day
);
646 if (bfd_write ((PTR
) & thdr
, sizeof (thdr
), 1, abfd
) != sizeof (thdr
))
648 bfd_set_error (bfd_error_system_call
);
653 /* Write out the extended header if there is one. */
654 if (find_nonzero ((PTR
) nlm_extended_header (abfd
),
655 sizeof (Nlm_Internal_Extended_Header
)))
657 Nlm_External_Extended_Header thdr
;
659 memcpy (thdr
.stamp
, "MeSsAgEs", 8);
661 (bfd_vma
) nlm_extended_header (abfd
)->languageID
,
662 (bfd_byte
*) thdr
.languageID
);
664 (bfd_vma
) nlm_extended_header (abfd
)->messageFileOffset
,
665 (bfd_byte
*) thdr
.messageFileOffset
);
667 (bfd_vma
) nlm_extended_header (abfd
)->messageFileLength
,
668 (bfd_byte
*) thdr
.messageFileLength
);
670 (bfd_vma
) nlm_extended_header (abfd
)->messageCount
,
671 (bfd_byte
*) thdr
.messageCount
);
673 (bfd_vma
) nlm_extended_header (abfd
)->helpFileOffset
,
674 (bfd_byte
*) thdr
.helpFileOffset
);
676 (bfd_vma
) nlm_extended_header (abfd
)->helpFileLength
,
677 (bfd_byte
*) thdr
.helpFileLength
);
679 (bfd_vma
) nlm_extended_header (abfd
)->RPCDataOffset
,
680 (bfd_byte
*) thdr
.RPCDataOffset
);
682 (bfd_vma
) nlm_extended_header (abfd
)->RPCDataLength
,
683 (bfd_byte
*) thdr
.RPCDataLength
);
685 (bfd_vma
) nlm_extended_header (abfd
)->sharedCodeOffset
,
686 (bfd_byte
*) thdr
.sharedCodeOffset
);
688 (bfd_vma
) nlm_extended_header (abfd
)->sharedCodeLength
,
689 (bfd_byte
*) thdr
.sharedCodeLength
);
691 (bfd_vma
) nlm_extended_header (abfd
)->sharedDataOffset
,
692 (bfd_byte
*) thdr
.sharedDataOffset
);
694 (bfd_vma
) nlm_extended_header (abfd
)->sharedDataLength
,
695 (bfd_byte
*) thdr
.sharedDataLength
);
697 (bfd_vma
) nlm_extended_header (abfd
)->sharedRelocationFixupOffset
,
698 (bfd_byte
*) thdr
.sharedRelocationFixupOffset
);
700 (bfd_vma
) nlm_extended_header (abfd
)->sharedRelocationFixupCount
,
701 (bfd_byte
*) thdr
.sharedRelocationFixupCount
);
703 (bfd_vma
) nlm_extended_header (abfd
)->sharedExternalReferenceOffset
,
704 (bfd_byte
*) thdr
.sharedExternalReferenceOffset
);
706 (bfd_vma
) nlm_extended_header (abfd
)->sharedExternalReferenceCount
,
707 (bfd_byte
*) thdr
.sharedExternalReferenceCount
);
709 (bfd_vma
) nlm_extended_header (abfd
)->sharedPublicsOffset
,
710 (bfd_byte
*) thdr
.sharedPublicsOffset
);
712 (bfd_vma
) nlm_extended_header (abfd
)->sharedPublicsCount
,
713 (bfd_byte
*) thdr
.sharedPublicsCount
);
715 (bfd_vma
) nlm_extended_header (abfd
)->sharedDebugRecordOffset
,
716 (bfd_byte
*) thdr
.sharedDebugRecordOffset
);
718 (bfd_vma
) nlm_extended_header (abfd
)->sharedDebugRecordCount
,
719 (bfd_byte
*) thdr
.sharedDebugRecordCount
);
721 (bfd_vma
) nlm_extended_header (abfd
)->SharedInitializationOffset
,
722 (bfd_byte
*) thdr
.sharedInitializationOffset
);
724 (bfd_vma
) nlm_extended_header (abfd
)->SharedExitProcedureOffset
,
725 (bfd_byte
*) thdr
.SharedExitProcedureOffset
);
727 (bfd_vma
) nlm_extended_header (abfd
)->productID
,
728 (bfd_byte
*) thdr
.productID
);
730 (bfd_vma
) nlm_extended_header (abfd
)->reserved0
,
731 (bfd_byte
*) thdr
.reserved0
);
733 (bfd_vma
) nlm_extended_header (abfd
)->reserved1
,
734 (bfd_byte
*) thdr
.reserved1
);
736 (bfd_vma
) nlm_extended_header (abfd
)->reserved2
,
737 (bfd_byte
*) thdr
.reserved2
);
739 (bfd_vma
) nlm_extended_header (abfd
)->reserved3
,
740 (bfd_byte
*) thdr
.reserved3
);
742 (bfd_vma
) nlm_extended_header (abfd
)->reserved4
,
743 (bfd_byte
*) thdr
.reserved4
);
745 (bfd_vma
) nlm_extended_header (abfd
)->reserved5
,
746 (bfd_byte
*) thdr
.reserved5
);
747 if (bfd_write ((PTR
) & thdr
, sizeof (thdr
), 1, abfd
) != sizeof (thdr
))
749 bfd_set_error (bfd_error_system_call
);
754 /* Write out the custom header if there is one. */
755 if (find_nonzero ((PTR
) nlm_custom_header (abfd
),
756 sizeof (Nlm_Internal_Custom_Header
)))
758 Nlm_External_Custom_Header thdr
;
760 /* Right now we assume the custom header is always the suggested
761 format for alternate debugging records. */
762 BFD_ASSERT (nlm_custom_header (abfd
)->dataLength
== 8);
764 memcpy (thdr
.stamp
, "CuStHeAd", 8);
765 put_word (abfd
, (bfd_vma
) nlm_custom_header (abfd
)->dataLength
,
766 (bfd_byte
*) thdr
.dataLength
);
767 put_word (abfd
, (bfd_vma
) nlm_custom_header (abfd
)->debugRecOffset
,
768 (bfd_byte
*) thdr
.debugRecOffset
);
769 put_word (abfd
, (bfd_vma
) nlm_custom_header (abfd
)->debugRecLength
,
770 (bfd_byte
*) thdr
.debugRecLength
);
771 if (bfd_write ((PTR
) & thdr
, sizeof (thdr
), 1, abfd
) != sizeof (thdr
))
773 bfd_set_error (bfd_error_system_call
);
778 /* Write out the copyright header if there is one. */
779 if (find_nonzero ((PTR
) nlm_copyright_header (abfd
),
780 sizeof (Nlm_Internal_Copyright_Header
)))
782 Nlm_External_Copyright_Header thdr
;
784 memcpy (thdr
.stamp
, "CoPyRiGhT=", 10);
785 if (bfd_write ((PTR
) thdr
.stamp
, sizeof (thdr
.stamp
), 1, abfd
)
786 != sizeof (thdr
.stamp
))
788 bfd_set_error (bfd_error_system_call
);
791 thdr
.copyrightMessageLength
[0] =
792 nlm_copyright_header (abfd
)->copyrightMessageLength
;
793 if (bfd_write ((PTR
) thdr
.copyrightMessageLength
, 1, 1, abfd
) != 1)
795 bfd_set_error (bfd_error_system_call
);
798 /* The copyright message is a variable length string. */
799 if (bfd_write ((PTR
) nlm_copyright_header (abfd
)->copyrightMessage
,
800 nlm_copyright_header (abfd
)->copyrightMessageLength
+ 1,
802 nlm_copyright_header (abfd
)->copyrightMessageLength
+ 1)
804 bfd_set_error (bfd_error_system_call
);
812 /* We read the NLM's public symbols and use it to generate a bfd symbol
813 table (hey, it's better than nothing) on a one-for-one basis. Thus
814 use the number of public symbols as the number of bfd symbols we will
815 have once we actually get around to reading them in.
817 Return the number of bytes required to hold the symtab vector, based on
818 the count plus 1, since we will NULL terminate the vector allocated based
822 nlm_get_symtab_upper_bound (abfd
)
825 Nlm_Internal_Fixed_Header
*i_fxdhdrp
; /* Nlm file header, internal form */
827 long symtab_size
= 0;
829 i_fxdhdrp
= nlm_fixed_header (abfd
);
830 symcount
= (i_fxdhdrp
->numberOfPublics
831 + i_fxdhdrp
->numberOfDebugRecords
832 + i_fxdhdrp
->numberOfExternalReferences
);
833 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
));
834 return (symtab_size
);
837 /* Note that bfd_get_symcount is guaranteed to be zero if slurping the
838 symbol table fails. */
841 nlm_get_symtab (abfd
, alocation
)
845 nlm_symbol_type
*symbase
;
846 bfd_size_type counter
= 0;
848 if (nlm_slurp_symbol_table (abfd
) == false)
850 symbase
= nlm_get_symbols (abfd
);
851 while (counter
< bfd_get_symcount (abfd
))
853 *alocation
++ = &symbase
->symbol
;
857 *alocation
= (asymbol
*) NULL
;
858 return bfd_get_symcount (abfd
);
861 /* Make an NLM symbol. There is nothing special to do here. */
864 nlm_make_empty_symbol (abfd
)
867 nlm_symbol_type
*new;
869 new = (nlm_symbol_type
*) bfd_zalloc (abfd
, sizeof (nlm_symbol_type
));
871 new->symbol
.the_bfd
= abfd
;
875 /* Get symbol information. */
878 nlm_get_symbol_info (ignore_abfd
, symbol
, ret
)
883 bfd_symbol_info (symbol
, ret
);
886 /* Print symbol information. */
889 nlm_print_symbol (abfd
, afile
, symbol
, how
)
893 bfd_print_symbol_type how
;
895 FILE *file
= (FILE *) afile
;
899 case bfd_print_symbol_name
:
900 case bfd_print_symbol_more
:
902 fprintf (file
, "%s", symbol
->name
);
904 case bfd_print_symbol_all
:
905 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
906 fprintf (file
, " %-5s", symbol
->section
->name
);
908 fprintf (file
, " %s", symbol
->name
);
913 /* Slurp in nlm symbol table.
915 In the external (in-file) form, NLM export records are variable length,
916 with the following form:
918 1 byte length of the symbol name (N)
919 N bytes the symbol name
920 4 bytes the symbol offset from start of it's section
922 We also read in the debugging symbols and import records. Import
923 records are treated as undefined symbols. As we read the import
924 records we also read in the associated reloc information, which is
925 attached to the symbol.
927 The bfd symbols are copied to SYMPTRS.
929 When we return, the bfd symcount is either zero or contains the correct
934 nlm_slurp_symbol_table (abfd
)
937 Nlm_Internal_Fixed_Header
*i_fxdhdrp
; /* Nlm file header, internal form */
938 bfd_size_type totsymcount
; /* Number of NLM symbols */
939 bfd_size_type symcount
; /* Counter of NLM symbols */
940 nlm_symbol_type
*sym
; /* Pointer to current bfd symbol */
941 unsigned char symlength
; /* Symbol length read into here */
942 unsigned char symtype
; /* Type of debugging symbol */
943 bfd_byte temp
[NLM_TARGET_LONG_SIZE
]; /* Symbol offsets read into here */
944 boolean (*read_import_func
) PARAMS ((bfd
*, nlm_symbol_type
*));
945 boolean (*set_public_section_func
) PARAMS ((bfd
*, nlm_symbol_type
*));
947 if (nlm_get_symbols (abfd
) != NULL
)
950 /* Read each raw NLM symbol, using the information to create a canonical bfd
953 Note that we allocate the initial bfd canonical symbol buffer based on a
954 one-to-one mapping of the NLM symbols to canonical symbols. We actually
955 use all the NLM symbols, so there will be no space left over at the end.
956 When we have all the symbols, we build the caller's pointer vector. */
959 i_fxdhdrp
= nlm_fixed_header (abfd
);
960 totsymcount
= (i_fxdhdrp
->numberOfPublics
961 + i_fxdhdrp
->numberOfDebugRecords
962 + i_fxdhdrp
->numberOfExternalReferences
);
963 if (totsymcount
== 0)
968 if (bfd_seek (abfd
, i_fxdhdrp
->publicsOffset
, SEEK_SET
) == -1)
970 bfd_set_error (bfd_error_system_call
);
974 sym
= ((nlm_symbol_type
*)
975 bfd_zalloc (abfd
, totsymcount
* sizeof (nlm_symbol_type
)));
978 bfd_set_error (bfd_error_no_memory
);
981 nlm_set_symbols (abfd
, sym
);
983 /* We use the bfd's symcount directly as the control count, so that early
984 termination of the loop leaves the symcount correct for the symbols that
987 set_public_section_func
= nlm_set_public_section_func (abfd
);
988 symcount
= i_fxdhdrp
->numberOfPublics
;
989 while (abfd
->symcount
< symcount
)
991 if (bfd_read ((PTR
) & symlength
, sizeof (symlength
), 1, abfd
)
992 != sizeof (symlength
))
994 bfd_set_error (bfd_error_system_call
);
997 sym
->symbol
.the_bfd
= abfd
;
998 sym
->symbol
.name
= bfd_alloc (abfd
, symlength
+ 1);
999 if (!sym
->symbol
.name
)
1001 bfd_set_error (bfd_error_no_memory
);
1004 if (bfd_read ((PTR
) sym
->symbol
.name
, symlength
, 1, abfd
)
1007 bfd_set_error (bfd_error_system_call
);
1010 /* Cast away const. */
1011 ((char *) (sym
->symbol
.name
))[symlength
] = '\0';
1012 if (bfd_read ((PTR
) temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
1014 bfd_set_error (bfd_error_system_call
);
1017 sym
->symbol
.flags
= BSF_GLOBAL
| BSF_EXPORT
;
1018 sym
->symbol
.value
= get_word (abfd
, temp
);
1019 if (set_public_section_func
)
1021 /* Most backends can use the code below, but unfortunately
1022 some use a different scheme. */
1023 if ((*set_public_section_func
) (abfd
, sym
) == false)
1028 if (sym
->symbol
.value
& NLM_HIBIT
)
1030 sym
->symbol
.value
&= ~NLM_HIBIT
;
1031 sym
->symbol
.flags
|= BSF_FUNCTION
;
1032 sym
->symbol
.section
=
1033 bfd_get_section_by_name (abfd
, NLM_CODE_NAME
);
1037 sym
->symbol
.section
=
1038 bfd_get_section_by_name (abfd
, NLM_INITIALIZED_DATA_NAME
);
1046 /* Read the debugging records. */
1048 if (i_fxdhdrp
->numberOfDebugRecords
> 0)
1050 if (bfd_seek (abfd
, i_fxdhdrp
->debugInfoOffset
, SEEK_SET
) == -1)
1052 bfd_set_error (bfd_error_system_call
);
1056 symcount
+= i_fxdhdrp
->numberOfDebugRecords
;
1057 while (abfd
->symcount
< symcount
)
1059 if ((bfd_read ((PTR
) & symtype
, sizeof (symtype
), 1, abfd
)
1060 != sizeof (symtype
))
1061 || bfd_read ((PTR
) temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
)
1062 || (bfd_read ((PTR
) & symlength
, sizeof (symlength
), 1, abfd
)
1063 != sizeof (symlength
)))
1065 bfd_set_error (bfd_error_system_call
);
1068 sym
->symbol
.the_bfd
= abfd
;
1069 sym
->symbol
.name
= bfd_alloc (abfd
, symlength
+ 1);
1070 if (!sym
->symbol
.name
)
1072 bfd_set_error (bfd_error_no_memory
);
1075 if (bfd_read ((PTR
) sym
->symbol
.name
, symlength
, 1, abfd
)
1078 bfd_set_error (bfd_error_system_call
);
1081 /* Cast away const. */
1082 ((char *) (sym
->symbol
.name
))[symlength
] = '\0';
1083 sym
->symbol
.flags
= BSF_LOCAL
;
1084 sym
->symbol
.value
= get_word (abfd
, temp
);
1087 sym
->symbol
.section
=
1088 bfd_get_section_by_name (abfd
, NLM_INITIALIZED_DATA_NAME
);
1090 else if (symtype
== 1)
1092 sym
->symbol
.flags
|= BSF_FUNCTION
;
1093 sym
->symbol
.section
=
1094 bfd_get_section_by_name (abfd
, NLM_CODE_NAME
);
1098 sym
->symbol
.section
= &bfd_abs_section
;
1106 /* Read in the import records. We can only do this if we know how
1107 to read relocs for this target. */
1109 read_import_func
= nlm_read_import_func (abfd
);
1110 if (read_import_func
!= NULL
)
1112 if (bfd_seek (abfd
, i_fxdhdrp
->externalReferencesOffset
, SEEK_SET
)
1115 bfd_set_error (bfd_error_system_call
);
1119 symcount
+= i_fxdhdrp
->numberOfExternalReferences
;
1120 while (abfd
->symcount
< symcount
)
1122 if ((*read_import_func
) (abfd
, sym
) == false)
1132 /* Get the relocs for an NLM file. There are two types of relocs.
1133 Imports are relocs against symbols defined in other NLM files. We
1134 treat these as relocs against global symbols. Relocation fixups
1135 are internal relocs.
1137 The actual format used to store the relocs is machine specific. */
1139 /* Read in the relocation fixup information. This is stored in
1140 nlm_relocation_fixups, an array of arelent structures, and
1141 nlm_relocation_fixup_secs, an array of section pointers. The
1142 section pointers are needed because the relocs are not sorted by
1146 nlm_slurp_reloc_fixups (abfd
)
1149 boolean (*read_func
) PARAMS ((bfd
*, nlm_symbol_type
*, asection
**,
1151 bfd_size_type count
;
1155 if (nlm_relocation_fixups (abfd
) != NULL
)
1157 read_func
= nlm_read_reloc_func (abfd
);
1158 if (read_func
== NULL
)
1161 if (bfd_seek (abfd
, nlm_fixed_header (abfd
)->relocationFixupOffset
,
1164 bfd_set_error (bfd_error_system_call
);
1168 count
= nlm_fixed_header (abfd
)->numberOfRelocationFixups
;
1169 rels
= (arelent
*) bfd_alloc (abfd
, count
* sizeof (arelent
));
1170 secs
= (asection
**) bfd_alloc (abfd
, count
* sizeof (asection
*));
1171 if (rels
== NULL
|| secs
== NULL
)
1173 bfd_set_error (bfd_error_no_memory
);
1176 nlm_relocation_fixups (abfd
) = rels
;
1177 nlm_relocation_fixup_secs (abfd
) = secs
;
1179 /* We have to read piece by piece, because we don't know how large
1180 the machine specific reloc information is. */
1181 while (count
-- != 0)
1183 if ((*read_func
) (abfd
, (nlm_symbol_type
*) NULL
, secs
, rels
) == false)
1185 nlm_relocation_fixups (abfd
) = NULL
;
1186 nlm_relocation_fixup_secs (abfd
) = NULL
;
1196 /* Get the number of relocs. This really just returns an upper bound,
1197 since it does not attempt to distinguish them based on the section.
1198 That will be handled when they are actually read. */
1201 nlm_get_reloc_upper_bound (abfd
, sec
)
1205 nlm_symbol_type
*syms
;
1206 bfd_size_type count
;
1209 /* If we don't know how to read relocs, just return 0. */
1210 if (nlm_read_reloc_func (abfd
) == NULL
)
1212 /* Make sure we have either the code or the data section. */
1213 if ((bfd_get_section_flags (abfd
, sec
) & (SEC_CODE
| SEC_DATA
)) == 0)
1216 syms
= nlm_get_symbols (abfd
);
1219 if (nlm_slurp_symbol_table (abfd
) == false)
1221 syms
= nlm_get_symbols (abfd
);
1224 ret
= nlm_fixed_header (abfd
)->numberOfRelocationFixups
;
1226 count
= bfd_get_symcount (abfd
);
1227 while (count
-- != 0)
1233 return (ret
+ 1) * sizeof (arelent
*);
1236 /* Get the relocs themselves. */
1239 nlm_canonicalize_reloc (abfd
, sec
, relptr
, symbols
)
1247 bfd_size_type count
, i
;
1250 /* Get the relocation fixups. */
1251 rels
= nlm_relocation_fixups (abfd
);
1254 if (nlm_slurp_reloc_fixups (abfd
) == false)
1256 rels
= nlm_relocation_fixups (abfd
);
1260 secs
= nlm_relocation_fixup_secs (abfd
);
1263 count
= nlm_fixed_header (abfd
)->numberOfRelocationFixups
;
1264 for (i
= 0; i
< count
; i
++, rels
++, secs
++)
1273 /* Get the import symbols. */
1274 count
= bfd_get_symcount (abfd
);
1275 for (i
= 0; i
< count
; i
++, symbols
++)
1280 if (bfd_asymbol_flavour (sym
) == bfd_target_nlm_flavour
)
1282 nlm_symbol_type
*nlm_sym
;
1285 nlm_sym
= (nlm_symbol_type
*) sym
;
1286 for (j
= 0; j
< nlm_sym
->rcnt
; j
++)
1288 if (nlm_sym
->relocs
[j
].section
== sec
)
1290 *relptr
= &nlm_sym
->relocs
[j
].reloc
;
1291 (*relptr
)->sym_ptr_ptr
= symbols
;
1304 /* Compute the section file positions for an NLM file. All variable
1305 length data in the file headers must be set before this function is
1306 called. If the variable length data is changed later, the
1307 resulting object file will be incorrect. Unfortunately, there is
1308 no way to check this.
1310 This routine also sets the Size and Offset fields in the fixed
1313 It also looks over the symbols and moves any common symbols into
1314 the .bss section; NLM has no way to represent a common symbol.
1315 This approach means that either the symbols must already have been
1316 set at this point, or there must be no common symbols. We need to
1317 move the symbols at this point so that mangle_relocs can see the
1321 nlm_compute_section_file_positions (abfd
)
1326 bfd_vma text
, data
, bss
;
1327 bfd_vma text_low
, data_low
;
1328 int text_align
, data_align
, other_align
;
1329 file_ptr text_ptr
, data_ptr
, other_ptr
;
1331 asymbol
**sym_ptr_ptr
;
1333 if (abfd
->output_has_begun
== true)
1336 /* Make sure we have a section to hold uninitialized data. */
1337 bss_sec
= bfd_get_section_by_name (abfd
, NLM_UNINITIALIZED_DATA_NAME
);
1338 if (bss_sec
== NULL
)
1340 if (!add_bfd_section (abfd
, NLM_UNINITIALIZED_DATA_NAME
,
1341 (file_ptr
) 0, (bfd_size_type
) 0,
1344 bss_sec
= bfd_get_section_by_name (abfd
, NLM_UNINITIALIZED_DATA_NAME
);
1347 abfd
->output_has_begun
= true;
1349 /* The fixed header. */
1350 sofar
= nlm_optional_prefix_size (abfd
) + nlm_fixed_header_size (abfd
);
1352 /* The variable header. */
1353 sofar
+= (sizeof (nlm_variable_header (abfd
)->descriptionLength
)
1354 + nlm_variable_header (abfd
)->descriptionLength
+ 1
1355 + NLM_TARGET_LONG_SIZE
/* stackSize */
1356 + NLM_TARGET_LONG_SIZE
/* reserved */
1357 + sizeof (nlm_variable_header (abfd
)->oldThreadName
)
1358 + sizeof (nlm_variable_header (abfd
)->screenNameLength
)
1359 + nlm_variable_header (abfd
)->screenNameLength
+ 1
1360 + sizeof (nlm_variable_header (abfd
)->threadNameLength
)
1361 + nlm_variable_header (abfd
)->threadNameLength
+ 1);
1363 /* The auxiliary headers. */
1364 if (find_nonzero ((PTR
) nlm_version_header (abfd
),
1365 sizeof (Nlm_Internal_Version_Header
)))
1366 sofar
+= sizeof (Nlm_External_Version_Header
);
1367 if (find_nonzero ((PTR
) nlm_extended_header (abfd
),
1368 sizeof (Nlm_Internal_Extended_Header
)))
1369 sofar
+= sizeof (Nlm_External_Extended_Header
);
1370 if (find_nonzero ((PTR
) nlm_custom_header (abfd
),
1371 sizeof (Nlm_Internal_Custom_Header
)))
1372 sofar
+= sizeof (Nlm_External_Custom_Header
);
1373 if (find_nonzero ((PTR
) nlm_copyright_header (abfd
),
1374 sizeof (Nlm_Internal_Copyright_Header
)))
1375 sofar
+= (sizeof (Nlm_External_Copyright_Header
)
1376 + nlm_copyright_header (abfd
)->copyrightMessageLength
+ 1);
1378 /* Compute the section file positions in two passes. First get the
1379 sizes of the text and data sections, and then set the file
1380 positions. This code aligns the sections in the file using the
1381 same alignment restrictions that apply to the sections in memory;
1382 this may not be necessary. */
1384 text_low
= (bfd_vma
) - 1;
1387 data_low
= (bfd_vma
) - 1;
1391 for (sec
= abfd
->sections
; sec
!= (asection
*) NULL
; sec
= sec
->next
)
1395 sec
->_raw_size
= BFD_ALIGN (sec
->_raw_size
, 1 << sec
->alignment_power
);
1397 f
= bfd_get_section_flags (abfd
, sec
);
1400 text
+= sec
->_raw_size
;
1401 if (bfd_get_section_vma (abfd
, sec
) < text_low
)
1402 text_low
= bfd_get_section_vma (abfd
, sec
);
1403 if (sec
->alignment_power
> text_align
)
1404 text_align
= sec
->alignment_power
;
1406 else if (f
& SEC_DATA
)
1408 data
+= sec
->_raw_size
;
1409 if (bfd_get_section_vma (abfd
, sec
) < data_low
)
1410 data_low
= bfd_get_section_vma (abfd
, sec
);
1411 if (sec
->alignment_power
> data_align
)
1412 data_align
= sec
->alignment_power
;
1414 else if (f
& SEC_HAS_CONTENTS
)
1416 if (sec
->alignment_power
> other_align
)
1417 other_align
= sec
->alignment_power
;
1419 else if (f
& SEC_ALLOC
)
1420 bss
+= sec
->_raw_size
;
1423 nlm_set_text_low (abfd
, text_low
);
1424 nlm_set_data_low (abfd
, data_low
);
1426 if (nlm_no_uninitialized_data (abfd
))
1428 /* This NetWare format does not use uninitialized data. We must
1429 increase the size of the data section. We will never wind up
1430 writing those file locations, so they will remain zero. */
1435 text_ptr
= BFD_ALIGN (sofar
, 1 << text_align
);
1436 data_ptr
= BFD_ALIGN (text_ptr
+ text
, 1 << data_align
);
1437 other_ptr
= BFD_ALIGN (data_ptr
+ data
, 1 << other_align
);
1439 /* Fill in some fields in the header for which we now have the
1441 nlm_fixed_header (abfd
)->codeImageOffset
= text_ptr
;
1442 nlm_fixed_header (abfd
)->codeImageSize
= text
;
1443 nlm_fixed_header (abfd
)->dataImageOffset
= data_ptr
;
1444 nlm_fixed_header (abfd
)->dataImageSize
= data
;
1445 nlm_fixed_header (abfd
)->uninitializedDataSize
= bss
;
1447 for (sec
= abfd
->sections
; sec
!= (asection
*) NULL
; sec
= sec
->next
)
1451 f
= bfd_get_section_flags (abfd
, sec
);
1455 sec
->filepos
= text_ptr
;
1456 text_ptr
+= sec
->_raw_size
;
1458 else if (f
& SEC_DATA
)
1460 sec
->filepos
= data_ptr
;
1461 data_ptr
+= sec
->_raw_size
;
1463 else if (f
& SEC_HAS_CONTENTS
)
1465 sec
->filepos
= other_ptr
;
1466 other_ptr
+= sec
->_raw_size
;
1470 nlm_fixed_header (abfd
)->relocationFixupOffset
= other_ptr
;
1472 /* Move all common symbols into the .bss section. */
1474 sym_ptr_ptr
= bfd_get_outsymbols (abfd
);
1475 if (sym_ptr_ptr
!= NULL
)
1480 sym_end
= sym_ptr_ptr
+ bfd_get_symcount (abfd
);
1482 for (; sym_ptr_ptr
< sym_end
; sym_ptr_ptr
++)
1489 if (!bfd_is_com_section (bfd_get_section (sym
)))
1492 /* Put the common symbol in the .bss section, and increase
1493 the size of the .bss section by the size of the common
1494 symbol (which is the old value of the symbol). */
1495 sym
->section
= bss_sec
;
1497 sym
->value
= bss_sec
->_raw_size
+ add
;
1499 add
= BFD_ALIGN (add
, 1 << bss_sec
->alignment_power
);
1503 if (nlm_no_uninitialized_data (abfd
))
1505 /* We could handle this case, but so far it hasn't been
1509 nlm_fixed_header (abfd
)->uninitializedDataSize
+= add
;
1510 bss_sec
->_raw_size
+= add
;
1517 /* Set the contents of a section. To do this we need to know where
1518 the section is going to be located in the output file. That means
1519 that the sizes of all the sections must be set, and all the
1520 variable size header information must be known. */
1523 nlm_set_section_contents (abfd
, section
, location
, offset
, count
)
1528 bfd_size_type count
;
1530 if (abfd
->output_has_begun
== false
1531 && nlm_compute_section_file_positions (abfd
) == false)
1537 /* i386 NetWare has a very restricted set of relocs. In order for
1538 objcopy to work, the NLM i386 backend needs a chance to rework
1539 the section contents so that its set of relocs will work. If all
1540 the relocs are already acceptable, this will not do anything. */
1541 if (section
->reloc_count
!= 0)
1543 boolean (*mangle_relocs_func
) PARAMS ((bfd
*, asection
*, PTR
,
1544 bfd_vma
, bfd_size_type
));
1546 mangle_relocs_func
= nlm_mangle_relocs_func (abfd
);
1547 if (mangle_relocs_func
!= NULL
)
1549 if (!(*mangle_relocs_func
) (abfd
, section
, location
,
1550 (bfd_vma
) offset
, count
))
1555 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) != 0
1556 || bfd_write (location
, 1, count
, abfd
) != count
)
1558 bfd_set_error (bfd_error_system_call
);
1565 /* We need to sort a list of relocs associated with sections when we
1566 write out the external relocs. */
1569 nlm_external_reloc_compare (p1
, p2
)
1573 const struct reloc_and_sec
*r1
= (const struct reloc_and_sec
*) p1
;
1574 const struct reloc_and_sec
*r2
= (const struct reloc_and_sec
*) p2
;
1577 cmp
= strcmp ((*r1
->rel
->sym_ptr_ptr
)->name
,
1578 (*r2
->rel
->sym_ptr_ptr
)->name
);
1582 /* We sort by address within symbol to make the sort more stable and
1583 increase the chances that different hosts will generate bit for
1584 bit equivalent results. */
1585 return (int) (r1
->rel
->address
- r2
->rel
->address
);
1588 /* Write out an NLM file. We write out the information in this order:
1594 other sections (custom data, messages, help, shared NLM, RPC,
1595 module dependencies)
1597 external references (imports)
1598 public symbols (exports)
1600 This is similar to the order used by the NetWare tools; the
1601 difference is that NetWare puts the sections other than code, data
1602 and custom data at the end of the NLM. It is convenient for us to
1603 know where the sections are going to be before worrying about the
1604 size of the other information.
1606 By the time this function is called, all the section data should
1607 have been output using set_section_contents. Note that custom
1608 data, the message file, the help file, the shared NLM file, the RPC
1609 data, and the module dependencies are all considered to be
1610 sections; the caller is responsible for filling in the offset and
1611 length fields in the NLM headers. The relocation fixups and
1612 imports are both obtained from the list of relocs attached to each
1613 section. The exports and debugging records are obtained from the
1614 list of outsymbols. */
1617 nlm_write_object_contents (abfd
)
1621 boolean (*write_import_func
) PARAMS ((bfd
*, asection
*, arelent
*));
1622 bfd_size_type external_reloc_count
, internal_reloc_count
, i
, c
;
1623 struct reloc_and_sec
*external_relocs
;
1624 asymbol
**sym_ptr_ptr
;
1626 boolean (*write_prefix_func
) PARAMS ((bfd
*));
1627 unsigned char *fixed_header
= NULL
;
1629 fixed_header
= (unsigned char *) malloc (nlm_fixed_header_size (abfd
));
1630 if (fixed_header
== NULL
)
1632 bfd_set_error (bfd_error_no_memory
);
1636 if (abfd
->output_has_begun
== false
1637 && nlm_compute_section_file_positions (abfd
) == false)
1640 /* Write out the variable length headers. */
1642 nlm_optional_prefix_size (abfd
) + nlm_fixed_header_size (abfd
),
1645 bfd_set_error (bfd_error_system_call
);
1648 if (nlm_swap_variable_header_out (abfd
) == false
1649 || nlm_swap_auxiliary_headers_out (abfd
) == false)
1651 bfd_set_error (bfd_error_system_call
);
1655 /* A weak check on whether the section file positions were
1657 if (bfd_tell (abfd
) > nlm_fixed_header (abfd
)->codeImageOffset
)
1659 bfd_set_error (bfd_error_invalid_operation
);
1663 /* Advance to the relocs. */
1664 if (bfd_seek (abfd
, nlm_fixed_header (abfd
)->relocationFixupOffset
,
1667 bfd_set_error (bfd_error_system_call
);
1671 /* The format of the relocation entries is dependent upon the
1672 particular target. We use an external routine to write the reloc
1674 write_import_func
= nlm_write_import_func (abfd
);
1676 /* Write out the internal relocation fixups. While we're looping
1677 over the relocs, we also count the external relocs, which is
1678 needed when they are written out below. */
1679 internal_reloc_count
= 0;
1680 external_reloc_count
= 0;
1681 for (sec
= abfd
->sections
; sec
!= (asection
*) NULL
; sec
= sec
->next
)
1683 arelent
**rel_ptr_ptr
, **rel_end
;
1685 if (sec
->reloc_count
== 0)
1688 /* We can only represent relocs within a code or data
1689 section. We ignore them for a debugging section. */
1690 if ((bfd_get_section_flags (abfd
, sec
) & (SEC_CODE
| SEC_DATA
)) == 0)
1693 /* We need to know how to write out imports */
1694 if (write_import_func
== NULL
)
1696 bfd_set_error (bfd_error_invalid_operation
);
1700 rel_ptr_ptr
= sec
->orelocation
;
1701 rel_end
= rel_ptr_ptr
+ sec
->reloc_count
;
1702 for (; rel_ptr_ptr
< rel_end
; rel_ptr_ptr
++)
1708 sym
= *rel
->sym_ptr_ptr
;
1710 if (bfd_get_section (sym
) != &bfd_und_section
)
1712 ++internal_reloc_count
;
1713 if ((*write_import_func
) (abfd
, sec
, rel
) == false)
1717 ++external_reloc_count
;
1720 nlm_fixed_header (abfd
)->numberOfRelocationFixups
= internal_reloc_count
;
1722 /* Write out the imports (relocs against external symbols). These
1723 are output as a symbol name followed by all the relocs for that
1724 symbol, so we must first gather together all the relocs against
1725 external symbols and sort them. */
1727 (struct reloc_and_sec
*) bfd_alloc (abfd
,
1728 (external_reloc_count
1729 * sizeof (struct reloc_and_sec
)));
1730 if (external_relocs
== (struct reloc_and_sec
*) NULL
)
1732 bfd_set_error (bfd_error_no_memory
);
1736 for (sec
= abfd
->sections
; sec
!= (asection
*) NULL
; sec
= sec
->next
)
1738 arelent
**rel_ptr_ptr
, **rel_end
;
1740 if (sec
->reloc_count
== 0)
1743 rel_ptr_ptr
= sec
->orelocation
;
1744 rel_end
= rel_ptr_ptr
+ sec
->reloc_count
;
1745 for (; rel_ptr_ptr
< rel_end
; rel_ptr_ptr
++)
1751 sym
= *rel
->sym_ptr_ptr
;
1753 if (bfd_get_section (sym
) != &bfd_und_section
)
1756 external_relocs
[i
].rel
= rel
;
1757 external_relocs
[i
].sec
= sec
;
1762 BFD_ASSERT (i
== external_reloc_count
);
1764 /* Sort the external relocs by name. */
1765 qsort ((PTR
) external_relocs
, (size_t) external_reloc_count
,
1766 sizeof (struct reloc_and_sec
), nlm_external_reloc_compare
);
1768 /* Write out the external relocs. */
1769 nlm_fixed_header (abfd
)->externalReferencesOffset
= bfd_tell (abfd
);
1772 while (i
< external_reloc_count
)
1776 bfd_size_type j
, cnt
;
1780 rel
= external_relocs
[i
].rel
;
1781 sym
= *rel
->sym_ptr_ptr
;
1785 (j
< external_reloc_count
1786 && *external_relocs
[j
].rel
->sym_ptr_ptr
== sym
);
1790 if ((*nlm_write_external_func (abfd
)) (abfd
, cnt
, sym
,
1791 &external_relocs
[i
])
1798 nlm_fixed_header (abfd
)->numberOfExternalReferences
= c
;
1800 /* Write out the public symbols (exports). */
1801 sym_ptr_ptr
= bfd_get_outsymbols (abfd
);
1802 if (sym_ptr_ptr
!= (asymbol
**) NULL
)
1804 bfd_vma (*get_public_offset_func
) PARAMS ((bfd
*, asymbol
*));
1805 boolean (*write_export_func
) PARAMS ((bfd
*, asymbol
*, bfd_vma
));
1809 nlm_fixed_header (abfd
)->publicsOffset
= bfd_tell (abfd
);
1810 get_public_offset_func
= nlm_get_public_offset_func (abfd
);
1811 write_export_func
= nlm_write_export_func (abfd
);
1813 sym_end
= sym_ptr_ptr
+ bfd_get_symcount (abfd
);
1814 for (; sym_ptr_ptr
< sym_end
; sym_ptr_ptr
++)
1819 bfd_byte temp
[NLM_TARGET_LONG_SIZE
];
1823 if ((sym
->flags
& (BSF_EXPORT
| BSF_GLOBAL
)) == 0
1824 || bfd_get_section (sym
) == &bfd_und_section
)
1829 if (get_public_offset_func
)
1831 /* Most backends can use the code below, but
1832 unfortunately some use a different scheme. */
1833 offset
= (*get_public_offset_func
) (abfd
, sym
);
1837 offset
= bfd_asymbol_value (sym
);
1839 if (sec
->flags
& SEC_CODE
)
1841 offset
-= nlm_get_text_low (abfd
);
1842 offset
|= NLM_HIBIT
;
1844 else if (sec
->flags
& (SEC_DATA
| SEC_ALLOC
))
1846 /* SEC_ALLOC is for the .bss section. */
1847 offset
-= nlm_get_data_low (abfd
);
1851 /* We can't handle an exported symbol that is not in
1852 the code or data segment. */
1853 bfd_set_error (bfd_error_invalid_operation
);
1858 if (write_export_func
)
1860 if ((*write_export_func
) (abfd
, sym
, offset
) == false)
1865 len
= strlen (sym
->name
);
1866 if ((bfd_write (&len
, sizeof (bfd_byte
), 1, abfd
)
1867 != sizeof (bfd_byte
))
1868 || bfd_write (sym
->name
, len
, 1, abfd
) != len
)
1870 bfd_set_error (bfd_error_system_call
);
1874 put_word (abfd
, offset
, temp
);
1875 if (bfd_write (temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
1877 bfd_set_error (bfd_error_system_call
);
1882 nlm_fixed_header (abfd
)->numberOfPublics
= c
;
1884 /* Write out the debugging records. The NLM conversion program
1885 wants to be able to inhibit this, so as a special hack if
1886 debugInfoOffset is set to -1 we don't write any debugging
1887 information. This can not be handled by fiddling with the
1888 symbol table, because exported symbols appear in both the
1889 exported symbol list and the debugging information. */
1890 if (nlm_fixed_header (abfd
)->debugInfoOffset
== (file_ptr
) - 1)
1892 nlm_fixed_header (abfd
)->debugInfoOffset
= 0;
1893 nlm_fixed_header (abfd
)->numberOfDebugRecords
= 0;
1897 nlm_fixed_header (abfd
)->debugInfoOffset
= bfd_tell (abfd
);
1899 sym_ptr_ptr
= bfd_get_outsymbols (abfd
);
1900 sym_end
= sym_ptr_ptr
+ bfd_get_symcount (abfd
);
1901 for (; sym_ptr_ptr
< sym_end
; sym_ptr_ptr
++)
1906 bfd_byte temp
[NLM_TARGET_LONG_SIZE
];
1910 /* The NLM notion of a debugging symbol is actually what
1911 BFD calls a local or global symbol. What BFD calls a
1912 debugging symbol NLM does not understand at all. */
1913 if ((sym
->flags
& (BSF_LOCAL
| BSF_GLOBAL
| BSF_EXPORT
)) == 0
1914 || (sym
->flags
& BSF_DEBUGGING
) != 0
1915 || bfd_get_section (sym
) == &bfd_und_section
)
1920 offset
= bfd_asymbol_value (sym
);
1922 if (sec
->flags
& SEC_CODE
)
1924 offset
-= nlm_get_text_low (abfd
);
1927 else if (sec
->flags
& (SEC_DATA
| SEC_ALLOC
))
1929 /* SEC_ALLOC is for the .bss section. */
1930 offset
-= nlm_get_data_low (abfd
);
1936 /* The type is 0 for data, 1 for code, 2 for absolute. */
1937 if (bfd_write (&type
, sizeof (bfd_byte
), 1, abfd
)
1938 != sizeof (bfd_byte
))
1940 bfd_set_error (bfd_error_system_call
);
1944 put_word (abfd
, offset
, temp
);
1945 if (bfd_write (temp
, sizeof (temp
), 1, abfd
) != sizeof (temp
))
1947 bfd_set_error (bfd_error_system_call
);
1951 len
= strlen (sym
->name
);
1952 if ((bfd_write (&len
, sizeof (bfd_byte
), 1, abfd
)
1953 != sizeof (bfd_byte
))
1954 || bfd_write (sym
->name
, len
, 1, abfd
) != len
)
1956 bfd_set_error (bfd_error_system_call
);
1960 nlm_fixed_header (abfd
)->numberOfDebugRecords
= c
;
1964 /* NLMLINK fills in offset values even if there is no data, so we do
1966 last
= bfd_tell (abfd
);
1967 if (nlm_fixed_header (abfd
)->codeImageOffset
== 0)
1968 nlm_fixed_header (abfd
)->codeImageOffset
= last
;
1969 if (nlm_fixed_header (abfd
)->dataImageOffset
== 0)
1970 nlm_fixed_header (abfd
)->dataImageOffset
= last
;
1971 if (nlm_fixed_header (abfd
)->customDataOffset
== 0)
1972 nlm_fixed_header (abfd
)->customDataOffset
= last
;
1973 if (nlm_fixed_header (abfd
)->moduleDependencyOffset
== 0)
1974 nlm_fixed_header (abfd
)->moduleDependencyOffset
= last
;
1975 if (nlm_fixed_header (abfd
)->relocationFixupOffset
== 0)
1976 nlm_fixed_header (abfd
)->relocationFixupOffset
= last
;
1977 if (nlm_fixed_header (abfd
)->externalReferencesOffset
== 0)
1978 nlm_fixed_header (abfd
)->externalReferencesOffset
= last
;
1979 if (nlm_fixed_header (abfd
)->publicsOffset
== 0)
1980 nlm_fixed_header (abfd
)->publicsOffset
= last
;
1981 if (nlm_fixed_header (abfd
)->debugInfoOffset
== 0)
1982 nlm_fixed_header (abfd
)->debugInfoOffset
= last
;
1984 /* At this point everything has been written out except the fixed
1986 memcpy (nlm_fixed_header (abfd
)->signature
, nlm_signature (abfd
),
1987 NLM_SIGNATURE_SIZE
);
1988 nlm_fixed_header (abfd
)->version
= NLM_HEADER_VERSION
;
1989 nlm_fixed_header (abfd
)->codeStartOffset
=
1990 (bfd_get_start_address (abfd
)
1991 - nlm_get_text_low (abfd
));
1993 /* We have no convenient way for the caller to pass in the exit
1994 procedure or the check unload procedure, so the caller must set
1995 the values in the header to the values of the symbols. */
1996 nlm_fixed_header (abfd
)->exitProcedureOffset
-= nlm_get_text_low (abfd
);
1997 if (nlm_fixed_header (abfd
)->checkUnloadProcedureOffset
!= 0)
1998 nlm_fixed_header (abfd
)->checkUnloadProcedureOffset
-=
1999 nlm_get_text_low (abfd
);
2001 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
2004 write_prefix_func
= nlm_write_prefix_func (abfd
);
2005 if (write_prefix_func
)
2007 if ((*write_prefix_func
) (abfd
) == false)
2011 BFD_ASSERT (bfd_tell (abfd
) == nlm_optional_prefix_size (abfd
));
2013 nlm_swap_fixed_header_out (abfd
, nlm_fixed_header (abfd
), fixed_header
);
2014 if (bfd_write (fixed_header
, nlm_fixed_header_size (abfd
), 1, abfd
)
2015 != nlm_fixed_header_size (abfd
))
2017 bfd_set_error (bfd_error_system_call
);
2021 if (fixed_header
!= NULL
)
2022 free (fixed_header
);
2026 if (fixed_header
!= NULL
)
2027 free (fixed_header
);