2001-01-02 Kazu Hirata <kazu@hxi.com>
[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
892339ee 37static bfd_reloc_status_type coff_mips_reloc
17505c5c
NC
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 *));
86033394 44#if 0
17505c5c
NC
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 *));
86033394 55#endif
17505c5c
NC
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;
86033394 77 asection *input_section ATTRIBUTE_UNUSED;
17505c5c 78 bfd *output_bfd;
86033394 79 char **error_message ATTRIBUTE_UNUSED;
17505c5c
NC
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
892339ee 169 appear in the output .reloc section. */
17505c5c
NC
170
171static boolean in_reloc_p(abfd, howto)
86033394 172 bfd * abfd ATTRIBUTE_UNUSED;
17505c5c
NC
173 reloc_howto_type *howto;
174{
175 return ! howto->pc_relative && howto->type != MIPS_R_RVA;
892339ee 176}
17505c5c
NC
177#endif
178
179#ifndef PCRELOFFSET
180#define PCRELOFFSET false
181#endif
182
892339ee 183static reloc_howto_type howto_table[] =
17505c5c
NC
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
86033394
NC
313 EMPTY_HOWTO (8),
314 EMPTY_HOWTO (9),
315 EMPTY_HOWTO (10),
316 EMPTY_HOWTO (11),
317 EMPTY_HOWTO (12),
318 EMPTY_HOWTO (13),
319 EMPTY_HOWTO (14),
320 EMPTY_HOWTO (15),
321 EMPTY_HOWTO (16),
322 EMPTY_HOWTO (17),
323 EMPTY_HOWTO (18),
324 EMPTY_HOWTO (19),
325 EMPTY_HOWTO (20),
326 EMPTY_HOWTO (21),
327 EMPTY_HOWTO (22),
328 EMPTY_HOWTO (23),
329 EMPTY_HOWTO (24),
330 EMPTY_HOWTO (25),
331 EMPTY_HOWTO (26),
332 EMPTY_HOWTO (27),
333 EMPTY_HOWTO (28),
334 EMPTY_HOWTO (29),
335 EMPTY_HOWTO (30),
336 EMPTY_HOWTO (31),
337 EMPTY_HOWTO (32),
338 EMPTY_HOWTO (33),
892339ee
KH
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 */
17505c5c 345 complain_overflow_bitfield, /* complain_on_overflow */
892339ee
KH
346 coff_mips_reloc, /* special_function */
347 "rva32", /* name */
348 true, /* partial_inplace */
349 0xffffffff, /* src_mask */
350 0xffffffff, /* dst_mask */
17505c5c 351 false), /* pcrel_offset */
86033394
NC
352 EMPTY_HOWTO (35),
353 EMPTY_HOWTO (36),
892339ee
KH
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 */
17505c5c 360 complain_overflow_bitfield, /* complain_on_overflow */
892339ee
KH
361 coff_mips_reloc, /* special_function */
362 "PAIR", /* name */
363 true, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
17505c5c
NC
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
17505c5c
NC
411/* Convert an rtype to howto for the COFF backend linker. */
412
413static reloc_howto_type *
414coff_mips_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
86033394 415 bfd *abfd ATTRIBUTE_UNUSED;
17505c5c
NC
416 asection *sec;
417 struct internal_reloc *rel;
418 struct coff_link_hash_entry *h;
419 struct internal_syment *sym;
420 bfd_vma *addendp;
421{
422
423 reloc_howto_type *howto;
424
425 howto = howto_table + rel->r_type;
426
427#ifdef COFF_WITH_PE
428 *addendp = 0;
429#endif
430
431 if (howto->pc_relative)
432 *addendp += sec->vma;
433
434 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
435 {
436 /* This is a common symbol. The section contents include the
437 size (sym->n_value) as an addend. The relocate_section
438 function will be adding in the final value of the symbol. We
439 need to subtract out the current size in order to get the
440 correct result. */
892339ee 441
17505c5c
NC
442 BFD_ASSERT (h != NULL);
443
444#ifndef COFF_WITH_PE
445 /* I think we *do* want to bypass this. If we don't, I have
446 seen some data parameters get the wrong relocation address.
447 If I link two versions with and without this section bypassed
448 and then do a binary comparison, the addresses which are
449 different can be looked up in the map. The case in which
450 this section has been bypassed has addresses which correspond
451 to values I can find in the map. */
452 *addendp -= sym->n_value;
453#endif
454 }
455
456#ifndef COFF_WITH_PE
457 /* If the output symbol is common (in which case this must be a
458 relocateable link), we need to add in the final size of the
459 common symbol. */
892339ee 460 if (h != NULL && h->root.type == bfd_link_hash_common)
17505c5c
NC
461 *addendp += h->root.u.c.size;
462#endif
463
464#ifdef COFF_WITH_PE
465 if (howto->pc_relative)
466 {
467 *addendp -= 4;
468
469 /* If the symbol is defined, then the generic code is going to
470 add back the symbol value in order to cancel out an
471 adjustment it made to the addend. However, we set the addend
472 to 0 at the start of this function. We need to adjust here,
473 to avoid the adjustment the generic code will make. FIXME:
474 This is getting a bit hackish. */
475 if (sym != NULL && sym->n_scnum != 0)
476 *addendp -= sym->n_value;
477 }
478
479 if (rel->r_type == MIPS_R_RVA)
480 {
481 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
482 }
483#endif
484
485 return howto;
486}
487
488#define coff_rtype_to_howto coff_mips_rtype_to_howto
489
17505c5c
NC
490#define coff_bfd_reloc_type_lookup coff_mips_reloc_type_lookup
491
17505c5c
NC
492/* Get the howto structure for a generic reloc type. */
493
494static reloc_howto_type *
495coff_mips_reloc_type_lookup (abfd, code)
86033394 496 bfd *abfd ATTRIBUTE_UNUSED;
17505c5c
NC
497 bfd_reloc_code_real_type code;
498{
499 int mips_type;
500
501 switch (code)
502 {
503 case BFD_RELOC_16:
504 mips_type = MIPS_R_REFHALF;
505 break;
506 case BFD_RELOC_32:
507 case BFD_RELOC_CTOR:
508 mips_type = MIPS_R_REFWORD;
509 break;
510 case BFD_RELOC_MIPS_JMP:
511 mips_type = MIPS_R_JMPADDR;
512 break;
513 case BFD_RELOC_HI16_S:
514 mips_type = MIPS_R_REFHI;
515 break;
516 case BFD_RELOC_LO16:
517 mips_type = MIPS_R_REFLO;
518 break;
519 case BFD_RELOC_MIPS_GPREL:
520 mips_type = MIPS_R_GPREL;
521 break;
522 case BFD_RELOC_MIPS_LITERAL:
523 mips_type = MIPS_R_LITERAL;
524 break;
525/* FIXME?
526 case BFD_RELOC_16_PCREL_S2:
527 mips_type = MIPS_R_PCREL16;
528 break;
529 case BFD_RELOC_PCREL_HI16_S:
530 mips_type = MIPS_R_RELHI;
531 break;
532 case BFD_RELOC_PCREL_LO16:
533 mips_type = MIPS_R_RELLO;
534 break;
535 case BFD_RELOC_GPREL32:
536 mips_type = MIPS_R_SWITCH;
537 break;
538*/
539 case BFD_RELOC_RVA:
540 mips_type = MIPS_R_RVA;
541 break;
542 default:
543 return (reloc_howto_type *) NULL;
544 }
545
546 return &howto_table[mips_type];
547}
548
549static void
550mips_swap_reloc_in (abfd, src, dst)
551 bfd *abfd;
552 PTR src;
553 PTR dst;
554{
555 static struct internal_reloc pair_prev;
556 RELOC *reloc_src = (RELOC *) src;
557 struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
558
559 reloc_dst->r_vaddr = bfd_h_get_32(abfd, (bfd_byte *)reloc_src->r_vaddr);
560 reloc_dst->r_symndx =
561 bfd_h_get_signed_32(abfd, (bfd_byte *) reloc_src->r_symndx);
562 reloc_dst->r_type = bfd_h_get_16(abfd, (bfd_byte *) reloc_src->r_type);
563 reloc_dst->r_size = 0;
564 reloc_dst->r_extern = 0;
565 reloc_dst->r_offset = 0;
566
567 switch (reloc_dst->r_type)
568 {
569 case MIPS_R_REFHI:
570 pair_prev = *reloc_dst;
571 break;
572 case MIPS_R_PAIR:
573 reloc_dst->r_offset = reloc_dst->r_symndx;
574 if (reloc_dst->r_offset & 0x8000)
575 reloc_dst->r_offset -= 0x10000;
892339ee 576 /*printf ("dj: pair offset is %08x\n", reloc_dst->r_offset);*/
17505c5c
NC
577 reloc_dst->r_symndx = pair_prev.r_symndx;
578 break;
579 }
580}
581
582static unsigned int
583mips_swap_reloc_out (abfd, src, dst)
584 bfd *abfd;
585 PTR src;
586 PTR dst;
587{
588 static int prev_offset = 1;
589 static bfd_vma prev_addr = 0;
590 struct internal_reloc *reloc_src = (struct internal_reloc *)src;
591 struct external_reloc *reloc_dst = (struct external_reloc *)dst;
592
593 switch (reloc_src->r_type)
594 {
595 case MIPS_R_REFHI:
596 prev_addr = reloc_src->r_vaddr;
597 prev_offset = reloc_src->r_offset;
598 break;
599 case MIPS_R_REFLO:
600 if (reloc_src->r_vaddr == prev_addr)
601 {
602 /* FIXME: only slightly hackish. If we see a REFLO pointing to
603 the same address as a REFHI, we assume this is the matching
604 PAIR reloc and output it accordingly. The symndx is really
605 the low 16 bits of the addend */
606 bfd_h_put_32 (abfd, reloc_src->r_vaddr,
607 (bfd_byte *) reloc_dst->r_vaddr);
608 bfd_h_put_32 (abfd, reloc_src->r_symndx,
609 (bfd_byte *) reloc_dst->r_symndx);
610
611 bfd_h_put_16(abfd, MIPS_R_PAIR, (bfd_byte *)
612 reloc_dst->r_type);
613 return RELSZ;
614 }
615 break;
616 }
617
618 bfd_h_put_32(abfd, reloc_src->r_vaddr, (bfd_byte *) reloc_dst->r_vaddr);
619 bfd_h_put_32(abfd, reloc_src->r_symndx, (bfd_byte *) reloc_dst->r_symndx);
620
621 bfd_h_put_16(abfd, reloc_src->r_type, (bfd_byte *)
622 reloc_dst->r_type);
623 return RELSZ;
624}
625
626#define coff_swap_reloc_in mips_swap_reloc_in
627#define coff_swap_reloc_out mips_swap_reloc_out
628#define NO_COFF_RELOCS
629
630static boolean
631coff_pe_mips_relocate_section (output_bfd, info, input_bfd,
632 input_section, contents, relocs, syms,
633 sections)
634 bfd *output_bfd;
635 struct bfd_link_info *info;
636 bfd *input_bfd;
637 asection *input_section;
638 bfd_byte *contents;
639 struct internal_reloc *relocs;
640 struct internal_syment *syms;
641 asection **sections;
642{
17505c5c
NC
643 bfd_vma gp;
644 boolean gp_undefined;
645 size_t adjust;
17505c5c
NC
646 struct internal_reloc *rel;
647 struct internal_reloc *rel_end;
648 unsigned int i;
649 boolean got_lo;
17505c5c
NC
650
651 if (info->relocateable)
652 {
892339ee 653 (*_bfd_error_handler) (_("\
17505c5c
NC
654%s: `ld -r' not supported with PE MIPS objects\n"),
655 bfd_get_filename (input_bfd));
656 bfd_set_error (bfd_error_bad_value);
657 return false;
658 }
659
660 BFD_ASSERT (input_bfd->xvec->byteorder
661 == output_bfd->xvec->byteorder);
662
663#if 0
892339ee 664 printf ("dj: relocate %s(%s) %08x\n",
17505c5c
NC
665 input_bfd->filename, input_section->name,
666 input_section->output_section->vma + input_section->output_offset);
667#endif
668
669 gp = _bfd_get_gp_value (output_bfd);
670 if (gp == 0)
671 gp_undefined = true;
672 else
673 gp_undefined = false;
674
675 got_lo = false;
676
677 adjust = 0;
678
679 rel = relocs;
680 rel_end = rel + input_section->reloc_count;
681 for (i = 0; rel < rel_end; rel++, i++)
682 {
683 long symndx;
684 struct coff_link_hash_entry *h;
685 struct internal_syment *sym;
686 bfd_vma addend = 0;
687 bfd_vma val, tmp, targ, src, low;
688 reloc_howto_type *howto;
17505c5c
NC
689 unsigned char *mem = contents + rel->r_vaddr;
690
691 symndx = rel->r_symndx;
692
693 if (symndx == -1)
694 {
695 h = NULL;
696 sym = NULL;
697 }
698 else
892339ee 699 {
17505c5c
NC
700 h = obj_coff_sym_hashes (input_bfd)[symndx];
701 sym = syms + symndx;
702 }
703
704 /* COFF treats common symbols in one of two ways. Either the
705 size of the symbol is included in the section contents, or it
706 is not. We assume that the size is not included, and force
707 the rtype_to_howto function to adjust the addend as needed. */
708
709 if (sym != NULL && sym->n_scnum != 0)
710 addend = - sym->n_value;
711 else
712 addend = 0;
713
17505c5c
NC
714 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
715 sym, &addend);
716 if (howto == NULL)
717 return false;
718
719 /* If we are doing a relocateable link, then we can just ignore
720 a PC relative reloc that is pcrel_offset. It will already
721 have the correct value. If this is not a relocateable link,
722 then we should ignore the symbol value. */
723 if (howto->pc_relative && howto->pcrel_offset)
724 {
725 if (info->relocateable)
726 continue;
727 if (sym != NULL && sym->n_scnum != 0)
728 addend += sym->n_value;
729 }
730
731 val = 0;
732
733 if (h == NULL)
734 {
735 asection *sec;
736
737 if (symndx == -1)
738 {
739 sec = bfd_abs_section_ptr;
740 val = 0;
741 }
742 else
743 {
744 sec = sections[symndx];
745 val = (sec->output_section->vma
746 + sec->output_offset
747 + sym->n_value);
748 if (! obj_pe (input_bfd))
749 val -= sec->vma;
750 }
751 }
752 else
753 {
754 if (h->root.type == bfd_link_hash_defined
755 || h->root.type == bfd_link_hash_defweak)
756 {
757 asection *sec;
758
759 sec = h->root.u.def.section;
760 val = (h->root.u.def.value
761 + sec->output_section->vma
762 + sec->output_offset);
763 }
764
765 else if (! info->relocateable)
766 {
767 if (! ((*info->callbacks->undefined_symbol)
768 (info, h->root.root.string, input_bfd, input_section,
5cc7c785 769 rel->r_vaddr - input_section->vma, true)))
17505c5c
NC
770 return false;
771 }
772 }
773
774 src = rel->r_vaddr + input_section->output_section->vma
775 + input_section->output_offset;
776#if 0
892339ee 777 printf ("dj: reloc %02x %-8s a=%08x/%08x(%08x) v=%08x+%08x %s\n",
17505c5c
NC
778 rel->r_type, howto_table[rel->r_type].name,
779 src, rel->r_vaddr, *(unsigned long *)mem, val, rel->r_offset,
780 h?h->root.root.string:"(none)");
781#endif
782
783 /* OK, at this point the following variables are set up:
784 src = VMA of the memory we're fixing up
785 mem = pointer to memory we're fixing up
786 val = VMA of what we need to refer to
787 */
788
892339ee 789#define UI(x) (*_bfd_error_handler) (_("%s: unimplemented %s\n"), \
17505c5c
NC
790 bfd_get_filename (input_bfd), x); \
791 bfd_set_error (bfd_error_bad_value);
792
793 switch (rel->r_type)
794 {
795 case MIPS_R_ABSOLUTE:
796 /* ignore these */
797 break;
798
799 case MIPS_R_REFHALF:
800 UI("refhalf");
801 break;
802
803 case MIPS_R_REFWORD:
804 tmp = bfd_get_32(input_bfd, mem);
892339ee 805 /* printf ("refword: src=%08x targ=%08x+%08x\n", src, tmp, val); */
17505c5c
NC
806 tmp += val;
807 bfd_put_32(input_bfd, tmp, mem);
808 break;
809
810 case MIPS_R_JMPADDR:
811 tmp = bfd_get_32(input_bfd, mem);
812 targ = val + (tmp&0x03ffffff)*4;
86033394 813 if ((src & 0xf0000000) != (targ & 0xf0000000))
17505c5c 814 {
892339ee 815 (*_bfd_error_handler) (_("%s: jump too far away\n"),
17505c5c
NC
816 bfd_get_filename (input_bfd));
817 bfd_set_error (bfd_error_bad_value);
818 return false;
819 }
820 tmp &= 0xfc000000;
821 tmp |= (targ/4) & 0x3ffffff;
822 bfd_put_32(input_bfd, tmp, mem);
823 break;
824
825 case MIPS_R_REFHI:
826 tmp = bfd_get_32(input_bfd, mem);
827 switch (rel[1].r_type)
828 {
829 case MIPS_R_PAIR:
830 /* MS PE object */
831 targ = val + rel[1].r_offset + ((tmp & 0xffff) << 16);
832 break;
833 case MIPS_R_REFLO:
834 /* GNU COFF object */
835 low = bfd_get_32(input_bfd, contents + rel[1].r_vaddr);
836 low &= 0xffff;
837 if (low & 0x8000)
838 low -= 0x10000;
839 targ = val + low + ((tmp & 0xffff) << 16);
840 break;
841 default:
892339ee 842 (*_bfd_error_handler) (_("%s: bad pair/reflo after refhi\n"),
17505c5c
NC
843 bfd_get_filename (input_bfd));
844 bfd_set_error (bfd_error_bad_value);
845 return false;
846 }
847 tmp &= 0xffff0000;
848 tmp |= (targ >> 16) & 0xffff;
849 bfd_put_32(input_bfd, tmp, mem);
850 break;
851
852 case MIPS_R_REFLO:
853 tmp = bfd_get_32(input_bfd, mem);
854 targ = val + (tmp & 0xffff);
892339ee 855 /* printf ("refword: src=%08x targ=%08x\n", src, targ); */
17505c5c
NC
856 tmp &= 0xffff0000;
857 tmp |= targ & 0xffff;
858 bfd_put_32(input_bfd, tmp, mem);
859 break;
860
861 case MIPS_R_GPREL:
862 case MIPS_R_LITERAL:
863 UI("gprel");
864 break;
865
866 case MIPS_R_SECTION:
867 UI("section");
868 break;
869
870 case MIPS_R_SECREL:
871 UI("secrel");
872 break;
873
874 case MIPS_R_SECRELLO:
875 UI("secrello");
876 break;
877
878 case MIPS_R_SECRELHI:
879 UI("secrelhi");
880 break;
881
882 case MIPS_R_RVA:
883 tmp = bfd_get_32 (input_bfd, mem);
892339ee 884 /* printf ("rva: src=%08x targ=%08x+%08x\n", src, tmp, val); */
17505c5c 885 tmp += val
86033394 886 - pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
17505c5c
NC
887 bfd_put_32 (input_bfd, tmp, mem);
888 break;
889
890 case MIPS_R_PAIR:
891 /* ignore these */
892 break;
893 }
894 }
895
896 return true;
897}
898
899#define coff_relocate_section coff_pe_mips_relocate_section
900
901#ifdef TARGET_UNDERSCORE
902
903/* If mips gcc uses underscores for symbol names, then it does not use
904 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
905 we treat all symbols starting with L as local. */
906
907static boolean coff_mips_is_local_label_name PARAMS ((bfd *, const char *));
908
909static boolean
910coff_mips_is_local_label_name (abfd, name)
911 bfd *abfd;
912 const char *name;
913{
914 if (name[0] == 'L')
915 return true;
916
917 return _bfd_coff_is_local_label_name (abfd, name);
918}
919
920#define coff_bfd_is_local_label_name coff_mips_is_local_label_name
921
922#endif /* TARGET_UNDERSCORE */
923
924#define COFF_NO_HACK_SCNHDR_SIZE
925
926#include "coffcode.h"
927
928const bfd_target
929#ifdef TARGET_SYM
930 TARGET_SYM =
931#else
932 mipslpe_vec =
933#endif
934{
935#ifdef TARGET_NAME
936 TARGET_NAME,
937#else
938 "pe-mips", /* name */
939#endif
940 bfd_target_coff_flavour,
941 BFD_ENDIAN_LITTLE, /* data byte order is little */
942 BFD_ENDIAN_LITTLE, /* header byte order is little */
943
944 (HAS_RELOC | EXEC_P | /* object flags */
945 HAS_LINENO | HAS_DEBUG |
946 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
947
948#ifndef COFF_WITH_PE
949 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
950 | SEC_CODE | SEC_DATA),
951#else
952 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
953 | SEC_CODE | SEC_DATA
954 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
955#endif
956
957#ifdef TARGET_UNDERSCORE
958 TARGET_UNDERSCORE, /* leading underscore */
959#else
960 0, /* leading underscore */
961#endif
962 '/', /* ar_pad_char */
963 15, /* ar_max_namelen */
964
965 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
966 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
967 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
968 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
969 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
970 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
971
892339ee 972/* Note that we allow an object file to be treated as a core file as well. */
17505c5c
NC
973 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
974 bfd_generic_archive_p, coff_object_p},
975 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
976 bfd_false},
977 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
978 _bfd_write_archive_contents, bfd_false},
979
980 BFD_JUMP_TABLE_GENERIC (coff),
981 BFD_JUMP_TABLE_COPY (coff),
982 BFD_JUMP_TABLE_CORE (_bfd_nocore),
983 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
984 BFD_JUMP_TABLE_SYMBOLS (coff),
985 BFD_JUMP_TABLE_RELOCS (coff),
986 BFD_JUMP_TABLE_WRITE (coff),
987 BFD_JUMP_TABLE_LINK (coff),
988 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
989
990 NULL,
892339ee 991
17505c5c
NC
992 COFF_SWAP_TABLE
993};
This page took 0.098749 seconds and 4 git commands to generate.