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