Update copyright years
[external/binutils.git] / bfd / coff-or32.c
1 /* BFD back-end for OpenRISC 1000 COFF binaries.
2    Copyright (C) 2002-2014 Free Software Foundation, Inc.
3    Contributed by Ivan Guzvinec  <ivang@opencores.org>
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #define OR32 1
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libbfd.h"
27 #include "coff/or32.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 static bfd_reloc_status_type or32_reloc
32   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
33
34 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
35
36 #define INSERT_HWORD(WORD,HWORD)              \
37     (((WORD) & 0xffff0000) | ((HWORD)& 0x0000ffff))
38 #define EXTRACT_HWORD(WORD)                   \
39     ((WORD) & 0x0000ffff)
40 #define SIGN_EXTEND_HWORD(HWORD)              \
41     ((HWORD) & 0x8000 ? (HWORD)|(~0xffffL) : (HWORD))
42
43 #define INSERT_JUMPTARG(WORD,JT)              \
44     (((WORD) & 0xfc000000) | ((JT)& 0x03ffffff))
45 #define EXTRACT_JUMPTARG(WORD)                   \
46     ((WORD) & 0x03ffffff)
47 #define SIGN_EXTEND_JUMPTARG(JT)              \
48     ((JT) & 0x04000000 ? (JT)|(~0x03ffffffL) : (JT))
49
50 /* Provided the symbol, returns the value reffed.  */
51
52 static long
53 get_symbol_value (asymbol *symbol)
54 {
55   long relocation = 0;
56
57   if (bfd_is_com_section (symbol->section))
58     relocation = 0;
59   else
60     relocation = symbol->value +
61       symbol->section->output_section->vma +
62       symbol->section->output_offset;
63
64   return relocation;
65 }
66
67 /* This function is in charge of performing all the or32 relocations.  */
68
69 static bfd_reloc_status_type
70 or32_reloc (bfd *abfd,
71             arelent *reloc_entry,
72             asymbol *symbol_in,
73             void * data,
74             asection *input_section,
75             bfd *output_bfd,
76             char **error_message)
77 {
78   /* The consth relocation comes in two parts, we have to remember
79      the state between calls, in these variables.  */
80   static bfd_boolean part1_consth_active = FALSE;
81   static unsigned long part1_consth_value;
82
83   unsigned long insn;
84   unsigned long sym_value;
85   unsigned long unsigned_value;
86   unsigned short r_type;
87   long signed_value;
88
89   unsigned long addr = reloc_entry->address ;   /*+ input_section->vma*/
90   bfd_byte *hit_data =addr + (bfd_byte *)(data);
91
92   r_type = reloc_entry->howto->type;
93
94   if (output_bfd)
95     {
96       /* Partial linking - do nothing.  */
97       reloc_entry->address += input_section->output_offset;
98       return bfd_reloc_ok;
99     }
100
101   if (symbol_in != NULL
102       && bfd_is_und_section (symbol_in->section))
103     {
104       /* Keep the state machine happy in case we're called again.  */
105       if (r_type == R_IHIHALF)
106         {
107           part1_consth_active = TRUE;
108           part1_consth_value  = 0;
109         }
110
111       return bfd_reloc_undefined;
112     }
113
114   if ((part1_consth_active) && (r_type != R_IHCONST))
115     {
116       part1_consth_active = FALSE;
117       *error_message = (char *) "Missing IHCONST";
118
119       return bfd_reloc_dangerous;
120     }
121
122   sym_value = get_symbol_value (symbol_in);
123
124   switch (r_type)
125     {
126     case R_IREL:
127       insn = bfd_get_32(abfd, hit_data);
128
129       /* Take the value in the field and sign extend it.  */
130       signed_value = EXTRACT_JUMPTARG (insn);
131       signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
132       signed_value <<= 2;
133
134       /* See the note on the R_IREL reloc in coff_or32_relocate_section.  */
135       if (signed_value == - (long) reloc_entry->address)
136         signed_value = 0;
137
138       signed_value += sym_value + reloc_entry->addend;
139       /* Relative jmp/call, so subtract from the value the
140          address of the place we're coming from.  */
141       signed_value -= (reloc_entry->address
142                        + input_section->output_section->vma
143                        + input_section->output_offset);
144       if (signed_value > 0x7ffffff || signed_value < -0x8000000)
145         return bfd_reloc_overflow;
146
147       signed_value >>= 2;
148       insn = INSERT_JUMPTARG (insn, signed_value);
149       bfd_put_32 (abfd, insn, hit_data);
150       break;
151
152     case R_ILOHALF:
153       insn = bfd_get_32 (abfd, hit_data);
154       unsigned_value = EXTRACT_HWORD (insn);
155       unsigned_value +=  sym_value + reloc_entry->addend;
156       insn = INSERT_HWORD (insn, unsigned_value);
157       bfd_put_32 (abfd, insn, hit_data);
158       break;
159
160     case R_IHIHALF:
161       insn = bfd_get_32 (abfd, hit_data);
162
163       /* consth, part 1
164          Just get the symbol value that is referenced.  */
165       part1_consth_active = TRUE;
166       part1_consth_value = sym_value + reloc_entry->addend;
167
168       /* Don't modify insn until R_IHCONST.  */
169       break;
170
171     case R_IHCONST:
172       insn = bfd_get_32 (abfd, hit_data);
173
174       /* consth, part 2
175          Now relocate the reference.  */
176       if (! part1_consth_active)
177         {
178           *error_message = (char *) "Missing IHIHALF";
179           return bfd_reloc_dangerous;
180         }
181
182       /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
183       unsigned_value = 0;   /*EXTRACT_HWORD(insn) << 16;*/
184       unsigned_value += reloc_entry->addend; /* r_symndx */
185       unsigned_value += part1_consth_value;
186       unsigned_value = unsigned_value >> 16;
187       insn = INSERT_HWORD (insn, unsigned_value);
188       part1_consth_active = FALSE;
189       bfd_put_32 (abfd, insn, hit_data);
190       break;
191
192     case R_BYTE:
193       insn = bfd_get_8 (abfd, hit_data);
194       unsigned_value = insn + sym_value + reloc_entry->addend;
195       if (unsigned_value & 0xffffff00)
196         return bfd_reloc_overflow;
197       bfd_put_8 (abfd, unsigned_value, hit_data);
198       break;
199
200     case R_HWORD:
201       insn = bfd_get_16 (abfd, hit_data);
202       unsigned_value = insn + sym_value + reloc_entry->addend;
203       if (unsigned_value & 0xffff0000)
204         return bfd_reloc_overflow;
205       bfd_put_16 (abfd, insn, hit_data);
206       break;
207
208     case R_WORD:
209       insn = bfd_get_32 (abfd, hit_data);
210       insn += sym_value + reloc_entry->addend;
211       bfd_put_32 (abfd, insn, hit_data);
212       break;
213
214     default:
215       *error_message = _("Unrecognized reloc");
216       return bfd_reloc_dangerous;
217     }
218
219   return bfd_reloc_ok;
220 }
221
222 /*      type     rightshift
223            size
224         bitsize
225              pc-relative
226              bitpos
227            absolute
228                complain_on_overflow
229               special_function
230                 relocation name
231                      partial_inplace
232                       src_mask
233 */
234
235 /* FIXME: I'm not real sure about this table.  */
236 static reloc_howto_type howto_table[] =
237 {
238   { R_ABS,      0, 3, 32, FALSE,  0, complain_overflow_bitfield,  or32_reloc, "ABS",     TRUE, 0xffffffff,0xffffffff, FALSE },
239     EMPTY_HOWTO (1),
240     EMPTY_HOWTO (2),
241     EMPTY_HOWTO (3),
242     EMPTY_HOWTO (4),
243     EMPTY_HOWTO (5),
244     EMPTY_HOWTO (6),
245     EMPTY_HOWTO (7),
246     EMPTY_HOWTO (8),
247     EMPTY_HOWTO (9),
248     EMPTY_HOWTO (10),
249     EMPTY_HOWTO (11),
250     EMPTY_HOWTO (12),
251     EMPTY_HOWTO (13),
252     EMPTY_HOWTO (14),
253     EMPTY_HOWTO (15),
254     EMPTY_HOWTO (16),
255     EMPTY_HOWTO (17),
256     EMPTY_HOWTO (18),
257     EMPTY_HOWTO (19),
258     EMPTY_HOWTO (20),
259     EMPTY_HOWTO (21),
260     EMPTY_HOWTO (22),
261     EMPTY_HOWTO (23),
262   { R_IREL,     0, 3, 32, TRUE,   0, complain_overflow_signed,    or32_reloc, "IREL",    TRUE, 0xffffffff,0xffffffff, FALSE },
263   { R_IABS,     0, 3, 32, FALSE,  0, complain_overflow_bitfield,  or32_reloc, "IABS",    TRUE, 0xffffffff,0xffffffff, FALSE },
264   { R_ILOHALF,  0, 3, 16, TRUE,   0, complain_overflow_signed,    or32_reloc, "ILOHALF", TRUE, 0x0000ffff,0x0000ffff, FALSE },
265   { R_IHIHALF,  0, 3, 16, TRUE,   16,complain_overflow_signed,    or32_reloc, "IHIHALF", TRUE, 0xffff0000,0xffff0000, FALSE },
266   { R_IHCONST,  0, 3, 16, TRUE,   0, complain_overflow_signed,    or32_reloc, "IHCONST", TRUE, 0xffff0000,0xffff0000, FALSE },
267   { R_BYTE,     0, 0, 8,  FALSE,  0, complain_overflow_bitfield,  or32_reloc, "BYTE",    TRUE, 0x000000ff,0x000000ff, FALSE },
268   { R_HWORD,    0, 1, 16, FALSE,  0, complain_overflow_bitfield,  or32_reloc, "HWORD",   TRUE, 0x0000ffff,0x0000ffff, FALSE },
269   { R_WORD,     0, 2, 32, FALSE,  0, complain_overflow_bitfield,  or32_reloc, "WORD",    TRUE, 0xffffffff,0xffffffff, FALSE },
270 };
271
272 #define BADMAG(x) OR32BADMAG (x)
273
274 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
275   reloc_processing (relent, reloc, symbols, abfd, section)
276
277 static void
278 reloc_processing (arelent *relent,
279                   struct internal_reloc *reloc,
280                   asymbol **symbols,
281                   bfd *abfd,
282                   asection *section)
283 {
284   static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
285
286   relent->address = reloc->r_vaddr;
287   relent->howto = howto_table + reloc->r_type;
288
289   if (reloc->r_type == R_IHCONST)
290     {
291       /* The address of an R_IHCONST should always be the address of
292          the immediately preceding R_IHIHALF.  relocs generated by gas
293          are correct, but relocs generated by High C are different (I
294          can't figure out what the address means for High C).  We can
295          handle both gas and High C by ignoring the address here, and
296          simply reusing the address saved for R_IHIHALF.  */
297       if (ihihalf_vaddr == (bfd_vma) -1)
298         abort ();
299
300       relent->address = ihihalf_vaddr;
301       ihihalf_vaddr = (bfd_vma) -1;
302       relent->addend = reloc->r_symndx;
303       relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
304     }
305   else
306     {
307       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
308       relent->addend = 0;
309       relent->address-= section->vma;
310
311       if (reloc->r_type == R_IHIHALF)
312         ihihalf_vaddr = relent->address;
313       else if (ihihalf_vaddr != (bfd_vma) -1)
314         abort ();
315     }
316 }
317
318 /* The reloc processing routine for the optimized COFF linker.  */
319
320 static bfd_boolean
321 coff_or32_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
322                             struct bfd_link_info *info,
323                             bfd *input_bfd,
324                             asection *input_section,
325                             bfd_byte *contents,
326                             struct internal_reloc *relocs,
327                             struct internal_syment *syms,
328                             asection **sections)
329 {
330   struct internal_reloc *rel;
331   struct internal_reloc *relend;
332   bfd_boolean hihalf;
333   bfd_vma hihalf_val;
334
335   /* If we are performing a relocatable link, we don't need to do a
336      thing.  The caller will take care of adjusting the reloc
337      addresses and symbol indices.  */
338   if (info->relocatable)
339     return TRUE;
340
341   hihalf = FALSE;
342   hihalf_val = 0;
343
344   rel = relocs;
345   relend = rel + input_section->reloc_count;
346
347   for (; rel < relend; rel++)
348     {
349       long symndx;
350       bfd_byte *loc;
351       struct coff_link_hash_entry *h;
352       struct internal_syment *sym;
353       asection *sec;
354       bfd_vma val;
355       bfd_boolean overflow;
356       unsigned long insn;
357       long signed_value;
358       unsigned long unsigned_value;
359       bfd_reloc_status_type rstat;
360
361       symndx = rel->r_symndx;
362       loc = contents + rel->r_vaddr - input_section->vma;
363
364       if (symndx == -1 || rel->r_type == R_IHCONST)
365         h = NULL;
366       else
367         h = obj_coff_sym_hashes (input_bfd)[symndx];
368
369       sym = NULL;
370       sec = NULL;
371       val = 0;
372
373       /* An R_IHCONST reloc does not have a symbol.  Instead, the
374          symbol index is an addend.  R_IHCONST is always used in
375          conjunction with R_IHHALF.  */
376       if (rel->r_type != R_IHCONST)
377         {
378           if (h == NULL)
379             {
380               if (symndx == -1)
381                 sec = bfd_abs_section_ptr;
382               else
383                 {
384                   sym = syms + symndx;
385                   sec = sections[symndx];
386                   val = (sec->output_section->vma
387                          + sec->output_offset
388                          + sym->n_value
389                          - sec->vma);
390                 }
391             }
392           else
393             {
394               if (h->root.type == bfd_link_hash_defined
395                   || h->root.type == bfd_link_hash_defweak)
396                 {
397                   sec = h->root.u.def.section;
398                   val = (h->root.u.def.value
399                          + sec->output_section->vma
400                          + sec->output_offset);
401                 }
402               else
403                 {
404                   if (! ((*info->callbacks->undefined_symbol)
405                          (info, h->root.root.string, input_bfd, input_section,
406                           rel->r_vaddr - input_section->vma, TRUE)))
407                     return FALSE;
408                 }
409             }
410
411           if (hihalf)
412             {
413               if (! ((*info->callbacks->reloc_dangerous)
414                      (info, "missing IHCONST reloc", input_bfd,
415                       input_section, rel->r_vaddr - input_section->vma)))
416                 return FALSE;
417               hihalf = FALSE;
418             }
419         }
420
421       overflow = FALSE;
422
423       switch (rel->r_type)
424         {
425         default:
426           bfd_set_error (bfd_error_bad_value);
427           return FALSE;
428
429         case R_IREL:
430           insn = bfd_get_32 (input_bfd, loc);
431
432           /* Extract the addend.  */
433           signed_value = EXTRACT_JUMPTARG (insn);
434           signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
435           signed_value <<= 2;
436
437           /* Determine the destination of the jump.  */
438           signed_value += val;
439
440           /* Make the destination PC relative.  */
441           signed_value -= (input_section->output_section->vma
442                            + input_section->output_offset
443                            + (rel->r_vaddr - input_section->vma));
444           if (signed_value > 0x7ffffff || signed_value < - 0x8000000)
445             {
446               overflow = TRUE;
447               signed_value = 0;
448             }
449
450           /* Put the adjusted value back into the instruction.  */
451           signed_value >>= 2;
452           insn = INSERT_JUMPTARG(insn, signed_value);
453
454           bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
455           break;
456
457         case R_ILOHALF:
458           insn = bfd_get_32 (input_bfd, loc);
459           unsigned_value = EXTRACT_HWORD (insn);
460           unsigned_value += val;
461           insn = INSERT_HWORD (insn, unsigned_value);
462           bfd_put_32 (input_bfd, insn, loc);
463           break;
464
465         case R_IHIHALF:
466           /* Save the value for the R_IHCONST reloc.  */
467           hihalf = TRUE;
468           hihalf_val = val;
469           break;
470
471         case R_IHCONST:
472           if (! hihalf)
473             {
474               if (! ((*info->callbacks->reloc_dangerous)
475                      (info, "missing IHIHALF reloc", input_bfd,
476                       input_section, rel->r_vaddr - input_section->vma)))
477                 return FALSE;
478               hihalf_val = 0;
479             }
480
481           insn = bfd_get_32 (input_bfd, loc);
482           unsigned_value = rel->r_symndx + hihalf_val;
483           unsigned_value >>= 16;
484           insn = INSERT_HWORD (insn, unsigned_value);
485           bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
486
487           hihalf = FALSE;
488           break;
489
490         case R_BYTE:
491         case R_HWORD:
492         case R_WORD:
493           rstat = _bfd_relocate_contents (howto_table + rel->r_type,
494                                           input_bfd, val, loc);
495           if (rstat == bfd_reloc_overflow)
496             overflow = TRUE;
497           else if (rstat != bfd_reloc_ok)
498             abort ();
499           break;
500         }
501
502       if (overflow)
503         {
504           const char *name;
505           char buf[SYMNMLEN + 1];
506
507           if (symndx == -1)
508             name = "*ABS*";
509           else if (h != NULL)
510             name = NULL;
511           else if (sym == NULL)
512             name = "*unknown*";
513           else if (sym->_n._n_n._n_zeroes == 0
514                    && sym->_n._n_n._n_offset != 0)
515             name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
516           else
517             {
518               strncpy (buf, sym->_n._n_name, SYMNMLEN);
519               buf[SYMNMLEN] = '\0';
520               name = buf;
521             }
522
523           if (! ((*info->callbacks->reloc_overflow)
524                  (info, (h ? &h->root : NULL), name,
525                   howto_table[rel->r_type].name, (bfd_vma) 0, input_bfd,
526                   input_section, rel->r_vaddr - input_section->vma)))
527             return FALSE;
528         }
529     }
530
531   return TRUE;
532 }
533
534 #define coff_relocate_section coff_or32_relocate_section
535
536 /* We don't want to change the symndx of a R_IHCONST reloc, since it
537    is actually an addend, not a symbol index at all.  */
538
539 static bfd_boolean
540 coff_or32_adjust_symndx (bfd *obfd ATTRIBUTE_UNUSED,
541                          struct bfd_link_info *info ATTRIBUTE_UNUSED,
542                          bfd *ibfd ATTRIBUTE_UNUSED,
543                          asection *sec ATTRIBUTE_UNUSED,
544                          struct internal_reloc *irel,
545                          bfd_boolean *adjustedp)
546 {
547   if (irel->r_type == R_IHCONST)
548     *adjustedp = TRUE;
549   else
550     *adjustedp = FALSE;
551   return TRUE;
552 }
553
554 #define coff_adjust_symndx coff_or32_adjust_symndx
555
556 #ifndef bfd_pe_print_pdata
557 #define bfd_pe_print_pdata      NULL
558 #endif
559
560 #include "coffcode.h"
561
562 const bfd_target or32coff_big_vec =
563 {
564   "coff-or32-big",  /* Name.  */
565   bfd_target_coff_flavour,
566   BFD_ENDIAN_BIG,   /* Data byte order is big.  */
567   BFD_ENDIAN_BIG,   /* Header byte order is big.  */
568
569   (HAS_RELOC  | EXEC_P |    /* Object flags.  */
570    HAS_LINENO | HAS_DEBUG |
571    HAS_SYMS   | HAS_LOCALS | WP_TEXT),
572
573   (SEC_HAS_CONTENTS | SEC_ALLOC | /* Section flags.  */
574    SEC_LOAD | SEC_RELOC |
575    SEC_READONLY ),
576   '_',        /* Leading underscore.  */
577   '/',        /* ar_pad_char.  */
578   15,         /* ar_max_namelen.  */
579   0,          /* match priority.  */
580
581   /* Data.  */
582   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
583   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
584   bfd_getb16, bfd_getb_signed_16, bfd_putb16,
585
586   /* Headers.  */
587   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
588   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
589   bfd_getb16, bfd_getb_signed_16, bfd_putb16,
590
591   {
592     _bfd_dummy_target,
593     coff_object_p,
594     bfd_generic_archive_p,
595     _bfd_dummy_target
596   },
597   {
598     bfd_false,
599     coff_mkobject,
600     _bfd_generic_mkarchive,
601     bfd_false
602   },
603   {
604     bfd_false,
605     coff_write_object_contents,
606     _bfd_write_archive_contents,
607     bfd_false
608   },
609
610   BFD_JUMP_TABLE_GENERIC (coff),
611   BFD_JUMP_TABLE_COPY (coff),
612   BFD_JUMP_TABLE_CORE (_bfd_nocore),
613   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
614   BFD_JUMP_TABLE_SYMBOLS (coff),
615   BFD_JUMP_TABLE_RELOCS (coff),
616   BFD_JUMP_TABLE_WRITE (coff),
617   BFD_JUMP_TABLE_LINK (coff),
618   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
619
620  /* Alternative_target.  */
621 #ifdef TARGET_LITTLE_SYM
622   & TARGET_LITTLE_SYM,
623 #else
624   NULL,
625 #endif
626
627   COFF_SWAP_TABLE
628 };