Add XCOFF64 support.
[binutils-gdb.git] / bfd / coff-rs6000.c
1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 FIXME: Can someone provide a transliteration of this name into ASCII?
5 Using the following chars caused a compiler warning on HIUX (so I replaced
6 them with octal escapes), and isn't useful without an understanding of what
7 character set it is.
8 Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365,
9 and John Gilmore.
10 Archive support from Damon A. Permezel.
11 Contributed by IBM Corporation and Cygnus Support.
12
13 This file is part of BFD, the Binary File Descriptor library.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28
29 /* Internalcoff.h and coffcode.h modify themselves based on this flag. */
30 #define RS6000COFF_C 1
31
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libbfd.h"
35 #include "coff/internal.h"
36 #include "coff/rs6000.h"
37 #include "libcoff.h"
38
39 /* The main body of code is in coffcode.h. */
40
41 boolean _bfd_xcoff_mkobject PARAMS ((bfd *));
42 boolean _bfd_xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *));
43 boolean _bfd_xcoff_is_local_label_name PARAMS ((bfd *, const char *));
44 void _bfd_xcoff_rtype2howto
45 PARAMS ((arelent *, struct internal_reloc *));
46 reloc_howto_type *_bfd_xcoff_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48 boolean _bfd_xcoff_slurp_armap PARAMS ((bfd *));
49 const bfd_target *_bfd_xcoff_archive_p PARAMS ((bfd *));
50 PTR _bfd_xcoff_read_ar_hdr PARAMS ((bfd *));
51 bfd *_bfd_xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *));
52 int _bfd_xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
53 static const char *normalize_filename PARAMS ((bfd *));
54 boolean _bfd_xcoff_write_armap
55 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int));
56 boolean _bfd_xcoff_write_archive_contents PARAMS ((bfd *));
57 int _bfd_xcoff_sizeof_headers PARAMS ((bfd *, boolean));
58 \f
59 /* We use our own tdata type. Its first field is the COFF tdata type,
60 so the COFF routines are compatible. */
61
62 boolean
63 _bfd_xcoff_mkobject (abfd)
64 bfd *abfd;
65 {
66 coff_data_type *coff;
67
68 abfd->tdata.xcoff_obj_data =
69 ((struct xcoff_tdata *)
70 bfd_zalloc (abfd, sizeof (struct xcoff_tdata)));
71 if (abfd->tdata.xcoff_obj_data == NULL)
72 return false;
73 coff = coff_data (abfd);
74 coff->symbols = (coff_symbol_type *) NULL;
75 coff->conversion_table = (unsigned int *) NULL;
76 coff->raw_syments = (struct coff_ptr_struct *) NULL;
77 coff->relocbase = 0;
78
79 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
80
81 /* We set cputype to -1 to indicate that it has not been
82 initialized. */
83 xcoff_data (abfd)->cputype = -1;
84
85 xcoff_data (abfd)->csects = NULL;
86 xcoff_data (abfd)->debug_indices = NULL;
87
88 return true;
89 }
90
91 /* Copy XCOFF data from one BFD to another. */
92
93 boolean
94 _bfd_xcoff_copy_private_bfd_data (ibfd, obfd)
95 bfd *ibfd;
96 bfd *obfd;
97 {
98 struct xcoff_tdata *ix, *ox;
99 asection *sec;
100
101 if (ibfd->xvec != obfd->xvec)
102 return true;
103 ix = xcoff_data (ibfd);
104 ox = xcoff_data (obfd);
105 ox->full_aouthdr = ix->full_aouthdr;
106 ox->toc = ix->toc;
107 if (ix->sntoc == 0)
108 ox->sntoc = 0;
109 else
110 {
111 sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
112 if (sec == NULL)
113 ox->sntoc = 0;
114 else
115 ox->sntoc = sec->output_section->target_index;
116 }
117 if (ix->snentry == 0)
118 ox->snentry = 0;
119 else
120 {
121 sec = coff_section_from_bfd_index (ibfd, ix->snentry);
122 if (sec == NULL)
123 ox->snentry = 0;
124 else
125 ox->snentry = sec->output_section->target_index;
126 }
127 ox->text_align_power = ix->text_align_power;
128 ox->data_align_power = ix->data_align_power;
129 ox->modtype = ix->modtype;
130 ox->cputype = ix->cputype;
131 ox->maxdata = ix->maxdata;
132 ox->maxstack = ix->maxstack;
133 return true;
134 }
135
136 /* I don't think XCOFF really has a notion of local labels based on
137 name. This will mean that ld -X doesn't actually strip anything.
138 The AIX native linker does not have a -X option, and it ignores the
139 -x option. */
140
141 boolean
142 _bfd_xcoff_is_local_label_name (abfd, name)
143 bfd *abfd ATTRIBUTE_UNUSED;
144 const char *name ATTRIBUTE_UNUSED;
145 {
146 return false;
147 }
148 \f
149
150 #define NO_COFF_SYMBOLS
151
152 static void xcoff_swap_sym_in PARAMS ((bfd *, PTR, PTR));
153 static unsigned int xcoff_swap_sym_out PARAMS ((bfd *, PTR, PTR));
154 static void xcoff_swap_aux_in PARAMS ((bfd *, PTR, int, int, int, int, PTR));
155 static unsigned int xcoff_swap_aux_out PARAMS ((bfd *, PTR, int, int, int, int, PTR));
156
157 static void
158 xcoff_swap_sym_in (abfd, ext1, in1)
159 bfd *abfd;
160 PTR ext1;
161 PTR in1;
162 {
163 SYMENT *ext = (SYMENT *)ext1;
164 struct internal_syment *in = (struct internal_syment *)in1;
165
166
167 if(ext->e.e_name[0] != 0)
168 {
169 memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
170 }
171 else
172 {
173 in->_n._n_n._n_zeroes = 0;
174 in->_n._n_n._n_offset =
175 bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
176 }
177
178 in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value);
179 in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
180 in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
181 in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
182 in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
183 }
184
185 static unsigned int
186 xcoff_swap_sym_out (abfd, inp, extp)
187 bfd *abfd;
188 PTR inp;
189 PTR extp;
190 {
191 struct internal_syment *in = (struct internal_syment *)inp;
192 SYMENT *ext =(SYMENT *)extp;
193
194 if(in->_n._n_name[0] != 0)
195 {
196 memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
197 }
198 else
199 {
200 bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
201 bfd_h_put_32(abfd, in->_n._n_n._n_offset,
202 (bfd_byte *) ext->e.e.e_offset);
203 }
204
205 bfd_h_put_32(abfd, in->n_value , (bfd_byte *) ext->e_value);
206 bfd_h_put_16(abfd, in->n_scnum , (bfd_byte *) ext->e_scnum);
207 bfd_h_put_16(abfd, in->n_type , (bfd_byte *) ext->e_type);
208 bfd_h_put_8(abfd, in->n_sclass , ext->e_sclass);
209 bfd_h_put_8(abfd, in->n_numaux , ext->e_numaux);
210 return bfd_coff_symesz (abfd);
211 }
212
213 #define PUTWORD bfd_h_put_32
214 #define PUTHALF bfd_h_put_16
215 #define PUTBYTE bfd_h_put_8
216 #define GETWORD bfd_h_get_32
217 #define GETHALF bfd_h_get_16
218 #define GETBYTE bfd_h_get_8
219
220 static void
221 xcoff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
222 bfd *abfd;
223 PTR ext1;
224 int type;
225 int class;
226 int indx;
227 int numaux;
228 PTR in1;
229 {
230 AUXENT *ext = (AUXENT *)ext1;
231 union internal_auxent *in = (union internal_auxent *)in1;
232
233 switch (class) {
234 case C_FILE:
235 if (ext->x_file.x_fname[0] == 0) {
236 in->x_file.x_n.x_zeroes = 0;
237 in->x_file.x_n.x_offset =
238 bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
239 } else {
240 if (numaux > 1)
241 {
242 if (indx == 0)
243 memcpy (in->x_file.x_fname, ext->x_file.x_fname,
244 numaux * sizeof (AUXENT));
245 }
246 else
247 {
248 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
249 }
250 }
251 goto end;
252
253 /* RS/6000 "csect" auxents */
254 case C_EXT:
255 case C_HIDEXT:
256 if (indx + 1 == numaux)
257 {
258 in->x_csect.x_scnlen.l =
259 bfd_h_get_32 (abfd, ext->x_csect.x_scnlen);
260 in->x_csect.x_parmhash = bfd_h_get_32 (abfd,
261 ext->x_csect.x_parmhash);
262 in->x_csect.x_snhash = bfd_h_get_16 (abfd, ext->x_csect.x_snhash);
263 /* We don't have to hack bitfields in x_smtyp because it's
264 defined by shifts-and-ands, which are equivalent on all
265 byte orders. */
266 in->x_csect.x_smtyp = bfd_h_get_8 (abfd, ext->x_csect.x_smtyp);
267 in->x_csect.x_smclas = bfd_h_get_8 (abfd, ext->x_csect.x_smclas);
268 in->x_csect.x_stab = bfd_h_get_32 (abfd, ext->x_csect.x_stab);
269 in->x_csect.x_snstab = bfd_h_get_16 (abfd, ext->x_csect.x_snstab);
270 goto end;
271 }
272 break;
273
274 case C_STAT:
275 case C_LEAFSTAT:
276 case C_HIDDEN:
277 if (type == T_NULL) {
278 in->x_scn.x_scnlen = bfd_h_get_32(abfd,
279 (bfd_byte *) ext->x_scn.x_scnlen);
280 in->x_scn.x_nreloc = bfd_h_get_16(abfd,
281 (bfd_byte *) ext->x_scn.x_nreloc);
282 in->x_scn.x_nlinno = bfd_h_get_16(abfd,
283 (bfd_byte *) ext->x_scn.x_nlinno);
284 /* PE defines some extra fields; we zero them out for
285 safety. */
286 in->x_scn.x_checksum = 0;
287 in->x_scn.x_associated = 0;
288 in->x_scn.x_comdat = 0;
289
290 goto end;
291 }
292 break;
293 }
294
295 in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
296 in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
297
298 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
299 {
300 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = bfd_h_get_32(abfd, (bfd_byte *)
301 ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
302 in->x_sym.x_fcnary.x_fcn.x_endndx.l = bfd_h_get_32(abfd, (bfd_byte *)
303 ext->x_sym.x_fcnary.x_fcn.x_endndx);
304 }
305 else
306 {
307 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
308 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
309 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
310 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
311 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
312 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
313 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
314 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
315 }
316 if (ISFCN(type)) {
317 in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
318 }
319 else {
320 in->x_sym.x_misc.x_lnsz.x_lnno = bfd_h_get_16(abfd, (bfd_byte *)
321 ext->x_sym.x_misc.x_lnsz.x_lnno);
322 in->x_sym.x_misc.x_lnsz.x_size = bfd_h_get_16(abfd, (bfd_byte *)
323 ext->x_sym.x_misc.x_lnsz.x_size);
324 }
325
326 end: ;
327 /* the semicolon is because MSVC doesn't like labels at
328 end of block. */
329
330 }
331
332
333
334 static unsigned int
335 xcoff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
336 bfd *abfd;
337 PTR inp;
338 int type;
339 int class;
340 int indx ATTRIBUTE_UNUSED;
341 int numaux ATTRIBUTE_UNUSED;
342 PTR extp;
343 {
344 union internal_auxent *in = (union internal_auxent *)inp;
345 AUXENT *ext = (AUXENT *)extp;
346
347 memset((PTR)ext, 0, bfd_coff_auxesz (abfd));
348 switch (class)
349 {
350 case C_FILE:
351 if (in->x_file.x_fname[0] == 0)
352 {
353 PUTWORD(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
354 PUTWORD(abfd,
355 in->x_file.x_n.x_offset,
356 (bfd_byte *) ext->x_file.x_n.x_offset);
357 }
358 else
359 {
360 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
361 }
362 goto end;
363
364 /* RS/6000 "csect" auxents */
365 case C_EXT:
366 case C_HIDEXT:
367 if (indx + 1 == numaux)
368 {
369 PUTWORD (abfd, in->x_csect.x_scnlen.l,ext->x_csect.x_scnlen);
370 PUTWORD (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
371 PUTHALF (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
372 /* We don't have to hack bitfields in x_smtyp because it's
373 defined by shifts-and-ands, which are equivalent on all
374 byte orders. */
375 PUTBYTE (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
376 PUTBYTE (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
377 PUTWORD (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
378 PUTHALF (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
379 goto end;
380 }
381 break;
382
383 case C_STAT:
384 case C_LEAFSTAT:
385 case C_HIDDEN:
386 if (type == T_NULL) {
387 bfd_h_put_32(abfd, in->x_scn.x_scnlen, (bfd_byte *) ext->x_scn.x_scnlen);
388 bfd_h_put_16(abfd, in->x_scn.x_nreloc, (bfd_byte *) ext->x_scn.x_nreloc);
389 bfd_h_put_16(abfd, in->x_scn.x_nlinno, (bfd_byte *) ext->x_scn.x_nlinno);
390 goto end;
391 }
392 break;
393 }
394
395 PUTWORD(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
396 PUTWORD(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
397
398 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
399 {
400 bfd_h_put_32(abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
401 (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
402 PUTWORD(abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
403 (bfd_byte *) ext->x_sym.x_fcnary.x_fcn.x_endndx);
404 }
405 else
406 {
407 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
408 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
409 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
410 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
411 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
412 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
413 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
414 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
415 }
416
417 if (ISFCN (type))
418 PUTWORD (abfd, in->x_sym.x_misc.x_fsize,
419 (bfd_byte *) ext->x_sym.x_misc.x_fsize);
420 else
421 {
422 bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
423 (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_lnno);
424 bfd_h_put_16(abfd, in->x_sym.x_misc.x_lnsz.x_size,
425 (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_size);
426 }
427
428 end:
429 return bfd_coff_auxesz (abfd);
430 }
431
432
433 \f
434 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
435 bitsize and whether they are signed or not, along with a
436 conventional type. This table is for the types, which are used for
437 different algorithms for putting in the reloc. Many of these
438 relocs need special_function entries, which I have not written. */
439
440 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
441 from smaller values. Start with zero, widen, *then* decrement. */
442 #define MINUS_ONE (((bfd_vma)0) - 1)
443
444 reloc_howto_type xcoff_howto_table[] =
445 {
446 /* Standard 32 bit relocation. */
447 HOWTO (0, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 32, /* bitsize */
451 false, /* pc_relative */
452 0, /* bitpos */
453 complain_overflow_bitfield, /* complain_on_overflow */
454 0, /* special_function */
455 "R_POS", /* name */
456 true, /* partial_inplace */
457 0xffffffff, /* src_mask */
458 0xffffffff, /* dst_mask */
459 false), /* pcrel_offset */
460
461 /* 32 bit relocation, but store negative value. */
462 HOWTO (1, /* type */
463 0, /* rightshift */
464 -2, /* size (0 = byte, 1 = short, 2 = long) */
465 32, /* bitsize */
466 false, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_bitfield, /* complain_on_overflow */
469 0, /* special_function */
470 "R_NEG", /* name */
471 true, /* partial_inplace */
472 0xffffffff, /* src_mask */
473 0xffffffff, /* dst_mask */
474 false), /* pcrel_offset */
475
476 /* 32 bit PC relative relocation. */
477 HOWTO (2, /* type */
478 0, /* rightshift */
479 2, /* size (0 = byte, 1 = short, 2 = long) */
480 32, /* bitsize */
481 true, /* pc_relative */
482 0, /* bitpos */
483 complain_overflow_signed, /* complain_on_overflow */
484 0, /* special_function */
485 "R_REL", /* name */
486 true, /* partial_inplace */
487 0xffffffff, /* src_mask */
488 0xffffffff, /* dst_mask */
489 false), /* pcrel_offset */
490
491 /* 16 bit TOC relative relocation. */
492 HOWTO (3, /* type */
493 0, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
496 false, /* pc_relative */
497 0, /* bitpos */
498 complain_overflow_bitfield, /* complain_on_overflow */
499 0, /* special_function */
500 "R_TOC", /* name */
501 true, /* partial_inplace */
502 0xffff, /* src_mask */
503 0xffff, /* dst_mask */
504 false), /* pcrel_offset */
505
506 /* I don't really know what this is. */
507 HOWTO (4, /* type */
508 1, /* rightshift */
509 2, /* size (0 = byte, 1 = short, 2 = long) */
510 32, /* bitsize */
511 false, /* pc_relative */
512 0, /* bitpos */
513 complain_overflow_bitfield, /* complain_on_overflow */
514 0, /* special_function */
515 "R_RTB", /* name */
516 true, /* partial_inplace */
517 0xffffffff, /* src_mask */
518 0xffffffff, /* dst_mask */
519 false), /* pcrel_offset */
520
521 /* External TOC relative symbol. */
522 HOWTO (5, /* type */
523 0, /* rightshift */
524 2, /* size (0 = byte, 1 = short, 2 = long) */
525 16, /* bitsize */
526 false, /* pc_relative */
527 0, /* bitpos */
528 complain_overflow_bitfield, /* complain_on_overflow */
529 0, /* special_function */
530 "R_GL", /* name */
531 true, /* partial_inplace */
532 0xffff, /* src_mask */
533 0xffff, /* dst_mask */
534 false), /* pcrel_offset */
535
536 /* Local TOC relative symbol. */
537 HOWTO (6, /* type */
538 0, /* rightshift */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
540 16, /* bitsize */
541 false, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_bitfield, /* complain_on_overflow */
544 0, /* special_function */
545 "R_TCL", /* name */
546 true, /* partial_inplace */
547 0xffff, /* src_mask */
548 0xffff, /* dst_mask */
549 false), /* pcrel_offset */
550
551 EMPTY_HOWTO (7),
552
553 /* Non modifiable absolute branch. */
554 HOWTO (8, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 26, /* bitsize */
558 false, /* pc_relative */
559 0, /* bitpos */
560 complain_overflow_bitfield, /* complain_on_overflow */
561 0, /* special_function */
562 "R_BA", /* name */
563 true, /* partial_inplace */
564 0x3fffffc, /* src_mask */
565 0x3fffffc, /* dst_mask */
566 false), /* pcrel_offset */
567
568 EMPTY_HOWTO (9),
569
570 /* Non modifiable relative branch. */
571 HOWTO (0xa, /* type */
572 0, /* rightshift */
573 2, /* size (0 = byte, 1 = short, 2 = long) */
574 26, /* bitsize */
575 true, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_signed, /* complain_on_overflow */
578 0, /* special_function */
579 "R_BR", /* name */
580 true, /* partial_inplace */
581 0x3fffffc, /* src_mask */
582 0x3fffffc, /* dst_mask */
583 false), /* pcrel_offset */
584
585 EMPTY_HOWTO (0xb),
586
587 /* Indirect load. */
588 HOWTO (0xc, /* type */
589 0, /* rightshift */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
591 16, /* bitsize */
592 false, /* pc_relative */
593 0, /* bitpos */
594 complain_overflow_bitfield, /* complain_on_overflow */
595 0, /* special_function */
596 "R_RL", /* name */
597 true, /* partial_inplace */
598 0xffff, /* src_mask */
599 0xffff, /* dst_mask */
600 false), /* pcrel_offset */
601
602 /* Load address. */
603 HOWTO (0xd, /* type */
604 0, /* rightshift */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
606 16, /* bitsize */
607 false, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_bitfield, /* complain_on_overflow */
610 0, /* special_function */
611 "R_RLA", /* name */
612 true, /* partial_inplace */
613 0xffff, /* src_mask */
614 0xffff, /* dst_mask */
615 false), /* pcrel_offset */
616
617 EMPTY_HOWTO (0xe),
618
619 /* Non-relocating reference. */
620 HOWTO (0xf, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 32, /* bitsize */
624 false, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
627 0, /* special_function */
628 "R_REF", /* name */
629 false, /* partial_inplace */
630 0, /* src_mask */
631 0, /* dst_mask */
632 false), /* pcrel_offset */
633
634 EMPTY_HOWTO (0x10),
635 EMPTY_HOWTO (0x11),
636
637 /* TOC relative indirect load. */
638 HOWTO (0x12, /* type */
639 0, /* rightshift */
640 2, /* size (0 = byte, 1 = short, 2 = long) */
641 16, /* bitsize */
642 false, /* pc_relative */
643 0, /* bitpos */
644 complain_overflow_bitfield, /* complain_on_overflow */
645 0, /* special_function */
646 "R_TRL", /* name */
647 true, /* partial_inplace */
648 0xffff, /* src_mask */
649 0xffff, /* dst_mask */
650 false), /* pcrel_offset */
651
652 /* TOC relative load address. */
653 HOWTO (0x13, /* type */
654 0, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 16, /* bitsize */
657 false, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_bitfield, /* complain_on_overflow */
660 0, /* special_function */
661 "R_TRLA", /* name */
662 true, /* partial_inplace */
663 0xffff, /* src_mask */
664 0xffff, /* dst_mask */
665 false), /* pcrel_offset */
666
667 /* Modifiable relative branch. */
668 HOWTO (0x14, /* type */
669 1, /* rightshift */
670 2, /* size (0 = byte, 1 = short, 2 = long) */
671 32, /* bitsize */
672 false, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_bitfield, /* complain_on_overflow */
675 0, /* special_function */
676 "R_RRTBI", /* name */
677 true, /* partial_inplace */
678 0xffffffff, /* src_mask */
679 0xffffffff, /* dst_mask */
680 false), /* pcrel_offset */
681
682 /* Modifiable absolute branch. */
683 HOWTO (0x15, /* type */
684 1, /* rightshift */
685 2, /* size (0 = byte, 1 = short, 2 = long) */
686 32, /* bitsize */
687 false, /* pc_relative */
688 0, /* bitpos */
689 complain_overflow_bitfield, /* complain_on_overflow */
690 0, /* special_function */
691 "R_RRTBA", /* name */
692 true, /* partial_inplace */
693 0xffffffff, /* src_mask */
694 0xffffffff, /* dst_mask */
695 false), /* pcrel_offset */
696
697 /* Modifiable call absolute indirect. */
698 HOWTO (0x16, /* type */
699 0, /* rightshift */
700 2, /* size (0 = byte, 1 = short, 2 = long) */
701 16, /* bitsize */
702 false, /* pc_relative */
703 0, /* bitpos */
704 complain_overflow_bitfield, /* complain_on_overflow */
705 0, /* special_function */
706 "R_CAI", /* name */
707 true, /* partial_inplace */
708 0xffff, /* src_mask */
709 0xffff, /* dst_mask */
710 false), /* pcrel_offset */
711
712 /* Modifiable call relative. */
713 HOWTO (0x17, /* type */
714 0, /* rightshift */
715 2, /* size (0 = byte, 1 = short, 2 = long) */
716 16, /* bitsize */
717 false, /* pc_relative */
718 0, /* bitpos */
719 complain_overflow_bitfield, /* complain_on_overflow */
720 0, /* special_function */
721 "R_CREL", /* name */
722 true, /* partial_inplace */
723 0xffff, /* src_mask */
724 0xffff, /* dst_mask */
725 false), /* pcrel_offset */
726
727 /* Modifiable branch absolute. */
728 HOWTO (0x18, /* type */
729 0, /* rightshift */
730 2, /* size (0 = byte, 1 = short, 2 = long) */
731 16, /* bitsize */
732 false, /* pc_relative */
733 0, /* bitpos */
734 complain_overflow_bitfield, /* complain_on_overflow */
735 0, /* special_function */
736 "R_RBA", /* name */
737 true, /* partial_inplace */
738 0xffff, /* src_mask */
739 0xffff, /* dst_mask */
740 false), /* pcrel_offset */
741
742 /* Modifiable branch absolute. */
743 HOWTO (0x19, /* type */
744 0, /* rightshift */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
746 16, /* bitsize */
747 false, /* pc_relative */
748 0, /* bitpos */
749 complain_overflow_bitfield, /* complain_on_overflow */
750 0, /* special_function */
751 "R_RBAC", /* name */
752 true, /* partial_inplace */
753 0xffff, /* src_mask */
754 0xffff, /* dst_mask */
755 false), /* pcrel_offset */
756
757 /* Modifiable branch relative. */
758 HOWTO (0x1a, /* type */
759 0, /* rightshift */
760 2, /* size (0 = byte, 1 = short, 2 = long) */
761 26, /* bitsize */
762 false, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_signed, /* complain_on_overflow */
765 0, /* special_function */
766 "R_RBR", /* name */
767 true, /* partial_inplace */
768 0xffff, /* src_mask */
769 0xffff, /* dst_mask */
770 false), /* pcrel_offset */
771
772 /* Modifiable branch absolute. */
773 HOWTO (0x1b, /* type */
774 0, /* rightshift */
775 2, /* size (0 = byte, 1 = short, 2 = long) */
776 16, /* bitsize */
777 false, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_bitfield, /* complain_on_overflow */
780 0, /* special_function */
781 "R_RBRC", /* name */
782 true, /* partial_inplace */
783 0xffff, /* src_mask */
784 0xffff, /* dst_mask */
785 false), /* pcrel_offset */
786 HOWTO (0, /* type */
787 0, /* rightshift */
788 4, /* size (0 = byte, 1 = short, 2 = long) */
789 64, /* bitsize */
790 false, /* pc_relative */
791 0, /* bitpos */
792 complain_overflow_bitfield, /* complain_on_overflow */
793 0, /* special_function */
794 "R_POS", /* name */
795 true, /* partial_inplace */
796 MINUS_ONE, /* src_mask */
797 MINUS_ONE, /* dst_mask */
798 false) /* pcrel_offset */
799
800 };
801
802 /* These are the first two like the above but for 16-bit relocs. */
803 static reloc_howto_type xcoff_howto_table_16[] =
804 {
805 /* Standard 16 bit relocation. */
806 HOWTO (0, /* type */
807 0, /* rightshift */
808 2, /* size (0 = byte, 1 = short, 2 = long) */
809 16, /* bitsize */
810 false, /* pc_relative */
811 0, /* bitpos */
812 complain_overflow_bitfield, /* complain_on_overflow */
813 0, /* special_function */
814 "R_POS_16", /* name */
815 true, /* partial_inplace */
816 0xffffffff, /* src_mask */
817 0xffffffff, /* dst_mask */
818 false), /* pcrel_offset */
819
820 /* 16 bit relocation, but store negative value. */
821 HOWTO (1, /* type */
822 0, /* rightshift */
823 -2, /* size (0 = byte, 1 = short, 2 = long) */
824 16, /* bitsize */
825 false, /* pc_relative */
826 0, /* bitpos */
827 complain_overflow_bitfield, /* complain_on_overflow */
828 0, /* special_function */
829 "R_NEG_16", /* name */
830 true, /* partial_inplace */
831 0xffffffff, /* src_mask */
832 0xffffffff, /* dst_mask */
833 false), /* pcrel_offset */
834
835 /* 16 bit PC relative relocation. */
836 HOWTO (2, /* type */
837 0, /* rightshift */
838 2, /* size (0 = byte, 1 = short, 2 = long) */
839 32, /* bitsize */
840 true, /* pc_relative */
841 0, /* bitpos */
842 complain_overflow_signed, /* complain_on_overflow */
843 0, /* special_function */
844 "R_REL_16", /* name */
845 true, /* partial_inplace */
846 0xffffffff, /* src_mask */
847 0xffffffff, /* dst_mask */
848 false) /* pcrel_offset */
849 };
850
851 void
852 _bfd_xcoff_rtype2howto (relent, internal)
853 arelent *relent;
854 struct internal_reloc *internal;
855 {
856 relent->howto = xcoff_howto_table + internal->r_type;
857
858 if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1
859 && (internal->r_type
860 < sizeof(xcoff_howto_table_16)/sizeof(xcoff_howto_table_16[0])))
861 relent->howto = xcoff_howto_table_16 + internal->r_type;
862
863 /* The r_size field of an XCOFF reloc encodes the bitsize of the
864 relocation, as well as indicating whether it is signed or not.
865 Doublecheck that the relocation information gathered from the
866 type matches this information. */
867 if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1)
868 abort ();
869 #if 0
870 if ((internal->r_size & 0x80) != 0
871 ? (relent->howto->complain_on_overflow != complain_overflow_signed)
872 : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
873 abort ();
874 #endif
875 }
876
877 reloc_howto_type *
878 _bfd_xcoff_reloc_type_lookup (abfd, code)
879 bfd *abfd ATTRIBUTE_UNUSED;
880 bfd_reloc_code_real_type code;
881 {
882 switch (code)
883 {
884 case BFD_RELOC_PPC_B26:
885 return &xcoff_howto_table[0xa];
886 case BFD_RELOC_PPC_BA26:
887 return &xcoff_howto_table[8];
888 case BFD_RELOC_PPC_TOC16:
889 return &xcoff_howto_table[3];
890 case BFD_RELOC_32:
891 case BFD_RELOC_CTOR:
892 return &xcoff_howto_table[0];
893 case BFD_RELOC_64:
894 return &xcoff_howto_table[0x1c];
895 default:
896 return NULL;
897 }
898 }
899
900 #define SELECT_RELOC(internal, howto) \
901 { \
902 internal.r_type = howto->type; \
903 internal.r_size = \
904 ((howto->complain_on_overflow == complain_overflow_signed \
905 ? 0x80 \
906 : 0) \
907 | (howto->bitsize - 1)); \
908 }
909 \f
910 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
911
912 #define COFF_LONG_FILENAMES
913
914 #define RTYPE2HOWTO(cache_ptr, dst) _bfd_xcoff_rtype2howto (cache_ptr, dst)
915
916 #define coff_SWAP_sym_in xcoff_swap_sym_in
917 #define coff_SWAP_sym_out xcoff_swap_sym_out
918 #define coff_SWAP_aux_in xcoff_swap_aux_in
919 #define coff_SWAP_aux_out xcoff_swap_aux_out
920 #define coff_mkobject _bfd_xcoff_mkobject
921 #define coff_bfd_copy_private_bfd_data _bfd_xcoff_copy_private_bfd_data
922 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
923 #define coff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
924 #define coff_relocate_section _bfd_ppc_xcoff_relocate_section
925
926 #include "coffcode.h"
927 \f
928 /* XCOFF archive support. The original version of this code was by
929 Damon A. Permezel. It was enhanced to permit cross support, and
930 writing archive files, by Ian Lance Taylor, Cygnus Support.
931
932 XCOFF uses its own archive format. Everything is hooked together
933 with file offset links, so it is possible to rapidly update an
934 archive in place. Of course, we don't do that. An XCOFF archive
935 has a real file header, not just an ARMAG string. The structure of
936 the file header and of each archive header appear below.
937
938 An XCOFF archive also has a member table, which is a list of
939 elements in the archive (you can get that by looking through the
940 linked list, but you have to read a lot more of the file). The
941 member table has a normal archive header with an empty name. It is
942 normally (and perhaps must be) the second to last entry in the
943 archive. The member table data is almost printable ASCII. It
944 starts with a 12 character decimal string which is the number of
945 entries in the table. For each entry it has a 12 character decimal
946 string which is the offset in the archive of that member. These
947 entries are followed by a series of null terminated strings which
948 are the member names for each entry.
949
950 Finally, an XCOFF archive has a global symbol table, which is what
951 we call the armap. The global symbol table has a normal archive
952 header with an empty name. It is normally (and perhaps must be)
953 the last entry in the archive. The contents start with a four byte
954 binary number which is the number of entries. This is followed by
955 a that many four byte binary numbers; each is the file offset of an
956 entry in the archive. These numbers are followed by a series of
957 null terminated strings, which are symbol names.
958
959 AIX 4.3 introduced a new archive format which can handle larger
960 files and also 32- and 64-bit objects in the same archive. The
961 things said above remain true except that there is now more than
962 one global symbol table. The one is used to index 32-bit objects,
963 the other for 64-bit objects.
964
965 The new archives (recognizable by the new ARMAG string) has larger
966 field lengths so that we cannot really share any code. Also we have
967 to take care that we are not generating the new form of archives
968 on AIX 4.2 or earlier systems. */
969
970 /* XCOFF archives use this as a magic string. Note that both strings
971 have the same length. */
972
973 #define XCOFFARMAG "<aiaff>\012"
974 #define XCOFFARMAGBIG "<bigaf>\012"
975 #define SXCOFFARMAG 8
976
977 /* This terminates an XCOFF archive member name. */
978
979 #define XCOFFARFMAG "`\012"
980 #define SXCOFFARFMAG 2
981
982 /* XCOFF archives start with this (printable) structure. */
983
984 struct xcoff_ar_file_hdr
985 {
986 /* Magic string. */
987 char magic[SXCOFFARMAG];
988
989 /* Offset of the member table (decimal ASCII string). */
990 char memoff[12];
991
992 /* Offset of the global symbol table (decimal ASCII string). */
993 char symoff[12];
994
995 /* Offset of the first member in the archive (decimal ASCII string). */
996 char firstmemoff[12];
997
998 /* Offset of the last member in the archive (decimal ASCII string). */
999 char lastmemoff[12];
1000
1001 /* Offset of the first member on the free list (decimal ASCII
1002 string). */
1003 char freeoff[12];
1004 };
1005
1006 #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
1007
1008 /* This is the equivalent data structure for the big archive format. */
1009
1010 struct xcoff_ar_file_hdr_big
1011 {
1012 /* Magic string. */
1013 char magic[SXCOFFARMAG];
1014
1015 /* Offset of the member table (decimal ASCII string). */
1016 char memoff[20];
1017
1018 /* Offset of the global symbol table for 32-bit objects (decimal ASCII
1019 string). */
1020 char symoff[20];
1021
1022 /* Offset of the global symbol table for 64-bit objects (decimal ASCII
1023 string). */
1024 char symoff64[20];
1025
1026 /* Offset of the first member in the archive (decimal ASCII string). */
1027 char firstmemoff[20];
1028
1029 /* Offset of the last member in the archive (decimal ASCII string). */
1030 char lastmemoff[20];
1031
1032 /* Offset of the first member on the free list (decimal ASCII
1033 string). */
1034 char freeoff[20];
1035 };
1036
1037 #define SIZEOF_AR_FILE_HDR_BIG (6 * 20 + SXCOFFARMAG)
1038
1039
1040 /* Each XCOFF archive member starts with this (printable) structure. */
1041
1042 struct xcoff_ar_hdr
1043 {
1044 /* File size not including the header (decimal ASCII string). */
1045 char size[12];
1046
1047 /* File offset of next archive member (decimal ASCII string). */
1048 char nextoff[12];
1049
1050 /* File offset of previous archive member (decimal ASCII string). */
1051 char prevoff[12];
1052
1053 /* File mtime (decimal ASCII string). */
1054 char date[12];
1055
1056 /* File UID (decimal ASCII string). */
1057 char uid[12];
1058
1059 /* File GID (decimal ASCII string). */
1060 char gid[12];
1061
1062 /* File mode (octal ASCII string). */
1063 char mode[12];
1064
1065 /* Length of file name (decimal ASCII string). */
1066 char namlen[4];
1067
1068 /* This structure is followed by the file name. The length of the
1069 name is given in the namlen field. If the length of the name is
1070 odd, the name is followed by a null byte. The name and optional
1071 null byte are followed by XCOFFARFMAG, which is not included in
1072 namlen. The contents of the archive member follow; the number of
1073 bytes is given in the size field. */
1074 };
1075
1076 #define SIZEOF_AR_HDR (7 * 12 + 4)
1077
1078 /* The equivalent for the big archive format. */
1079
1080 struct xcoff_ar_hdr_big
1081 {
1082 /* File size not including the header (decimal ASCII string). */
1083 char size[20];
1084
1085 /* File offset of next archive member (decimal ASCII string). */
1086 char nextoff[20];
1087
1088 /* File offset of previous archive member (decimal ASCII string). */
1089 char prevoff[20];
1090
1091 /* File mtime (decimal ASCII string). */
1092 char date[12];
1093
1094 /* File UID (decimal ASCII string). */
1095 char uid[12];
1096
1097 /* File GID (decimal ASCII string). */
1098 char gid[12];
1099
1100 /* File mode (octal ASCII string). */
1101 char mode[12];
1102
1103 /* Length of file name (decimal ASCII string). */
1104 char namlen[4];
1105
1106 /* This structure is followed by the file name. The length of the
1107 name is given in the namlen field. If the length of the name is
1108 odd, the name is followed by a null byte. The name and optional
1109 null byte are followed by XCOFFARFMAG, which is not included in
1110 namlen. The contents of the archive member follow; the number of
1111 bytes is given in the size field. */
1112 };
1113
1114 #define SIZEOF_AR_HDR_BIG (3 * 20 + 4 * 12 + 4)
1115
1116
1117 /* We often have to distinguish between the old and big file format.
1118 Make it a bit cleaner. We can use `xcoff_ardata' here because the
1119 `hdr' member has the same size and position in both formats. */
1120 #define xcoff_big_format_p(abfd) \
1121 (xcoff_ardata (abfd)->magic[1] == 'b')
1122
1123 /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
1124 artdata structure. Similar for the big archive. */
1125 #define xcoff_ardata(abfd) \
1126 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
1127 #define xcoff_ardata_big(abfd) \
1128 ((struct xcoff_ar_file_hdr_big *) bfd_ardata (abfd)->tdata)
1129
1130 /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
1131 archive element. Similar for the big archive. */
1132 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
1133 #define arch_xhdr(bfd) \
1134 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
1135 #define arch_xhdr_big(bfd) \
1136 ((struct xcoff_ar_hdr_big *) arch_eltdata (bfd)->arch_header)
1137
1138 /* XCOFF archives do not have anything which corresponds to an
1139 extended name table. */
1140
1141 #define _bfd_xcoff_slurp_extended_name_table bfd_false
1142 #define _bfd_xcoff_construct_extended_name_table \
1143 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
1144 bfd_false)
1145 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
1146
1147 /* We can use the standard get_elt_at_index routine. */
1148
1149 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
1150
1151 /* XCOFF archives do not have a timestamp. */
1152
1153 #define _bfd_xcoff_update_armap_timestamp bfd_true
1154
1155 /* Read in the armap of an XCOFF archive. */
1156
1157 boolean
1158 _bfd_xcoff_slurp_armap (abfd)
1159 bfd *abfd;
1160 {
1161 file_ptr off;
1162 size_t namlen;
1163 bfd_size_type sz;
1164 bfd_byte *contents, *cend;
1165 unsigned int c, i;
1166 carsym *arsym;
1167 bfd_byte *p;
1168
1169 if (xcoff_ardata (abfd) == NULL)
1170 {
1171 bfd_has_map (abfd) = false;
1172 return true;
1173 }
1174
1175 if (! xcoff_big_format_p (abfd))
1176 {
1177 /* This is for the old format. */
1178 struct xcoff_ar_hdr hdr;
1179
1180 off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
1181 if (off == 0)
1182 {
1183 bfd_has_map (abfd) = false;
1184 return true;
1185 }
1186
1187 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1188 return false;
1189
1190 /* The symbol table starts with a normal archive header. */
1191 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1192 return false;
1193
1194 /* Skip the name (normally empty). */
1195 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1196 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1197 return false;
1198
1199 sz = strtol (hdr.size, (char **) NULL, 10);
1200 }
1201 else
1202 {
1203 /* This is for the new format. */
1204 struct xcoff_ar_hdr_big hdr;
1205
1206 off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1207 if (off == 0)
1208 {
1209 bfd_has_map (abfd) = false;
1210 return true;
1211 }
1212
1213 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1214 return false;
1215
1216 /* The symbol table starts with a normal archive header. */
1217 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1218 != SIZEOF_AR_HDR_BIG)
1219 return false;
1220
1221 /* Skip the name (normally empty). */
1222 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1223 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1224 return false;
1225
1226 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1227 machines) since the field width is 20 and there numbers with more
1228 than 32 bits can be represented. */
1229 sz = strtol (hdr.size, (char **) NULL, 10);
1230 }
1231
1232 /* Read in the entire symbol table. */
1233 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1234 if (contents == NULL)
1235 return false;
1236 if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1237 return false;
1238
1239 /* The symbol table starts with a four byte count. */
1240 c = bfd_h_get_32 (abfd, contents);
1241
1242 if (c * 4 >= sz)
1243 {
1244 bfd_set_error (bfd_error_bad_value);
1245 return false;
1246 }
1247
1248 bfd_ardata (abfd)->symdefs = ((carsym *)
1249 bfd_alloc (abfd, c * sizeof (carsym)));
1250 if (bfd_ardata (abfd)->symdefs == NULL)
1251 return false;
1252
1253 /* After the count comes a list of four byte file offsets. */
1254 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1255 i < c;
1256 ++i, ++arsym, p += 4)
1257 arsym->file_offset = bfd_h_get_32 (abfd, p);
1258
1259 /* After the file offsets come null terminated symbol names. */
1260 cend = contents + sz;
1261 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1262 i < c;
1263 ++i, ++arsym, p += strlen ((char *) p) + 1)
1264 {
1265 if (p >= cend)
1266 {
1267 bfd_set_error (bfd_error_bad_value);
1268 return false;
1269 }
1270 arsym->name = (char *) p;
1271 }
1272
1273 bfd_ardata (abfd)->symdef_count = c;
1274 bfd_has_map (abfd) = true;
1275
1276 return true;
1277 }
1278
1279 /* See if this is an XCOFF archive. */
1280
1281 const bfd_target *
1282 _bfd_xcoff_archive_p (abfd)
1283 bfd *abfd;
1284 {
1285 char magic[SXCOFFARMAG];
1286
1287 if (bfd_read ((PTR) magic, SXCOFFARMAG, 1, abfd) != SXCOFFARMAG)
1288 {
1289 if (bfd_get_error () != bfd_error_system_call)
1290 bfd_set_error (bfd_error_wrong_format);
1291 return NULL;
1292 }
1293
1294 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1295 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1296 {
1297 bfd_set_error (bfd_error_wrong_format);
1298 return NULL;
1299 }
1300
1301 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
1302 involves a cast, we can't do it as the left operand of
1303 assignment. */
1304 abfd->tdata.aout_ar_data =
1305 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
1306
1307 if (bfd_ardata (abfd) == (struct artdata *) NULL)
1308 return NULL;
1309
1310 bfd_ardata (abfd)->cache = NULL;
1311 bfd_ardata (abfd)->archive_head = NULL;
1312 bfd_ardata (abfd)->symdefs = NULL;
1313 bfd_ardata (abfd)->extended_names = NULL;
1314
1315 /* Now handle the two formats. */
1316 if (magic[1] != 'b')
1317 {
1318 /* This is the old format. */
1319 struct xcoff_ar_file_hdr hdr;
1320
1321 /* Copy over the magic string. */
1322 memcpy (hdr.magic, magic, SXCOFFARMAG);
1323
1324 /* Now read the rest of the file header. */
1325 if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR - SXCOFFARMAG, 1,
1326 abfd) != SIZEOF_AR_FILE_HDR - SXCOFFARMAG)
1327 {
1328 if (bfd_get_error () != bfd_error_system_call)
1329 bfd_set_error (bfd_error_wrong_format);
1330 return NULL;
1331 }
1332
1333 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1334 (char **) NULL, 10);
1335
1336 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
1337 if (bfd_ardata (abfd)->tdata == NULL)
1338 return NULL;
1339
1340 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1341 }
1342 else
1343 {
1344 /* This is the new format. */
1345 struct xcoff_ar_file_hdr_big hdr;
1346
1347 /* Copy over the magic string. */
1348 memcpy (hdr.magic, magic, SXCOFFARMAG);
1349
1350 /* Now read the rest of the file header. */
1351 if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG, 1,
1352 abfd) != SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG)
1353 {
1354 if (bfd_get_error () != bfd_error_system_call)
1355 bfd_set_error (bfd_error_wrong_format);
1356 return NULL;
1357 }
1358
1359 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1360 machines) since the field width is 20 and there numbers with more
1361 than 32 bits can be represented. */
1362 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1363 (char **) NULL, 10);
1364
1365 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR_BIG);
1366 if (bfd_ardata (abfd)->tdata == NULL)
1367 return NULL;
1368
1369 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1370 }
1371
1372 if (! _bfd_xcoff_slurp_armap (abfd))
1373 {
1374 bfd_release (abfd, bfd_ardata (abfd));
1375 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
1376 return NULL;
1377 }
1378
1379 return abfd->xvec;
1380 }
1381
1382 /* Read the archive header in an XCOFF archive. */
1383
1384 PTR
1385 _bfd_xcoff_read_ar_hdr (abfd)
1386 bfd *abfd;
1387 {
1388 size_t namlen;
1389 struct areltdata *ret;
1390
1391 ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
1392 if (ret == NULL)
1393 return NULL;
1394
1395 if (! xcoff_big_format_p (abfd))
1396 {
1397 struct xcoff_ar_hdr hdr;
1398 struct xcoff_ar_hdr *hdrp;
1399
1400 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1401 {
1402 free (ret);
1403 return NULL;
1404 }
1405
1406 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1407 hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd,
1408 SIZEOF_AR_HDR + namlen + 1);
1409 if (hdrp == NULL)
1410 {
1411 free (ret);
1412 return NULL;
1413 }
1414 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1415 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
1416 {
1417 free (ret);
1418 return NULL;
1419 }
1420 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1421
1422 ret->arch_header = (char *) hdrp;
1423 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1424 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1425 }
1426 else
1427 {
1428 struct xcoff_ar_hdr_big hdr;
1429 struct xcoff_ar_hdr_big *hdrp;
1430
1431 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1432 != SIZEOF_AR_HDR_BIG)
1433 {
1434 free (ret);
1435 return NULL;
1436 }
1437
1438 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1439 hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd,
1440 SIZEOF_AR_HDR_BIG
1441 + namlen + 1);
1442 if (hdrp == NULL)
1443 {
1444 free (ret);
1445 return NULL;
1446 }
1447 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1448 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, 1, namlen, abfd) != namlen)
1449 {
1450 free (ret);
1451 return NULL;
1452 }
1453 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1454
1455 ret->arch_header = (char *) hdrp;
1456 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1457 machines) since the field width is 20 and there numbers with more
1458 than 32 bits can be represented. */
1459 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1460 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1461 }
1462
1463 /* Skip over the XCOFFARFMAG at the end of the file name. */
1464 if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1465 return NULL;
1466
1467 return (PTR) ret;
1468 }
1469
1470 /* Open the next element in an XCOFF archive. */
1471
1472 bfd *
1473 _bfd_xcoff_openr_next_archived_file (archive, last_file)
1474 bfd *archive;
1475 bfd *last_file;
1476 {
1477 file_ptr filestart;
1478
1479 if (xcoff_ardata (archive) == NULL)
1480 {
1481 bfd_set_error (bfd_error_invalid_operation);
1482 return NULL;
1483 }
1484
1485 if (! xcoff_big_format_p (archive))
1486 {
1487 if (last_file == NULL)
1488 filestart = bfd_ardata (archive)->first_file_filepos;
1489 else
1490 filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1491 10);
1492
1493 if (filestart == 0
1494 || filestart == strtol (xcoff_ardata (archive)->memoff,
1495 (char **) NULL, 10)
1496 || filestart == strtol (xcoff_ardata (archive)->symoff,
1497 (char **) NULL, 10))
1498 {
1499 bfd_set_error (bfd_error_no_more_archived_files);
1500 return NULL;
1501 }
1502 }
1503 else
1504 {
1505 if (last_file == NULL)
1506 filestart = bfd_ardata (archive)->first_file_filepos;
1507 else
1508 /* XXX These actually have to be a calls to strtoll (at least
1509 on 32-bit machines) since the fields's width is 20 and
1510 there numbers with more than 32 bits can be represented. */
1511 filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1512 10);
1513
1514 /* XXX These actually have to be calls to strtoll (at least on 32-bit
1515 machines) since the fields's width is 20 and there numbers with more
1516 than 32 bits can be represented. */
1517 if (filestart == 0
1518 || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1519 (char **) NULL, 10)
1520 || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1521 (char **) NULL, 10))
1522 {
1523 bfd_set_error (bfd_error_no_more_archived_files);
1524 return NULL;
1525 }
1526 }
1527
1528 return _bfd_get_elt_at_filepos (archive, filestart);
1529 }
1530
1531 /* Stat an element in an XCOFF archive. */
1532
1533 int
1534 _bfd_xcoff_generic_stat_arch_elt (abfd, s)
1535 bfd *abfd;
1536 struct stat *s;
1537 {
1538 if (abfd->arelt_data == NULL)
1539 {
1540 bfd_set_error (bfd_error_invalid_operation);
1541 return -1;
1542 }
1543
1544 if (! xcoff_big_format_p (abfd))
1545 {
1546 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1547
1548 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1549 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1550 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1551 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1552 s->st_size = arch_eltdata (abfd)->parsed_size;
1553 }
1554 else
1555 {
1556 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1557
1558 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1559 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1560 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1561 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1562 s->st_size = arch_eltdata (abfd)->parsed_size;
1563 }
1564
1565 return 0;
1566 }
1567
1568 /* Normalize a file name for inclusion in an archive. */
1569
1570 static const char *
1571 normalize_filename (abfd)
1572 bfd *abfd;
1573 {
1574 const char *file;
1575 const char *filename;
1576
1577 file = bfd_get_filename (abfd);
1578 filename = strrchr (file, '/');
1579 if (filename != NULL)
1580 filename++;
1581 else
1582 filename = file;
1583 return filename;
1584 }
1585
1586 /* Write out an XCOFF armap. */
1587
1588 /*ARGSUSED*/
1589 static boolean
1590 xcoff_write_armap_old (abfd, elength, map, orl_count, stridx)
1591 bfd *abfd;
1592 unsigned int elength ATTRIBUTE_UNUSED;
1593 struct orl *map;
1594 unsigned int orl_count;
1595 int stridx;
1596 {
1597 struct xcoff_ar_hdr hdr;
1598 char *p;
1599 unsigned char buf[4];
1600 bfd *sub;
1601 file_ptr fileoff;
1602 unsigned int i;
1603
1604 memset (&hdr, 0, sizeof hdr);
1605 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1606 sprintf (hdr.nextoff, "%d", 0);
1607 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1608 sprintf (hdr.date, "%d", 0);
1609 sprintf (hdr.uid, "%d", 0);
1610 sprintf (hdr.gid, "%d", 0);
1611 sprintf (hdr.mode, "%d", 0);
1612 sprintf (hdr.namlen, "%d", 0);
1613
1614 /* We need spaces, not null bytes, in the header. */
1615 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1616 if (*p == '\0')
1617 *p = ' ';
1618
1619 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
1620 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1621 return false;
1622
1623 bfd_h_put_32 (abfd, orl_count, buf);
1624 if (bfd_write (buf, 1, 4, abfd) != 4)
1625 return false;
1626
1627 sub = abfd->archive_head;
1628 fileoff = SIZEOF_AR_FILE_HDR;
1629 i = 0;
1630 while (sub != NULL && i < orl_count)
1631 {
1632 size_t namlen;
1633
1634 while (((bfd *) (map[i]).pos) == sub)
1635 {
1636 bfd_h_put_32 (abfd, fileoff, buf);
1637 if (bfd_write (buf, 1, 4, abfd) != 4)
1638 return false;
1639 ++i;
1640 }
1641 namlen = strlen (normalize_filename (sub));
1642 namlen = (namlen + 1) &~ 1;
1643 fileoff += (SIZEOF_AR_HDR
1644 + namlen
1645 + SXCOFFARFMAG
1646 + arelt_size (sub));
1647 fileoff = (fileoff + 1) &~ 1;
1648 sub = sub->next;
1649 }
1650
1651 for (i = 0; i < orl_count; i++)
1652 {
1653 const char *name;
1654 size_t namlen;
1655
1656 name = *map[i].name;
1657 namlen = strlen (name);
1658 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1659 return false;
1660 }
1661
1662 if ((stridx & 1) != 0)
1663 {
1664 char b;
1665
1666 b = '\0';
1667 if (bfd_write (&b, 1, 1, abfd) != 1)
1668 return false;
1669 }
1670
1671 return true;
1672 }
1673
1674 /*ARGSUSED*/
1675 static boolean
1676 xcoff_write_armap_big (abfd, elength, map, orl_count, stridx)
1677 bfd *abfd;
1678 unsigned int elength ATTRIBUTE_UNUSED;
1679 struct orl *map;
1680 unsigned int orl_count;
1681 int stridx;
1682 {
1683 struct xcoff_ar_hdr_big hdr;
1684 char *p;
1685 unsigned char buf[4];
1686 bfd *sub;
1687 file_ptr fileoff;
1688 unsigned int i;
1689
1690 memset (&hdr, 0, sizeof hdr);
1691 /* XXX This call actually should use %lld (at least on 32-bit
1692 machines) since the fields's width is 20 and there numbers with
1693 more than 32 bits can be represented. */
1694 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1695 sprintf (hdr.nextoff, "%d", 0);
1696 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1697 sprintf (hdr.date, "%d", 0);
1698 sprintf (hdr.uid, "%d", 0);
1699 sprintf (hdr.gid, "%d", 0);
1700 sprintf (hdr.mode, "%d", 0);
1701 sprintf (hdr.namlen, "%d", 0);
1702
1703 /* We need spaces, not null bytes, in the header. */
1704 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR_BIG; p++)
1705 if (*p == '\0')
1706 *p = ' ';
1707
1708 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd) != SIZEOF_AR_HDR_BIG
1709 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1710 return false;
1711
1712 bfd_h_put_32 (abfd, orl_count, buf);
1713 if (bfd_write (buf, 1, 4, abfd) != 4)
1714 return false;
1715
1716 sub = abfd->archive_head;
1717 fileoff = SIZEOF_AR_FILE_HDR_BIG;
1718 i = 0;
1719 while (sub != NULL && i < orl_count)
1720 {
1721 size_t namlen;
1722
1723 while (((bfd *) (map[i]).pos) == sub)
1724 {
1725 bfd_h_put_32 (abfd, fileoff, buf);
1726 if (bfd_write (buf, 1, 4, abfd) != 4)
1727 return false;
1728 ++i;
1729 }
1730 namlen = strlen (normalize_filename (sub));
1731 namlen = (namlen + 1) &~ 1;
1732 fileoff += (SIZEOF_AR_HDR_BIG
1733 + namlen
1734 + SXCOFFARFMAG
1735 + arelt_size (sub));
1736 fileoff = (fileoff + 1) &~ 1;
1737 sub = sub->next;
1738 }
1739
1740 for (i = 0; i < orl_count; i++)
1741 {
1742 const char *name;
1743 size_t namlen;
1744
1745 name = *map[i].name;
1746 namlen = strlen (name);
1747 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1748 return false;
1749 }
1750
1751 if ((stridx & 1) != 0)
1752 {
1753 char b;
1754
1755 b = '\0';
1756 if (bfd_write (&b, 1, 1, abfd) != 1)
1757 return false;
1758 }
1759
1760 return true;
1761 }
1762
1763 /*ARGSUSED*/
1764 boolean
1765 _bfd_xcoff_write_armap (abfd, elength, map, orl_count, stridx)
1766 bfd *abfd;
1767 unsigned int elength ATTRIBUTE_UNUSED;
1768 struct orl *map;
1769 unsigned int orl_count;
1770 int stridx;
1771 {
1772 if (! xcoff_big_format_p (abfd))
1773 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
1774 else
1775 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
1776 }
1777
1778 /* Write out an XCOFF archive. We always write an entire archive,
1779 rather than fussing with the freelist and so forth. */
1780
1781 static boolean
1782 xcoff_write_archive_contents_old (abfd)
1783 bfd *abfd;
1784 {
1785 struct xcoff_ar_file_hdr fhdr;
1786 size_t count;
1787 size_t total_namlen;
1788 file_ptr *offsets;
1789 boolean makemap;
1790 boolean hasobjects;
1791 file_ptr prevoff, nextoff;
1792 bfd *sub;
1793 unsigned int i;
1794 struct xcoff_ar_hdr ahdr;
1795 bfd_size_type size;
1796 char *p;
1797 char decbuf[13];
1798
1799 memset (&fhdr, 0, sizeof fhdr);
1800 strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1801 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1802 sprintf (fhdr.freeoff, "%d", 0);
1803
1804 count = 0;
1805 total_namlen = 0;
1806 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1807 {
1808 ++count;
1809 total_namlen += strlen (normalize_filename (sub)) + 1;
1810 }
1811 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1812 if (offsets == NULL)
1813 return false;
1814
1815 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1816 return false;
1817
1818 makemap = bfd_has_map (abfd);
1819 hasobjects = false;
1820 prevoff = 0;
1821 nextoff = SIZEOF_AR_FILE_HDR;
1822 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1823 {
1824 const char *name;
1825 size_t namlen;
1826 struct xcoff_ar_hdr *ahdrp;
1827 bfd_size_type remaining;
1828
1829 if (makemap && ! hasobjects)
1830 {
1831 if (bfd_check_format (sub, bfd_object))
1832 hasobjects = true;
1833 }
1834
1835 name = normalize_filename (sub);
1836 namlen = strlen (name);
1837
1838 if (sub->arelt_data != NULL)
1839 ahdrp = arch_xhdr (sub);
1840 else
1841 ahdrp = NULL;
1842
1843 if (ahdrp == NULL)
1844 {
1845 struct stat s;
1846
1847 memset (&ahdr, 0, sizeof ahdr);
1848 ahdrp = &ahdr;
1849 if (stat (bfd_get_filename (sub), &s) != 0)
1850 {
1851 bfd_set_error (bfd_error_system_call);
1852 return false;
1853 }
1854
1855 sprintf (ahdrp->size, "%ld", (long) s.st_size);
1856 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1857 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1858 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1859 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1860
1861 if (sub->arelt_data == NULL)
1862 {
1863 sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
1864 if (sub->arelt_data == NULL)
1865 return false;
1866 }
1867
1868 arch_eltdata (sub)->parsed_size = s.st_size;
1869 }
1870
1871 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1872 sprintf (ahdrp->namlen, "%ld", (long) namlen);
1873
1874 /* If the length of the name is odd, we write out the null byte
1875 after the name as well. */
1876 namlen = (namlen + 1) &~ 1;
1877
1878 remaining = arelt_size (sub);
1879 size = (SIZEOF_AR_HDR
1880 + namlen
1881 + SXCOFFARFMAG
1882 + remaining);
1883
1884 BFD_ASSERT (nextoff == bfd_tell (abfd));
1885
1886 offsets[i] = nextoff;
1887
1888 prevoff = nextoff;
1889 nextoff += size + (size & 1);
1890
1891 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1892
1893 /* We need spaces, not null bytes, in the header. */
1894 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1895 if (*p == '\0')
1896 *p = ' ';
1897
1898 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1899 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1900 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1901 != SXCOFFARFMAG))
1902 return false;
1903
1904 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1905 return false;
1906 while (remaining != 0)
1907 {
1908 bfd_size_type amt;
1909 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1910
1911 amt = sizeof buffer;
1912 if (amt > remaining)
1913 amt = remaining;
1914 if (bfd_read (buffer, 1, amt, sub) != amt
1915 || bfd_write (buffer, 1, amt, abfd) != amt)
1916 return false;
1917 remaining -= amt;
1918 }
1919
1920 if ((size & 1) != 0)
1921 {
1922 bfd_byte b;
1923
1924 b = '\0';
1925 if (bfd_write (&b, 1, 1, abfd) != 1)
1926 return false;
1927 }
1928 }
1929
1930 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1931
1932 /* Write out the member table. */
1933
1934 BFD_ASSERT (nextoff == bfd_tell (abfd));
1935 sprintf (fhdr.memoff, "%ld", (long) nextoff);
1936
1937 memset (&ahdr, 0, sizeof ahdr);
1938 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1939 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1940 sprintf (ahdr.date, "%d", 0);
1941 sprintf (ahdr.uid, "%d", 0);
1942 sprintf (ahdr.gid, "%d", 0);
1943 sprintf (ahdr.mode, "%d", 0);
1944 sprintf (ahdr.namlen, "%d", 0);
1945
1946 size = (SIZEOF_AR_HDR
1947 + 12
1948 + count * 12
1949 + total_namlen
1950 + SXCOFFARFMAG);
1951
1952 prevoff = nextoff;
1953 nextoff += size + (size & 1);
1954
1955 if (makemap && hasobjects)
1956 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1957 else
1958 sprintf (ahdr.nextoff, "%d", 0);
1959
1960 /* We need spaces, not null bytes, in the header. */
1961 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
1962 if (*p == '\0')
1963 *p = ' ';
1964
1965 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1966 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1967 != SXCOFFARFMAG))
1968 return false;
1969
1970 sprintf (decbuf, "%-12ld", (long) count);
1971 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1972 return false;
1973 for (i = 0; i < count; i++)
1974 {
1975 sprintf (decbuf, "%-12ld", (long) offsets[i]);
1976 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1977 return false;
1978 }
1979 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1980 {
1981 const char *name;
1982 size_t namlen;
1983
1984 name = normalize_filename (sub);
1985 namlen = strlen (name);
1986 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
1987 return false;
1988 }
1989 if ((size & 1) != 0)
1990 {
1991 bfd_byte b;
1992
1993 b = '\0';
1994 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
1995 return false;
1996 }
1997
1998 /* Write out the armap, if appropriate. */
1999
2000 if (! makemap || ! hasobjects)
2001 sprintf (fhdr.symoff, "%d", 0);
2002 else
2003 {
2004 BFD_ASSERT (nextoff == bfd_tell (abfd));
2005 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2006 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2007 if (! _bfd_compute_and_write_armap (abfd, 0))
2008 return false;
2009 }
2010
2011 /* Write out the archive file header. */
2012
2013 /* We need spaces, not null bytes, in the header. */
2014 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2015 if (*p == '\0')
2016 *p = ' ';
2017
2018 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2019 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
2020 SIZEOF_AR_FILE_HDR))
2021 return false;
2022
2023 return true;
2024 }
2025
2026 static boolean
2027 xcoff_write_archive_contents_big (abfd)
2028 bfd *abfd;
2029 {
2030 struct xcoff_ar_file_hdr_big fhdr;
2031 size_t count;
2032 size_t total_namlen;
2033 file_ptr *offsets;
2034 boolean makemap;
2035 boolean hasobjects;
2036 file_ptr prevoff, nextoff;
2037 bfd *sub;
2038 unsigned int i;
2039 struct xcoff_ar_hdr_big ahdr;
2040 bfd_size_type size;
2041 char *p;
2042 char decbuf[13];
2043
2044 memset (&fhdr, 0, sizeof fhdr);
2045 strncpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2046 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR_BIG);
2047 sprintf (fhdr.freeoff, "%d", 0);
2048
2049 count = 0;
2050 total_namlen = 0;
2051 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2052 {
2053 ++count;
2054 total_namlen += strlen (normalize_filename (sub)) + 1;
2055 }
2056 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2057 if (offsets == NULL)
2058 return false;
2059
2060 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2061 return false;
2062
2063 makemap = bfd_has_map (abfd);
2064 hasobjects = false;
2065 prevoff = 0;
2066 nextoff = SIZEOF_AR_FILE_HDR_BIG;
2067 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
2068 {
2069 const char *name;
2070 size_t namlen;
2071 struct xcoff_ar_hdr_big *ahdrp;
2072 bfd_size_type remaining;
2073
2074 if (makemap && ! hasobjects)
2075 {
2076 if (bfd_check_format (sub, bfd_object))
2077 hasobjects = true;
2078 }
2079
2080 name = normalize_filename (sub);
2081 namlen = strlen (name);
2082
2083 if (sub->arelt_data != NULL)
2084 ahdrp = arch_xhdr_big (sub);
2085 else
2086 ahdrp = NULL;
2087
2088 if (ahdrp == NULL)
2089 {
2090 struct stat s;
2091
2092 memset (&ahdr, 0, sizeof ahdr);
2093 ahdrp = &ahdr;
2094 /* XXX This should actually be a call to stat64 (at least on
2095 32-bit machines). */
2096 if (stat (bfd_get_filename (sub), &s) != 0)
2097 {
2098 bfd_set_error (bfd_error_system_call);
2099 return false;
2100 }
2101
2102 /* XXX This call actually should use %lld (at least on 32-bit
2103 machines) since the fields's width is 20 and there numbers with
2104 more than 32 bits can be represented. */
2105 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2106 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2107 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2108 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2109 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2110
2111 if (sub->arelt_data == NULL)
2112 {
2113 sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
2114 if (sub->arelt_data == NULL)
2115 return false;
2116 }
2117
2118 arch_eltdata (sub)->parsed_size = s.st_size;
2119 }
2120
2121 /* XXX These calls actually should use %lld (at least on 32-bit
2122 machines) since the fields's width is 20 and there numbers with
2123 more than 32 bits can be represented. */
2124 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2125 sprintf (ahdrp->namlen, "%ld", (long) namlen);
2126
2127 /* If the length of the name is odd, we write out the null byte
2128 after the name as well. */
2129 namlen = (namlen + 1) &~ 1;
2130
2131 remaining = arelt_size (sub);
2132 size = (SIZEOF_AR_HDR_BIG
2133 + namlen
2134 + SXCOFFARFMAG
2135 + remaining);
2136
2137 BFD_ASSERT (nextoff == bfd_tell (abfd));
2138
2139 offsets[i] = nextoff;
2140
2141 prevoff = nextoff;
2142 nextoff += size + (size & 1);
2143
2144 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
2145
2146 /* We need spaces, not null bytes, in the header. */
2147 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR_BIG; p++)
2148 if (*p == '\0')
2149 *p = ' ';
2150
2151 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR_BIG, abfd)
2152 != SIZEOF_AR_HDR_BIG
2153 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
2154 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2155 != SXCOFFARFMAG))
2156 return false;
2157
2158 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
2159 return false;
2160 while (remaining != 0)
2161 {
2162 bfd_size_type amt;
2163 bfd_byte buffer[DEFAULT_BUFFERSIZE];
2164
2165 amt = sizeof buffer;
2166 if (amt > remaining)
2167 amt = remaining;
2168 if (bfd_read (buffer, 1, amt, sub) != amt
2169 || bfd_write (buffer, 1, amt, abfd) != amt)
2170 return false;
2171 remaining -= amt;
2172 }
2173
2174 if ((size & 1) != 0)
2175 {
2176 bfd_byte b;
2177
2178 b = '\0';
2179 if (bfd_write (&b, 1, 1, abfd) != 1)
2180 return false;
2181 }
2182 }
2183
2184 /* XXX This call actually should use %lld (at least on 32-bit
2185 machines) since the fields's width is 20 and there numbers with
2186 more than 32 bits can be represented. */
2187 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2188
2189 /* Write out the member table. */
2190
2191 BFD_ASSERT (nextoff == bfd_tell (abfd));
2192 /* XXX This call actually should use %lld (at least on 32-bit
2193 machines) since the fields's width is 20 and there numbers with
2194 more than 32 bits can be represented. */
2195 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2196
2197 memset (&ahdr, 0, sizeof ahdr);
2198 /* XXX The next two calls actually should use %lld (at least on 32-bit
2199 machines) since the fields's width is 20 and there numbers with
2200 more than 32 bits can be represented. */
2201 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
2202 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2203 sprintf (ahdr.date, "%d", 0);
2204 sprintf (ahdr.uid, "%d", 0);
2205 sprintf (ahdr.gid, "%d", 0);
2206 sprintf (ahdr.mode, "%d", 0);
2207 sprintf (ahdr.namlen, "%d", 0);
2208
2209 size = (SIZEOF_AR_HDR_BIG
2210 + 12
2211 + count * 12
2212 + total_namlen
2213 + SXCOFFARFMAG);
2214
2215 prevoff = nextoff;
2216 nextoff += size + (size & 1);
2217
2218 if (makemap && hasobjects)
2219 /* XXX This call actually should use %lld (at least on 32-bit
2220 machines) since the fields's width is 20 and there numbers with
2221 more than 32 bits can be represented. */
2222 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2223 else
2224 sprintf (ahdr.nextoff, "%d", 0);
2225
2226 /* We need spaces, not null bytes, in the header. */
2227 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR_BIG; p++)
2228 if (*p == '\0')
2229 *p = ' ';
2230
2231 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2232 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2233 != SXCOFFARFMAG))
2234 return false;
2235
2236 sprintf (decbuf, "%-12ld", (long) count);
2237 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2238 return false;
2239 for (i = 0; i < count; i++)
2240 {
2241 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2242 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2243 return false;
2244 }
2245 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2246 {
2247 const char *name;
2248 size_t namlen;
2249
2250 name = normalize_filename (sub);
2251 namlen = strlen (name);
2252 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2253 return false;
2254 }
2255 if ((size & 1) != 0)
2256 {
2257 bfd_byte b;
2258
2259 b = '\0';
2260 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2261 return false;
2262 }
2263
2264 /* Write out the armap, if appropriate. */
2265
2266 if (! makemap || ! hasobjects)
2267 sprintf (fhdr.symoff, "%d", 0);
2268 else
2269 {
2270 BFD_ASSERT (nextoff == bfd_tell (abfd));
2271 /* XXX This call actually should use %lld (at least on 32-bit
2272 machines) since the fields's width is 20 and there numbers with
2273 more than 32 bits can be represented. */
2274 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2275 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2276 if (! _bfd_compute_and_write_armap (abfd, 0))
2277 return false;
2278 }
2279
2280 /* Write out the archive file header. */
2281
2282 /* We need spaces, not null bytes, in the header. */
2283 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR_BIG; p++)
2284 if (*p == '\0')
2285 *p = ' ';
2286
2287 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2288 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR_BIG, 1, abfd) !=
2289 SIZEOF_AR_FILE_HDR_BIG))
2290 return false;
2291
2292 return true;
2293 }
2294
2295 boolean
2296 _bfd_xcoff_write_archive_contents (abfd)
2297 bfd *abfd;
2298 {
2299 if (! xcoff_big_format_p (abfd))
2300 return xcoff_write_archive_contents_old (abfd);
2301 else
2302 return xcoff_write_archive_contents_big (abfd);
2303 }
2304 \f
2305 /* We can't use the usual coff_sizeof_headers routine, because AIX
2306 always uses an a.out header. */
2307
2308 /*ARGSUSED*/
2309 int
2310 _bfd_xcoff_sizeof_headers (abfd, reloc)
2311 bfd *abfd;
2312 boolean reloc ATTRIBUTE_UNUSED;
2313 {
2314 int size;
2315
2316 size = FILHSZ;
2317 if (xcoff_data (abfd)->full_aouthdr)
2318 size += AOUTSZ;
2319 else
2320 size += SMALL_AOUTSZ;
2321 size += abfd->section_count * SCNHSZ;
2322 return size;
2323 }
2324 \f
2325 #define CORE_FILE_P _bfd_dummy_target
2326
2327 #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command
2328 #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal
2329 #define coff_core_file_matches_executable_p \
2330 _bfd_nocore_core_file_matches_executable_p
2331
2332 #ifdef AIX_CORE
2333 #undef CORE_FILE_P
2334 #define CORE_FILE_P rs6000coff_core_p
2335 extern const bfd_target * rs6000coff_core_p ();
2336 extern boolean rs6000coff_get_section_contents ();
2337 extern boolean rs6000coff_core_file_matches_executable_p ();
2338
2339 #undef coff_core_file_matches_executable_p
2340 #define coff_core_file_matches_executable_p \
2341 rs6000coff_core_file_matches_executable_p
2342
2343 extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd));
2344 #undef coff_core_file_failing_command
2345 #define coff_core_file_failing_command rs6000coff_core_file_failing_command
2346
2347 extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd));
2348 #undef coff_core_file_failing_signal
2349 #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal
2350
2351 #undef coff_get_section_contents
2352 #define coff_get_section_contents rs6000coff_get_section_contents
2353 #endif /* AIX_CORE */
2354
2355 #ifdef LYNX_CORE
2356
2357 #undef CORE_FILE_P
2358 #define CORE_FILE_P lynx_core_file_p
2359 extern const bfd_target *lynx_core_file_p PARAMS ((bfd *abfd));
2360
2361 extern boolean lynx_core_file_matches_executable_p PARAMS ((bfd *core_bfd,
2362 bfd *exec_bfd));
2363 #undef coff_core_file_matches_executable_p
2364 #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p
2365
2366 extern char *lynx_core_file_failing_command PARAMS ((bfd *abfd));
2367 #undef coff_core_file_failing_command
2368 #define coff_core_file_failing_command lynx_core_file_failing_command
2369
2370 extern int lynx_core_file_failing_signal PARAMS ((bfd *abfd));
2371 #undef coff_core_file_failing_signal
2372 #define coff_core_file_failing_signal lynx_core_file_failing_signal
2373
2374 #endif /* LYNX_CORE */
2375
2376 #define _bfd_xcoff_bfd_get_relocated_section_contents \
2377 coff_bfd_get_relocated_section_contents
2378 #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section
2379 #define _bfd_xcoff_bfd_gc_sections coff_bfd_gc_sections
2380 #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section
2381
2382 /* The transfer vector that leads the outside world to all of the above. */
2383
2384 const bfd_target
2385 #ifdef TARGET_SYM
2386 TARGET_SYM =
2387 #else
2388 rs6000coff_vec =
2389 #endif
2390 {
2391 #ifdef TARGET_NAME
2392 TARGET_NAME,
2393 #else
2394 "aixcoff-rs6000", /* name */
2395 #endif
2396 bfd_target_coff_flavour,
2397 BFD_ENDIAN_BIG, /* data byte order is big */
2398 BFD_ENDIAN_BIG, /* header byte order is big */
2399
2400 (HAS_RELOC | EXEC_P | /* object flags */
2401 HAS_LINENO | HAS_DEBUG | DYNAMIC |
2402 HAS_SYMS | HAS_LOCALS | WP_TEXT),
2403
2404 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2405 0, /* leading char */
2406 '/', /* ar_pad_char */
2407 15, /* ar_max_namelen??? FIXMEmgo */
2408
2409 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2410 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2411 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2412 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2413 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2414 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2415
2416 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2417 _bfd_xcoff_archive_p, CORE_FILE_P},
2418 {bfd_false, coff_mkobject, /* bfd_set_format */
2419 _bfd_generic_mkarchive, bfd_false},
2420 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2421 _bfd_xcoff_write_archive_contents, bfd_false},
2422
2423 BFD_JUMP_TABLE_GENERIC (coff),
2424 BFD_JUMP_TABLE_COPY (coff),
2425 BFD_JUMP_TABLE_CORE (coff),
2426 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
2427 BFD_JUMP_TABLE_SYMBOLS (coff),
2428 BFD_JUMP_TABLE_RELOCS (coff),
2429 BFD_JUMP_TABLE_WRITE (coff),
2430 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
2431 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
2432
2433 NULL,
2434
2435 COFF_SWAP_TABLE
2436 };