* Makefile.am (BFD64_BACKENDS, BFD64_BACKENDS_CFILES): Add
[deliverable/binutils-gdb.git] / bfd / coff-rs6000.c
CommitLineData
252b5132 1/* BFD back-end for IBM RS/6000 "XCOFF" files.
5f771d47
ILT
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
252b5132
RH
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
13This file is part of BFD, the Binary File Descriptor library.
14
15This program is free software; you can redistribute it and/or modify
16it under the terms of the GNU General Public License as published by
17the Free Software Foundation; either version 2 of the License, or
18(at your option) any later version.
19
20This program is distributed in the hope that it will be useful,
21but WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23GNU General Public License for more details.
24
25You should have received a copy of the GNU General Public License
26along with this program; if not, write to the Free Software
27Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28
252b5132
RH
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"
14958a43
CP
36#define TARGET_NAME "aixcoff-rs6000"
37#define TARGET_SYM rs6000coff_vec
38#include "xcoff.h"
39
252b5132
RH
40
41/* The main body of code is in coffcode.h. */
42
252b5132 43static const char *normalize_filename PARAMS ((bfd *));
14958a43 44
252b5132
RH
45/* We use our own tdata type. Its first field is the COFF tdata type,
46 so the COFF routines are compatible. */
47
7f6d05e8
CP
48boolean
49_bfd_xcoff_mkobject (abfd)
252b5132
RH
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
7f6d05e8
CP
79boolean
80_bfd_xcoff_copy_private_bfd_data (ibfd, obfd)
252b5132
RH
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
7f6d05e8
CP
127boolean
128_bfd_xcoff_is_local_label_name (abfd, name)
5f771d47
ILT
129 bfd *abfd ATTRIBUTE_UNUSED;
130 const char *name ATTRIBUTE_UNUSED;
252b5132
RH
131{
132 return false;
133}
7f6d05e8
CP
134\f
135
7f6d05e8 136
14958a43
CP
137void
138_bfd_xcoff_swap_sym_in (abfd, ext1, in1)
7f6d05e8
CP
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
14958a43
CP
165unsigned int
166_bfd_xcoff_swap_sym_out (abfd, inp, extp)
7f6d05e8
CP
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
14958a43
CP
200void
201_bfd_xcoff_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
7f6d05e8
CP
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
306end: ;
307 /* the semicolon is because MSVC doesn't like labels at
308 end of block. */
309
310}
311
312
313
14958a43
CP
314unsigned int
315_bfd_xcoff_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
7f6d05e8
CP
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 PUTWORD(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
408end:
409 return bfd_coff_auxesz (abfd);
410}
411
412
252b5132
RH
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
7f6d05e8
CP
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
424reloc_howto_type xcoff_howto_table[] =
252b5132
RH
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
5f771d47 531 EMPTY_HOWTO (7),
252b5132
RH
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
5f771d47 548 EMPTY_HOWTO (9),
252b5132
RH
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
5f771d47 565 EMPTY_HOWTO (0xb),
252b5132
RH
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
5f771d47 597 EMPTY_HOWTO (0xe),
252b5132
RH
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
5f771d47
ILT
614 EMPTY_HOWTO (0x10),
615 EMPTY_HOWTO (0x11),
252b5132
RH
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 16, /* 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 16, /* 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 */
7f6d05e8
CP
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 */
252b5132 778 false) /* pcrel_offset */
7f6d05e8 779
252b5132
RH
780};
781
5ea1af0d
GK
782/* These are the first two like the above but for 16-bit relocs. */
783static 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 */
7f6d05e8
CP
828 false) /* pcrel_offset */
829 };
5ea1af0d 830
7f6d05e8
CP
831void
832_bfd_xcoff_rtype2howto (relent, internal)
252b5132
RH
833 arelent *relent;
834 struct internal_reloc *internal;
835{
836 relent->howto = xcoff_howto_table + internal->r_type;
837
5ea1af0d
GK
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
252b5132
RH
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. */
847 if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1)
848 abort ();
849#if 0
850 if ((internal->r_size & 0x80) != 0
851 ? (relent->howto->complain_on_overflow != complain_overflow_signed)
852 : (relent->howto->complain_on_overflow != complain_overflow_bitfield))
853 abort ();
854#endif
855}
856
7f6d05e8
CP
857reloc_howto_type *
858_bfd_xcoff_reloc_type_lookup (abfd, code)
5f771d47 859 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
860 bfd_reloc_code_real_type code;
861{
862 switch (code)
863 {
864 case BFD_RELOC_PPC_B26:
865 return &xcoff_howto_table[0xa];
866 case BFD_RELOC_PPC_BA26:
867 return &xcoff_howto_table[8];
868 case BFD_RELOC_PPC_TOC16:
869 return &xcoff_howto_table[3];
870 case BFD_RELOC_32:
871 case BFD_RELOC_CTOR:
872 return &xcoff_howto_table[0];
7f6d05e8
CP
873 case BFD_RELOC_64:
874 return &xcoff_howto_table[0x1c];
252b5132
RH
875 default:
876 return NULL;
877 }
878}
879
252b5132
RH
880\f
881/* XCOFF archive support. The original version of this code was by
882 Damon A. Permezel. It was enhanced to permit cross support, and
883 writing archive files, by Ian Lance Taylor, Cygnus Support.
884
885 XCOFF uses its own archive format. Everything is hooked together
886 with file offset links, so it is possible to rapidly update an
887 archive in place. Of course, we don't do that. An XCOFF archive
888 has a real file header, not just an ARMAG string. The structure of
889 the file header and of each archive header appear below.
890
891 An XCOFF archive also has a member table, which is a list of
892 elements in the archive (you can get that by looking through the
893 linked list, but you have to read a lot more of the file). The
894 member table has a normal archive header with an empty name. It is
895 normally (and perhaps must be) the second to last entry in the
896 archive. The member table data is almost printable ASCII. It
897 starts with a 12 character decimal string which is the number of
898 entries in the table. For each entry it has a 12 character decimal
899 string which is the offset in the archive of that member. These
900 entries are followed by a series of null terminated strings which
901 are the member names for each entry.
902
903 Finally, an XCOFF archive has a global symbol table, which is what
904 we call the armap. The global symbol table has a normal archive
905 header with an empty name. It is normally (and perhaps must be)
906 the last entry in the archive. The contents start with a four byte
907 binary number which is the number of entries. This is followed by
908 a that many four byte binary numbers; each is the file offset of an
909 entry in the archive. These numbers are followed by a series of
5ea1af0d
GK
910 null terminated strings, which are symbol names.
911
912 AIX 4.3 introduced a new archive format which can handle larger
913 files and also 32- and 64-bit objects in the same archive. The
914 things said above remain true except that there is now more than
915 one global symbol table. The one is used to index 32-bit objects,
916 the other for 64-bit objects.
917
918 The new archives (recognizable by the new ARMAG string) has larger
919 field lengths so that we cannot really share any code. Also we have
920 to take care that we are not generating the new form of archives
921 on AIX 4.2 or earlier systems. */
252b5132 922
5ea1af0d
GK
923/* XCOFF archives use this as a magic string. Note that both strings
924 have the same length. */
252b5132 925
5ea1af0d
GK
926#define XCOFFARMAG "<aiaff>\012"
927#define XCOFFARMAGBIG "<bigaf>\012"
928#define SXCOFFARMAG 8
252b5132
RH
929
930/* This terminates an XCOFF archive member name. */
931
932#define XCOFFARFMAG "`\012"
933#define SXCOFFARFMAG 2
934
935/* XCOFF archives start with this (printable) structure. */
936
937struct xcoff_ar_file_hdr
938{
939 /* Magic string. */
940 char magic[SXCOFFARMAG];
941
942 /* Offset of the member table (decimal ASCII string). */
943 char memoff[12];
944
945 /* Offset of the global symbol table (decimal ASCII string). */
946 char symoff[12];
947
948 /* Offset of the first member in the archive (decimal ASCII string). */
949 char firstmemoff[12];
950
951 /* Offset of the last member in the archive (decimal ASCII string). */
952 char lastmemoff[12];
953
954 /* Offset of the first member on the free list (decimal ASCII
955 string). */
956 char freeoff[12];
957};
958
959#define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG)
960
5ea1af0d
GK
961/* This is the equivalent data structure for the big archive format. */
962
963struct xcoff_ar_file_hdr_big
964{
965 /* Magic string. */
966 char magic[SXCOFFARMAG];
967
968 /* Offset of the member table (decimal ASCII string). */
969 char memoff[20];
970
971 /* Offset of the global symbol table for 32-bit objects (decimal ASCII
972 string). */
973 char symoff[20];
974
975 /* Offset of the global symbol table for 64-bit objects (decimal ASCII
976 string). */
977 char symoff64[20];
978
979 /* Offset of the first member in the archive (decimal ASCII string). */
980 char firstmemoff[20];
981
982 /* Offset of the last member in the archive (decimal ASCII string). */
983 char lastmemoff[20];
984
985 /* Offset of the first member on the free list (decimal ASCII
986 string). */
987 char freeoff[20];
988};
989
990#define SIZEOF_AR_FILE_HDR_BIG (6 * 20 + SXCOFFARMAG)
991
992
252b5132
RH
993/* Each XCOFF archive member starts with this (printable) structure. */
994
995struct xcoff_ar_hdr
996{
997 /* File size not including the header (decimal ASCII string). */
998 char size[12];
999
1000 /* File offset of next archive member (decimal ASCII string). */
1001 char nextoff[12];
1002
1003 /* File offset of previous archive member (decimal ASCII string). */
1004 char prevoff[12];
1005
1006 /* File mtime (decimal ASCII string). */
1007 char date[12];
1008
1009 /* File UID (decimal ASCII string). */
1010 char uid[12];
1011
1012 /* File GID (decimal ASCII string). */
1013 char gid[12];
1014
1015 /* File mode (octal ASCII string). */
1016 char mode[12];
1017
1018 /* Length of file name (decimal ASCII string). */
1019 char namlen[4];
1020
1021 /* This structure is followed by the file name. The length of the
1022 name is given in the namlen field. If the length of the name is
1023 odd, the name is followed by a null byte. The name and optional
1024 null byte are followed by XCOFFARFMAG, which is not included in
1025 namlen. The contents of the archive member follow; the number of
1026 bytes is given in the size field. */
1027};
1028
1029#define SIZEOF_AR_HDR (7 * 12 + 4)
1030
5ea1af0d
GK
1031/* The equivalent for the big archive format. */
1032
1033struct xcoff_ar_hdr_big
1034{
1035 /* File size not including the header (decimal ASCII string). */
1036 char size[20];
1037
1038 /* File offset of next archive member (decimal ASCII string). */
1039 char nextoff[20];
1040
1041 /* File offset of previous archive member (decimal ASCII string). */
1042 char prevoff[20];
1043
1044 /* File mtime (decimal ASCII string). */
1045 char date[12];
1046
1047 /* File UID (decimal ASCII string). */
1048 char uid[12];
1049
1050 /* File GID (decimal ASCII string). */
1051 char gid[12];
1052
1053 /* File mode (octal ASCII string). */
1054 char mode[12];
1055
1056 /* Length of file name (decimal ASCII string). */
1057 char namlen[4];
1058
1059 /* This structure is followed by the file name. The length of the
1060 name is given in the namlen field. If the length of the name is
1061 odd, the name is followed by a null byte. The name and optional
1062 null byte are followed by XCOFFARFMAG, which is not included in
1063 namlen. The contents of the archive member follow; the number of
1064 bytes is given in the size field. */
1065};
1066
1067#define SIZEOF_AR_HDR_BIG (3 * 20 + 4 * 12 + 4)
1068
1069
1070/* We often have to distinguish between the old and big file format.
1071 Make it a bit cleaner. We can use `xcoff_ardata' here because the
1072 `hdr' member has the same size and position in both formats. */
1073#define xcoff_big_format_p(abfd) \
1074 (xcoff_ardata (abfd)->magic[1] == 'b')
1075
252b5132 1076/* We store a copy of the xcoff_ar_file_hdr in the tdata field of the
5ea1af0d 1077 artdata structure. Similar for the big archive. */
252b5132
RH
1078#define xcoff_ardata(abfd) \
1079 ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata)
5ea1af0d
GK
1080#define xcoff_ardata_big(abfd) \
1081 ((struct xcoff_ar_file_hdr_big *) bfd_ardata (abfd)->tdata)
252b5132
RH
1082
1083/* We store a copy of the xcoff_ar_hdr in the arelt_data field of an
5ea1af0d 1084 archive element. Similar for the big archive. */
252b5132
RH
1085#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
1086#define arch_xhdr(bfd) \
1087 ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header)
5ea1af0d
GK
1088#define arch_xhdr_big(bfd) \
1089 ((struct xcoff_ar_hdr_big *) arch_eltdata (bfd)->arch_header)
252b5132 1090
252b5132
RH
1091/* Read in the armap of an XCOFF archive. */
1092
7f6d05e8
CP
1093boolean
1094_bfd_xcoff_slurp_armap (abfd)
252b5132
RH
1095 bfd *abfd;
1096{
1097 file_ptr off;
252b5132
RH
1098 size_t namlen;
1099 bfd_size_type sz;
1100 bfd_byte *contents, *cend;
1101 unsigned int c, i;
1102 carsym *arsym;
1103 bfd_byte *p;
1104
1105 if (xcoff_ardata (abfd) == NULL)
1106 {
1107 bfd_has_map (abfd) = false;
1108 return true;
1109 }
1110
5ea1af0d 1111 if (! xcoff_big_format_p (abfd))
252b5132 1112 {
5ea1af0d
GK
1113 /* This is for the old format. */
1114 struct xcoff_ar_hdr hdr;
1115
1116 off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10);
1117 if (off == 0)
1118 {
1119 bfd_has_map (abfd) = false;
1120 return true;
1121 }
1122
1123 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1124 return false;
1125
1126 /* The symbol table starts with a normal archive header. */
1127 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1128 return false;
1129
1130 /* Skip the name (normally empty). */
1131 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1132 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1133 return false;
1134
1135 sz = strtol (hdr.size, (char **) NULL, 10);
252b5132 1136 }
5ea1af0d
GK
1137 else
1138 {
1139 /* This is for the new format. */
1140 struct xcoff_ar_hdr_big hdr;
252b5132 1141
5ea1af0d
GK
1142 off = strtol (xcoff_ardata_big (abfd)->symoff, (char **) NULL, 10);
1143 if (off == 0)
1144 {
1145 bfd_has_map (abfd) = false;
1146 return true;
1147 }
252b5132 1148
5ea1af0d
GK
1149 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1150 return false;
252b5132 1151
5ea1af0d
GK
1152 /* The symbol table starts with a normal archive header. */
1153 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1154 != SIZEOF_AR_HDR_BIG)
1155 return false;
1156
1157 /* Skip the name (normally empty). */
1158 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1159 if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1160 return false;
1161
1162 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1163 machines) since the field width is 20 and there numbers with more
1164 than 32 bits can be represented. */
1165 sz = strtol (hdr.size, (char **) NULL, 10);
1166 }
252b5132
RH
1167
1168 /* Read in the entire symbol table. */
252b5132
RH
1169 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1170 if (contents == NULL)
1171 return false;
1172 if (bfd_read ((PTR) contents, 1, sz, abfd) != sz)
1173 return false;
1174
1175 /* The symbol table starts with a four byte count. */
1176 c = bfd_h_get_32 (abfd, contents);
1177
1178 if (c * 4 >= sz)
1179 {
1180 bfd_set_error (bfd_error_bad_value);
1181 return false;
1182 }
1183
1184 bfd_ardata (abfd)->symdefs = ((carsym *)
1185 bfd_alloc (abfd, c * sizeof (carsym)));
1186 if (bfd_ardata (abfd)->symdefs == NULL)
1187 return false;
1188
1189 /* After the count comes a list of four byte file offsets. */
1190 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1191 i < c;
1192 ++i, ++arsym, p += 4)
1193 arsym->file_offset = bfd_h_get_32 (abfd, p);
1194
1195 /* After the file offsets come null terminated symbol names. */
1196 cend = contents + sz;
1197 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1198 i < c;
1199 ++i, ++arsym, p += strlen ((char *) p) + 1)
1200 {
1201 if (p >= cend)
1202 {
1203 bfd_set_error (bfd_error_bad_value);
1204 return false;
1205 }
1206 arsym->name = (char *) p;
1207 }
1208
1209 bfd_ardata (abfd)->symdef_count = c;
1210 bfd_has_map (abfd) = true;
1211
1212 return true;
1213}
1214
1215/* See if this is an XCOFF archive. */
1216
7f6d05e8
CP
1217const bfd_target *
1218_bfd_xcoff_archive_p (abfd)
252b5132
RH
1219 bfd *abfd;
1220{
5ea1af0d 1221 char magic[SXCOFFARMAG];
252b5132 1222
5ea1af0d 1223 if (bfd_read ((PTR) magic, SXCOFFARMAG, 1, abfd) != SXCOFFARMAG)
252b5132
RH
1224 {
1225 if (bfd_get_error () != bfd_error_system_call)
1226 bfd_set_error (bfd_error_wrong_format);
1227 return NULL;
1228 }
1229
5ea1af0d
GK
1230 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1231 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
252b5132
RH
1232 {
1233 bfd_set_error (bfd_error_wrong_format);
1234 return NULL;
1235 }
1236
1237 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
1238 involves a cast, we can't do it as the left operand of
1239 assignment. */
1240 abfd->tdata.aout_ar_data =
1241 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
1242
1243 if (bfd_ardata (abfd) == (struct artdata *) NULL)
1244 return NULL;
1245
252b5132
RH
1246 bfd_ardata (abfd)->cache = NULL;
1247 bfd_ardata (abfd)->archive_head = NULL;
1248 bfd_ardata (abfd)->symdefs = NULL;
1249 bfd_ardata (abfd)->extended_names = NULL;
1250
5ea1af0d
GK
1251 /* Now handle the two formats. */
1252 if (magic[1] != 'b')
1253 {
1254 /* This is the old format. */
1255 struct xcoff_ar_file_hdr hdr;
252b5132 1256
5ea1af0d
GK
1257 /* Copy over the magic string. */
1258 memcpy (hdr.magic, magic, SXCOFFARMAG);
1259
1260 /* Now read the rest of the file header. */
1261 if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR - SXCOFFARMAG, 1,
1262 abfd) != SIZEOF_AR_FILE_HDR - SXCOFFARMAG)
1263 {
1264 if (bfd_get_error () != bfd_error_system_call)
1265 bfd_set_error (bfd_error_wrong_format);
1266 return NULL;
1267 }
1268
1269 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1270 (char **) NULL, 10);
1271
1272 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR);
1273 if (bfd_ardata (abfd)->tdata == NULL)
1274 return NULL;
1275
1276 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1277 }
1278 else
1279 {
1280 /* This is the new format. */
1281 struct xcoff_ar_file_hdr_big hdr;
1282
1283 /* Copy over the magic string. */
1284 memcpy (hdr.magic, magic, SXCOFFARMAG);
1285
1286 /* Now read the rest of the file header. */
1287 if (bfd_read ((PTR) &hdr.memoff, SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG, 1,
1288 abfd) != SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG)
1289 {
1290 if (bfd_get_error () != bfd_error_system_call)
1291 bfd_set_error (bfd_error_wrong_format);
1292 return NULL;
1293 }
1294
1295 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1296 machines) since the field width is 20 and there numbers with more
1297 than 32 bits can be represented. */
1298 bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff,
1299 (char **) NULL, 10);
1300
1301 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR_BIG);
1302 if (bfd_ardata (abfd)->tdata == NULL)
1303 return NULL;
1304
1305 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1306 }
252b5132 1307
7f6d05e8 1308 if (! _bfd_xcoff_slurp_armap (abfd))
252b5132
RH
1309 {
1310 bfd_release (abfd, bfd_ardata (abfd));
1311 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
1312 return NULL;
1313 }
1314
1315 return abfd->xvec;
1316}
1317
1318/* Read the archive header in an XCOFF archive. */
1319
7f6d05e8
CP
1320PTR
1321_bfd_xcoff_read_ar_hdr (abfd)
252b5132
RH
1322 bfd *abfd;
1323{
252b5132 1324 size_t namlen;
252b5132
RH
1325 struct areltdata *ret;
1326
252b5132
RH
1327 ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata));
1328 if (ret == NULL)
1329 return NULL;
5ea1af0d
GK
1330
1331 if (! xcoff_big_format_p (abfd))
1332 {
1333 struct xcoff_ar_hdr hdr;
1334 struct xcoff_ar_hdr *hdrp;
1335
1336 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR)
1337 {
1338 free (ret);
1339 return NULL;
1340 }
1341
1342 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1343 hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd,
1344 SIZEOF_AR_HDR + namlen + 1);
1345 if (hdrp == NULL)
1346 {
1347 free (ret);
1348 return NULL;
1349 }
1350 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1351 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen)
1352 {
1353 free (ret);
1354 return NULL;
1355 }
1356 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1357
1358 ret->arch_header = (char *) hdrp;
1359 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1360 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1361 }
1362 else
1363 {
1364 struct xcoff_ar_hdr_big hdr;
1365 struct xcoff_ar_hdr_big *hdrp;
1366
1367 if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd)
1368 != SIZEOF_AR_HDR_BIG)
1369 {
1370 free (ret);
1371 return NULL;
1372 }
1373
1374 namlen = strtol (hdr.namlen, (char **) NULL, 10);
1375 hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd,
1376 SIZEOF_AR_HDR_BIG
1377 + namlen + 1);
1378 if (hdrp == NULL)
1379 {
1380 free (ret);
1381 return NULL;
1382 }
1383 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1384 if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR_BIG, 1, namlen, abfd) != namlen)
1385 {
1386 free (ret);
1387 return NULL;
1388 }
1389 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1390
1391 ret->arch_header = (char *) hdrp;
1392 /* XXX This actually has to be a call to strtoll (at least on 32-bit
1393 machines) since the field width is 20 and there numbers with more
1394 than 32 bits can be represented. */
1395 ret->parsed_size = strtol (hdr.size, (char **) NULL, 10);
1396 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1397 }
252b5132
RH
1398
1399 /* Skip over the XCOFFARFMAG at the end of the file name. */
1400 if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0)
1401 return NULL;
1402
1403 return (PTR) ret;
1404}
1405
1406/* Open the next element in an XCOFF archive. */
1407
7f6d05e8
CP
1408bfd *
1409_bfd_xcoff_openr_next_archived_file (archive, last_file)
252b5132
RH
1410 bfd *archive;
1411 bfd *last_file;
1412{
1413 file_ptr filestart;
1414
1415 if (xcoff_ardata (archive) == NULL)
1416 {
1417 bfd_set_error (bfd_error_invalid_operation);
1418 return NULL;
1419 }
1420
5ea1af0d
GK
1421 if (! xcoff_big_format_p (archive))
1422 {
1423 if (last_file == NULL)
1424 filestart = bfd_ardata (archive)->first_file_filepos;
1425 else
1426 filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL,
1427 10);
1428
1429 if (filestart == 0
1430 || filestart == strtol (xcoff_ardata (archive)->memoff,
1431 (char **) NULL, 10)
1432 || filestart == strtol (xcoff_ardata (archive)->symoff,
1433 (char **) NULL, 10))
1434 {
1435 bfd_set_error (bfd_error_no_more_archived_files);
1436 return NULL;
1437 }
1438 }
252b5132 1439 else
252b5132 1440 {
5ea1af0d
GK
1441 if (last_file == NULL)
1442 filestart = bfd_ardata (archive)->first_file_filepos;
1443 else
1444 /* XXX These actually have to be a calls to strtoll (at least
1445 on 32-bit machines) since the fields's width is 20 and
1446 there numbers with more than 32 bits can be represented. */
1447 filestart = strtol (arch_xhdr_big (last_file)->nextoff, (char **) NULL,
1448 10);
1449
1450 /* XXX These actually have to be calls to strtoll (at least on 32-bit
1451 machines) since the fields's width is 20 and there numbers with more
1452 than 32 bits can be represented. */
1453 if (filestart == 0
1454 || filestart == strtol (xcoff_ardata_big (archive)->memoff,
1455 (char **) NULL, 10)
1456 || filestart == strtol (xcoff_ardata_big (archive)->symoff,
1457 (char **) NULL, 10))
1458 {
1459 bfd_set_error (bfd_error_no_more_archived_files);
1460 return NULL;
1461 }
252b5132
RH
1462 }
1463
1464 return _bfd_get_elt_at_filepos (archive, filestart);
1465}
1466
1467/* Stat an element in an XCOFF archive. */
1468
7f6d05e8
CP
1469int
1470_bfd_xcoff_generic_stat_arch_elt (abfd, s)
252b5132
RH
1471 bfd *abfd;
1472 struct stat *s;
1473{
252b5132
RH
1474 if (abfd->arelt_data == NULL)
1475 {
1476 bfd_set_error (bfd_error_invalid_operation);
1477 return -1;
1478 }
1479
5ea1af0d
GK
1480 if (! xcoff_big_format_p (abfd))
1481 {
1482 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1483
1484 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1485 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1486 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1487 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1488 s->st_size = arch_eltdata (abfd)->parsed_size;
1489 }
1490 else
1491 {
1492 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
252b5132 1493
5ea1af0d
GK
1494 s->st_mtime = strtol (hdrp->date, (char **) NULL, 10);
1495 s->st_uid = strtol (hdrp->uid, (char **) NULL, 10);
1496 s->st_gid = strtol (hdrp->gid, (char **) NULL, 10);
1497 s->st_mode = strtol (hdrp->mode, (char **) NULL, 8);
1498 s->st_size = arch_eltdata (abfd)->parsed_size;
1499 }
252b5132
RH
1500
1501 return 0;
1502}
1503
1504/* Normalize a file name for inclusion in an archive. */
1505
1506static const char *
1507normalize_filename (abfd)
1508 bfd *abfd;
1509{
1510 const char *file;
1511 const char *filename;
1512
1513 file = bfd_get_filename (abfd);
1514 filename = strrchr (file, '/');
1515 if (filename != NULL)
1516 filename++;
1517 else
1518 filename = file;
1519 return filename;
1520}
1521
1522/* Write out an XCOFF armap. */
1523
1524/*ARGSUSED*/
1525static boolean
5ea1af0d 1526xcoff_write_armap_old (abfd, elength, map, orl_count, stridx)
252b5132 1527 bfd *abfd;
5f771d47 1528 unsigned int elength ATTRIBUTE_UNUSED;
252b5132
RH
1529 struct orl *map;
1530 unsigned int orl_count;
1531 int stridx;
1532{
1533 struct xcoff_ar_hdr hdr;
1534 char *p;
1535 unsigned char buf[4];
1536 bfd *sub;
1537 file_ptr fileoff;
1538 unsigned int i;
1539
1540 memset (&hdr, 0, sizeof hdr);
1541 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1542 sprintf (hdr.nextoff, "%d", 0);
1543 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1544 sprintf (hdr.date, "%d", 0);
1545 sprintf (hdr.uid, "%d", 0);
1546 sprintf (hdr.gid, "%d", 0);
1547 sprintf (hdr.mode, "%d", 0);
1548 sprintf (hdr.namlen, "%d", 0);
1549
1550 /* We need spaces, not null bytes, in the header. */
1551 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1552 if (*p == '\0')
1553 *p = ' ';
1554
1555 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR
1556 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
1557 return false;
5ea1af0d 1558
252b5132
RH
1559 bfd_h_put_32 (abfd, orl_count, buf);
1560 if (bfd_write (buf, 1, 4, abfd) != 4)
1561 return false;
1562
1563 sub = abfd->archive_head;
1564 fileoff = SIZEOF_AR_FILE_HDR;
1565 i = 0;
1566 while (sub != NULL && i < orl_count)
1567 {
1568 size_t namlen;
1569
1570 while (((bfd *) (map[i]).pos) == sub)
1571 {
1572 bfd_h_put_32 (abfd, fileoff, buf);
1573 if (bfd_write (buf, 1, 4, abfd) != 4)
1574 return false;
1575 ++i;
1576 }
1577 namlen = strlen (normalize_filename (sub));
1578 namlen = (namlen + 1) &~ 1;
1579 fileoff += (SIZEOF_AR_HDR
1580 + namlen
1581 + SXCOFFARFMAG
1582 + arelt_size (sub));
1583 fileoff = (fileoff + 1) &~ 1;
1584 sub = sub->next;
1585 }
1586
1587 for (i = 0; i < orl_count; i++)
1588 {
1589 const char *name;
1590 size_t namlen;
1591
1592 name = *map[i].name;
1593 namlen = strlen (name);
1594 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1595 return false;
1596 }
1597
1598 if ((stridx & 1) != 0)
1599 {
1600 char b;
1601
1602 b = '\0';
1603 if (bfd_write (&b, 1, 1, abfd) != 1)
1604 return false;
1605 }
1606
1607 return true;
1608}
1609
5ea1af0d 1610/*ARGSUSED*/
252b5132 1611static boolean
5ea1af0d 1612xcoff_write_armap_big (abfd, elength, map, orl_count, stridx)
252b5132 1613 bfd *abfd;
5ea1af0d
GK
1614 unsigned int elength ATTRIBUTE_UNUSED;
1615 struct orl *map;
1616 unsigned int orl_count;
1617 int stridx;
252b5132 1618{
5ea1af0d
GK
1619 struct xcoff_ar_hdr_big hdr;
1620 char *p;
1621 unsigned char buf[4];
252b5132 1622 bfd *sub;
5ea1af0d 1623 file_ptr fileoff;
252b5132 1624 unsigned int i;
252b5132 1625
5ea1af0d
GK
1626 memset (&hdr, 0, sizeof hdr);
1627 /* XXX This call actually should use %lld (at least on 32-bit
1628 machines) since the fields's width is 20 and there numbers with
1629 more than 32 bits can be represented. */
1630 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1631 sprintf (hdr.nextoff, "%d", 0);
1632 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12);
1633 sprintf (hdr.date, "%d", 0);
1634 sprintf (hdr.uid, "%d", 0);
1635 sprintf (hdr.gid, "%d", 0);
1636 sprintf (hdr.mode, "%d", 0);
1637 sprintf (hdr.namlen, "%d", 0);
252b5132 1638
5ea1af0d
GK
1639 /* We need spaces, not null bytes, in the header. */
1640 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR_BIG; p++)
1641 if (*p == '\0')
1642 *p = ' ';
1643
1644 if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR_BIG, 1, abfd) != SIZEOF_AR_HDR_BIG
1645 || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG)
252b5132
RH
1646 return false;
1647
5ea1af0d
GK
1648 bfd_h_put_32 (abfd, orl_count, buf);
1649 if (bfd_write (buf, 1, 4, abfd) != 4)
252b5132
RH
1650 return false;
1651
5ea1af0d
GK
1652 sub = abfd->archive_head;
1653 fileoff = SIZEOF_AR_FILE_HDR_BIG;
1654 i = 0;
1655 while (sub != NULL && i < orl_count)
252b5132 1656 {
5ea1af0d
GK
1657 size_t namlen;
1658
1659 while (((bfd *) (map[i]).pos) == sub)
1660 {
1661 bfd_h_put_32 (abfd, fileoff, buf);
1662 if (bfd_write (buf, 1, 4, abfd) != 4)
1663 return false;
1664 ++i;
1665 }
1666 namlen = strlen (normalize_filename (sub));
1667 namlen = (namlen + 1) &~ 1;
1668 fileoff += (SIZEOF_AR_HDR_BIG
1669 + namlen
1670 + SXCOFFARFMAG
1671 + arelt_size (sub));
1672 fileoff = (fileoff + 1) &~ 1;
1673 sub = sub->next;
1674 }
1675
1676 for (i = 0; i < orl_count; i++)
1677 {
1678 const char *name;
1679 size_t namlen;
1680
1681 name = *map[i].name;
1682 namlen = strlen (name);
1683 if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1)
1684 return false;
1685 }
1686
1687 if ((stridx & 1) != 0)
1688 {
1689 char b;
1690
1691 b = '\0';
1692 if (bfd_write (&b, 1, 1, abfd) != 1)
1693 return false;
1694 }
1695
1696 return true;
1697}
1698
1699/*ARGSUSED*/
7f6d05e8
CP
1700boolean
1701_bfd_xcoff_write_armap (abfd, elength, map, orl_count, stridx)
5ea1af0d
GK
1702 bfd *abfd;
1703 unsigned int elength ATTRIBUTE_UNUSED;
1704 struct orl *map;
1705 unsigned int orl_count;
1706 int stridx;
1707{
1708 if (! xcoff_big_format_p (abfd))
1709 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
1710 else
1711 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
1712}
1713
1714/* Write out an XCOFF archive. We always write an entire archive,
1715 rather than fussing with the freelist and so forth. */
1716
1717static boolean
1718xcoff_write_archive_contents_old (abfd)
1719 bfd *abfd;
1720{
1721 struct xcoff_ar_file_hdr fhdr;
1722 size_t count;
1723 size_t total_namlen;
1724 file_ptr *offsets;
1725 boolean makemap;
1726 boolean hasobjects;
1727 file_ptr prevoff, nextoff;
1728 bfd *sub;
1729 unsigned int i;
1730 struct xcoff_ar_hdr ahdr;
1731 bfd_size_type size;
1732 char *p;
1733 char decbuf[13];
1734
1735 memset (&fhdr, 0, sizeof fhdr);
1736 strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
1737 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
1738 sprintf (fhdr.freeoff, "%d", 0);
1739
1740 count = 0;
1741 total_namlen = 0;
1742 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1743 {
1744 ++count;
1745 total_namlen += strlen (normalize_filename (sub)) + 1;
1746 }
1747 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1748 if (offsets == NULL)
1749 return false;
1750
1751 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
1752 return false;
1753
1754 makemap = bfd_has_map (abfd);
1755 hasobjects = false;
1756 prevoff = 0;
1757 nextoff = SIZEOF_AR_FILE_HDR;
1758 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
1759 {
1760 const char *name;
252b5132
RH
1761 size_t namlen;
1762 struct xcoff_ar_hdr *ahdrp;
1763 bfd_size_type remaining;
1764
1765 if (makemap && ! hasobjects)
1766 {
1767 if (bfd_check_format (sub, bfd_object))
1768 hasobjects = true;
1769 }
1770
1771 name = normalize_filename (sub);
1772 namlen = strlen (name);
1773
1774 if (sub->arelt_data != NULL)
1775 ahdrp = arch_xhdr (sub);
1776 else
1777 ahdrp = NULL;
1778
1779 if (ahdrp == NULL)
1780 {
1781 struct stat s;
1782
1783 memset (&ahdr, 0, sizeof ahdr);
1784 ahdrp = &ahdr;
1785 if (stat (bfd_get_filename (sub), &s) != 0)
1786 {
1787 bfd_set_error (bfd_error_system_call);
1788 return false;
1789 }
1790
1791 sprintf (ahdrp->size, "%ld", (long) s.st_size);
1792 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
1793 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
1794 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
1795 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
1796
1797 if (sub->arelt_data == NULL)
1798 {
1799 sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
1800 if (sub->arelt_data == NULL)
1801 return false;
1802 }
1803
1804 arch_eltdata (sub)->parsed_size = s.st_size;
1805 }
1806
1807 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
1808 sprintf (ahdrp->namlen, "%ld", (long) namlen);
1809
1810 /* If the length of the name is odd, we write out the null byte
1811 after the name as well. */
1812 namlen = (namlen + 1) &~ 1;
1813
1814 remaining = arelt_size (sub);
1815 size = (SIZEOF_AR_HDR
1816 + namlen
1817 + SXCOFFARFMAG
1818 + remaining);
1819
1820 BFD_ASSERT (nextoff == bfd_tell (abfd));
1821
1822 offsets[i] = nextoff;
1823
1824 prevoff = nextoff;
1825 nextoff += size + (size & 1);
1826
1827 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
1828
1829 /* We need spaces, not null bytes, in the header. */
1830 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
1831 if (*p == '\0')
1832 *p = ' ';
1833
1834 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1835 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
1836 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1837 != SXCOFFARFMAG))
1838 return false;
1839
1840 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
1841 return false;
1842 while (remaining != 0)
1843 {
1844 bfd_size_type amt;
1845 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1846
1847 amt = sizeof buffer;
1848 if (amt > remaining)
1849 amt = remaining;
1850 if (bfd_read (buffer, 1, amt, sub) != amt
1851 || bfd_write (buffer, 1, amt, abfd) != amt)
1852 return false;
1853 remaining -= amt;
1854 }
1855
1856 if ((size & 1) != 0)
1857 {
1858 bfd_byte b;
1859
1860 b = '\0';
1861 if (bfd_write (&b, 1, 1, abfd) != 1)
1862 return false;
1863 }
1864 }
1865
1866 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
1867
1868 /* Write out the member table. */
1869
1870 BFD_ASSERT (nextoff == bfd_tell (abfd));
1871 sprintf (fhdr.memoff, "%ld", (long) nextoff);
1872
1873 memset (&ahdr, 0, sizeof ahdr);
1874 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
1875 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
1876 sprintf (ahdr.date, "%d", 0);
1877 sprintf (ahdr.uid, "%d", 0);
1878 sprintf (ahdr.gid, "%d", 0);
1879 sprintf (ahdr.mode, "%d", 0);
1880 sprintf (ahdr.namlen, "%d", 0);
1881
1882 size = (SIZEOF_AR_HDR
1883 + 12
1884 + count * 12
1885 + total_namlen
1886 + SXCOFFARFMAG);
1887
1888 prevoff = nextoff;
1889 nextoff += size + (size & 1);
1890
1891 if (makemap && hasobjects)
1892 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
1893 else
1894 sprintf (ahdr.nextoff, "%d", 0);
1895
1896 /* We need spaces, not null bytes, in the header. */
1897 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
1898 if (*p == '\0')
1899 *p = ' ';
1900
1901 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
1902 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
1903 != SXCOFFARFMAG))
1904 return false;
1905
1906 sprintf (decbuf, "%-12ld", (long) count);
1907 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1908 return false;
1909 for (i = 0; i < count; i++)
1910 {
1911 sprintf (decbuf, "%-12ld", (long) offsets[i]);
1912 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
1913 return false;
1914 }
1915 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1916 {
1917 const char *name;
1918 size_t namlen;
1919
1920 name = normalize_filename (sub);
1921 namlen = strlen (name);
1922 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
1923 return false;
1924 }
1925 if ((size & 1) != 0)
1926 {
1927 bfd_byte b;
1928
1929 b = '\0';
1930 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
1931 return false;
1932 }
1933
1934 /* Write out the armap, if appropriate. */
1935
1936 if (! makemap || ! hasobjects)
1937 sprintf (fhdr.symoff, "%d", 0);
1938 else
1939 {
1940 BFD_ASSERT (nextoff == bfd_tell (abfd));
1941 sprintf (fhdr.symoff, "%ld", (long) nextoff);
1942 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
1943 if (! _bfd_compute_and_write_armap (abfd, 0))
1944 return false;
1945 }
1946
1947 /* Write out the archive file header. */
1948
1949 /* We need spaces, not null bytes, in the header. */
1950 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
1951 if (*p == '\0')
1952 *p = ' ';
1953
1954 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
1955 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) !=
1956 SIZEOF_AR_FILE_HDR))
1957 return false;
1958
1959 return true;
1960}
5ea1af0d
GK
1961
1962static boolean
1963xcoff_write_archive_contents_big (abfd)
1964 bfd *abfd;
1965{
1966 struct xcoff_ar_file_hdr_big fhdr;
1967 size_t count;
1968 size_t total_namlen;
1969 file_ptr *offsets;
1970 boolean makemap;
1971 boolean hasobjects;
1972 file_ptr prevoff, nextoff;
1973 bfd *sub;
1974 unsigned int i;
1975 struct xcoff_ar_hdr_big ahdr;
1976 bfd_size_type size;
1977 char *p;
1978 char decbuf[13];
1979
1980 memset (&fhdr, 0, sizeof fhdr);
1981 strncpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
1982 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR_BIG);
1983 sprintf (fhdr.freeoff, "%d", 0);
1984
1985 count = 0;
1986 total_namlen = 0;
1987 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
1988 {
1989 ++count;
1990 total_namlen += strlen (normalize_filename (sub)) + 1;
1991 }
1992 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
1993 if (offsets == NULL)
1994 return false;
1995
1996 if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
1997 return false;
1998
1999 makemap = bfd_has_map (abfd);
2000 hasobjects = false;
2001 prevoff = 0;
2002 nextoff = SIZEOF_AR_FILE_HDR_BIG;
2003 for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++)
2004 {
2005 const char *name;
2006 size_t namlen;
2007 struct xcoff_ar_hdr_big *ahdrp;
2008 bfd_size_type remaining;
2009
2010 if (makemap && ! hasobjects)
2011 {
2012 if (bfd_check_format (sub, bfd_object))
2013 hasobjects = true;
2014 }
2015
2016 name = normalize_filename (sub);
2017 namlen = strlen (name);
2018
2019 if (sub->arelt_data != NULL)
2020 ahdrp = arch_xhdr_big (sub);
2021 else
2022 ahdrp = NULL;
2023
2024 if (ahdrp == NULL)
2025 {
2026 struct stat s;
2027
2028 memset (&ahdr, 0, sizeof ahdr);
2029 ahdrp = &ahdr;
2030 /* XXX This should actually be a call to stat64 (at least on
2031 32-bit machines). */
2032 if (stat (bfd_get_filename (sub), &s) != 0)
2033 {
2034 bfd_set_error (bfd_error_system_call);
2035 return false;
2036 }
2037
2038 /* XXX This call actually should use %lld (at least on 32-bit
2039 machines) since the fields's width is 20 and there numbers with
2040 more than 32 bits can be represented. */
2041 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2042 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2043 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2044 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2045 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2046
2047 if (sub->arelt_data == NULL)
2048 {
2049 sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata));
2050 if (sub->arelt_data == NULL)
2051 return false;
2052 }
2053
2054 arch_eltdata (sub)->parsed_size = s.st_size;
2055 }
2056
2057 /* XXX These calls actually should use %lld (at least on 32-bit
2058 machines) since the fields's width is 20 and there numbers with
2059 more than 32 bits can be represented. */
2060 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2061 sprintf (ahdrp->namlen, "%ld", (long) namlen);
2062
2063 /* If the length of the name is odd, we write out the null byte
2064 after the name as well. */
2065 namlen = (namlen + 1) &~ 1;
2066
2067 remaining = arelt_size (sub);
2068 size = (SIZEOF_AR_HDR_BIG
2069 + namlen
2070 + SXCOFFARFMAG
2071 + remaining);
2072
2073 BFD_ASSERT (nextoff == bfd_tell (abfd));
2074
2075 offsets[i] = nextoff;
2076
2077 prevoff = nextoff;
2078 nextoff += size + (size & 1);
2079
2080 sprintf (ahdrp->nextoff, "%ld", (long) nextoff);
2081
2082 /* We need spaces, not null bytes, in the header. */
2083 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR_BIG; p++)
2084 if (*p == '\0')
2085 *p = ' ';
2086
2087 if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR_BIG, abfd)
2088 != SIZEOF_AR_HDR_BIG
2089 || bfd_write ((PTR) name, 1, namlen, abfd) != namlen
2090 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2091 != SXCOFFARFMAG))
2092 return false;
2093
2094 if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0)
2095 return false;
2096 while (remaining != 0)
2097 {
2098 bfd_size_type amt;
2099 bfd_byte buffer[DEFAULT_BUFFERSIZE];
2100
2101 amt = sizeof buffer;
2102 if (amt > remaining)
2103 amt = remaining;
2104 if (bfd_read (buffer, 1, amt, sub) != amt
2105 || bfd_write (buffer, 1, amt, abfd) != amt)
2106 return false;
2107 remaining -= amt;
2108 }
2109
2110 if ((size & 1) != 0)
2111 {
2112 bfd_byte b;
2113
2114 b = '\0';
2115 if (bfd_write (&b, 1, 1, abfd) != 1)
2116 return false;
2117 }
2118 }
2119
2120 /* XXX This call actually should use %lld (at least on 32-bit
2121 machines) since the fields's width is 20 and there numbers with
2122 more than 32 bits can be represented. */
2123 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2124
2125 /* Write out the member table. */
2126
2127 BFD_ASSERT (nextoff == bfd_tell (abfd));
2128 /* XXX This call actually should use %lld (at least on 32-bit
2129 machines) since the fields's width is 20 and there numbers with
2130 more than 32 bits can be represented. */
2131 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2132
2133 memset (&ahdr, 0, sizeof ahdr);
2134 /* XXX The next two calls actually should use %lld (at least on 32-bit
2135 machines) since the fields's width is 20 and there numbers with
2136 more than 32 bits can be represented. */
2137 sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen));
2138 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2139 sprintf (ahdr.date, "%d", 0);
2140 sprintf (ahdr.uid, "%d", 0);
2141 sprintf (ahdr.gid, "%d", 0);
2142 sprintf (ahdr.mode, "%d", 0);
2143 sprintf (ahdr.namlen, "%d", 0);
2144
2145 size = (SIZEOF_AR_HDR_BIG
2146 + 12
2147 + count * 12
2148 + total_namlen
2149 + SXCOFFARFMAG);
2150
2151 prevoff = nextoff;
2152 nextoff += size + (size & 1);
2153
2154 if (makemap && hasobjects)
2155 /* XXX This call actually should use %lld (at least on 32-bit
2156 machines) since the fields's width is 20 and there numbers with
2157 more than 32 bits can be represented. */
2158 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2159 else
2160 sprintf (ahdr.nextoff, "%d", 0);
2161
2162 /* We need spaces, not null bytes, in the header. */
2163 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR_BIG; p++)
2164 if (*p == '\0')
2165 *p = ' ';
2166
2167 if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2168 || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd)
2169 != SXCOFFARFMAG))
2170 return false;
2171
2172 sprintf (decbuf, "%-12ld", (long) count);
2173 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2174 return false;
2175 for (i = 0; i < count; i++)
2176 {
2177 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2178 if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12)
2179 return false;
2180 }
2181 for (sub = abfd->archive_head; sub != NULL; sub = sub->next)
2182 {
2183 const char *name;
2184 size_t namlen;
2185
2186 name = normalize_filename (sub);
2187 namlen = strlen (name);
2188 if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1)
2189 return false;
2190 }
2191 if ((size & 1) != 0)
2192 {
2193 bfd_byte b;
2194
2195 b = '\0';
2196 if (bfd_write ((PTR) &b, 1, 1, abfd) != 1)
2197 return false;
2198 }
2199
2200 /* Write out the armap, if appropriate. */
2201
2202 if (! makemap || ! hasobjects)
2203 sprintf (fhdr.symoff, "%d", 0);
2204 else
2205 {
2206 BFD_ASSERT (nextoff == bfd_tell (abfd));
2207 /* XXX This call actually should use %lld (at least on 32-bit
2208 machines) since the fields's width is 20 and there numbers with
2209 more than 32 bits can be represented. */
2210 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2211 bfd_ardata (abfd)->tdata = (PTR) &fhdr;
2212 if (! _bfd_compute_and_write_armap (abfd, 0))
2213 return false;
2214 }
2215
2216 /* Write out the archive file header. */
2217
2218 /* We need spaces, not null bytes, in the header. */
2219 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR_BIG; p++)
2220 if (*p == '\0')
2221 *p = ' ';
2222
2223 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2224 || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR_BIG, 1, abfd) !=
2225 SIZEOF_AR_FILE_HDR_BIG))
2226 return false;
2227
2228 return true;
2229}
2230
7f6d05e8
CP
2231boolean
2232_bfd_xcoff_write_archive_contents (abfd)
5ea1af0d
GK
2233 bfd *abfd;
2234{
2235 if (! xcoff_big_format_p (abfd))
2236 return xcoff_write_archive_contents_old (abfd);
2237 else
2238 return xcoff_write_archive_contents_big (abfd);
2239}
252b5132
RH
2240\f
2241/* We can't use the usual coff_sizeof_headers routine, because AIX
2242 always uses an a.out header. */
2243
2244/*ARGSUSED*/
7f6d05e8 2245int
252b5132
RH
2246_bfd_xcoff_sizeof_headers (abfd, reloc)
2247 bfd *abfd;
5f771d47 2248 boolean reloc ATTRIBUTE_UNUSED;
252b5132
RH
2249{
2250 int size;
2251
2252 size = FILHSZ;
2253 if (xcoff_data (abfd)->full_aouthdr)
2254 size += AOUTSZ;
2255 else
2256 size += SMALL_AOUTSZ;
2257 size += abfd->section_count * SCNHSZ;
2258 return size;
2259}
2260\f
14958a43 2261
This page took 0.186599 seconds and 4 git commands to generate.