Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for IBM RS/6000 "XCOFF" files. |
5f771d47 ILT |
2 | Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999 |
3 | Free Software Foundation, Inc. | |
252b5132 RH |
4 | FIXME: Can someone provide a transliteration of this name into ASCII? |
5 | Using the following chars caused a compiler warning on HIUX (so I replaced | |
6 | them with octal escapes), and isn't useful without an understanding of what | |
7 | character set it is. | |
8 | Written by Metin G. Ozisik, Mimi Ph\373\364ng-Th\345o V\365, | |
9 | and John Gilmore. | |
10 | Archive support from Damon A. Permezel. | |
11 | Contributed by IBM Corporation and Cygnus Support. | |
12 | ||
13 | This file is part of BFD, the Binary File Descriptor library. | |
14 | ||
15 | This program is free software; you can redistribute it and/or modify | |
16 | it under the terms of the GNU General Public License as published by | |
17 | the Free Software Foundation; either version 2 of the License, or | |
18 | (at your option) any later version. | |
19 | ||
20 | This program is distributed in the hope that it will be useful, | |
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | GNU General Public License for more details. | |
24 | ||
25 | You should have received a copy of the GNU General Public License | |
26 | along with this program; if not, write to the Free Software | |
27 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
28 | ||
29 | /* Internalcoff.h and coffcode.h modify themselves based on this flag. */ | |
30 | #define RS6000COFF_C 1 | |
31 | ||
32 | #include "bfd.h" | |
33 | #include "sysdep.h" | |
34 | #include "libbfd.h" | |
35 | #include "coff/internal.h" | |
36 | #include "coff/rs6000.h" | |
37 | #include "libcoff.h" | |
38 | ||
39 | /* The main body of code is in coffcode.h. */ | |
40 | ||
41 | static boolean xcoff_mkobject PARAMS ((bfd *)); | |
42 | static boolean xcoff_copy_private_bfd_data PARAMS ((bfd *, bfd *)); | |
43 | static boolean xcoff_is_local_label_name PARAMS ((bfd *, const char *)); | |
44 | static void xcoff_rtype2howto | |
45 | PARAMS ((arelent *, struct internal_reloc *)); | |
46 | static reloc_howto_type *xcoff_reloc_type_lookup | |
47 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
48 | static boolean xcoff_slurp_armap PARAMS ((bfd *)); | |
49 | static const bfd_target *xcoff_archive_p PARAMS ((bfd *)); | |
50 | static PTR xcoff_read_ar_hdr PARAMS ((bfd *)); | |
51 | static bfd *xcoff_openr_next_archived_file PARAMS ((bfd *, bfd *)); | |
52 | static int xcoff_generic_stat_arch_elt PARAMS ((bfd *, struct stat *)); | |
53 | static const char *normalize_filename PARAMS ((bfd *)); | |
54 | static boolean xcoff_write_armap | |
55 | PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int)); | |
56 | static boolean xcoff_write_archive_contents PARAMS ((bfd *)); | |
57 | static int _bfd_xcoff_sizeof_headers PARAMS ((bfd *, boolean)); | |
58 | \f | |
59 | /* We use our own tdata type. Its first field is the COFF tdata type, | |
60 | so the COFF routines are compatible. */ | |
61 | ||
62 | static boolean | |
63 | xcoff_mkobject (abfd) | |
64 | bfd *abfd; | |
65 | { | |
66 | coff_data_type *coff; | |
67 | ||
68 | abfd->tdata.xcoff_obj_data = | |
69 | ((struct xcoff_tdata *) | |
70 | bfd_zalloc (abfd, sizeof (struct xcoff_tdata))); | |
71 | if (abfd->tdata.xcoff_obj_data == NULL) | |
72 | return false; | |
73 | coff = coff_data (abfd); | |
74 | coff->symbols = (coff_symbol_type *) NULL; | |
75 | coff->conversion_table = (unsigned int *) NULL; | |
76 | coff->raw_syments = (struct coff_ptr_struct *) NULL; | |
77 | coff->relocbase = 0; | |
78 | ||
79 | xcoff_data (abfd)->modtype = ('1' << 8) | 'L'; | |
80 | ||
81 | /* We set cputype to -1 to indicate that it has not been | |
82 | initialized. */ | |
83 | xcoff_data (abfd)->cputype = -1; | |
84 | ||
85 | xcoff_data (abfd)->csects = NULL; | |
86 | xcoff_data (abfd)->debug_indices = NULL; | |
87 | ||
88 | return true; | |
89 | } | |
90 | ||
91 | /* Copy XCOFF data from one BFD to another. */ | |
92 | ||
93 | static boolean | |
94 | xcoff_copy_private_bfd_data (ibfd, obfd) | |
95 | bfd *ibfd; | |
96 | bfd *obfd; | |
97 | { | |
98 | struct xcoff_tdata *ix, *ox; | |
99 | asection *sec; | |
100 | ||
101 | if (ibfd->xvec != obfd->xvec) | |
102 | return true; | |
103 | ix = xcoff_data (ibfd); | |
104 | ox = xcoff_data (obfd); | |
105 | ox->full_aouthdr = ix->full_aouthdr; | |
106 | ox->toc = ix->toc; | |
107 | if (ix->sntoc == 0) | |
108 | ox->sntoc = 0; | |
109 | else | |
110 | { | |
111 | sec = coff_section_from_bfd_index (ibfd, ix->sntoc); | |
112 | if (sec == NULL) | |
113 | ox->sntoc = 0; | |
114 | else | |
115 | ox->sntoc = sec->output_section->target_index; | |
116 | } | |
117 | if (ix->snentry == 0) | |
118 | ox->snentry = 0; | |
119 | else | |
120 | { | |
121 | sec = coff_section_from_bfd_index (ibfd, ix->snentry); | |
122 | if (sec == NULL) | |
123 | ox->snentry = 0; | |
124 | else | |
125 | ox->snentry = sec->output_section->target_index; | |
126 | } | |
127 | ox->text_align_power = ix->text_align_power; | |
128 | ox->data_align_power = ix->data_align_power; | |
129 | ox->modtype = ix->modtype; | |
130 | ox->cputype = ix->cputype; | |
131 | ox->maxdata = ix->maxdata; | |
132 | ox->maxstack = ix->maxstack; | |
133 | return true; | |
134 | } | |
135 | ||
136 | /* I don't think XCOFF really has a notion of local labels based on | |
137 | name. This will mean that ld -X doesn't actually strip anything. | |
138 | The AIX native linker does not have a -X option, and it ignores the | |
139 | -x option. */ | |
140 | ||
141 | static boolean | |
142 | xcoff_is_local_label_name (abfd, name) | |
5f771d47 ILT |
143 | bfd *abfd ATTRIBUTE_UNUSED; |
144 | const char *name ATTRIBUTE_UNUSED; | |
252b5132 RH |
145 | { |
146 | return false; | |
147 | } | |
148 | \f | |
149 | /* The XCOFF reloc table. Actually, XCOFF relocations specify the | |
150 | bitsize and whether they are signed or not, along with a | |
151 | conventional type. This table is for the types, which are used for | |
152 | different algorithms for putting in the reloc. Many of these | |
153 | relocs need special_function entries, which I have not written. */ | |
154 | ||
155 | static reloc_howto_type xcoff_howto_table[] = | |
156 | { | |
157 | /* Standard 32 bit relocation. */ | |
158 | HOWTO (0, /* type */ | |
159 | 0, /* rightshift */ | |
160 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
161 | 32, /* bitsize */ | |
162 | false, /* pc_relative */ | |
163 | 0, /* bitpos */ | |
164 | complain_overflow_bitfield, /* complain_on_overflow */ | |
165 | 0, /* special_function */ | |
166 | "R_POS", /* name */ | |
167 | true, /* partial_inplace */ | |
168 | 0xffffffff, /* src_mask */ | |
169 | 0xffffffff, /* dst_mask */ | |
170 | false), /* pcrel_offset */ | |
171 | ||
172 | /* 32 bit relocation, but store negative value. */ | |
173 | HOWTO (1, /* type */ | |
174 | 0, /* rightshift */ | |
175 | -2, /* size (0 = byte, 1 = short, 2 = long) */ | |
176 | 32, /* bitsize */ | |
177 | false, /* pc_relative */ | |
178 | 0, /* bitpos */ | |
179 | complain_overflow_bitfield, /* complain_on_overflow */ | |
180 | 0, /* special_function */ | |
181 | "R_NEG", /* name */ | |
182 | true, /* partial_inplace */ | |
183 | 0xffffffff, /* src_mask */ | |
184 | 0xffffffff, /* dst_mask */ | |
185 | false), /* pcrel_offset */ | |
186 | ||
187 | /* 32 bit PC relative relocation. */ | |
188 | HOWTO (2, /* type */ | |
189 | 0, /* rightshift */ | |
190 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
191 | 32, /* bitsize */ | |
192 | true, /* pc_relative */ | |
193 | 0, /* bitpos */ | |
194 | complain_overflow_signed, /* complain_on_overflow */ | |
195 | 0, /* special_function */ | |
196 | "R_REL", /* name */ | |
197 | true, /* partial_inplace */ | |
198 | 0xffffffff, /* src_mask */ | |
199 | 0xffffffff, /* dst_mask */ | |
200 | false), /* pcrel_offset */ | |
201 | ||
202 | /* 16 bit TOC relative relocation. */ | |
203 | HOWTO (3, /* type */ | |
204 | 0, /* rightshift */ | |
205 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
206 | 16, /* bitsize */ | |
207 | false, /* pc_relative */ | |
208 | 0, /* bitpos */ | |
209 | complain_overflow_bitfield, /* complain_on_overflow */ | |
210 | 0, /* special_function */ | |
211 | "R_TOC", /* name */ | |
212 | true, /* partial_inplace */ | |
213 | 0xffff, /* src_mask */ | |
214 | 0xffff, /* dst_mask */ | |
215 | false), /* pcrel_offset */ | |
216 | ||
217 | /* I don't really know what this is. */ | |
218 | HOWTO (4, /* type */ | |
219 | 1, /* rightshift */ | |
220 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
221 | 32, /* bitsize */ | |
222 | false, /* pc_relative */ | |
223 | 0, /* bitpos */ | |
224 | complain_overflow_bitfield, /* complain_on_overflow */ | |
225 | 0, /* special_function */ | |
226 | "R_RTB", /* name */ | |
227 | true, /* partial_inplace */ | |
228 | 0xffffffff, /* src_mask */ | |
229 | 0xffffffff, /* dst_mask */ | |
230 | false), /* pcrel_offset */ | |
231 | ||
232 | /* External TOC relative symbol. */ | |
233 | HOWTO (5, /* type */ | |
234 | 0, /* rightshift */ | |
235 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
236 | 16, /* bitsize */ | |
237 | false, /* pc_relative */ | |
238 | 0, /* bitpos */ | |
239 | complain_overflow_bitfield, /* complain_on_overflow */ | |
240 | 0, /* special_function */ | |
241 | "R_GL", /* name */ | |
242 | true, /* partial_inplace */ | |
243 | 0xffff, /* src_mask */ | |
244 | 0xffff, /* dst_mask */ | |
245 | false), /* pcrel_offset */ | |
246 | ||
247 | /* Local TOC relative symbol. */ | |
248 | HOWTO (6, /* type */ | |
249 | 0, /* rightshift */ | |
250 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
251 | 16, /* bitsize */ | |
252 | false, /* pc_relative */ | |
253 | 0, /* bitpos */ | |
254 | complain_overflow_bitfield, /* complain_on_overflow */ | |
255 | 0, /* special_function */ | |
256 | "R_TCL", /* name */ | |
257 | true, /* partial_inplace */ | |
258 | 0xffff, /* src_mask */ | |
259 | 0xffff, /* dst_mask */ | |
260 | false), /* pcrel_offset */ | |
261 | ||
5f771d47 | 262 | EMPTY_HOWTO (7), |
252b5132 RH |
263 | |
264 | /* Non modifiable absolute branch. */ | |
265 | HOWTO (8, /* type */ | |
266 | 0, /* rightshift */ | |
267 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
268 | 26, /* bitsize */ | |
269 | false, /* pc_relative */ | |
270 | 0, /* bitpos */ | |
271 | complain_overflow_bitfield, /* complain_on_overflow */ | |
272 | 0, /* special_function */ | |
273 | "R_BA", /* name */ | |
274 | true, /* partial_inplace */ | |
275 | 0x3fffffc, /* src_mask */ | |
276 | 0x3fffffc, /* dst_mask */ | |
277 | false), /* pcrel_offset */ | |
278 | ||
5f771d47 | 279 | EMPTY_HOWTO (9), |
252b5132 RH |
280 | |
281 | /* Non modifiable relative branch. */ | |
282 | HOWTO (0xa, /* type */ | |
283 | 0, /* rightshift */ | |
284 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
285 | 26, /* bitsize */ | |
286 | true, /* pc_relative */ | |
287 | 0, /* bitpos */ | |
288 | complain_overflow_signed, /* complain_on_overflow */ | |
289 | 0, /* special_function */ | |
290 | "R_BR", /* name */ | |
291 | true, /* partial_inplace */ | |
292 | 0x3fffffc, /* src_mask */ | |
293 | 0x3fffffc, /* dst_mask */ | |
294 | false), /* pcrel_offset */ | |
295 | ||
5f771d47 | 296 | EMPTY_HOWTO (0xb), |
252b5132 RH |
297 | |
298 | /* Indirect load. */ | |
299 | HOWTO (0xc, /* type */ | |
300 | 0, /* rightshift */ | |
301 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
302 | 16, /* bitsize */ | |
303 | false, /* pc_relative */ | |
304 | 0, /* bitpos */ | |
305 | complain_overflow_bitfield, /* complain_on_overflow */ | |
306 | 0, /* special_function */ | |
307 | "R_RL", /* name */ | |
308 | true, /* partial_inplace */ | |
309 | 0xffff, /* src_mask */ | |
310 | 0xffff, /* dst_mask */ | |
311 | false), /* pcrel_offset */ | |
312 | ||
313 | /* Load address. */ | |
314 | HOWTO (0xd, /* type */ | |
315 | 0, /* rightshift */ | |
316 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
317 | 16, /* bitsize */ | |
318 | false, /* pc_relative */ | |
319 | 0, /* bitpos */ | |
320 | complain_overflow_bitfield, /* complain_on_overflow */ | |
321 | 0, /* special_function */ | |
322 | "R_RLA", /* name */ | |
323 | true, /* partial_inplace */ | |
324 | 0xffff, /* src_mask */ | |
325 | 0xffff, /* dst_mask */ | |
326 | false), /* pcrel_offset */ | |
327 | ||
5f771d47 | 328 | EMPTY_HOWTO (0xe), |
252b5132 RH |
329 | |
330 | /* Non-relocating reference. */ | |
331 | HOWTO (0xf, /* type */ | |
332 | 0, /* rightshift */ | |
333 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
334 | 32, /* bitsize */ | |
335 | false, /* pc_relative */ | |
336 | 0, /* bitpos */ | |
337 | complain_overflow_bitfield, /* complain_on_overflow */ | |
338 | 0, /* special_function */ | |
339 | "R_REF", /* name */ | |
340 | false, /* partial_inplace */ | |
341 | 0, /* src_mask */ | |
342 | 0, /* dst_mask */ | |
343 | false), /* pcrel_offset */ | |
344 | ||
5f771d47 ILT |
345 | EMPTY_HOWTO (0x10), |
346 | EMPTY_HOWTO (0x11), | |
252b5132 RH |
347 | |
348 | /* TOC relative indirect load. */ | |
349 | HOWTO (0x12, /* type */ | |
350 | 0, /* rightshift */ | |
351 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
352 | 16, /* bitsize */ | |
353 | false, /* pc_relative */ | |
354 | 0, /* bitpos */ | |
355 | complain_overflow_bitfield, /* complain_on_overflow */ | |
356 | 0, /* special_function */ | |
357 | "R_TRL", /* name */ | |
358 | true, /* partial_inplace */ | |
359 | 0xffff, /* src_mask */ | |
360 | 0xffff, /* dst_mask */ | |
361 | false), /* pcrel_offset */ | |
362 | ||
363 | /* TOC relative load address. */ | |
364 | HOWTO (0x13, /* type */ | |
365 | 0, /* rightshift */ | |
366 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
367 | 16, /* bitsize */ | |
368 | false, /* pc_relative */ | |
369 | 0, /* bitpos */ | |
370 | complain_overflow_bitfield, /* complain_on_overflow */ | |
371 | 0, /* special_function */ | |
372 | "R_TRLA", /* name */ | |
373 | true, /* partial_inplace */ | |
374 | 0xffff, /* src_mask */ | |
375 | 0xffff, /* dst_mask */ | |
376 | false), /* pcrel_offset */ | |
377 | ||
378 | /* Modifiable relative branch. */ | |
379 | HOWTO (0x14, /* type */ | |
380 | 1, /* rightshift */ | |
381 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
382 | 32, /* bitsize */ | |
383 | false, /* pc_relative */ | |
384 | 0, /* bitpos */ | |
385 | complain_overflow_bitfield, /* complain_on_overflow */ | |
386 | 0, /* special_function */ | |
387 | "R_RRTBI", /* name */ | |
388 | true, /* partial_inplace */ | |
389 | 0xffffffff, /* src_mask */ | |
390 | 0xffffffff, /* dst_mask */ | |
391 | false), /* pcrel_offset */ | |
392 | ||
393 | /* Modifiable absolute branch. */ | |
394 | HOWTO (0x15, /* type */ | |
395 | 1, /* rightshift */ | |
396 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
397 | 32, /* bitsize */ | |
398 | false, /* pc_relative */ | |
399 | 0, /* bitpos */ | |
400 | complain_overflow_bitfield, /* complain_on_overflow */ | |
401 | 0, /* special_function */ | |
402 | "R_RRTBA", /* name */ | |
403 | true, /* partial_inplace */ | |
404 | 0xffffffff, /* src_mask */ | |
405 | 0xffffffff, /* dst_mask */ | |
406 | false), /* pcrel_offset */ | |
407 | ||
408 | /* Modifiable call absolute indirect. */ | |
409 | HOWTO (0x16, /* type */ | |
410 | 0, /* rightshift */ | |
411 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
412 | 16, /* bitsize */ | |
413 | false, /* pc_relative */ | |
414 | 0, /* bitpos */ | |
415 | complain_overflow_bitfield, /* complain_on_overflow */ | |
416 | 0, /* special_function */ | |
417 | "R_CAI", /* name */ | |
418 | true, /* partial_inplace */ | |
419 | 0xffff, /* src_mask */ | |
420 | 0xffff, /* dst_mask */ | |
421 | false), /* pcrel_offset */ | |
422 | ||
423 | /* Modifiable call relative. */ | |
424 | HOWTO (0x17, /* type */ | |
425 | 0, /* rightshift */ | |
426 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
427 | 16, /* bitsize */ | |
428 | false, /* pc_relative */ | |
429 | 0, /* bitpos */ | |
430 | complain_overflow_bitfield, /* complain_on_overflow */ | |
431 | 0, /* special_function */ | |
432 | "R_CREL", /* name */ | |
433 | true, /* partial_inplace */ | |
434 | 0xffff, /* src_mask */ | |
435 | 0xffff, /* dst_mask */ | |
436 | false), /* pcrel_offset */ | |
437 | ||
438 | /* Modifiable branch absolute. */ | |
439 | HOWTO (0x18, /* type */ | |
440 | 0, /* rightshift */ | |
441 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
442 | 16, /* bitsize */ | |
443 | false, /* pc_relative */ | |
444 | 0, /* bitpos */ | |
445 | complain_overflow_bitfield, /* complain_on_overflow */ | |
446 | 0, /* special_function */ | |
447 | "R_RBA", /* name */ | |
448 | true, /* partial_inplace */ | |
449 | 0xffff, /* src_mask */ | |
450 | 0xffff, /* dst_mask */ | |
451 | false), /* pcrel_offset */ | |
452 | ||
453 | /* Modifiable branch absolute. */ | |
454 | HOWTO (0x19, /* type */ | |
455 | 0, /* rightshift */ | |
456 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
457 | 16, /* bitsize */ | |
458 | false, /* pc_relative */ | |
459 | 0, /* bitpos */ | |
460 | complain_overflow_bitfield, /* complain_on_overflow */ | |
461 | 0, /* special_function */ | |
462 | "R_RBAC", /* name */ | |
463 | true, /* partial_inplace */ | |
464 | 0xffff, /* src_mask */ | |
465 | 0xffff, /* dst_mask */ | |
466 | false), /* pcrel_offset */ | |
467 | ||
468 | /* Modifiable branch relative. */ | |
469 | HOWTO (0x1a, /* type */ | |
470 | 0, /* rightshift */ | |
471 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
472 | 26, /* bitsize */ | |
473 | false, /* pc_relative */ | |
474 | 0, /* bitpos */ | |
475 | complain_overflow_signed, /* complain_on_overflow */ | |
476 | 0, /* special_function */ | |
477 | "R_RBR", /* name */ | |
478 | true, /* partial_inplace */ | |
479 | 0xffff, /* src_mask */ | |
480 | 0xffff, /* dst_mask */ | |
481 | false), /* pcrel_offset */ | |
482 | ||
483 | /* Modifiable branch absolute. */ | |
484 | HOWTO (0x1b, /* type */ | |
485 | 0, /* rightshift */ | |
486 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
487 | 16, /* bitsize */ | |
488 | false, /* pc_relative */ | |
489 | 0, /* bitpos */ | |
490 | complain_overflow_bitfield, /* complain_on_overflow */ | |
491 | 0, /* special_function */ | |
492 | "R_RBRC", /* name */ | |
493 | true, /* partial_inplace */ | |
494 | 0xffff, /* src_mask */ | |
495 | 0xffff, /* dst_mask */ | |
496 | false) /* pcrel_offset */ | |
497 | }; | |
498 | ||
499 | static void | |
500 | xcoff_rtype2howto (relent, internal) | |
501 | arelent *relent; | |
502 | struct internal_reloc *internal; | |
503 | { | |
504 | relent->howto = xcoff_howto_table + internal->r_type; | |
505 | ||
506 | /* The r_size field of an XCOFF reloc encodes the bitsize of the | |
507 | relocation, as well as indicating whether it is signed or not. | |
508 | Doublecheck that the relocation information gathered from the | |
509 | type matches this information. */ | |
510 | if (relent->howto->bitsize != ((unsigned int) internal->r_size & 0x1f) + 1) | |
511 | abort (); | |
512 | #if 0 | |
513 | if ((internal->r_size & 0x80) != 0 | |
514 | ? (relent->howto->complain_on_overflow != complain_overflow_signed) | |
515 | : (relent->howto->complain_on_overflow != complain_overflow_bitfield)) | |
516 | abort (); | |
517 | #endif | |
518 | } | |
519 | ||
520 | static reloc_howto_type * | |
521 | xcoff_reloc_type_lookup (abfd, code) | |
5f771d47 | 522 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
523 | bfd_reloc_code_real_type code; |
524 | { | |
525 | switch (code) | |
526 | { | |
527 | case BFD_RELOC_PPC_B26: | |
528 | return &xcoff_howto_table[0xa]; | |
529 | case BFD_RELOC_PPC_BA26: | |
530 | return &xcoff_howto_table[8]; | |
531 | case BFD_RELOC_PPC_TOC16: | |
532 | return &xcoff_howto_table[3]; | |
533 | case BFD_RELOC_32: | |
534 | case BFD_RELOC_CTOR: | |
535 | return &xcoff_howto_table[0]; | |
536 | default: | |
537 | return NULL; | |
538 | } | |
539 | } | |
540 | ||
541 | #define SELECT_RELOC(internal, howto) \ | |
542 | { \ | |
543 | internal.r_type = howto->type; \ | |
544 | internal.r_size = \ | |
545 | ((howto->complain_on_overflow == complain_overflow_signed \ | |
546 | ? 0x80 \ | |
547 | : 0) \ | |
548 | | (howto->bitsize - 1)); \ | |
549 | } | |
550 | \f | |
551 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3) | |
552 | ||
553 | #define COFF_LONG_FILENAMES | |
554 | ||
555 | #define RTYPE2HOWTO(cache_ptr, dst) xcoff_rtype2howto (cache_ptr, dst) | |
556 | ||
557 | #define coff_mkobject xcoff_mkobject | |
558 | #define coff_bfd_copy_private_bfd_data xcoff_copy_private_bfd_data | |
559 | #define coff_bfd_is_local_label_name xcoff_is_local_label_name | |
560 | #define coff_bfd_reloc_type_lookup xcoff_reloc_type_lookup | |
561 | #define coff_relocate_section _bfd_ppc_xcoff_relocate_section | |
562 | ||
563 | #include "coffcode.h" | |
564 | \f | |
565 | /* XCOFF archive support. The original version of this code was by | |
566 | Damon A. Permezel. It was enhanced to permit cross support, and | |
567 | writing archive files, by Ian Lance Taylor, Cygnus Support. | |
568 | ||
569 | XCOFF uses its own archive format. Everything is hooked together | |
570 | with file offset links, so it is possible to rapidly update an | |
571 | archive in place. Of course, we don't do that. An XCOFF archive | |
572 | has a real file header, not just an ARMAG string. The structure of | |
573 | the file header and of each archive header appear below. | |
574 | ||
575 | An XCOFF archive also has a member table, which is a list of | |
576 | elements in the archive (you can get that by looking through the | |
577 | linked list, but you have to read a lot more of the file). The | |
578 | member table has a normal archive header with an empty name. It is | |
579 | normally (and perhaps must be) the second to last entry in the | |
580 | archive. The member table data is almost printable ASCII. It | |
581 | starts with a 12 character decimal string which is the number of | |
582 | entries in the table. For each entry it has a 12 character decimal | |
583 | string which is the offset in the archive of that member. These | |
584 | entries are followed by a series of null terminated strings which | |
585 | are the member names for each entry. | |
586 | ||
587 | Finally, an XCOFF archive has a global symbol table, which is what | |
588 | we call the armap. The global symbol table has a normal archive | |
589 | header with an empty name. It is normally (and perhaps must be) | |
590 | the last entry in the archive. The contents start with a four byte | |
591 | binary number which is the number of entries. This is followed by | |
592 | a that many four byte binary numbers; each is the file offset of an | |
593 | entry in the archive. These numbers are followed by a series of | |
594 | null terminated strings, which are symbol names. */ | |
595 | ||
596 | /* XCOFF archives use this as a magic string. */ | |
597 | ||
598 | #define XCOFFARMAG "<aiaff>\012" | |
599 | #define SXCOFFARMAG 8 | |
600 | ||
601 | /* This terminates an XCOFF archive member name. */ | |
602 | ||
603 | #define XCOFFARFMAG "`\012" | |
604 | #define SXCOFFARFMAG 2 | |
605 | ||
606 | /* XCOFF archives start with this (printable) structure. */ | |
607 | ||
608 | struct xcoff_ar_file_hdr | |
609 | { | |
610 | /* Magic string. */ | |
611 | char magic[SXCOFFARMAG]; | |
612 | ||
613 | /* Offset of the member table (decimal ASCII string). */ | |
614 | char memoff[12]; | |
615 | ||
616 | /* Offset of the global symbol table (decimal ASCII string). */ | |
617 | char symoff[12]; | |
618 | ||
619 | /* Offset of the first member in the archive (decimal ASCII string). */ | |
620 | char firstmemoff[12]; | |
621 | ||
622 | /* Offset of the last member in the archive (decimal ASCII string). */ | |
623 | char lastmemoff[12]; | |
624 | ||
625 | /* Offset of the first member on the free list (decimal ASCII | |
626 | string). */ | |
627 | char freeoff[12]; | |
628 | }; | |
629 | ||
630 | #define SIZEOF_AR_FILE_HDR (5 * 12 + SXCOFFARMAG) | |
631 | ||
632 | /* Each XCOFF archive member starts with this (printable) structure. */ | |
633 | ||
634 | struct xcoff_ar_hdr | |
635 | { | |
636 | /* File size not including the header (decimal ASCII string). */ | |
637 | char size[12]; | |
638 | ||
639 | /* File offset of next archive member (decimal ASCII string). */ | |
640 | char nextoff[12]; | |
641 | ||
642 | /* File offset of previous archive member (decimal ASCII string). */ | |
643 | char prevoff[12]; | |
644 | ||
645 | /* File mtime (decimal ASCII string). */ | |
646 | char date[12]; | |
647 | ||
648 | /* File UID (decimal ASCII string). */ | |
649 | char uid[12]; | |
650 | ||
651 | /* File GID (decimal ASCII string). */ | |
652 | char gid[12]; | |
653 | ||
654 | /* File mode (octal ASCII string). */ | |
655 | char mode[12]; | |
656 | ||
657 | /* Length of file name (decimal ASCII string). */ | |
658 | char namlen[4]; | |
659 | ||
660 | /* This structure is followed by the file name. The length of the | |
661 | name is given in the namlen field. If the length of the name is | |
662 | odd, the name is followed by a null byte. The name and optional | |
663 | null byte are followed by XCOFFARFMAG, which is not included in | |
664 | namlen. The contents of the archive member follow; the number of | |
665 | bytes is given in the size field. */ | |
666 | }; | |
667 | ||
668 | #define SIZEOF_AR_HDR (7 * 12 + 4) | |
669 | ||
670 | /* We store a copy of the xcoff_ar_file_hdr in the tdata field of the | |
671 | artdata structure. */ | |
672 | #define xcoff_ardata(abfd) \ | |
673 | ((struct xcoff_ar_file_hdr *) bfd_ardata (abfd)->tdata) | |
674 | ||
675 | /* We store a copy of the xcoff_ar_hdr in the arelt_data field of an | |
676 | archive element. */ | |
677 | #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data)) | |
678 | #define arch_xhdr(bfd) \ | |
679 | ((struct xcoff_ar_hdr *) arch_eltdata (bfd)->arch_header) | |
680 | ||
681 | /* XCOFF archives do not have anything which corresponds to an | |
682 | extended name table. */ | |
683 | ||
684 | #define xcoff_slurp_extended_name_table bfd_false | |
685 | #define xcoff_construct_extended_name_table \ | |
686 | ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \ | |
687 | bfd_false) | |
688 | #define xcoff_truncate_arname bfd_dont_truncate_arname | |
689 | ||
690 | /* We can use the standard get_elt_at_index routine. */ | |
691 | ||
692 | #define xcoff_get_elt_at_index _bfd_generic_get_elt_at_index | |
693 | ||
694 | /* XCOFF archives do not have a timestamp. */ | |
695 | ||
696 | #define xcoff_update_armap_timestamp bfd_true | |
ad247e3c | 697 | #define xcoff_allow_commons_in_armap bfd_true |
252b5132 RH |
698 | |
699 | /* Read in the armap of an XCOFF archive. */ | |
700 | ||
701 | static boolean | |
702 | xcoff_slurp_armap (abfd) | |
703 | bfd *abfd; | |
704 | { | |
705 | file_ptr off; | |
706 | struct xcoff_ar_hdr hdr; | |
707 | size_t namlen; | |
708 | bfd_size_type sz; | |
709 | bfd_byte *contents, *cend; | |
710 | unsigned int c, i; | |
711 | carsym *arsym; | |
712 | bfd_byte *p; | |
713 | ||
714 | if (xcoff_ardata (abfd) == NULL) | |
715 | { | |
716 | bfd_has_map (abfd) = false; | |
717 | return true; | |
718 | } | |
719 | ||
720 | off = strtol (xcoff_ardata (abfd)->symoff, (char **) NULL, 10); | |
721 | if (off == 0) | |
722 | { | |
723 | bfd_has_map (abfd) = false; | |
724 | return true; | |
725 | } | |
726 | ||
727 | if (bfd_seek (abfd, off, SEEK_SET) != 0) | |
728 | return false; | |
729 | ||
730 | /* The symbol table starts with a normal archive header. */ | |
731 | if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR) | |
732 | return false; | |
733 | ||
734 | /* Skip the name (normally empty). */ | |
735 | namlen = strtol (hdr.namlen, (char **) NULL, 10); | |
736 | if (bfd_seek (abfd, ((namlen + 1) & ~1) + SXCOFFARFMAG, SEEK_CUR) != 0) | |
737 | return false; | |
738 | ||
739 | /* Read in the entire symbol table. */ | |
740 | sz = strtol (hdr.size, (char **) NULL, 10); | |
741 | contents = (bfd_byte *) bfd_alloc (abfd, sz); | |
742 | if (contents == NULL) | |
743 | return false; | |
744 | if (bfd_read ((PTR) contents, 1, sz, abfd) != sz) | |
745 | return false; | |
746 | ||
747 | /* The symbol table starts with a four byte count. */ | |
748 | c = bfd_h_get_32 (abfd, contents); | |
749 | ||
750 | if (c * 4 >= sz) | |
751 | { | |
752 | bfd_set_error (bfd_error_bad_value); | |
753 | return false; | |
754 | } | |
755 | ||
756 | bfd_ardata (abfd)->symdefs = ((carsym *) | |
757 | bfd_alloc (abfd, c * sizeof (carsym))); | |
758 | if (bfd_ardata (abfd)->symdefs == NULL) | |
759 | return false; | |
760 | ||
761 | /* After the count comes a list of four byte file offsets. */ | |
762 | for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4; | |
763 | i < c; | |
764 | ++i, ++arsym, p += 4) | |
765 | arsym->file_offset = bfd_h_get_32 (abfd, p); | |
766 | ||
767 | /* After the file offsets come null terminated symbol names. */ | |
768 | cend = contents + sz; | |
769 | for (i = 0, arsym = bfd_ardata (abfd)->symdefs; | |
770 | i < c; | |
771 | ++i, ++arsym, p += strlen ((char *) p) + 1) | |
772 | { | |
773 | if (p >= cend) | |
774 | { | |
775 | bfd_set_error (bfd_error_bad_value); | |
776 | return false; | |
777 | } | |
778 | arsym->name = (char *) p; | |
779 | } | |
780 | ||
781 | bfd_ardata (abfd)->symdef_count = c; | |
782 | bfd_has_map (abfd) = true; | |
783 | ||
784 | return true; | |
785 | } | |
786 | ||
787 | /* See if this is an XCOFF archive. */ | |
788 | ||
789 | static const bfd_target * | |
790 | xcoff_archive_p (abfd) | |
791 | bfd *abfd; | |
792 | { | |
793 | struct xcoff_ar_file_hdr hdr; | |
794 | ||
795 | if (bfd_read ((PTR) &hdr, SIZEOF_AR_FILE_HDR, 1, abfd) | |
796 | != SIZEOF_AR_FILE_HDR) | |
797 | { | |
798 | if (bfd_get_error () != bfd_error_system_call) | |
799 | bfd_set_error (bfd_error_wrong_format); | |
800 | return NULL; | |
801 | } | |
802 | ||
803 | if (strncmp (hdr.magic, XCOFFARMAG, SXCOFFARMAG) != 0) | |
804 | { | |
805 | bfd_set_error (bfd_error_wrong_format); | |
806 | return NULL; | |
807 | } | |
808 | ||
809 | /* We are setting bfd_ardata(abfd) here, but since bfd_ardata | |
810 | involves a cast, we can't do it as the left operand of | |
811 | assignment. */ | |
812 | abfd->tdata.aout_ar_data = | |
813 | (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata)); | |
814 | ||
815 | if (bfd_ardata (abfd) == (struct artdata *) NULL) | |
816 | return NULL; | |
817 | ||
818 | bfd_ardata (abfd)->first_file_filepos = strtol (hdr.firstmemoff, | |
819 | (char **) NULL, 10); | |
820 | bfd_ardata (abfd)->cache = NULL; | |
821 | bfd_ardata (abfd)->archive_head = NULL; | |
822 | bfd_ardata (abfd)->symdefs = NULL; | |
823 | bfd_ardata (abfd)->extended_names = NULL; | |
824 | ||
825 | bfd_ardata (abfd)->tdata = bfd_zalloc (abfd, SIZEOF_AR_FILE_HDR); | |
826 | if (bfd_ardata (abfd)->tdata == NULL) | |
827 | return NULL; | |
828 | ||
829 | memcpy (bfd_ardata (abfd)->tdata, &hdr, SIZEOF_AR_FILE_HDR); | |
830 | ||
831 | if (! xcoff_slurp_armap (abfd)) | |
832 | { | |
833 | bfd_release (abfd, bfd_ardata (abfd)); | |
834 | abfd->tdata.aout_ar_data = (struct artdata *) NULL; | |
835 | return NULL; | |
836 | } | |
837 | ||
838 | return abfd->xvec; | |
839 | } | |
840 | ||
841 | /* Read the archive header in an XCOFF archive. */ | |
842 | ||
843 | static PTR | |
844 | xcoff_read_ar_hdr (abfd) | |
845 | bfd *abfd; | |
846 | { | |
847 | struct xcoff_ar_hdr hdr; | |
848 | size_t namlen; | |
849 | struct xcoff_ar_hdr *hdrp; | |
850 | struct areltdata *ret; | |
851 | ||
852 | if (bfd_read ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR) | |
853 | return NULL; | |
854 | ||
855 | namlen = strtol (hdr.namlen, (char **) NULL, 10); | |
856 | hdrp = (struct xcoff_ar_hdr *) bfd_alloc (abfd, SIZEOF_AR_HDR + namlen + 1); | |
857 | if (hdrp == NULL) | |
858 | return NULL; | |
859 | memcpy (hdrp, &hdr, SIZEOF_AR_HDR); | |
860 | if (bfd_read ((char *) hdrp + SIZEOF_AR_HDR, 1, namlen, abfd) != namlen) | |
861 | return NULL; | |
862 | ((char *) hdrp)[SIZEOF_AR_HDR + namlen] = '\0'; | |
863 | ||
864 | ret = (struct areltdata *) bfd_alloc (abfd, sizeof (struct areltdata)); | |
865 | if (ret == NULL) | |
866 | return NULL; | |
867 | ret->arch_header = (char *) hdrp; | |
868 | ret->parsed_size = strtol (hdr.size, (char **) NULL, 10); | |
869 | ret->filename = (char *) hdrp + SIZEOF_AR_HDR; | |
870 | ||
871 | /* Skip over the XCOFFARFMAG at the end of the file name. */ | |
872 | if (bfd_seek (abfd, (namlen & 1) + SXCOFFARFMAG, SEEK_CUR) != 0) | |
873 | return NULL; | |
874 | ||
875 | return (PTR) ret; | |
876 | } | |
877 | ||
878 | /* Open the next element in an XCOFF archive. */ | |
879 | ||
880 | static bfd * | |
881 | xcoff_openr_next_archived_file (archive, last_file) | |
882 | bfd *archive; | |
883 | bfd *last_file; | |
884 | { | |
885 | file_ptr filestart; | |
886 | ||
887 | if (xcoff_ardata (archive) == NULL) | |
888 | { | |
889 | bfd_set_error (bfd_error_invalid_operation); | |
890 | return NULL; | |
891 | } | |
892 | ||
893 | if (last_file == NULL) | |
894 | filestart = bfd_ardata (archive)->first_file_filepos; | |
895 | else | |
896 | filestart = strtol (arch_xhdr (last_file)->nextoff, (char **) NULL, 10); | |
897 | ||
898 | if (filestart == 0 | |
899 | || filestart == strtol (xcoff_ardata (archive)->memoff, | |
900 | (char **) NULL, 10) | |
901 | || filestart == strtol (xcoff_ardata (archive)->symoff, | |
902 | (char **) NULL, 10)) | |
903 | { | |
904 | bfd_set_error (bfd_error_no_more_archived_files); | |
905 | return NULL; | |
906 | } | |
907 | ||
908 | return _bfd_get_elt_at_filepos (archive, filestart); | |
909 | } | |
910 | ||
911 | /* Stat an element in an XCOFF archive. */ | |
912 | ||
913 | static int | |
914 | xcoff_generic_stat_arch_elt (abfd, s) | |
915 | bfd *abfd; | |
916 | struct stat *s; | |
917 | { | |
918 | struct xcoff_ar_hdr *hdrp; | |
919 | ||
920 | if (abfd->arelt_data == NULL) | |
921 | { | |
922 | bfd_set_error (bfd_error_invalid_operation); | |
923 | return -1; | |
924 | } | |
925 | ||
926 | hdrp = arch_xhdr (abfd); | |
927 | ||
928 | s->st_mtime = strtol (hdrp->date, (char **) NULL, 10); | |
929 | s->st_uid = strtol (hdrp->uid, (char **) NULL, 10); | |
930 | s->st_gid = strtol (hdrp->gid, (char **) NULL, 10); | |
931 | s->st_mode = strtol (hdrp->mode, (char **) NULL, 8); | |
932 | s->st_size = arch_eltdata (abfd)->parsed_size; | |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
937 | /* Normalize a file name for inclusion in an archive. */ | |
938 | ||
939 | static const char * | |
940 | normalize_filename (abfd) | |
941 | bfd *abfd; | |
942 | { | |
943 | const char *file; | |
944 | const char *filename; | |
945 | ||
946 | file = bfd_get_filename (abfd); | |
947 | filename = strrchr (file, '/'); | |
948 | if (filename != NULL) | |
949 | filename++; | |
950 | else | |
951 | filename = file; | |
952 | return filename; | |
953 | } | |
954 | ||
955 | /* Write out an XCOFF armap. */ | |
956 | ||
957 | /*ARGSUSED*/ | |
958 | static boolean | |
959 | xcoff_write_armap (abfd, elength, map, orl_count, stridx) | |
960 | bfd *abfd; | |
5f771d47 | 961 | unsigned int elength ATTRIBUTE_UNUSED; |
252b5132 RH |
962 | struct orl *map; |
963 | unsigned int orl_count; | |
964 | int stridx; | |
965 | { | |
966 | struct xcoff_ar_hdr hdr; | |
967 | char *p; | |
968 | unsigned char buf[4]; | |
969 | bfd *sub; | |
970 | file_ptr fileoff; | |
971 | unsigned int i; | |
972 | ||
973 | memset (&hdr, 0, sizeof hdr); | |
974 | sprintf (hdr.size, "%ld", (long) (4 + orl_count * 4 + stridx)); | |
975 | sprintf (hdr.nextoff, "%d", 0); | |
976 | memcpy (hdr.prevoff, xcoff_ardata (abfd)->memoff, 12); | |
977 | sprintf (hdr.date, "%d", 0); | |
978 | sprintf (hdr.uid, "%d", 0); | |
979 | sprintf (hdr.gid, "%d", 0); | |
980 | sprintf (hdr.mode, "%d", 0); | |
981 | sprintf (hdr.namlen, "%d", 0); | |
982 | ||
983 | /* We need spaces, not null bytes, in the header. */ | |
984 | for (p = (char *) &hdr; p < (char *) &hdr + SIZEOF_AR_HDR; p++) | |
985 | if (*p == '\0') | |
986 | *p = ' '; | |
987 | ||
988 | if (bfd_write ((PTR) &hdr, SIZEOF_AR_HDR, 1, abfd) != SIZEOF_AR_HDR | |
989 | || bfd_write (XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) != SXCOFFARFMAG) | |
990 | return false; | |
991 | ||
992 | bfd_h_put_32 (abfd, orl_count, buf); | |
993 | if (bfd_write (buf, 1, 4, abfd) != 4) | |
994 | return false; | |
995 | ||
996 | sub = abfd->archive_head; | |
997 | fileoff = SIZEOF_AR_FILE_HDR; | |
998 | i = 0; | |
999 | while (sub != NULL && i < orl_count) | |
1000 | { | |
1001 | size_t namlen; | |
1002 | ||
1003 | while (((bfd *) (map[i]).pos) == sub) | |
1004 | { | |
1005 | bfd_h_put_32 (abfd, fileoff, buf); | |
1006 | if (bfd_write (buf, 1, 4, abfd) != 4) | |
1007 | return false; | |
1008 | ++i; | |
1009 | } | |
1010 | namlen = strlen (normalize_filename (sub)); | |
1011 | namlen = (namlen + 1) &~ 1; | |
1012 | fileoff += (SIZEOF_AR_HDR | |
1013 | + namlen | |
1014 | + SXCOFFARFMAG | |
1015 | + arelt_size (sub)); | |
1016 | fileoff = (fileoff + 1) &~ 1; | |
1017 | sub = sub->next; | |
1018 | } | |
1019 | ||
1020 | for (i = 0; i < orl_count; i++) | |
1021 | { | |
1022 | const char *name; | |
1023 | size_t namlen; | |
1024 | ||
1025 | name = *map[i].name; | |
1026 | namlen = strlen (name); | |
1027 | if (bfd_write (name, 1, namlen + 1, abfd) != namlen + 1) | |
1028 | return false; | |
1029 | } | |
1030 | ||
1031 | if ((stridx & 1) != 0) | |
1032 | { | |
1033 | char b; | |
1034 | ||
1035 | b = '\0'; | |
1036 | if (bfd_write (&b, 1, 1, abfd) != 1) | |
1037 | return false; | |
1038 | } | |
1039 | ||
1040 | return true; | |
1041 | } | |
1042 | ||
1043 | /* Write out an XCOFF archive. We always write an entire archive, | |
1044 | rather than fussing with the freelist and so forth. */ | |
1045 | ||
1046 | static boolean | |
1047 | xcoff_write_archive_contents (abfd) | |
1048 | bfd *abfd; | |
1049 | { | |
1050 | struct xcoff_ar_file_hdr fhdr; | |
1051 | size_t count; | |
1052 | size_t total_namlen; | |
1053 | file_ptr *offsets; | |
1054 | boolean makemap; | |
1055 | boolean hasobjects; | |
1056 | file_ptr prevoff, nextoff; | |
1057 | bfd *sub; | |
1058 | unsigned int i; | |
1059 | struct xcoff_ar_hdr ahdr; | |
1060 | bfd_size_type size; | |
1061 | char *p; | |
1062 | char decbuf[13]; | |
1063 | ||
1064 | memset (&fhdr, 0, sizeof fhdr); | |
1065 | strncpy (fhdr.magic, XCOFFARMAG, SXCOFFARMAG); | |
1066 | sprintf (fhdr.firstmemoff, "%d", SIZEOF_AR_FILE_HDR); | |
1067 | sprintf (fhdr.freeoff, "%d", 0); | |
1068 | ||
1069 | count = 0; | |
1070 | total_namlen = 0; | |
1071 | for (sub = abfd->archive_head; sub != NULL; sub = sub->next) | |
1072 | { | |
1073 | ++count; | |
1074 | total_namlen += strlen (normalize_filename (sub)) + 1; | |
1075 | } | |
1076 | offsets = (file_ptr *) bfd_alloc (abfd, count * sizeof (file_ptr)); | |
1077 | if (offsets == NULL) | |
1078 | return false; | |
1079 | ||
1080 | if (bfd_seek (abfd, SIZEOF_AR_FILE_HDR, SEEK_SET) != 0) | |
1081 | return false; | |
1082 | ||
1083 | makemap = bfd_has_map (abfd); | |
1084 | hasobjects = false; | |
1085 | prevoff = 0; | |
1086 | nextoff = SIZEOF_AR_FILE_HDR; | |
1087 | for (sub = abfd->archive_head, i = 0; sub != NULL; sub = sub->next, i++) | |
1088 | { | |
1089 | const char *name; | |
1090 | size_t namlen; | |
1091 | struct xcoff_ar_hdr *ahdrp; | |
1092 | bfd_size_type remaining; | |
1093 | ||
1094 | if (makemap && ! hasobjects) | |
1095 | { | |
1096 | if (bfd_check_format (sub, bfd_object)) | |
1097 | hasobjects = true; | |
1098 | } | |
1099 | ||
1100 | name = normalize_filename (sub); | |
1101 | namlen = strlen (name); | |
1102 | ||
1103 | if (sub->arelt_data != NULL) | |
1104 | ahdrp = arch_xhdr (sub); | |
1105 | else | |
1106 | ahdrp = NULL; | |
1107 | ||
1108 | if (ahdrp == NULL) | |
1109 | { | |
1110 | struct stat s; | |
1111 | ||
1112 | memset (&ahdr, 0, sizeof ahdr); | |
1113 | ahdrp = &ahdr; | |
1114 | if (stat (bfd_get_filename (sub), &s) != 0) | |
1115 | { | |
1116 | bfd_set_error (bfd_error_system_call); | |
1117 | return false; | |
1118 | } | |
1119 | ||
1120 | sprintf (ahdrp->size, "%ld", (long) s.st_size); | |
1121 | sprintf (ahdrp->date, "%ld", (long) s.st_mtime); | |
1122 | sprintf (ahdrp->uid, "%ld", (long) s.st_uid); | |
1123 | sprintf (ahdrp->gid, "%ld", (long) s.st_gid); | |
1124 | sprintf (ahdrp->mode, "%o", (unsigned int) s.st_mode); | |
1125 | ||
1126 | if (sub->arelt_data == NULL) | |
1127 | { | |
1128 | sub->arelt_data = bfd_alloc (sub, sizeof (struct areltdata)); | |
1129 | if (sub->arelt_data == NULL) | |
1130 | return false; | |
1131 | } | |
1132 | ||
1133 | arch_eltdata (sub)->parsed_size = s.st_size; | |
1134 | } | |
1135 | ||
1136 | sprintf (ahdrp->prevoff, "%ld", (long) prevoff); | |
1137 | sprintf (ahdrp->namlen, "%ld", (long) namlen); | |
1138 | ||
1139 | /* If the length of the name is odd, we write out the null byte | |
1140 | after the name as well. */ | |
1141 | namlen = (namlen + 1) &~ 1; | |
1142 | ||
1143 | remaining = arelt_size (sub); | |
1144 | size = (SIZEOF_AR_HDR | |
1145 | + namlen | |
1146 | + SXCOFFARFMAG | |
1147 | + remaining); | |
1148 | ||
1149 | BFD_ASSERT (nextoff == bfd_tell (abfd)); | |
1150 | ||
1151 | offsets[i] = nextoff; | |
1152 | ||
1153 | prevoff = nextoff; | |
1154 | nextoff += size + (size & 1); | |
1155 | ||
1156 | sprintf (ahdrp->nextoff, "%ld", (long) nextoff); | |
1157 | ||
1158 | /* We need spaces, not null bytes, in the header. */ | |
1159 | for (p = (char *) ahdrp; p < (char *) ahdrp + SIZEOF_AR_HDR; p++) | |
1160 | if (*p == '\0') | |
1161 | *p = ' '; | |
1162 | ||
1163 | if (bfd_write ((PTR) ahdrp, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR | |
1164 | || bfd_write ((PTR) name, 1, namlen, abfd) != namlen | |
1165 | || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) | |
1166 | != SXCOFFARFMAG)) | |
1167 | return false; | |
1168 | ||
1169 | if (bfd_seek (sub, (file_ptr) 0, SEEK_SET) != 0) | |
1170 | return false; | |
1171 | while (remaining != 0) | |
1172 | { | |
1173 | bfd_size_type amt; | |
1174 | bfd_byte buffer[DEFAULT_BUFFERSIZE]; | |
1175 | ||
1176 | amt = sizeof buffer; | |
1177 | if (amt > remaining) | |
1178 | amt = remaining; | |
1179 | if (bfd_read (buffer, 1, amt, sub) != amt | |
1180 | || bfd_write (buffer, 1, amt, abfd) != amt) | |
1181 | return false; | |
1182 | remaining -= amt; | |
1183 | } | |
1184 | ||
1185 | if ((size & 1) != 0) | |
1186 | { | |
1187 | bfd_byte b; | |
1188 | ||
1189 | b = '\0'; | |
1190 | if (bfd_write (&b, 1, 1, abfd) != 1) | |
1191 | return false; | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | sprintf (fhdr.lastmemoff, "%ld", (long) prevoff); | |
1196 | ||
1197 | /* Write out the member table. */ | |
1198 | ||
1199 | BFD_ASSERT (nextoff == bfd_tell (abfd)); | |
1200 | sprintf (fhdr.memoff, "%ld", (long) nextoff); | |
1201 | ||
1202 | memset (&ahdr, 0, sizeof ahdr); | |
1203 | sprintf (ahdr.size, "%ld", (long) (12 + count * 12 + total_namlen)); | |
1204 | sprintf (ahdr.prevoff, "%ld", (long) prevoff); | |
1205 | sprintf (ahdr.date, "%d", 0); | |
1206 | sprintf (ahdr.uid, "%d", 0); | |
1207 | sprintf (ahdr.gid, "%d", 0); | |
1208 | sprintf (ahdr.mode, "%d", 0); | |
1209 | sprintf (ahdr.namlen, "%d", 0); | |
1210 | ||
1211 | size = (SIZEOF_AR_HDR | |
1212 | + 12 | |
1213 | + count * 12 | |
1214 | + total_namlen | |
1215 | + SXCOFFARFMAG); | |
1216 | ||
1217 | prevoff = nextoff; | |
1218 | nextoff += size + (size & 1); | |
1219 | ||
1220 | if (makemap && hasobjects) | |
1221 | sprintf (ahdr.nextoff, "%ld", (long) nextoff); | |
1222 | else | |
1223 | sprintf (ahdr.nextoff, "%d", 0); | |
1224 | ||
1225 | /* We need spaces, not null bytes, in the header. */ | |
1226 | for (p = (char *) &ahdr; p < (char *) &ahdr + SIZEOF_AR_HDR; p++) | |
1227 | if (*p == '\0') | |
1228 | *p = ' '; | |
1229 | ||
1230 | if (bfd_write ((PTR) &ahdr, 1, SIZEOF_AR_HDR, abfd) != SIZEOF_AR_HDR | |
1231 | || (bfd_write ((PTR) XCOFFARFMAG, 1, SXCOFFARFMAG, abfd) | |
1232 | != SXCOFFARFMAG)) | |
1233 | return false; | |
1234 | ||
1235 | sprintf (decbuf, "%-12ld", (long) count); | |
1236 | if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12) | |
1237 | return false; | |
1238 | for (i = 0; i < count; i++) | |
1239 | { | |
1240 | sprintf (decbuf, "%-12ld", (long) offsets[i]); | |
1241 | if (bfd_write ((PTR) decbuf, 1, 12, abfd) != 12) | |
1242 | return false; | |
1243 | } | |
1244 | for (sub = abfd->archive_head; sub != NULL; sub = sub->next) | |
1245 | { | |
1246 | const char *name; | |
1247 | size_t namlen; | |
1248 | ||
1249 | name = normalize_filename (sub); | |
1250 | namlen = strlen (name); | |
1251 | if (bfd_write ((PTR) name, 1, namlen + 1, abfd) != namlen + 1) | |
1252 | return false; | |
1253 | } | |
1254 | if ((size & 1) != 0) | |
1255 | { | |
1256 | bfd_byte b; | |
1257 | ||
1258 | b = '\0'; | |
1259 | if (bfd_write ((PTR) &b, 1, 1, abfd) != 1) | |
1260 | return false; | |
1261 | } | |
1262 | ||
1263 | /* Write out the armap, if appropriate. */ | |
1264 | ||
1265 | if (! makemap || ! hasobjects) | |
1266 | sprintf (fhdr.symoff, "%d", 0); | |
1267 | else | |
1268 | { | |
1269 | BFD_ASSERT (nextoff == bfd_tell (abfd)); | |
1270 | sprintf (fhdr.symoff, "%ld", (long) nextoff); | |
1271 | bfd_ardata (abfd)->tdata = (PTR) &fhdr; | |
1272 | if (! _bfd_compute_and_write_armap (abfd, 0)) | |
1273 | return false; | |
1274 | } | |
1275 | ||
1276 | /* Write out the archive file header. */ | |
1277 | ||
1278 | /* We need spaces, not null bytes, in the header. */ | |
1279 | for (p = (char *) &fhdr; p < (char *) &fhdr + SIZEOF_AR_FILE_HDR; p++) | |
1280 | if (*p == '\0') | |
1281 | *p = ' '; | |
1282 | ||
1283 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 | |
1284 | || (bfd_write ((PTR) &fhdr, SIZEOF_AR_FILE_HDR, 1, abfd) != | |
1285 | SIZEOF_AR_FILE_HDR)) | |
1286 | return false; | |
1287 | ||
1288 | return true; | |
1289 | } | |
1290 | \f | |
1291 | /* We can't use the usual coff_sizeof_headers routine, because AIX | |
1292 | always uses an a.out header. */ | |
1293 | ||
1294 | /*ARGSUSED*/ | |
1295 | static int | |
1296 | _bfd_xcoff_sizeof_headers (abfd, reloc) | |
1297 | bfd *abfd; | |
5f771d47 | 1298 | boolean reloc ATTRIBUTE_UNUSED; |
252b5132 RH |
1299 | { |
1300 | int size; | |
1301 | ||
1302 | size = FILHSZ; | |
1303 | if (xcoff_data (abfd)->full_aouthdr) | |
1304 | size += AOUTSZ; | |
1305 | else | |
1306 | size += SMALL_AOUTSZ; | |
1307 | size += abfd->section_count * SCNHSZ; | |
1308 | return size; | |
1309 | } | |
1310 | \f | |
1311 | #define CORE_FILE_P _bfd_dummy_target | |
1312 | ||
1313 | #define coff_core_file_failing_command _bfd_nocore_core_file_failing_command | |
1314 | #define coff_core_file_failing_signal _bfd_nocore_core_file_failing_signal | |
1315 | #define coff_core_file_matches_executable_p \ | |
1316 | _bfd_nocore_core_file_matches_executable_p | |
1317 | ||
1318 | #ifdef AIX_CORE | |
1319 | #undef CORE_FILE_P | |
1320 | #define CORE_FILE_P rs6000coff_core_p | |
1321 | extern const bfd_target * rs6000coff_core_p (); | |
1322 | extern boolean rs6000coff_get_section_contents (); | |
1323 | extern boolean rs6000coff_core_file_matches_executable_p (); | |
1324 | ||
1325 | #undef coff_core_file_matches_executable_p | |
1326 | #define coff_core_file_matches_executable_p \ | |
1327 | rs6000coff_core_file_matches_executable_p | |
1328 | ||
1329 | extern char *rs6000coff_core_file_failing_command PARAMS ((bfd *abfd)); | |
1330 | #undef coff_core_file_failing_command | |
1331 | #define coff_core_file_failing_command rs6000coff_core_file_failing_command | |
1332 | ||
1333 | extern int rs6000coff_core_file_failing_signal PARAMS ((bfd *abfd)); | |
1334 | #undef coff_core_file_failing_signal | |
1335 | #define coff_core_file_failing_signal rs6000coff_core_file_failing_signal | |
1336 | ||
1337 | #undef coff_get_section_contents | |
1338 | #define coff_get_section_contents rs6000coff_get_section_contents | |
1339 | #endif /* AIX_CORE */ | |
1340 | ||
1341 | #ifdef LYNX_CORE | |
1342 | ||
1343 | #undef CORE_FILE_P | |
1344 | #define CORE_FILE_P lynx_core_file_p | |
1345 | extern const bfd_target *lynx_core_file_p PARAMS ((bfd *abfd)); | |
1346 | ||
1347 | extern boolean lynx_core_file_matches_executable_p PARAMS ((bfd *core_bfd, | |
1348 | bfd *exec_bfd)); | |
1349 | #undef coff_core_file_matches_executable_p | |
1350 | #define coff_core_file_matches_executable_p lynx_core_file_matches_executable_p | |
1351 | ||
1352 | extern char *lynx_core_file_failing_command PARAMS ((bfd *abfd)); | |
1353 | #undef coff_core_file_failing_command | |
1354 | #define coff_core_file_failing_command lynx_core_file_failing_command | |
1355 | ||
1356 | extern int lynx_core_file_failing_signal PARAMS ((bfd *abfd)); | |
1357 | #undef coff_core_file_failing_signal | |
1358 | #define coff_core_file_failing_signal lynx_core_file_failing_signal | |
1359 | ||
1360 | #endif /* LYNX_CORE */ | |
1361 | ||
1362 | #define _bfd_xcoff_bfd_get_relocated_section_contents \ | |
1363 | coff_bfd_get_relocated_section_contents | |
1364 | #define _bfd_xcoff_bfd_relax_section coff_bfd_relax_section | |
1365 | #define _bfd_xcoff_bfd_gc_sections coff_bfd_gc_sections | |
1366 | #define _bfd_xcoff_bfd_link_split_section coff_bfd_link_split_section | |
1367 | ||
1368 | /* The transfer vector that leads the outside world to all of the above. */ | |
1369 | ||
1370 | const bfd_target | |
1371 | #ifdef TARGET_SYM | |
1372 | TARGET_SYM = | |
1373 | #else | |
1374 | rs6000coff_vec = | |
1375 | #endif | |
1376 | { | |
1377 | #ifdef TARGET_NAME | |
1378 | TARGET_NAME, | |
1379 | #else | |
1380 | "aixcoff-rs6000", /* name */ | |
1381 | #endif | |
1382 | bfd_target_coff_flavour, | |
1383 | BFD_ENDIAN_BIG, /* data byte order is big */ | |
1384 | BFD_ENDIAN_BIG, /* header byte order is big */ | |
1385 | ||
1386 | (HAS_RELOC | EXEC_P | /* object flags */ | |
1387 | HAS_LINENO | HAS_DEBUG | DYNAMIC | | |
1388 | HAS_SYMS | HAS_LOCALS | WP_TEXT), | |
1389 | ||
1390 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
1391 | 0, /* leading char */ | |
1392 | '/', /* ar_pad_char */ | |
1393 | 15, /* ar_max_namelen??? FIXMEmgo */ | |
1394 | ||
1395 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1396 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1397 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ | |
1398 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1399 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1400 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ | |
1401 | ||
1402 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ | |
1403 | xcoff_archive_p, CORE_FILE_P}, | |
1404 | {bfd_false, coff_mkobject, /* bfd_set_format */ | |
1405 | _bfd_generic_mkarchive, bfd_false}, | |
1406 | {bfd_false, coff_write_object_contents, /* bfd_write_contents */ | |
1407 | xcoff_write_archive_contents, bfd_false}, | |
1408 | ||
1409 | BFD_JUMP_TABLE_GENERIC (coff), | |
1410 | BFD_JUMP_TABLE_COPY (coff), | |
1411 | BFD_JUMP_TABLE_CORE (coff), | |
1412 | BFD_JUMP_TABLE_ARCHIVE (xcoff), | |
1413 | BFD_JUMP_TABLE_SYMBOLS (coff), | |
1414 | BFD_JUMP_TABLE_RELOCS (coff), | |
1415 | BFD_JUMP_TABLE_WRITE (coff), | |
1416 | BFD_JUMP_TABLE_LINK (_bfd_xcoff), | |
1417 | BFD_JUMP_TABLE_DYNAMIC (_bfd_xcoff), | |
1418 | ||
c3c89269 NC |
1419 | NULL, |
1420 | ||
1421 | COFF_SWAP_TABLE | |
252b5132 | 1422 | }; |