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