1 /* BFD back-end for Motorola MCore COFF/PE
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2010, 2011, 2012
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 3 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, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, 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 (bfd *, struct bfd_link_info *);
43 static bfd_reloc_status_type mcore_coff_unsupported_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 /* The NT loader points the toc register to &toc + 32768, in order to
48 use the complete range of a 16-bit displacement. We have to adjust
49 for this when we fix up loads displaced off the toc reg. */
50 #define TOC_LOAD_ADJUSTMENT (-32768)
51 #define TOC_SECTION_NAME ".private.toc"
53 /* The main body of code is in coffcode.h. */
54 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
56 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
57 from smaller values. Start with zero, widen, *then* decrement. */
58 #define MINUS_ONE (((bfd_vma)0) - 1)
60 static reloc_howto_type mcore_coff_howto_table[] =
63 HOWTO (IMAGE_REL_MCORE_ABSOLUTE,/* type */
65 0, /* size (0 = byte, 1 = short, 2 = long) */
67 FALSE, /* pc_relative */
69 complain_overflow_dont, /* dont complain_on_overflow */
70 NULL, /* special_function */
71 "ABSOLUTE", /* name */
72 FALSE, /* partial_inplace */
75 FALSE), /* pcrel_offset */
77 HOWTO (IMAGE_REL_MCORE_ADDR32,/* type */
79 2, /* size (0 = byte, 1 = short, 2 = long) */
81 FALSE, /* pc_relative */
83 complain_overflow_bitfield, /* complain_on_overflow */
84 NULL, /* special_function */
86 TRUE, /* partial_inplace */
87 0xffffffff, /* src_mask */
88 0xffffffff, /* dst_mask */
89 FALSE), /* pcrel_offset */
91 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
92 Should not appear in object files. */
93 HOWTO (IMAGE_REL_MCORE_PCREL_IMM8BY4, /* type */
95 1, /* size (0 = byte, 1 = short, 2 = long) */
97 TRUE, /* pc_relative */
99 complain_overflow_bitfield, /* complain_on_overflow */
100 mcore_coff_unsupported_reloc, /* special_function */
101 "IMM8BY4", /* name */
102 FALSE, /* partial_inplace */
105 TRUE), /* pcrel_offset */
107 /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
108 Span 2k instructions == 4k bytes.
109 Only useful pieces at the relocated address are the opcode (5 bits) */
110 HOWTO (IMAGE_REL_MCORE_PCREL_IMM11BY2,/* type */
112 1, /* size (0 = byte, 1 = short, 2 = long) */
114 TRUE, /* pc_relative */
116 complain_overflow_signed, /* complain_on_overflow */
117 NULL, /* special_function */
118 "IMM11BY2", /* name */
119 FALSE, /* partial_inplace */
121 0x7ff, /* dst_mask */
122 TRUE), /* pcrel_offset */
124 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
125 HOWTO (IMAGE_REL_MCORE_PCREL_IMM4BY2, /* type */
127 1, /* size (0 = byte, 1 = short, 2 = long) */
129 TRUE, /* pc_relative */
131 complain_overflow_bitfield, /* complain_on_overflow */
132 mcore_coff_unsupported_reloc, /* special_function */
133 "IMM4BY2", /* name */
134 FALSE, /* partial_inplace */
137 TRUE), /* pcrel_offset */
139 /* 32-bit pc-relative. Eventually this will help support PIC code. */
140 HOWTO (IMAGE_REL_MCORE_PCREL_32,/* type */
142 2, /* size (0 = byte, 1 = short, 2 = long) */
144 TRUE, /* pc_relative */
146 complain_overflow_bitfield, /* complain_on_overflow */
147 NULL, /* special_function */
148 "PCREL_32", /* name */
149 FALSE, /* partial_inplace */
151 0xffffffff, /* dst_mask */
152 TRUE), /* pcrel_offset */
154 /* Like PCREL_IMM11BY2, this relocation indicates that there is a
155 'jsri' at the specified address. There is a separate relocation
156 entry for the literal pool entry that it references, but we
157 might be able to change the jsri to a bsr if the target turns out
158 to be close enough [even though we won't reclaim the literal pool
159 entry, we'll get some runtime efficiency back]. Note that this
160 is a relocation that we are allowed to safely ignore. */
161 HOWTO (IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2,/* type */
163 1, /* size (0 = byte, 1 = short, 2 = long) */
165 TRUE, /* pc_relative */
167 complain_overflow_signed, /* complain_on_overflow */
168 NULL, /* special_function */
169 "JSR_IMM11BY2", /* name */
170 FALSE, /* partial_inplace */
172 0x7ff, /* dst_mask */
173 TRUE), /* pcrel_offset */
175 HOWTO (IMAGE_REL_MCORE_RVA, /* type */
177 2, /* size (0 = byte, 1 = short, 2 = long) */
179 FALSE, /* pc_relative */
181 complain_overflow_signed, /* complain_on_overflow */
182 NULL, /* special_function */
183 "MCORE_RVA", /* name */
184 TRUE, /* partial_inplace */
185 0xffffffff, /* src_mask */
186 0xffffffff, /* dst_mask */
187 TRUE) /* pcrel_offset */
190 /* Extend the coff_link_hash_table structure with a few M*Core specific fields.
191 This allows us to store global data here without actually creating any
192 global variables, which is a no-no in the BFD world. */
193 typedef struct coff_mcore_link_hash_table
195 /* The original coff_link_hash_table structure. MUST be first field. */
196 struct coff_link_hash_table root;
198 bfd * bfd_of_toc_owner;
199 long int global_toc_size;
200 long int import_table_size;
201 long int first_thunk_address;
206 /* Get the MCore coff linker hash table from a link_info structure. */
207 #define coff_mcore_hash_table(info) \
208 ((mcore_hash_table *) ((info)->hash))
211 /* Add an entry to the base file. */
214 mcore_emit_base_file_entry (struct bfd_link_info *info,
216 asection *input_section,
217 bfd_vma reloc_offset)
219 bfd_vma addr = reloc_offset
221 + input_section->output_offset
222 + input_section->output_section->vma;
224 if (coff_data (output_bfd)->pe)
225 addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
227 if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
230 bfd_set_error (bfd_error_system_call);
234 static bfd_reloc_status_type
235 mcore_coff_unsupported_reloc (bfd * abfd,
236 arelent * reloc_entry,
237 asymbol * symbol ATTRIBUTE_UNUSED,
238 void * data ATTRIBUTE_UNUSED,
239 asection * input_section ATTRIBUTE_UNUSED,
240 bfd * output_bfd ATTRIBUTE_UNUSED,
241 char ** error_message ATTRIBUTE_UNUSED)
243 BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
245 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
247 reloc_entry->howto->name,
248 reloc_entry->howto->type);
250 return bfd_reloc_notsupported;
253 /* A cheesy little macro to make the code a little more readable. */
254 #define HOW2MAP(bfd_rtype, mcore_rtype) \
255 case bfd_rtype: return & mcore_coff_howto_table [mcore_rtype]
257 static reloc_howto_type *
258 mcore_coff_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
259 bfd_reloc_code_real_type code)
263 HOW2MAP (BFD_RELOC_32, IMAGE_REL_MCORE_ADDR32);
264 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM8BY4, IMAGE_REL_MCORE_PCREL_IMM8BY4);
265 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM11BY2, IMAGE_REL_MCORE_PCREL_IMM11BY2);
266 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM4BY2, IMAGE_REL_MCORE_PCREL_IMM4BY2);
267 HOW2MAP (BFD_RELOC_32_PCREL, IMAGE_REL_MCORE_PCREL_32);
268 HOW2MAP (BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2, IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2);
269 HOW2MAP (BFD_RELOC_RVA, IMAGE_REL_MCORE_RVA);
277 static reloc_howto_type *
278 mcore_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
284 i < (sizeof (mcore_coff_howto_table)
285 / sizeof (mcore_coff_howto_table[0]));
287 if (mcore_coff_howto_table[i].name != NULL
288 && strcasecmp (mcore_coff_howto_table[i].name, r_name) == 0)
289 return &mcore_coff_howto_table[i];
294 #define RTYPE2HOWTO(cache_ptr, dst) \
295 (cache_ptr)->howto = mcore_coff_howto_table + (dst)->r_type;
297 static reloc_howto_type *
298 coff_mcore_rtype_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
300 struct internal_reloc * rel,
301 struct coff_link_hash_entry * h ATTRIBUTE_UNUSED,
302 struct internal_syment * sym,
305 reloc_howto_type * howto;
307 if (rel->r_type >= NUM_ELEM (mcore_coff_howto_table))
310 howto = mcore_coff_howto_table + rel->r_type;
312 if (rel->r_type == IMAGE_REL_MCORE_RVA)
313 * addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
315 else if (howto->pc_relative)
317 * addendp = sec->vma - 2; /* XXX guess - is this right ? */
319 /* If the symbol is defined, then the generic code is going to
320 add back the symbol value in order to cancel out an
321 adjustment it made to the addend. However, we set the addend
322 to 0 at the start of this function. We need to adjust here,
323 to avoid the adjustment the generic code will make. FIXME:
324 This is getting a bit hackish. */
325 if (sym != NULL && sym->n_scnum != 0)
326 * addendp -= sym->n_value;
334 /* Return TRUE if this relocation should appear in the output .reloc section.
335 This function is referenced in pe_mkobject in peicode.h. */
338 in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, reloc_howto_type * howto)
340 return ! howto->pc_relative && howto->type != IMAGE_REL_MCORE_RVA;
343 /* The reloc processing routine for the optimized COFF linker. */
345 coff_mcore_relocate_section (bfd * output_bfd,
346 struct bfd_link_info * info,
348 asection * input_section,
350 struct internal_reloc * relocs,
351 struct internal_syment * syms,
352 asection ** sections)
354 struct internal_reloc * rel;
355 struct internal_reloc * relend;
357 /* If we are performing a relocatable link, we don't need to do a
358 thing. The caller will take care of adjusting the reloc
359 addresses and symbol indices. */
360 if (info->relocatable)
363 /* Check if we have the same endianness */
364 if ( input_bfd->xvec->byteorder != output_bfd->xvec->byteorder
365 && output_bfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
367 (*_bfd_error_handler)
368 (_("%B: compiled for a %s system and target is %s.\n"),
370 bfd_big_endian (input_bfd) ? _("big endian") : _("little endian"),
371 bfd_big_endian (output_bfd) ? _("big endian") : _("little endian"));
373 bfd_set_error (bfd_error_wrong_format);
378 relend = rel + input_section->reloc_count;
380 for (; rel < relend; rel++)
383 struct internal_syment * sym;
386 bfd_reloc_status_type rstat;
388 unsigned short r_type = rel->r_type;
389 reloc_howto_type * howto = NULL;
390 struct coff_link_hash_entry * h;
391 const char * my_name;
393 symndx = rel->r_symndx;
394 loc = contents + rel->r_vaddr - input_section->vma;
403 h = obj_coff_sym_hashes (input_bfd)[symndx];
409 /* Get the howto and initialise the addend. */
410 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
423 asection * sec = sections[symndx];
426 + sec->output_section->vma
427 + sec->output_offset);
430 my_name = "*unknown*";
431 else if ( sym->_n._n_n._n_zeroes == 0
432 && sym->_n._n_n._n_offset != 0)
433 my_name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
436 static char buf [SYMNMLEN + 1];
438 strncpy (buf, sym->_n._n_name, SYMNMLEN);
439 buf[SYMNMLEN] = '\0';
446 if ( h->root.type == bfd_link_hash_defined
447 || h->root.type == bfd_link_hash_defweak)
449 asection * sec = h->root.u.def.section;
451 val = (h->root.u.def.value
452 + sec->output_section->vma
453 + sec->output_offset);
457 if (! ((*info->callbacks->undefined_symbol)
458 (info, h->root.root.string, input_bfd, input_section,
459 rel->r_vaddr - input_section->vma, TRUE)))
463 my_name = h->root.root.string;
466 rstat = bfd_reloc_ok;
468 /* Each case must do its own relocation, setting rstat appropriately. */
472 _bfd_error_handler (_("%B: unsupported relocation type 0x%02x"),
474 bfd_set_error (bfd_error_bad_value);
477 case IMAGE_REL_MCORE_ABSOLUTE:
479 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
480 "sym %ld (%s), r_vaddr %ld (%lx)"),
481 input_bfd, input_section, howto->name,
482 rel->r_symndx, my_name, (long) rel->r_vaddr,
483 (unsigned long) rel->r_vaddr);
486 case IMAGE_REL_MCORE_PCREL_IMM8BY4:
487 case IMAGE_REL_MCORE_PCREL_IMM11BY2:
488 case IMAGE_REL_MCORE_PCREL_IMM4BY2:
489 case IMAGE_REL_MCORE_PCREL_32:
490 case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2:
491 case IMAGE_REL_MCORE_ADDR32:
492 /* XXX fixme - shouldn't this be like the code for the RVA reloc ? */
493 rstat = _bfd_relocate_contents (howto, input_bfd, val, loc);
496 case IMAGE_REL_MCORE_RVA:
497 rstat = _bfd_final_link_relocate
499 input_section, contents, rel->r_vaddr - input_section->vma,
504 /* Emit a reloc if the backend thinks it needs it. */
507 && pe_data (output_bfd)->in_reloc_p (output_bfd, howto)
508 && !mcore_emit_base_file_entry (info, output_bfd, input_section,
520 case bfd_reloc_overflow:
521 if (! ((*info->callbacks->reloc_overflow)
522 (info, (h ? &h->root : NULL), my_name, howto->name,
523 (bfd_vma) 0, input_bfd,
524 input_section, rel->r_vaddr - input_section->vma)))
532 /* Tailor coffcode.h -- macro heaven. */
534 /* We use the special COFF backend linker, with our own special touch. */
536 #define coff_bfd_reloc_type_lookup mcore_coff_reloc_type_lookup
537 #define coff_bfd_reloc_name_lookup mcore_coff_reloc_name_lookup
538 #define coff_relocate_section coff_mcore_relocate_section
539 #define coff_rtype_to_howto coff_mcore_rtype_to_howto
541 #define SELECT_RELOC(internal, howto) {internal.r_type = howto->type;}
543 /* Make sure that the 'r_offset' field is copied properly
544 so that identical binaries will compare the same. */
545 #define SWAP_IN_RELOC_OFFSET H_GET_32
546 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
548 #define COFF_PAGE_SIZE 0x1000
550 #include "coffcode.h"
552 /* Forward declaration to initialise alternative_target field. */
553 extern const bfd_target TARGET_LITTLE_SYM;
555 /* The transfer vectors that lead the outside world to all of the above. */
556 CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM, TARGET_BIG_NAME, D_PAGED,
557 (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
558 0, & TARGET_LITTLE_SYM, COFF_SWAP_TABLE)
559 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM, TARGET_LITTLE_NAME, D_PAGED,
560 (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
561 0, & TARGET_BIG_SYM, COFF_SWAP_TABLE)