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