2004-11-03 Randolph Chung <tausq@debian.org>
[deliverable/binutils-gdb.git] / bfd / elf32-m32r.c
1 /* M32R-specific support for 32-bit ELF.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/m32r.h"
26
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 *));
81
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 *));
86
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 *));
90
91 static bfd_boolean m32r_elf_create_dynamic_sections
92 PARAMS ((bfd *, struct bfd_link_info *));
93
94 static bfd_boolean m32r_elf_finish_dynamic_sections
95 PARAMS ((bfd *, struct bfd_link_info *));
96
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 *));
100
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 *));
109
110 #define NOP_INSN 0x7000
111 #define MAKE_PARALLEL(insn) ((insn) | 0x8000)
112
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
118
119 #ifndef USE_REL
120 #define USE_REL 0
121 #endif */
122 /* Use RELA. But use REL to link old objects for backwords compatibility. */
123
124 /* Functions for the M32R ELF linker. */
125
126 /* The name of the dynamic interpreter. This is put in the .interp
127 section. */
128
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
130
131 /* The nop opcode we use. */
132
133 #define M32R_NOP 0x7000f000
134
135 #define PLT_EMPTY 0x10101010 /* RIE -> RIE */
136
137 /* The size in bytes of an entry in the procedure linkage table. */
138
139 #define PLT_ENTRY_SIZE 20
140 #define PLT_HEADER_SIZE 20
141
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. */
145
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 */
151
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 */
157
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. */
165
166
167 static reloc_howto_type m32r_elf_howto_table[] =
168 {
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 */
183
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 */
198
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 */
213
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 */
228
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 */
250
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 */
265
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 */
284
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 */
299
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 */
314
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 */
329
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 */
344
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 */
359
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 */
374
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),
395
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 */
410
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 */
425
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 */
440
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 */
454
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 */
469
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 */
484
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 */
499
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 */
514
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 */
529
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 */
544
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 */
559
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 */
574
575 EMPTY_HOWTO (45),
576 EMPTY_HOWTO (46),
577 EMPTY_HOWTO (47),
578
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 */
594
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 */
610
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 */
629
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 */
645
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 */
660
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 */
677
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 */
691
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 */
707
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 */
723
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 */
739
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 */
755
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 */
772
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 */
789
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 */
806
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 */
820
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 */
834
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 */
848 };
849 \f
850 /* Handle the R_M32R_10_PCREL reloc. */
851
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;
862 {
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))
868 {
869 reloc_entry->address += input_section->output_offset;
870 return bfd_reloc_ok;
871 }
872
873 if (output_bfd != NULL)
874 {
875 /* FIXME: See bfd_perform_relocation. Is this right? */
876 return bfd_reloc_continue;
877 }
878
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);
887 }
888
889 /* Utility to actually perform an R_M32R_10_PCREL reloc. */
890
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;
902 {
903 bfd_signed_vma relocation;
904 unsigned long x;
905 bfd_reloc_status_type status;
906
907 /* Sanity check the address (offset in section). */
908 if (offset > bfd_get_section_limit (abfd, input_section))
909 return bfd_reloc_outofrange;
910
911 relocation = symbol_value + addend;
912 /* Make it pc relative. */
913 relocation -= (input_section->output_section->vma
914 + input_section->output_offset);
915 /* These jumps mask off the lower two bits of the current address
916 before doing pcrel calculations. */
917 relocation -= (offset & -(bfd_vma) 4);
918
919 if (relocation < -0x200 || relocation > 0x1ff)
920 status = bfd_reloc_overflow;
921 else
922 status = bfd_reloc_ok;
923
924 x = bfd_get_16 (abfd, data + offset);
925 relocation >>= howto->rightshift;
926 relocation <<= howto->bitpos;
927 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
928 bfd_put_16 (abfd, (bfd_vma) x, data + offset);
929
930 return status;
931 }
932
933 /* Handle the R_M32R_HI16_[SU]LO relocs.
934 HI16_SLO is for the add3 and load/store with displacement instructions.
935 HI16_ULO is for the or3 instruction.
936 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
937 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
938 we must add one to the high 16 bytes (which will get subtracted off when
939 the low 16 bits are added).
940 These relocs have to be done in combination with an R_M32R_LO16 reloc
941 because there is a carry from the LO16 to the HI16. Here we just save
942 the information we need; we do the actual relocation when we see the LO16.
943 This code is copied from the elf32-mips.c. We also support an arbitrary
944 number of HI16 relocs to be associated with a single LO16 reloc. The
945 assembler sorts the relocs to ensure each HI16 immediately precedes its
946 LO16. However if there are multiple copies, the assembler may not find
947 the real LO16 so it picks the first one it finds. */
948
949 struct m32r_hi16
950 {
951 struct m32r_hi16 *next;
952 bfd_byte *addr;
953 bfd_vma addend;
954 };
955
956 /* FIXME: This should not be a static variable. */
957
958 static struct m32r_hi16 *m32r_hi16_list;
959
960 static bfd_reloc_status_type
961 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
962 input_section, output_bfd, error_message)
963 bfd *abfd ATTRIBUTE_UNUSED;
964 arelent *reloc_entry;
965 asymbol *symbol;
966 PTR data;
967 asection *input_section;
968 bfd *output_bfd;
969 char **error_message ATTRIBUTE_UNUSED;
970 {
971 bfd_reloc_status_type ret;
972 bfd_vma relocation;
973 struct m32r_hi16 *n;
974
975 /* This part is from bfd_elf_generic_reloc.
976 If we're relocating, and this an external symbol, we don't want
977 to change anything. */
978 if (output_bfd != (bfd *) NULL
979 && (symbol->flags & BSF_SECTION_SYM) == 0
980 && reloc_entry->addend == 0)
981 {
982 reloc_entry->address += input_section->output_offset;
983 return bfd_reloc_ok;
984 }
985
986 /* Sanity check the address (offset in section). */
987 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
988 return bfd_reloc_outofrange;
989
990 ret = bfd_reloc_ok;
991 if (bfd_is_und_section (symbol->section)
992 && output_bfd == (bfd *) NULL)
993 ret = bfd_reloc_undefined;
994
995 if (bfd_is_com_section (symbol->section))
996 relocation = 0;
997 else
998 relocation = symbol->value;
999
1000 relocation += symbol->section->output_section->vma;
1001 relocation += symbol->section->output_offset;
1002 relocation += reloc_entry->addend;
1003
1004 /* Save the information, and let LO16 do the actual relocation. */
1005 n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
1006 if (n == NULL)
1007 return bfd_reloc_outofrange;
1008 n->addr = (bfd_byte *) data + reloc_entry->address;
1009 n->addend = relocation;
1010 n->next = m32r_hi16_list;
1011 m32r_hi16_list = n;
1012
1013 if (output_bfd != (bfd *) NULL)
1014 reloc_entry->address += input_section->output_offset;
1015
1016 return ret;
1017 }
1018
1019 /* Handle an M32R ELF HI16 reloc. */
1020
1021 static void
1022 m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
1023 bfd *input_bfd;
1024 int type;
1025 Elf_Internal_Rela *relhi;
1026 Elf_Internal_Rela *rello;
1027 bfd_byte *contents;
1028 bfd_vma addend;
1029 {
1030 unsigned long insn;
1031 bfd_vma addlo;
1032
1033 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1034
1035 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
1036 if (type == R_M32R_HI16_SLO)
1037 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
1038 else
1039 addlo &= 0xffff;
1040
1041 addend += ((insn & 0xffff) << 16) + addlo;
1042
1043 /* Reaccount for sign extension of low part. */
1044 if (type == R_M32R_HI16_SLO
1045 && (addend & 0x8000) != 0)
1046 addend += 0x10000;
1047
1048 bfd_put_32 (input_bfd,
1049 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
1050 contents + relhi->r_offset);
1051 }
1052
1053 /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
1054 inplace relocation; this function exists in order to do the
1055 R_M32R_HI16_[SU]LO relocation described above. */
1056
1057 bfd_reloc_status_type
1058 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
1059 input_section, output_bfd, error_message)
1060 bfd *input_bfd;
1061 arelent *reloc_entry;
1062 asymbol *symbol;
1063 PTR data;
1064 asection *input_section;
1065 bfd *output_bfd;
1066 char **error_message;
1067 {
1068 /* This part is from bfd_elf_generic_reloc.
1069 If we're relocating, and this an external symbol, we don't want
1070 to change anything. */
1071 if (output_bfd != (bfd *) NULL
1072 && (symbol->flags & BSF_SECTION_SYM) == 0
1073 && reloc_entry->addend == 0)
1074 {
1075 reloc_entry->address += input_section->output_offset;
1076 return bfd_reloc_ok;
1077 }
1078
1079 if (m32r_hi16_list != NULL)
1080 {
1081 struct m32r_hi16 *l;
1082
1083 l = m32r_hi16_list;
1084 while (l != NULL)
1085 {
1086 unsigned long insn;
1087 unsigned long val;
1088 unsigned long vallo;
1089 struct m32r_hi16 *next;
1090
1091 /* Do the HI16 relocation. Note that we actually don't need
1092 to know anything about the LO16 itself, except where to
1093 find the low 16 bits of the addend needed by the LO16. */
1094 insn = bfd_get_32 (input_bfd, l->addr);
1095 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
1096 & 0xffff) ^ 0x8000) - 0x8000;
1097 val = ((insn & 0xffff) << 16) + vallo;
1098 val += l->addend;
1099
1100 /* Reaccount for sign extension of low part. */
1101 if ((val & 0x8000) != 0)
1102 val += 0x10000;
1103
1104 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1105 bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
1106
1107 next = l->next;
1108 free (l);
1109 l = next;
1110 }
1111
1112 m32r_hi16_list = NULL;
1113 }
1114
1115 /* Now do the LO16 reloc in the usual way.
1116 ??? It would be nice to call bfd_elf_generic_reloc here,
1117 but we have partial_inplace set. bfd_elf_generic_reloc will
1118 pass the handling back to bfd_install_relocation which will install
1119 a section relative addend which is wrong. */
1120 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1121 input_section, output_bfd, error_message);
1122 }
1123
1124 /* Do generic partial_inplace relocation.
1125 This is a local replacement for bfd_elf_generic_reloc. */
1126
1127 bfd_reloc_status_type
1128 m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1129 input_section, output_bfd, error_message)
1130 bfd *input_bfd;
1131 arelent *reloc_entry;
1132 asymbol *symbol;
1133 PTR data;
1134 asection *input_section;
1135 bfd *output_bfd;
1136 char **error_message ATTRIBUTE_UNUSED;
1137 {
1138 bfd_reloc_status_type ret;
1139 bfd_vma relocation;
1140 bfd_byte *inplace_address;
1141
1142 /* This part is from bfd_elf_generic_reloc.
1143 If we're relocating, and this an external symbol, we don't want
1144 to change anything. */
1145 if (output_bfd != (bfd *) NULL
1146 && (symbol->flags & BSF_SECTION_SYM) == 0
1147 && reloc_entry->addend == 0)
1148 {
1149 reloc_entry->address += input_section->output_offset;
1150 return bfd_reloc_ok;
1151 }
1152
1153 /* Now do the reloc in the usual way.
1154 ??? It would be nice to call bfd_elf_generic_reloc here,
1155 but we have partial_inplace set. bfd_elf_generic_reloc will
1156 pass the handling back to bfd_install_relocation which will install
1157 a section relative addend which is wrong. */
1158
1159 /* Sanity check the address (offset in section). */
1160 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
1161 return bfd_reloc_outofrange;
1162
1163 ret = bfd_reloc_ok;
1164 if (bfd_is_und_section (symbol->section)
1165 && output_bfd == (bfd *) NULL)
1166 ret = bfd_reloc_undefined;
1167
1168 if (bfd_is_com_section (symbol->section)
1169 || output_bfd != (bfd *) NULL)
1170 relocation = 0;
1171 else
1172 relocation = symbol->value;
1173
1174 /* Only do this for a final link. */
1175 if (output_bfd == (bfd *) NULL)
1176 {
1177 relocation += symbol->section->output_section->vma;
1178 relocation += symbol->section->output_offset;
1179 }
1180
1181 relocation += reloc_entry->addend;
1182 inplace_address = (bfd_byte *) data + reloc_entry->address;
1183
1184 #define DOIT(x) \
1185 x = ( (x & ~reloc_entry->howto->dst_mask) | \
1186 (((x & reloc_entry->howto->src_mask) + relocation) & \
1187 reloc_entry->howto->dst_mask))
1188
1189 switch (reloc_entry->howto->size)
1190 {
1191 case 1:
1192 {
1193 short x = bfd_get_16 (input_bfd, inplace_address);
1194 DOIT (x);
1195 bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
1196 }
1197 break;
1198 case 2:
1199 {
1200 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1201 DOIT (x);
1202 bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
1203 }
1204 break;
1205 default:
1206 BFD_ASSERT (0);
1207 }
1208
1209 if (output_bfd != (bfd *) NULL)
1210 reloc_entry->address += input_section->output_offset;
1211
1212 return ret;
1213 }
1214
1215 /* Handle the R_M32R_SDA16 reloc.
1216 This reloc is used to compute the address of objects in the small data area
1217 and to perform loads and stores from that area.
1218 The lower 16 bits are sign extended and added to the register specified
1219 in the instruction, which is assumed to point to _SDA_BASE_. */
1220
1221 static bfd_reloc_status_type
1222 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1223 input_section, output_bfd, error_message)
1224 bfd *abfd ATTRIBUTE_UNUSED;
1225 arelent *reloc_entry;
1226 asymbol *symbol;
1227 PTR data ATTRIBUTE_UNUSED;
1228 asection *input_section;
1229 bfd *output_bfd;
1230 char **error_message ATTRIBUTE_UNUSED;
1231 {
1232 /* This part is from bfd_elf_generic_reloc. */
1233 if (output_bfd != (bfd *) NULL
1234 && (symbol->flags & BSF_SECTION_SYM) == 0
1235 && (! reloc_entry->howto->partial_inplace
1236 || reloc_entry->addend == 0))
1237 {
1238 reloc_entry->address += input_section->output_offset;
1239 return bfd_reloc_ok;
1240 }
1241
1242 if (output_bfd != NULL)
1243 {
1244 /* FIXME: See bfd_perform_relocation. Is this right? */
1245 return bfd_reloc_continue;
1246 }
1247
1248 /* FIXME: not sure what to do here yet. But then again, the linker
1249 may never call us. */
1250 abort ();
1251 }
1252 \f
1253 /* Map BFD reloc types to M32R ELF reloc types. */
1254
1255 struct m32r_reloc_map
1256 {
1257 bfd_reloc_code_real_type bfd_reloc_val;
1258 unsigned char elf_reloc_val;
1259 };
1260
1261 static const struct m32r_reloc_map m32r_reloc_map_old[] =
1262 {
1263 { BFD_RELOC_NONE, R_M32R_NONE },
1264 { BFD_RELOC_16, R_M32R_16 },
1265 { BFD_RELOC_32, R_M32R_32 },
1266 { BFD_RELOC_M32R_24, R_M32R_24 },
1267 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
1268 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
1269 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
1270 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
1271 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
1272 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
1273 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
1274 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
1275 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
1276 };
1277
1278 static const struct m32r_reloc_map m32r_reloc_map[] =
1279 {
1280 { BFD_RELOC_NONE, R_M32R_NONE },
1281 { BFD_RELOC_16, R_M32R_16_RELA },
1282 { BFD_RELOC_32, R_M32R_32_RELA },
1283 { BFD_RELOC_M32R_24, R_M32R_24_RELA },
1284 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
1285 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
1286 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
1287 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
1288 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
1289 { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
1290 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
1291 { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
1292 { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
1293
1294 { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
1295 { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
1296 { BFD_RELOC_M32R_COPY, R_M32R_COPY },
1297 { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
1298 { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
1299 { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
1300 { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
1301 { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
1302 { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
1303 { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
1304 { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
1305 { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
1306 { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
1307 { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
1308 { BFD_RELOC_M32R_GOTOFF_HI_ULO, R_M32R_GOTOFF_HI_ULO },
1309 { BFD_RELOC_M32R_GOTOFF_HI_SLO, R_M32R_GOTOFF_HI_SLO },
1310 { BFD_RELOC_M32R_GOTOFF_LO, R_M32R_GOTOFF_LO },
1311 };
1312
1313 static reloc_howto_type *
1314 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1315 bfd *abfd ATTRIBUTE_UNUSED;
1316 bfd_reloc_code_real_type code;
1317 {
1318 unsigned int i;
1319
1320 #ifdef USE_M32R_OLD_RELOC
1321 for (i = 0;
1322 i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
1323 i++)
1324 {
1325 if (m32r_reloc_map_old[i].bfd_reloc_val == code)
1326 return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
1327 }
1328 #else /* ! USE_M32R_OLD_RELOC */
1329
1330 for (i = 0;
1331 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
1332 i++)
1333 {
1334 if (m32r_reloc_map[i].bfd_reloc_val == code)
1335 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
1336 }
1337 #endif
1338
1339 return NULL;
1340 }
1341
1342 /* Set the howto pointer for an M32R ELF reloc. */
1343
1344 static void
1345 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
1346 bfd *abfd ATTRIBUTE_UNUSED;
1347 arelent *cache_ptr;
1348 Elf_Internal_Rela *dst;
1349 {
1350 unsigned int r_type;
1351
1352 r_type = ELF32_R_TYPE (dst->r_info);
1353 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY);
1354 cache_ptr->howto = &m32r_elf_howto_table[r_type];
1355 }
1356
1357 static void
1358 m32r_info_to_howto (abfd, cache_ptr, dst)
1359 bfd *abfd ATTRIBUTE_UNUSED;
1360 arelent *cache_ptr;
1361 Elf_Internal_Rela *dst;
1362 {
1363 BFD_ASSERT ((ELF32_R_TYPE(dst->r_info) == (unsigned int) R_M32R_NONE)
1364 || ((ELF32_R_TYPE(dst->r_info) > (unsigned int) R_M32R_GNU_VTENTRY)
1365 && (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_M32R_max)));
1366 cache_ptr->howto = &m32r_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
1367 }
1368
1369 \f
1370 /* Given a BFD section, try to locate the corresponding ELF section
1371 index. */
1372
1373 bfd_boolean
1374 _bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
1375 bfd *abfd ATTRIBUTE_UNUSED;
1376 asection *sec;
1377 int *retval;
1378 {
1379 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1380 {
1381 *retval = SHN_M32R_SCOMMON;
1382 return TRUE;
1383 }
1384 return FALSE;
1385 }
1386
1387 /* M32R ELF uses two common sections. One is the usual one, and the other
1388 is for small objects. All the small objects are kept together, and then
1389 referenced via one register, which yields faster assembler code. It is
1390 up to the compiler to emit an instruction to load the register with
1391 _SDA_BASE. This is what we use for the small common section. This
1392 approach is copied from elf32-mips.c. */
1393 static asection m32r_elf_scom_section;
1394 static asymbol m32r_elf_scom_symbol;
1395 static asymbol *m32r_elf_scom_symbol_ptr;
1396
1397 /* Handle the special M32R section numbers that a symbol may use. */
1398
1399 void
1400 _bfd_m32r_elf_symbol_processing (abfd, asym)
1401 bfd *abfd ATTRIBUTE_UNUSED;
1402 asymbol *asym;
1403 {
1404 elf_symbol_type *elfsym;
1405
1406 elfsym = (elf_symbol_type *) asym;
1407
1408 switch (elfsym->internal_elf_sym.st_shndx)
1409 {
1410 case SHN_M32R_SCOMMON:
1411 if (m32r_elf_scom_section.name == NULL)
1412 {
1413 /* Initialize the small common section. */
1414 m32r_elf_scom_section.name = ".scommon";
1415 m32r_elf_scom_section.flags = SEC_IS_COMMON;
1416 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
1417 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
1418 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
1419 m32r_elf_scom_symbol.name = ".scommon";
1420 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
1421 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
1422 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
1423 }
1424 asym->section = &m32r_elf_scom_section;
1425 asym->value = elfsym->internal_elf_sym.st_size;
1426 break;
1427 }
1428 }
1429
1430 /* Hook called by the linker routine which adds symbols from an object
1431 file. We must handle the special M32R section numbers here.
1432 We also keep watching for whether we need to create the sdata special
1433 linker sections. */
1434
1435 static bfd_boolean
1436 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1437 bfd *abfd;
1438 struct bfd_link_info *info;
1439 Elf_Internal_Sym *sym;
1440 const char **namep;
1441 flagword *flagsp ATTRIBUTE_UNUSED;
1442 asection **secp;
1443 bfd_vma *valp;
1444 {
1445 if (! info->relocatable
1446 && (*namep)[0] == '_' && (*namep)[1] == 'S'
1447 && strcmp (*namep, "_SDA_BASE_") == 0
1448 && is_elf_hash_table (info->hash))
1449 {
1450 /* This is simpler than using _bfd_elf_create_linker_section
1451 (our needs are simpler than ppc's needs). Also
1452 _bfd_elf_create_linker_section currently has a bug where if a .sdata
1453 section already exists a new one is created that follows it which
1454 screws of _SDA_BASE_ address calcs because output_offset != 0. */
1455 struct elf_link_hash_entry *h;
1456 struct bfd_link_hash_entry *bh;
1457 asection *s = bfd_get_section_by_name (abfd, ".sdata");
1458
1459 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
1460
1461 if (s == NULL)
1462 {
1463 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1464 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1465
1466 s = bfd_make_section_anyway (abfd, ".sdata");
1467 if (s == NULL)
1468 return FALSE;
1469 bfd_set_section_flags (abfd, s, flags);
1470 bfd_set_section_alignment (abfd, s, 2);
1471 }
1472
1473 bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
1474 FALSE, FALSE, FALSE);
1475
1476 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1477 && !(_bfd_generic_link_add_one_symbol (info,
1478 abfd,
1479 "_SDA_BASE_",
1480 BSF_GLOBAL,
1481 s,
1482 (bfd_vma) 32768,
1483 (const char *) NULL,
1484 FALSE,
1485 get_elf_backend_data (abfd)->collect,
1486 &bh)))
1487 return FALSE;
1488 h = (struct elf_link_hash_entry *) bh;
1489 h->type = STT_OBJECT;
1490 }
1491
1492 switch (sym->st_shndx)
1493 {
1494 case SHN_M32R_SCOMMON:
1495 *secp = bfd_make_section_old_way (abfd, ".scommon");
1496 (*secp)->flags |= SEC_IS_COMMON;
1497 *valp = sym->st_size;
1498 break;
1499 }
1500
1501 return TRUE;
1502 }
1503
1504 /* We have to figure out the SDA_BASE value, so that we can adjust the
1505 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
1506 BFD. If we can't find it, we're stuck. We cache it in the ELF
1507 target data. We don't need to adjust the symbol value for an
1508 external symbol if we are producing relocatable output. */
1509
1510 static bfd_reloc_status_type
1511 m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
1512 bfd *output_bfd;
1513 struct bfd_link_info *info;
1514 const char **error_message;
1515 bfd_vma *psb;
1516 {
1517 if (elf_gp (output_bfd) == 0)
1518 {
1519 struct bfd_link_hash_entry *h;
1520
1521 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
1522 if (h != (struct bfd_link_hash_entry *) NULL
1523 && h->type == bfd_link_hash_defined)
1524 elf_gp (output_bfd) = (h->u.def.value
1525 + h->u.def.section->output_section->vma
1526 + h->u.def.section->output_offset);
1527 else
1528 {
1529 /* Only get the error once. */
1530 *psb = elf_gp (output_bfd) = 4;
1531 *error_message =
1532 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
1533 return bfd_reloc_dangerous;
1534 }
1535 }
1536 *psb = elf_gp (output_bfd);
1537 return bfd_reloc_ok;
1538 }
1539 \f
1540 /* Return size of a PLT entry. */
1541 #define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
1542
1543 /* The m32r linker needs to keep track of the number of relocs that it
1544 decides to copy in check_relocs for each symbol. This is so that
1545 it can discard PC relative relocs if it doesn't need them when
1546 linking with -Bsymbolic. We store the information in a field
1547 extending the regular ELF linker hash table. */
1548
1549 /* This structure keeps track of the number of PC relative relocs we
1550 have copied for a given symbol. */
1551
1552 struct elf_m32r_pcrel_relocs_copied
1553 {
1554 /* Next section. */
1555 struct elf_m32r_pcrel_relocs_copied *next;
1556 /* A section in dynobj. */
1557 asection *section;
1558 /* Number of relocs copied in this section. */
1559 bfd_size_type count;
1560 };
1561
1562 /* The sh linker needs to keep track of the number of relocs that it
1563 decides to copy as dynamic relocs in check_relocs for each symbol.
1564 This is so that it can later discard them if they are found to be
1565 unnecessary. We store the information in a field extending the
1566 regular ELF linker hash table. */
1567
1568 struct elf_m32r_dyn_relocs
1569 {
1570 struct elf_m32r_dyn_relocs *next;
1571
1572 /* The input section of the reloc. */
1573 asection *sec;
1574
1575 /* Total number of relocs copied for the input section. */
1576 bfd_size_type count;
1577
1578 /* Number of pc-relative relocs copied for the input section. */
1579 bfd_size_type pc_count;
1580 };
1581
1582
1583 /* m32r ELF linker hash entry. */
1584
1585 struct elf_m32r_link_hash_entry
1586 {
1587 struct elf_link_hash_entry root;
1588
1589 /* Track dynamic relocs copied for this symbol. */
1590 struct elf_m32r_dyn_relocs *dyn_relocs;
1591
1592 // bfd_signed_vma gotplt_refcount;
1593
1594 /* Number of PC relative relocs copied for this symbol. */
1595 /* struct elf_m32r_pcrel_relocs_copied *pcrel_relocs_copied; FIXME */
1596 };
1597
1598 /* m32r ELF linker hash table. */
1599
1600 struct elf_m32r_link_hash_table
1601 {
1602 struct elf_link_hash_table root;
1603
1604 /* Short-cuts to get to dynamic linker sections. */
1605 asection *sgot;
1606 asection *sgotplt;
1607 asection *srelgot;
1608 asection *splt;
1609 asection *srelplt;
1610 asection *sdynbss;
1611 asection *srelbss;
1612
1613 /* Small local sym to section mapping cache. */
1614 struct sym_sec_cache sym_sec;
1615 };
1616
1617 /* Traverse an m32r ELF linker hash table. */
1618
1619 #define m32r_elf_link_hash_traverse(table, func, info) \
1620 (elf_link_hash_traverse \
1621 (&(table)->root, \
1622 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1623 (info)))
1624
1625 /* Get the m32r ELF linker hash table from a link_info structure. */
1626
1627
1628 #define m32r_elf_hash_table(p) \
1629 ((struct elf_m32r_link_hash_table *) ((p)->hash))
1630
1631 /* Create an entry in an m32r ELF linker hash table. */
1632 static struct bfd_hash_entry *
1633 m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1634 const char * );
1635
1636 static struct bfd_hash_entry *
1637 m32r_elf_link_hash_newfunc (entry, table, string)
1638 struct bfd_hash_entry *entry;
1639 struct bfd_hash_table *table;
1640 const char *string;
1641 {
1642 struct elf_m32r_link_hash_entry *ret =
1643 (struct elf_m32r_link_hash_entry *) entry;
1644
1645 /* Allocate the structure if it has not already been allocated by a
1646 subclass. */
1647 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1648 ret = ((struct elf_m32r_link_hash_entry *)
1649 bfd_hash_allocate (table,
1650 sizeof (struct elf_m32r_link_hash_entry)));
1651 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1652 return (struct bfd_hash_entry *) ret;
1653
1654 /* Call the allocation method of the superclass. */
1655 ret = ((struct elf_m32r_link_hash_entry *)
1656 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1657 table, string));
1658 if (ret != (struct elf_m32r_link_hash_entry *) NULL)
1659 {
1660 struct elf_m32r_link_hash_entry *eh;
1661
1662 eh = (struct elf_m32r_link_hash_entry *) ret;
1663 eh->dyn_relocs = NULL;
1664 // eh->gotplt_refcount = 0;
1665 /* eh->pcrel_relocs_copied = NULL; FIXME */
1666 }
1667
1668 return (struct bfd_hash_entry *) ret;
1669 }
1670
1671 /* Create an m32r ELF linker hash table. */
1672 static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1673
1674 static struct bfd_link_hash_table *
1675 m32r_elf_link_hash_table_create (abfd)
1676 bfd *abfd;
1677 {
1678 struct elf_m32r_link_hash_table *ret;
1679 bfd_size_type amt = sizeof (struct elf_m32r_link_hash_table);
1680
1681 ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1682 if (ret == (struct elf_m32r_link_hash_table *) NULL)
1683 return NULL;
1684
1685 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1686 m32r_elf_link_hash_newfunc))
1687 {
1688 free (ret);
1689 return NULL;
1690 }
1691
1692 ret->sgot = NULL;
1693 ret->sgotplt = NULL;
1694 ret->srelgot = NULL;
1695 ret->splt = NULL;
1696 ret->srelplt = NULL;
1697 ret->sdynbss = NULL;
1698 ret->srelbss = NULL;
1699 ret->sym_sec.abfd = NULL;
1700
1701 return &ret->root.root;
1702 }
1703
1704 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1705 shortcuts to them in our hash table. */
1706 static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1707
1708 static bfd_boolean
1709 create_got_section (dynobj, info)
1710 bfd *dynobj;
1711 struct bfd_link_info *info;
1712 {
1713 struct elf_m32r_link_hash_table *htab;
1714
1715 if (! _bfd_elf_create_got_section (dynobj, info))
1716 return FALSE;
1717
1718 htab = m32r_elf_hash_table (info);
1719 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1720 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1721 if (! htab->sgot || ! htab->sgotplt)
1722 abort ();
1723
1724 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1725 if (htab->srelgot == NULL
1726 || ! bfd_set_section_flags (dynobj, htab->srelgot,
1727 (SEC_ALLOC
1728 | SEC_LOAD
1729 | SEC_HAS_CONTENTS
1730 | SEC_IN_MEMORY
1731 | SEC_LINKER_CREATED
1732 | SEC_READONLY))
1733 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1734 return FALSE;
1735
1736 return TRUE;
1737 }
1738
1739 /* Create dynamic sections when linking against a dynamic object. */
1740
1741 static bfd_boolean
1742 m32r_elf_create_dynamic_sections (abfd, info)
1743 bfd *abfd;
1744 struct bfd_link_info *info;
1745 {
1746 struct elf_m32r_link_hash_table *htab;
1747 flagword flags, pltflags;
1748 register asection *s;
1749 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1750 int ptralign = 2; /* 32bit */
1751
1752 htab = m32r_elf_hash_table (info);
1753
1754 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1755 .rel[a].bss sections. */
1756
1757 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1758 | SEC_LINKER_CREATED);
1759
1760 pltflags = flags;
1761 pltflags |= SEC_CODE;
1762 if (bed->plt_not_loaded)
1763 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1764 if (bed->plt_readonly)
1765 pltflags |= SEC_READONLY;
1766
1767 s = bfd_make_section (abfd, ".plt");
1768 htab->splt = s;
1769 if (s == NULL
1770 || ! bfd_set_section_flags (abfd, s, pltflags)
1771 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1772 return FALSE;
1773
1774 if (bed->want_plt_sym)
1775 {
1776 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1777 .plt section. */
1778 struct bfd_link_hash_entry *bh = NULL;
1779 struct elf_link_hash_entry *h;
1780 if (! (_bfd_generic_link_add_one_symbol
1781 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1782 (bfd_vma) 0, (const char *) NULL, FALSE,
1783 get_elf_backend_data (abfd)->collect, &bh)))
1784 return FALSE;
1785 h = (struct elf_link_hash_entry *) bh;
1786 h->def_regular = 1;
1787 h->type = STT_OBJECT;
1788
1789 if (info->shared
1790 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1791 return FALSE;
1792 }
1793
1794 s = bfd_make_section (abfd,
1795 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
1796 htab->srelplt = s;
1797 if (s == NULL
1798 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1799 || ! bfd_set_section_alignment (abfd, s, ptralign))
1800 return FALSE;
1801
1802 if (htab->sgot == NULL
1803 && ! create_got_section (abfd, info))
1804 return FALSE;
1805
1806 {
1807 const char *secname;
1808 char *relname;
1809 flagword secflags;
1810 asection *sec;
1811
1812 for (sec = abfd->sections; sec; sec = sec->next)
1813 {
1814 secflags = bfd_get_section_flags (abfd, sec);
1815 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1816 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1817 continue;
1818 secname = bfd_get_section_name (abfd, sec);
1819 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1820 strcpy (relname, ".rela");
1821 strcat (relname, secname);
1822 if (bfd_get_section_by_name (abfd, secname))
1823 continue;
1824 s = bfd_make_section (abfd, relname);
1825 if (s == NULL
1826 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1827 || ! bfd_set_section_alignment (abfd, s, ptralign))
1828 return FALSE;
1829 }
1830 }
1831
1832 if (bed->want_dynbss)
1833 {
1834 /* The .dynbss section is a place to put symbols which are defined
1835 by dynamic objects, are referenced by regular objects, and are
1836 not functions. We must allocate space for them in the process
1837 image and use a R_*_COPY reloc to tell the dynamic linker to
1838 initialize them at run time. The linker script puts the .dynbss
1839 section into the .bss section of the final image. */
1840 s = bfd_make_section (abfd, ".dynbss");
1841 htab->sdynbss = s;
1842 if (s == NULL
1843 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1844 return FALSE;
1845 /* The .rel[a].bss section holds copy relocs. This section is not
1846 normally needed. We need to create it here, though, so that the
1847 linker will map it to an output section. We can't just create it
1848 only if we need it, because we will not know whether we need it
1849 until we have seen all the input files, and the first time the
1850 main linker code calls BFD after examining all the input files
1851 (size_dynamic_sections) the input sections have already been
1852 mapped to the output sections. If the section turns out not to
1853 be needed, we can discard it later. We will never need this
1854 section when generating a shared object, since they do not use
1855 copy relocs. */
1856 if (! info->shared)
1857 {
1858 s = bfd_make_section (abfd,
1859 (bed->default_use_rela_p
1860 ? ".rela.bss" : ".rel.bss"));
1861 htab->srelbss = s;
1862 if (s == NULL
1863 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1864 || ! bfd_set_section_alignment (abfd, s, ptralign))
1865 return FALSE;
1866 }
1867 }
1868
1869 return TRUE;
1870 }
1871
1872 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1873 static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1874 struct elf_link_hash_entry *,
1875 struct elf_link_hash_entry *);
1876
1877 static void
1878 m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1879 struct elf_link_hash_entry *dir,
1880 struct elf_link_hash_entry *ind)
1881 {
1882 struct elf_m32r_link_hash_entry *edir, *eind;
1883
1884 edir = (struct elf_m32r_link_hash_entry *) dir;
1885 eind = (struct elf_m32r_link_hash_entry *) ind;
1886
1887 if (eind->dyn_relocs != NULL)
1888 {
1889 if (edir->dyn_relocs != NULL)
1890 {
1891 struct elf_m32r_dyn_relocs **pp;
1892 struct elf_m32r_dyn_relocs *p;
1893
1894 if (ind->root.type == bfd_link_hash_indirect)
1895 abort ();
1896
1897 /* Add reloc counts against the weak sym to the strong sym
1898 list. Merge any entries against the same section. */
1899 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1900 {
1901 struct elf_m32r_dyn_relocs *q;
1902
1903 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1904 if (q->sec == p->sec)
1905 {
1906 q->pc_count += p->pc_count;
1907 q->count += p->count;
1908 *pp = p->next;
1909 break;
1910 }
1911 if (q == NULL)
1912 pp = &p->next;
1913 }
1914 *pp = edir->dyn_relocs;
1915 }
1916
1917 edir->dyn_relocs = eind->dyn_relocs;
1918 eind->dyn_relocs = NULL;
1919 }
1920
1921 // if (ind->root.type == bfd_link_hash_indirect
1922 // && dir->got.refcount <= 0)
1923 // {
1924 // edir->tls_type = eind->tls_type;
1925 // eind->tls_type = GOT_UNKNOWN;
1926 // }
1927 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1928 }
1929
1930 \f
1931 /* Adjust a symbol defined by a dynamic object and referenced by a
1932 regular object. The current definition is in some section of the
1933 dynamic object, but we're not including those sections. We have to
1934 change the definition to something the rest of the link can
1935 understand. */
1936
1937 static bfd_boolean
1938 m32r_elf_adjust_dynamic_symbol (info, h)
1939 struct bfd_link_info *info;
1940 struct elf_link_hash_entry *h;
1941 {
1942 struct elf_m32r_link_hash_table *htab;
1943 struct elf_m32r_link_hash_entry *eh;
1944 struct elf_m32r_dyn_relocs *p;
1945 bfd *dynobj;
1946 asection *s;
1947 unsigned int power_of_two;
1948
1949 #ifdef DEBUG_PIC
1950 printf("m32r_elf_adjust_dynamic_symbol()\n");
1951 #endif
1952
1953 dynobj = elf_hash_table (info)->dynobj;
1954
1955 /* Make sure we know what is going on here. */
1956 BFD_ASSERT (dynobj != NULL
1957 && (h->needs_plt
1958 || h->u.weakdef != NULL
1959 || (h->def_dynamic
1960 && h->ref_regular
1961 && !h->def_regular)));
1962
1963
1964 /* If this is a function, put it in the procedure linkage table. We
1965 will fill in the contents of the procedure linkage table later,
1966 when we know the address of the .got section. */
1967 if (h->type == STT_FUNC
1968 || h->needs_plt)
1969 {
1970 if (! info->shared
1971 && !h->def_dynamic
1972 && !h->ref_dynamic
1973 && h->root.type != bfd_link_hash_undefweak
1974 && h->root.type != bfd_link_hash_undefined)
1975 {
1976 /* This case can occur if we saw a PLT reloc in an input
1977 file, but the symbol was never referred to by a dynamic
1978 object. In such a case, we don't actually need to build
1979 a procedure linkage table, and we can just do a PCREL
1980 reloc instead. */
1981 h->plt.offset = (bfd_vma) -1;
1982 h->needs_plt = 0;
1983 }
1984
1985 return TRUE;
1986 }
1987 else
1988 h->plt.offset = (bfd_vma) -1;
1989
1990 /* If this is a weak symbol, and there is a real definition, the
1991 processor independent code will have arranged for us to see the
1992 real definition first, and we can just use the same value. */
1993 if (h->u.weakdef != NULL)
1994 {
1995 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1996 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1997 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1998 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1999 return TRUE;
2000 }
2001
2002 /* This is a reference to a symbol defined by a dynamic object which
2003 is not a function. */
2004
2005 /* If we are creating a shared library, we must presume that the
2006 only references to the symbol are via the global offset table.
2007 For such cases we need not do anything here; the relocations will
2008 be handled correctly by relocate_section. */
2009 if (info->shared)
2010 return TRUE;
2011
2012 /* If there are no references to this symbol that do not use the
2013 GOT, we don't need to generate a copy reloc. */
2014 if (!h->non_got_ref)
2015 return TRUE;
2016
2017 /* If -z nocopyreloc was given, we won't generate them either. */
2018 if (info->nocopyreloc)
2019 {
2020 h->non_got_ref = 0;
2021 return TRUE;
2022 }
2023
2024 eh = (struct elf_m32r_link_hash_entry *) h;
2025 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2026 {
2027 s = p->sec->output_section;
2028 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2029 break;
2030 }
2031
2032 /* If we didn't find any dynamic relocs in sections which needs the
2033 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2034 the copy reloc. */
2035 if (p == NULL)
2036 {
2037 h->non_got_ref = 0;
2038 return TRUE;
2039 }
2040
2041 /* We must allocate the symbol in our .dynbss section, which will
2042 become part of the .bss section of the executable. There will be
2043 an entry for this symbol in the .dynsym section. The dynamic
2044 object will contain position independent code, so all references
2045 from the dynamic object to this symbol will go through the global
2046 offset table. The dynamic linker will use the .dynsym entry to
2047 determine the address it must put in the global offset table, so
2048 both the dynamic object and the regular object will refer to the
2049 same memory location for the variable. */
2050
2051 htab = m32r_elf_hash_table (info);
2052 s = htab->sdynbss;
2053 BFD_ASSERT (s != NULL);
2054
2055 /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2056 to copy the initial value out of the dynamic object and into the
2057 runtime process image. We need to remember the offset into the
2058 .rela.bss section we are going to use. */
2059 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2060 {
2061 asection *srel;
2062
2063 srel = htab->srelbss;
2064 BFD_ASSERT (srel != NULL);
2065 srel->size += sizeof (Elf32_External_Rela);
2066 h->needs_copy = 1;
2067 }
2068
2069 /* We need to figure out the alignment required for this symbol. I
2070 have no idea how ELF linkers handle this. */
2071 power_of_two = bfd_log2 (h->size);
2072 if (power_of_two > 3)
2073 power_of_two = 3;
2074
2075 /* Apply the required alignment. */
2076 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2077 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2078 {
2079 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2080 return FALSE;
2081 }
2082
2083 /* Define the symbol as being at this point in the section. */
2084 h->root.u.def.section = s;
2085 h->root.u.def.value = s->size;
2086
2087 /* Increment the section size to make room for the symbol. */
2088 s->size += h->size;
2089
2090 return TRUE;
2091 }
2092
2093 /* Allocate space in .plt, .got and associated reloc sections for
2094 dynamic relocs. */
2095
2096 static bfd_boolean
2097 allocate_dynrelocs (h, inf)
2098 struct elf_link_hash_entry *h;
2099 PTR inf;
2100 {
2101 struct bfd_link_info *info;
2102 struct elf_m32r_link_hash_table *htab;
2103 struct elf_m32r_link_hash_entry *eh;
2104 struct elf_m32r_dyn_relocs *p;
2105
2106 if (h->root.type == bfd_link_hash_indirect)
2107 return TRUE;
2108
2109 if (h->root.type == bfd_link_hash_warning)
2110 /* When warning symbols are created, they **replace** the "real"
2111 entry in the hash table, thus we never get to see the real
2112 symbol in a hash traversal. So look at it now. */
2113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2114
2115 info = (struct bfd_link_info *) inf;
2116 htab = m32r_elf_hash_table (info);
2117
2118 eh = (struct elf_m32r_link_hash_entry *) h;
2119 // if ((h->got.refcount > 0
2120 // || h->forced_local)
2121 // && eh->gotplt_refcount > 0)
2122 // {
2123 // /* The symbol has been forced local, or we have some direct got refs,
2124 // so treat all the gotplt refs as got refs. */
2125 // h->got.refcount += eh->gotplt_refcount;
2126 // if (h->plt.refcount >= eh->gotplt_refcount)
2127 // h->plt.refcount -= eh->gotplt_refcount;
2128 // }
2129
2130 if (htab->root.dynamic_sections_created
2131 && h->plt.refcount > 0)
2132 {
2133 /* Make sure this symbol is output as a dynamic symbol.
2134 Undefined weak syms won't yet be marked as dynamic. */
2135 if (h->dynindx == -1
2136 && !h->forced_local)
2137 {
2138 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2139 return FALSE;
2140 }
2141
2142 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2143 {
2144 asection *s = htab->splt;
2145
2146 /* If this is the first .plt entry, make room for the special
2147 first entry. */
2148 if (s->size == 0)
2149 s->size += PLT_ENTRY_SIZE;
2150
2151 h->plt.offset = s->size;
2152
2153 /* If this symbol is not defined in a regular file, and we are
2154 not generating a shared library, then set the symbol to this
2155 location in the .plt. This is required to make function
2156 pointers compare as equal between the normal executable and
2157 the shared library. */
2158 if (! info->shared
2159 && !h->def_regular)
2160 {
2161 h->root.u.def.section = s;
2162 h->root.u.def.value = h->plt.offset;
2163 }
2164
2165 /* Make room for this entry. */
2166 s->size += PLT_ENTRY_SIZE;
2167
2168 /* We also need to make an entry in the .got.plt section, which
2169 will be placed in the .got section by the linker script. */
2170 htab->sgotplt->size += 4;
2171
2172 /* We also need to make an entry in the .rel.plt section. */
2173 htab->srelplt->size += sizeof (Elf32_External_Rela);
2174 }
2175 else
2176 {
2177 h->plt.offset = (bfd_vma) -1;
2178 h->needs_plt = 0;
2179 }
2180 }
2181 else
2182 {
2183 h->plt.offset = (bfd_vma) -1;
2184 h->needs_plt = 0;
2185 }
2186
2187 if (h->got.refcount > 0)
2188 {
2189 asection *s;
2190 bfd_boolean dyn;
2191
2192 /* Make sure this symbol is output as a dynamic symbol.
2193 Undefined weak syms won't yet be marked as dynamic. */
2194 if (h->dynindx == -1
2195 && !h->forced_local)
2196 {
2197 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2198 return FALSE;
2199 }
2200
2201 s = htab->sgot;
2202
2203 h->got.offset = s->size;
2204 s->size += 4;
2205 dyn = htab->root.dynamic_sections_created;
2206 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2207 htab->srelgot->size += sizeof (Elf32_External_Rela);
2208 }
2209 else
2210 h->got.offset = (bfd_vma) -1;
2211
2212 if (eh->dyn_relocs == NULL)
2213 return TRUE;
2214
2215 /* In the shared -Bsymbolic case, discard space allocated for
2216 dynamic pc-relative relocs against symbols which turn out to be
2217 defined in regular objects. For the normal shared case, discard
2218 space for pc-relative relocs that have become local due to symbol
2219 visibility changes. */
2220
2221 if (info->shared)
2222 {
2223 if (h->def_regular
2224 && (h->forced_local
2225 || info->symbolic))
2226 {
2227 struct elf_m32r_dyn_relocs **pp;
2228 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2229 {
2230 p->count -= p->pc_count;
2231 p->pc_count = 0;
2232 if (p->count == 0)
2233 *pp = p->next;
2234 else
2235 pp = &p->next;
2236 }
2237 }
2238 }
2239 else
2240 {
2241 /* For the non-shared case, discard space for relocs against
2242 symbols which turn out to need copy relocs or are not
2243 dynamic. */
2244
2245 if (!h->non_got_ref
2246 && ((h->def_dynamic
2247 && !h->def_regular)
2248 || (htab->root.dynamic_sections_created
2249 && (h->root.type == bfd_link_hash_undefweak
2250 || h->root.type == bfd_link_hash_undefined))))
2251 {
2252 /* Make sure this symbol is output as a dynamic symbol.
2253 Undefined weak syms won't yet be marked as dynamic. */
2254 if (h->dynindx == -1
2255 && !h->forced_local)
2256 {
2257 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2258 return FALSE;
2259 }
2260
2261 /* If that succeeded, we know we'll be keeping all the
2262 relocs. */
2263 if (h->dynindx != -1)
2264 goto keep;
2265 }
2266
2267 eh->dyn_relocs = NULL;
2268
2269 keep: ;
2270 }
2271
2272 /* Finally, allocate space. */
2273 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2274 {
2275 asection *sreloc = elf_section_data (p->sec)->sreloc;
2276 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2277 }
2278
2279 return TRUE;
2280 }
2281 /* Find any dynamic relocs that apply to read-only sections. */
2282
2283 static bfd_boolean
2284 readonly_dynrelocs (h, inf)
2285 struct elf_link_hash_entry *h;
2286 PTR inf;
2287 {
2288 struct elf_m32r_link_hash_entry *eh;
2289 struct elf_m32r_dyn_relocs *p;
2290
2291 if (h->root.type == bfd_link_hash_warning)
2292 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2293
2294 eh = (struct elf_m32r_link_hash_entry *) h;
2295 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2296 {
2297 asection *s = p->sec->output_section;
2298
2299 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2300 {
2301 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2302
2303 info->flags |= DF_TEXTREL;
2304
2305 /* Not an error, just cut short the traversal. */
2306 return FALSE;
2307 }
2308 }
2309 return TRUE;
2310 }
2311
2312 /* Set the sizes of the dynamic sections. */
2313
2314 static bfd_boolean
2315 m32r_elf_size_dynamic_sections (output_bfd, info)
2316 bfd *output_bfd ATTRIBUTE_UNUSED;
2317 struct bfd_link_info *info;
2318 {
2319 struct elf_m32r_link_hash_table *htab;
2320 bfd *dynobj;
2321 asection *s;
2322 bfd_boolean relocs;
2323 bfd *ibfd;
2324
2325 #ifdef DEBUG_PIC
2326 printf("m32r_elf_size_dynamic_sections()\n");
2327 #endif
2328
2329 htab = m32r_elf_hash_table (info);
2330 dynobj = htab->root.dynobj;
2331 BFD_ASSERT (dynobj != NULL);
2332
2333 if (htab->root.dynamic_sections_created)
2334 {
2335 /* Set the contents of the .interp section to the interpreter. */
2336 if (! info->shared)
2337 {
2338 s = bfd_get_section_by_name (dynobj, ".interp");
2339 BFD_ASSERT (s != NULL);
2340 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2341 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2342 }
2343 }
2344
2345 /* Set up .got offsets for local syms, and space for local dynamic
2346 relocs. */
2347 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2348 {
2349 bfd_signed_vma *local_got;
2350 bfd_signed_vma *end_local_got;
2351 bfd_size_type locsymcount;
2352 Elf_Internal_Shdr *symtab_hdr;
2353 asection *srel;
2354
2355 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2356 continue;
2357
2358 for (s = ibfd->sections; s != NULL; s = s->next)
2359 {
2360 struct elf_m32r_dyn_relocs *p;
2361
2362 for (p = ((struct elf_m32r_dyn_relocs *)
2363 elf_section_data (s)->local_dynrel);
2364 p != NULL;
2365 p = p->next)
2366 {
2367 if (! bfd_is_abs_section (p->sec)
2368 && bfd_is_abs_section (p->sec->output_section))
2369 {
2370 /* Input section has been discarded, either because
2371 it is a copy of a linkonce section or due to
2372 linker script /DISCARD/, so we'll be discarding
2373 the relocs too. */
2374 }
2375 else if (p->count != 0)
2376 {
2377 srel = elf_section_data (p->sec)->sreloc;
2378 srel->size += p->count * sizeof (Elf32_External_Rela);
2379 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2380 info->flags |= DF_TEXTREL;
2381 }
2382 }
2383 }
2384
2385 local_got = elf_local_got_refcounts (ibfd);
2386 if (!local_got)
2387 continue;
2388
2389 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2390 locsymcount = symtab_hdr->sh_info;
2391 end_local_got = local_got + locsymcount;
2392 s = htab->sgot;
2393 srel = htab->srelgot;
2394 for (; local_got < end_local_got; ++local_got)
2395 {
2396 if (*local_got > 0)
2397 {
2398 *local_got = s->size;
2399 s->size += 4;
2400 if (info->shared)
2401 srel->size += sizeof (Elf32_External_Rela);
2402 }
2403 else
2404 *local_got = (bfd_vma) -1;
2405 }
2406 }
2407
2408 /* Allocate global sym .plt and .got entries, and space for global
2409 sym dynamic relocs. */
2410 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2411
2412 /* We now have determined the sizes of the various dynamic sections.
2413 Allocate memory for them. */
2414 relocs = FALSE;
2415 for (s = dynobj->sections; s != NULL; s = s->next)
2416 {
2417 if ((s->flags & SEC_LINKER_CREATED) == 0)
2418 continue;
2419
2420 if (s == htab->splt
2421 || s == htab->sgot
2422 || s == htab->sgotplt)
2423 {
2424 /* Strip this section if we don't need it; see the
2425 comment below. */
2426 }
2427 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2428 {
2429 if (s->size != 0 && s != htab->srelplt)
2430 relocs = TRUE;
2431
2432 /* We use the reloc_count field as a counter if we need
2433 to copy relocs into the output file. */
2434 s->reloc_count = 0;
2435 }
2436 else
2437 {
2438 /* It's not one of our sections, so don't allocate space. */
2439 continue;
2440 }
2441
2442 if (s->size == 0)
2443 {
2444 /* If we don't need this section, strip it from the
2445 output file. This is mostly to handle .rela.bss and
2446 .rela.plt. We must create both sections in
2447 create_dynamic_sections, because they must be created
2448 before the linker maps input sections to output
2449 sections. The linker does that before
2450 adjust_dynamic_symbol is called, and it is that
2451 function which decides whether anything needs to go
2452 into these sections. */
2453 _bfd_strip_section_from_output (info, s);
2454 continue;
2455 }
2456
2457 /* Allocate memory for the section contents. We use bfd_zalloc
2458 here in case unused entries are not reclaimed before the
2459 section's contents are written out. This should not happen,
2460 but this way if it does, we get a R_M32R_NONE reloc instead
2461 of garbage. */
2462 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2463 if (s->contents == NULL)
2464 return FALSE;
2465 }
2466
2467 if (htab->root.dynamic_sections_created)
2468 {
2469 /* Add some entries to the .dynamic section. We fill in the
2470 values later, in m32r_elf_finish_dynamic_sections, but we
2471 must add the entries now so that we get the correct size for
2472 the .dynamic section. The DT_DEBUG entry is filled in by the
2473 dynamic linker and used by the debugger. */
2474 #define add_dynamic_entry(TAG, VAL) \
2475 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2476
2477 if (! info->shared)
2478 {
2479 if (! add_dynamic_entry (DT_DEBUG, 0))
2480 return FALSE;
2481 }
2482
2483 if (htab->splt->size != 0)
2484 {
2485 if (! add_dynamic_entry (DT_PLTGOT, 0)
2486 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2487 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2488 || ! add_dynamic_entry (DT_JMPREL, 0))
2489 return FALSE;
2490 }
2491
2492 if (relocs)
2493 {
2494 if (! add_dynamic_entry (DT_RELA, 0)
2495 || ! add_dynamic_entry (DT_RELASZ, 0)
2496 || ! add_dynamic_entry (DT_RELAENT,
2497 sizeof (Elf32_External_Rela)))
2498 return FALSE;
2499
2500 /* If any dynamic relocs apply to a read-only section,
2501 then we need a DT_TEXTREL entry. */
2502 if ((info->flags & DF_TEXTREL) == 0)
2503 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2504 (PTR) info);
2505
2506 if ((info->flags & DF_TEXTREL) != 0)
2507 {
2508 if (! add_dynamic_entry (DT_TEXTREL, 0))
2509 return FALSE;
2510 }
2511 }
2512 }
2513 #undef add_dynamic_entry
2514
2515 return TRUE;
2516 }
2517 /* Relocate an M32R/D ELF section.
2518 There is some attempt to make this function usable for many architectures,
2519 both for RELA and REL type relocs, if only to serve as a learning tool.
2520
2521 The RELOCATE_SECTION function is called by the new ELF backend linker
2522 to handle the relocations for a section.
2523
2524 The relocs are always passed as Rela structures; if the section
2525 actually uses Rel structures, the r_addend field will always be
2526 zero.
2527
2528 This function is responsible for adjust the section contents as
2529 necessary, and (if using Rela relocs and generating a
2530 relocatable output file) adjusting the reloc addend as
2531 necessary.
2532
2533 This function does not have to worry about setting the reloc
2534 address or the reloc symbol index.
2535
2536 LOCAL_SYMS is a pointer to the swapped in local symbols.
2537
2538 LOCAL_SECTIONS is an array giving the section in the input file
2539 corresponding to the st_shndx field of each local symbol.
2540
2541 The global hash table entry for the global symbols can be found
2542 via elf_sym_hashes (input_bfd).
2543
2544 When generating relocatable output, this function must handle
2545 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2546 going to be the section symbol corresponding to the output
2547 section, which means that the addend must be adjusted
2548 accordingly. */
2549
2550 static bfd_boolean
2551 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2552 contents, relocs, local_syms, local_sections)
2553 bfd *output_bfd ATTRIBUTE_UNUSED;
2554 struct bfd_link_info *info;
2555 bfd *input_bfd;
2556 asection *input_section;
2557 bfd_byte *contents;
2558 Elf_Internal_Rela *relocs;
2559 Elf_Internal_Sym *local_syms;
2560 asection **local_sections;
2561 {
2562 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2563 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2564 Elf_Internal_Rela *rel, *relend;
2565 /* Assume success. */
2566 bfd_boolean ret = TRUE;
2567
2568 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2569 bfd *dynobj;
2570 bfd_vma *local_got_offsets;
2571 asection *sgot, *splt, *sreloc;
2572 bfd_vma high_address = bfd_get_section_limit (input_bfd, input_section);
2573
2574 dynobj = htab->root.dynobj;
2575 local_got_offsets = elf_local_got_offsets (input_bfd);
2576
2577 sgot = htab->sgot;
2578 splt = htab->splt;
2579 sreloc = NULL;
2580
2581 rel = relocs;
2582 relend = relocs + input_section->reloc_count;
2583 for (; rel < relend; rel++)
2584 {
2585 int r_type;
2586 reloc_howto_type *howto;
2587 unsigned long r_symndx;
2588 struct elf_link_hash_entry *h;
2589 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
2590 ensure it's zero (we use REL relocs, not RELA). Therefore this
2591 should be assigning zero to `addend', but for clarity we use
2592 `r_addend'. */
2593 bfd_vma addend = rel->r_addend;
2594 bfd_vma offset = rel->r_offset;
2595 Elf_Internal_Sym *sym;
2596 asection *sec;
2597 const char *sym_name;
2598 bfd_reloc_status_type r;
2599 const char *errmsg = NULL;
2600 bfd_boolean use_rel = FALSE;
2601
2602 h = NULL;
2603 r_type = ELF32_R_TYPE (rel->r_info);
2604 if (r_type < 0 || r_type >= (int) R_M32R_max)
2605 {
2606 (*_bfd_error_handler) (_("%B: unknown relocation type %d"),
2607 input_bfd,
2608 (int) r_type);
2609 bfd_set_error (bfd_error_bad_value);
2610 ret = FALSE;
2611 continue;
2612 }
2613
2614 if (r_type == R_M32R_GNU_VTENTRY
2615 || r_type == R_M32R_GNU_VTINHERIT
2616 || r_type == R_M32R_NONE
2617 || r_type == R_M32R_RELA_GNU_VTENTRY
2618 || r_type == R_M32R_RELA_GNU_VTINHERIT)
2619 continue;
2620
2621 if (r_type <= R_M32R_GNU_VTENTRY)
2622 use_rel = TRUE;
2623
2624 howto = m32r_elf_howto_table + r_type;
2625 r_symndx = ELF32_R_SYM (rel->r_info);
2626
2627 if (info->relocatable && use_rel)
2628 {
2629 /* This is a relocatable link. We don't have to change
2630 anything, unless the reloc is against a section symbol,
2631 in which case we have to adjust according to where the
2632 section symbol winds up in the output section. */
2633 sec = NULL;
2634 if (r_symndx >= symtab_hdr->sh_info)
2635 {
2636 /* External symbol. */
2637 continue;
2638 }
2639
2640 /* Local symbol. */
2641 sym = local_syms + r_symndx;
2642 sym_name = "<local symbol>";
2643 /* STT_SECTION: symbol is associated with a section. */
2644 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2645 {
2646 /* Symbol isn't associated with a section. Nothing to do. */
2647 continue;
2648 }
2649
2650 sec = local_sections[r_symndx];
2651 addend += sec->output_offset + sym->st_value;
2652
2653 /* If partial_inplace, we need to store any additional addend
2654 back in the section. */
2655 if (! howto->partial_inplace)
2656 continue;
2657 /* ??? Here is a nice place to call a special_function
2658 like handler. */
2659 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2660 r = _bfd_relocate_contents (howto, input_bfd,
2661 addend, contents + offset);
2662 else
2663 {
2664 Elf_Internal_Rela *lorel;
2665
2666 /* We allow an arbitrary number of HI16 relocs before the
2667 LO16 reloc. This permits gcc to emit the HI and LO relocs
2668 itself. */
2669 for (lorel = rel + 1;
2670 (lorel < relend
2671 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2672 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2673 lorel++)
2674 continue;
2675 if (lorel < relend
2676 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2677 {
2678 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2679 contents, addend);
2680 r = bfd_reloc_ok;
2681 }
2682 else
2683 r = _bfd_relocate_contents (howto, input_bfd,
2684 addend, contents + offset);
2685 }
2686 }
2687 else
2688 {
2689 bfd_vma relocation;
2690
2691 /* This is a final link. */
2692 sym = NULL;
2693 sec = NULL;
2694 h = NULL;
2695
2696 if (r_symndx < symtab_hdr->sh_info)
2697 {
2698 /* Local symbol. */
2699 sym = local_syms + r_symndx;
2700 sec = local_sections[r_symndx];
2701 sym_name = "<local symbol>";
2702
2703 if (!use_rel)
2704 {
2705 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2706 addend = rel->r_addend;
2707
2708 if (info->relocatable)
2709 {
2710 /* This is a relocatable link. We don't have to change
2711 anything, unless the reloc is against a section symbol,
2712 in which case we have to adjust according to where the
2713 section symbol winds up in the output section. */
2714 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2715 rel->r_addend += sec->output_offset + sym->st_value;
2716
2717 continue;
2718 }
2719 }
2720 else
2721 {
2722 relocation = (sec->output_section->vma
2723 + sec->output_offset
2724 + sym->st_value);
2725 }
2726 }
2727 else
2728 {
2729 /* External symbol. */
2730 if (info->relocatable && !use_rel)
2731 continue;
2732
2733 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2734 while (h->root.type == bfd_link_hash_indirect
2735 || h->root.type == bfd_link_hash_warning)
2736 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2737 sym_name = h->root.root.string;
2738
2739 if (h->root.type == bfd_link_hash_defined
2740 || h->root.type == bfd_link_hash_defweak)
2741 {
2742 bfd_boolean dyn;
2743 sec = h->root.u.def.section;
2744
2745 dyn = htab->root.dynamic_sections_created;
2746 sec = h->root.u.def.section;
2747 if (r_type == R_M32R_GOTPC24
2748 || (r_type == R_M32R_GOTPC_HI_ULO
2749 || r_type == R_M32R_GOTPC_HI_SLO
2750 || r_type == R_M32R_GOTPC_LO)
2751 || (r_type == R_M32R_26_PLTREL
2752 && h->plt.offset != (bfd_vma) -1)
2753 || ((r_type == R_M32R_GOT24
2754 || r_type == R_M32R_GOT16_HI_ULO
2755 || r_type == R_M32R_GOT16_HI_SLO
2756 || r_type == R_M32R_GOT16_LO)
2757 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2758 info->shared, h)
2759 && (! info->shared
2760 || (! info->symbolic && h->dynindx != -1)
2761 || !h->def_regular))
2762 || (info->shared
2763 && ((! info->symbolic && h->dynindx != -1)
2764 || !h->def_regular)
2765 && (((r_type == R_M32R_16_RELA
2766 || r_type == R_M32R_32_RELA
2767 || r_type == R_M32R_24_RELA
2768 || r_type == R_M32R_HI16_ULO_RELA
2769 || r_type == R_M32R_HI16_SLO_RELA
2770 || r_type == R_M32R_LO16_RELA)
2771 && !h->forced_local)
2772 || r_type == R_M32R_10_PCREL_RELA
2773 || r_type == R_M32R_18_PCREL_RELA
2774 || r_type == R_M32R_26_PCREL_RELA)
2775 && ((input_section->flags & SEC_ALLOC) != 0
2776 /* DWARF will emit R_M32R_16(24,32) relocations
2777 in its sections against symbols defined
2778 externally in shared libraries. We can't do
2779 anything with them here. */
2780 || ((input_section->flags & SEC_DEBUGGING) != 0
2781 && h->def_dynamic))))
2782 {
2783 /* In these cases, we don't need the relocation
2784 value. We check specially because in some
2785 obscure cases sec->output_section will be NULL. */
2786 relocation = 0;
2787 }
2788 else if (sec->output_section == NULL)
2789 {
2790 (*_bfd_error_handler)
2791 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2792 bfd_get_filename (input_bfd), h->root.root.string,
2793 bfd_get_section_name (input_bfd, input_section));
2794
2795 relocation = 0;
2796 }
2797 else
2798 relocation = (h->root.u.def.value
2799 + sec->output_section->vma
2800 + sec->output_offset);
2801 }
2802 else if (h->root.type == bfd_link_hash_undefweak)
2803 relocation = 0;
2804 else if (info->unresolved_syms_in_objects == RM_IGNORE
2805 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2806 relocation = 0;
2807 else
2808 {
2809 if (! ((*info->callbacks->undefined_symbol)
2810 (info, h->root.root.string, input_bfd,
2811 input_section, offset,
2812 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2813 || ELF_ST_VISIBILITY (h->other)))))
2814 return FALSE;
2815 relocation = 0;
2816 }
2817 }
2818
2819 /* Sanity check the address. */
2820 if (offset > high_address)
2821 {
2822 r = bfd_reloc_outofrange;
2823 goto check_reloc;
2824 }
2825
2826 switch ((int) r_type)
2827 {
2828 case R_M32R_GOTOFF:
2829 /* Relocation is relative to the start of the global offset
2830 table (for ld24 rx, #uimm24). eg access at label+addend
2831
2832 ld24 rx. #label@GOTOFF + addend
2833 sub rx, r12. */
2834
2835 BFD_ASSERT (sgot != NULL);
2836
2837 relocation = -(relocation - sgot->output_section->vma);
2838 rel->r_addend = -rel->r_addend;
2839 break;
2840
2841 case R_M32R_GOTOFF_HI_ULO:
2842 case R_M32R_GOTOFF_HI_SLO:
2843 case R_M32R_GOTOFF_LO:
2844 BFD_ASSERT (sgot != NULL);
2845
2846 relocation -= sgot->output_section->vma;
2847
2848 if ((r_type == R_M32R_GOTOFF_HI_SLO)
2849 && ((relocation + rel->r_addend) & 0x8000))
2850 rel->r_addend += 0x10000;
2851 break;
2852
2853 case R_M32R_GOTPC24:
2854 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2855 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2856 */
2857 relocation = sgot->output_section->vma;
2858 break;
2859
2860 case R_M32R_GOTPC_HI_ULO:
2861 case R_M32R_GOTPC_HI_SLO:
2862 case R_M32R_GOTPC_LO:
2863 {
2864 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2865 bl .+4
2866 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2867 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2868 or
2869 bl .+4
2870 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2871 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2872 */
2873 relocation = sgot->output_section->vma;
2874 relocation -= (input_section->output_section->vma
2875 + input_section->output_offset
2876 + rel->r_offset);
2877 if ((r_type == R_M32R_GOTPC_HI_SLO)
2878 && ((relocation + rel->r_addend) & 0x8000))
2879 rel->r_addend += 0x10000;
2880
2881 break;
2882 }
2883 case R_M32R_GOT16_HI_ULO:
2884 case R_M32R_GOT16_HI_SLO:
2885 case R_M32R_GOT16_LO:
2886 /* Fall through. */
2887 case R_M32R_GOT24:
2888 /* Relocation is to the entry for this symbol in the global
2889 offset table. */
2890 BFD_ASSERT (sgot != NULL);
2891
2892 if (h != NULL)
2893 {
2894 bfd_boolean dyn;
2895 bfd_vma off;
2896
2897 off = h->got.offset;
2898 BFD_ASSERT (off != (bfd_vma) -1);
2899
2900 dyn = htab->root.dynamic_sections_created;
2901 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2902 || (info->shared
2903 && (info->symbolic
2904 || h->dynindx == -1
2905 || h->forced_local)
2906 && h->def_regular))
2907 {
2908 /* This is actually a static link, or it is a
2909 -Bsymbolic link and the symbol is defined
2910 locally, or the symbol was forced to be local
2911 because of a version file. We must initialize
2912 this entry in the global offset table. Since the
2913 offset must always be a multiple of 4, we use the
2914 least significant bit to record whether we have
2915 initialized it already.
2916
2917 When doing a dynamic link, we create a .rela.got
2918 relocation entry to initialize the value. This
2919 is done in the finish_dynamic_symbol routine. */
2920 if ((off & 1) != 0)
2921 off &= ~1;
2922 else
2923 {
2924 bfd_put_32 (output_bfd, relocation,
2925 sgot->contents + off);
2926 h->got.offset |= 1;
2927 }
2928 }
2929
2930 relocation = sgot->output_offset + off;
2931 }
2932 else
2933 {
2934 bfd_vma off;
2935 bfd_byte *loc;
2936
2937 BFD_ASSERT (local_got_offsets != NULL
2938 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2939
2940 off = local_got_offsets[r_symndx];
2941
2942 /* The offset must always be a multiple of 4. We use
2943 the least significant bit to record whether we have
2944 already processed this entry. */
2945 if ((off & 1) != 0)
2946 off &= ~1;
2947 else
2948 {
2949 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2950
2951 if (info->shared)
2952 {
2953 asection *srelgot;
2954 Elf_Internal_Rela outrel;
2955
2956 /* We need to generate a R_M32R_RELATIVE reloc
2957 for the dynamic linker. */
2958 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2959 BFD_ASSERT (srelgot != NULL);
2960
2961 outrel.r_offset = (sgot->output_section->vma
2962 + sgot->output_offset
2963 + off);
2964 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2965 outrel.r_addend = relocation;
2966 loc = srelgot->contents;
2967 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2968 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2969 ++srelgot->reloc_count;
2970 }
2971
2972 local_got_offsets[r_symndx] |= 1;
2973 }
2974
2975 relocation = sgot->output_offset + off;
2976 }
2977 if ((r_type == R_M32R_GOT16_HI_SLO)
2978 && ((relocation + rel->r_addend) & 0x8000))
2979 rel->r_addend += 0x10000;
2980
2981 break;
2982
2983 case R_M32R_26_PLTREL:
2984 /* Relocation is to the entry for this symbol in the
2985 procedure linkage table. */
2986
2987 /* The native assembler will generate a 26_PLTREL reloc
2988 for a local symbol if you assemble a call from one
2989 section to another when using -K pic. */
2990 if (h == NULL)
2991 break;
2992
2993 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2994 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2995 // break;
2996 if (h->forced_local)
2997 break;
2998
2999 if (h->plt.offset == (bfd_vma) -1)
3000 {
3001 /* We didn't make a PLT entry for this symbol. This
3002 happens when statically linking PIC code, or when
3003 using -Bsymbolic. */
3004 break;
3005 }
3006
3007 relocation = (splt->output_section->vma
3008 + splt->output_offset
3009 + h->plt.offset);
3010 break;
3011
3012 case R_M32R_HI16_SLO_RELA:
3013 {
3014 if ((relocation + rel->r_addend) & 0x8000)
3015 {
3016 rel->r_addend += 0x10000;
3017 }
3018 }
3019 /* Fall through. */
3020 case R_M32R_16_RELA:
3021 case R_M32R_24_RELA:
3022 case R_M32R_32_RELA:
3023 case R_M32R_18_PCREL_RELA:
3024 case R_M32R_26_PCREL_RELA:
3025 case R_M32R_HI16_ULO_RELA:
3026 case R_M32R_LO16_RELA:
3027 if (info->shared
3028 && r_symndx != 0
3029 && (input_section->flags & SEC_ALLOC) != 0
3030 && ((r_type != R_M32R_18_PCREL_RELA
3031 && r_type != R_M32R_26_PCREL_RELA)
3032 || (h != NULL
3033 && h->dynindx != -1
3034 && (! info->symbolic
3035 || !h->def_regular))))
3036 {
3037 Elf_Internal_Rela outrel;
3038 bfd_boolean skip, relocate;
3039 bfd_byte *loc;
3040
3041 /* When generating a shared object, these relocations
3042 are copied into the output file to be resolved at run
3043 time. */
3044
3045 if (sreloc == NULL)
3046 {
3047 const char *name;
3048
3049 name = (bfd_elf_string_from_elf_section
3050 (input_bfd,
3051 elf_elfheader (input_bfd)->e_shstrndx,
3052 elf_section_data (input_section)->rel_hdr.sh_name));
3053 if (name == NULL)
3054 return FALSE;
3055
3056 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3057 && strcmp (bfd_get_section_name (input_bfd,
3058 input_section),
3059 name + 5) == 0);
3060
3061 sreloc = bfd_get_section_by_name (dynobj, name);
3062 BFD_ASSERT (sreloc != NULL);
3063 }
3064
3065 skip = FALSE;
3066 relocate = FALSE;
3067
3068 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3069 info,
3070 input_section,
3071 rel->r_offset);
3072 if (outrel.r_offset == (bfd_vma) -1)
3073 skip = TRUE;
3074 else if (outrel.r_offset == (bfd_vma) -2)
3075 skip = TRUE, relocate = TRUE;
3076 outrel.r_offset += (input_section->output_section->vma
3077 + input_section->output_offset);
3078
3079 if (skip)
3080 memset (&outrel, 0, sizeof outrel);
3081 else if (r_type == R_M32R_18_PCREL_RELA
3082 || r_type == R_M32R_26_PCREL_RELA)
3083 {
3084 BFD_ASSERT (h != NULL && h->dynindx != -1);
3085 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3086 outrel.r_addend = rel->r_addend;
3087 }
3088 else
3089 {
3090 /* h->dynindx may be -1 if this symbol was marked to
3091 become local. */
3092 if (h == NULL
3093 || ((info->symbolic || h->dynindx == -1)
3094 && h->def_regular))
3095 {
3096 relocate = TRUE;
3097 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3098 outrel.r_addend = relocation + rel->r_addend;
3099 }
3100 else
3101 {
3102 BFD_ASSERT (h->dynindx != -1);
3103 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3104 outrel.r_addend = relocation + rel->r_addend;
3105 }
3106 }
3107
3108 loc = sreloc->contents;
3109 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3110 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3111 ++sreloc->reloc_count;
3112
3113 /* If this reloc is against an external symbol, we do
3114 not want to fiddle with the addend. Otherwise, we
3115 need to include the symbol value so that it becomes
3116 an addend for the dynamic reloc. */
3117 if (! relocate)
3118 continue;
3119 }
3120 break;
3121
3122 case (int) R_M32R_10_PCREL :
3123 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3124 contents, offset,
3125 sec, relocation, addend);
3126 goto check_reloc;
3127
3128 case (int) R_M32R_HI16_SLO :
3129 case (int) R_M32R_HI16_ULO :
3130 {
3131 Elf_Internal_Rela *lorel;
3132
3133 /* We allow an arbitrary number of HI16 relocs before the
3134 LO16 reloc. This permits gcc to emit the HI and LO relocs
3135 itself. */
3136 for (lorel = rel + 1;
3137 (lorel < relend
3138 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3139 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3140 lorel++)
3141 continue;
3142 if (lorel < relend
3143 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3144 {
3145 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3146 contents, relocation + addend);
3147 r = bfd_reloc_ok;
3148 }
3149 else
3150 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3151 contents, offset,
3152 relocation, addend);
3153 }
3154
3155 goto check_reloc;
3156
3157 case (int) R_M32R_SDA16_RELA:
3158 case (int) R_M32R_SDA16 :
3159 {
3160 const char *name;
3161
3162 BFD_ASSERT (sec != NULL);
3163 name = bfd_get_section_name (abfd, sec);
3164
3165 if (strcmp (name, ".sdata") == 0
3166 || strcmp (name, ".sbss") == 0
3167 || strcmp (name, ".scommon") == 0)
3168 {
3169 bfd_vma sda_base;
3170 bfd *out_bfd = sec->output_section->owner;
3171
3172 r = m32r_elf_final_sda_base (out_bfd, info,
3173 &errmsg,
3174 &sda_base);
3175 if (r != bfd_reloc_ok)
3176 {
3177 ret = FALSE;
3178 goto check_reloc;
3179 }
3180
3181 /* At this point `relocation' contains the object's
3182 address. */
3183 relocation -= sda_base;
3184 /* Now it contains the offset from _SDA_BASE_. */
3185 }
3186 else
3187 {
3188 (*_bfd_error_handler)
3189 (_("%B: The target (%s) of an %s relocation is in the wrong section (%A)"),
3190 input_bfd,
3191 sec,
3192 sym_name,
3193 m32r_elf_howto_table[(int) r_type].name);
3194 /*bfd_set_error (bfd_error_bad_value); ??? why? */
3195 ret = FALSE;
3196 continue;
3197 }
3198 }
3199 /* fall through */
3200
3201 default : /* OLD_M32R_RELOC */
3202
3203 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3204 contents, offset,
3205 relocation, addend);
3206 goto check_reloc;
3207 }
3208
3209 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3210 contents, rel->r_offset,
3211 relocation, rel->r_addend);
3212
3213 }
3214
3215 check_reloc:
3216
3217 if (r != bfd_reloc_ok)
3218 {
3219 /* FIXME: This should be generic enough to go in a utility. */
3220 const char *name;
3221
3222 if (h != NULL)
3223 name = h->root.root.string;
3224 else
3225 {
3226 name = (bfd_elf_string_from_elf_section
3227 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3228 if (name == NULL || *name == '\0')
3229 name = bfd_section_name (input_bfd, sec);
3230 }
3231
3232 if (errmsg != NULL)
3233 goto common_error;
3234
3235 switch (r)
3236 {
3237 case bfd_reloc_overflow:
3238 if (! ((*info->callbacks->reloc_overflow)
3239 (info, (h ? &h->root : NULL), name, howto->name,
3240 (bfd_vma) 0, input_bfd, input_section, offset)))
3241 return FALSE;
3242 break;
3243
3244 case bfd_reloc_undefined:
3245 if (! ((*info->callbacks->undefined_symbol)
3246 (info, name, input_bfd, input_section,
3247 offset, TRUE)))
3248 return FALSE;
3249 break;
3250
3251 case bfd_reloc_outofrange:
3252 errmsg = _("internal error: out of range error");
3253 goto common_error;
3254
3255 case bfd_reloc_notsupported:
3256 errmsg = _("internal error: unsupported relocation error");
3257 goto common_error;
3258
3259 case bfd_reloc_dangerous:
3260 errmsg = _("internal error: dangerous error");
3261 goto common_error;
3262
3263 default:
3264 errmsg = _("internal error: unknown error");
3265 /* fall through */
3266
3267 common_error:
3268 if (!((*info->callbacks->warning)
3269 (info, errmsg, name, input_bfd, input_section,
3270 offset)))
3271 return FALSE;
3272 break;
3273 }
3274 }
3275 }
3276
3277 return ret;
3278 }
3279
3280 /* Finish up dynamic symbol handling. We set the contents of various
3281 dynamic sections here. */
3282 static bfd_boolean
3283 m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3284 bfd *output_bfd;
3285 struct bfd_link_info *info;
3286 struct elf_link_hash_entry *h;
3287 Elf_Internal_Sym *sym;
3288 {
3289 struct elf_m32r_link_hash_table *htab;
3290 bfd *dynobj;
3291 bfd_byte *loc;
3292
3293 #ifdef DEBUG_PIC
3294 printf("m32r_elf_finish_dynamic_symbol()\n");
3295 #endif
3296
3297 htab = m32r_elf_hash_table (info);
3298 dynobj = htab->root.dynobj;
3299
3300 if (h->plt.offset != (bfd_vma) -1)
3301 {
3302 asection *splt;
3303 asection *sgot;
3304 asection *srela;
3305
3306 bfd_vma plt_index;
3307 bfd_vma got_offset;
3308 Elf_Internal_Rela rela;
3309
3310 /* This symbol has an entry in the procedure linkage table. Set
3311 it up. */
3312
3313 BFD_ASSERT (h->dynindx != -1);
3314
3315 splt = htab->splt;
3316 sgot = htab->sgotplt;
3317 srela = htab->srelplt;
3318 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3319
3320 /* Get the index in the procedure linkage table which
3321 corresponds to this symbol. This is the index of this symbol
3322 in all the symbols for which we are making plt entries. The
3323 first entry in the procedure linkage table is reserved. */
3324 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3325
3326 /* Get the offset into the .got table of the entry that
3327 corresponds to this function. Each .got entry is 4 bytes.
3328 The first three are reserved. */
3329 got_offset = (plt_index + 3) * 4;
3330
3331 /* Fill in the entry in the procedure linkage table. */
3332 if (! info->shared)
3333 {
3334 bfd_put_32 (output_bfd,
3335 (PLT_ENTRY_WORD0b
3336 + (((sgot->output_section->vma
3337 + sgot->output_offset
3338 + got_offset) >> 16) & 0xffff)),
3339 splt->contents + h->plt.offset);
3340 bfd_put_32 (output_bfd,
3341 (PLT_ENTRY_WORD1b
3342 + ((sgot->output_section->vma
3343 + sgot->output_offset
3344 + got_offset) & 0xffff)),
3345 splt->contents + h->plt.offset + 4);
3346 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3347 splt->contents + h->plt.offset + 8);
3348 bfd_put_32 (output_bfd,
3349 (PLT_ENTRY_WORD3
3350 + plt_index * sizeof (Elf32_External_Rela)),
3351 splt->contents + h->plt.offset + 12);
3352 bfd_put_32 (output_bfd,
3353 (PLT_ENTRY_WORD4
3354 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3355 splt->contents + h->plt.offset + 16);
3356 }
3357 else
3358 {
3359 bfd_put_32 (output_bfd,
3360 PLT_ENTRY_WORD0 + got_offset,
3361 splt->contents + h->plt.offset);
3362 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
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);
3374 }
3375
3376 /* Fill in the entry in the global offset table. */
3377 bfd_put_32 (output_bfd,
3378 (splt->output_section->vma
3379 + splt->output_offset
3380 + h->plt.offset
3381 + 12), /* same offset */
3382 sgot->contents + got_offset);
3383
3384 /* Fill in the entry in the .rela.plt section. */
3385 rela.r_offset = (sgot->output_section->vma
3386 + sgot->output_offset
3387 + got_offset);
3388 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3389 rela.r_addend = 0;
3390 loc = srela->contents;
3391 loc += plt_index * sizeof(Elf32_External_Rela);
3392 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3393
3394 if (!h->def_regular)
3395 {
3396 /* Mark the symbol as undefined, rather than as defined in
3397 the .plt section. Leave the value alone. */
3398 sym->st_shndx = SHN_UNDEF;
3399 }
3400 }
3401
3402 if (h->got.offset != (bfd_vma) -1)
3403 {
3404 asection *sgot;
3405 asection *srela;
3406 Elf_Internal_Rela rela;
3407
3408 /* This symbol has an entry in the global offset table. Set it
3409 up. */
3410
3411 sgot = htab->sgot;
3412 srela = htab->srelgot;
3413 BFD_ASSERT (sgot != NULL && srela != NULL);
3414
3415 rela.r_offset = (sgot->output_section->vma
3416 + sgot->output_offset
3417 + (h->got.offset &~ 1));
3418
3419 /* If this is a -Bsymbolic link, and the symbol is defined
3420 locally, we just want to emit a RELATIVE reloc. Likewise if
3421 the symbol was forced to be local because of a version file.
3422 The entry in the global offset table will already have been
3423 initialized in the relocate_section function. */
3424 if (info->shared
3425 && (info->symbolic
3426 || h->dynindx == -1
3427 || h->forced_local)
3428 && h->def_regular)
3429 {
3430 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3431 rela.r_addend = (h->root.u.def.value
3432 + h->root.u.def.section->output_section->vma
3433 + h->root.u.def.section->output_offset);
3434 }
3435 else
3436 {
3437 BFD_ASSERT((h->got.offset & 1) == 0);
3438 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3439 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3440 rela.r_addend = 0;
3441 }
3442
3443 loc = srela->contents;
3444 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3445 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3446 ++srela->reloc_count;
3447 }
3448
3449 if (h->needs_copy)
3450 {
3451 asection *s;
3452 Elf_Internal_Rela rela;
3453
3454 /* This symbols needs a copy reloc. Set it up. */
3455
3456 BFD_ASSERT (h->dynindx != -1
3457 && (h->root.type == bfd_link_hash_defined
3458 || h->root.type == bfd_link_hash_defweak));
3459
3460 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3461 ".rela.bss");
3462 BFD_ASSERT (s != NULL);
3463
3464 rela.r_offset = (h->root.u.def.value
3465 + h->root.u.def.section->output_section->vma
3466 + h->root.u.def.section->output_offset);
3467 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3468 rela.r_addend = 0;
3469 loc = s->contents;
3470 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3471 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3472 ++s->reloc_count;
3473 }
3474
3475 /* Mark some specially defined symbols as absolute. */
3476 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3477 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3478 sym->st_shndx = SHN_ABS;
3479
3480 return TRUE;
3481 }
3482
3483
3484 /* Finish up the dynamic sections. */
3485
3486 static bfd_boolean
3487 m32r_elf_finish_dynamic_sections (output_bfd, info)
3488 bfd *output_bfd;
3489 struct bfd_link_info *info;
3490 {
3491 struct elf_m32r_link_hash_table *htab;
3492 bfd *dynobj;
3493 asection *sdyn;
3494 asection *sgot;
3495
3496 #ifdef DEBUG_PIC
3497 printf("m32r_elf_finish_dynamic_sections()\n");
3498 #endif
3499
3500 htab = m32r_elf_hash_table (info);
3501 dynobj = htab->root.dynobj;
3502
3503 sgot = htab->sgotplt;
3504 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3505
3506 if (htab->root.dynamic_sections_created)
3507 {
3508 asection *splt;
3509 Elf32_External_Dyn *dyncon, *dynconend;
3510
3511 BFD_ASSERT (sgot != NULL && sdyn != NULL);
3512
3513 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3514 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3515
3516 for (; dyncon < dynconend; dyncon++)
3517 {
3518 Elf_Internal_Dyn dyn;
3519 const char *name;
3520 asection *s;
3521
3522 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3523
3524 switch (dyn.d_tag)
3525 {
3526 default:
3527 break;
3528
3529 case DT_PLTGOT:
3530 name = ".got";
3531 s = htab->sgot->output_section;
3532 goto get_vma;
3533 case DT_JMPREL:
3534 name = ".rela.plt";
3535 s = htab->srelplt->output_section;
3536 get_vma:
3537 BFD_ASSERT (s != NULL);
3538 dyn.d_un.d_ptr = s->vma;
3539 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3540 break;
3541
3542 case DT_PLTRELSZ:
3543 s = htab->srelplt->output_section;
3544 BFD_ASSERT (s != NULL);
3545 dyn.d_un.d_val = s->size;
3546 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3547 break;
3548
3549 case DT_RELASZ:
3550 /* My reading of the SVR4 ABI indicates that the
3551 procedure linkage table relocs (DT_JMPREL) should be
3552 included in the overall relocs (DT_RELA). This is
3553 what Solaris does. However, UnixWare can not handle
3554 that case. Therefore, we override the DT_RELASZ entry
3555 here to make it not include the JMPREL relocs. Since
3556 the linker script arranges for .rela.plt to follow all
3557 other relocation sections, we don't have to worry
3558 about changing the DT_RELA entry. */
3559 if (htab->srelplt != NULL)
3560 {
3561 s = htab->srelplt->output_section;
3562 dyn.d_un.d_val -= s->size;
3563 }
3564 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3565 break;
3566 }
3567 }
3568
3569 /* Fill in the first entry in the procedure linkage table. */
3570 splt = htab->splt;
3571 if (splt && splt->size > 0)
3572 {
3573 if (info->shared)
3574 {
3575 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3576 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3577 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3578 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3579 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3580 }
3581 else
3582 {
3583 unsigned long addr;
3584 /* addr = .got + 4 */
3585 addr = sgot->output_section->vma + sgot->output_offset + 4;
3586 bfd_put_32 (output_bfd,
3587 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3588 splt->contents);
3589 bfd_put_32 (output_bfd,
3590 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3591 splt->contents + 4);
3592 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3593 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3594 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3595 }
3596
3597 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3598 PLT_ENTRY_SIZE;
3599 }
3600 }
3601
3602 /* Fill in the first three entries in the global offset table. */
3603 if (sgot && sgot->size > 0)
3604 {
3605 if (sdyn == NULL)
3606 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3607 else
3608 bfd_put_32 (output_bfd,
3609 sdyn->output_section->vma + sdyn->output_offset,
3610 sgot->contents);
3611 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3612 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3613
3614 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3615 }
3616
3617 return TRUE;
3618 }
3619
3620 \f
3621 #if 0 /* relaxing not supported yet */
3622
3623 /* This function handles relaxing for the m32r.
3624 Relaxing on the m32r is tricky because of instruction alignment
3625 requirements (4 byte instructions must be aligned on 4 byte boundaries).
3626
3627 The following relaxing opportunities are handled:
3628
3629 seth/add3/jl -> bl24 or bl8
3630 seth/add3 -> ld24
3631
3632 It would be nice to handle bl24 -> bl8 but given:
3633
3634 - 4 byte insns must be on 4 byte boundaries
3635 - branch instructions only branch to insns on 4 byte boundaries
3636
3637 this isn't much of a win because the insn in the 2 "deleted" bytes
3638 must become a nop. With some complexity some real relaxation could be
3639 done but the frequency just wouldn't make it worth it; it's better to
3640 try to do all the code compaction one can elsewhere.
3641 When the chip supports parallel 16 bit insns, things may change.
3642 */
3643
3644 static bfd_boolean
3645 m32r_elf_relax_section (abfd, sec, link_info, again)
3646 bfd *abfd;
3647 asection *sec;
3648 struct bfd_link_info *link_info;
3649 bfd_boolean *again;
3650 {
3651 Elf_Internal_Shdr *symtab_hdr;
3652 /* The Rela structures are used here because that's what
3653 _bfd_elf_link_read_relocs uses [for convenience - it sets the addend
3654 field to 0]. */
3655 Elf_Internal_Rela *internal_relocs = NULL;
3656 Elf_Internal_Rela *irel, *irelend;
3657 bfd_byte *contents = NULL;
3658 Elf_Internal_Sym *isymbuf = NULL;
3659
3660 /* Assume nothing changes. */
3661 *again = FALSE;
3662
3663 /* We don't have to do anything for a relocatable link, if
3664 this section does not have relocs, or if this is not a
3665 code section. */
3666 if (link_info->relocatable
3667 || (sec->flags & SEC_RELOC) == 0
3668 || sec->reloc_count == 0
3669 || (sec->flags & SEC_CODE) == 0
3670 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
3671 return TRUE;
3672
3673 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3674
3675 /* Get a copy of the native relocations. */
3676 internal_relocs = (_bfd_elf_link_read_relocs
3677 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3678 link_info->keep_memory));
3679 if (internal_relocs == NULL)
3680 goto error_return;
3681
3682 /* Walk through them looking for relaxing opportunities. */
3683 irelend = internal_relocs + sec->reloc_count;
3684 for (irel = internal_relocs; irel < irelend; irel++)
3685 {
3686 bfd_vma symval;
3687
3688 /* If this isn't something that can be relaxed, then ignore
3689 this reloc. */
3690 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
3691 continue;
3692
3693 /* Get the section contents if we haven't done so already. */
3694 if (contents == NULL)
3695 {
3696 /* Get cached copy if it exists. */
3697 if (elf_section_data (sec)->this_hdr.contents != NULL)
3698 contents = elf_section_data (sec)->this_hdr.contents;
3699 else
3700 {
3701 /* Go get them off disk. */
3702 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3703 goto error_return;
3704 }
3705 }
3706
3707 /* Read this BFD's local symbols if we haven't done so already. */
3708 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3709 {
3710 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3711 if (isymbuf == NULL)
3712 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3713 symtab_hdr->sh_info, 0,
3714 NULL, NULL, NULL);
3715 if (isymbuf == NULL)
3716 goto error_return;
3717 }
3718
3719 /* Get the value of the symbol referred to by the reloc. */
3720 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3721 {
3722 /* A local symbol. */
3723 Elf_Internal_Sym *isym;
3724 asection *sym_sec;
3725
3726 isym = isymbuf + ELF32_R_SYM (irel->r_info),
3727 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3728 symval = (isym->st_value
3729 + sym_sec->output_section->vma
3730 + sym_sec->output_offset);
3731 }
3732 else
3733 {
3734 unsigned long indx;
3735 struct elf_link_hash_entry *h;
3736
3737 /* An external symbol. */
3738 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3739 h = elf_sym_hashes (abfd)[indx];
3740 BFD_ASSERT (h != NULL);
3741 if (h->root.type != bfd_link_hash_defined
3742 && h->root.type != bfd_link_hash_defweak)
3743 {
3744 /* This appears to be a reference to an undefined
3745 symbol. Just ignore it--it will be caught by the
3746 regular reloc processing. */
3747 continue;
3748 }
3749
3750 symval = (h->root.u.def.value
3751 + h->root.u.def.section->output_section->vma
3752 + h->root.u.def.section->output_offset);
3753 }
3754
3755 /* For simplicity of coding, we are going to modify the section
3756 contents, the section relocs, and the BFD symbol table. We
3757 must tell the rest of the code not to free up this
3758 information. It would be possible to instead create a table
3759 of changes which have to be made, as is done in coff-mips.c;
3760 that would be more work, but would require less memory when
3761 the linker is run. */
3762
3763 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
3764 This sequence is generated by the compiler when compiling in
3765 32 bit mode. Also look for seth/add3 -> ld24. */
3766
3767 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
3768 {
3769 Elf_Internal_Rela *nrel;
3770 bfd_vma pc = (sec->output_section->vma + sec->output_offset
3771 + irel->r_offset);
3772 bfd_signed_vma pcrel_value = symval - pc;
3773 unsigned int code,reg;
3774 int addend,nop_p,bl8_p,to_delete;
3775
3776 /* The tests are ordered so that we get out as quickly as possible
3777 if this isn't something we can relax, taking into account that
3778 we are looking for two separate possibilities (jl/ld24). */
3779
3780 /* Do nothing if no room in the section for this to be what we're
3781 looking for. */
3782 if (irel->r_offset > sec->size - 8)
3783 continue;
3784
3785 /* Make sure the next relocation applies to the next
3786 instruction and that it's the add3's reloc. */
3787 nrel = irel + 1;
3788 if (nrel == irelend
3789 || irel->r_offset + 4 != nrel->r_offset
3790 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
3791 continue;
3792
3793 /* See if the instructions are seth/add3. */
3794 /* FIXME: This is where macros from cgen can come in. */
3795 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
3796 if ((code & 0xf0ff) != 0xd0c0)
3797 continue; /* not seth rN,foo */
3798 reg = (code & 0x0f00) >> 8;
3799 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
3800 if (code != (0x80a0 | reg | (reg << 8)))
3801 continue; /* not add3 rN,rN,foo */
3802
3803 /* At this point we've confirmed we have seth/add3. Now check
3804 whether the next insn is a jl, in which case try to change this
3805 to bl24 or bl8. */
3806
3807 /* Ensure the branch target is in range.
3808 The bl24 instruction has a 24 bit operand which is the target
3809 address right shifted by 2, giving a signed range of 26 bits.
3810 Note that 4 bytes are added to the high value because the target
3811 will be at least 4 bytes closer if we can relax. It'll actually
3812 be 4 or 8 bytes closer, but we don't know which just yet and
3813 the difference isn't significant enough to worry about. */
3814 #if !USE_REL /* put in for learning purposes */
3815 pcrel_value += irel->r_addend;
3816 #else
3817 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
3818 pcrel_value += addend;
3819 #endif
3820
3821 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
3822 /* Do nothing if no room in the section for this to be what we're
3823 looking for. */
3824 && (irel->r_offset <= sec->size - 12)
3825 /* Ensure the next insn is "jl rN". */
3826 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
3827 code != (0x1ec0 | reg)))
3828 {
3829 /* We can relax to bl24/bl8. */
3830
3831 /* See if there's a nop following the jl.
3832 Also see if we can use a bl8 insn. */
3833 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
3834 nop_p = (code & 0x7fff) == NOP_INSN;
3835 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
3836
3837 if (bl8_p)
3838 {
3839 /* Change "seth rN,foo" to "bl8 foo || nop".
3840 We OR in CODE just in case it's not a nop (technically,
3841 CODE currently must be a nop, but for cleanness we
3842 allow it to be anything). */
3843 #if !USE_REL /* put in for learning purposes */
3844 code = 0x7e000000 | MAKE_PARALLEL (code);
3845 #else
3846 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
3847 #endif
3848 to_delete = 8;
3849 }
3850 else
3851 {
3852 /* Change the seth rN,foo to a bl24 foo. */
3853 #if !USE_REL /* put in for learning purposes */
3854 code = 0xfe000000;
3855 #else
3856 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
3857 #endif
3858 to_delete = nop_p ? 8 : 4;
3859 }
3860
3861 bfd_put_32 (abfd, code, contents + irel->r_offset);
3862
3863 /* Set the new reloc type. */
3864 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3865 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
3866
3867 /* Delete the add3 reloc by making it a null reloc. */
3868 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3869 R_M32R_NONE);
3870 }
3871 else if (addend >= 0
3872 && symval + addend <= 0xffffff)
3873 {
3874 /* We can relax to ld24. */
3875
3876 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
3877 bfd_put_32 (abfd, code, contents + irel->r_offset);
3878 to_delete = 4;
3879 /* Tell the following code a nop filler isn't needed. */
3880 nop_p = 1;
3881 }
3882 else
3883 {
3884 /* Can't do anything here. */
3885 continue;
3886 }
3887
3888 /* Note that we've changed the relocs, section contents, etc. */
3889 elf_section_data (sec)->relocs = internal_relocs;
3890 elf_section_data (sec)->this_hdr.contents = contents;
3891 symtab_hdr->contents = (unsigned char *) isymbuf;
3892
3893 /* Delete TO_DELETE bytes of data. */
3894 if (!m32r_elf_relax_delete_bytes (abfd, sec,
3895 irel->r_offset + 4, to_delete))
3896 goto error_return;
3897
3898 /* Now that the following bytes have been moved into place, see if
3899 we need to replace the jl with a nop. This happens when we had
3900 to use a bl24 insn and the insn following the jl isn't a nop.
3901 Technically, this situation can't happen (since the insn can
3902 never be executed) but to be clean we do this. When the chip
3903 supports parallel 16 bit insns things may change.
3904 We don't need to do this in the case of relaxing to ld24,
3905 and the above code sets nop_p so this isn't done. */
3906 if (! nop_p && to_delete == 4)
3907 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
3908
3909 /* That will change things, so we should relax again.
3910 Note that this is not required, and it may be slow. */
3911 *again = TRUE;
3912
3913 continue;
3914 }
3915
3916 /* loop to try the next reloc */
3917 }
3918
3919 if (isymbuf != NULL
3920 && symtab_hdr->contents != (unsigned char *) isymbuf)
3921 {
3922 if (! link_info->keep_memory)
3923 free (isymbuf);
3924 else
3925 {
3926 /* Cache the symbols for elf_link_input_bfd. */
3927 symtab_hdr->contents = (unsigned char *) isymbuf;
3928 }
3929 }
3930
3931 if (contents != NULL
3932 && elf_section_data (sec)->this_hdr.contents != contents)
3933 {
3934 if (! link_info->keep_memory)
3935 free (contents);
3936 else
3937 {
3938 /* Cache the section contents for elf_link_input_bfd. */
3939 elf_section_data (sec)->this_hdr.contents = contents;
3940 }
3941 }
3942
3943 if (internal_relocs != NULL
3944 && elf_section_data (sec)->relocs != internal_relocs)
3945 free (internal_relocs);
3946
3947 return TRUE;
3948
3949 error_return:
3950 if (isymbuf != NULL
3951 && symtab_hdr->contents != (unsigned char *) isymbuf)
3952 free (isymbuf);
3953 if (contents != NULL
3954 && elf_section_data (sec)->this_hdr.contents != contents)
3955 free (contents);
3956 if (internal_relocs != NULL
3957 && elf_section_data (sec)->relocs != internal_relocs)
3958 free (internal_relocs);
3959
3960 return FALSE;
3961 }
3962
3963 /* Delete some bytes from a section while relaxing. */
3964
3965 static bfd_boolean
3966 m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
3967 bfd *abfd;
3968 asection *sec;
3969 bfd_vma addr;
3970 int count;
3971 {
3972 Elf_Internal_Shdr *symtab_hdr;
3973 int shndx;
3974 bfd_byte *contents;
3975 Elf_Internal_Rela *irel, *irelend;
3976 Elf_Internal_Rela *irelalign;
3977 bfd_vma toaddr;
3978 Elf_Internal_Sym *isym, *isymend;
3979 struct elf_link_hash_entry **sym_hashes;
3980 struct elf_link_hash_entry **end_hashes;
3981 unsigned int symcount;
3982
3983 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3984
3985 contents = elf_section_data (sec)->this_hdr.contents;
3986
3987 /* The deletion must stop at the next ALIGN reloc for an aligment
3988 power larger than the number of bytes we are deleting. */
3989
3990 irelalign = NULL;
3991 toaddr = sec->size;
3992
3993 irel = elf_section_data (sec)->relocs;
3994 irelend = irel + sec->reloc_count;
3995
3996 /* Actually delete the bytes. */
3997 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
3998 sec->size -= count;
3999
4000 /* Adjust all the relocs. */
4001 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
4002 {
4003 /* Get the new reloc address. */
4004 if ((irel->r_offset > addr
4005 && irel->r_offset < toaddr))
4006 irel->r_offset -= count;
4007 }
4008
4009 /* Adjust the local symbols defined in this section. */
4010 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4011 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
4012 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
4013 {
4014 if (isym->st_shndx == shndx
4015 && isym->st_value > addr
4016 && isym->st_value < toaddr)
4017 isym->st_value -= count;
4018 }
4019
4020 /* Now adjust the global symbols defined in this section. */
4021 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
4022 - symtab_hdr->sh_info);
4023 sym_hashes = elf_sym_hashes (abfd);
4024 end_hashes = sym_hashes + symcount;
4025 for (; sym_hashes < end_hashes; sym_hashes++)
4026 {
4027 struct elf_link_hash_entry *sym_hash = *sym_hashes;
4028
4029 if ((sym_hash->root.type == bfd_link_hash_defined
4030 || sym_hash->root.type == bfd_link_hash_defweak)
4031 && sym_hash->root.u.def.section == sec
4032 && sym_hash->root.u.def.value > addr
4033 && sym_hash->root.u.def.value < toaddr)
4034 {
4035 sym_hash->root.u.def.value -= count;
4036 }
4037 }
4038
4039 return TRUE;
4040 }
4041
4042 /* This is a version of bfd_generic_get_relocated_section_contents
4043 which uses m32r_elf_relocate_section. */
4044
4045 static bfd_byte *
4046 m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
4047 data, relocatable, symbols)
4048 bfd *output_bfd;
4049 struct bfd_link_info *link_info;
4050 struct bfd_link_order *link_order;
4051 bfd_byte *data;
4052 bfd_boolean relocatable;
4053 asymbol **symbols;
4054 {
4055 Elf_Internal_Shdr *symtab_hdr;
4056 asection *input_section = link_order->u.indirect.section;
4057 bfd *input_bfd = input_section->owner;
4058 asection **sections = NULL;
4059 Elf_Internal_Rela *internal_relocs = NULL;
4060 Elf_Internal_Sym *isymbuf = NULL;
4061 bfd_size_type amt;
4062
4063 /* We only need to handle the case of relaxing, or of having a
4064 particular set of section contents, specially. */
4065 if (relocatable
4066 || elf_section_data (input_section)->this_hdr.contents == NULL)
4067 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4068 link_order, data,
4069 relocatable,
4070 symbols);
4071
4072 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4073
4074 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4075 input_section->size);
4076
4077 if ((input_section->flags & SEC_RELOC) != 0
4078 && input_section->reloc_count > 0)
4079 {
4080 Elf_Internal_Sym *isymp;
4081 asection **secpp;
4082 Elf32_External_Sym *esym, *esymend;
4083
4084 internal_relocs = (_bfd_elf_link_read_relocs
4085 (input_bfd, input_section, (PTR) NULL,
4086 (Elf_Internal_Rela *) NULL, FALSE));
4087 if (internal_relocs == NULL)
4088 goto error_return;
4089
4090 if (symtab_hdr->sh_info != 0)
4091 {
4092 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4093 if (isymbuf == NULL)
4094 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4095 symtab_hdr->sh_info, 0,
4096 NULL, NULL, NULL);
4097 if (isymbuf == NULL)
4098 goto error_return;
4099 }
4100
4101 amt = symtab_hdr->sh_info;
4102 amt *= sizeof (asection *);
4103 sections = (asection **) bfd_malloc (amt);
4104 if (sections == NULL && symtab_hdr->sh_info > 0)
4105 goto error_return;
4106
4107 isymend = isymbuf + symtab_hdr->sh_info;
4108 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4109 {
4110 asection *isec;
4111
4112 if (isym->st_shndx == SHN_UNDEF)
4113 isec = bfd_und_section_ptr;
4114 else if (isym->st_shndx == SHN_ABS)
4115 isec = bfd_abs_section_ptr;
4116 else if (isym->st_shndx == SHN_COMMON)
4117 isec = bfd_com_section_ptr;
4118 else if (isym->st_shndx == SHN_M32R_SCOMMON)
4119 isec = &m32r_elf_scom_section;
4120 else
4121 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4122
4123 *secpp = isec;
4124 }
4125
4126 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
4127 input_section, data, internal_relocs,
4128 isymbuf, sections))
4129 goto error_return;
4130
4131 if (sections != NULL)
4132 free (sections);
4133 if (isymbuf != NULL
4134 && symtab_hdr->contents != (unsigned char *) isymbuf)
4135 free (isymbuf);
4136 if (elf_section_data (input_section)->relocs != internal_relocs)
4137 free (internal_relocs);
4138 }
4139
4140 return data;
4141
4142 error_return:
4143 if (sections != NULL)
4144 free (sections);
4145 if (isymbuf != NULL
4146 && symtab_hdr->contents != (unsigned char *) isymbuf)
4147 free (isymbuf);
4148 if (internal_relocs != NULL
4149 && elf_section_data (input_section)->relocs != internal_relocs)
4150 free (internal_relocs);
4151 return NULL;
4152 }
4153
4154 #endif /* #if 0 */
4155 \f
4156 /* Set the right machine number. */
4157 static bfd_boolean
4158 m32r_elf_object_p (abfd)
4159 bfd *abfd;
4160 {
4161 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4162 {
4163 default:
4164 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
4165 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
4166 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
4167 }
4168 return TRUE;
4169 }
4170
4171 /* Store the machine number in the flags field. */
4172 static void
4173 m32r_elf_final_write_processing (abfd, linker)
4174 bfd *abfd;
4175 bfd_boolean linker ATTRIBUTE_UNUSED;
4176 {
4177 unsigned long val;
4178
4179 switch (bfd_get_mach (abfd))
4180 {
4181 default:
4182 case bfd_mach_m32r: val = E_M32R_ARCH; break;
4183 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
4184 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
4185 }
4186
4187 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
4188 elf_elfheader (abfd)->e_flags |= val;
4189 }
4190
4191 /* Function to keep M32R specific file flags. */
4192 static bfd_boolean
4193 m32r_elf_set_private_flags (abfd, flags)
4194 bfd *abfd;
4195 flagword flags;
4196 {
4197 BFD_ASSERT (!elf_flags_init (abfd)
4198 || elf_elfheader (abfd)->e_flags == flags);
4199
4200 elf_elfheader (abfd)->e_flags = flags;
4201 elf_flags_init (abfd) = TRUE;
4202 return TRUE;
4203 }
4204
4205 /* Merge backend specific data from an object file to the output
4206 object file when linking. */
4207 static bfd_boolean
4208 m32r_elf_merge_private_bfd_data (ibfd, obfd)
4209 bfd *ibfd;
4210 bfd *obfd;
4211 {
4212 flagword out_flags;
4213 flagword in_flags;
4214
4215 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4216 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4217 return TRUE;
4218
4219 in_flags = elf_elfheader (ibfd)->e_flags;
4220 out_flags = elf_elfheader (obfd)->e_flags;
4221
4222 if (! elf_flags_init (obfd))
4223 {
4224 /* If the input is the default architecture then do not
4225 bother setting the flags for the output architecture,
4226 instead allow future merges to do this. If no future
4227 merges ever set these flags then they will retain their
4228 unitialised values, which surprise surprise, correspond
4229 to the default values. */
4230 if (bfd_get_arch_info (ibfd)->the_default)
4231 return TRUE;
4232
4233 elf_flags_init (obfd) = TRUE;
4234 elf_elfheader (obfd)->e_flags = in_flags;
4235
4236 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4237 && bfd_get_arch_info (obfd)->the_default)
4238 {
4239 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
4240 }
4241
4242 return TRUE;
4243 }
4244
4245 /* Check flag compatibility. */
4246 if (in_flags == out_flags)
4247 return TRUE;
4248
4249 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
4250 {
4251 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
4252 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4253 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
4254 {
4255 (*_bfd_error_handler)
4256 (_("%B: Instruction set mismatch with previous modules"), ibfd);
4257
4258 bfd_set_error (bfd_error_bad_value);
4259 return FALSE;
4260 }
4261 }
4262
4263 return TRUE;
4264 }
4265
4266 /* Display the flags field */
4267 static bfd_boolean
4268 m32r_elf_print_private_bfd_data (abfd, ptr)
4269 bfd *abfd;
4270 PTR ptr;
4271 {
4272 FILE * file = (FILE *) ptr;
4273
4274 BFD_ASSERT (abfd != NULL && ptr != NULL);
4275
4276 _bfd_elf_print_private_bfd_data (abfd, ptr);
4277
4278 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
4279
4280 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4281 {
4282 default:
4283 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
4284 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
4285 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
4286 }
4287
4288 fputc ('\n', file);
4289
4290 return TRUE;
4291 }
4292
4293 asection *
4294 m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
4295 asection *sec;
4296 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4297 Elf_Internal_Rela *rel;
4298 struct elf_link_hash_entry *h;
4299 Elf_Internal_Sym *sym;
4300 {
4301 if (h != NULL)
4302 {
4303 switch (ELF32_R_TYPE (rel->r_info))
4304 {
4305 case R_M32R_GNU_VTINHERIT:
4306 case R_M32R_GNU_VTENTRY:
4307 case R_M32R_RELA_GNU_VTINHERIT:
4308 case R_M32R_RELA_GNU_VTENTRY:
4309 break;
4310
4311 default:
4312 switch (h->root.type)
4313 {
4314 case bfd_link_hash_defined:
4315 case bfd_link_hash_defweak:
4316 return h->root.u.def.section;
4317
4318 case bfd_link_hash_common:
4319 return h->root.u.c.p->section;
4320
4321 default:
4322 break;
4323 }
4324 }
4325 }
4326 else
4327 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4328
4329 return NULL;
4330 }
4331
4332 static bfd_boolean
4333 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
4334 bfd *abfd ATTRIBUTE_UNUSED;
4335 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4336 asection *sec ATTRIBUTE_UNUSED;
4337 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
4338 {
4339 /* Update the got entry reference counts for the section being removed. */
4340 Elf_Internal_Shdr *symtab_hdr;
4341 struct elf_link_hash_entry **sym_hashes;
4342 bfd_signed_vma *local_got_refcounts;
4343 const Elf_Internal_Rela *rel, *relend;
4344 unsigned long r_symndx;
4345 struct elf_link_hash_entry *h;
4346
4347 elf_section_data (sec)->local_dynrel = NULL;
4348
4349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4350 sym_hashes = elf_sym_hashes (abfd);
4351 local_got_refcounts = elf_local_got_refcounts (abfd);
4352
4353 relend = relocs + sec->reloc_count;
4354 for (rel = relocs; rel < relend; rel++)
4355 switch (ELF32_R_TYPE (rel->r_info))
4356 {
4357 case R_M32R_GOT16_HI_ULO:
4358 case R_M32R_GOT16_HI_SLO:
4359 case R_M32R_GOT16_LO:
4360 case R_M32R_GOTOFF:
4361 case R_M32R_GOTOFF_HI_ULO:
4362 case R_M32R_GOTOFF_HI_SLO:
4363 case R_M32R_GOTOFF_LO:
4364 case R_M32R_GOT24:
4365 case R_M32R_GOTPC_HI_ULO:
4366 case R_M32R_GOTPC_HI_SLO:
4367 case R_M32R_GOTPC_LO:
4368 case R_M32R_GOTPC24:
4369 r_symndx = ELF32_R_SYM (rel->r_info);
4370 if (r_symndx >= symtab_hdr->sh_info)
4371 {
4372 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4373 if (h->got.refcount > 0)
4374 h->got.refcount--;
4375 }
4376 else
4377 {
4378 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
4379 local_got_refcounts[r_symndx]--;
4380 }
4381 break;
4382
4383 case R_M32R_16_RELA:
4384 case R_M32R_24_RELA:
4385 case R_M32R_32_RELA:
4386 case R_M32R_HI16_ULO_RELA:
4387 case R_M32R_HI16_SLO_RELA:
4388 case R_M32R_LO16_RELA:
4389 case R_M32R_SDA16_RELA:
4390 case R_M32R_18_PCREL_RELA:
4391 case R_M32R_26_PCREL_RELA:
4392 r_symndx = ELF32_R_SYM (rel->r_info);
4393 if (r_symndx >= symtab_hdr->sh_info)
4394 {
4395 struct elf_m32r_link_hash_entry *eh;
4396 struct elf_m32r_dyn_relocs **pp;
4397 struct elf_m32r_dyn_relocs *p;
4398
4399 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4400
4401 if (!info->shared && h->plt.refcount > 0)
4402 h->plt.refcount -= 1;
4403
4404 eh = (struct elf_m32r_link_hash_entry *) h;
4405
4406 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4407 if (p->sec == sec)
4408 {
4409 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4410 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
4411 p->pc_count -= 1;
4412 p->count -= 1;
4413 if (p->count == 0)
4414 *pp = p->next;
4415 break;
4416 }
4417 }
4418 break;
4419
4420 case R_M32R_26_PLTREL:
4421 r_symndx = ELF32_R_SYM (rel->r_info);
4422 if (r_symndx >= symtab_hdr->sh_info)
4423 {
4424 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4425 if (h->plt.refcount > 0)
4426 h->plt.refcount--;
4427 }
4428 break;
4429
4430 default:
4431 break;
4432 }
4433
4434 return TRUE;
4435 }
4436
4437 /* Look through the relocs for a section during the first phase.
4438 Since we don't do .gots or .plts, we just need to consider the
4439 virtual table relocs for gc. */
4440
4441 static bfd_boolean
4442 m32r_elf_check_relocs (abfd, info, sec, relocs)
4443 bfd *abfd;
4444 struct bfd_link_info *info;
4445 asection *sec;
4446 const Elf_Internal_Rela *relocs;
4447 {
4448 Elf_Internal_Shdr *symtab_hdr;
4449 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4450 const Elf_Internal_Rela *rel;
4451 const Elf_Internal_Rela *rel_end;
4452 struct elf_m32r_link_hash_table *htab;
4453 bfd *dynobj;
4454 bfd_vma *local_got_offsets;
4455 asection *sgot, *srelgot, *sreloc;
4456
4457 if (info->relocatable)
4458 return TRUE;
4459
4460 sgot = srelgot = sreloc = NULL;
4461
4462 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4463 sym_hashes = elf_sym_hashes (abfd);
4464 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
4465 if (!elf_bad_symtab (abfd))
4466 sym_hashes_end -= symtab_hdr->sh_info;
4467
4468 htab = m32r_elf_hash_table (info);
4469 dynobj = htab->root.dynobj;
4470 local_got_offsets = elf_local_got_offsets (abfd);
4471
4472 rel_end = relocs + sec->reloc_count;
4473 for (rel = relocs; rel < rel_end; rel++)
4474 {
4475 int r_type;
4476 struct elf_link_hash_entry *h;
4477 unsigned long r_symndx;
4478
4479 r_symndx = ELF32_R_SYM (rel->r_info);
4480 r_type = ELF32_R_TYPE (rel->r_info);
4481 if (r_symndx < symtab_hdr->sh_info)
4482 h = NULL;
4483 else
4484 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4485
4486 /* Some relocs require a global offset table. */
4487 if (htab->sgot == NULL)
4488 {
4489 switch (r_type)
4490 {
4491 case R_M32R_GOT16_HI_ULO:
4492 case R_M32R_GOT16_HI_SLO:
4493 case R_M32R_GOTOFF:
4494 case R_M32R_GOTOFF_HI_ULO:
4495 case R_M32R_GOTOFF_HI_SLO:
4496 case R_M32R_GOTOFF_LO:
4497 case R_M32R_GOT16_LO:
4498 case R_M32R_GOTPC24:
4499 case R_M32R_GOTPC_HI_ULO:
4500 case R_M32R_GOTPC_HI_SLO:
4501 case R_M32R_GOTPC_LO:
4502 case R_M32R_GOT24:
4503 if (dynobj == NULL)
4504 htab->root.dynobj = dynobj = abfd;
4505 if (! create_got_section (dynobj, info))
4506 return FALSE;
4507 break;
4508
4509 default:
4510 break;
4511 }
4512 }
4513
4514 switch (r_type)
4515 {
4516 case R_M32R_GOT16_HI_ULO:
4517 case R_M32R_GOT16_HI_SLO:
4518 case R_M32R_GOT16_LO:
4519 case R_M32R_GOT24:
4520
4521 if (h != NULL)
4522 h->got.refcount += 1;
4523 else
4524 {
4525 bfd_signed_vma *local_got_refcounts;
4526
4527 /* This is a global offset table entry for a local
4528 symbol. */
4529 local_got_refcounts = elf_local_got_refcounts (abfd);
4530 if (local_got_refcounts == NULL)
4531 {
4532 bfd_size_type size;
4533
4534 size = symtab_hdr->sh_info;
4535 size *= sizeof (bfd_signed_vma);
4536 local_got_refcounts = ((bfd_signed_vma *)
4537 bfd_zalloc (abfd, size));
4538 if (local_got_refcounts == NULL)
4539 return FALSE;
4540 elf_local_got_refcounts (abfd) = local_got_refcounts;
4541 }
4542 local_got_refcounts[r_symndx] += 1;
4543 }
4544 break;
4545
4546 case R_M32R_26_PLTREL:
4547 /* This symbol requires a procedure linkage table entry. We
4548 actually build the entry in adjust_dynamic_symbol,
4549 because this might be a case of linking PIC code without
4550 linking in any dynamic objects, in which case we don't
4551 need to generate a procedure linkage table after all. */
4552
4553 /* If this is a local symbol, we resolve it directly without
4554 creating a procedure linkage table entry. */
4555 if (h == NULL)
4556 continue;
4557
4558 if (h->forced_local)
4559 break;
4560
4561 h->needs_plt = 1;
4562 h->plt.refcount += 1;
4563 break;
4564
4565 case R_M32R_16_RELA:
4566 case R_M32R_24_RELA:
4567 case R_M32R_32_RELA:
4568 case R_M32R_HI16_ULO_RELA:
4569 case R_M32R_HI16_SLO_RELA:
4570 case R_M32R_LO16_RELA:
4571 case R_M32R_SDA16_RELA:
4572 case R_M32R_18_PCREL_RELA:
4573 case R_M32R_26_PCREL_RELA:
4574
4575 if (h != NULL && !info->shared)
4576 {
4577 h->non_got_ref = 1;
4578 h->plt.refcount += 1;
4579 }
4580
4581 /* If we are creating a shared library, and this is a reloc
4582 against a global symbol, or a non PC relative reloc
4583 against a local symbol, then we need to copy the reloc
4584 into the shared library. However, if we are linking with
4585 -Bsymbolic, we do not need to copy a reloc against a
4586 global symbol which is defined in an object we are
4587 including in the link (i.e., DEF_REGULAR is set). At
4588 this point we have not seen all the input files, so it is
4589 possible that DEF_REGULAR is not set now but will be set
4590 later (it is never cleared). We account for that
4591 possibility below by storing information in the
4592 dyn_relocs field of the hash table entry. A similar
4593 situation occurs when creating shared libraries and symbol
4594 visibility changes render the symbol local.
4595
4596 If on the other hand, we are creating an executable, we
4597 may need to keep relocations for symbols satisfied by a
4598 dynamic library if we manage to avoid copy relocs for the
4599 symbol. */
4600 if ((info->shared
4601 && (sec->flags & SEC_ALLOC) != 0
4602 && ((r_type != R_M32R_26_PCREL_RELA
4603 && r_type != R_M32R_18_PCREL_RELA)
4604 || (h != NULL
4605 && (! info->symbolic
4606 || h->root.type == bfd_link_hash_defweak
4607 || !h->def_regular))))
4608 || (!info->shared
4609 && (sec->flags & SEC_ALLOC) != 0
4610 && h != NULL
4611 && (h->root.type == bfd_link_hash_defweak
4612 || !h->def_regular)))
4613 {
4614 struct elf_m32r_dyn_relocs *p;
4615 struct elf_m32r_dyn_relocs **head;
4616
4617 if (dynobj == NULL)
4618 htab->root.dynobj = dynobj = abfd;
4619
4620 /* When creating a shared object, we must copy these
4621 relocs into the output file. We create a reloc
4622 section in dynobj and make room for the reloc. */
4623 if (sreloc == NULL)
4624 {
4625 const char *name;
4626
4627 name = (bfd_elf_string_from_elf_section
4628 (abfd,
4629 elf_elfheader (abfd)->e_shstrndx,
4630 elf_section_data (sec)->rel_hdr.sh_name));
4631 if (name == NULL)
4632 return FALSE;
4633
4634 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4635 && strcmp (bfd_get_section_name (abfd, sec),
4636 name + 5) == 0);
4637
4638 sreloc = bfd_get_section_by_name (dynobj, name);
4639 if (sreloc == NULL)
4640 {
4641 flagword flags;
4642
4643 sreloc = bfd_make_section (dynobj, name);
4644 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4645 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4646 if ((sec->flags & SEC_ALLOC) != 0)
4647 flags |= SEC_ALLOC | SEC_LOAD;
4648 if (sreloc == NULL
4649 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4650 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4651 return FALSE;
4652 }
4653 elf_section_data (sec)->sreloc = sreloc;
4654 }
4655
4656 /* If this is a global symbol, we count the number of
4657 relocations we need for this symbol. */
4658 if (h != NULL)
4659 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4660 else
4661 {
4662 asection *s;
4663
4664 /* Track dynamic relocs needed for local syms too. */
4665 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4666 sec, r_symndx);
4667 if (s == NULL)
4668 return FALSE;
4669
4670 head = ((struct elf_m32r_dyn_relocs **)
4671 &elf_section_data (s)->local_dynrel);
4672 }
4673
4674 p = *head;
4675 if (p == NULL || p->sec != sec)
4676 {
4677 bfd_size_type amt = sizeof (*p);
4678 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4679 if (p == NULL)
4680 return FALSE;
4681 p->next = *head;
4682 *head = p;
4683 p->sec = sec;
4684 p->count = 0;
4685 p->pc_count = 0;
4686 }
4687
4688 p->count += 1;
4689 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4690 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4691 p->pc_count += 1;
4692 }
4693 break;
4694
4695 /* This relocation describes the C++ object vtable hierarchy.
4696 Reconstruct it for later use during GC. */
4697 case R_M32R_RELA_GNU_VTINHERIT:
4698 case R_M32R_GNU_VTINHERIT:
4699 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4700 return FALSE;
4701 break;
4702
4703 /* This relocation describes which C++ vtable entries are actually
4704 used. Record for later use during GC. */
4705 case R_M32R_GNU_VTENTRY:
4706 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4707 return FALSE;
4708 break;
4709 case R_M32R_RELA_GNU_VTENTRY:
4710 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4711 return FALSE;
4712 break;
4713 }
4714 }
4715
4716 return TRUE;
4717 }
4718
4719 static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4720 {
4721 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4722 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
4723 { NULL, 0, 0, 0, 0 }
4724 };
4725
4726 static bfd_boolean
4727 m32r_elf_fake_sections (abfd, hdr, sec)
4728 bfd *abfd;
4729 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4730 asection *sec;
4731 {
4732 register const char *name;
4733
4734 name = bfd_get_section_name (abfd, sec);
4735
4736 /* The generic elf_fake_sections will set up REL_HDR using the
4737 default kind of relocations. But, we may actually need both
4738 kinds of relocations, so we set up the second header here.
4739
4740 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4741 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4742 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4743 of the resulting empty .rela.<section> sections starts with
4744 sh_offset == object size, and ld doesn't allow that. While the check
4745 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4746 avoided by not emitting those useless sections in the first place. */
4747 if ((sec->flags & SEC_RELOC) != 0)
4748 {
4749 struct bfd_elf_section_data *esd;
4750 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4751
4752 esd = elf_section_data (sec);
4753 BFD_ASSERT (esd->rel_hdr2 == NULL);
4754 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4755 if (!esd->rel_hdr2)
4756 return FALSE;
4757 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4758 !sec->use_rela_p);
4759 }
4760
4761 return TRUE;
4762 }
4763
4764 static enum elf_reloc_type_class
4765 m32r_elf_reloc_type_class (rela)
4766 const Elf_Internal_Rela *rela;
4767 {
4768 switch ((int) ELF32_R_TYPE (rela->r_info))
4769 {
4770 case R_M32R_RELATIVE:
4771 return reloc_class_relative;
4772 case R_M32R_JMP_SLOT:
4773 return reloc_class_plt;
4774 case R_M32R_COPY:
4775 return reloc_class_copy;
4776 default:
4777 return reloc_class_normal;
4778 }
4779 }
4780 \f
4781 #define ELF_ARCH bfd_arch_m32r
4782 #define ELF_MACHINE_CODE EM_M32R
4783 #define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
4784 #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
4785
4786 #define TARGET_BIG_SYM bfd_elf32_m32r_vec
4787 #define TARGET_BIG_NAME "elf32-m32r"
4788 #define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4789 #define TARGET_LITTLE_NAME "elf32-m32rle"
4790
4791 #define elf_info_to_howto m32r_info_to_howto
4792 #define elf_info_to_howto_rel m32r_info_to_howto_rel
4793 #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4794 #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4795 #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4796 #define elf_backend_relocate_section m32r_elf_relocate_section
4797 #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4798 #define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4799 #define elf_backend_check_relocs m32r_elf_check_relocs
4800
4801 #define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4802 #define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4803 #define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4804 #define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4805 #define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4806 #define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4807 #define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4808 #define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4809
4810 #define elf_backend_can_gc_sections 1
4811 /*#if !USE_REL
4812 #define elf_backend_rela_normal 1
4813 #endif*/
4814 #define elf_backend_can_refcount 1
4815 #define elf_backend_want_got_plt 1
4816 #define elf_backend_plt_readonly 1
4817 #define elf_backend_want_plt_sym 0
4818 #define elf_backend_got_header_size 12
4819
4820 #define elf_backend_may_use_rel_p 1
4821 #ifdef USE_M32R_OLD_RELOC
4822 #define elf_backend_default_use_rela_p 0
4823 #define elf_backend_may_use_rela_p 0
4824 #else
4825 #define elf_backend_default_use_rela_p 1
4826 #define elf_backend_may_use_rela_p 1
4827 #define elf_backend_fake_sections m32r_elf_fake_sections
4828 #endif
4829
4830 #if 0 /* not yet */
4831 /* relax support */
4832 #define bfd_elf32_bfd_relax_section m32r_elf_relax_section
4833 #define bfd_elf32_bfd_get_relocated_section_contents \
4834 m32r_elf_get_relocated_section_contents
4835 #endif
4836
4837 #define elf_backend_object_p m32r_elf_object_p
4838 #define elf_backend_final_write_processing m32r_elf_final_write_processing
4839 #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4840 #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4841 #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
4842 #define elf_backend_special_sections m32r_elf_special_sections
4843
4844 #include "elf32-target.h"
4845
4846 #undef ELF_MAXPAGESIZE
4847 #define ELF_MAXPAGESIZE 0x1000
4848
4849 #undef TARGET_BIG_SYM
4850 #define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4851 #undef TARGET_BIG_NAME
4852 #define TARGET_BIG_NAME "elf32-m32r-linux"
4853 #undef TARGET_LITTLE_SYM
4854 #define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4855 #undef TARGET_LITTLE_NAME
4856 #define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4857 #undef elf32_bed
4858 #define elf32_bed elf32_m32r_lin_bed
4859
4860 #include "elf32-target.h"
4861
This page took 0.1675 seconds and 4 git commands to generate.