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