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