dlx: move prototype of dlx_set_skip_hi16 to elf/dlx.h
[deliverable/binutils-gdb.git] / bfd / elf32-dlx.c
1 /* DLX specific support for 32-bit ELF
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/dlx.h"
26 #include "elf32-dlx.h"
27
28 #define USE_REL 1
29
30 #define bfd_elf32_bfd_reloc_type_lookup elf32_dlx_reloc_type_lookup
31 #define bfd_elf32_bfd_reloc_name_lookup elf32_dlx_reloc_name_lookup
32 #define elf_info_to_howto elf32_dlx_info_to_howto
33 #define elf_info_to_howto_rel elf32_dlx_info_to_howto_rel
34 #define elf_backend_check_relocs elf32_dlx_check_relocs
35
36 /* The gas default behavior is not to preform the %hi modifier so that the
37 GNU assembler can have the lower 16 bits offset placed in the insn, BUT
38 we do like the gas to indicate it is %hi reloc type so when we in the link
39 loader phase we can have the corrected hi16 vale replace the buggous lo16
40 value that was placed there by gas. */
41
42 static int skip_dlx_elf_hi16_reloc = 0;
43
44 int
45 set_dlx_skip_hi16_flag (int flag)
46 {
47 skip_dlx_elf_hi16_reloc = flag;
48 return flag;
49 }
50
51 static bfd_reloc_status_type
52 _bfd_dlx_elf_hi16_reloc (bfd *abfd,
53 arelent *reloc_entry,
54 asymbol *symbol,
55 void * data,
56 asection *input_section,
57 bfd *output_bfd,
58 char **error_message)
59 {
60 bfd_reloc_status_type ret;
61 bfd_vma relocation;
62
63 /* If the skip flag is set then we simply do the generic relocating, this
64 is more of a hack for dlx gas/gld, so we do not need to do the %hi/%lo
65 fixup like mips gld did. */
66 if (skip_dlx_elf_hi16_reloc)
67 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
68 input_section, output_bfd, error_message);
69
70 /* If we're relocating, and this an external symbol, we don't want
71 to change anything. */
72 if (output_bfd != (bfd *) NULL
73 && (symbol->flags & BSF_SECTION_SYM) == 0
74 && reloc_entry->addend == 0)
75 {
76 reloc_entry->address += input_section->output_offset;
77 return bfd_reloc_ok;
78 }
79
80 ret = bfd_reloc_ok;
81
82 if (bfd_is_und_section (symbol->section)
83 && output_bfd == (bfd *) NULL)
84 ret = bfd_reloc_undefined;
85
86 relocation = (bfd_is_com_section (symbol->section)) ? 0 : symbol->value;
87 relocation += symbol->section->output_section->vma;
88 relocation += symbol->section->output_offset;
89 relocation += reloc_entry->addend;
90 relocation += bfd_get_16 (abfd, (bfd_byte *)data + reloc_entry->address);
91
92 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
93 return bfd_reloc_outofrange;
94
95 bfd_put_16 (abfd, (short)((relocation >> 16) & 0xFFFF),
96 (bfd_byte *)data + reloc_entry->address);
97
98 return ret;
99 }
100
101 /* ELF relocs are against symbols. If we are producing relocatable
102 output, and the reloc is against an external symbol, and nothing
103 has given us any additional addend, the resulting reloc will also
104 be against the same symbol. In such a case, we don't want to
105 change anything about the way the reloc is handled, since it will
106 all be done at final link time. Rather than put special case code
107 into bfd_perform_relocation, all the reloc types use this howto
108 function. It just short circuits the reloc if producing
109 relocatable output against an external symbol. */
110
111 static bfd_reloc_status_type
112 elf32_dlx_relocate16 (bfd *abfd,
113 arelent *reloc_entry,
114 asymbol *symbol,
115 void * data,
116 asection *input_section,
117 bfd *output_bfd,
118 char **error_message ATTRIBUTE_UNUSED)
119 {
120 unsigned long insn, vallo, allignment;
121 int val;
122
123 /* HACK: I think this first condition is necessary when producing
124 relocatable output. After the end of HACK, the code is identical
125 to bfd_elf_generic_reloc(). I would _guess_ the first change
126 belongs there rather than here. martindo 1998-10-23. */
127
128 if (skip_dlx_elf_hi16_reloc)
129 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
130 input_section, output_bfd, error_message);
131
132 /* Check undefined section and undefined symbols. */
133 if (bfd_is_und_section (symbol->section)
134 && output_bfd == (bfd *) NULL)
135 return bfd_reloc_undefined;
136
137 /* Can not support a long jump to sections other then .text. */
138 if (strcmp (input_section->name, symbol->section->output_section->name) != 0)
139 {
140 (*_bfd_error_handler) (_("BFD Link Error: branch (PC rel16) to section (%s) not supported"),
141 symbol->section->output_section->name);
142 return bfd_reloc_undefined;
143 }
144
145 insn = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
146 allignment = 1 << (input_section->output_section->alignment_power - 1);
147 vallo = insn & 0x0000FFFF;
148
149 if (vallo & 0x8000)
150 vallo = ~(vallo | 0xFFFF0000) + 1;
151
152 /* vallo points to the vma of next instruction. */
153 vallo += (((unsigned long)(input_section->output_section->vma +
154 input_section->output_offset) +
155 allignment) & ~allignment);
156
157 /* val is the displacement (PC relative to next instruction). */
158 val = (symbol->section->output_offset +
159 symbol->section->output_section->vma +
160 symbol->value) - vallo;
161
162 if (abs ((int) val) > 0x00007FFF)
163 return bfd_reloc_outofrange;
164
165 insn = (insn & 0xFFFF0000) | (val & 0x0000FFFF);
166
167 bfd_put_32 (abfd, insn,
168 (bfd_byte *) data + reloc_entry->address);
169
170 return bfd_reloc_ok;
171 }
172
173 static bfd_reloc_status_type
174 elf32_dlx_relocate26 (bfd *abfd,
175 arelent *reloc_entry,
176 asymbol *symbol,
177 void * data,
178 asection *input_section,
179 bfd *output_bfd,
180 char **error_message ATTRIBUTE_UNUSED)
181 {
182 unsigned long insn, vallo, allignment;
183 int val;
184
185 /* HACK: I think this first condition is necessary when producing
186 relocatable output. After the end of HACK, the code is identical
187 to bfd_elf_generic_reloc(). I would _guess_ the first change
188 belongs there rather than here. martindo 1998-10-23. */
189
190 if (skip_dlx_elf_hi16_reloc)
191 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
192 input_section, output_bfd, error_message);
193
194 /* Check undefined section and undefined symbols. */
195 if (bfd_is_und_section (symbol->section)
196 && output_bfd == (bfd *) NULL)
197 return bfd_reloc_undefined;
198
199 /* Can not support a long jump to sections other then .text */
200 if (strcmp (input_section->name, symbol->section->output_section->name) != 0)
201 {
202 (*_bfd_error_handler) (_("BFD Link Error: jump (PC rel26) to section (%s) not supported"),
203 symbol->section->output_section->name);
204 return bfd_reloc_undefined;
205 }
206
207 insn = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
208 allignment = 1 << (input_section->output_section->alignment_power - 1);
209 vallo = insn & 0x03FFFFFF;
210
211 if (vallo & 0x03000000)
212 vallo = ~(vallo | 0xFC000000) + 1;
213
214 /* vallo is the vma for the next instruction. */
215 vallo += (((unsigned long) (input_section->output_section->vma +
216 input_section->output_offset) +
217 allignment) & ~allignment);
218
219 /* val is the displacement (PC relative to next instruction). */
220 val = (symbol->section->output_offset +
221 symbol->section->output_section->vma + symbol->value)
222 - vallo;
223
224 if (abs ((int) val) > 0x01FFFFFF)
225 return bfd_reloc_outofrange;
226
227 insn = (insn & 0xFC000000) | (val & 0x03FFFFFF);
228 bfd_put_32 (abfd, insn,
229 (bfd_byte *) data + reloc_entry->address);
230
231 return bfd_reloc_ok;
232 }
233
234 static reloc_howto_type dlx_elf_howto_table[]=
235 {
236 /* No relocation. */
237 HOWTO (R_DLX_NONE, /* Type. */
238 0, /* Rightshift. */
239 3, /* size (0 = byte, 1 = short, 2 = long). */
240 0, /* Bitsize. */
241 FALSE, /* PC_relative. */
242 0, /* Bitpos. */
243 complain_overflow_dont,/* Complain_on_overflow. */
244 bfd_elf_generic_reloc, /* Special_function. */
245 "R_DLX_NONE", /* Name. */
246 FALSE, /* Partial_inplace. */
247 0, /* Src_mask. */
248 0, /* Dst_mask. */
249 FALSE), /* PCrel_offset. */
250
251 /* 8 bit relocation. */
252 HOWTO (R_DLX_RELOC_8, /* Type. */
253 0, /* Rightshift. */
254 0, /* Size (0 = byte, 1 = short, 2 = long). */
255 8, /* Bitsize. */
256 FALSE, /* PC_relative. */
257 0, /* Bitpos. */
258 complain_overflow_dont,/* Complain_on_overflow. */
259 bfd_elf_generic_reloc, /* Special_function. */
260 "R_DLX_RELOC_8", /* Name. */
261 TRUE, /* Partial_inplace. */
262 0xff, /* Src_mask. */
263 0xff, /* Dst_mask. */
264 FALSE), /* PCrel_offset. */
265
266 /* 16 bit relocation. */
267 HOWTO (R_DLX_RELOC_16, /* Type. */
268 0, /* Rightshift. */
269 1, /* Size (0 = byte, 1 = short, 2 = long). */
270 16, /* Bitsize. */
271 FALSE, /* PC_relative. */
272 0, /* Bitpos. */
273 complain_overflow_dont,/* Complain_on_overflow. */
274 bfd_elf_generic_reloc, /* Special_function. */
275 "R_DLX_RELOC_16", /* Name. */
276 TRUE, /* Partial_inplace. */
277 0xffff, /* Src_mask. */
278 0xffff, /* Dst_mask. */
279 FALSE), /* PCrel_offset. */
280
281 /* 32 bit relocation. */
282 HOWTO (R_DLX_RELOC_32, /* Type. */
283 0, /* Rightshift. */
284 2, /* Size (0 = byte, 1 = short, 2 = long). */
285 32, /* Bitsize. */
286 FALSE, /* PC_relative. */
287 0, /* Bitpos. */
288 complain_overflow_dont,/* Complain_on_overflow. */
289 bfd_elf_generic_reloc, /* Special_function. */
290 "R_DLX_RELOC_32", /* Name. */
291 TRUE, /* Partial_inplace. */
292 0xffffffff, /* Src_mask. */
293 0xffffffff, /* Dst_mask. */
294 FALSE), /* PCrel_offset. */
295
296 /* GNU extension to record C++ vtable hierarchy. */
297 HOWTO (R_DLX_GNU_VTINHERIT, /* Type. */
298 0, /* Rightshift. */
299 2, /* Size (0 = byte, 1 = short, 2 = long). */
300 0, /* Bitsize. */
301 FALSE, /* PC_relative. */
302 0, /* Bitpos. */
303 complain_overflow_dont,/* Complain_on_overflow. */
304 NULL, /* Special_function. */
305 "R_DLX_GNU_VTINHERIT", /* Name. */
306 FALSE, /* Partial_inplace. */
307 0, /* Src_mask. */
308 0, /* Dst_mask. */
309 FALSE), /* PCrel_offset. */
310
311 /* GNU extension to record C++ vtable member usage. */
312 HOWTO (R_DLX_GNU_VTENTRY, /* Type. */
313 0, /* Rightshift. */
314 2, /* Size (0 = byte, 1 = short, 2 = long). */
315 0, /* Bitsize. */
316 FALSE, /* PC_relative. */
317 0, /* Bitpos. */
318 complain_overflow_dont,/* Complain_on_overflow. */
319 _bfd_elf_rel_vtable_reloc_fn,/* Special_function. */
320 "R_DLX_GNU_VTENTRY", /* Name. */
321 FALSE, /* Partial_inplace. */
322 0, /* Src_mask. */
323 0, /* Dst_mask. */
324 FALSE) /* PCrel_offset. */
325 };
326
327 /* 16 bit offset for pc-relative branches. */
328 static reloc_howto_type elf_dlx_gnu_rel16_s2 =
329 HOWTO (R_DLX_RELOC_16_PCREL, /* Type. */
330 0, /* Rightshift. */
331 1, /* Size (0 = byte, 1 = short, 2 = long). */
332 16, /* Bitsize. */
333 TRUE, /* PC_relative. */
334 0, /* Bitpos. */
335 complain_overflow_signed, /* Complain_on_overflow. */
336 elf32_dlx_relocate16, /* Special_function. */
337 "R_DLX_RELOC_16_PCREL",/* Name. */
338 TRUE, /* Partial_inplace. */
339 0xffff, /* Src_mask. */
340 0xffff, /* Dst_mask. */
341 TRUE); /* PCrel_offset. */
342
343 /* 26 bit offset for pc-relative branches. */
344 static reloc_howto_type elf_dlx_gnu_rel26_s2 =
345 HOWTO (R_DLX_RELOC_26_PCREL, /* Type. */
346 0, /* Rightshift. */
347 2, /* Size (0 = byte, 1 = short, 2 = long). */
348 26, /* Bitsize. */
349 TRUE, /* PC_relative. */
350 0, /* Bitpos. */
351 complain_overflow_dont,/* Complain_on_overflow. */
352 elf32_dlx_relocate26, /* Special_function. */
353 "R_DLX_RELOC_26_PCREL",/* Name. */
354 TRUE, /* Partial_inplace. */
355 0xffff, /* Src_mask. */
356 0xffff, /* Dst_mask. */
357 TRUE); /* PCrel_offset. */
358
359 /* High 16 bits of symbol value. */
360 static reloc_howto_type elf_dlx_reloc_16_hi =
361 HOWTO (R_DLX_RELOC_16_HI, /* Type. */
362 16, /* Rightshift. */
363 2, /* Size (0 = byte, 1 = short, 2 = long). */
364 32, /* Bitsize. */
365 FALSE, /* PC_relative. */
366 0, /* Bitpos. */
367 complain_overflow_dont,/* Complain_on_overflow. */
368 _bfd_dlx_elf_hi16_reloc,/* Special_function. */
369 "R_DLX_RELOC_16_HI", /* Name. */
370 TRUE, /* Partial_inplace. */
371 0xFFFF, /* Src_mask. */
372 0xffff, /* Dst_mask. */
373 FALSE); /* PCrel_offset. */
374
375 /* Low 16 bits of symbol value. */
376 static reloc_howto_type elf_dlx_reloc_16_lo =
377 HOWTO (R_DLX_RELOC_16_LO, /* Type. */
378 0, /* Rightshift. */
379 1, /* Size (0 = byte, 1 = short, 2 = long). */
380 16, /* Bitsize. */
381 FALSE, /* PC_relative. */
382 0, /* Bitpos. */
383 complain_overflow_dont,/* Complain_on_overflow. */
384 bfd_elf_generic_reloc, /* Special_function. */
385 "R_DLX_RELOC_16_LO", /* Name. */
386 TRUE, /* Partial_inplace. */
387 0xffff, /* Src_mask. */
388 0xffff, /* Dst_mask. */
389 FALSE); /* PCrel_offset. */
390
391 /* A mapping from BFD reloc types to DLX ELF reloc types.
392 Stolen from elf32-mips.c.
393
394 More about this table - for dlx elf relocation we do not really
395 need this table, if we have a rtype defined in this table will
396 caused tc_gen_relocate confused and die on us, but if we remove
397 this table it will caused more problem, so for now simple solution
398 is to remove those entries which may cause problem. */
399 struct elf_reloc_map
400 {
401 bfd_reloc_code_real_type bfd_reloc_val;
402 enum elf_dlx_reloc_type elf_reloc_val;
403 };
404
405 static const struct elf_reloc_map dlx_reloc_map[] =
406 {
407 { BFD_RELOC_NONE, R_DLX_NONE },
408 { BFD_RELOC_16, R_DLX_RELOC_16 },
409 { BFD_RELOC_32, R_DLX_RELOC_32 },
410 { BFD_RELOC_DLX_HI16_S, R_DLX_RELOC_16_HI },
411 { BFD_RELOC_DLX_LO16, R_DLX_RELOC_16_LO },
412 { BFD_RELOC_VTABLE_INHERIT, R_DLX_GNU_VTINHERIT },
413 { BFD_RELOC_VTABLE_ENTRY, R_DLX_GNU_VTENTRY }
414 };
415
416 /* Look through the relocs for a section during the first phase.
417 Since we don't do .gots or .plts, we just need to consider the
418 virtual table relocs for gc. */
419
420 static bfd_boolean
421 elf32_dlx_check_relocs (bfd *abfd,
422 struct bfd_link_info *info,
423 asection *sec,
424 const Elf_Internal_Rela *relocs)
425 {
426 Elf_Internal_Shdr *symtab_hdr;
427 struct elf_link_hash_entry **sym_hashes;
428 const Elf_Internal_Rela *rel;
429 const Elf_Internal_Rela *rel_end;
430
431 if (bfd_link_relocatable (info))
432 return TRUE;
433
434 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
435 sym_hashes = elf_sym_hashes (abfd);
436
437 rel_end = relocs + sec->reloc_count;
438 for (rel = relocs; rel < rel_end; rel++)
439 {
440 struct elf_link_hash_entry *h;
441 unsigned long r_symndx;
442
443 r_symndx = ELF32_R_SYM (rel->r_info);
444 if (r_symndx < symtab_hdr->sh_info)
445 h = NULL;
446 else
447 {
448 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
449 while (h->root.type == bfd_link_hash_indirect
450 || h->root.type == bfd_link_hash_warning)
451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
452
453 /* PR15323, ref flags aren't set for references in the same
454 object. */
455 h->root.non_ir_ref = 1;
456 }
457
458 switch (ELF32_R_TYPE (rel->r_info))
459 {
460 /* This relocation describes the C++ object vtable hierarchy.
461 Reconstruct it for later use during GC. */
462 case R_DLX_GNU_VTINHERIT:
463 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
464 return FALSE;
465 break;
466
467 /* This relocation describes which C++ vtable entries are actually
468 used. Record for later use during GC. */
469 case R_DLX_GNU_VTENTRY:
470 BFD_ASSERT (h != NULL);
471 if (h != NULL
472 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
473 return FALSE;
474 break;
475 }
476 }
477
478 return TRUE;
479 }
480
481 /* Given a BFD reloc type, return a howto structure. */
482
483 static reloc_howto_type *
484 elf32_dlx_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
485 bfd_reloc_code_real_type code)
486 {
487 unsigned int i;
488
489 for (i = 0; i < sizeof (dlx_reloc_map) / sizeof (struct elf_reloc_map); i++)
490 if (dlx_reloc_map[i].bfd_reloc_val == code)
491 return &dlx_elf_howto_table[(int) dlx_reloc_map[i].elf_reloc_val];
492
493 switch (code)
494 {
495 default:
496 bfd_set_error (bfd_error_bad_value);
497 return NULL;
498 case BFD_RELOC_16_PCREL_S2:
499 return &elf_dlx_gnu_rel16_s2;
500 case BFD_RELOC_DLX_JMP26:
501 return &elf_dlx_gnu_rel26_s2;
502 case BFD_RELOC_HI16_S:
503 return &elf_dlx_reloc_16_hi;
504 case BFD_RELOC_LO16:
505 return &elf_dlx_reloc_16_lo;
506 }
507 }
508
509 static reloc_howto_type *
510 elf32_dlx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
511 const char *r_name)
512 {
513 unsigned int i;
514
515 for (i = 0;
516 i < sizeof (dlx_elf_howto_table) / sizeof (dlx_elf_howto_table[0]);
517 i++)
518 if (dlx_elf_howto_table[i].name != NULL
519 && strcasecmp (dlx_elf_howto_table[i].name, r_name) == 0)
520 return &dlx_elf_howto_table[i];
521
522 if (strcasecmp (elf_dlx_gnu_rel16_s2.name, r_name) == 0)
523 return &elf_dlx_gnu_rel16_s2;
524 if (strcasecmp (elf_dlx_gnu_rel26_s2.name, r_name) == 0)
525 return &elf_dlx_gnu_rel26_s2;
526 if (strcasecmp (elf_dlx_reloc_16_hi.name, r_name) == 0)
527 return &elf_dlx_reloc_16_hi;
528 if (strcasecmp (elf_dlx_reloc_16_lo.name, r_name) == 0)
529 return &elf_dlx_reloc_16_lo;
530
531 return NULL;
532 }
533
534 static reloc_howto_type *
535 dlx_rtype_to_howto (unsigned int r_type)
536 {
537 switch (r_type)
538 {
539 case R_DLX_RELOC_16_PCREL:
540 return & elf_dlx_gnu_rel16_s2;
541 case R_DLX_RELOC_26_PCREL:
542 return & elf_dlx_gnu_rel26_s2;
543 case R_DLX_RELOC_16_HI:
544 return & elf_dlx_reloc_16_hi;
545 case R_DLX_RELOC_16_LO:
546 return & elf_dlx_reloc_16_lo;
547 default:
548 if (r_type >= (unsigned int) R_DLX_max)
549 {
550 _bfd_error_handler (_("Invalid DLX reloc number: %d"), r_type);
551 r_type = 0;
552 }
553 return & dlx_elf_howto_table[r_type];
554 }
555 }
556
557 static void
558 elf32_dlx_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
559 arelent * cache_ptr ATTRIBUTE_UNUSED,
560 Elf_Internal_Rela * dst ATTRIBUTE_UNUSED)
561 {
562 abort ();
563 }
564
565 static void
566 elf32_dlx_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
567 arelent *cache_ptr,
568 Elf_Internal_Rela *dst)
569 {
570 unsigned int r_type;
571
572 r_type = ELF32_R_TYPE (dst->r_info);
573 cache_ptr->howto = dlx_rtype_to_howto (r_type);
574 return;
575 }
576
577 #define TARGET_BIG_SYM dlx_elf32_be_vec
578 #define TARGET_BIG_NAME "elf32-dlx"
579 #define ELF_ARCH bfd_arch_dlx
580 #define ELF_MACHINE_CODE EM_DLX
581 #define ELF_MAXPAGESIZE 1 /* FIXME: This number is wrong, It should be the page size in bytes. */
582
583 #include "elf32-target.h"
This page took 0.05069 seconds and 5 git commands to generate.