2004-05-27 Michael Chastain <mec.gnu@mindspring.com>
[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
555cd476 54 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
252b5132 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;
555cd476 1394 Elf_Internal_Sym *sym;
252b5132 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
c152c796 1745 && ! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
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
6edf0760
NC
2052/* Allocate space in .plt, .got and associated reloc sections for
2053 dynamic relocs. */
41978308 2054
6edf0760
NC
2055static bfd_boolean
2056allocate_dynrelocs (h, inf)
2057 struct elf_link_hash_entry *h;
2058 PTR inf;
2059{
2060 struct bfd_link_info *info;
2061 struct elf_m32r_link_hash_table *htab;
2062 struct elf_m32r_link_hash_entry *eh;
2063 struct elf_m32r_dyn_relocs *p;
41978308 2064
6edf0760
NC
2065 if (h->root.type == bfd_link_hash_indirect)
2066 return TRUE;
41978308 2067
6edf0760
NC
2068 if (h->root.type == bfd_link_hash_warning)
2069 /* When warning symbols are created, they **replace** the "real"
2070 entry in the hash table, thus we never get to see the real
2071 symbol in a hash traversal. So look at it now. */
2072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
41978308 2073
6edf0760
NC
2074 info = (struct bfd_link_info *) inf;
2075 htab = m32r_elf_hash_table (info);
41978308 2076
6edf0760
NC
2077 eh = (struct elf_m32r_link_hash_entry *) h;
2078// if ((h->got.refcount > 0
2079// || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2080// && eh->gotplt_refcount > 0)
2081// {
2082// /* The symbol has been forced local, or we have some direct got refs,
2083// so treat all the gotplt refs as got refs. */
2084// h->got.refcount += eh->gotplt_refcount;
2085// if (h->plt.refcount >= eh->gotplt_refcount)
2086// h->plt.refcount -= eh->gotplt_refcount;
2087// }
41978308 2088
6edf0760
NC
2089 if (htab->root.dynamic_sections_created
2090 && h->plt.refcount > 0)
2091 {
2092 /* Make sure this symbol is output as a dynamic symbol.
2093 Undefined weak syms won't yet be marked as dynamic. */
2094 if (h->dynindx == -1
2095 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2096 {
c152c796 2097 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
2098 return FALSE;
2099 }
41978308 2100
c152c796 2101 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
6edf0760
NC
2102 {
2103 asection *s = htab->splt;
41978308 2104
6edf0760
NC
2105 /* If this is the first .plt entry, make room for the special
2106 first entry. */
2107 if (s->_raw_size == 0)
2108 s->_raw_size += PLT_ENTRY_SIZE;
41978308 2109
6edf0760 2110 h->plt.offset = s->_raw_size;
41978308 2111
6edf0760
NC
2112 /* If this symbol is not defined in a regular file, and we are
2113 not generating a shared library, then set the symbol to this
2114 location in the .plt. This is required to make function
2115 pointers compare as equal between the normal executable and
2116 the shared library. */
2117 if (! info->shared
2118 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2119 {
2120 h->root.u.def.section = s;
2121 h->root.u.def.value = h->plt.offset;
2122 }
41978308 2123
6edf0760
NC
2124 /* Make room for this entry. */
2125 s->_raw_size += PLT_ENTRY_SIZE;
41978308 2126
6edf0760
NC
2127 /* We also need to make an entry in the .got.plt section, which
2128 will be placed in the .got section by the linker script. */
2129 htab->sgotplt->_raw_size += 4;
41978308 2130
6edf0760
NC
2131 /* We also need to make an entry in the .rel.plt section. */
2132 htab->srelplt->_raw_size += sizeof (Elf32_External_Rela);
2133 }
2134 else
2135 {
2136 h->plt.offset = (bfd_vma) -1;
2137 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2138 }
2139 }
2140 else
2141 {
2142 h->plt.offset = (bfd_vma) -1;
2143 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2144 }
41978308 2145
6edf0760
NC
2146 if (h->got.refcount > 0)
2147 {
2148 asection *s;
2149 bfd_boolean dyn;
41978308 2150
6edf0760
NC
2151 /* Make sure this symbol is output as a dynamic symbol.
2152 Undefined weak syms won't yet be marked as dynamic. */
2153 if (h->dynindx == -1
2154 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2155 {
c152c796 2156 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
2157 return FALSE;
2158 }
41978308 2159
6edf0760
NC
2160 s = htab->sgot;
2161
2162 h->got.offset = s->_raw_size;
2163 s->_raw_size += 4;
2164 dyn = htab->root.dynamic_sections_created;
c152c796 2165 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
6edf0760
NC
2166 htab->srelgot->_raw_size += sizeof (Elf32_External_Rela);
2167 }
2168 else
2169 h->got.offset = (bfd_vma) -1;
41978308 2170
6edf0760
NC
2171 if (eh->dyn_relocs == NULL)
2172 return TRUE;
41978308 2173
6edf0760
NC
2174 /* In the shared -Bsymbolic case, discard space allocated for
2175 dynamic pc-relative relocs against symbols which turn out to be
2176 defined in regular objects. For the normal shared case, discard
2177 space for pc-relative relocs that have become local due to symbol
2178 visibility changes. */
2179
2180 if (info->shared)
2181 {
2182 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2183 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2184 || info->symbolic))
2185 {
2186 struct elf_m32r_dyn_relocs **pp;
2187 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2188 {
2189 p->count -= p->pc_count;
2190 p->pc_count = 0;
2191 if (p->count == 0)
2192 *pp = p->next;
2193 else
2194 pp = &p->next;
2195 }
2196 }
2197 }
2198 else
2199 {
2200 /* For the non-shared case, discard space for relocs against
2201 symbols which turn out to need copy relocs or are not
2202 dynamic. */
41978308 2203
6edf0760
NC
2204 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2205 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2206 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2207 || (htab->root.dynamic_sections_created
2208 && (h->root.type == bfd_link_hash_undefweak
2209 || h->root.type == bfd_link_hash_undefined))))
2210 {
2211 /* Make sure this symbol is output as a dynamic symbol.
2212 Undefined weak syms won't yet be marked as dynamic. */
2213 if (h->dynindx == -1
2214 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2215 {
c152c796 2216 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
2217 return FALSE;
2218 }
41978308 2219
6edf0760
NC
2220 /* If that succeeded, we know we'll be keeping all the
2221 relocs. */
2222 if (h->dynindx != -1)
2223 goto keep;
2224 }
41978308 2225
6edf0760 2226 eh->dyn_relocs = NULL;
41978308 2227
6edf0760
NC
2228 keep: ;
2229 }
41978308 2230
6edf0760
NC
2231 /* Finally, allocate space. */
2232 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2233 {
2234 asection *sreloc = elf_section_data (p->sec)->sreloc;
2235 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2236 }
41978308 2237
6edf0760
NC
2238 return TRUE;
2239}
2240/* Find any dynamic relocs that apply to read-only sections. */
41978308 2241
6edf0760
NC
2242static bfd_boolean
2243readonly_dynrelocs (h, inf)
2244 struct elf_link_hash_entry *h;
2245 PTR inf;
2246{
2247 struct elf_m32r_link_hash_entry *eh;
2248 struct elf_m32r_dyn_relocs *p;
2249
2250 if (h->root.type == bfd_link_hash_warning)
2251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
252b5132 2252
6edf0760
NC
2253 eh = (struct elf_m32r_link_hash_entry *) h;
2254 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2255 {
2256 asection *s = p->sec->output_section;
252b5132 2257
6edf0760
NC
2258 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2259 {
2260 struct bfd_link_info *info = (struct bfd_link_info *) inf;
252b5132 2261
6edf0760 2262 info->flags |= DF_TEXTREL;
252b5132 2263
6edf0760
NC
2264 /* Not an error, just cut short the traversal. */
2265 return FALSE;
2266 }
252b5132 2267 }
6edf0760
NC
2268 return TRUE;
2269}
252b5132 2270
6edf0760 2271/* Set the sizes of the dynamic sections. */
41978308 2272
6edf0760
NC
2273static bfd_boolean
2274m32r_elf_size_dynamic_sections (output_bfd, info)
2275 bfd *output_bfd ATTRIBUTE_UNUSED;
2276 struct bfd_link_info *info;
2277{
2278 struct elf_m32r_link_hash_table *htab;
2279 bfd *dynobj;
2280 asection *s;
2281 bfd_boolean relocs;
2282 bfd *ibfd;
2283
2284#ifdef DEBUG_PIC
2285printf("m32r_elf_size_dynamic_sections()\n");
2286#endif
2287
2288 htab = m32r_elf_hash_table (info);
2289 dynobj = htab->root.dynobj;
2290 BFD_ASSERT (dynobj != NULL);
2291
2292 if (htab->root.dynamic_sections_created)
252b5132 2293 {
6edf0760
NC
2294 /* Set the contents of the .interp section to the interpreter. */
2295 if (! info->shared)
2296 {
2297 s = bfd_get_section_by_name (dynobj, ".interp");
2298 BFD_ASSERT (s != NULL);
2299 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2300 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2301 }
252b5132
RH
2302 }
2303
6edf0760
NC
2304 /* Set up .got offsets for local syms, and space for local dynamic
2305 relocs. */
2306 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2307 {
2308 bfd_signed_vma *local_got;
2309 bfd_signed_vma *end_local_got;
2310 bfd_size_type locsymcount;
2311 Elf_Internal_Shdr *symtab_hdr;
2312 asection *srel;
252b5132 2313
6edf0760
NC
2314 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2315 continue;
252b5132 2316
6edf0760
NC
2317 for (s = ibfd->sections; s != NULL; s = s->next)
2318 {
2319 struct elf_m32r_dyn_relocs *p;
2320
2321 for (p = ((struct elf_m32r_dyn_relocs *)
2322 elf_section_data (s)->local_dynrel);
2323 p != NULL;
2324 p = p->next)
2325 {
2326 if (! bfd_is_abs_section (p->sec)
2327 && bfd_is_abs_section (p->sec->output_section))
2328 {
2329 /* Input section has been discarded, either because
2330 it is a copy of a linkonce section or due to
2331 linker script /DISCARD/, so we'll be discarding
2332 the relocs too. */
2333 }
2334 else if (p->count != 0)
2335 {
2336 srel = elf_section_data (p->sec)->sreloc;
2337 srel->_raw_size += p->count * sizeof (Elf32_External_Rela);
2338 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2339 info->flags |= DF_TEXTREL;
2340 }
2341 }
2342 }
2343
2344 local_got = elf_local_got_refcounts (ibfd);
2345 if (!local_got)
2346 continue;
2347
2348 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2349 locsymcount = symtab_hdr->sh_info;
2350 end_local_got = local_got + locsymcount;
2351 s = htab->sgot;
2352 srel = htab->srelgot;
2353 for (; local_got < end_local_got; ++local_got)
2354 {
2355 if (*local_got > 0)
2356 {
2357 *local_got = s->_raw_size;
2358 s->_raw_size += 4;
2359 if (info->shared)
2360 srel->_raw_size += sizeof (Elf32_External_Rela);
2361 }
2362 else
2363 *local_got = (bfd_vma) -1;
2364 }
2365 }
2366
2367 /* Allocate global sym .plt and .got entries, and space for global
2368 sym dynamic relocs. */
2369 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2370
2371 /* We now have determined the sizes of the various dynamic sections.
2372 Allocate memory for them. */
2373 relocs = FALSE;
2374 for (s = dynobj->sections; s != NULL; s = s->next)
252b5132 2375 {
6edf0760
NC
2376 if ((s->flags & SEC_LINKER_CREATED) == 0)
2377 continue;
252b5132 2378
6edf0760
NC
2379 if (s == htab->splt
2380 || s == htab->sgot
2381 || s == htab->sgotplt)
2382 {
2383 /* Strip this section if we don't need it; see the
2384 comment below. */
2385 }
2386 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2387 {
2388 if (s->_raw_size != 0 && s != htab->srelplt)
2389 relocs = TRUE;
2390
2391 /* We use the reloc_count field as a counter if we need
2392 to copy relocs into the output file. */
2393 s->reloc_count = 0;
2394 }
252b5132 2395 else
6edf0760
NC
2396 {
2397 /* It's not one of our sections, so don't allocate space. */
2398 continue;
2399 }
2400
2401 if (s->_raw_size == 0)
2402 {
2403 /* If we don't need this section, strip it from the
2404 output file. This is mostly to handle .rela.bss and
2405 .rela.plt. We must create both sections in
2406 create_dynamic_sections, because they must be created
2407 before the linker maps input sections to output
2408 sections. The linker does that before
2409 adjust_dynamic_symbol is called, and it is that
2410 function which decides whether anything needs to go
2411 into these sections. */
2412 _bfd_strip_section_from_output (info, s);
2413 continue;
2414 }
2415
2416 /* Allocate memory for the section contents. We use bfd_zalloc
2417 here in case unused entries are not reclaimed before the
2418 section's contents are written out. This should not happen,
2419 but this way if it does, we get a R_M32R_NONE reloc instead
2420 of garbage. */
2421 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2422 if (s->contents == NULL)
2423 return FALSE;
2424 }
2425
2426 if (htab->root.dynamic_sections_created)
2427 {
2428 /* Add some entries to the .dynamic section. We fill in the
2429 values later, in m32r_elf_finish_dynamic_sections, but we
2430 must add the entries now so that we get the correct size for
2431 the .dynamic section. The DT_DEBUG entry is filled in by the
2432 dynamic linker and used by the debugger. */
2433#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2434 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6edf0760
NC
2435
2436 if (! info->shared)
252b5132 2437 {
6edf0760
NC
2438 if (! add_dynamic_entry (DT_DEBUG, 0))
2439 return FALSE;
252b5132 2440 }
6edf0760
NC
2441
2442 if (htab->splt->_raw_size != 0)
2443 {
2444 if (! add_dynamic_entry (DT_PLTGOT, 0)
2445 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2446 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2447 || ! add_dynamic_entry (DT_JMPREL, 0))
2448 return FALSE;
2449 }
2450
2451 if (relocs)
2452 {
2453 if (! add_dynamic_entry (DT_RELA, 0)
2454 || ! add_dynamic_entry (DT_RELASZ, 0)
2455 || ! add_dynamic_entry (DT_RELAENT,
2456 sizeof (Elf32_External_Rela)))
2457 return FALSE;
2458
2459 /* If any dynamic relocs apply to a read-only section,
2460 then we need a DT_TEXTREL entry. */
2461 if ((info->flags & DF_TEXTREL) == 0)
2462 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2463 (PTR) info);
2464
2465 if ((info->flags & DF_TEXTREL) != 0)
2466 {
2467 if (! add_dynamic_entry (DT_TEXTREL, 0))
2468 return FALSE;
2469 }
2470 }
252b5132 2471 }
6edf0760
NC
2472#undef add_dynamic_entry
2473
2474 return TRUE;
252b5132 2475}
252b5132
RH
2476/* Relocate an M32R/D ELF section.
2477 There is some attempt to make this function usable for many architectures,
f0fe0e16 2478 both for RELA and REL type relocs, if only to serve as a learning tool.
252b5132
RH
2479
2480 The RELOCATE_SECTION function is called by the new ELF backend linker
2481 to handle the relocations for a section.
2482
2483 The relocs are always passed as Rela structures; if the section
2484 actually uses Rel structures, the r_addend field will always be
2485 zero.
2486
2487 This function is responsible for adjust the section contents as
2488 necessary, and (if using Rela relocs and generating a
1049f94e 2489 relocatable output file) adjusting the reloc addend as
252b5132
RH
2490 necessary.
2491
2492 This function does not have to worry about setting the reloc
2493 address or the reloc symbol index.
2494
2495 LOCAL_SYMS is a pointer to the swapped in local symbols.
2496
2497 LOCAL_SECTIONS is an array giving the section in the input file
2498 corresponding to the st_shndx field of each local symbol.
2499
2500 The global hash table entry for the global symbols can be found
2501 via elf_sym_hashes (input_bfd).
2502
1049f94e 2503 When generating relocatable output, this function must handle
252b5132
RH
2504 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2505 going to be the section symbol corresponding to the output
2506 section, which means that the addend must be adjusted
2507 accordingly. */
2508
b34976b6 2509static bfd_boolean
252b5132
RH
2510m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2511 contents, relocs, local_syms, local_sections)
5f771d47 2512 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
2513 struct bfd_link_info *info;
2514 bfd *input_bfd;
2515 asection *input_section;
2516 bfd_byte *contents;
2517 Elf_Internal_Rela *relocs;
2518 Elf_Internal_Sym *local_syms;
2519 asection **local_sections;
2520{
2521 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2522 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2523 Elf_Internal_Rela *rel, *relend;
2524 /* Assume success. */
b34976b6 2525 bfd_boolean ret = TRUE;
252b5132 2526
6edf0760
NC
2527 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2528 bfd *dynobj;
2529 bfd_vma *local_got_offsets;
2530 asection *sgot, *splt, *sreloc;
2531
2532 dynobj = htab->root.dynobj;
2533 local_got_offsets = elf_local_got_offsets (input_bfd);
41978308 2534
6edf0760
NC
2535 sgot = htab->sgot;
2536 splt = htab->splt;
2537 sreloc = NULL;
b491616a 2538
252b5132
RH
2539 rel = relocs;
2540 relend = relocs + input_section->reloc_count;
2541 for (; rel < relend; rel++)
2542 {
2543 int r_type;
2544 reloc_howto_type *howto;
2545 unsigned long r_symndx;
6edf0760 2546 struct elf_link_hash_entry *h;
252b5132 2547 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
6edf0760
NC
2548 ensure it's zero (we use REL relocs, not RELA). Therefore this
2549 should be assigning zero to `addend', but for clarity we use
2550 `r_addend'. */
252b5132
RH
2551 bfd_vma addend = rel->r_addend;
2552 bfd_vma offset = rel->r_offset;
252b5132
RH
2553 Elf_Internal_Sym *sym;
2554 asection *sec;
2555 const char *sym_name;
2556 bfd_reloc_status_type r;
2557 const char *errmsg = NULL;
6edf0760 2558 bfd_boolean use_rel = FALSE;
252b5132
RH
2559
2560 h = NULL;
2561 r_type = ELF32_R_TYPE (rel->r_info);
2562 if (r_type < 0 || r_type >= (int) R_M32R_max)
2563 {
2564 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
8f615d07 2565 bfd_archive_filename (input_bfd),
252b5132
RH
2566 (int) r_type);
2567 bfd_set_error (bfd_error_bad_value);
b34976b6 2568 ret = FALSE;
252b5132
RH
2569 continue;
2570 }
2571
2572 if (r_type == R_M32R_GNU_VTENTRY
6edf0760
NC
2573 || r_type == R_M32R_GNU_VTINHERIT
2574 || r_type == R_M32R_NONE
2575 || r_type == R_M32R_RELA_GNU_VTENTRY
2576 || r_type == R_M32R_RELA_GNU_VTINHERIT)
252b5132
RH
2577 continue;
2578
6edf0760
NC
2579 if (r_type <= R_M32R_GNU_VTENTRY)
2580 use_rel = TRUE;
2581
252b5132
RH
2582 howto = m32r_elf_howto_table + r_type;
2583 r_symndx = ELF32_R_SYM (rel->r_info);
2584
6edf0760 2585 if (info->relocatable && (use_rel == TRUE))
252b5132 2586 {
1049f94e 2587 /* This is a relocatable link. We don't have to change
252b5132
RH
2588 anything, unless the reloc is against a section symbol,
2589 in which case we have to adjust according to where the
2590 section symbol winds up in the output section. */
2591 sec = NULL;
2592 if (r_symndx >= symtab_hdr->sh_info)
2593 {
2594 /* External symbol. */
2595 continue;
2596 }
2597
2598 /* Local symbol. */
2599 sym = local_syms + r_symndx;
2600 sym_name = "<local symbol>";
2601 /* STT_SECTION: symbol is associated with a section. */
2602 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2603 {
2604 /* Symbol isn't associated with a section. Nothing to do. */
2605 continue;
2606 }
2607
2608 sec = local_sections[r_symndx];
2609 addend += sec->output_offset + sym->st_value;
252b5132 2610
252b5132
RH
2611 /* If partial_inplace, we need to store any additional addend
2612 back in the section. */
2613 if (! howto->partial_inplace)
2614 continue;
2615 /* ??? Here is a nice place to call a special_function
2616 like handler. */
2617 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2618 r = _bfd_relocate_contents (howto, input_bfd,
2619 addend, contents + offset);
2620 else
2621 {
2622 Elf_Internal_Rela *lorel;
2623
2624 /* We allow an arbitrary number of HI16 relocs before the
2625 LO16 reloc. This permits gcc to emit the HI and LO relocs
2626 itself. */
2627 for (lorel = rel + 1;
2628 (lorel < relend
2629 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2630 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2631 lorel++)
2632 continue;
2633 if (lorel < relend
2634 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2635 {
2636 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2637 contents, addend);
2638 r = bfd_reloc_ok;
2639 }
2640 else
2641 r = _bfd_relocate_contents (howto, input_bfd,
2642 addend, contents + offset);
2643 }
252b5132
RH
2644 }
2645 else
2646 {
2647 bfd_vma relocation;
2648
2649 /* This is a final link. */
2650 sym = NULL;
2651 sec = NULL;
6edf0760 2652 h = NULL;
252b5132
RH
2653
2654 if (r_symndx < symtab_hdr->sh_info)
2655 {
2656 /* Local symbol. */
2657 sym = local_syms + r_symndx;
2658 sec = local_sections[r_symndx];
2659 sym_name = "<local symbol>";
6edf0760
NC
2660
2661 if (use_rel == FALSE)
2662 {
2663 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2664 addend = rel->r_addend;
2665
2666 if (info->relocatable)
2667 {
2668 /* This is a relocatable link. We don't have to change
2669 anything, unless the reloc is against a section symbol,
2670 in which case we have to adjust according to where the
2671 section symbol winds up in the output section. */
2672 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2673 rel->r_addend += sec->output_offset + sym->st_value;
41978308 2674
6edf0760
NC
2675 continue;
2676 }
2677 }
2678 else
2679 {
2680 relocation = (sec->output_section->vma
2681 + sec->output_offset
2682 + sym->st_value);
2683 }
252b5132
RH
2684 }
2685 else
2686 {
2687 /* External symbol. */
6edf0760
NC
2688 if (info->relocatable && (use_rel == FALSE))
2689 continue;
2690
252b5132
RH
2691 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2692 while (h->root.type == bfd_link_hash_indirect
2693 || h->root.type == bfd_link_hash_warning)
2694 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2695 sym_name = h->root.root.string;
2696
2697 if (h->root.type == bfd_link_hash_defined
2698 || h->root.type == bfd_link_hash_defweak)
2699 {
6edf0760 2700 bfd_boolean dyn;
252b5132 2701 sec = h->root.u.def.section;
6edf0760
NC
2702
2703 dyn = htab->root.dynamic_sections_created;
2704 sec = h->root.u.def.section;
2705 if (r_type == R_M32R_GOTPC24
2706 || (r_type == R_M32R_GOTPC_HI_ULO
2707 || r_type == R_M32R_GOTPC_HI_SLO
2708 || r_type == R_M32R_GOTPC_LO)
2709 || (r_type == R_M32R_26_PLTREL
2710 && h->plt.offset != (bfd_vma) -1)
2711 || ((r_type == R_M32R_GOT24
2712 || r_type == R_M32R_GOT16_HI_ULO
2713 || r_type == R_M32R_GOT16_HI_SLO
2714 || r_type == R_M32R_GOT16_LO)
c152c796
AM
2715 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2716 info->shared, h)
6edf0760
NC
2717 && (! info->shared
2718 || (! info->symbolic && h->dynindx != -1)
2719 || (h->elf_link_hash_flags
2720 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2721 || (info->shared
2722 && ((! info->symbolic && h->dynindx != -1)
2723 || (h->elf_link_hash_flags
2724 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2725 && (((r_type == R_M32R_16_RELA
2726 || r_type == R_M32R_32_RELA
2727 || r_type == R_M32R_24_RELA
2728 || r_type == R_M32R_HI16_ULO_RELA
2729 || r_type == R_M32R_HI16_SLO_RELA
2730 || r_type == R_M32R_LO16_RELA)
2731 && (h->elf_link_hash_flags
2732 & ELF_LINK_FORCED_LOCAL) == 0)
2733 || r_type == R_M32R_10_PCREL_RELA
2734 || r_type == R_M32R_18_PCREL_RELA
2735 || r_type == R_M32R_26_PCREL_RELA)
2736 && ((input_section->flags & SEC_ALLOC) != 0
2737 /* DWARF will emit R_M32R_16(24,32) relocations
41978308 2738 in its sections against symbols defined
6edf0760
NC
2739 externally in shared libraries. We can't do
2740 anything with them here. */
2741 || ((input_section->flags & SEC_DEBUGGING) != 0
2742 && (h->elf_link_hash_flags
2743 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
2744 {
2745 /* In these cases, we don't need the relocation
2746 value. We check specially because in some
2747 obscure cases sec->output_section will be NULL. */
2748 relocation = 0;
2749 }
2750 else if (sec->output_section == NULL)
2751 {
2752 (*_bfd_error_handler)
2753 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2754 bfd_get_filename (input_bfd), h->root.root.string,
2755 bfd_get_section_name (input_bfd, input_section));
2756
2757 relocation = 0;
2758 }
252b5132
RH
2759 else
2760 relocation = (h->root.u.def.value
2761 + sec->output_section->vma
2762 + sec->output_offset);
2763 }
2764 else if (h->root.type == bfd_link_hash_undefweak)
2765 relocation = 0;
59c2e50f 2766 else if (info->unresolved_syms_in_objects == RM_IGNORE
6edf0760
NC
2767 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2768 relocation = 0;
252b5132
RH
2769 else
2770 {
2771 if (! ((*info->callbacks->undefined_symbol)
2772 (info, h->root.root.string, input_bfd,
6edf0760 2773 input_section, offset,
59c2e50f 2774 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
6edf0760 2775 || ELF_ST_VISIBILITY (h->other)))))
b34976b6 2776 return FALSE;
252b5132
RH
2777 relocation = 0;
2778 }
2779 }
2780
2781 /* Sanity check the address. */
2782 if (offset > input_section->_raw_size)
2783 {
2784 r = bfd_reloc_outofrange;
2785 goto check_reloc;
2786 }
2787
2788 switch ((int) r_type)
2789 {
6edf0760
NC
2790 case R_M32R_GOTPC24:
2791 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2792 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2793 */
2794 relocation = sgot->output_section->vma;
2795 break;
2796
2797 case R_M32R_GOTPC_HI_ULO:
2798 case R_M32R_GOTPC_HI_SLO:
41978308 2799 case R_M32R_GOTPC_LO:
6edf0760
NC
2800 {
2801 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2802 bl .+4
2803 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2804 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2805 or
2806 bl .+4
2807 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2808 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2809 */
2810 relocation = sgot->output_section->vma;
2811 relocation -= (input_section->output_section->vma
2812 + input_section->output_offset
2813 + rel->r_offset);
41978308 2814 if ((r_type == R_M32R_GOTPC_HI_SLO)
6edf0760
NC
2815 && ((relocation + rel->r_addend) & 0x8000))
2816 rel->r_addend += 0x10000;
2817
2818 break;
2819 }
2820 case R_M32R_GOT16_HI_ULO:
2821 case R_M32R_GOT16_HI_SLO:
41978308 2822 case R_M32R_GOT16_LO:
6edf0760
NC
2823 /* Fall through. */
2824 case R_M32R_GOT24:
2825 /* Relocation is to the entry for this symbol in the global
2826 offset table. */
2827 BFD_ASSERT (sgot != NULL);
41978308 2828
6edf0760
NC
2829 if (h != NULL)
2830 {
2831 bfd_boolean dyn;
2832 bfd_vma off;
41978308 2833
6edf0760
NC
2834 off = h->got.offset;
2835 BFD_ASSERT (off != (bfd_vma) -1);
2836
2837 dyn = htab->root.dynamic_sections_created;
c152c796 2838 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
6edf0760
NC
2839 || (info->shared
2840 && (info->symbolic
2841 || h->dynindx == -1
2842 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2843 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2844 {
2845 /* This is actually a static link, or it is a
2846 -Bsymbolic link and the symbol is defined
2847 locally, or the symbol was forced to be local
2848 because of a version file. We must initialize
2849 this entry in the global offset table. Since the
2850 offset must always be a multiple of 4, we use the
2851 least significant bit to record whether we have
2852 initialized it already.
41978308 2853
6edf0760
NC
2854 When doing a dynamic link, we create a .rela.got
2855 relocation entry to initialize the value. This
2856 is done in the finish_dynamic_symbol routine. */
2857 if ((off & 1) != 0)
2858 off &= ~1;
2859 else
2860 {
2861 bfd_put_32 (output_bfd, relocation,
2862 sgot->contents + off);
2863 h->got.offset |= 1;
2864 }
2865 }
41978308 2866
6edf0760
NC
2867 relocation = sgot->output_offset + off;
2868 }
2869 else
2870 {
2871 bfd_vma off;
2872 bfd_byte *loc;
41978308 2873
6edf0760
NC
2874 BFD_ASSERT (local_got_offsets != NULL
2875 && local_got_offsets[r_symndx] != (bfd_vma) -1);
41978308 2876
6edf0760 2877 off = local_got_offsets[r_symndx];
41978308 2878
6edf0760
NC
2879 /* The offset must always be a multiple of 4. We use
2880 the least significant bit to record whether we have
2881 already processed this entry. */
2882 if ((off & 1) != 0)
2883 off &= ~1;
2884 else
2885 {
2886 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
41978308 2887
6edf0760
NC
2888 if (info->shared)
2889 {
2890 asection *srelgot;
2891 Elf_Internal_Rela outrel;
41978308 2892
6edf0760
NC
2893 /* We need to generate a R_M32R_RELATIVE reloc
2894 for the dynamic linker. */
2895 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2896 BFD_ASSERT (srelgot != NULL);
41978308 2897
6edf0760
NC
2898 outrel.r_offset = (sgot->output_section->vma
2899 + sgot->output_offset
2900 + off);
2901 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2902 outrel.r_addend = relocation;
2903 loc = srelgot->contents;
2904 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2905 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2906 ++srelgot->reloc_count;
2907 }
41978308 2908
6edf0760
NC
2909 local_got_offsets[r_symndx] |= 1;
2910 }
41978308 2911
6edf0760
NC
2912 relocation = sgot->output_offset + off;
2913 }
2914 if ((r_type == R_M32R_GOT16_HI_SLO)
2915 && ((relocation + rel->r_addend) & 0x8000))
2916 rel->r_addend += 0x10000;
2917
2918 break;
41978308 2919
6edf0760
NC
2920 case R_M32R_26_PLTREL:
2921 /* Relocation is to the entry for this symbol in the
2922 procedure linkage table. */
41978308 2923
6edf0760
NC
2924 /* The native assembler will generate a 26_PLTREL reloc
2925 for a local symbol if you assemble a call from one
2926 section to another when using -K pic. */
2927 if (h == NULL)
2928 break;
2929
2930 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2931 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2932 // break;
2933 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2934 break;
2935
2936 if (h->plt.offset == (bfd_vma) -1)
2937 {
2938 /* We didn't make a PLT entry for this symbol. This
2939 happens when statically linking PIC code, or when
2940 using -Bsymbolic. */
2941 break;
2942 }
41978308 2943
6edf0760
NC
2944 relocation = (splt->output_section->vma
2945 + splt->output_offset
2946 + h->plt.offset);
2947 break;
41978308 2948
6edf0760
NC
2949 case R_M32R_HI16_SLO_RELA:
2950 {
2951 if ((relocation + rel->r_addend) & 0x8000)
2952 {
2953 rel->r_addend += 0x10000;
2954 }
2955 }
2956 /* Fall through. */
2957 case R_M32R_16_RELA:
2958 case R_M32R_24_RELA:
2959 case R_M32R_32_RELA:
2960 case R_M32R_18_PCREL_RELA:
2961 case R_M32R_26_PCREL_RELA:
2962 case R_M32R_HI16_ULO_RELA:
2963 case R_M32R_LO16_RELA:
2964 case R_M32R_SDA16_RELA:
2965 if (info->shared
2966 && r_symndx != 0
2967 && (input_section->flags & SEC_ALLOC) != 0
2968 && ((r_type != R_M32R_18_PCREL_RELA
2969 && r_type != R_M32R_26_PCREL_RELA)
2970 || (h != NULL
2971 && h->dynindx != -1
2972 && (! info->symbolic
2973 || (h->elf_link_hash_flags
2974 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2975 {
2976 Elf_Internal_Rela outrel;
2977 bfd_boolean skip, relocate;
2978 bfd_byte *loc;
41978308 2979
6edf0760
NC
2980 /* When generating a shared object, these relocations
2981 are copied into the output file to be resolved at run
2982 time. */
41978308 2983
6edf0760
NC
2984 if (sreloc == NULL)
2985 {
2986 const char *name;
41978308 2987
6edf0760
NC
2988 name = (bfd_elf_string_from_elf_section
2989 (input_bfd,
2990 elf_elfheader (input_bfd)->e_shstrndx,
2991 elf_section_data (input_section)->rel_hdr.sh_name));
2992 if (name == NULL)
2993 return FALSE;
41978308 2994
6edf0760
NC
2995 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2996 && strcmp (bfd_get_section_name (input_bfd,
2997 input_section),
2998 name + 5) == 0);
41978308 2999
6edf0760
NC
3000 sreloc = bfd_get_section_by_name (dynobj, name);
3001 BFD_ASSERT (sreloc != NULL);
3002 }
41978308 3003
6edf0760
NC
3004 skip = FALSE;
3005 relocate = FALSE;
41978308 3006
6edf0760
NC
3007 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3008 info,
3009 input_section,
3010 rel->r_offset);
3011 if (outrel.r_offset == (bfd_vma) -1)
3012 skip = TRUE;
3013 else if (outrel.r_offset == (bfd_vma) -2)
3014 skip = TRUE, relocate = TRUE;
3015 outrel.r_offset += (input_section->output_section->vma
3016 + input_section->output_offset);
41978308 3017
6edf0760
NC
3018 if (skip)
3019 memset (&outrel, 0, sizeof outrel);
3020 else if (r_type == R_M32R_18_PCREL_RELA
3021 || r_type == R_M32R_26_PCREL_RELA)
3022 {
3023 BFD_ASSERT (h != NULL && h->dynindx != -1);
3024 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3025 outrel.r_addend = rel->r_addend;
3026 }
3027 else
3028 {
3029 /* h->dynindx may be -1 if this symbol was marked to
3030 become local. */
3031 if (h == NULL
3032 || ((info->symbolic || h->dynindx == -1)
3033 && (h->elf_link_hash_flags
3034 & ELF_LINK_HASH_DEF_REGULAR) != 0))
3035 {
3036 relocate = TRUE;
3037 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3038 outrel.r_addend = relocation + rel->r_addend;
3039 }
3040 else
3041 {
3042 BFD_ASSERT (h->dynindx != -1);
3043 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3044 outrel.r_addend = relocation + rel->r_addend;
3045 }
3046 }
3047
3048 loc = sreloc->contents;
3049 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3050 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3051 ++sreloc->reloc_count;
3052
3053 /* If this reloc is against an external symbol, we do
3054 not want to fiddle with the addend. Otherwise, we
3055 need to include the symbol value so that it becomes
3056 an addend for the dynamic reloc. */
3057 if (! relocate)
3058 continue;
3059 }
3060 break;
41978308 3061
252b5132
RH
3062 case (int) R_M32R_10_PCREL :
3063 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3064 contents, offset,
3065 sec, relocation, addend);
6edf0760 3066 goto check_reloc;
252b5132
RH
3067
3068 case (int) R_M32R_HI16_SLO :
3069 case (int) R_M32R_HI16_ULO :
3070 {
3071 Elf_Internal_Rela *lorel;
3072
3073 /* We allow an arbitrary number of HI16 relocs before the
3074 LO16 reloc. This permits gcc to emit the HI and LO relocs
3075 itself. */
3076 for (lorel = rel + 1;
3077 (lorel < relend
3078 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3079 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3080 lorel++)
3081 continue;
3082 if (lorel < relend
3083 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3084 {
3085 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3086 contents, relocation + addend);
3087 r = bfd_reloc_ok;
3088 }
3089 else
3090 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3091 contents, offset,
3092 relocation, addend);
3093 }
6edf0760
NC
3094
3095 goto check_reloc;
252b5132
RH
3096
3097 case (int) R_M32R_SDA16 :
3098 {
3099 const char *name;
3100
3101 BFD_ASSERT (sec != NULL);
3102 name = bfd_get_section_name (abfd, sec);
3103
3104 if (strcmp (name, ".sdata") == 0
3105 || strcmp (name, ".sbss") == 0
3106 || strcmp (name, ".scommon") == 0)
3107 {
3108 bfd_vma sda_base;
3109 bfd *out_bfd = sec->output_section->owner;
3110
3111 r = m32r_elf_final_sda_base (out_bfd, info,
3112 &errmsg,
3113 &sda_base);
3114 if (r != bfd_reloc_ok)
3115 {
b34976b6 3116 ret = FALSE;
252b5132
RH
3117 goto check_reloc;
3118 }
3119
3120 /* At this point `relocation' contains the object's
3121 address. */
3122 relocation -= sda_base;
3123 /* Now it contains the offset from _SDA_BASE_. */
3124 }
3125 else
3126 {
8f615d07
AM
3127 (*_bfd_error_handler)
3128 (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
3129 bfd_archive_filename (input_bfd),
3130 sym_name,
3131 m32r_elf_howto_table[(int) r_type].name,
3132 bfd_get_section_name (abfd, sec));
252b5132 3133 /*bfd_set_error (bfd_error_bad_value); ??? why? */
b34976b6 3134 ret = FALSE;
252b5132
RH
3135 continue;
3136 }
3137 }
6edf0760
NC
3138 /* fall through */
3139
3140 default : /* OLD_M32R_RELOC */
252b5132 3141
252b5132
RH
3142 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3143 contents, offset,
3144 relocation, addend);
6edf0760 3145 goto check_reloc;
252b5132 3146 }
6edf0760
NC
3147
3148 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3149 contents, rel->r_offset,
3150 relocation, rel->r_addend);
3151
252b5132
RH
3152 }
3153
3154 check_reloc:
3155
3156 if (r != bfd_reloc_ok)
3157 {
3158 /* FIXME: This should be generic enough to go in a utility. */
3159 const char *name;
3160
3161 if (h != NULL)
3162 name = h->root.root.string;
3163 else
3164 {
3165 name = (bfd_elf_string_from_elf_section
3166 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3167 if (name == NULL || *name == '\0')
3168 name = bfd_section_name (input_bfd, sec);
3169 }
3170
3171 if (errmsg != NULL)
3172 goto common_error;
3173
3174 switch (r)
3175 {
3176 case bfd_reloc_overflow:
3177 if (! ((*info->callbacks->reloc_overflow)
3178 (info, name, howto->name, (bfd_vma) 0,
3179 input_bfd, input_section, offset)))
b34976b6 3180 return FALSE;
252b5132
RH
3181 break;
3182
3183 case bfd_reloc_undefined:
3184 if (! ((*info->callbacks->undefined_symbol)
3185 (info, name, input_bfd, input_section,
b34976b6
AM
3186 offset, TRUE)))
3187 return FALSE;
252b5132
RH
3188 break;
3189
3190 case bfd_reloc_outofrange:
3191 errmsg = _("internal error: out of range error");
3192 goto common_error;
3193
3194 case bfd_reloc_notsupported:
3195 errmsg = _("internal error: unsupported relocation error");
3196 goto common_error;
3197
3198 case bfd_reloc_dangerous:
3199 errmsg = _("internal error: dangerous error");
3200 goto common_error;
3201
3202 default:
3203 errmsg = _("internal error: unknown error");
3204 /* fall through */
3205
3206 common_error:
3207 if (!((*info->callbacks->warning)
3208 (info, errmsg, name, input_bfd, input_section,
3209 offset)))
b34976b6 3210 return FALSE;
252b5132
RH
3211 break;
3212 }
3213 }
3214 }
3215
3216 return ret;
3217}
6edf0760
NC
3218
3219/* Finish up dynamic symbol handling. We set the contents of various
3220 dynamic sections here. */
3221static bfd_boolean
3222m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3223 bfd *output_bfd;
3224 struct bfd_link_info *info;
3225 struct elf_link_hash_entry *h;
3226 Elf_Internal_Sym *sym;
3227{
3228 struct elf_m32r_link_hash_table *htab;
3229 bfd *dynobj;
3230 bfd_byte *loc;
41978308 3231
6edf0760
NC
3232#ifdef DEBUG_PIC
3233printf("m32r_elf_finish_dynamic_symbol()\n");
3234#endif
41978308 3235
6edf0760
NC
3236 htab = m32r_elf_hash_table (info);
3237 dynobj = htab->root.dynobj;
3238
3239 if (h->plt.offset != (bfd_vma) -1)
3240 {
3241 asection *splt;
3242 asection *sgot;
3243 asection *srela;
3244
3245 bfd_vma plt_index;
3246 bfd_vma got_offset;
3247 Elf_Internal_Rela rela;
41978308 3248
6edf0760
NC
3249 /* This symbol has an entry in the procedure linkage table. Set
3250 it up. */
41978308 3251
6edf0760 3252 BFD_ASSERT (h->dynindx != -1);
41978308 3253
6edf0760
NC
3254 splt = htab->splt;
3255 sgot = htab->sgotplt;
3256 srela = htab->srelplt;
3257 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3258
3259 /* Get the index in the procedure linkage table which
3260 corresponds to this symbol. This is the index of this symbol
3261 in all the symbols for which we are making plt entries. The
3262 first entry in the procedure linkage table is reserved. */
3263 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3264
3265 /* Get the offset into the .got table of the entry that
3266 corresponds to this function. Each .got entry is 4 bytes.
3267 The first three are reserved. */
3268 got_offset = (plt_index + 3) * 4;
3269
3270 /* Fill in the entry in the procedure linkage table. */
3271 if (! info->shared)
3272 {
3273 bfd_put_32 (output_bfd,
3274 (PLT_ENTRY_WORD0b
3275 + (((sgot->output_section->vma
3276 + sgot->output_offset
3277 + got_offset) >> 16) & 0xffff)),
3278 splt->contents + h->plt.offset);
3279 bfd_put_32 (output_bfd,
3280 (PLT_ENTRY_WORD1b
3281 + ((sgot->output_section->vma
3282 + sgot->output_offset
3283 + got_offset) & 0xffff)),
3284 splt->contents + h->plt.offset + 4);
3285 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3286 splt->contents + h->plt.offset + 8);
3287 bfd_put_32 (output_bfd,
3288 (PLT_ENTRY_WORD3
3289 + plt_index * sizeof (Elf32_External_Rela)),
3290 splt->contents + h->plt.offset + 12);
3291 bfd_put_32 (output_bfd,
3292 (PLT_ENTRY_WORD4
3293 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3294 splt->contents + h->plt.offset + 16);
3295 }
3296 else
3297 {
3298 bfd_put_32 (output_bfd,
3299 PLT_ENTRY_WORD0 + got_offset,
3300 splt->contents + h->plt.offset);
3301 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3302 splt->contents + h->plt.offset + 4);
3303 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3304 splt->contents + h->plt.offset + 8);
3305 bfd_put_32 (output_bfd,
3306 (PLT_ENTRY_WORD3
3307 + plt_index * sizeof (Elf32_External_Rela)),
3308 splt->contents + h->plt.offset + 12);
3309 bfd_put_32 (output_bfd,
3310 (PLT_ENTRY_WORD4
3311 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3312 splt->contents + h->plt.offset + 16);
3313 }
41978308 3314
6edf0760
NC
3315 /* Fill in the entry in the global offset table. */
3316 bfd_put_32 (output_bfd,
3317 (splt->output_section->vma
3318 + splt->output_offset
3319 + h->plt.offset
3320 + 12), /* same offset */
3321 sgot->contents + got_offset);
3322
3323 /* Fill in the entry in the .rela.plt section. */
3324 rela.r_offset = (sgot->output_section->vma
3325 + sgot->output_offset
3326 + got_offset);
3327 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3328 rela.r_addend = 0;
3329 loc = srela->contents;
3330 loc += plt_index * sizeof(Elf32_External_Rela);
3331 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
41978308 3332
6edf0760
NC
3333 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3334 {
3335 /* Mark the symbol as undefined, rather than as defined in
3336 the .plt section. Leave the value alone. */
3337 sym->st_shndx = SHN_UNDEF;
3338 }
3339 }
3340
3341 if (h->got.offset != (bfd_vma) -1)
3342 {
3343 asection *sgot;
3344 asection *srela;
3345 Elf_Internal_Rela rela;
41978308 3346
6edf0760
NC
3347 /* This symbol has an entry in the global offset table. Set it
3348 up. */
41978308 3349
6edf0760
NC
3350 sgot = htab->sgot;
3351 srela = htab->srelgot;
3352 BFD_ASSERT (sgot != NULL && srela != NULL);
41978308 3353
6edf0760
NC
3354 rela.r_offset = (sgot->output_section->vma
3355 + sgot->output_offset
3356 + (h->got.offset &~ 1));
41978308 3357
6edf0760
NC
3358 /* If this is a -Bsymbolic link, and the symbol is defined
3359 locally, we just want to emit a RELATIVE reloc. Likewise if
3360 the symbol was forced to be local because of a version file.
3361 The entry in the global offset table will already have been
3362 initialized in the relocate_section function. */
3363 if (info->shared
3364 && (info->symbolic
3365 || h->dynindx == -1
3366 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3367 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3368 {
3369 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3370 rela.r_addend = (h->root.u.def.value
3371 + h->root.u.def.section->output_section->vma
3372 + h->root.u.def.section->output_offset);
3373 }
3374 else
3375 {
3376 BFD_ASSERT((h->got.offset & 1) == 0);
3377 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3378 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3379 rela.r_addend = 0;
3380 }
41978308 3381
6edf0760
NC
3382 loc = srela->contents;
3383 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3384 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3385 ++srela->reloc_count;
3386 }
41978308 3387
6edf0760
NC
3388 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3389 {
3390 asection *s;
3391 Elf_Internal_Rela rela;
41978308 3392
6edf0760 3393 /* This symbols needs a copy reloc. Set it up. */
41978308 3394
6edf0760
NC
3395 BFD_ASSERT (h->dynindx != -1
3396 && (h->root.type == bfd_link_hash_defined
3397 || h->root.type == bfd_link_hash_defweak));
41978308 3398
6edf0760
NC
3399 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3400 ".rela.bss");
3401 BFD_ASSERT (s != NULL);
41978308 3402
6edf0760
NC
3403 rela.r_offset = (h->root.u.def.value
3404 + h->root.u.def.section->output_section->vma
3405 + h->root.u.def.section->output_offset);
3406 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3407 rela.r_addend = 0;
3408 loc = s->contents;
3409 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3410 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3411 ++s->reloc_count;
3412 }
41978308 3413
6edf0760
NC
3414 /* Mark some specially defined symbols as absolute. */
3415 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3416 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3417 sym->st_shndx = SHN_ABS;
41978308 3418
6edf0760
NC
3419 return TRUE;
3420}
3421
3422
3423/* Finish up the dynamic sections. */
41978308 3424
6edf0760
NC
3425static bfd_boolean
3426m32r_elf_finish_dynamic_sections (output_bfd, info)
3427 bfd *output_bfd;
3428 struct bfd_link_info *info;
3429{
3430 struct elf_m32r_link_hash_table *htab;
3431 bfd *dynobj;
3432 asection *sdyn;
3433 asection *sgot;
3434
3435#ifdef DEBUG_PIC
3436printf("m32r_elf_finish_dynamic_sections()\n");
3437#endif
41978308 3438
6edf0760
NC
3439 htab = m32r_elf_hash_table (info);
3440 dynobj = htab->root.dynobj;
3441
3442 sgot = htab->sgotplt;
3443 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
41978308 3444
6edf0760
NC
3445 if (htab->root.dynamic_sections_created)
3446 {
3447 asection *splt;
3448 Elf32_External_Dyn *dyncon, *dynconend;
41978308 3449
6edf0760 3450 BFD_ASSERT (sgot != NULL && sdyn != NULL);
41978308 3451
6edf0760
NC
3452 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3453 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3454
3455 for (; dyncon < dynconend; dyncon++)
3456 {
3457 Elf_Internal_Dyn dyn;
3458 const char *name;
3459 asection *s;
41978308 3460
6edf0760 3461 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
41978308 3462
6edf0760
NC
3463 switch (dyn.d_tag)
3464 {
3465 default:
3466 break;
3467
3468 case DT_PLTGOT:
3469 name = ".got";
41978308 3470 s = htab->sgot->output_section;
6edf0760
NC
3471 goto get_vma;
3472 case DT_JMPREL:
3473 name = ".rela.plt";
3474 s = htab->srelplt->output_section;
3475 get_vma:
3476 BFD_ASSERT (s != NULL);
3477 dyn.d_un.d_ptr = s->vma;
3478 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3479 break;
3480
3481 case DT_PLTRELSZ:
3482 s = htab->srelplt->output_section;
3483 BFD_ASSERT (s != NULL);
3484 if (s->_cooked_size != 0)
3485 dyn.d_un.d_val = s->_cooked_size;
3486 else
3487 dyn.d_un.d_val = s->_raw_size;
3488 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3489 break;
3490
3491 case DT_RELASZ:
3492 /* My reading of the SVR4 ABI indicates that the
3493 procedure linkage table relocs (DT_JMPREL) should be
3494 included in the overall relocs (DT_RELA). This is
3495 what Solaris does. However, UnixWare can not handle
3496 that case. Therefore, we override the DT_RELASZ entry
3497 here to make it not include the JMPREL relocs. Since
3498 the linker script arranges for .rela.plt to follow all
3499 other relocation sections, we don't have to worry
3500 about changing the DT_RELA entry. */
3501 if (htab->srelplt != NULL)
3502 {
3503 s = htab->srelplt->output_section;
3504 if (s->_cooked_size != 0)
3505 dyn.d_un.d_val -= s->_cooked_size;
3506 else
3507 dyn.d_un.d_val -= s->_raw_size;
3508 }
3509 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3510 break;
3511 }
3512 }
41978308 3513
6edf0760
NC
3514 /* Fill in the first entry in the procedure linkage table. */
3515 splt = htab->splt;
3516 if (splt && splt->_raw_size > 0)
3517 {
3518 if (info->shared)
3519 {
3520 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3521 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3522 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3523 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3524 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3525 }
3526 else
3527 {
3528 unsigned long addr;
3529 /* addr = .got + 4 */
3530 addr = sgot->output_section->vma + sgot->output_offset + 4;
3531 bfd_put_32 (output_bfd,
3532 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3533 splt->contents);
41978308 3534 bfd_put_32 (output_bfd,
6edf0760
NC
3535 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3536 splt->contents + 4);
3537 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3538 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3539 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3540 }
3541
3542 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3543 PLT_ENTRY_SIZE;
3544 }
3545 }
3546
3547 /* Fill in the first three entries in the global offset table. */
3548 if (sgot && sgot->_raw_size > 0)
3549 {
3550 if (sdyn == NULL)
3551 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3552 else
3553 bfd_put_32 (output_bfd,
3554 sdyn->output_section->vma + sdyn->output_offset,
3555 sgot->contents);
3556 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3557 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3558
3559 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3560 }
41978308 3561
6edf0760
NC
3562 return TRUE;
3563}
3564
252b5132
RH
3565\f
3566#if 0 /* relaxing not supported yet */
3567
3568/* This function handles relaxing for the m32r.
3569 Relaxing on the m32r is tricky because of instruction alignment
3570 requirements (4 byte instructions must be aligned on 4 byte boundaries).
3571
3572 The following relaxing opportunities are handled:
3573
3574 seth/add3/jl -> bl24 or bl8
3575 seth/add3 -> ld24
3576
3577 It would be nice to handle bl24 -> bl8 but given:
3578
3579 - 4 byte insns must be on 4 byte boundaries
3580 - branch instructions only branch to insns on 4 byte boundaries
3581
3582 this isn't much of a win because the insn in the 2 "deleted" bytes
3583 must become a nop. With some complexity some real relaxation could be
3584 done but the frequency just wouldn't make it worth it; it's better to
3585 try to do all the code compaction one can elsewhere.
3586 When the chip supports parallel 16 bit insns, things may change.
3587*/
3588
b34976b6 3589static bfd_boolean
252b5132
RH
3590m32r_elf_relax_section (abfd, sec, link_info, again)
3591 bfd *abfd;
3592 asection *sec;
3593 struct bfd_link_info *link_info;
b34976b6 3594 bfd_boolean *again;
252b5132
RH
3595{
3596 Elf_Internal_Shdr *symtab_hdr;
3597 /* The Rela structures are used here because that's what
45d6a902 3598 _bfd_elf_link_read_relocs uses [for convenience - it sets the addend
252b5132 3599 field to 0]. */
6cdc0ccc 3600 Elf_Internal_Rela *internal_relocs = NULL;
252b5132
RH
3601 Elf_Internal_Rela *irel, *irelend;
3602 bfd_byte *contents = NULL;
6cdc0ccc 3603 Elf_Internal_Sym *isymbuf = NULL;
252b5132
RH
3604
3605 /* Assume nothing changes. */
b34976b6 3606 *again = FALSE;
252b5132 3607
1049f94e 3608 /* We don't have to do anything for a relocatable link, if
252b5132
RH
3609 this section does not have relocs, or if this is not a
3610 code section. */
1049f94e 3611 if (link_info->relocatable
252b5132
RH
3612 || (sec->flags & SEC_RELOC) == 0
3613 || sec->reloc_count == 0
3614 || (sec->flags & SEC_CODE) == 0
3615 || 0 /* FIXME: check SHF_M32R_CAN_RELAX */)
b34976b6 3616 return TRUE;
252b5132
RH
3617
3618 /* If this is the first time we have been called for this section,
3619 initialize the cooked size. */
3620 if (sec->_cooked_size == 0)
3621 sec->_cooked_size = sec->_raw_size;
3622
3623 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3624
3625 /* Get a copy of the native relocations. */
45d6a902 3626 internal_relocs = (_bfd_elf_link_read_relocs
252b5132
RH
3627 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
3628 link_info->keep_memory));
3629 if (internal_relocs == NULL)
3630 goto error_return;
252b5132
RH
3631
3632 /* Walk through them looking for relaxing opportunities. */
3633 irelend = internal_relocs + sec->reloc_count;
3634 for (irel = internal_relocs; irel < irelend; irel++)
3635 {
3636 bfd_vma symval;
3637
3638 /* If this isn't something that can be relaxed, then ignore
3639 this reloc. */
3640 if (ELF32_R_TYPE (irel->r_info) != (int) R_M32R_HI16_SLO)
3641 continue;
3642
3643 /* Get the section contents if we haven't done so already. */
3644 if (contents == NULL)
3645 {
3646 /* Get cached copy if it exists. */
3647 if (elf_section_data (sec)->this_hdr.contents != NULL)
3648 contents = elf_section_data (sec)->this_hdr.contents;
3649 else
3650 {
3651 /* Go get them off disk. */
3652 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
3653 if (contents == NULL)
3654 goto error_return;
252b5132
RH
3655
3656 if (! bfd_get_section_contents (abfd, sec, contents,
3657 (file_ptr) 0, sec->_raw_size))
3658 goto error_return;
3659 }
3660 }
3661
6cdc0ccc
AM
3662 /* Read this BFD's local symbols if we haven't done so already. */
3663 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
252b5132 3664 {
6cdc0ccc
AM
3665 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3666 if (isymbuf == NULL)
3667 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3668 symtab_hdr->sh_info, 0,
3669 NULL, NULL, NULL);
3670 if (isymbuf == NULL)
3671 goto error_return;
252b5132
RH
3672 }
3673
3674 /* Get the value of the symbol referred to by the reloc. */
3675 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3676 {
252b5132 3677 /* A local symbol. */
6cdc0ccc
AM
3678 Elf_Internal_Sym *isym;
3679 asection *sym_sec;
252b5132 3680
6cdc0ccc
AM
3681 isym = isymbuf + ELF32_R_SYM (irel->r_info),
3682 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3683 symval = (isym->st_value
252b5132
RH
3684 + sym_sec->output_section->vma
3685 + sym_sec->output_offset);
3686 }
3687 else
3688 {
3689 unsigned long indx;
3690 struct elf_link_hash_entry *h;
3691
3692 /* An external symbol. */
3693 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3694 h = elf_sym_hashes (abfd)[indx];
3695 BFD_ASSERT (h != NULL);
3696 if (h->root.type != bfd_link_hash_defined
3697 && h->root.type != bfd_link_hash_defweak)
3698 {
3699 /* This appears to be a reference to an undefined
3700 symbol. Just ignore it--it will be caught by the
3701 regular reloc processing. */
3702 continue;
3703 }
3704
3705 symval = (h->root.u.def.value
3706 + h->root.u.def.section->output_section->vma
3707 + h->root.u.def.section->output_offset);
3708 }
3709
3710 /* For simplicity of coding, we are going to modify the section
3711 contents, the section relocs, and the BFD symbol table. We
3712 must tell the rest of the code not to free up this
3713 information. It would be possible to instead create a table
3714 of changes which have to be made, as is done in coff-mips.c;
3715 that would be more work, but would require less memory when
3716 the linker is run. */
3717
3718 /* Try to change a seth/add3/jl subroutine call to bl24 or bl8.
3719 This sequence is generated by the compiler when compiling in
3720 32 bit mode. Also look for seth/add3 -> ld24. */
3721
3722 if (ELF32_R_TYPE (irel->r_info) == (int) R_M32R_HI16_SLO)
3723 {
3724 Elf_Internal_Rela *nrel;
3725 bfd_vma pc = (sec->output_section->vma + sec->output_offset
3726 + irel->r_offset);
3727 bfd_signed_vma pcrel_value = symval - pc;
3728 unsigned int code,reg;
3729 int addend,nop_p,bl8_p,to_delete;
3730
3731 /* The tests are ordered so that we get out as quickly as possible
3732 if this isn't something we can relax, taking into account that
3733 we are looking for two separate possibilities (jl/ld24). */
3734
3735 /* Do nothing if no room in the section for this to be what we're
3736 looking for. */
3737 if (irel->r_offset > sec->_cooked_size - 8)
3738 continue;
3739
3740 /* Make sure the next relocation applies to the next
3741 instruction and that it's the add3's reloc. */
3742 nrel = irel + 1;
3743 if (nrel == irelend
3744 || irel->r_offset + 4 != nrel->r_offset
3745 || ELF32_R_TYPE (nrel->r_info) != (int) R_M32R_LO16)
3746 continue;
3747
3748 /* See if the instructions are seth/add3. */
3749 /* FIXME: This is where macros from cgen can come in. */
3750 code = bfd_get_16 (abfd, contents + irel->r_offset + 0);
3751 if ((code & 0xf0ff) != 0xd0c0)
3752 continue; /* not seth rN,foo */
3753 reg = (code & 0x0f00) >> 8;
3754 code = bfd_get_16 (abfd, contents + irel->r_offset + 4);
3755 if (code != (0x80a0 | reg | (reg << 8)))
3756 continue; /* not add3 rN,rN,foo */
3757
3758 /* At this point we've confirmed we have seth/add3. Now check
3759 whether the next insn is a jl, in which case try to change this
3760 to bl24 or bl8. */
3761
3762 /* Ensure the branch target is in range.
3763 The bl24 instruction has a 24 bit operand which is the target
3764 address right shifted by 2, giving a signed range of 26 bits.
3765 Note that 4 bytes are added to the high value because the target
3766 will be at least 4 bytes closer if we can relax. It'll actually
3767 be 4 or 8 bytes closer, but we don't know which just yet and
3768 the difference isn't significant enough to worry about. */
acf8aed4 3769#if !USE_REL /* put in for learning purposes */
252b5132
RH
3770 pcrel_value += irel->r_addend;
3771#else
3772 addend = bfd_get_signed_16 (abfd, contents + irel->r_offset + 2);
3773 pcrel_value += addend;
3774#endif
3775
3776 if (pcrel_value >= -(1 << 25) && pcrel_value < (1 << 25) + 4
3777 /* Do nothing if no room in the section for this to be what we're
3778 looking for. */
3779 && (irel->r_offset <= sec->_cooked_size - 12)
3780 /* Ensure the next insn is "jl rN". */
3781 && ((code = bfd_get_16 (abfd, contents + irel->r_offset + 8)),
3782 code != (0x1ec0 | reg)))
3783 {
3784 /* We can relax to bl24/bl8. */
3785
3786 /* See if there's a nop following the jl.
3787 Also see if we can use a bl8 insn. */
3788 code = bfd_get_16 (abfd, contents + irel->r_offset + 10);
3789 nop_p = (code & 0x7fff) == NOP_INSN;
3790 bl8_p = pcrel_value >= -0x200 && pcrel_value < 0x200;
3791
3792 if (bl8_p)
3793 {
3794 /* Change "seth rN,foo" to "bl8 foo || nop".
3795 We OR in CODE just in case it's not a nop (technically,
3796 CODE currently must be a nop, but for cleanness we
3797 allow it to be anything). */
acf8aed4 3798#if !USE_REL /* put in for learning purposes */
252b5132
RH
3799 code = 0x7e000000 | MAKE_PARALLEL (code);
3800#else
3801 code = (0x7e000000 + (((addend >> 2) & 0xff) << 16)) | MAKE_PARALLEL (code);
3802#endif
3803 to_delete = 8;
3804 }
3805 else
3806 {
3807 /* Change the seth rN,foo to a bl24 foo. */
acf8aed4 3808#if !USE_REL /* put in for learning purposes */
252b5132
RH
3809 code = 0xfe000000;
3810#else
3811 code = 0xfe000000 + ((addend >> 2) & 0xffffff);
3812#endif
3813 to_delete = nop_p ? 8 : 4;
3814 }
3815
3816 bfd_put_32 (abfd, code, contents + irel->r_offset);
3817
3818 /* Set the new reloc type. */
3819 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3820 bl8_p ? R_M32R_10_PCREL : R_M32R_26_PCREL);
3821
3822 /* Delete the add3 reloc by making it a null reloc. */
3823 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3824 R_M32R_NONE);
3825 }
3826 else if (addend >= 0
3827 && symval + addend <= 0xffffff)
3828 {
3829 /* We can relax to ld24. */
3830
3831 code = 0xe0000000 | (reg << 24) | (addend & 0xffffff);
3832 bfd_put_32 (abfd, code, contents + irel->r_offset);
3833 to_delete = 4;
3834 /* Tell the following code a nop filler isn't needed. */
3835 nop_p = 1;
3836 }
3837 else
3838 {
3839 /* Can't do anything here. */
3840 continue;
3841 }
3842
3843 /* Note that we've changed the relocs, section contents, etc. */
3844 elf_section_data (sec)->relocs = internal_relocs;
252b5132 3845 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 3846 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
3847
3848 /* Delete TO_DELETE bytes of data. */
3849 if (!m32r_elf_relax_delete_bytes (abfd, sec,
3850 irel->r_offset + 4, to_delete))
3851 goto error_return;
3852
3853 /* Now that the following bytes have been moved into place, see if
3854 we need to replace the jl with a nop. This happens when we had
3855 to use a bl24 insn and the insn following the jl isn't a nop.
3856 Technically, this situation can't happen (since the insn can
3857 never be executed) but to be clean we do this. When the chip
3858 supports parallel 16 bit insns things may change.
3859 We don't need to do this in the case of relaxing to ld24,
3860 and the above code sets nop_p so this isn't done. */
3861 if (! nop_p && to_delete == 4)
3862 bfd_put_16 (abfd, NOP_INSN, contents + irel->r_offset + 4);
3863
3864 /* That will change things, so we should relax again.
3865 Note that this is not required, and it may be slow. */
b34976b6 3866 *again = TRUE;
252b5132
RH
3867
3868 continue;
3869 }
3870
3871 /* loop to try the next reloc */
3872 }
3873
6cdc0ccc
AM
3874 if (isymbuf != NULL
3875 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132
RH
3876 {
3877 if (! link_info->keep_memory)
6cdc0ccc 3878 free (isymbuf);
252b5132
RH
3879 else
3880 {
6cdc0ccc
AM
3881 /* Cache the symbols for elf_link_input_bfd. */
3882 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132 3883 }
252b5132
RH
3884 }
3885
6cdc0ccc
AM
3886 if (contents != NULL
3887 && elf_section_data (sec)->this_hdr.contents != contents)
252b5132
RH
3888 {
3889 if (! link_info->keep_memory)
6cdc0ccc 3890 free (contents);
252b5132
RH
3891 else
3892 {
6cdc0ccc
AM
3893 /* Cache the section contents for elf_link_input_bfd. */
3894 elf_section_data (sec)->this_hdr.contents = contents;
252b5132 3895 }
252b5132
RH
3896 }
3897
6cdc0ccc
AM
3898 if (internal_relocs != NULL
3899 && elf_section_data (sec)->relocs != internal_relocs)
3900 free (internal_relocs);
3901
b34976b6 3902 return TRUE;
252b5132
RH
3903
3904 error_return:
6cdc0ccc
AM
3905 if (isymbuf != NULL
3906 && symtab_hdr->contents != (unsigned char *) isymbuf)
3907 free (isymbuf);
3908 if (contents != NULL
3909 && elf_section_data (sec)->this_hdr.contents != contents)
3910 free (contents);
3911 if (internal_relocs != NULL
3912 && elf_section_data (sec)->relocs != internal_relocs)
3913 free (internal_relocs);
3914
b34976b6 3915 return FALSE;
252b5132
RH
3916}
3917
3918/* Delete some bytes from a section while relaxing. */
3919
b34976b6 3920static bfd_boolean
252b5132
RH
3921m32r_elf_relax_delete_bytes (abfd, sec, addr, count)
3922 bfd *abfd;
3923 asection *sec;
3924 bfd_vma addr;
3925 int count;
3926{
3927 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 3928 int shndx;
252b5132
RH
3929 bfd_byte *contents;
3930 Elf_Internal_Rela *irel, *irelend;
3931 Elf_Internal_Rela *irelalign;
3932 bfd_vma toaddr;
6cdc0ccc
AM
3933 Elf_Internal_Sym *isym, *isymend;
3934 struct elf_link_hash_entry **sym_hashes;
3935 struct elf_link_hash_entry **end_hashes;
3936 unsigned int symcount;
252b5132
RH
3937
3938 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3939
3940 contents = elf_section_data (sec)->this_hdr.contents;
3941
3942 /* The deletion must stop at the next ALIGN reloc for an aligment
3943 power larger than the number of bytes we are deleting. */
3944
3945 irelalign = NULL;
3946 toaddr = sec->_cooked_size;
3947
3948 irel = elf_section_data (sec)->relocs;
3949 irelend = irel + sec->reloc_count;
3950
3951 /* Actually delete the bytes. */
3952 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
3953 sec->_cooked_size -= count;
3954
3955 /* Adjust all the relocs. */
3956 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3957 {
3958 /* Get the new reloc address. */
3959 if ((irel->r_offset > addr
3960 && irel->r_offset < toaddr))
3961 irel->r_offset -= count;
3962 }
3963
3964 /* Adjust the local symbols defined in this section. */
6cdc0ccc
AM
3965 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3966 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3967 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
252b5132 3968 {
6cdc0ccc
AM
3969 if (isym->st_shndx == shndx
3970 && isym->st_value > addr
3971 && isym->st_value < toaddr)
3972 isym->st_value -= count;
252b5132
RH
3973 }
3974
3975 /* Now adjust the global symbols defined in this section. */
6cdc0ccc
AM
3976 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3977 - symtab_hdr->sh_info);
3978 sym_hashes = elf_sym_hashes (abfd);
3979 end_hashes = sym_hashes + symcount;
3980 for (; sym_hashes < end_hashes; sym_hashes++)
252b5132 3981 {
6cdc0ccc
AM
3982 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3983
3984 if ((sym_hash->root.type == bfd_link_hash_defined
3985 || sym_hash->root.type == bfd_link_hash_defweak)
3986 && sym_hash->root.u.def.section == sec
3987 && sym_hash->root.u.def.value > addr
3988 && sym_hash->root.u.def.value < toaddr)
252b5132 3989 {
6cdc0ccc 3990 sym_hash->root.u.def.value -= count;
252b5132
RH
3991 }
3992 }
3993
b34976b6 3994 return TRUE;
252b5132
RH
3995}
3996
3997/* This is a version of bfd_generic_get_relocated_section_contents
3998 which uses m32r_elf_relocate_section. */
3999
4000static bfd_byte *
4001m32r_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1049f94e 4002 data, relocatable, symbols)
252b5132
RH
4003 bfd *output_bfd;
4004 struct bfd_link_info *link_info;
4005 struct bfd_link_order *link_order;
4006 bfd_byte *data;
1049f94e 4007 bfd_boolean relocatable;
252b5132
RH
4008 asymbol **symbols;
4009{
4010 Elf_Internal_Shdr *symtab_hdr;
4011 asection *input_section = link_order->u.indirect.section;
4012 bfd *input_bfd = input_section->owner;
4013 asection **sections = NULL;
4014 Elf_Internal_Rela *internal_relocs = NULL;
6cdc0ccc 4015 Elf_Internal_Sym *isymbuf = NULL;
dc810e39 4016 bfd_size_type amt;
252b5132
RH
4017
4018 /* We only need to handle the case of relaxing, or of having a
4019 particular set of section contents, specially. */
1049f94e 4020 if (relocatable
252b5132
RH
4021 || elf_section_data (input_section)->this_hdr.contents == NULL)
4022 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4023 link_order, data,
1049f94e 4024 relocatable,
252b5132
RH
4025 symbols);
4026
4027 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4028
4029 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4030 input_section->_raw_size);
4031
4032 if ((input_section->flags & SEC_RELOC) != 0
4033 && input_section->reloc_count > 0)
4034 {
4035 Elf_Internal_Sym *isymp;
4036 asection **secpp;
4037 Elf32_External_Sym *esym, *esymend;
4038
45d6a902 4039 internal_relocs = (_bfd_elf_link_read_relocs
252b5132 4040 (input_bfd, input_section, (PTR) NULL,
b34976b6 4041 (Elf_Internal_Rela *) NULL, FALSE));
252b5132
RH
4042 if (internal_relocs == NULL)
4043 goto error_return;
4044
6cdc0ccc
AM
4045 if (symtab_hdr->sh_info != 0)
4046 {
4047 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4048 if (isymbuf == NULL)
4049 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4050 symtab_hdr->sh_info, 0,
4051 NULL, NULL, NULL);
4052 if (isymbuf == NULL)
4053 goto error_return;
4054 }
252b5132 4055
dc810e39
AM
4056 amt = symtab_hdr->sh_info;
4057 amt *= sizeof (asection *);
4058 sections = (asection **) bfd_malloc (amt);
252b5132
RH
4059 if (sections == NULL && symtab_hdr->sh_info > 0)
4060 goto error_return;
4061
6cdc0ccc
AM
4062 isymend = isymbuf + symtab_hdr->sh_info;
4063 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
252b5132
RH
4064 {
4065 asection *isec;
4066
6cdc0ccc 4067 if (isym->st_shndx == SHN_UNDEF)
252b5132 4068 isec = bfd_und_section_ptr;
6cdc0ccc 4069 else if (isym->st_shndx == SHN_ABS)
252b5132 4070 isec = bfd_abs_section_ptr;
6cdc0ccc 4071 else if (isym->st_shndx == SHN_COMMON)
252b5132 4072 isec = bfd_com_section_ptr;
6cdc0ccc 4073 else if (isym->st_shndx == SHN_M32R_SCOMMON)
252b5132
RH
4074 isec = &m32r_elf_scom_section;
4075 else
6cdc0ccc 4076 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
252b5132
RH
4077
4078 *secpp = isec;
4079 }
4080
4081 if (! m32r_elf_relocate_section (output_bfd, link_info, input_bfd,
4082 input_section, data, internal_relocs,
6cdc0ccc 4083 isymbuf, sections))
252b5132
RH
4084 goto error_return;
4085
4086 if (sections != NULL)
4087 free (sections);
6cdc0ccc
AM
4088 if (isymbuf != NULL
4089 && symtab_hdr->contents != (unsigned char *) isymbuf)
4090 free (isymbuf);
4091 if (elf_section_data (input_section)->relocs != internal_relocs)
252b5132 4092 free (internal_relocs);
252b5132
RH
4093 }
4094
4095 return data;
4096
4097 error_return:
252b5132
RH
4098 if (sections != NULL)
4099 free (sections);
6cdc0ccc
AM
4100 if (isymbuf != NULL
4101 && symtab_hdr->contents != (unsigned char *) isymbuf)
4102 free (isymbuf);
4103 if (internal_relocs != NULL
4104 && elf_section_data (input_section)->relocs != internal_relocs)
4105 free (internal_relocs);
252b5132
RH
4106 return NULL;
4107}
4108
4109#endif /* #if 0 */
4110\f
4111/* Set the right machine number. */
b34976b6 4112static bfd_boolean
252b5132
RH
4113m32r_elf_object_p (abfd)
4114 bfd *abfd;
4115{
4116 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4117 {
4118 default:
4119 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
e916b64e 4120 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
88845958 4121 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
252b5132 4122 }
b34976b6 4123 return TRUE;
252b5132
RH
4124}
4125
4126/* Store the machine number in the flags field. */
4127static void
4128m32r_elf_final_write_processing (abfd, linker)
b34976b6
AM
4129 bfd *abfd;
4130 bfd_boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
4131{
4132 unsigned long val;
4133
4134 switch (bfd_get_mach (abfd))
4135 {
4136 default:
4137 case bfd_mach_m32r: val = E_M32R_ARCH; break;
e916b64e 4138 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
88845958 4139 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
252b5132
RH
4140 }
4141
4142 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
4143 elf_elfheader (abfd)->e_flags |= val;
4144}
4145
c3668558 4146/* Function to keep M32R specific file flags. */
b34976b6 4147static bfd_boolean
252b5132 4148m32r_elf_set_private_flags (abfd, flags)
b34976b6 4149 bfd *abfd;
252b5132
RH
4150 flagword flags;
4151{
4152 BFD_ASSERT (!elf_flags_init (abfd)
4153 || elf_elfheader (abfd)->e_flags == flags);
4154
4155 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
4156 elf_flags_init (abfd) = TRUE;
4157 return TRUE;
252b5132
RH
4158}
4159
252b5132
RH
4160/* Merge backend specific data from an object file to the output
4161 object file when linking. */
b34976b6 4162static bfd_boolean
252b5132 4163m32r_elf_merge_private_bfd_data (ibfd, obfd)
b34976b6
AM
4164 bfd *ibfd;
4165 bfd *obfd;
252b5132
RH
4166{
4167 flagword out_flags;
4168 flagword in_flags;
4169
4170 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4171 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 4172 return TRUE;
252b5132
RH
4173
4174 in_flags = elf_elfheader (ibfd)->e_flags;
4175 out_flags = elf_elfheader (obfd)->e_flags;
4176
4177 if (! elf_flags_init (obfd))
4178 {
4179 /* If the input is the default architecture then do not
4180 bother setting the flags for the output architecture,
4181 instead allow future merges to do this. If no future
4182 merges ever set these flags then they will retain their
4183 unitialised values, which surprise surprise, correspond
4184 to the default values. */
4185 if (bfd_get_arch_info (ibfd)->the_default)
b34976b6 4186 return TRUE;
c3668558 4187
b34976b6 4188 elf_flags_init (obfd) = TRUE;
252b5132
RH
4189 elf_elfheader (obfd)->e_flags = in_flags;
4190
4191 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4192 && bfd_get_arch_info (obfd)->the_default)
4193 {
4194 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
4195 }
4196
b34976b6 4197 return TRUE;
252b5132
RH
4198 }
4199
4200 /* Check flag compatibility. */
4201 if (in_flags == out_flags)
b34976b6 4202 return TRUE;
252b5132
RH
4203
4204 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
4205 {
88845958
NC
4206 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
4207 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4208 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
252b5132 4209 {
8f615d07
AM
4210 (*_bfd_error_handler)
4211 (_("%s: Instruction set mismatch with previous modules"),
4212 bfd_archive_filename (ibfd));
252b5132
RH
4213
4214 bfd_set_error (bfd_error_bad_value);
b34976b6 4215 return FALSE;
252b5132
RH
4216 }
4217 }
4218
b34976b6 4219 return TRUE;
252b5132
RH
4220}
4221
4222/* Display the flags field */
b34976b6 4223static bfd_boolean
252b5132 4224m32r_elf_print_private_bfd_data (abfd, ptr)
b34976b6
AM
4225 bfd *abfd;
4226 PTR ptr;
252b5132
RH
4227{
4228 FILE * file = (FILE *) ptr;
c3668558 4229
252b5132 4230 BFD_ASSERT (abfd != NULL && ptr != NULL)
c3668558 4231
252b5132 4232 _bfd_elf_print_private_bfd_data (abfd, ptr);
c3668558 4233
252b5132 4234 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
c3668558 4235
252b5132
RH
4236 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
4237 {
4238 default:
4239 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
e916b64e 4240 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
88845958 4241 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
252b5132 4242 }
c3668558 4243
252b5132 4244 fputc ('\n', file);
c3668558 4245
b34976b6 4246 return TRUE;
252b5132
RH
4247}
4248
4249asection *
1e2f5b6e
AM
4250m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
4251 asection *sec;
4252 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4253 Elf_Internal_Rela *rel;
4254 struct elf_link_hash_entry *h;
4255 Elf_Internal_Sym *sym;
252b5132
RH
4256{
4257 if (h != NULL)
4258 {
4259 switch (ELF32_R_TYPE (rel->r_info))
4260 {
4261 case R_M32R_GNU_VTINHERIT:
4262 case R_M32R_GNU_VTENTRY:
6edf0760
NC
4263 case R_M32R_RELA_GNU_VTINHERIT:
4264 case R_M32R_RELA_GNU_VTENTRY:
252b5132 4265 break;
c3668558 4266
252b5132
RH
4267 default:
4268 switch (h->root.type)
4269 {
4270 case bfd_link_hash_defined:
4271 case bfd_link_hash_defweak:
4272 return h->root.u.def.section;
c3668558 4273
252b5132
RH
4274 case bfd_link_hash_common:
4275 return h->root.u.c.p->section;
e049a0de
ILT
4276
4277 default:
4278 break;
252b5132
RH
4279 }
4280 }
4281 }
4282 else
1e2f5b6e
AM
4283 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4284
252b5132
RH
4285 return NULL;
4286}
4287
b34976b6 4288static bfd_boolean
252b5132 4289m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
5f771d47
ILT
4290 bfd *abfd ATTRIBUTE_UNUSED;
4291 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4292 asection *sec ATTRIBUTE_UNUSED;
4293 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
252b5132 4294{
6edf0760
NC
4295 /* Update the got entry reference counts for the section being removed. */
4296 Elf_Internal_Shdr *symtab_hdr;
4297 struct elf_link_hash_entry **sym_hashes;
4298 bfd_signed_vma *local_got_refcounts;
4299 const Elf_Internal_Rela *rel, *relend;
4300 unsigned long r_symndx;
4301 struct elf_link_hash_entry *h;
4302
4303 elf_section_data (sec)->local_dynrel = NULL;
4304
4305 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4306 sym_hashes = elf_sym_hashes (abfd);
4307 local_got_refcounts = elf_local_got_refcounts (abfd);
4308
4309 relend = relocs + sec->reloc_count;
4310 for (rel = relocs; rel < relend; rel++)
4311 switch (ELF32_R_TYPE (rel->r_info))
4312 {
4313 case R_M32R_GOT16_HI_ULO:
4314 case R_M32R_GOT16_HI_SLO:
4315 case R_M32R_GOT16_LO:
4316 case R_M32R_GOT24:
4317 case R_M32R_GOTPC_HI_ULO:
4318 case R_M32R_GOTPC_HI_SLO:
4319 case R_M32R_GOTPC_LO:
4320 case R_M32R_GOTPC24:
4321 r_symndx = ELF32_R_SYM (rel->r_info);
4322 if (r_symndx >= symtab_hdr->sh_info)
4323 {
4324 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4325 if (h->got.refcount > 0)
4326 h->got.refcount--;
4327 }
4328 else
4329 {
4330 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
4331 local_got_refcounts[r_symndx]--;
4332 }
4333 break;
4334
4335 case R_M32R_16_RELA:
4336 case R_M32R_24_RELA:
4337 case R_M32R_32_RELA:
4338 case R_M32R_HI16_ULO_RELA:
4339 case R_M32R_HI16_SLO_RELA:
4340 case R_M32R_LO16_RELA:
4341 case R_M32R_SDA16_RELA:
4342 case R_M32R_18_PCREL_RELA:
4343 case R_M32R_26_PCREL_RELA:
4344 r_symndx = ELF32_R_SYM (rel->r_info);
4345 if (r_symndx >= symtab_hdr->sh_info)
4346 {
4347 struct elf_m32r_link_hash_entry *eh;
4348 struct elf_m32r_dyn_relocs **pp;
4349 struct elf_m32r_dyn_relocs *p;
4350
4351 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4352
4353 if (!info->shared && h->plt.refcount > 0)
4354 h->plt.refcount -= 1;
4355
4356 eh = (struct elf_m32r_link_hash_entry *) h;
4357
4358 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4359 if (p->sec == sec)
4360 {
4361 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4362 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
4363 p->pc_count -= 1;
4364 p->count -= 1;
4365 if (p->count == 0)
4366 *pp = p->next;
4367 break;
4368 }
4369 }
4370 break;
4371
4372 case R_M32R_26_PLTREL:
4373 r_symndx = ELF32_R_SYM (rel->r_info);
4374 if (r_symndx >= symtab_hdr->sh_info)
4375 {
4376 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4377 if (h->plt.refcount > 0)
4378 h->plt.refcount--;
4379 }
4380 break;
4381
4382 default:
4383 break;
4384 }
4385
b34976b6 4386 return TRUE;
252b5132
RH
4387}
4388
252b5132
RH
4389/* Look through the relocs for a section during the first phase.
4390 Since we don't do .gots or .plts, we just need to consider the
4391 virtual table relocs for gc. */
c3668558 4392
b34976b6 4393static bfd_boolean
252b5132
RH
4394m32r_elf_check_relocs (abfd, info, sec, relocs)
4395 bfd *abfd;
4396 struct bfd_link_info *info;
4397 asection *sec;
4398 const Elf_Internal_Rela *relocs;
4399{
4400 Elf_Internal_Shdr *symtab_hdr;
4401 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4402 const Elf_Internal_Rela *rel;
4403 const Elf_Internal_Rela *rel_end;
6edf0760
NC
4404 struct elf_m32r_link_hash_table *htab;
4405 bfd *dynobj;
4406 bfd_vma *local_got_offsets;
4407 asection *sgot, *srelgot, *sreloc;
c3668558 4408
1049f94e 4409 if (info->relocatable)
b34976b6 4410 return TRUE;
c3668558 4411
6edf0760
NC
4412 sgot = srelgot = sreloc = NULL;
4413
252b5132
RH
4414 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4415 sym_hashes = elf_sym_hashes (abfd);
c3668558 4416 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
252b5132
RH
4417 if (!elf_bad_symtab (abfd))
4418 sym_hashes_end -= symtab_hdr->sh_info;
c3668558 4419
6edf0760
NC
4420 htab = m32r_elf_hash_table (info);
4421 dynobj = htab->root.dynobj;
4422 local_got_offsets = elf_local_got_offsets (abfd);
4423
252b5132
RH
4424 rel_end = relocs + sec->reloc_count;
4425 for (rel = relocs; rel < rel_end; rel++)
4426 {
6edf0760 4427 int r_type;
252b5132
RH
4428 struct elf_link_hash_entry *h;
4429 unsigned long r_symndx;
c3668558 4430
252b5132 4431 r_symndx = ELF32_R_SYM (rel->r_info);
6edf0760 4432 r_type = ELF32_R_TYPE (rel->r_info);
252b5132
RH
4433 if (r_symndx < symtab_hdr->sh_info)
4434 h = NULL;
4435 else
4436 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
c3668558 4437
6edf0760
NC
4438 /* Some relocs require a global offset table. */
4439 if (htab->sgot == NULL)
4440 {
4441 switch (r_type)
4442 {
4443 case R_M32R_GOT16_HI_ULO:
4444 case R_M32R_GOT16_HI_SLO:
4445 case R_M32R_GOT16_LO:
4446 case R_M32R_GOTPC24:
4447 case R_M32R_GOTPC_HI_ULO:
4448 case R_M32R_GOTPC_HI_SLO:
4449 case R_M32R_GOTPC_LO:
4450 case R_M32R_GOT24:
4451 if (dynobj == NULL)
4452 htab->root.dynobj = dynobj = abfd;
4453 if (! create_got_section (dynobj, info))
4454 return FALSE;
4455 break;
4456
4457 default:
4458 break;
4459 }
4460 }
4461
4462 switch (r_type)
252b5132 4463 {
6edf0760
NC
4464 case R_M32R_GOT16_HI_ULO:
4465 case R_M32R_GOT16_HI_SLO:
41978308 4466 case R_M32R_GOT16_LO:
6edf0760
NC
4467 case R_M32R_GOT24:
4468
4469 if (h != NULL)
4470 h->got.refcount += 1;
4471 else
4472 {
4473 bfd_signed_vma *local_got_refcounts;
4474
4475 /* This is a global offset table entry for a local
4476 symbol. */
4477 local_got_refcounts = elf_local_got_refcounts (abfd);
4478 if (local_got_refcounts == NULL)
4479 {
4480 bfd_size_type size;
4481
4482 size = symtab_hdr->sh_info;
4483 size *= sizeof (bfd_signed_vma);
4484 local_got_refcounts = ((bfd_signed_vma *)
4485 bfd_zalloc (abfd, size));
4486 if (local_got_refcounts == NULL)
4487 return FALSE;
4488 elf_local_got_refcounts (abfd) = local_got_refcounts;
4489 }
4490 local_got_refcounts[r_symndx] += 1;
4491 }
4492 break;
4493
4494 case R_M32R_26_PLTREL:
4495 /* This symbol requires a procedure linkage table entry. We
4496 actually build the entry in adjust_dynamic_symbol,
4497 because this might be a case of linking PIC code without
4498 linking in any dynamic objects, in which case we don't
4499 need to generate a procedure linkage table after all. */
41978308 4500
6edf0760
NC
4501 /* If this is a local symbol, we resolve it directly without
4502 creating a procedure linkage table entry. */
4503 if (h == NULL)
4504 continue;
4505
4506 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
4507 break;
4508
4509 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4510 h->plt.refcount += 1;
4511 break;
4512
4513 case R_M32R_16_RELA:
4514 case R_M32R_24_RELA:
4515 case R_M32R_32_RELA:
4516 case R_M32R_HI16_ULO_RELA:
4517 case R_M32R_HI16_SLO_RELA:
4518 case R_M32R_LO16_RELA:
4519 case R_M32R_SDA16_RELA:
4520 case R_M32R_18_PCREL_RELA:
4521 case R_M32R_26_PCREL_RELA:
4522
4523 if (h != NULL && !info->shared)
4524 {
4525 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
4526 h->plt.refcount += 1;
4527 }
4528
4529 /* If we are creating a shared library, and this is a reloc
4530 against a global symbol, or a non PC relative reloc
4531 against a local symbol, then we need to copy the reloc
4532 into the shared library. However, if we are linking with
4533 -Bsymbolic, we do not need to copy a reloc against a
4534 global symbol which is defined in an object we are
4535 including in the link (i.e., DEF_REGULAR is set). At
4536 this point we have not seen all the input files, so it is
4537 possible that DEF_REGULAR is not set now but will be set
4538 later (it is never cleared). We account for that
4539 possibility below by storing information in the
4540 dyn_relocs field of the hash table entry. A similar
4541 situation occurs when creating shared libraries and symbol
4542 visibility changes render the symbol local.
4543
4544 If on the other hand, we are creating an executable, we
4545 may need to keep relocations for symbols satisfied by a
4546 dynamic library if we manage to avoid copy relocs for the
4547 symbol. */
4548 if ((info->shared
4549 && (sec->flags & SEC_ALLOC) != 0
4550 && ((r_type != R_M32R_26_PCREL_RELA
4551 && r_type != R_M32R_18_PCREL_RELA)
4552 || (h != NULL
4553 && (! info->symbolic
4554 || h->root.type == bfd_link_hash_defweak
4555 || (h->elf_link_hash_flags
4556 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4557 || (!info->shared
4558 && (sec->flags & SEC_ALLOC) != 0
4559 && h != NULL
4560 && (h->root.type == bfd_link_hash_defweak
4561 || (h->elf_link_hash_flags
4562 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
4563 {
4564 struct elf_m32r_dyn_relocs *p;
4565 struct elf_m32r_dyn_relocs **head;
4566
4567 if (dynobj == NULL)
4568 htab->root.dynobj = dynobj = abfd;
4569
4570 /* When creating a shared object, we must copy these
4571 relocs into the output file. We create a reloc
4572 section in dynobj and make room for the reloc. */
4573 if (sreloc == NULL)
4574 {
4575 const char *name;
41978308 4576
6edf0760
NC
4577 name = (bfd_elf_string_from_elf_section
4578 (abfd,
4579 elf_elfheader (abfd)->e_shstrndx,
4580 elf_section_data (sec)->rel_hdr.sh_name));
4581 if (name == NULL)
4582 return FALSE;
41978308 4583
6edf0760
NC
4584 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4585 && strcmp (bfd_get_section_name (abfd, sec),
4586 name + 5) == 0);
41978308 4587
6edf0760
NC
4588 sreloc = bfd_get_section_by_name (dynobj, name);
4589 if (sreloc == NULL)
4590 {
4591 flagword flags;
41978308 4592
6edf0760
NC
4593 sreloc = bfd_make_section (dynobj, name);
4594 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4595 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4596 if ((sec->flags & SEC_ALLOC) != 0)
4597 flags |= SEC_ALLOC | SEC_LOAD;
4598 if (sreloc == NULL
4599 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4600 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4601 return FALSE;
4602 }
4603 elf_section_data (sec)->sreloc = sreloc;
4604 }
41978308 4605
6edf0760
NC
4606 /* If this is a global symbol, we count the number of
4607 relocations we need for this symbol. */
4608 if (h != NULL)
4609 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4610 else
4611 {
4612 asection *s;
4613
4614 /* Track dynamic relocs needed for local syms too. */
4615 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4616 sec, r_symndx);
4617 if (s == NULL)
4618 return FALSE;
4619
4620 head = ((struct elf_m32r_dyn_relocs **)
4621 &elf_section_data (s)->local_dynrel);
4622 }
4623
4624 p = *head;
4625 if (p == NULL || p->sec != sec)
4626 {
4627 bfd_size_type amt = sizeof (*p);
4628 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4629 if (p == NULL)
4630 return FALSE;
4631 p->next = *head;
4632 *head = p;
4633 p->sec = sec;
4634 p->count = 0;
4635 p->pc_count = 0;
4636 }
4637
4638 p->count += 1;
4639 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4640 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4641 p->pc_count += 1;
4642 }
4643 break;
4644
252b5132
RH
4645 /* This relocation describes the C++ object vtable hierarchy.
4646 Reconstruct it for later use during GC. */
6edf0760 4647 case R_M32R_RELA_GNU_VTINHERIT:
252b5132 4648 case R_M32R_GNU_VTINHERIT:
c152c796 4649 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4650 return FALSE;
252b5132 4651 break;
c3668558 4652
252b5132
RH
4653 /* This relocation describes which C++ vtable entries are actually
4654 used. Record for later use during GC. */
4655 case R_M32R_GNU_VTENTRY:
c152c796 4656 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 4657 return FALSE;
252b5132 4658 break;
6edf0760 4659 case R_M32R_RELA_GNU_VTENTRY:
c152c796 4660 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6edf0760
NC
4661 return FALSE;
4662 break;
252b5132
RH
4663 }
4664 }
c3668558 4665
b34976b6 4666 return TRUE;
252b5132 4667}
2f89ff8d
L
4668
4669static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4670{
7dcb9820
AM
4671 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4672 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
4673 { NULL, 0, 0, 0, 0 }
2f89ff8d 4674};
6edf0760
NC
4675
4676static bfd_boolean
4677m32r_elf_fake_sections (abfd, hdr, sec)
4678 bfd *abfd;
4679 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4680 asection *sec;
4681{
4682 register const char *name;
4683
4684 name = bfd_get_section_name (abfd, sec);
4685
4686 /* The generic elf_fake_sections will set up REL_HDR using the
4687 default kind of relocations. But, we may actually need both
4688 kinds of relocations, so we set up the second header here.
4689
4690 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4691 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4692 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4693 of the resulting empty .rela.<section> sections starts with
4694 sh_offset == object size, and ld doesn't allow that. While the check
4695 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4696 avoided by not emitting those useless sections in the first place. */
4697 if ((sec->flags & SEC_RELOC) != 0)
4698 {
4699 struct bfd_elf_section_data *esd;
4700 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4701
4702 esd = elf_section_data (sec);
4703 BFD_ASSERT (esd->rel_hdr2 == NULL);
4704 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4705 if (!esd->rel_hdr2)
4706 return FALSE;
4707 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4708 !sec->use_rela_p);
4709 }
4710
4711 return TRUE;
4712}
4713
4714static enum elf_reloc_type_class
4715m32r_elf_reloc_type_class (rela)
4716 const Elf_Internal_Rela *rela;
4717{
4718 switch ((int) ELF32_R_TYPE (rela->r_info))
4719 {
4720 case R_M32R_RELATIVE:
4721 return reloc_class_relative;
4722 case R_M32R_JMP_SLOT:
4723 return reloc_class_plt;
4724 case R_M32R_COPY:
4725 return reloc_class_copy;
4726 default:
4727 return reloc_class_normal;
4728 }
4729}
252b5132 4730\f
252b5132 4731#define ELF_ARCH bfd_arch_m32r
aa4f99bb
AO
4732#define ELF_MACHINE_CODE EM_M32R
4733#define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
e916b64e 4734#define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
252b5132
RH
4735
4736#define TARGET_BIG_SYM bfd_elf32_m32r_vec
4737#define TARGET_BIG_NAME "elf32-m32r"
6edf0760
NC
4738#define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4739#define TARGET_LITTLE_NAME "elf32-m32rle"
252b5132 4740
6edf0760 4741#define elf_info_to_howto m32r_info_to_howto
252b5132
RH
4742#define elf_info_to_howto_rel m32r_info_to_howto_rel
4743#define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4744#define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4745#define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4746#define elf_backend_relocate_section m32r_elf_relocate_section
4747#define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4748#define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4749#define elf_backend_check_relocs m32r_elf_check_relocs
4750
6edf0760
NC
4751#define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4752#define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4753#define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4754#define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4755#define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4756#define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4757#define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4758#define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4759
252b5132 4760#define elf_backend_can_gc_sections 1
6edf0760 4761/*#if !USE_REL
b491616a 4762#define elf_backend_rela_normal 1
6edf0760
NC
4763#endif*/
4764#define elf_backend_can_refcount 1
4765#define elf_backend_want_got_plt 1
4766#define elf_backend_plt_readonly 1
4767#define elf_backend_want_plt_sym 0
4768#define elf_backend_got_header_size 12
4769
4770#define elf_backend_may_use_rel_p 1
4771#ifdef USE_M32R_OLD_RELOC
4772#define elf_backend_default_use_rela_p 0
4773#define elf_backend_may_use_rela_p 0
4774#else
4775#define elf_backend_default_use_rela_p 1
4776#define elf_backend_may_use_rela_p 1
4777#define elf_backend_fake_sections m32r_elf_fake_sections
b491616a 4778#endif
6edf0760 4779
252b5132
RH
4780#if 0 /* not yet */
4781/* relax support */
4782#define bfd_elf32_bfd_relax_section m32r_elf_relax_section
4783#define bfd_elf32_bfd_get_relocated_section_contents \
4784 m32r_elf_get_relocated_section_contents
4785#endif
4786
4787#define elf_backend_object_p m32r_elf_object_p
4788#define elf_backend_final_write_processing m32r_elf_final_write_processing
252b5132
RH
4789#define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4790#define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4791#define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
2f89ff8d 4792#define elf_backend_special_sections m32r_elf_special_sections
c3668558 4793
252b5132 4794#include "elf32-target.h"
6edf0760
NC
4795
4796#undef ELF_MAXPAGESIZE
4797#define ELF_MAXPAGESIZE 0x1000
4798
4799#undef TARGET_BIG_SYM
4800#define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4801#undef TARGET_BIG_NAME
4802#define TARGET_BIG_NAME "elf32-m32r-linux"
4803#undef TARGET_LITTLE_SYM
4804#define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4805#undef TARGET_LITTLE_NAME
4806#define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4807#undef elf32_bed
4808#define elf32_bed elf32_m32r_lin_bed
4809
4810#include "elf32-target.h"
4811
This page took 0.461818 seconds and 4 git commands to generate.