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