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