bfd/
[deliverable/binutils-gdb.git] / bfd / elf32-mt.c
CommitLineData
d031aafb 1/* Morpho Technologies MT specific support for 32-bit ELF
ab96bf03 2 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007
de33e640
AH
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "elf-bfd.h"
4970f871 25#include "elf/mt.h"
de33e640
AH
26
27/* Prototypes. */
d031aafb 28static reloc_howto_type * mt_reloc_type_lookup
de33e640
AH
29 (bfd *, bfd_reloc_code_real_type);
30
d031aafb 31static void mt_info_to_howto_rela
de33e640
AH
32 (bfd *, arelent *, Elf_Internal_Rela *);
33
d031aafb 34static bfd_reloc_status_type mt_elf_relocate_hi16
de33e640
AH
35 (bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma);
36
d031aafb 37static bfd_reloc_status_type mt_final_link_relocate
de33e640
AH
38 (reloc_howto_type *, bfd *, asection *, bfd_byte *,
39 Elf_Internal_Rela *, bfd_vma);
40
d031aafb 41static bfd_boolean mt_elf_relocate_section
de33e640
AH
42 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
43 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
44
45/* Relocation tables. */
d031aafb 46static reloc_howto_type mt_elf_howto_table [] =
de33e640
AH
47{
48 /* This reloc does nothing. */
d031aafb 49 HOWTO (R_MT_NONE, /* type */
de33e640
AH
50 0, /* rightshift */
51 2, /* size (0 = byte, 1 = short, 2 = long) */
52 32, /* bitsize */
53 FALSE, /* pc_relative */
54 0, /* bitpos */
55 complain_overflow_dont, /* complain_on_overflow */
56 bfd_elf_generic_reloc, /* special_function */
d031aafb 57 "R_MT_NONE", /* name */
de33e640
AH
58 FALSE, /* partial_inplace */
59 0 , /* src_mask */
60 0, /* dst_mask */
61 FALSE), /* pcrel_offset */
62
63 /* A 16 bit absolute relocation. */
d031aafb 64 HOWTO (R_MT_16, /* type */
de33e640
AH
65 0, /* rightshift */
66 2, /* size (0 = byte, 1 = short, 2 = long) */
67 16, /* bitsize */
68 FALSE, /* pc_relative */
69 0, /* bitpos */
70 complain_overflow_dont, /* complain_on_overflow */
71 bfd_elf_generic_reloc, /* special_function */
d031aafb 72 "R_MT_16", /* name */
de33e640
AH
73 FALSE, /* partial_inplace */
74 0 , /* src_mask */
75 0xffff, /* dst_mask */
76 FALSE), /* pcrel_offset */
77
78 /* A 32 bit absolute relocation. */
d031aafb 79 HOWTO (R_MT_32, /* type */
de33e640
AH
80 0, /* rightshift */
81 2, /* size (0 = byte, 1 = short, 2 = long) */
82 32, /* bitsize */
83 FALSE, /* pc_relative */
84 0, /* bitpos */
85 complain_overflow_dont, /* complain_on_overflow */
86 bfd_elf_generic_reloc, /* special_function */
d031aafb 87 "R_MT_32", /* name */
de33e640
AH
88 FALSE, /* partial_inplace */
89 0 , /* src_mask */
90 0xffffffff, /* dst_mask */
91 FALSE), /* pcrel_offset */
92
93 /* A 32 bit pc-relative relocation. */
d031aafb 94 HOWTO (R_MT_32_PCREL, /* type */
de33e640
AH
95 0, /* rightshift */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
97 32, /* bitsize */
98 TRUE, /* pc_relative */
99 0, /* bitpos */
100 complain_overflow_dont, /* complain_on_overflow */
101 bfd_elf_generic_reloc, /* special_function */
d031aafb 102 "R_MT_32_PCREL", /* name */
de33e640
AH
103 FALSE, /* partial_inplace */
104 0 , /* src_mask */
105 0xffffffff, /* dst_mask */
106 TRUE), /* pcrel_offset */
107
108 /* A 16 bit pc-relative relocation. */
d031aafb 109 HOWTO (R_MT_PC16, /* type */
de33e640
AH
110 0, /* rightshift */
111 2, /* size (0 = byte, 1 = short, 2 = long) */
112 16, /* bitsize */
113 TRUE, /* pc_relative */
114 0, /* bitpos */
115 complain_overflow_signed, /* complain_on_overflow */
116 bfd_elf_generic_reloc, /* special_function */
d031aafb 117 "R_MT_PC16", /* name */
de33e640
AH
118 FALSE, /* partial_inplace */
119 0, /* src_mask */
120 0xffff, /* dst_mask */
121 TRUE), /* pcrel_offset */
122
123 /* high 16 bits of symbol value. */
d031aafb 124 HOWTO (R_MT_HI16, /* type */
de33e640
AH
125 0, /* rightshift */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
127 16, /* bitsize */
128 FALSE, /* pc_relative */
129 0, /* bitpos */
130 complain_overflow_dont, /* complain_on_overflow */
131 bfd_elf_generic_reloc, /* special_function */
d031aafb 132 "R_MT_HI16", /* name */
de33e640
AH
133 FALSE, /* partial_inplace */
134 0xffff0000, /* src_mask */
135 0xffff0000, /* dst_mask */
136 FALSE), /* pcrel_offset */
137
138 /* Low 16 bits of symbol value. */
d031aafb 139 HOWTO (R_MT_LO16, /* type */
de33e640
AH
140 0, /* rightshift */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
142 16, /* bitsize */
143 FALSE, /* pc_relative */
144 0, /* bitpos */
145 complain_overflow_dont, /* complain_on_overflow */
146 bfd_elf_generic_reloc, /* special_function */
d031aafb 147 "R_MT_LO16", /* name */
de33e640
AH
148 FALSE, /* partial_inplace */
149 0xffff, /* src_mask */
150 0xffff, /* dst_mask */
151 FALSE), /* pcrel_offset */
152};
153
d031aafb 154/* Map BFD reloc types to MT ELF reloc types. */
de33e640
AH
155
156static reloc_howto_type *
d031aafb 157mt_reloc_type_lookup
de33e640
AH
158 (bfd * abfd ATTRIBUTE_UNUSED,
159 bfd_reloc_code_real_type code)
160{
d031aafb 161 /* Note that the mt_elf_howto_table is indxed by the R_
de33e640
AH
162 constants. Thus, the order that the howto records appear in the
163 table *must* match the order of the relocation types defined in
d031aafb 164 include/elf/mt.h. */
de33e640
AH
165
166 switch (code)
167 {
168 case BFD_RELOC_NONE:
d031aafb 169 return &mt_elf_howto_table[ (int) R_MT_NONE];
de33e640 170 case BFD_RELOC_16:
d031aafb 171 return &mt_elf_howto_table[ (int) R_MT_16];
de33e640 172 case BFD_RELOC_32:
d031aafb 173 return &mt_elf_howto_table[ (int) R_MT_32];
de33e640 174 case BFD_RELOC_32_PCREL:
d031aafb 175 return &mt_elf_howto_table[ (int) R_MT_32_PCREL];
de33e640 176 case BFD_RELOC_16_PCREL:
d031aafb 177 return &mt_elf_howto_table[ (int) R_MT_PC16];
de33e640 178 case BFD_RELOC_HI16:
d031aafb 179 return &mt_elf_howto_table[ (int) R_MT_HI16];
de33e640 180 case BFD_RELOC_LO16:
d031aafb 181 return &mt_elf_howto_table[ (int) R_MT_LO16];
de33e640
AH
182
183 default:
184 /* Pacify gcc -Wall. */
185 return NULL;
186 }
187 return NULL;
188}
189
190bfd_reloc_status_type
d031aafb 191mt_elf_relocate_hi16
de33e640
AH
192 (bfd * input_bfd,
193 Elf_Internal_Rela * relhi,
194 bfd_byte * contents,
195 bfd_vma value)
196{
197 bfd_vma insn;
198
199 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
200
201 value += relhi->r_addend;
202 value >>= 16;
203 insn = ((insn & ~0xFFFF) | value);
204
205 bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
206 return bfd_reloc_ok;
207}
208\f
209/* XXX: The following code is the result of a cut&paste. This unfortunate
210 practice is very widespread in the various target back-end files. */
211
d031aafb 212/* Set the howto pointer for a MT ELF reloc. */
de33e640
AH
213
214static void
d031aafb 215mt_info_to_howto_rela
de33e640
AH
216 (bfd * abfd ATTRIBUTE_UNUSED,
217 arelent * cache_ptr,
218 Elf_Internal_Rela * dst)
219{
220 unsigned int r_type;
221
222 r_type = ELF32_R_TYPE (dst->r_info);
d031aafb 223 cache_ptr->howto = & mt_elf_howto_table [r_type];
de33e640
AH
224}
225
226/* Perform a single relocation. By default we use the standard BFD
227 routines. */
228
229static bfd_reloc_status_type
d031aafb 230mt_final_link_relocate
de33e640
AH
231 (reloc_howto_type * howto,
232 bfd * input_bfd,
233 asection * input_section,
234 bfd_byte * contents,
235 Elf_Internal_Rela * rel,
236 bfd_vma relocation)
237{
238 return _bfd_final_link_relocate (howto, input_bfd, input_section,
239 contents, rel->r_offset,
240 relocation, rel->r_addend);
241}
242
d031aafb 243/* Relocate a MT ELF section.
de33e640
AH
244 There is some attempt to make this function usable for many architectures,
245 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
246 if only to serve as a learning tool.
247
248 The RELOCATE_SECTION function is called by the new ELF backend linker
249 to handle the relocations for a section.
250
251 The relocs are always passed as Rela structures; if the section
252 actually uses Rel structures, the r_addend field will always be
253 zero.
254
255 This function is responsible for adjusting the section contents as
256 necessary, and (if using Rela relocs and generating a relocatable
257 output file) adjusting the reloc addend as necessary.
258
259 This function does not have to worry about setting the reloc
260 address or the reloc symbol index.
261
262 LOCAL_SYMS is a pointer to the swapped in local symbols.
263
264 LOCAL_SECTIONS is an array giving the section in the input file
265 corresponding to the st_shndx field of each local symbol.
266
267 The global hash table entry for the global symbols can be found
268 via elf_sym_hashes (input_bfd).
269
270 When generating relocatable output, this function must handle
271 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
272 going to be the section symbol corresponding to the output
273 section, which means that the addend must be adjusted
274 accordingly. */
275
276static bfd_boolean
d031aafb 277mt_elf_relocate_section
de33e640
AH
278 (bfd * output_bfd ATTRIBUTE_UNUSED,
279 struct bfd_link_info * info,
280 bfd * input_bfd,
281 asection * input_section,
282 bfd_byte * contents,
283 Elf_Internal_Rela * relocs,
284 Elf_Internal_Sym * local_syms,
285 asection ** local_sections)
286{
287 Elf_Internal_Shdr * symtab_hdr;
288 struct elf_link_hash_entry ** sym_hashes;
289 Elf_Internal_Rela * rel;
290 Elf_Internal_Rela * relend;
291
292 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
293 sym_hashes = elf_sym_hashes (input_bfd);
294 relend = relocs + input_section->reloc_count;
295
296 for (rel = relocs; rel < relend; rel ++)
297 {
298 reloc_howto_type * howto;
299 unsigned long r_symndx;
300 Elf_Internal_Sym * sym;
301 asection * sec;
302 struct elf_link_hash_entry * h;
303 bfd_vma relocation;
304 bfd_reloc_status_type r;
305 const char * name = NULL;
306 int r_type;
307
308 r_type = ELF32_R_TYPE (rel->r_info);
309
310 r_symndx = ELF32_R_SYM (rel->r_info);
311
d031aafb 312 howto = mt_elf_howto_table + ELF32_R_TYPE (rel->r_info);
de33e640
AH
313 h = NULL;
314 sym = NULL;
315 sec = NULL;
316
317 if (r_symndx < symtab_hdr->sh_info)
318 {
319 sym = local_syms + r_symndx;
320 sec = local_sections [r_symndx];
321 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
322
323 name = bfd_elf_string_from_elf_section
324 (input_bfd, symtab_hdr->sh_link, sym->st_name);
325 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
326 }
327 else
328 {
329 bfd_boolean unresolved_reloc;
330 bfd_boolean warned;
331
332 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
333 r_symndx, symtab_hdr, sym_hashes,
334 h, sec, relocation,
335 unresolved_reloc, warned);
336
337 name = h->root.root.string;
338 }
339
ab96bf03
AM
340 if (sec != NULL && elf_discarded_section (sec))
341 {
342 /* For relocs against symbols from removed linkonce sections,
343 or sections discarded by a linker script, we just want the
344 section contents zeroed. Avoid any special processing. */
345 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
346 rel->r_info = 0;
347 rel->r_addend = 0;
348 continue;
349 }
350
351 if (info->relocatable)
352 continue;
de33e640 353
d031aafb 354 /* Finally, the sole MT-specific part. */
de33e640
AH
355 switch (r_type)
356 {
d031aafb
NS
357 case R_MT_HI16:
358 r = mt_elf_relocate_hi16 (input_bfd, rel, contents, relocation);
de33e640
AH
359 break;
360 default:
d031aafb 361 r = mt_final_link_relocate (howto, input_bfd, input_section,
de33e640
AH
362 contents, rel, relocation);
363 break;
364 }
365
366
367 if (r != bfd_reloc_ok)
368 {
369 const char * msg = (const char *) NULL;
370
371 switch (r)
372 {
373 case bfd_reloc_overflow:
374 r = info->callbacks->reloc_overflow
375 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
376 input_bfd, input_section, rel->r_offset);
377 break;
378
379 case bfd_reloc_undefined:
380 r = info->callbacks->undefined_symbol
381 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
382 break;
383
384 case bfd_reloc_outofrange:
385 msg = _("internal error: out of range error");
386 break;
387
388 case bfd_reloc_dangerous:
389 msg = _("internal error: dangerous relocation");
390 break;
391
392 default:
393 msg = _("internal error: unknown error");
394 break;
395 }
396
397 if (msg)
398 r = info->callbacks->warning
399 (info, msg, name, input_bfd, input_section, rel->r_offset);
400
401 if (! r)
402 return FALSE;
403 }
404 }
405
406 return TRUE;
407}
408
de33e640
AH
409/* Look through the relocs for a section during the first phase.
410 Since we don't do .gots or .plts, we just need to consider the
411 virtual table relocs for gc. */
412
413static bfd_boolean
d031aafb 414mt_elf_check_relocs
de33e640
AH
415 (bfd * abfd,
416 struct bfd_link_info * info,
417 asection * sec,
418 const Elf_Internal_Rela * relocs)
419{
420 Elf_Internal_Shdr * symtab_hdr;
421 struct elf_link_hash_entry ** sym_hashes;
422 struct elf_link_hash_entry ** sym_hashes_end;
423 const Elf_Internal_Rela * rel;
424 const Elf_Internal_Rela * rel_end;
425
426 if (info->relocatable)
427 return TRUE;
428
429 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
430 sym_hashes = elf_sym_hashes (abfd);
431 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
432 if (!elf_bad_symtab (abfd))
433 sym_hashes_end -= symtab_hdr->sh_info;
434
435 rel_end = relocs + sec->reloc_count;
436 for (rel = relocs; rel < rel_end; rel++)
437 {
438 struct elf_link_hash_entry *h;
439 unsigned long r_symndx;
440
441 r_symndx = ELF32_R_SYM (rel->r_info);
442 if (r_symndx < symtab_hdr->sh_info)
443 h = NULL;
444 else
973a3492
L
445 {
446 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
447 while (h->root.type == bfd_link_hash_indirect
448 || h->root.type == bfd_link_hash_warning)
449 h = (struct elf_link_hash_entry *) h->root.u.i.link;
450 }
de33e640
AH
451 }
452
453 return TRUE;
454}
455
456/* Return the MACH for an e_flags value. */
457
458static int
d031aafb 459elf32_mt_machine (bfd *abfd)
de33e640 460{
d031aafb 461 switch (elf_elfheader (abfd)->e_flags & EF_MT_CPU_MASK)
de33e640 462 {
d031aafb
NS
463 case EF_MT_CPU_MRISC: return bfd_mach_ms1;
464 case EF_MT_CPU_MRISC2: return bfd_mach_mrisc2;
465 case EF_MT_CPU_MS2: return bfd_mach_ms2;
de33e640
AH
466 }
467
468 return bfd_mach_ms1;
469}
470
471static bfd_boolean
d031aafb 472mt_elf_object_p (bfd * abfd)
de33e640 473{
d031aafb 474 bfd_default_set_arch_mach (abfd, bfd_arch_mt, elf32_mt_machine (abfd));
de33e640
AH
475
476 return TRUE;
477}
478
479/* Function to set the ELF flag bits. */
480
481static bfd_boolean
d031aafb 482mt_elf_set_private_flags (bfd * abfd,
de33e640
AH
483 flagword flags)
484{
485 elf_elfheader (abfd)->e_flags = flags;
486 elf_flags_init (abfd) = TRUE;
487 return TRUE;
488}
489
490static bfd_boolean
d031aafb 491mt_elf_copy_private_bfd_data (bfd * ibfd, bfd * obfd)
de33e640
AH
492{
493 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
494 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
495 return TRUE;
496
497 BFD_ASSERT (!elf_flags_init (obfd)
498 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
499
500 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
501 elf_flags_init (obfd) = TRUE;
502 return TRUE;
503}
504
505/* Merge backend specific data from an object file to the output
506 object file when linking. */
507
508static bfd_boolean
d031aafb 509mt_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
de33e640
AH
510{
511 flagword old_flags, new_flags;
e510ff97 512 bfd_boolean ok = TRUE;
de33e640
AH
513
514 /* Check if we have the same endianess. */
515 if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
516 return FALSE;
517
d031aafb 518 /* If they're not both mt, then merging is meaningless, so just
de33e640 519 don't do it. */
d031aafb 520 if (strcmp (ibfd->arch_info->arch_name, "mt") != 0)
de33e640 521 return TRUE;
d031aafb 522 if (strcmp (obfd->arch_info->arch_name, "mt") != 0)
de33e640
AH
523 return TRUE;
524
525 new_flags = elf_elfheader (ibfd)->e_flags;
526 old_flags = elf_elfheader (obfd)->e_flags;
527
528#ifdef DEBUG
529 _bfd_error_handler ("%B: old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s",
530 ibfd, old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no");
531#endif
532
6f84a2a6
NS
533 if (!elf_flags_init (obfd))
534 {
535 old_flags = new_flags;
536 elf_flags_init (obfd) = TRUE;
537 }
d031aafb 538 else if ((new_flags & EF_MT_CPU_MASK) != (old_flags & EF_MT_CPU_MASK))
6f84a2a6
NS
539 {
540 /* CPU has changed. This is invalid, because MRISC, MRISC2 and
541 MS2 are not subsets of each other. */
e510ff97 542 ok = FALSE;
6f84a2a6 543 }
e510ff97
NS
544
545 if (ok)
de33e640 546 {
de33e640 547 obfd->arch_info = ibfd->arch_info;
6f84a2a6 548 elf_elfheader (obfd)->e_flags = old_flags;
de33e640
AH
549 }
550
e510ff97 551 return ok;
de33e640
AH
552}
553
554static bfd_boolean
d031aafb 555mt_elf_print_private_bfd_data (bfd * abfd, void * ptr)
de33e640
AH
556{
557 FILE * file = (FILE *) ptr;
558 flagword flags;
559
560 BFD_ASSERT (abfd != NULL && ptr != NULL);
561
562 /* Print normal ELF private data. */
563 _bfd_elf_print_private_bfd_data (abfd, ptr);
564
565 flags = elf_elfheader (abfd)->e_flags;
566 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
567
d031aafb 568 switch (flags & EF_MT_CPU_MASK)
de33e640
AH
569 {
570 default:
d031aafb
NS
571 case EF_MT_CPU_MRISC: fprintf (file, " ms1-16-002"); break;
572 case EF_MT_CPU_MRISC2: fprintf (file, " ms1-16-003"); break;
573 case EF_MT_CPU_MS2: fprintf (file, " ms2"); break;
de33e640
AH
574 }
575
576 fputc ('\n', file);
577
578 return TRUE;
579}
580
581\f
d031aafb
NS
582#define TARGET_BIG_SYM bfd_elf32_mt_vec
583#define TARGET_BIG_NAME "elf32-mt"
de33e640 584
d031aafb
NS
585#define ELF_ARCH bfd_arch_mt
586#define ELF_MACHINE_CODE EM_MT
587#define ELF_MAXPAGESIZE 1 /* No pages on the MT. */
de33e640
AH
588
589#define elf_info_to_howto_rel NULL
d031aafb 590#define elf_info_to_howto mt_info_to_howto_rela
de33e640 591
d031aafb 592#define elf_backend_relocate_section mt_elf_relocate_section
de33e640 593
d031aafb 594#define bfd_elf32_bfd_reloc_type_lookup mt_reloc_type_lookup
de33e640 595
d031aafb
NS
596#define elf_backend_check_relocs mt_elf_check_relocs
597#define elf_backend_object_p mt_elf_object_p
de33e640
AH
598#define elf_backend_rela_normal 1
599
600#define elf_backend_can_gc_sections 1
601
d031aafb
NS
602#define bfd_elf32_bfd_set_private_flags mt_elf_set_private_flags
603#define bfd_elf32_bfd_copy_private_bfd_data mt_elf_copy_private_bfd_data
604#define bfd_elf32_bfd_merge_private_bfd_data mt_elf_merge_private_bfd_data
605#define bfd_elf32_bfd_print_private_bfd_data mt_elf_print_private_bfd_data
de33e640
AH
606
607#include "elf32-target.h"
This page took 0.11455 seconds and 4 git commands to generate.