Regenerate translation files.
[binutils.git] / bfd / coff-mcore.c
blob861e19aaf61f883dd6949fe47225b14f50dda2b7
1 /* BFD back-end for Motorola MCore COFF/PE
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
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. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "coff/mcore.h"
26 #include "coff/internal.h"
27 #include "coff/pe.h"
28 #include "libcoff.h"
30 #ifdef BADMAG
31 #undef BADMAG
32 #endif
33 #define BADMAG(x) MCOREBADMAG(x)
35 #ifndef NUM_ELEM
36 #define NUM_ELEM(A) (sizeof (A) / sizeof (A)[0])
37 #endif
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 *));
43 static bfd_reloc_status_type mcore_coff_unsupported_reloc
44 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
45 static bfd_boolean coff_mcore_relocate_section
46 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
47 struct internal_reloc *, struct internal_syment *, asection **));
48 static reloc_howto_type *mcore_coff_reloc_type_lookup
49 PARAMS ((bfd *, bfd_reloc_code_real_type));
50 static reloc_howto_type *coff_mcore_rtype_to_howto
51 PARAMS ((bfd *, asection *, struct internal_reloc *,
52 struct coff_link_hash_entry *, struct internal_syment *,
53 bfd_vma *));
54 static void mcore_emit_base_file_entry
55 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_vma));
56 static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
58 /* The NT loader points the toc register to &toc + 32768, in order to
59 use the complete range of a 16-bit displacement. We have to adjust
60 for this when we fix up loads displaced off the toc reg. */
61 #define TOC_LOAD_ADJUSTMENT (-32768)
62 #define TOC_SECTION_NAME ".private.toc"
64 /* The main body of code is in coffcode.h. */
65 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
67 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
68 from smaller values. Start with zero, widen, *then* decrement. */
69 #define MINUS_ONE (((bfd_vma)0) - 1)
71 static reloc_howto_type mcore_coff_howto_table[] =
73 /* Unused: */
74 HOWTO (IMAGE_REL_MCORE_ABSOLUTE,/* type */
75 0, /* rightshift */
76 0, /* size (0 = byte, 1 = short, 2 = long) */
77 0, /* bitsize */
78 FALSE, /* pc_relative */
79 0, /* bitpos */
80 complain_overflow_dont, /* dont complain_on_overflow */
81 NULL, /* special_function */
82 "ABSOLUTE", /* name */
83 FALSE, /* partial_inplace */
84 0x00, /* src_mask */
85 0x00, /* dst_mask */
86 FALSE), /* pcrel_offset */
88 HOWTO (IMAGE_REL_MCORE_ADDR32,/* type */
89 0, /* rightshift */
90 2, /* size (0 = byte, 1 = short, 2 = long) */
91 32, /* bitsize */
92 FALSE, /* pc_relative */
93 0, /* bitpos */
94 complain_overflow_bitfield, /* complain_on_overflow */
95 NULL, /* special_function */
96 "ADDR32", /* name */
97 TRUE, /* partial_inplace */
98 0xffffffff, /* src_mask */
99 0xffffffff, /* dst_mask */
100 FALSE), /* pcrel_offset */
102 /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
103 Should not appear in object files. */
104 HOWTO (IMAGE_REL_MCORE_PCREL_IMM8BY4, /* type */
105 2, /* rightshift */
106 1, /* size (0 = byte, 1 = short, 2 = long) */
107 8, /* bitsize */
108 TRUE, /* pc_relative */
109 0, /* bitpos */
110 complain_overflow_bitfield, /* complain_on_overflow */
111 mcore_coff_unsupported_reloc, /* special_function */
112 "IMM8BY4", /* name */
113 FALSE, /* partial_inplace */
114 0, /* src_mask */
115 0, /* dst_mask */
116 TRUE), /* pcrel_offset */
118 /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
119 Span 2k instructions == 4k bytes.
120 Only useful pieces at the relocated address are the opcode (5 bits) */
121 HOWTO (IMAGE_REL_MCORE_PCREL_IMM11BY2,/* type */
122 1, /* rightshift */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
124 11, /* bitsize */
125 TRUE, /* pc_relative */
126 0, /* bitpos */
127 complain_overflow_signed, /* complain_on_overflow */
128 NULL, /* special_function */
129 "IMM11BY2", /* name */
130 FALSE, /* partial_inplace */
131 0x0, /* src_mask */
132 0x7ff, /* dst_mask */
133 TRUE), /* pcrel_offset */
135 /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
136 HOWTO (IMAGE_REL_MCORE_PCREL_IMM4BY2, /* type */
137 1, /* rightshift */
138 1, /* size (0 = byte, 1 = short, 2 = long) */
139 4, /* bitsize */
140 TRUE, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_bitfield, /* complain_on_overflow */
143 mcore_coff_unsupported_reloc, /* special_function */
144 "IMM4BY2", /* name */
145 FALSE, /* partial_inplace */
146 0, /* src_mask */
147 0, /* dst_mask */
148 TRUE), /* pcrel_offset */
150 /* 32-bit pc-relative. Eventually this will help support PIC code. */
151 HOWTO (IMAGE_REL_MCORE_PCREL_32,/* type */
152 0, /* rightshift */
153 2, /* size (0 = byte, 1 = short, 2 = long) */
154 32, /* bitsize */
155 TRUE, /* pc_relative */
156 0, /* bitpos */
157 complain_overflow_bitfield, /* complain_on_overflow */
158 NULL, /* special_function */
159 "PCREL_32", /* name */
160 FALSE, /* partial_inplace */
161 0x0, /* src_mask */
162 0xffffffff, /* dst_mask */
163 TRUE), /* pcrel_offset */
165 /* Like PCREL_IMM11BY2, this relocation indicates that there is a
166 'jsri' at the specified address. There is a separate relocation
167 entry for the literal pool entry that it references, but we
168 might be able to change the jsri to a bsr if the target turns out
169 to be close enough [even though we won't reclaim the literal pool
170 entry, we'll get some runtime efficiency back]. Note that this
171 is a relocation that we are allowed to safely ignore. */
172 HOWTO (IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2,/* type */
173 1, /* rightshift */
174 1, /* size (0 = byte, 1 = short, 2 = long) */
175 11, /* bitsize */
176 TRUE, /* pc_relative */
177 0, /* bitpos */
178 complain_overflow_signed, /* complain_on_overflow */
179 NULL, /* special_function */
180 "JSR_IMM11BY2", /* name */
181 FALSE, /* partial_inplace */
182 0x0, /* src_mask */
183 0x7ff, /* dst_mask */
184 TRUE), /* pcrel_offset */
186 HOWTO (IMAGE_REL_MCORE_RVA, /* type */
187 0, /* rightshift */
188 2, /* size (0 = byte, 1 = short, 2 = long) */
189 32, /* bitsize */
190 FALSE, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_signed, /* complain_on_overflow */
193 NULL, /* special_function */
194 "MCORE_RVA", /* name */
195 TRUE, /* partial_inplace */
196 0xffffffff, /* src_mask */
197 0xffffffff, /* dst_mask */
198 TRUE) /* pcrel_offset */
201 /* Extend the coff_link_hash_table structure with a few M*Core specific fields.
202 This allows us to store global data here without actually creating any
203 global variables, which is a no-no in the BFD world. */
204 typedef struct coff_mcore_link_hash_table
206 /* The original coff_link_hash_table structure. MUST be first field. */
207 struct coff_link_hash_table root;
209 bfd * bfd_of_toc_owner;
210 long int global_toc_size;
211 long int import_table_size;
212 long int first_thunk_address;
213 long int thunk_size;
215 mcore_hash_table;
217 /* Get the MCore coff linker hash table from a link_info structure. */
218 #define coff_mcore_hash_table(info) \
219 ((mcore_hash_table *) ((info)->hash))
222 /* Add an entry to the base file. */
224 static void
225 mcore_emit_base_file_entry (info, output_bfd, input_section, reloc_offset)
226 struct bfd_link_info * info;
227 bfd * output_bfd;
228 asection * input_section;
229 bfd_vma reloc_offset;
231 bfd_vma addr = reloc_offset
232 - input_section->vma
233 + input_section->output_offset
234 + input_section->output_section->vma;
236 if (coff_data (output_bfd)->pe)
237 addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
239 fwrite (&addr, 1, sizeof (addr), (FILE *) info->base_file);
242 static bfd_reloc_status_type
243 mcore_coff_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
244 output_bfd, error_message)
245 bfd * abfd;
246 arelent * reloc_entry;
247 asymbol * symbol ATTRIBUTE_UNUSED;
248 PTR data ATTRIBUTE_UNUSED;
249 asection * input_section ATTRIBUTE_UNUSED;
250 bfd * output_bfd ATTRIBUTE_UNUSED;
251 char ** error_message ATTRIBUTE_UNUSED;
253 BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
255 _bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
256 abfd,
257 reloc_entry->howto->name,
258 reloc_entry->howto->type);
260 return bfd_reloc_notsupported;
263 /* A cheesy little macro to make the code a little more readable. */
264 #define HOW2MAP(bfd_rtype, mcore_rtype) \
265 case bfd_rtype: return & mcore_coff_howto_table [mcore_rtype]
267 static reloc_howto_type *
268 mcore_coff_reloc_type_lookup (abfd, code)
269 bfd * abfd ATTRIBUTE_UNUSED;
270 bfd_reloc_code_real_type code;
272 switch (code)
274 HOW2MAP (BFD_RELOC_32, IMAGE_REL_MCORE_ADDR32);
275 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM8BY4, IMAGE_REL_MCORE_PCREL_IMM8BY4);
276 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM11BY2, IMAGE_REL_MCORE_PCREL_IMM11BY2);
277 HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM4BY2, IMAGE_REL_MCORE_PCREL_IMM4BY2);
278 HOW2MAP (BFD_RELOC_32_PCREL, IMAGE_REL_MCORE_PCREL_32);
279 HOW2MAP (BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2, IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2);
280 HOW2MAP (BFD_RELOC_RVA, IMAGE_REL_MCORE_RVA);
281 default:
282 return NULL;
284 /*NOTREACHED*/
286 #undef HOW2MAP
288 static reloc_howto_type *
289 mcore_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
290 const char *r_name)
292 unsigned int i;
294 for (i = 0;
295 i < (sizeof (mcore_coff_howto_table)
296 / sizeof (mcore_coff_howto_table[0]));
297 i++)
298 if (mcore_coff_howto_table[i].name != NULL
299 && strcasecmp (mcore_coff_howto_table[i].name, r_name) == 0)
300 return &mcore_coff_howto_table[i];
302 return NULL;
305 #define RTYPE2HOWTO(cache_ptr, dst) \
306 (cache_ptr)->howto = mcore_coff_howto_table + (dst)->r_type;
308 static reloc_howto_type *
309 coff_mcore_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
310 bfd * abfd ATTRIBUTE_UNUSED;
311 asection * sec;
312 struct internal_reloc * rel;
313 struct coff_link_hash_entry * h ATTRIBUTE_UNUSED;
314 struct internal_syment * sym;
315 bfd_vma * addendp;
317 reloc_howto_type * howto;
319 if (rel->r_type >= NUM_ELEM (mcore_coff_howto_table))
320 return NULL;
322 howto = mcore_coff_howto_table + rel->r_type;
324 if (rel->r_type == IMAGE_REL_MCORE_RVA)
325 * addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
327 else if (howto->pc_relative)
329 * addendp = sec->vma - 2; /* XXX guess - is this right ? */
331 /* If the symbol is defined, then the generic code is going to
332 add back the symbol value in order to cancel out an
333 adjustment it made to the addend. However, we set the addend
334 to 0 at the start of this function. We need to adjust here,
335 to avoid the adjustment the generic code will make. FIXME:
336 This is getting a bit hackish. */
337 if (sym != NULL && sym->n_scnum != 0)
338 * addendp -= sym->n_value;
340 else
341 * addendp = 0;
343 return howto;
346 /* Return TRUE if this relocation should appear in the output .reloc section.
347 This function is referenced in pe_mkobject in peicode.h. */
349 static bfd_boolean
350 in_reloc_p (abfd, howto)
351 bfd * abfd ATTRIBUTE_UNUSED;
352 reloc_howto_type * howto;
354 return ! howto->pc_relative && howto->type != IMAGE_REL_MCORE_RVA;
357 /* The reloc processing routine for the optimized COFF linker. */
358 static bfd_boolean
359 coff_mcore_relocate_section (output_bfd, info, input_bfd, input_section,
360 contents, relocs, syms, sections)
361 bfd * output_bfd;
362 struct bfd_link_info * info;
363 bfd * input_bfd;
364 asection * input_section;
365 bfd_byte * contents;
366 struct internal_reloc * relocs;
367 struct internal_syment * syms;
368 asection ** sections;
370 struct internal_reloc * rel;
371 struct internal_reloc * relend;
372 bfd_boolean hihalf;
373 bfd_vma hihalf_val;
375 /* If we are performing a relocatable link, we don't need to do a
376 thing. The caller will take care of adjusting the reloc
377 addresses and symbol indices. */
378 if (info->relocatable)
379 return TRUE;
381 /* Check if we have the same endianess */
382 if ( input_bfd->xvec->byteorder != output_bfd->xvec->byteorder
383 && output_bfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
385 (*_bfd_error_handler)
386 (_("%B: compiled for a %s system and target is %s.\n"),
387 input_bfd,
388 bfd_big_endian (input_bfd) ? _("big endian") : _("little endian"),
389 bfd_big_endian (output_bfd) ? _("big endian") : _("little endian"));
391 bfd_set_error (bfd_error_wrong_format);
392 return FALSE;
395 hihalf = FALSE;
396 hihalf_val = 0;
398 rel = relocs;
399 relend = rel + input_section->reloc_count;
401 for (; rel < relend; rel++)
403 long symndx;
404 struct internal_syment * sym;
405 bfd_vma val;
406 bfd_vma addend;
407 bfd_reloc_status_type rstat;
408 bfd_byte * loc;
409 unsigned short r_type = rel->r_type;
410 reloc_howto_type * howto = NULL;
411 struct coff_link_hash_entry * h;
412 const char * my_name;
414 symndx = rel->r_symndx;
415 loc = contents + rel->r_vaddr - input_section->vma;
417 if (symndx == -1)
419 h = NULL;
420 sym = NULL;
422 else
424 h = obj_coff_sym_hashes (input_bfd)[symndx];
425 sym = syms + symndx;
428 addend = 0;
430 /* Get the howto and initialise the addend. */
431 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
432 sym, & addend);
433 if (howto == NULL)
434 return FALSE;
436 val = 0;
438 if (h == NULL)
440 if (symndx == -1)
441 my_name = "*ABS*";
442 else
444 asection * sec = sections[symndx];
446 val = (sym->n_value
447 + sec->output_section->vma
448 + sec->output_offset);
450 if (sym == NULL)
451 my_name = "*unknown*";
452 else if ( sym->_n._n_n._n_zeroes == 0
453 && sym->_n._n_n._n_offset != 0)
454 my_name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
455 else
457 static char buf [SYMNMLEN + 1];
459 strncpy (buf, sym->_n._n_name, SYMNMLEN);
460 buf[SYMNMLEN] = '\0';
461 my_name = buf;
465 else
467 if ( h->root.type == bfd_link_hash_defined
468 || h->root.type == bfd_link_hash_defweak)
470 asection * sec = h->root.u.def.section;
472 val = (h->root.u.def.value
473 + sec->output_section->vma
474 + sec->output_offset);
476 else
478 if (! ((*info->callbacks->undefined_symbol)
479 (info, h->root.root.string, input_bfd, input_section,
480 rel->r_vaddr - input_section->vma, TRUE)))
481 return FALSE;
484 my_name = h->root.root.string;
487 rstat = bfd_reloc_ok;
489 /* Each case must do its own relocation, setting rstat appropriately. */
490 switch (r_type)
492 default:
493 _bfd_error_handler (_("%B: unsupported relocation type 0x%02x"),
494 input_bfd, r_type);
495 bfd_set_error (bfd_error_bad_value);
496 return FALSE;
498 case IMAGE_REL_MCORE_ABSOLUTE:
499 _bfd_error_handler
500 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
501 "sym %ld (%s), r_vaddr %ld (%lx)"),
502 input_bfd, input_section, howto->name,
503 rel->r_symndx, my_name, (long) rel->r_vaddr,
504 (unsigned long) rel->r_vaddr);
505 break;
507 case IMAGE_REL_MCORE_PCREL_IMM8BY4:
508 case IMAGE_REL_MCORE_PCREL_IMM11BY2:
509 case IMAGE_REL_MCORE_PCREL_IMM4BY2:
510 case IMAGE_REL_MCORE_PCREL_32:
511 case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2:
512 case IMAGE_REL_MCORE_ADDR32:
513 /* XXX fixme - shouldn't this be like the code for the RVA reloc ? */
514 rstat = _bfd_relocate_contents (howto, input_bfd, val, loc);
515 break;
517 case IMAGE_REL_MCORE_RVA:
518 rstat = _bfd_final_link_relocate
519 (howto, input_bfd,
520 input_section, contents, rel->r_vaddr - input_section->vma,
521 val, addend);
522 break;
525 if (info->base_file)
527 /* Emit a reloc if the backend thinks it needs it. */
528 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
529 mcore_emit_base_file_entry (info, output_bfd, input_section, rel->r_vaddr);
532 switch (rstat)
534 default:
535 abort ();
537 case bfd_reloc_ok:
538 break;
540 case bfd_reloc_overflow:
541 if (! ((*info->callbacks->reloc_overflow)
542 (info, (h ? &h->root : NULL), my_name, howto->name,
543 (bfd_vma) 0, input_bfd,
544 input_section, rel->r_vaddr - input_section->vma)))
545 return FALSE;
549 return TRUE;
552 /* Tailor coffcode.h -- macro heaven. */
554 /* We use the special COFF backend linker, with our own special touch. */
556 #define coff_bfd_reloc_type_lookup mcore_coff_reloc_type_lookup
557 #define coff_bfd_reloc_name_lookup mcore_coff_reloc_name_lookup
558 #define coff_relocate_section coff_mcore_relocate_section
559 #define coff_rtype_to_howto coff_mcore_rtype_to_howto
561 #define SELECT_RELOC(internal, howto) {internal.r_type = howto->type;}
563 /* Make sure that the 'r_offset' field is copied properly
564 so that identical binaries will compare the same. */
565 #define SWAP_IN_RELOC_OFFSET H_GET_32
566 #define SWAP_OUT_RELOC_OFFSET H_PUT_32
568 #define COFF_PAGE_SIZE 0x1000
570 #include "coffcode.h"
572 /* Forward declaration to initialise alternative_target field. */
573 extern const bfd_target TARGET_LITTLE_SYM;
575 /* The transfer vectors that lead the outside world to all of the above. */
576 CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM, TARGET_BIG_NAME, D_PAGED,
577 (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
578 0, & TARGET_LITTLE_SYM, COFF_SWAP_TABLE)
579 CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM, TARGET_LITTLE_NAME, D_PAGED,
580 (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
581 0, & TARGET_BIG_SYM, COFF_SWAP_TABLE)