1 /* BFD back-end for Motorola MCore COFF/PE
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
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
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "coff/mcore.h"
26 #include "coff/internal.h"
33 #define BADMAG(x) MCOREBADMAG(x)
36 #define NUM_ELEM(A) (sizeof (A) / sizeof (A)[0])
39 /* This file is compiled more than once, but we only compile the
40 final_link routine once. */
41 extern bfd_boolean mcore_bfd_coff_final_link
42 PARAMS ((bfd
*, struct bfd_link_info
*));
44 static struct bfd_link_hash_table
*coff_mcore_link_hash_table_create
47 static bfd_reloc_status_type mcore_coff_unsupported_reloc
48 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
49 static bfd_boolean coff_mcore_relocate_section
50 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
51 struct internal_reloc
*, struct internal_syment
*, asection
**));
52 static reloc_howto_type
*mcore_coff_reloc_type_lookup
53 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
54 static reloc_howto_type
*coff_mcore_rtype_to_howto
55 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
56 struct coff_link_hash_entry
*, struct internal_syment
*,
58 static void mcore_emit_base_file_entry
59 PARAMS ((struct bfd_link_info
*, bfd
*, asection
*, bfd_vma
));
60 static bfd_boolean in_reloc_p
PARAMS ((bfd
*, reloc_howto_type
*));
62 /* The NT loader points the toc register to &toc + 32768, in order to
63 use the complete range of a 16-bit displacement. We have to adjust
64 for this when we fix up loads displaced off the toc reg. */
65 #define TOC_LOAD_ADJUSTMENT (-32768)
66 #define TOC_SECTION_NAME ".private.toc"
68 /* The main body of code is in coffcode.h. */
69 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
71 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
72 from smaller values. Start with zero, widen, *then* decrement. */
73 #define MINUS_ONE (((bfd_vma)0) - 1)
75 static reloc_howto_type mcore_coff_howto_table
[] =
78 HOWTO (IMAGE_REL_MCORE_ABSOLUTE
,/* type */
80 0, /* size (0 = byte, 1 = short, 2 = long) */
82 FALSE
, /* pc_relative */
84 complain_overflow_dont
, /* dont complain_on_overflow */
85 NULL
, /* special_function */
86 "ABSOLUTE", /* name */
87 FALSE
, /* partial_inplace */
90 FALSE
), /* pcrel_offset */
92 HOWTO (IMAGE_REL_MCORE_ADDR32
,/* type */
94 2, /* size (0 = byte, 1 = short, 2 = long) */
96 FALSE
, /* pc_relative */
98 complain_overflow_bitfield
, /* complain_on_overflow */
99 NULL
, /* special_function */
101 TRUE
, /* partial_inplace */
102 0xffffffff, /* src_mask */
103 0xffffffff, /* dst_mask */
104 FALSE
), /* pcrel_offset */
106 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
107 Should not appear in object files. */
108 HOWTO (IMAGE_REL_MCORE_PCREL_IMM8BY4
, /* type */
110 1, /* size (0 = byte, 1 = short, 2 = long) */
112 TRUE
, /* pc_relative */
114 complain_overflow_bitfield
, /* complain_on_overflow */
115 mcore_coff_unsupported_reloc
, /* special_function */
116 "IMM8BY4", /* name */
117 FALSE
, /* partial_inplace */
120 TRUE
), /* pcrel_offset */
122 /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
123 Span 2k instructions == 4k bytes.
124 Only useful pieces at the relocated address are the opcode (5 bits) */
125 HOWTO (IMAGE_REL_MCORE_PCREL_IMM11BY2
,/* type */
127 1, /* size (0 = byte, 1 = short, 2 = long) */
129 TRUE
, /* pc_relative */
131 complain_overflow_signed
, /* complain_on_overflow */
132 NULL
, /* special_function */
133 "IMM11BY2", /* name */
134 FALSE
, /* partial_inplace */
136 0x7ff, /* dst_mask */
137 TRUE
), /* pcrel_offset */
139 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
140 HOWTO (IMAGE_REL_MCORE_PCREL_IMM4BY2
, /* type */
142 1, /* size (0 = byte, 1 = short, 2 = long) */
144 TRUE
, /* pc_relative */
146 complain_overflow_bitfield
, /* complain_on_overflow */
147 mcore_coff_unsupported_reloc
, /* special_function */
148 "IMM4BY2", /* name */
149 FALSE
, /* partial_inplace */
152 TRUE
), /* pcrel_offset */
154 /* 32-bit pc-relative. Eventually this will help support PIC code. */
155 HOWTO (IMAGE_REL_MCORE_PCREL_32
,/* type */
157 2, /* size (0 = byte, 1 = short, 2 = long) */
159 TRUE
, /* pc_relative */
161 complain_overflow_bitfield
, /* complain_on_overflow */
162 NULL
, /* special_function */
163 "PCREL_32", /* name */
164 FALSE
, /* partial_inplace */
166 0xffffffff, /* dst_mask */
167 TRUE
), /* pcrel_offset */
169 /* Like PCREL_IMM11BY2, this relocation indicates that there is a
170 'jsri' at the specified address. There is a separate relocation
171 entry for the literal pool entry that it references, but we
172 might be able to change the jsri to a bsr if the target turns out
173 to be close enough [even though we won't reclaim the literal pool
174 entry, we'll get some runtime efficiency back]. Note that this
175 is a relocation that we are allowed to safely ignore. */
176 HOWTO (IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2
,/* type */
178 1, /* size (0 = byte, 1 = short, 2 = long) */
180 TRUE
, /* pc_relative */
182 complain_overflow_signed
, /* complain_on_overflow */
183 NULL
, /* special_function */
184 "JSR_IMM11BY2", /* name */
185 FALSE
, /* partial_inplace */
187 0x7ff, /* dst_mask */
188 TRUE
), /* pcrel_offset */
190 HOWTO (IMAGE_REL_MCORE_RVA
, /* type */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
194 FALSE
, /* pc_relative */
196 complain_overflow_signed
, /* complain_on_overflow */
197 NULL
, /* special_function */
198 "MCORE_RVA", /* name */
199 TRUE
, /* partial_inplace */
200 0xffffffff, /* src_mask */
201 0xffffffff, /* dst_mask */
202 TRUE
) /* pcrel_offset */
205 /* Extend the coff_link_hash_table structure with a few M*Core specific fields.
206 This allows us to store global data here without actually creating any
207 global variables, which is a no-no in the BFD world. */
208 typedef struct coff_mcore_link_hash_table
210 /* The original coff_link_hash_table structure. MUST be first field. */
211 struct coff_link_hash_table root
;
213 bfd
* bfd_of_toc_owner
;
214 long int global_toc_size
;
215 long int import_table_size
;
216 long int first_thunk_address
;
221 /* Get the MCore coff linker hash table from a link_info structure. */
222 #define coff_mcore_hash_table(info) \
223 ((mcore_hash_table *) ((info)->hash))
226 /* Create an MCore coff linker hash table. */
228 static struct bfd_link_hash_table
*
229 coff_mcore_link_hash_table_create (abfd
)
232 mcore_hash_table
* ret
;
234 ret
= (mcore_hash_table
*) bfd_malloc ((bfd_size_type
) sizeof (* ret
));
235 if (ret
== (mcore_hash_table
*) NULL
)
238 if (! _bfd_coff_link_hash_table_init
239 (& ret
->root
, abfd
, _bfd_coff_link_hash_newfunc
))
242 return (struct bfd_link_hash_table
*) NULL
;
245 ret
->bfd_of_toc_owner
= NULL
;
246 ret
->global_toc_size
= 0;
247 ret
->import_table_size
= 0;
248 ret
->first_thunk_address
= 0;
251 return & ret
->root
.root
;
255 /* Add an entry to the base file. */
258 mcore_emit_base_file_entry (info
, output_bfd
, input_section
, reloc_offset
)
259 struct bfd_link_info
* info
;
261 asection
* input_section
;
262 bfd_vma reloc_offset
;
264 bfd_vma addr
= reloc_offset
266 + input_section
->output_offset
267 + input_section
->output_section
->vma
;
269 if (coff_data (output_bfd
)->pe
)
270 addr
-= pe_data (output_bfd
)->pe_opthdr
.ImageBase
;
272 fwrite (&addr
, 1, sizeof (addr
), (FILE *) info
->base_file
);
275 static bfd_reloc_status_type
276 mcore_coff_unsupported_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
,
277 output_bfd
, error_message
)
279 arelent
* reloc_entry
;
280 asymbol
* symbol ATTRIBUTE_UNUSED
;
281 PTR data ATTRIBUTE_UNUSED
;
282 asection
* input_section ATTRIBUTE_UNUSED
;
283 bfd
* output_bfd ATTRIBUTE_UNUSED
;
284 char ** error_message ATTRIBUTE_UNUSED
;
286 BFD_ASSERT (reloc_entry
->howto
!= (reloc_howto_type
*)0);
288 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
290 reloc_entry
->howto
->name
,
291 reloc_entry
->howto
->type
);
293 return bfd_reloc_notsupported
;
296 /* A cheesy little macro to make the code a little more readable. */
297 #define HOW2MAP(bfd_rtype, mcore_rtype) \
298 case bfd_rtype: return & mcore_coff_howto_table [mcore_rtype]
300 static reloc_howto_type
*
301 mcore_coff_reloc_type_lookup (abfd
, code
)
302 bfd
* abfd ATTRIBUTE_UNUSED
;
303 bfd_reloc_code_real_type code
;
307 HOW2MAP (BFD_RELOC_32
, IMAGE_REL_MCORE_ADDR32
);
308 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM8BY4
, IMAGE_REL_MCORE_PCREL_IMM8BY4
);
309 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM11BY2
, IMAGE_REL_MCORE_PCREL_IMM11BY2
);
310 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM4BY2
, IMAGE_REL_MCORE_PCREL_IMM4BY2
);
311 HOW2MAP (BFD_RELOC_32_PCREL
, IMAGE_REL_MCORE_PCREL_32
);
312 HOW2MAP (BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2
, IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2
);
313 HOW2MAP (BFD_RELOC_RVA
, IMAGE_REL_MCORE_RVA
);
322 #define RTYPE2HOWTO(cache_ptr, dst) \
323 (cache_ptr)->howto = mcore_coff_howto_table + (dst)->r_type;
325 static reloc_howto_type
*
326 coff_mcore_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
327 bfd
* abfd ATTRIBUTE_UNUSED
;
329 struct internal_reloc
* rel
;
330 struct coff_link_hash_entry
* h ATTRIBUTE_UNUSED
;
331 struct internal_syment
* sym
;
334 reloc_howto_type
* howto
;
336 if (rel
->r_type
>= NUM_ELEM (mcore_coff_howto_table
))
339 howto
= mcore_coff_howto_table
+ rel
->r_type
;
341 if (rel
->r_type
== IMAGE_REL_MCORE_RVA
)
342 * addendp
-= pe_data (sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
344 else if (howto
->pc_relative
)
346 * addendp
= sec
->vma
- 2; /* XXX guess - is this right ? */
348 /* If the symbol is defined, then the generic code is going to
349 add back the symbol value in order to cancel out an
350 adjustment it made to the addend. However, we set the addend
351 to 0 at the start of this function. We need to adjust here,
352 to avoid the adjustment the generic code will make. FIXME:
353 This is getting a bit hackish. */
354 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
355 * addendp
-= sym
->n_value
;
363 /* Return TRUE if this relocation should appear in the output .reloc section.
364 This function is referenced in pe_mkobject in peicode.h. */
367 in_reloc_p (abfd
, howto
)
368 bfd
* abfd ATTRIBUTE_UNUSED
;
369 reloc_howto_type
* howto
;
371 return ! howto
->pc_relative
&& howto
->type
!= IMAGE_REL_MCORE_RVA
;
374 /* The reloc processing routine for the optimized COFF linker. */
376 coff_mcore_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
377 contents
, relocs
, syms
, sections
)
379 struct bfd_link_info
* info
;
381 asection
* input_section
;
383 struct internal_reloc
* relocs
;
384 struct internal_syment
* syms
;
385 asection
** sections
;
387 struct internal_reloc
* rel
;
388 struct internal_reloc
* relend
;
392 /* If we are performing a relocatable link, we don't need to do a
393 thing. The caller will take care of adjusting the reloc
394 addresses and symbol indices. */
395 if (info
->relocatable
)
398 /* Check if we have the same endianess */
399 if ( input_bfd
->xvec
->byteorder
!= output_bfd
->xvec
->byteorder
400 && output_bfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
402 (*_bfd_error_handler
)
403 (_("%B: compiled for a %s system and target is %s.\n"),
405 bfd_big_endian (input_bfd
) ? _("big endian") : _("little endian"),
406 bfd_big_endian (output_bfd
) ? _("big endian") : _("little endian"));
408 bfd_set_error (bfd_error_wrong_format
);
416 relend
= rel
+ input_section
->reloc_count
;
418 for (; rel
< relend
; rel
++)
421 struct internal_syment
* sym
;
424 bfd_reloc_status_type rstat
;
426 unsigned short r_type
= rel
->r_type
;
427 reloc_howto_type
* howto
= NULL
;
428 struct coff_link_hash_entry
* h
;
429 const char * my_name
;
431 symndx
= rel
->r_symndx
;
432 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
441 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
447 /* Get the howto and initialise the addend. */
448 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
461 asection
* sec
= sections
[symndx
];
464 + sec
->output_section
->vma
465 + sec
->output_offset
);
468 my_name
= "*unknown*";
469 else if ( sym
->_n
._n_n
._n_zeroes
== 0
470 && sym
->_n
._n_n
._n_offset
!= 0)
471 my_name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
474 static char buf
[SYMNMLEN
+ 1];
476 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
477 buf
[SYMNMLEN
] = '\0';
484 if ( h
->root
.type
== bfd_link_hash_defined
485 || h
->root
.type
== bfd_link_hash_defweak
)
487 asection
* sec
= h
->root
.u
.def
.section
;
489 val
= (h
->root
.u
.def
.value
490 + sec
->output_section
->vma
491 + sec
->output_offset
);
495 if (! ((*info
->callbacks
->undefined_symbol
)
496 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
497 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
501 my_name
= h
->root
.root
.string
;
504 rstat
= bfd_reloc_ok
;
506 /* Each case must do its own relocation, setting rstat appropriately. */
510 _bfd_error_handler (_("%B: unsupported relocation type 0x%02x"),
512 bfd_set_error (bfd_error_bad_value
);
515 case IMAGE_REL_MCORE_ABSOLUTE
:
517 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
518 "sym %ld (%s), r_vaddr %ld (%lx)"),
519 input_bfd
, input_section
, howto
->name
,
520 rel
->r_symndx
, my_name
, (long) rel
->r_vaddr
,
521 (unsigned long) rel
->r_vaddr
);
524 case IMAGE_REL_MCORE_PCREL_IMM8BY4
:
525 case IMAGE_REL_MCORE_PCREL_IMM11BY2
:
526 case IMAGE_REL_MCORE_PCREL_IMM4BY2
:
527 case IMAGE_REL_MCORE_PCREL_32
:
528 case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2
:
529 case IMAGE_REL_MCORE_ADDR32
:
530 /* XXX fixme - shouldn't this be like the code for the RVA reloc ? */
531 rstat
= _bfd_relocate_contents (howto
, input_bfd
, val
, loc
);
534 case IMAGE_REL_MCORE_RVA
:
535 rstat
= _bfd_final_link_relocate
537 input_section
, contents
, rel
->r_vaddr
- input_section
->vma
,
544 /* Emit a reloc if the backend thinks it needs it. */
545 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
546 mcore_emit_base_file_entry (info
, output_bfd
, input_section
, rel
->r_vaddr
);
557 case bfd_reloc_overflow
:
558 if (! ((*info
->callbacks
->reloc_overflow
)
559 (info
, my_name
, howto
->name
,
560 (bfd_vma
) 0, input_bfd
,
561 input_section
, rel
->r_vaddr
- input_section
->vma
)))
569 /* Tailor coffcode.h -- macro heaven. */
571 /* We use the special COFF backend linker, with our own special touch. */
573 #define coff_bfd_reloc_type_lookup mcore_coff_reloc_type_lookup
574 #define coff_relocate_section coff_mcore_relocate_section
575 #define coff_rtype_to_howto coff_mcore_rtype_to_howto
577 #define SELECT_RELOC(internal, howto) {internal.r_type = howto->type;}
579 /* Make sure that the 'r_offset' field is copied properly
580 so that identical binaries will compare the same. */
581 #define SWAP_IN_RELOC_OFFSET H_GET_32
582 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
584 #define COFF_PAGE_SIZE 0x1000
586 #include "coffcode.h"
588 /* Forward declaration to initialise alternative_target field. */
589 extern const bfd_target TARGET_LITTLE_SYM
;
591 /* The transfer vectors that lead the outside world to all of the above. */
592 CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM
, TARGET_BIG_NAME
, D_PAGED
,
593 (SEC_CODE
| SEC_DATA
| SEC_DEBUGGING
| SEC_READONLY
| SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
594 0, & TARGET_LITTLE_SYM
, COFF_SWAP_TABLE
)
595 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM
, TARGET_LITTLE_NAME
, D_PAGED
,
596 (SEC_CODE
| SEC_DATA
| SEC_DEBUGGING
| SEC_READONLY
| SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
597 0, & TARGET_BIG_SYM
, COFF_SWAP_TABLE
)