Add XCOFF64 support.
[external/binutils.git] / bfd / coff64-rs6000.c
1 /* BFD back-end for IBM RS/6000 "XCOFF64" files.
2    Copyright 2000
3    Free Software Foundation, Inc.
4    Written Clinton Popetz.
5    Contributed by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 /* Internalcoff.h and coffcode.h modify themselves based on these flags.  */
24 #define XCOFF64 
25 #define RS6000COFF_C 1
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "coff/internal.h"
31 #include "coff/rs6k64.h"
32 #include "libcoff.h"
33
34
35 #define GET_FILEHDR_SYMPTR bfd_h_get_64
36 #define PUT_FILEHDR_SYMPTR bfd_h_put_64
37 #define GET_AOUTHDR_DATA_START bfd_h_get_64
38 #define PUT_AOUTHDR_DATA_START bfd_h_put_64
39 #define GET_AOUTHDR_TEXT_START bfd_h_get_64
40 #define PUT_AOUTHDR_TEXT_START bfd_h_put_64
41 #define GET_AOUTHDR_TSIZE bfd_h_get_64
42 #define PUT_AOUTHDR_TSIZE bfd_h_put_64
43 #define GET_AOUTHDR_DSIZE bfd_h_get_64
44 #define PUT_AOUTHDR_DSIZE bfd_h_put_64
45 #define GET_AOUTHDR_BSIZE bfd_h_get_64
46 #define PUT_AOUTHDR_BSIZE bfd_h_put_64
47 #define GET_AOUTHDR_ENTRY bfd_h_get_64
48 #define PUT_AOUTHDR_ENTRY bfd_h_put_64
49 #define GET_SCNHDR_PADDR bfd_h_get_64
50 #define PUT_SCNHDR_PADDR bfd_h_put_64
51 #define GET_SCNHDR_VADDR bfd_h_get_64
52 #define PUT_SCNHDR_VADDR bfd_h_put_64
53 #define GET_SCNHDR_SIZE bfd_h_get_64
54 #define PUT_SCNHDR_SIZE bfd_h_put_64
55 #define GET_SCNHDR_SCNPTR bfd_h_get_64
56 #define PUT_SCNHDR_SCNPTR bfd_h_put_64
57 #define GET_SCNHDR_RELPTR bfd_h_get_64
58 #define PUT_SCNHDR_RELPTR bfd_h_put_64
59 #define GET_SCNHDR_LNNOPTR bfd_h_get_64
60 #define PUT_SCNHDR_LNNOPTR bfd_h_put_64
61 #define GET_SCNHDR_NRELOC bfd_h_get_32
62 #define MAX_SCNHDR_NRELOC 0xffffffff
63 #define PUT_SCNHDR_NRELOC bfd_h_put_32
64 #define GET_SCNHDR_NLNNO bfd_h_get_32
65 #define MAX_SCNHDR_NLNNO 0xffffffff
66 #define PUT_SCNHDR_NLNNO bfd_h_put_32
67 #define GET_RELOC_VADDR bfd_h_get_64
68 #define PUT_RELOC_VADDR bfd_h_put_64
69
70 #define COFF_FORCE_SYMBOLS_IN_STRINGS
71 #define COFF_DEBUG_STRING_WIDE_PREFIX
72
73 #define TARGET_SYM rs6000coff64_vec
74 #define TARGET_NAME "aixcoff64-rs6000"
75
76 #define COFF_ADJUST_SCNHDR_OUT_POST(ABFD,INT,EXT) \
77 do { \
78   memset (((SCNHDR *)EXT)->s_pad, 0, sizeof (((SCNHDR *)EXT)->s_pad));\
79 } while(0)
80
81 #define NO_COFF_LINENOS
82
83 #define coff_SWAP_lineno_in xcoff64_swap_lineno_in
84 #define coff_SWAP_lineno_out xcoff64_swap_lineno_out
85
86 #define PUTWORD bfd_h_put_32
87 #define PUTHALF bfd_h_put_16
88 #define PUTBYTE bfd_h_put_8
89 #define GETWORD bfd_h_get_32
90 #define GETHALF bfd_h_get_16
91 #define GETBYTE bfd_h_get_8
92
93
94 /* For XCOFF64, the effective width of symndx changes depending on 
95    whether we are the first entry.  Sigh.  */
96 static void
97 xcoff64_swap_lineno_in (abfd, ext1, in1)
98      bfd            *abfd;
99      PTR ext1;
100      PTR in1;
101 {
102   LINENO *ext = (LINENO *)ext1;
103   struct internal_lineno      *in = (struct internal_lineno *)in1;
104
105   in->l_lnno = bfd_h_get_32(abfd, (bfd_byte *) (ext->l_lnno));
106   if (in->l_lnno == 0)
107     in->l_addr.l_symndx = 
108             bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx);
109   else
110     in->l_addr.l_symndx = 
111             bfd_h_get_64(abfd, (bfd_byte *) ext->l_addr.l_symndx);
112 }
113
114 static unsigned int
115 xcoff64_swap_lineno_out (abfd, inp, outp)
116      bfd       *abfd;
117      PTR        inp;
118      PTR        outp;
119 {
120   struct internal_lineno *in = (struct internal_lineno *)inp;
121   struct external_lineno *ext = (struct external_lineno *)outp;
122   PUTWORD(abfd, in->l_addr.l_symndx, (bfd_byte *)
123           ext->l_addr.l_symndx);
124
125   bfd_h_put_32 (abfd, in->l_lnno,  (bfd_byte *) (ext->l_lnno));
126   if (in->l_lnno == 0)
127     bfd_h_put_32 (abfd, in->l_addr.l_symndx, (bfd_byte *)ext->l_addr.l_symndx);
128   else
129     bfd_h_put_64 (abfd, in->l_addr.l_symndx, (bfd_byte *)ext->l_addr.l_symndx);
130
131   return bfd_coff_linesz (abfd);
132 }
133
134 #define NO_COFF_SYMBOLS
135
136 static void xcoff64_swap_sym_in PARAMS ((bfd *, PTR, PTR));
137 static unsigned int xcoff64_swap_sym_out PARAMS ((bfd *, PTR, PTR));
138 static void xcoff64_swap_aux_in PARAMS ((bfd *, PTR, int, int, int, int, PTR));
139 static unsigned int xcoff64_swap_aux_out PARAMS ((bfd *, PTR, int, int, int, int, PTR));
140
141 static void
142 xcoff64_swap_sym_in (abfd, ext1, in1)
143      bfd            *abfd;
144      PTR ext1;
145      PTR in1;
146 {
147   SYMENT *ext = (SYMENT *)ext1;
148   struct internal_syment      *in = (struct internal_syment *)in1;
149
150
151   in->_n._n_n._n_zeroes = 0;
152   in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e_offset);
153   in->n_value = bfd_h_get_64(abfd, (bfd_byte *) ext->e.e_value); 
154   in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
155   in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
156   in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
157   in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
158 }
159
160 static unsigned int
161 xcoff64_swap_sym_out (abfd, inp, extp)
162      bfd       *abfd;
163      PTR        inp;
164      PTR        extp;
165 {
166   struct internal_syment *in = (struct internal_syment *)inp;
167   SYMENT *ext =(SYMENT *)extp;
168
169   bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *)  ext->e_offset);
170   bfd_h_put_64(abfd,  in->n_value , (bfd_byte *) ext->e.e_value);
171   bfd_h_put_16(abfd,  in->n_scnum , (bfd_byte *) ext->e_scnum);
172   bfd_h_put_16(abfd,  in->n_type , (bfd_byte *) ext->e_type);
173   bfd_h_put_8(abfd,  in->n_sclass , ext->e_sclass);
174   bfd_h_put_8(abfd,  in->n_numaux , ext->e_numaux);
175   return bfd_coff_symesz (abfd);
176 }
177
178 static void
179 xcoff64_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
180      bfd            *abfd;
181      PTR              ext1;
182      int             type;
183      int             class;
184      int              indx;
185      int              numaux;
186      PTR              in1;
187 {
188   AUXENT    *ext = (AUXENT *)ext1;
189   union internal_auxent *in = (union internal_auxent *)in1;
190
191   switch (class) {
192     case C_FILE:
193       if (ext->x_file.x_fname[0] == 0) {
194           in->x_file.x_n.x_zeroes = 0;
195           in->x_file.x_n.x_offset = 
196            bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
197         } else {
198             if (numaux > 1)
199               {
200                 if (indx == 0)
201           memcpy (in->x_file.x_fname, ext->x_file.x_fname,
202                           numaux * sizeof (AUXENT));
203               }
204             else
205               {
206                 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
207               }
208           }
209       goto end;
210
211       /* RS/6000 "csect" auxents */
212     case C_EXT:
213     case C_HIDEXT:
214       if (indx + 1 == numaux)
215         {
216           in->x_csect.x_scnlen.l = 
217               bfd_h_get_32(abfd, ext->x_csect.x_scnlen_lo);
218           /* FIXME: If we want section lengths larger than 32 bits, we need 
219              to modify the internal coff structures to support it.  */
220           in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
221                                                  ext->x_csect.x_parmhash);
222           in->x_csect.x_snhash   = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
223           /* We don't have to hack bitfields in x_smtyp because it's
224              defined by shifts-and-ands, which are equivalent on all
225              byte orders.  */
226           in->x_csect.x_smtyp    = bfd_h_get_8  (abfd, ext->x_csect.x_smtyp);
227           in->x_csect.x_smclas   = bfd_h_get_8  (abfd, ext->x_csect.x_smclas);
228           goto end;
229         }
230       break;
231
232     case C_STAT:
233     case C_LEAFSTAT:
234     case C_HIDDEN:
235       if (type == T_NULL) {
236           /* PE defines some extra fields; we zero them out for
237              safety.  */
238           in->x_scn.x_checksum = 0;
239           in->x_scn.x_associated = 0;
240           in->x_scn.x_comdat = 0;
241
242           goto end;
243         }
244       break;
245     }
246
247   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
248     {
249       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = bfd_h_get_64(abfd, (bfd_byte *)
250               ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
251       in->x_sym.x_fcnary.x_fcn.x_endndx.l = bfd_h_get_32(abfd, (bfd_byte *)
252               ext->x_sym.x_fcnary.x_fcn.x_endndx);
253     }
254   if (ISFCN(type)) {
255     in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_fsize);
256   }
257   else {
258     in->x_sym.x_misc.x_lnsz.x_lnno = bfd_h_get_32(abfd, (bfd_byte *)
259             ext->x_sym.x_fcnary.x_lnsz.x_lnno);
260     in->x_sym.x_misc.x_lnsz.x_size = bfd_h_get_16(abfd, (bfd_byte *)
261             ext->x_sym.x_fcnary.x_lnsz.x_size);
262   }
263
264 end: ;
265   /* the semicolon is because MSVC doesn't like labels at
266      end of block. */
267
268 }
269
270
271
272 static unsigned int
273 xcoff64_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
274      bfd   *abfd;
275      PTR        inp;
276      int   type;
277      int   class;
278      int   indx ATTRIBUTE_UNUSED;
279      int   numaux ATTRIBUTE_UNUSED;
280      PTR        extp;
281 {
282   union internal_auxent *in = (union internal_auxent *)inp;
283   AUXENT *ext = (AUXENT *)extp;
284
285   memset((PTR)ext, 0, bfd_coff_auxesz (abfd));
286   switch (class)
287     {
288   case C_FILE:
289     if (in->x_file.x_fname[0] == 0)
290       {
291       PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
292       PUTWORD(abfd,
293               in->x_file.x_n.x_offset,
294               (bfd_byte *) ext->x_file.x_n.x_offset);
295     }
296     else
297       {
298       memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
299       }
300     PUTBYTE (abfd, _AUX_FILE, (bfd_byte *) ext->x_auxtype.x_auxtype);
301     goto end;
302
303   /* RS/6000 "csect" auxents */
304   case C_EXT:
305   case C_HIDEXT:
306     if (indx + 1 == numaux)
307       {
308         PUTWORD (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen_lo);
309         PUTWORD (abfd, in->x_csect.x_parmhash,  ext->x_csect.x_parmhash);
310         PUTHALF (abfd, in->x_csect.x_snhash,    ext->x_csect.x_snhash);
311         /* We don't have to hack bitfields in x_smtyp because it's
312            defined by shifts-and-ands, which are equivalent on all
313            byte orders.  */
314         PUTBYTE (abfd, in->x_csect.x_smtyp,     ext->x_csect.x_smtyp);
315         PUTBYTE (abfd, in->x_csect.x_smclas,    ext->x_csect.x_smclas);
316         PUTBYTE (abfd, _AUX_CSECT, (bfd_byte *) ext->x_auxtype.x_auxtype);
317         goto end;
318       }
319     break;
320
321   case C_STAT:
322   case C_LEAFSTAT:
323   case C_HIDDEN:
324     if (type == T_NULL) {
325       goto end;
326     }
327     break;
328   }
329
330   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
331     {
332       bfd_h_put_64(abfd,  in->x_sym.x_fcnary.x_fcn.x_lnnoptr, 
333               (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
334       PUTBYTE (abfd, _AUX_FCN, (bfd_byte *) ext->x_auxtype.x_auxtype);
335       PUTWORD(abfd,  in->x_sym.x_fcnary.x_fcn.x_endndx.l, 
336               (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx);
337     }
338   if (ISFCN (type))
339     PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
340              (bfd_byte *)  ext->x_sym.x_fcnary.x_fcn.x_fsize);
341   else
342     {
343       bfd_h_put_32(abfd, in->x_sym.x_misc.x_lnsz.x_lnno, 
344               (bfd_byte *)ext->x_sym.x_fcnary.x_lnsz.x_lnno);
345       bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_size, 
346               (bfd_byte *)ext->x_sym.x_fcnary.x_lnsz.x_size);
347     }
348
349 end:
350   return bfd_coff_auxesz (abfd);
351 }
352
353 #define SELECT_RELOC(internal, howto)                                   \
354   {                                                                     \
355     internal.r_type = howto->type;                                      \
356     internal.r_size =                                                   \
357       ((howto->complain_on_overflow == complain_overflow_signed         \
358         ? 0x80                                                          \
359         : 0)                                                            \
360        | (howto->bitsize - 1));                                         \
361   }
362 \f
363 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
364
365 #define COFF_LONG_FILENAMES
366
367 #define RTYPE2HOWTO(cache_ptr, dst) _bfd_xcoff_rtype2howto (cache_ptr, dst)
368
369 #define coff_SWAP_sym_in xcoff64_swap_sym_in
370 #define coff_SWAP_sym_out xcoff64_swap_sym_out
371 #define coff_SWAP_aux_in xcoff64_swap_aux_in
372 #define coff_SWAP_aux_out xcoff64_swap_aux_out
373 #define coff_mkobject _bfd_xcoff_mkobject
374 #define coff_bfd_copy_private_bfd_data _bfd_xcoff_copy_private_bfd_data
375 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
376 #define coff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
377 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
378
379 extern boolean _bfd_xcoff_mkobject PARAMS ((bfd *));
380 extern boolean _bfd_xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *));
381 extern boolean _bfd_xcoff_is_local_label_name PARAMS ((bfd *, const char *));
382 extern void _bfd_xcoff_rtype2howto
383   PARAMS ((arelent *, struct internal_reloc *));
384 extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
385   PARAMS ((bfd *, bfd_reloc_code_real_type));
386 extern boolean _bfd_xcoff_slurp_armap PARAMS ((bfd *));
387 extern const bfd_target *_bfd_xcoff_archive_p PARAMS ((bfd *));
388 extern PTR _bfd_xcoff_read_ar_hdr PARAMS ((bfd *));
389 extern bfd *_bfd_xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
390 extern int _bfd_xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
391 extern boolean _bfd_xcoff_write_armap
392   PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
393 extern boolean _bfd_xcoff_write_archive_contents PARAMS ((bfd *));
394 extern int _bfd_xcoff_sizeof_headers PARAMS ((bfd *, boolean));
395
396 #define _bfd_xcoff_slurp_extended_name_table bfd_false
397 #define _bfd_xcoff_construct_extended_name_table \
398   ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
399    bfd_false)
400 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
401
402 /* We can use the standard get_elt_at_index routine.  */
403
404 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
405
406 /* XCOFF archives do not have a timestamp.  */
407
408 #define _bfd_xcoff_update_armap_timestamp bfd_true
409
410 #include "coffcode.h"
411
412 #define CORE_FILE_P _bfd_dummy_target
413
414 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
415 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
416 #define coff_core_file_matches_executable_p \
417   _bfd_nocore_core_file_matches_executable_p
418
419 #define _bfd_xcoff_bfd_get_relocated_section_contents \
420   coff_bfd_get_relocated_section_contents
421 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
422 #define _bfd_xcoff_bfd_gc_sections coff_bfd_gc_sections
423 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
424
425 /* The transfer vector that leads the outside world to all of the above. */
426
427 const bfd_target
428 #ifdef TARGET_SYM
429   TARGET_SYM =
430 #else
431   rs6000coff64_vec =
432 #endif
433 {
434 #ifdef TARGET_NAME
435   TARGET_NAME,
436 #else
437   "aixcoff64-rs6000",           /* name */
438 #endif
439   bfd_target_coff_flavour,
440   BFD_ENDIAN_BIG,               /* data byte order is big */
441   BFD_ENDIAN_BIG,               /* header byte order is big */
442
443   (HAS_RELOC | EXEC_P |         /* object flags */
444    HAS_LINENO | HAS_DEBUG | DYNAMIC |
445    HAS_SYMS | HAS_LOCALS | WP_TEXT),
446
447   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
448   0,                            /* leading char */
449   '/',                          /* ar_pad_char */
450   15,                           /* ar_max_namelen??? FIXMEmgo */
451
452   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
453      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
454      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
455   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
456      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
457      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
458
459   {_bfd_dummy_target, coff_object_p,    /* bfd_check_format */
460      _bfd_xcoff_archive_p, CORE_FILE_P},
461   {bfd_false, coff_mkobject,            /* bfd_set_format */
462      _bfd_generic_mkarchive, bfd_false},
463   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
464      _bfd_xcoff_write_archive_contents, bfd_false},
465
466      BFD_JUMP_TABLE_GENERIC (coff),
467      BFD_JUMP_TABLE_COPY (coff),
468      BFD_JUMP_TABLE_CORE (coff),
469      BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
470      BFD_JUMP_TABLE_SYMBOLS (coff),
471      BFD_JUMP_TABLE_RELOCS (coff),
472      BFD_JUMP_TABLE_WRITE (coff),
473      BFD_JUMP_TABLE_LINK (_bfd_xcoff),
474      BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
475
476   NULL,
477   
478   COFF_SWAP_TABLE
479 };