coff-rs6000.c revision 1.1.1.6 1 /* BFD back-end for IBM RS/6000 "XCOFF" files.
2 Copyright (C) 1990-2018 Free Software Foundation, Inc.
3 Written by Metin G. Ozisik, Mimi Phuong-Thao Vo, and John Gilmore.
4 Archive support from Damon A. Permezel.
5 Contributed by IBM Corporation and Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
24 #include "sysdep.h"
25 #include "libiberty.h"
26 #include "bfd.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "coff/internal.h"
30 #include "coff/xcoff.h"
31 #include "coff/rs6000.h"
32 #include "libcoff.h"
33 #include "libxcoff.h"
34
35 extern bfd_boolean _bfd_xcoff_mkobject (bfd *);
36 extern bfd_boolean _bfd_xcoff_copy_private_bfd_data (bfd *, bfd *);
37 extern bfd_boolean _bfd_xcoff_is_local_label_name (bfd *, const char *);
38 extern reloc_howto_type *_bfd_xcoff_reloc_type_lookup
39 (bfd *, bfd_reloc_code_real_type);
40 extern bfd_boolean _bfd_xcoff_slurp_armap (bfd *);
41 extern const bfd_target *_bfd_xcoff_archive_p (bfd *);
42 extern void * _bfd_xcoff_read_ar_hdr (bfd *);
43 extern bfd *_bfd_xcoff_openr_next_archived_file (bfd *, bfd *);
44 extern int _bfd_xcoff_stat_arch_elt (bfd *, struct stat *);
45 extern bfd_boolean _bfd_xcoff_write_armap
46 (bfd *, unsigned int, struct orl *, unsigned int, int);
47 extern bfd_boolean _bfd_xcoff_write_archive_contents (bfd *);
48 extern int _bfd_xcoff_sizeof_headers (bfd *, struct bfd_link_info *);
49 extern void _bfd_xcoff_swap_sym_in (bfd *, void *, void *);
50 extern unsigned int _bfd_xcoff_swap_sym_out (bfd *, void *, void *);
51 extern void _bfd_xcoff_swap_aux_in (bfd *, void *, int, int, int, int, void *);
52 extern unsigned int _bfd_xcoff_swap_aux_out
53 (bfd *, void *, int, int, int, int, void *);
54 static void xcoff_swap_reloc_in (bfd *, void *, void *);
55 static unsigned int xcoff_swap_reloc_out (bfd *, void *, void *);
56
57 /* Forward declare xcoff_rtype2howto for coffcode.h macro. */
58 void xcoff_rtype2howto (arelent *, struct internal_reloc *);
59
60 /* coffcode.h needs these to be defined. */
61 #define RS6000COFF_C 1
62
63 #define SELECT_RELOC(internal, howto) \
64 { \
65 internal.r_type = howto->type; \
66 internal.r_size = \
67 ((howto->complain_on_overflow == complain_overflow_signed \
68 ? 0x80 \
69 : 0) \
70 | (howto->bitsize - 1)); \
71 }
72
73 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
74 #define COFF_LONG_FILENAMES
75 #define NO_COFF_SYMBOLS
76 #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst)
77 #define coff_mkobject _bfd_xcoff_mkobject
78 #define coff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
79 #ifdef AIX_CORE
80 extern const bfd_target * rs6000coff_core_p (bfd *abfd);
81 extern bfd_boolean rs6000coff_core_file_matches_executable_p
82 (bfd *cbfd, bfd *ebfd);
83 extern char *rs6000coff_core_file_failing_command (bfd *abfd);
84 extern int rs6000coff_core_file_failing_signal (bfd *abfd);
85 #define CORE_FILE_P rs6000coff_core_p
86 #define coff_core_file_failing_command \
87 rs6000coff_core_file_failing_command
88 #define coff_core_file_failing_signal \
89 rs6000coff_core_file_failing_signal
90 #define coff_core_file_matches_executable_p \
91 rs6000coff_core_file_matches_executable_p
92 #define coff_core_file_pid \
93 _bfd_nocore_core_file_pid
94 #else
95 #define CORE_FILE_P _bfd_dummy_target
96 #define coff_core_file_failing_command \
97 _bfd_nocore_core_file_failing_command
98 #define coff_core_file_failing_signal \
99 _bfd_nocore_core_file_failing_signal
100 #define coff_core_file_matches_executable_p \
101 _bfd_nocore_core_file_matches_executable_p
102 #define coff_core_file_pid \
103 _bfd_nocore_core_file_pid
104 #endif
105 #define coff_SWAP_sym_in _bfd_xcoff_swap_sym_in
106 #define coff_SWAP_sym_out _bfd_xcoff_swap_sym_out
107 #define coff_SWAP_aux_in _bfd_xcoff_swap_aux_in
108 #define coff_SWAP_aux_out _bfd_xcoff_swap_aux_out
109 #define coff_swap_reloc_in xcoff_swap_reloc_in
110 #define coff_swap_reloc_out xcoff_swap_reloc_out
111 #define NO_COFF_RELOCS
112
113 #ifndef bfd_pe_print_pdata
114 #define bfd_pe_print_pdata NULL
115 #endif
116
117 #include <stdint.h>
118 #include "coffcode.h"
119
120 /* The main body of code is in coffcode.h. */
121
122 static const char *normalize_filename (bfd *);
123 static bfd_boolean xcoff_write_armap_old
124 (bfd *, unsigned int, struct orl *, unsigned int, int);
125 static bfd_boolean xcoff_write_armap_big
126 (bfd *, unsigned int, struct orl *, unsigned int, int);
127 static bfd_boolean xcoff_write_archive_contents_old (bfd *);
128 static bfd_boolean xcoff_write_archive_contents_big (bfd *);
129 static void xcoff_swap_ldhdr_in (bfd *, const void *, struct internal_ldhdr *);
130 static void xcoff_swap_ldhdr_out (bfd *, const struct internal_ldhdr *, void *);
131 static void xcoff_swap_ldsym_in (bfd *, const void *, struct internal_ldsym *);
132 static void xcoff_swap_ldsym_out (bfd *, const struct internal_ldsym *, void *);
133 static void xcoff_swap_ldrel_in (bfd *, const void *, struct internal_ldrel *);
134 static void xcoff_swap_ldrel_out (bfd *, const struct internal_ldrel *, void *);
135 static bfd_boolean xcoff_ppc_relocate_section
136 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
137 struct internal_reloc *, struct internal_syment *, asection **);
138 static bfd_boolean _bfd_xcoff_put_ldsymbol_name
139 (bfd *, struct xcoff_loader_info *, struct internal_ldsym *, const char *);
140 static asection *xcoff_create_csect_from_smclas
141 (bfd *, union internal_auxent *, const char *);
142 static bfd_boolean xcoff_is_lineno_count_overflow (bfd *, bfd_vma);
143 static bfd_boolean xcoff_is_reloc_count_overflow (bfd *, bfd_vma);
144 static bfd_vma xcoff_loader_symbol_offset (bfd *, struct internal_ldhdr *);
145 static bfd_vma xcoff_loader_reloc_offset (bfd *, struct internal_ldhdr *);
146 static bfd_boolean xcoff_generate_rtinit
147 (bfd *, const char *, const char *, bfd_boolean);
148 static bfd_boolean do_pad (bfd *, unsigned int);
149 static bfd_boolean do_copy (bfd *, bfd *);
150
151 /* Relocation functions */
152 static bfd_boolean xcoff_reloc_type_br (XCOFF_RELOC_FUNCTION_ARGS);
153
154 static bfd_boolean xcoff_complain_overflow_dont_func
155 (XCOFF_COMPLAIN_FUNCTION_ARGS);
156 static bfd_boolean xcoff_complain_overflow_bitfield_func
157 (XCOFF_COMPLAIN_FUNCTION_ARGS);
158 static bfd_boolean xcoff_complain_overflow_signed_func
159 (XCOFF_COMPLAIN_FUNCTION_ARGS);
160 static bfd_boolean xcoff_complain_overflow_unsigned_func
161 (XCOFF_COMPLAIN_FUNCTION_ARGS);
162
163 bfd_boolean (*xcoff_calculate_relocation[XCOFF_MAX_CALCULATE_RELOCATION])
164 (XCOFF_RELOC_FUNCTION_ARGS) =
165 {
166 xcoff_reloc_type_pos, /* R_POS (0x00) */
167 xcoff_reloc_type_neg, /* R_NEG (0x01) */
168 xcoff_reloc_type_rel, /* R_REL (0x02) */
169 xcoff_reloc_type_toc, /* R_TOC (0x03) */
170 xcoff_reloc_type_fail, /* R_RTB (0x04) */
171 xcoff_reloc_type_toc, /* R_GL (0x05) */
172 xcoff_reloc_type_toc, /* R_TCL (0x06) */
173 xcoff_reloc_type_fail, /* (0x07) */
174 xcoff_reloc_type_ba, /* R_BA (0x08) */
175 xcoff_reloc_type_fail, /* (0x09) */
176 xcoff_reloc_type_br, /* R_BR (0x0a) */
177 xcoff_reloc_type_fail, /* (0x0b) */
178 xcoff_reloc_type_pos, /* R_RL (0x0c) */
179 xcoff_reloc_type_pos, /* R_RLA (0x0d) */
180 xcoff_reloc_type_fail, /* (0x0e) */
181 xcoff_reloc_type_noop, /* R_REF (0x0f) */
182 xcoff_reloc_type_fail, /* (0x10) */
183 xcoff_reloc_type_fail, /* (0x11) */
184 xcoff_reloc_type_toc, /* R_TRL (0x12) */
185 xcoff_reloc_type_toc, /* R_TRLA (0x13) */
186 xcoff_reloc_type_fail, /* R_RRTBI (0x14) */
187 xcoff_reloc_type_fail, /* R_RRTBA (0x15) */
188 xcoff_reloc_type_ba, /* R_CAI (0x16) */
189 xcoff_reloc_type_crel, /* R_CREL (0x17) */
190 xcoff_reloc_type_ba, /* R_RBA (0x18) */
191 xcoff_reloc_type_ba, /* R_RBAC (0x19) */
192 xcoff_reloc_type_br, /* R_RBR (0x1a) */
193 xcoff_reloc_type_ba, /* R_RBRC (0x1b) */
194 };
195
196 bfd_boolean (*xcoff_complain_overflow[XCOFF_MAX_COMPLAIN_OVERFLOW])
197 (XCOFF_COMPLAIN_FUNCTION_ARGS) =
198 {
199 xcoff_complain_overflow_dont_func,
200 xcoff_complain_overflow_bitfield_func,
201 xcoff_complain_overflow_signed_func,
202 xcoff_complain_overflow_unsigned_func,
203 };
204
205 /* Information about one member of an archive. */
206 struct member_layout
207 {
208 /* The archive member that this structure describes. */
209 bfd *member;
210
211 /* The number of bytes of padding that must be inserted before the
212 start of the member in order to ensure that the section contents
213 are correctly aligned. */
214 unsigned int leading_padding;
215
216 /* The offset of MEMBER from the start of the archive (i.e. the end
217 of the leading padding). */
218 file_ptr offset;
219
220 /* The normalized name of MEMBER. */
221 const char *name;
222
223 /* The length of NAME, without padding. */
224 bfd_size_type namlen;
225
226 /* The length of NAME, with padding. */
227 bfd_size_type padded_namlen;
228
229 /* The size of MEMBER's header, including the name and magic sequence. */
230 bfd_size_type header_size;
231
232 /* The size of the MEMBER's contents. */
233 bfd_size_type contents_size;
234
235 /* The number of bytes of padding that must be inserted after MEMBER
236 in order to preserve even alignment. */
237 bfd_size_type trailing_padding;
238 };
239
240 /* A structure used for iterating over the members of an archive. */
241 struct archive_iterator
242 {
243 /* The archive itself. */
244 bfd *archive;
245
246 /* Information about the current archive member. */
247 struct member_layout current;
248
249 /* Information about the next archive member. MEMBER is null if there
250 are no more archive members, in which case OFFSET is the offset of
251 the first unused byte. */
252 struct member_layout next;
253 };
254
255 /* Initialize INFO so that it describes member MEMBER of archive ARCHIVE.
256 OFFSET is the even-padded offset of MEMBER, not including any leading
257 padding needed for section alignment. */
258
259 static void
260 member_layout_init (struct member_layout *info, bfd *archive,
261 bfd *member, file_ptr offset)
262 {
263 info->member = member;
264 info->leading_padding = 0;
265 if (member)
266 {
267 info->name = normalize_filename (member);
268 info->namlen = strlen (info->name);
269 info->padded_namlen = info->namlen + (info->namlen & 1);
270 if (xcoff_big_format_p (archive))
271 info->header_size = SIZEOF_AR_HDR_BIG;
272 else
273 info->header_size = SIZEOF_AR_HDR;
274 info->header_size += info->padded_namlen + SXCOFFARFMAG;
275 info->contents_size = arelt_size (member);
276 info->trailing_padding = info->contents_size & 1;
277
278 if (bfd_check_format (member, bfd_object)
279 && bfd_get_flavour (member) == bfd_target_xcoff_flavour
280 && (member->flags & DYNAMIC) != 0)
281 info->leading_padding
282 = (-(offset + info->header_size)
283 & ((1 << bfd_xcoff_text_align_power (member)) - 1));
284 }
285 info->offset = offset + info->leading_padding;
286 }
287
288 /* Set up ITERATOR to iterate through archive ARCHIVE. */
289
290 static void
291 archive_iterator_begin (struct archive_iterator *iterator,
292 bfd *archive)
293 {
294 iterator->archive = archive;
295 member_layout_init (&iterator->next, archive, archive->archive_head,
296 xcoff_big_format_p (archive)
297 ? SIZEOF_AR_FILE_HDR_BIG
298 : SIZEOF_AR_FILE_HDR);
299 }
300
301 /* Make ITERATOR visit the first unvisited archive member. Return true
302 on success; return false if all members have been visited. */
303
304 static bfd_boolean
305 archive_iterator_next (struct archive_iterator *iterator)
306 {
307 if (!iterator->next.member)
308 return FALSE;
309
310 iterator->current = iterator->next;
311 member_layout_init (&iterator->next, iterator->archive,
312 iterator->current.member->archive_next,
313 iterator->current.offset
314 + iterator->current.header_size
315 + iterator->current.contents_size
316 + iterator->current.trailing_padding);
317 return TRUE;
318 }
319
320 /* We use our own tdata type. Its first field is the COFF tdata type,
321 so the COFF routines are compatible. */
322
323 bfd_boolean
324 _bfd_xcoff_mkobject (bfd *abfd)
325 {
326 coff_data_type *coff;
327 bfd_size_type amt = sizeof (struct xcoff_tdata);
328
329 abfd->tdata.xcoff_obj_data = (struct xcoff_tdata *) bfd_zalloc (abfd, amt);
330 if (abfd->tdata.xcoff_obj_data == NULL)
331 return FALSE;
332 coff = coff_data (abfd);
333 coff->symbols = (coff_symbol_type *) NULL;
334 coff->conversion_table = (unsigned int *) NULL;
335 coff->raw_syments = (struct coff_ptr_struct *) NULL;
336 coff->relocbase = 0;
337
338 xcoff_data (abfd)->modtype = ('1' << 8) | 'L';
339
340 /* We set cputype to -1 to indicate that it has not been
341 initialized. */
342 xcoff_data (abfd)->cputype = -1;
343
344 xcoff_data (abfd)->csects = NULL;
345 xcoff_data (abfd)->debug_indices = NULL;
346
347 /* text section alignment is different than the default */
348 bfd_xcoff_text_align_power (abfd) = 2;
349
350 return TRUE;
351 }
352
353 /* Copy XCOFF data from one BFD to another. */
354
355 bfd_boolean
356 _bfd_xcoff_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
357 {
358 struct xcoff_tdata *ix, *ox;
359 asection *sec;
360
361 if (ibfd->xvec != obfd->xvec)
362 return TRUE;
363 ix = xcoff_data (ibfd);
364 ox = xcoff_data (obfd);
365 ox->full_aouthdr = ix->full_aouthdr;
366 ox->toc = ix->toc;
367 if (ix->sntoc == 0)
368 ox->sntoc = 0;
369 else
370 {
371 sec = coff_section_from_bfd_index (ibfd, ix->sntoc);
372 if (sec == NULL)
373 ox->sntoc = 0;
374 else
375 ox->sntoc = sec->output_section->target_index;
376 }
377 if (ix->snentry == 0)
378 ox->snentry = 0;
379 else
380 {
381 sec = coff_section_from_bfd_index (ibfd, ix->snentry);
382 if (sec == NULL)
383 ox->snentry = 0;
384 else
385 ox->snentry = sec->output_section->target_index;
386 }
387 bfd_xcoff_text_align_power (obfd) = bfd_xcoff_text_align_power (ibfd);
388 bfd_xcoff_data_align_power (obfd) = bfd_xcoff_data_align_power (ibfd);
389 ox->modtype = ix->modtype;
390 ox->cputype = ix->cputype;
391 ox->maxdata = ix->maxdata;
392 ox->maxstack = ix->maxstack;
393 return TRUE;
394 }
395
396 /* I don't think XCOFF really has a notion of local labels based on
397 name. This will mean that ld -X doesn't actually strip anything.
398 The AIX native linker does not have a -X option, and it ignores the
399 -x option. */
400
401 bfd_boolean
402 _bfd_xcoff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
403 const char *name ATTRIBUTE_UNUSED)
404 {
405 return FALSE;
406 }
407
408 void
410 _bfd_xcoff_swap_sym_in (bfd *abfd, void * ext1, void * in1)
411 {
412 SYMENT *ext = (SYMENT *)ext1;
413 struct internal_syment * in = (struct internal_syment *)in1;
414
415 if (ext->e.e_name[0] != 0)
416 {
417 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
418 }
419 else
420 {
421 in->_n._n_n._n_zeroes = 0;
422 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
423 }
424
425 in->n_value = H_GET_32 (abfd, ext->e_value);
426 in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
427 in->n_type = H_GET_16 (abfd, ext->e_type);
428 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
429 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
430 }
431
432 unsigned int
433 _bfd_xcoff_swap_sym_out (bfd *abfd, void * inp, void * extp)
434 {
435 struct internal_syment *in = (struct internal_syment *)inp;
436 SYMENT *ext =(SYMENT *)extp;
437
438 if (in->_n._n_name[0] != 0)
439 {
440 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
441 }
442 else
443 {
444 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
445 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
446 }
447
448 H_PUT_32 (abfd, in->n_value, ext->e_value);
449 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
450 H_PUT_16 (abfd, in->n_type, ext->e_type);
451 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
452 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
453 return bfd_coff_symesz (abfd);
454 }
455
456 void
457 _bfd_xcoff_swap_aux_in (bfd *abfd, void * ext1, int type, int in_class,
458 int indx, int numaux, void * in1)
459 {
460 AUXENT * ext = (AUXENT *)ext1;
461 union internal_auxent *in = (union internal_auxent *)in1;
462
463 switch (in_class)
464 {
465 case C_FILE:
466 if (ext->x_file.x_n.x_fname[0] == 0)
467 {
468 in->x_file.x_n.x_zeroes = 0;
469 in->x_file.x_n.x_offset =
470 H_GET_32 (abfd, ext->x_file.x_n.x_n.x_offset);
471 }
472 else
473 {
474 if (numaux > 1)
475 {
476 if (indx == 0)
477 memcpy (in->x_file.x_fname, ext->x_file.x_n.x_fname,
478 numaux * sizeof (AUXENT));
479 }
480 else
481 {
482 memcpy (in->x_file.x_fname, ext->x_file.x_n.x_fname, FILNMLEN);
483 }
484 }
485 goto end;
486
487 /* RS/6000 "csect" auxents */
488 case C_EXT:
489 case C_AIX_WEAKEXT:
490 case C_HIDEXT:
491 if (indx + 1 == numaux)
492 {
493 in->x_csect.x_scnlen.l = H_GET_32 (abfd, ext->x_csect.x_scnlen);
494 in->x_csect.x_parmhash = H_GET_32 (abfd, ext->x_csect.x_parmhash);
495 in->x_csect.x_snhash = H_GET_16 (abfd, ext->x_csect.x_snhash);
496 /* We don't have to hack bitfields in x_smtyp because it's
497 defined by shifts-and-ands, which are equivalent on all
498 byte orders. */
499 in->x_csect.x_smtyp = H_GET_8 (abfd, ext->x_csect.x_smtyp);
500 in->x_csect.x_smclas = H_GET_8 (abfd, ext->x_csect.x_smclas);
501 in->x_csect.x_stab = H_GET_32 (abfd, ext->x_csect.x_stab);
502 in->x_csect.x_snstab = H_GET_16 (abfd, ext->x_csect.x_snstab);
503 goto end;
504 }
505 break;
506
507 case C_STAT:
508 case C_LEAFSTAT:
509 case C_HIDDEN:
510 if (type == T_NULL)
511 {
512 in->x_scn.x_scnlen = H_GET_32 (abfd, ext->x_scn.x_scnlen);
513 in->x_scn.x_nreloc = H_GET_16 (abfd, ext->x_scn.x_nreloc);
514 in->x_scn.x_nlinno = H_GET_16 (abfd, ext->x_scn.x_nlinno);
515 /* PE defines some extra fields; we zero them out for
516 safety. */
517 in->x_scn.x_checksum = 0;
518 in->x_scn.x_associated = 0;
519 in->x_scn.x_comdat = 0;
520
521 goto end;
522 }
523 break;
524 }
525
526 in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
527 in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
528
529 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
530 || ISTAG (in_class))
531 {
532 in->x_sym.x_fcnary.x_fcn.x_lnnoptr =
533 H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
534 in->x_sym.x_fcnary.x_fcn.x_endndx.l =
535 H_GET_32 (abfd, ext->x_sym.x_fcnary.x_fcn.x_endndx);
536 }
537 else
538 {
539 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
540 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
541 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
542 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
543 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
544 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
545 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
546 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
547 }
548
549 if (ISFCN (type))
550 {
551 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
552 }
553 else
554 {
555 in->x_sym.x_misc.x_lnsz.x_lnno =
556 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_lnno);
557 in->x_sym.x_misc.x_lnsz.x_size =
558 H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size);
559 }
560
561 end: ;
562 /* The semicolon is because MSVC doesn't like labels at
563 end of block. */
564 }
565
566 unsigned int
567 _bfd_xcoff_swap_aux_out (bfd *abfd, void * inp, int type, int in_class,
568 int indx ATTRIBUTE_UNUSED,
569 int numaux ATTRIBUTE_UNUSED,
570 void * extp)
571 {
572 union internal_auxent *in = (union internal_auxent *)inp;
573 AUXENT *ext = (AUXENT *)extp;
574
575 memset (ext, 0, bfd_coff_auxesz (abfd));
576 switch (in_class)
577 {
578 case C_FILE:
579 if (in->x_file.x_fname[0] == 0)
580 {
581 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_n.x_zeroes);
582 H_PUT_32 (abfd, in->x_file.x_n.x_offset,
583 ext->x_file.x_n.x_n.x_offset);
584 }
585 else
586 {
587 memcpy (ext->x_file.x_n.x_fname, in->x_file.x_fname, FILNMLEN);
588 }
589 goto end;
590
591 /* RS/6000 "csect" auxents */
592 case C_EXT:
593 case C_AIX_WEAKEXT:
594 case C_HIDEXT:
595 if (indx + 1 == numaux)
596 {
597 H_PUT_32 (abfd, in->x_csect.x_scnlen.l, ext->x_csect.x_scnlen);
598 H_PUT_32 (abfd, in->x_csect.x_parmhash, ext->x_csect.x_parmhash);
599 H_PUT_16 (abfd, in->x_csect.x_snhash, ext->x_csect.x_snhash);
600 /* We don't have to hack bitfields in x_smtyp because it's
601 defined by shifts-and-ands, which are equivalent on all
602 byte orders. */
603 H_PUT_8 (abfd, in->x_csect.x_smtyp, ext->x_csect.x_smtyp);
604 H_PUT_8 (abfd, in->x_csect.x_smclas, ext->x_csect.x_smclas);
605 H_PUT_32 (abfd, in->x_csect.x_stab, ext->x_csect.x_stab);
606 H_PUT_16 (abfd, in->x_csect.x_snstab, ext->x_csect.x_snstab);
607 goto end;
608 }
609 break;
610
611 case C_STAT:
612 case C_LEAFSTAT:
613 case C_HIDDEN:
614 if (type == T_NULL)
615 {
616 H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->x_scn.x_scnlen);
617 H_PUT_16 (abfd, in->x_scn.x_nreloc, ext->x_scn.x_nreloc);
618 H_PUT_16 (abfd, in->x_scn.x_nlinno, ext->x_scn.x_nlinno);
619 goto end;
620 }
621 break;
622 }
623
624 H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
625 H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
626
627 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
628 || ISTAG (in_class))
629 {
630 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,
631 ext->x_sym.x_fcnary.x_fcn.x_lnnoptr);
632 H_PUT_32 (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l,
633 ext->x_sym.x_fcnary.x_fcn.x_endndx);
634 }
635 else
636 {
637 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
638 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
639 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
640 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
641 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
642 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
643 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
644 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
645 }
646
647 if (ISFCN (type))
648 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
649 else
650 {
651 H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_lnno,
652 ext->x_sym.x_misc.x_lnsz.x_lnno);
653 H_PUT_16 (abfd, in->x_sym.x_misc.x_lnsz.x_size,
654 ext->x_sym.x_misc.x_lnsz.x_size);
655 }
656
657 end:
658 return bfd_coff_auxesz (abfd);
659 }
660
661 /* The XCOFF reloc table. Actually, XCOFF relocations specify the
663 bitsize and whether they are signed or not, along with a
664 conventional type. This table is for the types, which are used for
665 different algorithms for putting in the reloc. Many of these
666 relocs need special_function entries, which I have not written. */
667
668 reloc_howto_type xcoff_howto_table[] =
669 {
670 /* 0x00: Standard 32 bit relocation. */
671 HOWTO (R_POS, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 32, /* bitsize */
675 FALSE, /* pc_relative */
676 0, /* bitpos */
677 complain_overflow_bitfield, /* complain_on_overflow */
678 0, /* special_function */
679 "R_POS", /* name */
680 TRUE, /* partial_inplace */
681 0xffffffff, /* src_mask */
682 0xffffffff, /* dst_mask */
683 FALSE), /* pcrel_offset */
684
685 /* 0x01: 32 bit relocation, but store negative value. */
686 HOWTO (R_NEG, /* type */
687 0, /* rightshift */
688 -2, /* size (0 = byte, 1 = short, 2 = long) */
689 32, /* bitsize */
690 FALSE, /* pc_relative */
691 0, /* bitpos */
692 complain_overflow_bitfield, /* complain_on_overflow */
693 0, /* special_function */
694 "R_NEG", /* name */
695 TRUE, /* partial_inplace */
696 0xffffffff, /* src_mask */
697 0xffffffff, /* dst_mask */
698 FALSE), /* pcrel_offset */
699
700 /* 0x02: 32 bit PC relative relocation. */
701 HOWTO (R_REL, /* type */
702 0, /* rightshift */
703 2, /* size (0 = byte, 1 = short, 2 = long) */
704 32, /* bitsize */
705 TRUE, /* pc_relative */
706 0, /* bitpos */
707 complain_overflow_signed, /* complain_on_overflow */
708 0, /* special_function */
709 "R_REL", /* name */
710 TRUE, /* partial_inplace */
711 0xffffffff, /* src_mask */
712 0xffffffff, /* dst_mask */
713 FALSE), /* pcrel_offset */
714
715 /* 0x03: 16 bit TOC relative relocation. */
716 HOWTO (R_TOC, /* type */
717 0, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
720 FALSE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_bitfield, /* complain_on_overflow */
723 0, /* special_function */
724 "R_TOC", /* name */
725 TRUE, /* partial_inplace */
726 0xffff, /* src_mask */
727 0xffff, /* dst_mask */
728 FALSE), /* pcrel_offset */
729
730 /* 0x04: I don't really know what this is. */
731 HOWTO (R_RTB, /* type */
732 1, /* rightshift */
733 2, /* size (0 = byte, 1 = short, 2 = long) */
734 32, /* bitsize */
735 FALSE, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_bitfield, /* complain_on_overflow */
738 0, /* special_function */
739 "R_RTB", /* name */
740 TRUE, /* partial_inplace */
741 0xffffffff, /* src_mask */
742 0xffffffff, /* dst_mask */
743 FALSE), /* pcrel_offset */
744
745 /* 0x05: External TOC relative symbol. */
746 HOWTO (R_GL, /* type */
747 0, /* rightshift */
748 1, /* size (0 = byte, 1 = short, 2 = long) */
749 16, /* bitsize */
750 FALSE, /* pc_relative */
751 0, /* bitpos */
752 complain_overflow_bitfield, /* complain_on_overflow */
753 0, /* special_function */
754 "R_GL", /* name */
755 TRUE, /* partial_inplace */
756 0xffff, /* src_mask */
757 0xffff, /* dst_mask */
758 FALSE), /* pcrel_offset */
759
760 /* 0x06: Local TOC relative symbol. */
761 HOWTO (R_TCL, /* type */
762 0, /* rightshift */
763 1, /* size (0 = byte, 1 = short, 2 = long) */
764 16, /* bitsize */
765 FALSE, /* pc_relative */
766 0, /* bitpos */
767 complain_overflow_bitfield, /* complain_on_overflow */
768 0, /* special_function */
769 "R_TCL", /* name */
770 TRUE, /* partial_inplace */
771 0xffff, /* src_mask */
772 0xffff, /* dst_mask */
773 FALSE), /* pcrel_offset */
774
775 EMPTY_HOWTO (7),
776
777 /* 0x08: Non modifiable absolute branch. */
778 HOWTO (R_BA, /* type */
779 0, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 26, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_bitfield, /* complain_on_overflow */
785 0, /* special_function */
786 "R_BA_26", /* name */
787 TRUE, /* partial_inplace */
788 0x03fffffc, /* src_mask */
789 0x03fffffc, /* dst_mask */
790 FALSE), /* pcrel_offset */
791
792 EMPTY_HOWTO (9),
793
794 /* 0x0a: Non modifiable relative branch. */
795 HOWTO (R_BR, /* type */
796 0, /* rightshift */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
798 26, /* bitsize */
799 TRUE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_signed, /* complain_on_overflow */
802 0, /* special_function */
803 "R_BR", /* name */
804 TRUE, /* partial_inplace */
805 0x03fffffc, /* src_mask */
806 0x03fffffc, /* dst_mask */
807 FALSE), /* pcrel_offset */
808
809 EMPTY_HOWTO (0xb),
810
811 /* 0x0c: Indirect load. */
812 HOWTO (R_RL, /* type */
813 0, /* rightshift */
814 1, /* size (0 = byte, 1 = short, 2 = long) */
815 16, /* bitsize */
816 FALSE, /* pc_relative */
817 0, /* bitpos */
818 complain_overflow_bitfield, /* complain_on_overflow */
819 0, /* special_function */
820 "R_RL", /* name */
821 TRUE, /* partial_inplace */
822 0xffff, /* src_mask */
823 0xffff, /* dst_mask */
824 FALSE), /* pcrel_offset */
825
826 /* 0x0d: Load address. */
827 HOWTO (R_RLA, /* type */
828 0, /* rightshift */
829 1, /* size (0 = byte, 1 = short, 2 = long) */
830 16, /* bitsize */
831 FALSE, /* pc_relative */
832 0, /* bitpos */
833 complain_overflow_bitfield, /* complain_on_overflow */
834 0, /* special_function */
835 "R_RLA", /* name */
836 TRUE, /* partial_inplace */
837 0xffff, /* src_mask */
838 0xffff, /* dst_mask */
839 FALSE), /* pcrel_offset */
840
841 EMPTY_HOWTO (0xe),
842
843 /* 0x0f: Non-relocating reference. Bitsize is 1 so that r_rsize is 0. */
844 HOWTO (R_REF, /* type */
845 0, /* rightshift */
846 0, /* size (0 = byte, 1 = short, 2 = long) */
847 1, /* bitsize */
848 FALSE, /* pc_relative */
849 0, /* bitpos */
850 complain_overflow_dont, /* complain_on_overflow */
851 0, /* special_function */
852 "R_REF", /* name */
853 FALSE, /* partial_inplace */
854 0, /* src_mask */
855 0, /* dst_mask */
856 FALSE), /* pcrel_offset */
857
858 EMPTY_HOWTO (0x10),
859 EMPTY_HOWTO (0x11),
860
861 /* 0x12: TOC relative indirect load. */
862 HOWTO (R_TRL, /* type */
863 0, /* rightshift */
864 1, /* size (0 = byte, 1 = short, 2 = long) */
865 16, /* bitsize */
866 FALSE, /* pc_relative */
867 0, /* bitpos */
868 complain_overflow_bitfield, /* complain_on_overflow */
869 0, /* special_function */
870 "R_TRL", /* name */
871 TRUE, /* partial_inplace */
872 0xffff, /* src_mask */
873 0xffff, /* dst_mask */
874 FALSE), /* pcrel_offset */
875
876 /* 0x13: TOC relative load address. */
877 HOWTO (R_TRLA, /* type */
878 0, /* rightshift */
879 1, /* size (0 = byte, 1 = short, 2 = long) */
880 16, /* bitsize */
881 FALSE, /* pc_relative */
882 0, /* bitpos */
883 complain_overflow_bitfield, /* complain_on_overflow */
884 0, /* special_function */
885 "R_TRLA", /* name */
886 TRUE, /* partial_inplace */
887 0xffff, /* src_mask */
888 0xffff, /* dst_mask */
889 FALSE), /* pcrel_offset */
890
891 /* 0x14: Modifiable relative branch. */
892 HOWTO (R_RRTBI, /* type */
893 1, /* rightshift */
894 2, /* size (0 = byte, 1 = short, 2 = long) */
895 32, /* bitsize */
896 FALSE, /* pc_relative */
897 0, /* bitpos */
898 complain_overflow_bitfield, /* complain_on_overflow */
899 0, /* special_function */
900 "R_RRTBI", /* name */
901 TRUE, /* partial_inplace */
902 0xffffffff, /* src_mask */
903 0xffffffff, /* dst_mask */
904 FALSE), /* pcrel_offset */
905
906 /* 0x15: Modifiable absolute branch. */
907 HOWTO (R_RRTBA, /* type */
908 1, /* rightshift */
909 2, /* size (0 = byte, 1 = short, 2 = long) */
910 32, /* bitsize */
911 FALSE, /* pc_relative */
912 0, /* bitpos */
913 complain_overflow_bitfield, /* complain_on_overflow */
914 0, /* special_function */
915 "R_RRTBA", /* name */
916 TRUE, /* partial_inplace */
917 0xffffffff, /* src_mask */
918 0xffffffff, /* dst_mask */
919 FALSE), /* pcrel_offset */
920
921 /* 0x16: Modifiable call absolute indirect. */
922 HOWTO (R_CAI, /* type */
923 0, /* rightshift */
924 1, /* size (0 = byte, 1 = short, 2 = long) */
925 16, /* bitsize */
926 FALSE, /* pc_relative */
927 0, /* bitpos */
928 complain_overflow_bitfield, /* complain_on_overflow */
929 0, /* special_function */
930 "R_CAI", /* name */
931 TRUE, /* partial_inplace */
932 0xffff, /* src_mask */
933 0xffff, /* dst_mask */
934 FALSE), /* pcrel_offset */
935
936 /* 0x17: Modifiable call relative. */
937 HOWTO (R_CREL, /* type */
938 0, /* rightshift */
939 1, /* size (0 = byte, 1 = short, 2 = long) */
940 16, /* bitsize */
941 FALSE, /* pc_relative */
942 0, /* bitpos */
943 complain_overflow_bitfield, /* complain_on_overflow */
944 0, /* special_function */
945 "R_CREL", /* name */
946 TRUE, /* partial_inplace */
947 0xffff, /* src_mask */
948 0xffff, /* dst_mask */
949 FALSE), /* pcrel_offset */
950
951 /* 0x18: Modifiable branch absolute. */
952 HOWTO (R_RBA, /* type */
953 0, /* rightshift */
954 2, /* size (0 = byte, 1 = short, 2 = long) */
955 26, /* bitsize */
956 FALSE, /* pc_relative */
957 0, /* bitpos */
958 complain_overflow_bitfield, /* complain_on_overflow */
959 0, /* special_function */
960 "R_RBA", /* name */
961 TRUE, /* partial_inplace */
962 0x03fffffc, /* src_mask */
963 0x03fffffc, /* dst_mask */
964 FALSE), /* pcrel_offset */
965
966 /* 0x19: Modifiable branch absolute. */
967 HOWTO (R_RBAC, /* type */
968 0, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 32, /* bitsize */
971 FALSE, /* pc_relative */
972 0, /* bitpos */
973 complain_overflow_bitfield, /* complain_on_overflow */
974 0, /* special_function */
975 "R_RBAC", /* name */
976 TRUE, /* partial_inplace */
977 0xffffffff, /* src_mask */
978 0xffffffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
980
981 /* 0x1a: Modifiable branch relative. */
982 HOWTO (R_RBR, /* type */
983 0, /* rightshift */
984 2, /* size (0 = byte, 1 = short, 2 = long) */
985 26, /* bitsize */
986 FALSE, /* pc_relative */
987 0, /* bitpos */
988 complain_overflow_signed, /* complain_on_overflow */
989 0, /* special_function */
990 "R_RBR_26", /* name */
991 TRUE, /* partial_inplace */
992 0x03fffffc, /* src_mask */
993 0x03fffffc, /* dst_mask */
994 FALSE), /* pcrel_offset */
995
996 /* 0x1b: Modifiable branch absolute. */
997 HOWTO (R_RBRC, /* type */
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_bitfield, /* complain_on_overflow */
1004 0, /* special_function */
1005 "R_RBRC", /* name */
1006 TRUE, /* partial_inplace */
1007 0xffff, /* src_mask */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1010
1011 /* 0x1c: 16 bit Non modifiable absolute branch. */
1012 HOWTO (R_BA, /* type */
1013 0, /* rightshift */
1014 1, /* size (0 = byte, 1 = short, 2 = long) */
1015 16, /* bitsize */
1016 FALSE, /* pc_relative */
1017 0, /* bitpos */
1018 complain_overflow_bitfield, /* complain_on_overflow */
1019 0, /* special_function */
1020 "R_BA_16", /* name */
1021 TRUE, /* partial_inplace */
1022 0xfffc, /* src_mask */
1023 0xfffc, /* dst_mask */
1024 FALSE), /* pcrel_offset */
1025
1026 /* 0x1d: Modifiable branch relative. */
1027 HOWTO (R_RBR, /* type */
1028 0, /* rightshift */
1029 1, /* size (0 = byte, 1 = short, 2 = long) */
1030 16, /* bitsize */
1031 TRUE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_signed, /* complain_on_overflow */
1034 0, /* special_function */
1035 "R_RBR_16", /* name */
1036 TRUE, /* partial_inplace */
1037 0xfffc, /* src_mask */
1038 0xfffc, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1040
1041 /* 0x1e: Modifiable branch relative. */
1042 HOWTO (R_RBA, /* type */
1043 0, /* rightshift */
1044 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 16, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_signed, /* complain_on_overflow */
1049 0, /* special_function */
1050 "R_RBA_16", /* name */
1051 TRUE, /* partial_inplace */
1052 0xffff, /* src_mask */
1053 0xffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1055 };
1056
1057 void
1058 xcoff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1059 {
1060 if (internal->r_type > R_RBRC)
1061 abort ();
1062
1063 /* Default howto layout works most of the time */
1064 relent->howto = &xcoff_howto_table[internal->r_type];
1065
1066 /* Special case some 16 bit reloc */
1067 if (15 == (internal->r_size & 0x1f))
1068 {
1069 if (R_BA == internal->r_type)
1070 relent->howto = &xcoff_howto_table[0x1c];
1071 else if (R_RBR == internal->r_type)
1072 relent->howto = &xcoff_howto_table[0x1d];
1073 else if (R_RBA == internal->r_type)
1074 relent->howto = &xcoff_howto_table[0x1e];
1075 }
1076
1077 /* The r_size field of an XCOFF reloc encodes the bitsize of the
1078 relocation, as well as indicating whether it is signed or not.
1079 Doublecheck that the relocation information gathered from the
1080 type matches this information. The bitsize is not significant
1081 for R_REF relocs. */
1082 if (relent->howto->dst_mask != 0
1083 && (relent->howto->bitsize
1084 != ((unsigned int) internal->r_size & 0x1f) + 1))
1085 abort ();
1086 }
1087
1088 reloc_howto_type *
1089 _bfd_xcoff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1090 bfd_reloc_code_real_type code)
1091 {
1092 switch (code)
1093 {
1094 case BFD_RELOC_PPC_B26:
1095 return &xcoff_howto_table[0xa];
1096 case BFD_RELOC_PPC_BA16:
1097 return &xcoff_howto_table[0x1c];
1098 case BFD_RELOC_PPC_BA26:
1099 return &xcoff_howto_table[8];
1100 case BFD_RELOC_PPC_TOC16:
1101 return &xcoff_howto_table[3];
1102 case BFD_RELOC_16:
1103 /* Note that this relocation is only internally used by gas. */
1104 return &xcoff_howto_table[0xc];
1105 case BFD_RELOC_PPC_B16:
1106 return &xcoff_howto_table[0x1d];
1107 case BFD_RELOC_32:
1108 case BFD_RELOC_CTOR:
1109 return &xcoff_howto_table[0];
1110 case BFD_RELOC_NONE:
1111 return &xcoff_howto_table[0xf];
1112 default:
1113 return NULL;
1114 }
1115 }
1116
1117 static reloc_howto_type *
1118 _bfd_xcoff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1119 const char *r_name)
1120 {
1121 unsigned int i;
1122
1123 for (i = 0;
1124 i < sizeof (xcoff_howto_table) / sizeof (xcoff_howto_table[0]);
1125 i++)
1126 if (xcoff_howto_table[i].name != NULL
1127 && strcasecmp (xcoff_howto_table[i].name, r_name) == 0)
1128 return &xcoff_howto_table[i];
1129
1130 return NULL;
1131 }
1132
1133 /* XCOFF archive support. The original version of this code was by
1135 Damon A. Permezel. It was enhanced to permit cross support, and
1136 writing archive files, by Ian Lance Taylor, Cygnus Support.
1137
1138 XCOFF uses its own archive format. Everything is hooked together
1139 with file offset links, so it is possible to rapidly update an
1140 archive in place. Of course, we don't do that. An XCOFF archive
1141 has a real file header, not just an ARMAG string. The structure of
1142 the file header and of each archive header appear below.
1143
1144 An XCOFF archive also has a member table, which is a list of
1145 elements in the archive (you can get that by looking through the
1146 linked list, but you have to read a lot more of the file). The
1147 member table has a normal archive header with an empty name. It is
1148 normally (and perhaps must be) the second to last entry in the
1149 archive. The member table data is almost printable ASCII. It
1150 starts with a 12 character decimal string which is the number of
1151 entries in the table. For each entry it has a 12 character decimal
1152 string which is the offset in the archive of that member. These
1153 entries are followed by a series of null terminated strings which
1154 are the member names for each entry.
1155
1156 Finally, an XCOFF archive has a global symbol table, which is what
1157 we call the armap. The global symbol table has a normal archive
1158 header with an empty name. It is normally (and perhaps must be)
1159 the last entry in the archive. The contents start with a four byte
1160 binary number which is the number of entries. This is followed by
1161 a that many four byte binary numbers; each is the file offset of an
1162 entry in the archive. These numbers are followed by a series of
1163 null terminated strings, which are symbol names.
1164
1165 AIX 4.3 introduced a new archive format which can handle larger
1166 files and also 32- and 64-bit objects in the same archive. The
1167 things said above remain true except that there is now more than
1168 one global symbol table. The one is used to index 32-bit objects,
1169 the other for 64-bit objects.
1170
1171 The new archives (recognizable by the new ARMAG string) has larger
1172 field lengths so that we cannot really share any code. Also we have
1173 to take care that we are not generating the new form of archives
1174 on AIX 4.2 or earlier systems. */
1175
1176 /* XCOFF archives use this as a magic string. Note that both strings
1177 have the same length. */
1178
1179 /* Set the magic for archive. */
1180
1181 bfd_boolean
1182 bfd_xcoff_ar_archive_set_magic (bfd *abfd ATTRIBUTE_UNUSED,
1183 char *magic ATTRIBUTE_UNUSED)
1184 {
1185 /* Not supported yet. */
1186 return FALSE;
1187 /* bfd_xcoff_archive_set_magic (abfd, magic); */
1188 }
1189
1190 /* PR 21786: The PE/COFF standard does not require NUL termination for any of
1191 the ASCII fields in the archive headers. So in order to be able to extract
1192 numerical values we provide our own versions of strtol and strtoll which
1193 take a maximum length as an additional parameter. Also - just to save space,
1194 we omit the endptr return parameter, since we know that it is never used. */
1195
1196 static long
1197 _bfd_strntol (const char * nptr, int base, unsigned int maxlen)
1198 {
1199 char buf[24]; /* Should be enough. */
1200
1201 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1202
1203 memcpy (buf, nptr, maxlen);
1204 buf[maxlen] = 0;
1205 return strtol (buf, NULL, base);
1206 }
1207
1208 static long long
1209 _bfd_strntoll (const char * nptr, int base, unsigned int maxlen)
1210 {
1211 char buf[32]; /* Should be enough. */
1212
1213 BFD_ASSERT (maxlen < (sizeof (buf) - 1));
1214
1215 memcpy (buf, nptr, maxlen);
1216 buf[maxlen] = 0;
1217 return strtoll (buf, NULL, base);
1218 }
1219
1220 /* Macro to read an ASCII value stored in an archive header field. */
1221 #define GET_VALUE_IN_FIELD(VAR, FIELD) \
1222 do \
1223 { \
1224 (VAR) = sizeof (VAR) > sizeof (long) \
1225 ? _bfd_strntoll (FIELD, 10, sizeof FIELD) \
1226 : _bfd_strntol (FIELD, 10, sizeof FIELD); \
1227 } \
1228 while (0)
1229
1230 #define EQ_VALUE_IN_FIELD(VAR, FIELD) \
1231 (sizeof (VAR) > sizeof (long) \
1232 ? (VAR) ==_bfd_strntoll (FIELD, 10, sizeof FIELD) \
1233 : (VAR) == _bfd_strntol (FIELD, 10, sizeof FIELD))
1234
1235 /* Read in the armap of an XCOFF archive. */
1236
1237 bfd_boolean
1238 _bfd_xcoff_slurp_armap (bfd *abfd)
1239 {
1240 file_ptr off;
1241 size_t namlen;
1242 bfd_size_type sz;
1243 bfd_byte *contents, *cend;
1244 bfd_vma c, i;
1245 carsym *arsym;
1246 bfd_byte *p;
1247
1248 if (xcoff_ardata (abfd) == NULL)
1249 {
1250 bfd_has_map (abfd) = FALSE;
1251 return TRUE;
1252 }
1253
1254 if (! xcoff_big_format_p (abfd))
1255 {
1256 /* This is for the old format. */
1257 struct xcoff_ar_hdr hdr;
1258
1259 GET_VALUE_IN_FIELD (off, xcoff_ardata (abfd)->symoff);
1260 if (off == 0)
1261 {
1262 bfd_has_map (abfd) = FALSE;
1263 return TRUE;
1264 }
1265
1266 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1267 return FALSE;
1268
1269 /* The symbol table starts with a normal archive header. */
1270 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1271 != SIZEOF_AR_HDR)
1272 return FALSE;
1273
1274 /* Skip the name (normally empty). */
1275 GET_VALUE_IN_FIELD (namlen, hdr.namlen);
1276 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1277 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1278 return FALSE;
1279
1280 GET_VALUE_IN_FIELD (sz, hdr.size);
1281
1282 /* Read in the entire symbol table. */
1283 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1284 if (contents == NULL)
1285 return FALSE;
1286 if (bfd_bread (contents, sz, abfd) != sz)
1287 return FALSE;
1288
1289 /* The symbol table starts with a four byte count. */
1290 c = H_GET_32 (abfd, contents);
1291
1292 if (c * 4 >= sz)
1293 {
1294 bfd_set_error (bfd_error_bad_value);
1295 return FALSE;
1296 }
1297
1298 bfd_ardata (abfd)->symdefs =
1299 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1300 if (bfd_ardata (abfd)->symdefs == NULL)
1301 return FALSE;
1302
1303 /* After the count comes a list of four byte file offsets. */
1304 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
1305 i < c;
1306 ++i, ++arsym, p += 4)
1307 arsym->file_offset = H_GET_32 (abfd, p);
1308 }
1309 else
1310 {
1311 /* This is for the new format. */
1312 struct xcoff_ar_hdr_big hdr;
1313
1314 GET_VALUE_IN_FIELD (off, xcoff_ardata_big (abfd)->symoff);
1315 if (off == 0)
1316 {
1317 bfd_has_map (abfd) = FALSE;
1318 return TRUE;
1319 }
1320
1321 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1322 return FALSE;
1323
1324 /* The symbol table starts with a normal archive header. */
1325 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1326 != SIZEOF_AR_HDR_BIG)
1327 return FALSE;
1328
1329 /* Skip the name (normally empty). */
1330 GET_VALUE_IN_FIELD (namlen, hdr.namlen);
1331 off = ((namlen + 1) & ~ (size_t) 1) + SXCOFFARFMAG;
1332 if (bfd_seek (abfd, off, SEEK_CUR) != 0)
1333 return FALSE;
1334
1335 GET_VALUE_IN_FIELD (sz, hdr.size);
1336
1337 /* Read in the entire symbol table. */
1338 contents = (bfd_byte *) bfd_alloc (abfd, sz);
1339 if (contents == NULL)
1340 return FALSE;
1341 if (bfd_bread (contents, sz, abfd) != sz)
1342 return FALSE;
1343
1344 /* The symbol table starts with an eight byte count. */
1345 c = H_GET_64 (abfd, contents);
1346
1347 if (c * 8 >= sz)
1348 {
1349 bfd_set_error (bfd_error_bad_value);
1350 return FALSE;
1351 }
1352
1353 bfd_ardata (abfd)->symdefs =
1354 ((carsym *) bfd_alloc (abfd, c * sizeof (carsym)));
1355 if (bfd_ardata (abfd)->symdefs == NULL)
1356 return FALSE;
1357
1358 /* After the count comes a list of eight byte file offsets. */
1359 for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
1360 i < c;
1361 ++i, ++arsym, p += 8)
1362 arsym->file_offset = H_GET_64 (abfd, p);
1363 }
1364
1365 /* After the file offsets come null terminated symbol names. */
1366 cend = contents + sz;
1367 for (i = 0, arsym = bfd_ardata (abfd)->symdefs;
1368 i < c;
1369 ++i, ++arsym, p += strlen ((char *) p) + 1)
1370 {
1371 if (p >= cend)
1372 {
1373 bfd_set_error (bfd_error_bad_value);
1374 return FALSE;
1375 }
1376 arsym->name = (char *) p;
1377 }
1378
1379 bfd_ardata (abfd)->symdef_count = c;
1380 bfd_has_map (abfd) = TRUE;
1381
1382 return TRUE;
1383 }
1384
1385 /* See if this is an XCOFF archive. */
1386
1387 const bfd_target *
1388 _bfd_xcoff_archive_p (bfd *abfd)
1389 {
1390 struct artdata *tdata_hold;
1391 char magic[SXCOFFARMAG];
1392 bfd_size_type amt = SXCOFFARMAG;
1393
1394 if (bfd_bread (magic, amt, abfd) != amt)
1395 {
1396 if (bfd_get_error () != bfd_error_system_call)
1397 bfd_set_error (bfd_error_wrong_format);
1398 return NULL;
1399 }
1400
1401 if (strncmp (magic, XCOFFARMAG, SXCOFFARMAG) != 0
1402 && strncmp (magic, XCOFFARMAGBIG, SXCOFFARMAG) != 0)
1403 {
1404 bfd_set_error (bfd_error_wrong_format);
1405 return NULL;
1406 }
1407
1408 tdata_hold = bfd_ardata (abfd);
1409
1410 amt = sizeof (struct artdata);
1411 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
1412 if (bfd_ardata (abfd) == (struct artdata *) NULL)
1413 goto error_ret_restore;
1414
1415 /* Cleared by bfd_zalloc above.
1416 bfd_ardata (abfd)->cache = NULL;
1417 bfd_ardata (abfd)->archive_head = NULL;
1418 bfd_ardata (abfd)->symdefs = NULL;
1419 bfd_ardata (abfd)->extended_names = NULL;
1420 bfd_ardata (abfd)->extended_names_size = 0; */
1421
1422 /* Now handle the two formats. */
1423 if (magic[1] != 'b')
1424 {
1425 /* This is the old format. */
1426 struct xcoff_ar_file_hdr hdr;
1427
1428 /* Copy over the magic string. */
1429 memcpy (hdr.magic, magic, SXCOFFARMAG);
1430
1431 /* Now read the rest of the file header. */
1432 amt = SIZEOF_AR_FILE_HDR - SXCOFFARMAG;
1433 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1434 {
1435 if (bfd_get_error () != bfd_error_system_call)
1436 bfd_set_error (bfd_error_wrong_format);
1437 goto error_ret;
1438 }
1439
1440 GET_VALUE_IN_FIELD (bfd_ardata (abfd)->first_file_filepos,
1441 hdr.firstmemoff);
1442
1443 amt = SIZEOF_AR_FILE_HDR;
1444 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1445 if (bfd_ardata (abfd)->tdata == NULL)
1446 goto error_ret;
1447
1448 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR);
1449 }
1450 else
1451 {
1452 /* This is the new format. */
1453 struct xcoff_ar_file_hdr_big hdr;
1454
1455 /* Copy over the magic string. */
1456 memcpy (hdr.magic, magic, SXCOFFARMAG);
1457
1458 /* Now read the rest of the file header. */
1459 amt = SIZEOF_AR_FILE_HDR_BIG - SXCOFFARMAG;
1460 if (bfd_bread (&hdr.memoff, amt, abfd) != amt)
1461 {
1462 if (bfd_get_error () != bfd_error_system_call)
1463 bfd_set_error (bfd_error_wrong_format);
1464 goto error_ret;
1465 }
1466
1467 bfd_ardata (abfd)->first_file_filepos = bfd_scan_vma (hdr.firstmemoff,
1468 (const char **) 0,
1469 10);
1470
1471 amt = SIZEOF_AR_FILE_HDR_BIG;
1472 bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, amt);
1473 if (bfd_ardata (abfd)->tdata == NULL)
1474 goto error_ret;
1475
1476 memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR_BIG);
1477 }
1478
1479 if (! _bfd_xcoff_slurp_armap (abfd))
1480 {
1481 error_ret:
1482 bfd_release (abfd, bfd_ardata (abfd));
1483 error_ret_restore:
1484 bfd_ardata (abfd) = tdata_hold;
1485 return NULL;
1486 }
1487
1488 return abfd->xvec;
1489 }
1490
1491 /* Read the archive header in an XCOFF archive. */
1492
1493 void *
1494 _bfd_xcoff_read_ar_hdr (bfd *abfd)
1495 {
1496 bfd_size_type namlen;
1497 struct areltdata *ret;
1498 bfd_size_type amt = sizeof (struct areltdata);
1499
1500 ret = (struct areltdata *) bfd_zmalloc (amt);
1501 if (ret == NULL)
1502 return NULL;
1503
1504 if (! xcoff_big_format_p (abfd))
1505 {
1506 struct xcoff_ar_hdr hdr;
1507 struct xcoff_ar_hdr *hdrp;
1508
1509 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1510 != SIZEOF_AR_HDR)
1511 {
1512 free (ret);
1513 return NULL;
1514 }
1515
1516 GET_VALUE_IN_FIELD (namlen, hdr.namlen);
1517 amt = SIZEOF_AR_HDR + namlen + 1;
1518 hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd, amt);
1519 if (hdrp == NULL)
1520 {
1521 free (ret);
1522 return NULL;
1523 }
1524 memcpy (hdrp, &hdr, SIZEOF_AR_HDR);
1525 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR, namlen, abfd) != namlen)
1526 {
1527 free (ret);
1528 return NULL;
1529 }
1530 ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0';
1531
1532 ret->arch_header = (char *) hdrp;
1533 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size);
1534 ret->filename = (char *) hdrp + SIZEOF_AR_HDR;
1535 }
1536 else
1537 {
1538 struct xcoff_ar_hdr_big hdr;
1539 struct xcoff_ar_hdr_big *hdrp;
1540
1541 if (bfd_bread (&hdr, (bfd_size_type) SIZEOF_AR_HDR_BIG, abfd)
1542 != SIZEOF_AR_HDR_BIG)
1543 {
1544 free (ret);
1545 return NULL;
1546 }
1547
1548 GET_VALUE_IN_FIELD (namlen, hdr.namlen);
1549 amt = SIZEOF_AR_HDR_BIG + namlen + 1;
1550 hdrp = (struct xcoff_ar_hdr_big *) bfd_alloc (abfd, amt);
1551 if (hdrp == NULL)
1552 {
1553 free (ret);
1554 return NULL;
1555 }
1556 memcpy (hdrp, &hdr, SIZEOF_AR_HDR_BIG);
1557 if (bfd_bread ((char *) hdrp + SIZEOF_AR_HDR_BIG, namlen, abfd) != namlen)
1558 {
1559 free (ret);
1560 return NULL;
1561 }
1562 ((char *) hdrp)[SIZEOF_AR_HDR_BIG + namlen] = '\0';
1563
1564 ret->arch_header = (char *) hdrp;
1565 GET_VALUE_IN_FIELD (ret->parsed_size, hdr.size);
1566 ret->filename = (char *) hdrp + SIZEOF_AR_HDR_BIG;
1567 }
1568
1569 /* Skip over the XCOFFARFMAG at the end of the file name. */
1570 if (bfd_seek (abfd, (file_ptr) ((namlen & 1) + SXCOFFARFMAG), SEEK_CUR) != 0)
1571 return NULL;
1572
1573 return ret;
1574 }
1575
1576 /* Open the next element in an XCOFF archive. */
1577
1578 bfd *
1579 _bfd_xcoff_openr_next_archived_file (bfd *archive, bfd *last_file)
1580 {
1581 file_ptr filestart;
1582
1583 if (xcoff_ardata (archive) == NULL)
1584 {
1585 bfd_set_error (bfd_error_invalid_operation);
1586 return NULL;
1587 }
1588
1589 if (! xcoff_big_format_p (archive))
1590 {
1591 if (last_file == NULL)
1592 filestart = bfd_ardata (archive)->first_file_filepos;
1593 else
1594 GET_VALUE_IN_FIELD (filestart, arch_xhdr (last_file)->nextoff);
1595
1596 if (filestart == 0
1597 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->memoff)
1598 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata (archive)->symoff))
1599 {
1600 bfd_set_error (bfd_error_no_more_archived_files);
1601 return NULL;
1602 }
1603 }
1604 else
1605 {
1606 if (last_file == NULL)
1607 filestart = bfd_ardata (archive)->first_file_filepos;
1608 else
1609 GET_VALUE_IN_FIELD (filestart, arch_xhdr_big (last_file)->nextoff);
1610
1611 if (filestart == 0
1612 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->memoff)
1613 || EQ_VALUE_IN_FIELD (filestart, xcoff_ardata_big (archive)->symoff))
1614 {
1615 bfd_set_error (bfd_error_no_more_archived_files);
1616 return NULL;
1617 }
1618 }
1619
1620 return _bfd_get_elt_at_filepos (archive, filestart);
1621 }
1622
1623 /* Stat an element in an XCOFF archive. */
1624
1625 int
1626 _bfd_xcoff_stat_arch_elt (bfd *abfd, struct stat *s)
1627 {
1628 if (abfd->arelt_data == NULL)
1629 {
1630 bfd_set_error (bfd_error_invalid_operation);
1631 return -1;
1632 }
1633
1634 if (! xcoff_big_format_p (abfd->my_archive))
1635 {
1636 struct xcoff_ar_hdr *hdrp = arch_xhdr (abfd);
1637
1638 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date);
1639 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid);
1640 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid);
1641 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode);
1642 s->st_size = arch_eltdata (abfd)->parsed_size;
1643 }
1644 else
1645 {
1646 struct xcoff_ar_hdr_big *hdrp = arch_xhdr_big (abfd);
1647
1648 GET_VALUE_IN_FIELD (s->st_mtime, hdrp->date);
1649 GET_VALUE_IN_FIELD (s->st_uid, hdrp->uid);
1650 GET_VALUE_IN_FIELD (s->st_gid, hdrp->gid);
1651 GET_VALUE_IN_FIELD (s->st_mode, hdrp->mode);
1652 s->st_size = arch_eltdata (abfd)->parsed_size;
1653 }
1654
1655 return 0;
1656 }
1657
1658 /* Normalize a file name for inclusion in an archive. */
1659
1660 static const char *
1661 normalize_filename (bfd *abfd)
1662 {
1663 const char *file;
1664 const char *filename;
1665
1666 file = bfd_get_filename (abfd);
1667 filename = strrchr (file, '/');
1668 if (filename != NULL)
1669 filename++;
1670 else
1671 filename = file;
1672 return filename;
1673 }
1674
1675 /* Write out an XCOFF armap. */
1676
1677 static bfd_boolean
1678 xcoff_write_armap_old (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1679 struct orl *map, unsigned int orl_count, int stridx)
1680 {
1681 struct archive_iterator iterator;
1682 struct xcoff_ar_hdr hdr;
1683 char *p;
1684 unsigned char buf[4];
1685 unsigned int i;
1686
1687 memset (&hdr, 0, sizeof hdr);
1688 sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx));
1689 sprintf (hdr.nextoff, "%d", 0);
1690 memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, XCOFFARMAG_ELEMENT_SIZE);
1691 sprintf (hdr.date, "%d", 0);
1692 sprintf (hdr.uid, "%d", 0);
1693 sprintf (hdr.gid, "%d", 0);
1694 sprintf (hdr.mode, "%d", 0);
1695 sprintf (hdr.namlen, "%d", 0);
1696
1697 /* We need spaces, not null bytes, in the header. */
1698 for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++)
1699 if (*p == '\0')
1700 *p = ' ';
1701
1702 if (bfd_bwrite (&hdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
1703 != SIZEOF_AR_HDR
1704 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
1705 != SXCOFFARFMAG))
1706 return FALSE;
1707
1708 H_PUT_32 (abfd, orl_count, buf);
1709 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1710 return FALSE;
1711
1712 i = 0;
1713 archive_iterator_begin (&iterator, abfd);
1714 while (i < orl_count && archive_iterator_next (&iterator))
1715 while (map[i].u.abfd == iterator.current.member)
1716 {
1717 H_PUT_32 (abfd, iterator.current.offset, buf);
1718 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
1719 return FALSE;
1720 ++i;
1721 }
1722
1723 for (i = 0; i < orl_count; i++)
1724 {
1725 const char *name;
1726 size_t namlen;
1727
1728 name = *map[i].name;
1729 namlen = strlen (name);
1730 if (bfd_bwrite (name, (bfd_size_type) (namlen + 1), abfd) != namlen + 1)
1731 return FALSE;
1732 }
1733
1734 if ((stridx & 1) != 0)
1735 {
1736 char b;
1737
1738 b = '\0';
1739 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1740 return FALSE;
1741 }
1742
1743 return TRUE;
1744 }
1745
1746 static char buff20[XCOFFARMAGBIG_ELEMENT_SIZE + 1];
1747 #if BFD_HOST_64BIT_LONG
1748 #define FMT20 "%-20ld"
1749 #elif defined (__MSVCRT__)
1750 #define FMT20 "%-20I64d"
1751 #else
1752 #define FMT20 "%-20lld"
1753 #endif
1754 #define FMT12 "%-12d"
1755 #define FMT12_OCTAL "%-12o"
1756 #define FMT4 "%-4d"
1757 #define PRINT20(d, v) \
1758 sprintf (buff20, FMT20, (bfd_uint64_t)(v)), \
1759 memcpy ((void *) (d), buff20, 20)
1760
1761 #define PRINT12(d, v) \
1762 sprintf (buff20, FMT12, (int)(v)), \
1763 memcpy ((void *) (d), buff20, 12)
1764
1765 #define PRINT12_OCTAL(d, v) \
1766 sprintf (buff20, FMT12_OCTAL, (unsigned int)(v)), \
1767 memcpy ((void *) (d), buff20, 12)
1768
1769 #define PRINT4(d, v) \
1770 sprintf (buff20, FMT4, (int)(v)), \
1771 memcpy ((void *) (d), buff20, 4)
1772
1773 #define READ20(d, v) \
1774 buff20[20] = 0, \
1775 memcpy (buff20, (d), 20), \
1776 (v) = bfd_scan_vma (buff20, (const char **) NULL, 10)
1777
1778 static bfd_boolean
1779 do_pad (bfd *abfd, unsigned int number)
1780 {
1781 bfd_byte b = 0;
1782
1783 /* Limit pad to <= 4096. */
1784 if (number > 4096)
1785 return FALSE;
1786
1787 while (number--)
1788 if (bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
1789 return FALSE;
1790
1791 return TRUE;
1792 }
1793
1794 static bfd_boolean
1795 do_copy (bfd *out_bfd, bfd *in_bfd)
1796 {
1797 bfd_size_type remaining;
1798 bfd_byte buffer[DEFAULT_BUFFERSIZE];
1799
1800 if (bfd_seek (in_bfd, (file_ptr) 0, SEEK_SET) != 0)
1801 return FALSE;
1802
1803 remaining = arelt_size (in_bfd);
1804
1805 while (remaining >= DEFAULT_BUFFERSIZE)
1806 {
1807 if (bfd_bread (buffer, DEFAULT_BUFFERSIZE, in_bfd) != DEFAULT_BUFFERSIZE
1808 || bfd_bwrite (buffer, DEFAULT_BUFFERSIZE, out_bfd) != DEFAULT_BUFFERSIZE)
1809 return FALSE;
1810
1811 remaining -= DEFAULT_BUFFERSIZE;
1812 }
1813
1814 if (remaining)
1815 {
1816 if (bfd_bread (buffer, remaining, in_bfd) != remaining
1817 || bfd_bwrite (buffer, remaining, out_bfd) != remaining)
1818 return FALSE;
1819 }
1820
1821 return TRUE;
1822 }
1823
1824 static bfd_boolean
1825 xcoff_write_armap_big (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
1826 struct orl *map, unsigned int orl_count, int stridx)
1827 {
1828 struct archive_iterator iterator;
1829 struct xcoff_ar_file_hdr_big *fhdr;
1830 bfd_vma i, sym_32, sym_64, str_32, str_64;
1831 const bfd_arch_info_type *arch_info;
1832 bfd *current_bfd;
1833 size_t string_length;
1834 file_ptr nextoff, prevoff;
1835
1836 /* First, we look through the symbols and work out which are
1837 from 32-bit objects and which from 64-bit ones. */
1838 sym_32 = sym_64 = str_32 = str_64 = 0;
1839
1840 i = 0;
1841 for (current_bfd = abfd->archive_head;
1842 current_bfd != NULL && i < orl_count;
1843 current_bfd = current_bfd->archive_next)
1844 {
1845 arch_info = bfd_get_arch_info (current_bfd);
1846 while (map[i].u.abfd == current_bfd)
1847 {
1848 string_length = strlen (*map[i].name) + 1;
1849 if (arch_info->bits_per_address == 64)
1850 {
1851 sym_64++;
1852 str_64 += string_length;
1853 }
1854 else
1855 {
1856 sym_32++;
1857 str_32 += string_length;
1858 }
1859 i++;
1860 }
1861 }
1862
1863 /* A quick sanity check... */
1864 BFD_ASSERT (sym_64 + sym_32 == orl_count);
1865 /* Explicit cast to int for compiler. */
1866 BFD_ASSERT ((int)(str_64 + str_32) == stridx);
1867
1868 fhdr = xcoff_ardata_big (abfd);
1869
1870 /* xcoff_write_archive_contents_big passes nextoff in symoff. */
1871 READ20 (fhdr->memoff, prevoff);
1872 READ20 (fhdr->symoff, nextoff);
1873
1874 BFD_ASSERT (nextoff == bfd_tell (abfd));
1875
1876 /* Write out the symbol table.
1877 Layout :
1878
1879 standard big archive header
1880 0x0000 ar_size [0x14]
1881 0x0014 ar_nxtmem [0x14]
1882 0x0028 ar_prvmem [0x14]
1883 0x003C ar_date [0x0C]
1884 0x0048 ar_uid [0x0C]
1885 0x0054 ar_gid [0x0C]
1886 0x0060 ar_mod [0x0C]
1887 0x006C ar_namelen[0x04]
1888 0x0070 ar_fmag [SXCOFFARFMAG]
1889
1890 Symbol table
1891 0x0072 num_syms [0x08], binary
1892 0x0078 offsets [0x08 * num_syms], binary
1893 0x0086 + 0x08 * num_syms names [??]
1894 ?? pad to even bytes.
1895 */
1896
1897 if (sym_32)
1898 {
1899 struct xcoff_ar_hdr_big *hdr;
1900 char *symbol_table;
1901 char *st;
1902
1903 bfd_vma symbol_table_size =
1904 SIZEOF_AR_HDR_BIG
1905 + SXCOFFARFMAG
1906 + 8
1907 + 8 * sym_32
1908 + str_32 + (str_32 & 1);
1909
1910 symbol_table = bfd_zmalloc (symbol_table_size);
1911 if (symbol_table == NULL)
1912 return FALSE;
1913
1914 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
1915
1916 PRINT20 (hdr->size, 8 + 8 * sym_32 + str_32 + (str_32 & 1));
1917
1918 if (sym_64)
1919 PRINT20 (hdr->nextoff, nextoff + symbol_table_size);
1920 else
1921 PRINT20 (hdr->nextoff, 0);
1922
1923 PRINT20 (hdr->prevoff, prevoff);
1924 PRINT12 (hdr->date, 0);
1925 PRINT12 (hdr->uid, 0);
1926 PRINT12 (hdr->gid, 0);
1927 PRINT12 (hdr->mode, 0);
1928 PRINT4 (hdr->namlen, 0) ;
1929
1930 st = symbol_table + SIZEOF_AR_HDR_BIG;
1931 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
1932 st += SXCOFFARFMAG;
1933
1934 bfd_h_put_64 (abfd, sym_32, st);
1935 st += 8;
1936
1937 /* loop over the 32 bit offsets */
1938 i = 0;
1939 archive_iterator_begin (&iterator, abfd);
1940 while (i < orl_count && archive_iterator_next (&iterator))
1941 {
1942 arch_info = bfd_get_arch_info (iterator.current.member);
1943 while (map[i].u.abfd == iterator.current.member)
1944 {
1945 if (arch_info->bits_per_address == 32)
1946 {
1947 bfd_h_put_64 (abfd, iterator.current.offset, st);
1948 st += 8;
1949 }
1950 i++;
1951 }
1952 }
1953
1954 /* loop over the 32 bit symbol names */
1955 i = 0;
1956 for (current_bfd = abfd->archive_head;
1957 current_bfd != NULL && i < orl_count;
1958 current_bfd = current_bfd->archive_next)
1959 {
1960 arch_info = bfd_get_arch_info (current_bfd);
1961 while (map[i].u.abfd == current_bfd)
1962 {
1963 if (arch_info->bits_per_address == 32)
1964 {
1965 string_length = sprintf (st, "%s", *map[i].name);
1966 st += string_length + 1;
1967 }
1968 i++;
1969 }
1970 }
1971
1972 bfd_bwrite (symbol_table, symbol_table_size, abfd);
1973
1974 free (symbol_table);
1975
1976 prevoff = nextoff;
1977 nextoff = nextoff + symbol_table_size;
1978 }
1979 else
1980 PRINT20 (fhdr->symoff, 0);
1981
1982 if (sym_64)
1983 {
1984 struct xcoff_ar_hdr_big *hdr;
1985 char *symbol_table;
1986 char *st;
1987
1988 bfd_vma symbol_table_size =
1989 SIZEOF_AR_HDR_BIG
1990 + SXCOFFARFMAG
1991 + 8
1992 + 8 * sym_64
1993 + str_64 + (str_64 & 1);
1994
1995 symbol_table = bfd_zmalloc (symbol_table_size);
1996 if (symbol_table == NULL)
1997 return FALSE;
1998
1999 hdr = (struct xcoff_ar_hdr_big *) symbol_table;
2000
2001 PRINT20 (hdr->size, 8 + 8 * sym_64 + str_64 + (str_64 & 1));
2002 PRINT20 (hdr->nextoff, 0);
2003 PRINT20 (hdr->prevoff, prevoff);
2004 PRINT12 (hdr->date, 0);
2005 PRINT12 (hdr->uid, 0);
2006 PRINT12 (hdr->gid, 0);
2007 PRINT12 (hdr->mode, 0);
2008 PRINT4 (hdr->namlen, 0);
2009
2010 st = symbol_table + SIZEOF_AR_HDR_BIG;
2011 memcpy (st, XCOFFARFMAG, SXCOFFARFMAG);
2012 st += SXCOFFARFMAG;
2013
2014 bfd_h_put_64 (abfd, sym_64, st);
2015 st += 8;
2016
2017 /* loop over the 64 bit offsets */
2018 i = 0;
2019 archive_iterator_begin (&iterator, abfd);
2020 while (i < orl_count && archive_iterator_next (&iterator))
2021 {
2022 arch_info = bfd_get_arch_info (iterator.current.member);
2023 while (map[i].u.abfd == iterator.current.member)
2024 {
2025 if (arch_info->bits_per_address == 64)
2026 {
2027 bfd_h_put_64 (abfd, iterator.current.offset, st);
2028 st += 8;
2029 }
2030 i++;
2031 }
2032 }
2033
2034 /* loop over the 64 bit symbol names */
2035 i = 0;
2036 for (current_bfd = abfd->archive_head;
2037 current_bfd != NULL && i < orl_count;
2038 current_bfd = current_bfd->archive_next)
2039 {
2040 arch_info = bfd_get_arch_info (current_bfd);
2041 while (map[i].u.abfd == current_bfd)
2042 {
2043 if (arch_info->bits_per_address == 64)
2044 {
2045 string_length = sprintf (st, "%s", *map[i].name);
2046 st += string_length + 1;
2047 }
2048 i++;
2049 }
2050 }
2051
2052 bfd_bwrite (symbol_table, symbol_table_size, abfd);
2053
2054 free (symbol_table);
2055
2056 PRINT20 (fhdr->symoff64, nextoff);
2057 }
2058 else
2059 PRINT20 (fhdr->symoff64, 0);
2060
2061 return TRUE;
2062 }
2063
2064 bfd_boolean
2065 _bfd_xcoff_write_armap (bfd *abfd, unsigned int elength ATTRIBUTE_UNUSED,
2066 struct orl *map, unsigned int orl_count, int stridx)
2067 {
2068 if (! xcoff_big_format_p (abfd))
2069 return xcoff_write_armap_old (abfd, elength, map, orl_count, stridx);
2070 else
2071 return xcoff_write_armap_big (abfd, elength, map, orl_count, stridx);
2072 }
2073
2074 /* Write out an XCOFF archive. We always write an entire archive,
2075 rather than fussing with the freelist and so forth. */
2076
2077 static bfd_boolean
2078 xcoff_write_archive_contents_old (bfd *abfd)
2079 {
2080 struct archive_iterator iterator;
2081 struct xcoff_ar_file_hdr fhdr;
2082 bfd_size_type count;
2083 bfd_size_type total_namlen;
2084 file_ptr *offsets;
2085 bfd_boolean makemap;
2086 bfd_boolean hasobjects;
2087 file_ptr prevoff, nextoff;
2088 bfd *sub;
2089 size_t i;
2090 struct xcoff_ar_hdr ahdr;
2091 bfd_size_type size;
2092 char *p;
2093 char decbuf[XCOFFARMAG_ELEMENT_SIZE + 1];
2094
2095 memset (&fhdr, 0, sizeof fhdr);
2096 (void) strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG);
2097 sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR);
2098 sprintf (fhdr.freeoff, "%d", 0);
2099
2100 count = 0;
2101 total_namlen = 0;
2102 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2103 {
2104 ++count;
2105 total_namlen += strlen (normalize_filename (sub)) + 1;
2106 if (sub->arelt_data == NULL)
2107 {
2108 sub->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
2109 if (sub->arelt_data == NULL)
2110 return FALSE;
2111 }
2112 if (arch_xhdr (sub) == NULL)
2113 {
2114 struct xcoff_ar_hdr *ahdrp;
2115 struct stat s;
2116
2117 if (stat (bfd_get_filename (sub), &s) != 0)
2118 {
2119 bfd_set_error (bfd_error_system_call);
2120 return FALSE;
2121 }
2122
2123 ahdrp = bfd_zalloc (sub, sizeof (*ahdrp));
2124 if (ahdrp == NULL)
2125 return FALSE;
2126
2127 sprintf (ahdrp->size, "%ld", (long) s.st_size);
2128 sprintf (ahdrp->date, "%ld", (long) s.st_mtime);
2129 sprintf (ahdrp->uid, "%ld", (long) s.st_uid);
2130 sprintf (ahdrp->gid, "%ld", (long) s.st_gid);
2131 sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode);
2132
2133 arch_eltdata (sub)->arch_header = (char *) ahdrp;
2134 arch_eltdata (sub)->parsed_size = s.st_size;
2135 }
2136 }
2137 offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr));
2138 if (offsets == NULL)
2139 return FALSE;
2140
2141 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR, SEEK_SET) != 0)
2142 return FALSE;
2143
2144 makemap = bfd_has_map (abfd);
2145 hasobjects = FALSE;
2146 prevoff = 0;
2147 for (archive_iterator_begin (&iterator, abfd), i = 0;
2148 archive_iterator_next (&iterator);
2149 i++)
2150 {
2151 bfd_size_type namlen;
2152 struct xcoff_ar_hdr *ahdrp;
2153
2154 if (makemap && ! hasobjects)
2155 {
2156 if (bfd_check_format (iterator.current.member, bfd_object))
2157 hasobjects = TRUE;
2158 }
2159
2160 ahdrp = arch_xhdr (iterator.current.member);
2161 sprintf (ahdrp->prevoff, "%ld", (long) prevoff);
2162 sprintf (ahdrp->namlen, "%ld", (long) iterator.current.namlen);
2163 sprintf (ahdrp->nextoff, "%ld", (long) iterator.next.offset);
2164
2165 /* We need spaces, not null bytes, in the header. */
2166 for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++)
2167 if (*p == '\0')
2168 *p = ' ';
2169
2170 if (!do_pad (abfd, iterator.current.leading_padding))
2171 return FALSE;
2172
2173 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2174 namlen = iterator.current.padded_namlen;
2175 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR
2176 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2177 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2178 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2179 || !do_copy (abfd, iterator.current.member)
2180 || !do_pad (abfd, iterator.current.trailing_padding))
2181 return FALSE;
2182
2183 offsets[i] = iterator.current.offset;
2184 prevoff = iterator.current.offset;
2185 }
2186
2187 sprintf (fhdr.lastmemoff, "%ld", (long) prevoff);
2188
2189 /* Write out the member table. */
2190
2191 nextoff = iterator.next.offset;
2192 BFD_ASSERT (nextoff == bfd_tell (abfd));
2193 sprintf (fhdr.memoff, "%ld", (long) nextoff);
2194
2195 memset (&ahdr, 0, sizeof ahdr);
2196 sprintf (ahdr.size, "%ld", (long) (XCOFFARMAG_ELEMENT_SIZE
2197 + count * XCOFFARMAG_ELEMENT_SIZE
2198 + total_namlen));
2199 sprintf (ahdr.prevoff, "%ld", (long) prevoff);
2200 sprintf (ahdr.date, "%d", 0);
2201 sprintf (ahdr.uid, "%d", 0);
2202 sprintf (ahdr.gid, "%d", 0);
2203 sprintf (ahdr.mode, "%d", 0);
2204 sprintf (ahdr.namlen, "%d", 0);
2205
2206 size = (SIZEOF_AR_HDR
2207 + XCOFFARMAG_ELEMENT_SIZE
2208 + count * XCOFFARMAG_ELEMENT_SIZE
2209 + total_namlen
2210 + SXCOFFARFMAG);
2211
2212 prevoff = nextoff;
2213 nextoff += size + (size & 1);
2214
2215 if (makemap && hasobjects)
2216 sprintf (ahdr.nextoff, "%ld", (long) nextoff);
2217 else
2218 sprintf (ahdr.nextoff, "%d", 0);
2219
2220 /* We need spaces, not null bytes, in the header. */
2221 for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++)
2222 if (*p == '\0')
2223 *p = ' ';
2224
2225 if ((bfd_bwrite (&ahdr, (bfd_size_type) SIZEOF_AR_HDR, abfd)
2226 != SIZEOF_AR_HDR)
2227 || (bfd_bwrite (XCOFFARFMAG, (bfd_size_type) SXCOFFARFMAG, abfd)
2228 != SXCOFFARFMAG))
2229 return FALSE;
2230
2231 sprintf (decbuf, "%-12ld", (long) count);
2232 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE, abfd)
2233 != XCOFFARMAG_ELEMENT_SIZE)
2234 return FALSE;
2235 for (i = 0; i < (size_t) count; i++)
2236 {
2237 sprintf (decbuf, "%-12ld", (long) offsets[i]);
2238 if (bfd_bwrite (decbuf, (bfd_size_type) XCOFFARMAG_ELEMENT_SIZE,
2239 abfd) != XCOFFARMAG_ELEMENT_SIZE)
2240 return FALSE;
2241 }
2242 for (sub = abfd->archive_head; sub != NULL; sub = sub->archive_next)
2243 {
2244 const char *name;
2245 bfd_size_type namlen;
2246
2247 name = normalize_filename (sub);
2248 namlen = strlen (name);
2249 if (bfd_bwrite (name, namlen + 1, abfd) != namlen + 1)
2250 return FALSE;
2251 }
2252
2253 if (! do_pad (abfd, size & 1))
2254 return FALSE;
2255
2256 /* Write out the armap, if appropriate. */
2257 if (! makemap || ! hasobjects)
2258 sprintf (fhdr.symoff, "%d", 0);
2259 else
2260 {
2261 BFD_ASSERT (nextoff == bfd_tell (abfd));
2262 sprintf (fhdr.symoff, "%ld", (long) nextoff);
2263 bfd_ardata (abfd)->tdata = &fhdr;
2264 if (! _bfd_compute_and_write_armap (abfd, 0))
2265 return FALSE;
2266 }
2267
2268 /* Write out the archive file header. */
2269
2270 /* We need spaces, not null bytes, in the header. */
2271 for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++)
2272 if (*p == '\0')
2273 *p = ' ';
2274
2275 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2276 || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR, abfd)
2277 != SIZEOF_AR_FILE_HDR))
2278 return FALSE;
2279
2280 return TRUE;
2281 }
2282
2283 static bfd_boolean
2284 xcoff_write_archive_contents_big (bfd *abfd)
2285 {
2286 struct xcoff_ar_file_hdr_big fhdr;
2287 bfd_size_type count;
2288 bfd_size_type total_namlen;
2289 file_ptr *offsets;
2290 bfd_boolean makemap;
2291 bfd_boolean hasobjects;
2292 file_ptr prevoff, nextoff;
2293 bfd *current_bfd;
2294 size_t i;
2295 struct xcoff_ar_hdr_big *hdr;
2296 bfd_size_type size;
2297 char *member_table, *mt;
2298 bfd_vma member_table_size;
2299 struct archive_iterator iterator;
2300
2301 memset (&fhdr, 0, SIZEOF_AR_FILE_HDR_BIG);
2302 memcpy (fhdr.magic, XCOFFARMAGBIG, SXCOFFARMAG);
2303
2304 if (bfd_seek (abfd, (file_ptr) SIZEOF_AR_FILE_HDR_BIG, SEEK_SET) != 0)
2305 return FALSE;
2306
2307 /* Calculate count and total_namlen. */
2308 makemap = bfd_has_map (abfd);
2309 hasobjects = FALSE;
2310 for (current_bfd = abfd->archive_head, count = 0, total_namlen = 0;
2311 current_bfd != NULL;
2312 current_bfd = current_bfd->archive_next, count++)
2313 {
2314 total_namlen += strlen (normalize_filename (current_bfd)) + 1;
2315
2316 if (makemap
2317 && ! hasobjects
2318 && bfd_check_format (current_bfd, bfd_object))
2319 hasobjects = TRUE;
2320
2321 if (current_bfd->arelt_data == NULL)
2322 {
2323 size = sizeof (struct areltdata);
2324 current_bfd->arelt_data = bfd_zmalloc (size);
2325 if (current_bfd->arelt_data == NULL)
2326 return FALSE;
2327 }
2328
2329 if (arch_xhdr_big (current_bfd) == NULL)
2330 {
2331 struct xcoff_ar_hdr_big *ahdrp;
2332 struct stat s;
2333
2334 /* XXX This should actually be a call to stat64 (at least on
2335 32-bit machines).
2336 XXX This call will fail if the original object is not found. */
2337 if (stat (bfd_get_filename (current_bfd), &s) != 0)
2338 {
2339 bfd_set_error (bfd_error_system_call);
2340 return FALSE;
2341 }
2342
2343 ahdrp = bfd_zalloc (current_bfd, sizeof (*ahdrp));
2344 if (ahdrp == NULL)
2345 return FALSE;
2346
2347 PRINT20 (ahdrp->size, s.st_size);
2348 PRINT12 (ahdrp->date, s.st_mtime);
2349 PRINT12 (ahdrp->uid, s.st_uid);
2350 PRINT12 (ahdrp->gid, s.st_gid);
2351 PRINT12_OCTAL (ahdrp->mode, s.st_mode);
2352
2353 arch_eltdata (current_bfd)->arch_header = (char *) ahdrp;
2354 arch_eltdata (current_bfd)->parsed_size = s.st_size;
2355 }
2356 }
2357
2358 offsets = NULL;
2359 if (count)
2360 {
2361 offsets = (file_ptr *) bfd_malloc (count * sizeof (file_ptr));
2362 if (offsets == NULL)
2363 return FALSE;
2364 }
2365
2366 prevoff = 0;
2367 for (archive_iterator_begin (&iterator, abfd), i = 0;
2368 archive_iterator_next (&iterator);
2369 i++)
2370 {
2371 bfd_size_type namlen;
2372 struct xcoff_ar_hdr_big *ahdrp;
2373
2374 ahdrp = arch_xhdr_big (iterator.current.member);
2375 PRINT20 (ahdrp->prevoff, prevoff);
2376 PRINT4 (ahdrp->namlen, iterator.current.namlen);
2377 PRINT20 (ahdrp->nextoff, iterator.next.offset);
2378
2379 if (!do_pad (abfd, iterator.current.leading_padding))
2380 {
2381 free (offsets);
2382 return FALSE;
2383 }
2384
2385 BFD_ASSERT (iterator.current.offset == bfd_tell (abfd));
2386 namlen = iterator.current.padded_namlen;
2387 if (bfd_bwrite (ahdrp, SIZEOF_AR_HDR_BIG, abfd) != SIZEOF_AR_HDR_BIG
2388 || bfd_bwrite (iterator.current.name, namlen, abfd) != namlen
2389 || bfd_bwrite (XCOFFARFMAG, SXCOFFARFMAG, abfd) != SXCOFFARFMAG
2390 || bfd_seek (iterator.current.member, 0, SEEK_SET) != 0
2391 || !do_copy (abfd, iterator.current.member)
2392 || !do_pad (abfd, iterator.current.trailing_padding))
2393 {
2394 free (offsets);
2395 return FALSE;
2396 }
2397
2398 offsets[i] = iterator.current.offset;
2399 prevoff = iterator.current.offset;
2400 }
2401
2402 if (count)
2403 {
2404 PRINT20 (fhdr.firstmemoff, offsets[0]);
2405 PRINT20 (fhdr.lastmemoff, prevoff);
2406 }
2407
2408 /* Write out the member table.
2409 Layout :
2410
2411 standard big archive header
2412 0x0000 ar_size [0x14]
2413 0x0014 ar_nxtmem [0x14]
2414 0x0028 ar_prvmem [0x14]
2415 0x003C ar_date [0x0C]
2416 0x0048 ar_uid [0x0C]
2417 0x0054 ar_gid [0x0C]
2418 0x0060 ar_mod [0x0C]
2419 0x006C ar_namelen[0x04]
2420 0x0070 ar_fmag [0x02]
2421
2422 Member table
2423 0x0072 count [0x14]
2424 0x0086 offsets [0x14 * counts]
2425 0x0086 + 0x14 * counts names [??]
2426 ?? pad to even bytes.
2427 */
2428
2429 nextoff = iterator.next.offset;
2430 BFD_ASSERT (nextoff == bfd_tell (abfd));
2431
2432 member_table_size = (SIZEOF_AR_HDR_BIG
2433 + SXCOFFARFMAG
2434 + XCOFFARMAGBIG_ELEMENT_SIZE
2435 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2436 + total_namlen);
2437
2438 member_table_size += member_table_size & 1;
2439 member_table = bfd_zmalloc (member_table_size);
2440 if (member_table == NULL)
2441 {
2442 free (offsets);
2443 return FALSE;
2444 }
2445
2446 hdr = (struct xcoff_ar_hdr_big *) member_table;
2447
2448 PRINT20 (hdr->size, (XCOFFARMAGBIG_ELEMENT_SIZE
2449 + count * XCOFFARMAGBIG_ELEMENT_SIZE
2450 + total_namlen + (total_namlen & 1)));
2451 if (makemap && hasobjects)
2452 PRINT20 (hdr->nextoff, nextoff + member_table_size);
2453 else
2454 PRINT20 (hdr->nextoff, 0);
2455 PRINT20 (hdr->prevoff, prevoff);
2456 PRINT12 (hdr->date, 0);
2457 PRINT12 (hdr->uid, 0);
2458 PRINT12 (hdr->gid, 0);
2459 PRINT12 (hdr->mode, 0);
2460 PRINT4 (hdr->namlen, 0);
2461
2462 mt = member_table + SIZEOF_AR_HDR_BIG;
2463 memcpy (mt, XCOFFARFMAG, SXCOFFARFMAG);
2464 mt += SXCOFFARFMAG;
2465
2466 PRINT20 (mt, count);
2467 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2468 for (i = 0; i < (size_t) count; i++)
2469 {
2470 PRINT20 (mt, offsets[i]);
2471 mt += XCOFFARMAGBIG_ELEMENT_SIZE;
2472 }
2473
2474 if (count)
2475 {
2476 free (offsets);
2477 offsets = NULL;
2478 }
2479
2480 for (current_bfd = abfd->archive_head;
2481 current_bfd != NULL;
2482 current_bfd = current_bfd->archive_next)
2483 {
2484 const char *name;
2485 size_t namlen;
2486
2487 name = normalize_filename (current_bfd);
2488 namlen = sprintf (mt, "%s", name);
2489 mt += namlen + 1;
2490 }
2491
2492 if (bfd_bwrite (member_table, member_table_size, abfd) != member_table_size)
2493 return FALSE;
2494
2495 free (member_table);
2496
2497 PRINT20 (fhdr.memoff, nextoff);
2498
2499 prevoff = nextoff;
2500 nextoff += member_table_size;
2501
2502 /* Write out the armap, if appropriate. */
2503
2504 if (! makemap || ! hasobjects)
2505 PRINT20 (fhdr.symoff, 0);
2506 else
2507 {
2508 BFD_ASSERT (nextoff == bfd_tell (abfd));
2509
2510 /* Save nextoff in fhdr.symoff so the armap routine can use it. */
2511 PRINT20 (fhdr.symoff, nextoff);
2512
2513 bfd_ardata (abfd)->tdata = &fhdr;
2514 if (! _bfd_compute_and_write_armap (abfd, 0))
2515 return FALSE;
2516 }
2517
2518 /* Write out the archive file header. */
2519
2520 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2521 || (bfd_bwrite (&fhdr, (bfd_size_type) SIZEOF_AR_FILE_HDR_BIG,
2522 abfd) != SIZEOF_AR_FILE_HDR_BIG))
2523 return FALSE;
2524
2525 return TRUE;
2526 }
2527
2528 bfd_boolean
2529 _bfd_xcoff_write_archive_contents (bfd *abfd)
2530 {
2531 if (! xcoff_big_format_p (abfd))
2532 return xcoff_write_archive_contents_old (abfd);
2533 else
2534 return xcoff_write_archive_contents_big (abfd);
2535 }
2536
2537 /* We can't use the usual coff_sizeof_headers routine, because AIX
2539 always uses an a.out header. */
2540
2541 int
2542 _bfd_xcoff_sizeof_headers (bfd *abfd,
2543 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2544 {
2545 int size;
2546
2547 size = FILHSZ;
2548 if (xcoff_data (abfd)->full_aouthdr)
2549 size += AOUTSZ;
2550 else
2551 size += SMALL_AOUTSZ;
2552 size += abfd->section_count * SCNHSZ;
2553
2554 if (info->strip != strip_all)
2555 {
2556 /* There can be additional sections just for dealing with overflow in
2557 reloc and lineno counts. But the numbers of relocs and lineno aren't
2558 known when bfd_sizeof_headers is called, so we compute them by
2559 summing the numbers from input sections. */
2560 struct nbr_reloc_lineno
2561 {
2562 unsigned int reloc_count;
2563 unsigned int lineno_count;
2564 };
2565 struct nbr_reloc_lineno *n_rl;
2566 bfd *sub;
2567 unsigned int max_index;
2568 asection *s;
2569
2570 /* Although the number of sections is known, the maximum value of
2571 section->index isn't (because some sections may have been removed).
2572 Don't try to renumber sections, just compute the upper bound. */
2573 max_index = 0;
2574 for (s = abfd->sections; s != NULL; s = s->next)
2575 if (s->index > max_index)
2576 max_index = s->index;
2577
2578 /* Allocate the per section counters. It could be possible to use a
2579 preallocated array as the number of sections is limited on XCOFF,
2580 but this creates a maintainance issue. */
2581 n_rl = bfd_zmalloc ((max_index + 1) * sizeof (*n_rl));
2582 if (n_rl == NULL)
2583 return -1;
2584
2585 /* Sum. */
2586 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2587 for (s = sub->sections; s != NULL; s = s->next)
2588 {
2589 struct nbr_reloc_lineno *e = &n_rl[s->output_section->index];
2590 e->reloc_count += s->reloc_count;
2591 e->lineno_count += s->lineno_count;
2592 }
2593
2594 /* Add the size of a section for each section with an overflow. */
2595 for (s = abfd->sections; s != NULL; s = s->next)
2596 {
2597 struct nbr_reloc_lineno *e = &n_rl[s->index];
2598
2599 if (e->reloc_count >= 0xffff
2600 || (e->lineno_count >= 0xffff && info->strip != strip_debugger))
2601 size += SCNHSZ;
2602 }
2603
2604 free (n_rl);
2605 }
2606
2607 return size;
2608 }
2609
2610 /* Routines to swap information in the XCOFF .loader section. If we
2612 ever need to write an XCOFF loader, this stuff will need to be
2613 moved to another file shared by the linker (which XCOFF calls the
2614 ``binder'') and the loader. */
2615
2616 /* Swap in the ldhdr structure. */
2617
2618 static void
2619 xcoff_swap_ldhdr_in (bfd *abfd, const void * s, struct internal_ldhdr *dst)
2620 {
2621 const struct external_ldhdr *src = (const struct external_ldhdr *) s;
2622
2623 dst->l_version = bfd_get_32 (abfd, src->l_version);
2624 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
2625 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
2626 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
2627 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
2628 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
2629 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
2630 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
2631 }
2632
2633 /* Swap out the ldhdr structure. */
2634
2635 static void
2636 xcoff_swap_ldhdr_out (bfd *abfd, const struct internal_ldhdr *src, void * d)
2637 {
2638 struct external_ldhdr *dst = (struct external_ldhdr *) d;
2639
2640 bfd_put_32 (abfd, (bfd_vma) src->l_version, dst->l_version);
2641 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
2642 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
2643 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
2644 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
2645 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
2646 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
2647 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
2648 }
2649
2650 /* Swap in the ldsym structure. */
2651
2652 static void
2653 xcoff_swap_ldsym_in (bfd *abfd, const void * s, struct internal_ldsym *dst)
2654 {
2655 const struct external_ldsym *src = (const struct external_ldsym *) s;
2656
2657 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0) {
2658 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2659 } else {
2660 dst->_l._l_l._l_zeroes = 0;
2661 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
2662 }
2663 dst->l_value = bfd_get_32 (abfd, src->l_value);
2664 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
2665 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
2666 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
2667 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
2668 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
2669 }
2670
2671 /* Swap out the ldsym structure. */
2672
2673 static void
2674 xcoff_swap_ldsym_out (bfd *abfd, const struct internal_ldsym *src, void * d)
2675 {
2676 struct external_ldsym *dst = (struct external_ldsym *) d;
2677
2678 if (src->_l._l_l._l_zeroes != 0)
2679 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
2680 else
2681 {
2682 bfd_put_32 (abfd, (bfd_vma) 0, dst->_l._l_l._l_zeroes);
2683 bfd_put_32 (abfd, (bfd_vma) src->_l._l_l._l_offset,
2684 dst->_l._l_l._l_offset);
2685 }
2686 bfd_put_32 (abfd, src->l_value, dst->l_value);
2687 bfd_put_16 (abfd, (bfd_vma) src->l_scnum, dst->l_scnum);
2688 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
2689 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
2690 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
2691 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
2692 }
2693
2694 static void
2695 xcoff_swap_reloc_in (bfd *abfd, void * s, void * d)
2696 {
2697 struct external_reloc *src = (struct external_reloc *) s;
2698 struct internal_reloc *dst = (struct internal_reloc *) d;
2699
2700 memset (dst, 0, sizeof (struct internal_reloc));
2701
2702 dst->r_vaddr = bfd_get_32 (abfd, src->r_vaddr);
2703 dst->r_symndx = bfd_get_32 (abfd, src->r_symndx);
2704 dst->r_size = bfd_get_8 (abfd, src->r_size);
2705 dst->r_type = bfd_get_8 (abfd, src->r_type);
2706 }
2707
2708 static unsigned int
2709 xcoff_swap_reloc_out (bfd *abfd, void * s, void * d)
2710 {
2711 struct internal_reloc *src = (struct internal_reloc *) s;
2712 struct external_reloc *dst = (struct external_reloc *) d;
2713
2714 bfd_put_32 (abfd, src->r_vaddr, dst->r_vaddr);
2715 bfd_put_32 (abfd, src->r_symndx, dst->r_symndx);
2716 bfd_put_8 (abfd, src->r_type, dst->r_type);
2717 bfd_put_8 (abfd, src->r_size, dst->r_size);
2718
2719 return bfd_coff_relsz (abfd);
2720 }
2721
2722 /* Swap in the ldrel structure. */
2723
2724 static void
2725 xcoff_swap_ldrel_in (bfd *abfd, const void * s, struct internal_ldrel *dst)
2726 {
2727 const struct external_ldrel *src = (const struct external_ldrel *) s;
2728
2729 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
2730 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
2731 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
2732 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
2733 }
2734
2735 /* Swap out the ldrel structure. */
2736
2737 static void
2738 xcoff_swap_ldrel_out (bfd *abfd, const struct internal_ldrel *src, void * d)
2739 {
2740 struct external_ldrel *dst = (struct external_ldrel *) d;
2741
2742 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
2743 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
2744 bfd_put_16 (abfd, (bfd_vma) src->l_rtype, dst->l_rtype);
2745 bfd_put_16 (abfd, (bfd_vma) src->l_rsecnm, dst->l_rsecnm);
2746 }
2747
2748
2750 bfd_boolean
2751 xcoff_reloc_type_noop (bfd *input_bfd ATTRIBUTE_UNUSED,
2752 asection *input_section ATTRIBUTE_UNUSED,
2753 bfd *output_bfd ATTRIBUTE_UNUSED,
2754 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2755 struct internal_syment *sym ATTRIBUTE_UNUSED,
2756 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2757 bfd_vma val ATTRIBUTE_UNUSED,
2758 bfd_vma addend ATTRIBUTE_UNUSED,
2759 bfd_vma *relocation ATTRIBUTE_UNUSED,
2760 bfd_byte *contents ATTRIBUTE_UNUSED)
2761 {
2762 return TRUE;
2763 }
2764
2765 bfd_boolean
2766 xcoff_reloc_type_fail (bfd *input_bfd,
2767 asection *input_section ATTRIBUTE_UNUSED,
2768 bfd *output_bfd ATTRIBUTE_UNUSED,
2769 struct internal_reloc *rel,
2770 struct internal_syment *sym ATTRIBUTE_UNUSED,
2771 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2772 bfd_vma val ATTRIBUTE_UNUSED,
2773 bfd_vma addend ATTRIBUTE_UNUSED,
2774 bfd_vma *relocation ATTRIBUTE_UNUSED,
2775 bfd_byte *contents ATTRIBUTE_UNUSED)
2776 {
2777 _bfd_error_handler
2778 /* xgettext: c-format */
2779 (_("%B: unsupported relocation type 0x%02x"),
2780 input_bfd, (unsigned int) rel->r_type);
2781 bfd_set_error (bfd_error_bad_value);
2782 return FALSE;
2783 }
2784
2785 bfd_boolean
2786 xcoff_reloc_type_pos (bfd *input_bfd ATTRIBUTE_UNUSED,
2787 asection *input_section ATTRIBUTE_UNUSED,
2788 bfd *output_bfd ATTRIBUTE_UNUSED,
2789 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2790 struct internal_syment *sym ATTRIBUTE_UNUSED,
2791 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2792 bfd_vma val,
2793 bfd_vma addend,
2794 bfd_vma *relocation,
2795 bfd_byte *contents ATTRIBUTE_UNUSED)
2796 {
2797 *relocation = val + addend;
2798 return TRUE;
2799 }
2800
2801 bfd_boolean
2802 xcoff_reloc_type_neg (bfd *input_bfd ATTRIBUTE_UNUSED,
2803 asection *input_section ATTRIBUTE_UNUSED,
2804 bfd *output_bfd ATTRIBUTE_UNUSED,
2805 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2806 struct internal_syment *sym ATTRIBUTE_UNUSED,
2807 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2808 bfd_vma val,
2809 bfd_vma addend,
2810 bfd_vma *relocation,
2811 bfd_byte *contents ATTRIBUTE_UNUSED)
2812 {
2813 *relocation = addend - val;
2814 return TRUE;
2815 }
2816
2817 bfd_boolean
2818 xcoff_reloc_type_rel (bfd *input_bfd ATTRIBUTE_UNUSED,
2819 asection *input_section,
2820 bfd *output_bfd ATTRIBUTE_UNUSED,
2821 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2822 struct internal_syment *sym ATTRIBUTE_UNUSED,
2823 struct reloc_howto_struct *howto,
2824 bfd_vma val,
2825 bfd_vma addend,
2826 bfd_vma *relocation,
2827 bfd_byte *contents ATTRIBUTE_UNUSED)
2828 {
2829 howto->pc_relative = TRUE;
2830
2831 /* A PC relative reloc includes the section address. */
2832 addend += input_section->vma;
2833
2834 *relocation = val + addend;
2835 *relocation -= (input_section->output_section->vma
2836 + input_section->output_offset);
2837 return TRUE;
2838 }
2839
2840 bfd_boolean
2841 xcoff_reloc_type_toc (bfd *input_bfd,
2842 asection *input_section ATTRIBUTE_UNUSED,
2843 bfd *output_bfd,
2844 struct internal_reloc *rel,
2845 struct internal_syment *sym,
2846 struct reloc_howto_struct *howto ATTRIBUTE_UNUSED,
2847 bfd_vma val,
2848 bfd_vma addend ATTRIBUTE_UNUSED,
2849 bfd_vma *relocation,
2850 bfd_byte *contents ATTRIBUTE_UNUSED)
2851 {
2852 struct xcoff_link_hash_entry *h;
2853
2854 if (0 > rel->r_symndx)
2855 return FALSE;
2856
2857 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2858
2859 if (h != NULL && h->smclas != XMC_TD)
2860 {
2861 if (h->toc_section == NULL)
2862 {
2863 _bfd_error_handler
2864 /* xgettext: c-format */
2865 (_("%B: TOC reloc at %#Lx to symbol `%s' with no TOC entry"),
2866 input_bfd, rel->r_vaddr, h->root.root.string);
2867 bfd_set_error (bfd_error_bad_value);
2868 return FALSE;
2869 }
2870
2871 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
2872 val = (h->toc_section->output_section->vma
2873 + h->toc_section->output_offset);
2874 }
2875
2876 *relocation = ((val - xcoff_data (output_bfd)->toc)
2877 - (sym->n_value - xcoff_data (input_bfd)->toc));
2878 return TRUE;
2879 }
2880
2881 bfd_boolean
2882 xcoff_reloc_type_ba (bfd *input_bfd ATTRIBUTE_UNUSED,
2883 asection *input_section ATTRIBUTE_UNUSED,
2884 bfd *output_bfd ATTRIBUTE_UNUSED,
2885 struct internal_reloc *rel ATTRIBUTE_UNUSED,
2886 struct internal_syment *sym ATTRIBUTE_UNUSED,
2887 struct reloc_howto_struct *howto,
2888 bfd_vma val,
2889 bfd_vma addend,
2890 bfd_vma *relocation,
2891 bfd_byte *contents ATTRIBUTE_UNUSED)
2892 {
2893 howto->src_mask &= ~3;
2894 howto->dst_mask = howto->src_mask;
2895
2896 *relocation = val + addend;
2897
2898 return TRUE;
2899 }
2900
2901 static bfd_boolean
2902 xcoff_reloc_type_br (bfd *input_bfd,
2903 asection *input_section,
2904 bfd *output_bfd ATTRIBUTE_UNUSED,
2905 struct internal_reloc *rel,
2906 struct internal_syment *sym ATTRIBUTE_UNUSED,
2907 struct reloc_howto_struct *howto,
2908 bfd_vma val,
2909 bfd_vma addend,
2910 bfd_vma *relocation,
2911 bfd_byte *contents)
2912 {
2913 struct xcoff_link_hash_entry *h;
2914 bfd_vma section_offset;
2915
2916 if (0 > rel->r_symndx)
2917 return FALSE;
2918
2919 h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx];
2920 section_offset = rel->r_vaddr - input_section->vma;
2921
2922 /* If we see an R_BR or R_RBR reloc which is jumping to global
2923 linkage code, and it is followed by an appropriate cror nop
2924 instruction, we replace the cror with lwz r2,20(r1). This
2925 restores the TOC after the glink code. Contrariwise, if the
2926 call is followed by a lwz r2,20(r1), but the call is not
2927 going to global linkage code, we can replace the load with a
2928 cror. */
2929 if (NULL != h
2930 && (bfd_link_hash_defined == h->root.type
2931 || bfd_link_hash_defweak == h->root.type)
2932 && section_offset + 8 <= input_section->size)
2933 {
2934 bfd_byte *pnext;
2935 unsigned long next;
2936
2937 pnext = contents + section_offset + 4;
2938 next = bfd_get_32 (input_bfd, pnext);
2939
2940 /* The _ptrgl function is magic. It is used by the AIX
2941 compiler to call a function through a pointer. */
2942 if (h->smclas == XMC_GL || strcmp (h->root.root.string, "._ptrgl") == 0)
2943 {
2944 if (next == 0x4def7b82 /* cror 15,15,15 */
2945 || next == 0x4ffffb82 /* cror 31,31,31 */
2946 || next == 0x60000000) /* ori r0,r0,0 */
2947 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r2,20(r1) */
2948
2949 }
2950 else
2951 {
2952 if (next == 0x80410014) /* lwz r2,20(r1) */
2953 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
2954 }
2955 }
2956 else if (NULL != h && bfd_link_hash_undefined == h->root.type)
2957 {
2958 /* Normally, this relocation is against a defined symbol. In the
2959 case where this is a partial link and the output section offset
2960 is greater than 2^25, the linker will return an invalid error
2961 message that the relocation has been truncated. Yes it has been
2962 truncated but no it not important. For this case, disable the
2963 overflow checking. */
2964
2965 howto->complain_on_overflow = complain_overflow_dont;
2966 }
2967
2968 /* The original PC-relative relocation is biased by -r_vaddr, so adding
2969 the value below will give the absolute target address. */
2970 *relocation = val + addend + rel->r_vaddr;
2971
2972 howto->src_mask &= ~3;
2973 howto->dst_mask = howto->src_mask;
2974
2975 if (h != NULL
2976 && (h->root.type == bfd_link_hash_defined
2977 || h->root.type == bfd_link_hash_defweak)
2978 && bfd_is_abs_section (h->root.u.def.section)
2979 && section_offset + 4 <= input_section->size)
2980 {
2981 bfd_byte *ptr;
2982 bfd_vma insn;
2983
2984 /* Turn the relative branch into an absolute one by setting the
2985 AA bit. */
2986 ptr = contents + section_offset;
2987 insn = bfd_get_32 (input_bfd, ptr);
2988 insn |= 2;
2989 bfd_put_32 (input_bfd, insn, ptr);
2990
2991 /* Make the howto absolute too. */
2992 howto->pc_relative = FALSE;
2993 howto->complain_on_overflow = complain_overflow_bitfield;
2994 }
2995 else
2996 {
2997 /* Use a PC-relative howto and subtract the instruction's address
2998 from the target address we calculated above. */
2999 howto->pc_relative = TRUE;
3000 *relocation -= (input_section->output_section->vma
3001 + input_section->output_offset
3002 + section_offset);
3003 }
3004 return TRUE;
3005 }
3006
3007 bfd_boolean
3008 xcoff_reloc_type_crel (bfd *input_bfd ATTRIBUTE_UNUSED,
3009 asection *input_section,
3010 bfd *output_bfd ATTRIBUTE_UNUSED,
3011 struct internal_reloc *rel ATTRIBUTE_UNUSED,
3012 struct internal_syment *sym ATTRIBUTE_UNUSED,
3013 struct reloc_howto_struct *howto,
3014 bfd_vma val ATTRIBUTE_UNUSED,
3015 bfd_vma addend,
3016 bfd_vma *relocation,
3017 bfd_byte *contents ATTRIBUTE_UNUSED)
3018 {
3019 howto->pc_relative = TRUE;
3020 howto->src_mask &= ~3;
3021 howto->dst_mask = howto->src_mask;
3022
3023 /* A PC relative reloc includes the section address. */
3024 addend += input_section->vma;
3025
3026 *relocation = val + addend;
3027 *relocation -= (input_section->output_section->vma
3028 + input_section->output_offset);
3029 return TRUE;
3030 }
3031
3032 static bfd_boolean
3033 xcoff_complain_overflow_dont_func (bfd *input_bfd ATTRIBUTE_UNUSED,
3034 bfd_vma val ATTRIBUTE_UNUSED,
3035 bfd_vma relocation ATTRIBUTE_UNUSED,
3036 struct reloc_howto_struct *
3037 howto ATTRIBUTE_UNUSED)
3038 {
3039 return FALSE;
3040 }
3041
3042 static bfd_boolean
3043 xcoff_complain_overflow_bitfield_func (bfd *input_bfd,
3044 bfd_vma val,
3045 bfd_vma relocation,
3046 struct reloc_howto_struct *howto)
3047 {
3048 bfd_vma fieldmask, signmask, ss;
3049 bfd_vma a, b, sum;
3050
3051 /* Get the values to be added together. For signed and unsigned
3052 relocations, we assume that all values should be truncated to
3053 the size of an address. For bitfields, all the bits matter.
3054 See also bfd_check_overflow. */
3055 fieldmask = N_ONES (howto->bitsize);
3056 a = relocation;
3057 b = val & howto->src_mask;
3058
3059 /* Much like unsigned, except no trimming with addrmask. In
3060 addition, the sum overflows if there is a carry out of
3061 the bfd_vma, i.e., the sum is less than either input
3062 operand. */
3063 a >>= howto->rightshift;
3064 b >>= howto->bitpos;
3065
3066 /* Bitfields are sometimes used for signed numbers; for
3067 example, a 13-bit field sometimes represents values in
3068 0..8191 and sometimes represents values in -4096..4095.
3069 If the field is signed and a is -4095 (0x1001) and b is
3070 -1 (0x1fff), the sum is -4096 (0x1000), but (0x1001 +
3071 0x1fff is 0x3000). It's not clear how to handle this
3072 everywhere, since there is not way to know how many bits
3073 are significant in the relocation, but the original code
3074 assumed that it was fully sign extended, and we will keep
3075 that assumption. */
3076 signmask = (fieldmask >> 1) + 1;
3077
3078 if ((a & ~ fieldmask) != 0)
3079 {
3080 /* Some bits out of the field are set. This might not
3081 be a problem: if this is a signed bitfield, it is OK
3082 iff all the high bits are set, including the sign
3083 bit. We'll try setting all but the most significant
3084 bit in the original relocation value: if this is all
3085 ones, we are OK, assuming a signed bitfield. */
3086 ss = (signmask << howto->rightshift) - 1;
3087 if ((ss | relocation) != ~ (bfd_vma) 0)
3088 return TRUE;
3089 a &= fieldmask;
3090 }
3091
3092 /* We just assume (b & ~ fieldmask) == 0. */
3093
3094 /* We explicitly permit wrap around if this relocation
3095 covers the high bit of an address. The Linux kernel
3096 relies on it, and it is the only way to write assembler
3097 code which can run when loaded at a location 0x80000000
3098 away from the location at which it is linked. */
3099 if (howto->bitsize + howto->rightshift
3100 == bfd_arch_bits_per_address (input_bfd))
3101 return FALSE;
3102
3103 sum = a + b;
3104 if (sum < a || (sum & ~ fieldmask) != 0)
3105 {
3106 /* There was a carry out, or the field overflow. Test
3107 for signed operands again. Here is the overflow test
3108 is as for complain_overflow_signed. */
3109 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3110 return TRUE;
3111 }
3112
3113 return FALSE;
3114 }
3115
3116 static bfd_boolean
3117 xcoff_complain_overflow_signed_func (bfd *input_bfd,
3118 bfd_vma val,
3119 bfd_vma relocation,
3120 struct reloc_howto_struct *howto)
3121 {
3122 bfd_vma addrmask, fieldmask, signmask, ss;
3123 bfd_vma a, b, sum;
3124
3125 /* Get the values to be added together. For signed and unsigned
3126 relocations, we assume that all values should be truncated to
3127 the size of an address. For bitfields, all the bits matter.
3128 See also bfd_check_overflow. */
3129 fieldmask = N_ONES (howto->bitsize);
3130 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3131 a = relocation;
3132 b = val & howto->src_mask;
3133
3134 a = (a & addrmask) >> howto->rightshift;
3135
3136 /* If any sign bits are set, all sign bits must be set.
3137 That is, A must be a valid negative address after
3138 shifting. */
3139 signmask = ~ (fieldmask >> 1);
3140 ss = a & signmask;
3141 if (ss != 0 && ss != ((addrmask >> howto->rightshift) & signmask))
3142 return TRUE;
3143
3144 /* We only need this next bit of code if the sign bit of B
3145 is below the sign bit of A. This would only happen if
3146 SRC_MASK had fewer bits than BITSIZE. Note that if
3147 SRC_MASK has more bits than BITSIZE, we can get into
3148 trouble; we would need to verify that B is in range, as
3149 we do for A above. */
3150 signmask = ((~ howto->src_mask) >> 1) & howto->src_mask;
3151 if ((b & signmask) != 0)
3152 {
3153 /* Set all the bits above the sign bit. */
3154 b -= signmask <<= 1;
3155 }
3156
3157 b = (b & addrmask) >> howto->bitpos;
3158
3159 /* Now we can do the addition. */
3160 sum = a + b;
3161
3162 /* See if the result has the correct sign. Bits above the
3163 sign bit are junk now; ignore them. If the sum is
3164 positive, make sure we did not have all negative inputs;
3165 if the sum is negative, make sure we did not have all
3166 positive inputs. The test below looks only at the sign
3167 bits, and it really just
3168 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3169 */
3170 signmask = (fieldmask >> 1) + 1;
3171 if (((~ (a ^ b)) & (a ^ sum)) & signmask)
3172 return TRUE;
3173
3174 return FALSE;
3175 }
3176
3177 static bfd_boolean
3178 xcoff_complain_overflow_unsigned_func (bfd *input_bfd,
3179 bfd_vma val,
3180 bfd_vma relocation,
3181 struct reloc_howto_struct *howto)
3182 {
3183 bfd_vma addrmask, fieldmask;
3184 bfd_vma a, b, sum;
3185
3186 /* Get the values to be added together. For signed and unsigned
3187 relocations, we assume that all values should be truncated to
3188 the size of an address. For bitfields, all the bits matter.
3189 See also bfd_check_overflow. */
3190 fieldmask = N_ONES (howto->bitsize);
3191 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3192 a = relocation;
3193 b = val & howto->src_mask;
3194
3195 /* Checking for an unsigned overflow is relatively easy:
3196 trim the addresses and add, and trim the result as well.
3197 Overflow is normally indicated when the result does not
3198 fit in the field. However, we also need to consider the
3199 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3200 input is 0x80000000, and bfd_vma is only 32 bits; then we
3201 will get sum == 0, but there is an overflow, since the
3202 inputs did not fit in the field. Instead of doing a
3203 separate test, we can check for this by or-ing in the
3204 operands when testing for the sum overflowing its final
3205 field. */
3206 a = (a & addrmask) >> howto->rightshift;
3207 b = (b & addrmask) >> howto->bitpos;
3208 sum = (a + b) & addrmask;
3209 if ((a | b | sum) & ~ fieldmask)
3210 return TRUE;
3211
3212 return FALSE;
3213 }
3214
3215 /* This is the relocation function for the RS/6000/POWER/PowerPC.
3216 This is currently the only processor which uses XCOFF; I hope that
3217 will never change.
3218
3219 I took the relocation type definitions from two documents:
3220 the PowerPC AIX Version 4 Application Binary Interface, First
3221 Edition (April 1992), and the PowerOpen ABI, Big-Endian
3222 32-Bit Hardware Implementation (June 30, 1994). Differences
3223 between the documents are noted below.
3224
3225 Unsupported r_type's
3226
3227 R_RTB:
3228 R_RRTBI:
3229 R_RRTBA:
3230
3231 These relocs are defined by the PowerPC ABI to be
3232 relative branches which use half of the difference
3233 between the symbol and the program counter. I can't
3234 quite figure out when this is useful. These relocs are
3235 not defined by the PowerOpen ABI.
3236
3237 Supported r_type's
3238
3239 R_POS:
3240 Simple positive relocation.
3241
3242 R_NEG:
3243 Simple negative relocation.
3244
3245 R_REL:
3246 Simple PC relative relocation.
3247
3248 R_TOC:
3249 TOC relative relocation. The value in the instruction in
3250 the input file is the offset from the input file TOC to
3251 the desired location. We want the offset from the final
3252 TOC to the desired location. We have:
3253 isym = iTOC + in
3254 iinsn = in + o
3255 osym = oTOC + on
3256 oinsn = on + o
3257 so we must change insn by on - in.
3258
3259 R_GL:
3260 GL linkage relocation. The value of this relocation
3261 is the address of the entry in the TOC section.
3262
3263 R_TCL:
3264 Local object TOC address. I can't figure out the
3265 difference between this and case R_GL.
3266
3267 R_TRL:
3268 TOC relative relocation. A TOC relative load instruction
3269 which may be changed to a load address instruction.
3270 FIXME: We don't currently implement this optimization.
3271
3272 R_TRLA:
3273 TOC relative relocation. This is a TOC relative load
3274 address instruction which may be changed to a load
3275 instruction. FIXME: I don't know if this is the correct
3276 implementation.
3277
3278 R_BA:
3279 Absolute branch. We don't want to mess with the lower
3280 two bits of the instruction.
3281
3282 R_CAI:
3283 The PowerPC ABI defines this as an absolute call which
3284 may be modified to become a relative call. The PowerOpen
3285 ABI does not define this relocation type.
3286
3287 R_RBA:
3288 Absolute branch which may be modified to become a
3289 relative branch.
3290
3291 R_RBAC:
3292 The PowerPC ABI defines this as an absolute branch to a
3293 fixed address which may be modified to an absolute branch
3294 to a symbol. The PowerOpen ABI does not define this
3295 relocation type.
3296
3297 R_RBRC:
3298 The PowerPC ABI defines this as an absolute branch to a
3299 fixed address which may be modified to a relative branch.
3300 The PowerOpen ABI does not define this relocation type.
3301
3302 R_BR:
3303 Relative branch. We don't want to mess with the lower
3304 two bits of the instruction.
3305
3306 R_CREL:
3307 The PowerPC ABI defines this as a relative call which may
3308 be modified to become an absolute call. The PowerOpen
3309 ABI does not define this relocation type.
3310
3311 R_RBR:
3312 A relative branch which may be modified to become an
3313 absolute branch.
3314
3315 R_RL:
3316 The PowerPC AIX ABI describes this as a load which may be
3317 changed to a load address. The PowerOpen ABI says this
3318 is the same as case R_POS.
3319
3320 R_RLA:
3321 The PowerPC AIX ABI describes this as a load address
3322 which may be changed to a load. The PowerOpen ABI says
3323 this is the same as R_POS.
3324 */
3325
3326 bfd_boolean
3327 xcoff_ppc_relocate_section (bfd *output_bfd,
3328 struct bfd_link_info *info,
3329 bfd *input_bfd,
3330 asection *input_section,
3331 bfd_byte *contents,
3332 struct internal_reloc *relocs,
3333 struct internal_syment *syms,
3334 asection **sections)
3335 {
3336 struct internal_reloc *rel;
3337 struct internal_reloc *relend;
3338
3339 rel = relocs;
3340 relend = rel + input_section->reloc_count;
3341 for (; rel < relend; rel++)
3342 {
3343 long symndx;
3344 struct xcoff_link_hash_entry *h;
3345 struct internal_syment *sym;
3346 bfd_vma addend;
3347 bfd_vma val;
3348 struct reloc_howto_struct howto;
3349 bfd_vma relocation;
3350 bfd_vma value_to_relocate;
3351 bfd_vma address;
3352 bfd_byte *location;
3353
3354 /* Relocation type R_REF is a special relocation type which is
3355 merely used to prevent garbage collection from occurring for
3356 the csect including the symbol which it references. */
3357 if (rel->r_type == R_REF)
3358 continue;
3359
3360 /* howto */
3361 howto.type = rel->r_type;
3362 howto.rightshift = 0;
3363 howto.bitsize = (rel->r_size & 0x1f) + 1;
3364 howto.size = howto.bitsize > 16 ? 2 : 1;
3365 howto.pc_relative = FALSE;
3366 howto.bitpos = 0;
3367 howto.complain_on_overflow = (rel->r_size & 0x80
3368 ? complain_overflow_signed
3369 : complain_overflow_bitfield);
3370 howto.special_function = NULL;
3371 howto.name = "internal";
3372 howto.partial_inplace = TRUE;
3373 howto.src_mask = howto.dst_mask = N_ONES (howto.bitsize);
3374 howto.pcrel_offset = FALSE;
3375
3376 /* symbol */
3377 val = 0;
3378 addend = 0;
3379 h = NULL;
3380 sym = NULL;
3381 symndx = rel->r_symndx;
3382
3383 if (-1 != symndx)
3384 {
3385 asection *sec;
3386
3387 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
3388 sym = syms + symndx;
3389 addend = - sym->n_value;
3390
3391 if (NULL == h)
3392 {
3393 sec = sections[symndx];
3394 /* Hack to make sure we use the right TOC anchor value
3395 if this reloc is against the TOC anchor. */
3396 if (sec->name[3] == '0'
3397 && strcmp (sec->name, ".tc0") == 0)
3398 val = xcoff_data (output_bfd)->toc;
3399 else
3400 val = (sec->output_section->vma
3401 + sec->output_offset
3402 + sym->n_value
3403 - sec->vma);
3404 }
3405 else
3406 {
3407 if (info->unresolved_syms_in_objects != RM_IGNORE
3408 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3409 (*info->callbacks->undefined_symbol)
3410 (info, h->root.root.string,
3411 input_bfd, input_section,
3412 rel->r_vaddr - input_section->vma,
3413 info->unresolved_syms_in_objects == RM_GENERATE_ERROR);
3414
3415 if (h->root.type == bfd_link_hash_defined
3416 || h->root.type == bfd_link_hash_defweak)
3417 {
3418 sec = h->root.u.def.section;
3419 val = (h->root.u.def.value
3420 + sec->output_section->vma
3421 + sec->output_offset);
3422 }
3423 else if (h->root.type == bfd_link_hash_common)
3424 {
3425 sec = h->root.u.c.p->section;
3426 val = (sec->output_section->vma
3427 + sec->output_offset);
3428
3429 }
3430 else
3431 {
3432 BFD_ASSERT (bfd_link_relocatable (info)
3433 || (info->static_link
3434 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3435 || (h->flags & XCOFF_DEF_DYNAMIC) != 0
3436 || (h->flags & XCOFF_IMPORT) != 0);
3437 }
3438 }
3439 }
3440
3441 if (rel->r_type >= XCOFF_MAX_CALCULATE_RELOCATION
3442 || !((*xcoff_calculate_relocation[rel->r_type])
3443 (input_bfd, input_section, output_bfd, rel, sym, &howto, val,
3444 addend, &relocation, contents)))
3445 return FALSE;
3446
3447 /* address */
3448 address = rel->r_vaddr - input_section->vma;
3449 location = contents + address;
3450
3451 if (address > input_section->size)
3452 abort ();
3453
3454 /* Get the value we are going to relocate. */
3455 if (1 == howto.size)
3456 value_to_relocate = bfd_get_16 (input_bfd, location);
3457 else
3458 value_to_relocate = bfd_get_32 (input_bfd, location);
3459
3460 /* overflow.
3461
3462 FIXME: We may drop bits during the addition
3463 which we don't check for. We must either check at every single
3464 operation, which would be tedious, or we must do the computations
3465 in a type larger than bfd_vma, which would be inefficient. */
3466
3467 if ((unsigned int) howto.complain_on_overflow
3468 >= XCOFF_MAX_COMPLAIN_OVERFLOW)
3469 abort ();
3470
3471 if (((*xcoff_complain_overflow[howto.complain_on_overflow])
3472 (input_bfd, value_to_relocate, relocation, &howto)))
3473 {
3474 const char *name;
3475 char buf[SYMNMLEN + 1];
3476 char reloc_type_name[10];
3477
3478 if (symndx == -1)
3479 {
3480 name = "*ABS*";
3481 }
3482 else if (h != NULL)
3483 {
3484 name = NULL;
3485 }
3486 else
3487 {
3488 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3489 if (name == NULL)
3490 name = "UNKNOWN";
3491 }
3492 sprintf (reloc_type_name, "0x%02x", rel->r_type);
3493
3494 (*info->callbacks->reloc_overflow)
3495 (info, (h ? &h->root : NULL), name, reloc_type_name,
3496 (bfd_vma) 0, input_bfd, input_section,
3497 rel->r_vaddr - input_section->vma);
3498 }
3499
3500 /* Add RELOCATION to the right bits of VALUE_TO_RELOCATE. */
3501 value_to_relocate = ((value_to_relocate & ~howto.dst_mask)
3502 | (((value_to_relocate & howto.src_mask)
3503 + relocation) & howto.dst_mask));
3504
3505 /* Put the value back in the object file. */
3506 if (1 == howto.size)
3507 bfd_put_16 (input_bfd, value_to_relocate, location);
3508 else
3509 bfd_put_32 (input_bfd, value_to_relocate, location);
3510 }
3511
3512 return TRUE;
3513 }
3514
3515 static bfd_boolean
3516 _bfd_xcoff_put_ldsymbol_name (bfd *abfd ATTRIBUTE_UNUSED,
3517 struct xcoff_loader_info *ldinfo,
3518 struct internal_ldsym *ldsym,
3519 const char *name)
3520 {
3521 size_t len;
3522 len = strlen (name);
3523
3524 if (len <= SYMNMLEN)
3525 strncpy (ldsym->_l._l_name, name, SYMNMLEN);
3526 else
3527 {
3528 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3529 {
3530 bfd_size_type newalc;
3531 char *newstrings;
3532
3533 newalc = ldinfo->string_alc * 2;
3534 if (newalc == 0)
3535 newalc = 32;
3536 while (ldinfo->string_size + len + 3 > newalc)
3537 newalc *= 2;
3538
3539 newstrings = bfd_realloc (ldinfo->strings, newalc);
3540 if (newstrings == NULL)
3541 {
3542 ldinfo->failed = TRUE;
3543 return FALSE;
3544 }
3545 ldinfo->string_alc = newalc;
3546 ldinfo->strings = newstrings;
3547 }
3548
3549 bfd_put_16 (ldinfo->output_bfd, (bfd_vma) (len + 1),
3550 ldinfo->strings + ldinfo->string_size);
3551 strcpy (ldinfo->strings + ldinfo->string_size + 2, name);
3552 ldsym->_l._l_l._l_zeroes = 0;
3553 ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3554 ldinfo->string_size += len + 3;
3555 }
3556
3557 return TRUE;
3558 }
3559
3560 static bfd_boolean
3561 _bfd_xcoff_put_symbol_name (struct bfd_link_info *info,
3562 struct bfd_strtab_hash *strtab,
3563 struct internal_syment *sym,
3564 const char *name)
3565 {
3566 if (strlen (name) <= SYMNMLEN)
3567 {
3568 strncpy (sym->_n._n_name, name, SYMNMLEN);
3569 }
3570 else
3571 {
3572 bfd_boolean hash;
3573 bfd_size_type indx;
3574
3575 hash = !info->traditional_format;
3576 indx = _bfd_stringtab_add (strtab, name, hash, FALSE);
3577 if (indx == (bfd_size_type) -1)
3578 return FALSE;
3579 sym->_n._n_n._n_zeroes = 0;
3580 sym->_n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3581 }
3582 return TRUE;
3583 }
3584
3585 static asection *
3586 xcoff_create_csect_from_smclas (bfd *abfd,
3587 union internal_auxent *aux,
3588 const char *symbol_name)
3589 {
3590 asection *return_value = NULL;
3591
3592 /* .sv64 = x_smclas == 17
3593 This is an invalid csect for 32 bit apps. */
3594 static const char * const names[] =
3595 {
3596 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo", /* 0 - 7 */
3597 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0", /* 8 - 15 */
3598 ".td", NULL, ".sv3264", NULL, ".tl", ".ul", ".te"
3599 };
3600
3601 if ((aux->x_csect.x_smclas < ARRAY_SIZE (names))
3602 && (NULL != names[aux->x_csect.x_smclas]))
3603 {
3604 return_value = bfd_make_section_anyway
3605 (abfd, names[aux->x_csect.x_smclas]);
3606 }
3607 else
3608 {
3609 _bfd_error_handler
3610 /* xgettext: c-format */
3611 (_("%B: symbol `%s' has unrecognized smclas %d"),
3612 abfd, symbol_name, aux->x_csect.x_smclas);
3613 bfd_set_error (bfd_error_bad_value);
3614 }
3615
3616 return return_value;
3617 }
3618
3619 static bfd_boolean
3620 xcoff_is_lineno_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3621 {
3622 if (0xffff <= value)
3623 return TRUE;
3624
3625 return FALSE;
3626 }
3627
3628 static bfd_boolean
3629 xcoff_is_reloc_count_overflow (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma value)
3630 {
3631 if (0xffff <= value)
3632 return TRUE;
3633
3634 return FALSE;
3635 }
3636
3637 static bfd_vma
3638 xcoff_loader_symbol_offset (bfd *abfd,
3639 struct internal_ldhdr *ldhdr ATTRIBUTE_UNUSED)
3640 {
3641 return bfd_xcoff_ldhdrsz (abfd);
3642 }
3643
3644 static bfd_vma
3645 xcoff_loader_reloc_offset (bfd *abfd, struct internal_ldhdr *ldhdr)
3646 {
3647 return bfd_xcoff_ldhdrsz (abfd) + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (abfd);
3648 }
3649
3650 static bfd_boolean
3651 xcoff_generate_rtinit (bfd *abfd, const char *init, const char *fini,
3652 bfd_boolean rtld)
3653 {
3654 bfd_byte filehdr_ext[FILHSZ];
3655 bfd_byte scnhdr_ext[SCNHSZ];
3656 bfd_byte syment_ext[SYMESZ * 10];
3657 bfd_byte reloc_ext[RELSZ * 3];
3658 bfd_byte *data_buffer;
3659 bfd_size_type data_buffer_size;
3660 bfd_byte *string_table = NULL, *st_tmp = NULL;
3661 bfd_size_type string_table_size;
3662 bfd_vma val;
3663 size_t initsz, finisz;
3664 struct internal_filehdr filehdr;
3665 struct internal_scnhdr scnhdr;
3666 struct internal_syment syment;
3667 union internal_auxent auxent;
3668 struct internal_reloc reloc;
3669
3670 char *data_name = ".data";
3671 char *rtinit_name = "__rtinit";
3672 char *rtld_name = "__rtld";
3673
3674 if (! bfd_xcoff_rtinit_size (abfd))
3675 return FALSE;
3676
3677 initsz = (init == NULL ? 0 : 1 + strlen (init));
3678 finisz = (fini == NULL ? 0 : 1 + strlen (fini));
3679
3680 /* file header */
3681 memset (filehdr_ext, 0, FILHSZ);
3682 memset (&filehdr, 0, sizeof (struct internal_filehdr));
3683 filehdr.f_magic = bfd_xcoff_magic_number (abfd);
3684 filehdr.f_nscns = 1;
3685 filehdr.f_timdat = 0;
3686 filehdr.f_nsyms = 0; /* at least 6, no more than 10 */
3687 filehdr.f_symptr = 0; /* set below */
3688 filehdr.f_opthdr = 0;
3689 filehdr.f_flags = 0;
3690
3691 /* section header */
3692 memset (scnhdr_ext, 0, SCNHSZ);
3693 memset (&scnhdr, 0, sizeof (struct internal_scnhdr));
3694 memcpy (scnhdr.s_name, data_name, strlen (data_name));
3695 scnhdr.s_paddr = 0;
3696 scnhdr.s_vaddr = 0;
3697 scnhdr.s_size = 0; /* set below */
3698 scnhdr.s_scnptr = FILHSZ + SCNHSZ;
3699 scnhdr.s_relptr = 0; /* set below */
3700 scnhdr.s_lnnoptr = 0;
3701 scnhdr.s_nreloc = 0; /* either 1 or 2 */
3702 scnhdr.s_nlnno = 0;
3703 scnhdr.s_flags = STYP_DATA;
3704
3705 /* .data
3706 0x0000 0x00000000 : rtl
3707 0x0004 0x00000010 : offset to init, or 0
3708 0x0008 0x00000028 : offset to fini, or 0
3709 0x000C 0x0000000C : size of descriptor
3710 0x0010 0x00000000 : init, needs a reloc
3711 0x0014 0x00000040 : offset to init name
3712 0x0018 0x00000000 : flags, padded to a word
3713 0x001C 0x00000000 : empty init
3714 0x0020 0x00000000 :
3715 0x0024 0x00000000 :
3716 0x0028 0x00000000 : fini, needs a reloc
3717 0x002C 0x00000??? : offset to fini name
3718 0x0030 0x00000000 : flags, padded to a word
3719 0x0034 0x00000000 : empty fini
3720 0x0038 0x00000000 :
3721 0x003C 0x00000000 :
3722 0x0040 init name
3723 0x0040 + initsz fini name */
3724
3725 data_buffer_size = 0x0040 + initsz + finisz;
3726 data_buffer_size = (data_buffer_size + 7) &~ (bfd_size_type) 7;
3727 data_buffer = NULL;
3728 data_buffer = (bfd_byte *) bfd_zmalloc (data_buffer_size);
3729 if (data_buffer == NULL)
3730 return FALSE;
3731
3732 if (initsz)
3733 {
3734 val = 0x10;
3735 bfd_h_put_32 (abfd, val, &data_buffer[0x04]);
3736 val = 0x40;
3737 bfd_h_put_32 (abfd, val, &data_buffer[0x14]);
3738 memcpy (&data_buffer[val], init, initsz);
3739 }
3740
3741 if (finisz)
3742 {
3743 val = 0x28;
3744 bfd_h_put_32 (abfd, val, &data_buffer[0x08]);
3745 val = 0x40 + initsz;
3746 bfd_h_put_32 (abfd, val, &data_buffer[0x2C]);
3747 memcpy (&data_buffer[val], fini, finisz);
3748 }
3749
3750 val = 0x0C;
3751 bfd_h_put_32 (abfd, val, &data_buffer[0x0C]);
3752
3753 scnhdr.s_size = data_buffer_size;
3754
3755 /* string table */
3756 string_table_size = 0;
3757 if (initsz > 9)
3758 string_table_size += initsz;
3759 if (finisz > 9)
3760 string_table_size += finisz;
3761 if (string_table_size)
3762 {
3763 string_table_size += 4;
3764 string_table = (bfd_byte *) bfd_zmalloc (string_table_size);
3765 if (string_table == NULL)
3766 return FALSE;
3767
3768 val = string_table_size;
3769 bfd_h_put_32 (abfd, val, &string_table[0]);
3770 st_tmp = string_table + 4;
3771 }
3772
3773 /* symbols
3774 0. .data csect
3775 2. __rtinit
3776 4. init function
3777 6. fini function
3778 8. __rtld */
3779 memset (syment_ext, 0, 10 * SYMESZ);
3780 memset (reloc_ext, 0, 3 * RELSZ);
3781
3782 /* .data csect */
3783 memset (&syment, 0, sizeof (struct internal_syment));
3784 memset (&auxent, 0, sizeof (union internal_auxent));
3785 memcpy (syment._n._n_name, data_name, strlen (data_name));
3786 syment.n_scnum = 1;
3787 syment.n_sclass = C_HIDEXT;
3788 syment.n_numaux = 1;
3789 auxent.x_csect.x_scnlen.l = data_buffer_size;
3790 auxent.x_csect.x_smtyp = 3 << 3 | XTY_SD;
3791 auxent.x_csect.x_smclas = XMC_RW;
3792 bfd_coff_swap_sym_out (abfd, &syment,
3793 &syment_ext[filehdr.f_nsyms * SYMESZ]);
3794 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3795 syment.n_numaux,
3796 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3797 filehdr.f_nsyms += 2;
3798
3799 /* __rtinit */
3800 memset (&syment, 0, sizeof (struct internal_syment));
3801 memset (&auxent, 0, sizeof (union internal_auxent));
3802 memcpy (syment._n._n_name, rtinit_name, strlen (rtinit_name));
3803 syment.n_scnum = 1;
3804 syment.n_sclass = C_EXT;
3805 syment.n_numaux = 1;
3806 auxent.x_csect.x_smtyp = XTY_LD;
3807 auxent.x_csect.x_smclas = XMC_RW;
3808 bfd_coff_swap_sym_out (abfd, &syment,
3809 &syment_ext[filehdr.f_nsyms * SYMESZ]);
3810 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3811 syment.n_numaux,
3812 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3813 filehdr.f_nsyms += 2;
3814
3815 /* init */
3816 if (initsz)
3817 {
3818 memset (&syment, 0, sizeof (struct internal_syment));
3819 memset (&auxent, 0, sizeof (union internal_auxent));
3820
3821 if (initsz > 9)
3822 {
3823 syment._n._n_n._n_offset = st_tmp - string_table;
3824 memcpy (st_tmp, init, initsz);
3825 st_tmp += initsz;
3826 }
3827 else
3828 memcpy (syment._n._n_name, init, initsz - 1);
3829
3830 syment.n_sclass = C_EXT;
3831 syment.n_numaux = 1;
3832 bfd_coff_swap_sym_out (abfd, &syment,
3833 &syment_ext[filehdr.f_nsyms * SYMESZ]);
3834 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3835 syment.n_numaux,
3836 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3837
3838 /* reloc */
3839 memset (&reloc, 0, sizeof (struct internal_reloc));
3840 reloc.r_vaddr = 0x0010;
3841 reloc.r_symndx = filehdr.f_nsyms;
3842 reloc.r_type = R_POS;
3843 reloc.r_size = 31;
3844 bfd_coff_swap_reloc_out (abfd, &reloc, &reloc_ext[0]);
3845
3846 filehdr.f_nsyms += 2;
3847 scnhdr.s_nreloc += 1;
3848 }
3849
3850 /* fini */
3851 if (finisz)
3852 {
3853 memset (&syment, 0, sizeof (struct internal_syment));
3854 memset (&auxent, 0, sizeof (union internal_auxent));
3855
3856 if (finisz > 9)
3857 {
3858 syment._n._n_n._n_offset = st_tmp - string_table;
3859 memcpy (st_tmp, fini, finisz);
3860 st_tmp += finisz;
3861 }
3862 else
3863 memcpy (syment._n._n_name, fini, finisz - 1);
3864
3865 syment.n_sclass = C_EXT;
3866 syment.n_numaux = 1;
3867 bfd_coff_swap_sym_out (abfd, &syment,
3868 &syment_ext[filehdr.f_nsyms * SYMESZ]);
3869 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3870 syment.n_numaux,
3871 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3872
3873 /* reloc */
3874 memset (&reloc, 0, sizeof (struct internal_reloc));
3875 reloc.r_vaddr = 0x0028;
3876 reloc.r_symndx = filehdr.f_nsyms;
3877 reloc.r_type = R_POS;
3878 reloc.r_size = 31;
3879 bfd_coff_swap_reloc_out (abfd, &reloc,
3880 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3881
3882 filehdr.f_nsyms += 2;
3883 scnhdr.s_nreloc += 1;
3884 }
3885
3886 if (rtld)
3887 {
3888 memset (&syment, 0, sizeof (struct internal_syment));
3889 memset (&auxent, 0, sizeof (union internal_auxent));
3890 memcpy (syment._n._n_name, rtld_name, strlen (rtld_name));
3891 syment.n_sclass = C_EXT;
3892 syment.n_numaux = 1;
3893 bfd_coff_swap_sym_out (abfd, &syment,
3894 &syment_ext[filehdr.f_nsyms * SYMESZ]);
3895 bfd_coff_swap_aux_out (abfd, &auxent, syment.n_type, syment.n_sclass, 0,
3896 syment.n_numaux,
3897 &syment_ext[(filehdr.f_nsyms + 1) * SYMESZ]);
3898
3899 /* reloc */
3900 memset (&reloc, 0, sizeof (struct internal_reloc));
3901 reloc.r_vaddr = 0x0000;
3902 reloc.r_symndx = filehdr.f_nsyms;
3903 reloc.r_type = R_POS;
3904 reloc.r_size = 31;
3905 bfd_coff_swap_reloc_out (abfd, &reloc,
3906 &reloc_ext[scnhdr.s_nreloc * RELSZ]);
3907
3908 filehdr.f_nsyms += 2;
3909 scnhdr.s_nreloc += 1;
3910 }
3911
3912 scnhdr.s_relptr = scnhdr.s_scnptr + data_buffer_size;
3913 filehdr.f_symptr = scnhdr.s_relptr + scnhdr.s_nreloc * RELSZ;
3914
3915 bfd_coff_swap_filehdr_out (abfd, &filehdr, filehdr_ext);
3916 bfd_bwrite (filehdr_ext, FILHSZ, abfd);
3917 bfd_coff_swap_scnhdr_out (abfd, &scnhdr, scnhdr_ext);
3918 bfd_bwrite (scnhdr_ext, SCNHSZ, abfd);
3919 bfd_bwrite (data_buffer, data_buffer_size, abfd);
3920 bfd_bwrite (reloc_ext, scnhdr.s_nreloc * RELSZ, abfd);
3921 bfd_bwrite (syment_ext, filehdr.f_nsyms * SYMESZ, abfd);
3922 bfd_bwrite (string_table, string_table_size, abfd);
3923
3924 free (data_buffer);
3925 data_buffer = NULL;
3926
3927 return TRUE;
3928 }
3929
3930
3931 static reloc_howto_type xcoff_dynamic_reloc =
3932 HOWTO (0, /* type */
3933 0, /* rightshift */
3934 2, /* size (0 = byte, 1 = short, 2 = long) */
3935 32, /* bitsize */
3936 FALSE, /* pc_relative */
3937 0, /* bitpos */
3938 complain_overflow_bitfield, /* complain_on_overflow */
3939 0, /* special_function */
3940 "R_POS", /* name */
3941 TRUE, /* partial_inplace */
3942 0xffffffff, /* src_mask */
3943 0xffffffff, /* dst_mask */
3944 FALSE); /* pcrel_offset */
3945
3946 /* glink
3947
3948 The first word of global linkage code must be modified by filling in
3949 the correct TOC offset. */
3950
3951 static unsigned long xcoff_glink_code[9] =
3952 {
3953 0x81820000, /* lwz r12,0(r2) */
3954 0x90410014, /* stw r2,20(r1) */
3955 0x800c0000, /* lwz r0,0(r12) */
3956 0x804c0004, /* lwz r2,4(r12) */
3957 0x7c0903a6, /* mtctr r0 */
3958 0x4e800420, /* bctr */
3959 0x00000000, /* start of traceback table */
3960 0x000c8000, /* traceback table */
3961 0x00000000, /* traceback table */
3962 };
3963
3964 /* Table to convert DWARF flags to section names. */
3965
3966 const struct xcoff_dwsect_name xcoff_dwsect_names[] = {
3967 { SSUBTYP_DWINFO, ".dwinfo", TRUE },
3968 { SSUBTYP_DWLINE, ".dwline", TRUE },
3969 { SSUBTYP_DWPBNMS, ".dwpbnms", TRUE },
3970 { SSUBTYP_DWPBTYP, ".dwpbtyp", TRUE },
3971 { SSUBTYP_DWARNGE, ".dwarnge", TRUE },
3972 { SSUBTYP_DWABREV, ".dwabrev", FALSE },
3973 { SSUBTYP_DWSTR, ".dwstr", TRUE },
3974 { SSUBTYP_DWRNGES, ".dwrnges", TRUE }
3975 };
3976
3977 /* For generic entry points. */
3978 #define _bfd_xcoff_close_and_cleanup _bfd_archive_close_and_cleanup
3979 #define _bfd_xcoff_bfd_free_cached_info bfd_true
3980 #define _bfd_xcoff_new_section_hook coff_new_section_hook
3981 #define _bfd_xcoff_get_section_contents _bfd_generic_get_section_contents
3982 #define _bfd_xcoff_get_section_contents_in_window \
3983 _bfd_generic_get_section_contents_in_window
3984
3985 /* For copy private data entry points. */
3986 #define _bfd_xcoff_bfd_copy_private_bfd_data \
3987 _bfd_xcoff_copy_private_bfd_data
3988 #define _bfd_xcoff_bfd_merge_private_bfd_data \
3989 _bfd_generic_bfd_merge_private_bfd_data
3990 #define _bfd_xcoff_bfd_copy_private_section_data \
3991 _bfd_generic_bfd_copy_private_section_data
3992 #define _bfd_xcoff_bfd_copy_private_symbol_data \
3993 _bfd_generic_bfd_copy_private_symbol_data
3994 #define _bfd_xcoff_bfd_copy_private_header_data \
3995 _bfd_generic_bfd_copy_private_header_data
3996 #define _bfd_xcoff_bfd_set_private_flags \
3997 _bfd_generic_bfd_set_private_flags
3998 #define _bfd_xcoff_bfd_print_private_bfd_data \
3999 _bfd_generic_bfd_print_private_bfd_data
4000
4001 /* For archive entry points. */
4002 #define _bfd_xcoff_slurp_extended_name_table \
4003 _bfd_noarchive_slurp_extended_name_table
4004 #define _bfd_xcoff_construct_extended_name_table \
4005 _bfd_noarchive_construct_extended_name_table
4006 #define _bfd_xcoff_truncate_arname bfd_dont_truncate_arname
4007 #define _bfd_xcoff_write_ar_hdr _bfd_generic_write_ar_hdr
4008 #define _bfd_xcoff_get_elt_at_index _bfd_generic_get_elt_at_index
4009 #define _bfd_xcoff_generic_stat_arch_elt _bfd_xcoff_stat_arch_elt
4010 #define _bfd_xcoff_update_armap_timestamp bfd_true
4011
4012 /* For symbols entry points. */
4013 #define _bfd_xcoff_get_symtab_upper_bound coff_get_symtab_upper_bound
4014 #define _bfd_xcoff_canonicalize_symtab coff_canonicalize_symtab
4015 #define _bfd_xcoff_make_empty_symbol coff_make_empty_symbol
4016 #define _bfd_xcoff_print_symbol coff_print_symbol
4017 #define _bfd_xcoff_get_symbol_info coff_get_symbol_info
4018 #define _bfd_xcoff_get_symbol_version_string \
4019 _bfd_nosymbols_get_symbol_version_string
4020 #define _bfd_xcoff_bfd_is_local_label_name _bfd_xcoff_is_local_label_name
4021 #define _bfd_xcoff_bfd_is_target_special_symbol \
4022 coff_bfd_is_target_special_symbol
4023 #define _bfd_xcoff_get_lineno coff_get_lineno
4024 #define _bfd_xcoff_find_nearest_line coff_find_nearest_line
4025 #define _bfd_xcoff_find_line coff_find_line
4026 #define _bfd_xcoff_find_inliner_info coff_find_inliner_info
4027 #define _bfd_xcoff_bfd_make_debug_symbol coff_bfd_make_debug_symbol
4028 #define _bfd_xcoff_read_minisymbols _bfd_generic_read_minisymbols
4029 #define _bfd_xcoff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
4030
4031 /* For reloc entry points. */
4032 #define _bfd_xcoff_get_reloc_upper_bound coff_get_reloc_upper_bound
4033 #define _bfd_xcoff_canonicalize_reloc coff_canonicalize_reloc
4034 #define _bfd_xcoff_set_reloc _bfd_generic_set_reloc
4035 #define _bfd_xcoff_bfd_reloc_type_lookup _bfd_xcoff_reloc_type_lookup
4036 #define _bfd_xcoff_bfd_reloc_name_lookup _bfd_xcoff_reloc_name_lookup
4037
4038 /* For link entry points. */
4039 #define _bfd_xcoff_bfd_get_relocated_section_contents \
4040 bfd_generic_get_relocated_section_contents
4041 #define _bfd_xcoff_bfd_relax_section bfd_generic_relax_section
4042 #define _bfd_xcoff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
4043 #define _bfd_xcoff_bfd_link_just_syms _bfd_generic_link_just_syms
4044 #define _bfd_xcoff_bfd_copy_link_hash_symbol_type \
4045 _bfd_generic_copy_link_hash_symbol_type
4046 #define _bfd_xcoff_bfd_link_split_section _bfd_generic_link_split_section
4047 #define _bfd_xcoff_bfd_gc_sections bfd_generic_gc_sections
4048 #define _bfd_xcoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
4049 #define _bfd_xcoff_bfd_merge_sections bfd_generic_merge_sections
4050 #define _bfd_xcoff_bfd_is_group_section bfd_generic_is_group_section
4051 #define _bfd_xcoff_bfd_discard_group bfd_generic_discard_group
4052 #define _bfd_xcoff_section_already_linked _bfd_generic_section_already_linked
4053 #define _bfd_xcoff_bfd_define_common_symbol _bfd_xcoff_define_common_symbol
4054 #define _bfd_xcoff_bfd_define_start_stop bfd_generic_define_start_stop
4055 #define _bfd_xcoff_bfd_link_check_relocs _bfd_generic_link_check_relocs
4056
4057 /* For dynamic symbols and relocs entry points. */
4058 #define _bfd_xcoff_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
4059
4060 static const struct xcoff_backend_data_rec bfd_xcoff_backend_data =
4061 {
4062 { /* COFF backend, defined in libcoff.h. */
4063 _bfd_xcoff_swap_aux_in,
4064 _bfd_xcoff_swap_sym_in,
4065 coff_swap_lineno_in,
4066 _bfd_xcoff_swap_aux_out,
4067 _bfd_xcoff_swap_sym_out,
4068 coff_swap_lineno_out,
4069 xcoff_swap_reloc_out,
4070 coff_swap_filehdr_out,
4071 coff_swap_aouthdr_out,
4072 coff_swap_scnhdr_out,
4073 FILHSZ,
4074 AOUTSZ,
4075 SCNHSZ,
4076 SYMESZ,
4077 AUXESZ,
4078 RELSZ,
4079 LINESZ,
4080 FILNMLEN,
4081 TRUE, /* _bfd_coff_long_filenames */
4082 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4083 3, /* _bfd_coff_default_section_alignment_power */
4084 FALSE, /* _bfd_coff_force_symnames_in_strings */
4085 2, /* _bfd_coff_debug_string_prefix_length */
4086 32768, /* _bfd_coff_max_nscns */
4087 coff_swap_filehdr_in,
4088 coff_swap_aouthdr_in,
4089 coff_swap_scnhdr_in,
4090 xcoff_swap_reloc_in,
4091 coff_bad_format_hook,
4092 coff_set_arch_mach_hook,
4093 coff_mkobject_hook,
4094 styp_to_sec_flags,
4095 coff_set_alignment_hook,
4096 coff_slurp_symbol_table,
4097 symname_in_debug_hook,
4098 coff_pointerize_aux_hook,
4099 coff_print_aux,
4100 dummy_reloc16_extra_cases,
4101 dummy_reloc16_estimate,
4102 NULL, /* bfd_coff_sym_is_global */
4103 coff_compute_section_file_positions,
4104 NULL, /* _bfd_coff_start_final_link */
4105 xcoff_ppc_relocate_section,
4106 coff_rtype_to_howto,
4107 NULL, /* _bfd_coff_adjust_symndx */
4108 _bfd_generic_link_add_one_symbol,
4109 coff_link_output_has_begun,
4110 coff_final_link_postscript,
4111 NULL /* print_pdata. */
4112 },
4113
4114 0x01DF, /* magic number */
4115 bfd_arch_rs6000,
4116 bfd_mach_rs6k,
4117
4118 /* Function pointers to xcoff specific swap routines. */
4119 xcoff_swap_ldhdr_in,
4120 xcoff_swap_ldhdr_out,
4121 xcoff_swap_ldsym_in,
4122 xcoff_swap_ldsym_out,
4123 xcoff_swap_ldrel_in,
4124 xcoff_swap_ldrel_out,
4125
4126 /* Sizes. */
4127 LDHDRSZ,
4128 LDSYMSZ,
4129 LDRELSZ,
4130 12, /* _xcoff_function_descriptor_size */
4131 SMALL_AOUTSZ,
4132
4133 /* Versions. */
4134 1, /* _xcoff_ldhdr_version */
4135
4136 _bfd_xcoff_put_symbol_name,
4137 _bfd_xcoff_put_ldsymbol_name,
4138 &xcoff_dynamic_reloc,
4139 xcoff_create_csect_from_smclas,
4140
4141 /* Lineno and reloc count overflow. */
4142 xcoff_is_lineno_count_overflow,
4143 xcoff_is_reloc_count_overflow,
4144
4145 xcoff_loader_symbol_offset,
4146 xcoff_loader_reloc_offset,
4147
4148 /* glink. */
4149 &xcoff_glink_code[0],
4150 36, /* _xcoff_glink_size */
4151
4152 /* rtinit */
4153 64, /* _xcoff_rtinit_size */
4154 xcoff_generate_rtinit,
4155 };
4156
4157 /* The transfer vector that leads the outside world to all of the above. */
4158 const bfd_target rs6000_xcoff_vec =
4159 {
4160 "aixcoff-rs6000",
4161 bfd_target_xcoff_flavour,
4162 BFD_ENDIAN_BIG, /* data byte order is big */
4163 BFD_ENDIAN_BIG, /* header byte order is big */
4164
4165 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4166 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4167
4168 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4169 0, /* leading char */
4170 '/', /* ar_pad_char */
4171 15, /* ar_max_namelen */
4172 0, /* match priority. */
4173
4174 /* data */
4175 bfd_getb64,
4176 bfd_getb_signed_64,
4177 bfd_putb64,
4178 bfd_getb32,
4179 bfd_getb_signed_32,
4180 bfd_putb32,
4181 bfd_getb16,
4182 bfd_getb_signed_16,
4183 bfd_putb16,
4184
4185 /* hdrs */
4186 bfd_getb64,
4187 bfd_getb_signed_64,
4188 bfd_putb64,
4189 bfd_getb32,
4190 bfd_getb_signed_32,
4191 bfd_putb32,
4192 bfd_getb16,
4193 bfd_getb_signed_16,
4194 bfd_putb16,
4195
4196 { /* bfd_check_format */
4197 _bfd_dummy_target,
4198 coff_object_p,
4199 _bfd_xcoff_archive_p,
4200 CORE_FILE_P
4201 },
4202
4203 { /* bfd_set_format */
4204 bfd_false,
4205 coff_mkobject,
4206 _bfd_generic_mkarchive,
4207 bfd_false
4208 },
4209
4210 {/* bfd_write_contents */
4211 bfd_false,
4212 coff_write_object_contents,
4213 _bfd_xcoff_write_archive_contents,
4214 bfd_false
4215 },
4216
4217 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4218 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4219 BFD_JUMP_TABLE_CORE (coff),
4220 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4221 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4222 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4223 BFD_JUMP_TABLE_WRITE (coff),
4224 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4225 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4226
4227 /* Opposite endian version, none exists */
4228 NULL,
4229
4230 & bfd_xcoff_backend_data,
4231 };
4232
4233 /* xcoff-powermac target
4234 Old target.
4235 Only difference between this target and the rs6000 target is the
4236 the default architecture and machine type used in coffcode.h
4237
4238 PowerPC Macs use the same magic numbers as RS/6000
4239 (because that's how they were bootstrapped originally),
4240 but they are always PowerPC architecture. */
4241 static const struct xcoff_backend_data_rec bfd_pmac_xcoff_backend_data =
4242 {
4243 { /* COFF backend, defined in libcoff.h. */
4244 _bfd_xcoff_swap_aux_in,
4245 _bfd_xcoff_swap_sym_in,
4246 coff_swap_lineno_in,
4247 _bfd_xcoff_swap_aux_out,
4248 _bfd_xcoff_swap_sym_out,
4249 coff_swap_lineno_out,
4250 xcoff_swap_reloc_out,
4251 coff_swap_filehdr_out,
4252 coff_swap_aouthdr_out,
4253 coff_swap_scnhdr_out,
4254 FILHSZ,
4255 AOUTSZ,
4256 SCNHSZ,
4257 SYMESZ,
4258 AUXESZ,
4259 RELSZ,
4260 LINESZ,
4261 FILNMLEN,
4262 TRUE, /* _bfd_coff_long_filenames */
4263 XCOFF_NO_LONG_SECTION_NAMES, /* _bfd_coff_long_section_names */
4264 3, /* _bfd_coff_default_section_alignment_power */
4265 FALSE, /* _bfd_coff_force_symnames_in_strings */
4266 2, /* _bfd_coff_debug_string_prefix_length */
4267 32768, /* _bfd_coff_max_nscns */
4268 coff_swap_filehdr_in,
4269 coff_swap_aouthdr_in,
4270 coff_swap_scnhdr_in,
4271 xcoff_swap_reloc_in,
4272 coff_bad_format_hook,
4273 coff_set_arch_mach_hook,
4274 coff_mkobject_hook,
4275 styp_to_sec_flags,
4276 coff_set_alignment_hook,
4277 coff_slurp_symbol_table,
4278 symname_in_debug_hook,
4279 coff_pointerize_aux_hook,
4280 coff_print_aux,
4281 dummy_reloc16_extra_cases,
4282 dummy_reloc16_estimate,
4283 NULL, /* bfd_coff_sym_is_global */
4284 coff_compute_section_file_positions,
4285 NULL, /* _bfd_coff_start_final_link */
4286 xcoff_ppc_relocate_section,
4287 coff_rtype_to_howto,
4288 NULL, /* _bfd_coff_adjust_symndx */
4289 _bfd_generic_link_add_one_symbol,
4290 coff_link_output_has_begun,
4291 coff_final_link_postscript,
4292 NULL /* print_pdata. */
4293 },
4294
4295 0x01DF, /* magic number */
4296 bfd_arch_powerpc,
4297 bfd_mach_ppc,
4298
4299 /* Function pointers to xcoff specific swap routines. */
4300 xcoff_swap_ldhdr_in,
4301 xcoff_swap_ldhdr_out,
4302 xcoff_swap_ldsym_in,
4303 xcoff_swap_ldsym_out,
4304 xcoff_swap_ldrel_in,
4305 xcoff_swap_ldrel_out,
4306
4307 /* Sizes. */
4308 LDHDRSZ,
4309 LDSYMSZ,
4310 LDRELSZ,
4311 12, /* _xcoff_function_descriptor_size */
4312 SMALL_AOUTSZ,
4313
4314 /* Versions. */
4315 1, /* _xcoff_ldhdr_version */
4316
4317 _bfd_xcoff_put_symbol_name,
4318 _bfd_xcoff_put_ldsymbol_name,
4319 &xcoff_dynamic_reloc,
4320 xcoff_create_csect_from_smclas,
4321
4322 /* Lineno and reloc count overflow. */
4323 xcoff_is_lineno_count_overflow,
4324 xcoff_is_reloc_count_overflow,
4325
4326 xcoff_loader_symbol_offset,
4327 xcoff_loader_reloc_offset,
4328
4329 /* glink. */
4330 &xcoff_glink_code[0],
4331 36, /* _xcoff_glink_size */
4332
4333 /* rtinit */
4334 0, /* _xcoff_rtinit_size */
4335 xcoff_generate_rtinit,
4336 };
4337
4338 /* The transfer vector that leads the outside world to all of the above. */
4339 const bfd_target powerpc_xcoff_vec =
4340 {
4341 "xcoff-powermac",
4342 bfd_target_xcoff_flavour,
4343 BFD_ENDIAN_BIG, /* data byte order is big */
4344 BFD_ENDIAN_BIG, /* header byte order is big */
4345
4346 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | DYNAMIC
4347 | HAS_SYMS | HAS_LOCALS | WP_TEXT),
4348
4349 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA,
4350 0, /* leading char */
4351 '/', /* ar_pad_char */
4352 15, /* ar_max_namelen */
4353 0, /* match priority. */
4354
4355 /* data */
4356 bfd_getb64,
4357 bfd_getb_signed_64,
4358 bfd_putb64,
4359 bfd_getb32,
4360 bfd_getb_signed_32,
4361 bfd_putb32,
4362 bfd_getb16,
4363 bfd_getb_signed_16,
4364 bfd_putb16,
4365
4366 /* hdrs */
4367 bfd_getb64,
4368 bfd_getb_signed_64,
4369 bfd_putb64,
4370 bfd_getb32,
4371 bfd_getb_signed_32,
4372 bfd_putb32,
4373 bfd_getb16,
4374 bfd_getb_signed_16,
4375 bfd_putb16,
4376
4377 { /* bfd_check_format */
4378 _bfd_dummy_target,
4379 coff_object_p,
4380 _bfd_xcoff_archive_p,
4381 CORE_FILE_P
4382 },
4383
4384 { /* bfd_set_format */
4385 bfd_false,
4386 coff_mkobject,
4387 _bfd_generic_mkarchive,
4388 bfd_false
4389 },
4390
4391 {/* bfd_write_contents */
4392 bfd_false,
4393 coff_write_object_contents,
4394 _bfd_xcoff_write_archive_contents,
4395 bfd_false
4396 },
4397
4398 BFD_JUMP_TABLE_GENERIC (_bfd_xcoff),
4399 BFD_JUMP_TABLE_COPY (_bfd_xcoff),
4400 BFD_JUMP_TABLE_CORE (coff),
4401 BFD_JUMP_TABLE_ARCHIVE (_bfd_xcoff),
4402 BFD_JUMP_TABLE_SYMBOLS (_bfd_xcoff),
4403 BFD_JUMP_TABLE_RELOCS (_bfd_xcoff),
4404 BFD_JUMP_TABLE_WRITE (coff),
4405 BFD_JUMP_TABLE_LINK (_bfd_xcoff),
4406 BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff),
4407
4408 /* Opposite endian version, none exists */
4409 NULL,
4410
4411 & bfd_pmac_xcoff_backend_data,
4412 };
4413