1 | /* BFD back-end for MIPS Extended-Coff files.
|
---|
2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
|
---|
3 | 2000, 2001
|
---|
4 | Free Software Foundation, Inc.
|
---|
5 | Original version by Per Bothner.
|
---|
6 | Full support added by Ian Lance Taylor, ian@cygnus.com.
|
---|
7 |
|
---|
8 | This file is part of BFD, the Binary File Descriptor library.
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify
|
---|
11 | it under the terms of the GNU General Public License as published by
|
---|
12 | the Free Software Foundation; either version 2 of the License, or
|
---|
13 | (at your option) any later version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | GNU General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU General Public License
|
---|
21 | along with this program; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
---|
23 |
|
---|
24 | #include "bfd.h"
|
---|
25 | #include "sysdep.h"
|
---|
26 | #include "bfdlink.h"
|
---|
27 | #include "libbfd.h"
|
---|
28 | #include "coff/internal.h"
|
---|
29 | #include "coff/sym.h"
|
---|
30 | #include "coff/symconst.h"
|
---|
31 | #include "coff/ecoff.h"
|
---|
32 | #include "coff/mips.h"
|
---|
33 | #include "libcoff.h"
|
---|
34 | #include "libecoff.h"
|
---|
35 | |
---|
36 |
|
---|
37 | /* Prototypes for static functions. */
|
---|
38 |
|
---|
39 | static boolean mips_ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
|
---|
40 | static void mips_ecoff_swap_reloc_in PARAMS ((bfd *, PTR,
|
---|
41 | struct internal_reloc *));
|
---|
42 | static void mips_ecoff_swap_reloc_out PARAMS ((bfd *,
|
---|
43 | const struct internal_reloc *,
|
---|
44 | PTR));
|
---|
45 | static void mips_adjust_reloc_in PARAMS ((bfd *,
|
---|
46 | const struct internal_reloc *,
|
---|
47 | arelent *));
|
---|
48 | static void mips_adjust_reloc_out PARAMS ((bfd *, const arelent *,
|
---|
49 | struct internal_reloc *));
|
---|
50 | static bfd_reloc_status_type mips_generic_reloc PARAMS ((bfd *abfd,
|
---|
51 | arelent *reloc,
|
---|
52 | asymbol *symbol,
|
---|
53 | PTR data,
|
---|
54 | asection *section,
|
---|
55 | bfd *output_bfd,
|
---|
56 | char **error));
|
---|
57 | static bfd_reloc_status_type mips_refhi_reloc PARAMS ((bfd *abfd,
|
---|
58 | arelent *reloc,
|
---|
59 | asymbol *symbol,
|
---|
60 | PTR data,
|
---|
61 | asection *section,
|
---|
62 | bfd *output_bfd,
|
---|
63 | char **error));
|
---|
64 | static bfd_reloc_status_type mips_reflo_reloc PARAMS ((bfd *abfd,
|
---|
65 | arelent *reloc,
|
---|
66 | asymbol *symbol,
|
---|
67 | PTR data,
|
---|
68 | asection *section,
|
---|
69 | bfd *output_bfd,
|
---|
70 | char **error));
|
---|
71 | static bfd_reloc_status_type mips_gprel_reloc PARAMS ((bfd *abfd,
|
---|
72 | arelent *reloc,
|
---|
73 | asymbol *symbol,
|
---|
74 | PTR data,
|
---|
75 | asection *section,
|
---|
76 | bfd *output_bfd,
|
---|
77 | char **error));
|
---|
78 | static bfd_reloc_status_type mips_relhi_reloc PARAMS ((bfd *abfd,
|
---|
79 | arelent *reloc,
|
---|
80 | asymbol *symbol,
|
---|
81 | PTR data,
|
---|
82 | asection *section,
|
---|
83 | bfd *output_bfd,
|
---|
84 | char **error));
|
---|
85 | static bfd_reloc_status_type mips_rello_reloc PARAMS ((bfd *abfd,
|
---|
86 | arelent *reloc,
|
---|
87 | asymbol *symbol,
|
---|
88 | PTR data,
|
---|
89 | asection *section,
|
---|
90 | bfd *output_bfd,
|
---|
91 | char **error));
|
---|
92 | static bfd_reloc_status_type mips_switch_reloc PARAMS ((bfd *abfd,
|
---|
93 | arelent *reloc,
|
---|
94 | asymbol *symbol,
|
---|
95 | PTR data,
|
---|
96 | asection *section,
|
---|
97 | bfd *output_bfd,
|
---|
98 | char **error));
|
---|
99 | static void mips_relocate_hi PARAMS ((struct internal_reloc *refhi,
|
---|
100 | struct internal_reloc *reflo,
|
---|
101 | bfd *input_bfd,
|
---|
102 | asection *input_section,
|
---|
103 | bfd_byte *contents,
|
---|
104 | size_t adjust,
|
---|
105 | bfd_vma relocation,
|
---|
106 | boolean pcrel));
|
---|
107 | static boolean mips_relocate_section PARAMS ((bfd *, struct bfd_link_info *,
|
---|
108 | bfd *, asection *,
|
---|
109 | bfd_byte *, PTR));
|
---|
110 | static boolean mips_read_relocs PARAMS ((bfd *, asection *));
|
---|
111 | static boolean mips_relax_section PARAMS ((bfd *, asection *,
|
---|
112 | struct bfd_link_info *,
|
---|
113 | boolean *));
|
---|
114 | static boolean mips_relax_pcrel16 PARAMS ((struct bfd_link_info *, bfd *,
|
---|
115 | asection *,
|
---|
116 | struct ecoff_link_hash_entry *,
|
---|
117 | bfd_byte *, bfd_vma));
|
---|
118 | static reloc_howto_type *mips_bfd_reloc_type_lookup
|
---|
119 | PARAMS ((bfd *, bfd_reloc_code_real_type));
|
---|
120 | |
---|
121 |
|
---|
122 | /* ECOFF has COFF sections, but the debugging information is stored in
|
---|
123 | a completely different format. ECOFF targets use some of the
|
---|
124 | swapping routines from coffswap.h, and some of the generic COFF
|
---|
125 | routines in coffgen.c, but, unlike the real COFF targets, do not
|
---|
126 | use coffcode.h itself.
|
---|
127 |
|
---|
128 | Get the generic COFF swapping routines, except for the reloc,
|
---|
129 | symbol, and lineno ones. Give them ECOFF names. */
|
---|
130 | #define MIPSECOFF
|
---|
131 | #define NO_COFF_RELOCS
|
---|
132 | #define NO_COFF_SYMBOLS
|
---|
133 | #define NO_COFF_LINENOS
|
---|
134 | #define coff_swap_filehdr_in mips_ecoff_swap_filehdr_in
|
---|
135 | #define coff_swap_filehdr_out mips_ecoff_swap_filehdr_out
|
---|
136 | #define coff_swap_aouthdr_in mips_ecoff_swap_aouthdr_in
|
---|
137 | #define coff_swap_aouthdr_out mips_ecoff_swap_aouthdr_out
|
---|
138 | #define coff_swap_scnhdr_in mips_ecoff_swap_scnhdr_in
|
---|
139 | #define coff_swap_scnhdr_out mips_ecoff_swap_scnhdr_out
|
---|
140 | #include "coffswap.h"
|
---|
141 |
|
---|
142 | /* Get the ECOFF swapping routines. */
|
---|
143 | #define ECOFF_32
|
---|
144 | #include "ecoffswap.h"
|
---|
145 | |
---|
146 |
|
---|
147 | /* How to process the various relocs types. */
|
---|
148 |
|
---|
149 | static reloc_howto_type mips_howto_table[] =
|
---|
150 | {
|
---|
151 | /* Reloc type 0 is ignored. The reloc reading code ensures that
|
---|
152 | this is a reference to the .abs section, which will cause
|
---|
153 | bfd_perform_relocation to do nothing. */
|
---|
154 | HOWTO (MIPS_R_IGNORE, /* type */
|
---|
155 | 0, /* rightshift */
|
---|
156 | 0, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
157 | 8, /* bitsize */
|
---|
158 | false, /* pc_relative */
|
---|
159 | 0, /* bitpos */
|
---|
160 | complain_overflow_dont, /* complain_on_overflow */
|
---|
161 | 0, /* special_function */
|
---|
162 | "IGNORE", /* name */
|
---|
163 | false, /* partial_inplace */
|
---|
164 | 0, /* src_mask */
|
---|
165 | 0, /* dst_mask */
|
---|
166 | false), /* pcrel_offset */
|
---|
167 |
|
---|
168 | /* A 16 bit reference to a symbol, normally from a data section. */
|
---|
169 | HOWTO (MIPS_R_REFHALF, /* type */
|
---|
170 | 0, /* rightshift */
|
---|
171 | 1, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
172 | 16, /* bitsize */
|
---|
173 | false, /* pc_relative */
|
---|
174 | 0, /* bitpos */
|
---|
175 | complain_overflow_bitfield, /* complain_on_overflow */
|
---|
176 | mips_generic_reloc, /* special_function */
|
---|
177 | "REFHALF", /* name */
|
---|
178 | true, /* partial_inplace */
|
---|
179 | 0xffff, /* src_mask */
|
---|
180 | 0xffff, /* dst_mask */
|
---|
181 | false), /* pcrel_offset */
|
---|
182 |
|
---|
183 | /* A 32 bit reference to a symbol, normally from a data section. */
|
---|
184 | HOWTO (MIPS_R_REFWORD, /* type */
|
---|
185 | 0, /* rightshift */
|
---|
186 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
187 | 32, /* bitsize */
|
---|
188 | false, /* pc_relative */
|
---|
189 | 0, /* bitpos */
|
---|
190 | complain_overflow_bitfield, /* complain_on_overflow */
|
---|
191 | mips_generic_reloc, /* special_function */
|
---|
192 | "REFWORD", /* name */
|
---|
193 | true, /* partial_inplace */
|
---|
194 | 0xffffffff, /* src_mask */
|
---|
195 | 0xffffffff, /* dst_mask */
|
---|
196 | false), /* pcrel_offset */
|
---|
197 |
|
---|
198 | /* A 26 bit absolute jump address. */
|
---|
199 | HOWTO (MIPS_R_JMPADDR, /* type */
|
---|
200 | 2, /* rightshift */
|
---|
201 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
202 | 26, /* bitsize */
|
---|
203 | false, /* pc_relative */
|
---|
204 | 0, /* bitpos */
|
---|
205 | complain_overflow_dont, /* complain_on_overflow */
|
---|
206 | /* This needs complex overflow
|
---|
207 | detection, because the upper four
|
---|
208 | bits must match the PC. */
|
---|
209 | mips_generic_reloc, /* special_function */
|
---|
210 | "JMPADDR", /* name */
|
---|
211 | true, /* partial_inplace */
|
---|
212 | 0x3ffffff, /* src_mask */
|
---|
213 | 0x3ffffff, /* dst_mask */
|
---|
214 | false), /* pcrel_offset */
|
---|
215 |
|
---|
216 | /* The high 16 bits of a symbol value. Handled by the function
|
---|
217 | mips_refhi_reloc. */
|
---|
218 | HOWTO (MIPS_R_REFHI, /* type */
|
---|
219 | 16, /* rightshift */
|
---|
220 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
221 | 16, /* bitsize */
|
---|
222 | false, /* pc_relative */
|
---|
223 | 0, /* bitpos */
|
---|
224 | complain_overflow_bitfield, /* complain_on_overflow */
|
---|
225 | mips_refhi_reloc, /* special_function */
|
---|
226 | "REFHI", /* name */
|
---|
227 | true, /* partial_inplace */
|
---|
228 | 0xffff, /* src_mask */
|
---|
229 | 0xffff, /* dst_mask */
|
---|
230 | false), /* pcrel_offset */
|
---|
231 |
|
---|
232 | /* The low 16 bits of a symbol value. */
|
---|
233 | HOWTO (MIPS_R_REFLO, /* 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_dont, /* complain_on_overflow */
|
---|
240 | mips_reflo_reloc, /* special_function */
|
---|
241 | "REFLO", /* name */
|
---|
242 | true, /* partial_inplace */
|
---|
243 | 0xffff, /* src_mask */
|
---|
244 | 0xffff, /* dst_mask */
|
---|
245 | false), /* pcrel_offset */
|
---|
246 |
|
---|
247 | /* A reference to an offset from the gp register. Handled by the
|
---|
248 | function mips_gprel_reloc. */
|
---|
249 | HOWTO (MIPS_R_GPREL, /* type */
|
---|
250 | 0, /* rightshift */
|
---|
251 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
252 | 16, /* bitsize */
|
---|
253 | false, /* pc_relative */
|
---|
254 | 0, /* bitpos */
|
---|
255 | complain_overflow_signed, /* complain_on_overflow */
|
---|
256 | mips_gprel_reloc, /* special_function */
|
---|
257 | "GPREL", /* name */
|
---|
258 | true, /* partial_inplace */
|
---|
259 | 0xffff, /* src_mask */
|
---|
260 | 0xffff, /* dst_mask */
|
---|
261 | false), /* pcrel_offset */
|
---|
262 |
|
---|
263 | /* A reference to a literal using an offset from the gp register.
|
---|
264 | Handled by the function mips_gprel_reloc. */
|
---|
265 | HOWTO (MIPS_R_LITERAL, /* type */
|
---|
266 | 0, /* rightshift */
|
---|
267 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
268 | 16, /* bitsize */
|
---|
269 | false, /* pc_relative */
|
---|
270 | 0, /* bitpos */
|
---|
271 | complain_overflow_signed, /* complain_on_overflow */
|
---|
272 | mips_gprel_reloc, /* special_function */
|
---|
273 | "LITERAL", /* name */
|
---|
274 | true, /* partial_inplace */
|
---|
275 | 0xffff, /* src_mask */
|
---|
276 | 0xffff, /* dst_mask */
|
---|
277 | false), /* pcrel_offset */
|
---|
278 |
|
---|
279 | EMPTY_HOWTO (8),
|
---|
280 | EMPTY_HOWTO (9),
|
---|
281 | EMPTY_HOWTO (10),
|
---|
282 | EMPTY_HOWTO (11),
|
---|
283 |
|
---|
284 | /* This reloc is a Cygnus extension used when generating position
|
---|
285 | independent code for embedded systems. It represents a 16 bit PC
|
---|
286 | relative reloc rightshifted twice as used in the MIPS branch
|
---|
287 | instructions. */
|
---|
288 | HOWTO (MIPS_R_PCREL16, /* type */
|
---|
289 | 2, /* rightshift */
|
---|
290 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
291 | 16, /* bitsize */
|
---|
292 | true, /* pc_relative */
|
---|
293 | 0, /* bitpos */
|
---|
294 | complain_overflow_signed, /* complain_on_overflow */
|
---|
295 | mips_generic_reloc, /* special_function */
|
---|
296 | "PCREL16", /* name */
|
---|
297 | true, /* partial_inplace */
|
---|
298 | 0xffff, /* src_mask */
|
---|
299 | 0xffff, /* dst_mask */
|
---|
300 | true), /* pcrel_offset */
|
---|
301 |
|
---|
302 | /* This reloc is a Cygnus extension used when generating position
|
---|
303 | independent code for embedded systems. It represents the high 16
|
---|
304 | bits of a PC relative reloc. The next reloc must be
|
---|
305 | MIPS_R_RELLO, and the addend is formed from the addends of the
|
---|
306 | two instructions, just as in MIPS_R_REFHI and MIPS_R_REFLO. The
|
---|
307 | final value is actually PC relative to the location of the
|
---|
308 | MIPS_R_RELLO reloc, not the MIPS_R_RELHI reloc. */
|
---|
309 | HOWTO (MIPS_R_RELHI, /* type */
|
---|
310 | 16, /* rightshift */
|
---|
311 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
312 | 16, /* bitsize */
|
---|
313 | true, /* pc_relative */
|
---|
314 | 0, /* bitpos */
|
---|
315 | complain_overflow_bitfield, /* complain_on_overflow */
|
---|
316 | mips_relhi_reloc, /* special_function */
|
---|
317 | "RELHI", /* name */
|
---|
318 | true, /* partial_inplace */
|
---|
319 | 0xffff, /* src_mask */
|
---|
320 | 0xffff, /* dst_mask */
|
---|
321 | true), /* pcrel_offset */
|
---|
322 |
|
---|
323 | /* This reloc is a Cygnus extension used when generating position
|
---|
324 | independent code for embedded systems. It represents the low 16
|
---|
325 | bits of a PC relative reloc. */
|
---|
326 | HOWTO (MIPS_R_RELLO, /* type */
|
---|
327 | 0, /* rightshift */
|
---|
328 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
329 | 16, /* bitsize */
|
---|
330 | true, /* pc_relative */
|
---|
331 | 0, /* bitpos */
|
---|
332 | complain_overflow_dont, /* complain_on_overflow */
|
---|
333 | mips_rello_reloc, /* special_function */
|
---|
334 | "RELLO", /* name */
|
---|
335 | true, /* partial_inplace */
|
---|
336 | 0xffff, /* src_mask */
|
---|
337 | 0xffff, /* dst_mask */
|
---|
338 | true), /* pcrel_offset */
|
---|
339 |
|
---|
340 | EMPTY_HOWTO (15),
|
---|
341 | EMPTY_HOWTO (16),
|
---|
342 | EMPTY_HOWTO (17),
|
---|
343 | EMPTY_HOWTO (18),
|
---|
344 | EMPTY_HOWTO (19),
|
---|
345 | EMPTY_HOWTO (20),
|
---|
346 | EMPTY_HOWTO (21),
|
---|
347 |
|
---|
348 | /* This reloc is a Cygnus extension used when generating position
|
---|
349 | independent code for embedded systems. It represents an entry in
|
---|
350 | a switch table, which is the difference between two symbols in
|
---|
351 | the .text section. The symndx is actually the offset from the
|
---|
352 | reloc address to the subtrahend. See include/coff/mips.h for
|
---|
353 | more details. */
|
---|
354 | HOWTO (MIPS_R_SWITCH, /* type */
|
---|
355 | 0, /* rightshift */
|
---|
356 | 2, /* size (0 = byte, 1 = short, 2 = long) */
|
---|
357 | 32, /* bitsize */
|
---|
358 | true, /* pc_relative */
|
---|
359 | 0, /* bitpos */
|
---|
360 | complain_overflow_dont, /* complain_on_overflow */
|
---|
361 | mips_switch_reloc, /* special_function */
|
---|
362 | "SWITCH", /* name */
|
---|
363 | true, /* partial_inplace */
|
---|
364 | 0xffffffff, /* src_mask */
|
---|
365 | 0xffffffff, /* dst_mask */
|
---|
366 | true) /* pcrel_offset */
|
---|
367 | };
|
---|
368 |
|
---|
369 | #define MIPS_HOWTO_COUNT \
|
---|
370 | (sizeof mips_howto_table / sizeof mips_howto_table[0])
|
---|
371 |
|
---|
372 | /* When the linker is doing relaxing, it may change a external PCREL16
|
---|
373 | reloc. This typically represents an instruction like
|
---|
374 | bal foo
|
---|
375 | We change it to
|
---|
376 | .set noreorder
|
---|
377 | bal $L1
|
---|
378 | lui $at,%hi(foo - $L1)
|
---|
379 | $L1:
|
---|
380 | addiu $at,%lo(foo - $L1)
|
---|
381 | addu $at,$at,$31
|
---|
382 | jalr $at
|
---|
383 | PCREL16_EXPANSION_ADJUSTMENT is the number of bytes this changes the
|
---|
384 | instruction by. */
|
---|
385 |
|
---|
386 | #define PCREL16_EXPANSION_ADJUSTMENT (4 * 4)
|
---|
387 | |
---|
388 |
|
---|
389 | /* See whether the magic number matches. */
|
---|
390 |
|
---|
391 | static boolean
|
---|
392 | mips_ecoff_bad_format_hook (abfd, filehdr)
|
---|
393 | bfd *abfd;
|
---|
394 | PTR filehdr;
|
---|
395 | {
|
---|
396 | struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
|
---|
397 |
|
---|
398 | switch (internal_f->f_magic)
|
---|
399 | {
|
---|
400 | case MIPS_MAGIC_1:
|
---|
401 | /* I don't know what endianness this implies. */
|
---|
402 | return true;
|
---|
403 |
|
---|
404 | case MIPS_MAGIC_BIG:
|
---|
405 | case MIPS_MAGIC_BIG2:
|
---|
406 | case MIPS_MAGIC_BIG3:
|
---|
407 | return bfd_big_endian (abfd);
|
---|
408 |
|
---|
409 | case MIPS_MAGIC_LITTLE:
|
---|
410 | case MIPS_MAGIC_LITTLE2:
|
---|
411 | case MIPS_MAGIC_LITTLE3:
|
---|
412 | return bfd_little_endian (abfd);
|
---|
413 |
|
---|
414 | default:
|
---|
415 | return false;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | |
---|
419 |
|
---|
420 | /* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
|
---|
421 | external form. They use a bit which indicates whether the symbol
|
---|
422 | is external. */
|
---|
423 |
|
---|
424 | /* Swap a reloc in. */
|
---|
425 |
|
---|
426 | static void
|
---|
427 | mips_ecoff_swap_reloc_in (abfd, ext_ptr, intern)
|
---|
428 | bfd *abfd;
|
---|
429 | PTR ext_ptr;
|
---|
430 | struct internal_reloc *intern;
|
---|
431 | {
|
---|
432 | const RELOC *ext = (RELOC *) ext_ptr;
|
---|
433 |
|
---|
434 | intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
|
---|
435 | if (bfd_header_big_endian (abfd))
|
---|
436 | {
|
---|
437 | intern->r_symndx = (((int) ext->r_bits[0]
|
---|
438 | << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
|
---|
439 | | ((int) ext->r_bits[1]
|
---|
440 | << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
|
---|
441 | | ((int) ext->r_bits[2]
|
---|
442 | << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
|
---|
443 | intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
|
---|
444 | >> RELOC_BITS3_TYPE_SH_BIG);
|
---|
445 | intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
|
---|
446 | }
|
---|
447 | else
|
---|
448 | {
|
---|
449 | intern->r_symndx = (((int) ext->r_bits[0]
|
---|
450 | << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
|
---|
451 | | ((int) ext->r_bits[1]
|
---|
452 | << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
|
---|
453 | | ((int) ext->r_bits[2]
|
---|
454 | << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
|
---|
455 | intern->r_type = (((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
|
---|
456 | >> RELOC_BITS3_TYPE_SH_LITTLE)
|
---|
457 | | ((ext->r_bits[3] & RELOC_BITS3_TYPEHI_LITTLE)
|
---|
458 | << RELOC_BITS3_TYPEHI_SH_LITTLE));
|
---|
459 | intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
|
---|
463 | MIPS_R_RELLO reloc, r_symndx is actually the offset from the
|
---|
464 | reloc address to the base of the difference (see
|
---|
465 | include/coff/mips.h for more details). We copy symndx into the
|
---|
466 | r_offset field so as not to confuse ecoff_slurp_reloc_table in
|
---|
467 | ecoff.c. In adjust_reloc_in we then copy r_offset into the reloc
|
---|
468 | addend. */
|
---|
469 | if (intern->r_type == MIPS_R_SWITCH
|
---|
470 | || (! intern->r_extern
|
---|
471 | && (intern->r_type == MIPS_R_RELLO
|
---|
472 | || intern->r_type == MIPS_R_RELHI)))
|
---|
473 | {
|
---|
474 | BFD_ASSERT (! intern->r_extern);
|
---|
475 | intern->r_offset = intern->r_symndx;
|
---|
476 | if (intern->r_offset & 0x800000)
|
---|
477 | intern->r_offset -= 0x1000000;
|
---|
478 | intern->r_symndx = RELOC_SECTION_TEXT;
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 | /* Swap a reloc out. */
|
---|
483 |
|
---|
484 | static void
|
---|
485 | mips_ecoff_swap_reloc_out (abfd, intern, dst)
|
---|
486 | bfd *abfd;
|
---|
487 | const struct internal_reloc *intern;
|
---|
488 | PTR dst;
|
---|
489 | {
|
---|
490 | RELOC *ext = (RELOC *) dst;
|
---|
491 | long r_symndx;
|
---|
492 |
|
---|
493 | BFD_ASSERT (intern->r_extern
|
---|
494 | || (intern->r_symndx >= 0 && intern->r_symndx <= 12));
|
---|
495 |
|
---|
496 | /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELLO or
|
---|
497 | MIPS_R_RELHI reloc, we actually want to write the contents of
|
---|
498 | r_offset out as the symbol index. This undoes the change made by
|
---|
499 | mips_ecoff_swap_reloc_in. */
|
---|
500 | if (intern->r_type != MIPS_R_SWITCH
|
---|
501 | && (intern->r_extern
|
---|
502 | || (intern->r_type != MIPS_R_RELHI
|
---|
503 | && intern->r_type != MIPS_R_RELLO)))
|
---|
504 | r_symndx = intern->r_symndx;
|
---|
505 | else
|
---|
506 | {
|
---|
507 | BFD_ASSERT (intern->r_symndx == RELOC_SECTION_TEXT);
|
---|
508 | r_symndx = intern->r_offset & 0xffffff;
|
---|
509 | }
|
---|
510 |
|
---|
511 | bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
|
---|
512 | if (bfd_header_big_endian (abfd))
|
---|
513 | {
|
---|
514 | ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
|
---|
515 | ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
|
---|
516 | ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
|
---|
517 | ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
|
---|
518 | & RELOC_BITS3_TYPE_BIG)
|
---|
519 | | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
|
---|
520 | }
|
---|
521 | else
|
---|
522 | {
|
---|
523 | ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
|
---|
524 | ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
|
---|
525 | ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
|
---|
526 | ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
|
---|
527 | & RELOC_BITS3_TYPE_LITTLE)
|
---|
528 | | ((intern->r_type >> RELOC_BITS3_TYPEHI_SH_LITTLE
|
---|
529 | & RELOC_BITS3_TYPEHI_LITTLE))
|
---|
530 | | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | /* Finish canonicalizing a reloc. Part of this is generic to all
|
---|
535 | ECOFF targets, and that part is in ecoff.c. The rest is done in
|
---|
536 | this backend routine. It must fill in the howto field. */
|
---|
537 |
|
---|
538 | static void
|
---|
539 | mips_adjust_reloc_in (abfd, intern, rptr)
|
---|
540 | bfd *abfd;
|
---|
541 | const struct internal_reloc *intern;
|
---|
542 | arelent *rptr;
|
---|
543 | {
|
---|
544 | if (intern->r_type > MIPS_R_SWITCH)
|
---|
545 | abort ();
|
---|
546 |
|
---|
547 | if (! intern->r_extern
|
---|
548 | && (intern->r_type == MIPS_R_GPREL
|
---|
549 | || intern->r_type == MIPS_R_LITERAL))
|
---|
550 | rptr->addend += ecoff_data (abfd)->gp;
|
---|
551 |
|
---|
552 | /* If the type is MIPS_R_IGNORE, make sure this is a reference to
|
---|
553 | the absolute section so that the reloc is ignored. */
|
---|
554 | if (intern->r_type == MIPS_R_IGNORE)
|
---|
555 | rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
|
---|
556 |
|
---|
557 | /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
|
---|
558 | MIPS_R_RELLO reloc, we want the addend field of the BFD relocto
|
---|
559 | hold the value which was originally in the symndx field of the
|
---|
560 | internal MIPS ECOFF reloc. This value was copied into
|
---|
561 | intern->r_offset by mips_swap_reloc_in, and here we copy it into
|
---|
562 | the addend field. */
|
---|
563 | if (intern->r_type == MIPS_R_SWITCH
|
---|
564 | || (! intern->r_extern
|
---|
565 | && (intern->r_type == MIPS_R_RELHI
|
---|
566 | || intern->r_type == MIPS_R_RELLO)))
|
---|
567 | rptr->addend = intern->r_offset;
|
---|
568 |
|
---|
569 | rptr->howto = &mips_howto_table[intern->r_type];
|
---|
570 | }
|
---|
571 |
|
---|
572 | /* Make any adjustments needed to a reloc before writing it out. None
|
---|
573 | are needed for MIPS. */
|
---|
574 |
|
---|
575 | static void
|
---|
576 | mips_adjust_reloc_out (abfd, rel, intern)
|
---|
577 | bfd *abfd ATTRIBUTE_UNUSED;
|
---|
578 | const arelent *rel;
|
---|
579 | struct internal_reloc *intern;
|
---|
580 | {
|
---|
581 | /* For a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
|
---|
582 | MIPS_R_RELLO reloc, we must copy rel->addend into
|
---|
583 | intern->r_offset. This will then be written out as the symbol
|
---|
584 | index by mips_ecoff_swap_reloc_out. This operation parallels the
|
---|
585 | action of mips_adjust_reloc_in. */
|
---|
586 | if (intern->r_type == MIPS_R_SWITCH
|
---|
587 | || (! intern->r_extern
|
---|
588 | && (intern->r_type == MIPS_R_RELHI
|
---|
589 | || intern->r_type == MIPS_R_RELLO)))
|
---|
590 | intern->r_offset = rel->addend;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /* ECOFF relocs are either against external symbols, or against
|
---|
594 | sections. If we are producing relocateable output, and the reloc
|
---|
595 | is against an external symbol, and nothing has given us any
|
---|
596 | additional addend, the resulting reloc will also be against the
|
---|
597 | same symbol. In such a case, we don't want to change anything
|
---|
598 | about the way the reloc is handled, since it will all be done at
|
---|
599 | final link time. Rather than put special case code into
|
---|
600 | bfd_perform_relocation, all the reloc types use this howto
|
---|
601 | function. It just short circuits the reloc if producing
|
---|
602 | relocateable output against an external symbol. */
|
---|
603 |
|
---|
604 | static bfd_reloc_status_type
|
---|
605 | mips_generic_reloc (abfd,
|
---|
606 | reloc_entry,
|
---|
607 | symbol,
|
---|
608 | data,
|
---|
609 | input_section,
|
---|
610 | output_bfd,
|
---|
611 | error_message)
|
---|
612 | bfd *abfd ATTRIBUTE_UNUSED;
|
---|
613 | arelent *reloc_entry;
|
---|
614 | asymbol *symbol;
|
---|
615 | PTR data ATTRIBUTE_UNUSED;
|
---|
616 | asection *input_section;
|
---|
617 | bfd *output_bfd;
|
---|
618 | char **error_message ATTRIBUTE_UNUSED;
|
---|
619 | {
|
---|
620 | if (output_bfd != (bfd *) NULL
|
---|
621 | && (symbol->flags & BSF_SECTION_SYM) == 0
|
---|
622 | && reloc_entry->addend == 0)
|
---|
623 | {
|
---|
624 | reloc_entry->address += input_section->output_offset;
|
---|
625 | return bfd_reloc_ok;
|
---|
626 | }
|
---|
627 |
|
---|
628 | return bfd_reloc_continue;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /* Do a REFHI relocation. This has to be done in combination with a
|
---|
632 | REFLO reloc, because there is a carry from the REFLO to the REFHI.
|
---|
633 | Here we just save the information we need; we do the actual
|
---|
634 | relocation when we see the REFLO. MIPS ECOFF requires that the
|
---|
635 | REFLO immediately follow the REFHI. As a GNU extension, we permit
|
---|
636 | an arbitrary number of HI relocs to be associated with a single LO
|
---|
637 | reloc. This extension permits gcc to output the HI and LO relocs
|
---|
638 | itself. */
|
---|
639 |
|
---|
640 | struct mips_hi
|
---|
641 | {
|
---|
642 | struct mips_hi *next;
|
---|
643 | bfd_byte *addr;
|
---|
644 | bfd_vma addend;
|
---|
645 | };
|
---|
646 |
|
---|
647 | /* FIXME: This should not be a static variable. */
|
---|
648 |
|
---|
649 | static struct mips_hi *mips_refhi_list;
|
---|
650 |
|
---|
651 | static bfd_reloc_status_type
|
---|
652 | mips_refhi_reloc (abfd,
|
---|
653 | reloc_entry,
|
---|
654 | symbol,
|
---|
655 | data,
|
---|
656 | input_section,
|
---|
657 | output_bfd,
|
---|
658 | error_message)
|
---|
659 | bfd *abfd ATTRIBUTE_UNUSED;
|
---|
660 | arelent *reloc_entry;
|
---|
661 | asymbol *symbol;
|
---|
662 | PTR data;
|
---|
663 | asection *input_section;
|
---|
664 | bfd *output_bfd;
|
---|
665 | char **error_message ATTRIBUTE_UNUSED;
|
---|
666 | {
|
---|
667 | bfd_reloc_status_type ret;
|
---|
668 | bfd_vma relocation;
|
---|
669 | struct mips_hi *n;
|
---|
670 |
|
---|
671 | /* If we're relocating, and this an external symbol, we don't want
|
---|
672 | to change anything. */
|
---|
673 | if (output_bfd != (bfd *) NULL
|
---|
674 | && (symbol->flags & BSF_SECTION_SYM) == 0
|
---|
675 | && reloc_entry->addend == 0)
|
---|
676 | {
|
---|
677 | reloc_entry->address += input_section->output_offset;
|
---|
678 | return bfd_reloc_ok;
|
---|
679 | }
|
---|
680 |
|
---|
681 | ret = bfd_reloc_ok;
|
---|
682 | if (bfd_is_und_section (symbol->section)
|
---|
683 | && output_bfd == (bfd *) NULL)
|
---|
684 | ret = bfd_reloc_undefined;
|
---|
685 |
|
---|
686 | if (bfd_is_com_section (symbol->section))
|
---|
687 | relocation = 0;
|
---|
688 | else
|
---|
689 | relocation = symbol->value;
|
---|
690 |
|
---|
691 | relocation += symbol->section->output_section->vma;
|
---|
692 | relocation += symbol->section->output_offset;
|
---|
693 | relocation += reloc_entry->addend;
|
---|
694 |
|
---|
695 | if (reloc_entry->address > input_section->_cooked_size)
|
---|
696 | return bfd_reloc_outofrange;
|
---|
697 |
|
---|
698 | /* Save the information, and let REFLO do the actual relocation. */
|
---|
699 | n = (struct mips_hi *) bfd_malloc (sizeof *n);
|
---|
700 | if (n == NULL)
|
---|
701 | return bfd_reloc_outofrange;
|
---|
702 | n->addr = (bfd_byte *) data + reloc_entry->address;
|
---|
703 | n->addend = relocation;
|
---|
704 | n->next = mips_refhi_list;
|
---|
705 | mips_refhi_list = n;
|
---|
706 |
|
---|
707 | if (output_bfd != (bfd *) NULL)
|
---|
708 | reloc_entry->address += input_section->output_offset;
|
---|
709 |
|
---|
710 | return ret;
|
---|
711 | }
|
---|
712 |
|
---|
713 | /* Do a REFLO relocation. This is a straightforward 16 bit inplace
|
---|
714 | relocation; this function exists in order to do the REFHI
|
---|
715 | relocation described above. */
|
---|
716 |
|
---|
717 | static bfd_reloc_status_type
|
---|
718 | mips_reflo_reloc (abfd,
|
---|
719 | reloc_entry,
|
---|
720 | symbol,
|
---|
721 | data,
|
---|
722 | input_section,
|
---|
723 | output_bfd,
|
---|
724 | error_message)
|
---|
725 | bfd *abfd;
|
---|
726 | arelent *reloc_entry;
|
---|
727 | asymbol *symbol;
|
---|
728 | PTR data;
|
---|
729 | asection *input_section;
|
---|
730 | bfd *output_bfd;
|
---|
731 | char **error_message;
|
---|
732 | {
|
---|
733 | if (mips_refhi_list != NULL)
|
---|
734 | {
|
---|
735 | struct mips_hi *l;
|
---|
736 |
|
---|
737 | l = mips_refhi_list;
|
---|
738 | while (l != NULL)
|
---|
739 | {
|
---|
740 | unsigned long insn;
|
---|
741 | unsigned long val;
|
---|
742 | unsigned long vallo;
|
---|
743 | struct mips_hi *next;
|
---|
744 |
|
---|
745 | /* Do the REFHI relocation. Note that we actually don't
|
---|
746 | need to know anything about the REFLO itself, except
|
---|
747 | where to find the low 16 bits of the addend needed by the
|
---|
748 | REFHI. */
|
---|
749 | insn = bfd_get_32 (abfd, l->addr);
|
---|
750 | vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
|
---|
751 | & 0xffff);
|
---|
752 | val = ((insn & 0xffff) << 16) + vallo;
|
---|
753 | val += l->addend;
|
---|
754 |
|
---|
755 | /* The low order 16 bits are always treated as a signed
|
---|
756 | value. Therefore, a negative value in the low order bits
|
---|
757 | requires an adjustment in the high order bits. We need
|
---|
758 | to make this adjustment in two ways: once for the bits we
|
---|
759 | took from the data, and once for the bits we are putting
|
---|
760 | back in to the data. */
|
---|
761 | if ((vallo & 0x8000) != 0)
|
---|
762 | val -= 0x10000;
|
---|
763 | if ((val & 0x8000) != 0)
|
---|
764 | val += 0x10000;
|
---|
765 |
|
---|
766 | insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
|
---|
767 | bfd_put_32 (abfd, insn, l->addr);
|
---|
768 |
|
---|
769 | next = l->next;
|
---|
770 | free (l);
|
---|
771 | l = next;
|
---|
772 | }
|
---|
773 |
|
---|
774 | mips_refhi_list = NULL;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /* Now do the REFLO reloc in the usual way. */
|
---|
778 | return mips_generic_reloc (abfd, reloc_entry, symbol, data,
|
---|
779 | input_section, output_bfd, error_message);
|
---|
780 | }
|
---|
781 |
|
---|
782 | /* Do a GPREL relocation. This is a 16 bit value which must become
|
---|
783 | the offset from the gp register. */
|
---|
784 |
|
---|
785 | static bfd_reloc_status_type
|
---|
786 | mips_gprel_reloc (abfd,
|
---|
787 | reloc_entry,
|
---|
788 | symbol,
|
---|
789 | data,
|
---|
790 | input_section,
|
---|
791 | output_bfd,
|
---|
792 | error_message)
|
---|
793 | bfd *abfd;
|
---|
794 | arelent *reloc_entry;
|
---|
795 | asymbol *symbol;
|
---|
796 | PTR data;
|
---|
797 | asection *input_section;
|
---|
798 | bfd *output_bfd;
|
---|
799 | char **error_message;
|
---|
800 | {
|
---|
801 | boolean relocateable;
|
---|
802 | bfd_vma gp;
|
---|
803 | bfd_vma relocation;
|
---|
804 | unsigned long val;
|
---|
805 | unsigned long insn;
|
---|
806 |
|
---|
807 | /* If we're relocating, and this is an external symbol with no
|
---|
808 | addend, we don't want to change anything. We will only have an
|
---|
809 | addend if this is a newly created reloc, not read from an ECOFF
|
---|
810 | file. */
|
---|
811 | if (output_bfd != (bfd *) NULL
|
---|
812 | && (symbol->flags & BSF_SECTION_SYM) == 0
|
---|
813 | && reloc_entry->addend == 0)
|
---|
814 | {
|
---|
815 | reloc_entry->address += input_section->output_offset;
|
---|
816 | return bfd_reloc_ok;
|
---|
817 | }
|
---|
818 |
|
---|
819 | if (output_bfd != (bfd *) NULL)
|
---|
820 | relocateable = true;
|
---|
821 | else
|
---|
822 | {
|
---|
823 | relocateable = false;
|
---|
824 | output_bfd = symbol->section->output_section->owner;
|
---|
825 | }
|
---|
826 |
|
---|
827 | if (bfd_is_und_section (symbol->section)
|
---|
828 | && relocateable == false)
|
---|
829 | return bfd_reloc_undefined;
|
---|
830 |
|
---|
831 | /* We have to figure out the gp value, so that we can adjust the
|
---|
832 | symbol value correctly. We look up the symbol _gp in the output
|
---|
833 | BFD. If we can't find it, we're stuck. We cache it in the ECOFF
|
---|
834 | target data. We don't need to adjust the symbol value for an
|
---|
835 | external symbol if we are producing relocateable output. */
|
---|
836 | gp = _bfd_get_gp_value (output_bfd);
|
---|
837 | if (gp == 0
|
---|
838 | && (relocateable == false
|
---|
839 | || (symbol->flags & BSF_SECTION_SYM) != 0))
|
---|
840 | {
|
---|
841 | if (relocateable != false)
|
---|
842 | {
|
---|
843 | /* Make up a value. */
|
---|
844 | gp = symbol->section->output_section->vma + 0x4000;
|
---|
845 | _bfd_set_gp_value (output_bfd, gp);
|
---|
846 | }
|
---|
847 | else
|
---|
848 | {
|
---|
849 | unsigned int count;
|
---|
850 | asymbol **sym;
|
---|
851 | unsigned int i;
|
---|
852 |
|
---|
853 | count = bfd_get_symcount (output_bfd);
|
---|
854 | sym = bfd_get_outsymbols (output_bfd);
|
---|
855 |
|
---|
856 | if (sym == (asymbol **) NULL)
|
---|
857 | i = count;
|
---|
858 | else
|
---|
859 | {
|
---|
860 | for (i = 0; i < count; i++, sym++)
|
---|
861 | {
|
---|
862 | register CONST char *name;
|
---|
863 |
|
---|
864 | name = bfd_asymbol_name (*sym);
|
---|
865 | if (*name == '_' && strcmp (name, "_gp") == 0)
|
---|
866 | {
|
---|
867 | gp = bfd_asymbol_value (*sym);
|
---|
868 | _bfd_set_gp_value (output_bfd, gp);
|
---|
869 | break;
|
---|
870 | }
|
---|
871 | }
|
---|
872 | }
|
---|
873 |
|
---|
874 | if (i >= count)
|
---|
875 | {
|
---|
876 | /* Only get the error once. */
|
---|
877 | gp = 4;
|
---|
878 | _bfd_set_gp_value (output_bfd, gp);
|
---|
879 | *error_message =
|
---|
880 | (char *) _("GP relative relocation when _gp not defined");
|
---|
881 | return bfd_reloc_dangerous;
|
---|
882 | }
|
---|
883 | }
|
---|
884 | }
|
---|
885 |
|
---|
886 | if (bfd_is_com_section (symbol->section))
|
---|
887 | relocation = 0;
|
---|
888 | else
|
---|
889 | relocation = symbol->value;
|
---|
890 |
|
---|
891 | relocation += symbol->section->output_section->vma;
|
---|
892 | relocation += symbol->section->output_offset;
|
---|
893 |
|
---|
894 | if (reloc_entry->address > input_section->_cooked_size)
|
---|
895 | return bfd_reloc_outofrange;
|
---|
896 |
|
---|
897 | insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
|
---|
898 |
|
---|
899 | /* Set val to the offset into the section or symbol. */
|
---|
900 | val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
|
---|
901 | if (val & 0x8000)
|
---|
902 | val -= 0x10000;
|
---|
903 |
|
---|
904 | /* Adjust val for the final section location and GP value. If we
|
---|
905 | are producing relocateable output, we don't want to do this for
|
---|
906 | an external symbol. */
|
---|
907 | if (relocateable == false
|
---|
908 | || (symbol->flags & BSF_SECTION_SYM) != 0)
|
---|
909 | val += relocation - gp;
|
---|
910 |
|
---|
911 | insn = (insn &~ 0xffff) | (val & 0xffff);
|
---|
912 | bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
|
---|
913 |
|
---|
914 | if (relocateable != false)
|
---|
915 | reloc_entry->address += input_section->output_offset;
|
---|
916 |
|
---|
917 | /* Make sure it fit in 16 bits. */
|
---|
918 | if ((long) val >= 0x8000 || (long) val < -0x8000)
|
---|
919 | return bfd_reloc_overflow;
|
---|
920 |
|
---|
921 | return bfd_reloc_ok;
|
---|
922 | }
|
---|
923 |
|
---|
924 | /* Do a RELHI relocation. We do this in conjunction with a RELLO
|
---|
925 | reloc, just as REFHI and REFLO are done together. RELHI and RELLO
|
---|
926 | are Cygnus extensions used when generating position independent
|
---|
927 | code for embedded systems. */
|
---|
928 |
|
---|
929 | /* FIXME: This should not be a static variable. */
|
---|
930 |
|
---|
931 | static struct mips_hi *mips_relhi_list;
|
---|
932 |
|
---|
933 | static bfd_reloc_status_type
|
---|
934 | mips_relhi_reloc (abfd,
|
---|
935 | reloc_entry,
|
---|
936 | symbol,
|
---|
937 | data,
|
---|
938 | input_section,
|
---|
939 | output_bfd,
|
---|
940 | error_message)
|
---|
941 | bfd *abfd ATTRIBUTE_UNUSED;
|
---|
942 | arelent *reloc_entry;
|
---|
943 | asymbol *symbol;
|
---|
944 | PTR data;
|
---|
945 | asection *input_section;
|
---|
946 | bfd *output_bfd;
|
---|
947 | char **error_message ATTRIBUTE_UNUSED;
|
---|
948 | {
|
---|
949 | bfd_reloc_status_type ret;
|
---|
950 | bfd_vma relocation;
|
---|
951 | struct mips_hi *n;
|
---|
952 |
|
---|
953 | /* If this is a reloc against a section symbol, then it is correct
|
---|
954 | in the object file. The only time we want to change this case is
|
---|
955 | when we are relaxing, and that is handled entirely by
|
---|
956 | mips_relocate_section and never calls this function. */
|
---|
957 | if ((symbol->flags & BSF_SECTION_SYM) != 0)
|
---|
958 | {
|
---|
959 | if (output_bfd != (bfd *) NULL)
|
---|
960 | reloc_entry->address += input_section->output_offset;
|
---|
961 | return bfd_reloc_ok;
|
---|
962 | }
|
---|
963 |
|
---|
964 | /* This is an external symbol. If we're relocating, we don't want
|
---|
965 | to change anything. */
|
---|
966 | if (output_bfd != (bfd *) NULL)
|
---|
967 | {
|
---|
968 | reloc_entry->address += input_section->output_offset;
|
---|
969 | return bfd_reloc_ok;
|
---|
970 | }
|
---|
971 |
|
---|
972 | ret = bfd_reloc_ok;
|
---|
973 | if (bfd_is_und_section (symbol->section)
|
---|
974 | && output_bfd == (bfd *) NULL)
|
---|
975 | ret = bfd_reloc_undefined;
|
---|
976 |
|
---|
977 | if (bfd_is_com_section (symbol->section))
|
---|
978 | relocation = 0;
|
---|
979 | else
|
---|
980 | relocation = symbol->value;
|
---|
981 |
|
---|
982 | relocation += symbol->section->output_section->vma;
|
---|
983 | relocation += symbol->section->output_offset;
|
---|
984 | relocation += reloc_entry->addend;
|
---|
985 |
|
---|
986 | if (reloc_entry->address > input_section->_cooked_size)
|
---|
987 | return bfd_reloc_outofrange;
|
---|
988 |
|
---|
989 | /* Save the information, and let RELLO do the actual relocation. */
|
---|
990 | n = (struct mips_hi *) bfd_malloc (sizeof *n);
|
---|
991 | if (n == NULL)
|
---|
992 | return bfd_reloc_outofrange;
|
---|
993 | n->addr = (bfd_byte *) data + reloc_entry->address;
|
---|
994 | n->addend = relocation;
|
---|
995 | n->next = mips_relhi_list;
|
---|
996 | mips_relhi_list = n;
|
---|
997 |
|
---|
998 | if (output_bfd != (bfd *) NULL)
|
---|
999 | reloc_entry->address += input_section->output_offset;
|
---|
1000 |
|
---|
1001 | return ret;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /* Do a RELLO relocation. This is a straightforward 16 bit PC
|
---|
1005 | relative relocation; this function exists in order to do the RELHI
|
---|
1006 | relocation described above. */
|
---|
1007 |
|
---|
1008 | static bfd_reloc_status_type
|
---|
1009 | mips_rello_reloc (abfd,
|
---|
1010 | reloc_entry,
|
---|
1011 | symbol,
|
---|
1012 | data,
|
---|
1013 | input_section,
|
---|
1014 | output_bfd,
|
---|
1015 | error_message)
|
---|
1016 | bfd *abfd;
|
---|
1017 | arelent *reloc_entry;
|
---|
1018 | asymbol *symbol;
|
---|
1019 | PTR data;
|
---|
1020 | asection *input_section;
|
---|
1021 | bfd *output_bfd;
|
---|
1022 | char **error_message;
|
---|
1023 | {
|
---|
1024 | if (mips_relhi_list != NULL)
|
---|
1025 | {
|
---|
1026 | struct mips_hi *l;
|
---|
1027 |
|
---|
1028 | l = mips_relhi_list;
|
---|
1029 | while (l != NULL)
|
---|
1030 | {
|
---|
1031 | unsigned long insn;
|
---|
1032 | unsigned long val;
|
---|
1033 | unsigned long vallo;
|
---|
1034 | struct mips_hi *next;
|
---|
1035 |
|
---|
1036 | /* Do the RELHI relocation. Note that we actually don't
|
---|
1037 | need to know anything about the RELLO itself, except
|
---|
1038 | where to find the low 16 bits of the addend needed by the
|
---|
1039 | RELHI. */
|
---|
1040 | insn = bfd_get_32 (abfd, l->addr);
|
---|
1041 | vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
|
---|
1042 | & 0xffff);
|
---|
1043 | val = ((insn & 0xffff) << 16) + vallo;
|
---|
1044 | val += l->addend;
|
---|
1045 |
|
---|
1046 | /* If the symbol is defined, make val PC relative. If the
|
---|
1047 | symbol is not defined we don't want to do this, because
|
---|
1048 | we don't want the value in the object file to incorporate
|
---|
1049 | the address of the reloc. */
|
---|
1050 | if (! bfd_is_und_section (bfd_get_section (symbol))
|
---|
1051 | && ! bfd_is_com_section (bfd_get_section (symbol)))
|
---|
1052 | val -= (input_section->output_section->vma
|
---|
1053 | + input_section->output_offset
|
---|
1054 | + reloc_entry->address);
|
---|
1055 |
|
---|
1056 | /* The low order 16 bits are always treated as a signed
|
---|
1057 | value. Therefore, a negative value in the low order bits
|
---|
1058 | requires an adjustment in the high order bits. We need
|
---|
1059 | to make this adjustment in two ways: once for the bits we
|
---|
1060 | took from the data, and once for the bits we are putting
|
---|
1061 | back in to the data. */
|
---|
1062 | if ((vallo & 0x8000) != 0)
|
---|
1063 | val -= 0x10000;
|
---|
1064 | if ((val & 0x8000) != 0)
|
---|
1065 | val += 0x10000;
|
---|
1066 |
|
---|
1067 | insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
|
---|
1068 | bfd_put_32 (abfd, insn, l->addr);
|
---|
1069 |
|
---|
1070 | next = l->next;
|
---|
1071 | free (l);
|
---|
1072 | l = next;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | mips_relhi_list = NULL;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | /* If this is a reloc against a section symbol, then it is correct
|
---|
1079 | in the object file. The only time we want to change this case is
|
---|
1080 | when we are relaxing, and that is handled entirely by
|
---|
1081 | mips_relocate_section and never calls this function. */
|
---|
1082 | if ((symbol->flags & BSF_SECTION_SYM) != 0)
|
---|
1083 | {
|
---|
1084 | if (output_bfd != (bfd *) NULL)
|
---|
1085 | reloc_entry->address += input_section->output_offset;
|
---|
1086 | return bfd_reloc_ok;
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | /* bfd_perform_relocation does not handle pcrel_offset relocations
|
---|
1090 | correctly when generating a relocateable file, so handle them
|
---|
1091 | directly here. */
|
---|
1092 | if (output_bfd != (bfd *) NULL)
|
---|
1093 | {
|
---|
1094 | reloc_entry->address += input_section->output_offset;
|
---|
1095 | return bfd_reloc_ok;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /* Now do the RELLO reloc in the usual way. */
|
---|
1099 | return mips_generic_reloc (abfd, reloc_entry, symbol, data,
|
---|
1100 | input_section, output_bfd, error_message);
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /* This is the special function for the MIPS_R_SWITCH reloc. This
|
---|
1104 | special reloc is normally correct in the object file, and only
|
---|
1105 | requires special handling when relaxing. We don't want
|
---|
1106 | bfd_perform_relocation to tamper with it at all. */
|
---|
1107 |
|
---|
1108 | static bfd_reloc_status_type
|
---|
1109 | mips_switch_reloc (abfd,
|
---|
1110 | reloc_entry,
|
---|
1111 | symbol,
|
---|
1112 | data,
|
---|
1113 | input_section,
|
---|
1114 | output_bfd,
|
---|
1115 | error_message)
|
---|
1116 | bfd *abfd ATTRIBUTE_UNUSED;
|
---|
1117 | arelent *reloc_entry ATTRIBUTE_UNUSED;
|
---|
1118 | asymbol *symbol ATTRIBUTE_UNUSED;
|
---|
1119 | PTR data ATTRIBUTE_UNUSED;
|
---|
1120 | asection *input_section ATTRIBUTE_UNUSED;
|
---|
1121 | bfd *output_bfd ATTRIBUTE_UNUSED;
|
---|
1122 | char **error_message ATTRIBUTE_UNUSED;
|
---|
1123 | {
|
---|
1124 | return bfd_reloc_ok;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | /* Get the howto structure for a generic reloc type. */
|
---|
1128 |
|
---|
1129 | static reloc_howto_type *
|
---|
1130 | mips_bfd_reloc_type_lookup (abfd, code)
|
---|
1131 | bfd *abfd ATTRIBUTE_UNUSED;
|
---|
1132 | bfd_reloc_code_real_type code;
|
---|
1133 | {
|
---|
1134 | int mips_type;
|
---|
1135 |
|
---|
1136 | switch (code)
|
---|
1137 | {
|
---|
1138 | case BFD_RELOC_16:
|
---|
1139 | mips_type = MIPS_R_REFHALF;
|
---|
1140 | break;
|
---|
1141 | case BFD_RELOC_32:
|
---|
1142 | case BFD_RELOC_CTOR:
|
---|
1143 | mips_type = MIPS_R_REFWORD;
|
---|
1144 | break;
|
---|
1145 | case BFD_RELOC_MIPS_JMP:
|
---|
1146 | mips_type = MIPS_R_JMPADDR;
|
---|
1147 | break;
|
---|
1148 | case BFD_RELOC_HI16_S:
|
---|
1149 | mips_type = MIPS_R_REFHI;
|
---|
1150 | break;
|
---|
1151 | case BFD_RELOC_LO16:
|
---|
1152 | mips_type = MIPS_R_REFLO;
|
---|
1153 | break;
|
---|
1154 | case BFD_RELOC_MIPS_GPREL:
|
---|
1155 | mips_type = MIPS_R_GPREL;
|
---|
1156 | break;
|
---|
1157 | case BFD_RELOC_MIPS_LITERAL:
|
---|
1158 | mips_type = MIPS_R_LITERAL;
|
---|
1159 | break;
|
---|
1160 | case BFD_RELOC_16_PCREL_S2:
|
---|
1161 | mips_type = MIPS_R_PCREL16;
|
---|
1162 | break;
|
---|
1163 | case BFD_RELOC_PCREL_HI16_S:
|
---|
1164 | mips_type = MIPS_R_RELHI;
|
---|
1165 | break;
|
---|
1166 | case BFD_RELOC_PCREL_LO16:
|
---|
1167 | mips_type = MIPS_R_RELLO;
|
---|
1168 | break;
|
---|
1169 | case BFD_RELOC_GPREL32:
|
---|
1170 | mips_type = MIPS_R_SWITCH;
|
---|
1171 | break;
|
---|
1172 | default:
|
---|
1173 | return (reloc_howto_type *) NULL;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | return &mips_howto_table[mips_type];
|
---|
1177 | }
|
---|
1178 | |
---|
1179 |
|
---|
1180 | /* A helper routine for mips_relocate_section which handles the REFHI
|
---|
1181 | and RELHI relocations. The REFHI relocation must be followed by a
|
---|
1182 | REFLO relocation (and RELHI by a RELLO), and the addend used is
|
---|
1183 | formed from the addends of both instructions. */
|
---|
1184 |
|
---|
1185 | static void
|
---|
1186 | mips_relocate_hi (refhi, reflo, input_bfd, input_section, contents, adjust,
|
---|
1187 | relocation, pcrel)
|
---|
1188 | struct internal_reloc *refhi;
|
---|
1189 | struct internal_reloc *reflo;
|
---|
1190 | bfd *input_bfd;
|
---|
1191 | asection *input_section;
|
---|
1192 | bfd_byte *contents;
|
---|
1193 | size_t adjust;
|
---|
1194 | bfd_vma relocation;
|
---|
1195 | boolean pcrel;
|
---|
1196 | {
|
---|
1197 | unsigned long insn;
|
---|
1198 | unsigned long val;
|
---|
1199 | unsigned long vallo;
|
---|
1200 |
|
---|
1201 | if (refhi == NULL)
|
---|
1202 | return;
|
---|
1203 |
|
---|
1204 | insn = bfd_get_32 (input_bfd,
|
---|
1205 | contents + adjust + refhi->r_vaddr - input_section->vma);
|
---|
1206 | if (reflo == NULL)
|
---|
1207 | vallo = 0;
|
---|
1208 | else
|
---|
1209 | vallo = (bfd_get_32 (input_bfd,
|
---|
1210 | contents + adjust + reflo->r_vaddr - input_section->vma)
|
---|
1211 | & 0xffff);
|
---|
1212 |
|
---|
1213 | val = ((insn & 0xffff) << 16) + vallo;
|
---|
1214 | val += relocation;
|
---|
1215 |
|
---|
1216 | /* The low order 16 bits are always treated as a signed value.
|
---|
1217 | Therefore, a negative value in the low order bits requires an
|
---|
1218 | adjustment in the high order bits. We need to make this
|
---|
1219 | adjustment in two ways: once for the bits we took from the data,
|
---|
1220 | and once for the bits we are putting back in to the data. */
|
---|
1221 | if ((vallo & 0x8000) != 0)
|
---|
1222 | val -= 0x10000;
|
---|
1223 |
|
---|
1224 | if (pcrel)
|
---|
1225 | val -= (input_section->output_section->vma
|
---|
1226 | + input_section->output_offset
|
---|
1227 | + (reflo->r_vaddr - input_section->vma + adjust));
|
---|
1228 |
|
---|
1229 | if ((val & 0x8000) != 0)
|
---|
1230 | val += 0x10000;
|
---|
1231 |
|
---|
1232 | insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
|
---|
1233 | bfd_put_32 (input_bfd, (bfd_vma) insn,
|
---|
1234 | contents + adjust + refhi->r_vaddr - input_section->vma);
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | /* Relocate a section while linking a MIPS ECOFF file. */
|
---|
1238 |
|
---|
1239 | static boolean
|
---|
1240 | mips_relocate_section (output_bfd, info, input_bfd, input_section,
|
---|
1241 | contents, external_relocs)
|
---|
1242 | bfd *output_bfd;
|
---|
1243 | struct bfd_link_info *info;
|
---|
1244 | bfd *input_bfd;
|
---|
1245 | asection *input_section;
|
---|
1246 | bfd_byte *contents;
|
---|
1247 | PTR external_relocs;
|
---|
1248 | {
|
---|
1249 | asection **symndx_to_section;
|
---|
1250 | struct ecoff_link_hash_entry **sym_hashes;
|
---|
1251 | bfd_vma gp;
|
---|
1252 | boolean gp_undefined;
|
---|
1253 | size_t adjust;
|
---|
1254 | long *offsets;
|
---|
1255 | struct external_reloc *ext_rel;
|
---|
1256 | struct external_reloc *ext_rel_end;
|
---|
1257 | unsigned int i;
|
---|
1258 | boolean got_lo;
|
---|
1259 | struct internal_reloc lo_int_rel;
|
---|
1260 |
|
---|
1261 | BFD_ASSERT (input_bfd->xvec->byteorder
|
---|
1262 | == output_bfd->xvec->byteorder);
|
---|
1263 |
|
---|
1264 | /* We keep a table mapping the symndx found in an internal reloc to
|
---|
1265 | the appropriate section. This is faster than looking up the
|
---|
1266 | section by name each time. */
|
---|
1267 | symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
|
---|
1268 | if (symndx_to_section == (asection **) NULL)
|
---|
1269 | {
|
---|
1270 | symndx_to_section = ((asection **)
|
---|
1271 | bfd_alloc (input_bfd,
|
---|
1272 | (NUM_RELOC_SECTIONS
|
---|
1273 | * sizeof (asection *))));
|
---|
1274 | if (!symndx_to_section)
|
---|
1275 | return false;
|
---|
1276 |
|
---|
1277 | symndx_to_section[RELOC_SECTION_NONE] = NULL;
|
---|
1278 | symndx_to_section[RELOC_SECTION_TEXT] =
|
---|
1279 | bfd_get_section_by_name (input_bfd, ".text");
|
---|
1280 | symndx_to_section[RELOC_SECTION_RDATA] =
|
---|
1281 | bfd_get_section_by_name (input_bfd, ".rdata");
|
---|
1282 | symndx_to_section[RELOC_SECTION_DATA] =
|
---|
1283 | bfd_get_section_by_name (input_bfd, ".data");
|
---|
1284 | symndx_to_section[RELOC_SECTION_SDATA] =
|
---|
1285 | bfd_get_section_by_name (input_bfd, ".sdata");
|
---|
1286 | symndx_to_section[RELOC_SECTION_SBSS] =
|
---|
1287 | bfd_get_section_by_name (input_bfd, ".sbss");
|
---|
1288 | symndx_to_section[RELOC_SECTION_BSS] =
|
---|
1289 | bfd_get_section_by_name (input_bfd, ".bss");
|
---|
1290 | symndx_to_section[RELOC_SECTION_INIT] =
|
---|
1291 | bfd_get_section_by_name (input_bfd, ".init");
|
---|
1292 | symndx_to_section[RELOC_SECTION_LIT8] =
|
---|
1293 | bfd_get_section_by_name (input_bfd, ".lit8");
|
---|
1294 | symndx_to_section[RELOC_SECTION_LIT4] =
|
---|
1295 | bfd_get_section_by_name (input_bfd, ".lit4");
|
---|
1296 | symndx_to_section[RELOC_SECTION_XDATA] = NULL;
|
---|
1297 | symndx_to_section[RELOC_SECTION_PDATA] = NULL;
|
---|
1298 | symndx_to_section[RELOC_SECTION_FINI] =
|
---|
1299 | bfd_get_section_by_name (input_bfd, ".fini");
|
---|
1300 | symndx_to_section[RELOC_SECTION_LITA] = NULL;
|
---|
1301 | symndx_to_section[RELOC_SECTION_ABS] = NULL;
|
---|
1302 |
|
---|
1303 | ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | sym_hashes = ecoff_data (input_bfd)->sym_hashes;
|
---|
1307 |
|
---|
1308 | gp = _bfd_get_gp_value (output_bfd);
|
---|
1309 | if (gp == 0)
|
---|
1310 | gp_undefined = true;
|
---|
1311 | else
|
---|
1312 | gp_undefined = false;
|
---|
1313 |
|
---|
1314 | got_lo = false;
|
---|
1315 |
|
---|
1316 | adjust = 0;
|
---|
1317 |
|
---|
1318 | if (ecoff_section_data (input_bfd, input_section) == NULL)
|
---|
1319 | offsets = NULL;
|
---|
1320 | else
|
---|
1321 | offsets = ecoff_section_data (input_bfd, input_section)->offsets;
|
---|
1322 |
|
---|
1323 | ext_rel = (struct external_reloc *) external_relocs;
|
---|
1324 | ext_rel_end = ext_rel + input_section->reloc_count;
|
---|
1325 | for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++)
|
---|
1326 | {
|
---|
1327 | struct internal_reloc int_rel;
|
---|
1328 | boolean use_lo = false;
|
---|
1329 | bfd_vma addend;
|
---|
1330 | reloc_howto_type *howto;
|
---|
1331 | struct ecoff_link_hash_entry *h = NULL;
|
---|
1332 | asection *s = NULL;
|
---|
1333 | bfd_vma relocation;
|
---|
1334 | bfd_reloc_status_type r;
|
---|
1335 |
|
---|
1336 | if (! got_lo)
|
---|
1337 | mips_ecoff_swap_reloc_in (input_bfd, (PTR) ext_rel, &int_rel);
|
---|
1338 | else
|
---|
1339 | {
|
---|
1340 | int_rel = lo_int_rel;
|
---|
1341 | got_lo = false;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | BFD_ASSERT (int_rel.r_type
|
---|
1345 | < sizeof mips_howto_table / sizeof mips_howto_table[0]);
|
---|
1346 |
|
---|
1347 | /* The REFHI and RELHI relocs requires special handling. they
|
---|
1348 | must be followed by a REFLO or RELLO reloc, respectively, and
|
---|
1349 | the addend is formed from both relocs. */
|
---|
1350 | if (int_rel.r_type == MIPS_R_REFHI
|
---|
1351 | || int_rel.r_type == MIPS_R_RELHI)
|
---|
1352 | {
|
---|
1353 | struct external_reloc *lo_ext_rel;
|
---|
1354 |
|
---|
1355 | /* As a GNU extension, permit an arbitrary number of REFHI
|
---|
1356 | or RELHI relocs before the REFLO or RELLO reloc. This
|
---|
1357 | permits gcc to emit the HI and LO relocs itself. */
|
---|
1358 | for (lo_ext_rel = ext_rel + 1;
|
---|
1359 | lo_ext_rel < ext_rel_end;
|
---|
1360 | lo_ext_rel++)
|
---|
1361 | {
|
---|
1362 | mips_ecoff_swap_reloc_in (input_bfd, (PTR) lo_ext_rel,
|
---|
1363 | &lo_int_rel);
|
---|
1364 | if (lo_int_rel.r_type != int_rel.r_type)
|
---|
1365 | break;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | if (lo_ext_rel < ext_rel_end
|
---|
1369 | && (lo_int_rel.r_type
|
---|
1370 | == (int_rel.r_type == MIPS_R_REFHI
|
---|
1371 | ? MIPS_R_REFLO
|
---|
1372 | : MIPS_R_RELLO))
|
---|
1373 | && int_rel.r_extern == lo_int_rel.r_extern
|
---|
1374 | && int_rel.r_symndx == lo_int_rel.r_symndx)
|
---|
1375 | {
|
---|
1376 | use_lo = true;
|
---|
1377 | if (lo_ext_rel == ext_rel + 1)
|
---|
1378 | got_lo = true;
|
---|
1379 | }
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | howto = &mips_howto_table[int_rel.r_type];
|
---|
1383 |
|
---|
1384 | /* The SWITCH reloc must be handled specially. This reloc is
|
---|
1385 | marks the location of a difference between two portions of an
|
---|
1386 | object file. The symbol index does not reference a symbol,
|
---|
1387 | but is actually the offset from the reloc to the subtrahend
|
---|
1388 | of the difference. This reloc is correct in the object file,
|
---|
1389 | and needs no further adjustment, unless we are relaxing. If
|
---|
1390 | we are relaxing, we may have to add in an offset. Since no
|
---|
1391 | symbols are involved in this reloc, we handle it completely
|
---|
1392 | here. */
|
---|
1393 | if (int_rel.r_type == MIPS_R_SWITCH)
|
---|
1394 | {
|
---|
1395 | if (offsets != NULL
|
---|
1396 | && offsets[i] != 0)
|
---|
1397 | {
|
---|
1398 | r = _bfd_relocate_contents (howto, input_bfd,
|
---|
1399 | (bfd_vma) offsets[i],
|
---|
1400 | (contents
|
---|
1401 | + adjust
|
---|
1402 | + int_rel.r_vaddr
|
---|
1403 | - input_section->vma));
|
---|
1404 | BFD_ASSERT (r == bfd_reloc_ok);
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | continue;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | if (int_rel.r_extern)
|
---|
1411 | {
|
---|
1412 | h = sym_hashes[int_rel.r_symndx];
|
---|
1413 | /* If h is NULL, that means that there is a reloc against an
|
---|
1414 | external symbol which we thought was just a debugging
|
---|
1415 | symbol. This should not happen. */
|
---|
1416 | if (h == (struct ecoff_link_hash_entry *) NULL)
|
---|
1417 | abort ();
|
---|
1418 | }
|
---|
1419 | else
|
---|
1420 | {
|
---|
1421 | if (int_rel.r_symndx < 0 || int_rel.r_symndx >= NUM_RELOC_SECTIONS)
|
---|
1422 | s = NULL;
|
---|
1423 | else
|
---|
1424 | s = symndx_to_section[int_rel.r_symndx];
|
---|
1425 |
|
---|
1426 | if (s == (asection *) NULL)
|
---|
1427 | abort ();
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | /* The GPREL reloc uses an addend: the difference in the GP
|
---|
1431 | values. */
|
---|
1432 | if (int_rel.r_type != MIPS_R_GPREL
|
---|
1433 | && int_rel.r_type != MIPS_R_LITERAL)
|
---|
1434 | addend = 0;
|
---|
1435 | else
|
---|
1436 | {
|
---|
1437 | if (gp_undefined)
|
---|
1438 | {
|
---|
1439 | if (! ((*info->callbacks->reloc_dangerous)
|
---|
1440 | (info, _("GP relative relocation when GP not defined"),
|
---|
1441 | input_bfd, input_section,
|
---|
1442 | int_rel.r_vaddr - input_section->vma)))
|
---|
1443 | return false;
|
---|
1444 | /* Only give the error once per link. */
|
---|
1445 | gp = 4;
|
---|
1446 | _bfd_set_gp_value (output_bfd, gp);
|
---|
1447 | gp_undefined = false;
|
---|
1448 | }
|
---|
1449 | if (! int_rel.r_extern)
|
---|
1450 | {
|
---|
1451 | /* This is a relocation against a section. The current
|
---|
1452 | addend in the instruction is the difference between
|
---|
1453 | INPUT_SECTION->vma and the GP value of INPUT_BFD. We
|
---|
1454 | must change this to be the difference between the
|
---|
1455 | final definition (which will end up in RELOCATION)
|
---|
1456 | and the GP value of OUTPUT_BFD (which is in GP). */
|
---|
1457 | addend = ecoff_data (input_bfd)->gp - gp;
|
---|
1458 | }
|
---|
1459 | else if (! info->relocateable
|
---|
1460 | || h->root.type == bfd_link_hash_defined
|
---|
1461 | || h->root.type == bfd_link_hash_defweak)
|
---|
1462 | {
|
---|
1463 | /* This is a relocation against a defined symbol. The
|
---|
1464 | current addend in the instruction is simply the
|
---|
1465 | desired offset into the symbol (normally zero). We
|
---|
1466 | are going to change this into a relocation against a
|
---|
1467 | defined symbol, so we want the instruction to hold
|
---|
1468 | the difference between the final definition of the
|
---|
1469 | symbol (which will end up in RELOCATION) and the GP
|
---|
1470 | value of OUTPUT_BFD (which is in GP). */
|
---|
1471 | addend = - gp;
|
---|
1472 | }
|
---|
1473 | else
|
---|
1474 | {
|
---|
1475 | /* This is a relocation against an undefined or common
|
---|
1476 | symbol. The current addend in the instruction is
|
---|
1477 | simply the desired offset into the symbol (normally
|
---|
1478 | zero). We are generating relocateable output, and we
|
---|
1479 | aren't going to define this symbol, so we just leave
|
---|
1480 | the instruction alone. */
|
---|
1481 | addend = 0;
|
---|
1482 | }
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | /* If we are relaxing, mips_relax_section may have set
|
---|
1486 | offsets[i] to some value. A value of 1 means we must expand
|
---|
1487 | a PC relative branch into a multi-instruction of sequence,
|
---|
1488 | and any other value is an addend. */
|
---|
1489 | if (offsets != NULL
|
---|
1490 | && offsets[i] != 0)
|
---|
1491 | {
|
---|
1492 | BFD_ASSERT (! info->relocateable);
|
---|
1493 | BFD_ASSERT (int_rel.r_type == MIPS_R_PCREL16
|
---|
1494 | || int_rel.r_type == MIPS_R_RELHI
|
---|
1495 | || int_rel.r_type == MIPS_R_RELLO);
|
---|
1496 | if (offsets[i] != 1)
|
---|
1497 | addend += offsets[i];
|
---|
1498 | else
|
---|
1499 | {
|
---|
1500 | bfd_byte *here;
|
---|
1501 |
|
---|
1502 | BFD_ASSERT (int_rel.r_extern
|
---|
1503 | && int_rel.r_type == MIPS_R_PCREL16);
|
---|
1504 |
|
---|
1505 | /* Move the rest of the instructions up. */
|
---|
1506 | here = (contents
|
---|
1507 | + adjust
|
---|
1508 | + int_rel.r_vaddr
|
---|
1509 | - input_section->vma);
|
---|
1510 | memmove (here + PCREL16_EXPANSION_ADJUSTMENT, here,
|
---|
1511 | (size_t) (input_section->_raw_size
|
---|
1512 | - (int_rel.r_vaddr - input_section->vma)));
|
---|
1513 |
|
---|
1514 | /* Generate the new instructions. */
|
---|
1515 | if (! mips_relax_pcrel16 (info, input_bfd, input_section,
|
---|
1516 | h, here,
|
---|
1517 | (input_section->output_section->vma
|
---|
1518 | + input_section->output_offset
|
---|
1519 | + (int_rel.r_vaddr
|
---|
1520 | - input_section->vma)
|
---|
1521 | + adjust)))
|
---|
1522 | return false;
|
---|
1523 |
|
---|
1524 | /* We must adjust everything else up a notch. */
|
---|
1525 | adjust += PCREL16_EXPANSION_ADJUSTMENT;
|
---|
1526 |
|
---|
1527 | /* mips_relax_pcrel16 handles all the details of this
|
---|
1528 | relocation. */
|
---|
1529 | continue;
|
---|
1530 | }
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | /* If we are relaxing, and this is a reloc against the .text
|
---|
1534 | segment, we may need to adjust it if some branches have been
|
---|
1535 | expanded. The reloc types which are likely to occur in the
|
---|
1536 | .text section are handled efficiently by mips_relax_section,
|
---|
1537 | and thus do not need to be handled here. */
|
---|
1538 | if (ecoff_data (input_bfd)->debug_info.adjust != NULL
|
---|
1539 | && ! int_rel.r_extern
|
---|
1540 | && int_rel.r_symndx == RELOC_SECTION_TEXT
|
---|
1541 | && (strcmp (bfd_get_section_name (input_bfd, input_section),
|
---|
1542 | ".text") != 0
|
---|
1543 | || (int_rel.r_type != MIPS_R_PCREL16
|
---|
1544 | && int_rel.r_type != MIPS_R_SWITCH
|
---|
1545 | && int_rel.r_type != MIPS_R_RELHI
|
---|
1546 | && int_rel.r_type != MIPS_R_RELLO)))
|
---|
1547 | {
|
---|
1548 | bfd_vma adr;
|
---|
1549 | struct ecoff_value_adjust *a;
|
---|
1550 |
|
---|
1551 | /* We need to get the addend so that we know whether we need
|
---|
1552 | to adjust the address. */
|
---|
1553 | BFD_ASSERT (int_rel.r_type == MIPS_R_REFWORD);
|
---|
1554 |
|
---|
1555 | adr = bfd_get_32 (input_bfd,
|
---|
1556 | (contents
|
---|
1557 | + adjust
|
---|
1558 | + int_rel.r_vaddr
|
---|
1559 | - input_section->vma));
|
---|
1560 |
|
---|
1561 | for (a = ecoff_data (input_bfd)->debug_info.adjust;
|
---|
1562 | a != (struct ecoff_value_adjust *) NULL;
|
---|
1563 | a = a->next)
|
---|
1564 | {
|
---|
1565 | if (adr >= a->start && adr < a->end)
|
---|
1566 | addend += a->adjust;
|
---|
1567 | }
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | if (info->relocateable)
|
---|
1571 | {
|
---|
1572 | /* We are generating relocateable output, and must convert
|
---|
1573 | the existing reloc. */
|
---|
1574 | if (int_rel.r_extern)
|
---|
1575 | {
|
---|
1576 | if ((h->root.type == bfd_link_hash_defined
|
---|
1577 | || h->root.type == bfd_link_hash_defweak)
|
---|
1578 | && ! bfd_is_abs_section (h->root.u.def.section))
|
---|
1579 | {
|
---|
1580 | const char *name;
|
---|
1581 |
|
---|
1582 | /* This symbol is defined in the output. Convert
|
---|
1583 | the reloc from being against the symbol to being
|
---|
1584 | against the section. */
|
---|
1585 |
|
---|
1586 | /* Clear the r_extern bit. */
|
---|
1587 | int_rel.r_extern = 0;
|
---|
1588 |
|
---|
1589 | /* Compute a new r_symndx value. */
|
---|
1590 | s = h->root.u.def.section;
|
---|
1591 | name = bfd_get_section_name (output_bfd,
|
---|
1592 | s->output_section);
|
---|
1593 |
|
---|
1594 | int_rel.r_symndx = -1;
|
---|
1595 | switch (name[1])
|
---|
1596 | {
|
---|
1597 | case 'b':
|
---|
1598 | if (strcmp (name, ".bss") == 0)
|
---|
1599 | int_rel.r_symndx = RELOC_SECTION_BSS;
|
---|
1600 | break;
|
---|
1601 | case 'd':
|
---|
1602 | if (strcmp (name, ".data") == 0)
|
---|
1603 | int_rel.r_symndx = RELOC_SECTION_DATA;
|
---|
1604 | break;
|
---|
1605 | case 'f':
|
---|
1606 | if (strcmp (name, ".fini") == 0)
|
---|
1607 | int_rel.r_symndx = RELOC_SECTION_FINI;
|
---|
1608 | break;
|
---|
1609 | case 'i':
|
---|
1610 | if (strcmp (name, ".init") == 0)
|
---|
1611 | int_rel.r_symndx = RELOC_SECTION_INIT;
|
---|
1612 | break;
|
---|
1613 | case 'l':
|
---|
1614 | if (strcmp (name, ".lit8") == 0)
|
---|
1615 | int_rel.r_symndx = RELOC_SECTION_LIT8;
|
---|
1616 | else if (strcmp (name, ".lit4") == 0)
|
---|
1617 | int_rel.r_symndx = RELOC_SECTION_LIT4;
|
---|
1618 | break;
|
---|
1619 | case 'r':
|
---|
1620 | if (strcmp (name, ".rdata") == 0)
|
---|
1621 | int_rel.r_symndx = RELOC_SECTION_RDATA;
|
---|
1622 | break;
|
---|
1623 | case 's':
|
---|
1624 | if (strcmp (name, ".sdata") == 0)
|
---|
1625 | int_rel.r_symndx = RELOC_SECTION_SDATA;
|
---|
1626 | else if (strcmp (name, ".sbss") == 0)
|
---|
1627 | int_rel.r_symndx = RELOC_SECTION_SBSS;
|
---|
1628 | break;
|
---|
1629 | case 't':
|
---|
1630 | if (strcmp (name, ".text") == 0)
|
---|
1631 | int_rel.r_symndx = RELOC_SECTION_TEXT;
|
---|
1632 | break;
|
---|
1633 | }
|
---|
1634 |
|
---|
1635 | if (int_rel.r_symndx == -1)
|
---|
1636 | abort ();
|
---|
1637 |
|
---|
1638 | /* Add the section VMA and the symbol value. */
|
---|
1639 | relocation = (h->root.u.def.value
|
---|
1640 | + s->output_section->vma
|
---|
1641 | + s->output_offset);
|
---|
1642 |
|
---|
1643 | /* For a PC relative relocation, the object file
|
---|
1644 | currently holds just the addend. We must adjust
|
---|
1645 | by the address to get the right value. */
|
---|
1646 | if (howto->pc_relative)
|
---|
1647 | {
|
---|
1648 | relocation -= int_rel.r_vaddr - input_section->vma;
|
---|
1649 |
|
---|
1650 | /* If we are converting a RELHI or RELLO reloc
|
---|
1651 | from being against an external symbol to
|
---|
1652 | being against a section, we must put a
|
---|
1653 | special value into the r_offset field. This
|
---|
1654 | value is the old addend. The r_offset for
|
---|
1655 | both the RELHI and RELLO relocs are the same,
|
---|
1656 | and we set both when we see RELHI. */
|
---|
1657 | if (int_rel.r_type == MIPS_R_RELHI)
|
---|
1658 | {
|
---|
1659 | long addhi, addlo;
|
---|
1660 |
|
---|
1661 | addhi = bfd_get_32 (input_bfd,
|
---|
1662 | (contents
|
---|
1663 | + adjust
|
---|
1664 | + int_rel.r_vaddr
|
---|
1665 | - input_section->vma));
|
---|
1666 | addhi &= 0xffff;
|
---|
1667 | if (addhi & 0x8000)
|
---|
1668 | addhi -= 0x10000;
|
---|
1669 | addhi <<= 16;
|
---|
1670 |
|
---|
1671 | if (! use_lo)
|
---|
1672 | addlo = 0;
|
---|
1673 | else
|
---|
1674 | {
|
---|
1675 | addlo = bfd_get_32 (input_bfd,
|
---|
1676 | (contents
|
---|
1677 | + adjust
|
---|
1678 | + lo_int_rel.r_vaddr
|
---|
1679 | - input_section->vma));
|
---|
1680 | addlo &= 0xffff;
|
---|
1681 | if (addlo & 0x8000)
|
---|
1682 | addlo -= 0x10000;
|
---|
1683 |
|
---|
1684 | lo_int_rel.r_offset = addhi + addlo;
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | int_rel.r_offset = addhi + addlo;
|
---|
1688 | }
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 | h = NULL;
|
---|
1692 | }
|
---|
1693 | else
|
---|
1694 | {
|
---|
1695 | /* Change the symndx value to the right one for the
|
---|
1696 | output BFD. */
|
---|
1697 | int_rel.r_symndx = h->indx;
|
---|
1698 | if (int_rel.r_symndx == -1)
|
---|
1699 | {
|
---|
1700 | /* This symbol is not being written out. */
|
---|
1701 | if (! ((*info->callbacks->unattached_reloc)
|
---|
1702 | (info, h->root.root.string, input_bfd,
|
---|
1703 | input_section,
|
---|
1704 | int_rel.r_vaddr - input_section->vma)))
|
---|
1705 | return false;
|
---|
1706 | int_rel.r_symndx = 0;
|
---|
1707 | }
|
---|
1708 | relocation = 0;
|
---|
1709 | }
|
---|
1710 | }
|
---|
1711 | else
|
---|
1712 | {
|
---|
1713 | /* This is a relocation against a section. Adjust the
|
---|
1714 | value by the amount the section moved. */
|
---|
1715 | relocation = (s->output_section->vma
|
---|
1716 | + s->output_offset
|
---|
1717 | - s->vma);
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | relocation += addend;
|
---|
1721 | addend = 0;
|
---|
1722 |
|
---|
1723 | /* Adjust a PC relative relocation by removing the reference
|
---|
1724 | to the original address in the section and including the
|
---|
1725 | reference to the new address. However, external RELHI
|
---|
1726 | and RELLO relocs are PC relative, but don't include any
|
---|
1727 | reference to the address. The addend is merely an
|
---|
1728 | addend. */
|
---|
1729 | if (howto->pc_relative
|
---|
1730 | && (! int_rel.r_extern
|
---|
1731 | || (int_rel.r_type != MIPS_R_RELHI
|
---|
1732 | && int_rel.r_type != MIPS_R_RELLO)))
|
---|
1733 | relocation -= (input_section->output_section->vma
|
---|
1734 | + input_section->output_offset
|
---|
1735 | - input_section->vma);
|
---|
1736 |
|
---|
1737 | /* Adjust the contents. */
|
---|
1738 | if (relocation == 0)
|
---|
1739 | r = bfd_reloc_ok;
|
---|
1740 | else
|
---|
1741 | {
|
---|
1742 | if (int_rel.r_type != MIPS_R_REFHI
|
---|
1743 | && int_rel.r_type != MIPS_R_RELHI)
|
---|
1744 | r = _bfd_relocate_contents (howto, input_bfd, relocation,
|
---|
1745 | (contents
|
---|
1746 | + adjust
|
---|
1747 | + int_rel.r_vaddr
|
---|
1748 | - input_section->vma));
|
---|
1749 | else
|
---|
1750 | {
|
---|
1751 | mips_relocate_hi (&int_rel,
|
---|
1752 | use_lo ? &lo_int_rel : NULL,
|
---|
1753 | input_bfd, input_section, contents,
|
---|
1754 | adjust, relocation,
|
---|
1755 | int_rel.r_type == MIPS_R_RELHI);
|
---|
1756 | r = bfd_reloc_ok;
|
---|
1757 | }
|
---|
1758 | }
|
---|
1759 |
|
---|
1760 | /* Adjust the reloc address. */
|
---|
1761 | int_rel.r_vaddr += (input_section->output_section->vma
|
---|
1762 | + input_section->output_offset
|
---|
1763 | - input_section->vma);
|
---|
1764 |
|
---|
1765 | /* Save the changed reloc information. */
|
---|
1766 | mips_ecoff_swap_reloc_out (input_bfd, &int_rel, (PTR) ext_rel);
|
---|
1767 | }
|
---|
1768 | else
|
---|
1769 | {
|
---|
1770 | /* We are producing a final executable. */
|
---|
1771 | if (int_rel.r_extern)
|
---|
1772 | {
|
---|
1773 | /* This is a reloc against a symbol. */
|
---|
1774 | if (h->root.type == bfd_link_hash_defined
|
---|
1775 | || h->root.type == bfd_link_hash_defweak)
|
---|
1776 | {
|
---|
1777 | asection *hsec;
|
---|
1778 |
|
---|
1779 | hsec = h->root.u.def.section;
|
---|
1780 | relocation = (h->root.u.def.value
|
---|
1781 | + hsec->output_section->vma
|
---|
1782 | + hsec->output_offset);
|
---|
1783 | }
|
---|
1784 | else
|
---|
1785 | {
|
---|
1786 | if (! ((*info->callbacks->undefined_symbol)
|
---|
1787 | (info, h->root.root.string, input_bfd,
|
---|
1788 | input_section,
|
---|
1789 | int_rel.r_vaddr - input_section->vma, true)))
|
---|
1790 | return false;
|
---|
1791 | relocation = 0;
|
---|
1792 | }
|
---|
1793 | }
|
---|
1794 | else
|
---|
1795 | {
|
---|
1796 | /* This is a reloc against a section. */
|
---|
1797 | relocation = (s->output_section->vma
|
---|
1798 | + s->output_offset
|
---|
1799 | - s->vma);
|
---|
1800 |
|
---|
1801 | /* A PC relative reloc is already correct in the object
|
---|
1802 | file. Make it look like a pcrel_offset relocation by
|
---|
1803 | adding in the start address. */
|
---|
1804 | if (howto->pc_relative)
|
---|
1805 | {
|
---|
1806 | if (int_rel.r_type != MIPS_R_RELHI || ! use_lo)
|
---|
1807 | relocation += int_rel.r_vaddr + adjust;
|
---|
1808 | else
|
---|
1809 | relocation += lo_int_rel.r_vaddr + adjust;
|
---|
1810 | }
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | if (int_rel.r_type != MIPS_R_REFHI
|
---|
1814 | && int_rel.r_type != MIPS_R_RELHI)
|
---|
1815 | r = _bfd_final_link_relocate (howto,
|
---|
1816 | input_bfd,
|
---|
1817 | input_section,
|
---|
1818 | contents,
|
---|
1819 | (int_rel.r_vaddr
|
---|
1820 | - input_section->vma
|
---|
1821 | + adjust),
|
---|
1822 | relocation,
|
---|
1823 | addend);
|
---|
1824 | else
|
---|
1825 | {
|
---|
1826 | mips_relocate_hi (&int_rel,
|
---|
1827 | use_lo ? &lo_int_rel : NULL,
|
---|
1828 | input_bfd, input_section, contents, adjust,
|
---|
1829 | relocation,
|
---|
1830 | int_rel.r_type == MIPS_R_RELHI);
|
---|
1831 | r = bfd_reloc_ok;
|
---|
1832 | }
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 | /* MIPS_R_JMPADDR requires peculiar overflow detection. The
|
---|
1836 | instruction provides a 28 bit address (the two lower bits are
|
---|
1837 | implicit zeroes) which is combined with the upper four bits
|
---|
1838 | of the instruction address. */
|
---|
1839 | if (r == bfd_reloc_ok
|
---|
1840 | && int_rel.r_type == MIPS_R_JMPADDR
|
---|
1841 | && (((relocation
|
---|
1842 | + addend
|
---|
1843 | + (int_rel.r_extern ? 0 : s->vma))
|
---|
1844 | & 0xf0000000)
|
---|
1845 | != ((input_section->output_section->vma
|
---|
1846 | + input_section->output_offset
|
---|
1847 | + (int_rel.r_vaddr - input_section->vma)
|
---|
1848 | + adjust)
|
---|
1849 | & 0xf0000000)))
|
---|
1850 | r = bfd_reloc_overflow;
|
---|
1851 |
|
---|
1852 | if (r != bfd_reloc_ok)
|
---|
1853 | {
|
---|
1854 | switch (r)
|
---|
1855 | {
|
---|
1856 | default:
|
---|
1857 | case bfd_reloc_outofrange:
|
---|
1858 | abort ();
|
---|
1859 | case bfd_reloc_overflow:
|
---|
1860 | {
|
---|
1861 | const char *name;
|
---|
1862 |
|
---|
1863 | if (int_rel.r_extern)
|
---|
1864 | name = h->root.root.string;
|
---|
1865 | else
|
---|
1866 | name = bfd_section_name (input_bfd, s);
|
---|
1867 | if (! ((*info->callbacks->reloc_overflow)
|
---|
1868 | (info, name, howto->name, (bfd_vma) 0,
|
---|
1869 | input_bfd, input_section,
|
---|
1870 | int_rel.r_vaddr - input_section->vma)))
|
---|
1871 | return false;
|
---|
1872 | }
|
---|
1873 | break;
|
---|
1874 | }
|
---|
1875 | }
|
---|
1876 | }
|
---|
1877 |
|
---|
1878 | return true;
|
---|
1879 | }
|
---|
1880 | |
---|
1881 |
|
---|
1882 | /* Read in the relocs for a section. */
|
---|
1883 |
|
---|
1884 | static boolean
|
---|
1885 | mips_read_relocs (abfd, sec)
|
---|
1886 | bfd *abfd;
|
---|
1887 | asection *sec;
|
---|
1888 | {
|
---|
1889 | struct ecoff_section_tdata *section_tdata;
|
---|
1890 |
|
---|
1891 | section_tdata = ecoff_section_data (abfd, sec);
|
---|
1892 | if (section_tdata == (struct ecoff_section_tdata *) NULL)
|
---|
1893 | {
|
---|
1894 | sec->used_by_bfd =
|
---|
1895 | (PTR) bfd_alloc (abfd, sizeof (struct ecoff_section_tdata));
|
---|
1896 | if (sec->used_by_bfd == NULL)
|
---|
1897 | return false;
|
---|
1898 |
|
---|
1899 | section_tdata = ecoff_section_data (abfd, sec);
|
---|
1900 | section_tdata->external_relocs = NULL;
|
---|
1901 | section_tdata->contents = NULL;
|
---|
1902 | section_tdata->offsets = NULL;
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | if (section_tdata->external_relocs == NULL)
|
---|
1906 | {
|
---|
1907 | bfd_size_type external_relocs_size;
|
---|
1908 |
|
---|
1909 | external_relocs_size = (ecoff_backend (abfd)->external_reloc_size
|
---|
1910 | * sec->reloc_count);
|
---|
1911 |
|
---|
1912 | section_tdata->external_relocs =
|
---|
1913 | (PTR) bfd_alloc (abfd, external_relocs_size);
|
---|
1914 | if (section_tdata->external_relocs == NULL && external_relocs_size != 0)
|
---|
1915 | return false;
|
---|
1916 |
|
---|
1917 | if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
|
---|
1918 | || (bfd_read (section_tdata->external_relocs, 1,
|
---|
1919 | external_relocs_size, abfd)
|
---|
1920 | != external_relocs_size))
|
---|
1921 | return false;
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | return true;
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 | /* Relax a section when linking a MIPS ECOFF file. This is used for
|
---|
1928 | embedded PIC code, which always uses PC relative branches which
|
---|
1929 | only have an 18 bit range on MIPS. If a branch is not in range, we
|
---|
1930 | generate a long instruction sequence to compensate. Each time we
|
---|
1931 | find a branch to expand, we have to check all the others again to
|
---|
1932 | make sure they are still in range. This is slow, but it only has
|
---|
1933 | to be done when -relax is passed to the linker.
|
---|
1934 |
|
---|
1935 | This routine figures out which branches need to expand; the actual
|
---|
1936 | expansion is done in mips_relocate_section when the section
|
---|
1937 | contents are relocated. The information is stored in the offsets
|
---|
1938 | field of the ecoff_section_tdata structure. An offset of 1 means
|
---|
1939 | that the branch must be expanded into a multi-instruction PC
|
---|
1940 | relative branch (such an offset will only occur for a PC relative
|
---|
1941 | branch to an external symbol). Any other offset must be a multiple
|
---|
1942 | of four, and is the amount to change the branch by (such an offset
|
---|
1943 | will only occur for a PC relative branch within the same section).
|
---|
1944 |
|
---|
1945 | We do not modify the section relocs or contents themselves so that
|
---|
1946 | if memory usage becomes an issue we can discard them and read them
|
---|
1947 | again. The only information we must save in memory between this
|
---|
1948 | routine and the mips_relocate_section routine is the table of
|
---|
1949 | offsets. */
|
---|
1950 |
|
---|
1951 | static boolean
|
---|
1952 | mips_relax_section (abfd, sec, info, again)
|
---|
1953 | bfd *abfd;
|
---|
1954 | asection *sec;
|
---|
1955 | struct bfd_link_info *info;
|
---|
1956 | boolean *again;
|
---|
1957 | {
|
---|
1958 | struct ecoff_section_tdata *section_tdata;
|
---|
1959 | bfd_byte *contents = NULL;
|
---|
1960 | long *offsets;
|
---|
1961 | struct external_reloc *ext_rel;
|
---|
1962 | struct external_reloc *ext_rel_end;
|
---|
1963 | unsigned int i;
|
---|
1964 |
|
---|
1965 | /* Assume we are not going to need another pass. */
|
---|
1966 | *again = false;
|
---|
1967 |
|
---|
1968 | /* If we are not generating an ECOFF file, this is much too
|
---|
1969 | confusing to deal with. */
|
---|
1970 | if (info->hash->creator->flavour != bfd_get_flavour (abfd))
|
---|
1971 | return true;
|
---|
1972 |
|
---|
1973 | /* If there are no relocs, there is nothing to do. */
|
---|
1974 | if (sec->reloc_count == 0)
|
---|
1975 | return true;
|
---|
1976 |
|
---|
1977 | /* We are only interested in PC relative relocs, and why would there
|
---|
1978 | ever be one from anything but the .text section? */
|
---|
1979 | if (strcmp (bfd_get_section_name (abfd, sec), ".text") != 0)
|
---|
1980 | return true;
|
---|
1981 |
|
---|
1982 | /* Read in the relocs, if we haven't already got them. */
|
---|
1983 | section_tdata = ecoff_section_data (abfd, sec);
|
---|
1984 | if (section_tdata == (struct ecoff_section_tdata *) NULL
|
---|
1985 | || section_tdata->external_relocs == NULL)
|
---|
1986 | {
|
---|
1987 | if (! mips_read_relocs (abfd, sec))
|
---|
1988 | goto error_return;
|
---|
1989 | section_tdata = ecoff_section_data (abfd, sec);
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 | if (sec->_cooked_size == 0)
|
---|
1993 | {
|
---|
1994 | /* We must initialize _cooked_size only the first time we are
|
---|
1995 | called. */
|
---|
1996 | sec->_cooked_size = sec->_raw_size;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | contents = section_tdata->contents;
|
---|
2000 | offsets = section_tdata->offsets;
|
---|
2001 |
|
---|
2002 | /* Look for any external PC relative relocs. Internal PC relative
|
---|
2003 | relocs are already correct in the object file, so they certainly
|
---|
2004 | can not overflow. */
|
---|
2005 | ext_rel = (struct external_reloc *) section_tdata->external_relocs;
|
---|
2006 | ext_rel_end = ext_rel + sec->reloc_count;
|
---|
2007 | for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++)
|
---|
2008 | {
|
---|
2009 | struct internal_reloc int_rel;
|
---|
2010 | struct ecoff_link_hash_entry *h;
|
---|
2011 | asection *hsec;
|
---|
2012 | bfd_signed_vma relocation;
|
---|
2013 | struct external_reloc *adj_ext_rel;
|
---|
2014 | unsigned int adj_i;
|
---|
2015 | unsigned long ext_count;
|
---|
2016 | struct ecoff_link_hash_entry **adj_h_ptr;
|
---|
2017 | struct ecoff_link_hash_entry **adj_h_ptr_end;
|
---|
2018 | struct ecoff_value_adjust *adjust;
|
---|
2019 |
|
---|
2020 | /* If we have already expanded this reloc, we certainly don't
|
---|
2021 | need to do it again. */
|
---|
2022 | if (offsets != (long *) NULL && offsets[i] == 1)
|
---|
2023 | continue;
|
---|
2024 |
|
---|
2025 | /* Quickly check that this reloc is external PCREL16. */
|
---|
2026 | if (bfd_header_big_endian (abfd))
|
---|
2027 | {
|
---|
2028 | if ((ext_rel->r_bits[3] & RELOC_BITS3_EXTERN_BIG) == 0
|
---|
2029 | || (((ext_rel->r_bits[3] & RELOC_BITS3_TYPE_BIG)
|
---|
2030 | >> RELOC_BITS3_TYPE_SH_BIG)
|
---|
2031 | != MIPS_R_PCREL16))
|
---|
2032 | continue;
|
---|
2033 | }
|
---|
2034 | else
|
---|
2035 | {
|
---|
2036 | if ((ext_rel->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) == 0
|
---|
2037 | || (((ext_rel->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
|
---|
2038 | >> RELOC_BITS3_TYPE_SH_LITTLE)
|
---|
2039 | != MIPS_R_PCREL16))
|
---|
2040 | continue;
|
---|
2041 | }
|
---|
2042 |
|
---|
2043 | mips_ecoff_swap_reloc_in (abfd, (PTR) ext_rel, &int_rel);
|
---|
2044 |
|
---|
2045 | h = ecoff_data (abfd)->sym_hashes[int_rel.r_symndx];
|
---|
2046 | if (h == (struct ecoff_link_hash_entry *) NULL)
|
---|
2047 | abort ();
|
---|
2048 |
|
---|
2049 | if (h->root.type != bfd_link_hash_defined
|
---|
2050 | && h->root.type != bfd_link_hash_defweak)
|
---|
2051 | {
|
---|
2052 | /* Just ignore undefined symbols. These will presumably
|
---|
2053 | generate an error later in the link. */
|
---|
2054 | continue;
|
---|
2055 | }
|
---|
2056 |
|
---|
2057 | /* Get the value of the symbol. */
|
---|
2058 | hsec = h->root.u.def.section;
|
---|
2059 | relocation = (h->root.u.def.value
|
---|
2060 | + hsec->output_section->vma
|
---|
2061 | + hsec->output_offset);
|
---|
2062 |
|
---|
2063 | /* Subtract out the current address. */
|
---|
2064 | relocation -= (sec->output_section->vma
|
---|
2065 | + sec->output_offset
|
---|
2066 | + (int_rel.r_vaddr - sec->vma));
|
---|
2067 |
|
---|
2068 | /* The addend is stored in the object file. In the normal case
|
---|
2069 | of ``bal symbol'', the addend will be -4. It will only be
|
---|
2070 | different in the case of ``bal symbol+constant''. To avoid
|
---|
2071 | always reading in the section contents, we don't check the
|
---|
2072 | addend in the object file (we could easily check the contents
|
---|
2073 | if we happen to have already read them in, but I fear that
|
---|
2074 | this could be confusing). This means we will screw up if
|
---|
2075 | there is a branch to a symbol that is in range, but added to
|
---|
2076 | a constant which puts it out of range; in such a case the
|
---|
2077 | link will fail with a reloc overflow error. Since the
|
---|
2078 | compiler will never generate such code, it should be easy
|
---|
2079 | enough to work around it by changing the assembly code in the
|
---|
2080 | source file. */
|
---|
2081 | relocation -= 4;
|
---|
2082 |
|
---|
2083 | /* Now RELOCATION is the number we want to put in the object
|
---|
2084 | file. See whether it fits. */
|
---|
2085 | if (relocation >= -0x20000 && relocation < 0x20000)
|
---|
2086 | continue;
|
---|
2087 |
|
---|
2088 | /* Now that we know this reloc needs work, which will rarely
|
---|
2089 | happen, go ahead and grab the section contents. */
|
---|
2090 | if (contents == (bfd_byte *) NULL)
|
---|
2091 | {
|
---|
2092 | if (info->keep_memory)
|
---|
2093 | contents = (bfd_byte *) bfd_alloc (abfd, sec->_raw_size);
|
---|
2094 | else
|
---|
2095 | contents = (bfd_byte *) bfd_malloc ((size_t) sec->_raw_size);
|
---|
2096 | if (contents == (bfd_byte *) NULL)
|
---|
2097 | goto error_return;
|
---|
2098 | if (! bfd_get_section_contents (abfd, sec, (PTR) contents,
|
---|
2099 | (file_ptr) 0, sec->_raw_size))
|
---|
2100 | goto error_return;
|
---|
2101 | if (info->keep_memory)
|
---|
2102 | section_tdata->contents = contents;
|
---|
2103 | }
|
---|
2104 |
|
---|
2105 | /* We only support changing the bal instruction. It would be
|
---|
2106 | possible to handle other PC relative branches, but some of
|
---|
2107 | them (the conditional branches) would require a different
|
---|
2108 | length instruction sequence which would complicate both this
|
---|
2109 | routine and mips_relax_pcrel16. It could be written if
|
---|
2110 | somebody felt it were important. Ignoring this reloc will
|
---|
2111 | presumably cause a reloc overflow error later on. */
|
---|
2112 | if (bfd_get_32 (abfd, contents + int_rel.r_vaddr - sec->vma)
|
---|
2113 | != 0x0411ffff) /* bgezal $0,. == bal . */
|
---|
2114 | continue;
|
---|
2115 |
|
---|
2116 | /* Bother. We need to expand this reloc, and we will need to
|
---|
2117 | make another relaxation pass since this change may put other
|
---|
2118 | relocs out of range. We need to examine the local branches
|
---|
2119 | and we need to allocate memory to hold the offsets we must
|
---|
2120 | add to them. We also need to adjust the values of all
|
---|
2121 | symbols in the object file following this location. */
|
---|
2122 |
|
---|
2123 | sec->_cooked_size += PCREL16_EXPANSION_ADJUSTMENT;
|
---|
2124 | *again = true;
|
---|
2125 |
|
---|
2126 | if (offsets == (long *) NULL)
|
---|
2127 | {
|
---|
2128 | size_t size;
|
---|
2129 |
|
---|
2130 | size = sec->reloc_count * sizeof (long);
|
---|
2131 | offsets = (long *) bfd_alloc (abfd, size);
|
---|
2132 | if (offsets == (long *) NULL)
|
---|
2133 | goto error_return;
|
---|
2134 | memset (offsets, 0, size);
|
---|
2135 | section_tdata->offsets = offsets;
|
---|
2136 | }
|
---|
2137 |
|
---|
2138 | offsets[i] = 1;
|
---|
2139 |
|
---|
2140 | /* Now look for all PC relative references that cross this reloc
|
---|
2141 | and adjust their offsets. */
|
---|
2142 | adj_ext_rel = (struct external_reloc *) section_tdata->external_relocs;
|
---|
2143 | for (adj_i = 0; adj_ext_rel < ext_rel_end; adj_ext_rel++, adj_i++)
|
---|
2144 | {
|
---|
2145 | struct internal_reloc adj_int_rel;
|
---|
2146 | bfd_vma start, stop;
|
---|
2147 | int change;
|
---|
2148 |
|
---|
2149 | mips_ecoff_swap_reloc_in (abfd, (PTR) adj_ext_rel, &adj_int_rel);
|
---|
2150 |
|
---|
2151 | if (adj_int_rel.r_type == MIPS_R_PCREL16)
|
---|
2152 | {
|
---|
2153 | unsigned long insn;
|
---|
2154 |
|
---|
2155 | /* We only care about local references. External ones
|
---|
2156 | will be relocated correctly anyhow. */
|
---|
2157 | if (adj_int_rel.r_extern)
|
---|
2158 | continue;
|
---|
2159 |
|
---|
2160 | /* We are only interested in a PC relative reloc within
|
---|
2161 | this section. FIXME: Cross section PC relative
|
---|
2162 | relocs may not be handled correctly; does anybody
|
---|
2163 | care? */
|
---|
2164 | if (adj_int_rel.r_symndx != RELOC_SECTION_TEXT)
|
---|
2165 | continue;
|
---|
2166 |
|
---|
2167 | start = adj_int_rel.r_vaddr;
|
---|
2168 |
|
---|
2169 | insn = bfd_get_32 (abfd,
|
---|
2170 | contents + adj_int_rel.r_vaddr - sec->vma);
|
---|
2171 |
|
---|
2172 | stop = (insn & 0xffff) << 2;
|
---|
2173 | if ((stop & 0x20000) != 0)
|
---|
2174 | stop -= 0x40000;
|
---|
2175 | stop += adj_int_rel.r_vaddr + 4;
|
---|
2176 | }
|
---|
2177 | else if (adj_int_rel.r_type == MIPS_R_RELHI)
|
---|
2178 | {
|
---|
2179 | struct internal_reloc rello;
|
---|
2180 | long addhi, addlo;
|
---|
2181 |
|
---|
2182 | /* The next reloc must be MIPS_R_RELLO, and we handle
|
---|
2183 | them together. */
|
---|
2184 | BFD_ASSERT (adj_ext_rel + 1 < ext_rel_end);
|
---|
2185 |
|
---|
2186 | mips_ecoff_swap_reloc_in (abfd, (PTR) (adj_ext_rel + 1), &rello);
|
---|
2187 |
|
---|
2188 | BFD_ASSERT (rello.r_type == MIPS_R_RELLO);
|
---|
2189 |
|
---|
2190 | addhi = bfd_get_32 (abfd,
|
---|
2191 | contents + adj_int_rel.r_vaddr - sec->vma);
|
---|
2192 | addhi &= 0xffff;
|
---|
2193 | if (addhi & 0x8000)
|
---|
2194 | addhi -= 0x10000;
|
---|
2195 | addhi <<= 16;
|
---|
2196 |
|
---|
2197 | addlo = bfd_get_32 (abfd, contents + rello.r_vaddr - sec->vma);
|
---|
2198 | addlo &= 0xffff;
|
---|
2199 | if (addlo & 0x8000)
|
---|
2200 | addlo -= 0x10000;
|
---|
2201 |
|
---|
2202 | if (adj_int_rel.r_extern)
|
---|
2203 | {
|
---|
2204 | /* The value we want here is
|
---|
2205 | sym - RELLOaddr + addend
|
---|
2206 | which we can express as
|
---|
2207 | sym - (RELLOaddr - addend)
|
---|
2208 | Therefore if we are expanding the area between
|
---|
2209 | RELLOaddr and RELLOaddr - addend we must adjust
|
---|
2210 | the addend. This is admittedly ambiguous, since
|
---|
2211 | we might mean (sym + addend) - RELLOaddr, but in
|
---|
2212 | practice we don't, and there is no way to handle
|
---|
2213 | that case correctly since at this point we have
|
---|
2214 | no idea whether any reloc is being expanded
|
---|
2215 | between sym and sym + addend. */
|
---|
2216 | start = rello.r_vaddr - (addhi + addlo);
|
---|
2217 | stop = rello.r_vaddr;
|
---|
2218 | }
|
---|
2219 | else
|
---|
2220 | {
|
---|
2221 | /* An internal RELHI/RELLO pair represents the
|
---|
2222 | difference between two addresses, $LC0 - foo.
|
---|
2223 | The symndx value is actually the difference
|
---|
2224 | between the reloc address and $LC0. This lets us
|
---|
2225 | compute $LC0, and, by considering the addend,
|
---|
2226 | foo. If the reloc we are expanding falls between
|
---|
2227 | those two relocs, we must adjust the addend. At
|
---|
2228 | this point, the symndx value is actually in the
|
---|
2229 | r_offset field, where it was put by
|
---|
2230 | mips_ecoff_swap_reloc_in. */
|
---|
2231 | start = rello.r_vaddr - adj_int_rel.r_offset;
|
---|
2232 | stop = start + addhi + addlo;
|
---|
2233 | }
|
---|
2234 | }
|
---|
2235 | else if (adj_int_rel.r_type == MIPS_R_SWITCH)
|
---|
2236 | {
|
---|
2237 | /* A MIPS_R_SWITCH reloc represents a word of the form
|
---|
2238 | .word $L3-$LS12
|
---|
2239 | The value in the object file is correct, assuming the
|
---|
2240 | original value of $L3. The symndx value is actually
|
---|
2241 | the difference between the reloc address and $LS12.
|
---|
2242 | This lets us compute the original value of $LS12 as
|
---|
2243 | vaddr - symndx
|
---|
2244 | and the original value of $L3 as
|
---|
2245 | vaddr - symndx + addend
|
---|
2246 | where addend is the value from the object file. At
|
---|
2247 | this point, the symndx value is actually found in the
|
---|
2248 | r_offset field, since it was moved by
|
---|
2249 | mips_ecoff_swap_reloc_in. */
|
---|
2250 | start = adj_int_rel.r_vaddr - adj_int_rel.r_offset;
|
---|
2251 | stop = start + bfd_get_32 (abfd,
|
---|
2252 | (contents
|
---|
2253 | + adj_int_rel.r_vaddr
|
---|
2254 | - sec->vma));
|
---|
2255 | }
|
---|
2256 | else
|
---|
2257 | continue;
|
---|
2258 |
|
---|
2259 | /* If the range expressed by this reloc, which is the
|
---|
2260 | distance between START and STOP crosses the reloc we are
|
---|
2261 | expanding, we must adjust the offset. The sign of the
|
---|
2262 | adjustment depends upon the direction in which the range
|
---|
2263 | crosses the reloc being expanded. */
|
---|
2264 | if (start <= int_rel.r_vaddr && stop > int_rel.r_vaddr)
|
---|
2265 | change = PCREL16_EXPANSION_ADJUSTMENT;
|
---|
2266 | else if (start > int_rel.r_vaddr && stop <= int_rel.r_vaddr)
|
---|
2267 | change = - PCREL16_EXPANSION_ADJUSTMENT;
|
---|
2268 | else
|
---|
2269 | change = 0;
|
---|
2270 |
|
---|
2271 | offsets[adj_i] += change;
|
---|
2272 |
|
---|
2273 | if (adj_int_rel.r_type == MIPS_R_RELHI)
|
---|
2274 | {
|
---|
2275 | adj_ext_rel++;
|
---|
2276 | adj_i++;
|
---|
2277 | offsets[adj_i] += change;
|
---|
2278 | }
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | /* Find all symbols in this section defined by this object file
|
---|
2282 | and adjust their values. Note that we decide whether to
|
---|
2283 | adjust the value based on the value stored in the ECOFF EXTR
|
---|
2284 | structure, because the value stored in the hash table may
|
---|
2285 | have been changed by an earlier expanded reloc and thus may
|
---|
2286 | no longer correctly indicate whether the symbol is before or
|
---|
2287 | after the expanded reloc. */
|
---|
2288 | ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
|
---|
2289 | adj_h_ptr = ecoff_data (abfd)->sym_hashes;
|
---|
2290 | adj_h_ptr_end = adj_h_ptr + ext_count;
|
---|
2291 | for (; adj_h_ptr < adj_h_ptr_end; adj_h_ptr++)
|
---|
2292 | {
|
---|
2293 | struct ecoff_link_hash_entry *adj_h;
|
---|
2294 |
|
---|
2295 | adj_h = *adj_h_ptr;
|
---|
2296 | if (adj_h != (struct ecoff_link_hash_entry *) NULL
|
---|
2297 | && (adj_h->root.type == bfd_link_hash_defined
|
---|
2298 | || adj_h->root.type == bfd_link_hash_defweak)
|
---|
2299 | && adj_h->root.u.def.section == sec
|
---|
2300 | && adj_h->esym.asym.value > int_rel.r_vaddr)
|
---|
2301 | adj_h->root.u.def.value += PCREL16_EXPANSION_ADJUSTMENT;
|
---|
2302 | }
|
---|
2303 |
|
---|
2304 | /* Add an entry to the symbol value adjust list. This is used
|
---|
2305 | by bfd_ecoff_debug_accumulate to adjust the values of
|
---|
2306 | internal symbols and FDR's. */
|
---|
2307 | adjust = ((struct ecoff_value_adjust *)
|
---|
2308 | bfd_alloc (abfd, sizeof (struct ecoff_value_adjust)));
|
---|
2309 | if (adjust == (struct ecoff_value_adjust *) NULL)
|
---|
2310 | goto error_return;
|
---|
2311 |
|
---|
2312 | adjust->start = int_rel.r_vaddr;
|
---|
2313 | adjust->end = sec->vma + sec->_raw_size;
|
---|
2314 | adjust->adjust = PCREL16_EXPANSION_ADJUSTMENT;
|
---|
2315 |
|
---|
2316 | adjust->next = ecoff_data (abfd)->debug_info.adjust;
|
---|
2317 | ecoff_data (abfd)->debug_info.adjust = adjust;
|
---|
2318 | }
|
---|
2319 |
|
---|
2320 | if (contents != (bfd_byte *) NULL && ! info->keep_memory)
|
---|
2321 | free (contents);
|
---|
2322 |
|
---|
2323 | return true;
|
---|
2324 |
|
---|
2325 | error_return:
|
---|
2326 | if (contents != (bfd_byte *) NULL && ! info->keep_memory)
|
---|
2327 | free (contents);
|
---|
2328 | return false;
|
---|
2329 | }
|
---|
2330 |
|
---|
2331 | /* This routine is called from mips_relocate_section when a PC
|
---|
2332 | relative reloc must be expanded into the five instruction sequence.
|
---|
2333 | It handles all the details of the expansion, including resolving
|
---|
2334 | the reloc. */
|
---|
2335 |
|
---|
2336 | static boolean
|
---|
2337 | mips_relax_pcrel16 (info, input_bfd, input_section, h, location, address)
|
---|
2338 | struct bfd_link_info *info ATTRIBUTE_UNUSED;
|
---|
2339 | bfd *input_bfd;
|
---|
2340 | asection *input_section ATTRIBUTE_UNUSED;
|
---|
2341 | struct ecoff_link_hash_entry *h;
|
---|
2342 | bfd_byte *location;
|
---|
2343 | bfd_vma address;
|
---|
2344 | {
|
---|
2345 | bfd_vma relocation;
|
---|
2346 |
|
---|
2347 | /* 0x0411ffff is bgezal $0,. == bal . */
|
---|
2348 | BFD_ASSERT (bfd_get_32 (input_bfd, location) == 0x0411ffff);
|
---|
2349 |
|
---|
2350 | /* We need to compute the distance between the symbol and the
|
---|
2351 | current address plus eight. */
|
---|
2352 | relocation = (h->root.u.def.value
|
---|
2353 | + h->root.u.def.section->output_section->vma
|
---|
2354 | + h->root.u.def.section->output_offset);
|
---|
2355 | relocation -= address + 8;
|
---|
2356 |
|
---|
2357 | /* If the lower half is negative, increment the upper 16 half. */
|
---|
2358 | if ((relocation & 0x8000) != 0)
|
---|
2359 | relocation += 0x10000;
|
---|
2360 |
|
---|
2361 | bfd_put_32 (input_bfd, 0x04110001, location); /* bal .+8 */
|
---|
2362 | bfd_put_32 (input_bfd,
|
---|
2363 | 0x3c010000 | ((relocation >> 16) & 0xffff), /* lui $at,XX */
|
---|
2364 | location + 4);
|
---|
2365 | bfd_put_32 (input_bfd,
|
---|
2366 | 0x24210000 | (relocation & 0xffff), /* addiu $at,$at,XX */
|
---|
2367 | location + 8);
|
---|
2368 | bfd_put_32 (input_bfd, 0x003f0821, location + 12); /* addu $at,$at,$ra */
|
---|
2369 | bfd_put_32 (input_bfd, 0x0020f809, location + 16); /* jalr $at */
|
---|
2370 |
|
---|
2371 | return true;
|
---|
2372 | }
|
---|
2373 |
|
---|
2374 | /* Given a .sdata section and a .rel.sdata in-memory section, store
|
---|
2375 | relocation information into the .rel.sdata section which can be
|
---|
2376 | used at runtime to relocate the section. This is called by the
|
---|
2377 | linker when the --embedded-relocs switch is used. This is called
|
---|
2378 | after the add_symbols entry point has been called for all the
|
---|
2379 | objects, and before the final_link entry point is called. This
|
---|
2380 | function presumes that the object was compiled using
|
---|
2381 | -membedded-pic. */
|
---|
2382 |
|
---|
2383 | boolean
|
---|
2384 | bfd_mips_ecoff_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
|
---|
2385 | bfd *abfd;
|
---|
2386 | struct bfd_link_info *info;
|
---|
2387 | asection *datasec;
|
---|
2388 | asection *relsec;
|
---|
2389 | char **errmsg;
|
---|
2390 | {
|
---|
2391 | struct ecoff_link_hash_entry **sym_hashes;
|
---|
2392 | struct ecoff_section_tdata *section_tdata;
|
---|
2393 | struct external_reloc *ext_rel;
|
---|
2394 | struct external_reloc *ext_rel_end;
|
---|
2395 | bfd_byte *p;
|
---|
2396 |
|
---|
2397 | BFD_ASSERT (! info->relocateable);
|
---|
2398 |
|
---|
2399 | *errmsg = NULL;
|
---|
2400 |
|
---|
2401 | if (datasec->reloc_count == 0)
|
---|
2402 | return true;
|
---|
2403 |
|
---|
2404 | sym_hashes = ecoff_data (abfd)->sym_hashes;
|
---|
2405 |
|
---|
2406 | if (! mips_read_relocs (abfd, datasec))
|
---|
2407 | return false;
|
---|
2408 |
|
---|
2409 | relsec->contents = (bfd_byte *) bfd_alloc (abfd, datasec->reloc_count * 4);
|
---|
2410 | if (relsec->contents == NULL)
|
---|
2411 | return false;
|
---|
2412 |
|
---|
2413 | p = relsec->contents;
|
---|
2414 |
|
---|
2415 | section_tdata = ecoff_section_data (abfd, datasec);
|
---|
2416 | ext_rel = (struct external_reloc *) section_tdata->external_relocs;
|
---|
2417 | ext_rel_end = ext_rel + datasec->reloc_count;
|
---|
2418 | for (; ext_rel < ext_rel_end; ext_rel++, p += 4)
|
---|
2419 | {
|
---|
2420 | struct internal_reloc int_rel;
|
---|
2421 | boolean text_relative;
|
---|
2422 |
|
---|
2423 | mips_ecoff_swap_reloc_in (abfd, (PTR) ext_rel, &int_rel);
|
---|
2424 |
|
---|
2425 | /* We are going to write a four byte word into the runtime reloc
|
---|
2426 | section. The word will be the address in the data section
|
---|
2427 | which must be relocated. This must be on a word boundary,
|
---|
2428 | which means the lower two bits must be zero. We use the
|
---|
2429 | least significant bit to indicate how the value in the data
|
---|
2430 | section must be relocated. A 0 means that the value is
|
---|
2431 | relative to the text section, while a 1 indicates that the
|
---|
2432 | value is relative to the data section. Given that we are
|
---|
2433 | assuming the code was compiled using -membedded-pic, there
|
---|
2434 | should not be any other possibilities. */
|
---|
2435 |
|
---|
2436 | /* We can only relocate REFWORD relocs at run time. */
|
---|
2437 | if (int_rel.r_type != MIPS_R_REFWORD)
|
---|
2438 | {
|
---|
2439 | *errmsg = _("unsupported reloc type");
|
---|
2440 | bfd_set_error (bfd_error_bad_value);
|
---|
2441 | return false;
|
---|
2442 | }
|
---|
2443 |
|
---|
2444 | if (int_rel.r_extern)
|
---|
2445 | {
|
---|
2446 | struct ecoff_link_hash_entry *h;
|
---|
2447 |
|
---|
2448 | h = sym_hashes[int_rel.r_symndx];
|
---|
2449 | /* If h is NULL, that means that there is a reloc against an
|
---|
2450 | external symbol which we thought was just a debugging
|
---|
2451 | symbol. This should not happen. */
|
---|
2452 | if (h == (struct ecoff_link_hash_entry *) NULL)
|
---|
2453 | abort ();
|
---|
2454 | if ((h->root.type == bfd_link_hash_defined
|
---|
2455 | || h->root.type == bfd_link_hash_defweak)
|
---|
2456 | && (h->root.u.def.section->flags & SEC_CODE) != 0)
|
---|
2457 | text_relative = true;
|
---|
2458 | else
|
---|
2459 | text_relative = false;
|
---|
2460 | }
|
---|
2461 | else
|
---|
2462 | {
|
---|
2463 | switch (int_rel.r_symndx)
|
---|
2464 | {
|
---|
2465 | case RELOC_SECTION_TEXT:
|
---|
2466 | text_relative = true;
|
---|
2467 | break;
|
---|
2468 | case RELOC_SECTION_SDATA:
|
---|
2469 | case RELOC_SECTION_SBSS:
|
---|
2470 | case RELOC_SECTION_LIT8:
|
---|
2471 | text_relative = false;
|
---|
2472 | break;
|
---|
2473 | default:
|
---|
2474 | /* No other sections should appear in -membedded-pic
|
---|
2475 | code. */
|
---|
2476 | *errmsg = _("reloc against unsupported section");
|
---|
2477 | bfd_set_error (bfd_error_bad_value);
|
---|
2478 | return false;
|
---|
2479 | }
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | if ((int_rel.r_offset & 3) != 0)
|
---|
2483 | {
|
---|
2484 | *errmsg = _("reloc not properly aligned");
|
---|
2485 | bfd_set_error (bfd_error_bad_value);
|
---|
2486 | return false;
|
---|
2487 | }
|
---|
2488 |
|
---|
2489 | bfd_put_32 (abfd,
|
---|
2490 | (int_rel.r_vaddr - datasec->vma + datasec->output_offset
|
---|
2491 | + (text_relative ? 0 : 1)),
|
---|
2492 | p);
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | return true;
|
---|
2496 | }
|
---|
2497 | |
---|
2498 |
|
---|
2499 | /* This is the ECOFF backend structure. The backend field of the
|
---|
2500 | target vector points to this. */
|
---|
2501 |
|
---|
2502 | static const struct ecoff_backend_data mips_ecoff_backend_data =
|
---|
2503 | {
|
---|
2504 | /* COFF backend structure. */
|
---|
2505 | {
|
---|
2506 | (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */
|
---|
2507 | (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
|
---|
2508 | (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
|
---|
2509 | (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/
|
---|
2510 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
|
---|
2511 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
|
---|
2512 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */
|
---|
2513 | mips_ecoff_swap_filehdr_out, mips_ecoff_swap_aouthdr_out,
|
---|
2514 | mips_ecoff_swap_scnhdr_out,
|
---|
2515 | FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, true, false, 4, false, 2,
|
---|
2516 | mips_ecoff_swap_filehdr_in, mips_ecoff_swap_aouthdr_in,
|
---|
2517 | mips_ecoff_swap_scnhdr_in, NULL,
|
---|
2518 | mips_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
|
---|
2519 | _bfd_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
|
---|
2520 | _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
|
---|
2521 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
2522 | NULL, NULL
|
---|
2523 | },
|
---|
2524 | /* Supported architecture. */
|
---|
2525 | bfd_arch_mips,
|
---|
2526 | /* Initial portion of armap string. */
|
---|
2527 | "__________",
|
---|
2528 | /* The page boundary used to align sections in a demand-paged
|
---|
2529 | executable file. E.g., 0x1000. */
|
---|
2530 | 0x1000,
|
---|
2531 | /* True if the .rdata section is part of the text segment, as on the
|
---|
2532 | Alpha. False if .rdata is part of the data segment, as on the
|
---|
2533 | MIPS. */
|
---|
2534 | false,
|
---|
2535 | /* Bitsize of constructor entries. */
|
---|
2536 | 32,
|
---|
2537 | /* Reloc to use for constructor entries. */
|
---|
2538 | &mips_howto_table[MIPS_R_REFWORD],
|
---|
2539 | {
|
---|
2540 | /* Symbol table magic number. */
|
---|
2541 | magicSym,
|
---|
2542 | /* Alignment of debugging information. E.g., 4. */
|
---|
2543 | 4,
|
---|
2544 | /* Sizes of external symbolic information. */
|
---|
2545 | sizeof (struct hdr_ext),
|
---|
2546 | sizeof (struct dnr_ext),
|
---|
2547 | sizeof (struct pdr_ext),
|
---|
2548 | sizeof (struct sym_ext),
|
---|
2549 | sizeof (struct opt_ext),
|
---|
2550 | sizeof (struct fdr_ext),
|
---|
2551 | sizeof (struct rfd_ext),
|
---|
2552 | sizeof (struct ext_ext),
|
---|
2553 | /* Functions to swap in external symbolic data. */
|
---|
2554 | ecoff_swap_hdr_in,
|
---|
2555 | ecoff_swap_dnr_in,
|
---|
2556 | ecoff_swap_pdr_in,
|
---|
2557 | ecoff_swap_sym_in,
|
---|
2558 | ecoff_swap_opt_in,
|
---|
2559 | ecoff_swap_fdr_in,
|
---|
2560 | ecoff_swap_rfd_in,
|
---|
2561 | ecoff_swap_ext_in,
|
---|
2562 | _bfd_ecoff_swap_tir_in,
|
---|
2563 | _bfd_ecoff_swap_rndx_in,
|
---|
2564 | /* Functions to swap out external symbolic data. */
|
---|
2565 | ecoff_swap_hdr_out,
|
---|
2566 | ecoff_swap_dnr_out,
|
---|
2567 | ecoff_swap_pdr_out,
|
---|
2568 | ecoff_swap_sym_out,
|
---|
2569 | ecoff_swap_opt_out,
|
---|
2570 | ecoff_swap_fdr_out,
|
---|
2571 | ecoff_swap_rfd_out,
|
---|
2572 | ecoff_swap_ext_out,
|
---|
2573 | _bfd_ecoff_swap_tir_out,
|
---|
2574 | _bfd_ecoff_swap_rndx_out,
|
---|
2575 | /* Function to read in symbolic data. */
|
---|
2576 | _bfd_ecoff_slurp_symbolic_info
|
---|
2577 | },
|
---|
2578 | /* External reloc size. */
|
---|
2579 | RELSZ,
|
---|
2580 | /* Reloc swapping functions. */
|
---|
2581 | mips_ecoff_swap_reloc_in,
|
---|
2582 | mips_ecoff_swap_reloc_out,
|
---|
2583 | /* Backend reloc tweaking. */
|
---|
2584 | mips_adjust_reloc_in,
|
---|
2585 | mips_adjust_reloc_out,
|
---|
2586 | /* Relocate section contents while linking. */
|
---|
2587 | mips_relocate_section,
|
---|
2588 | /* Do final adjustments to filehdr and aouthdr. */
|
---|
2589 | NULL,
|
---|
2590 | /* Read an element from an archive at a given file position. */
|
---|
2591 | _bfd_get_elt_at_filepos
|
---|
2592 | };
|
---|
2593 |
|
---|
2594 | /* Looking up a reloc type is MIPS specific. */
|
---|
2595 | #define _bfd_ecoff_bfd_reloc_type_lookup mips_bfd_reloc_type_lookup
|
---|
2596 |
|
---|
2597 | /* Getting relocated section contents is generic. */
|
---|
2598 | #define _bfd_ecoff_bfd_get_relocated_section_contents \
|
---|
2599 | bfd_generic_get_relocated_section_contents
|
---|
2600 |
|
---|
2601 | /* Handling file windows is generic. */
|
---|
2602 | #define _bfd_ecoff_get_section_contents_in_window \
|
---|
2603 | _bfd_generic_get_section_contents_in_window
|
---|
2604 |
|
---|
2605 | /* Relaxing sections is MIPS specific. */
|
---|
2606 | #define _bfd_ecoff_bfd_relax_section mips_relax_section
|
---|
2607 |
|
---|
2608 | /* GC of sections is not done. */
|
---|
2609 | #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
|
---|
2610 |
|
---|
2611 | extern const bfd_target ecoff_big_vec;
|
---|
2612 |
|
---|
2613 | const bfd_target ecoff_little_vec =
|
---|
2614 | {
|
---|
2615 | "ecoff-littlemips", /* name */
|
---|
2616 | bfd_target_ecoff_flavour,
|
---|
2617 | BFD_ENDIAN_LITTLE, /* data byte order is little */
|
---|
2618 | BFD_ENDIAN_LITTLE, /* header byte order is little */
|
---|
2619 |
|
---|
2620 | (HAS_RELOC | EXEC_P | /* object flags */
|
---|
2621 | HAS_LINENO | HAS_DEBUG |
|
---|
2622 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
|
---|
2623 |
|
---|
2624 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
|
---|
2625 | 0, /* leading underscore */
|
---|
2626 | ' ', /* ar_pad_char */
|
---|
2627 | 15, /* ar_max_namelen */
|
---|
2628 | bfd_getl64, bfd_getl_signed_64, bfd_putl64,
|
---|
2629 | bfd_getl32, bfd_getl_signed_32, bfd_putl32,
|
---|
2630 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
|
---|
2631 | bfd_getl64, bfd_getl_signed_64, bfd_putl64,
|
---|
2632 | bfd_getl32, bfd_getl_signed_32, bfd_putl32,
|
---|
2633 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
|
---|
2634 |
|
---|
2635 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
|
---|
2636 | _bfd_ecoff_archive_p, _bfd_dummy_target},
|
---|
2637 | {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
|
---|
2638 | _bfd_generic_mkarchive, bfd_false},
|
---|
2639 | {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
|
---|
2640 | _bfd_write_archive_contents, bfd_false},
|
---|
2641 |
|
---|
2642 | BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
|
---|
2643 | BFD_JUMP_TABLE_COPY (_bfd_ecoff),
|
---|
2644 | BFD_JUMP_TABLE_CORE (_bfd_nocore),
|
---|
2645 | BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
|
---|
2646 | BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
|
---|
2647 | BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
|
---|
2648 | BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
|
---|
2649 | BFD_JUMP_TABLE_LINK (_bfd_ecoff),
|
---|
2650 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
|
---|
2651 |
|
---|
2652 | & ecoff_big_vec,
|
---|
2653 |
|
---|
2654 | (PTR) &mips_ecoff_backend_data
|
---|
2655 | };
|
---|
2656 |
|
---|
2657 | const bfd_target ecoff_big_vec =
|
---|
2658 | {
|
---|
2659 | "ecoff-bigmips", /* name */
|
---|
2660 | bfd_target_ecoff_flavour,
|
---|
2661 | BFD_ENDIAN_BIG, /* data byte order is big */
|
---|
2662 | BFD_ENDIAN_BIG, /* header byte order is big */
|
---|
2663 |
|
---|
2664 | (HAS_RELOC | EXEC_P | /* object flags */
|
---|
2665 | HAS_LINENO | HAS_DEBUG |
|
---|
2666 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
|
---|
2667 |
|
---|
2668 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
|
---|
2669 | 0, /* leading underscore */
|
---|
2670 | ' ', /* ar_pad_char */
|
---|
2671 | 15, /* ar_max_namelen */
|
---|
2672 | bfd_getb64, bfd_getb_signed_64, bfd_putb64,
|
---|
2673 | bfd_getb32, bfd_getb_signed_32, bfd_putb32,
|
---|
2674 | bfd_getb16, bfd_getb_signed_16, bfd_putb16,
|
---|
2675 | bfd_getb64, bfd_getb_signed_64, bfd_putb64,
|
---|
2676 | bfd_getb32, bfd_getb_signed_32, bfd_putb32,
|
---|
2677 | bfd_getb16, bfd_getb_signed_16, bfd_putb16,
|
---|
2678 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
|
---|
2679 | _bfd_ecoff_archive_p, _bfd_dummy_target},
|
---|
2680 | {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
|
---|
2681 | _bfd_generic_mkarchive, bfd_false},
|
---|
2682 | {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
|
---|
2683 | _bfd_write_archive_contents, bfd_false},
|
---|
2684 |
|
---|
2685 | BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
|
---|
2686 | BFD_JUMP_TABLE_COPY (_bfd_ecoff),
|
---|
2687 | BFD_JUMP_TABLE_CORE (_bfd_nocore),
|
---|
2688 | BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
|
---|
2689 | BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
|
---|
2690 | BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
|
---|
2691 | BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
|
---|
2692 | BFD_JUMP_TABLE_LINK (_bfd_ecoff),
|
---|
2693 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
|
---|
2694 |
|
---|
2695 | & ecoff_little_vec,
|
---|
2696 |
|
---|
2697 | (PTR) &mips_ecoff_backend_data
|
---|
2698 | };
|
---|
2699 |
|
---|
2700 | const bfd_target ecoff_biglittle_vec =
|
---|
2701 | {
|
---|
2702 | "ecoff-biglittlemips", /* name */
|
---|
2703 | bfd_target_ecoff_flavour,
|
---|
2704 | BFD_ENDIAN_LITTLE, /* data byte order is little */
|
---|
2705 | BFD_ENDIAN_BIG, /* header byte order is big */
|
---|
2706 |
|
---|
2707 | (HAS_RELOC | EXEC_P | /* object flags */
|
---|
2708 | HAS_LINENO | HAS_DEBUG |
|
---|
2709 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
|
---|
2710 |
|
---|
2711 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
|
---|
2712 | 0, /* leading underscore */
|
---|
2713 | ' ', /* ar_pad_char */
|
---|
2714 | 15, /* ar_max_namelen */
|
---|
2715 | bfd_getl64, bfd_getl_signed_64, bfd_putl64,
|
---|
2716 | bfd_getl32, bfd_getl_signed_32, bfd_putl32,
|
---|
2717 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
|
---|
2718 | bfd_getb64, bfd_getb_signed_64, bfd_putb64,
|
---|
2719 | bfd_getb32, bfd_getb_signed_32, bfd_putb32,
|
---|
2720 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
|
---|
2721 |
|
---|
2722 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
|
---|
2723 | _bfd_ecoff_archive_p, _bfd_dummy_target},
|
---|
2724 | {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
|
---|
2725 | _bfd_generic_mkarchive, bfd_false},
|
---|
2726 | {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
|
---|
2727 | _bfd_write_archive_contents, bfd_false},
|
---|
2728 |
|
---|
2729 | BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
|
---|
2730 | BFD_JUMP_TABLE_COPY (_bfd_ecoff),
|
---|
2731 | BFD_JUMP_TABLE_CORE (_bfd_nocore),
|
---|
2732 | BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
|
---|
2733 | BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
|
---|
2734 | BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
|
---|
2735 | BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
|
---|
2736 | BFD_JUMP_TABLE_LINK (_bfd_ecoff),
|
---|
2737 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
|
---|
2738 |
|
---|
2739 | NULL,
|
---|
2740 |
|
---|
2741 | (PTR) &mips_ecoff_backend_data
|
---|
2742 | };
|
---|