1*3d8817e4Smiod /* NLM (NetWare Loadable Module) executable support for BFD.
2*3d8817e4Smiod Copyright 1993, 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3*3d8817e4Smiod 2005 Free Software Foundation, Inc.
4*3d8817e4Smiod
5*3d8817e4Smiod Written by Fred Fish @ Cygnus Support, using ELF support as the
6*3d8817e4Smiod template.
7*3d8817e4Smiod
8*3d8817e4Smiod This file is part of BFD, the Binary File Descriptor library.
9*3d8817e4Smiod
10*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
11*3d8817e4Smiod it under the terms of the GNU General Public License as published by
12*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
13*3d8817e4Smiod (at your option) any later version.
14*3d8817e4Smiod
15*3d8817e4Smiod This program is distributed in the hope that it will be useful,
16*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
17*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18*3d8817e4Smiod GNU General Public License for more details.
19*3d8817e4Smiod
20*3d8817e4Smiod You should have received a copy of the GNU General Public License
21*3d8817e4Smiod along with this program; if not, write to the Free Software
22*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23*3d8817e4Smiod
24*3d8817e4Smiod #include "bfd.h"
25*3d8817e4Smiod #include "sysdep.h"
26*3d8817e4Smiod #include "libbfd.h"
27*3d8817e4Smiod #include "libnlm.h"
28*3d8817e4Smiod
29*3d8817e4Smiod /* The functions in this file do not use the names they appear to use.
30*3d8817e4Smiod This file is actually compiled multiple times, once for each size
31*3d8817e4Smiod of NLM target we are using. At each size we use a different name,
32*3d8817e4Smiod constructed by the macro nlmNAME. For example, the function which
33*3d8817e4Smiod is named nlm_symbol_type below is actually named nlm32_symbol_type
34*3d8817e4Smiod in the final executable. */
35*3d8817e4Smiod
36*3d8817e4Smiod #define Nlm_External_Fixed_Header NlmNAME (External_Fixed_Header)
37*3d8817e4Smiod #define Nlm_External_Version_Header NlmNAME (External_Version_Header)
38*3d8817e4Smiod #define Nlm_External_Copyright_Header NlmNAME (External_Copyright_Header)
39*3d8817e4Smiod #define Nlm_External_Extended_Header NlmNAME (External_Extended_Header)
40*3d8817e4Smiod #define Nlm_External_Custom_Header NlmNAME (External_Custom_Header)
41*3d8817e4Smiod #define Nlm_External_Cygnus_Ext_Header NlmNAME (External_Cygnus_Ext_Header)
42*3d8817e4Smiod
43*3d8817e4Smiod #define nlm_symbol_type nlmNAME (symbol_type)
44*3d8817e4Smiod #define nlm_get_symtab_upper_bound nlmNAME (get_symtab_upper_bound)
45*3d8817e4Smiod #define nlm_canonicalize_symtab nlmNAME (canonicalize_symtab)
46*3d8817e4Smiod #define nlm_make_empty_symbol nlmNAME (make_empty_symbol)
47*3d8817e4Smiod #define nlm_print_symbol nlmNAME (print_symbol)
48*3d8817e4Smiod #define nlm_get_symbol_info nlmNAME (get_symbol_info)
49*3d8817e4Smiod #define nlm_get_reloc_upper_bound nlmNAME (get_reloc_upper_bound)
50*3d8817e4Smiod #define nlm_canonicalize_reloc nlmNAME (canonicalize_reloc)
51*3d8817e4Smiod #define nlm_object_p nlmNAME (object_p)
52*3d8817e4Smiod #define nlm_set_section_contents nlmNAME (set_section_contents)
53*3d8817e4Smiod #define nlm_write_object_contents nlmNAME (write_object_contents)
54*3d8817e4Smiod
55*3d8817e4Smiod #define nlm_swap_fixed_header_in(abfd,src,dst) \
56*3d8817e4Smiod (nlm_swap_fixed_header_in_func (abfd)) (abfd, src, dst)
57*3d8817e4Smiod #define nlm_swap_fixed_header_out(abfd,src,dst) \
58*3d8817e4Smiod (nlm_swap_fixed_header_out_func (abfd)) (abfd, src, dst)
59*3d8817e4Smiod
60*3d8817e4Smiod /* Should perhaps use put_offset, put_word, etc. For now, the two versions
61*3d8817e4Smiod can be handled by explicitly specifying 32 bits or "the long type". */
62*3d8817e4Smiod #if ARCH_SIZE == 64
63*3d8817e4Smiod #define put_word H_PUT_64
64*3d8817e4Smiod #define get_word H_GET_64
65*3d8817e4Smiod #endif
66*3d8817e4Smiod #if ARCH_SIZE == 32
67*3d8817e4Smiod #define put_word H_PUT_32
68*3d8817e4Smiod #define get_word H_GET_32
69*3d8817e4Smiod #endif
70*3d8817e4Smiod
71*3d8817e4Smiod /* Read and swap in the variable length header. All the fields must
72*3d8817e4Smiod exist in the NLM, and must exist in the order they are read here. */
73*3d8817e4Smiod
74*3d8817e4Smiod static bfd_boolean
nlm_swap_variable_header_in(bfd * abfd)75*3d8817e4Smiod nlm_swap_variable_header_in (bfd *abfd)
76*3d8817e4Smiod {
77*3d8817e4Smiod unsigned char temp[NLM_TARGET_LONG_SIZE];
78*3d8817e4Smiod bfd_size_type amt;
79*3d8817e4Smiod
80*3d8817e4Smiod /* Read the description length and text members. */
81*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->descriptionLength);
82*3d8817e4Smiod if (bfd_bread ((void *) &nlm_variable_header (abfd)->descriptionLength,
83*3d8817e4Smiod amt, abfd) != amt)
84*3d8817e4Smiod return FALSE;
85*3d8817e4Smiod amt = nlm_variable_header (abfd)->descriptionLength + 1;
86*3d8817e4Smiod if (bfd_bread ((void *) nlm_variable_header (abfd)->descriptionText,
87*3d8817e4Smiod amt, abfd) != amt)
88*3d8817e4Smiod return FALSE;
89*3d8817e4Smiod
90*3d8817e4Smiod /* Read and convert the stackSize field. */
91*3d8817e4Smiod amt = sizeof (temp);
92*3d8817e4Smiod if (bfd_bread ((void *) temp, amt, abfd) != amt)
93*3d8817e4Smiod return FALSE;
94*3d8817e4Smiod nlm_variable_header (abfd)->stackSize = get_word (abfd, (bfd_byte *) temp);
95*3d8817e4Smiod
96*3d8817e4Smiod /* Read and convert the reserved field. */
97*3d8817e4Smiod amt = sizeof (temp);
98*3d8817e4Smiod if (bfd_bread ((void *) temp, amt, abfd) != amt)
99*3d8817e4Smiod return FALSE;
100*3d8817e4Smiod nlm_variable_header (abfd)->reserved = get_word (abfd, (bfd_byte *) temp);
101*3d8817e4Smiod
102*3d8817e4Smiod /* Read the oldThreadName field. This field is a fixed length string. */
103*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->oldThreadName);
104*3d8817e4Smiod if (bfd_bread ((void *) nlm_variable_header (abfd)->oldThreadName,
105*3d8817e4Smiod amt, abfd) != amt)
106*3d8817e4Smiod return FALSE;
107*3d8817e4Smiod
108*3d8817e4Smiod /* Read the screen name length and text members. */
109*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->screenNameLength);
110*3d8817e4Smiod if (bfd_bread ((void *) & nlm_variable_header (abfd)->screenNameLength,
111*3d8817e4Smiod amt, abfd) != amt)
112*3d8817e4Smiod return FALSE;
113*3d8817e4Smiod amt = nlm_variable_header (abfd)->screenNameLength + 1;
114*3d8817e4Smiod if (bfd_bread ((void *) nlm_variable_header (abfd)->screenName,
115*3d8817e4Smiod amt, abfd) != amt)
116*3d8817e4Smiod return FALSE;
117*3d8817e4Smiod
118*3d8817e4Smiod /* Read the thread name length and text members. */
119*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->threadNameLength);
120*3d8817e4Smiod if (bfd_bread ((void *) & nlm_variable_header (abfd)->threadNameLength,
121*3d8817e4Smiod amt, abfd) != amt)
122*3d8817e4Smiod return FALSE;
123*3d8817e4Smiod amt = nlm_variable_header (abfd)->threadNameLength + 1;
124*3d8817e4Smiod if (bfd_bread ((void *) nlm_variable_header (abfd)->threadName,
125*3d8817e4Smiod amt, abfd) != amt)
126*3d8817e4Smiod return FALSE;
127*3d8817e4Smiod return TRUE;
128*3d8817e4Smiod }
129*3d8817e4Smiod
130*3d8817e4Smiod /* Add a section to the bfd. */
131*3d8817e4Smiod
132*3d8817e4Smiod static bfd_boolean
add_bfd_section(bfd * abfd,char * name,file_ptr offset,bfd_size_type size,flagword flags)133*3d8817e4Smiod add_bfd_section (bfd *abfd,
134*3d8817e4Smiod char *name,
135*3d8817e4Smiod file_ptr offset,
136*3d8817e4Smiod bfd_size_type size,
137*3d8817e4Smiod flagword flags)
138*3d8817e4Smiod {
139*3d8817e4Smiod asection *newsect;
140*3d8817e4Smiod
141*3d8817e4Smiod newsect = bfd_make_section (abfd, name);
142*3d8817e4Smiod if (newsect == NULL)
143*3d8817e4Smiod return FALSE;
144*3d8817e4Smiod
145*3d8817e4Smiod newsect->vma = 0; /* NLM's are relocatable. */
146*3d8817e4Smiod newsect->size = size;
147*3d8817e4Smiod newsect->filepos = offset;
148*3d8817e4Smiod newsect->flags = flags;
149*3d8817e4Smiod newsect->alignment_power = bfd_log2 ((bfd_vma) 0); /* FIXME */
150*3d8817e4Smiod
151*3d8817e4Smiod return TRUE;
152*3d8817e4Smiod }
153*3d8817e4Smiod
154*3d8817e4Smiod /* Read and swap in the contents of all the auxiliary headers. Because of
155*3d8817e4Smiod the braindead design, we have to do strcmps on strings of indeterminate
156*3d8817e4Smiod length to figure out what each auxiliary header is. Even worse, we have
157*3d8817e4Smiod no way of knowing how many auxiliary headers there are or where the end
158*3d8817e4Smiod of the auxiliary headers are, except by finding something that doesn't
159*3d8817e4Smiod look like a known auxiliary header. This means that the first new type
160*3d8817e4Smiod of auxiliary header added will break all existing tools that don't
161*3d8817e4Smiod recognize it. */
162*3d8817e4Smiod
163*3d8817e4Smiod static bfd_boolean
nlm_swap_auxiliary_headers_in(bfd * abfd)164*3d8817e4Smiod nlm_swap_auxiliary_headers_in (bfd *abfd)
165*3d8817e4Smiod {
166*3d8817e4Smiod char tempstr[16];
167*3d8817e4Smiod file_ptr position;
168*3d8817e4Smiod bfd_size_type amt;
169*3d8817e4Smiod
170*3d8817e4Smiod for (;;)
171*3d8817e4Smiod {
172*3d8817e4Smiod position = bfd_tell (abfd);
173*3d8817e4Smiod amt = sizeof (tempstr);
174*3d8817e4Smiod if (bfd_bread ((void *) tempstr, amt, abfd) != amt)
175*3d8817e4Smiod return FALSE;
176*3d8817e4Smiod if (bfd_seek (abfd, position, SEEK_SET) != 0)
177*3d8817e4Smiod return FALSE;
178*3d8817e4Smiod if (strncmp (tempstr, "VeRsIoN#", 8) == 0)
179*3d8817e4Smiod {
180*3d8817e4Smiod Nlm_External_Version_Header thdr;
181*3d8817e4Smiod
182*3d8817e4Smiod amt = sizeof (thdr);
183*3d8817e4Smiod if (bfd_bread ((void *) &thdr, amt, abfd) != amt)
184*3d8817e4Smiod return FALSE;
185*3d8817e4Smiod memcpy (nlm_version_header (abfd)->stamp, thdr.stamp,
186*3d8817e4Smiod sizeof (thdr.stamp));
187*3d8817e4Smiod nlm_version_header (abfd)->majorVersion =
188*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.majorVersion);
189*3d8817e4Smiod nlm_version_header (abfd)->minorVersion =
190*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.minorVersion);
191*3d8817e4Smiod nlm_version_header (abfd)->revision =
192*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.revision);
193*3d8817e4Smiod nlm_version_header (abfd)->year =
194*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.year);
195*3d8817e4Smiod nlm_version_header (abfd)->month =
196*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.month);
197*3d8817e4Smiod nlm_version_header (abfd)->day =
198*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.day);
199*3d8817e4Smiod }
200*3d8817e4Smiod else if (strncmp (tempstr, "MeSsAgEs", 8) == 0)
201*3d8817e4Smiod {
202*3d8817e4Smiod Nlm_External_Extended_Header thdr;
203*3d8817e4Smiod
204*3d8817e4Smiod amt = sizeof (thdr);
205*3d8817e4Smiod if (bfd_bread ((void *) &thdr, amt, abfd) != amt)
206*3d8817e4Smiod return FALSE;
207*3d8817e4Smiod memcpy (nlm_extended_header (abfd)->stamp, thdr.stamp,
208*3d8817e4Smiod sizeof (thdr.stamp));
209*3d8817e4Smiod nlm_extended_header (abfd)->languageID =
210*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.languageID);
211*3d8817e4Smiod nlm_extended_header (abfd)->messageFileOffset =
212*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.messageFileOffset);
213*3d8817e4Smiod nlm_extended_header (abfd)->messageFileLength =
214*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.messageFileLength);
215*3d8817e4Smiod nlm_extended_header (abfd)->messageCount =
216*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.messageCount);
217*3d8817e4Smiod nlm_extended_header (abfd)->helpFileOffset =
218*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.helpFileOffset);
219*3d8817e4Smiod nlm_extended_header (abfd)->helpFileLength =
220*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.helpFileLength);
221*3d8817e4Smiod nlm_extended_header (abfd)->RPCDataOffset =
222*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.RPCDataOffset);
223*3d8817e4Smiod nlm_extended_header (abfd)->RPCDataLength =
224*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.RPCDataLength);
225*3d8817e4Smiod nlm_extended_header (abfd)->sharedCodeOffset =
226*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedCodeOffset);
227*3d8817e4Smiod nlm_extended_header (abfd)->sharedCodeLength =
228*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedCodeLength);
229*3d8817e4Smiod nlm_extended_header (abfd)->sharedDataOffset =
230*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedDataOffset);
231*3d8817e4Smiod nlm_extended_header (abfd)->sharedDataLength =
232*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedDataLength);
233*3d8817e4Smiod nlm_extended_header (abfd)->sharedRelocationFixupOffset =
234*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupOffset);
235*3d8817e4Smiod nlm_extended_header (abfd)->sharedRelocationFixupCount =
236*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedRelocationFixupCount);
237*3d8817e4Smiod nlm_extended_header (abfd)->sharedExternalReferenceOffset =
238*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceOffset);
239*3d8817e4Smiod nlm_extended_header (abfd)->sharedExternalReferenceCount =
240*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedExternalReferenceCount);
241*3d8817e4Smiod nlm_extended_header (abfd)->sharedPublicsOffset =
242*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedPublicsOffset);
243*3d8817e4Smiod nlm_extended_header (abfd)->sharedPublicsCount =
244*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedPublicsCount);
245*3d8817e4Smiod nlm_extended_header (abfd)->sharedDebugRecordOffset =
246*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordOffset);
247*3d8817e4Smiod nlm_extended_header (abfd)->sharedDebugRecordCount =
248*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedDebugRecordCount);
249*3d8817e4Smiod nlm_extended_header (abfd)->SharedInitializationOffset =
250*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.sharedInitializationOffset);
251*3d8817e4Smiod nlm_extended_header (abfd)->SharedExitProcedureOffset =
252*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.SharedExitProcedureOffset);
253*3d8817e4Smiod nlm_extended_header (abfd)->productID =
254*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.productID);
255*3d8817e4Smiod nlm_extended_header (abfd)->reserved0 =
256*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.reserved0);
257*3d8817e4Smiod nlm_extended_header (abfd)->reserved1 =
258*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.reserved1);
259*3d8817e4Smiod nlm_extended_header (abfd)->reserved2 =
260*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.reserved2);
261*3d8817e4Smiod nlm_extended_header (abfd)->reserved3 =
262*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.reserved3);
263*3d8817e4Smiod nlm_extended_header (abfd)->reserved4 =
264*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.reserved4);
265*3d8817e4Smiod nlm_extended_header (abfd)->reserved5 =
266*3d8817e4Smiod get_word (abfd, (bfd_byte *) thdr.reserved5);
267*3d8817e4Smiod }
268*3d8817e4Smiod else if (strncmp (tempstr, "CoPyRiGhT=", 10) == 0)
269*3d8817e4Smiod {
270*3d8817e4Smiod amt = sizeof (nlm_copyright_header (abfd)->stamp);
271*3d8817e4Smiod if (bfd_bread ((void *) nlm_copyright_header (abfd)->stamp,
272*3d8817e4Smiod amt, abfd) != amt)
273*3d8817e4Smiod return FALSE;
274*3d8817e4Smiod if (bfd_bread ((void *) &(nlm_copyright_header (abfd)
275*3d8817e4Smiod ->copyrightMessageLength),
276*3d8817e4Smiod (bfd_size_type) 1, abfd) != 1)
277*3d8817e4Smiod return FALSE;
278*3d8817e4Smiod /* The copyright message is a variable length string. */
279*3d8817e4Smiod amt = nlm_copyright_header (abfd)->copyrightMessageLength + 1;
280*3d8817e4Smiod if (bfd_bread ((void *) nlm_copyright_header (abfd)->copyrightMessage,
281*3d8817e4Smiod amt, abfd) != amt)
282*3d8817e4Smiod return FALSE;
283*3d8817e4Smiod }
284*3d8817e4Smiod else if (strncmp (tempstr, "CuStHeAd", 8) == 0)
285*3d8817e4Smiod {
286*3d8817e4Smiod Nlm_External_Custom_Header thdr;
287*3d8817e4Smiod bfd_size_type hdrLength;
288*3d8817e4Smiod file_ptr dataOffset;
289*3d8817e4Smiod bfd_size_type dataLength;
290*3d8817e4Smiod char dataStamp[8];
291*3d8817e4Smiod void * hdr;
292*3d8817e4Smiod
293*3d8817e4Smiod /* Read the stamp ("CuStHeAd"). */
294*3d8817e4Smiod amt = sizeof (thdr.stamp);
295*3d8817e4Smiod if (bfd_bread ((void *) thdr.stamp, amt, abfd) != amt)
296*3d8817e4Smiod return FALSE;
297*3d8817e4Smiod /* Read the length of this custom header. */
298*3d8817e4Smiod amt = sizeof (thdr.length);
299*3d8817e4Smiod if (bfd_bread ((void *) thdr.length, amt, abfd) != amt)
300*3d8817e4Smiod return FALSE;
301*3d8817e4Smiod hdrLength = get_word (abfd, (bfd_byte *) thdr.length);
302*3d8817e4Smiod /* Read further fields if we have them. */
303*3d8817e4Smiod if (hdrLength < NLM_TARGET_LONG_SIZE)
304*3d8817e4Smiod dataOffset = 0;
305*3d8817e4Smiod else
306*3d8817e4Smiod {
307*3d8817e4Smiod amt = sizeof (thdr.dataOffset);
308*3d8817e4Smiod if (bfd_bread ((void *) thdr.dataOffset, amt, abfd) != amt)
309*3d8817e4Smiod return FALSE;
310*3d8817e4Smiod dataOffset = get_word (abfd, (bfd_byte *) thdr.dataOffset);
311*3d8817e4Smiod }
312*3d8817e4Smiod if (hdrLength < 2 * NLM_TARGET_LONG_SIZE)
313*3d8817e4Smiod dataLength = 0;
314*3d8817e4Smiod else
315*3d8817e4Smiod {
316*3d8817e4Smiod amt = sizeof (thdr.dataLength);
317*3d8817e4Smiod if (bfd_bread ((void *) thdr.dataLength, amt, abfd) != amt)
318*3d8817e4Smiod return FALSE;
319*3d8817e4Smiod dataLength = get_word (abfd, (bfd_byte *) thdr.dataLength);
320*3d8817e4Smiod }
321*3d8817e4Smiod if (hdrLength < 2 * NLM_TARGET_LONG_SIZE + 8)
322*3d8817e4Smiod memset (dataStamp, 0, sizeof (dataStamp));
323*3d8817e4Smiod else
324*3d8817e4Smiod {
325*3d8817e4Smiod amt = sizeof (dataStamp);
326*3d8817e4Smiod if (bfd_bread ((void *) dataStamp, amt, abfd) != amt)
327*3d8817e4Smiod return FALSE;
328*3d8817e4Smiod }
329*3d8817e4Smiod
330*3d8817e4Smiod /* Read the rest of the header, if any. */
331*3d8817e4Smiod if (hdrLength <= 2 * NLM_TARGET_LONG_SIZE + 8)
332*3d8817e4Smiod {
333*3d8817e4Smiod hdr = NULL;
334*3d8817e4Smiod hdrLength = 0;
335*3d8817e4Smiod }
336*3d8817e4Smiod else
337*3d8817e4Smiod {
338*3d8817e4Smiod hdrLength -= 2 * NLM_TARGET_LONG_SIZE + 8;
339*3d8817e4Smiod hdr = bfd_alloc (abfd, hdrLength);
340*3d8817e4Smiod if (hdr == NULL)
341*3d8817e4Smiod return FALSE;
342*3d8817e4Smiod if (bfd_bread (hdr, hdrLength, abfd) != hdrLength)
343*3d8817e4Smiod return FALSE;
344*3d8817e4Smiod }
345*3d8817e4Smiod
346*3d8817e4Smiod /* If we have found a Cygnus header, process it. Otherwise,
347*3d8817e4Smiod just save the associated data without trying to interpret
348*3d8817e4Smiod it. */
349*3d8817e4Smiod if (strncmp (dataStamp, "CyGnUsEx", 8) == 0)
350*3d8817e4Smiod {
351*3d8817e4Smiod file_ptr pos;
352*3d8817e4Smiod bfd_byte *contents;
353*3d8817e4Smiod bfd_byte *p, *pend;
354*3d8817e4Smiod
355*3d8817e4Smiod BFD_ASSERT (hdrLength == 0 && hdr == NULL);
356*3d8817e4Smiod
357*3d8817e4Smiod pos = bfd_tell (abfd);
358*3d8817e4Smiod if (bfd_seek (abfd, dataOffset, SEEK_SET) != 0)
359*3d8817e4Smiod return FALSE;
360*3d8817e4Smiod contents = bfd_alloc (abfd, dataLength);
361*3d8817e4Smiod if (contents == NULL)
362*3d8817e4Smiod return FALSE;
363*3d8817e4Smiod if (bfd_bread (contents, dataLength, abfd) != dataLength)
364*3d8817e4Smiod return FALSE;
365*3d8817e4Smiod if (bfd_seek (abfd, pos, SEEK_SET) != 0)
366*3d8817e4Smiod return FALSE;
367*3d8817e4Smiod
368*3d8817e4Smiod memcpy (nlm_cygnus_ext_header (abfd), "CyGnUsEx", 8);
369*3d8817e4Smiod nlm_cygnus_ext_header (abfd)->offset = dataOffset;
370*3d8817e4Smiod nlm_cygnus_ext_header (abfd)->length = dataLength;
371*3d8817e4Smiod
372*3d8817e4Smiod /* This data this header points to provides a list of
373*3d8817e4Smiod the sections which were in the original object file
374*3d8817e4Smiod which was converted to become an NLM. We locate
375*3d8817e4Smiod those sections and add them to the BFD. Note that
376*3d8817e4Smiod this is likely to create a second .text, .data and
377*3d8817e4Smiod .bss section; retrieving the sections by name will
378*3d8817e4Smiod get the actual NLM sections, which is what we want to
379*3d8817e4Smiod happen. The sections from the original file, which
380*3d8817e4Smiod may be subsets of the NLM section, can only be found
381*3d8817e4Smiod using bfd_map_over_sections. */
382*3d8817e4Smiod p = contents;
383*3d8817e4Smiod pend = p + dataLength;
384*3d8817e4Smiod while (p < pend)
385*3d8817e4Smiod {
386*3d8817e4Smiod char *name;
387*3d8817e4Smiod size_t l;
388*3d8817e4Smiod file_ptr filepos;
389*3d8817e4Smiod bfd_size_type size;
390*3d8817e4Smiod asection *newsec;
391*3d8817e4Smiod
392*3d8817e4Smiod /* The format of this information is
393*3d8817e4Smiod null terminated section name
394*3d8817e4Smiod zeroes to adjust to 4 byte boundary
395*3d8817e4Smiod 4 byte section data file pointer
396*3d8817e4Smiod 4 byte section size. */
397*3d8817e4Smiod
398*3d8817e4Smiod name = (char *) p;
399*3d8817e4Smiod l = strlen (name) + 1;
400*3d8817e4Smiod l = (l + 3) &~ (size_t) 3;
401*3d8817e4Smiod p += l;
402*3d8817e4Smiod filepos = H_GET_32 (abfd, p);
403*3d8817e4Smiod p += 4;
404*3d8817e4Smiod size = H_GET_32 (abfd, p);
405*3d8817e4Smiod p += 4;
406*3d8817e4Smiod
407*3d8817e4Smiod newsec = bfd_make_section_anyway (abfd, name);
408*3d8817e4Smiod if (newsec == NULL)
409*3d8817e4Smiod return FALSE;
410*3d8817e4Smiod newsec->size = size;
411*3d8817e4Smiod if (filepos != 0)
412*3d8817e4Smiod {
413*3d8817e4Smiod newsec->filepos = filepos;
414*3d8817e4Smiod newsec->flags |= SEC_HAS_CONTENTS;
415*3d8817e4Smiod }
416*3d8817e4Smiod }
417*3d8817e4Smiod }
418*3d8817e4Smiod else
419*3d8817e4Smiod {
420*3d8817e4Smiod memcpy (nlm_custom_header (abfd)->stamp, thdr.stamp,
421*3d8817e4Smiod sizeof (thdr.stamp));
422*3d8817e4Smiod nlm_custom_header (abfd)->hdrLength = hdrLength;
423*3d8817e4Smiod nlm_custom_header (abfd)->dataOffset = dataOffset;
424*3d8817e4Smiod nlm_custom_header (abfd)->dataLength = dataLength;
425*3d8817e4Smiod memcpy (nlm_custom_header (abfd)->dataStamp, dataStamp,
426*3d8817e4Smiod sizeof (dataStamp));
427*3d8817e4Smiod nlm_custom_header (abfd)->hdr = hdr;
428*3d8817e4Smiod }
429*3d8817e4Smiod }
430*3d8817e4Smiod else
431*3d8817e4Smiod break;
432*3d8817e4Smiod }
433*3d8817e4Smiod return TRUE;
434*3d8817e4Smiod }
435*3d8817e4Smiod
436*3d8817e4Smiod const bfd_target *
nlm_object_p(bfd * abfd)437*3d8817e4Smiod nlm_object_p (bfd *abfd)
438*3d8817e4Smiod {
439*3d8817e4Smiod struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd);
440*3d8817e4Smiod bfd_boolean (*backend_object_p) (bfd *);
441*3d8817e4Smiod void * x_fxdhdr = NULL;
442*3d8817e4Smiod Nlm_Internal_Fixed_Header *i_fxdhdrp;
443*3d8817e4Smiod struct nlm_obj_tdata *new_tdata = NULL;
444*3d8817e4Smiod const char *signature;
445*3d8817e4Smiod enum bfd_architecture arch;
446*3d8817e4Smiod bfd_size_type amt;
447*3d8817e4Smiod
448*3d8817e4Smiod /* Some NLM formats have a prefix before the standard NLM fixed
449*3d8817e4Smiod header. */
450*3d8817e4Smiod backend_object_p = nlm_backend_object_p_func (abfd);
451*3d8817e4Smiod if (backend_object_p)
452*3d8817e4Smiod {
453*3d8817e4Smiod if (!(*backend_object_p) (abfd))
454*3d8817e4Smiod goto got_wrong_format_error;
455*3d8817e4Smiod }
456*3d8817e4Smiod
457*3d8817e4Smiod /* Read in the fixed length portion of the NLM header in external format. */
458*3d8817e4Smiod amt = nlm_fixed_header_size (abfd);
459*3d8817e4Smiod x_fxdhdr = bfd_malloc (amt);
460*3d8817e4Smiod if (x_fxdhdr == NULL)
461*3d8817e4Smiod goto got_no_match;
462*3d8817e4Smiod
463*3d8817e4Smiod if (bfd_bread ((void *) x_fxdhdr, amt, abfd) != amt)
464*3d8817e4Smiod {
465*3d8817e4Smiod if (bfd_get_error () != bfd_error_system_call)
466*3d8817e4Smiod goto got_wrong_format_error;
467*3d8817e4Smiod else
468*3d8817e4Smiod goto got_no_match;
469*3d8817e4Smiod }
470*3d8817e4Smiod
471*3d8817e4Smiod /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
472*3d8817e4Smiod the tdata pointer in the bfd. */
473*3d8817e4Smiod amt = sizeof (struct nlm_obj_tdata);
474*3d8817e4Smiod new_tdata = bfd_zalloc (abfd, amt);
475*3d8817e4Smiod if (new_tdata == NULL)
476*3d8817e4Smiod goto got_no_match;
477*3d8817e4Smiod
478*3d8817e4Smiod nlm_tdata (abfd) = new_tdata;
479*3d8817e4Smiod
480*3d8817e4Smiod i_fxdhdrp = nlm_fixed_header (abfd);
481*3d8817e4Smiod nlm_swap_fixed_header_in (abfd, x_fxdhdr, i_fxdhdrp);
482*3d8817e4Smiod free (x_fxdhdr);
483*3d8817e4Smiod x_fxdhdr = NULL;
484*3d8817e4Smiod
485*3d8817e4Smiod /* Check to see if we have an NLM file for this backend by matching
486*3d8817e4Smiod the NLM signature. */
487*3d8817e4Smiod signature = nlm_signature (abfd);
488*3d8817e4Smiod if (signature != NULL
489*3d8817e4Smiod && *signature != '\0'
490*3d8817e4Smiod && strncmp ((char *) i_fxdhdrp->signature, signature,
491*3d8817e4Smiod NLM_SIGNATURE_SIZE) != 0)
492*3d8817e4Smiod goto got_wrong_format_error;
493*3d8817e4Smiod
494*3d8817e4Smiod /* There's no supported way to discover the endianess of an NLM, so test for
495*3d8817e4Smiod a sane version number after doing byte swapping appropriate for this
496*3d8817e4Smiod XVEC. (Hack alert!) */
497*3d8817e4Smiod if (i_fxdhdrp->version > 0xFFFF)
498*3d8817e4Smiod goto got_wrong_format_error;
499*3d8817e4Smiod
500*3d8817e4Smiod /* There's no supported way to check for 32 bit versus 64 bit addresses,
501*3d8817e4Smiod so ignore this distinction for now. (FIXME) */
502*3d8817e4Smiod /* Swap in the rest of the required header. */
503*3d8817e4Smiod if (!nlm_swap_variable_header_in (abfd))
504*3d8817e4Smiod {
505*3d8817e4Smiod if (bfd_get_error () != bfd_error_system_call)
506*3d8817e4Smiod goto got_wrong_format_error;
507*3d8817e4Smiod else
508*3d8817e4Smiod goto got_no_match;
509*3d8817e4Smiod }
510*3d8817e4Smiod
511*3d8817e4Smiod /* Add the sections supplied by all NLM's, and then read in the
512*3d8817e4Smiod auxiliary headers. Reading the auxiliary headers may create
513*3d8817e4Smiod additional sections described in the cygnus_ext header.
514*3d8817e4Smiod From this point on we assume that we have an NLM, and do not
515*3d8817e4Smiod treat errors as indicating the wrong format. */
516*3d8817e4Smiod if (!add_bfd_section (abfd, NLM_CODE_NAME,
517*3d8817e4Smiod i_fxdhdrp->codeImageOffset,
518*3d8817e4Smiod i_fxdhdrp->codeImageSize,
519*3d8817e4Smiod (SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
520*3d8817e4Smiod | SEC_RELOC))
521*3d8817e4Smiod || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
522*3d8817e4Smiod i_fxdhdrp->dataImageOffset,
523*3d8817e4Smiod i_fxdhdrp->dataImageSize,
524*3d8817e4Smiod (SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
525*3d8817e4Smiod | SEC_RELOC))
526*3d8817e4Smiod || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
527*3d8817e4Smiod (file_ptr) 0,
528*3d8817e4Smiod i_fxdhdrp->uninitializedDataSize,
529*3d8817e4Smiod SEC_ALLOC))
530*3d8817e4Smiod goto got_no_match;
531*3d8817e4Smiod
532*3d8817e4Smiod if (!nlm_swap_auxiliary_headers_in (abfd))
533*3d8817e4Smiod goto got_no_match;
534*3d8817e4Smiod
535*3d8817e4Smiod if (nlm_fixed_header (abfd)->numberOfRelocationFixups != 0
536*3d8817e4Smiod || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
537*3d8817e4Smiod abfd->flags |= HAS_RELOC;
538*3d8817e4Smiod if (nlm_fixed_header (abfd)->numberOfPublics != 0
539*3d8817e4Smiod || nlm_fixed_header (abfd)->numberOfDebugRecords != 0
540*3d8817e4Smiod || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
541*3d8817e4Smiod abfd->flags |= HAS_SYMS;
542*3d8817e4Smiod
543*3d8817e4Smiod arch = nlm_architecture (abfd);
544*3d8817e4Smiod if (arch != bfd_arch_unknown)
545*3d8817e4Smiod bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
546*3d8817e4Smiod
547*3d8817e4Smiod abfd->flags |= EXEC_P;
548*3d8817e4Smiod bfd_get_start_address (abfd) = nlm_fixed_header (abfd)->codeStartOffset;
549*3d8817e4Smiod
550*3d8817e4Smiod return abfd->xvec;
551*3d8817e4Smiod
552*3d8817e4Smiod got_wrong_format_error:
553*3d8817e4Smiod bfd_set_error (bfd_error_wrong_format);
554*3d8817e4Smiod got_no_match:
555*3d8817e4Smiod nlm_tdata (abfd) = preserved_tdata;
556*3d8817e4Smiod if (new_tdata != NULL)
557*3d8817e4Smiod bfd_release (abfd, new_tdata);
558*3d8817e4Smiod if (x_fxdhdr != NULL)
559*3d8817e4Smiod free (x_fxdhdr);
560*3d8817e4Smiod
561*3d8817e4Smiod return NULL;
562*3d8817e4Smiod }
563*3d8817e4Smiod
564*3d8817e4Smiod /* Swap and write out the variable length header. All the fields must
565*3d8817e4Smiod exist in the NLM, and must exist in this order. */
566*3d8817e4Smiod
567*3d8817e4Smiod static bfd_boolean
nlm_swap_variable_header_out(bfd * abfd)568*3d8817e4Smiod nlm_swap_variable_header_out (bfd *abfd)
569*3d8817e4Smiod {
570*3d8817e4Smiod bfd_byte temp[NLM_TARGET_LONG_SIZE];
571*3d8817e4Smiod bfd_size_type amt;
572*3d8817e4Smiod
573*3d8817e4Smiod /* Write the description length and text members. */
574*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->descriptionLength);
575*3d8817e4Smiod if (bfd_bwrite (& nlm_variable_header (abfd)->descriptionLength, amt,
576*3d8817e4Smiod abfd) != amt)
577*3d8817e4Smiod return FALSE;
578*3d8817e4Smiod amt = nlm_variable_header (abfd)->descriptionLength + 1;
579*3d8817e4Smiod if (bfd_bwrite ((void *) nlm_variable_header (abfd)->descriptionText, amt,
580*3d8817e4Smiod abfd) != amt)
581*3d8817e4Smiod return FALSE;
582*3d8817e4Smiod
583*3d8817e4Smiod /* Convert and write the stackSize field. */
584*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->stackSize, temp);
585*3d8817e4Smiod amt = sizeof (temp);
586*3d8817e4Smiod if (bfd_bwrite (temp, amt, abfd) != amt)
587*3d8817e4Smiod return FALSE;
588*3d8817e4Smiod
589*3d8817e4Smiod /* Convert and write the reserved field. */
590*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->reserved, temp);
591*3d8817e4Smiod amt = sizeof (temp);
592*3d8817e4Smiod if (bfd_bwrite (temp, amt, abfd) != amt)
593*3d8817e4Smiod return FALSE;
594*3d8817e4Smiod
595*3d8817e4Smiod /* Write the oldThreadName field. This field is a fixed length string. */
596*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->oldThreadName);
597*3d8817e4Smiod if (bfd_bwrite (nlm_variable_header (abfd)->oldThreadName, amt,
598*3d8817e4Smiod abfd) != amt)
599*3d8817e4Smiod return FALSE;
600*3d8817e4Smiod
601*3d8817e4Smiod /* Write the screen name length and text members. */
602*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->screenNameLength);
603*3d8817e4Smiod if (bfd_bwrite (& nlm_variable_header (abfd)->screenNameLength, amt,
604*3d8817e4Smiod abfd) != amt)
605*3d8817e4Smiod return FALSE;
606*3d8817e4Smiod amt = nlm_variable_header (abfd)->screenNameLength + 1;
607*3d8817e4Smiod if (bfd_bwrite (nlm_variable_header (abfd)->screenName, amt, abfd) != amt)
608*3d8817e4Smiod return FALSE;
609*3d8817e4Smiod
610*3d8817e4Smiod /* Write the thread name length and text members. */
611*3d8817e4Smiod amt = sizeof (nlm_variable_header (abfd)->threadNameLength);
612*3d8817e4Smiod if (bfd_bwrite (& nlm_variable_header (abfd)->threadNameLength, amt,
613*3d8817e4Smiod abfd) != amt)
614*3d8817e4Smiod return FALSE;
615*3d8817e4Smiod amt = nlm_variable_header (abfd)->threadNameLength + 1;
616*3d8817e4Smiod if (bfd_bwrite (nlm_variable_header (abfd)->threadName, amt, abfd) != amt)
617*3d8817e4Smiod return FALSE;
618*3d8817e4Smiod return TRUE;
619*3d8817e4Smiod }
620*3d8817e4Smiod
621*3d8817e4Smiod /* Return whether there is a non-zero byte in a memory block. */
622*3d8817e4Smiod
623*3d8817e4Smiod static bfd_boolean
find_nonzero(void * buf,size_t size)624*3d8817e4Smiod find_nonzero (void * buf, size_t size)
625*3d8817e4Smiod {
626*3d8817e4Smiod char *p = (char *) buf;
627*3d8817e4Smiod
628*3d8817e4Smiod while (size-- != 0)
629*3d8817e4Smiod if (*p++ != 0)
630*3d8817e4Smiod return TRUE;
631*3d8817e4Smiod return FALSE;
632*3d8817e4Smiod }
633*3d8817e4Smiod
634*3d8817e4Smiod /* Swap out the contents of the auxiliary headers. We create those
635*3d8817e4Smiod auxiliary headers which have been set non-zero. We do not require
636*3d8817e4Smiod the caller to set up the stamp fields. */
637*3d8817e4Smiod
638*3d8817e4Smiod static bfd_boolean
nlm_swap_auxiliary_headers_out(bfd * abfd)639*3d8817e4Smiod nlm_swap_auxiliary_headers_out (bfd *abfd)
640*3d8817e4Smiod {
641*3d8817e4Smiod bfd_size_type amt;
642*3d8817e4Smiod
643*3d8817e4Smiod /* Write out the version header if there is one. */
644*3d8817e4Smiod if (find_nonzero (nlm_version_header (abfd),
645*3d8817e4Smiod sizeof (Nlm_Internal_Version_Header)))
646*3d8817e4Smiod {
647*3d8817e4Smiod Nlm_External_Version_Header thdr;
648*3d8817e4Smiod
649*3d8817e4Smiod memcpy (thdr.stamp, "VeRsIoN#", 8);
650*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_version_header (abfd)->majorVersion,
651*3d8817e4Smiod (bfd_byte *) thdr.majorVersion);
652*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_version_header (abfd)->minorVersion,
653*3d8817e4Smiod (bfd_byte *) thdr.minorVersion);
654*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_version_header (abfd)->revision,
655*3d8817e4Smiod (bfd_byte *) thdr.revision);
656*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_version_header (abfd)->year,
657*3d8817e4Smiod (bfd_byte *) thdr.year);
658*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_version_header (abfd)->month,
659*3d8817e4Smiod (bfd_byte *) thdr.month);
660*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_version_header (abfd)->day,
661*3d8817e4Smiod (bfd_byte *) thdr.day);
662*3d8817e4Smiod if (bfd_bwrite ((void *) &thdr, (bfd_size_type) sizeof (thdr), abfd)
663*3d8817e4Smiod != sizeof (thdr))
664*3d8817e4Smiod return FALSE;
665*3d8817e4Smiod }
666*3d8817e4Smiod
667*3d8817e4Smiod /* Note - the CoPyRiGhT tag is emitted before the MeSsAgEs
668*3d8817e4Smiod tag in order to make the NW4.x and NW5.x loaders happy. */
669*3d8817e4Smiod
670*3d8817e4Smiod /* Write out the copyright header if there is one. */
671*3d8817e4Smiod if (find_nonzero (nlm_copyright_header (abfd),
672*3d8817e4Smiod sizeof (Nlm_Internal_Copyright_Header)))
673*3d8817e4Smiod {
674*3d8817e4Smiod Nlm_External_Copyright_Header thdr;
675*3d8817e4Smiod
676*3d8817e4Smiod memcpy (thdr.stamp, "CoPyRiGhT=", 10);
677*3d8817e4Smiod amt = sizeof (thdr.stamp);
678*3d8817e4Smiod if (bfd_bwrite ((void *) thdr.stamp, amt, abfd) != amt)
679*3d8817e4Smiod return FALSE;
680*3d8817e4Smiod thdr.copyrightMessageLength[0] =
681*3d8817e4Smiod nlm_copyright_header (abfd)->copyrightMessageLength;
682*3d8817e4Smiod amt = 1;
683*3d8817e4Smiod if (bfd_bwrite ((void *) thdr.copyrightMessageLength, amt, abfd) != amt)
684*3d8817e4Smiod return FALSE;
685*3d8817e4Smiod /* The copyright message is a variable length string. */
686*3d8817e4Smiod amt = nlm_copyright_header (abfd)->copyrightMessageLength + 1;
687*3d8817e4Smiod if (bfd_bwrite ((void *) nlm_copyright_header (abfd)->copyrightMessage,
688*3d8817e4Smiod amt, abfd) != amt)
689*3d8817e4Smiod return FALSE;
690*3d8817e4Smiod }
691*3d8817e4Smiod
692*3d8817e4Smiod /* Write out the extended header if there is one. */
693*3d8817e4Smiod if (find_nonzero (nlm_extended_header (abfd),
694*3d8817e4Smiod sizeof (Nlm_Internal_Extended_Header)))
695*3d8817e4Smiod {
696*3d8817e4Smiod Nlm_External_Extended_Header thdr;
697*3d8817e4Smiod
698*3d8817e4Smiod memcpy (thdr.stamp, "MeSsAgEs", 8);
699*3d8817e4Smiod put_word (abfd,
700*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->languageID,
701*3d8817e4Smiod (bfd_byte *) thdr.languageID);
702*3d8817e4Smiod put_word (abfd,
703*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->messageFileOffset,
704*3d8817e4Smiod (bfd_byte *) thdr.messageFileOffset);
705*3d8817e4Smiod put_word (abfd,
706*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->messageFileLength,
707*3d8817e4Smiod (bfd_byte *) thdr.messageFileLength);
708*3d8817e4Smiod put_word (abfd,
709*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->messageCount,
710*3d8817e4Smiod (bfd_byte *) thdr.messageCount);
711*3d8817e4Smiod put_word (abfd,
712*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->helpFileOffset,
713*3d8817e4Smiod (bfd_byte *) thdr.helpFileOffset);
714*3d8817e4Smiod put_word (abfd,
715*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->helpFileLength,
716*3d8817e4Smiod (bfd_byte *) thdr.helpFileLength);
717*3d8817e4Smiod put_word (abfd,
718*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->RPCDataOffset,
719*3d8817e4Smiod (bfd_byte *) thdr.RPCDataOffset);
720*3d8817e4Smiod put_word (abfd,
721*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->RPCDataLength,
722*3d8817e4Smiod (bfd_byte *) thdr.RPCDataLength);
723*3d8817e4Smiod put_word (abfd,
724*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedCodeOffset,
725*3d8817e4Smiod (bfd_byte *) thdr.sharedCodeOffset);
726*3d8817e4Smiod put_word (abfd,
727*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedCodeLength,
728*3d8817e4Smiod (bfd_byte *) thdr.sharedCodeLength);
729*3d8817e4Smiod put_word (abfd,
730*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedDataOffset,
731*3d8817e4Smiod (bfd_byte *) thdr.sharedDataOffset);
732*3d8817e4Smiod put_word (abfd,
733*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedDataLength,
734*3d8817e4Smiod (bfd_byte *) thdr.sharedDataLength);
735*3d8817e4Smiod put_word (abfd,
736*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupOffset,
737*3d8817e4Smiod (bfd_byte *) thdr.sharedRelocationFixupOffset);
738*3d8817e4Smiod put_word (abfd,
739*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedRelocationFixupCount,
740*3d8817e4Smiod (bfd_byte *) thdr.sharedRelocationFixupCount);
741*3d8817e4Smiod put_word (abfd,
742*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceOffset,
743*3d8817e4Smiod (bfd_byte *) thdr.sharedExternalReferenceOffset);
744*3d8817e4Smiod put_word (abfd,
745*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedExternalReferenceCount,
746*3d8817e4Smiod (bfd_byte *) thdr.sharedExternalReferenceCount);
747*3d8817e4Smiod put_word (abfd,
748*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedPublicsOffset,
749*3d8817e4Smiod (bfd_byte *) thdr.sharedPublicsOffset);
750*3d8817e4Smiod put_word (abfd,
751*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedPublicsCount,
752*3d8817e4Smiod (bfd_byte *) thdr.sharedPublicsCount);
753*3d8817e4Smiod put_word (abfd,
754*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordOffset,
755*3d8817e4Smiod (bfd_byte *) thdr.sharedDebugRecordOffset);
756*3d8817e4Smiod put_word (abfd,
757*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->sharedDebugRecordCount,
758*3d8817e4Smiod (bfd_byte *) thdr.sharedDebugRecordCount);
759*3d8817e4Smiod put_word (abfd,
760*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->SharedInitializationOffset,
761*3d8817e4Smiod (bfd_byte *) thdr.sharedInitializationOffset);
762*3d8817e4Smiod put_word (abfd,
763*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->SharedExitProcedureOffset,
764*3d8817e4Smiod (bfd_byte *) thdr.SharedExitProcedureOffset);
765*3d8817e4Smiod put_word (abfd,
766*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->productID,
767*3d8817e4Smiod (bfd_byte *) thdr.productID);
768*3d8817e4Smiod put_word (abfd,
769*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->reserved0,
770*3d8817e4Smiod (bfd_byte *) thdr.reserved0);
771*3d8817e4Smiod put_word (abfd,
772*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->reserved1,
773*3d8817e4Smiod (bfd_byte *) thdr.reserved1);
774*3d8817e4Smiod put_word (abfd,
775*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->reserved2,
776*3d8817e4Smiod (bfd_byte *) thdr.reserved2);
777*3d8817e4Smiod put_word (abfd,
778*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->reserved3,
779*3d8817e4Smiod (bfd_byte *) thdr.reserved3);
780*3d8817e4Smiod put_word (abfd,
781*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->reserved4,
782*3d8817e4Smiod (bfd_byte *) thdr.reserved4);
783*3d8817e4Smiod put_word (abfd,
784*3d8817e4Smiod (bfd_vma) nlm_extended_header (abfd)->reserved5,
785*3d8817e4Smiod (bfd_byte *) thdr.reserved5);
786*3d8817e4Smiod if (bfd_bwrite ((void *) &thdr, (bfd_size_type) sizeof (thdr), abfd)
787*3d8817e4Smiod != sizeof (thdr))
788*3d8817e4Smiod return FALSE;
789*3d8817e4Smiod }
790*3d8817e4Smiod
791*3d8817e4Smiod /* Write out the custom header if there is one. */
792*3d8817e4Smiod if (find_nonzero (nlm_custom_header (abfd),
793*3d8817e4Smiod sizeof (Nlm_Internal_Custom_Header)))
794*3d8817e4Smiod {
795*3d8817e4Smiod Nlm_External_Custom_Header thdr;
796*3d8817e4Smiod bfd_boolean ds;
797*3d8817e4Smiod bfd_size_type hdrLength;
798*3d8817e4Smiod
799*3d8817e4Smiod ds = find_nonzero (nlm_custom_header (abfd)->dataStamp,
800*3d8817e4Smiod sizeof (nlm_custom_header (abfd)->dataStamp));
801*3d8817e4Smiod memcpy (thdr.stamp, "CuStHeAd", 8);
802*3d8817e4Smiod hdrLength = (2 * NLM_TARGET_LONG_SIZE + (ds ? 8 : 0)
803*3d8817e4Smiod + nlm_custom_header (abfd)->hdrLength);
804*3d8817e4Smiod put_word (abfd, hdrLength, thdr.length);
805*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataOffset,
806*3d8817e4Smiod thdr.dataOffset);
807*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_custom_header (abfd)->dataLength,
808*3d8817e4Smiod thdr.dataLength);
809*3d8817e4Smiod if (! ds)
810*3d8817e4Smiod {
811*3d8817e4Smiod BFD_ASSERT (nlm_custom_header (abfd)->hdrLength == 0);
812*3d8817e4Smiod amt = sizeof (thdr) - sizeof (thdr.dataStamp);
813*3d8817e4Smiod if (bfd_bwrite ((void *) &thdr, amt, abfd) != amt)
814*3d8817e4Smiod return FALSE;
815*3d8817e4Smiod }
816*3d8817e4Smiod else
817*3d8817e4Smiod {
818*3d8817e4Smiod memcpy (thdr.dataStamp, nlm_custom_header (abfd)->dataStamp,
819*3d8817e4Smiod sizeof (thdr.dataStamp));
820*3d8817e4Smiod amt = sizeof (thdr);
821*3d8817e4Smiod if (bfd_bwrite ((void *) &thdr, amt, abfd) != amt)
822*3d8817e4Smiod return FALSE;
823*3d8817e4Smiod amt = nlm_custom_header (abfd)->hdrLength;
824*3d8817e4Smiod if (bfd_bwrite (nlm_custom_header (abfd)->hdr, amt, abfd) != amt)
825*3d8817e4Smiod return FALSE;
826*3d8817e4Smiod }
827*3d8817e4Smiod }
828*3d8817e4Smiod
829*3d8817e4Smiod /* Write out the Cygnus debugging header if there is one. */
830*3d8817e4Smiod if (find_nonzero (nlm_cygnus_ext_header (abfd),
831*3d8817e4Smiod sizeof (Nlm_Internal_Cygnus_Ext_Header)))
832*3d8817e4Smiod {
833*3d8817e4Smiod Nlm_External_Custom_Header thdr;
834*3d8817e4Smiod
835*3d8817e4Smiod memcpy (thdr.stamp, "CuStHeAd", 8);
836*3d8817e4Smiod put_word (abfd, (bfd_vma) 2 * NLM_TARGET_LONG_SIZE + 8,
837*3d8817e4Smiod (bfd_byte *) thdr.length);
838*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_cygnus_ext_header (abfd)->offset,
839*3d8817e4Smiod (bfd_byte *) thdr.dataOffset);
840*3d8817e4Smiod put_word (abfd, (bfd_vma) nlm_cygnus_ext_header (abfd)->length,
841*3d8817e4Smiod (bfd_byte *) thdr.dataLength);
842*3d8817e4Smiod memcpy (thdr.dataStamp, "CyGnUsEx", 8);
843*3d8817e4Smiod amt = sizeof (thdr);
844*3d8817e4Smiod if (bfd_bwrite ((void *) &thdr, amt, abfd) != amt)
845*3d8817e4Smiod return FALSE;
846*3d8817e4Smiod }
847*3d8817e4Smiod
848*3d8817e4Smiod return TRUE;
849*3d8817e4Smiod }
850*3d8817e4Smiod
851*3d8817e4Smiod /* We read the NLM's public symbols and use it to generate a bfd symbol
852*3d8817e4Smiod table (hey, it's better than nothing) on a one-for-one basis. Thus
853*3d8817e4Smiod use the number of public symbols as the number of bfd symbols we will
854*3d8817e4Smiod have once we actually get around to reading them in.
855*3d8817e4Smiod
856*3d8817e4Smiod Return the number of bytes required to hold the symtab vector, based on
857*3d8817e4Smiod the count plus 1, since we will NULL terminate the vector allocated based
858*3d8817e4Smiod on this size. */
859*3d8817e4Smiod
860*3d8817e4Smiod long
nlm_get_symtab_upper_bound(bfd * abfd)861*3d8817e4Smiod nlm_get_symtab_upper_bound (bfd *abfd)
862*3d8817e4Smiod {
863*3d8817e4Smiod Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form. */
864*3d8817e4Smiod long symcount;
865*3d8817e4Smiod long symtab_size = 0;
866*3d8817e4Smiod
867*3d8817e4Smiod i_fxdhdrp = nlm_fixed_header (abfd);
868*3d8817e4Smiod symcount = (i_fxdhdrp->numberOfPublics
869*3d8817e4Smiod + i_fxdhdrp->numberOfDebugRecords
870*3d8817e4Smiod + i_fxdhdrp->numberOfExternalReferences);
871*3d8817e4Smiod symtab_size = (symcount + 1) * (sizeof (asymbol));
872*3d8817e4Smiod return symtab_size;
873*3d8817e4Smiod }
874*3d8817e4Smiod
875*3d8817e4Smiod /* Slurp in nlm symbol table.
876*3d8817e4Smiod
877*3d8817e4Smiod In the external (in-file) form, NLM export records are variable length,
878*3d8817e4Smiod with the following form:
879*3d8817e4Smiod
880*3d8817e4Smiod 1 byte length of the symbol name (N)
881*3d8817e4Smiod N bytes the symbol name
882*3d8817e4Smiod 4 bytes the symbol offset from start of it's section
883*3d8817e4Smiod
884*3d8817e4Smiod We also read in the debugging symbols and import records. Import
885*3d8817e4Smiod records are treated as undefined symbols. As we read the import
886*3d8817e4Smiod records we also read in the associated reloc information, which is
887*3d8817e4Smiod attached to the symbol.
888*3d8817e4Smiod
889*3d8817e4Smiod The bfd symbols are copied to SYMvoid *S.
890*3d8817e4Smiod
891*3d8817e4Smiod When we return, the bfd symcount is either zero or contains the correct
892*3d8817e4Smiod number of symbols. */
893*3d8817e4Smiod
894*3d8817e4Smiod static bfd_boolean
nlm_slurp_symbol_table(bfd * abfd)895*3d8817e4Smiod nlm_slurp_symbol_table (bfd *abfd)
896*3d8817e4Smiod {
897*3d8817e4Smiod Nlm_Internal_Fixed_Header *i_fxdhdrp; /* Nlm file header, internal form. */
898*3d8817e4Smiod bfd_size_type totsymcount; /* Number of NLM symbols. */
899*3d8817e4Smiod bfd_size_type symcount; /* Counter of NLM symbols. */
900*3d8817e4Smiod nlm_symbol_type *sym; /* Pointer to current bfd symbol. */
901*3d8817e4Smiod unsigned char symlength; /* Symbol length read into here. */
902*3d8817e4Smiod unsigned char symtype; /* Type of debugging symbol. */
903*3d8817e4Smiod bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* Symbol offsets read into here. */
904*3d8817e4Smiod bfd_boolean (*read_import_func) (bfd *, nlm_symbol_type *);
905*3d8817e4Smiod bfd_boolean (*set_public_section_func) (bfd *, nlm_symbol_type *);
906*3d8817e4Smiod bfd_size_type amt;
907*3d8817e4Smiod
908*3d8817e4Smiod if (nlm_get_symbols (abfd) != NULL)
909*3d8817e4Smiod return TRUE;
910*3d8817e4Smiod
911*3d8817e4Smiod /* Read each raw NLM symbol, using the information to create a canonical bfd
912*3d8817e4Smiod symbol table entry.
913*3d8817e4Smiod
914*3d8817e4Smiod Note that we allocate the initial bfd canonical symbol buffer based on a
915*3d8817e4Smiod one-to-one mapping of the NLM symbols to canonical symbols. We actually
916*3d8817e4Smiod use all the NLM symbols, so there will be no space left over at the end.
917*3d8817e4Smiod When we have all the symbols, we build the caller's pointer vector. */
918*3d8817e4Smiod
919*3d8817e4Smiod abfd->symcount = 0;
920*3d8817e4Smiod i_fxdhdrp = nlm_fixed_header (abfd);
921*3d8817e4Smiod totsymcount = (i_fxdhdrp->numberOfPublics
922*3d8817e4Smiod + i_fxdhdrp->numberOfDebugRecords
923*3d8817e4Smiod + i_fxdhdrp->numberOfExternalReferences);
924*3d8817e4Smiod if (totsymcount == 0)
925*3d8817e4Smiod return TRUE;
926*3d8817e4Smiod
927*3d8817e4Smiod if (bfd_seek (abfd, i_fxdhdrp->publicsOffset, SEEK_SET) != 0)
928*3d8817e4Smiod return FALSE;
929*3d8817e4Smiod
930*3d8817e4Smiod amt = totsymcount * sizeof (nlm_symbol_type);
931*3d8817e4Smiod sym = bfd_zalloc (abfd, amt);
932*3d8817e4Smiod if (!sym)
933*3d8817e4Smiod return FALSE;
934*3d8817e4Smiod nlm_set_symbols (abfd, sym);
935*3d8817e4Smiod
936*3d8817e4Smiod /* We use the bfd's symcount directly as the control count, so that early
937*3d8817e4Smiod termination of the loop leaves the symcount correct for the symbols that
938*3d8817e4Smiod were read. */
939*3d8817e4Smiod
940*3d8817e4Smiod set_public_section_func = nlm_set_public_section_func (abfd);
941*3d8817e4Smiod symcount = i_fxdhdrp->numberOfPublics;
942*3d8817e4Smiod while (abfd->symcount < symcount)
943*3d8817e4Smiod {
944*3d8817e4Smiod amt = sizeof (symlength);
945*3d8817e4Smiod if (bfd_bread ((void *) &symlength, amt, abfd) != amt)
946*3d8817e4Smiod return FALSE;
947*3d8817e4Smiod amt = symlength;
948*3d8817e4Smiod sym->symbol.the_bfd = abfd;
949*3d8817e4Smiod sym->symbol.name = bfd_alloc (abfd, amt + 1);
950*3d8817e4Smiod if (!sym->symbol.name)
951*3d8817e4Smiod return FALSE;
952*3d8817e4Smiod if (bfd_bread ((void *) sym->symbol.name, amt, abfd) != amt)
953*3d8817e4Smiod return FALSE;
954*3d8817e4Smiod /* Cast away const. */
955*3d8817e4Smiod ((char *) (sym->symbol.name))[symlength] = '\0';
956*3d8817e4Smiod amt = sizeof (temp);
957*3d8817e4Smiod if (bfd_bread ((void *) temp, amt, abfd) != amt)
958*3d8817e4Smiod return FALSE;
959*3d8817e4Smiod sym->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
960*3d8817e4Smiod sym->symbol.value = get_word (abfd, temp);
961*3d8817e4Smiod if (set_public_section_func)
962*3d8817e4Smiod {
963*3d8817e4Smiod /* Most backends can use the code below, but unfortunately
964*3d8817e4Smiod some use a different scheme. */
965*3d8817e4Smiod if (! (*set_public_section_func) (abfd, sym))
966*3d8817e4Smiod return FALSE;
967*3d8817e4Smiod }
968*3d8817e4Smiod else
969*3d8817e4Smiod {
970*3d8817e4Smiod if (sym->symbol.value & NLM_HIBIT)
971*3d8817e4Smiod {
972*3d8817e4Smiod sym->symbol.value &= ~NLM_HIBIT;
973*3d8817e4Smiod sym->symbol.flags |= BSF_FUNCTION;
974*3d8817e4Smiod sym->symbol.section =
975*3d8817e4Smiod bfd_get_section_by_name (abfd, NLM_CODE_NAME);
976*3d8817e4Smiod }
977*3d8817e4Smiod else
978*3d8817e4Smiod sym->symbol.section =
979*3d8817e4Smiod bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
980*3d8817e4Smiod }
981*3d8817e4Smiod sym->rcnt = 0;
982*3d8817e4Smiod abfd->symcount++;
983*3d8817e4Smiod sym++;
984*3d8817e4Smiod }
985*3d8817e4Smiod
986*3d8817e4Smiod /* Read the debugging records. */
987*3d8817e4Smiod
988*3d8817e4Smiod if (i_fxdhdrp->numberOfDebugRecords > 0)
989*3d8817e4Smiod {
990*3d8817e4Smiod if (bfd_seek (abfd, i_fxdhdrp->debugInfoOffset, SEEK_SET) != 0)
991*3d8817e4Smiod return FALSE;
992*3d8817e4Smiod
993*3d8817e4Smiod symcount += i_fxdhdrp->numberOfDebugRecords;
994*3d8817e4Smiod while (abfd->symcount < symcount)
995*3d8817e4Smiod {
996*3d8817e4Smiod amt = sizeof (symtype);
997*3d8817e4Smiod if (bfd_bread ((void *) &symtype, amt, abfd) != amt)
998*3d8817e4Smiod return FALSE;
999*3d8817e4Smiod amt = sizeof (temp);
1000*3d8817e4Smiod if (bfd_bread ((void *) temp, amt, abfd) != amt)
1001*3d8817e4Smiod return FALSE;
1002*3d8817e4Smiod amt = sizeof (symlength);
1003*3d8817e4Smiod if (bfd_bread ((void *) &symlength, amt, abfd) != amt)
1004*3d8817e4Smiod return FALSE;
1005*3d8817e4Smiod amt = symlength;
1006*3d8817e4Smiod sym->symbol.the_bfd = abfd;
1007*3d8817e4Smiod sym->symbol.name = bfd_alloc (abfd, amt + 1);
1008*3d8817e4Smiod if (!sym->symbol.name)
1009*3d8817e4Smiod return FALSE;
1010*3d8817e4Smiod if (bfd_bread ((void *) sym->symbol.name, amt, abfd) != amt)
1011*3d8817e4Smiod return FALSE;
1012*3d8817e4Smiod /* Cast away const. */
1013*3d8817e4Smiod ((char *) (sym->symbol.name))[symlength] = '\0';
1014*3d8817e4Smiod sym->symbol.flags = BSF_LOCAL;
1015*3d8817e4Smiod sym->symbol.value = get_word (abfd, temp);
1016*3d8817e4Smiod
1017*3d8817e4Smiod if (symtype == 0)
1018*3d8817e4Smiod sym->symbol.section =
1019*3d8817e4Smiod bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
1020*3d8817e4Smiod else if (symtype == 1)
1021*3d8817e4Smiod {
1022*3d8817e4Smiod sym->symbol.flags |= BSF_FUNCTION;
1023*3d8817e4Smiod sym->symbol.section =
1024*3d8817e4Smiod bfd_get_section_by_name (abfd, NLM_CODE_NAME);
1025*3d8817e4Smiod }
1026*3d8817e4Smiod else
1027*3d8817e4Smiod sym->symbol.section = bfd_abs_section_ptr;
1028*3d8817e4Smiod
1029*3d8817e4Smiod sym->rcnt = 0;
1030*3d8817e4Smiod abfd->symcount++;
1031*3d8817e4Smiod sym++;
1032*3d8817e4Smiod }
1033*3d8817e4Smiod }
1034*3d8817e4Smiod
1035*3d8817e4Smiod /* Read in the import records. We can only do this if we know how
1036*3d8817e4Smiod to read relocs for this target. */
1037*3d8817e4Smiod read_import_func = nlm_read_import_func (abfd);
1038*3d8817e4Smiod if (read_import_func != NULL)
1039*3d8817e4Smiod {
1040*3d8817e4Smiod if (bfd_seek (abfd, i_fxdhdrp->externalReferencesOffset, SEEK_SET) != 0)
1041*3d8817e4Smiod return FALSE;
1042*3d8817e4Smiod
1043*3d8817e4Smiod symcount += i_fxdhdrp->numberOfExternalReferences;
1044*3d8817e4Smiod while (abfd->symcount < symcount)
1045*3d8817e4Smiod {
1046*3d8817e4Smiod if (! (*read_import_func) (abfd, sym))
1047*3d8817e4Smiod return FALSE;
1048*3d8817e4Smiod sym++;
1049*3d8817e4Smiod abfd->symcount++;
1050*3d8817e4Smiod }
1051*3d8817e4Smiod }
1052*3d8817e4Smiod
1053*3d8817e4Smiod return TRUE;
1054*3d8817e4Smiod }
1055*3d8817e4Smiod
1056*3d8817e4Smiod /* Note that bfd_get_symcount is guaranteed to be zero if slurping the
1057*3d8817e4Smiod symbol table fails. */
1058*3d8817e4Smiod
1059*3d8817e4Smiod long
nlm_canonicalize_symtab(bfd * abfd,asymbol ** alocation)1060*3d8817e4Smiod nlm_canonicalize_symtab (bfd *abfd, asymbol **alocation)
1061*3d8817e4Smiod {
1062*3d8817e4Smiod nlm_symbol_type *symbase;
1063*3d8817e4Smiod bfd_size_type counter = 0;
1064*3d8817e4Smiod
1065*3d8817e4Smiod if (! nlm_slurp_symbol_table (abfd))
1066*3d8817e4Smiod return -1;
1067*3d8817e4Smiod symbase = nlm_get_symbols (abfd);
1068*3d8817e4Smiod while (counter < bfd_get_symcount (abfd))
1069*3d8817e4Smiod {
1070*3d8817e4Smiod *alocation++ = &symbase->symbol;
1071*3d8817e4Smiod symbase++;
1072*3d8817e4Smiod counter++;
1073*3d8817e4Smiod }
1074*3d8817e4Smiod *alocation = NULL;
1075*3d8817e4Smiod return bfd_get_symcount (abfd);
1076*3d8817e4Smiod }
1077*3d8817e4Smiod
1078*3d8817e4Smiod /* Make an NLM symbol. There is nothing special to do here. */
1079*3d8817e4Smiod
1080*3d8817e4Smiod asymbol *
nlm_make_empty_symbol(bfd * abfd)1081*3d8817e4Smiod nlm_make_empty_symbol (bfd *abfd)
1082*3d8817e4Smiod {
1083*3d8817e4Smiod bfd_size_type amt = sizeof (nlm_symbol_type);
1084*3d8817e4Smiod nlm_symbol_type *new = bfd_zalloc (abfd, amt);
1085*3d8817e4Smiod
1086*3d8817e4Smiod if (new == NULL)
1087*3d8817e4Smiod return NULL;
1088*3d8817e4Smiod new->symbol.the_bfd = abfd;
1089*3d8817e4Smiod return & new->symbol;
1090*3d8817e4Smiod }
1091*3d8817e4Smiod
1092*3d8817e4Smiod /* Get symbol information. */
1093*3d8817e4Smiod
1094*3d8817e4Smiod void
nlm_get_symbol_info(bfd * ignore_abfd ATTRIBUTE_UNUSED,asymbol * symbol,symbol_info * ret)1095*3d8817e4Smiod nlm_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
1096*3d8817e4Smiod asymbol *symbol,
1097*3d8817e4Smiod symbol_info *ret)
1098*3d8817e4Smiod {
1099*3d8817e4Smiod bfd_symbol_info (symbol, ret);
1100*3d8817e4Smiod }
1101*3d8817e4Smiod
1102*3d8817e4Smiod /* Print symbol information. */
1103*3d8817e4Smiod
1104*3d8817e4Smiod void
nlm_print_symbol(bfd * abfd,void * afile,asymbol * symbol,bfd_print_symbol_type how)1105*3d8817e4Smiod nlm_print_symbol (bfd *abfd,
1106*3d8817e4Smiod void * afile,
1107*3d8817e4Smiod asymbol *symbol,
1108*3d8817e4Smiod bfd_print_symbol_type how)
1109*3d8817e4Smiod {
1110*3d8817e4Smiod FILE *file = (FILE *) afile;
1111*3d8817e4Smiod
1112*3d8817e4Smiod switch (how)
1113*3d8817e4Smiod {
1114*3d8817e4Smiod case bfd_print_symbol_name:
1115*3d8817e4Smiod case bfd_print_symbol_more:
1116*3d8817e4Smiod if (symbol->name)
1117*3d8817e4Smiod fprintf (file, "%s", symbol->name);
1118*3d8817e4Smiod break;
1119*3d8817e4Smiod case bfd_print_symbol_all:
1120*3d8817e4Smiod bfd_print_symbol_vandf (abfd, (void *) file, symbol);
1121*3d8817e4Smiod fprintf (file, " %-5s", symbol->section->name);
1122*3d8817e4Smiod if (symbol->name)
1123*3d8817e4Smiod fprintf (file, " %s", symbol->name);
1124*3d8817e4Smiod break;
1125*3d8817e4Smiod }
1126*3d8817e4Smiod }
1127*3d8817e4Smiod
1128*3d8817e4Smiod /* Get the relocs for an NLM file. There are two types of relocs.
1129*3d8817e4Smiod Imports are relocs against symbols defined in other NLM files. We
1130*3d8817e4Smiod treat these as relocs against global symbols. Relocation fixups
1131*3d8817e4Smiod are internal relocs.
1132*3d8817e4Smiod
1133*3d8817e4Smiod The actual format used to store the relocs is machine specific. */
1134*3d8817e4Smiod
1135*3d8817e4Smiod /* Read in the relocation fixup information. This is stored in
1136*3d8817e4Smiod nlm_relocation_fixups, an array of arelent structures, and
1137*3d8817e4Smiod nlm_relocation_fixup_secs, an array of section pointers. The
1138*3d8817e4Smiod section pointers are needed because the relocs are not sorted by
1139*3d8817e4Smiod section. */
1140*3d8817e4Smiod
1141*3d8817e4Smiod static bfd_boolean
nlm_slurp_reloc_fixups(bfd * abfd)1142*3d8817e4Smiod nlm_slurp_reloc_fixups (bfd *abfd)
1143*3d8817e4Smiod {
1144*3d8817e4Smiod bfd_boolean (*read_func) (bfd *, nlm_symbol_type *, asection **, arelent *);
1145*3d8817e4Smiod bfd_size_type count, amt;
1146*3d8817e4Smiod arelent *rels;
1147*3d8817e4Smiod asection **secs;
1148*3d8817e4Smiod
1149*3d8817e4Smiod if (nlm_relocation_fixups (abfd) != NULL)
1150*3d8817e4Smiod return TRUE;
1151*3d8817e4Smiod read_func = nlm_read_reloc_func (abfd);
1152*3d8817e4Smiod if (read_func == NULL)
1153*3d8817e4Smiod return TRUE;
1154*3d8817e4Smiod
1155*3d8817e4Smiod if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
1156*3d8817e4Smiod SEEK_SET) != 0)
1157*3d8817e4Smiod return FALSE;
1158*3d8817e4Smiod
1159*3d8817e4Smiod count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1160*3d8817e4Smiod amt = count * sizeof (arelent);
1161*3d8817e4Smiod rels = bfd_alloc (abfd, amt);
1162*3d8817e4Smiod amt = count * sizeof (asection *);
1163*3d8817e4Smiod secs = bfd_alloc (abfd, amt);
1164*3d8817e4Smiod if ((rels == NULL || secs == NULL) && count != 0)
1165*3d8817e4Smiod return FALSE;
1166*3d8817e4Smiod nlm_relocation_fixups (abfd) = rels;
1167*3d8817e4Smiod nlm_relocation_fixup_secs (abfd) = secs;
1168*3d8817e4Smiod
1169*3d8817e4Smiod /* We have to read piece by piece, because we don't know how large
1170*3d8817e4Smiod the machine specific reloc information is. */
1171*3d8817e4Smiod while (count-- != 0)
1172*3d8817e4Smiod {
1173*3d8817e4Smiod if (! (*read_func) (abfd, NULL, secs, rels))
1174*3d8817e4Smiod {
1175*3d8817e4Smiod nlm_relocation_fixups (abfd) = NULL;
1176*3d8817e4Smiod nlm_relocation_fixup_secs (abfd) = NULL;
1177*3d8817e4Smiod return FALSE;
1178*3d8817e4Smiod }
1179*3d8817e4Smiod ++secs;
1180*3d8817e4Smiod ++rels;
1181*3d8817e4Smiod }
1182*3d8817e4Smiod
1183*3d8817e4Smiod return TRUE;
1184*3d8817e4Smiod }
1185*3d8817e4Smiod
1186*3d8817e4Smiod /* Get the number of relocs. This really just returns an upper bound,
1187*3d8817e4Smiod since it does not attempt to distinguish them based on the section.
1188*3d8817e4Smiod That will be handled when they are actually read. */
1189*3d8817e4Smiod
1190*3d8817e4Smiod long
nlm_get_reloc_upper_bound(bfd * abfd,asection * sec)1191*3d8817e4Smiod nlm_get_reloc_upper_bound (bfd *abfd, asection *sec)
1192*3d8817e4Smiod {
1193*3d8817e4Smiod nlm_symbol_type *syms;
1194*3d8817e4Smiod bfd_size_type count;
1195*3d8817e4Smiod unsigned int ret;
1196*3d8817e4Smiod
1197*3d8817e4Smiod /* If we don't know how to read relocs, just return 0. */
1198*3d8817e4Smiod if (nlm_read_reloc_func (abfd) == NULL)
1199*3d8817e4Smiod return -1;
1200*3d8817e4Smiod /* Make sure we have either the code or the data section. */
1201*3d8817e4Smiod if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
1202*3d8817e4Smiod return 0;
1203*3d8817e4Smiod
1204*3d8817e4Smiod syms = nlm_get_symbols (abfd);
1205*3d8817e4Smiod if (syms == NULL)
1206*3d8817e4Smiod {
1207*3d8817e4Smiod if (! nlm_slurp_symbol_table (abfd))
1208*3d8817e4Smiod return -1;
1209*3d8817e4Smiod syms = nlm_get_symbols (abfd);
1210*3d8817e4Smiod }
1211*3d8817e4Smiod
1212*3d8817e4Smiod ret = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1213*3d8817e4Smiod
1214*3d8817e4Smiod count = bfd_get_symcount (abfd);
1215*3d8817e4Smiod while (count-- != 0)
1216*3d8817e4Smiod {
1217*3d8817e4Smiod ret += syms->rcnt;
1218*3d8817e4Smiod ++syms;
1219*3d8817e4Smiod }
1220*3d8817e4Smiod
1221*3d8817e4Smiod return (ret + 1) * sizeof (arelent *);
1222*3d8817e4Smiod }
1223*3d8817e4Smiod
1224*3d8817e4Smiod /* Get the relocs themselves. */
1225*3d8817e4Smiod
1226*3d8817e4Smiod long
nlm_canonicalize_reloc(bfd * abfd,asection * sec,arelent ** relptr,asymbol ** symbols)1227*3d8817e4Smiod nlm_canonicalize_reloc (bfd *abfd,
1228*3d8817e4Smiod asection *sec,
1229*3d8817e4Smiod arelent **relptr,
1230*3d8817e4Smiod asymbol **symbols)
1231*3d8817e4Smiod {
1232*3d8817e4Smiod arelent *rels;
1233*3d8817e4Smiod asection **secs;
1234*3d8817e4Smiod bfd_size_type count, i;
1235*3d8817e4Smiod long ret;
1236*3d8817e4Smiod
1237*3d8817e4Smiod /* Get the relocation fixups. */
1238*3d8817e4Smiod rels = nlm_relocation_fixups (abfd);
1239*3d8817e4Smiod if (rels == NULL)
1240*3d8817e4Smiod {
1241*3d8817e4Smiod if (! nlm_slurp_reloc_fixups (abfd))
1242*3d8817e4Smiod return -1;
1243*3d8817e4Smiod rels = nlm_relocation_fixups (abfd);
1244*3d8817e4Smiod }
1245*3d8817e4Smiod secs = nlm_relocation_fixup_secs (abfd);
1246*3d8817e4Smiod
1247*3d8817e4Smiod ret = 0;
1248*3d8817e4Smiod count = nlm_fixed_header (abfd)->numberOfRelocationFixups;
1249*3d8817e4Smiod for (i = 0; i < count; i++, rels++, secs++)
1250*3d8817e4Smiod {
1251*3d8817e4Smiod if (*secs == sec)
1252*3d8817e4Smiod {
1253*3d8817e4Smiod *relptr++ = rels;
1254*3d8817e4Smiod ++ret;
1255*3d8817e4Smiod }
1256*3d8817e4Smiod }
1257*3d8817e4Smiod
1258*3d8817e4Smiod /* Get the import symbols. */
1259*3d8817e4Smiod count = bfd_get_symcount (abfd);
1260*3d8817e4Smiod for (i = 0; i < count; i++, symbols++)
1261*3d8817e4Smiod {
1262*3d8817e4Smiod asymbol *sym;
1263*3d8817e4Smiod
1264*3d8817e4Smiod sym = *symbols;
1265*3d8817e4Smiod if (bfd_asymbol_flavour (sym) == bfd_target_nlm_flavour)
1266*3d8817e4Smiod {
1267*3d8817e4Smiod nlm_symbol_type *nlm_sym;
1268*3d8817e4Smiod bfd_size_type j;
1269*3d8817e4Smiod
1270*3d8817e4Smiod nlm_sym = (nlm_symbol_type *) sym;
1271*3d8817e4Smiod for (j = 0; j < nlm_sym->rcnt; j++)
1272*3d8817e4Smiod {
1273*3d8817e4Smiod if (nlm_sym->relocs[j].section == sec)
1274*3d8817e4Smiod {
1275*3d8817e4Smiod *relptr = &nlm_sym->relocs[j].reloc;
1276*3d8817e4Smiod (*relptr)->sym_ptr_ptr = symbols;
1277*3d8817e4Smiod ++relptr;
1278*3d8817e4Smiod ++ret;
1279*3d8817e4Smiod }
1280*3d8817e4Smiod }
1281*3d8817e4Smiod }
1282*3d8817e4Smiod }
1283*3d8817e4Smiod
1284*3d8817e4Smiod *relptr = NULL;
1285*3d8817e4Smiod
1286*3d8817e4Smiod return ret;
1287*3d8817e4Smiod }
1288*3d8817e4Smiod
1289*3d8817e4Smiod /* Compute the section file positions for an NLM file. All variable
1290*3d8817e4Smiod length data in the file headers must be set before this function is
1291*3d8817e4Smiod called. If the variable length data is changed later, the
1292*3d8817e4Smiod resulting object file will be incorrect. Unfortunately, there is
1293*3d8817e4Smiod no way to check this.
1294*3d8817e4Smiod
1295*3d8817e4Smiod This routine also sets the Size and Offset fields in the fixed
1296*3d8817e4Smiod header.
1297*3d8817e4Smiod
1298*3d8817e4Smiod It also looks over the symbols and moves any common symbols into
1299*3d8817e4Smiod the .bss section; NLM has no way to represent a common symbol.
1300*3d8817e4Smiod This approach means that either the symbols must already have been
1301*3d8817e4Smiod set at this point, or there must be no common symbols. We need to
1302*3d8817e4Smiod move the symbols at this point so that mangle_relocs can see the
1303*3d8817e4Smiod final values. */
1304*3d8817e4Smiod
1305*3d8817e4Smiod static bfd_boolean
nlm_compute_section_file_positions(bfd * abfd)1306*3d8817e4Smiod nlm_compute_section_file_positions (bfd *abfd)
1307*3d8817e4Smiod {
1308*3d8817e4Smiod file_ptr sofar;
1309*3d8817e4Smiod asection *sec;
1310*3d8817e4Smiod bfd_vma text, data, bss;
1311*3d8817e4Smiod bfd_vma text_low, data_low;
1312*3d8817e4Smiod unsigned int text_align, data_align, other_align;
1313*3d8817e4Smiod file_ptr text_ptr, data_ptr, other_ptr;
1314*3d8817e4Smiod asection *bss_sec;
1315*3d8817e4Smiod asymbol **sym_ptr_ptr;
1316*3d8817e4Smiod
1317*3d8817e4Smiod if (abfd->output_has_begun)
1318*3d8817e4Smiod return TRUE;
1319*3d8817e4Smiod
1320*3d8817e4Smiod /* Make sure we have a section to hold uninitialized data. */
1321*3d8817e4Smiod bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
1322*3d8817e4Smiod if (bss_sec == NULL)
1323*3d8817e4Smiod {
1324*3d8817e4Smiod if (!add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
1325*3d8817e4Smiod (file_ptr) 0, (bfd_size_type) 0,
1326*3d8817e4Smiod SEC_ALLOC))
1327*3d8817e4Smiod return FALSE;
1328*3d8817e4Smiod bss_sec = bfd_get_section_by_name (abfd, NLM_UNINITIALIZED_DATA_NAME);
1329*3d8817e4Smiod }
1330*3d8817e4Smiod
1331*3d8817e4Smiod abfd->output_has_begun = TRUE;
1332*3d8817e4Smiod
1333*3d8817e4Smiod /* The fixed header. */
1334*3d8817e4Smiod sofar = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
1335*3d8817e4Smiod
1336*3d8817e4Smiod /* The variable header. */
1337*3d8817e4Smiod sofar += (sizeof (nlm_variable_header (abfd)->descriptionLength)
1338*3d8817e4Smiod + nlm_variable_header (abfd)->descriptionLength + 1
1339*3d8817e4Smiod + NLM_TARGET_LONG_SIZE /* stackSize */
1340*3d8817e4Smiod + NLM_TARGET_LONG_SIZE /* reserved */
1341*3d8817e4Smiod + sizeof (nlm_variable_header (abfd)->oldThreadName)
1342*3d8817e4Smiod + sizeof (nlm_variable_header (abfd)->screenNameLength)
1343*3d8817e4Smiod + nlm_variable_header (abfd)->screenNameLength + 1
1344*3d8817e4Smiod + sizeof (nlm_variable_header (abfd)->threadNameLength)
1345*3d8817e4Smiod + nlm_variable_header (abfd)->threadNameLength + 1);
1346*3d8817e4Smiod
1347*3d8817e4Smiod /* The auxiliary headers. */
1348*3d8817e4Smiod if (find_nonzero (nlm_version_header (abfd),
1349*3d8817e4Smiod sizeof (Nlm_Internal_Version_Header)))
1350*3d8817e4Smiod sofar += sizeof (Nlm_External_Version_Header);
1351*3d8817e4Smiod if (find_nonzero (nlm_extended_header (abfd),
1352*3d8817e4Smiod sizeof (Nlm_Internal_Extended_Header)))
1353*3d8817e4Smiod sofar += sizeof (Nlm_External_Extended_Header);
1354*3d8817e4Smiod if (find_nonzero (nlm_copyright_header (abfd),
1355*3d8817e4Smiod sizeof (Nlm_Internal_Copyright_Header)))
1356*3d8817e4Smiod sofar += (sizeof (Nlm_External_Copyright_Header)
1357*3d8817e4Smiod + nlm_copyright_header (abfd)->copyrightMessageLength + 1);
1358*3d8817e4Smiod if (find_nonzero (nlm_custom_header (abfd),
1359*3d8817e4Smiod sizeof (Nlm_Internal_Custom_Header)))
1360*3d8817e4Smiod sofar += (sizeof (Nlm_External_Custom_Header)
1361*3d8817e4Smiod + nlm_custom_header (abfd)->hdrLength);
1362*3d8817e4Smiod if (find_nonzero (nlm_cygnus_ext_header (abfd),
1363*3d8817e4Smiod sizeof (Nlm_Internal_Cygnus_Ext_Header)))
1364*3d8817e4Smiod sofar += sizeof (Nlm_External_Custom_Header);
1365*3d8817e4Smiod
1366*3d8817e4Smiod /* Compute the section file positions in two passes. First get the
1367*3d8817e4Smiod sizes of the text and data sections, and then set the file
1368*3d8817e4Smiod positions. This code aligns the sections in the file using the
1369*3d8817e4Smiod same alignment restrictions that apply to the sections in memory;
1370*3d8817e4Smiod this may not be necessary. */
1371*3d8817e4Smiod text = 0;
1372*3d8817e4Smiod text_low = (bfd_vma) - 1;
1373*3d8817e4Smiod text_align = 0;
1374*3d8817e4Smiod data = 0;
1375*3d8817e4Smiod data_low = (bfd_vma) - 1;
1376*3d8817e4Smiod data_align = 0;
1377*3d8817e4Smiod bss = 0;
1378*3d8817e4Smiod other_align = 0;
1379*3d8817e4Smiod for (sec = abfd->sections; sec != NULL; sec = sec->next)
1380*3d8817e4Smiod {
1381*3d8817e4Smiod flagword f;
1382*3d8817e4Smiod
1383*3d8817e4Smiod sec->size = BFD_ALIGN (sec->size, 1 << sec->alignment_power);
1384*3d8817e4Smiod
1385*3d8817e4Smiod f = bfd_get_section_flags (abfd, sec);
1386*3d8817e4Smiod if (f & SEC_CODE)
1387*3d8817e4Smiod {
1388*3d8817e4Smiod text += sec->size;
1389*3d8817e4Smiod if (bfd_get_section_vma (abfd, sec) < text_low)
1390*3d8817e4Smiod text_low = bfd_get_section_vma (abfd, sec);
1391*3d8817e4Smiod if (sec->alignment_power > text_align)
1392*3d8817e4Smiod text_align = sec->alignment_power;
1393*3d8817e4Smiod }
1394*3d8817e4Smiod else if (f & SEC_DATA)
1395*3d8817e4Smiod {
1396*3d8817e4Smiod data += sec->size;
1397*3d8817e4Smiod if (bfd_get_section_vma (abfd, sec) < data_low)
1398*3d8817e4Smiod data_low = bfd_get_section_vma (abfd, sec);
1399*3d8817e4Smiod if (sec->alignment_power > data_align)
1400*3d8817e4Smiod data_align = sec->alignment_power;
1401*3d8817e4Smiod }
1402*3d8817e4Smiod else if (f & SEC_HAS_CONTENTS)
1403*3d8817e4Smiod {
1404*3d8817e4Smiod if (sec->alignment_power > other_align)
1405*3d8817e4Smiod other_align = sec->alignment_power;
1406*3d8817e4Smiod }
1407*3d8817e4Smiod else if (f & SEC_ALLOC)
1408*3d8817e4Smiod bss += sec->size;
1409*3d8817e4Smiod }
1410*3d8817e4Smiod
1411*3d8817e4Smiod nlm_set_text_low (abfd, text_low);
1412*3d8817e4Smiod nlm_set_data_low (abfd, data_low);
1413*3d8817e4Smiod
1414*3d8817e4Smiod if (nlm_no_uninitialized_data (abfd))
1415*3d8817e4Smiod {
1416*3d8817e4Smiod /* This NetWare format does not use uninitialized data. We must
1417*3d8817e4Smiod increase the size of the data section. We will never wind up
1418*3d8817e4Smiod writing those file locations, so they will remain zero. */
1419*3d8817e4Smiod data += bss;
1420*3d8817e4Smiod bss = 0;
1421*3d8817e4Smiod }
1422*3d8817e4Smiod
1423*3d8817e4Smiod text_ptr = BFD_ALIGN (sofar, 1 << text_align);
1424*3d8817e4Smiod data_ptr = BFD_ALIGN (text_ptr + text, 1 << data_align);
1425*3d8817e4Smiod other_ptr = BFD_ALIGN (data_ptr + data, 1 << other_align);
1426*3d8817e4Smiod
1427*3d8817e4Smiod /* Fill in some fields in the header for which we now have the
1428*3d8817e4Smiod information. */
1429*3d8817e4Smiod nlm_fixed_header (abfd)->codeImageOffset = text_ptr;
1430*3d8817e4Smiod nlm_fixed_header (abfd)->codeImageSize = text;
1431*3d8817e4Smiod nlm_fixed_header (abfd)->dataImageOffset = data_ptr;
1432*3d8817e4Smiod nlm_fixed_header (abfd)->dataImageSize = data;
1433*3d8817e4Smiod nlm_fixed_header (abfd)->uninitializedDataSize = bss;
1434*3d8817e4Smiod
1435*3d8817e4Smiod for (sec = abfd->sections; sec != NULL; sec = sec->next)
1436*3d8817e4Smiod {
1437*3d8817e4Smiod flagword f;
1438*3d8817e4Smiod
1439*3d8817e4Smiod f = bfd_get_section_flags (abfd, sec);
1440*3d8817e4Smiod
1441*3d8817e4Smiod if (f & SEC_CODE)
1442*3d8817e4Smiod {
1443*3d8817e4Smiod sec->filepos = text_ptr;
1444*3d8817e4Smiod text_ptr += sec->size;
1445*3d8817e4Smiod }
1446*3d8817e4Smiod else if (f & SEC_DATA)
1447*3d8817e4Smiod {
1448*3d8817e4Smiod sec->filepos = data_ptr;
1449*3d8817e4Smiod data_ptr += sec->size;
1450*3d8817e4Smiod }
1451*3d8817e4Smiod else if (f & SEC_HAS_CONTENTS)
1452*3d8817e4Smiod {
1453*3d8817e4Smiod sec->filepos = other_ptr;
1454*3d8817e4Smiod other_ptr += sec->size;
1455*3d8817e4Smiod }
1456*3d8817e4Smiod }
1457*3d8817e4Smiod
1458*3d8817e4Smiod nlm_fixed_header (abfd)->relocationFixupOffset = other_ptr;
1459*3d8817e4Smiod
1460*3d8817e4Smiod /* Move all common symbols into the .bss section. */
1461*3d8817e4Smiod
1462*3d8817e4Smiod sym_ptr_ptr = bfd_get_outsymbols (abfd);
1463*3d8817e4Smiod if (sym_ptr_ptr != NULL)
1464*3d8817e4Smiod {
1465*3d8817e4Smiod asymbol **sym_end;
1466*3d8817e4Smiod bfd_vma add;
1467*3d8817e4Smiod
1468*3d8817e4Smiod sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1469*3d8817e4Smiod add = 0;
1470*3d8817e4Smiod for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1471*3d8817e4Smiod {
1472*3d8817e4Smiod asymbol *sym;
1473*3d8817e4Smiod bfd_vma size;
1474*3d8817e4Smiod
1475*3d8817e4Smiod sym = *sym_ptr_ptr;
1476*3d8817e4Smiod
1477*3d8817e4Smiod if (!bfd_is_com_section (bfd_get_section (sym)))
1478*3d8817e4Smiod continue;
1479*3d8817e4Smiod
1480*3d8817e4Smiod /* Put the common symbol in the .bss section, and increase
1481*3d8817e4Smiod the size of the .bss section by the size of the common
1482*3d8817e4Smiod symbol (which is the old value of the symbol). */
1483*3d8817e4Smiod sym->section = bss_sec;
1484*3d8817e4Smiod size = sym->value;
1485*3d8817e4Smiod sym->value = bss_sec->size + add;
1486*3d8817e4Smiod add += size;
1487*3d8817e4Smiod add = BFD_ALIGN (add, 1 << bss_sec->alignment_power);
1488*3d8817e4Smiod }
1489*3d8817e4Smiod if (add != 0)
1490*3d8817e4Smiod {
1491*3d8817e4Smiod if (nlm_no_uninitialized_data (abfd))
1492*3d8817e4Smiod {
1493*3d8817e4Smiod /* We could handle this case, but so far it hasn't been
1494*3d8817e4Smiod necessary. */
1495*3d8817e4Smiod abort ();
1496*3d8817e4Smiod }
1497*3d8817e4Smiod nlm_fixed_header (abfd)->uninitializedDataSize += add;
1498*3d8817e4Smiod bss_sec->size += add;
1499*3d8817e4Smiod }
1500*3d8817e4Smiod }
1501*3d8817e4Smiod
1502*3d8817e4Smiod return TRUE;
1503*3d8817e4Smiod }
1504*3d8817e4Smiod
1505*3d8817e4Smiod /* Set the contents of a section. To do this we need to know where
1506*3d8817e4Smiod the section is going to be located in the output file. That means
1507*3d8817e4Smiod that the sizes of all the sections must be set, and all the
1508*3d8817e4Smiod variable size header information must be known. */
1509*3d8817e4Smiod
1510*3d8817e4Smiod bfd_boolean
nlm_set_section_contents(bfd * abfd,asection * section,const void * location,file_ptr offset,bfd_size_type count)1511*3d8817e4Smiod nlm_set_section_contents (bfd *abfd,
1512*3d8817e4Smiod asection *section,
1513*3d8817e4Smiod const void * location,
1514*3d8817e4Smiod file_ptr offset,
1515*3d8817e4Smiod bfd_size_type count)
1516*3d8817e4Smiod {
1517*3d8817e4Smiod if (! abfd->output_has_begun
1518*3d8817e4Smiod && ! nlm_compute_section_file_positions (abfd))
1519*3d8817e4Smiod return FALSE;
1520*3d8817e4Smiod
1521*3d8817e4Smiod if (count == 0)
1522*3d8817e4Smiod return TRUE;
1523*3d8817e4Smiod
1524*3d8817e4Smiod /* i386 NetWare has a very restricted set of relocs. In order for
1525*3d8817e4Smiod objcopy to work, the NLM i386 backend needs a chance to rework
1526*3d8817e4Smiod the section contents so that its set of relocs will work. If all
1527*3d8817e4Smiod the relocs are already acceptable, this will not do anything. */
1528*3d8817e4Smiod if (section->reloc_count != 0)
1529*3d8817e4Smiod {
1530*3d8817e4Smiod bfd_boolean (*mangle_relocs_func)
1531*3d8817e4Smiod (bfd *, asection *, const void *, bfd_vma, bfd_size_type);
1532*3d8817e4Smiod
1533*3d8817e4Smiod mangle_relocs_func = nlm_mangle_relocs_func (abfd);
1534*3d8817e4Smiod if (mangle_relocs_func != NULL)
1535*3d8817e4Smiod {
1536*3d8817e4Smiod if (!(*mangle_relocs_func) (abfd, section, location,
1537*3d8817e4Smiod (bfd_vma) offset, count))
1538*3d8817e4Smiod return FALSE;
1539*3d8817e4Smiod }
1540*3d8817e4Smiod }
1541*3d8817e4Smiod
1542*3d8817e4Smiod if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1543*3d8817e4Smiod || bfd_bwrite (location, count, abfd) != count)
1544*3d8817e4Smiod return FALSE;
1545*3d8817e4Smiod
1546*3d8817e4Smiod return TRUE;
1547*3d8817e4Smiod }
1548*3d8817e4Smiod
1549*3d8817e4Smiod /* We need to sort a list of relocs associated with sections when we
1550*3d8817e4Smiod write out the external relocs. */
1551*3d8817e4Smiod
1552*3d8817e4Smiod static int
nlm_external_reloc_compare(const void * p1,const void * p2)1553*3d8817e4Smiod nlm_external_reloc_compare (const void *p1, const void *p2)
1554*3d8817e4Smiod {
1555*3d8817e4Smiod const struct reloc_and_sec *r1 = (const struct reloc_and_sec *) p1;
1556*3d8817e4Smiod const struct reloc_and_sec *r2 = (const struct reloc_and_sec *) p2;
1557*3d8817e4Smiod int cmp;
1558*3d8817e4Smiod
1559*3d8817e4Smiod cmp = strcmp ((*r1->rel->sym_ptr_ptr)->name,
1560*3d8817e4Smiod (*r2->rel->sym_ptr_ptr)->name);
1561*3d8817e4Smiod if (cmp != 0)
1562*3d8817e4Smiod return cmp;
1563*3d8817e4Smiod
1564*3d8817e4Smiod /* We sort by address within symbol to make the sort more stable and
1565*3d8817e4Smiod increase the chances that different hosts will generate bit for
1566*3d8817e4Smiod bit equivalent results. */
1567*3d8817e4Smiod return (int) (r1->rel->address - r2->rel->address);
1568*3d8817e4Smiod }
1569*3d8817e4Smiod
1570*3d8817e4Smiod /* Write out an NLM file. We write out the information in this order:
1571*3d8817e4Smiod fixed header
1572*3d8817e4Smiod variable header
1573*3d8817e4Smiod auxiliary headers
1574*3d8817e4Smiod code sections
1575*3d8817e4Smiod data sections
1576*3d8817e4Smiod other sections (custom data, messages, help, shared NLM, RPC,
1577*3d8817e4Smiod module dependencies)
1578*3d8817e4Smiod relocation fixups
1579*3d8817e4Smiod external references (imports)
1580*3d8817e4Smiod public symbols (exports)
1581*3d8817e4Smiod debugging records
1582*3d8817e4Smiod This is similar to the order used by the NetWare tools; the
1583*3d8817e4Smiod difference is that NetWare puts the sections other than code, data
1584*3d8817e4Smiod and custom data at the end of the NLM. It is convenient for us to
1585*3d8817e4Smiod know where the sections are going to be before worrying about the
1586*3d8817e4Smiod size of the other information.
1587*3d8817e4Smiod
1588*3d8817e4Smiod By the time this function is called, all the section data should
1589*3d8817e4Smiod have been output using set_section_contents. Note that custom
1590*3d8817e4Smiod data, the message file, the help file, the shared NLM file, the RPC
1591*3d8817e4Smiod data, and the module dependencies are all considered to be
1592*3d8817e4Smiod sections; the caller is responsible for filling in the offset and
1593*3d8817e4Smiod length fields in the NLM headers. The relocation fixups and
1594*3d8817e4Smiod imports are both obtained from the list of relocs attached to each
1595*3d8817e4Smiod section. The exports and debugging records are obtained from the
1596*3d8817e4Smiod list of outsymbols. */
1597*3d8817e4Smiod
1598*3d8817e4Smiod bfd_boolean
nlm_write_object_contents(bfd * abfd)1599*3d8817e4Smiod nlm_write_object_contents (bfd *abfd)
1600*3d8817e4Smiod {
1601*3d8817e4Smiod asection *sec;
1602*3d8817e4Smiod bfd_boolean (*write_import_func) (bfd *, asection *, arelent *);
1603*3d8817e4Smiod bfd_size_type external_reloc_count, internal_reloc_count, i, c;
1604*3d8817e4Smiod struct reloc_and_sec *external_relocs;
1605*3d8817e4Smiod asymbol **sym_ptr_ptr;
1606*3d8817e4Smiod file_ptr last;
1607*3d8817e4Smiod bfd_boolean (*write_prefix_func) (bfd *);
1608*3d8817e4Smiod unsigned char *fixed_header = NULL;
1609*3d8817e4Smiod file_ptr pos;
1610*3d8817e4Smiod bfd_size_type amt;
1611*3d8817e4Smiod
1612*3d8817e4Smiod fixed_header = bfd_malloc (nlm_fixed_header_size (abfd));
1613*3d8817e4Smiod if (fixed_header == NULL)
1614*3d8817e4Smiod goto error_return;
1615*3d8817e4Smiod
1616*3d8817e4Smiod if (! abfd->output_has_begun
1617*3d8817e4Smiod && ! nlm_compute_section_file_positions (abfd))
1618*3d8817e4Smiod goto error_return;
1619*3d8817e4Smiod
1620*3d8817e4Smiod /* Write out the variable length headers. */
1621*3d8817e4Smiod pos = nlm_optional_prefix_size (abfd) + nlm_fixed_header_size (abfd);
1622*3d8817e4Smiod if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1623*3d8817e4Smiod goto error_return;
1624*3d8817e4Smiod if (! nlm_swap_variable_header_out (abfd)
1625*3d8817e4Smiod || ! nlm_swap_auxiliary_headers_out (abfd))
1626*3d8817e4Smiod {
1627*3d8817e4Smiod bfd_set_error (bfd_error_system_call);
1628*3d8817e4Smiod goto error_return;
1629*3d8817e4Smiod }
1630*3d8817e4Smiod
1631*3d8817e4Smiod /* A weak check on whether the section file positions were
1632*3d8817e4Smiod reasonable. */
1633*3d8817e4Smiod if (bfd_tell (abfd) > nlm_fixed_header (abfd)->codeImageOffset)
1634*3d8817e4Smiod {
1635*3d8817e4Smiod bfd_set_error (bfd_error_invalid_operation);
1636*3d8817e4Smiod goto error_return;
1637*3d8817e4Smiod }
1638*3d8817e4Smiod
1639*3d8817e4Smiod /* Advance to the relocs. */
1640*3d8817e4Smiod if (bfd_seek (abfd, nlm_fixed_header (abfd)->relocationFixupOffset,
1641*3d8817e4Smiod SEEK_SET) != 0)
1642*3d8817e4Smiod goto error_return;
1643*3d8817e4Smiod
1644*3d8817e4Smiod /* The format of the relocation entries is dependent upon the
1645*3d8817e4Smiod particular target. We use an external routine to write the reloc
1646*3d8817e4Smiod out. */
1647*3d8817e4Smiod write_import_func = nlm_write_import_func (abfd);
1648*3d8817e4Smiod
1649*3d8817e4Smiod /* Write out the internal relocation fixups. While we're looping
1650*3d8817e4Smiod over the relocs, we also count the external relocs, which is
1651*3d8817e4Smiod needed when they are written out below. */
1652*3d8817e4Smiod internal_reloc_count = 0;
1653*3d8817e4Smiod external_reloc_count = 0;
1654*3d8817e4Smiod for (sec = abfd->sections; sec != NULL; sec = sec->next)
1655*3d8817e4Smiod {
1656*3d8817e4Smiod arelent **rel_ptr_ptr, **rel_end;
1657*3d8817e4Smiod
1658*3d8817e4Smiod if (sec->reloc_count == 0)
1659*3d8817e4Smiod continue;
1660*3d8817e4Smiod
1661*3d8817e4Smiod /* We can only represent relocs within a code or data
1662*3d8817e4Smiod section. We ignore them for a debugging section. */
1663*3d8817e4Smiod if ((bfd_get_section_flags (abfd, sec) & (SEC_CODE | SEC_DATA)) == 0)
1664*3d8817e4Smiod continue;
1665*3d8817e4Smiod
1666*3d8817e4Smiod /* We need to know how to write out imports */
1667*3d8817e4Smiod if (write_import_func == NULL)
1668*3d8817e4Smiod {
1669*3d8817e4Smiod bfd_set_error (bfd_error_invalid_operation);
1670*3d8817e4Smiod goto error_return;
1671*3d8817e4Smiod }
1672*3d8817e4Smiod
1673*3d8817e4Smiod rel_ptr_ptr = sec->orelocation;
1674*3d8817e4Smiod rel_end = rel_ptr_ptr + sec->reloc_count;
1675*3d8817e4Smiod for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
1676*3d8817e4Smiod {
1677*3d8817e4Smiod arelent *rel;
1678*3d8817e4Smiod asymbol *sym;
1679*3d8817e4Smiod
1680*3d8817e4Smiod rel = *rel_ptr_ptr;
1681*3d8817e4Smiod sym = *rel->sym_ptr_ptr;
1682*3d8817e4Smiod
1683*3d8817e4Smiod if (! bfd_is_und_section (bfd_get_section (sym)))
1684*3d8817e4Smiod {
1685*3d8817e4Smiod ++internal_reloc_count;
1686*3d8817e4Smiod if (! (*write_import_func) (abfd, sec, rel))
1687*3d8817e4Smiod goto error_return;
1688*3d8817e4Smiod }
1689*3d8817e4Smiod else
1690*3d8817e4Smiod ++external_reloc_count;
1691*3d8817e4Smiod }
1692*3d8817e4Smiod }
1693*3d8817e4Smiod nlm_fixed_header (abfd)->numberOfRelocationFixups = internal_reloc_count;
1694*3d8817e4Smiod
1695*3d8817e4Smiod /* Write out the imports (relocs against external symbols). These
1696*3d8817e4Smiod are output as a symbol name followed by all the relocs for that
1697*3d8817e4Smiod symbol, so we must first gather together all the relocs against
1698*3d8817e4Smiod external symbols and sort them. */
1699*3d8817e4Smiod amt = external_reloc_count * sizeof (struct reloc_and_sec);
1700*3d8817e4Smiod external_relocs = bfd_alloc (abfd, amt);
1701*3d8817e4Smiod if (external_relocs == NULL)
1702*3d8817e4Smiod goto error_return;
1703*3d8817e4Smiod i = 0;
1704*3d8817e4Smiod for (sec = abfd->sections; sec != NULL; sec = sec->next)
1705*3d8817e4Smiod {
1706*3d8817e4Smiod arelent **rel_ptr_ptr, **rel_end;
1707*3d8817e4Smiod
1708*3d8817e4Smiod if (sec->reloc_count == 0)
1709*3d8817e4Smiod continue;
1710*3d8817e4Smiod
1711*3d8817e4Smiod rel_ptr_ptr = sec->orelocation;
1712*3d8817e4Smiod rel_end = rel_ptr_ptr + sec->reloc_count;
1713*3d8817e4Smiod for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
1714*3d8817e4Smiod {
1715*3d8817e4Smiod arelent *rel;
1716*3d8817e4Smiod asymbol *sym;
1717*3d8817e4Smiod
1718*3d8817e4Smiod rel = *rel_ptr_ptr;
1719*3d8817e4Smiod sym = *rel->sym_ptr_ptr;
1720*3d8817e4Smiod
1721*3d8817e4Smiod if (! bfd_is_und_section (bfd_get_section (sym)))
1722*3d8817e4Smiod continue;
1723*3d8817e4Smiod
1724*3d8817e4Smiod external_relocs[i].rel = rel;
1725*3d8817e4Smiod external_relocs[i].sec = sec;
1726*3d8817e4Smiod ++i;
1727*3d8817e4Smiod }
1728*3d8817e4Smiod }
1729*3d8817e4Smiod
1730*3d8817e4Smiod BFD_ASSERT (i == external_reloc_count);
1731*3d8817e4Smiod
1732*3d8817e4Smiod /* Sort the external relocs by name. */
1733*3d8817e4Smiod qsort (external_relocs, (size_t) external_reloc_count,
1734*3d8817e4Smiod sizeof (struct reloc_and_sec), nlm_external_reloc_compare);
1735*3d8817e4Smiod
1736*3d8817e4Smiod /* Write out the external relocs. */
1737*3d8817e4Smiod nlm_fixed_header (abfd)->externalReferencesOffset = bfd_tell (abfd);
1738*3d8817e4Smiod c = 0;
1739*3d8817e4Smiod i = 0;
1740*3d8817e4Smiod while (i < external_reloc_count)
1741*3d8817e4Smiod {
1742*3d8817e4Smiod arelent *rel;
1743*3d8817e4Smiod asymbol *sym;
1744*3d8817e4Smiod bfd_size_type j, cnt;
1745*3d8817e4Smiod
1746*3d8817e4Smiod ++c;
1747*3d8817e4Smiod
1748*3d8817e4Smiod rel = external_relocs[i].rel;
1749*3d8817e4Smiod sym = *rel->sym_ptr_ptr;
1750*3d8817e4Smiod
1751*3d8817e4Smiod cnt = 0;
1752*3d8817e4Smiod for (j = i;
1753*3d8817e4Smiod (j < external_reloc_count
1754*3d8817e4Smiod && *external_relocs[j].rel->sym_ptr_ptr == sym);
1755*3d8817e4Smiod j++)
1756*3d8817e4Smiod ++cnt;
1757*3d8817e4Smiod
1758*3d8817e4Smiod if (! (*nlm_write_external_func (abfd)) (abfd, cnt, sym,
1759*3d8817e4Smiod &external_relocs[i]))
1760*3d8817e4Smiod goto error_return;
1761*3d8817e4Smiod
1762*3d8817e4Smiod i += cnt;
1763*3d8817e4Smiod }
1764*3d8817e4Smiod
1765*3d8817e4Smiod nlm_fixed_header (abfd)->numberOfExternalReferences = c;
1766*3d8817e4Smiod
1767*3d8817e4Smiod /* Write out the public symbols (exports). */
1768*3d8817e4Smiod sym_ptr_ptr = bfd_get_outsymbols (abfd);
1769*3d8817e4Smiod if (sym_ptr_ptr != NULL)
1770*3d8817e4Smiod {
1771*3d8817e4Smiod bfd_vma (*get_public_offset_func) (bfd *, asymbol *);
1772*3d8817e4Smiod bfd_boolean (*write_export_func) (bfd *, asymbol *, bfd_vma);
1773*3d8817e4Smiod
1774*3d8817e4Smiod asymbol **sym_end;
1775*3d8817e4Smiod
1776*3d8817e4Smiod nlm_fixed_header (abfd)->publicsOffset = bfd_tell (abfd);
1777*3d8817e4Smiod get_public_offset_func = nlm_get_public_offset_func (abfd);
1778*3d8817e4Smiod write_export_func = nlm_write_export_func (abfd);
1779*3d8817e4Smiod c = 0;
1780*3d8817e4Smiod sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1781*3d8817e4Smiod for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1782*3d8817e4Smiod {
1783*3d8817e4Smiod asymbol *sym;
1784*3d8817e4Smiod bfd_byte len;
1785*3d8817e4Smiod bfd_vma offset;
1786*3d8817e4Smiod bfd_byte temp[NLM_TARGET_LONG_SIZE];
1787*3d8817e4Smiod
1788*3d8817e4Smiod sym = *sym_ptr_ptr;
1789*3d8817e4Smiod
1790*3d8817e4Smiod if ((sym->flags & (BSF_EXPORT | BSF_GLOBAL)) == 0
1791*3d8817e4Smiod || bfd_is_und_section (bfd_get_section (sym)))
1792*3d8817e4Smiod continue;
1793*3d8817e4Smiod
1794*3d8817e4Smiod ++c;
1795*3d8817e4Smiod
1796*3d8817e4Smiod if (get_public_offset_func)
1797*3d8817e4Smiod {
1798*3d8817e4Smiod /* Most backends can use the code below, but
1799*3d8817e4Smiod unfortunately some use a different scheme. */
1800*3d8817e4Smiod offset = (*get_public_offset_func) (abfd, sym);
1801*3d8817e4Smiod }
1802*3d8817e4Smiod else
1803*3d8817e4Smiod {
1804*3d8817e4Smiod offset = bfd_asymbol_value (sym);
1805*3d8817e4Smiod sec = sym->section;
1806*3d8817e4Smiod if (sec->flags & SEC_CODE)
1807*3d8817e4Smiod {
1808*3d8817e4Smiod offset -= nlm_get_text_low (abfd);
1809*3d8817e4Smiod offset |= NLM_HIBIT;
1810*3d8817e4Smiod }
1811*3d8817e4Smiod else if (sec->flags & (SEC_DATA | SEC_ALLOC))
1812*3d8817e4Smiod {
1813*3d8817e4Smiod /* SEC_ALLOC is for the .bss section. */
1814*3d8817e4Smiod offset -= nlm_get_data_low (abfd);
1815*3d8817e4Smiod }
1816*3d8817e4Smiod else
1817*3d8817e4Smiod {
1818*3d8817e4Smiod /* We can't handle an exported symbol that is not in
1819*3d8817e4Smiod the code or data segment. */
1820*3d8817e4Smiod bfd_set_error (bfd_error_invalid_operation);
1821*3d8817e4Smiod goto error_return;
1822*3d8817e4Smiod }
1823*3d8817e4Smiod }
1824*3d8817e4Smiod
1825*3d8817e4Smiod if (write_export_func)
1826*3d8817e4Smiod {
1827*3d8817e4Smiod if (! (*write_export_func) (abfd, sym, offset))
1828*3d8817e4Smiod goto error_return;
1829*3d8817e4Smiod }
1830*3d8817e4Smiod else
1831*3d8817e4Smiod {
1832*3d8817e4Smiod len = strlen (sym->name);
1833*3d8817e4Smiod if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
1834*3d8817e4Smiod != sizeof (bfd_byte))
1835*3d8817e4Smiod || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
1836*3d8817e4Smiod goto error_return;
1837*3d8817e4Smiod
1838*3d8817e4Smiod put_word (abfd, offset, temp);
1839*3d8817e4Smiod if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd)
1840*3d8817e4Smiod != sizeof (temp))
1841*3d8817e4Smiod goto error_return;
1842*3d8817e4Smiod }
1843*3d8817e4Smiod }
1844*3d8817e4Smiod nlm_fixed_header (abfd)->numberOfPublics = c;
1845*3d8817e4Smiod
1846*3d8817e4Smiod /* Write out the debugging records. The NLM conversion program
1847*3d8817e4Smiod wants to be able to inhibit this, so as a special hack if
1848*3d8817e4Smiod debugInfoOffset is set to -1 we don't write any debugging
1849*3d8817e4Smiod information. This can not be handled by fiddling with the
1850*3d8817e4Smiod symbol table, because exported symbols appear in both the
1851*3d8817e4Smiod exported symbol list and the debugging information. */
1852*3d8817e4Smiod if (nlm_fixed_header (abfd)->debugInfoOffset == (file_ptr) - 1)
1853*3d8817e4Smiod {
1854*3d8817e4Smiod nlm_fixed_header (abfd)->debugInfoOffset = 0;
1855*3d8817e4Smiod nlm_fixed_header (abfd)->numberOfDebugRecords = 0;
1856*3d8817e4Smiod }
1857*3d8817e4Smiod else
1858*3d8817e4Smiod {
1859*3d8817e4Smiod nlm_fixed_header (abfd)->debugInfoOffset = bfd_tell (abfd);
1860*3d8817e4Smiod c = 0;
1861*3d8817e4Smiod sym_ptr_ptr = bfd_get_outsymbols (abfd);
1862*3d8817e4Smiod sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
1863*3d8817e4Smiod for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
1864*3d8817e4Smiod {
1865*3d8817e4Smiod asymbol *sym;
1866*3d8817e4Smiod bfd_byte type, len;
1867*3d8817e4Smiod bfd_vma offset;
1868*3d8817e4Smiod bfd_byte temp[NLM_TARGET_LONG_SIZE];
1869*3d8817e4Smiod
1870*3d8817e4Smiod sym = *sym_ptr_ptr;
1871*3d8817e4Smiod
1872*3d8817e4Smiod /* The NLM notion of a debugging symbol is actually what
1873*3d8817e4Smiod BFD calls a local or global symbol. What BFD calls a
1874*3d8817e4Smiod debugging symbol NLM does not understand at all. */
1875*3d8817e4Smiod if ((sym->flags & (BSF_LOCAL | BSF_GLOBAL | BSF_EXPORT)) == 0
1876*3d8817e4Smiod || (sym->flags & BSF_DEBUGGING) != 0
1877*3d8817e4Smiod || bfd_is_und_section (bfd_get_section (sym)))
1878*3d8817e4Smiod continue;
1879*3d8817e4Smiod
1880*3d8817e4Smiod ++c;
1881*3d8817e4Smiod
1882*3d8817e4Smiod offset = bfd_asymbol_value (sym);
1883*3d8817e4Smiod sec = sym->section;
1884*3d8817e4Smiod if (sec->flags & SEC_CODE)
1885*3d8817e4Smiod {
1886*3d8817e4Smiod offset -= nlm_get_text_low (abfd);
1887*3d8817e4Smiod type = 1;
1888*3d8817e4Smiod }
1889*3d8817e4Smiod else if (sec->flags & (SEC_DATA | SEC_ALLOC))
1890*3d8817e4Smiod {
1891*3d8817e4Smiod /* SEC_ALLOC is for the .bss section. */
1892*3d8817e4Smiod offset -= nlm_get_data_low (abfd);
1893*3d8817e4Smiod type = 0;
1894*3d8817e4Smiod }
1895*3d8817e4Smiod else
1896*3d8817e4Smiod type = 2;
1897*3d8817e4Smiod
1898*3d8817e4Smiod /* The type is 0 for data, 1 for code, 2 for absolute. */
1899*3d8817e4Smiod if (bfd_bwrite (&type, (bfd_size_type) sizeof (bfd_byte), abfd)
1900*3d8817e4Smiod != sizeof (bfd_byte))
1901*3d8817e4Smiod goto error_return;
1902*3d8817e4Smiod
1903*3d8817e4Smiod put_word (abfd, offset, temp);
1904*3d8817e4Smiod if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd)
1905*3d8817e4Smiod != sizeof (temp))
1906*3d8817e4Smiod goto error_return;
1907*3d8817e4Smiod
1908*3d8817e4Smiod len = strlen (sym->name);
1909*3d8817e4Smiod if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
1910*3d8817e4Smiod != sizeof (bfd_byte))
1911*3d8817e4Smiod || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
1912*3d8817e4Smiod goto error_return;
1913*3d8817e4Smiod }
1914*3d8817e4Smiod nlm_fixed_header (abfd)->numberOfDebugRecords = c;
1915*3d8817e4Smiod }
1916*3d8817e4Smiod }
1917*3d8817e4Smiod
1918*3d8817e4Smiod /* NLMLINK fills in offset values even if there is no data, so we do
1919*3d8817e4Smiod the same. */
1920*3d8817e4Smiod last = bfd_tell (abfd);
1921*3d8817e4Smiod if (nlm_fixed_header (abfd)->codeImageOffset == 0)
1922*3d8817e4Smiod nlm_fixed_header (abfd)->codeImageOffset = last;
1923*3d8817e4Smiod if (nlm_fixed_header (abfd)->dataImageOffset == 0)
1924*3d8817e4Smiod nlm_fixed_header (abfd)->dataImageOffset = last;
1925*3d8817e4Smiod if (nlm_fixed_header (abfd)->customDataOffset == 0)
1926*3d8817e4Smiod nlm_fixed_header (abfd)->customDataOffset = last;
1927*3d8817e4Smiod if (nlm_fixed_header (abfd)->moduleDependencyOffset == 0)
1928*3d8817e4Smiod nlm_fixed_header (abfd)->moduleDependencyOffset = last;
1929*3d8817e4Smiod if (nlm_fixed_header (abfd)->relocationFixupOffset == 0)
1930*3d8817e4Smiod nlm_fixed_header (abfd)->relocationFixupOffset = last;
1931*3d8817e4Smiod if (nlm_fixed_header (abfd)->externalReferencesOffset == 0)
1932*3d8817e4Smiod nlm_fixed_header (abfd)->externalReferencesOffset = last;
1933*3d8817e4Smiod if (nlm_fixed_header (abfd)->publicsOffset == 0)
1934*3d8817e4Smiod nlm_fixed_header (abfd)->publicsOffset = last;
1935*3d8817e4Smiod if (nlm_fixed_header (abfd)->debugInfoOffset == 0)
1936*3d8817e4Smiod nlm_fixed_header (abfd)->debugInfoOffset = last;
1937*3d8817e4Smiod
1938*3d8817e4Smiod /* At this point everything has been written out except the fixed
1939*3d8817e4Smiod header. */
1940*3d8817e4Smiod memcpy (nlm_fixed_header (abfd)->signature, nlm_signature (abfd),
1941*3d8817e4Smiod NLM_SIGNATURE_SIZE);
1942*3d8817e4Smiod nlm_fixed_header (abfd)->version = NLM_HEADER_VERSION;
1943*3d8817e4Smiod nlm_fixed_header (abfd)->codeStartOffset =
1944*3d8817e4Smiod (bfd_get_start_address (abfd)
1945*3d8817e4Smiod - nlm_get_text_low (abfd));
1946*3d8817e4Smiod
1947*3d8817e4Smiod /* We have no convenient way for the caller to pass in the exit
1948*3d8817e4Smiod procedure or the check unload procedure, so the caller must set
1949*3d8817e4Smiod the values in the header to the values of the symbols. */
1950*3d8817e4Smiod nlm_fixed_header (abfd)->exitProcedureOffset -= nlm_get_text_low (abfd);
1951*3d8817e4Smiod if (nlm_fixed_header (abfd)->checkUnloadProcedureOffset != 0)
1952*3d8817e4Smiod nlm_fixed_header (abfd)->checkUnloadProcedureOffset -=
1953*3d8817e4Smiod nlm_get_text_low (abfd);
1954*3d8817e4Smiod
1955*3d8817e4Smiod if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1956*3d8817e4Smiod goto error_return;
1957*3d8817e4Smiod
1958*3d8817e4Smiod write_prefix_func = nlm_write_prefix_func (abfd);
1959*3d8817e4Smiod if (write_prefix_func)
1960*3d8817e4Smiod {
1961*3d8817e4Smiod if (! (*write_prefix_func) (abfd))
1962*3d8817e4Smiod goto error_return;
1963*3d8817e4Smiod }
1964*3d8817e4Smiod
1965*3d8817e4Smiod BFD_ASSERT ((bfd_size_type) bfd_tell (abfd)
1966*3d8817e4Smiod == nlm_optional_prefix_size (abfd));
1967*3d8817e4Smiod
1968*3d8817e4Smiod nlm_swap_fixed_header_out (abfd, nlm_fixed_header (abfd), fixed_header);
1969*3d8817e4Smiod if (bfd_bwrite (fixed_header, nlm_fixed_header_size (abfd), abfd)
1970*3d8817e4Smiod != nlm_fixed_header_size (abfd))
1971*3d8817e4Smiod goto error_return;
1972*3d8817e4Smiod
1973*3d8817e4Smiod if (fixed_header != NULL)
1974*3d8817e4Smiod free (fixed_header);
1975*3d8817e4Smiod return TRUE;
1976*3d8817e4Smiod
1977*3d8817e4Smiod error_return:
1978*3d8817e4Smiod if (fixed_header != NULL)
1979*3d8817e4Smiod free (fixed_header);
1980*3d8817e4Smiod return FALSE;
1981*3d8817e4Smiod }
1982