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