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