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