source: trunk/src/binutils/bfd/elf32-m32r.c@ 524

Last change on this file since 524 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 66.3 KB
Line 
1/* M32R-specific support for 32-bit ELF.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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"
25#include "elf/m32r.h"
26
27static bfd_reloc_status_type m32r_elf_10_pcrel_reloc
28 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
29static bfd_reloc_status_type m32r_elf_do_10_pcrel_reloc
30 PARAMS ((bfd *, reloc_howto_type *, asection *,
31 bfd_byte *, bfd_vma, asection *, bfd_vma, bfd_vma));
32static bfd_reloc_status_type m32r_elf_hi16_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34static void m32r_elf_relocate_hi16
35 PARAMS ((bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *,
36 bfd_byte *, bfd_vma));
37bfd_reloc_status_type m32r_elf_lo16_reloc
38 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39bfd_reloc_status_type m32r_elf_generic_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41static bfd_reloc_status_type m32r_elf_sda16_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
44 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
45static void m32r_info_to_howto_rel
46 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
47boolean _bfd_m32r_elf_section_from_bfd_section
48 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
49void _bfd_m32r_elf_symbol_processing
50 PARAMS ((bfd *, asymbol *));
51static boolean m32r_elf_add_symbol_hook
52 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
53 const char **, flagword *, asection **, bfd_vma *));
54static boolean m32r_elf_relocate_section
55 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
56 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
57#if 0 /* not yet */
58static boolean m32r_elf_relax_delete_bytes
59 PARAMS ((bfd *, asection *, bfd_vma, int));
60#endif
61
62static bfd_reloc_status_type m32r_elf_final_sda_base
63 PARAMS ((bfd *, struct bfd_link_info *, const char **, bfd_vma *));
64static boolean m32r_elf_object_p
65 PARAMS ((bfd *));
66static void m32r_elf_final_write_processing
67 PARAMS ((bfd *, boolean));
68static boolean m32r_elf_set_private_flags
69 PARAMS ((bfd *, flagword));
70static boolean m32r_elf_copy_private_bfd_data
71 PARAMS ((bfd *, bfd *));
72static boolean m32r_elf_merge_private_bfd_data
73 PARAMS ((bfd *, bfd *));
74static boolean m32r_elf_print_private_bfd_data
75 PARAMS ((bfd *, PTR));
76
77#define NOP_INSN 0x7000
78#define MAKE_PARALLEL(insn) ((insn) | 0x8000)
79
80/* Use REL instead of RELA to save space.
81 This only saves space in libraries and object files, but perhaps
82 relocs will be put in ROM? All in all though, REL relocs are a pain
83 to work with. */
84#define USE_REL
85
86static reloc_howto_type m32r_elf_howto_table[] =
87{
88 /* This reloc does nothing. */
89 HOWTO (R_M32R_NONE, /* type */
90 0, /* rightshift */
91 2, /* size (0 = byte, 1 = short, 2 = long) */
92 32, /* bitsize */
93 false, /* pc_relative */
94 0, /* bitpos */
95 complain_overflow_bitfield, /* complain_on_overflow */
96 bfd_elf_generic_reloc, /* special_function */
97 "R_M32R_NONE", /* name */
98 false, /* partial_inplace */
99 0, /* src_mask */
100 0, /* dst_mask */
101 false), /* pcrel_offset */
102
103 /* A 16 bit absolute relocation. */
104 HOWTO (R_M32R_16, /* type */
105 0, /* rightshift */
106 1, /* size (0 = byte, 1 = short, 2 = long) */
107 16, /* bitsize */
108 false, /* pc_relative */
109 0, /* bitpos */
110 complain_overflow_bitfield, /* complain_on_overflow */
111 m32r_elf_generic_reloc,/* special_function */
112 "R_M32R_16", /* name */
113 true, /* partial_inplace */
114 0xffff, /* src_mask */
115 0xffff, /* dst_mask */
116 false), /* pcrel_offset */
117
118 /* A 32 bit absolute relocation. */
119 HOWTO (R_M32R_32, /* type */
120 0, /* rightshift */
121 2, /* size (0 = byte, 1 = short, 2 = long) */
122 32, /* bitsize */
123 false, /* pc_relative */
124 0, /* bitpos */
125 complain_overflow_bitfield, /* complain_on_overflow */
126 m32r_elf_generic_reloc,/* special_function */
127 "R_M32R_32", /* name */
128 true, /* partial_inplace */
129 0xffffffff, /* src_mask */
130 0xffffffff, /* dst_mask */
131 false), /* pcrel_offset */
132
133 /* A 24 bit address. */
134 HOWTO (R_M32R_24, /* type */
135 0, /* rightshift */
136 2, /* size (0 = byte, 1 = short, 2 = long) */
137 24, /* bitsize */
138 false, /* pc_relative */
139 0, /* bitpos */
140 complain_overflow_unsigned, /* complain_on_overflow */
141 m32r_elf_generic_reloc,/* special_function */
142 "R_M32R_24", /* name */
143 true, /* partial_inplace */
144 0xffffff, /* src_mask */
145 0xffffff, /* dst_mask */
146 false), /* pcrel_offset */
147
148 /* An PC Relative 10-bit relocation, shifted by 2.
149 This reloc is complicated because relocations are relative to pc & -4.
150 i.e. branches in the right insn slot use the address of the left insn
151 slot for pc. */
152 /* ??? It's not clear whether this should have partial_inplace set or not.
153 Branch relaxing in the assembler can store the addend in the insn,
154 and if bfd_install_relocation gets called the addend may get added
155 again. */
156 HOWTO (R_M32R_10_PCREL, /* type */
157 2, /* rightshift */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
159 10, /* bitsize */
160 true, /* pc_relative */
161 0, /* bitpos */
162 complain_overflow_signed, /* complain_on_overflow */
163 m32r_elf_10_pcrel_reloc, /* special_function */
164 "R_M32R_10_PCREL", /* name */
165 false, /* partial_inplace */
166 0xff, /* src_mask */
167 0xff, /* dst_mask */
168 true), /* pcrel_offset */
169
170 /* A relative 18 bit relocation, right shifted by 2. */
171 HOWTO (R_M32R_18_PCREL, /* type */
172 2, /* rightshift */
173 2, /* size (0 = byte, 1 = short, 2 = long) */
174 16, /* bitsize */
175 true, /* pc_relative */
176 0, /* bitpos */
177 complain_overflow_signed, /* complain_on_overflow */
178 bfd_elf_generic_reloc, /* special_function */
179 "R_M32R_18_PCREL", /* name */
180 false, /* partial_inplace */
181 0xffff, /* src_mask */
182 0xffff, /* dst_mask */
183 true), /* pcrel_offset */
184
185 /* A relative 26 bit relocation, right shifted by 2. */
186 /* ??? It's not clear whether this should have partial_inplace set or not.
187 Branch relaxing in the assembler can store the addend in the insn,
188 and if bfd_install_relocation gets called the addend may get added
189 again. */
190 HOWTO (R_M32R_26_PCREL, /* type */
191 2, /* rightshift */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
193 26, /* bitsize */
194 true, /* pc_relative */
195 0, /* bitpos */
196 complain_overflow_signed, /* complain_on_overflow */
197 bfd_elf_generic_reloc, /* special_function */
198 "R_M32R_26_PCREL", /* name */
199 false, /* partial_inplace */
200 0xffffff, /* src_mask */
201 0xffffff, /* dst_mask */
202 true), /* pcrel_offset */
203
204 /* High 16 bits of address when lower 16 is or'd in. */
205 HOWTO (R_M32R_HI16_ULO, /* type */
206 16, /* rightshift */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
208 16, /* bitsize */
209 false, /* pc_relative */
210 0, /* bitpos */
211 complain_overflow_dont, /* complain_on_overflow */
212 m32r_elf_hi16_reloc, /* special_function */
213 "R_M32R_HI16_ULO", /* name */
214 true, /* partial_inplace */
215 0x0000ffff, /* src_mask */
216 0x0000ffff, /* dst_mask */
217 false), /* pcrel_offset */
218
219 /* High 16 bits of address when lower 16 is added in. */
220 HOWTO (R_M32R_HI16_SLO, /* type */
221 16, /* rightshift */
222 2, /* size (0 = byte, 1 = short, 2 = long) */
223 16, /* bitsize */
224 false, /* pc_relative */
225 0, /* bitpos */
226 complain_overflow_dont, /* complain_on_overflow */
227 m32r_elf_hi16_reloc, /* special_function */
228 "R_M32R_HI16_SLO", /* name */
229 true, /* partial_inplace */
230 0x0000ffff, /* src_mask */
231 0x0000ffff, /* dst_mask */
232 false), /* pcrel_offset */
233
234 /* Lower 16 bits of address. */
235 HOWTO (R_M32R_LO16, /* type */
236 0, /* rightshift */
237 2, /* size (0 = byte, 1 = short, 2 = long) */
238 16, /* bitsize */
239 false, /* pc_relative */
240 0, /* bitpos */
241 complain_overflow_dont, /* complain_on_overflow */
242 m32r_elf_lo16_reloc, /* special_function */
243 "R_M32R_LO16", /* name */
244 true, /* partial_inplace */
245 0x0000ffff, /* src_mask */
246 0x0000ffff, /* dst_mask */
247 false), /* pcrel_offset */
248
249 /* Small data area 16 bits offset. */
250 HOWTO (R_M32R_SDA16, /* type */
251 0, /* rightshift */
252 2, /* size (0 = byte, 1 = short, 2 = long) */
253 16, /* bitsize */
254 false, /* pc_relative */
255 0, /* bitpos */
256 complain_overflow_signed, /* complain_on_overflow */
257 m32r_elf_sda16_reloc, /* special_function */
258 "R_M32R_SDA16", /* name */
259 true, /* partial_inplace */ /* FIXME: correct? */
260 0x0000ffff, /* src_mask */
261 0x0000ffff, /* dst_mask */
262 false), /* pcrel_offset */
263
264 /* GNU extension to record C++ vtable hierarchy */
265 HOWTO (R_M32R_GNU_VTINHERIT, /* type */
266 0, /* rightshift */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
268 0, /* bitsize */
269 false, /* pc_relative */
270 0, /* bitpos */
271 complain_overflow_dont, /* complain_on_overflow */
272 NULL, /* special_function */
273 "R_M32R_GNU_VTINHERIT", /* name */
274 false, /* partial_inplace */
275 0, /* src_mask */
276 0, /* dst_mask */
277 false), /* pcrel_offset */
278
279 /* GNU extension to record C++ vtable member usage */
280 HOWTO (R_M32R_GNU_VTENTRY, /* type */
281 0, /* rightshift */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
283 0, /* bitsize */
284 false, /* pc_relative */
285 0, /* bitpos */
286 complain_overflow_dont, /* complain_on_overflow */
287 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
288 "R_M32R_GNU_VTENTRY", /* name */
289 false, /* partial_inplace */
290 0, /* src_mask */
291 0, /* dst_mask */
292 false), /* pcrel_offset */
293
294};
295
296
297/* Handle the R_M32R_10_PCREL reloc. */
298
299static bfd_reloc_status_type
300m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
301 input_section, output_bfd, error_message)
302 bfd * abfd;
303 arelent * reloc_entry;
304 asymbol * symbol;
305 PTR data;
306 asection * input_section;
307 bfd * output_bfd;
308 char ** error_message ATTRIBUTE_UNUSED;
309{
310 /* This part is from bfd_elf_generic_reloc. */
311 if (output_bfd != (bfd *) NULL
312 && (symbol->flags & BSF_SECTION_SYM) == 0
313 && (! reloc_entry->howto->partial_inplace
314 || reloc_entry->addend == 0))
315 {
316 reloc_entry->address += input_section->output_offset;
317 return bfd_reloc_ok;
318 }
319
320 if (output_bfd != NULL)
321 {
322 /* FIXME: See bfd_perform_relocation. Is this right? */
323 return bfd_reloc_continue;
324 }
325
326 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
327 input_section,
328 data, reloc_entry->address,
329 symbol->section,
330 (symbol->value
331 + symbol->section->output_section->vma
332 + symbol->section->output_offset),
333 reloc_entry->addend);
334}
335
336/* Utility to actually perform an R_M32R_10_PCREL reloc. */
337
338static bfd_reloc_status_type
339m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
340 symbol_section, symbol_value, addend)
341 bfd *abfd;
342 reloc_howto_type *howto;
343 asection *input_section;
344 bfd_byte *data;
345 bfd_vma offset;
346 asection *symbol_section ATTRIBUTE_UNUSED;
347 bfd_vma symbol_value;
348 bfd_vma addend;
349{
350 bfd_signed_vma relocation;
351 unsigned long x;
352 bfd_reloc_status_type status;
353
354 /* Sanity check the address (offset in section). */
355 if (offset > input_section->_cooked_size)
356 return bfd_reloc_outofrange;
357
358 relocation = symbol_value + addend;
359 /* Make it pc relative. */
360 relocation -= (input_section->output_section->vma
361 + input_section->output_offset);
362 /* These jumps mask off the lower two bits of the current address
363 before doing pcrel calculations. */
364 relocation -= (offset & -4L);
365
366 if (relocation < -0x200 || relocation > 0x1ff)
367 status = bfd_reloc_overflow;
368 else
369 status = bfd_reloc_ok;
370
371 x = bfd_get_16 (abfd, data + offset);
372 relocation >>= howto->rightshift;
373 relocation <<= howto->bitpos;
374 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
375 bfd_put_16 (abfd, x, data + offset);
376
377 return status;
378}
379
380/* Handle the R_M32R_HI16_[SU]LO relocs.
381 HI16_SLO is for the add3 and load/store with displacement instructions.
382 HI16_ULO is for the or3 instruction.
383 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
384 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
385 we must add one to the high 16 bytes (which will get subtracted off when
386 the low 16 bits are added).
387 These relocs have to be done in combination with an R_M32R_LO16 reloc
388 because there is a carry from the LO16 to the HI16. Here we just save
389 the information we need; we do the actual relocation when we see the LO16.
390 This code is copied from the elf32-mips.c. We also support an arbitrary
391 number of HI16 relocs to be associated with a single LO16 reloc. The
392 assembler sorts the relocs to ensure each HI16 immediately precedes its
393 LO16. However if there are multiple copies, the assembler may not find
394 the real LO16 so it picks the first one it finds. */
395
396struct m32r_hi16
397{
398 struct m32r_hi16 *next;
399 bfd_byte *addr;
400 bfd_vma addend;
401};
402
403/* FIXME: This should not be a static variable. */
404
405static struct m32r_hi16 *m32r_hi16_list;
406
407static bfd_reloc_status_type
408m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
409 input_section, output_bfd, error_message)
410 bfd *abfd ATTRIBUTE_UNUSED;
411 arelent *reloc_entry;
412 asymbol *symbol;
413 PTR data;
414 asection *input_section;
415 bfd *output_bfd;
416 char **error_message ATTRIBUTE_UNUSED;
417{
418 bfd_reloc_status_type ret;
419 bfd_vma relocation;
420 struct m32r_hi16 *n;
421
422 /* This part is from bfd_elf_generic_reloc.
423 If we're relocating, and this an external symbol, we don't want
424 to change anything. */
425 if (output_bfd != (bfd *) NULL
426 && (symbol->flags & BSF_SECTION_SYM) == 0
427 && reloc_entry->addend == 0)
428 {
429 reloc_entry->address += input_section->output_offset;
430 return bfd_reloc_ok;
431 }
432
433 /* Sanity check the address (offset in section). */
434 if (reloc_entry->address > input_section->_cooked_size)
435 return bfd_reloc_outofrange;
436
437 ret = bfd_reloc_ok;
438 if (bfd_is_und_section (symbol->section)
439 && output_bfd == (bfd *) NULL)
440 ret = bfd_reloc_undefined;
441
442 if (bfd_is_com_section (symbol->section))
443 relocation = 0;
444 else
445 relocation = symbol->value;
446
447 relocation += symbol->section->output_section->vma;
448 relocation += symbol->section->output_offset;
449 relocation += reloc_entry->addend;
450
451 /* Save the information, and let LO16 do the actual relocation. */
452 n = (struct m32r_hi16 *) bfd_malloc (sizeof *n);
453 if (n == NULL)
454 return bfd_reloc_outofrange;
455 n->addr = (bfd_byte *) data + reloc_entry->address;
456 n->addend = relocation;
457 n->next = m32r_hi16_list;
458 m32r_hi16_list = n;
459
460 if (output_bfd != (bfd *) NULL)
461 reloc_entry->address += input_section->output_offset;
462
463 return ret;
464}
465
466/* Handle an M32R ELF HI16 reloc. */
467
468static void
469m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
470 bfd *input_bfd;
471 int type;
472 Elf_Internal_Rela *relhi;
473 Elf_Internal_Rela *rello;
474 bfd_byte *contents;
475 bfd_vma addend;
476{
477 unsigned long insn;
478 bfd_vma addlo;
479
480 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
481
482 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
483 if (type == R_M32R_HI16_SLO)
484 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
485 else
486 addlo &= 0xffff;
487
488 addend += ((insn & 0xffff) << 16) + addlo;
489
490 /* Reaccount for sign extension of low part. */
491 if (type == R_M32R_HI16_SLO
492 && (addend & 0x8000) != 0)
493 addend += 0x10000;
494
495 bfd_put_32 (input_bfd,
496 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
497 contents + relhi->r_offset);
498}
499
500/* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
501 inplace relocation; this function exists in order to do the
502 R_M32R_HI16_[SU]LO relocation described above. */
503
504bfd_reloc_status_type
505m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
506 input_section, output_bfd, error_message)
507 bfd *input_bfd;
508 arelent *reloc_entry;
509 asymbol *symbol;
510 PTR data;
511 asection *input_section;
512 bfd *output_bfd;
513 char **error_message;
514{
515 /* This part is from bfd_elf_generic_reloc.
516 If we're relocating, and this an external symbol, we don't want
517 to change anything. */
518 if (output_bfd != (bfd *) NULL
519 && (symbol->flags & BSF_SECTION_SYM) == 0
520 && reloc_entry->addend == 0)
521 {
522 reloc_entry->address += input_section->output_offset;
523 return bfd_reloc_ok;
524 }
525
526 if (m32r_hi16_list != NULL)
527 {
528 struct m32r_hi16 *l;
529
530 l = m32r_hi16_list;
531 while (l != NULL)
532 {
533 unsigned long insn;
534 unsigned long val;
535 unsigned long vallo;
536 struct m32r_hi16 *next;
537
538 /* Do the HI16 relocation. Note that we actually don't need
539 to know anything about the LO16 itself, except where to
540 find the low 16 bits of the addend needed by the LO16. */
541 insn = bfd_get_32 (input_bfd, l->addr);
542 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
543 & 0xffff) ^ 0x8000) - 0x8000;
544 val = ((insn & 0xffff) << 16) + vallo;
545 val += l->addend;
546
547 /* Reaccount for sign extension of low part. */
548 if ((val & 0x8000) != 0)
549 val += 0x10000;
550
551 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
552 bfd_put_32 (input_bfd, insn, l->addr);
553
554 next = l->next;
555 free (l);
556 l = next;
557 }
558
559 m32r_hi16_list = NULL;
560 }
561
562 /* Now do the LO16 reloc in the usual way.
563 ??? It would be nice to call bfd_elf_generic_reloc here,
564 but we have partial_inplace == TRUE. bfd_elf_generic_reloc will
565 pass the handling back to bfd_install_relocation which will install
566 a section relative addend which is wrong. */
567 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
568 input_section, output_bfd, error_message);
569}
570
571/* Do generic partial_inplace relocation.
572 This is a local replacement for bfd_elf_generic_reloc. */
573
574bfd_reloc_status_type
575m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
576 input_section, output_bfd, error_message)
577 bfd *input_bfd;
578 arelent *reloc_entry;
579 asymbol *symbol;
580 PTR data;
581 asection *input_section;
582 bfd *output_bfd;
583 char **error_message ATTRIBUTE_UNUSED;
584{
585 bfd_reloc_status_type ret;
586 bfd_vma relocation;
587 bfd_byte *inplace_address;
588
589 /* This part is from bfd_elf_generic_reloc.
590 If we're relocating, and this an external symbol, we don't want
591 to change anything. */
592 if (output_bfd != (bfd *) NULL
593 && (symbol->flags & BSF_SECTION_SYM) == 0
594 && reloc_entry->addend == 0)
595 {
596 reloc_entry->address += input_section->output_offset;
597 return bfd_reloc_ok;
598 }
599
600 /* Now do the the reloc in the usual way.
601 ??? It would be nice to call bfd_elf_generic_reloc here,
602 but we have partial_inplace == TRUE. bfd_elf_generic_reloc will
603 pass the handling back to bfd_install_relocation which will install
604 a section relative addend which is wrong. */
605
606 /* Sanity check the address (offset in section). */
607 if (reloc_entry->address > input_section->_cooked_size)
608 return bfd_reloc_outofrange;
609
610 ret = bfd_reloc_ok;
611 if (bfd_is_und_section (symbol->section)
612 && output_bfd == (bfd *) NULL)
613 ret = bfd_reloc_undefined;
614
615 if (bfd_is_com_section (symbol->section)
616 || output_bfd != (bfd *) NULL)
617 relocation = 0;
618 else
619 relocation = symbol->value;
620
621 /* Only do this for a final link. */
622 if (output_bfd == (bfd *) NULL)
623 {
624 relocation += symbol->section->output_section->vma;
625 relocation += symbol->section->output_offset;
626 }
627
628 relocation += reloc_entry->addend;
629 inplace_address = (bfd_byte *) data + reloc_entry->address;
630
631#define DOIT(x) \
632 x = ( (x & ~reloc_entry->howto->dst_mask) | \
633 (((x & reloc_entry->howto->src_mask) + relocation) & \
634 reloc_entry->howto->dst_mask))
635
636 switch (reloc_entry->howto->size)
637 {
638 case 1:
639 {
640 short x = bfd_get_16 (input_bfd, inplace_address);
641 DOIT (x);
642 bfd_put_16 (input_bfd, x, inplace_address);
643 }
644 break;
645 case 2:
646 {
647 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
648 DOIT (x);
649 bfd_put_32 (input_bfd, x, inplace_address);
650 }
651 break;
652 default:
653 BFD_ASSERT (0);
654 }
655
656 if (output_bfd != (bfd *) NULL)
657 reloc_entry->address += input_section->output_offset;
658
659 return ret;
660}
661
662/* Handle the R_M32R_SDA16 reloc.
663 This reloc is used to compute the address of objects in the small data area
664 and to perform loads and stores from that area.
665 The lower 16 bits are sign extended and added to the register specified
666 in the instruction, which is assumed to point to _SDA_BASE_. */
667
668static bfd_reloc_status_type
669m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
670 input_section, output_bfd, error_message)
671 bfd *abfd ATTRIBUTE_UNUSED;
672 arelent *reloc_entry;
673 asymbol *symbol;
674 PTR data ATTRIBUTE_UNUSED;
675 asection *input_section;
676 bfd *output_bfd;
677 char **error_message ATTRIBUTE_UNUSED;
678{
679 /* This part is from bfd_elf_generic_reloc. */
680 if (output_bfd != (bfd *) NULL
681 && (symbol->flags & BSF_SECTION_SYM) == 0
682 && (! reloc_entry->howto->partial_inplace
683 || reloc_entry->addend == 0))
684 {
685 reloc_entry->address += input_section->output_offset;
686 return bfd_reloc_ok;
687 }
688
689 if (output_bfd != NULL)
690 {
691 /* FIXME: See bfd_perform_relocation. Is this right? */
692 return bfd_reloc_continue;
693 }
694
695 /* FIXME: not sure what to do here yet. But then again, the linker
696 may never call us. */
697 abort ();
698}
699
700
701/* Map BFD reloc types to M32R ELF reloc types. */
702
703struct m32r_reloc_map
704{
705 bfd_reloc_code_real_type bfd_reloc_val;
706 unsigned char elf_reloc_val;
707};
708
709static const struct m32r_reloc_map m32r_reloc_map[] =
710{
711 { BFD_RELOC_NONE, R_M32R_NONE },
712 { BFD_RELOC_16, R_M32R_16 },
713 { BFD_RELOC_32, R_M32R_32 },
714 { BFD_RELOC_M32R_24, R_M32R_24 },
715 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
716 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
717 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
718 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
719 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
720 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
721 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
722 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
723 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
724};
725
726static reloc_howto_type *
727bfd_elf32_bfd_reloc_type_lookup (abfd, code)
728 bfd *abfd ATTRIBUTE_UNUSED;
729 bfd_reloc_code_real_type code;
730{
731 unsigned int i;
732
733 for (i = 0;
734 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
735 i++)
736 {
737 if (m32r_reloc_map[i].bfd_reloc_val == code)
738 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
739 }
740
741 return NULL;
742}
743
744/* Set the howto pointer for an M32R ELF reloc. */
745
746static void
747m32r_info_to_howto_rel (abfd, cache_ptr, dst)
748 bfd *abfd ATTRIBUTE_UNUSED;
749 arelent *cache_ptr;
750 Elf32_Internal_Rel *dst;
751{
752 unsigned int r_type;
753
754 r_type = ELF32_R_TYPE (dst->r_info);
755 BFD_ASSERT (r_type < (unsigned int) R_M32R_max);
756 cache_ptr->howto = &m32r_elf_howto_table[r_type];
757}
758
759
760/* Given a BFD section, try to locate the corresponding ELF section
761 index. */
762
763boolean
764_bfd_m32r_elf_section_from_bfd_section (abfd, hdr, sec, retval)
765 bfd *abfd ATTRIBUTE_UNUSED;
766 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
767 asection *sec;
768 int *retval;
769{
770 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
771 {
772 *retval = SHN_M32R_SCOMMON;
773 return true;
774 }
775 return false;
776}
777
778/* M32R ELF uses two common sections. One is the usual one, and the other
779 is for small objects. All the small objects are kept together, and then
780 referenced via one register, which yields faster assembler code. It is
781 up to the compiler to emit an instruction to load the register with
782 _SDA_BASE. This is what we use for the small common section. This
783 approach is copied from elf32-mips.c. */
784static asection m32r_elf_scom_section;
785static asymbol m32r_elf_scom_symbol;
786static asymbol *m32r_elf_scom_symbol_ptr;
787
788/* Handle the special M32R section numbers that a symbol may use. */
789
790void
791_bfd_m32r_elf_symbol_processing (abfd, asym)
792 bfd *abfd ATTRIBUTE_UNUSED;
793 asymbol *asym;
794{
795 elf_symbol_type *elfsym;
796
797 elfsym = (elf_symbol_type *) asym;
798
799 switch (elfsym->internal_elf_sym.st_shndx)
800 {
801 case SHN_M32R_SCOMMON:
802 if (m32r_elf_scom_section.name == NULL)
803 {
804 /* Initialize the small common section. */
805 m32r_elf_scom_section.name = ".scommon";
806 m32r_elf_scom_section.flags = SEC_IS_COMMON;
807 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
808 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
809 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
810 m32r_elf_scom_symbol.name = ".scommon";
811 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
812 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
813 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
814 }
815 asym->section = &m32r_elf_scom_section;
816 asym->value = elfsym->internal_elf_sym.st_size;
817 break;
818 }
819}
820
821/* Hook called by the linker routine which adds symbols from an object
822 file. We must handle the special M32R section numbers here.
823 We also keep watching for whether we need to create the sdata special
824 linker sections. */
825
826static boolean
827m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
828 bfd *abfd;
829 struct bfd_link_info *info;
830 const Elf_Internal_Sym *sym;
831 const char **namep;
832 flagword *flagsp ATTRIBUTE_UNUSED;
833 asection **secp;
834 bfd_vma *valp;
835{
836 if (! info->relocateable
837 && (*namep)[0] == '_' && (*namep)[1] == 'S'
838 && strcmp (*namep, "_SDA_BASE_") == 0)
839 {
840 /* This is simpler than using _bfd_elf_create_linker_section
841 (our needs are simpler than ppc's needs). Also
842 _bfd_elf_create_linker_section currently has a bug where if a .sdata
843 section already exists a new one is created that follows it which
844 screws of _SDA_BASE_ address calcs because output_offset != 0. */
845 struct elf_link_hash_entry *h;
846 asection *s = bfd_get_section_by_name (abfd, ".sdata");
847
848 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
849
850 if (s == NULL)
851 {
852 int flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
853 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
854
855 s = bfd_make_section_anyway (abfd, ".sdata");
856 if (s == NULL)
857 return false;
858 bfd_set_section_flags (abfd, s, flags);
859 bfd_set_section_alignment (abfd, s, 2);
860 }
861
862 h = (struct elf_link_hash_entry *)
863 bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, false);
864
865 if ((h == NULL || h->root.type == bfd_link_hash_undefined)
866 && !(_bfd_generic_link_add_one_symbol (info,
867 abfd,
868 "_SDA_BASE_",
869 BSF_GLOBAL,
870 s,
871 32768,
872 (const char *) NULL,
873 false,
874 get_elf_backend_data (abfd)->collect,
875 (struct bfd_link_hash_entry **) &h)))
876 return false;
877 h->type = STT_OBJECT;
878 }
879
880 switch (sym->st_shndx)
881 {
882 case SHN_M32R_SCOMMON:
883 *secp = bfd_make_section_old_way (abfd, ".scommon");
884 (*secp)->flags |= SEC_IS_COMMON;
885 *valp = sym->st_size;
886 break;
887 }
888
889 return true;
890}
891
892/* We have to figure out the SDA_BASE value, so that we can adjust the
893 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
894 BFD. If we can't find it, we're stuck. We cache it in the ELF
895 target data. We don't need to adjust the symbol value for an
896 external symbol if we are producing relocateable output. */
897
898static bfd_reloc_status_type
899m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
900 bfd *output_bfd;
901 struct bfd_link_info *info;
902 const char **error_message;
903 bfd_vma *psb;
904{
905 if (elf_gp (output_bfd) == 0)
906 {
907 struct bfd_link_hash_entry *h;
908
909 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
910 if (h != (struct bfd_link_hash_entry *) NULL
911 && h->type == bfd_link_hash_defined)
912 elf_gp (output_bfd) = (h->u.def.value
913 + h->u.def.section->output_section->vma
914 + h->u.def.section->output_offset);
915 else
916 {
917 /* Only get the error once. */
918 *psb = elf_gp (output_bfd) = 4;
919 *error_message =
920 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
921 return bfd_reloc_dangerous;
922 }
923 }
924 *psb = elf_gp (output_bfd);
925 return bfd_reloc_ok;
926}
927
928
929/* Relocate an M32R/D ELF section.
930 There is some attempt to make this function usable for many architectures,
931 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
932 if only to serve as a learning tool.
933
934 The RELOCATE_SECTION function is called by the new ELF backend linker
935 to handle the relocations for a section.
936
937 The relocs are always passed as Rela structures; if the section
938 actually uses Rel structures, the r_addend field will always be
939 zero.
940
941 This function is responsible for adjust the section contents as
942 necessary, and (if using Rela relocs and generating a
943 relocateable output file) adjusting the reloc addend as
944 necessary.
945
946 This function does not have to worry about setting the reloc
947 address or the reloc symbol index.
948
949 LOCAL_SYMS is a pointer to the swapped in local symbols.
950
951 LOCAL_SECTIONS is an array giving the section in the input file
952 corresponding to the st_shndx field of each local symbol.
953
954 The global hash table entry for the global symbols can be found
955 via elf_sym_hashes (input_bfd).
956
957 When generating relocateable output, this function must handle
958 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
959 going to be the section symbol corresponding to the output
960 section, which means that the addend must be adjusted
961 accordingly. */
962
963static boolean
964m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
965 contents, relocs, local_syms, local_sections)
966 bfd *output_bfd ATTRIBUTE_UNUSED;
967 struct bfd_link_info *info;
968 bfd *input_bfd;
969 asection *input_section;
970 bfd_byte *contents;
971 Elf_Internal_Rela *relocs;
972 Elf_Internal_Sym *local_syms;
973 asection **local_sections;
974{
975 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
976 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
977 Elf_Internal_Rela *rel, *relend;
978 /* Assume success. */
979 boolean ret = true;
980
981 rel = relocs;
982 relend = relocs + input_section->reloc_count;
983 for (; rel < relend; rel++)
984 {
985 int r_type;
986 reloc_howto_type *howto;
987 unsigned long r_symndx;
988 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
989 ensure it's zero (we use REL relocs, not RELA). Therefore this
990 should be assigning zero to `addend', but for clarity we use
991 `r_addend'. */
992 bfd_vma addend = rel->r_addend;
993 bfd_vma offset = rel->r_offset;
994 struct elf_link_hash_entry *h;
995 Elf_Internal_Sym *sym;
996 asection *sec;
997 const char *sym_name;
998 bfd_reloc_status_type r;
999 const char *errmsg = NULL;
1000
1001 h = NULL;
1002 r_type = ELF32_R_TYPE (rel->r_info);
1003 if (r_type < 0 || r_type >= (int) R_M32R_max)
1004 {
1005 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
1006 bfd_get_filename (input_bfd),
1007 (int) r_type);
1008 bfd_set_error (bfd_error_bad_value);
1009 ret = false;
1010 continue;
1011 }
1012
1013 if (r_type == R_M32R_GNU_VTENTRY
1014 || r_type == R_M32R_GNU_VTINHERIT)
1015 continue;
1016
1017 howto = m32r_elf_howto_table + r_type;
1018 r_symndx = ELF32_R_SYM (rel->r_info);
1019
1020 if (info->relocateable)
1021 {
1022 /* This is a relocateable link. We don't have to change
1023 anything, unless the reloc is against a section symbol,
1024 in which case we have to adjust according to where the
1025 section symbol winds up in the output section. */
1026 sec = NULL;
1027 if (r_symndx >= symtab_hdr->sh_info)
1028 {
1029 /* External symbol. */
1030 continue;
1031 }
1032
1033 /* Local symbol. */
1034 sym = local_syms + r_symndx;
1035 sym_name = "<local symbol>";
1036 /* STT_SECTION: symbol is associated with a section. */
1037 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
1038 {
1039 /* Symbol isn't associated with a section. Nothing to do. */
1040 continue;
1041 }
1042
1043 sec = local_sections[r_symndx];
1044 addend += sec->output_offset + sym->st_value;
1045#ifndef USE_REL
1046 /* This can't be done for USE_REL because it doesn't mean anything
1047 and elf_link_input_bfd asserts this stays zero. */
1048 rel->r_addend = addend;
1049#endif
1050
1051#ifndef USE_REL
1052 /* Addends are stored with relocs. We're done. */
1053 continue;
1054#else /* USE_REL */
1055 /* If partial_inplace, we need to store any additional addend
1056 back in the section. */
1057 if (! howto->partial_inplace)
1058 continue;
1059 /* ??? Here is a nice place to call a special_function
1060 like handler. */
1061 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
1062 r = _bfd_relocate_contents (howto, input_bfd,
1063 addend, contents + offset);
1064 else
1065 {
1066 Elf_Internal_Rela *lorel;
1067
1068 /* We allow an arbitrary number of HI16 relocs before the
1069 LO16 reloc. This permits gcc to emit the HI and LO relocs
1070 itself. */
1071 for (lorel = rel + 1;
1072 (lorel < relend
1073 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
1074 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
1075 lorel++)
1076 continue;
1077 if (lorel < relend
1078 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
1079 {
1080 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
1081 contents, addend);
1082 r = bfd_reloc_ok;
1083 }
1084 else
1085 r = _bfd_relocate_contents (howto, input_bfd,
1086 addend, contents + offset);
1087 }
1088#endif /* USE_REL */
1089 }
1090 else
1091 {
1092 bfd_vma relocation;
1093
1094 /* This is a final link. */
1095 sym = NULL;
1096 sec = NULL;
1097
1098 if (r_symndx < symtab_hdr->sh_info)
1099 {
1100 /* Local symbol. */
1101 sym = local_syms + r_symndx;
1102 sec = local_sections[r_symndx];
1103 sym_name = "<local symbol>";
1104 relocation = (sec->output_section->vma
1105 + sec->output_offset
1106 + sym->st_value);
1107 }
1108 else
1109 {
1110 /* External symbol. */
1111 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1112 while (h->root.type == bfd_link_hash_indirect
1113 || h->root.type == bfd_link_hash_warning)
1114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1115 sym_name = h->root.root.string;
1116
1117 if (h->root.type == bfd_link_hash_defined
1118 || h->root.type == bfd_link_hash_defweak)
1119 {
1120 sec = h->root.u.def.section;
1121 if (sec->output_section == NULL)
1122 relocation = 0;
1123 else
1124 relocation = (h->root.u.def.value
1125 + sec->output_section->vma
1126 + sec->output_offset);
1127 }
1128 else if (h->root.type == bfd_link_hash_undefweak)
1129 relocation = 0;
1130 else
1131 {
1132 if (! ((*info->callbacks->undefined_symbol)
1133 (info, h->root.root.string, input_bfd,
1134 input_section, offset, true)))
1135 return false;
1136 relocation = 0;
1137 }
1138 }
1139
1140 /* Sanity check the address. */
1141 if (offset > input_section->_raw_size)
1142 {
1143 r = bfd_reloc_outofrange;
1144 goto check_reloc;
1145 }
1146
1147 switch ((int) r_type)
1148 {
1149 case (int) R_M32R_10_PCREL :
1150 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
1151 contents, offset,
1152 sec, relocation, addend);
1153 break;
1154
1155 case (int) R_M32R_HI16_SLO :
1156 case (int) R_M32R_HI16_ULO :
1157 {
1158 Elf_Internal_Rela *lorel;
1159
1160 /* We allow an arbitrary number of HI16 relocs before the
1161 LO16 reloc. This permits gcc to emit the HI and LO relocs
1162 itself. */
1163 for (lorel = rel + 1;
1164 (lorel < relend
1165 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
1166 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
1167 lorel++)
1168 continue;
1169 if (lorel < relend
1170 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
1171 {
1172 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
1173 contents, relocation + addend);
1174 r = bfd_reloc_ok;
1175 }
1176 else
1177 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1178 contents, offset,
1179 relocation, addend);
1180 }
1181 break;
1182
1183 case (int) R_M32R_SDA16 :
1184 {
1185 const char *name;
1186
1187 BFD_ASSERT (sec != NULL);
1188 name = bfd_get_section_name (abfd, sec);
1189
1190 if (strcmp (name, ".sdata") == 0
1191 || strcmp (name, ".sbss") == 0
1192 || strcmp (name, ".scommon") == 0)
1193 {
1194 bfd_vma sda_base;
1195 bfd *out_bfd = sec->output_section->owner;
1196
1197 r = m32r_elf_final_sda_base (out_bfd, info,
1198 &errmsg,
1199 &sda_base);
1200 if (r != bfd_reloc_ok)
1201 {
1202 ret = false;
1203 goto check_reloc;
1204 }
1205
1206 /* At this point `relocation' contains the object's
1207 address. */
1208 relocation -= sda_base;
1209 /* Now it contains the offset from _SDA_BASE_. */
1210 }
1211 else
1212 {
1213 (*_bfd_error_handler) (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
1214 bfd_get_filename (input_bfd),
1215 sym_name,
1216 m32r_elf_howto_table[(int) r_type].name,
1217 bfd_get_section_name (abfd, sec));
1218 /*bfd_set_error (bfd_error_bad_value); ??? why? */
1219 ret = false;
1220 continue;
1221 }
1222 }
1223 /* fall through */
1224
1225 default :
1226 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1227 contents, offset,
1228 relocation, addend);
1229 break;
1230 }
1231 }
1232
1233 check_reloc:
1234
1235 if (r != bfd_reloc_ok)
1236 {
1237 /* FIXME: This should be generic enough to go in a utility. */
1238 const char *name;
1239
1240 if (h != NULL)
1241 name = h->root.root.string;
1242 else
1243 {
1244 name = (bfd_elf_string_from_elf_section
1245 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1246 if (name == NULL || *name == '\0')
1247 name = bfd_section_name (input_bfd, sec);
1248 }
1249
1250 if (errmsg != NULL)
1251 goto common_error;
1252
1253 switch (r)
1254 {
1255 case bfd_reloc_overflow:
1256 if (! ((*info->callbacks->reloc_overflow)
1257 (info, name, howto->name, (bfd_vma) 0,
1258 input_bfd, input_section, offset)))
1259 return false;
1260 break;
1261
1262 case bfd_reloc_undefined:
1263 if (! ((*info->callbacks->undefined_symbol)
1264 (info, name, input_bfd, input_section,
1265 offset, true)))
1266 return false;
1267 break;
1268
1269 case bfd_reloc_outofrange:
1270 errmsg = _("internal error: out of range error");
1271 goto common_error;
1272
1273 case bfd_reloc_notsupported:
1274 errmsg = _("internal error: unsupported relocation error");
1275 goto common_error;
1276
1277 case bfd_reloc_dangerous:
1278 errmsg = _("internal error: dangerous error");
1279 goto common_error;
1280
1281 default:
1282 errmsg = _("internal error: unknown error");
1283 /* fall through */
1284
1285 common_error:
1286 if (!((*info->callbacks->warning)
1287 (info, errmsg, name, input_bfd, input_section,
1288 offset)))
1289 return false;
1290 break;
1291 }
1292 }
1293 }
1294
1295 return ret;
1296}
1297
1298
1299#if 0 /* relaxing not supported yet */
1300
1301/* This function handles relaxing for the m32r.
1302 Relaxing on the m32r is tricky because of instruction alignment
1303 requirements (4 byte instructions must be aligned on 4 byte boundaries).
1304
1305 The following relaxing opportunities are handled:
1306
1307 seth/add3/jl -> bl24 or bl8
1308 seth/add3 -> ld24
1309
1310 It would be nice to handle bl24 -> bl8 but given:
1311
1312 - 4 byte insns must be on 4 byte boundaries
1313 - branch instructions only branch to insns on 4 byte boundaries
1314
1315 this isn't much of a win because the insn in the 2 "deleted" bytes
1316 must become a nop. With some complexity some real relaxation could be
1317 done but the frequency just wouldn't make it worth it; it's better to
1318 try to do all the code compaction one can elsewhere.
1319 When the chip supports parallel 16 bit insns, things may change.
1320*/
1321
1322static boolean
1323m32r_elf_relax_section (abfd, sec, link_info, again)
1324 bfd *abfd;
1325 asection *sec;
1326 struct bfd_link_info *link_info;
1327 boolean *again;
1328{
1329 Elf_Internal_Shdr *symtab_hdr;
1330 /* The Rela structures are used here because that's what
1331 _bfd_elf32_link_read_relocs uses [for convenience - it sets the addend
1332 field to 0]. */
1333 Elf_Internal_Rela *internal_relocs;
1334 Elf_Internal_Rela *free_relocs = NULL;
1335 Elf_Internal_Rela *irel, *irelend;
1336 bfd_byte *contents = NULL;
1337 bfd_byte *free_contents = NULL;
1338 Elf32_External_Sym *extsyms = NULL;
1339 Elf32_External_Sym *free_extsyms = NULL;
1340
1341 /* Assume nothing changes. */
1342 *again = false;
1343
1344 /* We don't have to do anything for a relocateable link, if
1345 this section does not have relocs, or if this is not a
1346 code section. */
1347 if (link_info->relocateable
1348 || (sec->flags & SEC_RELOC) == 0
1349 || sec->reloc_count == 0
1350 || (sec->flags & SEC_CODE) == 0
1351 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
1352 return true;
1353
1354 /* If this is the first time we have been called for this section,
1355 initialize the cooked size. */
1356 if (sec->_cooked_size == 0)
1357 sec->_cooked_size = sec->_raw_size;
1358
1359 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1360
1361 /* Get a copy of the native relocations. */
1362 internal_relocs = (_bfd_elf32_link_read_relocs
1363 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1364 link_info->keep_memory));
1365 if (internal_relocs == NULL)
1366 goto error_return;
1367 if (! link_info->keep_memory)
1368 free_relocs = internal_relocs;
1369
1370 /* Walk through them looking for relaxing opportunities. */
1371 irelend = internal_relocs + sec->reloc_count;
1372 for (irel = internal_relocs; irel < irelend; irel++)
1373 {
1374 bfd_vma symval;
1375
1376 /* If this isn't something that can be relaxed, then ignore
1377 this reloc. */
1378 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
1379 continue;
1380
1381 /* Get the section contents if we haven't done so already. */
1382 if (contents == NULL)
1383 {
1384 /* Get cached copy if it exists. */
1385 if (elf_section_data (sec)->this_hdr.contents != NULL)
1386 contents = elf_section_data (sec)->this_hdr.contents;
1387 else
1388 {
1389 /* Go get them off disk. */
1390 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1391 if (contents == NULL)
1392 goto error_return;
1393 free_contents = contents;
1394
1395 if (! bfd_get_section_contents (abfd, sec, contents,
1396 (file_ptr) 0, sec->_raw_size))
1397 goto error_return;
1398 }
1399 }
1400
1401 /* Read this BFD's symbols if we haven't done so already. */
1402 if (extsyms == NULL)
1403 {
1404 /* Get cached copy if it exists. */
1405 if (symtab_hdr->contents != NULL)
1406 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1407 else
1408 {
1409 /* Go get them off disk. */
1410 extsyms = ((Elf32_External_Sym *)
1411 bfd_malloc (symtab_hdr->sh_size));
1412 if (extsyms == NULL)
1413 goto error_return;
1414 free_extsyms = extsyms;
1415 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1416 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1417 != symtab_hdr->sh_size))
1418 goto error_return;
1419 }
1420 }
1421
1422 /* Get the value of the symbol referred to by the reloc. */
1423 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1424 {
1425 Elf_Internal_Sym isym;
1426 asection *sym_sec;
1427
1428 /* A local symbol. */
1429 bfd_elf32_swap_symbol_in (abfd,
1430 extsyms + ELF32_R_SYM (irel->r_info),
1431 &isym);
1432
1433 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1434 symval = (isym.st_value
1435 + sym_sec->output_section->vma
1436 + sym_sec->output_offset);
1437 }
1438 else
1439 {
1440 unsigned long indx;
1441 struct elf_link_hash_entry *h;
1442
1443 /* An external symbol. */
1444 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1445 h = elf_sym_hashes (abfd)[indx];
1446 BFD_ASSERT (h != NULL);
1447 if (h->root.type != bfd_link_hash_defined
1448 && h->root.type != bfd_link_hash_defweak)
1449 {
1450 /* This appears to be a reference to an undefined
1451 symbol. Just ignore it--it will be caught by the
1452 regular reloc processing. */
1453 continue;
1454 }
1455
1456 symval = (h->root.u.def.value
1457 + h->root.u.def.section->output_section->vma
1458 + h->root.u.def.section->output_offset);
1459 }
1460
1461 /* For simplicity of coding, we are going to modify the section
1462 contents, the section relocs, and the BFD symbol table. We
1463 must tell the rest of the code not to free up this
1464 information. It would be possible to instead create a table
1465 of changes which have to be made, as is done in coff-mips.c;
1466 that would be more work, but would require less memory when
1467 the linker is run. */
1468
1469 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
1470 This sequence is generated by the compiler when compiling in
1471 32 bit mode. Also look for seth/add3 -> ld24. */
1472
1473 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
1474 {
1475 Elf_Internal_Rela *nrel;
1476 bfd_vma pc = (sec->output_section->vma + sec->output_offset
1477 + irel->r_offset);
1478 bfd_signed_vma pcrel_value = symval - pc;
1479 unsigned int code,reg;
1480 int addend,nop_p,bl8_p,to_delete;
1481
1482 /* The tests are ordered so that we get out as quickly as possible
1483 if this isn't something we can relax, taking into account that
1484 we are looking for two separate possibilities (jl/ld24). */
1485
1486 /* Do nothing if no room in the section for this to be what we're
1487 looking for. */
1488 if (irel->r_offset > sec->_cooked_size - 8)
1489 continue;
1490
1491 /* Make sure the next relocation applies to the next
1492 instruction and that it's the add3's reloc. */
1493 nrel = irel + 1;
1494 if (nrel == irelend
1495 || irel->r_offset + 4 != nrel->r_offset
1496 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
1497 continue;
1498
1499 /* See if the instructions are seth/add3. */
1500 /* FIXME: This is where macros from cgen can come in. */
1501 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
1502 if ((code & 0xf0ff) != 0xd0c0)
1503 continue; /* not seth rN,foo */
1504 reg = (code & 0x0f00) >> 8;
1505 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
1506 if (code != (0x80a0 | reg | (reg << 8)))
1507 continue; /* not add3 rN,rN,foo */
1508
1509 /* At this point we've confirmed we have seth/add3. Now check
1510 whether the next insn is a jl, in which case try to change this
1511 to bl24 or bl8. */
1512
1513 /* Ensure the branch target is in range.
1514 The bl24 instruction has a 24 bit operand which is the target
1515 address right shifted by 2, giving a signed range of 26 bits.
1516 Note that 4 bytes are added to the high value because the target
1517 will be at least 4 bytes closer if we can relax. It'll actually
1518 be 4 or 8 bytes closer, but we don't know which just yet and
1519 the difference isn't significant enough to worry about. */
1520#ifndef USE_REL /* put in for learning purposes */
1521 pcrel_value += irel->r_addend;
1522#else
1523 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
1524 pcrel_value += addend;
1525#endif
1526
1527 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
1528 /* Do nothing if no room in the section for this to be what we're
1529 looking for. */
1530 && (irel->r_offset <= sec->_cooked_size - 12)
1531 /* Ensure the next insn is "jl rN". */
1532 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
1533 code != (0x1ec0 | reg)))
1534 {
1535 /* We can relax to bl24/bl8. */
1536
1537 /* See if there's a nop following the jl.
1538 Also see if we can use a bl8 insn. */
1539 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
1540 nop_p = (code & 0x7fff) == NOP_INSN;
1541 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
1542
1543 if (bl8_p)
1544 {
1545 /* Change "seth rN,foo" to "bl8 foo || nop".
1546 We OR in CODE just in case it's not a nop (technically,
1547 CODE currently must be a nop, but for cleanness we
1548 allow it to be anything). */
1549#ifndef USE_REL /* put in for learning purposes */
1550 code = 0x7e000000 | MAKE_PARALLEL (code);
1551#else
1552 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
1553#endif
1554 to_delete = 8;
1555 }
1556 else
1557 {
1558 /* Change the seth rN,foo to a bl24 foo. */
1559#ifndef USE_REL /* put in for learning purposes */
1560 code = 0xfe000000;
1561#else
1562 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
1563#endif
1564 to_delete = nop_p ? 8 : 4;
1565 }
1566
1567 bfd_put_32 (abfd, code, contents + irel->r_offset);
1568
1569 /* Set the new reloc type. */
1570 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1571 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
1572
1573 /* Delete the add3 reloc by making it a null reloc. */
1574 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1575 R_M32R_NONE);
1576 }
1577 else if (addend >= 0
1578 && symval + addend <= 0xffffff)
1579 {
1580 /* We can relax to ld24. */
1581
1582 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
1583 bfd_put_32 (abfd, code, contents + irel->r_offset);
1584 to_delete = 4;
1585 /* Tell the following code a nop filler isn't needed. */
1586 nop_p = 1;
1587 }
1588 else
1589 {
1590 /* Can't do anything here. */
1591 continue;
1592 }
1593
1594 /* Note that we've changed the relocs, section contents, etc. */
1595 elf_section_data (sec)->relocs = internal_relocs;
1596 free_relocs = NULL;
1597
1598 elf_section_data (sec)->this_hdr.contents = contents;
1599 free_contents = NULL;
1600
1601 symtab_hdr->contents = (bfd_byte *) extsyms;
1602 free_extsyms = NULL;
1603
1604 /* Delete TO_DELETE bytes of data. */
1605 if (!m32r_elf_relax_delete_bytes (abfd, sec,
1606 irel->r_offset + 4, to_delete))
1607 goto error_return;
1608
1609 /* Now that the following bytes have been moved into place, see if
1610 we need to replace the jl with a nop. This happens when we had
1611 to use a bl24 insn and the insn following the jl isn't a nop.
1612 Technically, this situation can't happen (since the insn can
1613 never be executed) but to be clean we do this. When the chip
1614 supports parallel 16 bit insns things may change.
1615 We don't need to do this in the case of relaxing to ld24,
1616 and the above code sets nop_p so this isn't done. */
1617 if (! nop_p && to_delete == 4)
1618 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
1619
1620 /* That will change things, so we should relax again.
1621 Note that this is not required, and it may be slow. */
1622 *again = true;
1623
1624 continue;
1625 }
1626
1627 /* loop to try the next reloc */
1628 }
1629
1630 if (free_relocs != NULL)
1631 {
1632 free (free_relocs);
1633 free_relocs = NULL;
1634 }
1635
1636 if (free_contents != NULL)
1637 {
1638 if (! link_info->keep_memory)
1639 free (free_contents);
1640 else
1641 {
1642 /* Cache the section contents for elf_link_input_bfd. */
1643 elf_section_data (sec)->this_hdr.contents = contents;
1644 }
1645 free_contents = NULL;
1646 }
1647
1648 if (free_extsyms != NULL)
1649 {
1650 if (! link_info->keep_memory)
1651 free (free_extsyms);
1652 else
1653 {
1654 /* Cache the symbols for elf_link_input_bfd. */
1655 symtab_hdr->contents = extsyms;
1656 }
1657 free_extsyms = NULL;
1658 }
1659
1660 return true;
1661
1662 error_return:
1663 if (free_relocs != NULL)
1664 free (free_relocs);
1665 if (free_contents != NULL)
1666 free (free_contents);
1667 if (free_extsyms != NULL)
1668 free (free_extsyms);
1669 return false;
1670}
1671
1672/* Delete some bytes from a section while relaxing. */
1673
1674static boolean
1675m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
1676 bfd *abfd;
1677 asection *sec;
1678 bfd_vma addr;
1679 int count;
1680{
1681 Elf_Internal_Shdr *symtab_hdr;
1682 Elf32_External_Sym *extsyms;
1683 int shndx, index;
1684 bfd_byte *contents;
1685 Elf_Internal_Rela *irel, *irelend;
1686 Elf_Internal_Rela *irelalign;
1687 bfd_vma toaddr;
1688 Elf32_External_Sym *esym, *esymend;
1689 struct elf_link_hash_entry *sym_hash;
1690
1691 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1692 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1693
1694 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1695
1696 contents = elf_section_data (sec)->this_hdr.contents;
1697
1698 /* The deletion must stop at the next ALIGN reloc for an aligment
1699 power larger than the number of bytes we are deleting. */
1700
1701 irelalign = NULL;
1702 toaddr = sec->_cooked_size;
1703
1704 irel = elf_section_data (sec)->relocs;
1705 irelend = irel + sec->reloc_count;
1706
1707 /* Actually delete the bytes. */
1708 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1709 sec->_cooked_size -= count;
1710
1711 /* Adjust all the relocs. */
1712 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1713 {
1714 /* Get the new reloc address. */
1715 if ((irel->r_offset > addr
1716 && irel->r_offset < toaddr))
1717 irel->r_offset -= count;
1718 }
1719
1720 /* Adjust the local symbols defined in this section. */
1721 esym = extsyms;
1722 esymend = esym + symtab_hdr->sh_info;
1723 for (; esym < esymend; esym++)
1724 {
1725 Elf_Internal_Sym isym;
1726
1727 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1728
1729 if (isym.st_shndx == shndx
1730 && isym.st_value > addr
1731 && isym.st_value < toaddr)
1732 {
1733 isym.st_value -= count;
1734 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1735 }
1736 }
1737
1738 /* Now adjust the global symbols defined in this section. */
1739 esym = extsyms + symtab_hdr->sh_info;
1740 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1741 for (index = 0; esym < esymend; esym++, index++)
1742 {
1743 Elf_Internal_Sym isym;
1744
1745 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1746 sym_hash = elf_sym_hashes (abfd)[index];
1747 if (isym.st_shndx == shndx
1748 && ((sym_hash)->root.type == bfd_link_hash_defined
1749 || (sym_hash)->root.type == bfd_link_hash_defweak)
1750 && (sym_hash)->root.u.def.section == sec
1751 && (sym_hash)->root.u.def.value > addr
1752 && (sym_hash)->root.u.def.value < toaddr)
1753 {
1754 (sym_hash)->root.u.def.value -= count;
1755 }
1756 }
1757
1758 return true;
1759}
1760
1761/* This is a version of bfd_generic_get_relocated_section_contents
1762 which uses m32r_elf_relocate_section. */
1763
1764static bfd_byte *
1765m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1766 data, relocateable, symbols)
1767 bfd *output_bfd;
1768 struct bfd_link_info *link_info;
1769 struct bfd_link_order *link_order;
1770 bfd_byte *data;
1771 boolean relocateable;
1772 asymbol **symbols;
1773{
1774 Elf_Internal_Shdr *symtab_hdr;
1775 asection *input_section = link_order->u.indirect.section;
1776 bfd *input_bfd = input_section->owner;
1777 asection **sections = NULL;
1778 Elf_Internal_Rela *internal_relocs = NULL;
1779 Elf32_External_Sym *external_syms = NULL;
1780 Elf_Internal_Sym *internal_syms = NULL;
1781
1782 /* We only need to handle the case of relaxing, or of having a
1783 particular set of section contents, specially. */
1784 if (relocateable
1785 || elf_section_data (input_section)->this_hdr.contents == NULL)
1786 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1787 link_order, data,
1788 relocateable,
1789 symbols);
1790
1791 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1792
1793 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1794 input_section->_raw_size);
1795
1796 if ((input_section->flags & SEC_RELOC) != 0
1797 && input_section->reloc_count > 0)
1798 {
1799 Elf_Internal_Sym *isymp;
1800 asection **secpp;
1801 Elf32_External_Sym *esym, *esymend;
1802
1803 if (symtab_hdr->contents != NULL)
1804 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1805 else
1806 {
1807 external_syms = ((Elf32_External_Sym *)
1808 bfd_malloc (symtab_hdr->sh_info
1809 * sizeof (Elf32_External_Sym)));
1810 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1811 goto error_return;
1812 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1813 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1814 symtab_hdr->sh_info, input_bfd)
1815 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1816 goto error_return;
1817 }
1818
1819 internal_relocs = (_bfd_elf32_link_read_relocs
1820 (input_bfd, input_section, (PTR) NULL,
1821 (Elf_Internal_Rela *) NULL, false));
1822 if (internal_relocs == NULL)
1823 goto error_return;
1824
1825 internal_syms = ((Elf_Internal_Sym *)
1826 bfd_malloc (symtab_hdr->sh_info
1827 * sizeof (Elf_Internal_Sym)));
1828 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1829 goto error_return;
1830
1831 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1832 * sizeof (asection *));
1833 if (sections == NULL && symtab_hdr->sh_info > 0)
1834 goto error_return;
1835
1836 isymp = internal_syms;
1837 secpp = sections;
1838 esym = external_syms;
1839 esymend = esym + symtab_hdr->sh_info;
1840 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1841 {
1842 asection *isec;
1843
1844 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1845
1846 if (isymp->st_shndx == SHN_UNDEF)
1847 isec = bfd_und_section_ptr;
1848 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1849 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1850 else if (isymp->st_shndx == SHN_ABS)
1851 isec = bfd_abs_section_ptr;
1852 else if (isymp->st_shndx == SHN_COMMON)
1853 isec = bfd_com_section_ptr;
1854 else if (isymp->st_shndx == SHN_M32R_SCOMMON)
1855 isec = &m32r_elf_scom_section;
1856 else
1857 {
1858 /* Who knows? */
1859 isec = NULL;
1860 }
1861
1862 *secpp = isec;
1863 }
1864
1865 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
1866 input_section, data, internal_relocs,
1867 internal_syms, sections))
1868 goto error_return;
1869
1870 if (sections != NULL)
1871 free (sections);
1872 sections = NULL;
1873 if (internal_syms != NULL)
1874 free (internal_syms);
1875 internal_syms = NULL;
1876 if (external_syms != NULL && symtab_hdr->contents == NULL)
1877 free (external_syms);
1878 external_syms = NULL;
1879 if (internal_relocs != elf_section_data (input_section)->relocs)
1880 free (internal_relocs);
1881 internal_relocs = NULL;
1882 }
1883
1884 return data;
1885
1886 error_return:
1887 if (internal_relocs != NULL
1888 && internal_relocs != elf_section_data (input_section)->relocs)
1889 free (internal_relocs);
1890 if (external_syms != NULL && symtab_hdr->contents == NULL)
1891 free (external_syms);
1892 if (internal_syms != NULL)
1893 free (internal_syms);
1894 if (sections != NULL)
1895 free (sections);
1896 return NULL;
1897}
1898
1899#endif /* #if 0 */
1900
1901
1902/* Set the right machine number. */
1903static boolean
1904m32r_elf_object_p (abfd)
1905 bfd *abfd;
1906{
1907 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
1908 {
1909 default:
1910 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
1911 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
1912 }
1913 return true;
1914}
1915
1916/* Store the machine number in the flags field. */
1917static void
1918m32r_elf_final_write_processing (abfd, linker)
1919 bfd * abfd;
1920 boolean linker ATTRIBUTE_UNUSED;
1921{
1922 unsigned long val;
1923
1924 switch (bfd_get_mach (abfd))
1925 {
1926 default:
1927 case bfd_mach_m32r: val = E_M32R_ARCH; break;
1928 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
1929 }
1930
1931 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
1932 elf_elfheader (abfd)->e_flags |= val;
1933}
1934
1935/* Function to keep M32R specific file flags. */
1936static boolean
1937m32r_elf_set_private_flags (abfd, flags)
1938 bfd * abfd;
1939 flagword flags;
1940{
1941 BFD_ASSERT (!elf_flags_init (abfd)
1942 || elf_elfheader (abfd)->e_flags == flags);
1943
1944 elf_elfheader (abfd)->e_flags = flags;
1945 elf_flags_init (abfd) = true;
1946 return true;
1947}
1948
1949/* Copy backend specific data from one object module to another */
1950static boolean
1951m32r_elf_copy_private_bfd_data (ibfd, obfd)
1952 bfd * ibfd;
1953 bfd * obfd;
1954{
1955 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1956 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1957 return true;
1958
1959 BFD_ASSERT (!elf_flags_init (obfd)
1960 || (elf_elfheader (obfd)->e_flags
1961 == elf_elfheader (ibfd)->e_flags));
1962
1963 elf_gp (obfd) = elf_gp (ibfd);
1964 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1965 elf_flags_init (obfd) = true;
1966 return true;
1967}
1968
1969/* Merge backend specific data from an object file to the output
1970 object file when linking. */
1971static boolean
1972m32r_elf_merge_private_bfd_data (ibfd, obfd)
1973 bfd * ibfd;
1974 bfd * obfd;
1975{
1976 flagword out_flags;
1977 flagword in_flags;
1978
1979 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1980 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1981 return true;
1982
1983 in_flags = elf_elfheader (ibfd)->e_flags;
1984 out_flags = elf_elfheader (obfd)->e_flags;
1985
1986 if (! elf_flags_init (obfd))
1987 {
1988 /* If the input is the default architecture then do not
1989 bother setting the flags for the output architecture,
1990 instead allow future merges to do this. If no future
1991 merges ever set these flags then they will retain their
1992 unitialised values, which surprise surprise, correspond
1993 to the default values. */
1994 if (bfd_get_arch_info (ibfd)->the_default)
1995 return true;
1996
1997 elf_flags_init (obfd) = true;
1998 elf_elfheader (obfd)->e_flags = in_flags;
1999
2000 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2001 && bfd_get_arch_info (obfd)->the_default)
2002 {
2003 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
2004 }
2005
2006 return true;
2007 }
2008
2009 /* Check flag compatibility. */
2010 if (in_flags == out_flags)
2011 return true;
2012
2013 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
2014 {
2015 if ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
2016 {
2017 _bfd_error_handler (_("%s: Instruction set mismatch with previous modules"),
2018 bfd_get_filename (ibfd));
2019
2020 bfd_set_error (bfd_error_bad_value);
2021 return false;
2022 }
2023 }
2024
2025 return true;
2026}
2027
2028/* Display the flags field */
2029static boolean
2030m32r_elf_print_private_bfd_data (abfd, ptr)
2031 bfd * abfd;
2032 PTR ptr;
2033{
2034 FILE * file = (FILE *) ptr;
2035
2036 BFD_ASSERT (abfd != NULL && ptr != NULL)
2037
2038 _bfd_elf_print_private_bfd_data (abfd, ptr);
2039
2040 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
2041
2042 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
2043 {
2044 default:
2045 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
2046 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
2047 }
2048
2049 fputc ('\n', file);
2050
2051 return true;
2052}
2053
2054asection *
2055m32r_elf_gc_mark_hook (abfd, info, rel, h, sym)
2056 bfd *abfd;
2057 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2058 Elf_Internal_Rela *rel;
2059 struct elf_link_hash_entry *h;
2060 Elf_Internal_Sym *sym;
2061{
2062 if (h != NULL)
2063 {
2064 switch (ELF32_R_TYPE (rel->r_info))
2065 {
2066 case R_M32R_GNU_VTINHERIT:
2067 case R_M32R_GNU_VTENTRY:
2068 break;
2069
2070 default:
2071 switch (h->root.type)
2072 {
2073 case bfd_link_hash_defined:
2074 case bfd_link_hash_defweak:
2075 return h->root.u.def.section;
2076
2077 case bfd_link_hash_common:
2078 return h->root.u.c.p->section;
2079
2080 default:
2081 break;
2082 }
2083 }
2084 }
2085 else
2086 {
2087 if (!(elf_bad_symtab (abfd)
2088 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2089 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2090 && sym->st_shndx != SHN_COMMON))
2091 {
2092 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2093 }
2094 }
2095 return NULL;
2096}
2097
2098static boolean
2099m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
2100 bfd *abfd ATTRIBUTE_UNUSED;
2101 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2102 asection *sec ATTRIBUTE_UNUSED;
2103 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2104{
2105 /* we don't use got and plt entries for m32r */
2106 return true;
2107}
2108
2109/* Look through the relocs for a section during the first phase.
2110 Since we don't do .gots or .plts, we just need to consider the
2111 virtual table relocs for gc. */
2112
2113static boolean
2114m32r_elf_check_relocs (abfd, info, sec, relocs)
2115 bfd *abfd;
2116 struct bfd_link_info *info;
2117 asection *sec;
2118 const Elf_Internal_Rela *relocs;
2119{
2120 Elf_Internal_Shdr *symtab_hdr;
2121 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2122 const Elf_Internal_Rela *rel;
2123 const Elf_Internal_Rela *rel_end;
2124
2125 if (info->relocateable)
2126 return true;
2127
2128 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2129 sym_hashes = elf_sym_hashes (abfd);
2130 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2131 if (!elf_bad_symtab (abfd))
2132 sym_hashes_end -= symtab_hdr->sh_info;
2133
2134 rel_end = relocs + sec->reloc_count;
2135 for (rel = relocs; rel < rel_end; rel++)
2136 {
2137 struct elf_link_hash_entry *h;
2138 unsigned long r_symndx;
2139
2140 r_symndx = ELF32_R_SYM (rel->r_info);
2141 if (r_symndx < symtab_hdr->sh_info)
2142 h = NULL;
2143 else
2144 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2145
2146 switch (ELF32_R_TYPE (rel->r_info))
2147 {
2148 /* This relocation describes the C++ object vtable hierarchy.
2149 Reconstruct it for later use during GC. */
2150 case R_M32R_GNU_VTINHERIT:
2151 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2152 return false;
2153 break;
2154
2155 /* This relocation describes which C++ vtable entries are actually
2156 used. Record for later use during GC. */
2157 case R_M32R_GNU_VTENTRY:
2158 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2159 return false;
2160 break;
2161 }
2162 }
2163
2164 return true;
2165}
2166
2167
2168#define ELF_ARCH bfd_arch_m32r
2169#define ELF_MACHINE_CODE EM_CYGNUS_M32R
2170#define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
2171
2172#define TARGET_BIG_SYM bfd_elf32_m32r_vec
2173#define TARGET_BIG_NAME "elf32-m32r"
2174
2175#define elf_info_to_howto 0
2176#define elf_info_to_howto_rel m32r_info_to_howto_rel
2177#define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
2178#define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
2179#define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
2180#define elf_backend_relocate_section m32r_elf_relocate_section
2181#define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
2182#define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
2183#define elf_backend_check_relocs m32r_elf_check_relocs
2184
2185#define elf_backend_can_gc_sections 1
2186#if 0 /* not yet */
2187/* relax support */
2188#define bfd_elf32_bfd_relax_section m32r_elf_relax_section
2189#define bfd_elf32_bfd_get_relocated_section_contents \
2190 m32r_elf_get_relocated_section_contents
2191#endif
2192
2193#define elf_backend_object_p m32r_elf_object_p
2194#define elf_backend_final_write_processing m32r_elf_final_write_processing
2195#define bfd_elf32_bfd_copy_private_bfd_data m32r_elf_copy_private_bfd_data
2196#define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
2197#define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
2198#define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
2199
2200#include "elf32-target.h"
Note: See TracBrowser for help on using the repository browser.