2004-06-01 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / elf32-m32r.c
blob21d37b176cee443298fb39c5e512229486915882
1 /* M32R-specific support for 32-bit ELF.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/m32r.h"
27 static bfd_reloc_status_type m32r_elf_10_pcrel_reloc
28 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
29 static bfd_reloc_status_type m32r_elf_do_10_pcrel_reloc
30 PARAMS ((bfd *, reloc_howto_type *, asection *,
31 bfd_byte *, bfd_vma, asection *, bfd_vma, bfd_vma));
32 static bfd_reloc_status_type m32r_elf_hi16_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static void m32r_elf_relocate_hi16
35 PARAMS ((bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *,
36 bfd_byte *, bfd_vma));
37 bfd_reloc_status_type m32r_elf_lo16_reloc
38 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39 bfd_reloc_status_type m32r_elf_generic_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41 static bfd_reloc_status_type m32r_elf_sda16_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
44 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
45 static void m32r_info_to_howto_rel
46 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
47 static void m32r_info_to_howto
48 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
49 bfd_boolean _bfd_m32r_elf_section_from_bfd_section
50 PARAMS ((bfd *, asection *, int *));
51 void _bfd_m32r_elf_symbol_processing
52 PARAMS ((bfd *, asymbol *));
53 static bfd_boolean m32r_elf_add_symbol_hook
54 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
55 const char **, flagword *, asection **, bfd_vma *));
56 static bfd_boolean m32r_elf_relocate_section
57 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
58 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
59 #if 0 /* not yet */
60 static bfd_boolean m32r_elf_relax_delete_bytes
61 PARAMS ((bfd *, asection *, bfd_vma, int));
62 #endif
63 static bfd_reloc_status_type m32r_elf_final_sda_base
64 PARAMS ((bfd *, struct bfd_link_info *, const char **, bfd_vma *));
65 static bfd_boolean m32r_elf_object_p
66 PARAMS ((bfd *));
67 static void m32r_elf_final_write_processing
68 PARAMS ((bfd *, bfd_boolean));
69 static bfd_boolean m32r_elf_set_private_flags
70 PARAMS ((bfd *, flagword));
71 static bfd_boolean m32r_elf_merge_private_bfd_data
72 PARAMS ((bfd *, bfd *));
73 static bfd_boolean m32r_elf_print_private_bfd_data
74 PARAMS ((bfd *, PTR));
75 static bfd_boolean m32r_elf_gc_sweep_hook
76 PARAMS ((bfd *, struct bfd_link_info *, asection *,
77 const Elf_Internal_Rela *));
78 static bfd_boolean m32r_elf_check_relocs
79 PARAMS ((bfd *, struct bfd_link_info *, asection *,
80 const Elf_Internal_Rela *));
82 static bfd_boolean m32r_elf_adjust_dynamic_symbol
83 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
84 static bfd_boolean m32r_elf_size_dynamic_sections
85 PARAMS ((bfd *, struct bfd_link_info *));
87 asection * m32r_elf_gc_mark_hook
88 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
89 struct elf_link_hash_entry *, Elf_Internal_Sym *));
91 static bfd_boolean m32r_elf_create_dynamic_sections
92 PARAMS ((bfd *, struct bfd_link_info *));
94 static bfd_boolean m32r_elf_finish_dynamic_sections
95 PARAMS ((bfd *, struct bfd_link_info *));
97 static bfd_boolean m32r_elf_finish_dynamic_symbol
98 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
99 Elf_Internal_Sym *));
101 static bfd_boolean allocate_dynrelocs
102 PARAMS ((struct elf_link_hash_entry *, PTR));
103 static bfd_boolean readonly_dynrelocs
104 PARAMS ((struct elf_link_hash_entry *, PTR));
105 static enum elf_reloc_type_class m32r_elf_reloc_type_class
106 PARAMS ((const Elf_Internal_Rela *));
107 static bfd_boolean m32r_elf_fake_sections
108 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
110 #define NOP_INSN 0x7000
111 #define MAKE_PARALLEL(insn) ((insn) | 0x8000)
113 /* Use REL instead of RELA to save space.
114 This only saves space in libraries and object files, but perhaps
115 relocs will be put in ROM? All in all though, REL relocs are a pain
116 to work with. */
117 /* #define USE_REL 1
119 #ifndef USE_REL
120 #define USE_REL 0
121 #endif */
122 /* Use RELA. But use REL to link old objects for backwords compatibility. */
124 /* Functions for the M32R ELF linker. */
126 /* The name of the dynamic interpreter. This is put in the .interp
127 section. */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
131 /* The nop opcode we use. */
133 #define M32R_NOP 0x7000f000
135 #define PLT_EMPTY 0x10101010 /* RIE -> RIE */
137 /* The size in bytes of an entry in the procedure linkage table. */
139 #define PLT_ENTRY_SIZE 20
140 #define PLT_HEADER_SIZE 20
142 /* The first one entries in a procedure linkage table are reserved,
143 and the initial contents are unimportant (we zero them out).
144 Subsequent entries look like this. */
146 #define PLT0_ENTRY_WORD0 0xd6c00000 /* seth r6, #high(.got+4) */
147 #define PLT0_ENTRY_WORD1 0x86e60000 /* or3 r6, r6, #low(.got)+4) */
148 #define PLT0_ENTRY_WORD2 0x24e626c6 /* ld r4, @r6+ -> ld r6, @r6 */
149 #define PLT0_ENTRY_WORD3 0x1fc6f000 /* jmp r6 || pnop */
150 #define PLT0_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
152 #define PLT0_PIC_ENTRY_WORD0 0xa4cc0004 /* ld r4, @(4,r12) */
153 #define PLT0_PIC_ENTRY_WORD1 0xa6cc0008 /* ld r6, @(8,r12) */
154 #define PLT0_PIC_ENTRY_WORD2 0x1fc6f000 /* jmp r6 || nop */
155 #define PLT0_PIC_ENTRY_WORD3 PLT_EMPTY /* RIE -> RIE */
156 #define PLT0_PIC_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
158 #define PLT_ENTRY_WORD0 0xe6000000 /* ld24 r6, .name_in_GOT */
159 #define PLT_ENTRY_WORD1 0x06acf000 /* add r6, r12 || nop */
160 #define PLT_ENTRY_WORD0b 0xd6c00000 /* seth r6, #high(.name_in_GOT) */
161 #define PLT_ENTRY_WORD1b 0x86e60000 /* or3 r6, r6, #low(.name_in_GOT) */
162 #define PLT_ENTRY_WORD2 0x26c61fc6 /* ld r6, @r6 -> jmp r6 */
163 #define PLT_ENTRY_WORD3 0xe5000000 /* ld24 r5, $offset */
164 #define PLT_ENTRY_WORD4 0xff000000 /* bra .plt0. */
167 static reloc_howto_type m32r_elf_howto_table[] =
169 /* This reloc does nothing. */
170 HOWTO (R_M32R_NONE, /* type */
171 0, /* rightshift */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
173 32, /* bitsize */
174 FALSE, /* pc_relative */
175 0, /* bitpos */
176 complain_overflow_bitfield, /* complain_on_overflow */
177 bfd_elf_generic_reloc, /* special_function */
178 "R_M32R_NONE", /* name */
179 FALSE, /* partial_inplace */
180 0, /* src_mask */
181 0, /* dst_mask */
182 FALSE), /* pcrel_offset */
184 /* A 16 bit absolute relocation. */
185 HOWTO (R_M32R_16, /* type */
186 0, /* rightshift */
187 1, /* size (0 = byte, 1 = short, 2 = long) */
188 16, /* bitsize */
189 FALSE, /* pc_relative */
190 0, /* bitpos */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 m32r_elf_generic_reloc,/* special_function */
193 "R_M32R_16", /* name */
194 TRUE, /* partial_inplace */
195 0xffff, /* src_mask */
196 0xffff, /* dst_mask */
197 FALSE), /* pcrel_offset */
199 /* A 32 bit absolute relocation. */
200 HOWTO (R_M32R_32, /* type */
201 0, /* rightshift */
202 2, /* size (0 = byte, 1 = short, 2 = long) */
203 32, /* bitsize */
204 FALSE, /* pc_relative */
205 0, /* bitpos */
206 complain_overflow_bitfield, /* complain_on_overflow */
207 m32r_elf_generic_reloc,/* special_function */
208 "R_M32R_32", /* name */
209 TRUE, /* partial_inplace */
210 0xffffffff, /* src_mask */
211 0xffffffff, /* dst_mask */
212 FALSE), /* pcrel_offset */
214 /* A 24 bit address. */
215 HOWTO (R_M32R_24, /* type */
216 0, /* rightshift */
217 2, /* size (0 = byte, 1 = short, 2 = long) */
218 24, /* bitsize */
219 FALSE, /* pc_relative */
220 0, /* bitpos */
221 complain_overflow_unsigned, /* complain_on_overflow */
222 m32r_elf_generic_reloc,/* special_function */
223 "R_M32R_24", /* name */
224 TRUE, /* partial_inplace */
225 0xffffff, /* src_mask */
226 0xffffff, /* dst_mask */
227 FALSE), /* pcrel_offset */
229 /* An PC Relative 10-bit relocation, shifted by 2.
230 This reloc is complicated because relocations are relative to pc & -4.
231 i.e. branches in the right insn slot use the address of the left insn
232 slot for pc. */
233 /* ??? It's not clear whether this should have partial_inplace set or not.
234 Branch relaxing in the assembler can store the addend in the insn,
235 and if bfd_install_relocation gets called the addend may get added
236 again. */
237 HOWTO (R_M32R_10_PCREL, /* type */
238 2, /* rightshift */
239 1, /* size (0 = byte, 1 = short, 2 = long) */
240 10, /* bitsize */
241 TRUE, /* pc_relative */
242 0, /* bitpos */
243 complain_overflow_signed, /* complain_on_overflow */
244 m32r_elf_10_pcrel_reloc, /* special_function */
245 "R_M32R_10_PCREL", /* name */
246 FALSE, /* partial_inplace */
247 0xff, /* src_mask */
248 0xff, /* dst_mask */
249 TRUE), /* pcrel_offset */
251 /* A relative 18 bit relocation, right shifted by 2. */
252 HOWTO (R_M32R_18_PCREL, /* type */
253 2, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 16, /* bitsize */
256 TRUE, /* pc_relative */
257 0, /* bitpos */
258 complain_overflow_signed, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_M32R_18_PCREL", /* name */
261 FALSE, /* partial_inplace */
262 0xffff, /* src_mask */
263 0xffff, /* dst_mask */
264 TRUE), /* pcrel_offset */
266 /* A relative 26 bit relocation, right shifted by 2. */
267 /* ??? It's not clear whether this should have partial_inplace set or not.
268 Branch relaxing in the assembler can store the addend in the insn,
269 and if bfd_install_relocation gets called the addend may get added
270 again. */
271 HOWTO (R_M32R_26_PCREL, /* type */
272 2, /* rightshift */
273 2, /* size (0 = byte, 1 = short, 2 = long) */
274 26, /* bitsize */
275 TRUE, /* pc_relative */
276 0, /* bitpos */
277 complain_overflow_signed, /* complain_on_overflow */
278 bfd_elf_generic_reloc, /* special_function */
279 "R_M32R_26_PCREL", /* name */
280 FALSE, /* partial_inplace */
281 0xffffff, /* src_mask */
282 0xffffff, /* dst_mask */
283 TRUE), /* pcrel_offset */
285 /* High 16 bits of address when lower 16 is or'd in. */
286 HOWTO (R_M32R_HI16_ULO, /* type */
287 16, /* rightshift */
288 2, /* size (0 = byte, 1 = short, 2 = long) */
289 16, /* bitsize */
290 FALSE, /* pc_relative */
291 0, /* bitpos */
292 complain_overflow_dont, /* complain_on_overflow */
293 m32r_elf_hi16_reloc, /* special_function */
294 "R_M32R_HI16_ULO", /* name */
295 TRUE, /* partial_inplace */
296 0x0000ffff, /* src_mask */
297 0x0000ffff, /* dst_mask */
298 FALSE), /* pcrel_offset */
300 /* High 16 bits of address when lower 16 is added in. */
301 HOWTO (R_M32R_HI16_SLO, /* type */
302 16, /* rightshift */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
304 16, /* bitsize */
305 FALSE, /* pc_relative */
306 0, /* bitpos */
307 complain_overflow_dont, /* complain_on_overflow */
308 m32r_elf_hi16_reloc, /* special_function */
309 "R_M32R_HI16_SLO", /* name */
310 TRUE, /* partial_inplace */
311 0x0000ffff, /* src_mask */
312 0x0000ffff, /* dst_mask */
313 FALSE), /* pcrel_offset */
315 /* Lower 16 bits of address. */
316 HOWTO (R_M32R_LO16, /* type */
317 0, /* rightshift */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
319 16, /* bitsize */
320 FALSE, /* pc_relative */
321 0, /* bitpos */
322 complain_overflow_dont, /* complain_on_overflow */
323 m32r_elf_lo16_reloc, /* special_function */
324 "R_M32R_LO16", /* name */
325 TRUE, /* partial_inplace */
326 0x0000ffff, /* src_mask */
327 0x0000ffff, /* dst_mask */
328 FALSE), /* pcrel_offset */
330 /* Small data area 16 bits offset. */
331 HOWTO (R_M32R_SDA16, /* type */
332 0, /* rightshift */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 FALSE, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_signed, /* complain_on_overflow */
338 m32r_elf_sda16_reloc, /* special_function */
339 "R_M32R_SDA16", /* name */
340 TRUE, /* partial_inplace */ /* FIXME: correct? */
341 0x0000ffff, /* src_mask */
342 0x0000ffff, /* dst_mask */
343 FALSE), /* pcrel_offset */
345 /* GNU extension to record C++ vtable hierarchy */
346 HOWTO (R_M32R_GNU_VTINHERIT, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 0, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_dont, /* complain_on_overflow */
353 NULL, /* special_function */
354 "R_M32R_GNU_VTINHERIT", /* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0, /* dst_mask */
358 FALSE), /* pcrel_offset */
360 /* GNU extension to record C++ vtable member usage */
361 HOWTO (R_M32R_GNU_VTENTRY, /* type */
362 0, /* rightshift */
363 2, /* size (0 = byte, 1 = short, 2 = long) */
364 0, /* bitsize */
365 FALSE, /* pc_relative */
366 0, /* bitpos */
367 complain_overflow_dont, /* complain_on_overflow */
368 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
369 "R_M32R_GNU_VTENTRY", /* name */
370 FALSE, /* partial_inplace */
371 0, /* src_mask */
372 0, /* dst_mask */
373 FALSE), /* pcrel_offset */
375 EMPTY_HOWTO (13),
376 EMPTY_HOWTO (14),
377 EMPTY_HOWTO (15),
378 EMPTY_HOWTO (16),
379 EMPTY_HOWTO (17),
380 EMPTY_HOWTO (18),
381 EMPTY_HOWTO (19),
382 EMPTY_HOWTO (20),
383 EMPTY_HOWTO (21),
384 EMPTY_HOWTO (22),
385 EMPTY_HOWTO (23),
386 EMPTY_HOWTO (24),
387 EMPTY_HOWTO (25),
388 EMPTY_HOWTO (26),
389 EMPTY_HOWTO (27),
390 EMPTY_HOWTO (28),
391 EMPTY_HOWTO (29),
392 EMPTY_HOWTO (30),
393 EMPTY_HOWTO (31),
394 EMPTY_HOWTO (32),
396 /* A 16 bit absolute relocation. */
397 HOWTO (R_M32R_16_RELA, /* type */
398 0, /* rightshift */
399 1, /* size (0 = byte, 1 = short, 2 = long) */
400 16, /* bitsize */
401 FALSE, /* pc_relative */
402 0, /* bitpos */
403 complain_overflow_bitfield, /* complain_on_overflow */
404 bfd_elf_generic_reloc, /* special_function */
405 "R_M32R_16_RELA", /* name */
406 FALSE, /* partial_inplace */
407 0xffff, /* src_mask */
408 0xffff, /* dst_mask */
409 FALSE), /* pcrel_offset */
411 /* A 32 bit absolute relocation. */
412 HOWTO (R_M32R_32_RELA, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 32, /* bitsize */
416 FALSE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_bitfield, /* complain_on_overflow */
419 bfd_elf_generic_reloc,/* special_function */
420 "R_M32R_32_RELA", /* name */
421 FALSE, /* partial_inplace */
422 0xffffffff, /* src_mask */
423 0xffffffff, /* dst_mask */
424 FALSE), /* pcrel_offset */
426 /* A 24 bit address. */
427 HOWTO (R_M32R_24_RELA, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 24, /* bitsize */
431 FALSE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_unsigned, /* complain_on_overflow */
434 bfd_elf_generic_reloc,/* special_function */
435 "R_M32R_24_RELA", /* name */
436 FALSE, /* partial_inplace */
437 0xffffff, /* src_mask */
438 0xffffff, /* dst_mask */
439 FALSE), /* pcrel_offset */
441 HOWTO (R_M32R_10_PCREL_RELA, /* type */
442 2, /* rightshift */
443 1, /* size (0 = byte, 1 = short, 2 = long) */
444 10, /* bitsize */
445 TRUE, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_signed, /* complain_on_overflow */
448 m32r_elf_10_pcrel_reloc, /* special_function */
449 "R_M32R_10_PCREL_RELA",/* name */
450 FALSE, /* partial_inplace */
451 0xff, /* src_mask */
452 0xff, /* dst_mask */
453 TRUE), /* pcrel_offset */
455 /* A relative 18 bit relocation, right shifted by 2. */
456 HOWTO (R_M32R_18_PCREL_RELA, /* type */
457 2, /* rightshift */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
459 16, /* bitsize */
460 TRUE, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_signed, /* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_M32R_18_PCREL_RELA",/* name */
465 FALSE, /* partial_inplace */
466 0xffff, /* src_mask */
467 0xffff, /* dst_mask */
468 TRUE), /* pcrel_offset */
470 /* A relative 26 bit relocation, right shifted by 2. */
471 HOWTO (R_M32R_26_PCREL_RELA, /* type */
472 2, /* rightshift */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
474 26, /* bitsize */
475 TRUE, /* pc_relative */
476 0, /* bitpos */
477 complain_overflow_signed, /* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_M32R_26_PCREL_RELA",/* name */
480 FALSE, /* partial_inplace */
481 0xffffff, /* src_mask */
482 0xffffff, /* dst_mask */
483 TRUE), /* pcrel_offset */
485 /* High 16 bits of address when lower 16 is or'd in. */
486 HOWTO (R_M32R_HI16_ULO_RELA, /* type */
487 16, /* rightshift */
488 2, /* size (0 = byte, 1 = short, 2 = long) */
489 16, /* bitsize */
490 FALSE, /* pc_relative */
491 0, /* bitpos */
492 complain_overflow_dont, /* complain_on_overflow */
493 bfd_elf_generic_reloc, /* special_function */
494 "R_M32R_HI16_ULO_RELA",/* name */
495 FALSE, /* partial_inplace */
496 0x0000ffff, /* src_mask */
497 0x0000ffff, /* dst_mask */
498 FALSE), /* pcrel_offset */
500 /* High 16 bits of address when lower 16 is added in. */
501 HOWTO (R_M32R_HI16_SLO_RELA, /* type */
502 16, /* rightshift */
503 2, /* size (0 = byte, 1 = short, 2 = long) */
504 16, /* bitsize */
505 FALSE, /* pc_relative */
506 0, /* bitpos */
507 complain_overflow_dont, /* complain_on_overflow */
508 bfd_elf_generic_reloc, /* special_function */
509 "R_M32R_HI16_SLO_RELA",/* name */
510 FALSE, /* partial_inplace */
511 0x0000ffff, /* src_mask */
512 0x0000ffff, /* dst_mask */
513 FALSE), /* pcrel_offset */
515 /* Lower 16 bits of address. */
516 HOWTO (R_M32R_LO16_RELA, /* type */
517 0, /* rightshift */
518 2, /* size (0 = byte, 1 = short, 2 = long) */
519 16, /* bitsize */
520 FALSE, /* pc_relative */
521 0, /* bitpos */
522 complain_overflow_dont, /* complain_on_overflow */
523 bfd_elf_generic_reloc, /* special_function */
524 "R_M32R_LO16_RELA", /* name */
525 FALSE, /* partial_inplace */
526 0x0000ffff, /* src_mask */
527 0x0000ffff, /* dst_mask */
528 FALSE), /* pcrel_offset */
530 /* Small data area 16 bits offset. */
531 HOWTO (R_M32R_SDA16_RELA, /* type */
532 0, /* rightshift */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
534 16, /* bitsize */
535 FALSE, /* pc_relative */
536 0, /* bitpos */
537 complain_overflow_signed, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_M32R_SDA16_RELA", /* name */
540 TRUE, /* partial_inplace */ /* FIXME: correct? */
541 0x0000ffff, /* src_mask */
542 0x0000ffff, /* dst_mask */
543 FALSE), /* pcrel_offset */
545 /* GNU extension to record C++ vtable hierarchy */
546 HOWTO (R_M32R_RELA_GNU_VTINHERIT, /* type */
547 0, /* rightshift */
548 2, /* size (0 = byte, 1 = short, 2 = long) */
549 0, /* bitsize */
550 FALSE, /* pc_relative */
551 0, /* bitpos */
552 complain_overflow_dont, /* complain_on_overflow */
553 NULL, /* special_function */
554 "R_M32R_RELA_GNU_VTINHERIT", /* name */
555 FALSE, /* partial_inplace */
556 0, /* src_mask */
557 0, /* dst_mask */
558 FALSE), /* pcrel_offset */
560 /* GNU extension to record C++ vtable member usage */
561 HOWTO (R_M32R_RELA_GNU_VTENTRY, /* type */
562 0, /* rightshift */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
564 0, /* bitsize */
565 FALSE, /* pc_relative */
566 0, /* bitpos */
567 complain_overflow_dont, /* complain_on_overflow */
568 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
569 "R_M32R_RELA_GNU_VTENTRY", /* name */
570 FALSE, /* partial_inplace */
571 0, /* src_mask */
572 0, /* dst_mask */
573 FALSE), /* pcrel_offset */
575 EMPTY_HOWTO (45),
576 EMPTY_HOWTO (46),
577 EMPTY_HOWTO (47),
579 /* Like R_M32R_24, but referring to the GOT table entry for
580 the symbol. */
581 HOWTO (R_M32R_GOT24, /* type */
582 0, /* rightshift */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
584 24, /* bitsize */
585 FALSE, /* pc_relative */
586 0, /* bitpos */
587 complain_overflow_unsigned, /* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_M32R_GOT24", /* name */
590 FALSE, /* partial_inplace */
591 0xffffff, /* src_mask */
592 0xffffff, /* dst_mask */
593 FALSE), /* pcrel_offset */
595 /* Like R_M32R_PCREL, but referring to the procedure linkage table
596 entry for the symbol. */
597 HOWTO (R_M32R_26_PLTREL, /* type */
598 2, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 24, /* bitsize */
601 TRUE, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_signed, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_M32R_26_PLTREL", /* name */
606 FALSE, /* partial_inplace */
607 0xffffff, /* src_mask */
608 0xffffff, /* dst_mask */
609 TRUE), /* pcrel_offset */
611 /* This is used only by the dynamic linker. The symbol should exist
612 both in the object being run and in some shared library. The
613 dynamic linker copies the data addressed by the symbol from the
614 shared library into the object, because the object being
615 run has to have the data at some particular address. */
616 HOWTO (R_M32R_COPY, /* type */
617 0, /* rightshift */
618 2, /* size (0 = byte, 1 = short, 2 = long) */
619 32, /* bitsize */
620 FALSE, /* pc_relative */
621 0, /* bitpos */
622 complain_overflow_bitfield, /* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_M32R_COPY", /* name */
625 FALSE, /* partial_inplace */
626 0xffffffff, /* src_mask */
627 0xffffffff, /* dst_mask */
628 FALSE), /* pcrel_offset */
630 /* Like R_M32R_24, but used when setting global offset table
631 entries. */
632 HOWTO (R_M32R_GLOB_DAT, /* type */
633 0, /* rightshift */
634 2, /* size (0 = byte, 1 = short, 2 = long) */
635 32, /* bitsize */
636 FALSE, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_bitfield, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_M32R_GLOB_DAT", /* name */
641 FALSE, /* partial_inplace */
642 0xffffffff, /* src_mask */
643 0xffffffff, /* dst_mask */
644 FALSE), /* pcrel_offset */
646 /* Marks a procedure linkage table entry for a symbol. */
647 HOWTO (R_M32R_JMP_SLOT, /* type */
648 0, /* rightshift */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
650 32, /* bitsize */
651 FALSE, /* pc_relative */
652 0, /* bitpos */
653 complain_overflow_bitfield, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_M32R_JMP_SLOT", /* name */
656 FALSE, /* partial_inplace */
657 0xffffffff, /* src_mask */
658 0xffffffff, /* dst_mask */
659 FALSE), /* pcrel_offset */
661 /* Used only by the dynamic linker. When the object is run, this
662 longword is set to the load address of the object, plus the
663 addend. */
664 HOWTO (R_M32R_RELATIVE, /* type */
665 0, /* rightshift */
666 2, /* size (0 = byte, 1 = short, 2 = long) */
667 32, /* bitsize */
668 FALSE, /* pc_relative */
669 0, /* bitpos */
670 complain_overflow_bitfield, /* complain_on_overflow */
671 bfd_elf_generic_reloc, /* special_function */
672 "R_M32R_RELATIVE", /* name */
673 FALSE, /* partial_inplace */
674 0xffffffff, /* src_mask */
675 0xffffffff, /* dst_mask */
676 FALSE), /* pcrel_offset */
678 HOWTO (R_M32R_GOTOFF, /* type */
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 32, /* bitsize */
682 FALSE, /* pc_relative */
683 0, /* bitpos */
684 complain_overflow_bitfield, /* complain_on_overflow */
685 bfd_elf_generic_reloc, /* special_function */
686 "R_M32R_GOTOFF", /* name */
687 FALSE, /* partial_inplace */
688 0xffffffff, /* src_mask */
689 0xffffffff, /* dst_mask */
690 FALSE), /* pcrel_offset */
692 /* An PC Relative 24-bit relocation used when setting PIC offset
693 table register. */
694 HOWTO (R_M32R_GOTPC24, /* type */
695 0, /* rightshift */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
697 24, /* bitsize */
698 TRUE, /* pc_relative */
699 0, /* bitpos */
700 complain_overflow_unsigned, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_M32R_GOTPC24", /* name */
703 FALSE, /* partial_inplace */
704 0xffffff, /* src_mask */
705 0xffffff, /* dst_mask */
706 TRUE), /* pcrel_offset */
708 /* Like R_M32R_HI16_ULO, but referring to the GOT table entry for
709 the symbol. */
710 HOWTO (R_M32R_GOT16_HI_ULO, /* type */
711 16, /* rightshift */
712 2, /* size (0 = byte, 1 = short, 2 = long) */
713 16, /* bitsize */
714 FALSE, /* pc_relative */
715 0, /* bitpos */
716 complain_overflow_dont, /* complain_on_overflow */
717 bfd_elf_generic_reloc, /* special_function */
718 "R_M32R_GOT16_HI_ULO", /* name */
719 FALSE, /* partial_inplace */
720 0x0000ffff, /* src_mask */
721 0x0000ffff, /* dst_mask */
722 FALSE), /* pcrel_offset */
724 /* Like R_M32R_HI16_SLO, but referring to the GOT table entry for
725 the symbol. */
726 HOWTO (R_M32R_GOT16_HI_SLO, /* type */
727 16, /* rightshift */
728 2, /* size (0 = byte, 1 = short, 2 = long) */
729 16, /* bitsize */
730 FALSE, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_dont, /* complain_on_overflow */
733 bfd_elf_generic_reloc, /* special_function */
734 "R_M32R_GOT16_HI_SLO", /* name */
735 FALSE, /* partial_inplace */
736 0x0000ffff, /* src_mask */
737 0x0000ffff, /* dst_mask */
738 FALSE), /* pcrel_offset */
740 /* Like R_M32R_LO16, but referring to the GOT table entry for
741 the symbol. */
742 HOWTO (R_M32R_GOT16_LO, /* type */
743 0, /* rightshift */
744 2, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
746 FALSE, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_dont, /* complain_on_overflow */
749 bfd_elf_generic_reloc, /* special_function */
750 "R_M32R_GOT16_LO", /* name */
751 FALSE, /* partial_inplace */
752 0x0000ffff, /* src_mask */
753 0x0000ffff, /* dst_mask */
754 FALSE), /* pcrel_offset */
756 /* An PC Relative relocation used when setting PIC offset table register.
757 Like R_M32R_HI16_ULO, but referring to the GOT table entry for
758 the symbol. */
759 HOWTO (R_M32R_GOTPC_HI_ULO, /* type */
760 16, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 16, /* bitsize */
763 FALSE, /* pc_relative */
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
767 "R_M32R_GOTPC_HI_ULO", /* name */
768 FALSE, /* partial_inplace */
769 0x0000ffff, /* src_mask */
770 0x0000ffff, /* dst_mask */
771 TRUE), /* pcrel_offset */
773 /* An PC Relative relocation used when setting PIC offset table register.
774 Like R_M32R_HI16_SLO, but referring to the GOT table entry for
775 the symbol. */
776 HOWTO (R_M32R_GOTPC_HI_SLO, /* type */
777 16, /* rightshift */
778 2, /* size (0 = byte, 1 = short, 2 = long) */
779 16, /* bitsize */
780 FALSE, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_M32R_GOTPC_HI_SLO", /* name */
785 FALSE, /* partial_inplace */
786 0x0000ffff, /* src_mask */
787 0x0000ffff, /* dst_mask */
788 TRUE), /* pcrel_offset */
790 /* An PC Relative relocation used when setting PIC offset table register.
791 Like R_M32R_LO16, but referring to the GOT table entry for
792 the symbol. */
793 HOWTO (R_M32R_GOTPC_LO, /* type */
794 0, /* rightshift */
795 2, /* size (0 = byte, 1 = short, 2 = long) */
796 16, /* bitsize */
797 FALSE, /* pc_relative */
798 0, /* bitpos */
799 complain_overflow_dont, /* complain_on_overflow */
800 bfd_elf_generic_reloc, /* special_function */
801 "R_M32R_GOTPC_LO", /* name */
802 FALSE, /* partial_inplace */
803 0x0000ffff, /* src_mask */
804 0x0000ffff, /* dst_mask */
805 TRUE), /* pcrel_offset */
808 /* Handle the R_M32R_10_PCREL reloc. */
810 static bfd_reloc_status_type
811 m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
812 input_section, output_bfd, error_message)
813 bfd * abfd;
814 arelent * reloc_entry;
815 asymbol * symbol;
816 PTR data;
817 asection * input_section;
818 bfd * output_bfd;
819 char ** error_message ATTRIBUTE_UNUSED;
821 /* This part is from bfd_elf_generic_reloc. */
822 if (output_bfd != (bfd *) NULL
823 && (symbol->flags & BSF_SECTION_SYM) == 0
824 && (! reloc_entry->howto->partial_inplace
825 || reloc_entry->addend == 0))
827 reloc_entry->address += input_section->output_offset;
828 return bfd_reloc_ok;
831 if (output_bfd != NULL)
833 /* FIXME: See bfd_perform_relocation. Is this right? */
834 return bfd_reloc_continue;
837 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
838 input_section,
839 data, reloc_entry->address,
840 symbol->section,
841 (symbol->value
842 + symbol->section->output_section->vma
843 + symbol->section->output_offset),
844 reloc_entry->addend);
847 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
849 static bfd_reloc_status_type
850 m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
851 symbol_section, symbol_value, addend)
852 bfd *abfd;
853 reloc_howto_type *howto;
854 asection *input_section;
855 bfd_byte *data;
856 bfd_vma offset;
857 asection *symbol_section ATTRIBUTE_UNUSED;
858 bfd_vma symbol_value;
859 bfd_vma addend;
861 bfd_signed_vma relocation;
862 unsigned long x;
863 bfd_reloc_status_type status;
865 /* Sanity check the address (offset in section). */
866 if (offset > input_section->_cooked_size)
867 return bfd_reloc_outofrange;
869 relocation = symbol_value + addend;
870 /* Make it pc relative. */
871 relocation -= (input_section->output_section->vma
872 + input_section->output_offset);
873 /* These jumps mask off the lower two bits of the current address
874 before doing pcrel calculations. */
875 relocation -= (offset & -(bfd_vma) 4);
877 if (relocation < -0x200 || relocation > 0x1ff)
878 status = bfd_reloc_overflow;
879 else
880 status = bfd_reloc_ok;
882 x = bfd_get_16 (abfd, data + offset);
883 relocation >>= howto->rightshift;
884 relocation <<= howto->bitpos;
885 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
886 bfd_put_16 (abfd, (bfd_vma) x, data + offset);
888 return status;
891 /* Handle the R_M32R_HI16_[SU]LO relocs.
892 HI16_SLO is for the add3 and load/store with displacement instructions.
893 HI16_ULO is for the or3 instruction.
894 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
895 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
896 we must add one to the high 16 bytes (which will get subtracted off when
897 the low 16 bits are added).
898 These relocs have to be done in combination with an R_M32R_LO16 reloc
899 because there is a carry from the LO16 to the HI16. Here we just save
900 the information we need; we do the actual relocation when we see the LO16.
901 This code is copied from the elf32-mips.c. We also support an arbitrary
902 number of HI16 relocs to be associated with a single LO16 reloc. The
903 assembler sorts the relocs to ensure each HI16 immediately precedes its
904 LO16. However if there are multiple copies, the assembler may not find
905 the real LO16 so it picks the first one it finds. */
907 struct m32r_hi16
909 struct m32r_hi16 *next;
910 bfd_byte *addr;
911 bfd_vma addend;
914 /* FIXME: This should not be a static variable. */
916 static struct m32r_hi16 *m32r_hi16_list;
918 static bfd_reloc_status_type
919 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
920 input_section, output_bfd, error_message)
921 bfd *abfd ATTRIBUTE_UNUSED;
922 arelent *reloc_entry;
923 asymbol *symbol;
924 PTR data;
925 asection *input_section;
926 bfd *output_bfd;
927 char **error_message ATTRIBUTE_UNUSED;
929 bfd_reloc_status_type ret;
930 bfd_vma relocation;
931 struct m32r_hi16 *n;
933 /* This part is from bfd_elf_generic_reloc.
934 If we're relocating, and this an external symbol, we don't want
935 to change anything. */
936 if (output_bfd != (bfd *) NULL
937 && (symbol->flags & BSF_SECTION_SYM) == 0
938 && reloc_entry->addend == 0)
940 reloc_entry->address += input_section->output_offset;
941 return bfd_reloc_ok;
944 /* Sanity check the address (offset in section). */
945 if (reloc_entry->address > input_section->_cooked_size)
946 return bfd_reloc_outofrange;
948 ret = bfd_reloc_ok;
949 if (bfd_is_und_section (symbol->section)
950 && output_bfd == (bfd *) NULL)
951 ret = bfd_reloc_undefined;
953 if (bfd_is_com_section (symbol->section))
954 relocation = 0;
955 else
956 relocation = symbol->value;
958 relocation += symbol->section->output_section->vma;
959 relocation += symbol->section->output_offset;
960 relocation += reloc_entry->addend;
962 /* Save the information, and let LO16 do the actual relocation. */
963 n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
964 if (n == NULL)
965 return bfd_reloc_outofrange;
966 n->addr = (bfd_byte *) data + reloc_entry->address;
967 n->addend = relocation;
968 n->next = m32r_hi16_list;
969 m32r_hi16_list = n;
971 if (output_bfd != (bfd *) NULL)
972 reloc_entry->address += input_section->output_offset;
974 return ret;
977 /* Handle an M32R ELF HI16 reloc. */
979 static void
980 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
981 bfd *input_bfd;
982 int type;
983 Elf_Internal_Rela *relhi;
984 Elf_Internal_Rela *rello;
985 bfd_byte *contents;
986 bfd_vma addend;
988 unsigned long insn;
989 bfd_vma addlo;
991 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
993 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
994 if (type == R_M32R_HI16_SLO)
995 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
996 else
997 addlo &= 0xffff;
999 addend += ((insn & 0xffff) << 16) + addlo;
1001 /* Reaccount for sign extension of low part. */
1002 if (type == R_M32R_HI16_SLO
1003 && (addend & 0x8000) != 0)
1004 addend += 0x10000;
1006 bfd_put_32 (input_bfd,
1007 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
1008 contents + relhi->r_offset);
1011 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
1012 inplace relocation; this function exists in order to do the
1013 R_M32R_HI16_[SU]LO relocation described above. */
1015 bfd_reloc_status_type
1016 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
1017 input_section, output_bfd, error_message)
1018 bfd *input_bfd;
1019 arelent *reloc_entry;
1020 asymbol *symbol;
1021 PTR data;
1022 asection *input_section;
1023 bfd *output_bfd;
1024 char **error_message;
1026 /* This part is from bfd_elf_generic_reloc.
1027 If we're relocating, and this an external symbol, we don't want
1028 to change anything. */
1029 if (output_bfd != (bfd *) NULL
1030 && (symbol->flags & BSF_SECTION_SYM) == 0
1031 && reloc_entry->addend == 0)
1033 reloc_entry->address += input_section->output_offset;
1034 return bfd_reloc_ok;
1037 if (m32r_hi16_list != NULL)
1039 struct m32r_hi16 *l;
1041 l = m32r_hi16_list;
1042 while (l != NULL)
1044 unsigned long insn;
1045 unsigned long val;
1046 unsigned long vallo;
1047 struct m32r_hi16 *next;
1049 /* Do the HI16 relocation. Note that we actually don't need
1050 to know anything about the LO16 itself, except where to
1051 find the low 16 bits of the addend needed by the LO16. */
1052 insn = bfd_get_32 (input_bfd, l->addr);
1053 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
1054 & 0xffff) ^ 0x8000) - 0x8000;
1055 val = ((insn & 0xffff) << 16) + vallo;
1056 val += l->addend;
1058 /* Reaccount for sign extension of low part. */
1059 if ((val & 0x8000) != 0)
1060 val += 0x10000;
1062 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1063 bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
1065 next = l->next;
1066 free (l);
1067 l = next;
1070 m32r_hi16_list = NULL;
1073 /* Now do the LO16 reloc in the usual way.
1074 ??? It would be nice to call bfd_elf_generic_reloc here,
1075 but we have partial_inplace set. bfd_elf_generic_reloc will
1076 pass the handling back to bfd_install_relocation which will install
1077 a section relative addend which is wrong. */
1078 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1079 input_section, output_bfd, error_message);
1082 /* Do generic partial_inplace relocation.
1083 This is a local replacement for bfd_elf_generic_reloc. */
1085 bfd_reloc_status_type
1086 m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1087 input_section, output_bfd, error_message)
1088 bfd *input_bfd;
1089 arelent *reloc_entry;
1090 asymbol *symbol;
1091 PTR data;
1092 asection *input_section;
1093 bfd *output_bfd;
1094 char **error_message ATTRIBUTE_UNUSED;
1096 bfd_reloc_status_type ret;
1097 bfd_vma relocation;
1098 bfd_byte *inplace_address;
1100 /* This part is from bfd_elf_generic_reloc.
1101 If we're relocating, and this an external symbol, we don't want
1102 to change anything. */
1103 if (output_bfd != (bfd *) NULL
1104 && (symbol->flags & BSF_SECTION_SYM) == 0
1105 && reloc_entry->addend == 0)
1107 reloc_entry->address += input_section->output_offset;
1108 return bfd_reloc_ok;
1111 /* Now do the reloc in the usual way.
1112 ??? It would be nice to call bfd_elf_generic_reloc here,
1113 but we have partial_inplace set. bfd_elf_generic_reloc will
1114 pass the handling back to bfd_install_relocation which will install
1115 a section relative addend which is wrong. */
1117 /* Sanity check the address (offset in section). */
1118 if (reloc_entry->address > input_section->_cooked_size)
1119 return bfd_reloc_outofrange;
1121 ret = bfd_reloc_ok;
1122 if (bfd_is_und_section (symbol->section)
1123 && output_bfd == (bfd *) NULL)
1124 ret = bfd_reloc_undefined;
1126 if (bfd_is_com_section (symbol->section)
1127 || output_bfd != (bfd *) NULL)
1128 relocation = 0;
1129 else
1130 relocation = symbol->value;
1132 /* Only do this for a final link. */
1133 if (output_bfd == (bfd *) NULL)
1135 relocation += symbol->section->output_section->vma;
1136 relocation += symbol->section->output_offset;
1139 relocation += reloc_entry->addend;
1140 inplace_address = (bfd_byte *) data + reloc_entry->address;
1142 #define DOIT(x) \
1143 x = ( (x & ~reloc_entry->howto->dst_mask) | \
1144 (((x & reloc_entry->howto->src_mask) + relocation) & \
1145 reloc_entry->howto->dst_mask))
1147 switch (reloc_entry->howto->size)
1149 case 1:
1151 short x = bfd_get_16 (input_bfd, inplace_address);
1152 DOIT (x);
1153 bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
1155 break;
1156 case 2:
1158 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1159 DOIT (x);
1160 bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
1162 break;
1163 default:
1164 BFD_ASSERT (0);
1167 if (output_bfd != (bfd *) NULL)
1168 reloc_entry->address += input_section->output_offset;
1170 return ret;
1173 /* Handle the R_M32R_SDA16 reloc.
1174 This reloc is used to compute the address of objects in the small data area
1175 and to perform loads and stores from that area.
1176 The lower 16 bits are sign extended and added to the register specified
1177 in the instruction, which is assumed to point to _SDA_BASE_. */
1179 static bfd_reloc_status_type
1180 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1181 input_section, output_bfd, error_message)
1182 bfd *abfd ATTRIBUTE_UNUSED;
1183 arelent *reloc_entry;
1184 asymbol *symbol;
1185 PTR data ATTRIBUTE_UNUSED;
1186 asection *input_section;
1187 bfd *output_bfd;
1188 char **error_message ATTRIBUTE_UNUSED;
1190 /* This part is from bfd_elf_generic_reloc. */
1191 if (output_bfd != (bfd *) NULL
1192 && (symbol->flags & BSF_SECTION_SYM) == 0
1193 && (! reloc_entry->howto->partial_inplace
1194 || reloc_entry->addend == 0))
1196 reloc_entry->address += input_section->output_offset;
1197 return bfd_reloc_ok;
1200 if (output_bfd != NULL)
1202 /* FIXME: See bfd_perform_relocation. Is this right? */
1203 return bfd_reloc_continue;
1206 /* FIXME: not sure what to do here yet. But then again, the linker
1207 may never call us. */
1208 abort ();
1211 /* Map BFD reloc types to M32R ELF reloc types. */
1213 struct m32r_reloc_map
1215 bfd_reloc_code_real_type bfd_reloc_val;
1216 unsigned char elf_reloc_val;
1219 static const struct m32r_reloc_map m32r_reloc_map_old[] =
1221 { BFD_RELOC_NONE, R_M32R_NONE },
1222 { BFD_RELOC_16, R_M32R_16 },
1223 { BFD_RELOC_32, R_M32R_32 },
1224 { BFD_RELOC_M32R_24, R_M32R_24 },
1225 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
1226 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
1227 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
1228 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
1229 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
1230 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
1231 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
1232 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
1233 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
1236 static const struct m32r_reloc_map m32r_reloc_map[] =
1238 { BFD_RELOC_NONE, R_M32R_NONE },
1239 { BFD_RELOC_16, R_M32R_16_RELA },
1240 { BFD_RELOC_32, R_M32R_32_RELA },
1241 { BFD_RELOC_M32R_24, R_M32R_24_RELA },
1242 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
1243 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
1244 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
1245 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
1246 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
1247 { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
1248 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
1249 { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
1250 { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
1252 { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
1253 { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
1254 { BFD_RELOC_M32R_COPY, R_M32R_COPY },
1255 { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
1256 { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
1257 { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
1258 { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
1259 { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
1260 { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
1261 { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
1262 { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
1263 { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
1264 { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
1265 { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
1268 static reloc_howto_type *
1269 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1270 bfd *abfd ATTRIBUTE_UNUSED;
1271 bfd_reloc_code_real_type code;
1273 unsigned int i;
1275 #ifdef USE_M32R_OLD_RELOC
1276 for (i = 0;
1277 i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
1278 i++)
1280 if (m32r_reloc_map_old[i].bfd_reloc_val == code)
1281 return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
1283 #else /* ! USE_M32R_OLD_RELOC */
1285 for (i = 0;
1286 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
1287 i++)
1289 if (m32r_reloc_map[i].bfd_reloc_val == code)
1290 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
1292 #endif
1294 return NULL;
1297 /* Set the howto pointer for an M32R ELF reloc. */
1299 static void
1300 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
1301 bfd *abfd ATTRIBUTE_UNUSED;
1302 arelent *cache_ptr;
1303 Elf_Internal_Rela *dst;
1305 unsigned int r_type;
1307 r_type = ELF32_R_TYPE (dst->r_info);
1308 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY)
1309 cache_ptr->howto = &m32r_elf_howto_table[r_type];
1312 static void
1313 m32r_info_to_howto (abfd, cache_ptr, dst)
1314 bfd *abfd ATTRIBUTE_UNUSED;
1315 arelent *cache_ptr;
1316 Elf_Internal_Rela *dst;
1318 BFD_ASSERT ((ELF32_R_TYPE(dst->r_info) == (unsigned int) R_M32R_NONE)
1319 || ((ELF32_R_TYPE(dst->r_info) > (unsigned int) R_M32R_GNU_VTENTRY)
1320 && (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_M32R_max)));
1321 cache_ptr->howto = &m32r_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
1325 /* Given a BFD section, try to locate the corresponding ELF section
1326 index. */
1328 bfd_boolean
1329 _bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
1330 bfd *abfd ATTRIBUTE_UNUSED;
1331 asection *sec;
1332 int *retval;
1334 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1336 *retval = SHN_M32R_SCOMMON;
1337 return TRUE;
1339 return FALSE;
1342 /* M32R ELF uses two common sections. One is the usual one, and the other
1343 is for small objects. All the small objects are kept together, and then
1344 referenced via one register, which yields faster assembler code. It is
1345 up to the compiler to emit an instruction to load the register with
1346 _SDA_BASE. This is what we use for the small common section. This
1347 approach is copied from elf32-mips.c. */
1348 static asection m32r_elf_scom_section;
1349 static asymbol m32r_elf_scom_symbol;
1350 static asymbol *m32r_elf_scom_symbol_ptr;
1352 /* Handle the special M32R section numbers that a symbol may use. */
1354 void
1355 _bfd_m32r_elf_symbol_processing (abfd, asym)
1356 bfd *abfd ATTRIBUTE_UNUSED;
1357 asymbol *asym;
1359 elf_symbol_type *elfsym;
1361 elfsym = (elf_symbol_type *) asym;
1363 switch (elfsym->internal_elf_sym.st_shndx)
1365 case SHN_M32R_SCOMMON:
1366 if (m32r_elf_scom_section.name == NULL)
1368 /* Initialize the small common section. */
1369 m32r_elf_scom_section.name = ".scommon";
1370 m32r_elf_scom_section.flags = SEC_IS_COMMON;
1371 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
1372 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
1373 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
1374 m32r_elf_scom_symbol.name = ".scommon";
1375 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
1376 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
1377 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
1379 asym->section = &m32r_elf_scom_section;
1380 asym->value = elfsym->internal_elf_sym.st_size;
1381 break;
1385 /* Hook called by the linker routine which adds symbols from an object
1386 file. We must handle the special M32R section numbers here.
1387 We also keep watching for whether we need to create the sdata special
1388 linker sections. */
1390 static bfd_boolean
1391 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1392 bfd *abfd;
1393 struct bfd_link_info *info;
1394 Elf_Internal_Sym *sym;
1395 const char **namep;
1396 flagword *flagsp ATTRIBUTE_UNUSED;
1397 asection **secp;
1398 bfd_vma *valp;
1400 if (! info->relocatable
1401 && (*namep)[0] == '_' && (*namep)[1] == 'S'
1402 && strcmp (*namep, "_SDA_BASE_") == 0
1403 && is_elf_hash_table (info->hash))
1405 /* This is simpler than using _bfd_elf_create_linker_section
1406 (our needs are simpler than ppc's needs). Also
1407 _bfd_elf_create_linker_section currently has a bug where if a .sdata
1408 section already exists a new one is created that follows it which
1409 screws of _SDA_BASE_ address calcs because output_offset != 0. */
1410 struct elf_link_hash_entry *h;
1411 struct bfd_link_hash_entry *bh;
1412 asection *s = bfd_get_section_by_name (abfd, ".sdata");
1414 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
1416 if (s == NULL)
1418 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1419 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1421 s = bfd_make_section_anyway (abfd, ".sdata");
1422 if (s == NULL)
1423 return FALSE;
1424 bfd_set_section_flags (abfd, s, flags);
1425 bfd_set_section_alignment (abfd, s, 2);
1428 bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
1429 FALSE, FALSE, FALSE);
1431 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1432 && !(_bfd_generic_link_add_one_symbol (info,
1433 abfd,
1434 "_SDA_BASE_",
1435 BSF_GLOBAL,
1437 (bfd_vma) 32768,
1438 (const char *) NULL,
1439 FALSE,
1440 get_elf_backend_data (abfd)->collect,
1441 &bh)))
1442 return FALSE;
1443 h = (struct elf_link_hash_entry *) bh;
1444 h->type = STT_OBJECT;
1447 switch (sym->st_shndx)
1449 case SHN_M32R_SCOMMON:
1450 *secp = bfd_make_section_old_way (abfd, ".scommon");
1451 (*secp)->flags |= SEC_IS_COMMON;
1452 *valp = sym->st_size;
1453 break;
1456 return TRUE;
1459 /* We have to figure out the SDA_BASE value, so that we can adjust the
1460 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
1461 BFD. If we can't find it, we're stuck. We cache it in the ELF
1462 target data. We don't need to adjust the symbol value for an
1463 external symbol if we are producing relocatable output. */
1465 static bfd_reloc_status_type
1466 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
1467 bfd *output_bfd;
1468 struct bfd_link_info *info;
1469 const char **error_message;
1470 bfd_vma *psb;
1472 if (elf_gp (output_bfd) == 0)
1474 struct bfd_link_hash_entry *h;
1476 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
1477 if (h != (struct bfd_link_hash_entry *) NULL
1478 && h->type == bfd_link_hash_defined)
1479 elf_gp (output_bfd) = (h->u.def.value
1480 + h->u.def.section->output_section->vma
1481 + h->u.def.section->output_offset);
1482 else
1484 /* Only get the error once. */
1485 *psb = elf_gp (output_bfd) = 4;
1486 *error_message =
1487 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
1488 return bfd_reloc_dangerous;
1491 *psb = elf_gp (output_bfd);
1492 return bfd_reloc_ok;
1495 /* Return size of a PLT entry. */
1496 #define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
1498 /* The m32r linker needs to keep track of the number of relocs that it
1499 decides to copy in check_relocs for each symbol. This is so that
1500 it can discard PC relative relocs if it doesn't need them when
1501 linking with -Bsymbolic. We store the information in a field
1502 extending the regular ELF linker hash table. */
1504 /* This structure keeps track of the number of PC relative relocs we
1505 have copied for a given symbol. */
1507 struct elf_m32r_pcrel_relocs_copied
1509 /* Next section. */
1510 struct elf_m32r_pcrel_relocs_copied *next;
1511 /* A section in dynobj. */
1512 asection *section;
1513 /* Number of relocs copied in this section. */
1514 bfd_size_type count;
1517 /* The sh linker needs to keep track of the number of relocs that it
1518 decides to copy as dynamic relocs in check_relocs for each symbol.
1519 This is so that it can later discard them if they are found to be
1520 unnecessary. We store the information in a field extending the
1521 regular ELF linker hash table. */
1523 struct elf_m32r_dyn_relocs
1525 struct elf_m32r_dyn_relocs *next;
1527 /* The input section of the reloc. */
1528 asection *sec;
1530 /* Total number of relocs copied for the input section. */
1531 bfd_size_type count;
1533 /* Number of pc-relative relocs copied for the input section. */
1534 bfd_size_type pc_count;
1538 /* m32r ELF linker hash entry. */
1540 struct elf_m32r_link_hash_entry
1542 struct elf_link_hash_entry root;
1544 /* Track dynamic relocs copied for this symbol. */
1545 struct elf_m32r_dyn_relocs *dyn_relocs;
1547 // bfd_signed_vma gotplt_refcount;
1549 /* Number of PC relative relocs copied for this symbol. */
1550 /* struct elf_m32r_pcrel_relocs_copied *pcrel_relocs_copied; FIXME */
1553 /* m32r ELF linker hash table. */
1555 struct elf_m32r_link_hash_table
1557 struct elf_link_hash_table root;
1559 /* Short-cuts to get to dynamic linker sections. */
1560 asection *sgot;
1561 asection *sgotplt;
1562 asection *srelgot;
1563 asection *splt;
1564 asection *srelplt;
1565 asection *sdynbss;
1566 asection *srelbss;
1568 /* Small local sym to section mapping cache. */
1569 struct sym_sec_cache sym_sec;
1572 /* Traverse an m32r ELF linker hash table. */
1574 #define m32r_elf_link_hash_traverse(table, func, info) \
1575 (elf_link_hash_traverse \
1576 (&(table)->root, \
1577 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1578 (info)))
1580 /* Get the m32r ELF linker hash table from a link_info structure. */
1583 #define m32r_elf_hash_table(p) \
1584 ((struct elf_m32r_link_hash_table *) ((p)->hash))
1586 /* Create an entry in an m32r ELF linker hash table. */
1587 static struct bfd_hash_entry *
1588 m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1589 const char * );
1591 static struct bfd_hash_entry *
1592 m32r_elf_link_hash_newfunc (entry, table, string)
1593 struct bfd_hash_entry *entry;
1594 struct bfd_hash_table *table;
1595 const char *string;
1597 struct elf_m32r_link_hash_entry *ret =
1598 (struct elf_m32r_link_hash_entry *) entry;
1600 /* Allocate the structure if it has not already been allocated by a
1601 subclass. */
1602 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1603 ret = ((struct elf_m32r_link_hash_entry *)
1604 bfd_hash_allocate (table,
1605 sizeof (struct elf_m32r_link_hash_entry)));
1606 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1607 return (struct bfd_hash_entry *) ret;
1609 /* Call the allocation method of the superclass. */
1610 ret = ((struct elf_m32r_link_hash_entry *)
1611 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1612 table, string));
1613 if (ret != (struct elf_m32r_link_hash_entry *) NULL)
1615 struct elf_m32r_link_hash_entry *eh;
1617 eh = (struct elf_m32r_link_hash_entry *) ret;
1618 eh->dyn_relocs = NULL;
1619 // eh->gotplt_refcount = 0;
1620 /* eh->pcrel_relocs_copied = NULL; FIXME */
1623 return (struct bfd_hash_entry *) ret;
1626 /* Create an m32r ELF linker hash table. */
1627 static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1629 static struct bfd_link_hash_table *
1630 m32r_elf_link_hash_table_create (abfd)
1631 bfd *abfd;
1633 struct elf_m32r_link_hash_table *ret;
1634 bfd_size_type amt = sizeof (struct elf_m32r_link_hash_table);
1636 ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1637 if (ret == (struct elf_m32r_link_hash_table *) NULL)
1638 return NULL;
1640 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1641 m32r_elf_link_hash_newfunc))
1643 free (ret);
1644 return NULL;
1647 ret->sgot = NULL;
1648 ret->sgotplt = NULL;
1649 ret->srelgot = NULL;
1650 ret->splt = NULL;
1651 ret->srelplt = NULL;
1652 ret->sdynbss = NULL;
1653 ret->srelbss = NULL;
1654 ret->sym_sec.abfd = NULL;
1656 return &ret->root.root;
1659 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1660 shortcuts to them in our hash table. */
1661 static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1663 static bfd_boolean
1664 create_got_section (dynobj, info)
1665 bfd *dynobj;
1666 struct bfd_link_info *info;
1668 struct elf_m32r_link_hash_table *htab;
1670 if (! _bfd_elf_create_got_section (dynobj, info))
1671 return FALSE;
1673 htab = m32r_elf_hash_table (info);
1674 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1675 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1676 if (! htab->sgot || ! htab->sgotplt)
1677 abort ();
1679 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1680 if (htab->srelgot == NULL
1681 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1682 (SEC_ALLOC
1683 | SEC_LOAD
1684 | SEC_HAS_CONTENTS
1685 | SEC_IN_MEMORY
1686 | SEC_LINKER_CREATED
1687 | SEC_READONLY))
1688 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1689 return FALSE;
1691 return TRUE;
1694 /* Create dynamic sections when linking against a dynamic object. */
1696 static bfd_boolean
1697 m32r_elf_create_dynamic_sections (abfd, info)
1698 bfd *abfd;
1699 struct bfd_link_info *info;
1701 struct elf_m32r_link_hash_table *htab;
1702 flagword flags, pltflags;
1703 register asection *s;
1704 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1705 int ptralign = 2; /* 32bit */
1707 htab = m32r_elf_hash_table (info);
1709 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1710 .rel[a].bss sections. */
1712 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1713 | SEC_LINKER_CREATED);
1715 pltflags = flags;
1716 pltflags |= SEC_CODE;
1717 if (bed->plt_not_loaded)
1718 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1719 if (bed->plt_readonly)
1720 pltflags |= SEC_READONLY;
1722 s = bfd_make_section (abfd, ".plt");
1723 htab->splt = s;
1724 if (s == NULL
1725 || ! bfd_set_section_flags (abfd, s, pltflags)
1726 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1727 return FALSE;
1729 if (bed->want_plt_sym)
1731 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1732 .plt section. */
1733 struct bfd_link_hash_entry *bh = NULL;
1734 struct elf_link_hash_entry *h;
1735 if (! (_bfd_generic_link_add_one_symbol
1736 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1737 (bfd_vma) 0, (const char *) NULL, FALSE,
1738 get_elf_backend_data (abfd)->collect, &bh)))
1739 return FALSE;
1740 h = (struct elf_link_hash_entry *) bh;
1741 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1742 h->type = STT_OBJECT;
1744 if (info->shared
1745 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1746 return FALSE;
1749 s = bfd_make_section (abfd,
1750 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
1751 htab->srelplt = s;
1752 if (s == NULL
1753 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1754 || ! bfd_set_section_alignment (abfd, s, ptralign))
1755 return FALSE;
1757 if (htab->sgot == NULL
1758 && ! create_got_section (abfd, info))
1759 return FALSE;
1762 const char *secname;
1763 char *relname;
1764 flagword secflags;
1765 asection *sec;
1767 for (sec = abfd->sections; sec; sec = sec->next)
1769 secflags = bfd_get_section_flags (abfd, sec);
1770 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1771 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1772 continue;
1773 secname = bfd_get_section_name (abfd, sec);
1774 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1775 strcpy (relname, ".rela");
1776 strcat (relname, secname);
1777 if (bfd_get_section_by_name (abfd, secname))
1778 continue;
1779 s = bfd_make_section (abfd, relname);
1780 if (s == NULL
1781 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1782 || ! bfd_set_section_alignment (abfd, s, ptralign))
1783 return FALSE;
1787 if (bed->want_dynbss)
1789 /* The .dynbss section is a place to put symbols which are defined
1790 by dynamic objects, are referenced by regular objects, and are
1791 not functions. We must allocate space for them in the process
1792 image and use a R_*_COPY reloc to tell the dynamic linker to
1793 initialize them at run time. The linker script puts the .dynbss
1794 section into the .bss section of the final image. */
1795 s = bfd_make_section (abfd, ".dynbss");
1796 htab->sdynbss = s;
1797 if (s == NULL
1798 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1799 return FALSE;
1800 /* The .rel[a].bss section holds copy relocs. This section is not
1801 normally needed. We need to create it here, though, so that the
1802 linker will map it to an output section. We can't just create it
1803 only if we need it, because we will not know whether we need it
1804 until we have seen all the input files, and the first time the
1805 main linker code calls BFD after examining all the input files
1806 (size_dynamic_sections) the input sections have already been
1807 mapped to the output sections. If the section turns out not to
1808 be needed, we can discard it later. We will never need this
1809 section when generating a shared object, since they do not use
1810 copy relocs. */
1811 if (! info->shared)
1813 s = bfd_make_section (abfd,
1814 (bed->default_use_rela_p
1815 ? ".rela.bss" : ".rel.bss"));
1816 htab->srelbss = s;
1817 if (s == NULL
1818 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1819 || ! bfd_set_section_alignment (abfd, s, ptralign))
1820 return FALSE;
1824 return TRUE;
1827 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1828 static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1829 struct elf_link_hash_entry *,
1830 struct elf_link_hash_entry *);
1832 static void
1833 m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1834 struct elf_link_hash_entry *dir,
1835 struct elf_link_hash_entry *ind)
1837 struct elf_m32r_link_hash_entry *edir, *eind;
1839 edir = (struct elf_m32r_link_hash_entry *) dir;
1840 eind = (struct elf_m32r_link_hash_entry *) ind;
1842 if (eind->dyn_relocs != NULL)
1844 if (edir->dyn_relocs != NULL)
1846 struct elf_m32r_dyn_relocs **pp;
1847 struct elf_m32r_dyn_relocs *p;
1849 if (ind->root.type == bfd_link_hash_indirect)
1850 abort ();
1852 /* Add reloc counts against the weak sym to the strong sym
1853 list. Merge any entries against the same section. */
1854 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1856 struct elf_m32r_dyn_relocs *q;
1858 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1859 if (q->sec == p->sec)
1861 q->pc_count += p->pc_count;
1862 q->count += p->count;
1863 *pp = p->next;
1864 break;
1866 if (q == NULL)
1867 pp = &p->next;
1869 *pp = edir->dyn_relocs;
1872 edir->dyn_relocs = eind->dyn_relocs;
1873 eind->dyn_relocs = NULL;
1876 // if (ind->root.type == bfd_link_hash_indirect
1877 // && dir->got.refcount <= 0)
1878 // {
1879 // edir->tls_type = eind->tls_type;
1880 // eind->tls_type = GOT_UNKNOWN;
1881 // }
1882 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1886 /* Adjust a symbol defined by a dynamic object and referenced by a
1887 regular object. The current definition is in some section of the
1888 dynamic object, but we're not including those sections. We have to
1889 change the definition to something the rest of the link can
1890 understand. */
1892 static bfd_boolean
1893 m32r_elf_adjust_dynamic_symbol (info, h)
1894 struct bfd_link_info *info;
1895 struct elf_link_hash_entry *h;
1897 struct elf_m32r_link_hash_table *htab;
1898 struct elf_m32r_link_hash_entry *eh;
1899 struct elf_m32r_dyn_relocs *p;
1900 bfd *dynobj;
1901 asection *s;
1902 unsigned int power_of_two;
1904 #ifdef DEBUG_PIC
1905 printf("m32r_elf_adjust_dynamic_symbol()\n");
1906 #endif
1908 dynobj = elf_hash_table (info)->dynobj;
1910 /* Make sure we know what is going on here. */
1911 BFD_ASSERT (dynobj != NULL
1912 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1913 || h->weakdef != NULL
1914 || ((h->elf_link_hash_flags
1915 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1916 && (h->elf_link_hash_flags
1917 & ELF_LINK_HASH_REF_REGULAR) != 0
1918 && (h->elf_link_hash_flags
1919 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1922 /* If this is a function, put it in the procedure linkage table. We
1923 will fill in the contents of the procedure linkage table later,
1924 when we know the address of the .got section. */
1925 if (h->type == STT_FUNC
1926 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1928 if (! info->shared
1929 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1930 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1931 && h->root.type != bfd_link_hash_undefweak
1932 && h->root.type != bfd_link_hash_undefined)
1934 /* This case can occur if we saw a PLT reloc in an input
1935 file, but the symbol was never referred to by a dynamic
1936 object. In such a case, we don't actually need to build
1937 a procedure linkage table, and we can just do a PCREL
1938 reloc instead. */
1939 h->plt.offset = (bfd_vma) -1;
1940 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1943 return TRUE;
1945 else
1946 h->plt.offset = (bfd_vma) -1;
1948 /* If this is a weak symbol, and there is a real definition, the
1949 processor independent code will have arranged for us to see the
1950 real definition first, and we can just use the same value. */
1951 if (h->weakdef != NULL)
1953 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1954 || h->weakdef->root.type == bfd_link_hash_defweak);
1955 h->root.u.def.section = h->weakdef->root.u.def.section;
1956 h->root.u.def.value = h->weakdef->root.u.def.value;
1957 return TRUE;
1960 /* This is a reference to a symbol defined by a dynamic object which
1961 is not a function. */
1963 /* If we are creating a shared library, we must presume that the
1964 only references to the symbol are via the global offset table.
1965 For such cases we need not do anything here; the relocations will
1966 be handled correctly by relocate_section. */
1967 if (info->shared)
1968 return TRUE;
1970 /* If there are no references to this symbol that do not use the
1971 GOT, we don't need to generate a copy reloc. */
1972 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1973 return TRUE;
1975 /* If -z nocopyreloc was given, we won't generate them either. */
1976 if (info->nocopyreloc)
1978 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1979 return TRUE;
1982 eh = (struct elf_m32r_link_hash_entry *) h;
1983 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1985 s = p->sec->output_section;
1986 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
1987 break;
1990 /* If we didn't find any dynamic relocs in sections which needs the
1991 copy reloc, then we'll be keeping the dynamic relocs and avoiding
1992 the copy reloc. */
1993 if (p == NULL)
1995 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1996 return TRUE;
1999 /* We must allocate the symbol in our .dynbss section, which will
2000 become part of the .bss section of the executable. There will be
2001 an entry for this symbol in the .dynsym section. The dynamic
2002 object will contain position independent code, so all references
2003 from the dynamic object to this symbol will go through the global
2004 offset table. The dynamic linker will use the .dynsym entry to
2005 determine the address it must put in the global offset table, so
2006 both the dynamic object and the regular object will refer to the
2007 same memory location for the variable. */
2009 htab = m32r_elf_hash_table (info);
2010 s = htab->sdynbss;
2011 BFD_ASSERT (s != NULL);
2013 /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2014 to copy the initial value out of the dynamic object and into the
2015 runtime process image. We need to remember the offset into the
2016 .rela.bss section we are going to use. */
2017 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2019 asection *srel;
2021 srel = htab->srelbss;
2022 BFD_ASSERT (srel != NULL);
2023 srel->_raw_size += sizeof (Elf32_External_Rela);
2024 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2027 /* We need to figure out the alignment required for this symbol. I
2028 have no idea how ELF linkers handle this. */
2029 power_of_two = bfd_log2 (h->size);
2030 if (power_of_two > 3)
2031 power_of_two = 3;
2033 /* Apply the required alignment. */
2034 s->_raw_size = BFD_ALIGN (s->_raw_size,
2035 (bfd_size_type) (1 << power_of_two));
2036 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2038 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2039 return FALSE;
2042 /* Define the symbol as being at this point in the section. */
2043 h->root.u.def.section = s;
2044 h->root.u.def.value = s->_raw_size;
2046 /* Increment the section size to make room for the symbol. */
2047 s->_raw_size += h->size;
2049 return TRUE;
2052 /* Allocate space in .plt, .got and associated reloc sections for
2053 dynamic relocs. */
2055 static bfd_boolean
2056 allocate_dynrelocs (h, inf)
2057 struct elf_link_hash_entry *h;
2058 PTR inf;
2060 struct bfd_link_info *info;
2061 struct elf_m32r_link_hash_table *htab;
2062 struct elf_m32r_link_hash_entry *eh;
2063 struct elf_m32r_dyn_relocs *p;
2065 if (h->root.type == bfd_link_hash_indirect)
2066 return TRUE;
2068 if (h->root.type == bfd_link_hash_warning)
2069 /* When warning symbols are created, they **replace** the "real"
2070 entry in the hash table, thus we never get to see the real
2071 symbol in a hash traversal. So look at it now. */
2072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2074 info = (struct bfd_link_info *) inf;
2075 htab = m32r_elf_hash_table (info);
2077 eh = (struct elf_m32r_link_hash_entry *) h;
2078 // if ((h->got.refcount > 0
2079 // || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2080 // && eh->gotplt_refcount > 0)
2081 // {
2082 // /* The symbol has been forced local, or we have some direct got refs,
2083 // so treat all the gotplt refs as got refs. */
2084 // h->got.refcount += eh->gotplt_refcount;
2085 // if (h->plt.refcount >= eh->gotplt_refcount)
2086 // h->plt.refcount -= eh->gotplt_refcount;
2087 // }
2089 if (htab->root.dynamic_sections_created
2090 && h->plt.refcount > 0)
2092 /* Make sure this symbol is output as a dynamic symbol.
2093 Undefined weak syms won't yet be marked as dynamic. */
2094 if (h->dynindx == -1
2095 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2097 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2098 return FALSE;
2101 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2103 asection *s = htab->splt;
2105 /* If this is the first .plt entry, make room for the special
2106 first entry. */
2107 if (s->_raw_size == 0)
2108 s->_raw_size += PLT_ENTRY_SIZE;
2110 h->plt.offset = s->_raw_size;
2112 /* If this symbol is not defined in a regular file, and we are
2113 not generating a shared library, then set the symbol to this
2114 location in the .plt. This is required to make function
2115 pointers compare as equal between the normal executable and
2116 the shared library. */
2117 if (! info->shared
2118 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2120 h->root.u.def.section = s;
2121 h->root.u.def.value = h->plt.offset;
2124 /* Make room for this entry. */
2125 s->_raw_size += PLT_ENTRY_SIZE;
2127 /* We also need to make an entry in the .got.plt section, which
2128 will be placed in the .got section by the linker script. */
2129 htab->sgotplt->_raw_size += 4;
2131 /* We also need to make an entry in the .rel.plt section. */
2132 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2134 else
2136 h->plt.offset = (bfd_vma) -1;
2137 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2140 else
2142 h->plt.offset = (bfd_vma) -1;
2143 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2146 if (h->got.refcount > 0)
2148 asection *s;
2149 bfd_boolean dyn;
2151 /* Make sure this symbol is output as a dynamic symbol.
2152 Undefined weak syms won't yet be marked as dynamic. */
2153 if (h->dynindx == -1
2154 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2156 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2157 return FALSE;
2160 s = htab->sgot;
2162 h->got.offset = s->_raw_size;
2163 s->_raw_size += 4;
2164 dyn = htab->root.dynamic_sections_created;
2165 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2166 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
2168 else
2169 h->got.offset = (bfd_vma) -1;
2171 if (eh->dyn_relocs == NULL)
2172 return TRUE;
2174 /* In the shared -Bsymbolic case, discard space allocated for
2175 dynamic pc-relative relocs against symbols which turn out to be
2176 defined in regular objects. For the normal shared case, discard
2177 space for pc-relative relocs that have become local due to symbol
2178 visibility changes. */
2180 if (info->shared)
2182 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2183 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2184 || info->symbolic))
2186 struct elf_m32r_dyn_relocs **pp;
2187 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2189 p->count -= p->pc_count;
2190 p->pc_count = 0;
2191 if (p->count == 0)
2192 *pp = p->next;
2193 else
2194 pp = &p->next;
2198 else
2200 /* For the non-shared case, discard space for relocs against
2201 symbols which turn out to need copy relocs or are not
2202 dynamic. */
2204 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2205 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2206 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2207 || (htab->root.dynamic_sections_created
2208 && (h->root.type == bfd_link_hash_undefweak
2209 || h->root.type == bfd_link_hash_undefined))))
2211 /* Make sure this symbol is output as a dynamic symbol.
2212 Undefined weak syms won't yet be marked as dynamic. */
2213 if (h->dynindx == -1
2214 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2216 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2217 return FALSE;
2220 /* If that succeeded, we know we'll be keeping all the
2221 relocs. */
2222 if (h->dynindx != -1)
2223 goto keep;
2226 eh->dyn_relocs = NULL;
2228 keep: ;
2231 /* Finally, allocate space. */
2232 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2234 asection *sreloc = elf_section_data (p->sec)->sreloc;
2235 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2238 return TRUE;
2240 /* Find any dynamic relocs that apply to read-only sections. */
2242 static bfd_boolean
2243 readonly_dynrelocs (h, inf)
2244 struct elf_link_hash_entry *h;
2245 PTR inf;
2247 struct elf_m32r_link_hash_entry *eh;
2248 struct elf_m32r_dyn_relocs *p;
2250 if (h->root.type == bfd_link_hash_warning)
2251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2253 eh = (struct elf_m32r_link_hash_entry *) h;
2254 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2256 asection *s = p->sec->output_section;
2258 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2260 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2262 info->flags |= DF_TEXTREL;
2264 /* Not an error, just cut short the traversal. */
2265 return FALSE;
2268 return TRUE;
2271 /* Set the sizes of the dynamic sections. */
2273 static bfd_boolean
2274 m32r_elf_size_dynamic_sections (output_bfd, info)
2275 bfd *output_bfd ATTRIBUTE_UNUSED;
2276 struct bfd_link_info *info;
2278 struct elf_m32r_link_hash_table *htab;
2279 bfd *dynobj;
2280 asection *s;
2281 bfd_boolean relocs;
2282 bfd *ibfd;
2284 #ifdef DEBUG_PIC
2285 printf("m32r_elf_size_dynamic_sections()\n");
2286 #endif
2288 htab = m32r_elf_hash_table (info);
2289 dynobj = htab->root.dynobj;
2290 BFD_ASSERT (dynobj != NULL);
2292 if (htab->root.dynamic_sections_created)
2294 /* Set the contents of the .interp section to the interpreter. */
2295 if (! info->shared)
2297 s = bfd_get_section_by_name (dynobj, ".interp");
2298 BFD_ASSERT (s != NULL);
2299 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2300 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2304 /* Set up .got offsets for local syms, and space for local dynamic
2305 relocs. */
2306 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2308 bfd_signed_vma *local_got;
2309 bfd_signed_vma *end_local_got;
2310 bfd_size_type locsymcount;
2311 Elf_Internal_Shdr *symtab_hdr;
2312 asection *srel;
2314 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2315 continue;
2317 for (s = ibfd->sections; s != NULL; s = s->next)
2319 struct elf_m32r_dyn_relocs *p;
2321 for (p = ((struct elf_m32r_dyn_relocs *)
2322 elf_section_data (s)->local_dynrel);
2323 p != NULL;
2324 p = p->next)
2326 if (! bfd_is_abs_section (p->sec)
2327 && bfd_is_abs_section (p->sec->output_section))
2329 /* Input section has been discarded, either because
2330 it is a copy of a linkonce section or due to
2331 linker script /DISCARD/, so we'll be discarding
2332 the relocs too. */
2334 else if (p->count != 0)
2336 srel = elf_section_data (p->sec)->sreloc;
2337 srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
2338 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2339 info->flags |= DF_TEXTREL;
2344 local_got = elf_local_got_refcounts (ibfd);
2345 if (!local_got)
2346 continue;
2348 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2349 locsymcount = symtab_hdr->sh_info;
2350 end_local_got = local_got + locsymcount;
2351 s = htab->sgot;
2352 srel = htab->srelgot;
2353 for (; local_got < end_local_got; ++local_got)
2355 if (*local_got > 0)
2357 *local_got = s->_raw_size;
2358 s->_raw_size += 4;
2359 if (info->shared)
2360 srel->_raw_size += sizeof (Elf32_External_Rela);
2362 else
2363 *local_got = (bfd_vma) -1;
2367 /* Allocate global sym .plt and .got entries, and space for global
2368 sym dynamic relocs. */
2369 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2371 /* We now have determined the sizes of the various dynamic sections.
2372 Allocate memory for them. */
2373 relocs = FALSE;
2374 for (s = dynobj->sections; s != NULL; s = s->next)
2376 if ((s->flags & SEC_LINKER_CREATED) == 0)
2377 continue;
2379 if (s == htab->splt
2380 || s == htab->sgot
2381 || s == htab->sgotplt)
2383 /* Strip this section if we don't need it; see the
2384 comment below. */
2386 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2388 if (s->_raw_size != 0 && s != htab->srelplt)
2389 relocs = TRUE;
2391 /* We use the reloc_count field as a counter if we need
2392 to copy relocs into the output file. */
2393 s->reloc_count = 0;
2395 else
2397 /* It's not one of our sections, so don't allocate space. */
2398 continue;
2401 if (s->_raw_size == 0)
2403 /* If we don't need this section, strip it from the
2404 output file. This is mostly to handle .rela.bss and
2405 .rela.plt. We must create both sections in
2406 create_dynamic_sections, because they must be created
2407 before the linker maps input sections to output
2408 sections. The linker does that before
2409 adjust_dynamic_symbol is called, and it is that
2410 function which decides whether anything needs to go
2411 into these sections. */
2412 _bfd_strip_section_from_output (info, s);
2413 continue;
2416 /* Allocate memory for the section contents. We use bfd_zalloc
2417 here in case unused entries are not reclaimed before the
2418 section's contents are written out. This should not happen,
2419 but this way if it does, we get a R_M32R_NONE reloc instead
2420 of garbage. */
2421 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2422 if (s->contents == NULL)
2423 return FALSE;
2426 if (htab->root.dynamic_sections_created)
2428 /* Add some entries to the .dynamic section. We fill in the
2429 values later, in m32r_elf_finish_dynamic_sections, but we
2430 must add the entries now so that we get the correct size for
2431 the .dynamic section. The DT_DEBUG entry is filled in by the
2432 dynamic linker and used by the debugger. */
2433 #define add_dynamic_entry(TAG, VAL) \
2434 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2436 if (! info->shared)
2438 if (! add_dynamic_entry (DT_DEBUG, 0))
2439 return FALSE;
2442 if (htab->splt->_raw_size != 0)
2444 if (! add_dynamic_entry (DT_PLTGOT, 0)
2445 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2446 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2447 || ! add_dynamic_entry (DT_JMPREL, 0))
2448 return FALSE;
2451 if (relocs)
2453 if (! add_dynamic_entry (DT_RELA, 0)
2454 || ! add_dynamic_entry (DT_RELASZ, 0)
2455 || ! add_dynamic_entry (DT_RELAENT,
2456 sizeof (Elf32_External_Rela)))
2457 return FALSE;
2459 /* If any dynamic relocs apply to a read-only section,
2460 then we need a DT_TEXTREL entry. */
2461 if ((info->flags & DF_TEXTREL) == 0)
2462 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2463 (PTR) info);
2465 if ((info->flags & DF_TEXTREL) != 0)
2467 if (! add_dynamic_entry (DT_TEXTREL, 0))
2468 return FALSE;
2472 #undef add_dynamic_entry
2474 return TRUE;
2476 /* Relocate an M32R/D ELF section.
2477 There is some attempt to make this function usable for many architectures,
2478 both for RELA and REL type relocs, if only to serve as a learning tool.
2480 The RELOCATE_SECTION function is called by the new ELF backend linker
2481 to handle the relocations for a section.
2483 The relocs are always passed as Rela structures; if the section
2484 actually uses Rel structures, the r_addend field will always be
2485 zero.
2487 This function is responsible for adjust the section contents as
2488 necessary, and (if using Rela relocs and generating a
2489 relocatable output file) adjusting the reloc addend as
2490 necessary.
2492 This function does not have to worry about setting the reloc
2493 address or the reloc symbol index.
2495 LOCAL_SYMS is a pointer to the swapped in local symbols.
2497 LOCAL_SECTIONS is an array giving the section in the input file
2498 corresponding to the st_shndx field of each local symbol.
2500 The global hash table entry for the global symbols can be found
2501 via elf_sym_hashes (input_bfd).
2503 When generating relocatable output, this function must handle
2504 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2505 going to be the section symbol corresponding to the output
2506 section, which means that the addend must be adjusted
2507 accordingly. */
2509 static bfd_boolean
2510 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2511 contents, relocs, local_syms, local_sections)
2512 bfd *output_bfd ATTRIBUTE_UNUSED;
2513 struct bfd_link_info *info;
2514 bfd *input_bfd;
2515 asection *input_section;
2516 bfd_byte *contents;
2517 Elf_Internal_Rela *relocs;
2518 Elf_Internal_Sym *local_syms;
2519 asection **local_sections;
2521 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2522 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2523 Elf_Internal_Rela *rel, *relend;
2524 /* Assume success. */
2525 bfd_boolean ret = TRUE;
2527 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2528 bfd *dynobj;
2529 bfd_vma *local_got_offsets;
2530 asection *sgot, *splt, *sreloc;
2532 dynobj = htab->root.dynobj;
2533 local_got_offsets = elf_local_got_offsets (input_bfd);
2535 sgot = htab->sgot;
2536 splt = htab->splt;
2537 sreloc = NULL;
2539 rel = relocs;
2540 relend = relocs + input_section->reloc_count;
2541 for (; rel < relend; rel++)
2543 int r_type;
2544 reloc_howto_type *howto;
2545 unsigned long r_symndx;
2546 struct elf_link_hash_entry *h;
2547 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
2548 ensure it's zero (we use REL relocs, not RELA). Therefore this
2549 should be assigning zero to `addend', but for clarity we use
2550 `r_addend'. */
2551 bfd_vma addend = rel->r_addend;
2552 bfd_vma offset = rel->r_offset;
2553 Elf_Internal_Sym *sym;
2554 asection *sec;
2555 const char *sym_name;
2556 bfd_reloc_status_type r;
2557 const char *errmsg = NULL;
2558 bfd_boolean use_rel = FALSE;
2560 h = NULL;
2561 r_type = ELF32_R_TYPE (rel->r_info);
2562 if (r_type < 0 || r_type >= (int) R_M32R_max)
2564 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2565 bfd_archive_filename (input_bfd),
2566 (int) r_type);
2567 bfd_set_error (bfd_error_bad_value);
2568 ret = FALSE;
2569 continue;
2572 if (r_type == R_M32R_GNU_VTENTRY
2573 || r_type == R_M32R_GNU_VTINHERIT
2574 || r_type == R_M32R_NONE
2575 || r_type == R_M32R_RELA_GNU_VTENTRY
2576 || r_type == R_M32R_RELA_GNU_VTINHERIT)
2577 continue;
2579 if (r_type <= R_M32R_GNU_VTENTRY)
2580 use_rel = TRUE;
2582 howto = m32r_elf_howto_table + r_type;
2583 r_symndx = ELF32_R_SYM (rel->r_info);
2585 if (info->relocatable && (use_rel == TRUE))
2587 /* This is a relocatable link. We don't have to change
2588 anything, unless the reloc is against a section symbol,
2589 in which case we have to adjust according to where the
2590 section symbol winds up in the output section. */
2591 sec = NULL;
2592 if (r_symndx >= symtab_hdr->sh_info)
2594 /* External symbol. */
2595 continue;
2598 /* Local symbol. */
2599 sym = local_syms + r_symndx;
2600 sym_name = "<local symbol>";
2601 /* STT_SECTION: symbol is associated with a section. */
2602 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2604 /* Symbol isn't associated with a section. Nothing to do. */
2605 continue;
2608 sec = local_sections[r_symndx];
2609 addend += sec->output_offset + sym->st_value;
2611 /* If partial_inplace, we need to store any additional addend
2612 back in the section. */
2613 if (! howto->partial_inplace)
2614 continue;
2615 /* ??? Here is a nice place to call a special_function
2616 like handler. */
2617 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2618 r = _bfd_relocate_contents (howto, input_bfd,
2619 addend, contents + offset);
2620 else
2622 Elf_Internal_Rela *lorel;
2624 /* We allow an arbitrary number of HI16 relocs before the
2625 LO16 reloc. This permits gcc to emit the HI and LO relocs
2626 itself. */
2627 for (lorel = rel + 1;
2628 (lorel < relend
2629 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2630 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2631 lorel++)
2632 continue;
2633 if (lorel < relend
2634 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2636 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2637 contents, addend);
2638 r = bfd_reloc_ok;
2640 else
2641 r = _bfd_relocate_contents (howto, input_bfd,
2642 addend, contents + offset);
2645 else
2647 bfd_vma relocation;
2649 /* This is a final link. */
2650 sym = NULL;
2651 sec = NULL;
2652 h = NULL;
2654 if (r_symndx < symtab_hdr->sh_info)
2656 /* Local symbol. */
2657 sym = local_syms + r_symndx;
2658 sec = local_sections[r_symndx];
2659 sym_name = "<local symbol>";
2661 if (use_rel == FALSE)
2663 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2664 addend = rel->r_addend;
2666 if (info->relocatable)
2668 /* This is a relocatable link. We don't have to change
2669 anything, unless the reloc is against a section symbol,
2670 in which case we have to adjust according to where the
2671 section symbol winds up in the output section. */
2672 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2673 rel->r_addend += sec->output_offset + sym->st_value;
2675 continue;
2678 else
2680 relocation = (sec->output_section->vma
2681 + sec->output_offset
2682 + sym->st_value);
2685 else
2687 /* External symbol. */
2688 if (info->relocatable && (use_rel == FALSE))
2689 continue;
2691 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2692 while (h->root.type == bfd_link_hash_indirect
2693 || h->root.type == bfd_link_hash_warning)
2694 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2695 sym_name = h->root.root.string;
2697 if (h->root.type == bfd_link_hash_defined
2698 || h->root.type == bfd_link_hash_defweak)
2700 bfd_boolean dyn;
2701 sec = h->root.u.def.section;
2703 dyn = htab->root.dynamic_sections_created;
2704 sec = h->root.u.def.section;
2705 if (r_type == R_M32R_GOTPC24
2706 || (r_type == R_M32R_GOTPC_HI_ULO
2707 || r_type == R_M32R_GOTPC_HI_SLO
2708 || r_type == R_M32R_GOTPC_LO)
2709 || (r_type == R_M32R_26_PLTREL
2710 && h->plt.offset != (bfd_vma) -1)
2711 || ((r_type == R_M32R_GOT24
2712 || r_type == R_M32R_GOT16_HI_ULO
2713 || r_type == R_M32R_GOT16_HI_SLO
2714 || r_type == R_M32R_GOT16_LO)
2715 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2716 info->shared, h)
2717 && (! info->shared
2718 || (! info->symbolic && h->dynindx != -1)
2719 || (h->elf_link_hash_flags
2720 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2721 || (info->shared
2722 && ((! info->symbolic && h->dynindx != -1)
2723 || (h->elf_link_hash_flags
2724 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2725 && (((r_type == R_M32R_16_RELA
2726 || r_type == R_M32R_32_RELA
2727 || r_type == R_M32R_24_RELA
2728 || r_type == R_M32R_HI16_ULO_RELA
2729 || r_type == R_M32R_HI16_SLO_RELA
2730 || r_type == R_M32R_LO16_RELA)
2731 && (h->elf_link_hash_flags
2732 & ELF_LINK_FORCED_LOCAL) == 0)
2733 || r_type == R_M32R_10_PCREL_RELA
2734 || r_type == R_M32R_18_PCREL_RELA
2735 || r_type == R_M32R_26_PCREL_RELA)
2736 && ((input_section->flags & SEC_ALLOC) != 0
2737 /* DWARF will emit R_M32R_16(24,32) relocations
2738 in its sections against symbols defined
2739 externally in shared libraries. We can't do
2740 anything with them here. */
2741 || ((input_section->flags & SEC_DEBUGGING) != 0
2742 && (h->elf_link_hash_flags
2743 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
2745 /* In these cases, we don't need the relocation
2746 value. We check specially because in some
2747 obscure cases sec->output_section will be NULL. */
2748 relocation = 0;
2750 else if (sec->output_section == NULL)
2752 (*_bfd_error_handler)
2753 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2754 bfd_get_filename (input_bfd), h->root.root.string,
2755 bfd_get_section_name (input_bfd, input_section));
2757 relocation = 0;
2759 else
2760 relocation = (h->root.u.def.value
2761 + sec->output_section->vma
2762 + sec->output_offset);
2764 else if (h->root.type == bfd_link_hash_undefweak)
2765 relocation = 0;
2766 else if (info->unresolved_syms_in_objects == RM_IGNORE
2767 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2768 relocation = 0;
2769 else
2771 if (! ((*info->callbacks->undefined_symbol)
2772 (info, h->root.root.string, input_bfd,
2773 input_section, offset,
2774 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2775 || ELF_ST_VISIBILITY (h->other)))))
2776 return FALSE;
2777 relocation = 0;
2781 /* Sanity check the address. */
2782 if (offset > input_section->_raw_size)
2784 r = bfd_reloc_outofrange;
2785 goto check_reloc;
2788 switch ((int) r_type)
2790 case R_M32R_GOTPC24:
2791 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2792 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2794 relocation = sgot->output_section->vma;
2795 break;
2797 case R_M32R_GOTPC_HI_ULO:
2798 case R_M32R_GOTPC_HI_SLO:
2799 case R_M32R_GOTPC_LO:
2801 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2802 bl .+4
2803 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2804 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2806 bl .+4
2807 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2808 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2810 relocation = sgot->output_section->vma;
2811 relocation -= (input_section->output_section->vma
2812 + input_section->output_offset
2813 + rel->r_offset);
2814 if ((r_type == R_M32R_GOTPC_HI_SLO)
2815 && ((relocation + rel->r_addend) & 0x8000))
2816 rel->r_addend += 0x10000;
2818 break;
2820 case R_M32R_GOT16_HI_ULO:
2821 case R_M32R_GOT16_HI_SLO:
2822 case R_M32R_GOT16_LO:
2823 /* Fall through. */
2824 case R_M32R_GOT24:
2825 /* Relocation is to the entry for this symbol in the global
2826 offset table. */
2827 BFD_ASSERT (sgot != NULL);
2829 if (h != NULL)
2831 bfd_boolean dyn;
2832 bfd_vma off;
2834 off = h->got.offset;
2835 BFD_ASSERT (off != (bfd_vma) -1);
2837 dyn = htab->root.dynamic_sections_created;
2838 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2839 || (info->shared
2840 && (info->symbolic
2841 || h->dynindx == -1
2842 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2843 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2845 /* This is actually a static link, or it is a
2846 -Bsymbolic link and the symbol is defined
2847 locally, or the symbol was forced to be local
2848 because of a version file. We must initialize
2849 this entry in the global offset table. Since the
2850 offset must always be a multiple of 4, we use the
2851 least significant bit to record whether we have
2852 initialized it already.
2854 When doing a dynamic link, we create a .rela.got
2855 relocation entry to initialize the value. This
2856 is done in the finish_dynamic_symbol routine. */
2857 if ((off & 1) != 0)
2858 off &= ~1;
2859 else
2861 bfd_put_32 (output_bfd, relocation,
2862 sgot->contents + off);
2863 h->got.offset |= 1;
2867 relocation = sgot->output_offset + off;
2869 else
2871 bfd_vma off;
2872 bfd_byte *loc;
2874 BFD_ASSERT (local_got_offsets != NULL
2875 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2877 off = local_got_offsets[r_symndx];
2879 /* The offset must always be a multiple of 4. We use
2880 the least significant bit to record whether we have
2881 already processed this entry. */
2882 if ((off & 1) != 0)
2883 off &= ~1;
2884 else
2886 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2888 if (info->shared)
2890 asection *srelgot;
2891 Elf_Internal_Rela outrel;
2893 /* We need to generate a R_M32R_RELATIVE reloc
2894 for the dynamic linker. */
2895 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2896 BFD_ASSERT (srelgot != NULL);
2898 outrel.r_offset = (sgot->output_section->vma
2899 + sgot->output_offset
2900 + off);
2901 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2902 outrel.r_addend = relocation;
2903 loc = srelgot->contents;
2904 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2905 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2906 ++srelgot->reloc_count;
2909 local_got_offsets[r_symndx] |= 1;
2912 relocation = sgot->output_offset + off;
2914 if ((r_type == R_M32R_GOT16_HI_SLO)
2915 && ((relocation + rel->r_addend) & 0x8000))
2916 rel->r_addend += 0x10000;
2918 break;
2920 case R_M32R_26_PLTREL:
2921 /* Relocation is to the entry for this symbol in the
2922 procedure linkage table. */
2924 /* The native assembler will generate a 26_PLTREL reloc
2925 for a local symbol if you assemble a call from one
2926 section to another when using -K pic. */
2927 if (h == NULL)
2928 break;
2930 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2931 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2932 // break;
2933 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2934 break;
2936 if (h->plt.offset == (bfd_vma) -1)
2938 /* We didn't make a PLT entry for this symbol. This
2939 happens when statically linking PIC code, or when
2940 using -Bsymbolic. */
2941 break;
2944 relocation = (splt->output_section->vma
2945 + splt->output_offset
2946 + h->plt.offset);
2947 break;
2949 case R_M32R_HI16_SLO_RELA:
2951 if ((relocation + rel->r_addend) & 0x8000)
2953 rel->r_addend += 0x10000;
2956 /* Fall through. */
2957 case R_M32R_16_RELA:
2958 case R_M32R_24_RELA:
2959 case R_M32R_32_RELA:
2960 case R_M32R_18_PCREL_RELA:
2961 case R_M32R_26_PCREL_RELA:
2962 case R_M32R_HI16_ULO_RELA:
2963 case R_M32R_LO16_RELA:
2964 case R_M32R_SDA16_RELA:
2965 if (info->shared
2966 && r_symndx != 0
2967 && (input_section->flags & SEC_ALLOC) != 0
2968 && ((r_type != R_M32R_18_PCREL_RELA
2969 && r_type != R_M32R_26_PCREL_RELA)
2970 || (h != NULL
2971 && h->dynindx != -1
2972 && (! info->symbolic
2973 || (h->elf_link_hash_flags
2974 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2976 Elf_Internal_Rela outrel;
2977 bfd_boolean skip, relocate;
2978 bfd_byte *loc;
2980 /* When generating a shared object, these relocations
2981 are copied into the output file to be resolved at run
2982 time. */
2984 if (sreloc == NULL)
2986 const char *name;
2988 name = (bfd_elf_string_from_elf_section
2989 (input_bfd,
2990 elf_elfheader (input_bfd)->e_shstrndx,
2991 elf_section_data (input_section)->rel_hdr.sh_name));
2992 if (name == NULL)
2993 return FALSE;
2995 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2996 && strcmp (bfd_get_section_name (input_bfd,
2997 input_section),
2998 name + 5) == 0);
3000 sreloc = bfd_get_section_by_name (dynobj, name);
3001 BFD_ASSERT (sreloc != NULL);
3004 skip = FALSE;
3005 relocate = FALSE;
3007 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3008 info,
3009 input_section,
3010 rel->r_offset);
3011 if (outrel.r_offset == (bfd_vma) -1)
3012 skip = TRUE;
3013 else if (outrel.r_offset == (bfd_vma) -2)
3014 skip = TRUE, relocate = TRUE;
3015 outrel.r_offset += (input_section->output_section->vma
3016 + input_section->output_offset);
3018 if (skip)
3019 memset (&outrel, 0, sizeof outrel);
3020 else if (r_type == R_M32R_18_PCREL_RELA
3021 || r_type == R_M32R_26_PCREL_RELA)
3023 BFD_ASSERT (h != NULL && h->dynindx != -1);
3024 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3025 outrel.r_addend = rel->r_addend;
3027 else
3029 /* h->dynindx may be -1 if this symbol was marked to
3030 become local. */
3031 if (h == NULL
3032 || ((info->symbolic || h->dynindx == -1)
3033 && (h->elf_link_hash_flags
3034 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3036 relocate = TRUE;
3037 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3038 outrel.r_addend = relocation + rel->r_addend;
3040 else
3042 BFD_ASSERT (h->dynindx != -1);
3043 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3044 outrel.r_addend = relocation + rel->r_addend;
3048 loc = sreloc->contents;
3049 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3050 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3051 ++sreloc->reloc_count;
3053 /* If this reloc is against an external symbol, we do
3054 not want to fiddle with the addend. Otherwise, we
3055 need to include the symbol value so that it becomes
3056 an addend for the dynamic reloc. */
3057 if (! relocate)
3058 continue;
3060 break;
3062 case (int) R_M32R_10_PCREL :
3063 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3064 contents, offset,
3065 sec, relocation, addend);
3066 goto check_reloc;
3068 case (int) R_M32R_HI16_SLO :
3069 case (int) R_M32R_HI16_ULO :
3071 Elf_Internal_Rela *lorel;
3073 /* We allow an arbitrary number of HI16 relocs before the
3074 LO16 reloc. This permits gcc to emit the HI and LO relocs
3075 itself. */
3076 for (lorel = rel + 1;
3077 (lorel < relend
3078 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3079 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3080 lorel++)
3081 continue;
3082 if (lorel < relend
3083 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3085 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3086 contents, relocation + addend);
3087 r = bfd_reloc_ok;
3089 else
3090 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3091 contents, offset,
3092 relocation, addend);
3095 goto check_reloc;
3097 case (int) R_M32R_SDA16 :
3099 const char *name;
3101 BFD_ASSERT (sec != NULL);
3102 name = bfd_get_section_name (abfd, sec);
3104 if (strcmp (name, ".sdata") == 0
3105 || strcmp (name, ".sbss") == 0
3106 || strcmp (name, ".scommon") == 0)
3108 bfd_vma sda_base;
3109 bfd *out_bfd = sec->output_section->owner;
3111 r = m32r_elf_final_sda_base (out_bfd, info,
3112 &errmsg,
3113 &sda_base);
3114 if (r != bfd_reloc_ok)
3116 ret = FALSE;
3117 goto check_reloc;
3120 /* At this point `relocation' contains the object's
3121 address. */
3122 relocation -= sda_base;
3123 /* Now it contains the offset from _SDA_BASE_. */
3125 else
3127 (*_bfd_error_handler)
3128 (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
3129 bfd_archive_filename (input_bfd),
3130 sym_name,
3131 m32r_elf_howto_table[(int) r_type].name,
3132 bfd_get_section_name (abfd, sec));
3133 /*bfd_set_error (bfd_error_bad_value); ??? why? */
3134 ret = FALSE;
3135 continue;
3138 /* fall through */
3140 default : /* OLD_M32R_RELOC */
3142 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3143 contents, offset,
3144 relocation, addend);
3145 goto check_reloc;
3148 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3149 contents, rel->r_offset,
3150 relocation, rel->r_addend);
3154 check_reloc:
3156 if (r != bfd_reloc_ok)
3158 /* FIXME: This should be generic enough to go in a utility. */
3159 const char *name;
3161 if (h != NULL)
3162 name = h->root.root.string;
3163 else
3165 name = (bfd_elf_string_from_elf_section
3166 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3167 if (name == NULL || *name == '\0')
3168 name = bfd_section_name (input_bfd, sec);
3171 if (errmsg != NULL)
3172 goto common_error;
3174 switch (r)
3176 case bfd_reloc_overflow:
3177 if (! ((*info->callbacks->reloc_overflow)
3178 (info, name, howto->name, (bfd_vma) 0,
3179 input_bfd, input_section, offset)))
3180 return FALSE;
3181 break;
3183 case bfd_reloc_undefined:
3184 if (! ((*info->callbacks->undefined_symbol)
3185 (info, name, input_bfd, input_section,
3186 offset, TRUE)))
3187 return FALSE;
3188 break;
3190 case bfd_reloc_outofrange:
3191 errmsg = _("internal error: out of range error");
3192 goto common_error;
3194 case bfd_reloc_notsupported:
3195 errmsg = _("internal error: unsupported relocation error");
3196 goto common_error;
3198 case bfd_reloc_dangerous:
3199 errmsg = _("internal error: dangerous error");
3200 goto common_error;
3202 default:
3203 errmsg = _("internal error: unknown error");
3204 /* fall through */
3206 common_error:
3207 if (!((*info->callbacks->warning)
3208 (info, errmsg, name, input_bfd, input_section,
3209 offset)))
3210 return FALSE;
3211 break;
3216 return ret;
3219 /* Finish up dynamic symbol handling. We set the contents of various
3220 dynamic sections here. */
3221 static bfd_boolean
3222 m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3223 bfd *output_bfd;
3224 struct bfd_link_info *info;
3225 struct elf_link_hash_entry *h;
3226 Elf_Internal_Sym *sym;
3228 struct elf_m32r_link_hash_table *htab;
3229 bfd *dynobj;
3230 bfd_byte *loc;
3232 #ifdef DEBUG_PIC
3233 printf("m32r_elf_finish_dynamic_symbol()\n");
3234 #endif
3236 htab = m32r_elf_hash_table (info);
3237 dynobj = htab->root.dynobj;
3239 if (h->plt.offset != (bfd_vma) -1)
3241 asection *splt;
3242 asection *sgot;
3243 asection *srela;
3245 bfd_vma plt_index;
3246 bfd_vma got_offset;
3247 Elf_Internal_Rela rela;
3249 /* This symbol has an entry in the procedure linkage table. Set
3250 it up. */
3252 BFD_ASSERT (h->dynindx != -1);
3254 splt = htab->splt;
3255 sgot = htab->sgotplt;
3256 srela = htab->srelplt;
3257 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3259 /* Get the index in the procedure linkage table which
3260 corresponds to this symbol. This is the index of this symbol
3261 in all the symbols for which we are making plt entries. The
3262 first entry in the procedure linkage table is reserved. */
3263 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3265 /* Get the offset into the .got table of the entry that
3266 corresponds to this function. Each .got entry is 4 bytes.
3267 The first three are reserved. */
3268 got_offset = (plt_index + 3) * 4;
3270 /* Fill in the entry in the procedure linkage table. */
3271 if (! info->shared)
3273 bfd_put_32 (output_bfd,
3274 (PLT_ENTRY_WORD0b
3275 + (((sgot->output_section->vma
3276 + sgot->output_offset
3277 + got_offset) >> 16) & 0xffff)),
3278 splt->contents + h->plt.offset);
3279 bfd_put_32 (output_bfd,
3280 (PLT_ENTRY_WORD1b
3281 + ((sgot->output_section->vma
3282 + sgot->output_offset
3283 + got_offset) & 0xffff)),
3284 splt->contents + h->plt.offset + 4);
3285 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3286 splt->contents + h->plt.offset + 8);
3287 bfd_put_32 (output_bfd,
3288 (PLT_ENTRY_WORD3
3289 + plt_index * sizeof (Elf32_External_Rela)),
3290 splt->contents + h->plt.offset + 12);
3291 bfd_put_32 (output_bfd,
3292 (PLT_ENTRY_WORD4
3293 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3294 splt->contents + h->plt.offset + 16);
3296 else
3298 bfd_put_32 (output_bfd,
3299 PLT_ENTRY_WORD0 + got_offset,
3300 splt->contents + h->plt.offset);
3301 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3302 splt->contents + h->plt.offset + 4);
3303 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3304 splt->contents + h->plt.offset + 8);
3305 bfd_put_32 (output_bfd,
3306 (PLT_ENTRY_WORD3
3307 + plt_index * sizeof (Elf32_External_Rela)),
3308 splt->contents + h->plt.offset + 12);
3309 bfd_put_32 (output_bfd,
3310 (PLT_ENTRY_WORD4
3311 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3312 splt->contents + h->plt.offset + 16);
3315 /* Fill in the entry in the global offset table. */
3316 bfd_put_32 (output_bfd,
3317 (splt->output_section->vma
3318 + splt->output_offset
3319 + h->plt.offset
3320 + 12), /* same offset */
3321 sgot->contents + got_offset);
3323 /* Fill in the entry in the .rela.plt section. */
3324 rela.r_offset = (sgot->output_section->vma
3325 + sgot->output_offset
3326 + got_offset);
3327 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3328 rela.r_addend = 0;
3329 loc = srela->contents;
3330 loc += plt_index * sizeof(Elf32_External_Rela);
3331 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3333 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3335 /* Mark the symbol as undefined, rather than as defined in
3336 the .plt section. Leave the value alone. */
3337 sym->st_shndx = SHN_UNDEF;
3341 if (h->got.offset != (bfd_vma) -1)
3343 asection *sgot;
3344 asection *srela;
3345 Elf_Internal_Rela rela;
3347 /* This symbol has an entry in the global offset table. Set it
3348 up. */
3350 sgot = htab->sgot;
3351 srela = htab->srelgot;
3352 BFD_ASSERT (sgot != NULL && srela != NULL);
3354 rela.r_offset = (sgot->output_section->vma
3355 + sgot->output_offset
3356 + (h->got.offset &~ 1));
3358 /* If this is a -Bsymbolic link, and the symbol is defined
3359 locally, we just want to emit a RELATIVE reloc. Likewise if
3360 the symbol was forced to be local because of a version file.
3361 The entry in the global offset table will already have been
3362 initialized in the relocate_section function. */
3363 if (info->shared
3364 && (info->symbolic
3365 || h->dynindx == -1
3366 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3367 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3369 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3370 rela.r_addend = (h->root.u.def.value
3371 + h->root.u.def.section->output_section->vma
3372 + h->root.u.def.section->output_offset);
3374 else
3376 BFD_ASSERT((h->got.offset & 1) == 0);
3377 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3378 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3379 rela.r_addend = 0;
3382 loc = srela->contents;
3383 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3384 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3385 ++srela->reloc_count;
3388 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3390 asection *s;
3391 Elf_Internal_Rela rela;
3393 /* This symbols needs a copy reloc. Set it up. */
3395 BFD_ASSERT (h->dynindx != -1
3396 && (h->root.type == bfd_link_hash_defined
3397 || h->root.type == bfd_link_hash_defweak));
3399 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3400 ".rela.bss");
3401 BFD_ASSERT (s != NULL);
3403 rela.r_offset = (h->root.u.def.value
3404 + h->root.u.def.section->output_section->vma
3405 + h->root.u.def.section->output_offset);
3406 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3407 rela.r_addend = 0;
3408 loc = s->contents;
3409 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3410 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3411 ++s->reloc_count;
3414 /* Mark some specially defined symbols as absolute. */
3415 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3416 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3417 sym->st_shndx = SHN_ABS;
3419 return TRUE;
3423 /* Finish up the dynamic sections. */
3425 static bfd_boolean
3426 m32r_elf_finish_dynamic_sections (output_bfd, info)
3427 bfd *output_bfd;
3428 struct bfd_link_info *info;
3430 struct elf_m32r_link_hash_table *htab;
3431 bfd *dynobj;
3432 asection *sdyn;
3433 asection *sgot;
3435 #ifdef DEBUG_PIC
3436 printf("m32r_elf_finish_dynamic_sections()\n");
3437 #endif
3439 htab = m32r_elf_hash_table (info);
3440 dynobj = htab->root.dynobj;
3442 sgot = htab->sgotplt;
3443 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3445 if (htab->root.dynamic_sections_created)
3447 asection *splt;
3448 Elf32_External_Dyn *dyncon, *dynconend;
3450 BFD_ASSERT (sgot != NULL && sdyn != NULL);
3452 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3453 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3455 for (; dyncon < dynconend; dyncon++)
3457 Elf_Internal_Dyn dyn;
3458 const char *name;
3459 asection *s;
3461 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3463 switch (dyn.d_tag)
3465 default:
3466 break;
3468 case DT_PLTGOT:
3469 name = ".got";
3470 s = htab->sgot->output_section;
3471 goto get_vma;
3472 case DT_JMPREL:
3473 name = ".rela.plt";
3474 s = htab->srelplt->output_section;
3475 get_vma:
3476 BFD_ASSERT (s != NULL);
3477 dyn.d_un.d_ptr = s->vma;
3478 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3479 break;
3481 case DT_PLTRELSZ:
3482 s = htab->srelplt->output_section;
3483 BFD_ASSERT (s != NULL);
3484 if (s->_cooked_size != 0)
3485 dyn.d_un.d_val = s->_cooked_size;
3486 else
3487 dyn.d_un.d_val = s->_raw_size;
3488 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3489 break;
3491 case DT_RELASZ:
3492 /* My reading of the SVR4 ABI indicates that the
3493 procedure linkage table relocs (DT_JMPREL) should be
3494 included in the overall relocs (DT_RELA). This is
3495 what Solaris does. However, UnixWare can not handle
3496 that case. Therefore, we override the DT_RELASZ entry
3497 here to make it not include the JMPREL relocs. Since
3498 the linker script arranges for .rela.plt to follow all
3499 other relocation sections, we don't have to worry
3500 about changing the DT_RELA entry. */
3501 if (htab->srelplt != NULL)
3503 s = htab->srelplt->output_section;
3504 if (s->_cooked_size != 0)
3505 dyn.d_un.d_val -= s->_cooked_size;
3506 else
3507 dyn.d_un.d_val -= s->_raw_size;
3509 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3510 break;
3514 /* Fill in the first entry in the procedure linkage table. */
3515 splt = htab->splt;
3516 if (splt && splt->_raw_size > 0)
3518 if (info->shared)
3520 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3521 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3522 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3523 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3524 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3526 else
3528 unsigned long addr;
3529 /* addr = .got + 4 */
3530 addr = sgot->output_section->vma + sgot->output_offset + 4;
3531 bfd_put_32 (output_bfd,
3532 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3533 splt->contents);
3534 bfd_put_32 (output_bfd,
3535 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3536 splt->contents + 4);
3537 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3538 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3539 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3542 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3543 PLT_ENTRY_SIZE;
3547 /* Fill in the first three entries in the global offset table. */
3548 if (sgot && sgot->_raw_size > 0)
3550 if (sdyn == NULL)
3551 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3552 else
3553 bfd_put_32 (output_bfd,
3554 sdyn->output_section->vma + sdyn->output_offset,
3555 sgot->contents);
3556 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3557 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3559 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3562 return TRUE;
3566 #if 0 /* relaxing not supported yet */
3568 /* This function handles relaxing for the m32r.
3569 Relaxing on the m32r is tricky because of instruction alignment
3570 requirements (4 byte instructions must be aligned on 4 byte boundaries).
3572 The following relaxing opportunities are handled:
3574 seth/add3/jl -> bl24 or bl8
3575 seth/add3 -> ld24
3577 It would be nice to handle bl24 -> bl8 but given:
3579 - 4 byte insns must be on 4 byte boundaries
3580 - branch instructions only branch to insns on 4 byte boundaries
3582 this isn't much of a win because the insn in the 2 "deleted" bytes
3583 must become a nop. With some complexity some real relaxation could be
3584 done but the frequency just wouldn't make it worth it; it's better to
3585 try to do all the code compaction one can elsewhere.
3586 When the chip supports parallel 16 bit insns, things may change.
3589 static bfd_boolean
3590 m32r_elf_relax_section (abfd, sec, link_info, again)
3591 bfd *abfd;
3592 asection *sec;
3593 struct bfd_link_info *link_info;
3594 bfd_boolean *again;
3596 Elf_Internal_Shdr *symtab_hdr;
3597 /* The Rela structures are used here because that's what
3598 _bfd_elf_link_read_relocs uses [for convenience - it sets the addend
3599 field to 0]. */
3600 Elf_Internal_Rela *internal_relocs = NULL;
3601 Elf_Internal_Rela *irel, *irelend;
3602 bfd_byte *contents = NULL;
3603 Elf_Internal_Sym *isymbuf = NULL;
3605 /* Assume nothing changes. */
3606 *again = FALSE;
3608 /* We don't have to do anything for a relocatable link, if
3609 this section does not have relocs, or if this is not a
3610 code section. */
3611 if (link_info->relocatable
3612 || (sec->flags & SEC_RELOC) == 0
3613 || sec->reloc_count == 0
3614 || (sec->flags & SEC_CODE) == 0
3615 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
3616 return TRUE;
3618 /* If this is the first time we have been called for this section,
3619 initialize the cooked size. */
3620 if (sec->_cooked_size == 0)
3621 sec->_cooked_size = sec->_raw_size;
3623 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3625 /* Get a copy of the native relocations. */
3626 internal_relocs = (_bfd_elf_link_read_relocs
3627 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3628 link_info->keep_memory));
3629 if (internal_relocs == NULL)
3630 goto error_return;
3632 /* Walk through them looking for relaxing opportunities. */
3633 irelend = internal_relocs + sec->reloc_count;
3634 for (irel = internal_relocs; irel < irelend; irel++)
3636 bfd_vma symval;
3638 /* If this isn't something that can be relaxed, then ignore
3639 this reloc. */
3640 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
3641 continue;
3643 /* Get the section contents if we haven't done so already. */
3644 if (contents == NULL)
3646 /* Get cached copy if it exists. */
3647 if (elf_section_data (sec)->this_hdr.contents != NULL)
3648 contents = elf_section_data (sec)->this_hdr.contents;
3649 else
3651 /* Go get them off disk. */
3652 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
3653 if (contents == NULL)
3654 goto error_return;
3656 if (! bfd_get_section_contents (abfd, sec, contents,
3657 (file_ptr) 0, sec->_raw_size))
3658 goto error_return;
3662 /* Read this BFD's local symbols if we haven't done so already. */
3663 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3665 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3666 if (isymbuf == NULL)
3667 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3668 symtab_hdr->sh_info, 0,
3669 NULL, NULL, NULL);
3670 if (isymbuf == NULL)
3671 goto error_return;
3674 /* Get the value of the symbol referred to by the reloc. */
3675 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3677 /* A local symbol. */
3678 Elf_Internal_Sym *isym;
3679 asection *sym_sec;
3681 isym = isymbuf + ELF32_R_SYM (irel->r_info),
3682 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3683 symval = (isym->st_value
3684 + sym_sec->output_section->vma
3685 + sym_sec->output_offset);
3687 else
3689 unsigned long indx;
3690 struct elf_link_hash_entry *h;
3692 /* An external symbol. */
3693 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3694 h = elf_sym_hashes (abfd)[indx];
3695 BFD_ASSERT (h != NULL);
3696 if (h->root.type != bfd_link_hash_defined
3697 && h->root.type != bfd_link_hash_defweak)
3699 /* This appears to be a reference to an undefined
3700 symbol. Just ignore it--it will be caught by the
3701 regular reloc processing. */
3702 continue;
3705 symval = (h->root.u.def.value
3706 + h->root.u.def.section->output_section->vma
3707 + h->root.u.def.section->output_offset);
3710 /* For simplicity of coding, we are going to modify the section
3711 contents, the section relocs, and the BFD symbol table. We
3712 must tell the rest of the code not to free up this
3713 information. It would be possible to instead create a table
3714 of changes which have to be made, as is done in coff-mips.c;
3715 that would be more work, but would require less memory when
3716 the linker is run. */
3718 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
3719 This sequence is generated by the compiler when compiling in
3720 32 bit mode. Also look for seth/add3 -> ld24. */
3722 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
3724 Elf_Internal_Rela *nrel;
3725 bfd_vma pc = (sec->output_section->vma + sec->output_offset
3726 + irel->r_offset);
3727 bfd_signed_vma pcrel_value = symval - pc;
3728 unsigned int code,reg;
3729 int addend,nop_p,bl8_p,to_delete;
3731 /* The tests are ordered so that we get out as quickly as possible
3732 if this isn't something we can relax, taking into account that
3733 we are looking for two separate possibilities (jl/ld24). */
3735 /* Do nothing if no room in the section for this to be what we're
3736 looking for. */
3737 if (irel->r_offset > sec->_cooked_size - 8)
3738 continue;
3740 /* Make sure the next relocation applies to the next
3741 instruction and that it's the add3's reloc. */
3742 nrel = irel + 1;
3743 if (nrel == irelend
3744 || irel->r_offset + 4 != nrel->r_offset
3745 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
3746 continue;
3748 /* See if the instructions are seth/add3. */
3749 /* FIXME: This is where macros from cgen can come in. */
3750 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
3751 if ((code & 0xf0ff) != 0xd0c0)
3752 continue; /* not seth rN,foo */
3753 reg = (code & 0x0f00) >> 8;
3754 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
3755 if (code != (0x80a0 | reg | (reg << 8)))
3756 continue; /* not add3 rN,rN,foo */
3758 /* At this point we've confirmed we have seth/add3. Now check
3759 whether the next insn is a jl, in which case try to change this
3760 to bl24 or bl8. */
3762 /* Ensure the branch target is in range.
3763 The bl24 instruction has a 24 bit operand which is the target
3764 address right shifted by 2, giving a signed range of 26 bits.
3765 Note that 4 bytes are added to the high value because the target
3766 will be at least 4 bytes closer if we can relax. It'll actually
3767 be 4 or 8 bytes closer, but we don't know which just yet and
3768 the difference isn't significant enough to worry about. */
3769 #if !USE_REL /* put in for learning purposes */
3770 pcrel_value += irel->r_addend;
3771 #else
3772 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
3773 pcrel_value += addend;
3774 #endif
3776 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
3777 /* Do nothing if no room in the section for this to be what we're
3778 looking for. */
3779 && (irel->r_offset <= sec->_cooked_size - 12)
3780 /* Ensure the next insn is "jl rN". */
3781 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
3782 code != (0x1ec0 | reg)))
3784 /* We can relax to bl24/bl8. */
3786 /* See if there's a nop following the jl.
3787 Also see if we can use a bl8 insn. */
3788 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
3789 nop_p = (code & 0x7fff) == NOP_INSN;
3790 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
3792 if (bl8_p)
3794 /* Change "seth rN,foo" to "bl8 foo || nop".
3795 We OR in CODE just in case it's not a nop (technically,
3796 CODE currently must be a nop, but for cleanness we
3797 allow it to be anything). */
3798 #if !USE_REL /* put in for learning purposes */
3799 code = 0x7e000000 | MAKE_PARALLEL (code);
3800 #else
3801 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
3802 #endif
3803 to_delete = 8;
3805 else
3807 /* Change the seth rN,foo to a bl24 foo. */
3808 #if !USE_REL /* put in for learning purposes */
3809 code = 0xfe000000;
3810 #else
3811 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
3812 #endif
3813 to_delete = nop_p ? 8 : 4;
3816 bfd_put_32 (abfd, code, contents + irel->r_offset);
3818 /* Set the new reloc type. */
3819 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3820 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
3822 /* Delete the add3 reloc by making it a null reloc. */
3823 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3824 R_M32R_NONE);
3826 else if (addend >= 0
3827 && symval + addend <= 0xffffff)
3829 /* We can relax to ld24. */
3831 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
3832 bfd_put_32 (abfd, code, contents + irel->r_offset);
3833 to_delete = 4;
3834 /* Tell the following code a nop filler isn't needed. */
3835 nop_p = 1;
3837 else
3839 /* Can't do anything here. */
3840 continue;
3843 /* Note that we've changed the relocs, section contents, etc. */
3844 elf_section_data (sec)->relocs = internal_relocs;
3845 elf_section_data (sec)->this_hdr.contents = contents;
3846 symtab_hdr->contents = (unsigned char *) isymbuf;
3848 /* Delete TO_DELETE bytes of data. */
3849 if (!m32r_elf_relax_delete_bytes (abfd, sec,
3850 irel->r_offset + 4, to_delete))
3851 goto error_return;
3853 /* Now that the following bytes have been moved into place, see if
3854 we need to replace the jl with a nop. This happens when we had
3855 to use a bl24 insn and the insn following the jl isn't a nop.
3856 Technically, this situation can't happen (since the insn can
3857 never be executed) but to be clean we do this. When the chip
3858 supports parallel 16 bit insns things may change.
3859 We don't need to do this in the case of relaxing to ld24,
3860 and the above code sets nop_p so this isn't done. */
3861 if (! nop_p && to_delete == 4)
3862 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
3864 /* That will change things, so we should relax again.
3865 Note that this is not required, and it may be slow. */
3866 *again = TRUE;
3868 continue;
3871 /* loop to try the next reloc */
3874 if (isymbuf != NULL
3875 && symtab_hdr->contents != (unsigned char *) isymbuf)
3877 if (! link_info->keep_memory)
3878 free (isymbuf);
3879 else
3881 /* Cache the symbols for elf_link_input_bfd. */
3882 symtab_hdr->contents = (unsigned char *) isymbuf;
3886 if (contents != NULL
3887 && elf_section_data (sec)->this_hdr.contents != contents)
3889 if (! link_info->keep_memory)
3890 free (contents);
3891 else
3893 /* Cache the section contents for elf_link_input_bfd. */
3894 elf_section_data (sec)->this_hdr.contents = contents;
3898 if (internal_relocs != NULL
3899 && elf_section_data (sec)->relocs != internal_relocs)
3900 free (internal_relocs);
3902 return TRUE;
3904 error_return:
3905 if (isymbuf != NULL
3906 && symtab_hdr->contents != (unsigned char *) isymbuf)
3907 free (isymbuf);
3908 if (contents != NULL
3909 && elf_section_data (sec)->this_hdr.contents != contents)
3910 free (contents);
3911 if (internal_relocs != NULL
3912 && elf_section_data (sec)->relocs != internal_relocs)
3913 free (internal_relocs);
3915 return FALSE;
3918 /* Delete some bytes from a section while relaxing. */
3920 static bfd_boolean
3921 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
3922 bfd *abfd;
3923 asection *sec;
3924 bfd_vma addr;
3925 int count;
3927 Elf_Internal_Shdr *symtab_hdr;
3928 int shndx;
3929 bfd_byte *contents;
3930 Elf_Internal_Rela *irel, *irelend;
3931 Elf_Internal_Rela *irelalign;
3932 bfd_vma toaddr;
3933 Elf_Internal_Sym *isym, *isymend;
3934 struct elf_link_hash_entry **sym_hashes;
3935 struct elf_link_hash_entry **end_hashes;
3936 unsigned int symcount;
3938 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3940 contents = elf_section_data (sec)->this_hdr.contents;
3942 /* The deletion must stop at the next ALIGN reloc for an aligment
3943 power larger than the number of bytes we are deleting. */
3945 irelalign = NULL;
3946 toaddr = sec->_cooked_size;
3948 irel = elf_section_data (sec)->relocs;
3949 irelend = irel + sec->reloc_count;
3951 /* Actually delete the bytes. */
3952 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
3953 sec->_cooked_size -= count;
3955 /* Adjust all the relocs. */
3956 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3958 /* Get the new reloc address. */
3959 if ((irel->r_offset > addr
3960 && irel->r_offset < toaddr))
3961 irel->r_offset -= count;
3964 /* Adjust the local symbols defined in this section. */
3965 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3966 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3967 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3969 if (isym->st_shndx == shndx
3970 && isym->st_value > addr
3971 && isym->st_value < toaddr)
3972 isym->st_value -= count;
3975 /* Now adjust the global symbols defined in this section. */
3976 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3977 - symtab_hdr->sh_info);
3978 sym_hashes = elf_sym_hashes (abfd);
3979 end_hashes = sym_hashes + symcount;
3980 for (; sym_hashes < end_hashes; sym_hashes++)
3982 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3984 if ((sym_hash->root.type == bfd_link_hash_defined
3985 || sym_hash->root.type == bfd_link_hash_defweak)
3986 && sym_hash->root.u.def.section == sec
3987 && sym_hash->root.u.def.value > addr
3988 && sym_hash->root.u.def.value < toaddr)
3990 sym_hash->root.u.def.value -= count;
3994 return TRUE;
3997 /* This is a version of bfd_generic_get_relocated_section_contents
3998 which uses m32r_elf_relocate_section. */
4000 static bfd_byte *
4001 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
4002 data, relocatable, symbols)
4003 bfd *output_bfd;
4004 struct bfd_link_info *link_info;
4005 struct bfd_link_order *link_order;
4006 bfd_byte *data;
4007 bfd_boolean relocatable;
4008 asymbol **symbols;
4010 Elf_Internal_Shdr *symtab_hdr;
4011 asection *input_section = link_order->u.indirect.section;
4012 bfd *input_bfd = input_section->owner;
4013 asection **sections = NULL;
4014 Elf_Internal_Rela *internal_relocs = NULL;
4015 Elf_Internal_Sym *isymbuf = NULL;
4016 bfd_size_type amt;
4018 /* We only need to handle the case of relaxing, or of having a
4019 particular set of section contents, specially. */
4020 if (relocatable
4021 || elf_section_data (input_section)->this_hdr.contents == NULL)
4022 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4023 link_order, data,
4024 relocatable,
4025 symbols);
4027 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4029 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4030 input_section->_raw_size);
4032 if ((input_section->flags & SEC_RELOC) != 0
4033 && input_section->reloc_count > 0)
4035 Elf_Internal_Sym *isymp;
4036 asection **secpp;
4037 Elf32_External_Sym *esym, *esymend;
4039 internal_relocs = (_bfd_elf_link_read_relocs
4040 (input_bfd, input_section, (PTR) NULL,
4041 (Elf_Internal_Rela *) NULL, FALSE));
4042 if (internal_relocs == NULL)
4043 goto error_return;
4045 if (symtab_hdr->sh_info != 0)
4047 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4048 if (isymbuf == NULL)
4049 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4050 symtab_hdr->sh_info, 0,
4051 NULL, NULL, NULL);
4052 if (isymbuf == NULL)
4053 goto error_return;
4056 amt = symtab_hdr->sh_info;
4057 amt *= sizeof (asection *);
4058 sections = (asection **) bfd_malloc (amt);
4059 if (sections == NULL && symtab_hdr->sh_info > 0)
4060 goto error_return;
4062 isymend = isymbuf + symtab_hdr->sh_info;
4063 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4065 asection *isec;
4067 if (isym->st_shndx == SHN_UNDEF)
4068 isec = bfd_und_section_ptr;
4069 else if (isym->st_shndx == SHN_ABS)
4070 isec = bfd_abs_section_ptr;
4071 else if (isym->st_shndx == SHN_COMMON)
4072 isec = bfd_com_section_ptr;
4073 else if (isym->st_shndx == SHN_M32R_SCOMMON)
4074 isec = &m32r_elf_scom_section;
4075 else
4076 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4078 *secpp = isec;
4081 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
4082 input_section, data, internal_relocs,
4083 isymbuf, sections))
4084 goto error_return;
4086 if (sections != NULL)
4087 free (sections);
4088 if (isymbuf != NULL
4089 && symtab_hdr->contents != (unsigned char *) isymbuf)
4090 free (isymbuf);
4091 if (elf_section_data (input_section)->relocs != internal_relocs)
4092 free (internal_relocs);
4095 return data;
4097 error_return:
4098 if (sections != NULL)
4099 free (sections);
4100 if (isymbuf != NULL
4101 && symtab_hdr->contents != (unsigned char *) isymbuf)
4102 free (isymbuf);
4103 if (internal_relocs != NULL
4104 && elf_section_data (input_section)->relocs != internal_relocs)
4105 free (internal_relocs);
4106 return NULL;
4109 #endif /* #if 0 */
4111 /* Set the right machine number. */
4112 static bfd_boolean
4113 m32r_elf_object_p (abfd)
4114 bfd *abfd;
4116 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4118 default:
4119 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
4120 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
4121 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
4123 return TRUE;
4126 /* Store the machine number in the flags field. */
4127 static void
4128 m32r_elf_final_write_processing (abfd, linker)
4129 bfd *abfd;
4130 bfd_boolean linker ATTRIBUTE_UNUSED;
4132 unsigned long val;
4134 switch (bfd_get_mach (abfd))
4136 default:
4137 case bfd_mach_m32r: val = E_M32R_ARCH; break;
4138 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
4139 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
4142 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
4143 elf_elfheader (abfd)->e_flags |= val;
4146 /* Function to keep M32R specific file flags. */
4147 static bfd_boolean
4148 m32r_elf_set_private_flags (abfd, flags)
4149 bfd *abfd;
4150 flagword flags;
4152 BFD_ASSERT (!elf_flags_init (abfd)
4153 || elf_elfheader (abfd)->e_flags == flags);
4155 elf_elfheader (abfd)->e_flags = flags;
4156 elf_flags_init (abfd) = TRUE;
4157 return TRUE;
4160 /* Merge backend specific data from an object file to the output
4161 object file when linking. */
4162 static bfd_boolean
4163 m32r_elf_merge_private_bfd_data (ibfd, obfd)
4164 bfd *ibfd;
4165 bfd *obfd;
4167 flagword out_flags;
4168 flagword in_flags;
4170 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4171 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4172 return TRUE;
4174 in_flags = elf_elfheader (ibfd)->e_flags;
4175 out_flags = elf_elfheader (obfd)->e_flags;
4177 if (! elf_flags_init (obfd))
4179 /* If the input is the default architecture then do not
4180 bother setting the flags for the output architecture,
4181 instead allow future merges to do this. If no future
4182 merges ever set these flags then they will retain their
4183 unitialised values, which surprise surprise, correspond
4184 to the default values. */
4185 if (bfd_get_arch_info (ibfd)->the_default)
4186 return TRUE;
4188 elf_flags_init (obfd) = TRUE;
4189 elf_elfheader (obfd)->e_flags = in_flags;
4191 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4192 && bfd_get_arch_info (obfd)->the_default)
4194 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
4197 return TRUE;
4200 /* Check flag compatibility. */
4201 if (in_flags == out_flags)
4202 return TRUE;
4204 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
4206 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
4207 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4208 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
4210 (*_bfd_error_handler)
4211 (_("%s: Instruction set mismatch with previous modules"),
4212 bfd_archive_filename (ibfd));
4214 bfd_set_error (bfd_error_bad_value);
4215 return FALSE;
4219 return TRUE;
4222 /* Display the flags field */
4223 static bfd_boolean
4224 m32r_elf_print_private_bfd_data (abfd, ptr)
4225 bfd *abfd;
4226 PTR ptr;
4228 FILE * file = (FILE *) ptr;
4230 BFD_ASSERT (abfd != NULL && ptr != NULL)
4232 _bfd_elf_print_private_bfd_data (abfd, ptr);
4234 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
4236 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4238 default:
4239 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
4240 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
4241 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
4244 fputc ('\n', file);
4246 return TRUE;
4249 asection *
4250 m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
4251 asection *sec;
4252 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4253 Elf_Internal_Rela *rel;
4254 struct elf_link_hash_entry *h;
4255 Elf_Internal_Sym *sym;
4257 if (h != NULL)
4259 switch (ELF32_R_TYPE (rel->r_info))
4261 case R_M32R_GNU_VTINHERIT:
4262 case R_M32R_GNU_VTENTRY:
4263 case R_M32R_RELA_GNU_VTINHERIT:
4264 case R_M32R_RELA_GNU_VTENTRY:
4265 break;
4267 default:
4268 switch (h->root.type)
4270 case bfd_link_hash_defined:
4271 case bfd_link_hash_defweak:
4272 return h->root.u.def.section;
4274 case bfd_link_hash_common:
4275 return h->root.u.c.p->section;
4277 default:
4278 break;
4282 else
4283 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4285 return NULL;
4288 static bfd_boolean
4289 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
4290 bfd *abfd ATTRIBUTE_UNUSED;
4291 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4292 asection *sec ATTRIBUTE_UNUSED;
4293 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
4295 /* Update the got entry reference counts for the section being removed. */
4296 Elf_Internal_Shdr *symtab_hdr;
4297 struct elf_link_hash_entry **sym_hashes;
4298 bfd_signed_vma *local_got_refcounts;
4299 const Elf_Internal_Rela *rel, *relend;
4300 unsigned long r_symndx;
4301 struct elf_link_hash_entry *h;
4303 elf_section_data (sec)->local_dynrel = NULL;
4305 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4306 sym_hashes = elf_sym_hashes (abfd);
4307 local_got_refcounts = elf_local_got_refcounts (abfd);
4309 relend = relocs + sec->reloc_count;
4310 for (rel = relocs; rel < relend; rel++)
4311 switch (ELF32_R_TYPE (rel->r_info))
4313 case R_M32R_GOT16_HI_ULO:
4314 case R_M32R_GOT16_HI_SLO:
4315 case R_M32R_GOT16_LO:
4316 case R_M32R_GOT24:
4317 case R_M32R_GOTPC_HI_ULO:
4318 case R_M32R_GOTPC_HI_SLO:
4319 case R_M32R_GOTPC_LO:
4320 case R_M32R_GOTPC24:
4321 r_symndx = ELF32_R_SYM (rel->r_info);
4322 if (r_symndx >= symtab_hdr->sh_info)
4324 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4325 if (h->got.refcount > 0)
4326 h->got.refcount--;
4328 else
4330 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
4331 local_got_refcounts[r_symndx]--;
4333 break;
4335 case R_M32R_16_RELA:
4336 case R_M32R_24_RELA:
4337 case R_M32R_32_RELA:
4338 case R_M32R_HI16_ULO_RELA:
4339 case R_M32R_HI16_SLO_RELA:
4340 case R_M32R_LO16_RELA:
4341 case R_M32R_SDA16_RELA:
4342 case R_M32R_18_PCREL_RELA:
4343 case R_M32R_26_PCREL_RELA:
4344 r_symndx = ELF32_R_SYM (rel->r_info);
4345 if (r_symndx >= symtab_hdr->sh_info)
4347 struct elf_m32r_link_hash_entry *eh;
4348 struct elf_m32r_dyn_relocs **pp;
4349 struct elf_m32r_dyn_relocs *p;
4351 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4353 if (!info->shared && h->plt.refcount > 0)
4354 h->plt.refcount -= 1;
4356 eh = (struct elf_m32r_link_hash_entry *) h;
4358 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4359 if (p->sec == sec)
4361 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4362 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
4363 p->pc_count -= 1;
4364 p->count -= 1;
4365 if (p->count == 0)
4366 *pp = p->next;
4367 break;
4370 break;
4372 case R_M32R_26_PLTREL:
4373 r_symndx = ELF32_R_SYM (rel->r_info);
4374 if (r_symndx >= symtab_hdr->sh_info)
4376 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4377 if (h->plt.refcount > 0)
4378 h->plt.refcount--;
4380 break;
4382 default:
4383 break;
4386 return TRUE;
4389 /* Look through the relocs for a section during the first phase.
4390 Since we don't do .gots or .plts, we just need to consider the
4391 virtual table relocs for gc. */
4393 static bfd_boolean
4394 m32r_elf_check_relocs (abfd, info, sec, relocs)
4395 bfd *abfd;
4396 struct bfd_link_info *info;
4397 asection *sec;
4398 const Elf_Internal_Rela *relocs;
4400 Elf_Internal_Shdr *symtab_hdr;
4401 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4402 const Elf_Internal_Rela *rel;
4403 const Elf_Internal_Rela *rel_end;
4404 struct elf_m32r_link_hash_table *htab;
4405 bfd *dynobj;
4406 bfd_vma *local_got_offsets;
4407 asection *sgot, *srelgot, *sreloc;
4409 if (info->relocatable)
4410 return TRUE;
4412 sgot = srelgot = sreloc = NULL;
4414 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4415 sym_hashes = elf_sym_hashes (abfd);
4416 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
4417 if (!elf_bad_symtab (abfd))
4418 sym_hashes_end -= symtab_hdr->sh_info;
4420 htab = m32r_elf_hash_table (info);
4421 dynobj = htab->root.dynobj;
4422 local_got_offsets = elf_local_got_offsets (abfd);
4424 rel_end = relocs + sec->reloc_count;
4425 for (rel = relocs; rel < rel_end; rel++)
4427 int r_type;
4428 struct elf_link_hash_entry *h;
4429 unsigned long r_symndx;
4431 r_symndx = ELF32_R_SYM (rel->r_info);
4432 r_type = ELF32_R_TYPE (rel->r_info);
4433 if (r_symndx < symtab_hdr->sh_info)
4434 h = NULL;
4435 else
4436 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4438 /* Some relocs require a global offset table. */
4439 if (htab->sgot == NULL)
4441 switch (r_type)
4443 case R_M32R_GOT16_HI_ULO:
4444 case R_M32R_GOT16_HI_SLO:
4445 case R_M32R_GOT16_LO:
4446 case R_M32R_GOTPC24:
4447 case R_M32R_GOTPC_HI_ULO:
4448 case R_M32R_GOTPC_HI_SLO:
4449 case R_M32R_GOTPC_LO:
4450 case R_M32R_GOT24:
4451 if (dynobj == NULL)
4452 htab->root.dynobj = dynobj = abfd;
4453 if (! create_got_section (dynobj, info))
4454 return FALSE;
4455 break;
4457 default:
4458 break;
4462 switch (r_type)
4464 case R_M32R_GOT16_HI_ULO:
4465 case R_M32R_GOT16_HI_SLO:
4466 case R_M32R_GOT16_LO:
4467 case R_M32R_GOT24:
4469 if (h != NULL)
4470 h->got.refcount += 1;
4471 else
4473 bfd_signed_vma *local_got_refcounts;
4475 /* This is a global offset table entry for a local
4476 symbol. */
4477 local_got_refcounts = elf_local_got_refcounts (abfd);
4478 if (local_got_refcounts == NULL)
4480 bfd_size_type size;
4482 size = symtab_hdr->sh_info;
4483 size *= sizeof (bfd_signed_vma);
4484 local_got_refcounts = ((bfd_signed_vma *)
4485 bfd_zalloc (abfd, size));
4486 if (local_got_refcounts == NULL)
4487 return FALSE;
4488 elf_local_got_refcounts (abfd) = local_got_refcounts;
4490 local_got_refcounts[r_symndx] += 1;
4492 break;
4494 case R_M32R_26_PLTREL:
4495 /* This symbol requires a procedure linkage table entry. We
4496 actually build the entry in adjust_dynamic_symbol,
4497 because this might be a case of linking PIC code without
4498 linking in any dynamic objects, in which case we don't
4499 need to generate a procedure linkage table after all. */
4501 /* If this is a local symbol, we resolve it directly without
4502 creating a procedure linkage table entry. */
4503 if (h == NULL)
4504 continue;
4506 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4507 break;
4509 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4510 h->plt.refcount += 1;
4511 break;
4513 case R_M32R_16_RELA:
4514 case R_M32R_24_RELA:
4515 case R_M32R_32_RELA:
4516 case R_M32R_HI16_ULO_RELA:
4517 case R_M32R_HI16_SLO_RELA:
4518 case R_M32R_LO16_RELA:
4519 case R_M32R_SDA16_RELA:
4520 case R_M32R_18_PCREL_RELA:
4521 case R_M32R_26_PCREL_RELA:
4523 if (h != NULL && !info->shared)
4525 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
4526 h->plt.refcount += 1;
4529 /* If we are creating a shared library, and this is a reloc
4530 against a global symbol, or a non PC relative reloc
4531 against a local symbol, then we need to copy the reloc
4532 into the shared library. However, if we are linking with
4533 -Bsymbolic, we do not need to copy a reloc against a
4534 global symbol which is defined in an object we are
4535 including in the link (i.e., DEF_REGULAR is set). At
4536 this point we have not seen all the input files, so it is
4537 possible that DEF_REGULAR is not set now but will be set
4538 later (it is never cleared). We account for that
4539 possibility below by storing information in the
4540 dyn_relocs field of the hash table entry. A similar
4541 situation occurs when creating shared libraries and symbol
4542 visibility changes render the symbol local.
4544 If on the other hand, we are creating an executable, we
4545 may need to keep relocations for symbols satisfied by a
4546 dynamic library if we manage to avoid copy relocs for the
4547 symbol. */
4548 if ((info->shared
4549 && (sec->flags & SEC_ALLOC) != 0
4550 && ((r_type != R_M32R_26_PCREL_RELA
4551 && r_type != R_M32R_18_PCREL_RELA)
4552 || (h != NULL
4553 && (! info->symbolic
4554 || h->root.type == bfd_link_hash_defweak
4555 || (h->elf_link_hash_flags
4556 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4557 || (!info->shared
4558 && (sec->flags & SEC_ALLOC) != 0
4559 && h != NULL
4560 && (h->root.type == bfd_link_hash_defweak
4561 || (h->elf_link_hash_flags
4562 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
4564 struct elf_m32r_dyn_relocs *p;
4565 struct elf_m32r_dyn_relocs **head;
4567 if (dynobj == NULL)
4568 htab->root.dynobj = dynobj = abfd;
4570 /* When creating a shared object, we must copy these
4571 relocs into the output file. We create a reloc
4572 section in dynobj and make room for the reloc. */
4573 if (sreloc == NULL)
4575 const char *name;
4577 name = (bfd_elf_string_from_elf_section
4578 (abfd,
4579 elf_elfheader (abfd)->e_shstrndx,
4580 elf_section_data (sec)->rel_hdr.sh_name));
4581 if (name == NULL)
4582 return FALSE;
4584 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4585 && strcmp (bfd_get_section_name (abfd, sec),
4586 name + 5) == 0);
4588 sreloc = bfd_get_section_by_name (dynobj, name);
4589 if (sreloc == NULL)
4591 flagword flags;
4593 sreloc = bfd_make_section (dynobj, name);
4594 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4595 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4596 if ((sec->flags & SEC_ALLOC) != 0)
4597 flags |= SEC_ALLOC | SEC_LOAD;
4598 if (sreloc == NULL
4599 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4600 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4601 return FALSE;
4603 elf_section_data (sec)->sreloc = sreloc;
4606 /* If this is a global symbol, we count the number of
4607 relocations we need for this symbol. */
4608 if (h != NULL)
4609 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4610 else
4612 asection *s;
4614 /* Track dynamic relocs needed for local syms too. */
4615 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4616 sec, r_symndx);
4617 if (s == NULL)
4618 return FALSE;
4620 head = ((struct elf_m32r_dyn_relocs **)
4621 &elf_section_data (s)->local_dynrel);
4624 p = *head;
4625 if (p == NULL || p->sec != sec)
4627 bfd_size_type amt = sizeof (*p);
4628 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4629 if (p == NULL)
4630 return FALSE;
4631 p->next = *head;
4632 *head = p;
4633 p->sec = sec;
4634 p->count = 0;
4635 p->pc_count = 0;
4638 p->count += 1;
4639 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4640 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4641 p->pc_count += 1;
4643 break;
4645 /* This relocation describes the C++ object vtable hierarchy.
4646 Reconstruct it for later use during GC. */
4647 case R_M32R_RELA_GNU_VTINHERIT:
4648 case R_M32R_GNU_VTINHERIT:
4649 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4650 return FALSE;
4651 break;
4653 /* This relocation describes which C++ vtable entries are actually
4654 used. Record for later use during GC. */
4655 case R_M32R_GNU_VTENTRY:
4656 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4657 return FALSE;
4658 break;
4659 case R_M32R_RELA_GNU_VTENTRY:
4660 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4661 return FALSE;
4662 break;
4666 return TRUE;
4669 static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4671 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4672 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
4673 { NULL, 0, 0, 0, 0 }
4676 static bfd_boolean
4677 m32r_elf_fake_sections (abfd, hdr, sec)
4678 bfd *abfd;
4679 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4680 asection *sec;
4682 register const char *name;
4684 name = bfd_get_section_name (abfd, sec);
4686 /* The generic elf_fake_sections will set up REL_HDR using the
4687 default kind of relocations. But, we may actually need both
4688 kinds of relocations, so we set up the second header here.
4690 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4691 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4692 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4693 of the resulting empty .rela.<section> sections starts with
4694 sh_offset == object size, and ld doesn't allow that. While the check
4695 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4696 avoided by not emitting those useless sections in the first place. */
4697 if ((sec->flags & SEC_RELOC) != 0)
4699 struct bfd_elf_section_data *esd;
4700 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4702 esd = elf_section_data (sec);
4703 BFD_ASSERT (esd->rel_hdr2 == NULL);
4704 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4705 if (!esd->rel_hdr2)
4706 return FALSE;
4707 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4708 !sec->use_rela_p);
4711 return TRUE;
4714 static enum elf_reloc_type_class
4715 m32r_elf_reloc_type_class (rela)
4716 const Elf_Internal_Rela *rela;
4718 switch ((int) ELF32_R_TYPE (rela->r_info))
4720 case R_M32R_RELATIVE:
4721 return reloc_class_relative;
4722 case R_M32R_JMP_SLOT:
4723 return reloc_class_plt;
4724 case R_M32R_COPY:
4725 return reloc_class_copy;
4726 default:
4727 return reloc_class_normal;
4731 #define ELF_ARCH bfd_arch_m32r
4732 #define ELF_MACHINE_CODE EM_M32R
4733 #define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
4734 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
4736 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
4737 #define TARGET_BIG_NAME "elf32-m32r"
4738 #define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4739 #define TARGET_LITTLE_NAME "elf32-m32rle"
4741 #define elf_info_to_howto m32r_info_to_howto
4742 #define elf_info_to_howto_rel m32r_info_to_howto_rel
4743 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4744 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4745 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4746 #define elf_backend_relocate_section m32r_elf_relocate_section
4747 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4748 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4749 #define elf_backend_check_relocs m32r_elf_check_relocs
4751 #define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4752 #define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4753 #define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4754 #define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4755 #define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4756 #define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4757 #define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4758 #define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4760 #define elf_backend_can_gc_sections 1
4761 /*#if !USE_REL
4762 #define elf_backend_rela_normal 1
4763 #endif*/
4764 #define elf_backend_can_refcount 1
4765 #define elf_backend_want_got_plt 1
4766 #define elf_backend_plt_readonly 1
4767 #define elf_backend_want_plt_sym 0
4768 #define elf_backend_got_header_size 12
4770 #define elf_backend_may_use_rel_p 1
4771 #ifdef USE_M32R_OLD_RELOC
4772 #define elf_backend_default_use_rela_p 0
4773 #define elf_backend_may_use_rela_p 0
4774 #else
4775 #define elf_backend_default_use_rela_p 1
4776 #define elf_backend_may_use_rela_p 1
4777 #define elf_backend_fake_sections m32r_elf_fake_sections
4778 #endif
4780 #if 0 /* not yet */
4781 /* relax support */
4782 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
4783 #define bfd_elf32_bfd_get_relocated_section_contents \
4784 m32r_elf_get_relocated_section_contents
4785 #endif
4787 #define elf_backend_object_p m32r_elf_object_p
4788 #define elf_backend_final_write_processing m32r_elf_final_write_processing
4789 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4790 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4791 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
4792 #define elf_backend_special_sections m32r_elf_special_sections
4794 #include "elf32-target.h"
4796 #undef ELF_MAXPAGESIZE
4797 #define ELF_MAXPAGESIZE 0x1000
4799 #undef TARGET_BIG_SYM
4800 #define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4801 #undef TARGET_BIG_NAME
4802 #define TARGET_BIG_NAME "elf32-m32r-linux"
4803 #undef TARGET_LITTLE_SYM
4804 #define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4805 #undef TARGET_LITTLE_NAME
4806 #define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4807 #undef elf32_bed
4808 #define elf32_bed elf32_m32r_lin_bed
4810 #include "elf32-target.h"