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