Wed Aug 30 20:41:27 1995 steve chamberlain <sac@slash.cygnus.com>
[external/binutils.git] / bfd / coffswap.h
1 /* Generic COFF swapping routines, for BFD.
2    Copyright 1990, 1991, 1992, 1993, 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 /* This file contains routines used to swap COFF data.  It is a header
22    file because the details of swapping depend on the details of the
23    structures used by each COFF implementation.  This is included by
24    coffcode.h, as well as by the ECOFF backend.
25
26    Any file which uses this must first include "coff/internal.h" and
27    "coff/CPU.h".  The functions will then be correct for that CPU.  */
28
29 #ifndef IMAGE_BASE
30 #define IMAGE_BASE 0
31 #endif
32
33 #define PUTWORD bfd_h_put_32
34 #define PUTHALF bfd_h_put_16
35 #define PUTBYTE bfd_h_put_8
36
37 #ifndef GET_FCN_LNNOPTR
38 #define GET_FCN_LNNOPTR(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
39 #endif
40
41 #ifndef GET_FCN_ENDNDX
42 #define GET_FCN_ENDNDX(abfd, ext)  bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
43 #endif
44
45 #ifndef PUT_FCN_LNNOPTR
46 #define PUT_FCN_LNNOPTR(abfd, in, ext)  PUTWORD(abfd,  in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr)
47 #endif
48 #ifndef PUT_FCN_ENDNDX
49 #define PUT_FCN_ENDNDX(abfd, in, ext) PUTWORD(abfd, in, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx)
50 #endif
51 #ifndef GET_LNSZ_LNNO
52 #define GET_LNSZ_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_lnno)
53 #endif
54 #ifndef GET_LNSZ_SIZE
55 #define GET_LNSZ_SIZE(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_misc.x_lnsz.x_size)
56 #endif
57 #ifndef PUT_LNSZ_LNNO
58 #define PUT_LNSZ_LNNO(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno)
59 #endif
60 #ifndef PUT_LNSZ_SIZE
61 #define PUT_LNSZ_SIZE(abfd, in, ext) bfd_h_put_16(abfd, in, (bfd_byte*) ext->x_sym.x_misc.x_lnsz.x_size)
62 #endif
63 #ifndef GET_SCN_SCNLEN
64 #define GET_SCN_SCNLEN(abfd,  ext) bfd_h_get_32(abfd, (bfd_byte *) ext->x_scn.x_scnlen)
65 #endif
66 #ifndef GET_SCN_NRELOC
67 #define GET_SCN_NRELOC(abfd,  ext) bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nreloc)
68 #endif
69 #ifndef GET_SCN_NLINNO
70 #define GET_SCN_NLINNO(abfd, ext)  bfd_h_get_16(abfd, (bfd_byte *)ext->x_scn.x_nlinno)
71 #endif
72 #ifndef PUT_SCN_SCNLEN
73 #define PUT_SCN_SCNLEN(abfd,in, ext) bfd_h_put_32(abfd, in, (bfd_byte *) ext->x_scn.x_scnlen)
74 #endif
75 #ifndef PUT_SCN_NRELOC
76 #define PUT_SCN_NRELOC(abfd,in, ext) bfd_h_put_16(abfd, in, (bfd_byte *)ext->x_scn.x_nreloc)
77 #endif
78 #ifndef PUT_SCN_NLINNO
79 #define PUT_SCN_NLINNO(abfd,in, ext)  bfd_h_put_16(abfd,in, (bfd_byte  *) ext->x_scn.x_nlinno)
80 #endif
81 #ifndef GET_LINENO_LNNO
82 #define GET_LINENO_LNNO(abfd, ext) bfd_h_get_16(abfd, (bfd_byte *) (ext->l_lnno));
83 #endif
84 #ifndef PUT_LINENO_LNNO
85 #define PUT_LINENO_LNNO(abfd,val, ext) bfd_h_put_16(abfd,val,  (bfd_byte *) (ext->l_lnno));
86 #endif
87
88 /* The f_symptr field in the filehdr is sometimes 64 bits.  */
89 #ifndef GET_FILEHDR_SYMPTR
90 #define GET_FILEHDR_SYMPTR bfd_h_get_32
91 #endif
92 #ifndef PUT_FILEHDR_SYMPTR
93 #define PUT_FILEHDR_SYMPTR bfd_h_put_32
94 #endif
95
96 /* Some fields in the aouthdr are sometimes 64 bits.  */
97 #ifndef GET_AOUTHDR_TSIZE
98 #define GET_AOUTHDR_TSIZE bfd_h_get_32
99 #endif
100 #ifndef PUT_AOUTHDR_TSIZE
101 #define PUT_AOUTHDR_TSIZE bfd_h_put_32
102 #endif
103 #ifndef GET_AOUTHDR_DSIZE
104 #define GET_AOUTHDR_DSIZE bfd_h_get_32
105 #endif
106 #ifndef PUT_AOUTHDR_DSIZE
107 #define PUT_AOUTHDR_DSIZE bfd_h_put_32
108 #endif
109 #ifndef GET_AOUTHDR_BSIZE
110 #define GET_AOUTHDR_BSIZE bfd_h_get_32
111 #endif
112 #ifndef PUT_AOUTHDR_BSIZE
113 #define PUT_AOUTHDR_BSIZE bfd_h_put_32
114 #endif
115 #ifndef GET_AOUTHDR_ENTRY
116 #define GET_AOUTHDR_ENTRY bfd_h_get_32
117 #endif
118 #ifndef PUT_AOUTHDR_ENTRY
119 #define PUT_AOUTHDR_ENTRY bfd_h_put_32
120 #endif
121 #ifndef GET_AOUTHDR_TEXT_START
122 #define GET_AOUTHDR_TEXT_START bfd_h_get_32
123 #endif
124 #ifndef PUT_AOUTHDR_TEXT_START
125 #define PUT_AOUTHDR_TEXT_START bfd_h_put_32
126 #endif
127 #ifndef GET_AOUTHDR_DATA_START
128 #define GET_AOUTHDR_DATA_START bfd_h_get_32
129 #endif
130 #ifndef PUT_AOUTHDR_DATA_START
131 #define PUT_AOUTHDR_DATA_START bfd_h_put_32
132 #endif
133
134 /* Some fields in the scnhdr are sometimes 64 bits.  */
135 #ifndef GET_SCNHDR_PADDR
136 #define GET_SCNHDR_PADDR bfd_h_get_32
137 #endif
138 #ifndef PUT_SCNHDR_PADDR
139 #define PUT_SCNHDR_PADDR bfd_h_put_32
140 #endif
141 #ifndef GET_SCNHDR_VADDR
142 #define GET_SCNHDR_VADDR bfd_h_get_32
143 #endif
144 #ifndef PUT_SCNHDR_VADDR
145 #define PUT_SCNHDR_VADDR bfd_h_put_32
146 #endif
147 #ifndef GET_SCNHDR_SIZE
148 #define GET_SCNHDR_SIZE bfd_h_get_32
149 #endif
150 #ifndef PUT_SCNHDR_SIZE
151 #define PUT_SCNHDR_SIZE bfd_h_put_32
152 #endif
153 #ifndef GET_SCNHDR_SCNPTR
154 #define GET_SCNHDR_SCNPTR bfd_h_get_32
155 #endif
156 #ifndef PUT_SCNHDR_SCNPTR
157 #define PUT_SCNHDR_SCNPTR bfd_h_put_32
158 #endif
159 #ifndef GET_SCNHDR_RELPTR
160 #define GET_SCNHDR_RELPTR bfd_h_get_32
161 #endif
162 #ifndef PUT_SCNHDR_RELPTR
163 #define PUT_SCNHDR_RELPTR bfd_h_put_32
164 #endif
165 #ifndef GET_SCNHDR_LNNOPTR
166 #define GET_SCNHDR_LNNOPTR bfd_h_get_32
167 #endif
168 #ifndef PUT_SCNHDR_LNNOPTR
169 #define PUT_SCNHDR_LNNOPTR bfd_h_put_32
170 #endif
171
172 #ifndef NO_COFF_RELOCS
173
174 static void
175 coff_swap_reloc_in (abfd, src, dst)
176      bfd *abfd;
177      PTR src;
178      PTR dst;
179 {
180   RELOC *reloc_src = (RELOC *) src;
181   struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
182
183   reloc_dst->r_vaddr = bfd_h_get_32(abfd, (bfd_byte *)reloc_src->r_vaddr);
184   reloc_dst->r_symndx = bfd_h_get_signed_32(abfd, (bfd_byte *) reloc_src->r_symndx);
185
186 #ifdef RS6000COFF_C
187   reloc_dst->r_type = bfd_h_get_8(abfd, reloc_src->r_type);
188   reloc_dst->r_size = bfd_h_get_8(abfd, reloc_src->r_size);
189 #else
190   reloc_dst->r_type = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_type);
191 #endif
192
193 #ifdef SWAP_IN_RELOC_OFFSET
194   reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET(abfd,
195                                              (bfd_byte *) reloc_src->r_offset);
196 #endif
197 }
198
199
200 static unsigned int
201 coff_swap_reloc_out (abfd, src, dst)
202      bfd       *abfd;
203      PTR        src;
204      PTR        dst;
205 {
206   struct internal_reloc *reloc_src = (struct internal_reloc *)src;
207   struct external_reloc *reloc_dst = (struct external_reloc *)dst;
208   bfd_h_put_32(abfd, reloc_src->r_vaddr, (bfd_byte *) reloc_dst->r_vaddr);
209   bfd_h_put_32(abfd, reloc_src->r_symndx, (bfd_byte *) reloc_dst->r_symndx);
210
211 #ifdef RS6000COFF_C
212   bfd_h_put_8 (abfd, reloc_src->r_type, (bfd_byte *) reloc_dst->r_type);
213   bfd_h_put_8 (abfd, reloc_src->r_size, (bfd_byte *) reloc_dst->r_size);
214 #else
215   bfd_h_put_16(abfd, reloc_src->r_type, (bfd_byte *)
216                reloc_dst->r_type);
217 #endif
218
219 #ifdef SWAP_OUT_RELOC_OFFSET
220   SWAP_OUT_RELOC_OFFSET(abfd,
221                         reloc_src->r_offset,
222                         (bfd_byte *) reloc_dst->r_offset);
223 #endif
224 #ifdef SWAP_OUT_RELOC_EXTRA
225   SWAP_OUT_RELOC_EXTRA(abfd,reloc_src, reloc_dst);
226 #endif
227
228   return sizeof(struct external_reloc);
229 }
230
231 #endif /* NO_COFF_RELOCS */
232
233 static void
234 coff_swap_filehdr_in (abfd, src, dst)
235      bfd            *abfd;
236      PTR             src;
237      PTR             dst;
238 {
239   FILHDR *filehdr_src = (FILHDR *) src;
240   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
241   filehdr_dst->f_magic = bfd_h_get_16(abfd, (bfd_byte *) filehdr_src->f_magic);
242   filehdr_dst->f_nscns = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_nscns);
243   filehdr_dst->f_timdat = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_timdat);
244   filehdr_dst->f_symptr =
245     GET_FILEHDR_SYMPTR (abfd, (bfd_byte *) filehdr_src->f_symptr);
246   filehdr_dst->f_nsyms = bfd_h_get_32(abfd, (bfd_byte *)filehdr_src-> f_nsyms);
247   filehdr_dst->f_opthdr = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_opthdr);
248   filehdr_dst->f_flags = bfd_h_get_16(abfd, (bfd_byte *)filehdr_src-> f_flags);
249 }
250
251 static  unsigned int
252 coff_swap_filehdr_out (abfd, in, out)
253      bfd       *abfd;
254      PTR        in;
255      PTR        out;
256 {
257   struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
258   FILHDR *filehdr_out = (FILHDR *)out;
259
260   bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
261   bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
262   bfd_h_put_32(abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);
263   PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,
264                       (bfd_byte *) filehdr_out->f_symptr);
265   bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
266   bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
267   bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
268
269 #ifdef COFF_IMAGE_WITH_PE
270   /* put in extra dos header stuff.  This data remains essentially
271      constant, it just has to be tacked on to the beginning of all exes 
272      for NT */
273   bfd_h_put_16(abfd, filehdr_in->pe->e_magic, (bfd_byte *) filehdr_out->e_magic);
274   bfd_h_put_16(abfd, filehdr_in->pe->e_cblp, (bfd_byte *) filehdr_out->e_cblp);
275   bfd_h_put_16(abfd, filehdr_in->pe->e_cp, (bfd_byte *) filehdr_out->e_cp);
276   bfd_h_put_16(abfd, filehdr_in->pe->e_crlc, (bfd_byte *) filehdr_out->e_crlc);
277   bfd_h_put_16(abfd, filehdr_in->pe->e_cparhdr, 
278                      (bfd_byte *) filehdr_out->e_cparhdr);
279   bfd_h_put_16(abfd, filehdr_in->pe->e_minalloc, 
280                      (bfd_byte *) filehdr_out->e_minalloc);
281   bfd_h_put_16(abfd, filehdr_in->pe->e_maxalloc, 
282                      (bfd_byte *) filehdr_out->e_maxalloc);
283   bfd_h_put_16(abfd, filehdr_in->pe->e_ss, (bfd_byte *) filehdr_out->e_ss);
284   bfd_h_put_16(abfd, filehdr_in->pe->e_sp, (bfd_byte *) filehdr_out->e_sp);
285   bfd_h_put_16(abfd, filehdr_in->pe->e_csum, (bfd_byte *) filehdr_out->e_csum);
286   bfd_h_put_16(abfd, filehdr_in->pe->e_ip, (bfd_byte *) filehdr_out->e_ip);
287   bfd_h_put_16(abfd, filehdr_in->pe->e_cs, (bfd_byte *) filehdr_out->e_cs);
288   bfd_h_put_16(abfd, filehdr_in->pe->e_lfarlc, (bfd_byte *) filehdr_out->e_lfarlc);
289   bfd_h_put_16(abfd, filehdr_in->pe->e_ovno, (bfd_byte *) filehdr_out->e_ovno);
290   {
291     int idx;
292     for (idx=0; idx < 4; idx++)
293       bfd_h_put_16(abfd, filehdr_in->pe->e_res[idx], 
294                          (bfd_byte *) filehdr_out->e_res[idx]);
295   }
296   bfd_h_put_16(abfd, filehdr_in->pe->e_oemid, (bfd_byte *) filehdr_out->e_oemid);
297   bfd_h_put_16(abfd, filehdr_in->pe->e_oeminfo,
298                      (bfd_byte *) filehdr_out->e_oeminfo);
299   {
300     int idx;
301     for (idx=0; idx < 10; idx++)
302       bfd_h_put_16(abfd, filehdr_in->pe->e_res2[idx],
303                          (bfd_byte *) filehdr_out->e_res2[idx]);
304   }
305   bfd_h_put_32(abfd, filehdr_in->pe->e_lfanew, (bfd_byte *) filehdr_out->e_lfanew);
306
307   {
308     int idx;
309     for (idx=0; idx < 16; idx++)
310       bfd_h_put_32(abfd, filehdr_in->pe->dos_message[idx],
311                          (bfd_byte *) filehdr_out->dos_message[idx]);
312   }
313
314   /* also put in the NT signature */
315   bfd_h_put_32(abfd, filehdr_in->pe->nt_signature, 
316                      (bfd_byte *) filehdr_out->nt_signature);
317
318
319 #endif
320
321
322
323   return sizeof(FILHDR);
324 }
325
326
327 #ifndef NO_COFF_SYMBOLS
328
329 static void
330 coff_swap_sym_in (abfd, ext1, in1)
331      bfd            *abfd;
332      PTR ext1;
333      PTR in1;
334 {
335   SYMENT *ext = (SYMENT *)ext1;
336   struct internal_syment      *in = (struct internal_syment *)in1;
337
338   if( ext->e.e_name[0] == 0) {
339     in->_n._n_n._n_zeroes = 0;
340     in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
341   }
342   else {
343 #if SYMNMLEN != E_SYMNMLEN
344    -> Error, we need to cope with truncating or extending SYMNMLEN!;
345 #else
346     memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
347 #endif
348   }
349   in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value); 
350   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
351   if (sizeof(ext->e_type) == 2){
352     in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
353   }
354   else {
355     in->n_type = bfd_h_get_32(abfd, (bfd_byte *) ext->e_type);
356   }
357   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
358   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
359
360 #ifdef COFF_WITH_PE
361   /* The section symbols for the .idata$ sections have class 68, which MS
362      documentation indicates is a section symbol.  The problem is that the
363      value field in the symbol is simply a copy of the .idata section's flags
364      rather than something useful.  When these symbols are encountered, change
365      the value to 0 and the section number to 1 so that they will be handled
366      somewhat correctly in the bfd code. */
367   if (in->n_sclass == 0x68) {
368     in->n_value = 0x0;
369     in->n_scnum = 1;
370     /* I have tried setting the class to 3 and using the following to set
371        the section number.  This will put the address of the pointer to the
372        string kernel32.dll at addresses 0 and 0x10 off start of idata section
373        which is not correct */
374 /*    if (strcmp (in->_n._n_name, ".idata$4") == 0) */
375 /*      in->n_scnum = 3; */
376 /*    else */
377 /*      in->n_scnum = 2; */
378     }
379 #endif
380 }
381
382 static unsigned int
383 coff_swap_sym_out (abfd, inp, extp)
384      bfd       *abfd;
385      PTR        inp;
386      PTR        extp;
387 {
388   struct internal_syment *in = (struct internal_syment *)inp;
389   SYMENT *ext =(SYMENT *)extp;
390   if(in->_n._n_name[0] == 0) {
391     bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
392     bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *)  ext->e.e.e_offset);
393   }
394   else {
395 #if SYMNMLEN != E_SYMNMLEN
396     -> Error, we need to cope with truncating or extending SYMNMLEN!;
397 #else
398     memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
399 #endif
400   }
401   bfd_h_put_32(abfd,  in->n_value , (bfd_byte *) ext->e_value);
402   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
403   if (sizeof(ext->e_type) == 2)
404       {
405         bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
406       }
407   else
408       {
409         bfd_h_put_32(abfd,  in->n_type , (bfd_byte *) ext->e_type);
410       }
411   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
412   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
413   return sizeof(SYMENT);
414 }
415
416 static void
417 coff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
418      bfd            *abfd;
419      PTR              ext1;
420      int             type;
421      int             class;
422      int              indx;
423      int              numaux;
424      PTR              in1;
425 {
426   AUXENT    *ext = (AUXENT *)ext1;
427   union internal_auxent *in = (union internal_auxent *)in1;
428
429   switch (class) {
430     case C_FILE:
431       if (ext->x_file.x_fname[0] == 0) {
432           in->x_file.x_n.x_zeroes = 0;
433           in->x_file.x_n.x_offset = 
434            bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
435         } else {
436 #if FILNMLEN != E_FILNMLEN
437             -> Error, we need to cope with truncating or extending FILNMLEN!;
438 #else
439             memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
440 #endif
441           }
442       return;
443
444       /* RS/6000 "csect" auxents */
445 #ifdef RS6000COFF_C
446     case C_EXT:
447     case C_HIDEXT:
448       if (indx + 1 == numaux)
449         {
450           in->x_csect.x_scnlen.l = bfd_h_get_32 (abfd, ext->x_csect.x_scnlen);
451           in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
452                                                  ext->x_csect.x_parmhash);
453           in->x_csect.x_snhash   = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
454           /* We don't have to hack bitfields in x_smtyp because it's
455              defined by shifts-and-ands, which are equivalent on all
456              byte orders.  */
457           in->x_csect.x_smtyp    = bfd_h_get_8  (abfd, ext->x_csect.x_smtyp);
458           in->x_csect.x_smclas   = bfd_h_get_8  (abfd, ext->x_csect.x_smclas);
459           in->x_csect.x_stab     = bfd_h_get_32 (abfd, ext->x_csect.x_stab);
460           in->x_csect.x_snstab   = bfd_h_get_16 (abfd, ext->x_csect.x_snstab);
461           return;
462         }
463       break;
464 #endif
465
466     case C_STAT:
467 #ifdef C_LEAFSTAT
468     case C_LEAFSTAT:
469 #endif
470     case C_HIDDEN:
471       if (type == T_NULL) {
472           in->x_scn.x_scnlen = GET_SCN_SCNLEN(abfd, ext);
473           in->x_scn.x_nreloc = GET_SCN_NRELOC(abfd, ext);
474           in->x_scn.x_nlinno = GET_SCN_NLINNO(abfd, ext);
475           return;
476         }
477       break;
478     }
479
480   in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
481 #ifndef NO_TVNDX
482   in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
483 #endif
484
485   if (class == C_BLOCK || ISFCN (type) || ISTAG (class))
486     {
487       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
488       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
489     }
490   else
491     {
492 #if DIMNUM != E_DIMNUM
493  #error we need to cope with truncating or extending DIMNUM
494 #endif
495       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
496         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
497       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
498         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
499       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
500         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
501       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
502         bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
503     }
504
505   if (ISFCN(type)) {
506     in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
507   }
508   else {
509     in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO(abfd, ext);
510     in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE(abfd, ext);
511   }
512 }
513
514 static unsigned int
515 coff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
516      bfd   *abfd;
517      PTR        inp;
518      int   type;
519      int   class;
520      int   indx;
521      int   numaux;
522      PTR        extp;
523 {
524   union internal_auxent *in = (union internal_auxent *)inp;
525   AUXENT *ext = (AUXENT *)extp;
526
527   memset((PTR)ext, 0, AUXESZ);
528   switch (class) {
529   case C_FILE:
530     if (in->x_file.x_fname[0] == 0) {
531       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
532       PUTWORD(abfd,
533               in->x_file.x_n.x_offset,
534               (bfd_byte *) ext->x_file.x_n.x_offset);
535     }
536     else {
537 #if FILNMLEN != E_FILNMLEN
538       -> Error, we need to cope with truncating or extending FILNMLEN!;
539 #else
540       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
541 #endif
542     }
543     return sizeof (AUXENT);
544
545 #ifdef RS6000COFF_C
546   /* RS/6000 "csect" auxents */
547   case C_EXT:
548   case C_HIDEXT:
549     if (indx + 1 == numaux)
550       {
551         PUTWORD (abfd, in->x_csect.x_scnlen.l,  ext->x_csect.x_scnlen);
552         PUTWORD (abfd, in->x_csect.x_parmhash,  ext->x_csect.x_parmhash);
553         PUTHALF (abfd, in->x_csect.x_snhash,    ext->x_csect.x_snhash);
554         /* We don't have to hack bitfields in x_smtyp because it's
555            defined by shifts-and-ands, which are equivalent on all
556            byte orders.  */
557         PUTBYTE (abfd, in->x_csect.x_smtyp,     ext->x_csect.x_smtyp);
558         PUTBYTE (abfd, in->x_csect.x_smclas,    ext->x_csect.x_smclas);
559         PUTWORD (abfd, in->x_csect.x_stab,      ext->x_csect.x_stab);
560         PUTHALF (abfd, in->x_csect.x_snstab,    ext->x_csect.x_snstab);
561         return sizeof (AUXENT);
562       }
563     break;
564 #endif
565
566   case C_STAT:
567 #ifdef C_LEAFSTAT
568   case C_LEAFSTAT:
569 #endif
570   case C_HIDDEN:
571     if (type == T_NULL) {
572       PUT_SCN_SCNLEN(abfd, in->x_scn.x_scnlen, ext);
573       PUT_SCN_NRELOC(abfd, in->x_scn.x_nreloc, ext);
574       PUT_SCN_NLINNO(abfd, in->x_scn.x_nlinno, ext);
575       return sizeof (AUXENT);
576     }
577     break;
578   }
579
580   PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
581 #ifndef NO_TVNDX
582   bfd_h_put_16(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
583 #endif
584
585   if (class == C_BLOCK || ISFCN (type) || ISTAG (class))
586     {
587       PUT_FCN_LNNOPTR(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
588       PUT_FCN_ENDNDX(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
589     }
590   else
591     {
592 #if DIMNUM != E_DIMNUM
593  #error we need to cope with truncating or extending DIMNUM
594 #endif
595       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
596                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
597       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
598                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
599       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
600                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
601       bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
602                     (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
603     }
604
605   if (ISFCN (type))
606     PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
607              (bfd_byte *)  ext->x_sym.x_misc.x_fsize);
608   else
609     {
610       PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
611       PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
612     }
613
614   return sizeof(AUXENT);
615 }
616
617 #endif /* NO_COFF_SYMBOLS */
618
619 #ifndef NO_COFF_LINENOS
620
621 static void
622 coff_swap_lineno_in (abfd, ext1, in1)
623      bfd            *abfd;
624      PTR ext1;
625      PTR in1;
626 {
627   LINENO *ext = (LINENO *)ext1;
628   struct internal_lineno      *in = (struct internal_lineno *)in1;
629
630   in->l_addr.l_symndx = bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx);
631   in->l_lnno = GET_LINENO_LNNO(abfd, ext);
632 }
633
634 static unsigned int
635 coff_swap_lineno_out (abfd, inp, outp)
636      bfd       *abfd;
637      PTR        inp;
638      PTR        outp;
639 {
640   struct internal_lineno *in = (struct internal_lineno *)inp;
641   struct external_lineno *ext = (struct external_lineno *)outp;
642   PUTWORD(abfd, in->l_addr.l_symndx, (bfd_byte *)
643           ext->l_addr.l_symndx);
644
645   PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
646   return sizeof(struct external_lineno);
647 }
648
649 #endif /* NO_COFF_LINENOS */
650
651
652 static void
653 coff_swap_aouthdr_in (abfd, aouthdr_ext1, aouthdr_int1)
654      bfd            *abfd;
655      PTR aouthdr_ext1;
656      PTR aouthdr_int1;
657 {
658   AOUTHDR        *aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
659   struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1;
660
661   aouthdr_int->magic = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->magic);
662   aouthdr_int->vstamp = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->vstamp);
663   aouthdr_int->tsize =
664     GET_AOUTHDR_TSIZE (abfd, (bfd_byte *) aouthdr_ext->tsize);
665   aouthdr_int->dsize =
666     GET_AOUTHDR_DSIZE (abfd, (bfd_byte *) aouthdr_ext->dsize);
667   aouthdr_int->bsize =
668     GET_AOUTHDR_BSIZE (abfd, (bfd_byte *) aouthdr_ext->bsize);
669   aouthdr_int->entry =
670     GET_AOUTHDR_ENTRY (abfd, (bfd_byte *) aouthdr_ext->entry);
671   aouthdr_int->text_start =
672     GET_AOUTHDR_TEXT_START (abfd, (bfd_byte *) aouthdr_ext->text_start);
673   aouthdr_int->data_start =
674     GET_AOUTHDR_DATA_START (abfd, (bfd_byte *) aouthdr_ext->data_start);
675
676 #ifdef I960
677   aouthdr_int->tagentries = bfd_h_get_32(abfd, (bfd_byte *) aouthdr_ext->tagentries);
678 #endif
679
680 #ifdef APOLLO_M68
681   bfd_h_put_32(abfd, aouthdr_int->o_inlib, (bfd_byte *) aouthdr_ext->o_inlib);
682   bfd_h_put_32(abfd, aouthdr_int->o_sri, (bfd_byte *) aouthdr_ext->o_sri);
683   bfd_h_put_32(abfd, aouthdr_int->vid[0], (bfd_byte *) aouthdr_ext->vid);
684   bfd_h_put_32(abfd, aouthdr_int->vid[1], (bfd_byte *) aouthdr_ext->vid + 4);
685 #endif
686
687
688 #ifdef RS6000COFF_C
689   aouthdr_int->o_toc = bfd_h_get_32(abfd, aouthdr_ext->o_toc);
690   aouthdr_int->o_snentry = bfd_h_get_16(abfd, aouthdr_ext->o_snentry);
691   aouthdr_int->o_sntext = bfd_h_get_16(abfd, aouthdr_ext->o_sntext);
692   aouthdr_int->o_sndata = bfd_h_get_16(abfd, aouthdr_ext->o_sndata);
693   aouthdr_int->o_sntoc = bfd_h_get_16(abfd, aouthdr_ext->o_sntoc);
694   aouthdr_int->o_snloader = bfd_h_get_16(abfd, aouthdr_ext->o_snloader);
695   aouthdr_int->o_snbss = bfd_h_get_16(abfd, aouthdr_ext->o_snbss);
696   aouthdr_int->o_algntext = bfd_h_get_16(abfd, aouthdr_ext->o_algntext);
697   aouthdr_int->o_algndata = bfd_h_get_16(abfd, aouthdr_ext->o_algndata);
698   aouthdr_int->o_modtype = bfd_h_get_16(abfd, aouthdr_ext->o_modtype);
699   aouthdr_int->o_maxstack = bfd_h_get_32(abfd, aouthdr_ext->o_maxstack);
700 #endif
701
702 #ifdef MIPSECOFF
703   aouthdr_int->bss_start = bfd_h_get_32(abfd, aouthdr_ext->bss_start);
704   aouthdr_int->gp_value = bfd_h_get_32(abfd, aouthdr_ext->gp_value);
705   aouthdr_int->gprmask = bfd_h_get_32(abfd, aouthdr_ext->gprmask);
706   aouthdr_int->cprmask[0] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[0]);
707   aouthdr_int->cprmask[1] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[1]);
708   aouthdr_int->cprmask[2] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[2]);
709   aouthdr_int->cprmask[3] = bfd_h_get_32(abfd, aouthdr_ext->cprmask[3]);
710 #endif
711
712 #ifdef ALPHAECOFF
713   aouthdr_int->bss_start = bfd_h_get_64(abfd, aouthdr_ext->bss_start);
714   aouthdr_int->gp_value = bfd_h_get_64(abfd, aouthdr_ext->gp_value);
715   aouthdr_int->gprmask = bfd_h_get_32(abfd, aouthdr_ext->gprmask);
716   aouthdr_int->fprmask = bfd_h_get_32(abfd, aouthdr_ext->fprmask);
717 #endif
718
719 #ifdef COFF_IMAGE_WITH_PE
720
721   {
722     struct internal_extra_pe_aouthdr *a;
723     PEAOUTHDR *src = (PEAOUTHDR *)(aouthdr_ext);
724     a = aouthdr_int->pe = bfd_alloc (abfd, sizeof (*a));
725
726     a->ImageBase = bfd_h_get_32 (abfd, src->ImageBase);
727     a->SectionAlignment = bfd_h_get_32 (abfd, src->SectionAlignment);
728     a->FileAlignment = bfd_h_get_32 (abfd, src->FileAlignment);
729     a->MajorOperatingSystemVersion = 
730       bfd_h_get_16 (abfd, src->MajorOperatingSystemVersion);
731     a->MinorOperatingSystemVersion = 
732       bfd_h_get_16 (abfd, src->MinorOperatingSystemVersion);
733     a->MajorImageVersion = bfd_h_get_16 (abfd, src->MajorImageVersion);
734     a->MinorImageVersion = bfd_h_get_16 (abfd, src->MinorImageVersion);
735     a->MajorSubsystemVersion = bfd_h_get_16 (abfd, src->MajorSubsystemVersion);
736     a->MinorSubsystemVersion = bfd_h_get_16 (abfd, src->MinorSubsystemVersion);
737     a->Reserved1 = bfd_h_get_32 (abfd, src->Reserved1);
738     a->SizeOfImage = bfd_h_get_32 (abfd, src->SizeOfImage);
739     a->SizeOfHeaders = bfd_h_get_32 (abfd, src->SizeOfHeaders);
740     a->CheckSum = bfd_h_get_32 (abfd, src->CheckSum);
741     a->Subsystem = bfd_h_get_16 (abfd, src->Subsystem);
742     a->DllCharacteristics = bfd_h_get_16 (abfd, src->DllCharacteristics);
743     a->SizeOfStackReserve = bfd_h_get_32 (abfd, src->SizeOfStackReserve);
744     a->SizeOfStackCommit = bfd_h_get_32 (abfd, src->SizeOfStackCommit);
745     a->SizeOfHeapReserve = bfd_h_get_32 (abfd, src->SizeOfHeapReserve);
746     a->SizeOfHeapCommit = bfd_h_get_32 (abfd, src->SizeOfHeapCommit);
747     a->LoaderFlags = bfd_h_get_32 (abfd, src->LoaderFlags);
748     a->NumberOfRvaAndSizes = bfd_h_get_32 (abfd, src->NumberOfRvaAndSizes);
749
750     {
751       int idx;
752       for (idx=0; idx < 16; idx++)
753         {
754           a->DataDirectory[idx].VirtualAddress =
755             bfd_h_get_32 (abfd, src->DataDirectory[idx][0]);
756           a->DataDirectory[idx].Size =
757             bfd_h_get_32 (abfd, src->DataDirectory[idx][1]);
758         }
759     }
760   }
761 #endif
762
763 }
764
765 static unsigned int
766 coff_swap_aouthdr_out (abfd, in, out)
767      bfd       *abfd;
768      PTR        in;
769      PTR        out;
770 {
771   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in;
772   AOUTHDR *aouthdr_out = (AOUTHDR *)out;
773
774   bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->magic);
775   bfd_h_put_16(abfd, aouthdr_in->vstamp, (bfd_byte *) aouthdr_out->vstamp);
776   PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->tsize);
777   PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->dsize);
778   PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->bsize);
779   PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->entry);
780   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
781                           (bfd_byte *) aouthdr_out->text_start);
782   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
783                           (bfd_byte *) aouthdr_out->data_start);
784 #ifdef COFF_IMAGE_WITH_PE
785   {
786   PEAOUTHDR *peaouthdr_out = (PEAOUTHDR *)aouthdr_out;
787   bfd_h_put_32 (abfd, aouthdr_in->pe->ImageBase, 
788                 (bfd_byte *) peaouthdr_out->ImageBase);
789   bfd_h_put_32 (abfd, aouthdr_in->pe->SectionAlignment,
790                 (bfd_byte *) peaouthdr_out->SectionAlignment);
791   bfd_h_put_32 (abfd, aouthdr_in->pe->FileAlignment,
792                 (bfd_byte *) peaouthdr_out->FileAlignment);
793   bfd_h_put_16 (abfd, aouthdr_in->pe->MajorOperatingSystemVersion,
794                 (bfd_byte *) peaouthdr_out->MajorOperatingSystemVersion);
795   bfd_h_put_16 (abfd, aouthdr_in->pe->MinorOperatingSystemVersion,
796                 (bfd_byte *) peaouthdr_out->MinorOperatingSystemVersion);
797   bfd_h_put_16 (abfd, aouthdr_in->pe->MajorImageVersion,
798                 (bfd_byte *) peaouthdr_out->MajorImageVersion);
799   bfd_h_put_16 (abfd, aouthdr_in->pe->MinorImageVersion,
800                 (bfd_byte *) peaouthdr_out->MinorImageVersion);
801   bfd_h_put_16 (abfd, aouthdr_in->pe->MajorSubsystemVersion,
802                 (bfd_byte *) peaouthdr_out->MajorSubsystemVersion);
803   bfd_h_put_16 (abfd, aouthdr_in->pe->MinorSubsystemVersion,
804                 (bfd_byte *) peaouthdr_out->MinorSubsystemVersion);
805   bfd_h_put_32 (abfd, aouthdr_in->pe->Reserved1,
806                 (bfd_byte *) peaouthdr_out->Reserved1);
807   bfd_h_put_32 (abfd, aouthdr_in->pe->SizeOfImage,
808                 (bfd_byte *) peaouthdr_out->SizeOfImage);
809   bfd_h_put_32 (abfd, aouthdr_in->pe->SizeOfHeaders,
810                 (bfd_byte *) peaouthdr_out->SizeOfHeaders);
811   bfd_h_put_32 (abfd, aouthdr_in->pe->CheckSum,
812                 (bfd_byte *) peaouthdr_out->CheckSum);
813   bfd_h_put_16 (abfd, aouthdr_in->pe->Subsystem,
814                 (bfd_byte *) peaouthdr_out->Subsystem);
815   bfd_h_put_16 (abfd, aouthdr_in->pe->DllCharacteristics,
816                 (bfd_byte *) peaouthdr_out->DllCharacteristics);
817   bfd_h_put_32 (abfd, aouthdr_in->pe->SizeOfStackReserve,
818                 (bfd_byte *) peaouthdr_out->SizeOfStackReserve);
819   bfd_h_put_32 (abfd, aouthdr_in->pe->SizeOfStackCommit,
820                 (bfd_byte *) peaouthdr_out->SizeOfStackCommit);
821   bfd_h_put_32 (abfd, aouthdr_in->pe->SizeOfHeapReserve,
822                 (bfd_byte *) peaouthdr_out->SizeOfHeapReserve);
823   bfd_h_put_32 (abfd, aouthdr_in->pe->SizeOfHeapCommit,
824                 (bfd_byte *) peaouthdr_out->SizeOfHeapCommit);
825   bfd_h_put_32 (abfd, aouthdr_in->pe->LoaderFlags,
826                 (bfd_byte *) peaouthdr_out->LoaderFlags);
827   bfd_h_put_32 (abfd, aouthdr_in->pe->NumberOfRvaAndSizes,
828                 (bfd_byte *) peaouthdr_out->NumberOfRvaAndSizes);
829   {
830     int idx;
831     for (idx=0; idx < 16; idx++)
832     {
833       bfd_h_put_32 (abfd, aouthdr_in->pe->DataDirectory[idx].VirtualAddress,
834                (bfd_byte *) peaouthdr_out->DataDirectory[idx][0]);
835       bfd_h_put_32 (abfd, aouthdr_in->pe->DataDirectory[idx].Size,
836                 (bfd_byte *) peaouthdr_out->DataDirectory[idx][1]);
837     }
838   }
839 }
840 #endif
841
842 #ifdef I960
843   bfd_h_put_32(abfd, aouthdr_in->tagentries, (bfd_byte *) aouthdr_out->tagentries);
844 #endif
845
846 #ifdef MIPSECOFF
847   bfd_h_put_32(abfd, aouthdr_in->bss_start, (bfd_byte *) aouthdr_out->bss_start);
848   bfd_h_put_32(abfd, aouthdr_in->gp_value, (bfd_byte *) aouthdr_out->gp_value);
849   bfd_h_put_32(abfd, aouthdr_in->gprmask, (bfd_byte *) aouthdr_out->gprmask);
850   bfd_h_put_32(abfd, aouthdr_in->cprmask[0], (bfd_byte *) aouthdr_out->cprmask[0]);
851   bfd_h_put_32(abfd, aouthdr_in->cprmask[1], (bfd_byte *) aouthdr_out->cprmask[1]);
852   bfd_h_put_32(abfd, aouthdr_in->cprmask[2], (bfd_byte *) aouthdr_out->cprmask[2]);
853   bfd_h_put_32(abfd, aouthdr_in->cprmask[3], (bfd_byte *) aouthdr_out->cprmask[3]);
854 #endif
855
856 #ifdef ALPHAECOFF
857   /* FIXME: What does bldrev mean?  */
858   bfd_h_put_16(abfd, (bfd_vma) 2, (bfd_byte *) aouthdr_out->bldrev);
859   bfd_h_put_16(abfd, (bfd_vma) 0, (bfd_byte *) aouthdr_out->padding);
860   bfd_h_put_64(abfd, aouthdr_in->bss_start, (bfd_byte *) aouthdr_out->bss_start);
861   bfd_h_put_64(abfd, aouthdr_in->gp_value, (bfd_byte *) aouthdr_out->gp_value);
862   bfd_h_put_32(abfd, aouthdr_in->gprmask, (bfd_byte *) aouthdr_out->gprmask);
863   bfd_h_put_32(abfd, aouthdr_in->fprmask, (bfd_byte *) aouthdr_out->fprmask);
864 #endif
865
866   return sizeof(AOUTHDR);
867 }
868
869 static void
870 coff_swap_scnhdr_in (abfd, ext, in)
871      bfd            *abfd;
872      PTR             ext;
873      PTR             in;
874 {
875   SCNHDR *scnhdr_ext = (SCNHDR *) ext;
876   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
877
878   memcpy(scnhdr_int->s_name, scnhdr_ext->s_name, sizeof(scnhdr_int->s_name));
879   scnhdr_int->s_vaddr =
880     GET_SCNHDR_VADDR (abfd, (bfd_byte *) scnhdr_ext->s_vaddr);
881   scnhdr_int->s_paddr =
882     GET_SCNHDR_PADDR (abfd, (bfd_byte *) scnhdr_ext->s_paddr);
883   scnhdr_int->s_size =
884     GET_SCNHDR_SIZE (abfd, (bfd_byte *) scnhdr_ext->s_size);
885
886   scnhdr_int->s_scnptr =
887     GET_SCNHDR_SCNPTR (abfd, (bfd_byte *) scnhdr_ext->s_scnptr);
888   scnhdr_int->s_relptr =
889     GET_SCNHDR_RELPTR (abfd, (bfd_byte *) scnhdr_ext->s_relptr);
890   scnhdr_int->s_lnnoptr =
891     GET_SCNHDR_LNNOPTR (abfd, (bfd_byte *) scnhdr_ext->s_lnnoptr);
892   scnhdr_int->s_flags = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_flags);
893 #if defined(M88)
894   scnhdr_int->s_nreloc = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
895   scnhdr_int->s_nlnno = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
896 #else
897   scnhdr_int->s_nreloc = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nreloc);
898   scnhdr_int->s_nlnno = bfd_h_get_16(abfd, (bfd_byte *) scnhdr_ext->s_nlnno);
899 #endif
900 #ifdef I960
901   scnhdr_int->s_align = bfd_h_get_32(abfd, (bfd_byte *) scnhdr_ext->s_align);
902 #endif
903 #ifdef COFF_IMAGE_WITH_PE
904 #ifdef NT_EXE_IMAGE_BASE
905   if (scnhdr_int->s_vaddr != 0) {
906     scnhdr_int->s_vaddr += NT_EXE_IMAGE_BASE;
907   }
908 #endif
909 #endif
910 }
911
912 static unsigned int
913 coff_swap_scnhdr_out (abfd, in, out)
914      bfd       *abfd;
915      PTR        in;
916      PTR        out;
917 {
918   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in;
919   SCNHDR *scnhdr_ext = (SCNHDR *)out;
920   unsigned int ret = sizeof (SCNHDR);
921
922   memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name));
923
924 #ifdef COFF_IMAGE_WITH_PE
925
926   {
927   bfd_link_pe_info *pe_info = coff_data (abfd)->link_info->pe_info;
928
929   PUT_SCNHDR_VADDR (abfd, 
930                     (scnhdr_int->s_vaddr 
931                      - pe_value (&pe_info->image_base,
932                                  NT_EXE_IMAGE_BASE)),
933                     (bfd_byte *) scnhdr_ext->s_vaddr);
934
935
936   /* NT wants the physical address data to be the size (s_size data) of
937      the section */
938 #if 1
939   PUT_SCNHDR_PADDR (abfd, scnhdr_int->s_size,
940                     (bfd_byte *) scnhdr_ext->s_paddr);
941 #endif
942   /* NT wants the size data to be rounded up to the next NT_FILE_ALIGNMENT
943      value except for the BSS section, its s_size should be 0 */
944   if (strcmp (scnhdr_int->s_name, _BSS) == 0)
945     PUT_SCNHDR_SIZE (abfd, 0, (bfd_byte *) scnhdr_ext->s_size);
946   else
947     {
948       bfd_vma rounded_size;
949       rounded_size = ((scnhdr_int->s_size + NT_FILE_ALIGNMENT - 1) / 
950                       NT_FILE_ALIGNMENT) *
951                         NT_FILE_ALIGNMENT;
952       PUT_SCNHDR_SIZE (abfd, rounded_size, (bfd_byte *) scnhdr_ext->s_size);
953     }
954 }
955 #else
956   PUT_SCNHDR_VADDR (abfd, scnhdr_int->s_vaddr,
957                     (bfd_byte *) scnhdr_ext->s_vaddr);
958
959
960   PUT_SCNHDR_PADDR (abfd, scnhdr_int->s_paddr,
961                     (bfd_byte *) scnhdr_ext->s_paddr);
962   PUT_SCNHDR_SIZE (abfd, scnhdr_int->s_size,
963                    (bfd_byte *) scnhdr_ext->s_size);
964 #endif
965   PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
966                      (bfd_byte *) scnhdr_ext->s_scnptr);
967   PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
968                      (bfd_byte *) scnhdr_ext->s_relptr);
969   PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
970                       (bfd_byte *) scnhdr_ext->s_lnnoptr);
971 #ifdef COFF_IMAGE_WITH_PE
972   /* Extra flags must be set when dealing with NT.  All sections should also
973      have the IMAGE_SCN_MEM_READ (0x40000000) flag set.  In addition, the
974      .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
975      sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
976      (this is especially important when dealing with the .idata section since
977      the addresses for routines from .dlls must be overwritten).  If .reloc
978      section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
979      (0x02000000).  Also, the resource data should also be read and
980      writable.  */
981   {
982     int flags = scnhdr_int->s_flags;
983     if (strcmp (scnhdr_int->s_name, ".data")  == 0 ||
984         strcmp (scnhdr_int->s_name, ".CRT")   == 0 ||
985         strcmp (scnhdr_int->s_name, ".rsrc")  == 0 ||
986         strcmp (scnhdr_int->s_name, ".bss")   == 0)
987       flags |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
988     else if (strcmp (scnhdr_int->s_name, ".text") == 0)
989       flags |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
990     else if (strcmp (scnhdr_int->s_name, ".reloc") == 0)
991       flags = SEC_DATA| IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE;
992     else if (strcmp (scnhdr_int->s_name, ".idata") == 0)
993       flags = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | SEC_DATA;     
994     else if (strcmp (scnhdr_int->s_name, ".rdata") == 0
995              || strcmp (scnhdr_int->s_name, ".edata") == 0)
996       flags =  IMAGE_SCN_MEM_READ | SEC_DATA;     
997
998     PUTWORD(abfd, flags, (bfd_byte *) scnhdr_ext->s_flags);
999   }
1000 #else
1001   PUTWORD(abfd, scnhdr_int->s_flags, (bfd_byte *) scnhdr_ext->s_flags);
1002 #endif
1003 #if defined(M88)
1004   PUTWORD(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
1005   PUTWORD(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
1006 #else
1007   if (scnhdr_int->s_nlnno <= 0xffff)
1008     PUTHALF(abfd, scnhdr_int->s_nlnno, (bfd_byte *) scnhdr_ext->s_nlnno);
1009   else
1010     {
1011       (*_bfd_error_handler) ("%s: line number overflow: 0x%lx > 0xffff",
1012                              bfd_get_filename (abfd),
1013                              scnhdr_int->s_nlnno);
1014       bfd_set_error (bfd_error_file_truncated);
1015       PUTHALF (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nlnno);
1016       ret = 0;
1017     }
1018   if (scnhdr_int->s_nreloc <= 0xffff)
1019     PUTHALF(abfd, scnhdr_int->s_nreloc, (bfd_byte *) scnhdr_ext->s_nreloc);
1020   else
1021     {
1022       (*_bfd_error_handler) ("%s: reloc overflow: 0x%lx > 0xffff",
1023                              bfd_get_filename (abfd),
1024                              scnhdr_int->s_nreloc);
1025       bfd_set_error (bfd_error_file_truncated);
1026       PUTHALF (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);
1027       ret = 0;
1028     }
1029 #endif
1030
1031 #if defined(I960)
1032   PUTWORD(abfd, scnhdr_int->s_align, (bfd_byte *) scnhdr_ext->s_align);
1033 #endif
1034   return ret;
1035 }