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