source: trunk/src/binutils/bfd/coff-rs6000.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

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