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