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