Add support for M32R GOT relocs
[binutils.git] / bfd / elf32-m32r.c
blobc6bc2bee843487af5c45fbceb96fd9a6c77caf67
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 24, /* 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 0xffffff, /* src_mask */
689 0xffffff, /* 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 */
807 HOWTO (R_M32R_GOTOFF_HI_ULO, /* type */
808 16, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
815 "R_M32R_GOTOFF_HI_ULO",/* name */
816 FALSE, /* partial_inplace */
817 0x0000ffff, /* src_mask */
818 0x0000ffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
821 HOWTO (R_M32R_GOTOFF_HI_SLO, /* type */
822 16, /* rightshift */
823 2, /* size (0 = byte, 1 = short, 2 = long) */
824 16, /* bitsize */
825 FALSE, /* pc_relative */
826 0, /* bitpos */
827 complain_overflow_dont, /* complain_on_overflow */
828 bfd_elf_generic_reloc, /* special_function */
829 "R_M32R_GOTOFF_HI_SLO",/* name */
830 FALSE, /* partial_inplace */
831 0x0000ffff, /* src_mask */
832 0x0000ffff, /* dst_mask */
833 FALSE), /* pcrel_offset */
835 HOWTO (R_M32R_GOTOFF_LO, /* type */
836 0, /* rightshift */
837 2, /* size (0 = byte, 1 = short, 2 = long) */
838 16, /* bitsize */
839 FALSE, /* pc_relative */
840 0, /* bitpos */
841 complain_overflow_dont, /* complain_on_overflow */
842 bfd_elf_generic_reloc, /* special_function */
843 "R_M32R_GOTOFF_LO", /* name */
844 FALSE, /* partial_inplace */
845 0x0000ffff, /* src_mask */
846 0x0000ffff, /* dst_mask */
847 FALSE), /* pcrel_offset */
850 /* Handle the R_M32R_10_PCREL reloc. */
852 static bfd_reloc_status_type
853 m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
854 input_section, output_bfd, error_message)
855 bfd * abfd;
856 arelent * reloc_entry;
857 asymbol * symbol;
858 PTR data;
859 asection * input_section;
860 bfd * output_bfd;
861 char ** error_message ATTRIBUTE_UNUSED;
863 /* This part is from bfd_elf_generic_reloc. */
864 if (output_bfd != (bfd *) NULL
865 && (symbol->flags & BSF_SECTION_SYM) == 0
866 && (! reloc_entry->howto->partial_inplace
867 || reloc_entry->addend == 0))
869 reloc_entry->address += input_section->output_offset;
870 return bfd_reloc_ok;
873 if (output_bfd != NULL)
875 /* FIXME: See bfd_perform_relocation. Is this right? */
876 return bfd_reloc_continue;
879 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
880 input_section,
881 data, reloc_entry->address,
882 symbol->section,
883 (symbol->value
884 + symbol->section->output_section->vma
885 + symbol->section->output_offset),
886 reloc_entry->addend);
889 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
891 static bfd_reloc_status_type
892 m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
893 symbol_section, symbol_value, addend)
894 bfd *abfd;
895 reloc_howto_type *howto;
896 asection *input_section;
897 bfd_byte *data;
898 bfd_vma offset;
899 asection *symbol_section ATTRIBUTE_UNUSED;
900 bfd_vma symbol_value;
901 bfd_vma addend;
903 bfd_signed_vma relocation;
904 bfd_size_type sz;
905 unsigned long x;
906 bfd_reloc_status_type status;
908 /* Sanity check the address (offset in section). */
909 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
910 if (offset > sz)
911 return bfd_reloc_outofrange;
913 relocation = symbol_value + addend;
914 /* Make it pc relative. */
915 relocation -= (input_section->output_section->vma
916 + input_section->output_offset);
917 /* These jumps mask off the lower two bits of the current address
918 before doing pcrel calculations. */
919 relocation -= (offset & -(bfd_vma) 4);
921 if (relocation < -0x200 || relocation > 0x1ff)
922 status = bfd_reloc_overflow;
923 else
924 status = bfd_reloc_ok;
926 x = bfd_get_16 (abfd, data + offset);
927 relocation >>= howto->rightshift;
928 relocation <<= howto->bitpos;
929 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
930 bfd_put_16 (abfd, (bfd_vma) x, data + offset);
932 return status;
935 /* Handle the R_M32R_HI16_[SU]LO relocs.
936 HI16_SLO is for the add3 and load/store with displacement instructions.
937 HI16_ULO is for the or3 instruction.
938 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
939 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
940 we must add one to the high 16 bytes (which will get subtracted off when
941 the low 16 bits are added).
942 These relocs have to be done in combination with an R_M32R_LO16 reloc
943 because there is a carry from the LO16 to the HI16. Here we just save
944 the information we need; we do the actual relocation when we see the LO16.
945 This code is copied from the elf32-mips.c. We also support an arbitrary
946 number of HI16 relocs to be associated with a single LO16 reloc. The
947 assembler sorts the relocs to ensure each HI16 immediately precedes its
948 LO16. However if there are multiple copies, the assembler may not find
949 the real LO16 so it picks the first one it finds. */
951 struct m32r_hi16
953 struct m32r_hi16 *next;
954 bfd_byte *addr;
955 bfd_vma addend;
958 /* FIXME: This should not be a static variable. */
960 static struct m32r_hi16 *m32r_hi16_list;
962 static bfd_reloc_status_type
963 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
964 input_section, output_bfd, error_message)
965 bfd *abfd ATTRIBUTE_UNUSED;
966 arelent *reloc_entry;
967 asymbol *symbol;
968 PTR data;
969 asection *input_section;
970 bfd *output_bfd;
971 char **error_message ATTRIBUTE_UNUSED;
973 bfd_reloc_status_type ret;
974 bfd_vma relocation;
975 bfd_size_type sz;
976 struct m32r_hi16 *n;
978 /* This part is from bfd_elf_generic_reloc.
979 If we're relocating, and this an external symbol, we don't want
980 to change anything. */
981 if (output_bfd != (bfd *) NULL
982 && (symbol->flags & BSF_SECTION_SYM) == 0
983 && reloc_entry->addend == 0)
985 reloc_entry->address += input_section->output_offset;
986 return bfd_reloc_ok;
989 /* Sanity check the address (offset in section). */
990 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
991 if (reloc_entry->address > sz)
992 return bfd_reloc_outofrange;
994 ret = bfd_reloc_ok;
995 if (bfd_is_und_section (symbol->section)
996 && output_bfd == (bfd *) NULL)
997 ret = bfd_reloc_undefined;
999 if (bfd_is_com_section (symbol->section))
1000 relocation = 0;
1001 else
1002 relocation = symbol->value;
1004 relocation += symbol->section->output_section->vma;
1005 relocation += symbol->section->output_offset;
1006 relocation += reloc_entry->addend;
1008 /* Save the information, and let LO16 do the actual relocation. */
1009 n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
1010 if (n == NULL)
1011 return bfd_reloc_outofrange;
1012 n->addr = (bfd_byte *) data + reloc_entry->address;
1013 n->addend = relocation;
1014 n->next = m32r_hi16_list;
1015 m32r_hi16_list = n;
1017 if (output_bfd != (bfd *) NULL)
1018 reloc_entry->address += input_section->output_offset;
1020 return ret;
1023 /* Handle an M32R ELF HI16 reloc. */
1025 static void
1026 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
1027 bfd *input_bfd;
1028 int type;
1029 Elf_Internal_Rela *relhi;
1030 Elf_Internal_Rela *rello;
1031 bfd_byte *contents;
1032 bfd_vma addend;
1034 unsigned long insn;
1035 bfd_vma addlo;
1037 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1039 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
1040 if (type == R_M32R_HI16_SLO)
1041 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
1042 else
1043 addlo &= 0xffff;
1045 addend += ((insn & 0xffff) << 16) + addlo;
1047 /* Reaccount for sign extension of low part. */
1048 if (type == R_M32R_HI16_SLO
1049 && (addend & 0x8000) != 0)
1050 addend += 0x10000;
1052 bfd_put_32 (input_bfd,
1053 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
1054 contents + relhi->r_offset);
1057 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
1058 inplace relocation; this function exists in order to do the
1059 R_M32R_HI16_[SU]LO relocation described above. */
1061 bfd_reloc_status_type
1062 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
1063 input_section, output_bfd, error_message)
1064 bfd *input_bfd;
1065 arelent *reloc_entry;
1066 asymbol *symbol;
1067 PTR data;
1068 asection *input_section;
1069 bfd *output_bfd;
1070 char **error_message;
1072 /* This part is from bfd_elf_generic_reloc.
1073 If we're relocating, and this an external symbol, we don't want
1074 to change anything. */
1075 if (output_bfd != (bfd *) NULL
1076 && (symbol->flags & BSF_SECTION_SYM) == 0
1077 && reloc_entry->addend == 0)
1079 reloc_entry->address += input_section->output_offset;
1080 return bfd_reloc_ok;
1083 if (m32r_hi16_list != NULL)
1085 struct m32r_hi16 *l;
1087 l = m32r_hi16_list;
1088 while (l != NULL)
1090 unsigned long insn;
1091 unsigned long val;
1092 unsigned long vallo;
1093 struct m32r_hi16 *next;
1095 /* Do the HI16 relocation. Note that we actually don't need
1096 to know anything about the LO16 itself, except where to
1097 find the low 16 bits of the addend needed by the LO16. */
1098 insn = bfd_get_32 (input_bfd, l->addr);
1099 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
1100 & 0xffff) ^ 0x8000) - 0x8000;
1101 val = ((insn & 0xffff) << 16) + vallo;
1102 val += l->addend;
1104 /* Reaccount for sign extension of low part. */
1105 if ((val & 0x8000) != 0)
1106 val += 0x10000;
1108 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1109 bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
1111 next = l->next;
1112 free (l);
1113 l = next;
1116 m32r_hi16_list = NULL;
1119 /* Now do the LO16 reloc in the usual way.
1120 ??? It would be nice to call bfd_elf_generic_reloc here,
1121 but we have partial_inplace set. bfd_elf_generic_reloc will
1122 pass the handling back to bfd_install_relocation which will install
1123 a section relative addend which is wrong. */
1124 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1125 input_section, output_bfd, error_message);
1128 /* Do generic partial_inplace relocation.
1129 This is a local replacement for bfd_elf_generic_reloc. */
1131 bfd_reloc_status_type
1132 m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1133 input_section, output_bfd, error_message)
1134 bfd *input_bfd;
1135 arelent *reloc_entry;
1136 asymbol *symbol;
1137 PTR data;
1138 asection *input_section;
1139 bfd *output_bfd;
1140 char **error_message ATTRIBUTE_UNUSED;
1142 bfd_reloc_status_type ret;
1143 bfd_vma relocation;
1144 bfd_size_type sz;
1145 bfd_byte *inplace_address;
1147 /* This part is from bfd_elf_generic_reloc.
1148 If we're relocating, and this an external symbol, we don't want
1149 to change anything. */
1150 if (output_bfd != (bfd *) NULL
1151 && (symbol->flags & BSF_SECTION_SYM) == 0
1152 && reloc_entry->addend == 0)
1154 reloc_entry->address += input_section->output_offset;
1155 return bfd_reloc_ok;
1158 /* Now do the reloc in the usual way.
1159 ??? It would be nice to call bfd_elf_generic_reloc here,
1160 but we have partial_inplace set. bfd_elf_generic_reloc will
1161 pass the handling back to bfd_install_relocation which will install
1162 a section relative addend which is wrong. */
1164 /* Sanity check the address (offset in section). */
1165 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
1166 if (reloc_entry->address > sz)
1167 return bfd_reloc_outofrange;
1169 ret = bfd_reloc_ok;
1170 if (bfd_is_und_section (symbol->section)
1171 && output_bfd == (bfd *) NULL)
1172 ret = bfd_reloc_undefined;
1174 if (bfd_is_com_section (symbol->section)
1175 || output_bfd != (bfd *) NULL)
1176 relocation = 0;
1177 else
1178 relocation = symbol->value;
1180 /* Only do this for a final link. */
1181 if (output_bfd == (bfd *) NULL)
1183 relocation += symbol->section->output_section->vma;
1184 relocation += symbol->section->output_offset;
1187 relocation += reloc_entry->addend;
1188 inplace_address = (bfd_byte *) data + reloc_entry->address;
1190 #define DOIT(x) \
1191 x = ( (x & ~reloc_entry->howto->dst_mask) | \
1192 (((x & reloc_entry->howto->src_mask) + relocation) & \
1193 reloc_entry->howto->dst_mask))
1195 switch (reloc_entry->howto->size)
1197 case 1:
1199 short x = bfd_get_16 (input_bfd, inplace_address);
1200 DOIT (x);
1201 bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
1203 break;
1204 case 2:
1206 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1207 DOIT (x);
1208 bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
1210 break;
1211 default:
1212 BFD_ASSERT (0);
1215 if (output_bfd != (bfd *) NULL)
1216 reloc_entry->address += input_section->output_offset;
1218 return ret;
1221 /* Handle the R_M32R_SDA16 reloc.
1222 This reloc is used to compute the address of objects in the small data area
1223 and to perform loads and stores from that area.
1224 The lower 16 bits are sign extended and added to the register specified
1225 in the instruction, which is assumed to point to _SDA_BASE_. */
1227 static bfd_reloc_status_type
1228 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1229 input_section, output_bfd, error_message)
1230 bfd *abfd ATTRIBUTE_UNUSED;
1231 arelent *reloc_entry;
1232 asymbol *symbol;
1233 PTR data ATTRIBUTE_UNUSED;
1234 asection *input_section;
1235 bfd *output_bfd;
1236 char **error_message ATTRIBUTE_UNUSED;
1238 /* This part is from bfd_elf_generic_reloc. */
1239 if (output_bfd != (bfd *) NULL
1240 && (symbol->flags & BSF_SECTION_SYM) == 0
1241 && (! reloc_entry->howto->partial_inplace
1242 || reloc_entry->addend == 0))
1244 reloc_entry->address += input_section->output_offset;
1245 return bfd_reloc_ok;
1248 if (output_bfd != NULL)
1250 /* FIXME: See bfd_perform_relocation. Is this right? */
1251 return bfd_reloc_continue;
1254 /* FIXME: not sure what to do here yet. But then again, the linker
1255 may never call us. */
1256 abort ();
1259 /* Map BFD reloc types to M32R ELF reloc types. */
1261 struct m32r_reloc_map
1263 bfd_reloc_code_real_type bfd_reloc_val;
1264 unsigned char elf_reloc_val;
1267 static const struct m32r_reloc_map m32r_reloc_map_old[] =
1269 { BFD_RELOC_NONE, R_M32R_NONE },
1270 { BFD_RELOC_16, R_M32R_16 },
1271 { BFD_RELOC_32, R_M32R_32 },
1272 { BFD_RELOC_M32R_24, R_M32R_24 },
1273 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
1274 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
1275 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
1276 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
1277 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
1278 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
1279 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
1280 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
1281 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
1284 static const struct m32r_reloc_map m32r_reloc_map[] =
1286 { BFD_RELOC_NONE, R_M32R_NONE },
1287 { BFD_RELOC_16, R_M32R_16_RELA },
1288 { BFD_RELOC_32, R_M32R_32_RELA },
1289 { BFD_RELOC_M32R_24, R_M32R_24_RELA },
1290 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
1291 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
1292 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
1293 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
1294 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
1295 { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
1296 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
1297 { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
1298 { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
1300 { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
1301 { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
1302 { BFD_RELOC_M32R_COPY, R_M32R_COPY },
1303 { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
1304 { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
1305 { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
1306 { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
1307 { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
1308 { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
1309 { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
1310 { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
1311 { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
1312 { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
1313 { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
1314 { BFD_RELOC_M32R_GOTOFF_HI_ULO, R_M32R_GOTOFF_HI_ULO },
1315 { BFD_RELOC_M32R_GOTOFF_HI_SLO, R_M32R_GOTOFF_HI_SLO },
1316 { BFD_RELOC_M32R_GOTOFF_LO, R_M32R_GOTOFF_LO },
1319 static reloc_howto_type *
1320 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1321 bfd *abfd ATTRIBUTE_UNUSED;
1322 bfd_reloc_code_real_type code;
1324 unsigned int i;
1326 #ifdef USE_M32R_OLD_RELOC
1327 for (i = 0;
1328 i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
1329 i++)
1331 if (m32r_reloc_map_old[i].bfd_reloc_val == code)
1332 return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
1334 #else /* ! USE_M32R_OLD_RELOC */
1336 for (i = 0;
1337 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
1338 i++)
1340 if (m32r_reloc_map[i].bfd_reloc_val == code)
1341 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
1343 #endif
1345 return NULL;
1348 /* Set the howto pointer for an M32R ELF reloc. */
1350 static void
1351 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
1352 bfd *abfd ATTRIBUTE_UNUSED;
1353 arelent *cache_ptr;
1354 Elf_Internal_Rela *dst;
1356 unsigned int r_type;
1358 r_type = ELF32_R_TYPE (dst->r_info);
1359 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY)
1360 cache_ptr->howto = &m32r_elf_howto_table[r_type];
1363 static void
1364 m32r_info_to_howto (abfd, cache_ptr, dst)
1365 bfd *abfd ATTRIBUTE_UNUSED;
1366 arelent *cache_ptr;
1367 Elf_Internal_Rela *dst;
1369 BFD_ASSERT ((ELF32_R_TYPE(dst->r_info) == (unsigned int) R_M32R_NONE)
1370 || ((ELF32_R_TYPE(dst->r_info) > (unsigned int) R_M32R_GNU_VTENTRY)
1371 && (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_M32R_max)));
1372 cache_ptr->howto = &m32r_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
1376 /* Given a BFD section, try to locate the corresponding ELF section
1377 index. */
1379 bfd_boolean
1380 _bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
1381 bfd *abfd ATTRIBUTE_UNUSED;
1382 asection *sec;
1383 int *retval;
1385 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1387 *retval = SHN_M32R_SCOMMON;
1388 return TRUE;
1390 return FALSE;
1393 /* M32R ELF uses two common sections. One is the usual one, and the other
1394 is for small objects. All the small objects are kept together, and then
1395 referenced via one register, which yields faster assembler code. It is
1396 up to the compiler to emit an instruction to load the register with
1397 _SDA_BASE. This is what we use for the small common section. This
1398 approach is copied from elf32-mips.c. */
1399 static asection m32r_elf_scom_section;
1400 static asymbol m32r_elf_scom_symbol;
1401 static asymbol *m32r_elf_scom_symbol_ptr;
1403 /* Handle the special M32R section numbers that a symbol may use. */
1405 void
1406 _bfd_m32r_elf_symbol_processing (abfd, asym)
1407 bfd *abfd ATTRIBUTE_UNUSED;
1408 asymbol *asym;
1410 elf_symbol_type *elfsym;
1412 elfsym = (elf_symbol_type *) asym;
1414 switch (elfsym->internal_elf_sym.st_shndx)
1416 case SHN_M32R_SCOMMON:
1417 if (m32r_elf_scom_section.name == NULL)
1419 /* Initialize the small common section. */
1420 m32r_elf_scom_section.name = ".scommon";
1421 m32r_elf_scom_section.flags = SEC_IS_COMMON;
1422 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
1423 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
1424 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
1425 m32r_elf_scom_symbol.name = ".scommon";
1426 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
1427 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
1428 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
1430 asym->section = &m32r_elf_scom_section;
1431 asym->value = elfsym->internal_elf_sym.st_size;
1432 break;
1436 /* Hook called by the linker routine which adds symbols from an object
1437 file. We must handle the special M32R section numbers here.
1438 We also keep watching for whether we need to create the sdata special
1439 linker sections. */
1441 static bfd_boolean
1442 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1443 bfd *abfd;
1444 struct bfd_link_info *info;
1445 Elf_Internal_Sym *sym;
1446 const char **namep;
1447 flagword *flagsp ATTRIBUTE_UNUSED;
1448 asection **secp;
1449 bfd_vma *valp;
1451 if (! info->relocatable
1452 && (*namep)[0] == '_' && (*namep)[1] == 'S'
1453 && strcmp (*namep, "_SDA_BASE_") == 0
1454 && is_elf_hash_table (info->hash))
1456 /* This is simpler than using _bfd_elf_create_linker_section
1457 (our needs are simpler than ppc's needs). Also
1458 _bfd_elf_create_linker_section currently has a bug where if a .sdata
1459 section already exists a new one is created that follows it which
1460 screws of _SDA_BASE_ address calcs because output_offset != 0. */
1461 struct elf_link_hash_entry *h;
1462 struct bfd_link_hash_entry *bh;
1463 asection *s = bfd_get_section_by_name (abfd, ".sdata");
1465 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
1467 if (s == NULL)
1469 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1470 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1472 s = bfd_make_section_anyway (abfd, ".sdata");
1473 if (s == NULL)
1474 return FALSE;
1475 bfd_set_section_flags (abfd, s, flags);
1476 bfd_set_section_alignment (abfd, s, 2);
1479 bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
1480 FALSE, FALSE, FALSE);
1482 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1483 && !(_bfd_generic_link_add_one_symbol (info,
1484 abfd,
1485 "_SDA_BASE_",
1486 BSF_GLOBAL,
1488 (bfd_vma) 32768,
1489 (const char *) NULL,
1490 FALSE,
1491 get_elf_backend_data (abfd)->collect,
1492 &bh)))
1493 return FALSE;
1494 h = (struct elf_link_hash_entry *) bh;
1495 h->type = STT_OBJECT;
1498 switch (sym->st_shndx)
1500 case SHN_M32R_SCOMMON:
1501 *secp = bfd_make_section_old_way (abfd, ".scommon");
1502 (*secp)->flags |= SEC_IS_COMMON;
1503 *valp = sym->st_size;
1504 break;
1507 return TRUE;
1510 /* We have to figure out the SDA_BASE value, so that we can adjust the
1511 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
1512 BFD. If we can't find it, we're stuck. We cache it in the ELF
1513 target data. We don't need to adjust the symbol value for an
1514 external symbol if we are producing relocatable output. */
1516 static bfd_reloc_status_type
1517 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
1518 bfd *output_bfd;
1519 struct bfd_link_info *info;
1520 const char **error_message;
1521 bfd_vma *psb;
1523 if (elf_gp (output_bfd) == 0)
1525 struct bfd_link_hash_entry *h;
1527 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
1528 if (h != (struct bfd_link_hash_entry *) NULL
1529 && h->type == bfd_link_hash_defined)
1530 elf_gp (output_bfd) = (h->u.def.value
1531 + h->u.def.section->output_section->vma
1532 + h->u.def.section->output_offset);
1533 else
1535 /* Only get the error once. */
1536 *psb = elf_gp (output_bfd) = 4;
1537 *error_message =
1538 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
1539 return bfd_reloc_dangerous;
1542 *psb = elf_gp (output_bfd);
1543 return bfd_reloc_ok;
1546 /* Return size of a PLT entry. */
1547 #define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
1549 /* The m32r linker needs to keep track of the number of relocs that it
1550 decides to copy in check_relocs for each symbol. This is so that
1551 it can discard PC relative relocs if it doesn't need them when
1552 linking with -Bsymbolic. We store the information in a field
1553 extending the regular ELF linker hash table. */
1555 /* This structure keeps track of the number of PC relative relocs we
1556 have copied for a given symbol. */
1558 struct elf_m32r_pcrel_relocs_copied
1560 /* Next section. */
1561 struct elf_m32r_pcrel_relocs_copied *next;
1562 /* A section in dynobj. */
1563 asection *section;
1564 /* Number of relocs copied in this section. */
1565 bfd_size_type count;
1568 /* The sh linker needs to keep track of the number of relocs that it
1569 decides to copy as dynamic relocs in check_relocs for each symbol.
1570 This is so that it can later discard them if they are found to be
1571 unnecessary. We store the information in a field extending the
1572 regular ELF linker hash table. */
1574 struct elf_m32r_dyn_relocs
1576 struct elf_m32r_dyn_relocs *next;
1578 /* The input section of the reloc. */
1579 asection *sec;
1581 /* Total number of relocs copied for the input section. */
1582 bfd_size_type count;
1584 /* Number of pc-relative relocs copied for the input section. */
1585 bfd_size_type pc_count;
1589 /* m32r ELF linker hash entry. */
1591 struct elf_m32r_link_hash_entry
1593 struct elf_link_hash_entry root;
1595 /* Track dynamic relocs copied for this symbol. */
1596 struct elf_m32r_dyn_relocs *dyn_relocs;
1598 // bfd_signed_vma gotplt_refcount;
1600 /* Number of PC relative relocs copied for this symbol. */
1601 /* struct elf_m32r_pcrel_relocs_copied *pcrel_relocs_copied; FIXME */
1604 /* m32r ELF linker hash table. */
1606 struct elf_m32r_link_hash_table
1608 struct elf_link_hash_table root;
1610 /* Short-cuts to get to dynamic linker sections. */
1611 asection *sgot;
1612 asection *sgotplt;
1613 asection *srelgot;
1614 asection *splt;
1615 asection *srelplt;
1616 asection *sdynbss;
1617 asection *srelbss;
1619 /* Small local sym to section mapping cache. */
1620 struct sym_sec_cache sym_sec;
1623 /* Traverse an m32r ELF linker hash table. */
1625 #define m32r_elf_link_hash_traverse(table, func, info) \
1626 (elf_link_hash_traverse \
1627 (&(table)->root, \
1628 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1629 (info)))
1631 /* Get the m32r ELF linker hash table from a link_info structure. */
1634 #define m32r_elf_hash_table(p) \
1635 ((struct elf_m32r_link_hash_table *) ((p)->hash))
1637 /* Create an entry in an m32r ELF linker hash table. */
1638 static struct bfd_hash_entry *
1639 m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1640 const char * );
1642 static struct bfd_hash_entry *
1643 m32r_elf_link_hash_newfunc (entry, table, string)
1644 struct bfd_hash_entry *entry;
1645 struct bfd_hash_table *table;
1646 const char *string;
1648 struct elf_m32r_link_hash_entry *ret =
1649 (struct elf_m32r_link_hash_entry *) entry;
1651 /* Allocate the structure if it has not already been allocated by a
1652 subclass. */
1653 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1654 ret = ((struct elf_m32r_link_hash_entry *)
1655 bfd_hash_allocate (table,
1656 sizeof (struct elf_m32r_link_hash_entry)));
1657 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1658 return (struct bfd_hash_entry *) ret;
1660 /* Call the allocation method of the superclass. */
1661 ret = ((struct elf_m32r_link_hash_entry *)
1662 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1663 table, string));
1664 if (ret != (struct elf_m32r_link_hash_entry *) NULL)
1666 struct elf_m32r_link_hash_entry *eh;
1668 eh = (struct elf_m32r_link_hash_entry *) ret;
1669 eh->dyn_relocs = NULL;
1670 // eh->gotplt_refcount = 0;
1671 /* eh->pcrel_relocs_copied = NULL; FIXME */
1674 return (struct bfd_hash_entry *) ret;
1677 /* Create an m32r ELF linker hash table. */
1678 static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1680 static struct bfd_link_hash_table *
1681 m32r_elf_link_hash_table_create (abfd)
1682 bfd *abfd;
1684 struct elf_m32r_link_hash_table *ret;
1685 bfd_size_type amt = sizeof (struct elf_m32r_link_hash_table);
1687 ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1688 if (ret == (struct elf_m32r_link_hash_table *) NULL)
1689 return NULL;
1691 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1692 m32r_elf_link_hash_newfunc))
1694 free (ret);
1695 return NULL;
1698 ret->sgot = NULL;
1699 ret->sgotplt = NULL;
1700 ret->srelgot = NULL;
1701 ret->splt = NULL;
1702 ret->srelplt = NULL;
1703 ret->sdynbss = NULL;
1704 ret->srelbss = NULL;
1705 ret->sym_sec.abfd = NULL;
1707 return &ret->root.root;
1710 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1711 shortcuts to them in our hash table. */
1712 static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1714 static bfd_boolean
1715 create_got_section (dynobj, info)
1716 bfd *dynobj;
1717 struct bfd_link_info *info;
1719 struct elf_m32r_link_hash_table *htab;
1721 if (! _bfd_elf_create_got_section (dynobj, info))
1722 return FALSE;
1724 htab = m32r_elf_hash_table (info);
1725 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1726 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1727 if (! htab->sgot || ! htab->sgotplt)
1728 abort ();
1730 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1731 if (htab->srelgot == NULL
1732 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1733 (SEC_ALLOC
1734 | SEC_LOAD
1735 | SEC_HAS_CONTENTS
1736 | SEC_IN_MEMORY
1737 | SEC_LINKER_CREATED
1738 | SEC_READONLY))
1739 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1740 return FALSE;
1742 return TRUE;
1745 /* Create dynamic sections when linking against a dynamic object. */
1747 static bfd_boolean
1748 m32r_elf_create_dynamic_sections (abfd, info)
1749 bfd *abfd;
1750 struct bfd_link_info *info;
1752 struct elf_m32r_link_hash_table *htab;
1753 flagword flags, pltflags;
1754 register asection *s;
1755 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1756 int ptralign = 2; /* 32bit */
1758 htab = m32r_elf_hash_table (info);
1760 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1761 .rel[a].bss sections. */
1763 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1764 | SEC_LINKER_CREATED);
1766 pltflags = flags;
1767 pltflags |= SEC_CODE;
1768 if (bed->plt_not_loaded)
1769 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1770 if (bed->plt_readonly)
1771 pltflags |= SEC_READONLY;
1773 s = bfd_make_section (abfd, ".plt");
1774 htab->splt = s;
1775 if (s == NULL
1776 || ! bfd_set_section_flags (abfd, s, pltflags)
1777 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1778 return FALSE;
1780 if (bed->want_plt_sym)
1782 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1783 .plt section. */
1784 struct bfd_link_hash_entry *bh = NULL;
1785 struct elf_link_hash_entry *h;
1786 if (! (_bfd_generic_link_add_one_symbol
1787 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1788 (bfd_vma) 0, (const char *) NULL, FALSE,
1789 get_elf_backend_data (abfd)->collect, &bh)))
1790 return FALSE;
1791 h = (struct elf_link_hash_entry *) bh;
1792 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1793 h->type = STT_OBJECT;
1795 if (info->shared
1796 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1797 return FALSE;
1800 s = bfd_make_section (abfd,
1801 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
1802 htab->srelplt = s;
1803 if (s == NULL
1804 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1805 || ! bfd_set_section_alignment (abfd, s, ptralign))
1806 return FALSE;
1808 if (htab->sgot == NULL
1809 && ! create_got_section (abfd, info))
1810 return FALSE;
1813 const char *secname;
1814 char *relname;
1815 flagword secflags;
1816 asection *sec;
1818 for (sec = abfd->sections; sec; sec = sec->next)
1820 secflags = bfd_get_section_flags (abfd, sec);
1821 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1822 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1823 continue;
1824 secname = bfd_get_section_name (abfd, sec);
1825 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1826 strcpy (relname, ".rela");
1827 strcat (relname, secname);
1828 if (bfd_get_section_by_name (abfd, secname))
1829 continue;
1830 s = bfd_make_section (abfd, relname);
1831 if (s == NULL
1832 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1833 || ! bfd_set_section_alignment (abfd, s, ptralign))
1834 return FALSE;
1838 if (bed->want_dynbss)
1840 /* The .dynbss section is a place to put symbols which are defined
1841 by dynamic objects, are referenced by regular objects, and are
1842 not functions. We must allocate space for them in the process
1843 image and use a R_*_COPY reloc to tell the dynamic linker to
1844 initialize them at run time. The linker script puts the .dynbss
1845 section into the .bss section of the final image. */
1846 s = bfd_make_section (abfd, ".dynbss");
1847 htab->sdynbss = s;
1848 if (s == NULL
1849 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1850 return FALSE;
1851 /* The .rel[a].bss section holds copy relocs. This section is not
1852 normally needed. We need to create it here, though, so that the
1853 linker will map it to an output section. We can't just create it
1854 only if we need it, because we will not know whether we need it
1855 until we have seen all the input files, and the first time the
1856 main linker code calls BFD after examining all the input files
1857 (size_dynamic_sections) the input sections have already been
1858 mapped to the output sections. If the section turns out not to
1859 be needed, we can discard it later. We will never need this
1860 section when generating a shared object, since they do not use
1861 copy relocs. */
1862 if (! info->shared)
1864 s = bfd_make_section (abfd,
1865 (bed->default_use_rela_p
1866 ? ".rela.bss" : ".rel.bss"));
1867 htab->srelbss = s;
1868 if (s == NULL
1869 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1870 || ! bfd_set_section_alignment (abfd, s, ptralign))
1871 return FALSE;
1875 return TRUE;
1878 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1879 static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1880 struct elf_link_hash_entry *,
1881 struct elf_link_hash_entry *);
1883 static void
1884 m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1885 struct elf_link_hash_entry *dir,
1886 struct elf_link_hash_entry *ind)
1888 struct elf_m32r_link_hash_entry *edir, *eind;
1890 edir = (struct elf_m32r_link_hash_entry *) dir;
1891 eind = (struct elf_m32r_link_hash_entry *) ind;
1893 if (eind->dyn_relocs != NULL)
1895 if (edir->dyn_relocs != NULL)
1897 struct elf_m32r_dyn_relocs **pp;
1898 struct elf_m32r_dyn_relocs *p;
1900 if (ind->root.type == bfd_link_hash_indirect)
1901 abort ();
1903 /* Add reloc counts against the weak sym to the strong sym
1904 list. Merge any entries against the same section. */
1905 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1907 struct elf_m32r_dyn_relocs *q;
1909 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1910 if (q->sec == p->sec)
1912 q->pc_count += p->pc_count;
1913 q->count += p->count;
1914 *pp = p->next;
1915 break;
1917 if (q == NULL)
1918 pp = &p->next;
1920 *pp = edir->dyn_relocs;
1923 edir->dyn_relocs = eind->dyn_relocs;
1924 eind->dyn_relocs = NULL;
1927 // if (ind->root.type == bfd_link_hash_indirect
1928 // && dir->got.refcount <= 0)
1929 // {
1930 // edir->tls_type = eind->tls_type;
1931 // eind->tls_type = GOT_UNKNOWN;
1932 // }
1933 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1937 /* Adjust a symbol defined by a dynamic object and referenced by a
1938 regular object. The current definition is in some section of the
1939 dynamic object, but we're not including those sections. We have to
1940 change the definition to something the rest of the link can
1941 understand. */
1943 static bfd_boolean
1944 m32r_elf_adjust_dynamic_symbol (info, h)
1945 struct bfd_link_info *info;
1946 struct elf_link_hash_entry *h;
1948 struct elf_m32r_link_hash_table *htab;
1949 struct elf_m32r_link_hash_entry *eh;
1950 struct elf_m32r_dyn_relocs *p;
1951 bfd *dynobj;
1952 asection *s;
1953 unsigned int power_of_two;
1955 #ifdef DEBUG_PIC
1956 printf("m32r_elf_adjust_dynamic_symbol()\n");
1957 #endif
1959 dynobj = elf_hash_table (info)->dynobj;
1961 /* Make sure we know what is going on here. */
1962 BFD_ASSERT (dynobj != NULL
1963 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1964 || h->weakdef != NULL
1965 || ((h->elf_link_hash_flags
1966 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1967 && (h->elf_link_hash_flags
1968 & ELF_LINK_HASH_REF_REGULAR) != 0
1969 && (h->elf_link_hash_flags
1970 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1973 /* If this is a function, put it in the procedure linkage table. We
1974 will fill in the contents of the procedure linkage table later,
1975 when we know the address of the .got section. */
1976 if (h->type == STT_FUNC
1977 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1979 if (! info->shared
1980 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1981 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1982 && h->root.type != bfd_link_hash_undefweak
1983 && h->root.type != bfd_link_hash_undefined)
1985 /* This case can occur if we saw a PLT reloc in an input
1986 file, but the symbol was never referred to by a dynamic
1987 object. In such a case, we don't actually need to build
1988 a procedure linkage table, and we can just do a PCREL
1989 reloc instead. */
1990 h->plt.offset = (bfd_vma) -1;
1991 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1994 return TRUE;
1996 else
1997 h->plt.offset = (bfd_vma) -1;
1999 /* If this is a weak symbol, and there is a real definition, the
2000 processor independent code will have arranged for us to see the
2001 real definition first, and we can just use the same value. */
2002 if (h->weakdef != NULL)
2004 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2005 || h->weakdef->root.type == bfd_link_hash_defweak);
2006 h->root.u.def.section = h->weakdef->root.u.def.section;
2007 h->root.u.def.value = h->weakdef->root.u.def.value;
2008 return TRUE;
2011 /* This is a reference to a symbol defined by a dynamic object which
2012 is not a function. */
2014 /* If we are creating a shared library, we must presume that the
2015 only references to the symbol are via the global offset table.
2016 For such cases we need not do anything here; the relocations will
2017 be handled correctly by relocate_section. */
2018 if (info->shared)
2019 return TRUE;
2021 /* If there are no references to this symbol that do not use the
2022 GOT, we don't need to generate a copy reloc. */
2023 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2024 return TRUE;
2026 /* If -z nocopyreloc was given, we won't generate them either. */
2027 if (info->nocopyreloc)
2029 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2030 return TRUE;
2033 eh = (struct elf_m32r_link_hash_entry *) h;
2034 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2036 s = p->sec->output_section;
2037 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2038 break;
2041 /* If we didn't find any dynamic relocs in sections which needs the
2042 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2043 the copy reloc. */
2044 if (p == NULL)
2046 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2047 return TRUE;
2050 /* We must allocate the symbol in our .dynbss section, which will
2051 become part of the .bss section of the executable. There will be
2052 an entry for this symbol in the .dynsym section. The dynamic
2053 object will contain position independent code, so all references
2054 from the dynamic object to this symbol will go through the global
2055 offset table. The dynamic linker will use the .dynsym entry to
2056 determine the address it must put in the global offset table, so
2057 both the dynamic object and the regular object will refer to the
2058 same memory location for the variable. */
2060 htab = m32r_elf_hash_table (info);
2061 s = htab->sdynbss;
2062 BFD_ASSERT (s != NULL);
2064 /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2065 to copy the initial value out of the dynamic object and into the
2066 runtime process image. We need to remember the offset into the
2067 .rela.bss section we are going to use. */
2068 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2070 asection *srel;
2072 srel = htab->srelbss;
2073 BFD_ASSERT (srel != NULL);
2074 srel->size += sizeof (Elf32_External_Rela);
2075 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2078 /* We need to figure out the alignment required for this symbol. I
2079 have no idea how ELF linkers handle this. */
2080 power_of_two = bfd_log2 (h->size);
2081 if (power_of_two > 3)
2082 power_of_two = 3;
2084 /* Apply the required alignment. */
2085 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2086 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2088 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2089 return FALSE;
2092 /* Define the symbol as being at this point in the section. */
2093 h->root.u.def.section = s;
2094 h->root.u.def.value = s->size;
2096 /* Increment the section size to make room for the symbol. */
2097 s->size += h->size;
2099 return TRUE;
2102 /* Allocate space in .plt, .got and associated reloc sections for
2103 dynamic relocs. */
2105 static bfd_boolean
2106 allocate_dynrelocs (h, inf)
2107 struct elf_link_hash_entry *h;
2108 PTR inf;
2110 struct bfd_link_info *info;
2111 struct elf_m32r_link_hash_table *htab;
2112 struct elf_m32r_link_hash_entry *eh;
2113 struct elf_m32r_dyn_relocs *p;
2115 if (h->root.type == bfd_link_hash_indirect)
2116 return TRUE;
2118 if (h->root.type == bfd_link_hash_warning)
2119 /* When warning symbols are created, they **replace** the "real"
2120 entry in the hash table, thus we never get to see the real
2121 symbol in a hash traversal. So look at it now. */
2122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2124 info = (struct bfd_link_info *) inf;
2125 htab = m32r_elf_hash_table (info);
2127 eh = (struct elf_m32r_link_hash_entry *) h;
2128 // if ((h->got.refcount > 0
2129 // || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2130 // && eh->gotplt_refcount > 0)
2131 // {
2132 // /* The symbol has been forced local, or we have some direct got refs,
2133 // so treat all the gotplt refs as got refs. */
2134 // h->got.refcount += eh->gotplt_refcount;
2135 // if (h->plt.refcount >= eh->gotplt_refcount)
2136 // h->plt.refcount -= eh->gotplt_refcount;
2137 // }
2139 if (htab->root.dynamic_sections_created
2140 && h->plt.refcount > 0)
2142 /* Make sure this symbol is output as a dynamic symbol.
2143 Undefined weak syms won't yet be marked as dynamic. */
2144 if (h->dynindx == -1
2145 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2147 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2148 return FALSE;
2151 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2153 asection *s = htab->splt;
2155 /* If this is the first .plt entry, make room for the special
2156 first entry. */
2157 if (s->size == 0)
2158 s->size += PLT_ENTRY_SIZE;
2160 h->plt.offset = s->size;
2162 /* If this symbol is not defined in a regular file, and we are
2163 not generating a shared library, then set the symbol to this
2164 location in the .plt. This is required to make function
2165 pointers compare as equal between the normal executable and
2166 the shared library. */
2167 if (! info->shared
2168 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2170 h->root.u.def.section = s;
2171 h->root.u.def.value = h->plt.offset;
2174 /* Make room for this entry. */
2175 s->size += PLT_ENTRY_SIZE;
2177 /* We also need to make an entry in the .got.plt section, which
2178 will be placed in the .got section by the linker script. */
2179 htab->sgotplt->size += 4;
2181 /* We also need to make an entry in the .rel.plt section. */
2182 htab->srelplt->size += sizeof (Elf32_External_Rela);
2184 else
2186 h->plt.offset = (bfd_vma) -1;
2187 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2190 else
2192 h->plt.offset = (bfd_vma) -1;
2193 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2196 if (h->got.refcount > 0)
2198 asection *s;
2199 bfd_boolean dyn;
2201 /* Make sure this symbol is output as a dynamic symbol.
2202 Undefined weak syms won't yet be marked as dynamic. */
2203 if (h->dynindx == -1
2204 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2206 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2207 return FALSE;
2210 s = htab->sgot;
2212 h->got.offset = s->size;
2213 s->size += 4;
2214 dyn = htab->root.dynamic_sections_created;
2215 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2216 htab->srelgot->size += sizeof (Elf32_External_Rela);
2218 else
2219 h->got.offset = (bfd_vma) -1;
2221 if (eh->dyn_relocs == NULL)
2222 return TRUE;
2224 /* In the shared -Bsymbolic case, discard space allocated for
2225 dynamic pc-relative relocs against symbols which turn out to be
2226 defined in regular objects. For the normal shared case, discard
2227 space for pc-relative relocs that have become local due to symbol
2228 visibility changes. */
2230 if (info->shared)
2232 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2233 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2234 || info->symbolic))
2236 struct elf_m32r_dyn_relocs **pp;
2237 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2239 p->count -= p->pc_count;
2240 p->pc_count = 0;
2241 if (p->count == 0)
2242 *pp = p->next;
2243 else
2244 pp = &p->next;
2248 else
2250 /* For the non-shared case, discard space for relocs against
2251 symbols which turn out to need copy relocs or are not
2252 dynamic. */
2254 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2255 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2256 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2257 || (htab->root.dynamic_sections_created
2258 && (h->root.type == bfd_link_hash_undefweak
2259 || h->root.type == bfd_link_hash_undefined))))
2261 /* Make sure this symbol is output as a dynamic symbol.
2262 Undefined weak syms won't yet be marked as dynamic. */
2263 if (h->dynindx == -1
2264 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2266 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2267 return FALSE;
2270 /* If that succeeded, we know we'll be keeping all the
2271 relocs. */
2272 if (h->dynindx != -1)
2273 goto keep;
2276 eh->dyn_relocs = NULL;
2278 keep: ;
2281 /* Finally, allocate space. */
2282 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2284 asection *sreloc = elf_section_data (p->sec)->sreloc;
2285 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2288 return TRUE;
2290 /* Find any dynamic relocs that apply to read-only sections. */
2292 static bfd_boolean
2293 readonly_dynrelocs (h, inf)
2294 struct elf_link_hash_entry *h;
2295 PTR inf;
2297 struct elf_m32r_link_hash_entry *eh;
2298 struct elf_m32r_dyn_relocs *p;
2300 if (h->root.type == bfd_link_hash_warning)
2301 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2303 eh = (struct elf_m32r_link_hash_entry *) h;
2304 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2306 asection *s = p->sec->output_section;
2308 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2310 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2312 info->flags |= DF_TEXTREL;
2314 /* Not an error, just cut short the traversal. */
2315 return FALSE;
2318 return TRUE;
2321 /* Set the sizes of the dynamic sections. */
2323 static bfd_boolean
2324 m32r_elf_size_dynamic_sections (output_bfd, info)
2325 bfd *output_bfd ATTRIBUTE_UNUSED;
2326 struct bfd_link_info *info;
2328 struct elf_m32r_link_hash_table *htab;
2329 bfd *dynobj;
2330 asection *s;
2331 bfd_boolean relocs;
2332 bfd *ibfd;
2334 #ifdef DEBUG_PIC
2335 printf("m32r_elf_size_dynamic_sections()\n");
2336 #endif
2338 htab = m32r_elf_hash_table (info);
2339 dynobj = htab->root.dynobj;
2340 BFD_ASSERT (dynobj != NULL);
2342 if (htab->root.dynamic_sections_created)
2344 /* Set the contents of the .interp section to the interpreter. */
2345 if (! info->shared)
2347 s = bfd_get_section_by_name (dynobj, ".interp");
2348 BFD_ASSERT (s != NULL);
2349 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2350 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2354 /* Set up .got offsets for local syms, and space for local dynamic
2355 relocs. */
2356 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2358 bfd_signed_vma *local_got;
2359 bfd_signed_vma *end_local_got;
2360 bfd_size_type locsymcount;
2361 Elf_Internal_Shdr *symtab_hdr;
2362 asection *srel;
2364 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2365 continue;
2367 for (s = ibfd->sections; s != NULL; s = s->next)
2369 struct elf_m32r_dyn_relocs *p;
2371 for (p = ((struct elf_m32r_dyn_relocs *)
2372 elf_section_data (s)->local_dynrel);
2373 p != NULL;
2374 p = p->next)
2376 if (! bfd_is_abs_section (p->sec)
2377 && bfd_is_abs_section (p->sec->output_section))
2379 /* Input section has been discarded, either because
2380 it is a copy of a linkonce section or due to
2381 linker script /DISCARD/, so we'll be discarding
2382 the relocs too. */
2384 else if (p->count != 0)
2386 srel = elf_section_data (p->sec)->sreloc;
2387 srel->size += p->count * sizeof (Elf32_External_Rela);
2388 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2389 info->flags |= DF_TEXTREL;
2394 local_got = elf_local_got_refcounts (ibfd);
2395 if (!local_got)
2396 continue;
2398 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2399 locsymcount = symtab_hdr->sh_info;
2400 end_local_got = local_got + locsymcount;
2401 s = htab->sgot;
2402 srel = htab->srelgot;
2403 for (; local_got < end_local_got; ++local_got)
2405 if (*local_got > 0)
2407 *local_got = s->size;
2408 s->size += 4;
2409 if (info->shared)
2410 srel->size += sizeof (Elf32_External_Rela);
2412 else
2413 *local_got = (bfd_vma) -1;
2417 /* Allocate global sym .plt and .got entries, and space for global
2418 sym dynamic relocs. */
2419 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2421 /* We now have determined the sizes of the various dynamic sections.
2422 Allocate memory for them. */
2423 relocs = FALSE;
2424 for (s = dynobj->sections; s != NULL; s = s->next)
2426 if ((s->flags & SEC_LINKER_CREATED) == 0)
2427 continue;
2429 if (s == htab->splt
2430 || s == htab->sgot
2431 || s == htab->sgotplt)
2433 /* Strip this section if we don't need it; see the
2434 comment below. */
2436 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2438 if (s->size != 0 && s != htab->srelplt)
2439 relocs = TRUE;
2441 /* We use the reloc_count field as a counter if we need
2442 to copy relocs into the output file. */
2443 s->reloc_count = 0;
2445 else
2447 /* It's not one of our sections, so don't allocate space. */
2448 continue;
2451 if (s->size == 0)
2453 /* If we don't need this section, strip it from the
2454 output file. This is mostly to handle .rela.bss and
2455 .rela.plt. We must create both sections in
2456 create_dynamic_sections, because they must be created
2457 before the linker maps input sections to output
2458 sections. The linker does that before
2459 adjust_dynamic_symbol is called, and it is that
2460 function which decides whether anything needs to go
2461 into these sections. */
2462 _bfd_strip_section_from_output (info, s);
2463 continue;
2466 /* Allocate memory for the section contents. We use bfd_zalloc
2467 here in case unused entries are not reclaimed before the
2468 section's contents are written out. This should not happen,
2469 but this way if it does, we get a R_M32R_NONE reloc instead
2470 of garbage. */
2471 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2472 if (s->contents == NULL)
2473 return FALSE;
2476 if (htab->root.dynamic_sections_created)
2478 /* Add some entries to the .dynamic section. We fill in the
2479 values later, in m32r_elf_finish_dynamic_sections, but we
2480 must add the entries now so that we get the correct size for
2481 the .dynamic section. The DT_DEBUG entry is filled in by the
2482 dynamic linker and used by the debugger. */
2483 #define add_dynamic_entry(TAG, VAL) \
2484 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2486 if (! info->shared)
2488 if (! add_dynamic_entry (DT_DEBUG, 0))
2489 return FALSE;
2492 if (htab->splt->size != 0)
2494 if (! add_dynamic_entry (DT_PLTGOT, 0)
2495 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2496 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2497 || ! add_dynamic_entry (DT_JMPREL, 0))
2498 return FALSE;
2501 if (relocs)
2503 if (! add_dynamic_entry (DT_RELA, 0)
2504 || ! add_dynamic_entry (DT_RELASZ, 0)
2505 || ! add_dynamic_entry (DT_RELAENT,
2506 sizeof (Elf32_External_Rela)))
2507 return FALSE;
2509 /* If any dynamic relocs apply to a read-only section,
2510 then we need a DT_TEXTREL entry. */
2511 if ((info->flags & DF_TEXTREL) == 0)
2512 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2513 (PTR) info);
2515 if ((info->flags & DF_TEXTREL) != 0)
2517 if (! add_dynamic_entry (DT_TEXTREL, 0))
2518 return FALSE;
2522 #undef add_dynamic_entry
2524 return TRUE;
2526 /* Relocate an M32R/D ELF section.
2527 There is some attempt to make this function usable for many architectures,
2528 both for RELA and REL type relocs, if only to serve as a learning tool.
2530 The RELOCATE_SECTION function is called by the new ELF backend linker
2531 to handle the relocations for a section.
2533 The relocs are always passed as Rela structures; if the section
2534 actually uses Rel structures, the r_addend field will always be
2535 zero.
2537 This function is responsible for adjust the section contents as
2538 necessary, and (if using Rela relocs and generating a
2539 relocatable output file) adjusting the reloc addend as
2540 necessary.
2542 This function does not have to worry about setting the reloc
2543 address or the reloc symbol index.
2545 LOCAL_SYMS is a pointer to the swapped in local symbols.
2547 LOCAL_SECTIONS is an array giving the section in the input file
2548 corresponding to the st_shndx field of each local symbol.
2550 The global hash table entry for the global symbols can be found
2551 via elf_sym_hashes (input_bfd).
2553 When generating relocatable output, this function must handle
2554 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2555 going to be the section symbol corresponding to the output
2556 section, which means that the addend must be adjusted
2557 accordingly. */
2559 static bfd_boolean
2560 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2561 contents, relocs, local_syms, local_sections)
2562 bfd *output_bfd ATTRIBUTE_UNUSED;
2563 struct bfd_link_info *info;
2564 bfd *input_bfd;
2565 asection *input_section;
2566 bfd_byte *contents;
2567 Elf_Internal_Rela *relocs;
2568 Elf_Internal_Sym *local_syms;
2569 asection **local_sections;
2571 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2572 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2573 Elf_Internal_Rela *rel, *relend;
2574 /* Assume success. */
2575 bfd_boolean ret = TRUE;
2577 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2578 bfd *dynobj;
2579 bfd_vma *local_got_offsets;
2580 asection *sgot, *splt, *sreloc;
2582 dynobj = htab->root.dynobj;
2583 local_got_offsets = elf_local_got_offsets (input_bfd);
2585 sgot = htab->sgot;
2586 splt = htab->splt;
2587 sreloc = NULL;
2589 rel = relocs;
2590 relend = relocs + input_section->reloc_count;
2591 for (; rel < relend; rel++)
2593 int r_type;
2594 reloc_howto_type *howto;
2595 unsigned long r_symndx;
2596 struct elf_link_hash_entry *h;
2597 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
2598 ensure it's zero (we use REL relocs, not RELA). Therefore this
2599 should be assigning zero to `addend', but for clarity we use
2600 `r_addend'. */
2601 bfd_vma addend = rel->r_addend;
2602 bfd_vma offset = rel->r_offset;
2603 Elf_Internal_Sym *sym;
2604 asection *sec;
2605 const char *sym_name;
2606 bfd_reloc_status_type r;
2607 const char *errmsg = NULL;
2608 bfd_boolean use_rel = FALSE;
2610 h = NULL;
2611 r_type = ELF32_R_TYPE (rel->r_info);
2612 if (r_type < 0 || r_type >= (int) R_M32R_max)
2614 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2615 bfd_archive_filename (input_bfd),
2616 (int) r_type);
2617 bfd_set_error (bfd_error_bad_value);
2618 ret = FALSE;
2619 continue;
2622 if (r_type == R_M32R_GNU_VTENTRY
2623 || r_type == R_M32R_GNU_VTINHERIT
2624 || r_type == R_M32R_NONE
2625 || r_type == R_M32R_RELA_GNU_VTENTRY
2626 || r_type == R_M32R_RELA_GNU_VTINHERIT)
2627 continue;
2629 if (r_type <= R_M32R_GNU_VTENTRY)
2630 use_rel = TRUE;
2632 howto = m32r_elf_howto_table + r_type;
2633 r_symndx = ELF32_R_SYM (rel->r_info);
2635 if (info->relocatable && (use_rel == TRUE))
2637 /* This is a relocatable link. We don't have to change
2638 anything, unless the reloc is against a section symbol,
2639 in which case we have to adjust according to where the
2640 section symbol winds up in the output section. */
2641 sec = NULL;
2642 if (r_symndx >= symtab_hdr->sh_info)
2644 /* External symbol. */
2645 continue;
2648 /* Local symbol. */
2649 sym = local_syms + r_symndx;
2650 sym_name = "<local symbol>";
2651 /* STT_SECTION: symbol is associated with a section. */
2652 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2654 /* Symbol isn't associated with a section. Nothing to do. */
2655 continue;
2658 sec = local_sections[r_symndx];
2659 addend += sec->output_offset + sym->st_value;
2661 /* If partial_inplace, we need to store any additional addend
2662 back in the section. */
2663 if (! howto->partial_inplace)
2664 continue;
2665 /* ??? Here is a nice place to call a special_function
2666 like handler. */
2667 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2668 r = _bfd_relocate_contents (howto, input_bfd,
2669 addend, contents + offset);
2670 else
2672 Elf_Internal_Rela *lorel;
2674 /* We allow an arbitrary number of HI16 relocs before the
2675 LO16 reloc. This permits gcc to emit the HI and LO relocs
2676 itself. */
2677 for (lorel = rel + 1;
2678 (lorel < relend
2679 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2680 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2681 lorel++)
2682 continue;
2683 if (lorel < relend
2684 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2686 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2687 contents, addend);
2688 r = bfd_reloc_ok;
2690 else
2691 r = _bfd_relocate_contents (howto, input_bfd,
2692 addend, contents + offset);
2695 else
2697 bfd_vma relocation;
2698 bfd_size_type sz;
2700 /* This is a final link. */
2701 sym = NULL;
2702 sec = NULL;
2703 h = NULL;
2705 if (r_symndx < symtab_hdr->sh_info)
2707 /* Local symbol. */
2708 sym = local_syms + r_symndx;
2709 sec = local_sections[r_symndx];
2710 sym_name = "<local symbol>";
2712 if (use_rel == FALSE)
2714 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2715 addend = rel->r_addend;
2717 if (info->relocatable)
2719 /* This is a relocatable link. We don't have to change
2720 anything, unless the reloc is against a section symbol,
2721 in which case we have to adjust according to where the
2722 section symbol winds up in the output section. */
2723 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2724 rel->r_addend += sec->output_offset + sym->st_value;
2726 continue;
2729 else
2731 relocation = (sec->output_section->vma
2732 + sec->output_offset
2733 + sym->st_value);
2736 else
2738 /* External symbol. */
2739 if (info->relocatable && (use_rel == FALSE))
2740 continue;
2742 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2743 while (h->root.type == bfd_link_hash_indirect
2744 || h->root.type == bfd_link_hash_warning)
2745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2746 sym_name = h->root.root.string;
2748 if (h->root.type == bfd_link_hash_defined
2749 || h->root.type == bfd_link_hash_defweak)
2751 bfd_boolean dyn;
2752 sec = h->root.u.def.section;
2754 dyn = htab->root.dynamic_sections_created;
2755 sec = h->root.u.def.section;
2756 if (r_type == R_M32R_GOTPC24
2757 || (r_type == R_M32R_GOTPC_HI_ULO
2758 || r_type == R_M32R_GOTPC_HI_SLO
2759 || r_type == R_M32R_GOTPC_LO)
2760 || (r_type == R_M32R_26_PLTREL
2761 && h->plt.offset != (bfd_vma) -1)
2762 || ((r_type == R_M32R_GOT24
2763 || r_type == R_M32R_GOT16_HI_ULO
2764 || r_type == R_M32R_GOT16_HI_SLO
2765 || r_type == R_M32R_GOT16_LO)
2766 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2767 info->shared, h)
2768 && (! info->shared
2769 || (! info->symbolic && h->dynindx != -1)
2770 || (h->elf_link_hash_flags
2771 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2772 || (info->shared
2773 && ((! info->symbolic && h->dynindx != -1)
2774 || (h->elf_link_hash_flags
2775 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2776 && (((r_type == R_M32R_16_RELA
2777 || r_type == R_M32R_32_RELA
2778 || r_type == R_M32R_24_RELA
2779 || r_type == R_M32R_HI16_ULO_RELA
2780 || r_type == R_M32R_HI16_SLO_RELA
2781 || r_type == R_M32R_LO16_RELA)
2782 && (h->elf_link_hash_flags
2783 & ELF_LINK_FORCED_LOCAL) == 0)
2784 || r_type == R_M32R_10_PCREL_RELA
2785 || r_type == R_M32R_18_PCREL_RELA
2786 || r_type == R_M32R_26_PCREL_RELA)
2787 && ((input_section->flags & SEC_ALLOC) != 0
2788 /* DWARF will emit R_M32R_16(24,32) relocations
2789 in its sections against symbols defined
2790 externally in shared libraries. We can't do
2791 anything with them here. */
2792 || ((input_section->flags & SEC_DEBUGGING) != 0
2793 && (h->elf_link_hash_flags
2794 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
2796 /* In these cases, we don't need the relocation
2797 value. We check specially because in some
2798 obscure cases sec->output_section will be NULL. */
2799 relocation = 0;
2801 else if (sec->output_section == NULL)
2803 (*_bfd_error_handler)
2804 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2805 bfd_get_filename (input_bfd), h->root.root.string,
2806 bfd_get_section_name (input_bfd, input_section));
2808 relocation = 0;
2810 else
2811 relocation = (h->root.u.def.value
2812 + sec->output_section->vma
2813 + sec->output_offset);
2815 else if (h->root.type == bfd_link_hash_undefweak)
2816 relocation = 0;
2817 else if (info->unresolved_syms_in_objects == RM_IGNORE
2818 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2819 relocation = 0;
2820 else
2822 if (! ((*info->callbacks->undefined_symbol)
2823 (info, h->root.root.string, input_bfd,
2824 input_section, offset,
2825 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2826 || ELF_ST_VISIBILITY (h->other)))))
2827 return FALSE;
2828 relocation = 0;
2832 /* Sanity check the address. */
2833 sz = (input_section->rawsize
2834 ? input_section->rawsize
2835 : input_section->size);
2836 if (offset > input_section->size)
2838 r = bfd_reloc_outofrange;
2839 goto check_reloc;
2842 switch ((int) r_type)
2844 case R_M32R_GOTOFF:
2845 /* Relocation is relative to the start of the global offset
2846 table (for ld24 rx, #uimm24). eg access at label+addend
2848 ld24 rx. #label@GOTOFF + addend
2849 sub rx, r12. */
2851 BFD_ASSERT (sgot != NULL);
2853 relocation = -(relocation - sgot->output_section->vma);
2854 rel->r_addend = -rel->r_addend;
2855 break;
2857 case R_M32R_GOTOFF_HI_ULO:
2858 case R_M32R_GOTOFF_HI_SLO:
2859 case R_M32R_GOTOFF_LO:
2860 BFD_ASSERT (sgot != NULL);
2862 relocation -= sgot->output_section->vma;
2864 if ((r_type == R_M32R_GOTOFF_HI_SLO)
2865 && ((relocation + rel->r_addend) & 0x8000))
2866 rel->r_addend += 0x10000;
2867 break;
2869 case R_M32R_GOTPC24:
2870 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2871 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2873 relocation = sgot->output_section->vma;
2874 break;
2876 case R_M32R_GOTPC_HI_ULO:
2877 case R_M32R_GOTPC_HI_SLO:
2878 case R_M32R_GOTPC_LO:
2880 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2881 bl .+4
2882 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2883 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2885 bl .+4
2886 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2887 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2889 relocation = sgot->output_section->vma;
2890 relocation -= (input_section->output_section->vma
2891 + input_section->output_offset
2892 + rel->r_offset);
2893 if ((r_type == R_M32R_GOTPC_HI_SLO)
2894 && ((relocation + rel->r_addend) & 0x8000))
2895 rel->r_addend += 0x10000;
2897 break;
2899 case R_M32R_GOT16_HI_ULO:
2900 case R_M32R_GOT16_HI_SLO:
2901 case R_M32R_GOT16_LO:
2902 /* Fall through. */
2903 case R_M32R_GOT24:
2904 /* Relocation is to the entry for this symbol in the global
2905 offset table. */
2906 BFD_ASSERT (sgot != NULL);
2908 if (h != NULL)
2910 bfd_boolean dyn;
2911 bfd_vma off;
2913 off = h->got.offset;
2914 BFD_ASSERT (off != (bfd_vma) -1);
2916 dyn = htab->root.dynamic_sections_created;
2917 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2918 || (info->shared
2919 && (info->symbolic
2920 || h->dynindx == -1
2921 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2922 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2924 /* This is actually a static link, or it is a
2925 -Bsymbolic link and the symbol is defined
2926 locally, or the symbol was forced to be local
2927 because of a version file. We must initialize
2928 this entry in the global offset table. Since the
2929 offset must always be a multiple of 4, we use the
2930 least significant bit to record whether we have
2931 initialized it already.
2933 When doing a dynamic link, we create a .rela.got
2934 relocation entry to initialize the value. This
2935 is done in the finish_dynamic_symbol routine. */
2936 if ((off & 1) != 0)
2937 off &= ~1;
2938 else
2940 bfd_put_32 (output_bfd, relocation,
2941 sgot->contents + off);
2942 h->got.offset |= 1;
2946 relocation = sgot->output_offset + off;
2948 else
2950 bfd_vma off;
2951 bfd_byte *loc;
2953 BFD_ASSERT (local_got_offsets != NULL
2954 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2956 off = local_got_offsets[r_symndx];
2958 /* The offset must always be a multiple of 4. We use
2959 the least significant bit to record whether we have
2960 already processed this entry. */
2961 if ((off & 1) != 0)
2962 off &= ~1;
2963 else
2965 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2967 if (info->shared)
2969 asection *srelgot;
2970 Elf_Internal_Rela outrel;
2972 /* We need to generate a R_M32R_RELATIVE reloc
2973 for the dynamic linker. */
2974 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2975 BFD_ASSERT (srelgot != NULL);
2977 outrel.r_offset = (sgot->output_section->vma
2978 + sgot->output_offset
2979 + off);
2980 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2981 outrel.r_addend = relocation;
2982 loc = srelgot->contents;
2983 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2984 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2985 ++srelgot->reloc_count;
2988 local_got_offsets[r_symndx] |= 1;
2991 relocation = sgot->output_offset + off;
2993 if ((r_type == R_M32R_GOT16_HI_SLO)
2994 && ((relocation + rel->r_addend) & 0x8000))
2995 rel->r_addend += 0x10000;
2997 break;
2999 case R_M32R_26_PLTREL:
3000 /* Relocation is to the entry for this symbol in the
3001 procedure linkage table. */
3003 /* The native assembler will generate a 26_PLTREL reloc
3004 for a local symbol if you assemble a call from one
3005 section to another when using -K pic. */
3006 if (h == NULL)
3007 break;
3009 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3010 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
3011 // break;
3012 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
3013 break;
3015 if (h->plt.offset == (bfd_vma) -1)
3017 /* We didn't make a PLT entry for this symbol. This
3018 happens when statically linking PIC code, or when
3019 using -Bsymbolic. */
3020 break;
3023 relocation = (splt->output_section->vma
3024 + splt->output_offset
3025 + h->plt.offset);
3026 break;
3028 case R_M32R_HI16_SLO_RELA:
3030 if ((relocation + rel->r_addend) & 0x8000)
3032 rel->r_addend += 0x10000;
3035 /* Fall through. */
3036 case R_M32R_16_RELA:
3037 case R_M32R_24_RELA:
3038 case R_M32R_32_RELA:
3039 case R_M32R_18_PCREL_RELA:
3040 case R_M32R_26_PCREL_RELA:
3041 case R_M32R_HI16_ULO_RELA:
3042 case R_M32R_LO16_RELA:
3043 case R_M32R_SDA16_RELA:
3044 if (info->shared
3045 && r_symndx != 0
3046 && (input_section->flags & SEC_ALLOC) != 0
3047 && ((r_type != R_M32R_18_PCREL_RELA
3048 && r_type != R_M32R_26_PCREL_RELA)
3049 || (h != NULL
3050 && h->dynindx != -1
3051 && (! info->symbolic
3052 || (h->elf_link_hash_flags
3053 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3055 Elf_Internal_Rela outrel;
3056 bfd_boolean skip, relocate;
3057 bfd_byte *loc;
3059 /* When generating a shared object, these relocations
3060 are copied into the output file to be resolved at run
3061 time. */
3063 if (sreloc == NULL)
3065 const char *name;
3067 name = (bfd_elf_string_from_elf_section
3068 (input_bfd,
3069 elf_elfheader (input_bfd)->e_shstrndx,
3070 elf_section_data (input_section)->rel_hdr.sh_name));
3071 if (name == NULL)
3072 return FALSE;
3074 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3075 && strcmp (bfd_get_section_name (input_bfd,
3076 input_section),
3077 name + 5) == 0);
3079 sreloc = bfd_get_section_by_name (dynobj, name);
3080 BFD_ASSERT (sreloc != NULL);
3083 skip = FALSE;
3084 relocate = FALSE;
3086 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3087 info,
3088 input_section,
3089 rel->r_offset);
3090 if (outrel.r_offset == (bfd_vma) -1)
3091 skip = TRUE;
3092 else if (outrel.r_offset == (bfd_vma) -2)
3093 skip = TRUE, relocate = TRUE;
3094 outrel.r_offset += (input_section->output_section->vma
3095 + input_section->output_offset);
3097 if (skip)
3098 memset (&outrel, 0, sizeof outrel);
3099 else if (r_type == R_M32R_18_PCREL_RELA
3100 || r_type == R_M32R_26_PCREL_RELA)
3102 BFD_ASSERT (h != NULL && h->dynindx != -1);
3103 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3104 outrel.r_addend = rel->r_addend;
3106 else
3108 /* h->dynindx may be -1 if this symbol was marked to
3109 become local. */
3110 if (h == NULL
3111 || ((info->symbolic || h->dynindx == -1)
3112 && (h->elf_link_hash_flags
3113 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3115 relocate = TRUE;
3116 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3117 outrel.r_addend = relocation + rel->r_addend;
3119 else
3121 BFD_ASSERT (h->dynindx != -1);
3122 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3123 outrel.r_addend = relocation + rel->r_addend;
3127 loc = sreloc->contents;
3128 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3129 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3130 ++sreloc->reloc_count;
3132 /* If this reloc is against an external symbol, we do
3133 not want to fiddle with the addend. Otherwise, we
3134 need to include the symbol value so that it becomes
3135 an addend for the dynamic reloc. */
3136 if (! relocate)
3137 continue;
3139 break;
3141 case (int) R_M32R_10_PCREL :
3142 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3143 contents, offset,
3144 sec, relocation, addend);
3145 goto check_reloc;
3147 case (int) R_M32R_HI16_SLO :
3148 case (int) R_M32R_HI16_ULO :
3150 Elf_Internal_Rela *lorel;
3152 /* We allow an arbitrary number of HI16 relocs before the
3153 LO16 reloc. This permits gcc to emit the HI and LO relocs
3154 itself. */
3155 for (lorel = rel + 1;
3156 (lorel < relend
3157 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3158 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3159 lorel++)
3160 continue;
3161 if (lorel < relend
3162 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3164 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3165 contents, relocation + addend);
3166 r = bfd_reloc_ok;
3168 else
3169 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3170 contents, offset,
3171 relocation, addend);
3174 goto check_reloc;
3176 case (int) R_M32R_SDA16 :
3178 const char *name;
3180 BFD_ASSERT (sec != NULL);
3181 name = bfd_get_section_name (abfd, sec);
3183 if (strcmp (name, ".sdata") == 0
3184 || strcmp (name, ".sbss") == 0
3185 || strcmp (name, ".scommon") == 0)
3187 bfd_vma sda_base;
3188 bfd *out_bfd = sec->output_section->owner;
3190 r = m32r_elf_final_sda_base (out_bfd, info,
3191 &errmsg,
3192 &sda_base);
3193 if (r != bfd_reloc_ok)
3195 ret = FALSE;
3196 goto check_reloc;
3199 /* At this point `relocation' contains the object's
3200 address. */
3201 relocation -= sda_base;
3202 /* Now it contains the offset from _SDA_BASE_. */
3204 else
3206 (*_bfd_error_handler)
3207 (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
3208 bfd_archive_filename (input_bfd),
3209 sym_name,
3210 m32r_elf_howto_table[(int) r_type].name,
3211 bfd_get_section_name (abfd, sec));
3212 /*bfd_set_error (bfd_error_bad_value); ??? why? */
3213 ret = FALSE;
3214 continue;
3217 /* fall through */
3219 default : /* OLD_M32R_RELOC */
3221 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3222 contents, offset,
3223 relocation, addend);
3224 goto check_reloc;
3227 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3228 contents, rel->r_offset,
3229 relocation, rel->r_addend);
3233 check_reloc:
3235 if (r != bfd_reloc_ok)
3237 /* FIXME: This should be generic enough to go in a utility. */
3238 const char *name;
3240 if (h != NULL)
3241 name = h->root.root.string;
3242 else
3244 name = (bfd_elf_string_from_elf_section
3245 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3246 if (name == NULL || *name == '\0')
3247 name = bfd_section_name (input_bfd, sec);
3250 if (errmsg != NULL)
3251 goto common_error;
3253 switch (r)
3255 case bfd_reloc_overflow:
3256 if (! ((*info->callbacks->reloc_overflow)
3257 (info, name, howto->name, (bfd_vma) 0,
3258 input_bfd, input_section, offset)))
3259 return FALSE;
3260 break;
3262 case bfd_reloc_undefined:
3263 if (! ((*info->callbacks->undefined_symbol)
3264 (info, name, input_bfd, input_section,
3265 offset, TRUE)))
3266 return FALSE;
3267 break;
3269 case bfd_reloc_outofrange:
3270 errmsg = _("internal error: out of range error");
3271 goto common_error;
3273 case bfd_reloc_notsupported:
3274 errmsg = _("internal error: unsupported relocation error");
3275 goto common_error;
3277 case bfd_reloc_dangerous:
3278 errmsg = _("internal error: dangerous error");
3279 goto common_error;
3281 default:
3282 errmsg = _("internal error: unknown error");
3283 /* fall through */
3285 common_error:
3286 if (!((*info->callbacks->warning)
3287 (info, errmsg, name, input_bfd, input_section,
3288 offset)))
3289 return FALSE;
3290 break;
3295 return ret;
3298 /* Finish up dynamic symbol handling. We set the contents of various
3299 dynamic sections here. */
3300 static bfd_boolean
3301 m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3302 bfd *output_bfd;
3303 struct bfd_link_info *info;
3304 struct elf_link_hash_entry *h;
3305 Elf_Internal_Sym *sym;
3307 struct elf_m32r_link_hash_table *htab;
3308 bfd *dynobj;
3309 bfd_byte *loc;
3311 #ifdef DEBUG_PIC
3312 printf("m32r_elf_finish_dynamic_symbol()\n");
3313 #endif
3315 htab = m32r_elf_hash_table (info);
3316 dynobj = htab->root.dynobj;
3318 if (h->plt.offset != (bfd_vma) -1)
3320 asection *splt;
3321 asection *sgot;
3322 asection *srela;
3324 bfd_vma plt_index;
3325 bfd_vma got_offset;
3326 Elf_Internal_Rela rela;
3328 /* This symbol has an entry in the procedure linkage table. Set
3329 it up. */
3331 BFD_ASSERT (h->dynindx != -1);
3333 splt = htab->splt;
3334 sgot = htab->sgotplt;
3335 srela = htab->srelplt;
3336 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3338 /* Get the index in the procedure linkage table which
3339 corresponds to this symbol. This is the index of this symbol
3340 in all the symbols for which we are making plt entries. The
3341 first entry in the procedure linkage table is reserved. */
3342 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3344 /* Get the offset into the .got table of the entry that
3345 corresponds to this function. Each .got entry is 4 bytes.
3346 The first three are reserved. */
3347 got_offset = (plt_index + 3) * 4;
3349 /* Fill in the entry in the procedure linkage table. */
3350 if (! info->shared)
3352 bfd_put_32 (output_bfd,
3353 (PLT_ENTRY_WORD0b
3354 + (((sgot->output_section->vma
3355 + sgot->output_offset
3356 + got_offset) >> 16) & 0xffff)),
3357 splt->contents + h->plt.offset);
3358 bfd_put_32 (output_bfd,
3359 (PLT_ENTRY_WORD1b
3360 + ((sgot->output_section->vma
3361 + sgot->output_offset
3362 + got_offset) & 0xffff)),
3363 splt->contents + h->plt.offset + 4);
3364 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3365 splt->contents + h->plt.offset + 8);
3366 bfd_put_32 (output_bfd,
3367 (PLT_ENTRY_WORD3
3368 + plt_index * sizeof (Elf32_External_Rela)),
3369 splt->contents + h->plt.offset + 12);
3370 bfd_put_32 (output_bfd,
3371 (PLT_ENTRY_WORD4
3372 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3373 splt->contents + h->plt.offset + 16);
3375 else
3377 bfd_put_32 (output_bfd,
3378 PLT_ENTRY_WORD0 + got_offset,
3379 splt->contents + h->plt.offset);
3380 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3381 splt->contents + h->plt.offset + 4);
3382 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3383 splt->contents + h->plt.offset + 8);
3384 bfd_put_32 (output_bfd,
3385 (PLT_ENTRY_WORD3
3386 + plt_index * sizeof (Elf32_External_Rela)),
3387 splt->contents + h->plt.offset + 12);
3388 bfd_put_32 (output_bfd,
3389 (PLT_ENTRY_WORD4
3390 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3391 splt->contents + h->plt.offset + 16);
3394 /* Fill in the entry in the global offset table. */
3395 bfd_put_32 (output_bfd,
3396 (splt->output_section->vma
3397 + splt->output_offset
3398 + h->plt.offset
3399 + 12), /* same offset */
3400 sgot->contents + got_offset);
3402 /* Fill in the entry in the .rela.plt section. */
3403 rela.r_offset = (sgot->output_section->vma
3404 + sgot->output_offset
3405 + got_offset);
3406 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3407 rela.r_addend = 0;
3408 loc = srela->contents;
3409 loc += plt_index * sizeof(Elf32_External_Rela);
3410 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3412 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3414 /* Mark the symbol as undefined, rather than as defined in
3415 the .plt section. Leave the value alone. */
3416 sym->st_shndx = SHN_UNDEF;
3420 if (h->got.offset != (bfd_vma) -1)
3422 asection *sgot;
3423 asection *srela;
3424 Elf_Internal_Rela rela;
3426 /* This symbol has an entry in the global offset table. Set it
3427 up. */
3429 sgot = htab->sgot;
3430 srela = htab->srelgot;
3431 BFD_ASSERT (sgot != NULL && srela != NULL);
3433 rela.r_offset = (sgot->output_section->vma
3434 + sgot->output_offset
3435 + (h->got.offset &~ 1));
3437 /* If this is a -Bsymbolic link, and the symbol is defined
3438 locally, we just want to emit a RELATIVE reloc. Likewise if
3439 the symbol was forced to be local because of a version file.
3440 The entry in the global offset table will already have been
3441 initialized in the relocate_section function. */
3442 if (info->shared
3443 && (info->symbolic
3444 || h->dynindx == -1
3445 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3446 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3448 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3449 rela.r_addend = (h->root.u.def.value
3450 + h->root.u.def.section->output_section->vma
3451 + h->root.u.def.section->output_offset);
3453 else
3455 BFD_ASSERT((h->got.offset & 1) == 0);
3456 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3457 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3458 rela.r_addend = 0;
3461 loc = srela->contents;
3462 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3463 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3464 ++srela->reloc_count;
3467 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3469 asection *s;
3470 Elf_Internal_Rela rela;
3472 /* This symbols needs a copy reloc. Set it up. */
3474 BFD_ASSERT (h->dynindx != -1
3475 && (h->root.type == bfd_link_hash_defined
3476 || h->root.type == bfd_link_hash_defweak));
3478 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3479 ".rela.bss");
3480 BFD_ASSERT (s != NULL);
3482 rela.r_offset = (h->root.u.def.value
3483 + h->root.u.def.section->output_section->vma
3484 + h->root.u.def.section->output_offset);
3485 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3486 rela.r_addend = 0;
3487 loc = s->contents;
3488 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3489 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3490 ++s->reloc_count;
3493 /* Mark some specially defined symbols as absolute. */
3494 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3495 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3496 sym->st_shndx = SHN_ABS;
3498 return TRUE;
3502 /* Finish up the dynamic sections. */
3504 static bfd_boolean
3505 m32r_elf_finish_dynamic_sections (output_bfd, info)
3506 bfd *output_bfd;
3507 struct bfd_link_info *info;
3509 struct elf_m32r_link_hash_table *htab;
3510 bfd *dynobj;
3511 asection *sdyn;
3512 asection *sgot;
3514 #ifdef DEBUG_PIC
3515 printf("m32r_elf_finish_dynamic_sections()\n");
3516 #endif
3518 htab = m32r_elf_hash_table (info);
3519 dynobj = htab->root.dynobj;
3521 sgot = htab->sgotplt;
3522 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3524 if (htab->root.dynamic_sections_created)
3526 asection *splt;
3527 Elf32_External_Dyn *dyncon, *dynconend;
3529 BFD_ASSERT (sgot != NULL && sdyn != NULL);
3531 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3532 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3534 for (; dyncon < dynconend; dyncon++)
3536 Elf_Internal_Dyn dyn;
3537 const char *name;
3538 asection *s;
3540 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3542 switch (dyn.d_tag)
3544 default:
3545 break;
3547 case DT_PLTGOT:
3548 name = ".got";
3549 s = htab->sgot->output_section;
3550 goto get_vma;
3551 case DT_JMPREL:
3552 name = ".rela.plt";
3553 s = htab->srelplt->output_section;
3554 get_vma:
3555 BFD_ASSERT (s != NULL);
3556 dyn.d_un.d_ptr = s->vma;
3557 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3558 break;
3560 case DT_PLTRELSZ:
3561 s = htab->srelplt->output_section;
3562 BFD_ASSERT (s != NULL);
3563 dyn.d_un.d_val = s->size;
3564 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3565 break;
3567 case DT_RELASZ:
3568 /* My reading of the SVR4 ABI indicates that the
3569 procedure linkage table relocs (DT_JMPREL) should be
3570 included in the overall relocs (DT_RELA). This is
3571 what Solaris does. However, UnixWare can not handle
3572 that case. Therefore, we override the DT_RELASZ entry
3573 here to make it not include the JMPREL relocs. Since
3574 the linker script arranges for .rela.plt to follow all
3575 other relocation sections, we don't have to worry
3576 about changing the DT_RELA entry. */
3577 if (htab->srelplt != NULL)
3579 s = htab->srelplt->output_section;
3580 dyn.d_un.d_val -= s->size;
3582 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3583 break;
3587 /* Fill in the first entry in the procedure linkage table. */
3588 splt = htab->splt;
3589 if (splt && splt->size > 0)
3591 if (info->shared)
3593 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3594 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3595 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3596 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3597 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3599 else
3601 unsigned long addr;
3602 /* addr = .got + 4 */
3603 addr = sgot->output_section->vma + sgot->output_offset + 4;
3604 bfd_put_32 (output_bfd,
3605 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3606 splt->contents);
3607 bfd_put_32 (output_bfd,
3608 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3609 splt->contents + 4);
3610 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3611 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3612 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3615 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3616 PLT_ENTRY_SIZE;
3620 /* Fill in the first three entries in the global offset table. */
3621 if (sgot && sgot->size > 0)
3623 if (sdyn == NULL)
3624 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3625 else
3626 bfd_put_32 (output_bfd,
3627 sdyn->output_section->vma + sdyn->output_offset,
3628 sgot->contents);
3629 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3630 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3632 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3635 return TRUE;
3639 #if 0 /* relaxing not supported yet */
3641 /* This function handles relaxing for the m32r.
3642 Relaxing on the m32r is tricky because of instruction alignment
3643 requirements (4 byte instructions must be aligned on 4 byte boundaries).
3645 The following relaxing opportunities are handled:
3647 seth/add3/jl -> bl24 or bl8
3648 seth/add3 -> ld24
3650 It would be nice to handle bl24 -> bl8 but given:
3652 - 4 byte insns must be on 4 byte boundaries
3653 - branch instructions only branch to insns on 4 byte boundaries
3655 this isn't much of a win because the insn in the 2 "deleted" bytes
3656 must become a nop. With some complexity some real relaxation could be
3657 done but the frequency just wouldn't make it worth it; it's better to
3658 try to do all the code compaction one can elsewhere.
3659 When the chip supports parallel 16 bit insns, things may change.
3662 static bfd_boolean
3663 m32r_elf_relax_section (abfd, sec, link_info, again)
3664 bfd *abfd;
3665 asection *sec;
3666 struct bfd_link_info *link_info;
3667 bfd_boolean *again;
3669 Elf_Internal_Shdr *symtab_hdr;
3670 /* The Rela structures are used here because that's what
3671 _bfd_elf_link_read_relocs uses [for convenience - it sets the addend
3672 field to 0]. */
3673 Elf_Internal_Rela *internal_relocs = NULL;
3674 Elf_Internal_Rela *irel, *irelend;
3675 bfd_byte *contents = NULL;
3676 Elf_Internal_Sym *isymbuf = NULL;
3678 /* Assume nothing changes. */
3679 *again = FALSE;
3681 /* We don't have to do anything for a relocatable link, if
3682 this section does not have relocs, or if this is not a
3683 code section. */
3684 if (link_info->relocatable
3685 || (sec->flags & SEC_RELOC) == 0
3686 || sec->reloc_count == 0
3687 || (sec->flags & SEC_CODE) == 0
3688 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
3689 return TRUE;
3691 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3693 /* Get a copy of the native relocations. */
3694 internal_relocs = (_bfd_elf_link_read_relocs
3695 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3696 link_info->keep_memory));
3697 if (internal_relocs == NULL)
3698 goto error_return;
3700 /* Walk through them looking for relaxing opportunities. */
3701 irelend = internal_relocs + sec->reloc_count;
3702 for (irel = internal_relocs; irel < irelend; irel++)
3704 bfd_vma symval;
3706 /* If this isn't something that can be relaxed, then ignore
3707 this reloc. */
3708 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
3709 continue;
3711 /* Get the section contents if we haven't done so already. */
3712 if (contents == NULL)
3714 /* Get cached copy if it exists. */
3715 if (elf_section_data (sec)->this_hdr.contents != NULL)
3716 contents = elf_section_data (sec)->this_hdr.contents;
3717 else
3719 /* Go get them off disk. */
3720 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3721 goto error_return;
3725 /* Read this BFD's local symbols if we haven't done so already. */
3726 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3728 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3729 if (isymbuf == NULL)
3730 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3731 symtab_hdr->sh_info, 0,
3732 NULL, NULL, NULL);
3733 if (isymbuf == NULL)
3734 goto error_return;
3737 /* Get the value of the symbol referred to by the reloc. */
3738 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3740 /* A local symbol. */
3741 Elf_Internal_Sym *isym;
3742 asection *sym_sec;
3744 isym = isymbuf + ELF32_R_SYM (irel->r_info),
3745 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3746 symval = (isym->st_value
3747 + sym_sec->output_section->vma
3748 + sym_sec->output_offset);
3750 else
3752 unsigned long indx;
3753 struct elf_link_hash_entry *h;
3755 /* An external symbol. */
3756 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3757 h = elf_sym_hashes (abfd)[indx];
3758 BFD_ASSERT (h != NULL);
3759 if (h->root.type != bfd_link_hash_defined
3760 && h->root.type != bfd_link_hash_defweak)
3762 /* This appears to be a reference to an undefined
3763 symbol. Just ignore it--it will be caught by the
3764 regular reloc processing. */
3765 continue;
3768 symval = (h->root.u.def.value
3769 + h->root.u.def.section->output_section->vma
3770 + h->root.u.def.section->output_offset);
3773 /* For simplicity of coding, we are going to modify the section
3774 contents, the section relocs, and the BFD symbol table. We
3775 must tell the rest of the code not to free up this
3776 information. It would be possible to instead create a table
3777 of changes which have to be made, as is done in coff-mips.c;
3778 that would be more work, but would require less memory when
3779 the linker is run. */
3781 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
3782 This sequence is generated by the compiler when compiling in
3783 32 bit mode. Also look for seth/add3 -> ld24. */
3785 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
3787 Elf_Internal_Rela *nrel;
3788 bfd_vma pc = (sec->output_section->vma + sec->output_offset
3789 + irel->r_offset);
3790 bfd_signed_vma pcrel_value = symval - pc;
3791 unsigned int code,reg;
3792 int addend,nop_p,bl8_p,to_delete;
3794 /* The tests are ordered so that we get out as quickly as possible
3795 if this isn't something we can relax, taking into account that
3796 we are looking for two separate possibilities (jl/ld24). */
3798 /* Do nothing if no room in the section for this to be what we're
3799 looking for. */
3800 if (irel->r_offset > sec->size - 8)
3801 continue;
3803 /* Make sure the next relocation applies to the next
3804 instruction and that it's the add3's reloc. */
3805 nrel = irel + 1;
3806 if (nrel == irelend
3807 || irel->r_offset + 4 != nrel->r_offset
3808 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
3809 continue;
3811 /* See if the instructions are seth/add3. */
3812 /* FIXME: This is where macros from cgen can come in. */
3813 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
3814 if ((code & 0xf0ff) != 0xd0c0)
3815 continue; /* not seth rN,foo */
3816 reg = (code & 0x0f00) >> 8;
3817 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
3818 if (code != (0x80a0 | reg | (reg << 8)))
3819 continue; /* not add3 rN,rN,foo */
3821 /* At this point we've confirmed we have seth/add3. Now check
3822 whether the next insn is a jl, in which case try to change this
3823 to bl24 or bl8. */
3825 /* Ensure the branch target is in range.
3826 The bl24 instruction has a 24 bit operand which is the target
3827 address right shifted by 2, giving a signed range of 26 bits.
3828 Note that 4 bytes are added to the high value because the target
3829 will be at least 4 bytes closer if we can relax. It'll actually
3830 be 4 or 8 bytes closer, but we don't know which just yet and
3831 the difference isn't significant enough to worry about. */
3832 #if !USE_REL /* put in for learning purposes */
3833 pcrel_value += irel->r_addend;
3834 #else
3835 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
3836 pcrel_value += addend;
3837 #endif
3839 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
3840 /* Do nothing if no room in the section for this to be what we're
3841 looking for. */
3842 && (irel->r_offset <= sec->size - 12)
3843 /* Ensure the next insn is "jl rN". */
3844 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
3845 code != (0x1ec0 | reg)))
3847 /* We can relax to bl24/bl8. */
3849 /* See if there's a nop following the jl.
3850 Also see if we can use a bl8 insn. */
3851 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
3852 nop_p = (code & 0x7fff) == NOP_INSN;
3853 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
3855 if (bl8_p)
3857 /* Change "seth rN,foo" to "bl8 foo || nop".
3858 We OR in CODE just in case it's not a nop (technically,
3859 CODE currently must be a nop, but for cleanness we
3860 allow it to be anything). */
3861 #if !USE_REL /* put in for learning purposes */
3862 code = 0x7e000000 | MAKE_PARALLEL (code);
3863 #else
3864 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
3865 #endif
3866 to_delete = 8;
3868 else
3870 /* Change the seth rN,foo to a bl24 foo. */
3871 #if !USE_REL /* put in for learning purposes */
3872 code = 0xfe000000;
3873 #else
3874 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
3875 #endif
3876 to_delete = nop_p ? 8 : 4;
3879 bfd_put_32 (abfd, code, contents + irel->r_offset);
3881 /* Set the new reloc type. */
3882 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3883 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
3885 /* Delete the add3 reloc by making it a null reloc. */
3886 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3887 R_M32R_NONE);
3889 else if (addend >= 0
3890 && symval + addend <= 0xffffff)
3892 /* We can relax to ld24. */
3894 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
3895 bfd_put_32 (abfd, code, contents + irel->r_offset);
3896 to_delete = 4;
3897 /* Tell the following code a nop filler isn't needed. */
3898 nop_p = 1;
3900 else
3902 /* Can't do anything here. */
3903 continue;
3906 /* Note that we've changed the relocs, section contents, etc. */
3907 elf_section_data (sec)->relocs = internal_relocs;
3908 elf_section_data (sec)->this_hdr.contents = contents;
3909 symtab_hdr->contents = (unsigned char *) isymbuf;
3911 /* Delete TO_DELETE bytes of data. */
3912 if (!m32r_elf_relax_delete_bytes (abfd, sec,
3913 irel->r_offset + 4, to_delete))
3914 goto error_return;
3916 /* Now that the following bytes have been moved into place, see if
3917 we need to replace the jl with a nop. This happens when we had
3918 to use a bl24 insn and the insn following the jl isn't a nop.
3919 Technically, this situation can't happen (since the insn can
3920 never be executed) but to be clean we do this. When the chip
3921 supports parallel 16 bit insns things may change.
3922 We don't need to do this in the case of relaxing to ld24,
3923 and the above code sets nop_p so this isn't done. */
3924 if (! nop_p && to_delete == 4)
3925 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
3927 /* That will change things, so we should relax again.
3928 Note that this is not required, and it may be slow. */
3929 *again = TRUE;
3931 continue;
3934 /* loop to try the next reloc */
3937 if (isymbuf != NULL
3938 && symtab_hdr->contents != (unsigned char *) isymbuf)
3940 if (! link_info->keep_memory)
3941 free (isymbuf);
3942 else
3944 /* Cache the symbols for elf_link_input_bfd. */
3945 symtab_hdr->contents = (unsigned char *) isymbuf;
3949 if (contents != NULL
3950 && elf_section_data (sec)->this_hdr.contents != contents)
3952 if (! link_info->keep_memory)
3953 free (contents);
3954 else
3956 /* Cache the section contents for elf_link_input_bfd. */
3957 elf_section_data (sec)->this_hdr.contents = contents;
3961 if (internal_relocs != NULL
3962 && elf_section_data (sec)->relocs != internal_relocs)
3963 free (internal_relocs);
3965 return TRUE;
3967 error_return:
3968 if (isymbuf != NULL
3969 && symtab_hdr->contents != (unsigned char *) isymbuf)
3970 free (isymbuf);
3971 if (contents != NULL
3972 && elf_section_data (sec)->this_hdr.contents != contents)
3973 free (contents);
3974 if (internal_relocs != NULL
3975 && elf_section_data (sec)->relocs != internal_relocs)
3976 free (internal_relocs);
3978 return FALSE;
3981 /* Delete some bytes from a section while relaxing. */
3983 static bfd_boolean
3984 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
3985 bfd *abfd;
3986 asection *sec;
3987 bfd_vma addr;
3988 int count;
3990 Elf_Internal_Shdr *symtab_hdr;
3991 int shndx;
3992 bfd_byte *contents;
3993 Elf_Internal_Rela *irel, *irelend;
3994 Elf_Internal_Rela *irelalign;
3995 bfd_vma toaddr;
3996 Elf_Internal_Sym *isym, *isymend;
3997 struct elf_link_hash_entry **sym_hashes;
3998 struct elf_link_hash_entry **end_hashes;
3999 unsigned int symcount;
4001 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4003 contents = elf_section_data (sec)->this_hdr.contents;
4005 /* The deletion must stop at the next ALIGN reloc for an aligment
4006 power larger than the number of bytes we are deleting. */
4008 irelalign = NULL;
4009 toaddr = sec->size;
4011 irel = elf_section_data (sec)->relocs;
4012 irelend = irel + sec->reloc_count;
4014 /* Actually delete the bytes. */
4015 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
4016 sec->size -= count;
4018 /* Adjust all the relocs. */
4019 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
4021 /* Get the new reloc address. */
4022 if ((irel->r_offset > addr
4023 && irel->r_offset < toaddr))
4024 irel->r_offset -= count;
4027 /* Adjust the local symbols defined in this section. */
4028 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4029 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
4030 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
4032 if (isym->st_shndx == shndx
4033 && isym->st_value > addr
4034 && isym->st_value < toaddr)
4035 isym->st_value -= count;
4038 /* Now adjust the global symbols defined in this section. */
4039 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
4040 - symtab_hdr->sh_info);
4041 sym_hashes = elf_sym_hashes (abfd);
4042 end_hashes = sym_hashes + symcount;
4043 for (; sym_hashes < end_hashes; sym_hashes++)
4045 struct elf_link_hash_entry *sym_hash = *sym_hashes;
4047 if ((sym_hash->root.type == bfd_link_hash_defined
4048 || sym_hash->root.type == bfd_link_hash_defweak)
4049 && sym_hash->root.u.def.section == sec
4050 && sym_hash->root.u.def.value > addr
4051 && sym_hash->root.u.def.value < toaddr)
4053 sym_hash->root.u.def.value -= count;
4057 return TRUE;
4060 /* This is a version of bfd_generic_get_relocated_section_contents
4061 which uses m32r_elf_relocate_section. */
4063 static bfd_byte *
4064 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
4065 data, relocatable, symbols)
4066 bfd *output_bfd;
4067 struct bfd_link_info *link_info;
4068 struct bfd_link_order *link_order;
4069 bfd_byte *data;
4070 bfd_boolean relocatable;
4071 asymbol **symbols;
4073 Elf_Internal_Shdr *symtab_hdr;
4074 asection *input_section = link_order->u.indirect.section;
4075 bfd *input_bfd = input_section->owner;
4076 asection **sections = NULL;
4077 Elf_Internal_Rela *internal_relocs = NULL;
4078 Elf_Internal_Sym *isymbuf = NULL;
4079 bfd_size_type amt;
4081 /* We only need to handle the case of relaxing, or of having a
4082 particular set of section contents, specially. */
4083 if (relocatable
4084 || elf_section_data (input_section)->this_hdr.contents == NULL)
4085 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4086 link_order, data,
4087 relocatable,
4088 symbols);
4090 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4092 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4093 input_section->size);
4095 if ((input_section->flags & SEC_RELOC) != 0
4096 && input_section->reloc_count > 0)
4098 Elf_Internal_Sym *isymp;
4099 asection **secpp;
4100 Elf32_External_Sym *esym, *esymend;
4102 internal_relocs = (_bfd_elf_link_read_relocs
4103 (input_bfd, input_section, (PTR) NULL,
4104 (Elf_Internal_Rela *) NULL, FALSE));
4105 if (internal_relocs == NULL)
4106 goto error_return;
4108 if (symtab_hdr->sh_info != 0)
4110 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4111 if (isymbuf == NULL)
4112 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4113 symtab_hdr->sh_info, 0,
4114 NULL, NULL, NULL);
4115 if (isymbuf == NULL)
4116 goto error_return;
4119 amt = symtab_hdr->sh_info;
4120 amt *= sizeof (asection *);
4121 sections = (asection **) bfd_malloc (amt);
4122 if (sections == NULL && symtab_hdr->sh_info > 0)
4123 goto error_return;
4125 isymend = isymbuf + symtab_hdr->sh_info;
4126 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4128 asection *isec;
4130 if (isym->st_shndx == SHN_UNDEF)
4131 isec = bfd_und_section_ptr;
4132 else if (isym->st_shndx == SHN_ABS)
4133 isec = bfd_abs_section_ptr;
4134 else if (isym->st_shndx == SHN_COMMON)
4135 isec = bfd_com_section_ptr;
4136 else if (isym->st_shndx == SHN_M32R_SCOMMON)
4137 isec = &m32r_elf_scom_section;
4138 else
4139 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4141 *secpp = isec;
4144 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
4145 input_section, data, internal_relocs,
4146 isymbuf, sections))
4147 goto error_return;
4149 if (sections != NULL)
4150 free (sections);
4151 if (isymbuf != NULL
4152 && symtab_hdr->contents != (unsigned char *) isymbuf)
4153 free (isymbuf);
4154 if (elf_section_data (input_section)->relocs != internal_relocs)
4155 free (internal_relocs);
4158 return data;
4160 error_return:
4161 if (sections != NULL)
4162 free (sections);
4163 if (isymbuf != NULL
4164 && symtab_hdr->contents != (unsigned char *) isymbuf)
4165 free (isymbuf);
4166 if (internal_relocs != NULL
4167 && elf_section_data (input_section)->relocs != internal_relocs)
4168 free (internal_relocs);
4169 return NULL;
4172 #endif /* #if 0 */
4174 /* Set the right machine number. */
4175 static bfd_boolean
4176 m32r_elf_object_p (abfd)
4177 bfd *abfd;
4179 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4181 default:
4182 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
4183 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
4184 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
4186 return TRUE;
4189 /* Store the machine number in the flags field. */
4190 static void
4191 m32r_elf_final_write_processing (abfd, linker)
4192 bfd *abfd;
4193 bfd_boolean linker ATTRIBUTE_UNUSED;
4195 unsigned long val;
4197 switch (bfd_get_mach (abfd))
4199 default:
4200 case bfd_mach_m32r: val = E_M32R_ARCH; break;
4201 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
4202 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
4205 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
4206 elf_elfheader (abfd)->e_flags |= val;
4209 /* Function to keep M32R specific file flags. */
4210 static bfd_boolean
4211 m32r_elf_set_private_flags (abfd, flags)
4212 bfd *abfd;
4213 flagword flags;
4215 BFD_ASSERT (!elf_flags_init (abfd)
4216 || elf_elfheader (abfd)->e_flags == flags);
4218 elf_elfheader (abfd)->e_flags = flags;
4219 elf_flags_init (abfd) = TRUE;
4220 return TRUE;
4223 /* Merge backend specific data from an object file to the output
4224 object file when linking. */
4225 static bfd_boolean
4226 m32r_elf_merge_private_bfd_data (ibfd, obfd)
4227 bfd *ibfd;
4228 bfd *obfd;
4230 flagword out_flags;
4231 flagword in_flags;
4233 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4234 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4235 return TRUE;
4237 in_flags = elf_elfheader (ibfd)->e_flags;
4238 out_flags = elf_elfheader (obfd)->e_flags;
4240 if (! elf_flags_init (obfd))
4242 /* If the input is the default architecture then do not
4243 bother setting the flags for the output architecture,
4244 instead allow future merges to do this. If no future
4245 merges ever set these flags then they will retain their
4246 unitialised values, which surprise surprise, correspond
4247 to the default values. */
4248 if (bfd_get_arch_info (ibfd)->the_default)
4249 return TRUE;
4251 elf_flags_init (obfd) = TRUE;
4252 elf_elfheader (obfd)->e_flags = in_flags;
4254 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4255 && bfd_get_arch_info (obfd)->the_default)
4257 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
4260 return TRUE;
4263 /* Check flag compatibility. */
4264 if (in_flags == out_flags)
4265 return TRUE;
4267 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
4269 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
4270 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4271 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
4273 (*_bfd_error_handler)
4274 (_("%s: Instruction set mismatch with previous modules"),
4275 bfd_archive_filename (ibfd));
4277 bfd_set_error (bfd_error_bad_value);
4278 return FALSE;
4282 return TRUE;
4285 /* Display the flags field */
4286 static bfd_boolean
4287 m32r_elf_print_private_bfd_data (abfd, ptr)
4288 bfd *abfd;
4289 PTR ptr;
4291 FILE * file = (FILE *) ptr;
4293 BFD_ASSERT (abfd != NULL && ptr != NULL)
4295 _bfd_elf_print_private_bfd_data (abfd, ptr);
4297 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
4299 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4301 default:
4302 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
4303 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
4304 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
4307 fputc ('\n', file);
4309 return TRUE;
4312 asection *
4313 m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
4314 asection *sec;
4315 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4316 Elf_Internal_Rela *rel;
4317 struct elf_link_hash_entry *h;
4318 Elf_Internal_Sym *sym;
4320 if (h != NULL)
4322 switch (ELF32_R_TYPE (rel->r_info))
4324 case R_M32R_GNU_VTINHERIT:
4325 case R_M32R_GNU_VTENTRY:
4326 case R_M32R_RELA_GNU_VTINHERIT:
4327 case R_M32R_RELA_GNU_VTENTRY:
4328 break;
4330 default:
4331 switch (h->root.type)
4333 case bfd_link_hash_defined:
4334 case bfd_link_hash_defweak:
4335 return h->root.u.def.section;
4337 case bfd_link_hash_common:
4338 return h->root.u.c.p->section;
4340 default:
4341 break;
4345 else
4346 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4348 return NULL;
4351 static bfd_boolean
4352 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
4353 bfd *abfd ATTRIBUTE_UNUSED;
4354 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4355 asection *sec ATTRIBUTE_UNUSED;
4356 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
4358 /* Update the got entry reference counts for the section being removed. */
4359 Elf_Internal_Shdr *symtab_hdr;
4360 struct elf_link_hash_entry **sym_hashes;
4361 bfd_signed_vma *local_got_refcounts;
4362 const Elf_Internal_Rela *rel, *relend;
4363 unsigned long r_symndx;
4364 struct elf_link_hash_entry *h;
4366 elf_section_data (sec)->local_dynrel = NULL;
4368 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4369 sym_hashes = elf_sym_hashes (abfd);
4370 local_got_refcounts = elf_local_got_refcounts (abfd);
4372 relend = relocs + sec->reloc_count;
4373 for (rel = relocs; rel < relend; rel++)
4374 switch (ELF32_R_TYPE (rel->r_info))
4376 case R_M32R_GOT16_HI_ULO:
4377 case R_M32R_GOT16_HI_SLO:
4378 case R_M32R_GOT16_LO:
4379 case R_M32R_GOTOFF:
4380 case R_M32R_GOTOFF_HI_ULO:
4381 case R_M32R_GOTOFF_HI_SLO:
4382 case R_M32R_GOTOFF_LO:
4383 case R_M32R_GOT24:
4384 case R_M32R_GOTPC_HI_ULO:
4385 case R_M32R_GOTPC_HI_SLO:
4386 case R_M32R_GOTPC_LO:
4387 case R_M32R_GOTPC24:
4388 r_symndx = ELF32_R_SYM (rel->r_info);
4389 if (r_symndx >= symtab_hdr->sh_info)
4391 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4392 if (h->got.refcount > 0)
4393 h->got.refcount--;
4395 else
4397 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
4398 local_got_refcounts[r_symndx]--;
4400 break;
4402 case R_M32R_16_RELA:
4403 case R_M32R_24_RELA:
4404 case R_M32R_32_RELA:
4405 case R_M32R_HI16_ULO_RELA:
4406 case R_M32R_HI16_SLO_RELA:
4407 case R_M32R_LO16_RELA:
4408 case R_M32R_SDA16_RELA:
4409 case R_M32R_18_PCREL_RELA:
4410 case R_M32R_26_PCREL_RELA:
4411 r_symndx = ELF32_R_SYM (rel->r_info);
4412 if (r_symndx >= symtab_hdr->sh_info)
4414 struct elf_m32r_link_hash_entry *eh;
4415 struct elf_m32r_dyn_relocs **pp;
4416 struct elf_m32r_dyn_relocs *p;
4418 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4420 if (!info->shared && h->plt.refcount > 0)
4421 h->plt.refcount -= 1;
4423 eh = (struct elf_m32r_link_hash_entry *) h;
4425 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4426 if (p->sec == sec)
4428 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4429 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
4430 p->pc_count -= 1;
4431 p->count -= 1;
4432 if (p->count == 0)
4433 *pp = p->next;
4434 break;
4437 break;
4439 case R_M32R_26_PLTREL:
4440 r_symndx = ELF32_R_SYM (rel->r_info);
4441 if (r_symndx >= symtab_hdr->sh_info)
4443 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4444 if (h->plt.refcount > 0)
4445 h->plt.refcount--;
4447 break;
4449 default:
4450 break;
4453 return TRUE;
4456 /* Look through the relocs for a section during the first phase.
4457 Since we don't do .gots or .plts, we just need to consider the
4458 virtual table relocs for gc. */
4460 static bfd_boolean
4461 m32r_elf_check_relocs (abfd, info, sec, relocs)
4462 bfd *abfd;
4463 struct bfd_link_info *info;
4464 asection *sec;
4465 const Elf_Internal_Rela *relocs;
4467 Elf_Internal_Shdr *symtab_hdr;
4468 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4469 const Elf_Internal_Rela *rel;
4470 const Elf_Internal_Rela *rel_end;
4471 struct elf_m32r_link_hash_table *htab;
4472 bfd *dynobj;
4473 bfd_vma *local_got_offsets;
4474 asection *sgot, *srelgot, *sreloc;
4476 if (info->relocatable)
4477 return TRUE;
4479 sgot = srelgot = sreloc = NULL;
4481 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4482 sym_hashes = elf_sym_hashes (abfd);
4483 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
4484 if (!elf_bad_symtab (abfd))
4485 sym_hashes_end -= symtab_hdr->sh_info;
4487 htab = m32r_elf_hash_table (info);
4488 dynobj = htab->root.dynobj;
4489 local_got_offsets = elf_local_got_offsets (abfd);
4491 rel_end = relocs + sec->reloc_count;
4492 for (rel = relocs; rel < rel_end; rel++)
4494 int r_type;
4495 struct elf_link_hash_entry *h;
4496 unsigned long r_symndx;
4498 r_symndx = ELF32_R_SYM (rel->r_info);
4499 r_type = ELF32_R_TYPE (rel->r_info);
4500 if (r_symndx < symtab_hdr->sh_info)
4501 h = NULL;
4502 else
4503 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4505 /* Some relocs require a global offset table. */
4506 if (htab->sgot == NULL)
4508 switch (r_type)
4510 case R_M32R_GOT16_HI_ULO:
4511 case R_M32R_GOT16_HI_SLO:
4512 case R_M32R_GOTOFF:
4513 case R_M32R_GOTOFF_HI_ULO:
4514 case R_M32R_GOTOFF_HI_SLO:
4515 case R_M32R_GOTOFF_LO:
4516 case R_M32R_GOT16_LO:
4517 case R_M32R_GOTPC24:
4518 case R_M32R_GOTPC_HI_ULO:
4519 case R_M32R_GOTPC_HI_SLO:
4520 case R_M32R_GOTPC_LO:
4521 case R_M32R_GOT24:
4522 if (dynobj == NULL)
4523 htab->root.dynobj = dynobj = abfd;
4524 if (! create_got_section (dynobj, info))
4525 return FALSE;
4526 break;
4528 default:
4529 break;
4533 switch (r_type)
4535 case R_M32R_GOT16_HI_ULO:
4536 case R_M32R_GOT16_HI_SLO:
4537 case R_M32R_GOT16_LO:
4538 case R_M32R_GOT24:
4540 if (h != NULL)
4541 h->got.refcount += 1;
4542 else
4544 bfd_signed_vma *local_got_refcounts;
4546 /* This is a global offset table entry for a local
4547 symbol. */
4548 local_got_refcounts = elf_local_got_refcounts (abfd);
4549 if (local_got_refcounts == NULL)
4551 bfd_size_type size;
4553 size = symtab_hdr->sh_info;
4554 size *= sizeof (bfd_signed_vma);
4555 local_got_refcounts = ((bfd_signed_vma *)
4556 bfd_zalloc (abfd, size));
4557 if (local_got_refcounts == NULL)
4558 return FALSE;
4559 elf_local_got_refcounts (abfd) = local_got_refcounts;
4561 local_got_refcounts[r_symndx] += 1;
4563 break;
4565 case R_M32R_26_PLTREL:
4566 /* This symbol requires a procedure linkage table entry. We
4567 actually build the entry in adjust_dynamic_symbol,
4568 because this might be a case of linking PIC code without
4569 linking in any dynamic objects, in which case we don't
4570 need to generate a procedure linkage table after all. */
4572 /* If this is a local symbol, we resolve it directly without
4573 creating a procedure linkage table entry. */
4574 if (h == NULL)
4575 continue;
4577 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4578 break;
4580 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4581 h->plt.refcount += 1;
4582 break;
4584 case R_M32R_16_RELA:
4585 case R_M32R_24_RELA:
4586 case R_M32R_32_RELA:
4587 case R_M32R_HI16_ULO_RELA:
4588 case R_M32R_HI16_SLO_RELA:
4589 case R_M32R_LO16_RELA:
4590 case R_M32R_SDA16_RELA:
4591 case R_M32R_18_PCREL_RELA:
4592 case R_M32R_26_PCREL_RELA:
4594 if (h != NULL && !info->shared)
4596 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
4597 h->plt.refcount += 1;
4600 /* If we are creating a shared library, and this is a reloc
4601 against a global symbol, or a non PC relative reloc
4602 against a local symbol, then we need to copy the reloc
4603 into the shared library. However, if we are linking with
4604 -Bsymbolic, we do not need to copy a reloc against a
4605 global symbol which is defined in an object we are
4606 including in the link (i.e., DEF_REGULAR is set). At
4607 this point we have not seen all the input files, so it is
4608 possible that DEF_REGULAR is not set now but will be set
4609 later (it is never cleared). We account for that
4610 possibility below by storing information in the
4611 dyn_relocs field of the hash table entry. A similar
4612 situation occurs when creating shared libraries and symbol
4613 visibility changes render the symbol local.
4615 If on the other hand, we are creating an executable, we
4616 may need to keep relocations for symbols satisfied by a
4617 dynamic library if we manage to avoid copy relocs for the
4618 symbol. */
4619 if ((info->shared
4620 && (sec->flags & SEC_ALLOC) != 0
4621 && ((r_type != R_M32R_26_PCREL_RELA
4622 && r_type != R_M32R_18_PCREL_RELA)
4623 || (h != NULL
4624 && (! info->symbolic
4625 || h->root.type == bfd_link_hash_defweak
4626 || (h->elf_link_hash_flags
4627 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4628 || (!info->shared
4629 && (sec->flags & SEC_ALLOC) != 0
4630 && h != NULL
4631 && (h->root.type == bfd_link_hash_defweak
4632 || (h->elf_link_hash_flags
4633 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
4635 struct elf_m32r_dyn_relocs *p;
4636 struct elf_m32r_dyn_relocs **head;
4638 if (dynobj == NULL)
4639 htab->root.dynobj = dynobj = abfd;
4641 /* When creating a shared object, we must copy these
4642 relocs into the output file. We create a reloc
4643 section in dynobj and make room for the reloc. */
4644 if (sreloc == NULL)
4646 const char *name;
4648 name = (bfd_elf_string_from_elf_section
4649 (abfd,
4650 elf_elfheader (abfd)->e_shstrndx,
4651 elf_section_data (sec)->rel_hdr.sh_name));
4652 if (name == NULL)
4653 return FALSE;
4655 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4656 && strcmp (bfd_get_section_name (abfd, sec),
4657 name + 5) == 0);
4659 sreloc = bfd_get_section_by_name (dynobj, name);
4660 if (sreloc == NULL)
4662 flagword flags;
4664 sreloc = bfd_make_section (dynobj, name);
4665 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4666 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4667 if ((sec->flags & SEC_ALLOC) != 0)
4668 flags |= SEC_ALLOC | SEC_LOAD;
4669 if (sreloc == NULL
4670 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4671 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4672 return FALSE;
4674 elf_section_data (sec)->sreloc = sreloc;
4677 /* If this is a global symbol, we count the number of
4678 relocations we need for this symbol. */
4679 if (h != NULL)
4680 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4681 else
4683 asection *s;
4685 /* Track dynamic relocs needed for local syms too. */
4686 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4687 sec, r_symndx);
4688 if (s == NULL)
4689 return FALSE;
4691 head = ((struct elf_m32r_dyn_relocs **)
4692 &elf_section_data (s)->local_dynrel);
4695 p = *head;
4696 if (p == NULL || p->sec != sec)
4698 bfd_size_type amt = sizeof (*p);
4699 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4700 if (p == NULL)
4701 return FALSE;
4702 p->next = *head;
4703 *head = p;
4704 p->sec = sec;
4705 p->count = 0;
4706 p->pc_count = 0;
4709 p->count += 1;
4710 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4711 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4712 p->pc_count += 1;
4714 break;
4716 /* This relocation describes the C++ object vtable hierarchy.
4717 Reconstruct it for later use during GC. */
4718 case R_M32R_RELA_GNU_VTINHERIT:
4719 case R_M32R_GNU_VTINHERIT:
4720 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4721 return FALSE;
4722 break;
4724 /* This relocation describes which C++ vtable entries are actually
4725 used. Record for later use during GC. */
4726 case R_M32R_GNU_VTENTRY:
4727 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4728 return FALSE;
4729 break;
4730 case R_M32R_RELA_GNU_VTENTRY:
4731 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4732 return FALSE;
4733 break;
4737 return TRUE;
4740 static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4742 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4743 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
4744 { NULL, 0, 0, 0, 0 }
4747 static bfd_boolean
4748 m32r_elf_fake_sections (abfd, hdr, sec)
4749 bfd *abfd;
4750 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4751 asection *sec;
4753 register const char *name;
4755 name = bfd_get_section_name (abfd, sec);
4757 /* The generic elf_fake_sections will set up REL_HDR using the
4758 default kind of relocations. But, we may actually need both
4759 kinds of relocations, so we set up the second header here.
4761 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4762 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4763 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4764 of the resulting empty .rela.<section> sections starts with
4765 sh_offset == object size, and ld doesn't allow that. While the check
4766 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4767 avoided by not emitting those useless sections in the first place. */
4768 if ((sec->flags & SEC_RELOC) != 0)
4770 struct bfd_elf_section_data *esd;
4771 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4773 esd = elf_section_data (sec);
4774 BFD_ASSERT (esd->rel_hdr2 == NULL);
4775 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4776 if (!esd->rel_hdr2)
4777 return FALSE;
4778 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4779 !sec->use_rela_p);
4782 return TRUE;
4785 static enum elf_reloc_type_class
4786 m32r_elf_reloc_type_class (rela)
4787 const Elf_Internal_Rela *rela;
4789 switch ((int) ELF32_R_TYPE (rela->r_info))
4791 case R_M32R_RELATIVE:
4792 return reloc_class_relative;
4793 case R_M32R_JMP_SLOT:
4794 return reloc_class_plt;
4795 case R_M32R_COPY:
4796 return reloc_class_copy;
4797 default:
4798 return reloc_class_normal;
4802 #define ELF_ARCH bfd_arch_m32r
4803 #define ELF_MACHINE_CODE EM_M32R
4804 #define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
4805 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
4807 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
4808 #define TARGET_BIG_NAME "elf32-m32r"
4809 #define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4810 #define TARGET_LITTLE_NAME "elf32-m32rle"
4812 #define elf_info_to_howto m32r_info_to_howto
4813 #define elf_info_to_howto_rel m32r_info_to_howto_rel
4814 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4815 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4816 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4817 #define elf_backend_relocate_section m32r_elf_relocate_section
4818 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4819 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4820 #define elf_backend_check_relocs m32r_elf_check_relocs
4822 #define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4823 #define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4824 #define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4825 #define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4826 #define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4827 #define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4828 #define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4829 #define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4831 #define elf_backend_can_gc_sections 1
4832 /*#if !USE_REL
4833 #define elf_backend_rela_normal 1
4834 #endif*/
4835 #define elf_backend_can_refcount 1
4836 #define elf_backend_want_got_plt 1
4837 #define elf_backend_plt_readonly 1
4838 #define elf_backend_want_plt_sym 0
4839 #define elf_backend_got_header_size 12
4841 #define elf_backend_may_use_rel_p 1
4842 #ifdef USE_M32R_OLD_RELOC
4843 #define elf_backend_default_use_rela_p 0
4844 #define elf_backend_may_use_rela_p 0
4845 #else
4846 #define elf_backend_default_use_rela_p 1
4847 #define elf_backend_may_use_rela_p 1
4848 #define elf_backend_fake_sections m32r_elf_fake_sections
4849 #endif
4851 #if 0 /* not yet */
4852 /* relax support */
4853 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
4854 #define bfd_elf32_bfd_get_relocated_section_contents \
4855 m32r_elf_get_relocated_section_contents
4856 #endif
4858 #define elf_backend_object_p m32r_elf_object_p
4859 #define elf_backend_final_write_processing m32r_elf_final_write_processing
4860 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4861 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4862 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
4863 #define elf_backend_special_sections m32r_elf_special_sections
4865 #include "elf32-target.h"
4867 #undef ELF_MAXPAGESIZE
4868 #define ELF_MAXPAGESIZE 0x1000
4870 #undef TARGET_BIG_SYM
4871 #define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4872 #undef TARGET_BIG_NAME
4873 #define TARGET_BIG_NAME "elf32-m32r-linux"
4874 #undef TARGET_LITTLE_SYM
4875 #define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4876 #undef TARGET_LITTLE_NAME
4877 #define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4878 #undef elf32_bed
4879 #define elf32_bed elf32_m32r_lin_bed
4881 #include "elf32-target.h"