Commit | Line | Data |
---|---|---|
4e98461f SC |
1 | /* Support for the generic parts of most COFF variants, for BFD. |
2 | Copyright 1995 Free Software Foundation, Inc. | |
3 | Written by Cygnus Support. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | /* | |
22 | Most of this hacked by Steve Chamberlain, | |
23 | sac@cygnus.com | |
24 | */ | |
25 | ||
26 | ||
27 | ||
28 | #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data | |
29 | #define coff_mkobject pe_mkobject | |
30 | #define coff_mkobject_hook pe_mkobject_hook | |
31 | ||
32 | ||
33 | #ifndef GET_FCN_LNNOPTR | |
34 | #define GET_FCN_LNNOPTR(abfd, ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr) | |
35 | #endif | |
36 | ||
37 | #ifndef GET_FCN_ENDNDX | |
38 | #define GET_FCN_ENDNDX(abfd, ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx) | |
39 | #endif | |
40 | ||
41 | #ifndef PUT_FCN_LNNOPTR | |
42 | #define PUT_FCN_LNNOPTR(abfd, in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr) | |
43 | #endif | |
44 | #ifndef PUT_FCN_ENDNDX | |
45 | #define PUT_FCN_ENDNDX(abfd, in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx) | |
46 | #endif | |
47 | #ifndef GET_LNSZ_LNNO | |
48 | #define GET_LNSZ_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_lnno) | |
49 | #endif | |
50 | #ifndef GET_LNSZ_SIZE | |
51 | #define GET_LNSZ_SIZE(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_size) | |
52 | #endif | |
53 | #ifndef PUT_LNSZ_LNNO | |
54 | #define PUT_LNSZ_LNNO(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno) | |
55 | #endif | |
56 | #ifndef PUT_LNSZ_SIZE | |
57 | #define PUT_LNSZ_SIZE(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte*) ext->x_sym.x_misc.x_lnsz.x_size) | |
58 | #endif | |
59 | #ifndef GET_SCN_SCNLEN | |
60 | #define GET_SCN_SCNLEN(abfd, ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_scn.x_scnlen) | |
61 | #endif | |
62 | #ifndef GET_SCN_NRELOC | |
63 | #define GET_SCN_NRELOC(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nreloc) | |
64 | #endif | |
65 | #ifndef GET_SCN_NLINNO | |
66 | #define GET_SCN_NLINNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nlinno) | |
67 | #endif | |
68 | #ifndef PUT_SCN_SCNLEN | |
69 | #define PUT_SCN_SCNLEN(abfd,in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_scn.x_scnlen) | |
70 | #endif | |
71 | #ifndef PUT_SCN_NRELOC | |
72 | #define PUT_SCN_NRELOC(abfd,in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_scn.x_nreloc) | |
73 | #endif | |
74 | #ifndef PUT_SCN_NLINNO | |
75 | #define PUT_SCN_NLINNO(abfd,in, ext) bfd_h_put_16(abfd,in, (bfd_byte *) ext->x_scn.x_nlinno) | |
76 | #endif | |
77 | #ifndef GET_LINENO_LNNO | |
78 | #define GET_LINENO_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) (ext->l_lnno)); | |
79 | #endif | |
80 | #ifndef PUT_LINENO_LNNO | |
81 | #define PUT_LINENO_LNNO(abfd,val, ext) bfd_h_put_16(abfd,val, (bfd_byte *) (ext->l_lnno)); | |
82 | #endif | |
83 | ||
84 | /* The f_symptr field in the filehdr is sometimes 64 bits. */ | |
85 | #ifndef GET_FILEHDR_SYMPTR | |
86 | #define GET_FILEHDR_SYMPTR bfd_h_get_32 | |
87 | #endif | |
88 | #ifndef PUT_FILEHDR_SYMPTR | |
89 | #define PUT_FILEHDR_SYMPTR bfd_h_put_32 | |
90 | #endif | |
91 | ||
92 | /* Some fields in the aouthdr are sometimes 64 bits. */ | |
93 | #ifndef GET_AOUTHDR_TSIZE | |
94 | #define GET_AOUTHDR_TSIZE bfd_h_get_32 | |
95 | #endif | |
96 | #ifndef PUT_AOUTHDR_TSIZE | |
97 | #define PUT_AOUTHDR_TSIZE bfd_h_put_32 | |
98 | #endif | |
99 | #ifndef GET_AOUTHDR_DSIZE | |
100 | #define GET_AOUTHDR_DSIZE bfd_h_get_32 | |
101 | #endif | |
102 | #ifndef PUT_AOUTHDR_DSIZE | |
103 | #define PUT_AOUTHDR_DSIZE bfd_h_put_32 | |
104 | #endif | |
105 | #ifndef GET_AOUTHDR_BSIZE | |
106 | #define GET_AOUTHDR_BSIZE bfd_h_get_32 | |
107 | #endif | |
108 | #ifndef PUT_AOUTHDR_BSIZE | |
109 | #define PUT_AOUTHDR_BSIZE bfd_h_put_32 | |
110 | #endif | |
111 | #ifndef GET_AOUTHDR_ENTRY | |
112 | #define GET_AOUTHDR_ENTRY bfd_h_get_32 | |
113 | #endif | |
114 | #ifndef PUT_AOUTHDR_ENTRY | |
115 | #define PUT_AOUTHDR_ENTRY bfd_h_put_32 | |
116 | #endif | |
117 | #ifndef GET_AOUTHDR_TEXT_START | |
118 | #define GET_AOUTHDR_TEXT_START bfd_h_get_32 | |
119 | #endif | |
120 | #ifndef PUT_AOUTHDR_TEXT_START | |
121 | #define PUT_AOUTHDR_TEXT_START bfd_h_put_32 | |
122 | #endif | |
123 | #ifndef GET_AOUTHDR_DATA_START | |
124 | #define GET_AOUTHDR_DATA_START bfd_h_get_32 | |
125 | #endif | |
126 | #ifndef PUT_AOUTHDR_DATA_START | |
127 | #define PUT_AOUTHDR_DATA_START bfd_h_put_32 | |
128 | #endif | |
129 | ||
130 | /* Some fields in the scnhdr are sometimes 64 bits. */ | |
131 | #ifndef GET_SCNHDR_PADDR | |
132 | #define GET_SCNHDR_PADDR bfd_h_get_32 | |
133 | #endif | |
134 | #ifndef PUT_SCNHDR_PADDR | |
135 | #define PUT_SCNHDR_PADDR bfd_h_put_32 | |
136 | #endif | |
137 | #ifndef GET_SCNHDR_VADDR | |
138 | #define GET_SCNHDR_VADDR bfd_h_get_32 | |
139 | #endif | |
140 | #ifndef PUT_SCNHDR_VADDR | |
141 | #define PUT_SCNHDR_VADDR bfd_h_put_32 | |
142 | #endif | |
143 | #ifndef GET_SCNHDR_SIZE | |
144 | #define GET_SCNHDR_SIZE bfd_h_get_32 | |
145 | #endif | |
146 | #ifndef PUT_SCNHDR_SIZE | |
147 | #define PUT_SCNHDR_SIZE bfd_h_put_32 | |
148 | #endif | |
149 | #ifndef GET_SCNHDR_SCNPTR | |
150 | #define GET_SCNHDR_SCNPTR bfd_h_get_32 | |
151 | #endif | |
152 | #ifndef PUT_SCNHDR_SCNPTR | |
153 | #define PUT_SCNHDR_SCNPTR bfd_h_put_32 | |
154 | #endif | |
155 | #ifndef GET_SCNHDR_RELPTR | |
156 | #define GET_SCNHDR_RELPTR bfd_h_get_32 | |
157 | #endif | |
158 | #ifndef PUT_SCNHDR_RELPTR | |
159 | #define PUT_SCNHDR_RELPTR bfd_h_put_32 | |
160 | #endif | |
161 | #ifndef GET_SCNHDR_LNNOPTR | |
162 | #define GET_SCNHDR_LNNOPTR bfd_h_get_32 | |
163 | #endif | |
164 | #ifndef PUT_SCNHDR_LNNOPTR | |
165 | #define PUT_SCNHDR_LNNOPTR bfd_h_put_32 | |
166 | #endif | |
167 | ||
168 | ||
169 | ||
170 | /**********************************************************************/ | |
171 | ||
172 | static void | |
173 | coff_swap_reloc_in (abfd, src, dst) | |
174 | bfd *abfd; | |
175 | PTR src; | |
176 | PTR dst; | |
177 | { | |
178 | RELOC *reloc_src = (RELOC *) src; | |
179 | struct internal_reloc *reloc_dst = (struct internal_reloc *) dst; | |
180 | ||
181 | reloc_dst->r_vaddr = bfd_h_get_32(abfd, (bfd_byte *)reloc_src->r_vaddr); | |
182 | reloc_dst->r_symndx = bfd_h_get_signed_32(abfd, (bfd_byte *) reloc_src->r_symndx); | |
183 | ||
184 | reloc_dst->r_type = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_type); | |
185 | ||
186 | #ifdef SWAP_IN_RELOC_OFFSET | |
187 | reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET(abfd, | |
188 | (bfd_byte *) reloc_src->r_offset); | |
189 | #endif | |
190 | ||
191 | } | |
192 | ||
193 | ||
194 | static unsigned int | |
195 | coff_swap_reloc_out (abfd, src, dst) | |
196 | bfd *abfd; | |
197 | PTR src; | |
198 | PTR dst; | |
199 | { | |
200 | struct internal_reloc *reloc_src = (struct internal_reloc *)src; | |
201 | struct external_reloc *reloc_dst = (struct external_reloc *)dst; | |
202 | bfd_h_put_32(abfd, reloc_src->r_vaddr, (bfd_byte *) reloc_dst->r_vaddr); | |
203 | bfd_h_put_32(abfd, reloc_src->r_symndx, (bfd_byte *) reloc_dst->r_symndx); | |
204 | ||
205 | bfd_h_put_16(abfd, reloc_src->r_type, (bfd_byte *) | |
206 | reloc_dst->r_type); | |
207 | ||
208 | #ifdef SWAP_OUT_RELOC_OFFSET | |
209 | SWAP_OUT_RELOC_OFFSET(abfd, | |
210 | reloc_src->r_offset, | |
211 | (bfd_byte *) reloc_dst->r_offset); | |
212 | #endif | |
213 | #ifdef SWAP_OUT_RELOC_EXTRA | |
214 | SWAP_OUT_RELOC_EXTRA(abfd,reloc_src, reloc_dst); | |
215 | #endif | |
216 | return sizeof(struct external_reloc); | |
217 | } | |
218 | ||
219 | ||
220 | static void | |
221 | coff_swap_filehdr_in (abfd, src, dst) | |
222 | bfd *abfd; | |
223 | PTR src; | |
224 | PTR dst; | |
225 | { | |
226 | FILHDR *filehdr_src = (FILHDR *) src; | |
227 | struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst; | |
228 | filehdr_dst->f_magic = bfd_h_get_16(abfd, (bfd_byte *) filehdr_src->f_magic); | |
229 | filehdr_dst->f_nscns = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_nscns); | |
230 | filehdr_dst->f_timdat = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_timdat); | |
231 | filehdr_dst->f_symptr = | |
232 | bfd_h_get_32 (abfd, (bfd_byte *) filehdr_src->f_symptr); | |
233 | filehdr_dst->f_nsyms = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_nsyms); | |
234 | filehdr_dst->f_opthdr = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_opthdr); | |
235 | filehdr_dst->f_flags = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_flags); | |
236 | } | |
237 | ||
238 | static unsigned int | |
239 | coff_swap_filehdr_out (abfd, in, out) | |
240 | bfd *abfd; | |
241 | PTR in; | |
242 | PTR out; | |
243 | { | |
beee31b1 | 244 | int idx; |
4e98461f SC |
245 | struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in; |
246 | FILHDR *filehdr_out = (FILHDR *)out; | |
247 | ||
beee31b1 | 248 | if (pe_data (abfd)->has_reloc_section) |
4e98461f SC |
249 | filehdr_in->f_flags &= ~F_RELFLG; |
250 | ||
251 | if (pe_data (abfd)->dll) | |
252 | filehdr_in->f_flags |= F_DLL; | |
253 | ||
254 | filehdr_in->pe.e_magic = DOSMAGIC; | |
255 | filehdr_in->pe.e_cblp = 0x90; | |
256 | filehdr_in->pe.e_cp = 0x3; | |
257 | filehdr_in->pe.e_crlc = 0x0; | |
258 | filehdr_in->pe.e_cparhdr = 0x4; | |
259 | filehdr_in->pe.e_minalloc = 0x0; | |
260 | filehdr_in->pe.e_maxalloc = 0xffff; | |
261 | filehdr_in->pe.e_ss = 0x0; | |
262 | filehdr_in->pe.e_sp = 0xb8; | |
263 | filehdr_in->pe.e_csum = 0x0; | |
264 | filehdr_in->pe.e_ip = 0x0; | |
265 | filehdr_in->pe.e_cs = 0x0; | |
266 | filehdr_in->pe.e_lfarlc = 0x40; | |
267 | filehdr_in->pe.e_ovno = 0x0; | |
beee31b1 SC |
268 | |
269 | for (idx=0; idx < 4; idx++) | |
270 | filehdr_in->pe.e_res[idx] = 0x0; | |
271 | ||
4e98461f SC |
272 | filehdr_in->pe.e_oemid = 0x0; |
273 | filehdr_in->pe.e_oeminfo = 0x0; | |
beee31b1 SC |
274 | |
275 | for (idx=0; idx < 10; idx++) | |
276 | filehdr_in->pe.e_res2[idx] = 0x0; | |
277 | ||
4e98461f SC |
278 | filehdr_in->pe.e_lfanew = 0x80; |
279 | ||
280 | /* this next collection of data are mostly just characters. It appears | |
281 | to be constant within the headers put on NT exes */ | |
282 | filehdr_in->pe.dos_message[0] = 0x0eba1f0e; | |
283 | filehdr_in->pe.dos_message[1] = 0xcd09b400; | |
284 | filehdr_in->pe.dos_message[2] = 0x4c01b821; | |
285 | filehdr_in->pe.dos_message[3] = 0x685421cd; | |
286 | filehdr_in->pe.dos_message[4] = 0x70207369; | |
287 | filehdr_in->pe.dos_message[5] = 0x72676f72; | |
288 | filehdr_in->pe.dos_message[6] = 0x63206d61; | |
289 | filehdr_in->pe.dos_message[7] = 0x6f6e6e61; | |
290 | filehdr_in->pe.dos_message[8] = 0x65622074; | |
291 | filehdr_in->pe.dos_message[9] = 0x6e757220; | |
292 | filehdr_in->pe.dos_message[10] = 0x206e6920; | |
293 | filehdr_in->pe.dos_message[11] = 0x20534f44; | |
294 | filehdr_in->pe.dos_message[12] = 0x65646f6d; | |
295 | filehdr_in->pe.dos_message[13] = 0x0a0d0d2e; | |
296 | filehdr_in->pe.dos_message[14] = 0x24; | |
297 | filehdr_in->pe.dos_message[15] = 0x0; | |
298 | filehdr_in->pe.nt_signature = NT_SIGNATURE; | |
299 | ||
300 | ||
301 | ||
302 | bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic); | |
303 | bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns); | |
304 | ||
305 | bfd_h_put_32(abfd, time (0), (bfd_byte *) filehdr_out->f_timdat); | |
306 | PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr, | |
307 | (bfd_byte *) filehdr_out->f_symptr); | |
308 | bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms); | |
309 | bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr); | |
310 | bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags); | |
311 | ||
312 | ||
313 | /* put in extra dos header stuff. This data remains essentially | |
314 | constant, it just has to be tacked on to the beginning of all exes | |
315 | for NT */ | |
316 | bfd_h_put_16(abfd, filehdr_in->pe.e_magic, (bfd_byte *) filehdr_out->e_magic); | |
317 | bfd_h_put_16(abfd, filehdr_in->pe.e_cblp, (bfd_byte *) filehdr_out->e_cblp); | |
318 | bfd_h_put_16(abfd, filehdr_in->pe.e_cp, (bfd_byte *) filehdr_out->e_cp); | |
319 | bfd_h_put_16(abfd, filehdr_in->pe.e_crlc, (bfd_byte *) filehdr_out->e_crlc); | |
320 | bfd_h_put_16(abfd, filehdr_in->pe.e_cparhdr, | |
321 | (bfd_byte *) filehdr_out->e_cparhdr); | |
322 | bfd_h_put_16(abfd, filehdr_in->pe.e_minalloc, | |
323 | (bfd_byte *) filehdr_out->e_minalloc); | |
324 | bfd_h_put_16(abfd, filehdr_in->pe.e_maxalloc, | |
325 | (bfd_byte *) filehdr_out->e_maxalloc); | |
326 | bfd_h_put_16(abfd, filehdr_in->pe.e_ss, (bfd_byte *) filehdr_out->e_ss); | |
327 | bfd_h_put_16(abfd, filehdr_in->pe.e_sp, (bfd_byte *) filehdr_out->e_sp); | |
328 | bfd_h_put_16(abfd, filehdr_in->pe.e_csum, (bfd_byte *) filehdr_out->e_csum); | |
329 | bfd_h_put_16(abfd, filehdr_in->pe.e_ip, (bfd_byte *) filehdr_out->e_ip); | |
330 | bfd_h_put_16(abfd, filehdr_in->pe.e_cs, (bfd_byte *) filehdr_out->e_cs); | |
331 | bfd_h_put_16(abfd, filehdr_in->pe.e_lfarlc, (bfd_byte *) filehdr_out->e_lfarlc); | |
332 | bfd_h_put_16(abfd, filehdr_in->pe.e_ovno, (bfd_byte *) filehdr_out->e_ovno); | |
333 | { | |
334 | int idx; | |
335 | for (idx=0; idx < 4; idx++) | |
336 | bfd_h_put_16(abfd, filehdr_in->pe.e_res[idx], | |
337 | (bfd_byte *) filehdr_out->e_res[idx]); | |
338 | } | |
339 | bfd_h_put_16(abfd, filehdr_in->pe.e_oemid, (bfd_byte *) filehdr_out->e_oemid); | |
340 | bfd_h_put_16(abfd, filehdr_in->pe.e_oeminfo, | |
341 | (bfd_byte *) filehdr_out->e_oeminfo); | |
342 | { | |
343 | int idx; | |
344 | for (idx=0; idx < 10; idx++) | |
345 | bfd_h_put_16(abfd, filehdr_in->pe.e_res2[idx], | |
346 | (bfd_byte *) filehdr_out->e_res2[idx]); | |
347 | } | |
348 | bfd_h_put_32(abfd, filehdr_in->pe.e_lfanew, (bfd_byte *) filehdr_out->e_lfanew); | |
349 | ||
350 | { | |
351 | int idx; | |
352 | for (idx=0; idx < 16; idx++) | |
353 | bfd_h_put_32(abfd, filehdr_in->pe.dos_message[idx], | |
354 | (bfd_byte *) filehdr_out->dos_message[idx]); | |
355 | } | |
356 | ||
357 | /* also put in the NT signature */ | |
358 | bfd_h_put_32(abfd, filehdr_in->pe.nt_signature, | |
359 | (bfd_byte *) filehdr_out->nt_signature); | |
360 | ||
361 | ||
362 | ||
363 | ||
364 | return sizeof(FILHDR); | |
365 | } | |
366 | ||
367 | ||
368 | ||
369 | static void | |
370 | coff_swap_sym_in (abfd, ext1, in1) | |
371 | bfd *abfd; | |
372 | PTR ext1; | |
373 | PTR in1; | |
374 | { | |
375 | SYMENT *ext = (SYMENT *)ext1; | |
376 | struct internal_syment *in = (struct internal_syment *)in1; | |
377 | ||
378 | if( ext->e.e_name[0] == 0) { | |
379 | in->_n._n_n._n_zeroes = 0; | |
380 | in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset); | |
381 | } | |
382 | else { | |
383 | #if SYMNMLEN != E_SYMNMLEN | |
384 | -> Error, we need to cope with truncating or extending SYMNMLEN!; | |
385 | #else | |
386 | memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN); | |
387 | #endif | |
388 | } | |
389 | in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value); | |
390 | in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum); | |
391 | if (sizeof(ext->e_type) == 2){ | |
392 | in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type); | |
393 | } | |
394 | else { | |
395 | in->n_type = bfd_h_get_32(abfd, (bfd_byte *) ext->e_type); | |
396 | } | |
397 | in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass); | |
398 | in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux); | |
399 | ||
400 | /* The section symbols for the .idata$ sections have class 68, which MS | |
401 | documentation indicates is a section symbol. The problem is that the | |
402 | value field in the symbol is simply a copy of the .idata section's flags | |
403 | rather than something useful. When these symbols are encountered, change | |
404 | the value to 0 and the section number to 1 so that they will be handled | |
405 | somewhat correctly in the bfd code. */ | |
406 | if (in->n_sclass == 0x68) { | |
407 | in->n_value = 0x0; | |
408 | in->n_scnum = 1; | |
409 | /* I have tried setting the class to 3 and using the following to set | |
410 | the section number. This will put the address of the pointer to the | |
411 | string kernel32.dll at addresses 0 and 0x10 off start of idata section | |
412 | which is not correct */ | |
413 | /* if (strcmp (in->_n._n_name, ".idata$4") == 0) */ | |
414 | /* in->n_scnum = 3; */ | |
415 | /* else */ | |
416 | /* in->n_scnum = 2; */ | |
417 | } | |
418 | } | |
419 | ||
420 | static unsigned int | |
421 | coff_swap_sym_out (abfd, inp, extp) | |
422 | bfd *abfd; | |
423 | PTR inp; | |
424 | PTR extp; | |
425 | { | |
426 | struct internal_syment *in = (struct internal_syment *)inp; | |
427 | SYMENT *ext =(SYMENT *)extp; | |
428 | if(in->_n._n_name[0] == 0) { | |
429 | bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes); | |
430 | bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *) ext->e.e.e_offset); | |
431 | } | |
432 | else { | |
433 | #if SYMNMLEN != E_SYMNMLEN | |
434 | -> Error, we need to cope with truncating or extending SYMNMLEN!; | |
435 | #else | |
436 | memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN); | |
437 | #endif | |
438 | } | |
439 | bfd_h_put_32(abfd, in->n_value , (bfd_byte *) ext->e_value); | |
440 | bfd_h_put_16(abfd, in->n_scnum , (bfd_byte *) ext->e_scnum); | |
441 | if (sizeof(ext->e_type) == 2) | |
442 | { | |
443 | bfd_h_put_16(abfd, in->n_type , (bfd_byte *) ext->e_type); | |
444 | } | |
445 | else | |
446 | { | |
447 | bfd_h_put_32(abfd, in->n_type , (bfd_byte *) ext->e_type); | |
448 | } | |
449 | bfd_h_put_8(abfd, in->n_sclass , ext->e_sclass); | |
450 | bfd_h_put_8(abfd, in->n_numaux , ext->e_numaux); | |
451 | return sizeof(SYMENT); | |
452 | } | |
453 | ||
454 | static void | |
455 | coff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1) | |
456 | bfd *abfd; | |
457 | PTR ext1; | |
458 | int type; | |
459 | int class; | |
460 | int indx; | |
461 | int numaux; | |
462 | PTR in1; | |
463 | { | |
464 | AUXENT *ext = (AUXENT *)ext1; | |
465 | union internal_auxent *in = (union internal_auxent *)in1; | |
466 | ||
467 | switch (class) { | |
468 | case C_FILE: | |
469 | if (ext->x_file.x_fname[0] == 0) { | |
470 | in->x_file.x_n.x_zeroes = 0; | |
471 | in->x_file.x_n.x_offset = | |
472 | bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset); | |
473 | } else { | |
474 | #if FILNMLEN != E_FILNMLEN | |
475 | -> Error, we need to cope with truncating or extending FILNMLEN!; | |
476 | #else | |
477 | memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN); | |
478 | #endif | |
479 | } | |
480 | return; | |
481 | ||
482 | ||
483 | case C_STAT: | |
484 | #ifdef C_LEAFSTAT | |
485 | case C_LEAFSTAT: | |
486 | #endif | |
487 | case C_HIDDEN: | |
488 | if (type == T_NULL) { | |
489 | in->x_scn.x_scnlen = GET_SCN_SCNLEN(abfd, ext); | |
490 | in->x_scn.x_nreloc = GET_SCN_NRELOC(abfd, ext); | |
491 | in->x_scn.x_nlinno = GET_SCN_NLINNO(abfd, ext); | |
492 | return; | |
493 | } | |
494 | break; | |
495 | } | |
496 | ||
497 | in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx); | |
498 | #ifndef NO_TVNDX | |
499 | in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx); | |
500 | #endif | |
501 | ||
502 | if (class == C_BLOCK || ISFCN (type) || ISTAG (class)) | |
503 | { | |
504 | in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext); | |
505 | in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext); | |
506 | } | |
507 | else | |
508 | { | |
509 | #if DIMNUM != E_DIMNUM | |
510 | #error we need to cope with truncating or extending DIMNUM | |
511 | #endif | |
512 | in->x_sym.x_fcnary.x_ary.x_dimen[0] = | |
513 | bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]); | |
514 | in->x_sym.x_fcnary.x_ary.x_dimen[1] = | |
515 | bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]); | |
516 | in->x_sym.x_fcnary.x_ary.x_dimen[2] = | |
517 | bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]); | |
518 | in->x_sym.x_fcnary.x_ary.x_dimen[3] = | |
519 | bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]); | |
520 | } | |
521 | ||
522 | if (ISFCN(type)) { | |
523 | in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize); | |
524 | } | |
525 | else { | |
526 | in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO(abfd, ext); | |
527 | in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE(abfd, ext); | |
528 | } | |
529 | } | |
530 | ||
531 | static unsigned int | |
532 | coff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp) | |
533 | bfd *abfd; | |
534 | PTR inp; | |
535 | int type; | |
536 | int class; | |
537 | int indx; | |
538 | int numaux; | |
539 | PTR extp; | |
540 | { | |
541 | union internal_auxent *in = (union internal_auxent *)inp; | |
542 | AUXENT *ext = (AUXENT *)extp; | |
543 | ||
544 | memset((PTR)ext, 0, AUXESZ); | |
545 | switch (class) { | |
546 | case C_FILE: | |
547 | if (in->x_file.x_fname[0] == 0) { | |
548 | bfd_h_put_32(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes); | |
549 | bfd_h_put_32(abfd, | |
550 | in->x_file.x_n.x_offset, | |
551 | (bfd_byte *) ext->x_file.x_n.x_offset); | |
552 | } | |
553 | else { | |
554 | #if FILNMLEN != E_FILNMLEN | |
555 | -> Error, we need to cope with truncating or extending FILNMLEN!; | |
556 | #else | |
557 | memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN); | |
558 | #endif | |
559 | } | |
560 | return sizeof (AUXENT); | |
561 | ||
562 | ||
563 | case C_STAT: | |
564 | #ifdef C_LEAFSTAT | |
565 | case C_LEAFSTAT: | |
566 | #endif | |
567 | case C_HIDDEN: | |
568 | if (type == T_NULL) { | |
569 | PUT_SCN_SCNLEN(abfd, in->x_scn.x_scnlen, ext); | |
570 | PUT_SCN_NRELOC(abfd, in->x_scn.x_nreloc, ext); | |
571 | PUT_SCN_NLINNO(abfd, in->x_scn.x_nlinno, ext); | |
572 | return sizeof (AUXENT); | |
573 | } | |
574 | break; | |
575 | } | |
576 | ||
577 | bfd_h_put_32(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx); | |
578 | #ifndef NO_TVNDX | |
579 | bfd_h_put_16(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx); | |
580 | #endif | |
581 | ||
582 | if (class == C_BLOCK || ISFCN (type) || ISTAG (class)) | |
583 | { | |
584 | PUT_FCN_LNNOPTR(abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext); | |
585 | PUT_FCN_ENDNDX(abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext); | |
586 | } | |
587 | else | |
588 | { | |
589 | #if DIMNUM != E_DIMNUM | |
590 | #error we need to cope with truncating or extending DIMNUM | |
591 | #endif | |
592 | bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0], | |
593 | (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]); | |
594 | bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1], | |
595 | (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]); | |
596 | bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2], | |
597 | (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]); | |
598 | bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3], | |
599 | (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]); | |
600 | } | |
601 | ||
602 | if (ISFCN (type)) | |
603 | bfd_h_put_32 (abfd, in->x_sym.x_misc.x_fsize, | |
604 | (bfd_byte *) ext->x_sym.x_misc.x_fsize); | |
605 | else | |
606 | { | |
607 | PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext); | |
608 | PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext); | |
609 | } | |
610 | ||
611 | return sizeof(AUXENT); | |
612 | } | |
613 | ||
614 | ||
615 | static void | |
616 | coff_swap_lineno_in (abfd, ext1, in1) | |
617 | bfd *abfd; | |
618 | PTR ext1; | |
619 | PTR in1; | |
620 | { | |
621 | LINENO *ext = (LINENO *)ext1; | |
622 | struct internal_lineno *in = (struct internal_lineno *)in1; | |
623 | ||
624 | in->l_addr.l_symndx = bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx); | |
625 | in->l_lnno = GET_LINENO_LNNO(abfd, ext); | |
626 | } | |
627 | ||
628 | static unsigned int | |
629 | coff_swap_lineno_out (abfd, inp, outp) | |
630 | bfd *abfd; | |
631 | PTR inp; | |
632 | PTR outp; | |
633 | { | |
634 | struct internal_lineno *in = (struct internal_lineno *)inp; | |
635 | struct external_lineno *ext = (struct external_lineno *)outp; | |
636 | bfd_h_put_32(abfd, in->l_addr.l_symndx, (bfd_byte *) | |
637 | ext->l_addr.l_symndx); | |
638 | ||
639 | PUT_LINENO_LNNO (abfd, in->l_lnno, ext); | |
640 | return sizeof(struct external_lineno); | |
641 | } | |
642 | ||
643 | ||
644 | ||
645 | static void | |
646 | coff_swap_aouthdr_in (abfd, aouthdr_ext1, aouthdr_int1) | |
647 | bfd *abfd; | |
648 | PTR aouthdr_ext1; | |
649 | PTR aouthdr_int1; | |
650 | { | |
651 | struct internal_extra_pe_aouthdr *a; | |
652 | PEAOUTHDR *src = (PEAOUTHDR *)(aouthdr_ext1); | |
653 | AOUTHDR *aouthdr_ext = (AOUTHDR *) aouthdr_ext1; | |
654 | struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1; | |
655 | ||
656 | aouthdr_int->magic = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->magic); | |
657 | aouthdr_int->vstamp = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->vstamp); | |
658 | aouthdr_int->tsize = | |
659 | GET_AOUTHDR_TSIZE (abfd, (bfd_byte *) aouthdr_ext->tsize); | |
660 | aouthdr_int->dsize = | |
661 | GET_AOUTHDR_DSIZE (abfd, (bfd_byte *) aouthdr_ext->dsize); | |
662 | aouthdr_int->bsize = | |
663 | GET_AOUTHDR_BSIZE (abfd, (bfd_byte *) aouthdr_ext->bsize); | |
664 | aouthdr_int->entry = | |
665 | GET_AOUTHDR_ENTRY (abfd, (bfd_byte *) aouthdr_ext->entry); | |
666 | aouthdr_int->text_start = | |
667 | GET_AOUTHDR_TEXT_START (abfd, (bfd_byte *) aouthdr_ext->text_start); | |
668 | aouthdr_int->data_start = | |
669 | GET_AOUTHDR_DATA_START (abfd, (bfd_byte *) aouthdr_ext->data_start); | |
670 | ||
671 | a = &aouthdr_int->pe; | |
672 | a->ImageBase = bfd_h_get_32 (abfd, src->ImageBase); | |
673 | a->SectionAlignment = bfd_h_get_32 (abfd, src->SectionAlignment); | |
674 | a->FileAlignment = bfd_h_get_32 (abfd, src->FileAlignment); | |
675 | a->MajorOperatingSystemVersion = | |
676 | bfd_h_get_16 (abfd, src->MajorOperatingSystemVersion); | |
677 | a->MinorOperatingSystemVersion = | |
678 | bfd_h_get_16 (abfd, src->MinorOperatingSystemVersion); | |
679 | a->MajorImageVersion = bfd_h_get_16 (abfd, src->MajorImageVersion); | |
680 | a->MinorImageVersion = bfd_h_get_16 (abfd, src->MinorImageVersion); | |
681 | a->MajorSubsystemVersion = bfd_h_get_16 (abfd, src->MajorSubsystemVersion); | |
682 | a->MinorSubsystemVersion = bfd_h_get_16 (abfd, src->MinorSubsystemVersion); | |
683 | a->Reserved1 = bfd_h_get_32 (abfd, src->Reserved1); | |
684 | a->SizeOfImage = bfd_h_get_32 (abfd, src->SizeOfImage); | |
685 | a->SizeOfHeaders = bfd_h_get_32 (abfd, src->SizeOfHeaders); | |
686 | a->CheckSum = bfd_h_get_32 (abfd, src->CheckSum); | |
687 | a->Subsystem = bfd_h_get_16 (abfd, src->Subsystem); | |
688 | a->DllCharacteristics = bfd_h_get_16 (abfd, src->DllCharacteristics); | |
689 | a->SizeOfStackReserve = bfd_h_get_32 (abfd, src->SizeOfStackReserve); | |
690 | a->SizeOfStackCommit = bfd_h_get_32 (abfd, src->SizeOfStackCommit); | |
691 | a->SizeOfHeapReserve = bfd_h_get_32 (abfd, src->SizeOfHeapReserve); | |
692 | a->SizeOfHeapCommit = bfd_h_get_32 (abfd, src->SizeOfHeapCommit); | |
693 | a->LoaderFlags = bfd_h_get_32 (abfd, src->LoaderFlags); | |
694 | a->NumberOfRvaAndSizes = bfd_h_get_32 (abfd, src->NumberOfRvaAndSizes); | |
695 | ||
696 | { | |
697 | int idx; | |
698 | for (idx=0; idx < 16; idx++) | |
699 | { | |
700 | a->DataDirectory[idx].VirtualAddress = | |
701 | bfd_h_get_32 (abfd, src->DataDirectory[idx][0]); | |
702 | a->DataDirectory[idx].Size = | |
703 | bfd_h_get_32 (abfd, src->DataDirectory[idx][1]); | |
704 | } | |
705 | } | |
ae115e51 ILT |
706 | |
707 | if (aouthdr_int->entry) | |
708 | aouthdr_int->entry += a->ImageBase; | |
709 | if (aouthdr_int->tsize) | |
710 | aouthdr_int->text_start += a->ImageBase; | |
711 | if (aouthdr_int->dsize) | |
712 | aouthdr_int->data_start += a->ImageBase; | |
4e98461f SC |
713 | } |
714 | ||
715 | ||
716 | static void add_data_entry (abfd, aout, idx, name, base) | |
717 | bfd *abfd; | |
718 | struct internal_extra_pe_aouthdr *aout; | |
719 | int idx; | |
720 | char *name; | |
721 | bfd_vma base; | |
722 | { | |
723 | asection *sec = bfd_get_section_by_name (abfd, name); | |
724 | ||
725 | /* add import directory information if it exists */ | |
726 | if (sec != NULL) | |
727 | { | |
728 | aout->DataDirectory[idx].VirtualAddress = sec->lma - base; | |
729 | aout->DataDirectory[idx].Size = sec->_raw_size; | |
730 | sec->flags |= SEC_DATA; | |
731 | } | |
732 | } | |
733 | ||
734 | ||
735 | static unsigned int | |
736 | coff_swap_aouthdr_out (abfd, in, out) | |
737 | bfd *abfd; | |
738 | PTR in; | |
739 | PTR out; | |
740 | { | |
741 | struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in; | |
742 | struct internal_extra_pe_aouthdr *extra = &pe_data (abfd)->pe_opthdr; | |
743 | PEAOUTHDR *aouthdr_out = (PEAOUTHDR *)out; | |
744 | ||
745 | bfd_vma sa = extra->SectionAlignment; | |
746 | bfd_vma fa = extra->FileAlignment; | |
747 | bfd_vma ib = extra->ImageBase ; | |
748 | ||
749 | if (aouthdr_in->tsize) | |
750 | aouthdr_in->text_start -= ib; | |
751 | if (aouthdr_in->dsize) | |
752 | aouthdr_in->data_start -= ib; | |
753 | if (aouthdr_in->entry) | |
754 | aouthdr_in->entry -= ib; | |
755 | ||
756 | #define FA(x) (((x) + fa -1 ) & (- fa)) | |
757 | #define SA(x) (((x) + sa -1 ) & (- sa)) | |
758 | ||
759 | /* We like to have the sizes aligned */ | |
760 | ||
761 | aouthdr_in->bsize = FA (aouthdr_in->bsize); | |
762 | ||
763 | ||
764 | extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES; | |
765 | ||
766 | /* first null out all data directory entries .. */ | |
767 | memset (extra->DataDirectory, sizeof (extra->DataDirectory), 0); | |
768 | ||
769 | add_data_entry (abfd, extra, 0, ".edata", ib); | |
770 | add_data_entry (abfd, extra, 1, ".idata", ib); | |
771 | add_data_entry (abfd, extra, 2, ".rsrc" ,ib); | |
772 | add_data_entry (abfd, extra, 5, ".reloc", ib); | |
773 | { | |
774 | asection *sec; | |
775 | bfd_vma dsize= 0; | |
776 | bfd_vma isize = SA(abfd->sections->filepos); | |
777 | bfd_vma tsize= 0; | |
778 | for (sec = abfd->sections; sec; sec = sec->next) | |
779 | { | |
780 | int rounded = FA(sec->_raw_size); | |
781 | if (sec->flags & SEC_DATA) | |
782 | dsize += rounded; | |
783 | if (sec->flags & SEC_CODE) | |
784 | tsize += rounded; | |
785 | isize += SA(rounded); | |
786 | } | |
787 | ||
788 | aouthdr_in->dsize = dsize; | |
789 | aouthdr_in->tsize = tsize; | |
790 | extra->SizeOfImage = isize; | |
791 | } | |
792 | ||
793 | extra->SizeOfHeaders = abfd->sections->filepos; | |
794 | bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->standard.magic); | |
795 | bfd_h_put_16(abfd, aouthdr_in->vstamp, (bfd_byte *) aouthdr_out->standard.vstamp); | |
796 | PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->standard.tsize); | |
797 | PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->standard.dsize); | |
798 | PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->standard.bsize); | |
799 | PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->standard.entry); | |
800 | PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start, | |
801 | (bfd_byte *) aouthdr_out->standard.text_start); | |
802 | PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start, | |
803 | (bfd_byte *) aouthdr_out->standard.data_start); | |
804 | ||
805 | ||
806 | bfd_h_put_32 (abfd, extra->ImageBase, | |
807 | (bfd_byte *) aouthdr_out->ImageBase); | |
808 | bfd_h_put_32 (abfd, extra->SectionAlignment, | |
809 | (bfd_byte *) aouthdr_out->SectionAlignment); | |
810 | bfd_h_put_32 (abfd, extra->FileAlignment, | |
811 | (bfd_byte *) aouthdr_out->FileAlignment); | |
812 | bfd_h_put_16 (abfd, extra->MajorOperatingSystemVersion, | |
813 | (bfd_byte *) aouthdr_out->MajorOperatingSystemVersion); | |
814 | bfd_h_put_16 (abfd, extra->MinorOperatingSystemVersion, | |
815 | (bfd_byte *) aouthdr_out->MinorOperatingSystemVersion); | |
816 | bfd_h_put_16 (abfd, extra->MajorImageVersion, | |
817 | (bfd_byte *) aouthdr_out->MajorImageVersion); | |
818 | bfd_h_put_16 (abfd, extra->MinorImageVersion, | |
819 | (bfd_byte *) aouthdr_out->MinorImageVersion); | |
820 | bfd_h_put_16 (abfd, extra->MajorSubsystemVersion, | |
821 | (bfd_byte *) aouthdr_out->MajorSubsystemVersion); | |
822 | bfd_h_put_16 (abfd, extra->MinorSubsystemVersion, | |
823 | (bfd_byte *) aouthdr_out->MinorSubsystemVersion); | |
824 | bfd_h_put_32 (abfd, extra->Reserved1, | |
825 | (bfd_byte *) aouthdr_out->Reserved1); | |
826 | bfd_h_put_32 (abfd, extra->SizeOfImage, | |
827 | (bfd_byte *) aouthdr_out->SizeOfImage); | |
828 | bfd_h_put_32 (abfd, extra->SizeOfHeaders, | |
829 | (bfd_byte *) aouthdr_out->SizeOfHeaders); | |
830 | bfd_h_put_32 (abfd, extra->CheckSum, | |
831 | (bfd_byte *) aouthdr_out->CheckSum); | |
832 | bfd_h_put_16 (abfd, extra->Subsystem, | |
833 | (bfd_byte *) aouthdr_out->Subsystem); | |
834 | bfd_h_put_16 (abfd, extra->DllCharacteristics, | |
835 | (bfd_byte *) aouthdr_out->DllCharacteristics); | |
836 | bfd_h_put_32 (abfd, extra->SizeOfStackReserve, | |
837 | (bfd_byte *) aouthdr_out->SizeOfStackReserve); | |
838 | bfd_h_put_32 (abfd, extra->SizeOfStackCommit, | |
839 | (bfd_byte *) aouthdr_out->SizeOfStackCommit); | |
840 | bfd_h_put_32 (abfd, extra->SizeOfHeapReserve, | |
841 | (bfd_byte *) aouthdr_out->SizeOfHeapReserve); | |
842 | bfd_h_put_32 (abfd, extra->SizeOfHeapCommit, | |
843 | (bfd_byte *) aouthdr_out->SizeOfHeapCommit); | |
844 | bfd_h_put_32 (abfd, extra->LoaderFlags, | |
845 | (bfd_byte *) aouthdr_out->LoaderFlags); | |
846 | bfd_h_put_32 (abfd, extra->NumberOfRvaAndSizes, | |
847 | (bfd_byte *) aouthdr_out->NumberOfRvaAndSizes); | |
848 | { | |
849 | int idx; | |
850 | for (idx=0; idx < 16; idx++) | |
851 | { | |
852 | bfd_h_put_32 (abfd, extra->DataDirectory[idx].VirtualAddress, | |
853 | (bfd_byte *) aouthdr_out->DataDirectory[idx][0]); | |
854 | bfd_h_put_32 (abfd, extra->DataDirectory[idx].Size, | |
855 | (bfd_byte *) aouthdr_out->DataDirectory[idx][1]); | |
856 | } | |
857 | } | |
858 | ||
859 | ||
860 | return sizeof(AOUTHDR); | |
861 | } | |
862 | ||
863 | static void | |
864 | coff_swap_scnhdr_in (abfd, ext, in) | |
865 | bfd *abfd; | |
866 | PTR ext; | |
867 | PTR in; | |
868 | { | |
869 | SCNHDR *scnhdr_ext = (SCNHDR *) ext; | |
870 | struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in; | |
871 | ||
872 | memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof(scnhdr_int->s_name)); | |
873 | scnhdr_int->s_vaddr = | |
874 | GET_SCNHDR_VADDR (abfd, (bfd_byte *) scnhdr_ext->s_vaddr); | |
875 | scnhdr_int->s_paddr = | |
876 | GET_SCNHDR_PADDR (abfd, (bfd_byte *) scnhdr_ext->s_paddr); | |
877 | scnhdr_int->s_size = | |
878 | GET_SCNHDR_SIZE (abfd, (bfd_byte *) scnhdr_ext->s_size); | |
4e98461f SC |
879 | scnhdr_int->s_scnptr = |
880 | GET_SCNHDR_SCNPTR (abfd, (bfd_byte *) scnhdr_ext->s_scnptr); | |
881 | scnhdr_int->s_relptr = | |
882 | GET_SCNHDR_RELPTR (abfd, (bfd_byte *) scnhdr_ext->s_relptr); | |
883 | scnhdr_int->s_lnnoptr = | |
884 | GET_SCNHDR_LNNOPTR (abfd, (bfd_byte *) scnhdr_ext->s_lnnoptr); | |
885 | scnhdr_int->s_flags = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_flags); | |
886 | ||
887 | scnhdr_int->s_nreloc = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nreloc); | |
888 | scnhdr_int->s_nlnno = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nlnno); | |
889 | ||
890 | if (scnhdr_int->s_vaddr != 0) | |
891 | { | |
892 | scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase; | |
893 | } | |
894 | } | |
895 | ||
896 | static unsigned int | |
500d7394 SC |
897 | coff_swap_scnhdr_out (abfd, in, out) |
898 | bfd *abfd; | |
899 | PTR in; | |
900 | PTR out; | |
4e98461f SC |
901 | { |
902 | struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in; | |
903 | SCNHDR *scnhdr_ext = (SCNHDR *)out; | |
904 | unsigned int ret = sizeof (SCNHDR); | |
500d7394 SC |
905 | bfd_vma ps; |
906 | bfd_vma ss; | |
4e98461f SC |
907 | |
908 | memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name)); | |
909 | ||
910 | ||
911 | PUT_SCNHDR_VADDR (abfd, | |
912 | (scnhdr_int->s_vaddr | |
913 | - pe_data(abfd)->pe_opthdr.ImageBase), | |
914 | (bfd_byte *) scnhdr_ext->s_vaddr); | |
915 | ||
4e98461f SC |
916 | /* NT wants the size data to be rounded up to the next NT_FILE_ALIGNMENT |
917 | value except for the BSS section, its s_size should be 0 */ | |
beee31b1 | 918 | |
500d7394 | 919 | |
beee31b1 | 920 | if (strcmp (scnhdr_int->s_name, _BSS) == 0) |
500d7394 SC |
921 | { |
922 | ps = scnhdr_int->s_size; | |
923 | ss = 0; | |
924 | } | |
4e98461f | 925 | else |
500d7394 SC |
926 | { |
927 | ps = scnhdr_int->s_paddr; | |
928 | ss = scnhdr_int->s_size; | |
929 | } | |
930 | ||
931 | PUT_SCNHDR_SIZE (abfd, ss, | |
932 | (bfd_byte *) scnhdr_ext->s_size); | |
933 | ||
beee31b1 | 934 | |
500d7394 | 935 | PUT_SCNHDR_PADDR (abfd, ps, (bfd_byte *) scnhdr_ext->s_paddr); |
4e98461f SC |
936 | |
937 | PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr, | |
938 | (bfd_byte *) scnhdr_ext->s_scnptr); | |
939 | PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr, | |
940 | (bfd_byte *) scnhdr_ext->s_relptr); | |
941 | PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr, | |
942 | (bfd_byte *) scnhdr_ext->s_lnnoptr); | |
943 | ||
944 | /* Extra flags must be set when dealing with NT. All sections should also | |
945 | have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the | |
946 | .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data | |
947 | sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set | |
948 | (this is especially important when dealing with the .idata section since | |
949 | the addresses for routines from .dlls must be overwritten). If .reloc | |
950 | section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE | |
951 | (0x02000000). Also, the resource data should also be read and | |
952 | writable. */ | |
953 | { | |
954 | int flags = scnhdr_int->s_flags; | |
955 | if (strcmp (scnhdr_int->s_name, ".data") == 0 || | |
956 | strcmp (scnhdr_int->s_name, ".CRT") == 0 || | |
957 | strcmp (scnhdr_int->s_name, ".rsrc") == 0 || | |
958 | strcmp (scnhdr_int->s_name, ".bss") == 0) | |
959 | flags |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE; | |
960 | else if (strcmp (scnhdr_int->s_name, ".text") == 0) | |
961 | flags |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE; | |
962 | else if (strcmp (scnhdr_int->s_name, ".reloc") == 0) | |
963 | flags = SEC_DATA| IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE; | |
964 | else if (strcmp (scnhdr_int->s_name, ".idata") == 0) | |
965 | flags = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | SEC_DATA; | |
966 | else if (strcmp (scnhdr_int->s_name, ".rdata") == 0 | |
967 | || strcmp (scnhdr_int->s_name, ".edata") == 0) | |
968 | flags = IMAGE_SCN_MEM_READ | SEC_DATA; | |
969 | ||
970 | bfd_h_put_32(abfd, flags, (bfd_byte *) scnhdr_ext->s_flags); | |
971 | } | |
972 | ||
973 | if (scnhdr_int->s_nlnno <= 0xffff) | |
974 | bfd_h_put_16(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno); | |
975 | else | |
976 | { | |
977 | (*_bfd_error_handler) ("%s: line number overflow: 0x%lx > 0xffff", | |
978 | bfd_get_filename (abfd), | |
979 | scnhdr_int->s_nlnno); | |
980 | bfd_set_error (bfd_error_file_truncated); | |
981 | bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nlnno); | |
982 | ret = 0; | |
983 | } | |
984 | if (scnhdr_int->s_nreloc <= 0xffff) | |
985 | bfd_h_put_16(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc); | |
986 | else | |
987 | { | |
988 | (*_bfd_error_handler) ("%s: reloc overflow: 0x%lx > 0xffff", | |
989 | bfd_get_filename (abfd), | |
990 | scnhdr_int->s_nreloc); | |
991 | bfd_set_error (bfd_error_file_truncated); | |
992 | bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc); | |
993 | ret = 0; | |
994 | } | |
995 | return ret; | |
996 | } | |
997 | /**********************************************************************/ | |
998 | static boolean | |
999 | pe_print_private_bfd_data (abfd, vfile) | |
1000 | bfd*abfd; | |
1001 | void *vfile; | |
1002 | { | |
1003 | FILE *file = vfile; | |
1004 | int j; | |
1005 | pe_data_type *pe = pe_data (abfd); | |
1006 | struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr; | |
ae115e51 ILT |
1007 | fprintf (file,"ImageBase\t\t"); |
1008 | fprintf_vma (file, i->ImageBase); | |
1009 | fprintf (file,"SectionAlignment\t"); | |
1010 | fprintf_vma (file, i->SectionAlignment); | |
1011 | fprintf (file,"FileAlignment\t\t"); | |
1012 | fprintf_vma (file, i->FileAlignment); | |
4e98461f SC |
1013 | fprintf (file,"MajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion); |
1014 | fprintf (file,"MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion); | |
1015 | fprintf (file,"MajorImageVersion\t%d\n", i->MajorImageVersion); | |
1016 | fprintf (file,"MinorImageVersion\t%d\n", i->MinorImageVersion); | |
1017 | fprintf (file,"MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion); | |
1018 | fprintf (file,"MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion); | |
ae115e51 ILT |
1019 | fprintf (file,"Reserved1\t\t%08lx\n", i->Reserved1); |
1020 | fprintf (file,"SizeOfImage\t\t%08lx\n", i->SizeOfImage); | |
1021 | fprintf (file,"SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders); | |
1022 | fprintf (file,"CheckSum\t\t%08lx\n", i->CheckSum); | |
4e98461f SC |
1023 | fprintf (file,"Subsystem\t\t%08x\n", i->Subsystem); |
1024 | fprintf (file,"DllCharacteristics\t%08x\n", i->DllCharacteristics); | |
ae115e51 ILT |
1025 | fprintf (file,"SizeOfStackReserve\t"); |
1026 | fprintf_vma (file, i->SizeOfStackReserve); | |
1027 | fprintf (file,"SizeOfStackCommit\t"); | |
1028 | fprintf_vma (file, i->SizeOfStackCommit); | |
1029 | fprintf (file,"SizeOfHeapReserve\t"); | |
1030 | fprintf_vma (file, i->SizeOfHeapReserve); | |
1031 | fprintf (file,"SizeOfHeapCommit\t"); | |
1032 | fprintf_vma (file, i->SizeOfHeapCommit); | |
1033 | fprintf (file,"LoaderFlags\t\t%08lx\n", i->LoaderFlags); | |
1034 | fprintf (file,"NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes); | |
4e98461f SC |
1035 | |
1036 | for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++) | |
1037 | { | |
ae115e51 ILT |
1038 | fprintf (file, "Entry %2d ", j); |
1039 | fprintf_vma (file, i->DataDirectory[j].VirtualAddress); | |
1040 | fprintf (file, " %08lx\n", i->DataDirectory[j].Size); | |
4e98461f | 1041 | } |
ae115e51 ILT |
1042 | |
1043 | return true; | |
4e98461f SC |
1044 | } |
1045 | ||
1046 | static boolean | |
1047 | pe_mkobject (abfd) | |
1048 | bfd * abfd; | |
1049 | { | |
1050 | pe_data_type *pe; | |
1051 | ||
1052 | abfd->tdata.pe_obj_data = | |
1053 | (struct pe_tdata *) bfd_zalloc (abfd, sizeof (pe_data_type)); | |
1054 | if (abfd->tdata.pe_obj_data == 0) | |
1055 | { | |
1056 | bfd_set_error (bfd_error_no_memory); | |
1057 | return false; | |
1058 | } | |
1059 | pe =pe_data (abfd); | |
1060 | pe->coff.pe = 1; | |
1061 | return true; | |
1062 | } | |
1063 | ||
1064 | /* Create the COFF backend specific information. */ | |
1065 | static PTR | |
1066 | pe_mkobject_hook (abfd, filehdr, aouthdr) | |
1067 | bfd * abfd; | |
1068 | PTR filehdr; | |
1069 | PTR aouthdr; | |
1070 | { | |
1071 | struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; | |
1072 | pe_data_type *pe; | |
1073 | ||
1074 | if (pe_mkobject (abfd) == false) | |
1075 | return NULL; | |
1076 | ||
1077 | pe = pe_data (abfd); | |
1078 | ||
1079 | pe->coff.sym_filepos = internal_f->f_symptr; | |
1080 | ||
1081 | /* These members communicate important constants about the symbol | |
1082 | table to GDB's symbol-reading code. These `constants' | |
1083 | unfortunately vary among coff implementations... */ | |
1084 | pe->coff.local_n_btmask = N_BTMASK; | |
1085 | pe->coff.local_n_btshft = N_BTSHFT; | |
1086 | pe->coff.local_n_tmask = N_TMASK; | |
1087 | pe->coff.local_n_tshift = N_TSHIFT; | |
1088 | pe->coff.local_symesz = SYMESZ; | |
1089 | pe->coff.local_auxesz = AUXESZ; | |
1090 | pe->coff.local_linesz = LINESZ; | |
1091 | ||
1092 | obj_raw_syment_count (abfd) = | |
1093 | obj_conv_table_size (abfd) = | |
1094 | internal_f->f_nsyms; | |
1095 | ||
1096 | pe->pe_opthdr = ((struct internal_aouthdr *)aouthdr)->pe; | |
1097 | return (PTR) pe; | |
1098 | } | |
1099 | ||
1100 | ||
1101 | ||
1102 |