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