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