1 /* M32R-specific support for 32-bit ELF.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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 **));
55 static boolean m32r_elf_relax_delete_bytes
56 PARAMS ((bfd *, asection *, bfd_vma, int));
59 /* Use REL instead of RELA to save space.
60 This only saves space in libraries and object files, but perhaps
61 relocs will be put in ROM? All in all though, REL relocs are a pain
65 static reloc_howto_type m32r_elf_howto_table[] =
67 /* This reloc does nothing. */
68 HOWTO (R_M32R_NONE, /* type */
70 2, /* size (0 = byte, 1 = short, 2 = long) */
72 false, /* pc_relative */
74 complain_overflow_bitfield, /* complain_on_overflow */
75 bfd_elf_generic_reloc, /* special_function */
76 "R_M32R_NONE", /* name */
77 false, /* partial_inplace */
80 false), /* pcrel_offset */
82 /* A 16 bit absolute relocation. */
83 HOWTO (R_M32R_16, /* type */
85 1, /* size (0 = byte, 1 = short, 2 = long) */
87 false, /* pc_relative */
89 complain_overflow_bitfield, /* complain_on_overflow */
90 bfd_elf_generic_reloc, /* special_function */
91 "R_M32R_16", /* name */
92 true, /* partial_inplace */
93 0xffff, /* src_mask */
94 0xffff, /* dst_mask */
95 false), /* pcrel_offset */
97 /* A 32 bit absolute relocation. */
98 HOWTO (R_M32R_32, /* type */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
102 false, /* pc_relative */
104 complain_overflow_bitfield, /* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_M32R_32", /* name */
107 true, /* partial_inplace */
108 0xffffffff, /* src_mask */
109 0xffffffff, /* dst_mask */
110 false), /* pcrel_offset */
112 /* A 24 bit address. */
113 HOWTO (R_M32R_24, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 false, /* pc_relative */
119 complain_overflow_unsigned, /* complain_on_overflow */
120 bfd_elf_generic_reloc, /* special_function */
121 "R_M32R_24", /* name */
122 true, /* partial_inplace */
123 0xffffff, /* src_mask */
124 0xffffff, /* dst_mask */
125 false), /* pcrel_offset */
127 /* An PC Relative 10-bit relocation, shifted by 2.
128 This reloc is complicated because relocations are relative to pc & -4.
129 i.e. branches in the right insn slot use the address of the left insn
131 /* ??? It's not clear whether this should have partial_inplace set or not.
132 Branch relaxing in the assembler can store the addend in the insn,
133 and if bfd_install_relocation gets called the addend may get added
135 HOWTO (R_M32R_10_PCREL, /* type */
137 1, /* size (0 = byte, 1 = short, 2 = long) */
139 true, /* pc_relative */
141 complain_overflow_signed, /* complain_on_overflow */
142 m32r_elf_10_pcrel_reloc, /* special_function */
143 "R_M32R_10_PCREL", /* name */
144 false, /* partial_inplace */
147 true), /* pcrel_offset */
149 /* A relative 18 bit relocation, right shifted by 2. */
150 HOWTO (R_M32R_18_PCREL, /* type */
152 2, /* size (0 = byte, 1 = short, 2 = long) */
154 true, /* pc_relative */
156 complain_overflow_signed, /* complain_on_overflow */
157 bfd_elf_generic_reloc, /* special_function */
158 "R_M32R_18_PCREL", /* name */
159 false, /* partial_inplace */
160 0xffff, /* src_mask */
161 0xffff, /* dst_mask */
162 true), /* pcrel_offset */
164 /* A relative 26 bit relocation, right shifted by 2. */
165 /* ??? It's not clear whether this should have partial_inplace set or not.
166 Branch relaxing in the assembler can store the addend in the insn,
167 and if bfd_install_relocation gets called the addend may get added
169 HOWTO (R_M32R_26_PCREL, /* type */
171 2, /* size (0 = byte, 1 = short, 2 = long) */
173 true, /* pc_relative */
175 complain_overflow_signed, /* complain_on_overflow */
176 bfd_elf_generic_reloc, /* special_function */
177 "R_M32R_26_PCREL", /* name */
178 false, /* partial_inplace */
179 0xffffff, /* src_mask */
180 0xffffff, /* dst_mask */
181 true), /* pcrel_offset */
183 /* High 16 bits of address when lower 16 is or'd in. */
184 HOWTO (R_M32R_HI16_ULO, /* type */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
188 false, /* pc_relative */
190 complain_overflow_dont, /* complain_on_overflow */
191 m32r_elf_hi16_reloc, /* special_function */
192 "R_M32R_HI16_ULO", /* name */
193 true, /* partial_inplace */
194 0x0000ffff, /* src_mask */
195 0x0000ffff, /* dst_mask */
196 false), /* pcrel_offset */
198 /* High 16 bits of address when lower 16 is added in. */
199 HOWTO (R_M32R_HI16_SLO, /* type */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
203 false, /* pc_relative */
205 complain_overflow_dont, /* complain_on_overflow */
206 m32r_elf_hi16_reloc, /* special_function */
207 "R_M32R_HI16_SLO", /* name */
208 true, /* partial_inplace */
209 0x0000ffff, /* src_mask */
210 0x0000ffff, /* dst_mask */
211 false), /* pcrel_offset */
213 /* Lower 16 bits of address. */
214 HOWTO (R_M32R_LO16, /* type */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
218 false, /* pc_relative */
220 complain_overflow_dont, /* complain_on_overflow */
221 m32r_elf_lo16_reloc, /* special_function */
222 "R_M32R_LO16", /* name */
223 true, /* partial_inplace */
224 0x0000ffff, /* src_mask */
225 0x0000ffff, /* dst_mask */
226 false), /* pcrel_offset */
228 /* Small data area 16 bits offset. */
229 HOWTO (R_M32R_SDA16, /* type */
231 2, /* size (0 = byte, 1 = short, 2 = long) */
233 false, /* pc_relative */
235 complain_overflow_signed, /* complain_on_overflow */
236 m32r_elf_sda16_reloc, /* special_function */
237 "R_M32R_SDA16", /* name */
238 true, /* partial_inplace */ /* FIXME: correct? */
239 0x0000ffff, /* src_mask */
240 0x0000ffff, /* dst_mask */
241 false), /* pcrel_offset */
244 /* Handle the R_M32R_10_PCREL reloc. */
246 static bfd_reloc_status_type
247 m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
248 input_section, output_bfd, error_message)
250 arelent *reloc_entry;
253 asection *input_section;
255 char **error_message;
257 /* This part is from bfd_elf_generic_reloc. */
258 if (output_bfd != (bfd *) NULL
259 && (symbol->flags & BSF_SECTION_SYM) == 0
260 && (! reloc_entry->howto->partial_inplace
261 || reloc_entry->addend == 0))
263 reloc_entry->address += input_section->output_offset;
267 if (output_bfd != NULL)
269 /* FIXME: See bfd_perform_relocation. Is this right? */
270 return bfd_reloc_continue;
273 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
275 data, reloc_entry->address,
278 + symbol->section->output_section->vma
279 + symbol->section->output_offset),
280 reloc_entry->addend);
283 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
285 static bfd_reloc_status_type
286 m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
287 symbol_section, symbol_value, addend)
289 reloc_howto_type *howto;
290 asection *input_section;
293 asection *symbol_section;
294 bfd_vma symbol_value;
297 bfd_signed_vma relocation;
299 bfd_reloc_status_type status;
301 /* Sanity check the address (offset in section). */
302 if (offset > input_section->_cooked_size)
303 return bfd_reloc_outofrange;
305 relocation = symbol_value + addend;
306 /* Make it pc relative. */
307 relocation -= (input_section->output_section->vma
308 + input_section->output_offset);
309 /* These jumps mask off the lower two bits of the current address
310 before doing pcrel calculations. */
311 relocation -= (offset & -4L);
313 if (relocation < -0x200 || relocation > 0x1ff)
314 status = bfd_reloc_overflow;
316 status = bfd_reloc_ok;
318 x = bfd_get_16 (abfd, data + offset);
319 relocation >>= howto->rightshift;
320 relocation <<= howto->bitpos;
321 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
322 bfd_put_16 (abfd, x, data + offset);
327 /* Handle the R_M32R_HI16_[SU]LO relocs.
328 HI16_SLO is for the add3 and load/store with displacement instructions.
329 HI16_ULO is for the or3 instruction.
330 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
331 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
332 we must add one to the high 16 bytes (which will get subtracted off when
333 the low 16 bits are added).
334 These relocs have to be done in combination with an R_M32R_LO16 reloc
335 because there is a carry from the LO16 to the HI16. Here we just save
336 the information we need; we do the actual relocation when we see the LO16.
337 This code is copied from the elf32-mips.c. We also support an arbitrary
338 number of HI16 relocs to be associated with a single LO16 reloc. The
339 assembler sorts the relocs to ensure each HI16 immediately precedes its
340 LO16. However if there are multiple copies, the assembler may not find
341 the real LO16 so it picks the first one it finds. */
345 struct m32r_hi16 *next;
350 /* FIXME: This should not be a static variable. */
352 static struct m32r_hi16 *m32r_hi16_list;
354 static bfd_reloc_status_type
355 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
356 input_section, output_bfd, error_message)
358 arelent *reloc_entry;
361 asection *input_section;
363 char **error_message;
365 bfd_reloc_status_type ret;
369 /* This part is from bfd_elf_generic_reloc.
370 If we're relocating, and this an external symbol, we don't want
371 to change anything. */
372 if (output_bfd != (bfd *) NULL
373 && (symbol->flags & BSF_SECTION_SYM) == 0
374 && reloc_entry->addend == 0)
376 reloc_entry->address += input_section->output_offset;
380 /* Sanity check the address (offset in section). */
381 if (reloc_entry->address > input_section->_cooked_size)
382 return bfd_reloc_outofrange;
385 if (bfd_is_und_section (symbol->section)
386 && output_bfd == (bfd *) NULL)
387 ret = bfd_reloc_undefined;
389 if (bfd_is_com_section (symbol->section))
392 relocation = symbol->value;
394 relocation += symbol->section->output_section->vma;
395 relocation += symbol->section->output_offset;
396 relocation += reloc_entry->addend;
398 /* Save the information, and let LO16 do the actual relocation. */
399 n = (struct m32r_hi16 *) bfd_malloc (sizeof *n);
401 return bfd_reloc_outofrange;
402 n->addr = (bfd_byte *) data + reloc_entry->address;
403 n->addend = relocation;
404 n->next = m32r_hi16_list;
407 if (output_bfd != (bfd *) NULL)
408 reloc_entry->address += input_section->output_offset;
413 /* Handle an M32R ELF HI16 reloc. */
416 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
419 Elf_Internal_Rela *relhi;
420 Elf_Internal_Rela *rello;
427 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
429 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
430 if (type == R_M32R_HI16_SLO)
431 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
435 addend += ((insn & 0xffff) << 16) + addlo;
437 /* Reaccount for sign extension of low part. */
438 if (type == R_M32R_HI16_SLO
439 && (addend & 0x8000) != 0)
442 bfd_put_32 (input_bfd,
443 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
444 contents + relhi->r_offset);
447 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
448 inplace relocation; this function exists in order to do the
449 R_M32R_HI16_[SU]LO relocation described above. */
451 bfd_reloc_status_type
452 m32r_elf_lo16_reloc (abfd, reloc_entry, symbol, data,
453 input_section, output_bfd, error_message)
455 arelent *reloc_entry;
458 asection *input_section;
460 char **error_message;
462 if (m32r_hi16_list != NULL)
472 struct m32r_hi16 *next;
474 /* Do the HI16 relocation. Note that we actually don't need
475 to know anything about the LO16 itself, except where to
476 find the low 16 bits of the addend needed by the LO16. */
477 insn = bfd_get_32 (abfd, l->addr);
478 vallo = ((bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
479 & 0xffff) ^ 0x8000) - 0x8000;
480 val = ((insn & 0xffff) << 16) + vallo;
483 /* Reaccount for sign extension of low part. */
484 if ((val & 0x8000) != 0)
487 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
488 bfd_put_32 (abfd, insn, l->addr);
495 m32r_hi16_list = NULL;
498 /* Now do the LO16 reloc in the usual way. */
499 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
500 input_section, output_bfd, error_message);
503 /* Handle the R_M32R_SDA16 reloc.
504 This reloc is used to compute the address of objects in the small data area
505 and to perform loads and stores from that area.
506 The lower 16 bits are sign extended and added to the register specified
507 in the instruction, which is assumed to point to _SDA_BASE_. */
509 static bfd_reloc_status_type
510 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
511 input_section, output_bfd, error_message)
513 arelent *reloc_entry;
516 asection *input_section;
518 char **error_message;
522 reloc_howto_type *howto;
524 /* This part is from bfd_elf_generic_reloc. */
525 if (output_bfd != (bfd *) NULL
526 && (symbol->flags & BSF_SECTION_SYM) == 0
527 && (! reloc_entry->howto->partial_inplace
528 || reloc_entry->addend == 0))
530 reloc_entry->address += input_section->output_offset;
534 if (output_bfd != NULL)
536 /* FIXME: See bfd_perform_relocation. Is this right? */
537 return bfd_reloc_continue;
540 /* FIXME: not sure what to do here yet. But then again, the linker
541 may never call us. */
545 /* Map BFD reloc types to M32R ELF reloc types. */
547 struct m32r_reloc_map
549 unsigned char bfd_reloc_val;
550 unsigned char elf_reloc_val;
553 static const struct m32r_reloc_map m32r_reloc_map[] =
555 { BFD_RELOC_NONE, R_M32R_NONE },
556 { BFD_RELOC_16, R_M32R_16 },
557 { BFD_RELOC_32, R_M32R_32 },
558 { BFD_RELOC_M32R_24, R_M32R_24 },
559 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
560 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
561 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
562 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
563 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
564 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
565 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
568 static reloc_howto_type *
569 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
571 bfd_reloc_code_real_type code;
576 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
579 if (m32r_reloc_map[i].bfd_reloc_val == code)
580 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
586 /* Set the howto pointer for an M32R ELF reloc. */
589 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
592 Elf32_Internal_Rel *dst;
596 r_type = ELF32_R_TYPE (dst->r_info);
597 BFD_ASSERT (r_type < (unsigned int) R_M32R_max);
598 cache_ptr->howto = &m32r_elf_howto_table[r_type];
601 /* Given a BFD section, try to locate the corresponding ELF section
605 _bfd_m32r_elf_section_from_bfd_section (abfd, hdr, sec, retval)
607 Elf32_Internal_Shdr *hdr;
611 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
613 *retval = SHN_M32R_SCOMMON;
619 /* M32R ELF uses two common sections. One is the usual one, and the other
620 is for small objects. All the small objects are kept together, and then
621 referenced via one register, which yields faster assembler code. It is
622 up to the compiler to emit an instruction to load the register with
623 _SDA_BASE. This is what we use for the small common section. This
624 approach is copied from elf32-mips.c. */
625 static asection m32r_elf_scom_section;
626 static asymbol m32r_elf_scom_symbol;
627 static asymbol *m32r_elf_scom_symbol_ptr;
629 /* Handle the special M32R section numbers that a symbol may use. */
632 _bfd_m32r_elf_symbol_processing (abfd, asym)
636 elf_symbol_type *elfsym;
638 elfsym = (elf_symbol_type *) asym;
640 switch (elfsym->internal_elf_sym.st_shndx)
642 case SHN_M32R_SCOMMON:
643 if (m32r_elf_scom_section.name == NULL)
645 /* Initialize the small common section. */
646 m32r_elf_scom_section.name = ".scommon";
647 m32r_elf_scom_section.flags = SEC_IS_COMMON;
648 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
649 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
650 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
651 m32r_elf_scom_symbol.name = ".scommon";
652 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
653 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
654 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
656 asym->section = &m32r_elf_scom_section;
657 asym->value = elfsym->internal_elf_sym.st_size;
662 /* Hook called by the linker routine which adds symbols from an object
663 file. We must handle the special M32R section numbers here.
664 We also keep watching for whether we need to create the sdata special
668 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
670 struct bfd_link_info *info;
671 const Elf_Internal_Sym *sym;
677 if (! info->relocateable
678 && (*namep)[0] == '_' && (*namep)[1] == 'S'
679 && strcmp (*namep, "_SDA_BASE_") == 0)
681 /* This is simpler than using _bfd_elf_create_linker_section
682 (our needs are simpler than ppc's needs). Also
683 _bfd_elf_create_linker_section currently has a bug where if a .sdata
684 section already exists a new one is created that follows it which
685 screws of _SDA_BASE_ address calcs because output_offset != 0. */
686 struct elf_link_hash_entry *h;
687 asection *s = bfd_get_section_by_name (abfd, ".sdata");
689 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
693 int flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
694 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
696 s = bfd_make_section_anyway (abfd, ".sdata");
699 bfd_set_section_flags (abfd, s, flags);
700 bfd_set_section_alignment (abfd, s, 2);
703 h = (struct elf_link_hash_entry *)
704 bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, false);
706 if ((h == NULL || h->root.type == bfd_link_hash_undefined)
707 && !(_bfd_generic_link_add_one_symbol (info,
715 get_elf_backend_data (abfd)->collect,
716 (struct bfd_link_hash_entry **) &h)))
718 h->type = STT_OBJECT;
721 switch (sym->st_shndx)
723 case SHN_M32R_SCOMMON:
724 *secp = bfd_make_section_old_way (abfd, ".scommon");
725 (*secp)->flags |= SEC_IS_COMMON;
726 *valp = sym->st_size;
733 /* We have to figure out the SDA_BASE value, so that we can adjust the
734 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
735 BFD. If we can't find it, we're stuck. We cache it in the ELF
736 target data. We don't need to adjust the symbol value for an
737 external symbol if we are producing relocateable output. */
739 static bfd_reloc_status_type
740 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
742 struct bfd_link_info *info;
743 const char **error_message;
746 if (elf_gp (output_bfd) == 0)
748 struct bfd_link_hash_entry *h;
750 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
751 if (h != (struct bfd_link_hash_entry *) NULL
752 && h->type == bfd_link_hash_defined)
753 elf_gp (output_bfd) = (h->u.def.value
754 + h->u.def.section->output_section->vma
755 + h->u.def.section->output_offset);
758 /* Only get the error once. */
759 *psb = elf_gp (output_bfd) = 4;
761 (const char *) "SDA relocation when _SDA_BASE_ not defined";
762 return bfd_reloc_dangerous;
765 *psb = elf_gp (output_bfd);
769 /* Relocate an M32R/D ELF section.
770 There is some attempt to make this function usable for many architectures,
771 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
772 if only to serve as a learning tool.
774 The RELOCATE_SECTION function is called by the new ELF backend linker
775 to handle the relocations for a section.
777 The relocs are always passed as Rela structures; if the section
778 actually uses Rel structures, the r_addend field will always be
781 This function is responsible for adjust the section contents as
782 necessary, and (if using Rela relocs and generating a
783 relocateable output file) adjusting the reloc addend as
786 This function does not have to worry about setting the reloc
787 address or the reloc symbol index.
789 LOCAL_SYMS is a pointer to the swapped in local symbols.
791 LOCAL_SECTIONS is an array giving the section in the input file
792 corresponding to the st_shndx field of each local symbol.
794 The global hash table entry for the global symbols can be found
795 via elf_sym_hashes (input_bfd).
797 When generating relocateable output, this function must handle
798 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
799 going to be the section symbol corresponding to the output
800 section, which means that the addend must be adjusted
804 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
805 contents, relocs, local_syms, local_sections)
807 struct bfd_link_info *info;
809 asection *input_section;
811 Elf_Internal_Rela *relocs;
812 Elf_Internal_Sym *local_syms;
813 asection **local_sections;
815 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
816 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
817 Elf_Internal_Rela *rel, *relend;
818 bfd *dynobj = elf_hash_table (info)->dynobj;
819 /* Assume success. */
823 relend = relocs + input_section->reloc_count;
824 for (; rel < relend; rel++)
827 reloc_howto_type *howto;
828 unsigned long r_symndx;
829 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
830 ensure it's zero (we use REL relocs, not RELA). Therefore this
831 should be assigning zero to `addend', but for clarity we use
833 bfd_vma addend = rel->r_addend;
834 bfd_vma offset = rel->r_offset;
835 struct elf_link_hash_entry *h;
836 Elf_Internal_Sym *sym;
838 const char *sym_name;
839 bfd_reloc_status_type r;
840 const char *errmsg = NULL;
842 r_type = ELF32_R_TYPE (rel->r_info);
843 if (r_type < 0 || r_type >= (int) R_M32R_max)
845 (*_bfd_error_handler) ("%s: unknown relocation type %d",
846 bfd_get_filename (input_bfd),
848 bfd_set_error (bfd_error_bad_value);
853 howto = m32r_elf_howto_table + r_type;
854 r_symndx = ELF32_R_SYM (rel->r_info);
856 if (info->relocateable)
858 /* This is a relocateable link. We don't have to change
859 anything, unless the reloc is against a section symbol,
860 in which case we have to adjust according to where the
861 section symbol winds up in the output section. */
863 if (r_symndx >= symtab_hdr->sh_info)
865 /* External symbol. */
870 sym = local_syms + r_symndx;
871 sym_name = "<local symbol>";
872 /* STT_SECTION: symbol is associated with a section. */
873 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
875 /* Symbol isn't associated with a section. Nothing to do. */
879 sec = local_sections[r_symndx];
880 addend += sec->output_offset + sym->st_value;
882 /* This can't be done for USE_REL because it doesn't mean anything
883 and elf_link_input_bfd asserts this stays zero. */
884 rel->r_addend = addend;
888 /* Addends are stored with relocs. We're done. */
891 /* If partial_inplace, we need to store any additional addend
892 back in the section. */
893 if (! howto->partial_inplace)
895 /* ??? Here is a nice place to call a special_function
897 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
898 r = _bfd_relocate_contents (howto, input_bfd,
899 addend, contents + offset);
902 Elf_Internal_Rela *lorel;
904 /* We allow an arbitrary number of HI16 relocs before the
905 LO16 reloc. This permits gcc to emit the HI and LO relocs
907 for (lorel = rel + 1;
909 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
910 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
914 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
916 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
921 r = _bfd_relocate_contents (howto, input_bfd,
922 addend, contents + offset);
930 /* This is a final link. */
935 if (r_symndx < symtab_hdr->sh_info)
938 sym = local_syms + r_symndx;
939 sec = local_sections[r_symndx];
940 sym_name = "<local symbol>";
941 relocation = (sec->output_section->vma
947 /* External symbol. */
948 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
949 while (h->root.type == bfd_link_hash_indirect
950 || h->root.type == bfd_link_hash_warning)
951 h = (struct elf_link_hash_entry *) h->root.u.i.link;
952 sym_name = h->root.root.string;
954 if (h->root.type == bfd_link_hash_defined
955 || h->root.type == bfd_link_hash_defweak)
957 sec = h->root.u.def.section;
958 if (sec->output_section == NULL)
961 relocation = (h->root.u.def.value
962 + sec->output_section->vma
963 + sec->output_offset);
965 else if (h->root.type == bfd_link_hash_undefweak)
969 if (! ((*info->callbacks->undefined_symbol)
970 (info, h->root.root.string, input_bfd,
971 input_section, offset)))
977 /* Sanity check the address. */
978 if (offset > input_section->_raw_size)
980 r = bfd_reloc_outofrange;
984 switch ((int) r_type)
986 case (int) R_M32R_10_PCREL :
987 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
989 sec, relocation, addend);
992 case (int) R_M32R_HI16_SLO :
993 case (int) R_M32R_HI16_ULO :
995 Elf_Internal_Rela *lorel;
997 /* We allow an arbitrary number of HI16 relocs before the
998 LO16 reloc. This permits gcc to emit the HI and LO relocs
1000 for (lorel = rel + 1;
1002 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
1003 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
1007 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
1009 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
1010 contents, relocation + addend);
1014 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1016 relocation, addend);
1020 case (int) R_M32R_SDA16 :
1024 BFD_ASSERT (sec != NULL);
1025 name = bfd_get_section_name (abfd, sec);
1027 if (strcmp (name, ".sdata") == 0
1028 || strcmp (name, ".sbss") == 0
1029 || strcmp (name, ".scommon") == 0)
1032 bfd *out_bfd = sec->output_section->owner;
1034 r = m32r_elf_final_sda_base (out_bfd, info,
1037 if (r != bfd_reloc_ok)
1043 /* At this point `relocation' contains the object's
1045 relocation -= sda_base;
1046 /* Now it contains the offset from _SDA_BASE_. */
1050 (*_bfd_error_handler) ("%s: The target (%s) of an %s relocation is in the wrong section (%s)",
1051 bfd_get_filename (input_bfd),
1053 m32r_elf_howto_table[(int) r_type].name,
1054 bfd_get_section_name (abfd, sec));
1055 /*bfd_set_error (bfd_error_bad_value); ??? why? */
1063 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1065 relocation, addend);
1072 if (r != bfd_reloc_ok)
1074 /* FIXME: This should be generic enough to go in a utility. */
1078 name = h->root.root.string;
1081 name = (bfd_elf_string_from_elf_section
1082 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1083 if (name == NULL || *name == '\0')
1084 name = bfd_section_name (input_bfd, sec);
1092 case bfd_reloc_overflow:
1093 if (! ((*info->callbacks->reloc_overflow)
1094 (info, name, howto->name, (bfd_vma) 0,
1095 input_bfd, input_section, offset)))
1099 case bfd_reloc_undefined:
1100 if (! ((*info->callbacks->undefined_symbol)
1101 (info, name, input_bfd, input_section,
1106 case bfd_reloc_outofrange:
1107 errmsg = "internal error: out of range error";
1110 case bfd_reloc_notsupported:
1111 errmsg = "internal error: unsupported relocation error";
1114 case bfd_reloc_dangerous:
1115 errmsg = "internal error: dangerous error";
1119 errmsg = "internal error: unknown error";
1123 if (!((*info->callbacks->warning)
1124 (info, errmsg, name, input_bfd, input_section,
1135 #if 0 /* relaxing not supported yet */
1137 /* This function handles relaxing for the m32r.
1138 Relaxing on the m32r is tricky because of instruction alignment
1139 requirements (4 byte instructions must be aligned on 4 byte boundaries).
1141 The following relaxing opportunities are handled:
1143 seth/add3/jl -> bl24 or bl8
1146 It would be nice to handle bl24 -> bl8 but given:
1148 - 4 byte insns must be on 4 byte boundaries
1149 - branch instructions only branch to insns on 4 byte boundaries
1151 this isn't much of a win because the insn in the 2 "deleted" bytes
1152 must become a nop. With some complexity some real relaxation could be
1153 done but the frequency just wouldn't make it worth it; it's better to
1154 try to do all the code compaction one can elsewhere.
1155 When the chip supports parallel 16 bit insns, things may change.
1159 m32r_elf_relax_section (abfd, sec, link_info, again)
1162 struct bfd_link_info *link_info;
1165 Elf_Internal_Shdr *symtab_hdr;
1166 /* The Rela structures are used here because that's what
1167 _bfd_elf32_link_read_relocs uses [for convenience - it sets the addend
1169 Elf_Internal_Rela *internal_relocs;
1170 Elf_Internal_Rela *free_relocs = NULL;
1171 Elf_Internal_Rela *irel, *irelend;
1172 bfd_byte *contents = NULL;
1173 bfd_byte *free_contents = NULL;
1174 Elf32_External_Sym *extsyms = NULL;
1175 Elf32_External_Sym *free_extsyms = NULL;
1177 /* Assume nothing changes. */
1180 /* We don't have to do anything for a relocateable link, if
1181 this section does not have relocs, or if this is not a
1183 if (link_info->relocateable
1184 || (sec->flags & SEC_RELOC) == 0
1185 || sec->reloc_count == 0
1186 || (sec->flags & SEC_CODE) == 0
1187 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
1190 /* If this is the first time we have been called for this section,
1191 initialize the cooked size. */
1192 if (sec->_cooked_size == 0)
1193 sec->_cooked_size = sec->_raw_size;
1195 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1197 /* Get a copy of the native relocations. */
1198 internal_relocs = (_bfd_elf32_link_read_relocs
1199 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1200 link_info->keep_memory));
1201 if (internal_relocs == NULL)
1203 if (! link_info->keep_memory)
1204 free_relocs = internal_relocs;
1206 /* Walk through them looking for relaxing opportunities. */
1207 irelend = internal_relocs + sec->reloc_count;
1208 for (irel = internal_relocs; irel < irelend; irel++)
1212 /* If this isn't something that can be relaxed, then ignore
1214 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
1217 /* Get the section contents if we haven't done so already. */
1218 if (contents == NULL)
1220 /* Get cached copy if it exists. */
1221 if (elf_section_data (sec)->this_hdr.contents != NULL)
1222 contents = elf_section_data (sec)->this_hdr.contents;
1225 /* Go get them off disk. */
1226 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1227 if (contents == NULL)
1229 free_contents = contents;
1231 if (! bfd_get_section_contents (abfd, sec, contents,
1232 (file_ptr) 0, sec->_raw_size))
1237 /* Read this BFD's symbols if we haven't done so already. */
1238 if (extsyms == NULL)
1240 /* Get cached copy if it exists. */
1241 if (symtab_hdr->contents != NULL)
1242 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1245 /* Go get them off disk. */
1246 extsyms = ((Elf32_External_Sym *)
1247 bfd_malloc (symtab_hdr->sh_size));
1248 if (extsyms == NULL)
1250 free_extsyms = extsyms;
1251 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1252 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1253 != symtab_hdr->sh_size))
1258 /* Get the value of the symbol referred to by the reloc. */
1259 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1261 Elf_Internal_Sym isym;
1264 /* A local symbol. */
1265 bfd_elf32_swap_symbol_in (abfd,
1266 extsyms + ELF32_R_SYM (irel->r_info),
1269 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1270 symval = (isym.st_value
1271 + sym_sec->output_section->vma
1272 + sym_sec->output_offset);
1277 struct elf_link_hash_entry *h;
1279 /* An external symbol. */
1280 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1281 h = elf_sym_hashes (abfd)[indx];
1282 BFD_ASSERT (h != NULL);
1283 if (h->root.type != bfd_link_hash_defined
1284 && h->root.type != bfd_link_hash_defweak)
1286 /* This appears to be a reference to an undefined
1287 symbol. Just ignore it--it will be caught by the
1288 regular reloc processing. */
1292 symval = (h->root.u.def.value
1293 + h->root.u.def.section->output_section->vma
1294 + h->root.u.def.section->output_offset);
1297 /* For simplicity of coding, we are going to modify the section
1298 contents, the section relocs, and the BFD symbol table. We
1299 must tell the rest of the code not to free up this
1300 information. It would be possible to instead create a table
1301 of changes which have to be made, as is done in coff-mips.c;
1302 that would be more work, but would require less memory when
1303 the linker is run. */
1305 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
1306 This sequence is generated by the compiler when compiling in
1307 32 bit mode. Also look for seth/add3 -> ld24. */
1309 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
1311 Elf_Internal_Rela *nrel;
1312 bfd_vma pc = (sec->output_section->vma + sec->output_offset
1314 bfd_signed_vma pcrel_value = symval - pc;
1315 unsigned int code,reg;
1316 int addend,nop_p,bl8_p,to_delete;
1318 /* The tests are ordered so that we get out as quickly as possible
1319 if this isn't something we can relax, taking into account that
1320 we are looking for two separate possibilities (jl/ld24). */
1322 /* Do nothing if no room in the section for this to be what we're
1324 if (irel->r_offset > sec->_cooked_size - 8)
1327 /* Make sure the next relocation applies to the next
1328 instruction and that it's the add3's reloc. */
1331 || irel->r_offset + 4 != nrel->r_offset
1332 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
1335 /* See if the instructions are seth/add3. */
1336 /* FIXME: This is where macros from cgen can come in. */
1337 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
1338 if ((code & 0xf0ff) != 0xd0c0)
1339 continue; /* not seth rN,foo */
1340 reg = (code & 0x0f00) >> 8;
1341 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
1342 if (code != (0x80a0 | reg | (reg << 8)))
1343 continue; /* not add3 rN,rN,foo */
1345 /* At this point we've confirmed we have seth/add3. Now check
1346 whether the next insn is a jl, in which case try to change this
1349 /* Ensure the branch target is in range.
1350 The bl24 instruction has a 24 bit operand which is the target
1351 address right shifted by 2, giving a signed range of 26 bits.
1352 Note that 4 bytes are added to the high value because the target
1353 will be at least 4 bytes closer if we can relax. It'll actually
1354 be 4 or 8 bytes closer, but we don't know which just yet and
1355 the difference isn't significant enough to worry about. */
1356 #ifndef USE_REL /* put in for learning purposes */
1357 pcrel_value += irel->r_addend;
1359 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
1360 pcrel_value += addend;
1363 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
1364 /* Do nothing if no room in the section for this to be what we're
1366 && (irel->r_offset <= sec->_cooked_size - 12)
1367 /* Ensure the next insn is "jl rN". */
1368 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
1369 code != (0x1ec0 | reg)))
1371 /* We can relax to bl24/bl8. */
1373 /* See if there's a nop following the jl.
1374 Also see if we can use a bl8 insn. */
1375 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
1376 nop_p = (code & 0x7fff) == 7000;
1377 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
1381 /* Change "seth rN,foo" to "bl8 foo || nop".
1382 We OR in CODE just in case it's not a nop (technically,
1383 CODE currently must be a nop, but for cleanness we
1384 allow it to be anything). */
1385 #ifndef USE_REL /* put in for learning purposes */
1386 code = 0x7e000000 | code;
1388 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | code;
1394 /* Change the seth rN,foo to a bl24 foo. */
1395 #ifndef USE_REL /* put in for learning purposes */
1398 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
1400 to_delete = nop_p ? 8 : 4;
1403 bfd_put_32 (abfd, code, contents + irel->r_offset);
1405 /* Set the new reloc type. */
1406 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1407 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
1409 /* Delete the add3 reloc by making it a null reloc. */
1410 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1413 else if (addend >= 0
1414 && symval + addend <= 0xffffff)
1416 /* We can relax to ld24. */
1418 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
1419 bfd_put_32 (abfd, code, contents + irel->r_offset);
1421 /* Tell the following code a nop filler isn't needed. */
1426 /* Can't do anything here. */
1430 /* Note that we've changed the relocs, section contents, etc. */
1431 elf_section_data (sec)->relocs = internal_relocs;
1434 elf_section_data (sec)->this_hdr.contents = contents;
1435 free_contents = NULL;
1437 symtab_hdr->contents = (bfd_byte *) extsyms;
1438 free_extsyms = NULL;
1440 /* Delete TO_DELETE bytes of data. */
1441 if (!m32r_elf_relax_delete_bytes (abfd, sec,
1442 irel->r_offset + 4, to_delete))
1445 /* Now that the following bytes have been moved into place, see if
1446 we need to replace the jl with a nop. This happens when we had
1447 to use a bl24 insn and the insn following the jl isn't a nop.
1448 Technically, this situation can't happen (since the insn can
1449 never be executed) but to be clean we do this. When the chip
1450 supports parallel 16 bit insns things may change.
1451 We don't need to do this in the case of relaxing to ld24,
1452 and the above code sets nop_p so this isn't done. */
1453 if (! nop_p && to_delete == 4)
1454 bfd_put_16 (abfd, 0x7000, contents + irel->r_offset + 4);
1456 /* That will change things, so we should relax again.
1457 Note that this is not required, and it may be slow. */
1463 /* loop to try the next reloc */
1466 if (free_relocs != NULL)
1472 if (free_contents != NULL)
1474 if (! link_info->keep_memory)
1475 free (free_contents);
1478 /* Cache the section contents for elf_link_input_bfd. */
1479 elf_section_data (sec)->this_hdr.contents = contents;
1481 free_contents = NULL;
1484 if (free_extsyms != NULL)
1486 if (! link_info->keep_memory)
1487 free (free_extsyms);
1490 /* Cache the symbols for elf_link_input_bfd. */
1491 symtab_hdr->contents = extsyms;
1493 free_extsyms = NULL;
1499 if (free_relocs != NULL)
1501 if (free_contents != NULL)
1502 free (free_contents);
1503 if (free_extsyms != NULL)
1504 free (free_extsyms);
1508 /* Delete some bytes from a section while relaxing. */
1511 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
1517 Elf_Internal_Shdr *symtab_hdr;
1518 Elf32_External_Sym *extsyms;
1521 Elf_Internal_Rela *irel, *irelend;
1522 Elf_Internal_Rela *irelalign;
1524 Elf32_External_Sym *esym, *esymend;
1525 struct elf_link_hash_entry *sym_hash;
1527 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1528 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1530 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1532 contents = elf_section_data (sec)->this_hdr.contents;
1534 /* The deletion must stop at the next ALIGN reloc for an aligment
1535 power larger than the number of bytes we are deleting. */
1538 toaddr = sec->_cooked_size;
1540 irel = elf_section_data (sec)->relocs;
1541 irelend = irel + sec->reloc_count;
1543 /* Actually delete the bytes. */
1544 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1545 sec->_cooked_size -= count;
1547 /* Adjust all the relocs. */
1548 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1550 /* Get the new reloc address. */
1551 if ((irel->r_offset > addr
1552 && irel->r_offset < toaddr))
1553 irel->r_offset -= count;
1556 /* Adjust the local symbols defined in this section. */
1558 esymend = esym + symtab_hdr->sh_info;
1559 for (; esym < esymend; esym++)
1561 Elf_Internal_Sym isym;
1563 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1565 if (isym.st_shndx == shndx
1566 && isym.st_value > addr
1567 && isym.st_value < toaddr)
1569 isym.st_value -= count;
1570 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1574 /* Now adjust the global symbols defined in this section. */
1575 esym = extsyms + symtab_hdr->sh_info;
1576 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1577 for (index = 0; esym < esymend; esym++, index++)
1579 Elf_Internal_Sym isym;
1581 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1582 sym_hash = elf_sym_hashes (abfd)[index];
1583 if (isym.st_shndx == shndx
1584 && ((sym_hash)->root.type == bfd_link_hash_defined
1585 || (sym_hash)->root.type == bfd_link_hash_defweak)
1586 && (sym_hash)->root.u.def.section == sec
1587 && (sym_hash)->root.u.def.value > addr
1588 && (sym_hash)->root.u.def.value < toaddr)
1590 (sym_hash)->root.u.def.value -= count;
1597 /* This is a version of bfd_generic_get_relocated_section_contents
1598 which uses m32r_elf_relocate_section. */
1601 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1602 data, relocateable, symbols)
1604 struct bfd_link_info *link_info;
1605 struct bfd_link_order *link_order;
1607 boolean relocateable;
1610 Elf_Internal_Shdr *symtab_hdr;
1611 asection *input_section = link_order->u.indirect.section;
1612 bfd *input_bfd = input_section->owner;
1613 asection **sections = NULL;
1614 Elf_Internal_Rela *internal_relocs = NULL;
1615 Elf32_External_Sym *external_syms = NULL;
1616 Elf_Internal_Sym *internal_syms = NULL;
1618 /* We only need to handle the case of relaxing, or of having a
1619 particular set of section contents, specially. */
1621 || elf_section_data (input_section)->this_hdr.contents == NULL)
1622 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1627 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1629 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1630 input_section->_raw_size);
1632 if ((input_section->flags & SEC_RELOC) != 0
1633 && input_section->reloc_count > 0)
1635 Elf_Internal_Sym *isymp;
1637 Elf32_External_Sym *esym, *esymend;
1639 if (symtab_hdr->contents != NULL)
1640 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1643 external_syms = ((Elf32_External_Sym *)
1644 bfd_malloc (symtab_hdr->sh_info
1645 * sizeof (Elf32_External_Sym)));
1646 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1648 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1649 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1650 symtab_hdr->sh_info, input_bfd)
1651 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1655 internal_relocs = (_bfd_elf32_link_read_relocs
1656 (input_bfd, input_section, (PTR) NULL,
1657 (Elf_Internal_Rela *) NULL, false));
1658 if (internal_relocs == NULL)
1661 internal_syms = ((Elf_Internal_Sym *)
1662 bfd_malloc (symtab_hdr->sh_info
1663 * sizeof (Elf_Internal_Sym)));
1664 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1667 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1668 * sizeof (asection *));
1669 if (sections == NULL && symtab_hdr->sh_info > 0)
1672 isymp = internal_syms;
1674 esym = external_syms;
1675 esymend = esym + symtab_hdr->sh_info;
1676 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1680 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1682 if (isymp->st_shndx == SHN_UNDEF)
1683 isec = bfd_und_section_ptr;
1684 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1685 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1686 else if (isymp->st_shndx == SHN_ABS)
1687 isec = bfd_abs_section_ptr;
1688 else if (isymp->st_shndx == SHN_COMMON)
1689 isec = bfd_com_section_ptr;
1690 else if (isymp->st_shndx == SHN_M32R_SCOMMON)
1691 isec = &m32r_elf_scom_section;
1701 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
1702 input_section, data, internal_relocs,
1703 internal_syms, sections))
1706 if (sections != NULL)
1709 if (internal_syms != NULL)
1710 free (internal_syms);
1711 internal_syms = NULL;
1712 if (external_syms != NULL && symtab_hdr->contents == NULL)
1713 free (external_syms);
1714 external_syms = NULL;
1715 if (internal_relocs != elf_section_data (input_section)->relocs)
1716 free (internal_relocs);
1717 internal_relocs = NULL;
1723 if (internal_relocs != NULL
1724 && internal_relocs != elf_section_data (input_section)->relocs)
1725 free (internal_relocs);
1726 if (external_syms != NULL && symtab_hdr->contents == NULL)
1727 free (external_syms);
1728 if (internal_syms != NULL)
1729 free (internal_syms);
1730 if (sections != NULL)
1737 /* Set the right machine number. */
1739 m32r_elf_object_p (abfd)
1742 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
1745 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
1746 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
1750 /* Store the machine number in the flags field. */
1752 m32r_elf_final_write_processing (abfd, linker)
1758 switch (bfd_get_mach (abfd))
1761 case bfd_mach_m32r: val = E_M32R_ARCH; break;
1762 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
1765 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
1766 elf_elfheader (abfd)->e_flags |= val;
1769 /* Function to keep M32R specific file flags. */
1771 m32r_elf_set_private_flags (abfd, flags)
1775 BFD_ASSERT (!elf_flags_init (abfd)
1776 || elf_elfheader (abfd)->e_flags == flags);
1778 elf_elfheader (abfd)->e_flags = flags;
1779 elf_flags_init (abfd) = true;
1783 /* Copy backend specific data from one object module to another */
1785 m32r_elf_copy_private_bfd_data (ibfd, obfd)
1789 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1790 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1793 BFD_ASSERT (!elf_flags_init (obfd)
1794 || (elf_elfheader (obfd)->e_flags
1795 == elf_elfheader (ibfd)->e_flags));
1797 elf_gp (obfd) = elf_gp (ibfd);
1798 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1799 elf_flags_init (obfd) = true;
1803 /* Merge backend specific data from an object file to the output
1804 object file when linking. */
1806 m32r_elf_merge_private_bfd_data (ibfd, obfd)
1813 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1814 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1817 new_flags = elf_elfheader (ibfd)->e_flags;
1818 old_flags = elf_elfheader (obfd)->e_flags;
1820 if (! elf_flags_init (obfd))
1822 elf_flags_init (obfd) = true;
1823 elf_elfheader (obfd)->e_flags = new_flags;
1825 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1826 && bfd_get_arch_info (obfd)->the_default)
1828 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
1834 /* Check flag compatibility. */
1835 if (new_flags == old_flags)
1838 if ((new_flags & EF_M32R_ARCH) != (old_flags & EF_M32R_ARCH))
1840 if ((new_flags & EF_M32R_ARCH) == E_M32RX_ARCH)
1842 _bfd_error_handler ("%s: Instruction set mismatch with previous modules",
1843 bfd_get_filename (ibfd));
1845 bfd_set_error (bfd_error_bad_value);
1851 _bfd_error_handler ("%s: Merging M32R instructions with M32RX instructions",
1852 bfd_get_filename (ibfd));
1859 /* Display the flags field */
1861 m32r_elf_print_private_bfd_data (abfd, ptr)
1865 FILE * file = (FILE *) ptr;
1867 BFD_ASSERT (abfd != NULL && ptr != NULL)
1869 fprintf (file, "private flags = %x", elf_elfheader (abfd)->e_flags);
1871 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
1874 case E_M32R_ARCH: fprintf (file, ": m32r instructions"); break;
1875 case E_M32RX_ARCH: fprintf (file, ": m32rx instructions"); break;
1885 #define ELF_ARCH bfd_arch_m32r
1886 #define ELF_MACHINE_CODE EM_CYGNUS_M32R
1887 #define ELF_MAXPAGESIZE 0x1000
1889 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
1890 #define TARGET_BIG_NAME "elf32-m32r"
1892 #define elf_info_to_howto 0
1893 #define elf_info_to_howto_rel m32r_info_to_howto_rel
1894 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
1895 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
1896 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
1897 #define elf_backend_relocate_section m32r_elf_relocate_section
1901 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
1902 #define bfd_elf32_bfd_get_relocated_section_contents \
1903 m32r_elf_get_relocated_section_contents
1906 #define elf_backend_object_p m32r_elf_object_p
1907 #define elf_backend_final_write_processing m32r_elf_final_write_processing
1908 #define bfd_elf32_bfd_copy_private_bfd_data m32r_elf_copy_private_bfd_data
1909 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
1910 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
1911 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
1913 #include "elf32-target.h"