Commit | Line | Data |
---|---|---|
dae31cf5 | 1 | /* BFD back-end for ALPHA Extended-Coff files. |
fa965415 | 2 | Copyright 1993, 1994 Free Software Foundation, Inc. |
dae31cf5 ILT |
3 | Modified from coff-mips.c by Steve Chamberlain <sac@cygnus.com> and |
4 | Ian Lance Taylor <ian@cygnus.com>. | |
5f8f6d56 | 5 | |
5f8f6d56 SC |
6 | This file is part of BFD, the Binary File Descriptor library. |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
693f8191 | 24 | #include "bfdlink.h" |
5f8f6d56 | 25 | #include "libbfd.h" |
5f8f6d56 SC |
26 | #include "coff/internal.h" |
27 | #include "coff/sym.h" | |
28 | #include "coff/symconst.h" | |
dae31cf5 ILT |
29 | #include "coff/ecoff.h" |
30 | #include "coff/alpha.h" | |
5f8f6d56 SC |
31 | #include "libcoff.h" |
32 | #include "libecoff.h" | |
5f8f6d56 SC |
33 | \f |
34 | /* Prototypes for static functions. */ | |
35 | ||
3f048f7f | 36 | static bfd_target *alpha_ecoff_object_p PARAMS ((bfd *)); |
dae31cf5 | 37 | static boolean alpha_ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr)); |
dae31cf5 ILT |
38 | static void alpha_ecoff_swap_reloc_in PARAMS ((bfd *, PTR, |
39 | struct internal_reloc *)); | |
40 | static void alpha_ecoff_swap_reloc_out PARAMS ((bfd *, | |
41 | const struct internal_reloc *, | |
42 | PTR)); | |
693f8191 KR |
43 | static void alpha_adjust_reloc_in PARAMS ((bfd *, |
44 | const struct internal_reloc *, | |
45 | arelent *)); | |
46 | static void alpha_adjust_reloc_out PARAMS ((bfd *, const arelent *, | |
47 | struct internal_reloc *)); | |
48 | static bfd_byte *alpha_ecoff_get_relocated_section_contents | |
49 | PARAMS ((bfd *abfd, struct bfd_link_info *, struct bfd_link_order *, | |
50 | bfd_byte *data, boolean relocateable, asymbol **symbols)); | |
51 | static bfd_vma alpha_convert_external_reloc | |
52 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, struct external_reloc *, | |
53 | struct ecoff_link_hash_entry *)); | |
54 | static boolean alpha_relocate_section PARAMS ((bfd *, struct bfd_link_info *, | |
55 | bfd *, asection *, | |
56 | bfd_byte *, PTR)); | |
5f8f6d56 | 57 | \f |
dae31cf5 ILT |
58 | /* ECOFF has COFF sections, but the debugging information is stored in |
59 | a completely different format. ECOFF targets use some of the | |
60 | swapping routines from coffswap.h, and some of the generic COFF | |
61 | routines in coffgen.c, but, unlike the real COFF targets, do not | |
62 | use coffcode.h itself. | |
63 | ||
64 | Get the generic COFF swapping routines, except for the reloc, | |
693f8191 KR |
65 | symbol, and lineno ones. Give them ecoff names. Define some |
66 | accessor macros for the large sizes used for Alpha ECOFF. */ | |
67 | ||
68 | #define GET_FILEHDR_SYMPTR bfd_h_get_64 | |
69 | #define PUT_FILEHDR_SYMPTR bfd_h_put_64 | |
70 | #define GET_AOUTHDR_TSIZE bfd_h_get_64 | |
71 | #define PUT_AOUTHDR_TSIZE bfd_h_put_64 | |
72 | #define GET_AOUTHDR_DSIZE bfd_h_get_64 | |
73 | #define PUT_AOUTHDR_DSIZE bfd_h_put_64 | |
74 | #define GET_AOUTHDR_BSIZE bfd_h_get_64 | |
75 | #define PUT_AOUTHDR_BSIZE bfd_h_put_64 | |
76 | #define GET_AOUTHDR_ENTRY bfd_h_get_64 | |
77 | #define PUT_AOUTHDR_ENTRY bfd_h_put_64 | |
78 | #define GET_AOUTHDR_TEXT_START bfd_h_get_64 | |
79 | #define PUT_AOUTHDR_TEXT_START bfd_h_put_64 | |
80 | #define GET_AOUTHDR_DATA_START bfd_h_get_64 | |
81 | #define PUT_AOUTHDR_DATA_START bfd_h_put_64 | |
82 | #define GET_SCNHDR_PADDR bfd_h_get_64 | |
83 | #define PUT_SCNHDR_PADDR bfd_h_put_64 | |
84 | #define GET_SCNHDR_VADDR bfd_h_get_64 | |
85 | #define PUT_SCNHDR_VADDR bfd_h_put_64 | |
86 | #define GET_SCNHDR_SIZE bfd_h_get_64 | |
87 | #define PUT_SCNHDR_SIZE bfd_h_put_64 | |
88 | #define GET_SCNHDR_SCNPTR bfd_h_get_64 | |
89 | #define PUT_SCNHDR_SCNPTR bfd_h_put_64 | |
90 | #define GET_SCNHDR_RELPTR bfd_h_get_64 | |
91 | #define PUT_SCNHDR_RELPTR bfd_h_put_64 | |
92 | #define GET_SCNHDR_LNNOPTR bfd_h_get_64 | |
93 | #define PUT_SCNHDR_LNNOPTR bfd_h_put_64 | |
94 | ||
dae31cf5 | 95 | #define ALPHAECOFF |
693f8191 | 96 | |
5f8f6d56 SC |
97 | #define NO_COFF_RELOCS |
98 | #define NO_COFF_SYMBOLS | |
99 | #define NO_COFF_LINENOS | |
dae31cf5 ILT |
100 | #define coff_swap_filehdr_in alpha_ecoff_swap_filehdr_in |
101 | #define coff_swap_filehdr_out alpha_ecoff_swap_filehdr_out | |
102 | #define coff_swap_aouthdr_in alpha_ecoff_swap_aouthdr_in | |
103 | #define coff_swap_aouthdr_out alpha_ecoff_swap_aouthdr_out | |
104 | #define coff_swap_scnhdr_in alpha_ecoff_swap_scnhdr_in | |
105 | #define coff_swap_scnhdr_out alpha_ecoff_swap_scnhdr_out | |
5f8f6d56 | 106 | #include "coffswap.h" |
5f8f6d56 | 107 | |
dae31cf5 ILT |
108 | /* Get the ECOFF swapping routines. */ |
109 | #define ECOFF_64 | |
110 | #include "ecoffswap.h" | |
111 | \f | |
693f8191 | 112 | /* How to process the various reloc types. */ |
dae31cf5 | 113 | |
fa965415 KR |
114 | static bfd_reloc_status_type |
115 | reloc_nil (abfd, reloc, sym, data, sec, output_bfd) | |
116 | bfd *abfd; | |
117 | arelent *reloc; | |
118 | asymbol *sym; | |
119 | PTR data; | |
120 | asection *sec; | |
121 | bfd *output_bfd; | |
122 | { | |
123 | return bfd_reloc_ok; | |
124 | } | |
125 | ||
693f8191 KR |
126 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value |
127 | from smaller values. Start with zero, widen, *then* decrement. */ | |
128 | #define MINUS_ONE (((bfd_vma)0) - 1) | |
129 | ||
130 | static reloc_howto_type alpha_howto_table[] = | |
dae31cf5 | 131 | { |
693f8191 KR |
132 | /* Reloc type 0 is ignored by itself. However, it appears after a |
133 | GPDISP reloc to identify the location where the low order 16 bits | |
134 | of the gp register are loaded. */ | |
135 | HOWTO (ALPHA_R_IGNORE, /* type */ | |
136 | 0, /* rightshift */ | |
137 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
138 | 8, /* bitsize */ | |
139 | true, /* pc_relative */ | |
140 | 0, /* bitpos */ | |
141 | complain_overflow_dont, /* complain_on_overflow */ | |
fa965415 | 142 | reloc_nil, /* special_function */ |
693f8191 | 143 | "IGNORE", /* name */ |
fa965415 | 144 | true, /* partial_inplace */ |
693f8191 KR |
145 | 0, /* src_mask */ |
146 | 0, /* dst_mask */ | |
147 | true), /* pcrel_offset */ | |
148 | ||
149 | /* A 32 bit reference to a symbol. */ | |
150 | HOWTO (ALPHA_R_REFLONG, /* type */ | |
151 | 0, /* rightshift */ | |
152 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
153 | 32, /* bitsize */ | |
154 | false, /* pc_relative */ | |
155 | 0, /* bitpos */ | |
156 | complain_overflow_bitfield, /* complain_on_overflow */ | |
157 | 0, /* special_function */ | |
158 | "REFLONG", /* name */ | |
159 | true, /* partial_inplace */ | |
160 | 0xffffffff, /* src_mask */ | |
161 | 0xffffffff, /* dst_mask */ | |
162 | false), /* pcrel_offset */ | |
163 | ||
164 | /* A 64 bit reference to a symbol. */ | |
165 | HOWTO (ALPHA_R_REFQUAD, /* type */ | |
166 | 0, /* rightshift */ | |
167 | 4, /* size (0 = byte, 1 = short, 2 = long) */ | |
168 | 64, /* bitsize */ | |
169 | false, /* pc_relative */ | |
170 | 0, /* bitpos */ | |
171 | complain_overflow_bitfield, /* complain_on_overflow */ | |
172 | 0, /* special_function */ | |
173 | "REFQUAD", /* name */ | |
174 | true, /* partial_inplace */ | |
175 | MINUS_ONE, /* src_mask */ | |
176 | MINUS_ONE, /* dst_mask */ | |
177 | false), /* pcrel_offset */ | |
178 | ||
179 | /* A 32 bit GP relative offset. This is just like REFLONG except | |
180 | that when the value is used the value of the gp register will be | |
181 | added in. */ | |
182 | HOWTO (ALPHA_R_GPREL32, /* type */ | |
183 | 0, /* rightshift */ | |
184 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
185 | 32, /* bitsize */ | |
186 | false, /* pc_relative */ | |
187 | 0, /* bitpos */ | |
188 | complain_overflow_bitfield, /* complain_on_overflow */ | |
189 | 0, /* special_function */ | |
190 | "GPREL32", /* name */ | |
191 | true, /* partial_inplace */ | |
192 | 0xffffffff, /* src_mask */ | |
193 | 0xffffffff, /* dst_mask */ | |
194 | false), /* pcrel_offset */ | |
195 | ||
196 | /* Used for an instruction that refers to memory off the GP | |
197 | register. The offset is 16 bits of the 32 bit instruction. This | |
198 | reloc always seems to be against the .lita section. */ | |
199 | HOWTO (ALPHA_R_LITERAL, /* type */ | |
200 | 0, /* rightshift */ | |
201 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
202 | 16, /* bitsize */ | |
203 | false, /* pc_relative */ | |
204 | 0, /* bitpos */ | |
205 | complain_overflow_signed, /* complain_on_overflow */ | |
206 | 0, /* special_function */ | |
207 | "LITERAL", /* name */ | |
208 | true, /* partial_inplace */ | |
209 | 0xffff, /* src_mask */ | |
210 | 0xffff, /* dst_mask */ | |
211 | false), /* pcrel_offset */ | |
212 | ||
213 | /* This reloc only appears immediately following a LITERAL reloc. | |
214 | It identifies a use of the literal. It seems that the linker can | |
215 | use this to eliminate a portion of the .lita section. The symbol | |
216 | index is special: 1 means the literal address is in the base | |
217 | register of a memory format instruction; 2 means the literal | |
218 | address is in the byte offset register of a byte-manipulation | |
219 | instruction; 3 means the literal address is in the target | |
220 | register of a jsr instruction. This does not actually do any | |
221 | relocation. */ | |
222 | HOWTO (ALPHA_R_LITUSE, /* type */ | |
223 | 0, /* rightshift */ | |
224 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
225 | 32, /* bitsize */ | |
226 | false, /* pc_relative */ | |
227 | 0, /* bitpos */ | |
228 | complain_overflow_dont, /* complain_on_overflow */ | |
fa965415 | 229 | reloc_nil, /* special_function */ |
693f8191 KR |
230 | "LITUSE", /* name */ |
231 | false, /* partial_inplace */ | |
232 | 0, /* src_mask */ | |
233 | 0, /* dst_mask */ | |
234 | false), /* pcrel_offset */ | |
235 | ||
236 | /* Load the gp register. This is always used for a ldah instruction | |
237 | which loads the upper 16 bits of the gp register. The next reloc | |
238 | will be an IGNORE reloc which identifies the location of the lda | |
239 | instruction which loads the lower 16 bits. The symbol index of | |
240 | the GPDISP instruction appears to actually be the number of bytes | |
241 | between the ldah and lda instructions. This gives two different | |
242 | ways to determine where the lda instruction is; I don't know why | |
243 | both are used. The value to use for the relocation is the | |
244 | difference between the GP value and the current location; the | |
245 | load will always be done against a register holding the current | |
246 | address. */ | |
247 | HOWTO (ALPHA_R_GPDISP, /* type */ | |
248 | 16, /* rightshift */ | |
249 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
250 | 16, /* bitsize */ | |
251 | true, /* pc_relative */ | |
252 | 0, /* bitpos */ | |
253 | complain_overflow_dont, /* complain_on_overflow */ | |
fa965415 | 254 | reloc_nil, /* special_function */ |
693f8191 KR |
255 | "GPDISP", /* name */ |
256 | true, /* partial_inplace */ | |
257 | 0xffff, /* src_mask */ | |
258 | 0xffff, /* dst_mask */ | |
259 | true), /* pcrel_offset */ | |
260 | ||
261 | /* A 21 bit branch. The native assembler generates these for | |
262 | branches within the text segment, and also fills in the PC | |
263 | relative offset in the instruction. */ | |
264 | HOWTO (ALPHA_R_BRADDR, /* type */ | |
265 | 2, /* rightshift */ | |
266 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
267 | 21, /* bitsize */ | |
268 | true, /* pc_relative */ | |
269 | 0, /* bitpos */ | |
270 | complain_overflow_signed, /* complain_on_overflow */ | |
271 | 0, /* special_function */ | |
272 | "BRADDR", /* name */ | |
273 | true, /* partial_inplace */ | |
274 | 0x1fffff, /* src_mask */ | |
275 | 0x1fffff, /* dst_mask */ | |
276 | false), /* pcrel_offset */ | |
277 | ||
278 | /* A hint for a jump to a register. */ | |
279 | HOWTO (ALPHA_R_HINT, /* type */ | |
280 | 2, /* rightshift */ | |
281 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
282 | 14, /* bitsize */ | |
283 | true, /* pc_relative */ | |
284 | 0, /* bitpos */ | |
285 | complain_overflow_dont, /* complain_on_overflow */ | |
286 | 0, /* special_function */ | |
287 | "HINT", /* name */ | |
288 | true, /* partial_inplace */ | |
289 | 0x3fff, /* src_mask */ | |
290 | 0x3fff, /* dst_mask */ | |
291 | false), /* pcrel_offset */ | |
292 | ||
293 | /* 16 bit PC relative offset. */ | |
294 | HOWTO (ALPHA_R_SREL16, /* type */ | |
295 | 0, /* rightshift */ | |
296 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
297 | 16, /* bitsize */ | |
298 | true, /* pc_relative */ | |
299 | 0, /* bitpos */ | |
300 | complain_overflow_signed, /* complain_on_overflow */ | |
301 | 0, /* special_function */ | |
302 | "SREL16", /* name */ | |
303 | true, /* partial_inplace */ | |
304 | 0xffff, /* src_mask */ | |
305 | 0xffff, /* dst_mask */ | |
306 | false), /* pcrel_offset */ | |
307 | ||
308 | /* 32 bit PC relative offset. */ | |
309 | HOWTO (ALPHA_R_SREL32, /* type */ | |
310 | 0, /* rightshift */ | |
311 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
312 | 32, /* bitsize */ | |
313 | true, /* pc_relative */ | |
314 | 0, /* bitpos */ | |
315 | complain_overflow_signed, /* complain_on_overflow */ | |
316 | 0, /* special_function */ | |
317 | "SREL32", /* name */ | |
318 | true, /* partial_inplace */ | |
319 | 0xffffffff, /* src_mask */ | |
320 | 0xffffffff, /* dst_mask */ | |
321 | false), /* pcrel_offset */ | |
322 | ||
323 | /* A 64 bit PC relative offset. */ | |
324 | HOWTO (ALPHA_R_SREL64, /* type */ | |
325 | 0, /* rightshift */ | |
326 | 4, /* size (0 = byte, 1 = short, 2 = long) */ | |
327 | 64, /* bitsize */ | |
328 | true, /* pc_relative */ | |
329 | 0, /* bitpos */ | |
330 | complain_overflow_signed, /* complain_on_overflow */ | |
331 | 0, /* special_function */ | |
332 | "SREL64", /* name */ | |
333 | true, /* partial_inplace */ | |
334 | MINUS_ONE, /* src_mask */ | |
335 | MINUS_ONE, /* dst_mask */ | |
336 | false), /* pcrel_offset */ | |
337 | ||
338 | /* Push a value on the reloc evaluation stack. */ | |
339 | HOWTO (ALPHA_R_OP_PUSH, /* type */ | |
340 | 0, /* rightshift */ | |
341 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
342 | 0, /* bitsize */ | |
343 | false, /* pc_relative */ | |
344 | 0, /* bitpos */ | |
345 | complain_overflow_dont, /* complain_on_overflow */ | |
346 | 0, /* special_function */ | |
347 | "OP_PUSH", /* name */ | |
348 | false, /* partial_inplace */ | |
349 | 0, /* src_mask */ | |
350 | 0, /* dst_mask */ | |
351 | false), /* pcrel_offset */ | |
352 | ||
353 | /* Store the value from the stack at the given address. Store it in | |
354 | a bitfield of size r_size starting at bit position r_offset. */ | |
355 | HOWTO (ALPHA_R_OP_STORE, /* type */ | |
356 | 0, /* rightshift */ | |
357 | 4, /* size (0 = byte, 1 = short, 2 = long) */ | |
358 | 64, /* bitsize */ | |
359 | false, /* pc_relative */ | |
360 | 0, /* bitpos */ | |
361 | complain_overflow_dont, /* complain_on_overflow */ | |
362 | 0, /* special_function */ | |
363 | "OP_STORE", /* name */ | |
364 | false, /* partial_inplace */ | |
365 | 0, /* src_mask */ | |
366 | MINUS_ONE, /* dst_mask */ | |
367 | false), /* pcrel_offset */ | |
368 | ||
369 | /* Subtract the reloc address from the value on the top of the | |
370 | relocation stack. */ | |
371 | HOWTO (ALPHA_R_OP_PSUB, /* type */ | |
372 | 0, /* rightshift */ | |
373 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
374 | 0, /* bitsize */ | |
375 | false, /* pc_relative */ | |
376 | 0, /* bitpos */ | |
377 | complain_overflow_dont, /* complain_on_overflow */ | |
378 | 0, /* special_function */ | |
379 | "OP_PSUB", /* name */ | |
380 | false, /* partial_inplace */ | |
381 | 0, /* src_mask */ | |
382 | 0, /* dst_mask */ | |
383 | false), /* pcrel_offset */ | |
384 | ||
385 | /* Shift the value on the top of the relocation stack right by the | |
386 | given value. */ | |
387 | HOWTO (ALPHA_R_OP_PRSHIFT, /* type */ | |
388 | 0, /* rightshift */ | |
389 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
390 | 0, /* bitsize */ | |
391 | false, /* pc_relative */ | |
392 | 0, /* bitpos */ | |
393 | complain_overflow_dont, /* complain_on_overflow */ | |
394 | 0, /* special_function */ | |
395 | "OP_PRSHIFT", /* name */ | |
396 | false, /* partial_inplace */ | |
397 | 0, /* src_mask */ | |
398 | 0, /* dst_mask */ | |
399 | false), /* pcrel_offset */ | |
400 | ||
401 | /* Adjust the GP value for a new range in the object file. */ | |
402 | HOWTO (ALPHA_R_GPVALUE, /* type */ | |
403 | 0, /* rightshift */ | |
404 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
405 | 0, /* bitsize */ | |
406 | false, /* pc_relative */ | |
407 | 0, /* bitpos */ | |
408 | complain_overflow_dont, /* complain_on_overflow */ | |
409 | 0, /* special_function */ | |
410 | "GPVALUE", /* name */ | |
411 | false, /* partial_inplace */ | |
412 | 0, /* src_mask */ | |
413 | 0, /* dst_mask */ | |
414 | false) /* pcrel_offset */ | |
dae31cf5 ILT |
415 | }; |
416 | \f | |
3f048f7f ILT |
417 | /* Recognize an Alpha ECOFF file. */ |
418 | ||
419 | static bfd_target * | |
420 | alpha_ecoff_object_p (abfd) | |
421 | bfd *abfd; | |
422 | { | |
423 | static bfd_target *ret; | |
424 | ||
425 | ret = coff_object_p (abfd); | |
426 | ||
427 | if (ret != (bfd_target *) NULL) | |
428 | { | |
429 | asection *sec; | |
430 | ||
431 | /* Alpha ECOFF has a .pdata section. The lnnoptr field of the | |
432 | .pdata section is the number of entries it contains. Each | |
433 | entry takes up 8 bytes. The number of entries is required | |
434 | since the section is aligned to a 16 byte boundary. When we | |
435 | link .pdata sections together, we do not want to include the | |
436 | alignment bytes. We handle this on input by faking the size | |
437 | of the .pdata section to remove the unwanted alignment bytes. | |
438 | On output we will set the lnnoptr field and force the | |
439 | alignment. */ | |
440 | sec = bfd_get_section_by_name (abfd, _PDATA); | |
441 | if (sec != (asection *) NULL) | |
442 | { | |
443 | bfd_size_type size; | |
444 | ||
445 | size = sec->line_filepos * 8; | |
446 | BFD_ASSERT (size == bfd_section_size (abfd, sec) | |
447 | || size + 8 == bfd_section_size (abfd, sec)); | |
448 | if (! bfd_set_section_size (abfd, sec, size)) | |
449 | return NULL; | |
450 | } | |
451 | } | |
452 | ||
453 | return ret; | |
454 | } | |
455 | ||
5f8f6d56 SC |
456 | /* See whether the magic number matches. */ |
457 | ||
458 | static boolean | |
dae31cf5 | 459 | alpha_ecoff_bad_format_hook (abfd, filehdr) |
5f8f6d56 SC |
460 | bfd *abfd; |
461 | PTR filehdr; | |
462 | { | |
463 | struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; | |
464 | ||
dae31cf5 | 465 | if (ALPHA_ECOFF_BADMAG (*internal_f)) |
5f8f6d56 SC |
466 | return false; |
467 | ||
468 | return true; | |
469 | } | |
693f8191 KR |
470 | \f |
471 | /* Reloc handling. */ | |
5f8f6d56 | 472 | |
693f8191 | 473 | /* Swap a reloc in. */ |
5f8f6d56 | 474 | |
693f8191 KR |
475 | static void |
476 | alpha_ecoff_swap_reloc_in (abfd, ext_ptr, intern) | |
5f8f6d56 | 477 | bfd *abfd; |
693f8191 KR |
478 | PTR ext_ptr; |
479 | struct internal_reloc *intern; | |
5f8f6d56 | 480 | { |
693f8191 KR |
481 | const RELOC *ext = (RELOC *) ext_ptr; |
482 | ||
483 | intern->r_vaddr = bfd_h_get_64 (abfd, (bfd_byte *) ext->r_vaddr); | |
484 | intern->r_symndx = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_symndx); | |
485 | ||
486 | BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false); | |
487 | ||
488 | intern->r_type = ((ext->r_bits[0] & RELOC_BITS0_TYPE_LITTLE) | |
489 | >> RELOC_BITS0_TYPE_SH_LITTLE); | |
490 | intern->r_extern = (ext->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0; | |
491 | intern->r_offset = ((ext->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE) | |
492 | >> RELOC_BITS1_OFFSET_SH_LITTLE); | |
493 | /* Ignored the reserved bits. */ | |
494 | intern->r_size = ((ext->r_bits[3] & RELOC_BITS3_SIZE_LITTLE) | |
495 | >> RELOC_BITS3_SIZE_SH_LITTLE); | |
496 | ||
497 | if (intern->r_type == ALPHA_R_LITUSE | |
498 | || intern->r_type == ALPHA_R_GPDISP) | |
499 | { | |
500 | /* Handle the LITUSE and GPDISP relocs specially. Its symndx | |
501 | value is not actually a symbol index, but is instead a | |
502 | special code. We put the code in the r_size field, and | |
503 | clobber the symndx. */ | |
504 | if (intern->r_size != 0) | |
505 | abort (); | |
506 | intern->r_size = intern->r_symndx; | |
507 | intern->r_symndx = RELOC_SECTION_NONE; | |
508 | } | |
509 | else if (intern->r_type == ALPHA_R_IGNORE) | |
5f8f6d56 | 510 | { |
693f8191 KR |
511 | /* The IGNORE reloc generally follows a GPDISP reloc, and is |
512 | against the .lita section. The section is irrelevant. */ | |
513 | if (! intern->r_extern && | |
514 | (intern->r_symndx == RELOC_SECTION_NONE | |
515 | || intern->r_symndx == RELOC_SECTION_ABS)) | |
516 | abort (); | |
517 | if (! intern->r_extern && intern->r_symndx == RELOC_SECTION_LITA) | |
518 | intern->r_symndx = RELOC_SECTION_NONE; | |
5f8f6d56 | 519 | } |
693f8191 KR |
520 | } |
521 | ||
522 | /* Swap a reloc out. */ | |
5f8f6d56 | 523 | |
693f8191 KR |
524 | static void |
525 | alpha_ecoff_swap_reloc_out (abfd, intern, dst) | |
526 | bfd *abfd; | |
527 | const struct internal_reloc *intern; | |
528 | PTR dst; | |
529 | { | |
530 | RELOC *ext = (RELOC *) dst; | |
531 | long symndx; | |
532 | unsigned char size; | |
533 | ||
534 | /* Undo the hackery done in swap_reloc_in. */ | |
535 | if (intern->r_type == ALPHA_R_LITUSE | |
536 | || intern->r_type == ALPHA_R_GPDISP) | |
537 | { | |
538 | symndx = intern->r_size; | |
539 | size = 0; | |
540 | } | |
541 | else if (intern->r_type == ALPHA_R_IGNORE | |
542 | && ! intern->r_extern | |
543 | && intern->r_symndx == RELOC_SECTION_NONE) | |
544 | { | |
545 | symndx = RELOC_SECTION_LITA; | |
546 | size = intern->r_size; | |
547 | } | |
548 | else | |
549 | { | |
550 | symndx = intern->r_symndx; | |
551 | size = intern->r_size; | |
552 | } | |
dae31cf5 | 553 | |
693f8191 KR |
554 | BFD_ASSERT (intern->r_extern |
555 | || (intern->r_symndx >= 0 && intern->r_symndx <= 14)); | |
5f8f6d56 | 556 | |
693f8191 KR |
557 | bfd_h_put_64 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr); |
558 | bfd_h_put_32 (abfd, symndx, (bfd_byte *) ext->r_symndx); | |
559 | ||
560 | BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false); | |
561 | ||
562 | ext->r_bits[0] = ((intern->r_type << RELOC_BITS0_TYPE_SH_LITTLE) | |
563 | & RELOC_BITS0_TYPE_LITTLE); | |
564 | ext->r_bits[1] = ((intern->r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0) | |
565 | | ((intern->r_offset << RELOC_BITS1_OFFSET_SH_LITTLE) | |
566 | & RELOC_BITS1_OFFSET_LITTLE)); | |
567 | ext->r_bits[2] = 0; | |
568 | ext->r_bits[3] = ((size << RELOC_BITS3_SIZE_SH_LITTLE) | |
569 | & RELOC_BITS3_SIZE_LITTLE); | |
5f8f6d56 SC |
570 | } |
571 | ||
693f8191 KR |
572 | /* Finish canonicalizing a reloc. Part of this is generic to all |
573 | ECOFF targets, and that part is in ecoff.c. The rest is done in | |
574 | this backend routine. It must fill in the howto field. */ | |
5f8f6d56 | 575 | |
693f8191 KR |
576 | static void |
577 | alpha_adjust_reloc_in (abfd, intern, rptr) | |
5f8f6d56 | 578 | bfd *abfd; |
693f8191 KR |
579 | const struct internal_reloc *intern; |
580 | arelent *rptr; | |
5f8f6d56 | 581 | { |
693f8191 KR |
582 | if (intern->r_type > ALPHA_R_GPVALUE) |
583 | abort (); | |
5f8f6d56 | 584 | |
693f8191 KR |
585 | switch (intern->r_type) |
586 | { | |
587 | case ALPHA_R_BRADDR: | |
588 | case ALPHA_R_SREL16: | |
589 | case ALPHA_R_SREL32: | |
590 | case ALPHA_R_SREL64: | |
591 | /* The PC relative relocs do not seem to use the section VMA as | |
592 | a negative addend. */ | |
593 | rptr->addend = 0; | |
594 | break; | |
5f8f6d56 | 595 | |
693f8191 KR |
596 | case ALPHA_R_GPREL32: |
597 | case ALPHA_R_LITERAL: | |
598 | /* Copy the gp value for this object file into the addend, to | |
599 | ensure that we are not confused by the linker. */ | |
600 | if (! intern->r_extern) | |
601 | rptr->addend += ecoff_data (abfd)->gp; | |
602 | break; | |
5f8f6d56 | 603 | |
693f8191 KR |
604 | case ALPHA_R_LITUSE: |
605 | case ALPHA_R_GPDISP: | |
606 | /* The LITUSE and GPDISP relocs do not use a symbol, or an | |
607 | addend, but they do use a special code. Put this code in the | |
608 | addend field. */ | |
609 | rptr->addend = intern->r_size; | |
610 | break; | |
611 | ||
612 | case ALPHA_R_OP_STORE: | |
613 | /* The STORE reloc needs the size and offset fields. We store | |
614 | them in the addend. */ | |
615 | BFD_ASSERT (intern->r_offset <= 256 && intern->r_size <= 256); | |
616 | rptr->addend = (intern->r_offset << 8) + intern->r_size; | |
617 | break; | |
618 | ||
619 | case ALPHA_R_OP_PUSH: | |
620 | case ALPHA_R_OP_PSUB: | |
621 | case ALPHA_R_OP_PRSHIFT: | |
622 | /* The PUSH, PSUB and PRSHIFT relocs do not actually use an | |
623 | address. I believe that the address supplied is really an | |
624 | addend. */ | |
625 | rptr->addend = intern->r_vaddr; | |
626 | break; | |
627 | ||
628 | case ALPHA_R_GPVALUE: | |
629 | /* Set the addend field to the new GP value. */ | |
630 | rptr->addend = intern->r_symndx + ecoff_data (abfd)->gp; | |
631 | break; | |
632 | ||
633 | case ALPHA_R_IGNORE: | |
634 | /* If the type is ALPHA_R_IGNORE, make sure this is a reference | |
635 | to the absolute section so that the reloc is ignored. For | |
636 | some reason the address of this reloc type is not adjusted by | |
637 | the section vma. We record the gp value for this object file | |
638 | here, for convenience when doing the GPDISP relocation. */ | |
639 | rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; | |
640 | rptr->address = intern->r_vaddr; | |
641 | rptr->addend = ecoff_data (abfd)->gp; | |
642 | break; | |
643 | ||
644 | default: | |
645 | break; | |
5f8f6d56 SC |
646 | } |
647 | ||
693f8191 | 648 | rptr->howto = &alpha_howto_table[intern->r_type]; |
5f8f6d56 | 649 | } |
5f8f6d56 | 650 | |
693f8191 KR |
651 | /* When writing out a reloc we need to pull some values back out of |
652 | the addend field into the reloc. This is roughly the reverse of | |
653 | alpha_adjust_reloc_in, except that there are several changes we do | |
654 | not need to undo. */ | |
5f8f6d56 SC |
655 | |
656 | static void | |
693f8191 | 657 | alpha_adjust_reloc_out (abfd, rel, intern) |
5f8f6d56 | 658 | bfd *abfd; |
693f8191 | 659 | const arelent *rel; |
dae31cf5 | 660 | struct internal_reloc *intern; |
5f8f6d56 | 661 | { |
693f8191 KR |
662 | switch (intern->r_type) |
663 | { | |
664 | case ALPHA_R_LITUSE: | |
665 | case ALPHA_R_GPDISP: | |
666 | intern->r_size = rel->addend; | |
667 | break; | |
668 | ||
669 | case ALPHA_R_OP_STORE: | |
670 | intern->r_size = rel->addend & 0xff; | |
671 | intern->r_offset = (rel->addend >> 8) & 0xff; | |
672 | break; | |
673 | ||
674 | case ALPHA_R_OP_PUSH: | |
675 | case ALPHA_R_OP_PSUB: | |
676 | case ALPHA_R_OP_PRSHIFT: | |
677 | intern->r_vaddr = rel->addend; | |
678 | break; | |
679 | ||
680 | case ALPHA_R_IGNORE: | |
681 | intern->r_vaddr = rel->address; | |
682 | if (intern->r_symndx == RELOC_SECTION_ABS) | |
683 | intern->r_symndx = RELOC_SECTION_NONE; | |
684 | break; | |
685 | ||
686 | default: | |
687 | break; | |
688 | } | |
689 | } | |
690 | ||
691 | /* The size of the stack for the relocation evaluator. */ | |
692 | #define RELOC_STACKSIZE (10) | |
693 | ||
694 | /* Alpha ECOFF relocs have a built in expression evaluator as well as | |
695 | other interdependencies. Rather than use a bunch of special | |
696 | functions and global variables, we use a single routine to do all | |
697 | the relocation for a section. I haven't yet worked out how the | |
698 | assembler is going to handle this. */ | |
699 | ||
700 | static bfd_byte * | |
701 | alpha_ecoff_get_relocated_section_contents (abfd, link_info, link_order, | |
702 | data, relocateable, symbols) | |
703 | bfd *abfd; | |
704 | struct bfd_link_info *link_info; | |
705 | struct bfd_link_order *link_order; | |
706 | bfd_byte *data; | |
707 | boolean relocateable; | |
708 | asymbol **symbols; | |
709 | { | |
710 | bfd *input_bfd = link_order->u.indirect.section->owner; | |
711 | asection *input_section = link_order->u.indirect.section; | |
712 | size_t reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section); | |
713 | arelent **reloc_vector = (arelent **) alloca (reloc_size); | |
714 | bfd *output_bfd = relocateable ? abfd : (bfd *) NULL; | |
715 | bfd_vma gp; | |
716 | boolean gp_undefined; | |
717 | bfd_vma stack[RELOC_STACKSIZE]; | |
718 | int tos = 0; | |
719 | ||
720 | if (! bfd_get_section_contents (input_bfd, input_section, data, | |
721 | (file_ptr) 0, input_section->_raw_size)) | |
722 | return NULL; | |
5f8f6d56 | 723 | |
693f8191 KR |
724 | /* The section size is not going to change. */ |
725 | input_section->_cooked_size = input_section->_raw_size; | |
726 | input_section->reloc_done = true; | |
727 | ||
728 | if (bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector, | |
729 | symbols) | |
730 | == 0) | |
731 | return data; | |
732 | ||
733 | /* Get the GP value for the output BFD. */ | |
734 | gp_undefined = false; | |
735 | if (ecoff_data (abfd)->gp == 0) | |
5f8f6d56 | 736 | { |
693f8191 KR |
737 | if (relocateable != false) |
738 | { | |
739 | asection *sec; | |
740 | bfd_vma lo; | |
741 | ||
742 | /* Make up a value. */ | |
743 | lo = (bfd_vma) -1; | |
744 | for (sec = abfd->sections; sec != NULL; sec = sec->next) | |
745 | { | |
746 | if (sec->vma < lo | |
747 | && (strcmp (sec->name, ".sbss") == 0 | |
748 | || strcmp (sec->name, ".sdata") == 0 | |
749 | || strcmp (sec->name, ".lit4") == 0 | |
750 | || strcmp (sec->name, ".lit8") == 0 | |
751 | || strcmp (sec->name, ".lita") == 0)) | |
752 | lo = sec->vma; | |
753 | } | |
754 | ecoff_data (abfd)->gp = lo + 0x8000; | |
755 | } | |
756 | else | |
757 | { | |
758 | struct bfd_link_hash_entry *h; | |
759 | ||
760 | h = bfd_link_hash_lookup (link_info->hash, "_gp", false, false, | |
761 | true); | |
762 | if (h == (struct bfd_link_hash_entry *) NULL | |
763 | || h->type != bfd_link_hash_defined) | |
764 | gp_undefined = true; | |
765 | else | |
766 | ecoff_data (abfd)->gp = (h->u.def.value | |
767 | + h->u.def.section->output_section->vma | |
768 | + h->u.def.section->output_offset); | |
769 | } | |
5f8f6d56 | 770 | } |
693f8191 KR |
771 | gp = ecoff_data (abfd)->gp; |
772 | ||
773 | for (; *reloc_vector != (arelent *) NULL; reloc_vector++) | |
5f8f6d56 | 774 | { |
693f8191 KR |
775 | arelent *rel; |
776 | bfd_reloc_status_type r; | |
777 | char *err; | |
778 | ||
779 | rel = *reloc_vector; | |
780 | r = bfd_reloc_ok; | |
781 | switch (rel->howto->type) | |
782 | { | |
783 | case ALPHA_R_IGNORE: | |
784 | rel->address += input_section->output_offset; | |
785 | break; | |
786 | ||
787 | case ALPHA_R_REFLONG: | |
788 | case ALPHA_R_REFQUAD: | |
789 | case ALPHA_R_BRADDR: | |
790 | case ALPHA_R_HINT: | |
791 | case ALPHA_R_SREL16: | |
792 | case ALPHA_R_SREL32: | |
793 | case ALPHA_R_SREL64: | |
794 | if (relocateable | |
795 | && ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0) | |
796 | { | |
797 | rel->address += input_section->output_offset; | |
798 | break; | |
799 | } | |
800 | r = bfd_perform_relocation (input_bfd, rel, data, input_section, | |
801 | output_bfd, &err); | |
802 | break; | |
803 | ||
804 | case ALPHA_R_GPREL32: | |
805 | /* This relocation is used in a switch table. It is a 32 | |
806 | bit offset from the current GP value. We must adjust it | |
807 | by the different between the original GP value and the | |
808 | current GP value. The original GP value is stored in the | |
809 | addend. We adjust the addend and let | |
810 | bfd_perform_relocation finish the job. */ | |
811 | rel->addend -= gp; | |
812 | r = bfd_perform_relocation (input_bfd, rel, data, input_section, | |
813 | output_bfd, &err); | |
814 | if (r == bfd_reloc_ok && gp_undefined) | |
815 | { | |
816 | r = bfd_reloc_dangerous; | |
817 | err = (char *) "GP relative relocation used when GP not defined"; | |
818 | } | |
819 | break; | |
820 | ||
821 | case ALPHA_R_LITERAL: | |
822 | /* This is a reference to a literal value, generally | |
823 | (always?) in the .lita section. This is a 16 bit GP | |
824 | relative relocation. Sometimes the subsequent reloc is a | |
825 | LITUSE reloc, which indicates how this reloc is used. | |
826 | This sometimes permits rewriting the two instructions | |
827 | referred to by the LITERAL and the LITUSE into different | |
828 | instructions which do not refer to .lita. This can save | |
829 | a memory reference, and permits removing a value from | |
830 | .lita thus saving GP relative space. | |
831 | ||
832 | We do not these optimizations. To do them we would need | |
833 | to arrange to link the .lita section first, so that by | |
834 | the time we got here we would know the final values to | |
835 | use. This would not be particularly difficult, but it is | |
836 | not currently implemented. */ | |
837 | ||
838 | { | |
839 | unsigned long insn; | |
840 | ||
841 | /* I believe that the LITERAL reloc will only apply to a | |
fa965415 | 842 | ldq or ldl instruction, so check my assumption. */ |
693f8191 | 843 | insn = bfd_get_32 (input_bfd, data + rel->address); |
fa965415 KR |
844 | BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29 |
845 | || ((insn >> 26) & 0x3f) == 0x28); | |
693f8191 KR |
846 | |
847 | rel->addend -= gp; | |
848 | r = bfd_perform_relocation (input_bfd, rel, data, input_section, | |
849 | output_bfd, &err); | |
850 | if (r == bfd_reloc_ok && gp_undefined) | |
851 | { | |
852 | r = bfd_reloc_dangerous; | |
853 | err = | |
854 | (char *) "GP relative relocation used when GP not defined"; | |
855 | } | |
856 | } | |
857 | break; | |
858 | ||
859 | case ALPHA_R_LITUSE: | |
860 | /* See ALPHA_R_LITERAL above for the uses of this reloc. It | |
861 | does not cause anything to happen, itself. */ | |
862 | rel->address += input_section->output_offset; | |
863 | break; | |
864 | ||
865 | case ALPHA_R_GPDISP: | |
866 | /* This marks the ldah of an ldah/lda pair which loads the | |
867 | gp register with the difference of the gp value and the | |
868 | current location. The second of the pair is r_size bytes | |
869 | ahead, and is marked with an ALPHA_R_IGNORE reloc. */ | |
870 | { | |
871 | unsigned long insn1, insn2; | |
872 | bfd_vma addend; | |
873 | ||
874 | BFD_ASSERT (reloc_vector[1] != NULL | |
875 | && reloc_vector[1]->howto->type == ALPHA_R_IGNORE | |
876 | && (rel->address + rel->addend | |
877 | == reloc_vector[1]->address)); | |
878 | ||
879 | /* Get the two instructions. */ | |
880 | insn1 = bfd_get_32 (input_bfd, data + rel->address); | |
881 | insn2 = bfd_get_32 (input_bfd, data + rel->address + rel->addend); | |
882 | ||
883 | BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */ | |
884 | BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */ | |
885 | ||
886 | /* Get the existing addend. We must account for the sign | |
887 | extension done by lda and ldah. */ | |
888 | addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff); | |
889 | if (insn1 & 0x8000) | |
890 | { | |
891 | addend -= 0x80000000; | |
892 | addend -= 0x80000000; | |
893 | } | |
894 | if (insn2 & 0x8000) | |
895 | addend -= 0x10000; | |
896 | ||
897 | /* The existing addend includes the different between the | |
898 | gp of the input BFD and the address in the input BFD. | |
899 | Subtract this out. */ | |
900 | addend -= (reloc_vector[1]->addend | |
901 | - (input_section->vma + rel->address)); | |
902 | ||
903 | /* Now add in the final gp value, and subtract out the | |
904 | final address. */ | |
905 | addend += (gp | |
906 | - (input_section->output_section->vma | |
907 | + input_section->output_offset | |
908 | + rel->address)); | |
909 | ||
910 | /* Change the instructions, accounting for the sign | |
911 | extension, and write them out. */ | |
912 | if (addend & 0x8000) | |
913 | addend += 0x10000; | |
914 | insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff); | |
915 | insn2 = (insn2 & 0xffff0000) | (addend & 0xffff); | |
916 | ||
917 | bfd_put_32 (input_bfd, (bfd_vma) insn1, data + rel->address); | |
918 | bfd_put_32 (input_bfd, (bfd_vma) insn2, | |
919 | data + rel->address + rel->addend); | |
920 | ||
921 | rel->address += input_section->output_offset; | |
922 | } | |
923 | break; | |
924 | ||
925 | case ALPHA_R_OP_PUSH: | |
926 | /* Push a value on the reloc evaluation stack. */ | |
927 | { | |
928 | asymbol *symbol; | |
929 | bfd_vma relocation; | |
930 | ||
931 | if (relocateable) | |
932 | { | |
933 | rel->address += input_section->output_offset; | |
934 | break; | |
935 | } | |
936 | ||
937 | /* Figure out the relocation of this symbol. */ | |
938 | symbol = *rel->sym_ptr_ptr; | |
939 | ||
940 | if (symbol->section == &bfd_und_section) | |
941 | r = bfd_reloc_undefined; | |
942 | ||
943 | if (bfd_is_com_section (symbol->section)) | |
944 | relocation = 0; | |
945 | else | |
946 | relocation = symbol->value; | |
947 | relocation += symbol->section->output_section->vma; | |
948 | relocation += symbol->section->output_offset; | |
949 | relocation += rel->addend; | |
950 | ||
951 | if (tos >= RELOC_STACKSIZE) | |
952 | abort (); | |
953 | ||
954 | stack[tos++] = relocation; | |
955 | } | |
956 | break; | |
957 | ||
958 | case ALPHA_R_OP_STORE: | |
959 | /* Store a value from the reloc stack into a bitfield. */ | |
960 | { | |
961 | bfd_vma val; | |
962 | int offset, size; | |
963 | ||
964 | if (relocateable) | |
965 | { | |
966 | rel->address += input_section->output_offset; | |
967 | break; | |
968 | } | |
969 | ||
970 | if (tos == 0) | |
971 | abort (); | |
972 | ||
973 | /* The offset and size for this reloc are encoded into the | |
974 | addend field by alpha_adjust_reloc_in. */ | |
975 | offset = (rel->addend >> 8) & 0xff; | |
976 | size = rel->addend & 0xff; | |
977 | ||
978 | val = bfd_get_64 (abfd, data + rel->address); | |
979 | val &=~ (((1 << size) - 1) << offset); | |
980 | val |= (stack[--tos] & ((1 << size) - 1)) << offset; | |
981 | bfd_put_64 (abfd, val, data + rel->address); | |
982 | } | |
983 | break; | |
984 | ||
985 | case ALPHA_R_OP_PSUB: | |
986 | /* Subtract a value from the top of the stack. */ | |
987 | { | |
988 | asymbol *symbol; | |
989 | bfd_vma relocation; | |
990 | ||
991 | if (relocateable) | |
992 | { | |
993 | rel->address += input_section->output_offset; | |
994 | break; | |
995 | } | |
996 | ||
997 | /* Figure out the relocation of this symbol. */ | |
998 | symbol = *rel->sym_ptr_ptr; | |
999 | ||
1000 | if (symbol->section == &bfd_und_section) | |
1001 | r = bfd_reloc_undefined; | |
1002 | ||
1003 | if (bfd_is_com_section (symbol->section)) | |
1004 | relocation = 0; | |
1005 | else | |
1006 | relocation = symbol->value; | |
1007 | relocation += symbol->section->output_section->vma; | |
1008 | relocation += symbol->section->output_offset; | |
1009 | relocation += rel->addend; | |
1010 | ||
1011 | if (tos == 0) | |
1012 | abort (); | |
1013 | ||
1014 | stack[tos - 1] -= relocation; | |
1015 | } | |
1016 | break; | |
1017 | ||
1018 | case ALPHA_R_OP_PRSHIFT: | |
1019 | /* Shift the value on the top of the stack. */ | |
1020 | { | |
1021 | asymbol *symbol; | |
1022 | bfd_vma relocation; | |
1023 | ||
1024 | if (relocateable) | |
1025 | { | |
1026 | rel->address += input_section->output_offset; | |
1027 | break; | |
1028 | } | |
1029 | ||
1030 | /* Figure out the relocation of this symbol. */ | |
1031 | symbol = *rel->sym_ptr_ptr; | |
1032 | ||
1033 | if (symbol->section == &bfd_und_section) | |
1034 | r = bfd_reloc_undefined; | |
1035 | ||
1036 | if (bfd_is_com_section (symbol->section)) | |
1037 | relocation = 0; | |
1038 | else | |
1039 | relocation = symbol->value; | |
1040 | relocation += symbol->section->output_section->vma; | |
1041 | relocation += symbol->section->output_offset; | |
1042 | relocation += rel->addend; | |
1043 | ||
1044 | if (tos == 0) | |
1045 | abort (); | |
1046 | ||
1047 | stack[tos - 1] >>= relocation; | |
1048 | } | |
1049 | break; | |
1050 | ||
1051 | case ALPHA_R_GPVALUE: | |
1052 | /* I really don't know if this does the right thing. */ | |
1053 | gp = rel->addend; | |
1054 | gp_undefined = false; | |
1055 | break; | |
1056 | ||
1057 | default: | |
1058 | abort (); | |
1059 | } | |
1060 | ||
1061 | if (relocateable) | |
1062 | { | |
1063 | asection *os = input_section->output_section; | |
1064 | ||
1065 | /* A partial link, so keep the relocs. */ | |
1066 | os->orelocation[os->reloc_count] = rel; | |
1067 | os->reloc_count++; | |
1068 | } | |
1069 | ||
1070 | if (r != bfd_reloc_ok) | |
1071 | { | |
1072 | switch (r) | |
1073 | { | |
1074 | case bfd_reloc_undefined: | |
1075 | if (! ((*link_info->callbacks->undefined_symbol) | |
1076 | (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr), | |
1077 | input_bfd, input_section, rel->address))) | |
1078 | return NULL; | |
1079 | break; | |
1080 | case bfd_reloc_dangerous: | |
1081 | if (! ((*link_info->callbacks->reloc_dangerous) | |
1082 | (link_info, err, input_bfd, input_section, | |
1083 | rel->address))) | |
1084 | return NULL; | |
1085 | break; | |
1086 | case bfd_reloc_overflow: | |
1087 | if (! ((*link_info->callbacks->reloc_overflow) | |
4991ebb9 ILT |
1088 | (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr), |
1089 | rel->howto->name, rel->addend, input_bfd, | |
1090 | input_section, rel->address))) | |
693f8191 KR |
1091 | return NULL; |
1092 | break; | |
1093 | case bfd_reloc_outofrange: | |
1094 | default: | |
1095 | abort (); | |
1096 | break; | |
1097 | } | |
1098 | } | |
5f8f6d56 | 1099 | } |
693f8191 KR |
1100 | |
1101 | if (tos != 0) | |
1102 | abort (); | |
1103 | ||
1104 | return data; | |
5f8f6d56 SC |
1105 | } |
1106 | ||
693f8191 | 1107 | /* Get the howto structure for a generic reloc type. */ |
5f8f6d56 | 1108 | |
693f8191 KR |
1109 | static CONST struct reloc_howto_struct * |
1110 | alpha_bfd_reloc_type_lookup (abfd, code) | |
5f8f6d56 | 1111 | bfd *abfd; |
693f8191 | 1112 | bfd_reloc_code_real_type code; |
5f8f6d56 | 1113 | { |
693f8191 | 1114 | int alpha_type; |
5f8f6d56 | 1115 | |
693f8191 | 1116 | switch (code) |
5f8f6d56 | 1117 | { |
693f8191 KR |
1118 | case BFD_RELOC_32: |
1119 | alpha_type = ALPHA_R_REFLONG; | |
1120 | break; | |
1121 | case BFD_RELOC_64: | |
1122 | alpha_type = ALPHA_R_REFQUAD; | |
1123 | break; | |
1124 | case BFD_RELOC_GPREL32: | |
1125 | alpha_type = ALPHA_R_GPREL32; | |
1126 | break; | |
1127 | case BFD_RELOC_ALPHA_LITERAL: | |
1128 | alpha_type = ALPHA_R_LITERAL; | |
1129 | break; | |
1130 | case BFD_RELOC_ALPHA_LITUSE: | |
1131 | alpha_type = ALPHA_R_LITUSE; | |
1132 | break; | |
1133 | case BFD_RELOC_ALPHA_GPDISP_HI16: | |
1134 | alpha_type = ALPHA_R_GPDISP; | |
1135 | break; | |
1136 | case BFD_RELOC_ALPHA_GPDISP_LO16: | |
1137 | alpha_type = ALPHA_R_IGNORE; | |
1138 | break; | |
1139 | case BFD_RELOC_23_PCREL_S2: | |
1140 | alpha_type = ALPHA_R_BRADDR; | |
1141 | break; | |
1142 | case BFD_RELOC_ALPHA_HINT: | |
1143 | alpha_type = ALPHA_R_HINT; | |
1144 | break; | |
1145 | case BFD_RELOC_16_PCREL: | |
1146 | alpha_type = ALPHA_R_SREL16; | |
1147 | break; | |
1148 | case BFD_RELOC_32_PCREL: | |
1149 | alpha_type = ALPHA_R_SREL32; | |
1150 | break; | |
1151 | case BFD_RELOC_64_PCREL: | |
1152 | alpha_type = ALPHA_R_SREL64; | |
1153 | break; | |
1154 | #if 0 | |
1155 | case ???: | |
1156 | alpha_type = ALPHA_R_OP_PUSH; | |
1157 | break; | |
1158 | case ???: | |
1159 | alpha_type = ALPHA_R_OP_STORE; | |
1160 | break; | |
1161 | case ???: | |
1162 | alpha_type = ALPHA_R_OP_PSUB; | |
1163 | break; | |
1164 | case ???: | |
1165 | alpha_type = ALPHA_R_OP_PRSHIFT; | |
1166 | break; | |
1167 | case ???: | |
1168 | alpha_type = ALPHA_R_GPVALUE; | |
1169 | break; | |
1170 | #endif | |
1171 | default: | |
1172 | return (CONST struct reloc_howto_struct *) NULL; | |
1173 | } | |
1174 | ||
1175 | return &alpha_howto_table[alpha_type]; | |
1176 | } | |
1177 | \f | |
1178 | /* A helper routine for alpha_relocate_section which converts an | |
1179 | external reloc when generating relocateable output. Returns the | |
1180 | relocation amount. */ | |
1181 | ||
1182 | static bfd_vma | |
1183 | alpha_convert_external_reloc (output_bfd, info, input_bfd, ext_rel, h) | |
1184 | bfd *output_bfd; | |
1185 | struct bfd_link_info *info; | |
1186 | bfd *input_bfd; | |
1187 | struct external_reloc *ext_rel; | |
1188 | struct ecoff_link_hash_entry *h; | |
1189 | { | |
1190 | unsigned long r_symndx; | |
1191 | bfd_vma relocation; | |
1192 | ||
1193 | BFD_ASSERT (info->relocateable); | |
1194 | ||
1195 | if (h->root.type == bfd_link_hash_defined) | |
1196 | { | |
1197 | asection *hsec; | |
1198 | const char *name; | |
1199 | ||
1200 | /* This symbol is defined in the output. Convert the reloc from | |
1201 | being against the symbol to being against the section. */ | |
1202 | ||
1203 | /* Clear the r_extern bit. */ | |
1204 | ext_rel->r_bits[1] &=~ RELOC_BITS1_EXTERN_LITTLE; | |
1205 | ||
1206 | /* Compute a new r_symndx value. */ | |
1207 | hsec = h->root.u.def.section; | |
1208 | name = bfd_get_section_name (output_bfd, hsec->output_section); | |
1209 | ||
1210 | r_symndx = -1; | |
1211 | switch (name[1]) | |
1212 | { | |
1213 | case 'A': | |
1214 | if (strcmp (name, "*ABS*") == 0) | |
1215 | r_symndx = RELOC_SECTION_ABS; | |
1216 | break; | |
1217 | case 'b': | |
1218 | if (strcmp (name, ".bss") == 0) | |
1219 | r_symndx = RELOC_SECTION_BSS; | |
1220 | break; | |
1221 | case 'd': | |
1222 | if (strcmp (name, ".data") == 0) | |
1223 | r_symndx = RELOC_SECTION_DATA; | |
1224 | break; | |
1225 | case 'f': | |
1226 | if (strcmp (name, ".fini") == 0) | |
1227 | r_symndx = RELOC_SECTION_FINI; | |
1228 | break; | |
1229 | case 'i': | |
1230 | if (strcmp (name, ".init") == 0) | |
1231 | r_symndx = RELOC_SECTION_INIT; | |
1232 | break; | |
1233 | case 'l': | |
1234 | if (strcmp (name, ".lita") == 0) | |
1235 | r_symndx = RELOC_SECTION_LITA; | |
1236 | else if (strcmp (name, ".lit8") == 0) | |
1237 | r_symndx = RELOC_SECTION_LIT8; | |
1238 | else if (strcmp (name, ".lit4") == 0) | |
1239 | r_symndx = RELOC_SECTION_LIT4; | |
1240 | break; | |
1241 | case 'p': | |
1242 | if (strcmp (name, ".pdata") == 0) | |
1243 | r_symndx = RELOC_SECTION_PDATA; | |
1244 | break; | |
1245 | case 'r': | |
1246 | if (strcmp (name, ".rdata") == 0) | |
1247 | r_symndx = RELOC_SECTION_RDATA; | |
1248 | break; | |
1249 | case 's': | |
1250 | if (strcmp (name, ".sdata") == 0) | |
1251 | r_symndx = RELOC_SECTION_SDATA; | |
1252 | else if (strcmp (name, ".sbss") == 0) | |
1253 | r_symndx = RELOC_SECTION_SBSS; | |
1254 | break; | |
1255 | case 't': | |
1256 | if (strcmp (name, ".text") == 0) | |
1257 | r_symndx = RELOC_SECTION_TEXT; | |
1258 | break; | |
1259 | case 'x': | |
1260 | if (strcmp (name, ".xdata") == 0) | |
1261 | r_symndx = RELOC_SECTION_XDATA; | |
1262 | break; | |
1263 | } | |
1264 | ||
1265 | if (r_symndx == -1) | |
1266 | abort (); | |
1267 | ||
1268 | /* Add the section VMA and the symbol value. */ | |
1269 | relocation = (h->root.u.def.value | |
1270 | + hsec->output_section->vma | |
1271 | + hsec->output_offset); | |
5f8f6d56 | 1272 | } |
5f8f6d56 SC |
1273 | else |
1274 | { | |
693f8191 KR |
1275 | /* Change the symndx value to the right one for |
1276 | the output BFD. */ | |
1277 | r_symndx = h->indx; | |
1278 | if (r_symndx == -1) | |
1279 | { | |
1280 | /* Caller must give an error. */ | |
1281 | r_symndx = 0; | |
1282 | } | |
1283 | relocation = 0; | |
5f8f6d56 | 1284 | } |
693f8191 KR |
1285 | |
1286 | /* Write out the new r_symndx value. */ | |
1287 | bfd_h_put_32 (input_bfd, (bfd_vma) r_symndx, | |
1288 | (bfd_byte *) ext_rel->r_symndx); | |
1289 | ||
1290 | return relocation; | |
1291 | } | |
1292 | ||
1293 | /* Relocate a section while linking an Alpha ECOFF file. This is | |
1294 | quite similar to get_relocated_section_contents. Perhaps they | |
1295 | could be combined somehow. */ | |
1296 | ||
1297 | static boolean | |
1298 | alpha_relocate_section (output_bfd, info, input_bfd, input_section, | |
1299 | contents, external_relocs) | |
1300 | bfd *output_bfd; | |
1301 | struct bfd_link_info *info; | |
1302 | bfd *input_bfd; | |
1303 | asection *input_section; | |
1304 | bfd_byte *contents; | |
1305 | PTR external_relocs; | |
1306 | { | |
1307 | asection **symndx_to_section; | |
1308 | struct ecoff_link_hash_entry **sym_hashes; | |
1309 | bfd_vma gp; | |
1310 | boolean gp_undefined; | |
1311 | bfd_vma stack[RELOC_STACKSIZE]; | |
1312 | int tos = 0; | |
1313 | struct external_reloc *ext_rel; | |
1314 | struct external_reloc *ext_rel_end; | |
1315 | ||
1316 | /* We keep a table mapping the symndx found in an internal reloc to | |
1317 | the appropriate section. This is faster than looking up the | |
1318 | section by name each time. */ | |
1319 | symndx_to_section = ecoff_data (input_bfd)->symndx_to_section; | |
1320 | if (symndx_to_section == (asection **) NULL) | |
1321 | { | |
1322 | symndx_to_section = ((asection **) | |
1323 | bfd_alloc (input_bfd, | |
1324 | (NUM_RELOC_SECTIONS | |
1325 | * sizeof (asection *)))); | |
9783e04a DM |
1326 | if (!symndx_to_section) |
1327 | { | |
1328 | bfd_error = no_memory; | |
1329 | return false; | |
1330 | } | |
693f8191 KR |
1331 | |
1332 | symndx_to_section[RELOC_SECTION_NONE] = NULL; | |
1333 | symndx_to_section[RELOC_SECTION_TEXT] = | |
1334 | bfd_get_section_by_name (input_bfd, ".text"); | |
1335 | symndx_to_section[RELOC_SECTION_RDATA] = | |
1336 | bfd_get_section_by_name (input_bfd, ".rdata"); | |
1337 | symndx_to_section[RELOC_SECTION_DATA] = | |
1338 | bfd_get_section_by_name (input_bfd, ".data"); | |
1339 | symndx_to_section[RELOC_SECTION_SDATA] = | |
1340 | bfd_get_section_by_name (input_bfd, ".sdata"); | |
1341 | symndx_to_section[RELOC_SECTION_SBSS] = | |
1342 | bfd_get_section_by_name (input_bfd, ".sbss"); | |
1343 | symndx_to_section[RELOC_SECTION_BSS] = | |
1344 | bfd_get_section_by_name (input_bfd, ".bss"); | |
1345 | symndx_to_section[RELOC_SECTION_INIT] = | |
1346 | bfd_get_section_by_name (input_bfd, ".init"); | |
1347 | symndx_to_section[RELOC_SECTION_LIT8] = | |
1348 | bfd_get_section_by_name (input_bfd, ".lit8"); | |
1349 | symndx_to_section[RELOC_SECTION_LIT4] = | |
1350 | bfd_get_section_by_name (input_bfd, ".lit4"); | |
1351 | symndx_to_section[RELOC_SECTION_XDATA] = | |
1352 | bfd_get_section_by_name (input_bfd, ".xdata"); | |
1353 | symndx_to_section[RELOC_SECTION_PDATA] = | |
1354 | bfd_get_section_by_name (input_bfd, ".pdata"); | |
1355 | symndx_to_section[RELOC_SECTION_FINI] = | |
1356 | bfd_get_section_by_name (input_bfd, ".fini"); | |
1357 | symndx_to_section[RELOC_SECTION_LITA] = | |
1358 | bfd_get_section_by_name (input_bfd, ".lita"); | |
1359 | symndx_to_section[RELOC_SECTION_ABS] = &bfd_abs_section; | |
1360 | ||
1361 | ecoff_data (input_bfd)->symndx_to_section = symndx_to_section; | |
1362 | } | |
1363 | ||
1364 | sym_hashes = ecoff_data (input_bfd)->sym_hashes; | |
1365 | ||
1366 | gp = ecoff_data (output_bfd)->gp; | |
1367 | if (gp == 0) | |
1368 | gp_undefined = true; | |
1369 | else | |
1370 | gp_undefined = false; | |
1371 | ||
1372 | BFD_ASSERT (output_bfd->xvec->header_byteorder_big_p == false); | |
1373 | BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p == false); | |
1374 | ||
1375 | ext_rel = (struct external_reloc *) external_relocs; | |
1376 | ext_rel_end = ext_rel + input_section->reloc_count; | |
1377 | for (; ext_rel < ext_rel_end; ext_rel++) | |
1378 | { | |
1379 | bfd_vma r_vaddr; | |
1380 | unsigned long r_symndx; | |
1381 | int r_type; | |
1382 | int r_extern; | |
1383 | int r_offset; | |
1384 | int r_size; | |
1385 | boolean relocatep; | |
1386 | boolean adjust_addrp; | |
1387 | boolean gp_usedp; | |
1388 | bfd_vma addend; | |
1389 | ||
1390 | r_vaddr = bfd_h_get_64 (input_bfd, (bfd_byte *) ext_rel->r_vaddr); | |
1391 | r_symndx = bfd_h_get_32 (input_bfd, (bfd_byte *) ext_rel->r_symndx); | |
1392 | ||
1393 | r_type = ((ext_rel->r_bits[0] & RELOC_BITS0_TYPE_LITTLE) | |
1394 | >> RELOC_BITS0_TYPE_SH_LITTLE); | |
1395 | r_extern = (ext_rel->r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0; | |
1396 | r_offset = ((ext_rel->r_bits[1] & RELOC_BITS1_OFFSET_LITTLE) | |
1397 | >> RELOC_BITS1_OFFSET_SH_LITTLE); | |
1398 | /* Ignored the reserved bits. */ | |
1399 | r_size = ((ext_rel->r_bits[3] & RELOC_BITS3_SIZE_LITTLE) | |
1400 | >> RELOC_BITS3_SIZE_SH_LITTLE); | |
1401 | ||
1402 | relocatep = false; | |
1403 | adjust_addrp = true; | |
1404 | gp_usedp = false; | |
1405 | addend = 0; | |
1406 | ||
1407 | switch (r_type) | |
1408 | { | |
1409 | default: | |
1410 | abort (); | |
1411 | ||
1412 | case ALPHA_R_IGNORE: | |
1413 | /* This reloc appears after a GPDISP reloc. It marks the | |
1414 | position of the second instruction to be altered by the | |
1415 | GPDISP reloc, but is not otherwise used for anything. | |
1416 | For some reason, the address of the relocation does not | |
1417 | appear to include the section VMA, unlike the other | |
1418 | relocation types. */ | |
1419 | if (info->relocateable) | |
1420 | bfd_h_put_64 (input_bfd, | |
1421 | input_section->output_offset + r_vaddr, | |
1422 | (bfd_byte *) ext_rel->r_vaddr); | |
1423 | adjust_addrp = false; | |
1424 | break; | |
1425 | ||
1426 | case ALPHA_R_REFLONG: | |
1427 | case ALPHA_R_REFQUAD: | |
1428 | case ALPHA_R_BRADDR: | |
1429 | case ALPHA_R_HINT: | |
1430 | case ALPHA_R_SREL16: | |
1431 | case ALPHA_R_SREL32: | |
1432 | case ALPHA_R_SREL64: | |
1433 | relocatep = true; | |
1434 | break; | |
1435 | ||
1436 | case ALPHA_R_GPREL32: | |
1437 | /* This relocation is used in a switch table. It is a 32 | |
1438 | bit offset from the current GP value. We must adjust it | |
1439 | by the different between the original GP value and the | |
1440 | current GP value. */ | |
1441 | relocatep = true; | |
1442 | addend = ecoff_data (input_bfd)->gp - gp; | |
1443 | gp_usedp = true; | |
1444 | break; | |
1445 | ||
1446 | case ALPHA_R_LITERAL: | |
1447 | /* This is a reference to a literal value, generally | |
1448 | (always?) in the .lita section. This is a 16 bit GP | |
1449 | relative relocation. Sometimes the subsequent reloc is a | |
1450 | LITUSE reloc, which indicates how this reloc is used. | |
1451 | This sometimes permits rewriting the two instructions | |
1452 | referred to by the LITERAL and the LITUSE into different | |
1453 | instructions which do not refer to .lita. This can save | |
1454 | a memory reference, and permits removing a value from | |
1455 | .lita thus saving GP relative space. | |
1456 | ||
1457 | We do not these optimizations. To do them we would need | |
1458 | to arrange to link the .lita section first, so that by | |
1459 | the time we got here we would know the final values to | |
1460 | use. This would not be particularly difficult, but it is | |
1461 | not currently implemented. */ | |
1462 | ||
1463 | /* I believe that the LITERAL reloc will only apply to a ldq | |
9783e04a DM |
1464 | or ldl instruction, so check my assumption. */ |
1465 | { | |
1466 | unsigned long insn; | |
1467 | ||
1468 | insn = bfd_get_32 (input_bfd, | |
1469 | contents + r_vaddr - input_section->vma); | |
1470 | BFD_ASSERT (((insn >> 26) & 0x3f) == 0x29 | |
1471 | || ((insn >> 26) & 0x3f) == 0x28); | |
1472 | } | |
693f8191 KR |
1473 | |
1474 | relocatep = true; | |
1475 | addend = ecoff_data (input_bfd)->gp - gp; | |
1476 | gp_usedp = true; | |
1477 | break; | |
1478 | ||
1479 | case ALPHA_R_LITUSE: | |
1480 | /* See ALPHA_R_LITERAL above for the uses of this reloc. It | |
1481 | does not cause anything to happen, itself. */ | |
1482 | break; | |
1483 | ||
1484 | case ALPHA_R_GPDISP: | |
1485 | /* This marks the ldah of an ldah/lda pair which loads the | |
1486 | gp register with the difference of the gp value and the | |
1487 | current location. The second of the pair is r_symndx | |
1488 | bytes ahead, and is also marked with an ALPHA_R_IGNORE | |
1489 | reloc. */ | |
1490 | { | |
1491 | unsigned long insn1, insn2; | |
1492 | ||
1493 | BFD_ASSERT (ext_rel + 1 < ext_rel_end | |
1494 | && (((ext_rel + 1)->r_bits[0] | |
1495 | & RELOC_BITS0_TYPE_LITTLE) | |
1496 | >> RELOC_BITS0_TYPE_SH_LITTLE) == ALPHA_R_IGNORE | |
1497 | && (bfd_h_get_64 (input_bfd, | |
1498 | (bfd_byte *) (ext_rel + 1)->r_vaddr) | |
1499 | == r_vaddr - input_section->vma + r_symndx)); | |
1500 | ||
1501 | /* Get the two instructions. */ | |
1502 | insn1 = bfd_get_32 (input_bfd, | |
1503 | contents + r_vaddr - input_section->vma); | |
1504 | insn2 = bfd_get_32 (input_bfd, | |
1505 | (contents | |
1506 | + r_vaddr | |
1507 | - input_section->vma | |
1508 | + r_symndx)); | |
1509 | ||
1510 | BFD_ASSERT (((insn1 >> 26) & 0x3f) == 0x09); /* ldah */ | |
1511 | BFD_ASSERT (((insn2 >> 26) & 0x3f) == 0x08); /* lda */ | |
1512 | ||
1513 | /* Get the existing addend. We must account for the sign | |
1514 | extension done by lda and ldah. */ | |
1515 | addend = ((insn1 & 0xffff) << 16) + (insn2 & 0xffff); | |
1516 | if (insn1 & 0x8000) | |
1517 | { | |
1518 | /* This is addend -= 0x100000000 without causing an | |
1519 | integer overflow on a 32 bit host. */ | |
1520 | addend -= 0x80000000; | |
1521 | addend -= 0x80000000; | |
1522 | } | |
1523 | if (insn2 & 0x8000) | |
1524 | addend -= 0x10000; | |
1525 | ||
1526 | /* The existing addend includes the difference between the | |
1527 | gp of the input BFD and the address in the input BFD. | |
1528 | We want to change this to the difference between the | |
1529 | final GP and the final address. */ | |
1530 | addend += (gp | |
1531 | - ecoff_data (input_bfd)->gp | |
1532 | + input_section->vma | |
1533 | - (input_section->output_section->vma | |
1534 | + input_section->output_offset)); | |
1535 | ||
1536 | /* Change the instructions, accounting for the sign | |
1537 | extension, and write them out. */ | |
1538 | if (addend & 0x8000) | |
1539 | addend += 0x10000; | |
1540 | insn1 = (insn1 & 0xffff0000) | ((addend >> 16) & 0xffff); | |
1541 | insn2 = (insn2 & 0xffff0000) | (addend & 0xffff); | |
1542 | ||
1543 | bfd_put_32 (input_bfd, (bfd_vma) insn1, | |
1544 | contents + r_vaddr - input_section->vma); | |
1545 | bfd_put_32 (input_bfd, (bfd_vma) insn2, | |
1546 | contents + r_vaddr - input_section->vma + r_symndx); | |
1547 | ||
1548 | gp_usedp = true; | |
1549 | } | |
1550 | break; | |
1551 | ||
1552 | case ALPHA_R_OP_PUSH: | |
1553 | case ALPHA_R_OP_PSUB: | |
1554 | case ALPHA_R_OP_PRSHIFT: | |
1555 | /* Manipulate values on the reloc evaluation stack. The | |
1556 | r_vaddr field is not an address in input_section, it is | |
1557 | the current value (including any addend) of the object | |
1558 | being used. */ | |
1559 | if (! r_extern) | |
1560 | { | |
1561 | asection *s; | |
1562 | ||
1563 | s = symndx_to_section[r_symndx]; | |
1564 | if (s == (asection *) NULL) | |
1565 | abort (); | |
1566 | addend = s->output_section->vma + s->output_offset - s->vma; | |
1567 | } | |
1568 | else | |
1569 | { | |
1570 | struct ecoff_link_hash_entry *h; | |
1571 | ||
1572 | h = sym_hashes[r_symndx]; | |
1573 | if (h == (struct ecoff_link_hash_entry *) NULL) | |
1574 | abort (); | |
1575 | ||
1576 | if (! info->relocateable) | |
1577 | { | |
1578 | if (h->root.type == bfd_link_hash_defined) | |
1579 | addend = (h->root.u.def.value | |
1580 | + h->root.u.def.section->output_section->vma | |
1581 | + h->root.u.def.section->output_offset); | |
1582 | else | |
1583 | { | |
1584 | /* Note that we pass the address as 0, since we | |
1585 | do not have a meaningful number for the | |
1586 | location within the section that is being | |
1587 | relocated. */ | |
1588 | if (! ((*info->callbacks->undefined_symbol) | |
1589 | (info, h->root.root.string, input_bfd, | |
1590 | input_section, (bfd_vma) 0))) | |
1591 | return false; | |
1592 | addend = 0; | |
1593 | } | |
1594 | } | |
1595 | else | |
1596 | { | |
1597 | if (h->root.type != bfd_link_hash_defined | |
1598 | && h->indx == -1) | |
1599 | { | |
1600 | /* This symbol is not being written out. Pass | |
1601 | the address as 0, as with undefined_symbol, | |
1602 | above. */ | |
1603 | if (! ((*info->callbacks->unattached_reloc) | |
1604 | (info, h->root.root.string, input_bfd, | |
1605 | input_section, (bfd_vma) 0))) | |
1606 | return false; | |
1607 | } | |
1608 | ||
1609 | addend = alpha_convert_external_reloc (output_bfd, info, | |
1610 | input_bfd, | |
1611 | ext_rel, h); | |
1612 | } | |
1613 | } | |
1614 | ||
1615 | addend += r_vaddr; | |
1616 | ||
1617 | if (info->relocateable) | |
1618 | { | |
1619 | /* Adjust r_vaddr by the addend. */ | |
1620 | bfd_h_put_64 (input_bfd, addend, | |
1621 | (bfd_byte *) ext_rel->r_vaddr); | |
1622 | } | |
1623 | else | |
1624 | { | |
1625 | switch (r_type) | |
1626 | { | |
1627 | case ALPHA_R_OP_PUSH: | |
1628 | if (tos >= RELOC_STACKSIZE) | |
1629 | abort (); | |
1630 | stack[tos++] = addend; | |
1631 | break; | |
1632 | ||
1633 | case ALPHA_R_OP_PSUB: | |
1634 | if (tos == 0) | |
1635 | abort (); | |
1636 | stack[tos - 1] -= addend; | |
1637 | break; | |
1638 | ||
1639 | case ALPHA_R_OP_PRSHIFT: | |
1640 | if (tos == 0) | |
1641 | abort (); | |
1642 | stack[tos - 1] >>= addend; | |
1643 | break; | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | adjust_addrp = false; | |
1648 | break; | |
1649 | ||
1650 | case ALPHA_R_OP_STORE: | |
1651 | /* Store a value from the reloc stack into a bitfield. If | |
1652 | we are generating relocateable output, all we do is | |
1653 | adjust the address of the reloc. */ | |
1654 | if (! info->relocateable) | |
1655 | { | |
1656 | bfd_vma val; | |
1657 | ||
1658 | if (tos == 0) | |
1659 | abort (); | |
1660 | ||
1661 | /* FIXME: I don't know what kind of overflow checking, | |
1662 | if any, should be done here. */ | |
1663 | val = bfd_get_64 (input_bfd, | |
1664 | contents + r_vaddr - input_section->vma); | |
1665 | val &=~ (((1 << r_size) - 1) << r_offset); | |
1666 | val |= (stack[--tos] & ((1 << r_size) - 1)) << r_offset; | |
1667 | bfd_put_64 (input_bfd, val, | |
1668 | contents + r_vaddr - input_section->vma); | |
1669 | } | |
1670 | break; | |
1671 | ||
1672 | case ALPHA_R_GPVALUE: | |
1673 | /* I really don't know if this does the right thing. */ | |
1674 | gp = ecoff_data (input_bfd)->gp + r_symndx; | |
1675 | gp_undefined = false; | |
1676 | break; | |
1677 | } | |
1678 | ||
1679 | if (relocatep) | |
1680 | { | |
1681 | reloc_howto_type *howto; | |
1682 | struct ecoff_link_hash_entry *h = NULL; | |
1683 | asection *s = NULL; | |
1684 | bfd_vma relocation; | |
1685 | bfd_reloc_status_type r; | |
1686 | ||
1687 | /* Perform a relocation. */ | |
1688 | ||
1689 | howto = &alpha_howto_table[r_type]; | |
1690 | ||
1691 | if (r_extern) | |
1692 | { | |
1693 | h = sym_hashes[r_symndx]; | |
1694 | /* If h is NULL, that means that there is a reloc | |
1695 | against an external symbol which we thought was just | |
1696 | a debugging symbol. This should not happen. */ | |
1697 | if (h == (struct ecoff_link_hash_entry *) NULL) | |
1698 | abort (); | |
1699 | } | |
1700 | else | |
1701 | { | |
3f048f7f | 1702 | if (r_symndx >= NUM_RELOC_SECTIONS) |
693f8191 KR |
1703 | s = NULL; |
1704 | else | |
1705 | s = symndx_to_section[r_symndx]; | |
1706 | ||
1707 | if (s == (asection *) NULL) | |
1708 | abort (); | |
1709 | } | |
1710 | ||
1711 | if (info->relocateable) | |
1712 | { | |
1713 | /* We are generating relocateable output, and must | |
1714 | convert the existing reloc. */ | |
1715 | if (r_extern) | |
1716 | { | |
1717 | if (h->root.type != bfd_link_hash_defined | |
1718 | && h->indx == -1) | |
1719 | { | |
1720 | /* This symbol is not being written out. */ | |
1721 | if (! ((*info->callbacks->unattached_reloc) | |
1722 | (info, h->root.root.string, input_bfd, | |
1723 | input_section, r_vaddr - input_section->vma))) | |
1724 | return false; | |
1725 | } | |
1726 | ||
1727 | relocation = alpha_convert_external_reloc (output_bfd, | |
1728 | info, | |
1729 | input_bfd, | |
1730 | ext_rel, | |
1731 | h); | |
1732 | } | |
1733 | else | |
1734 | { | |
1735 | /* This is a relocation against a section. Adjust | |
1736 | the value by the amount the section moved. */ | |
1737 | relocation = (s->output_section->vma | |
1738 | + s->output_offset | |
1739 | - s->vma); | |
1740 | } | |
1741 | ||
1742 | /* If this is PC relative, the existing object file | |
1743 | appears to already have the reloc worked out. We | |
1744 | must subtract out the old value and add in the new | |
1745 | one. */ | |
1746 | if (howto->pc_relative) | |
1747 | relocation -= (input_section->output_section->vma | |
1748 | + input_section->output_offset | |
1749 | - input_section->vma); | |
1750 | ||
1751 | /* Put in any addend. */ | |
1752 | relocation += addend; | |
1753 | ||
1754 | /* Adjust the contents. */ | |
1755 | r = _bfd_relocate_contents (howto, input_bfd, relocation, | |
1756 | (contents | |
1757 | + r_vaddr | |
1758 | - input_section->vma)); | |
1759 | } | |
1760 | else | |
1761 | { | |
1762 | /* We are producing a final executable. */ | |
1763 | if (r_extern) | |
1764 | { | |
1765 | /* This is a reloc against a symbol. */ | |
1766 | if (h->root.type == bfd_link_hash_defined) | |
1767 | { | |
1768 | asection *hsec; | |
1769 | ||
1770 | hsec = h->root.u.def.section; | |
1771 | relocation = (h->root.u.def.value | |
1772 | + hsec->output_section->vma | |
1773 | + hsec->output_offset); | |
1774 | } | |
1775 | else | |
1776 | { | |
1777 | if (! ((*info->callbacks->undefined_symbol) | |
1778 | (info, h->root.root.string, input_bfd, | |
1779 | input_section, | |
1780 | r_vaddr - input_section->vma))) | |
1781 | return false; | |
1782 | relocation = 0; | |
1783 | } | |
1784 | } | |
1785 | else | |
1786 | { | |
1787 | /* This is a reloc against a section. */ | |
1788 | relocation = (s->output_section->vma | |
1789 | + s->output_offset | |
1790 | - s->vma); | |
1791 | ||
1792 | /* Adjust a PC relative relocation by removing the | |
1793 | reference to the original source section. */ | |
1794 | if (howto->pc_relative) | |
1795 | relocation += input_section->vma; | |
1796 | } | |
1797 | ||
1798 | r = _bfd_final_link_relocate (howto, | |
1799 | input_bfd, | |
1800 | input_section, | |
1801 | contents, | |
1802 | r_vaddr - input_section->vma, | |
1803 | relocation, | |
1804 | addend); | |
1805 | } | |
1806 | ||
1807 | if (r != bfd_reloc_ok) | |
1808 | { | |
1809 | switch (r) | |
1810 | { | |
1811 | default: | |
1812 | case bfd_reloc_outofrange: | |
1813 | abort (); | |
1814 | case bfd_reloc_overflow: | |
4991ebb9 ILT |
1815 | { |
1816 | const char *name; | |
1817 | ||
1818 | if (r_extern) | |
1819 | name = sym_hashes[r_symndx]->root.root.string; | |
1820 | else | |
1821 | name = bfd_section_name (input_bfd, | |
1822 | symndx_to_section[r_symndx]); | |
1823 | if (! ((*info->callbacks->reloc_overflow) | |
1824 | (info, name, alpha_howto_table[r_type].name, | |
1825 | (bfd_vma) 0, input_bfd, input_section, | |
1826 | r_vaddr - input_section->vma))) | |
1827 | return false; | |
1828 | } | |
693f8191 KR |
1829 | break; |
1830 | } | |
1831 | } | |
1832 | } | |
1833 | ||
1834 | if (info->relocateable && adjust_addrp) | |
1835 | { | |
1836 | /* Change the address of the relocation. */ | |
1837 | bfd_h_put_64 (input_bfd, | |
1838 | (input_section->output_section->vma | |
1839 | + input_section->output_offset | |
1840 | - input_section->vma | |
1841 | + r_vaddr), | |
1842 | (bfd_byte *) ext_rel->r_vaddr); | |
1843 | } | |
1844 | ||
1845 | if (gp_usedp && gp_undefined) | |
1846 | { | |
1847 | if (! ((*info->callbacks->reloc_dangerous) | |
1848 | (info, "GP relative relocation when GP not defined", | |
1849 | input_bfd, input_section, r_vaddr - input_section->vma))) | |
1850 | return false; | |
1851 | /* Only give the error once per link. */ | |
1852 | ecoff_data (output_bfd)->gp = gp = 4; | |
1853 | gp_undefined = false; | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | if (tos != 0) | |
1858 | abort (); | |
1859 | ||
1860 | return true; | |
5f8f6d56 | 1861 | } |
dae31cf5 ILT |
1862 | \f |
1863 | #define ecoff_core_file_p _bfd_dummy_target | |
1864 | #define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command | |
1865 | #define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal | |
1866 | #define ecoff_core_file_matches_executable_p \ | |
1867 | _bfd_dummy_core_file_matches_executable_p | |
1868 | \f | |
693f8191 KR |
1869 | /* This is the ECOFF backend structure. The backend field of the |
1870 | target vector points to this. */ | |
1871 | ||
1872 | static const struct ecoff_backend_data alpha_ecoff_backend_data = | |
1873 | { | |
1874 | /* COFF backend structure. */ | |
1875 | { | |
fa965415 | 1876 | (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */ |
693f8191 KR |
1877 | (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */ |
1878 | (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */ | |
fa965415 | 1879 | (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/ |
693f8191 KR |
1880 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */ |
1881 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */ | |
1882 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */ | |
1883 | alpha_ecoff_swap_filehdr_out, alpha_ecoff_swap_aouthdr_out, | |
1884 | alpha_ecoff_swap_scnhdr_out, | |
1885 | FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true, | |
1886 | alpha_ecoff_swap_filehdr_in, alpha_ecoff_swap_aouthdr_in, | |
1887 | alpha_ecoff_swap_scnhdr_in, alpha_ecoff_bad_format_hook, | |
1888 | ecoff_set_arch_mach_hook, ecoff_mkobject_hook, | |
1889 | ecoff_styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook, | |
1890 | ecoff_slurp_symbol_table, NULL, NULL | |
1891 | }, | |
1892 | /* Supported architecture. */ | |
1893 | bfd_arch_alpha, | |
1894 | /* Initial portion of armap string. */ | |
1895 | "________64", | |
1896 | /* The page boundary used to align sections in a demand-paged | |
1897 | executable file. E.g., 0x1000. */ | |
1898 | 0x2000, | |
1899 | /* True if the .rdata section is part of the text segment, as on the | |
1900 | Alpha. False if .rdata is part of the data segment, as on the | |
1901 | MIPS. */ | |
1902 | true, | |
1903 | /* Bitsize of constructor entries. */ | |
1904 | 64, | |
1905 | /* Reloc to use for constructor entries. */ | |
1906 | &alpha_howto_table[ALPHA_R_REFQUAD], | |
1907 | { | |
1908 | /* Symbol table magic number. */ | |
1909 | magicSym2, | |
1910 | /* Alignment of debugging information. E.g., 4. */ | |
1911 | 8, | |
1912 | /* Sizes of external symbolic information. */ | |
1913 | sizeof (struct hdr_ext), | |
1914 | sizeof (struct dnr_ext), | |
1915 | sizeof (struct pdr_ext), | |
1916 | sizeof (struct sym_ext), | |
1917 | sizeof (struct opt_ext), | |
1918 | sizeof (struct fdr_ext), | |
1919 | sizeof (struct rfd_ext), | |
1920 | sizeof (struct ext_ext), | |
1921 | /* Functions to swap in external symbolic data. */ | |
1922 | ecoff_swap_hdr_in, | |
1923 | ecoff_swap_dnr_in, | |
1924 | ecoff_swap_pdr_in, | |
1925 | ecoff_swap_sym_in, | |
1926 | ecoff_swap_opt_in, | |
1927 | ecoff_swap_fdr_in, | |
1928 | ecoff_swap_rfd_in, | |
1929 | ecoff_swap_ext_in, | |
1930 | /* Functions to swap out external symbolic data. */ | |
1931 | ecoff_swap_hdr_out, | |
1932 | ecoff_swap_dnr_out, | |
1933 | ecoff_swap_pdr_out, | |
1934 | ecoff_swap_sym_out, | |
1935 | ecoff_swap_opt_out, | |
1936 | ecoff_swap_fdr_out, | |
1937 | ecoff_swap_rfd_out, | |
1938 | ecoff_swap_ext_out | |
1939 | }, | |
1940 | /* External reloc size. */ | |
1941 | RELSZ, | |
1942 | /* Reloc swapping functions. */ | |
1943 | alpha_ecoff_swap_reloc_in, | |
1944 | alpha_ecoff_swap_reloc_out, | |
1945 | /* Backend reloc tweaking. */ | |
1946 | alpha_adjust_reloc_in, | |
1947 | alpha_adjust_reloc_out, | |
1948 | /* Relocate section contents while linking. */ | |
1949 | alpha_relocate_section | |
dae31cf5 | 1950 | }; |
5f8f6d56 | 1951 | |
693f8191 KR |
1952 | /* Looking up a reloc type is Alpha specific. */ |
1953 | #define ecoff_bfd_reloc_type_lookup alpha_bfd_reloc_type_lookup | |
1954 | ||
1955 | /* So is getting relocated section contents. */ | |
1956 | #define ecoff_bfd_get_relocated_section_contents \ | |
1957 | alpha_ecoff_get_relocated_section_contents | |
1958 | ||
dae31cf5 | 1959 | bfd_target ecoffalpha_little_vec = |
5f8f6d56 | 1960 | { |
dae31cf5 ILT |
1961 | "ecoff-littlealpha", /* name */ |
1962 | bfd_target_ecoff_flavour, | |
1963 | false, /* data byte order is little */ | |
1964 | false, /* header byte order is little */ | |
5f8f6d56 | 1965 | |
dae31cf5 ILT |
1966 | (HAS_RELOC | EXEC_P | /* object flags */ |
1967 | HAS_LINENO | HAS_DEBUG | | |
693f8191 | 1968 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
5f8f6d56 SC |
1969 | |
1970 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect | |
1971 | flags */ | |
1972 | 0, /* leading underscore */ | |
dae31cf5 | 1973 | ' ', /* ar_pad_char */ |
5f8f6d56 | 1974 | 15, /* ar_max_namelen */ |
dae31cf5 | 1975 | 4, /* minimum alignment power */ |
693f8191 KR |
1976 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
1977 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
1978 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | |
1979 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
1980 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
1981 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | |
5f8f6d56 | 1982 | |
3f048f7f | 1983 | {_bfd_dummy_target, alpha_ecoff_object_p, /* bfd_check_format */ |
5f8f6d56 | 1984 | ecoff_archive_p, _bfd_dummy_target}, |
693f8191 | 1985 | {bfd_false, ecoff_mkobject, /* bfd_set_format */ |
dae31cf5 | 1986 | _bfd_generic_mkarchive, bfd_false}, |
5f8f6d56 SC |
1987 | {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */ |
1988 | _bfd_write_archive_contents, bfd_false}, | |
1989 | JUMP_TABLE (ecoff), | |
693f8191 | 1990 | (PTR) &alpha_ecoff_backend_data |
5f8f6d56 | 1991 | }; |