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