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