Commit | Line | Data |
---|---|---|
17505c5c | 1 | /* BFD back-end for MIPS PE COFF files. |
4b95cf5c | 2 | Copyright (C) 1990-2014 Free Software Foundation, Inc. |
17505c5c NC |
3 | Modified from coff-i386.c by DJ Delorie, dj@cygnus.com |
4 | ||
7920ce38 | 5 | This file is part of BFD, the Binary File Descriptor library. |
17505c5c | 6 | |
7920ce38 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
7920ce38 | 10 | (at your option) any later version. |
17505c5c | 11 | |
7920ce38 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
17505c5c | 16 | |
7920ce38 NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
17505c5c NC |
21 | |
22 | #define COFF_WITH_PE | |
88183869 DK |
23 | /* pei-mips.c may have defined this to default off (0) before |
24 | including this file, so don't redefine if that's the case. | |
25 | Otherwise we're generating objects, not executable images, | |
26 | so we want to define it to default on. */ | |
27 | #ifndef COFF_LONG_SECTION_NAMES | |
17505c5c | 28 | #define COFF_LONG_SECTION_NAMES |
88183869 | 29 | #endif /* COFF_LONG_SECTION_NAMES */ |
b34976b6 | 30 | #define PCRELOFFSET TRUE |
17505c5c | 31 | |
17505c5c | 32 | #include "sysdep.h" |
3db64b00 | 33 | #include "bfd.h" |
17505c5c | 34 | #include "libbfd.h" |
17505c5c | 35 | #include "coff/mipspe.h" |
17505c5c | 36 | #include "coff/internal.h" |
17505c5c | 37 | #include "coff/pe.h" |
17505c5c NC |
38 | #include "libcoff.h" |
39 | ||
7920ce38 | 40 | #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2 |
17505c5c NC |
41 | /* The page size is a guess based on ELF. */ |
42 | ||
43 | #define COFF_PAGE_SIZE 0x1000 | |
44 | ||
45 | /* For some reason when using mips COFF the value stored in the .text | |
46 | section for a reference to a common symbol is the value itself plus | |
47 | any desired offset. Ian Taylor, Cygnus Support. */ | |
48 | ||
1049f94e | 49 | /* If we are producing relocatable output, we need to do some |
17505c5c NC |
50 | adjustments to the object file that are not done by the |
51 | bfd_perform_relocation function. This function is called by every | |
52 | reloc type to make any required adjustments. */ | |
53 | ||
54 | static bfd_reloc_status_type | |
7920ce38 NC |
55 | coff_mips_reloc (bfd *abfd, |
56 | arelent *reloc_entry, | |
57 | asymbol *symbol, | |
58 | void * data, | |
59 | asection *input_section ATTRIBUTE_UNUSED, | |
60 | bfd *output_bfd, | |
61 | char **error_message ATTRIBUTE_UNUSED) | |
17505c5c NC |
62 | { |
63 | symvalue diff; | |
64 | ||
7920ce38 | 65 | if (output_bfd == NULL) |
17505c5c NC |
66 | return bfd_reloc_continue; |
67 | ||
68 | if (bfd_is_com_section (symbol->section)) | |
69 | { | |
70 | #ifndef COFF_WITH_PE | |
71 | /* We are relocating a common symbol. The current value in the | |
72 | object file is ORIG + OFFSET, where ORIG is the value of the | |
73 | common symbol as seen by the object file when it was compiled | |
74 | (this may be zero if the symbol was undefined) and OFFSET is | |
75 | the offset into the common symbol (normally zero, but may be | |
76 | non-zero when referring to a field in a common structure). | |
77 | ORIG is the negative of reloc_entry->addend, which is set by | |
78 | the CALC_ADDEND macro below. We want to replace the value in | |
79 | the object file with NEW + OFFSET, where NEW is the value of | |
80 | the common symbol which we are going to put in the final | |
81 | object file. NEW is symbol->value. */ | |
82 | diff = symbol->value + reloc_entry->addend; | |
83 | #else | |
84 | /* In PE mode, we do not offset the common symbol. */ | |
85 | diff = reloc_entry->addend; | |
86 | #endif | |
87 | } | |
88 | else | |
7920ce38 NC |
89 | /* For some reason bfd_perform_relocation always effectively |
90 | ignores the addend for a COFF target when producing | |
91 | relocatable output. This seems to be always wrong for 386 | |
92 | COFF, so we handle the addend here instead. */ | |
93 | diff = reloc_entry->addend; | |
17505c5c | 94 | |
17505c5c NC |
95 | #define DOIT(x) \ |
96 | x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + (diff >> howto->rightshift)) & howto->dst_mask)) | |
97 | ||
98 | if (diff != 0) | |
99 | { | |
100 | reloc_howto_type *howto = reloc_entry->howto; | |
101 | unsigned char *addr = (unsigned char *) data + reloc_entry->address; | |
102 | ||
103 | switch (howto->size) | |
104 | { | |
105 | case 0: | |
106 | { | |
107 | char x = bfd_get_8 (abfd, addr); | |
7920ce38 | 108 | |
17505c5c NC |
109 | DOIT (x); |
110 | bfd_put_8 (abfd, x, addr); | |
111 | } | |
112 | break; | |
113 | ||
114 | case 1: | |
115 | { | |
116 | short x = bfd_get_16 (abfd, addr); | |
7920ce38 | 117 | |
17505c5c | 118 | DOIT (x); |
dc810e39 | 119 | bfd_put_16 (abfd, (bfd_vma) x, addr); |
17505c5c NC |
120 | } |
121 | break; | |
122 | ||
123 | case 2: | |
124 | { | |
125 | long x = bfd_get_32 (abfd, addr); | |
7920ce38 | 126 | |
17505c5c | 127 | DOIT (x); |
dc810e39 | 128 | bfd_put_32 (abfd, (bfd_vma) x, addr); |
17505c5c NC |
129 | } |
130 | break; | |
131 | ||
132 | default: | |
133 | abort (); | |
134 | } | |
135 | } | |
136 | ||
137 | /* Now let bfd_perform_relocation finish everything up. */ | |
138 | return bfd_reloc_continue; | |
139 | } | |
140 | ||
141 | #ifdef COFF_WITH_PE | |
b34976b6 | 142 | /* Return TRUE if this relocation should |
892339ee | 143 | appear in the output .reloc section. */ |
17505c5c | 144 | |
b34976b6 | 145 | static bfd_boolean |
7920ce38 | 146 | in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, reloc_howto_type *howto) |
17505c5c NC |
147 | { |
148 | return ! howto->pc_relative && howto->type != MIPS_R_RVA; | |
892339ee | 149 | } |
17505c5c NC |
150 | #endif |
151 | ||
152 | #ifndef PCRELOFFSET | |
b34976b6 | 153 | #define PCRELOFFSET FALSE |
17505c5c NC |
154 | #endif |
155 | ||
892339ee | 156 | static reloc_howto_type howto_table[] = |
17505c5c NC |
157 | { |
158 | /* Reloc type 0 is ignored. The reloc reading code ensures that | |
159 | this is a reference to the .abs section, which will cause | |
160 | bfd_perform_relocation to do nothing. */ | |
7920ce38 NC |
161 | HOWTO (MIPS_R_ABSOLUTE, /* Type. */ |
162 | 0, /* Rightshift. */ | |
163 | 0, /* Size (0 = byte, 1 = short, 2 = long). */ | |
164 | 8, /* Bitsize. */ | |
165 | FALSE, /* PC_relative. */ | |
166 | 0, /* Bitpos. */ | |
167 | complain_overflow_dont, /* Complain_on_overflow. */ | |
168 | 0, /* Special_function. */ | |
169 | "IGNORE", /* Name. */ | |
170 | FALSE, /* Partial_inplace. */ | |
171 | 0, /* Src_mask. */ | |
172 | 0, /* Dst_mask. */ | |
173 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
174 | |
175 | /* A 16 bit reference to a symbol, normally from a data section. */ | |
7920ce38 NC |
176 | HOWTO (MIPS_R_REFHALF, /* Type. */ |
177 | 0, /* Rightshift. */ | |
178 | 1, /* Size (0 = byte, 1 = short, 2 = long). */ | |
179 | 16, /* Bitsize. */ | |
180 | FALSE, /* PC_relative. */ | |
181 | 0, /* Bitpos. */ | |
182 | complain_overflow_bitfield, /* Complain_on_overflow. */ | |
183 | coff_mips_reloc, /* Special_function. */ | |
184 | "REFHALF", /* Name. */ | |
185 | TRUE, /* Partial_inplace. */ | |
186 | 0xffff, /* Src_mask. */ | |
187 | 0xffff, /* Dst_mask. */ | |
188 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
189 | |
190 | /* A 32 bit reference to a symbol, normally from a data section. */ | |
7920ce38 NC |
191 | HOWTO (MIPS_R_REFWORD, /* Type. */ |
192 | 0, /* Rightshift. */ | |
193 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
194 | 32, /* Bitsize. */ | |
195 | FALSE, /* PC_relative. */ | |
196 | 0, /* Bitpos. */ | |
197 | complain_overflow_bitfield, /* Complain_on_overflow. */ | |
198 | coff_mips_reloc, /* Special_function. */ | |
199 | "REFWORD", /* Name. */ | |
200 | TRUE, /* Partial_inplace. */ | |
201 | 0xffffffff, /* Src_mask. */ | |
202 | 0xffffffff, /* Dst_mask. */ | |
203 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
204 | |
205 | /* A 26 bit absolute jump address. */ | |
7920ce38 NC |
206 | HOWTO (MIPS_R_JMPADDR, /* Type. */ |
207 | 2, /* Rightshift. */ | |
208 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
209 | 26, /* Bitsize. */ | |
210 | FALSE, /* PC_relative. */ | |
211 | 0, /* Bitpos. */ | |
212 | complain_overflow_dont, /* Complain_on_overflow. */ | |
17505c5c NC |
213 | /* This needs complex overflow |
214 | detection, because the upper four | |
215 | bits must match the PC. */ | |
7920ce38 NC |
216 | coff_mips_reloc, /* Special_function. */ |
217 | "JMPADDR", /* Name. */ | |
218 | TRUE, /* Partial_inplace. */ | |
219 | 0x3ffffff, /* Src_mask. */ | |
220 | 0x3ffffff, /* Dst_mask. */ | |
221 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
222 | |
223 | /* The high 16 bits of a symbol value. Handled by the function | |
224 | mips_refhi_reloc. */ | |
7920ce38 NC |
225 | HOWTO (MIPS_R_REFHI, /* Type. */ |
226 | 16, /* Rightshift. */ | |
227 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
228 | 16, /* Bitsize. */ | |
229 | FALSE, /* PC_relative. */ | |
230 | 0, /* Bitpos. */ | |
231 | complain_overflow_bitfield, /* Complain_on_overflow. */ | |
232 | coff_mips_reloc, /* Special_function. */ | |
233 | "REFHI", /* Name. */ | |
234 | TRUE, /* Partial_inplace. */ | |
235 | 0xffff, /* Src_mask. */ | |
236 | 0xffff, /* Dst_mask. */ | |
237 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
238 | |
239 | /* The low 16 bits of a symbol value. */ | |
7920ce38 NC |
240 | HOWTO (MIPS_R_REFLO, /* Type. */ |
241 | 0, /* Rightshift. */ | |
242 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
243 | 16, /* Bitsize. */ | |
244 | FALSE, /* PC_relative. */ | |
245 | 0, /* Bitpos. */ | |
246 | complain_overflow_dont, /* Complain_on_overflow. */ | |
247 | coff_mips_reloc, /* Special_function. */ | |
248 | "REFLO", /* Name. */ | |
249 | TRUE, /* Partial_inplace. */ | |
250 | 0xffff, /* Src_mask. */ | |
251 | 0xffff, /* Dst_mask. */ | |
252 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
253 | |
254 | /* A reference to an offset from the gp register. Handled by the | |
255 | function mips_gprel_reloc. */ | |
7920ce38 NC |
256 | HOWTO (MIPS_R_GPREL, /* Type. */ |
257 | 0, /* Rightshift. */ | |
258 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
259 | 16, /* Bitsize. */ | |
260 | FALSE, /* PC_relative. */ | |
261 | 0, /* Bitpos. */ | |
262 | complain_overflow_signed, /* Complain_on_overflow. */ | |
263 | coff_mips_reloc, /* Special_function. */ | |
264 | "GPREL", /* Name. */ | |
265 | TRUE, /* Partial_inplace. */ | |
266 | 0xffff, /* Src_mask. */ | |
267 | 0xffff, /* Dst_mask. */ | |
268 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
269 | |
270 | /* A reference to a literal using an offset from the gp register. | |
271 | Handled by the function mips_gprel_reloc. */ | |
7920ce38 NC |
272 | HOWTO (MIPS_R_LITERAL, /* Type. */ |
273 | 0, /* Rightshift. */ | |
274 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
275 | 16, /* Bitsize. */ | |
276 | FALSE, /* PC_relative. */ | |
277 | 0, /* Bitpos. */ | |
278 | complain_overflow_signed, /* Complain_on_overflow. */ | |
279 | coff_mips_reloc, /* Special_function. */ | |
280 | "LITERAL", /* Name. */ | |
281 | TRUE, /* Partial_inplace. */ | |
282 | 0xffff, /* Src_mask. */ | |
283 | 0xffff, /* Dst_mask. */ | |
284 | FALSE), /* Pcrel_offset. */ | |
17505c5c | 285 | |
86033394 NC |
286 | EMPTY_HOWTO (8), |
287 | EMPTY_HOWTO (9), | |
288 | EMPTY_HOWTO (10), | |
289 | EMPTY_HOWTO (11), | |
290 | EMPTY_HOWTO (12), | |
291 | EMPTY_HOWTO (13), | |
292 | EMPTY_HOWTO (14), | |
293 | EMPTY_HOWTO (15), | |
294 | EMPTY_HOWTO (16), | |
295 | EMPTY_HOWTO (17), | |
296 | EMPTY_HOWTO (18), | |
297 | EMPTY_HOWTO (19), | |
298 | EMPTY_HOWTO (20), | |
299 | EMPTY_HOWTO (21), | |
300 | EMPTY_HOWTO (22), | |
301 | EMPTY_HOWTO (23), | |
302 | EMPTY_HOWTO (24), | |
303 | EMPTY_HOWTO (25), | |
304 | EMPTY_HOWTO (26), | |
305 | EMPTY_HOWTO (27), | |
306 | EMPTY_HOWTO (28), | |
307 | EMPTY_HOWTO (29), | |
308 | EMPTY_HOWTO (30), | |
309 | EMPTY_HOWTO (31), | |
310 | EMPTY_HOWTO (32), | |
311 | EMPTY_HOWTO (33), | |
7920ce38 NC |
312 | HOWTO (MIPS_R_RVA, /* Type. */ |
313 | 0, /* Rightshift. */ | |
314 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
315 | 32, /* Bitsize. */ | |
316 | FALSE, /* PC_relative. */ | |
317 | 0, /* Bitpos. */ | |
318 | complain_overflow_bitfield, /* Complain_on_overflow. */ | |
319 | coff_mips_reloc, /* Special_function. */ | |
320 | "rva32", /* Name. */ | |
321 | TRUE, /* Partial_inplace. */ | |
322 | 0xffffffff, /* Src_mask. */ | |
323 | 0xffffffff, /* Dst_mask. */ | |
324 | FALSE), /* Pcrel_offset. */ | |
86033394 NC |
325 | EMPTY_HOWTO (35), |
326 | EMPTY_HOWTO (36), | |
7920ce38 NC |
327 | HOWTO (MIPS_R_PAIR, /* Type. */ |
328 | 0, /* Rightshift. */ | |
329 | 2, /* Size (0 = byte, 1 = short, 2 = long). */ | |
330 | 32, /* Bitsize. */ | |
331 | FALSE, /* PC_relative. */ | |
332 | 0, /* Bitpos. */ | |
333 | complain_overflow_bitfield, /* Complain_on_overflow. */ | |
334 | coff_mips_reloc, /* Special_function. */ | |
335 | "PAIR", /* Name. */ | |
336 | TRUE, /* Partial_inplace. */ | |
337 | 0xffffffff, /* Src_mask. */ | |
338 | 0xffffffff, /* Dst_mask. */ | |
339 | FALSE), /* Pcrel_offset. */ | |
17505c5c NC |
340 | }; |
341 | ||
7920ce38 NC |
342 | /* Turn a howto into a reloc nunmber. */ |
343 | ||
344 | #define SELECT_RELOC(x, howto) { x.r_type = howto->type; } | |
345 | #define BADMAG(x) MIPSBADMAG (x) | |
17505c5c | 346 | |
7920ce38 NC |
347 | /* Customize coffcode.h. */ |
348 | #define MIPS 1 | |
17505c5c NC |
349 | |
350 | #define RTYPE2HOWTO(cache_ptr, dst) \ | |
351 | (cache_ptr)->howto = howto_table + (dst)->r_type; | |
352 | ||
353 | /* Compute the addend of a reloc. If the reloc is to a common symbol, | |
354 | the object file contains the value of the common symbol. By the | |
355 | time this is called, the linker may be using a different symbol | |
356 | from a different object file with a different value. Therefore, we | |
357 | hack wildly to locate the original symbol from this file so that we | |
358 | can make the correct adjustment. This macro sets coffsym to the | |
359 | symbol from the original file, and uses it to set the addend value | |
360 | correctly. If this is not a common symbol, the usual addend | |
361 | calculation is done, except that an additional tweak is needed for | |
362 | PC relative relocs. | |
363 | FIXME: This macro refers to symbols and asect; these are from the | |
364 | calling function, not the macro arguments. */ | |
365 | ||
366 | #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \ | |
367 | { \ | |
7920ce38 | 368 | coff_symbol_type *coffsym = NULL; \ |
17505c5c NC |
369 | if (ptr && bfd_asymbol_bfd (ptr) != abfd) \ |
370 | coffsym = (obj_symbols (abfd) \ | |
371 | + (cache_ptr->sym_ptr_ptr - symbols)); \ | |
372 | else if (ptr) \ | |
373 | coffsym = coff_symbol_from (abfd, ptr); \ | |
7920ce38 | 374 | if (coffsym != NULL \ |
17505c5c NC |
375 | && coffsym->native->u.syment.n_scnum == 0) \ |
376 | cache_ptr->addend = - coffsym->native->u.syment.n_value; \ | |
377 | else if (ptr && bfd_asymbol_bfd (ptr) == abfd \ | |
7920ce38 | 378 | && ptr->section != NULL) \ |
17505c5c NC |
379 | cache_ptr->addend = - (ptr->section->vma + ptr->value); \ |
380 | else \ | |
381 | cache_ptr->addend = 0; \ | |
382 | if (ptr && howto_table[reloc.r_type].pc_relative) \ | |
383 | cache_ptr->addend += asect->vma; \ | |
384 | } | |
385 | ||
17505c5c NC |
386 | /* Convert an rtype to howto for the COFF backend linker. */ |
387 | ||
388 | static reloc_howto_type * | |
7920ce38 NC |
389 | coff_mips_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
390 | asection *sec, | |
391 | struct internal_reloc *rel, | |
392 | struct coff_link_hash_entry *h, | |
393 | struct internal_syment *sym, | |
394 | bfd_vma *addendp) | |
17505c5c NC |
395 | { |
396 | ||
397 | reloc_howto_type *howto; | |
398 | ||
399 | howto = howto_table + rel->r_type; | |
400 | ||
401 | #ifdef COFF_WITH_PE | |
402 | *addendp = 0; | |
403 | #endif | |
404 | ||
405 | if (howto->pc_relative) | |
406 | *addendp += sec->vma; | |
407 | ||
408 | if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0) | |
409 | { | |
410 | /* This is a common symbol. The section contents include the | |
411 | size (sym->n_value) as an addend. The relocate_section | |
412 | function will be adding in the final value of the symbol. We | |
413 | need to subtract out the current size in order to get the | |
414 | correct result. */ | |
892339ee | 415 | |
17505c5c NC |
416 | BFD_ASSERT (h != NULL); |
417 | ||
418 | #ifndef COFF_WITH_PE | |
419 | /* I think we *do* want to bypass this. If we don't, I have | |
420 | seen some data parameters get the wrong relocation address. | |
421 | If I link two versions with and without this section bypassed | |
422 | and then do a binary comparison, the addresses which are | |
423 | different can be looked up in the map. The case in which | |
424 | this section has been bypassed has addresses which correspond | |
425 | to values I can find in the map. */ | |
426 | *addendp -= sym->n_value; | |
427 | #endif | |
428 | } | |
429 | ||
430 | #ifndef COFF_WITH_PE | |
431 | /* If the output symbol is common (in which case this must be a | |
1049f94e | 432 | relocatable link), we need to add in the final size of the |
17505c5c | 433 | common symbol. */ |
892339ee | 434 | if (h != NULL && h->root.type == bfd_link_hash_common) |
17505c5c NC |
435 | *addendp += h->root.u.c.size; |
436 | #endif | |
437 | ||
438 | #ifdef COFF_WITH_PE | |
439 | if (howto->pc_relative) | |
440 | { | |
441 | *addendp -= 4; | |
442 | ||
443 | /* If the symbol is defined, then the generic code is going to | |
444 | add back the symbol value in order to cancel out an | |
445 | adjustment it made to the addend. However, we set the addend | |
446 | to 0 at the start of this function. We need to adjust here, | |
447 | to avoid the adjustment the generic code will make. FIXME: | |
448 | This is getting a bit hackish. */ | |
449 | if (sym != NULL && sym->n_scnum != 0) | |
450 | *addendp -= sym->n_value; | |
451 | } | |
452 | ||
453 | if (rel->r_type == MIPS_R_RVA) | |
7920ce38 | 454 | *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase; |
17505c5c NC |
455 | #endif |
456 | ||
457 | return howto; | |
458 | } | |
459 | ||
7920ce38 NC |
460 | #define coff_rtype_to_howto coff_mips_rtype_to_howto |
461 | #define coff_bfd_reloc_type_lookup coff_mips_reloc_type_lookup | |
157090f7 | 462 | #define coff_bfd_reloc_name_lookup coff_mips_reloc_name_lookup |
17505c5c | 463 | |
17505c5c NC |
464 | /* Get the howto structure for a generic reloc type. */ |
465 | ||
466 | static reloc_howto_type * | |
7920ce38 NC |
467 | coff_mips_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
468 | bfd_reloc_code_real_type code) | |
17505c5c NC |
469 | { |
470 | int mips_type; | |
471 | ||
472 | switch (code) | |
473 | { | |
474 | case BFD_RELOC_16: | |
475 | mips_type = MIPS_R_REFHALF; | |
476 | break; | |
477 | case BFD_RELOC_32: | |
478 | case BFD_RELOC_CTOR: | |
479 | mips_type = MIPS_R_REFWORD; | |
480 | break; | |
481 | case BFD_RELOC_MIPS_JMP: | |
482 | mips_type = MIPS_R_JMPADDR; | |
483 | break; | |
484 | case BFD_RELOC_HI16_S: | |
485 | mips_type = MIPS_R_REFHI; | |
486 | break; | |
487 | case BFD_RELOC_LO16: | |
488 | mips_type = MIPS_R_REFLO; | |
489 | break; | |
cdf6fd85 | 490 | case BFD_RELOC_GPREL16: |
17505c5c NC |
491 | mips_type = MIPS_R_GPREL; |
492 | break; | |
493 | case BFD_RELOC_MIPS_LITERAL: | |
494 | mips_type = MIPS_R_LITERAL; | |
495 | break; | |
17505c5c NC |
496 | case BFD_RELOC_RVA: |
497 | mips_type = MIPS_R_RVA; | |
498 | break; | |
499 | default: | |
7920ce38 | 500 | return NULL; |
17505c5c NC |
501 | } |
502 | ||
7920ce38 | 503 | return & howto_table [mips_type]; |
17505c5c NC |
504 | } |
505 | ||
157090f7 AM |
506 | static reloc_howto_type * |
507 | coff_mips_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
508 | const char *r_name) | |
509 | { | |
510 | unsigned int i; | |
511 | ||
512 | for (i = 0; | |
513 | i < sizeof (howto_table) / sizeof (howto_table[0]); | |
514 | i++) | |
515 | if (howto_table[i].name != NULL | |
516 | && strcasecmp (howto_table[i].name, r_name) == 0) | |
517 | return &howto_table[i]; | |
518 | ||
519 | return NULL; | |
520 | } | |
521 | ||
17505c5c | 522 | static void |
7920ce38 | 523 | mips_swap_reloc_in (bfd * abfd, void * src, void * dst) |
17505c5c NC |
524 | { |
525 | static struct internal_reloc pair_prev; | |
526 | RELOC *reloc_src = (RELOC *) src; | |
527 | struct internal_reloc *reloc_dst = (struct internal_reloc *) dst; | |
528 | ||
dc810e39 AM |
529 | reloc_dst->r_vaddr = H_GET_32 (abfd, reloc_src->r_vaddr); |
530 | reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx); | |
531 | reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type); | |
17505c5c NC |
532 | reloc_dst->r_size = 0; |
533 | reloc_dst->r_extern = 0; | |
534 | reloc_dst->r_offset = 0; | |
535 | ||
536 | switch (reloc_dst->r_type) | |
537 | { | |
538 | case MIPS_R_REFHI: | |
539 | pair_prev = *reloc_dst; | |
540 | break; | |
541 | case MIPS_R_PAIR: | |
542 | reloc_dst->r_offset = reloc_dst->r_symndx; | |
543 | if (reloc_dst->r_offset & 0x8000) | |
544 | reloc_dst->r_offset -= 0x10000; | |
17505c5c NC |
545 | reloc_dst->r_symndx = pair_prev.r_symndx; |
546 | break; | |
547 | } | |
548 | } | |
549 | ||
550 | static unsigned int | |
7920ce38 | 551 | mips_swap_reloc_out (bfd * abfd, void * src, void * dst) |
17505c5c | 552 | { |
17505c5c NC |
553 | static bfd_vma prev_addr = 0; |
554 | struct internal_reloc *reloc_src = (struct internal_reloc *)src; | |
555 | struct external_reloc *reloc_dst = (struct external_reloc *)dst; | |
556 | ||
557 | switch (reloc_src->r_type) | |
558 | { | |
559 | case MIPS_R_REFHI: | |
560 | prev_addr = reloc_src->r_vaddr; | |
17505c5c NC |
561 | break; |
562 | case MIPS_R_REFLO: | |
563 | if (reloc_src->r_vaddr == prev_addr) | |
564 | { | |
565 | /* FIXME: only slightly hackish. If we see a REFLO pointing to | |
566 | the same address as a REFHI, we assume this is the matching | |
567 | PAIR reloc and output it accordingly. The symndx is really | |
568 | the low 16 bits of the addend */ | |
dc810e39 AM |
569 | H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr); |
570 | H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx); | |
571 | H_PUT_16 (abfd, MIPS_R_PAIR, reloc_dst->r_type); | |
17505c5c NC |
572 | return RELSZ; |
573 | } | |
574 | break; | |
575 | } | |
576 | ||
dc810e39 AM |
577 | H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr); |
578 | H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx); | |
17505c5c | 579 | |
dc810e39 | 580 | H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type); |
17505c5c NC |
581 | return RELSZ; |
582 | } | |
583 | ||
7920ce38 NC |
584 | #define coff_swap_reloc_in mips_swap_reloc_in |
585 | #define coff_swap_reloc_out mips_swap_reloc_out | |
17505c5c NC |
586 | #define NO_COFF_RELOCS |
587 | ||
b34976b6 | 588 | static bfd_boolean |
7920ce38 NC |
589 | coff_pe_mips_relocate_section (bfd *output_bfd, |
590 | struct bfd_link_info *info, | |
591 | bfd *input_bfd, | |
592 | asection *input_section, | |
593 | bfd_byte *contents, | |
594 | struct internal_reloc *relocs, | |
595 | struct internal_syment *syms, | |
596 | asection **sections) | |
17505c5c | 597 | { |
17505c5c NC |
598 | struct internal_reloc *rel; |
599 | struct internal_reloc *rel_end; | |
600 | unsigned int i; | |
17505c5c | 601 | |
1049f94e | 602 | if (info->relocatable) |
7920ce38 NC |
603 | { |
604 | (*_bfd_error_handler) | |
605 | (_("%B: `ld -r' not supported with PE MIPS objects\n"), input_bfd); | |
606 | bfd_set_error (bfd_error_bad_value); | |
607 | return FALSE; | |
608 | } | |
17505c5c NC |
609 | |
610 | BFD_ASSERT (input_bfd->xvec->byteorder | |
611 | == output_bfd->xvec->byteorder); | |
612 | ||
17505c5c NC |
613 | rel = relocs; |
614 | rel_end = rel + input_section->reloc_count; | |
7920ce38 | 615 | |
17505c5c NC |
616 | for (i = 0; rel < rel_end; rel++, i++) |
617 | { | |
618 | long symndx; | |
619 | struct coff_link_hash_entry *h; | |
620 | struct internal_syment *sym; | |
621 | bfd_vma addend = 0; | |
622 | bfd_vma val, tmp, targ, src, low; | |
623 | reloc_howto_type *howto; | |
17505c5c NC |
624 | unsigned char *mem = contents + rel->r_vaddr; |
625 | ||
626 | symndx = rel->r_symndx; | |
627 | ||
628 | if (symndx == -1) | |
629 | { | |
630 | h = NULL; | |
631 | sym = NULL; | |
632 | } | |
633 | else | |
892339ee | 634 | { |
17505c5c NC |
635 | h = obj_coff_sym_hashes (input_bfd)[symndx]; |
636 | sym = syms + symndx; | |
637 | } | |
638 | ||
639 | /* COFF treats common symbols in one of two ways. Either the | |
640 | size of the symbol is included in the section contents, or it | |
641 | is not. We assume that the size is not included, and force | |
642 | the rtype_to_howto function to adjust the addend as needed. */ | |
643 | ||
644 | if (sym != NULL && sym->n_scnum != 0) | |
645 | addend = - sym->n_value; | |
646 | else | |
647 | addend = 0; | |
648 | ||
17505c5c NC |
649 | howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h, |
650 | sym, &addend); | |
651 | if (howto == NULL) | |
b34976b6 | 652 | return FALSE; |
17505c5c | 653 | |
1049f94e | 654 | /* If we are doing a relocatable link, then we can just ignore |
17505c5c | 655 | a PC relative reloc that is pcrel_offset. It will already |
1049f94e | 656 | have the correct value. If this is not a relocatable link, |
17505c5c NC |
657 | then we should ignore the symbol value. */ |
658 | if (howto->pc_relative && howto->pcrel_offset) | |
659 | { | |
1049f94e | 660 | if (info->relocatable) |
17505c5c NC |
661 | continue; |
662 | if (sym != NULL && sym->n_scnum != 0) | |
663 | addend += sym->n_value; | |
664 | } | |
665 | ||
666 | val = 0; | |
667 | ||
668 | if (h == NULL) | |
669 | { | |
670 | asection *sec; | |
671 | ||
672 | if (symndx == -1) | |
673 | { | |
674 | sec = bfd_abs_section_ptr; | |
675 | val = 0; | |
676 | } | |
677 | else | |
678 | { | |
679 | sec = sections[symndx]; | |
680 | val = (sec->output_section->vma | |
681 | + sec->output_offset | |
682 | + sym->n_value); | |
683 | if (! obj_pe (input_bfd)) | |
684 | val -= sec->vma; | |
685 | } | |
686 | } | |
687 | else | |
688 | { | |
689 | if (h->root.type == bfd_link_hash_defined | |
690 | || h->root.type == bfd_link_hash_defweak) | |
691 | { | |
692 | asection *sec; | |
693 | ||
694 | sec = h->root.u.def.section; | |
695 | val = (h->root.u.def.value | |
696 | + sec->output_section->vma | |
697 | + sec->output_offset); | |
698 | } | |
699 | ||
1049f94e | 700 | else if (! info->relocatable) |
17505c5c NC |
701 | { |
702 | if (! ((*info->callbacks->undefined_symbol) | |
703 | (info, h->root.root.string, input_bfd, input_section, | |
b34976b6 AM |
704 | rel->r_vaddr - input_section->vma, TRUE))) |
705 | return FALSE; | |
17505c5c NC |
706 | } |
707 | } | |
708 | ||
709 | src = rel->r_vaddr + input_section->output_section->vma | |
710 | + input_section->output_offset; | |
17505c5c NC |
711 | |
712 | /* OK, at this point the following variables are set up: | |
713 | src = VMA of the memory we're fixing up | |
714 | mem = pointer to memory we're fixing up | |
7920ce38 | 715 | val = VMA of what we need to refer to. */ |
17505c5c | 716 | |
d003868e AM |
717 | #define UI(x) (*_bfd_error_handler) (_("%B: unimplemented %s\n"), \ |
718 | input_bfd, x); \ | |
17505c5c NC |
719 | bfd_set_error (bfd_error_bad_value); |
720 | ||
721 | switch (rel->r_type) | |
722 | { | |
723 | case MIPS_R_ABSOLUTE: | |
7920ce38 | 724 | /* Ignore these. */ |
17505c5c NC |
725 | break; |
726 | ||
727 | case MIPS_R_REFHALF: | |
7920ce38 | 728 | UI ("refhalf"); |
17505c5c NC |
729 | break; |
730 | ||
731 | case MIPS_R_REFWORD: | |
7920ce38 | 732 | tmp = bfd_get_32 (input_bfd, mem); |
892339ee | 733 | /* printf ("refword: src=%08x targ=%08x+%08x\n", src, tmp, val); */ |
17505c5c | 734 | tmp += val; |
7920ce38 | 735 | bfd_put_32 (input_bfd, tmp, mem); |
17505c5c NC |
736 | break; |
737 | ||
738 | case MIPS_R_JMPADDR: | |
7920ce38 NC |
739 | tmp = bfd_get_32 (input_bfd, mem); |
740 | targ = val + (tmp & 0x03ffffff) * 4; | |
86033394 | 741 | if ((src & 0xf0000000) != (targ & 0xf0000000)) |
17505c5c | 742 | { |
d003868e | 743 | (*_bfd_error_handler) (_("%B: jump too far away\n"), input_bfd); |
17505c5c | 744 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 745 | return FALSE; |
17505c5c NC |
746 | } |
747 | tmp &= 0xfc000000; | |
7920ce38 NC |
748 | tmp |= (targ / 4) & 0x3ffffff; |
749 | bfd_put_32 (input_bfd, tmp, mem); | |
17505c5c NC |
750 | break; |
751 | ||
752 | case MIPS_R_REFHI: | |
7920ce38 | 753 | tmp = bfd_get_32 (input_bfd, mem); |
17505c5c NC |
754 | switch (rel[1].r_type) |
755 | { | |
756 | case MIPS_R_PAIR: | |
757 | /* MS PE object */ | |
758 | targ = val + rel[1].r_offset + ((tmp & 0xffff) << 16); | |
759 | break; | |
760 | case MIPS_R_REFLO: | |
761 | /* GNU COFF object */ | |
7920ce38 | 762 | low = bfd_get_32 (input_bfd, contents + rel[1].r_vaddr); |
17505c5c NC |
763 | low &= 0xffff; |
764 | if (low & 0x8000) | |
765 | low -= 0x10000; | |
766 | targ = val + low + ((tmp & 0xffff) << 16); | |
767 | break; | |
768 | default: | |
d003868e AM |
769 | (*_bfd_error_handler) (_("%B: bad pair/reflo after refhi\n"), |
770 | input_bfd); | |
17505c5c | 771 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 772 | return FALSE; |
17505c5c NC |
773 | } |
774 | tmp &= 0xffff0000; | |
775 | tmp |= (targ >> 16) & 0xffff; | |
7920ce38 | 776 | bfd_put_32 (input_bfd, tmp, mem); |
17505c5c NC |
777 | break; |
778 | ||
779 | case MIPS_R_REFLO: | |
7920ce38 | 780 | tmp = bfd_get_32 (input_bfd, mem); |
17505c5c | 781 | targ = val + (tmp & 0xffff); |
892339ee | 782 | /* printf ("refword: src=%08x targ=%08x\n", src, targ); */ |
17505c5c NC |
783 | tmp &= 0xffff0000; |
784 | tmp |= targ & 0xffff; | |
7920ce38 | 785 | bfd_put_32 (input_bfd, tmp, mem); |
17505c5c NC |
786 | break; |
787 | ||
788 | case MIPS_R_GPREL: | |
789 | case MIPS_R_LITERAL: | |
7920ce38 | 790 | UI ("gprel"); |
17505c5c NC |
791 | break; |
792 | ||
793 | case MIPS_R_SECTION: | |
7920ce38 | 794 | UI ("section"); |
17505c5c NC |
795 | break; |
796 | ||
797 | case MIPS_R_SECREL: | |
7920ce38 | 798 | UI ("secrel"); |
17505c5c NC |
799 | break; |
800 | ||
801 | case MIPS_R_SECRELLO: | |
7920ce38 | 802 | UI ("secrello"); |
17505c5c NC |
803 | break; |
804 | ||
805 | case MIPS_R_SECRELHI: | |
7920ce38 | 806 | UI ("secrelhi"); |
17505c5c NC |
807 | break; |
808 | ||
809 | case MIPS_R_RVA: | |
810 | tmp = bfd_get_32 (input_bfd, mem); | |
892339ee | 811 | /* printf ("rva: src=%08x targ=%08x+%08x\n", src, tmp, val); */ |
17505c5c | 812 | tmp += val |
86033394 | 813 | - pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase; |
17505c5c NC |
814 | bfd_put_32 (input_bfd, tmp, mem); |
815 | break; | |
816 | ||
817 | case MIPS_R_PAIR: | |
818 | /* ignore these */ | |
819 | break; | |
820 | } | |
821 | } | |
822 | ||
b34976b6 | 823 | return TRUE; |
17505c5c NC |
824 | } |
825 | ||
826 | #define coff_relocate_section coff_pe_mips_relocate_section | |
827 | ||
828 | #ifdef TARGET_UNDERSCORE | |
829 | ||
830 | /* If mips gcc uses underscores for symbol names, then it does not use | |
831 | a leading dot for local labels, so if TARGET_UNDERSCORE is defined | |
832 | we treat all symbols starting with L as local. */ | |
833 | ||
b34976b6 | 834 | static bfd_boolean |
7920ce38 | 835 | coff_mips_is_local_label_name (bfd *abfd, const char *name) |
17505c5c NC |
836 | { |
837 | if (name[0] == 'L') | |
b34976b6 | 838 | return TRUE; |
17505c5c NC |
839 | |
840 | return _bfd_coff_is_local_label_name (abfd, name); | |
841 | } | |
842 | ||
843 | #define coff_bfd_is_local_label_name coff_mips_is_local_label_name | |
844 | ||
845 | #endif /* TARGET_UNDERSCORE */ | |
846 | ||
847 | #define COFF_NO_HACK_SCNHDR_SIZE | |
848 | ||
2b5c217d NC |
849 | #ifndef bfd_pe_print_pdata |
850 | #define bfd_pe_print_pdata NULL | |
851 | #endif | |
852 | ||
17505c5c NC |
853 | #include "coffcode.h" |
854 | ||
855 | const bfd_target | |
856 | #ifdef TARGET_SYM | |
857 | TARGET_SYM = | |
858 | #else | |
6d00b590 | 859 | mips_pe_le_vec = |
17505c5c NC |
860 | #endif |
861 | { | |
862 | #ifdef TARGET_NAME | |
863 | TARGET_NAME, | |
864 | #else | |
7920ce38 | 865 | "pe-mips", /* Name. */ |
17505c5c NC |
866 | #endif |
867 | bfd_target_coff_flavour, | |
7920ce38 NC |
868 | BFD_ENDIAN_LITTLE, /* Data byte order is little. */ |
869 | BFD_ENDIAN_LITTLE, /* Header byte order is little. */ | |
17505c5c | 870 | |
7920ce38 | 871 | (HAS_RELOC | EXEC_P | /* Object flags. */ |
17505c5c NC |
872 | HAS_LINENO | HAS_DEBUG | |
873 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | |
874 | ||
875 | #ifndef COFF_WITH_PE | |
7920ce38 | 876 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */ |
17505c5c NC |
877 | | SEC_CODE | SEC_DATA), |
878 | #else | |
7920ce38 | 879 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */ |
17505c5c NC |
880 | | SEC_CODE | SEC_DATA |
881 | | SEC_LINK_ONCE | SEC_LINK_DUPLICATES), | |
882 | #endif | |
883 | ||
884 | #ifdef TARGET_UNDERSCORE | |
7920ce38 | 885 | TARGET_UNDERSCORE, /* Leading underscore. */ |
17505c5c NC |
886 | #else |
887 | 0, /* leading underscore */ | |
888 | #endif | |
7920ce38 NC |
889 | '/', /* AR_pad_char. */ |
890 | 15, /* AR_max_namelen. */ | |
0aabe54e | 891 | 0, /* match priority. */ |
17505c5c NC |
892 | |
893 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
894 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
7920ce38 | 895 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */ |
17505c5c NC |
896 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
897 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
7920ce38 NC |
898 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers. */ |
899 | ||
900 | /* Note that we allow an object file to be treated as a core file as well. */ | |
901 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format. */ | |
902 | bfd_generic_archive_p, coff_object_p}, | |
903 | {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format. */ | |
904 | bfd_false}, | |
905 | {bfd_false, coff_write_object_contents, /* bfd_write_contents. */ | |
906 | _bfd_write_archive_contents, bfd_false}, | |
907 | ||
908 | BFD_JUMP_TABLE_GENERIC (coff), | |
909 | BFD_JUMP_TABLE_COPY (coff), | |
910 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
911 | BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), | |
912 | BFD_JUMP_TABLE_SYMBOLS (coff), | |
913 | BFD_JUMP_TABLE_RELOCS (coff), | |
914 | BFD_JUMP_TABLE_WRITE (coff), | |
915 | BFD_JUMP_TABLE_LINK (coff), | |
916 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
17505c5c NC |
917 | |
918 | NULL, | |
892339ee | 919 | |
17505c5c NC |
920 | COFF_SWAP_TABLE |
921 | }; |