2005-06-20 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / bfd / elf32-m32r.c
CommitLineData
252b5132 1/* M32R-specific support for 32-bit ELF.
3eb128b2 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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
3e110533 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 **));
252b5132
RH
59static bfd_reloc_status_type m32r_elf_final_sda_base
60 PARAMS ((bfd *, struct bfd_link_info *, const char **, bfd_vma *));
b34976b6 61static bfd_boolean m32r_elf_object_p
252b5132
RH
62 PARAMS ((bfd *));
63static void m32r_elf_final_write_processing
b34976b6
AM
64 PARAMS ((bfd *, bfd_boolean));
65static bfd_boolean m32r_elf_set_private_flags
252b5132 66 PARAMS ((bfd *, flagword));
b34976b6 67static bfd_boolean m32r_elf_merge_private_bfd_data
252b5132 68 PARAMS ((bfd *, bfd *));
b34976b6 69static bfd_boolean m32r_elf_print_private_bfd_data
252b5132 70 PARAMS ((bfd *, PTR));
b34976b6 71static bfd_boolean m32r_elf_gc_sweep_hook
917583ad
NC
72 PARAMS ((bfd *, struct bfd_link_info *, asection *,
73 const Elf_Internal_Rela *));
b34976b6 74static bfd_boolean m32r_elf_check_relocs
917583ad
NC
75 PARAMS ((bfd *, struct bfd_link_info *, asection *,
76 const Elf_Internal_Rela *));
77
6edf0760
NC
78static bfd_boolean m32r_elf_adjust_dynamic_symbol
79 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
80static bfd_boolean m32r_elf_size_dynamic_sections
81 PARAMS ((bfd *, struct bfd_link_info *));
82
917583ad 83asection * m32r_elf_gc_mark_hook
1e2f5b6e 84 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
917583ad 85 struct elf_link_hash_entry *, Elf_Internal_Sym *));
252b5132 86
6edf0760
NC
87static bfd_boolean m32r_elf_create_dynamic_sections
88 PARAMS ((bfd *, struct bfd_link_info *));
89
90static bfd_boolean m32r_elf_finish_dynamic_sections
91 PARAMS ((bfd *, struct bfd_link_info *));
41978308 92
6edf0760
NC
93static bfd_boolean m32r_elf_finish_dynamic_symbol
94 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
95 Elf_Internal_Sym *));
41978308 96
6edf0760
NC
97static bfd_boolean allocate_dynrelocs
98 PARAMS ((struct elf_link_hash_entry *, PTR));
99static bfd_boolean readonly_dynrelocs
100 PARAMS ((struct elf_link_hash_entry *, PTR));
101static enum elf_reloc_type_class m32r_elf_reloc_type_class
102 PARAMS ((const Elf_Internal_Rela *));
103static bfd_boolean m32r_elf_fake_sections
104 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
105
252b5132
RH
106#define NOP_INSN 0x7000
107#define MAKE_PARALLEL(insn) ((insn) | 0x8000)
108
109/* Use REL instead of RELA to save space.
110 This only saves space in libraries and object files, but perhaps
111 relocs will be put in ROM? All in all though, REL relocs are a pain
112 to work with. */
6edf0760 113/* #define USE_REL 1
acf8aed4
AM
114
115#ifndef USE_REL
116#define USE_REL 0
6edf0760
NC
117#endif */
118/* Use RELA. But use REL to link old objects for backwords compatibility. */
119
120/* Functions for the M32R ELF linker. */
41978308 121
6edf0760
NC
122/* The name of the dynamic interpreter. This is put in the .interp
123 section. */
41978308 124
6edf0760 125#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
41978308 126
6edf0760 127/* The nop opcode we use. */
41978308 128
6edf0760 129#define M32R_NOP 0x7000f000
41978308 130
6edf0760 131#define PLT_EMPTY 0x10101010 /* RIE -> RIE */
41978308 132
6edf0760
NC
133/* The size in bytes of an entry in the procedure linkage table. */
134
135#define PLT_ENTRY_SIZE 20
136#define PLT_HEADER_SIZE 20
41978308 137
6edf0760
NC
138/* The first one entries in a procedure linkage table are reserved,
139 and the initial contents are unimportant (we zero them out).
140 Subsequent entries look like this. */
41978308 141
6edf0760
NC
142#define PLT0_ENTRY_WORD0 0xd6c00000 /* seth r6, #high(.got+4) */
143#define PLT0_ENTRY_WORD1 0x86e60000 /* or3 r6, r6, #low(.got)+4) */
144#define PLT0_ENTRY_WORD2 0x24e626c6 /* ld r4, @r6+ -> ld r6, @r6 */
145#define PLT0_ENTRY_WORD3 0x1fc6f000 /* jmp r6 || pnop */
146#define PLT0_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
41978308 147
6edf0760
NC
148#define PLT0_PIC_ENTRY_WORD0 0xa4cc0004 /* ld r4, @(4,r12) */
149#define PLT0_PIC_ENTRY_WORD1 0xa6cc0008 /* ld r6, @(8,r12) */
150#define PLT0_PIC_ENTRY_WORD2 0x1fc6f000 /* jmp r6 || nop */
151#define PLT0_PIC_ENTRY_WORD3 PLT_EMPTY /* RIE -> RIE */
152#define PLT0_PIC_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
41978308 153
6edf0760
NC
154#define PLT_ENTRY_WORD0 0xe6000000 /* ld24 r6, .name_in_GOT */
155#define PLT_ENTRY_WORD1 0x06acf000 /* add r6, r12 || nop */
156#define PLT_ENTRY_WORD0b 0xd6c00000 /* seth r6, #high(.name_in_GOT) */
157#define PLT_ENTRY_WORD1b 0x86e60000 /* or3 r6, r6, #low(.name_in_GOT) */
158#define PLT_ENTRY_WORD2 0x26c61fc6 /* ld r6, @r6 -> jmp r6 */
159#define PLT_ENTRY_WORD3 0xe5000000 /* ld24 r5, $offset */
160#define PLT_ENTRY_WORD4 0xff000000 /* bra .plt0. */
161
252b5132
RH
162
163static reloc_howto_type m32r_elf_howto_table[] =
164{
165 /* This reloc does nothing. */
166 HOWTO (R_M32R_NONE, /* type */
167 0, /* rightshift */
168 2, /* size (0 = byte, 1 = short, 2 = long) */
169 32, /* bitsize */
b34976b6 170 FALSE, /* pc_relative */
252b5132
RH
171 0, /* bitpos */
172 complain_overflow_bitfield, /* complain_on_overflow */
173 bfd_elf_generic_reloc, /* special_function */
174 "R_M32R_NONE", /* name */
b34976b6 175 FALSE, /* partial_inplace */
252b5132
RH
176 0, /* src_mask */
177 0, /* dst_mask */
b34976b6 178 FALSE), /* pcrel_offset */
252b5132
RH
179
180 /* A 16 bit absolute relocation. */
181 HOWTO (R_M32R_16, /* type */
182 0, /* rightshift */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
184 16, /* bitsize */
b34976b6 185 FALSE, /* pc_relative */
252b5132
RH
186 0, /* bitpos */
187 complain_overflow_bitfield, /* complain_on_overflow */
8b125cde 188 m32r_elf_generic_reloc,/* special_function */
252b5132 189 "R_M32R_16", /* name */
b34976b6 190 TRUE, /* partial_inplace */
252b5132
RH
191 0xffff, /* src_mask */
192 0xffff, /* dst_mask */
b34976b6 193 FALSE), /* pcrel_offset */
252b5132
RH
194
195 /* A 32 bit absolute relocation. */
196 HOWTO (R_M32R_32, /* type */
197 0, /* rightshift */
198 2, /* size (0 = byte, 1 = short, 2 = long) */
199 32, /* bitsize */
b34976b6 200 FALSE, /* pc_relative */
252b5132
RH
201 0, /* bitpos */
202 complain_overflow_bitfield, /* complain_on_overflow */
8b125cde 203 m32r_elf_generic_reloc,/* special_function */
252b5132 204 "R_M32R_32", /* name */
b34976b6 205 TRUE, /* partial_inplace */
252b5132
RH
206 0xffffffff, /* src_mask */
207 0xffffffff, /* dst_mask */
b34976b6 208 FALSE), /* pcrel_offset */
252b5132
RH
209
210 /* A 24 bit address. */
211 HOWTO (R_M32R_24, /* type */
212 0, /* rightshift */
213 2, /* size (0 = byte, 1 = short, 2 = long) */
214 24, /* bitsize */
b34976b6 215 FALSE, /* pc_relative */
252b5132
RH
216 0, /* bitpos */
217 complain_overflow_unsigned, /* complain_on_overflow */
8b125cde 218 m32r_elf_generic_reloc,/* special_function */
252b5132 219 "R_M32R_24", /* name */
b34976b6 220 TRUE, /* partial_inplace */
252b5132
RH
221 0xffffff, /* src_mask */
222 0xffffff, /* dst_mask */
b34976b6 223 FALSE), /* pcrel_offset */
252b5132
RH
224
225 /* An PC Relative 10-bit relocation, shifted by 2.
226 This reloc is complicated because relocations are relative to pc & -4.
227 i.e. branches in the right insn slot use the address of the left insn
228 slot for pc. */
229 /* ??? It's not clear whether this should have partial_inplace set or not.
230 Branch relaxing in the assembler can store the addend in the insn,
231 and if bfd_install_relocation gets called the addend may get added
232 again. */
233 HOWTO (R_M32R_10_PCREL, /* type */
234 2, /* rightshift */
235 1, /* size (0 = byte, 1 = short, 2 = long) */
236 10, /* bitsize */
b34976b6 237 TRUE, /* pc_relative */
252b5132
RH
238 0, /* bitpos */
239 complain_overflow_signed, /* complain_on_overflow */
240 m32r_elf_10_pcrel_reloc, /* special_function */
241 "R_M32R_10_PCREL", /* name */
b34976b6 242 FALSE, /* partial_inplace */
252b5132
RH
243 0xff, /* src_mask */
244 0xff, /* dst_mask */
b34976b6 245 TRUE), /* pcrel_offset */
252b5132
RH
246
247 /* A relative 18 bit relocation, right shifted by 2. */
248 HOWTO (R_M32R_18_PCREL, /* type */
249 2, /* rightshift */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
251 16, /* bitsize */
b34976b6 252 TRUE, /* pc_relative */
252b5132
RH
253 0, /* bitpos */
254 complain_overflow_signed, /* complain_on_overflow */
255 bfd_elf_generic_reloc, /* special_function */
256 "R_M32R_18_PCREL", /* name */
b34976b6 257 FALSE, /* partial_inplace */
252b5132
RH
258 0xffff, /* src_mask */
259 0xffff, /* dst_mask */
b34976b6 260 TRUE), /* pcrel_offset */
252b5132
RH
261
262 /* A relative 26 bit relocation, right shifted by 2. */
263 /* ??? It's not clear whether this should have partial_inplace set or not.
264 Branch relaxing in the assembler can store the addend in the insn,
265 and if bfd_install_relocation gets called the addend may get added
266 again. */
267 HOWTO (R_M32R_26_PCREL, /* type */
268 2, /* rightshift */
269 2, /* size (0 = byte, 1 = short, 2 = long) */
270 26, /* bitsize */
b34976b6 271 TRUE, /* pc_relative */
252b5132
RH
272 0, /* bitpos */
273 complain_overflow_signed, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_M32R_26_PCREL", /* name */
b34976b6 276 FALSE, /* partial_inplace */
252b5132
RH
277 0xffffff, /* src_mask */
278 0xffffff, /* dst_mask */
b34976b6 279 TRUE), /* pcrel_offset */
252b5132
RH
280
281 /* High 16 bits of address when lower 16 is or'd in. */
282 HOWTO (R_M32R_HI16_ULO, /* type */
283 16, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
b34976b6 286 FALSE, /* pc_relative */
252b5132
RH
287 0, /* bitpos */
288 complain_overflow_dont, /* complain_on_overflow */
289 m32r_elf_hi16_reloc, /* special_function */
290 "R_M32R_HI16_ULO", /* name */
b34976b6 291 TRUE, /* partial_inplace */
252b5132
RH
292 0x0000ffff, /* src_mask */
293 0x0000ffff, /* dst_mask */
b34976b6 294 FALSE), /* pcrel_offset */
252b5132
RH
295
296 /* High 16 bits of address when lower 16 is added in. */
297 HOWTO (R_M32R_HI16_SLO, /* type */
298 16, /* rightshift */
299 2, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
b34976b6 301 FALSE, /* pc_relative */
252b5132
RH
302 0, /* bitpos */
303 complain_overflow_dont, /* complain_on_overflow */
304 m32r_elf_hi16_reloc, /* special_function */
305 "R_M32R_HI16_SLO", /* name */
b34976b6 306 TRUE, /* partial_inplace */
252b5132
RH
307 0x0000ffff, /* src_mask */
308 0x0000ffff, /* dst_mask */
b34976b6 309 FALSE), /* pcrel_offset */
252b5132
RH
310
311 /* Lower 16 bits of address. */
312 HOWTO (R_M32R_LO16, /* type */
313 0, /* rightshift */
314 2, /* size (0 = byte, 1 = short, 2 = long) */
315 16, /* bitsize */
b34976b6 316 FALSE, /* pc_relative */
252b5132
RH
317 0, /* bitpos */
318 complain_overflow_dont, /* complain_on_overflow */
319 m32r_elf_lo16_reloc, /* special_function */
320 "R_M32R_LO16", /* name */
b34976b6 321 TRUE, /* partial_inplace */
252b5132
RH
322 0x0000ffff, /* src_mask */
323 0x0000ffff, /* dst_mask */
b34976b6 324 FALSE), /* pcrel_offset */
252b5132
RH
325
326 /* Small data area 16 bits offset. */
327 HOWTO (R_M32R_SDA16, /* type */
328 0, /* rightshift */
329 2, /* size (0 = byte, 1 = short, 2 = long) */
330 16, /* bitsize */
b34976b6 331 FALSE, /* pc_relative */
252b5132
RH
332 0, /* bitpos */
333 complain_overflow_signed, /* complain_on_overflow */
334 m32r_elf_sda16_reloc, /* special_function */
335 "R_M32R_SDA16", /* name */
b34976b6 336 TRUE, /* partial_inplace */ /* FIXME: correct? */
252b5132
RH
337 0x0000ffff, /* src_mask */
338 0x0000ffff, /* dst_mask */
b34976b6 339 FALSE), /* pcrel_offset */
252b5132
RH
340
341 /* GNU extension to record C++ vtable hierarchy */
342 HOWTO (R_M32R_GNU_VTINHERIT, /* type */
343 0, /* rightshift */
344 2, /* size (0 = byte, 1 = short, 2 = long) */
345 0, /* bitsize */
b34976b6 346 FALSE, /* pc_relative */
252b5132
RH
347 0, /* bitpos */
348 complain_overflow_dont, /* complain_on_overflow */
349 NULL, /* special_function */
350 "R_M32R_GNU_VTINHERIT", /* name */
b34976b6 351 FALSE, /* partial_inplace */
252b5132
RH
352 0, /* src_mask */
353 0, /* dst_mask */
b34976b6 354 FALSE), /* pcrel_offset */
252b5132
RH
355
356 /* GNU extension to record C++ vtable member usage */
357 HOWTO (R_M32R_GNU_VTENTRY, /* type */
358 0, /* rightshift */
359 2, /* size (0 = byte, 1 = short, 2 = long) */
360 0, /* bitsize */
b34976b6 361 FALSE, /* pc_relative */
252b5132
RH
362 0, /* bitpos */
363 complain_overflow_dont, /* complain_on_overflow */
364 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
365 "R_M32R_GNU_VTENTRY", /* name */
b34976b6 366 FALSE, /* partial_inplace */
252b5132
RH
367 0, /* src_mask */
368 0, /* dst_mask */
b34976b6 369 FALSE), /* pcrel_offset */
252b5132 370
6edf0760
NC
371 EMPTY_HOWTO (13),
372 EMPTY_HOWTO (14),
373 EMPTY_HOWTO (15),
374 EMPTY_HOWTO (16),
375 EMPTY_HOWTO (17),
376 EMPTY_HOWTO (18),
377 EMPTY_HOWTO (19),
378 EMPTY_HOWTO (20),
379 EMPTY_HOWTO (21),
380 EMPTY_HOWTO (22),
381 EMPTY_HOWTO (23),
382 EMPTY_HOWTO (24),
383 EMPTY_HOWTO (25),
384 EMPTY_HOWTO (26),
385 EMPTY_HOWTO (27),
386 EMPTY_HOWTO (28),
387 EMPTY_HOWTO (29),
388 EMPTY_HOWTO (30),
389 EMPTY_HOWTO (31),
390 EMPTY_HOWTO (32),
391
392 /* A 16 bit absolute relocation. */
393 HOWTO (R_M32R_16_RELA, /* type */
394 0, /* rightshift */
395 1, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
397 FALSE, /* pc_relative */
398 0, /* bitpos */
399 complain_overflow_bitfield, /* complain_on_overflow */
400 bfd_elf_generic_reloc, /* special_function */
401 "R_M32R_16_RELA", /* name */
402 FALSE, /* partial_inplace */
403 0xffff, /* src_mask */
404 0xffff, /* dst_mask */
405 FALSE), /* pcrel_offset */
406
407 /* A 32 bit absolute relocation. */
408 HOWTO (R_M32R_32_RELA, /* type */
409 0, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 32, /* bitsize */
412 FALSE, /* pc_relative */
413 0, /* bitpos */
414 complain_overflow_bitfield, /* complain_on_overflow */
415 bfd_elf_generic_reloc,/* special_function */
416 "R_M32R_32_RELA", /* name */
417 FALSE, /* partial_inplace */
418 0xffffffff, /* src_mask */
419 0xffffffff, /* dst_mask */
420 FALSE), /* pcrel_offset */
421
422 /* A 24 bit address. */
423 HOWTO (R_M32R_24_RELA, /* type */
424 0, /* rightshift */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
426 24, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_unsigned, /* complain_on_overflow */
430 bfd_elf_generic_reloc,/* special_function */
431 "R_M32R_24_RELA", /* name */
432 FALSE, /* partial_inplace */
433 0xffffff, /* src_mask */
434 0xffffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
436
437 HOWTO (R_M32R_10_PCREL_RELA, /* type */
438 2, /* rightshift */
439 1, /* size (0 = byte, 1 = short, 2 = long) */
440 10, /* bitsize */
441 TRUE, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_signed, /* complain_on_overflow */
444 m32r_elf_10_pcrel_reloc, /* special_function */
445 "R_M32R_10_PCREL_RELA",/* name */
446 FALSE, /* partial_inplace */
447 0xff, /* src_mask */
448 0xff, /* dst_mask */
449 TRUE), /* pcrel_offset */
450
451 /* A relative 18 bit relocation, right shifted by 2. */
452 HOWTO (R_M32R_18_PCREL_RELA, /* type */
453 2, /* rightshift */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
455 16, /* bitsize */
456 TRUE, /* pc_relative */
457 0, /* bitpos */
458 complain_overflow_signed, /* complain_on_overflow */
459 bfd_elf_generic_reloc, /* special_function */
460 "R_M32R_18_PCREL_RELA",/* name */
461 FALSE, /* partial_inplace */
462 0xffff, /* src_mask */
463 0xffff, /* dst_mask */
464 TRUE), /* pcrel_offset */
465
466 /* A relative 26 bit relocation, right shifted by 2. */
467 HOWTO (R_M32R_26_PCREL_RELA, /* type */
468 2, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 26, /* bitsize */
471 TRUE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_signed, /* complain_on_overflow */
474 bfd_elf_generic_reloc, /* special_function */
475 "R_M32R_26_PCREL_RELA",/* name */
476 FALSE, /* partial_inplace */
477 0xffffff, /* src_mask */
478 0xffffff, /* dst_mask */
479 TRUE), /* pcrel_offset */
480
481 /* High 16 bits of address when lower 16 is or'd in. */
482 HOWTO (R_M32R_HI16_ULO_RELA, /* type */
483 16, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
486 FALSE, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_dont, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* special_function */
490 "R_M32R_HI16_ULO_RELA",/* name */
491 FALSE, /* partial_inplace */
492 0x0000ffff, /* src_mask */
493 0x0000ffff, /* dst_mask */
494 FALSE), /* pcrel_offset */
495
496 /* High 16 bits of address when lower 16 is added in. */
497 HOWTO (R_M32R_HI16_SLO_RELA, /* type */
498 16, /* rightshift */
499 2, /* size (0 = byte, 1 = short, 2 = long) */
500 16, /* bitsize */
501 FALSE, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_dont, /* complain_on_overflow */
504 bfd_elf_generic_reloc, /* special_function */
505 "R_M32R_HI16_SLO_RELA",/* name */
506 FALSE, /* partial_inplace */
507 0x0000ffff, /* src_mask */
508 0x0000ffff, /* dst_mask */
509 FALSE), /* pcrel_offset */
510
511 /* Lower 16 bits of address. */
512 HOWTO (R_M32R_LO16_RELA, /* type */
513 0, /* rightshift */
514 2, /* size (0 = byte, 1 = short, 2 = long) */
515 16, /* bitsize */
516 FALSE, /* pc_relative */
517 0, /* bitpos */
518 complain_overflow_dont, /* complain_on_overflow */
519 bfd_elf_generic_reloc, /* special_function */
520 "R_M32R_LO16_RELA", /* name */
521 FALSE, /* partial_inplace */
522 0x0000ffff, /* src_mask */
523 0x0000ffff, /* dst_mask */
524 FALSE), /* pcrel_offset */
525
526 /* Small data area 16 bits offset. */
527 HOWTO (R_M32R_SDA16_RELA, /* type */
528 0, /* rightshift */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
530 16, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_signed, /* complain_on_overflow */
534 bfd_elf_generic_reloc, /* special_function */
535 "R_M32R_SDA16_RELA", /* name */
536 TRUE, /* partial_inplace */ /* FIXME: correct? */
537 0x0000ffff, /* src_mask */
538 0x0000ffff, /* dst_mask */
539 FALSE), /* pcrel_offset */
540
541 /* GNU extension to record C++ vtable hierarchy */
542 HOWTO (R_M32R_RELA_GNU_VTINHERIT, /* type */
543 0, /* rightshift */
544 2, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
549 NULL, /* special_function */
550 "R_M32R_RELA_GNU_VTINHERIT", /* name */
551 FALSE, /* partial_inplace */
552 0, /* src_mask */
553 0, /* dst_mask */
554 FALSE), /* pcrel_offset */
555
556 /* GNU extension to record C++ vtable member usage */
557 HOWTO (R_M32R_RELA_GNU_VTENTRY, /* type */
558 0, /* rightshift */
559 2, /* size (0 = byte, 1 = short, 2 = long) */
560 0, /* bitsize */
561 FALSE, /* pc_relative */
562 0, /* bitpos */
563 complain_overflow_dont, /* complain_on_overflow */
564 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
565 "R_M32R_RELA_GNU_VTENTRY", /* name */
566 FALSE, /* partial_inplace */
567 0, /* src_mask */
568 0, /* dst_mask */
569 FALSE), /* pcrel_offset */
570
571 EMPTY_HOWTO (45),
572 EMPTY_HOWTO (46),
573 EMPTY_HOWTO (47),
574
575 /* Like R_M32R_24, but referring to the GOT table entry for
576 the symbol. */
577 HOWTO (R_M32R_GOT24, /* type */
578 0, /* rightshift */
579 2, /* size (0 = byte, 1 = short, 2 = long) */
580 24, /* bitsize */
581 FALSE, /* pc_relative */
582 0, /* bitpos */
583 complain_overflow_unsigned, /* complain_on_overflow */
584 bfd_elf_generic_reloc, /* special_function */
585 "R_M32R_GOT24", /* name */
586 FALSE, /* partial_inplace */
587 0xffffff, /* src_mask */
588 0xffffff, /* dst_mask */
589 FALSE), /* pcrel_offset */
590
591 /* Like R_M32R_PCREL, but referring to the procedure linkage table
592 entry for the symbol. */
593 HOWTO (R_M32R_26_PLTREL, /* type */
594 2, /* rightshift */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
596 24, /* bitsize */
597 TRUE, /* pc_relative */
598 0, /* bitpos */
599 complain_overflow_signed, /* complain_on_overflow */
600 bfd_elf_generic_reloc, /* special_function */
601 "R_M32R_26_PLTREL", /* name */
602 FALSE, /* partial_inplace */
603 0xffffff, /* src_mask */
604 0xffffff, /* dst_mask */
605 TRUE), /* pcrel_offset */
606
607 /* This is used only by the dynamic linker. The symbol should exist
608 both in the object being run and in some shared library. The
609 dynamic linker copies the data addressed by the symbol from the
610 shared library into the object, because the object being
611 run has to have the data at some particular address. */
612 HOWTO (R_M32R_COPY, /* type */
613 0, /* rightshift */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
615 32, /* bitsize */
616 FALSE, /* pc_relative */
617 0, /* bitpos */
618 complain_overflow_bitfield, /* complain_on_overflow */
619 bfd_elf_generic_reloc, /* special_function */
620 "R_M32R_COPY", /* name */
621 FALSE, /* partial_inplace */
622 0xffffffff, /* src_mask */
623 0xffffffff, /* dst_mask */
624 FALSE), /* pcrel_offset */
625
626 /* Like R_M32R_24, but used when setting global offset table
627 entries. */
628 HOWTO (R_M32R_GLOB_DAT, /* type */
629 0, /* rightshift */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
631 32, /* bitsize */
632 FALSE, /* pc_relative */
633 0, /* bitpos */
634 complain_overflow_bitfield, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_M32R_GLOB_DAT", /* name */
637 FALSE, /* partial_inplace */
638 0xffffffff, /* src_mask */
639 0xffffffff, /* dst_mask */
640 FALSE), /* pcrel_offset */
641
642 /* Marks a procedure linkage table entry for a symbol. */
643 HOWTO (R_M32R_JMP_SLOT, /* type */
644 0, /* rightshift */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
646 32, /* bitsize */
647 FALSE, /* pc_relative */
648 0, /* bitpos */
649 complain_overflow_bitfield, /* complain_on_overflow */
650 bfd_elf_generic_reloc, /* special_function */
651 "R_M32R_JMP_SLOT", /* name */
652 FALSE, /* partial_inplace */
653 0xffffffff, /* src_mask */
654 0xffffffff, /* dst_mask */
655 FALSE), /* pcrel_offset */
656
657 /* Used only by the dynamic linker. When the object is run, this
658 longword is set to the load address of the object, plus the
659 addend. */
660 HOWTO (R_M32R_RELATIVE, /* type */
661 0, /* rightshift */
662 2, /* size (0 = byte, 1 = short, 2 = long) */
663 32, /* bitsize */
664 FALSE, /* pc_relative */
665 0, /* bitpos */
666 complain_overflow_bitfield, /* complain_on_overflow */
667 bfd_elf_generic_reloc, /* special_function */
097f809a 668 "R_M32R_RELATIVE", /* name */
6edf0760
NC
669 FALSE, /* partial_inplace */
670 0xffffffff, /* src_mask */
671 0xffffffff, /* dst_mask */
672 FALSE), /* pcrel_offset */
673
674 HOWTO (R_M32R_GOTOFF, /* type */
675 0, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
097f809a 677 24, /* bitsize */
6edf0760
NC
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_bitfield, /* complain_on_overflow */
681 bfd_elf_generic_reloc, /* special_function */
097f809a 682 "R_M32R_GOTOFF", /* name */
6edf0760 683 FALSE, /* partial_inplace */
097f809a
NC
684 0xffffff, /* src_mask */
685 0xffffff, /* dst_mask */
6edf0760
NC
686 FALSE), /* pcrel_offset */
687
688 /* An PC Relative 24-bit relocation used when setting PIC offset
689 table register. */
690 HOWTO (R_M32R_GOTPC24, /* type */
691 0, /* rightshift */
692 2, /* size (0 = byte, 1 = short, 2 = long) */
693 24, /* bitsize */
694 TRUE, /* pc_relative */
695 0, /* bitpos */
696 complain_overflow_unsigned, /* complain_on_overflow */
697 bfd_elf_generic_reloc, /* special_function */
698 "R_M32R_GOTPC24", /* name */
699 FALSE, /* partial_inplace */
700 0xffffff, /* src_mask */
701 0xffffff, /* dst_mask */
702 TRUE), /* pcrel_offset */
703
704 /* Like R_M32R_HI16_ULO, but referring to the GOT table entry for
705 the symbol. */
706 HOWTO (R_M32R_GOT16_HI_ULO, /* type */
707 16, /* rightshift */
708 2, /* size (0 = byte, 1 = short, 2 = long) */
709 16, /* bitsize */
710 FALSE, /* pc_relative */
711 0, /* bitpos */
712 complain_overflow_dont, /* complain_on_overflow */
713 bfd_elf_generic_reloc, /* special_function */
714 "R_M32R_GOT16_HI_ULO", /* name */
715 FALSE, /* partial_inplace */
716 0x0000ffff, /* src_mask */
717 0x0000ffff, /* dst_mask */
718 FALSE), /* pcrel_offset */
719
720 /* Like R_M32R_HI16_SLO, but referring to the GOT table entry for
721 the symbol. */
722 HOWTO (R_M32R_GOT16_HI_SLO, /* type */
723 16, /* rightshift */
724 2, /* size (0 = byte, 1 = short, 2 = long) */
725 16, /* bitsize */
726 FALSE, /* pc_relative */
727 0, /* bitpos */
728 complain_overflow_dont, /* complain_on_overflow */
729 bfd_elf_generic_reloc, /* special_function */
730 "R_M32R_GOT16_HI_SLO", /* name */
731 FALSE, /* partial_inplace */
732 0x0000ffff, /* src_mask */
733 0x0000ffff, /* dst_mask */
734 FALSE), /* pcrel_offset */
735
736 /* Like R_M32R_LO16, but referring to the GOT table entry for
737 the symbol. */
738 HOWTO (R_M32R_GOT16_LO, /* type */
739 0, /* rightshift */
740 2, /* size (0 = byte, 1 = short, 2 = long) */
741 16, /* bitsize */
742 FALSE, /* pc_relative */
743 0, /* bitpos */
744 complain_overflow_dont, /* complain_on_overflow */
745 bfd_elf_generic_reloc, /* special_function */
746 "R_M32R_GOT16_LO", /* name */
747 FALSE, /* partial_inplace */
748 0x0000ffff, /* src_mask */
749 0x0000ffff, /* dst_mask */
750 FALSE), /* pcrel_offset */
751
752 /* An PC Relative relocation used when setting PIC offset table register.
753 Like R_M32R_HI16_ULO, but referring to the GOT table entry for
754 the symbol. */
755 HOWTO (R_M32R_GOTPC_HI_ULO, /* type */
756 16, /* rightshift */
757 2, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
759 FALSE, /* pc_relative */
760 0, /* bitpos */
761 complain_overflow_dont, /* complain_on_overflow */
762 bfd_elf_generic_reloc, /* special_function */
763 "R_M32R_GOTPC_HI_ULO", /* name */
764 FALSE, /* partial_inplace */
765 0x0000ffff, /* src_mask */
766 0x0000ffff, /* dst_mask */
767 TRUE), /* pcrel_offset */
768
769 /* An PC Relative relocation used when setting PIC offset table register.
770 Like R_M32R_HI16_SLO, but referring to the GOT table entry for
771 the symbol. */
772 HOWTO (R_M32R_GOTPC_HI_SLO, /* type */
773 16, /* rightshift */
774 2, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
776 FALSE, /* pc_relative */
777 0, /* bitpos */
778 complain_overflow_dont, /* complain_on_overflow */
779 bfd_elf_generic_reloc, /* special_function */
780 "R_M32R_GOTPC_HI_SLO", /* name */
781 FALSE, /* partial_inplace */
782 0x0000ffff, /* src_mask */
783 0x0000ffff, /* dst_mask */
784 TRUE), /* pcrel_offset */
785
786 /* An PC Relative relocation used when setting PIC offset table register.
787 Like R_M32R_LO16, but referring to the GOT table entry for
788 the symbol. */
789 HOWTO (R_M32R_GOTPC_LO, /* type */
790 0, /* rightshift */
791 2, /* size (0 = byte, 1 = short, 2 = long) */
792 16, /* bitsize */
793 FALSE, /* pc_relative */
794 0, /* bitpos */
795 complain_overflow_dont, /* complain_on_overflow */
796 bfd_elf_generic_reloc, /* special_function */
797 "R_M32R_GOTPC_LO", /* name */
798 FALSE, /* partial_inplace */
799 0x0000ffff, /* src_mask */
800 0x0000ffff, /* dst_mask */
801 TRUE), /* pcrel_offset */
097f809a
NC
802
803 HOWTO (R_M32R_GOTOFF_HI_ULO, /* type */
804 16, /* rightshift */
805 2, /* size (0 = byte, 1 = short, 2 = long) */
806 16, /* bitsize */
807 FALSE, /* pc_relative */
808 0, /* bitpos */
809 complain_overflow_dont, /* complain_on_overflow */
810 bfd_elf_generic_reloc, /* special_function */
811 "R_M32R_GOTOFF_HI_ULO",/* name */
812 FALSE, /* partial_inplace */
813 0x0000ffff, /* src_mask */
814 0x0000ffff, /* dst_mask */
815 FALSE), /* pcrel_offset */
816
817 HOWTO (R_M32R_GOTOFF_HI_SLO, /* type */
818 16, /* rightshift */
819 2, /* size (0 = byte, 1 = short, 2 = long) */
820 16, /* bitsize */
821 FALSE, /* pc_relative */
822 0, /* bitpos */
823 complain_overflow_dont, /* complain_on_overflow */
824 bfd_elf_generic_reloc, /* special_function */
825 "R_M32R_GOTOFF_HI_SLO",/* name */
826 FALSE, /* partial_inplace */
827 0x0000ffff, /* src_mask */
828 0x0000ffff, /* dst_mask */
829 FALSE), /* pcrel_offset */
830
831 HOWTO (R_M32R_GOTOFF_LO, /* type */
832 0, /* rightshift */
833 2, /* size (0 = byte, 1 = short, 2 = long) */
834 16, /* bitsize */
835 FALSE, /* pc_relative */
836 0, /* bitpos */
837 complain_overflow_dont, /* complain_on_overflow */
838 bfd_elf_generic_reloc, /* special_function */
839 "R_M32R_GOTOFF_LO", /* name */
840 FALSE, /* partial_inplace */
841 0x0000ffff, /* src_mask */
842 0x0000ffff, /* dst_mask */
843 FALSE), /* pcrel_offset */
252b5132
RH
844};
845\f
846/* Handle the R_M32R_10_PCREL reloc. */
847
848static bfd_reloc_status_type
849m32r_elf_10_pcrel_reloc (abfd, reloc_entry, symbol, data,
850 input_section, output_bfd, error_message)
851 bfd * abfd;
852 arelent * reloc_entry;
853 asymbol * symbol;
854 PTR data;
855 asection * input_section;
856 bfd * output_bfd;
5f771d47 857 char ** error_message ATTRIBUTE_UNUSED;
252b5132
RH
858{
859 /* This part is from bfd_elf_generic_reloc. */
860 if (output_bfd != (bfd *) NULL
861 && (symbol->flags & BSF_SECTION_SYM) == 0
862 && (! reloc_entry->howto->partial_inplace
863 || reloc_entry->addend == 0))
864 {
865 reloc_entry->address += input_section->output_offset;
866 return bfd_reloc_ok;
867 }
868
869 if (output_bfd != NULL)
870 {
871 /* FIXME: See bfd_perform_relocation. Is this right? */
872 return bfd_reloc_continue;
873 }
874
875 return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
876 input_section,
877 data, reloc_entry->address,
878 symbol->section,
879 (symbol->value
880 + symbol->section->output_section->vma
881 + symbol->section->output_offset),
882 reloc_entry->addend);
883}
884
885/* Utility to actually perform an R_M32R_10_PCREL reloc. */
886
887static bfd_reloc_status_type
888m32r_elf_do_10_pcrel_reloc (abfd, howto, input_section, data, offset,
889 symbol_section, symbol_value, addend)
890 bfd *abfd;
891 reloc_howto_type *howto;
892 asection *input_section;
893 bfd_byte *data;
894 bfd_vma offset;
5f771d47 895 asection *symbol_section ATTRIBUTE_UNUSED;
252b5132
RH
896 bfd_vma symbol_value;
897 bfd_vma addend;
898{
899 bfd_signed_vma relocation;
900 unsigned long x;
901 bfd_reloc_status_type status;
902
903 /* Sanity check the address (offset in section). */
07515404 904 if (offset > bfd_get_section_limit (abfd, input_section))
252b5132
RH
905 return bfd_reloc_outofrange;
906
907 relocation = symbol_value + addend;
908 /* Make it pc relative. */
909 relocation -= (input_section->output_section->vma
910 + input_section->output_offset);
911 /* These jumps mask off the lower two bits of the current address
912 before doing pcrel calculations. */
dc810e39 913 relocation -= (offset & -(bfd_vma) 4);
252b5132
RH
914
915 if (relocation < -0x200 || relocation > 0x1ff)
916 status = bfd_reloc_overflow;
917 else
918 status = bfd_reloc_ok;
919
920 x = bfd_get_16 (abfd, data + offset);
921 relocation >>= howto->rightshift;
922 relocation <<= howto->bitpos;
923 x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
dc810e39 924 bfd_put_16 (abfd, (bfd_vma) x, data + offset);
252b5132
RH
925
926 return status;
927}
928
929/* Handle the R_M32R_HI16_[SU]LO relocs.
930 HI16_SLO is for the add3 and load/store with displacement instructions.
931 HI16_ULO is for the or3 instruction.
932 For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
933 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
934 we must add one to the high 16 bytes (which will get subtracted off when
935 the low 16 bits are added).
936 These relocs have to be done in combination with an R_M32R_LO16 reloc
937 because there is a carry from the LO16 to the HI16. Here we just save
938 the information we need; we do the actual relocation when we see the LO16.
939 This code is copied from the elf32-mips.c. We also support an arbitrary
940 number of HI16 relocs to be associated with a single LO16 reloc. The
941 assembler sorts the relocs to ensure each HI16 immediately precedes its
942 LO16. However if there are multiple copies, the assembler may not find
943 the real LO16 so it picks the first one it finds. */
944
945struct m32r_hi16
946{
947 struct m32r_hi16 *next;
948 bfd_byte *addr;
949 bfd_vma addend;
950};
951
952/* FIXME: This should not be a static variable. */
953
954static struct m32r_hi16 *m32r_hi16_list;
955
956static bfd_reloc_status_type
957m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
958 input_section, output_bfd, error_message)
5f771d47 959 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
960 arelent *reloc_entry;
961 asymbol *symbol;
962 PTR data;
963 asection *input_section;
964 bfd *output_bfd;
5f771d47 965 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
966{
967 bfd_reloc_status_type ret;
968 bfd_vma relocation;
969 struct m32r_hi16 *n;
970
971 /* This part is from bfd_elf_generic_reloc.
972 If we're relocating, and this an external symbol, we don't want
973 to change anything. */
974 if (output_bfd != (bfd *) NULL
975 && (symbol->flags & BSF_SECTION_SYM) == 0
976 && reloc_entry->addend == 0)
977 {
978 reloc_entry->address += input_section->output_offset;
979 return bfd_reloc_ok;
980 }
981
982 /* Sanity check the address (offset in section). */
07515404 983 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
252b5132
RH
984 return bfd_reloc_outofrange;
985
986 ret = bfd_reloc_ok;
987 if (bfd_is_und_section (symbol->section)
988 && output_bfd == (bfd *) NULL)
989 ret = bfd_reloc_undefined;
990
991 if (bfd_is_com_section (symbol->section))
992 relocation = 0;
993 else
994 relocation = symbol->value;
995
996 relocation += symbol->section->output_section->vma;
997 relocation += symbol->section->output_offset;
998 relocation += reloc_entry->addend;
999
1000 /* Save the information, and let LO16 do the actual relocation. */
dc810e39 1001 n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
252b5132
RH
1002 if (n == NULL)
1003 return bfd_reloc_outofrange;
1004 n->addr = (bfd_byte *) data + reloc_entry->address;
1005 n->addend = relocation;
1006 n->next = m32r_hi16_list;
1007 m32r_hi16_list = n;
1008
1009 if (output_bfd != (bfd *) NULL)
1010 reloc_entry->address += input_section->output_offset;
1011
1012 return ret;
1013}
1014
1015/* Handle an M32R ELF HI16 reloc. */
1016
1017static void
1018m32r_elf_relocate_hi16 (input_bfd, type, relhi, rello, contents, addend)
1019 bfd *input_bfd;
1020 int type;
1021 Elf_Internal_Rela *relhi;
1022 Elf_Internal_Rela *rello;
1023 bfd_byte *contents;
1024 bfd_vma addend;
1025{
1026 unsigned long insn;
1027 bfd_vma addlo;
1028
1029 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1030
1031 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
1032 if (type == R_M32R_HI16_SLO)
1033 addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
1034 else
1035 addlo &= 0xffff;
1036
1037 addend += ((insn & 0xffff) << 16) + addlo;
1038
1039 /* Reaccount for sign extension of low part. */
1040 if (type == R_M32R_HI16_SLO
1041 && (addend & 0x8000) != 0)
1042 addend += 0x10000;
1043
1044 bfd_put_32 (input_bfd,
1045 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
1046 contents + relhi->r_offset);
1047}
1048
1049/* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
1050 inplace relocation; this function exists in order to do the
1051 R_M32R_HI16_[SU]LO relocation described above. */
1052
1053bfd_reloc_status_type
4c72e23a 1054m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
252b5132 1055 input_section, output_bfd, error_message)
4c72e23a 1056 bfd *input_bfd;
252b5132
RH
1057 arelent *reloc_entry;
1058 asymbol *symbol;
1059 PTR data;
1060 asection *input_section;
1061 bfd *output_bfd;
1062 char **error_message;
1063{
4c72e23a
CM
1064 /* This part is from bfd_elf_generic_reloc.
1065 If we're relocating, and this an external symbol, we don't want
1066 to change anything. */
1067 if (output_bfd != (bfd *) NULL
1068 && (symbol->flags & BSF_SECTION_SYM) == 0
1069 && reloc_entry->addend == 0)
1070 {
1071 reloc_entry->address += input_section->output_offset;
1072 return bfd_reloc_ok;
1073 }
1074
252b5132
RH
1075 if (m32r_hi16_list != NULL)
1076 {
1077 struct m32r_hi16 *l;
1078
1079 l = m32r_hi16_list;
1080 while (l != NULL)
1081 {
1082 unsigned long insn;
1083 unsigned long val;
1084 unsigned long vallo;
1085 struct m32r_hi16 *next;
1086
1087 /* Do the HI16 relocation. Note that we actually don't need
1088 to know anything about the LO16 itself, except where to
1089 find the low 16 bits of the addend needed by the LO16. */
4c72e23a
CM
1090 insn = bfd_get_32 (input_bfd, l->addr);
1091 vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
252b5132
RH
1092 & 0xffff) ^ 0x8000) - 0x8000;
1093 val = ((insn & 0xffff) << 16) + vallo;
1094 val += l->addend;
1095
1096 /* Reaccount for sign extension of low part. */
1097 if ((val & 0x8000) != 0)
1098 val += 0x10000;
1099
dc810e39
AM
1100 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1101 bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
252b5132
RH
1102
1103 next = l->next;
1104 free (l);
1105 l = next;
1106 }
1107
1108 m32r_hi16_list = NULL;
1109 }
1110
4c72e23a
CM
1111 /* Now do the LO16 reloc in the usual way.
1112 ??? It would be nice to call bfd_elf_generic_reloc here,
ab3acfbe 1113 but we have partial_inplace set. bfd_elf_generic_reloc will
4c72e23a
CM
1114 pass the handling back to bfd_install_relocation which will install
1115 a section relative addend which is wrong. */
8b125cde
DE
1116 return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1117 input_section, output_bfd, error_message);
1118}
1119
c3668558
KH
1120/* Do generic partial_inplace relocation.
1121 This is a local replacement for bfd_elf_generic_reloc. */
8b125cde
DE
1122
1123bfd_reloc_status_type
1124m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1125 input_section, output_bfd, error_message)
1126 bfd *input_bfd;
1127 arelent *reloc_entry;
1128 asymbol *symbol;
1129 PTR data;
1130 asection *input_section;
1131 bfd *output_bfd;
23ccc829 1132 char **error_message ATTRIBUTE_UNUSED;
8b125cde
DE
1133{
1134 bfd_reloc_status_type ret;
1135 bfd_vma relocation;
1136 bfd_byte *inplace_address;
1137
1138 /* This part is from bfd_elf_generic_reloc.
1139 If we're relocating, and this an external symbol, we don't want
1140 to change anything. */
1141 if (output_bfd != (bfd *) NULL
1142 && (symbol->flags & BSF_SECTION_SYM) == 0
1143 && reloc_entry->addend == 0)
1144 {
1145 reloc_entry->address += input_section->output_offset;
1146 return bfd_reloc_ok;
1147 }
1148
08da05b0 1149 /* Now do the reloc in the usual way.
8b125cde 1150 ??? It would be nice to call bfd_elf_generic_reloc here,
ab3acfbe 1151 but we have partial_inplace set. bfd_elf_generic_reloc will
8b125cde
DE
1152 pass the handling back to bfd_install_relocation which will install
1153 a section relative addend which is wrong. */
4c72e23a
CM
1154
1155 /* Sanity check the address (offset in section). */
07515404 1156 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
4c72e23a
CM
1157 return bfd_reloc_outofrange;
1158
1159 ret = bfd_reloc_ok;
1160 if (bfd_is_und_section (symbol->section)
1161 && output_bfd == (bfd *) NULL)
1162 ret = bfd_reloc_undefined;
1163
1164 if (bfd_is_com_section (symbol->section)
1165 || output_bfd != (bfd *) NULL)
1166 relocation = 0;
1167 else
1168 relocation = symbol->value;
1169
1170 /* Only do this for a final link. */
1171 if (output_bfd == (bfd *) NULL)
1172 {
1173 relocation += symbol->section->output_section->vma;
1174 relocation += symbol->section->output_offset;
1175 }
1176
1177 relocation += reloc_entry->addend;
23ccc829 1178 inplace_address = (bfd_byte *) data + reloc_entry->address;
8b125cde
DE
1179
1180#define DOIT(x) \
1181 x = ( (x & ~reloc_entry->howto->dst_mask) | \
1182 (((x & reloc_entry->howto->src_mask) + relocation) & \
1183 reloc_entry->howto->dst_mask))
4c72e23a 1184
8b125cde
DE
1185 switch (reloc_entry->howto->size)
1186 {
1187 case 1:
1188 {
1189 short x = bfd_get_16 (input_bfd, inplace_address);
1190 DOIT (x);
dc810e39 1191 bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
8b125cde
DE
1192 }
1193 break;
1194 case 2:
1195 {
1196 unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1197 DOIT (x);
dc810e39 1198 bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
8b125cde
DE
1199 }
1200 break;
1201 default:
1202 BFD_ASSERT (0);
1203 }
4c72e23a
CM
1204
1205 if (output_bfd != (bfd *) NULL)
1206 reloc_entry->address += input_section->output_offset;
1207
1208 return ret;
252b5132
RH
1209}
1210
1211/* Handle the R_M32R_SDA16 reloc.
1212 This reloc is used to compute the address of objects in the small data area
1213 and to perform loads and stores from that area.
1214 The lower 16 bits are sign extended and added to the register specified
1215 in the instruction, which is assumed to point to _SDA_BASE_. */
1216
1217static bfd_reloc_status_type
1218m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1219 input_section, output_bfd, error_message)
5f771d47 1220 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1221 arelent *reloc_entry;
1222 asymbol *symbol;
5f771d47 1223 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
1224 asection *input_section;
1225 bfd *output_bfd;
5f771d47 1226 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
1227{
1228 /* This part is from bfd_elf_generic_reloc. */
1229 if (output_bfd != (bfd *) NULL
1230 && (symbol->flags & BSF_SECTION_SYM) == 0
1231 && (! reloc_entry->howto->partial_inplace
1232 || reloc_entry->addend == 0))
1233 {
1234 reloc_entry->address += input_section->output_offset;
1235 return bfd_reloc_ok;
1236 }
1237
1238 if (output_bfd != NULL)
1239 {
1240 /* FIXME: See bfd_perform_relocation. Is this right? */
1241 return bfd_reloc_continue;
1242 }
1243
1244 /* FIXME: not sure what to do here yet. But then again, the linker
1245 may never call us. */
1246 abort ();
1247}
1248\f
1249/* Map BFD reloc types to M32R ELF reloc types. */
1250
1251struct m32r_reloc_map
1252{
1253 bfd_reloc_code_real_type bfd_reloc_val;
1254 unsigned char elf_reloc_val;
1255};
1256
d5b2f13b 1257#ifdef USE_M32R_OLD_RELOC
6edf0760 1258static const struct m32r_reloc_map m32r_reloc_map_old[] =
252b5132
RH
1259{
1260 { BFD_RELOC_NONE, R_M32R_NONE },
1261 { BFD_RELOC_16, R_M32R_16 },
1262 { BFD_RELOC_32, R_M32R_32 },
1263 { BFD_RELOC_M32R_24, R_M32R_24 },
1264 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
1265 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
1266 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
1267 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
1268 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
1269 { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
1270 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
1271 { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
1272 { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
1273};
d5b2f13b 1274#else
6edf0760
NC
1275static const struct m32r_reloc_map m32r_reloc_map[] =
1276{
1277 { BFD_RELOC_NONE, R_M32R_NONE },
1278 { BFD_RELOC_16, R_M32R_16_RELA },
1279 { BFD_RELOC_32, R_M32R_32_RELA },
1280 { BFD_RELOC_M32R_24, R_M32R_24_RELA },
1281 { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
1282 { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
1283 { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
1284 { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
1285 { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
1286 { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
1287 { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
1288 { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
1289 { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
41978308 1290
6edf0760
NC
1291 { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
1292 { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
1293 { BFD_RELOC_M32R_COPY, R_M32R_COPY },
1294 { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
1295 { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
1296 { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
1297 { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
1298 { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
1299 { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
1300 { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
1301 { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
1302 { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
1303 { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
1304 { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
097f809a
NC
1305 { BFD_RELOC_M32R_GOTOFF_HI_ULO, R_M32R_GOTOFF_HI_ULO },
1306 { BFD_RELOC_M32R_GOTOFF_HI_SLO, R_M32R_GOTOFF_HI_SLO },
1307 { BFD_RELOC_M32R_GOTOFF_LO, R_M32R_GOTOFF_LO },
6edf0760 1308};
d5b2f13b 1309#endif
6edf0760 1310
252b5132
RH
1311static reloc_howto_type *
1312bfd_elf32_bfd_reloc_type_lookup (abfd, code)
5f771d47 1313 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1314 bfd_reloc_code_real_type code;
1315{
1316 unsigned int i;
1317
6edf0760
NC
1318#ifdef USE_M32R_OLD_RELOC
1319 for (i = 0;
1320 i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
1321 i++)
1322 {
1323 if (m32r_reloc_map_old[i].bfd_reloc_val == code)
1324 return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
1325 }
1326#else /* ! USE_M32R_OLD_RELOC */
1327
252b5132
RH
1328 for (i = 0;
1329 i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
1330 i++)
1331 {
1332 if (m32r_reloc_map[i].bfd_reloc_val == code)
1333 return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
1334 }
6edf0760 1335#endif
252b5132
RH
1336
1337 return NULL;
1338}
1339
1340/* Set the howto pointer for an M32R ELF reloc. */
1341
1342static void
1343m32r_info_to_howto_rel (abfd, cache_ptr, dst)
5f771d47 1344 bfd *abfd ATTRIBUTE_UNUSED;
252b5132 1345 arelent *cache_ptr;
947216bf 1346 Elf_Internal_Rela *dst;
252b5132
RH
1347{
1348 unsigned int r_type;
1349
1350 r_type = ELF32_R_TYPE (dst->r_info);
f12123c0 1351 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY);
252b5132
RH
1352 cache_ptr->howto = &m32r_elf_howto_table[r_type];
1353}
6edf0760
NC
1354
1355static void
1356m32r_info_to_howto (abfd, cache_ptr, dst)
1357 bfd *abfd ATTRIBUTE_UNUSED;
1358 arelent *cache_ptr;
1359 Elf_Internal_Rela *dst;
1360{
1361 BFD_ASSERT ((ELF32_R_TYPE(dst->r_info) == (unsigned int) R_M32R_NONE)
1362 || ((ELF32_R_TYPE(dst->r_info) > (unsigned int) R_M32R_GNU_VTENTRY)
1363 && (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_M32R_max)));
1364 cache_ptr->howto = &m32r_elf_howto_table[ELF32_R_TYPE(dst->r_info)];
1365}
1366
252b5132
RH
1367\f
1368/* Given a BFD section, try to locate the corresponding ELF section
1369 index. */
1370
b34976b6 1371bfd_boolean
af746e92 1372_bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
5f771d47 1373 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1374 asection *sec;
1375 int *retval;
1376{
1377 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1378 {
1379 *retval = SHN_M32R_SCOMMON;
b34976b6 1380 return TRUE;
252b5132 1381 }
b34976b6 1382 return FALSE;
252b5132
RH
1383}
1384
1385/* M32R ELF uses two common sections. One is the usual one, and the other
1386 is for small objects. All the small objects are kept together, and then
1387 referenced via one register, which yields faster assembler code. It is
1388 up to the compiler to emit an instruction to load the register with
1389 _SDA_BASE. This is what we use for the small common section. This
1390 approach is copied from elf32-mips.c. */
1391static asection m32r_elf_scom_section;
1392static asymbol m32r_elf_scom_symbol;
1393static asymbol *m32r_elf_scom_symbol_ptr;
1394
1395/* Handle the special M32R section numbers that a symbol may use. */
1396
1397void
1398_bfd_m32r_elf_symbol_processing (abfd, asym)
5f771d47 1399 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1400 asymbol *asym;
1401{
1402 elf_symbol_type *elfsym;
1403
1404 elfsym = (elf_symbol_type *) asym;
1405
1406 switch (elfsym->internal_elf_sym.st_shndx)
1407 {
1408 case SHN_M32R_SCOMMON:
1409 if (m32r_elf_scom_section.name == NULL)
1410 {
1411 /* Initialize the small common section. */
1412 m32r_elf_scom_section.name = ".scommon";
1413 m32r_elf_scom_section.flags = SEC_IS_COMMON;
1414 m32r_elf_scom_section.output_section = &m32r_elf_scom_section;
1415 m32r_elf_scom_section.symbol = &m32r_elf_scom_symbol;
1416 m32r_elf_scom_section.symbol_ptr_ptr = &m32r_elf_scom_symbol_ptr;
1417 m32r_elf_scom_symbol.name = ".scommon";
1418 m32r_elf_scom_symbol.flags = BSF_SECTION_SYM;
1419 m32r_elf_scom_symbol.section = &m32r_elf_scom_section;
1420 m32r_elf_scom_symbol_ptr = &m32r_elf_scom_symbol;
1421 }
1422 asym->section = &m32r_elf_scom_section;
1423 asym->value = elfsym->internal_elf_sym.st_size;
1424 break;
1425 }
1426}
1427
1428/* Hook called by the linker routine which adds symbols from an object
1429 file. We must handle the special M32R section numbers here.
1430 We also keep watching for whether we need to create the sdata special
1431 linker sections. */
1432
b34976b6 1433static bfd_boolean
252b5132
RH
1434m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1435 bfd *abfd;
1436 struct bfd_link_info *info;
555cd476 1437 Elf_Internal_Sym *sym;
252b5132 1438 const char **namep;
5f771d47 1439 flagword *flagsp ATTRIBUTE_UNUSED;
252b5132
RH
1440 asection **secp;
1441 bfd_vma *valp;
1442{
1049f94e 1443 if (! info->relocatable
252b5132 1444 && (*namep)[0] == '_' && (*namep)[1] == 'S'
4ab82700 1445 && strcmp (*namep, "_SDA_BASE_") == 0
0eddce27 1446 && is_elf_hash_table (info->hash))
252b5132
RH
1447 {
1448 /* This is simpler than using _bfd_elf_create_linker_section
1449 (our needs are simpler than ppc's needs). Also
1450 _bfd_elf_create_linker_section currently has a bug where if a .sdata
1451 section already exists a new one is created that follows it which
1452 screws of _SDA_BASE_ address calcs because output_offset != 0. */
1453 struct elf_link_hash_entry *h;
14a793b2 1454 struct bfd_link_hash_entry *bh;
252b5132
RH
1455 asection *s = bfd_get_section_by_name (abfd, ".sdata");
1456
6edf0760
NC
1457 /* The following code was cobbled from elf32-ppc.c and elflink.c. */
1458
1459 if (s == NULL)
1460 {
1461 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1462 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1463
3496cb2a
L
1464 s = bfd_make_section_anyway_with_flags (abfd, ".sdata",
1465 flags);
6edf0760
NC
1466 if (s == NULL)
1467 return FALSE;
6edf0760
NC
1468 bfd_set_section_alignment (abfd, s, 2);
1469 }
1470
1471 bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
1472 FALSE, FALSE, FALSE);
1473
1474 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1475 && !(_bfd_generic_link_add_one_symbol (info,
1476 abfd,
1477 "_SDA_BASE_",
1478 BSF_GLOBAL,
1479 s,
1480 (bfd_vma) 32768,
1481 (const char *) NULL,
1482 FALSE,
1483 get_elf_backend_data (abfd)->collect,
1484 &bh)))
1485 return FALSE;
1486 h = (struct elf_link_hash_entry *) bh;
1487 h->type = STT_OBJECT;
1488 }
1489
1490 switch (sym->st_shndx)
1491 {
1492 case SHN_M32R_SCOMMON:
1493 *secp = bfd_make_section_old_way (abfd, ".scommon");
1494 (*secp)->flags |= SEC_IS_COMMON;
1495 *valp = sym->st_size;
1496 break;
1497 }
1498
1499 return TRUE;
1500}
1501
1502/* We have to figure out the SDA_BASE value, so that we can adjust the
1503 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
1504 BFD. If we can't find it, we're stuck. We cache it in the ELF
1505 target data. We don't need to adjust the symbol value for an
1506 external symbol if we are producing relocatable output. */
1507
1508static bfd_reloc_status_type
1509m32r_elf_final_sda_base (output_bfd, info, error_message, psb)
1510 bfd *output_bfd;
1511 struct bfd_link_info *info;
1512 const char **error_message;
1513 bfd_vma *psb;
1514{
1515 if (elf_gp (output_bfd) == 0)
1516 {
1517 struct bfd_link_hash_entry *h;
1518
1519 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
1520 if (h != (struct bfd_link_hash_entry *) NULL
1521 && h->type == bfd_link_hash_defined)
1522 elf_gp (output_bfd) = (h->u.def.value
1523 + h->u.def.section->output_section->vma
1524 + h->u.def.section->output_offset);
1525 else
1526 {
1527 /* Only get the error once. */
1528 *psb = elf_gp (output_bfd) = 4;
1529 *error_message =
1530 (const char *) _("SDA relocation when _SDA_BASE_ not defined");
1531 return bfd_reloc_dangerous;
1532 }
1533 }
1534 *psb = elf_gp (output_bfd);
1535 return bfd_reloc_ok;
1536}
1537\f
1538/* Return size of a PLT entry. */
1539#define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
1540
1541/* The m32r linker needs to keep track of the number of relocs that it
1542 decides to copy in check_relocs for each symbol. This is so that
1543 it can discard PC relative relocs if it doesn't need them when
1544 linking with -Bsymbolic. We store the information in a field
1545 extending the regular ELF linker hash table. */
1546
1547/* This structure keeps track of the number of PC relative relocs we
1548 have copied for a given symbol. */
1549
1550struct elf_m32r_pcrel_relocs_copied
1551{
1552 /* Next section. */
1553 struct elf_m32r_pcrel_relocs_copied *next;
1554 /* A section in dynobj. */
1555 asection *section;
1556 /* Number of relocs copied in this section. */
1557 bfd_size_type count;
1558};
1559
1560/* The sh linker needs to keep track of the number of relocs that it
1561 decides to copy as dynamic relocs in check_relocs for each symbol.
1562 This is so that it can later discard them if they are found to be
1563 unnecessary. We store the information in a field extending the
1564 regular ELF linker hash table. */
1565
1566struct elf_m32r_dyn_relocs
1567{
1568 struct elf_m32r_dyn_relocs *next;
1569
1570 /* The input section of the reloc. */
1571 asection *sec;
1572
1573 /* Total number of relocs copied for the input section. */
1574 bfd_size_type count;
1575
1576 /* Number of pc-relative relocs copied for the input section. */
1577 bfd_size_type pc_count;
1578};
1579
1580
1581/* m32r ELF linker hash entry. */
1582
1583struct elf_m32r_link_hash_entry
1584{
1585 struct elf_link_hash_entry root;
1586
1587 /* Track dynamic relocs copied for this symbol. */
1588 struct elf_m32r_dyn_relocs *dyn_relocs;
1589
1590// bfd_signed_vma gotplt_refcount;
1591
1592 /* Number of PC relative relocs copied for this symbol. */
1593 /* struct elf_m32r_pcrel_relocs_copied *pcrel_relocs_copied; FIXME */
1594};
1595
1596/* m32r ELF linker hash table. */
1597
1598struct elf_m32r_link_hash_table
1599{
1600 struct elf_link_hash_table root;
1601
1602 /* Short-cuts to get to dynamic linker sections. */
1603 asection *sgot;
1604 asection *sgotplt;
1605 asection *srelgot;
1606 asection *splt;
1607 asection *srelplt;
1608 asection *sdynbss;
1609 asection *srelbss;
1610
1611 /* Small local sym to section mapping cache. */
1612 struct sym_sec_cache sym_sec;
1613};
1614
1615/* Traverse an m32r ELF linker hash table. */
1616
1617#define m32r_elf_link_hash_traverse(table, func, info) \
1618 (elf_link_hash_traverse \
1619 (&(table)->root, \
1620 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1621 (info)))
1622
1623/* Get the m32r ELF linker hash table from a link_info structure. */
1624
1625
1626#define m32r_elf_hash_table(p) \
1627 ((struct elf_m32r_link_hash_table *) ((p)->hash))
1628
1629/* Create an entry in an m32r ELF linker hash table. */
1630static struct bfd_hash_entry *
1631m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1632 const char * );
1633
1634static struct bfd_hash_entry *
1635m32r_elf_link_hash_newfunc (entry, table, string)
1636 struct bfd_hash_entry *entry;
1637 struct bfd_hash_table *table;
1638 const char *string;
1639{
1640 struct elf_m32r_link_hash_entry *ret =
1641 (struct elf_m32r_link_hash_entry *) entry;
41978308 1642
6edf0760
NC
1643 /* Allocate the structure if it has not already been allocated by a
1644 subclass. */
1645 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1646 ret = ((struct elf_m32r_link_hash_entry *)
1647 bfd_hash_allocate (table,
1648 sizeof (struct elf_m32r_link_hash_entry)));
1649 if (ret == (struct elf_m32r_link_hash_entry *) NULL)
1650 return (struct bfd_hash_entry *) ret;
41978308 1651
6edf0760
NC
1652 /* Call the allocation method of the superclass. */
1653 ret = ((struct elf_m32r_link_hash_entry *)
1654 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1655 table, string));
1656 if (ret != (struct elf_m32r_link_hash_entry *) NULL)
1657 {
1658 struct elf_m32r_link_hash_entry *eh;
41978308 1659
6edf0760
NC
1660 eh = (struct elf_m32r_link_hash_entry *) ret;
1661 eh->dyn_relocs = NULL;
1662// eh->gotplt_refcount = 0;
1663 /* eh->pcrel_relocs_copied = NULL; FIXME */
1664 }
41978308 1665
6edf0760
NC
1666 return (struct bfd_hash_entry *) ret;
1667}
1668
1669/* Create an m32r ELF linker hash table. */
1670static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1671
1672static struct bfd_link_hash_table *
1673m32r_elf_link_hash_table_create (abfd)
1674 bfd *abfd;
1675{
1676 struct elf_m32r_link_hash_table *ret;
1677 bfd_size_type amt = sizeof (struct elf_m32r_link_hash_table);
41978308 1678
6edf0760
NC
1679 ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1680 if (ret == (struct elf_m32r_link_hash_table *) NULL)
1681 return NULL;
41978308 1682
6edf0760
NC
1683 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1684 m32r_elf_link_hash_newfunc))
1685 {
1686 free (ret);
1687 return NULL;
1688 }
41978308 1689
6edf0760
NC
1690 ret->sgot = NULL;
1691 ret->sgotplt = NULL;
1692 ret->srelgot = NULL;
1693 ret->splt = NULL;
1694 ret->srelplt = NULL;
1695 ret->sdynbss = NULL;
1696 ret->srelbss = NULL;
1697 ret->sym_sec.abfd = NULL;
41978308 1698
6edf0760
NC
1699 return &ret->root.root;
1700}
1701
1702/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1703 shortcuts to them in our hash table. */
1704static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1705
1706static bfd_boolean
1707create_got_section (dynobj, info)
1708 bfd *dynobj;
1709 struct bfd_link_info *info;
1710{
1711 struct elf_m32r_link_hash_table *htab;
1712
1713 if (! _bfd_elf_create_got_section (dynobj, info))
1714 return FALSE;
1715
1716 htab = m32r_elf_hash_table (info);
1717 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1718 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1719 if (! htab->sgot || ! htab->sgotplt)
1720 abort ();
1721
3496cb2a
L
1722 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
1723 (SEC_ALLOC
1724 | SEC_LOAD
1725 | SEC_HAS_CONTENTS
1726 | SEC_IN_MEMORY
1727 | SEC_LINKER_CREATED
1728 | SEC_READONLY));
6edf0760 1729 if (htab->srelgot == NULL
6edf0760
NC
1730 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1731 return FALSE;
1732
1733 return TRUE;
1734}
1735
1736/* Create dynamic sections when linking against a dynamic object. */
1737
1738static bfd_boolean
1739m32r_elf_create_dynamic_sections (abfd, info)
1740 bfd *abfd;
1741 struct bfd_link_info *info;
1742{
1743 struct elf_m32r_link_hash_table *htab;
1744 flagword flags, pltflags;
1745 register asection *s;
1746 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1747 int ptralign = 2; /* 32bit */
1748
1749 htab = m32r_elf_hash_table (info);
1750
1751 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1752 .rel[a].bss sections. */
1753
1754 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1755 | SEC_LINKER_CREATED);
1756
1757 pltflags = flags;
1758 pltflags |= SEC_CODE;
1759 if (bed->plt_not_loaded)
1760 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1761 if (bed->plt_readonly)
1762 pltflags |= SEC_READONLY;
1763
3496cb2a 1764 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
6edf0760
NC
1765 htab->splt = s;
1766 if (s == NULL
6edf0760
NC
1767 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1768 return FALSE;
1769
1770 if (bed->want_plt_sym)
1771 {
1772 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1773 .plt section. */
41978308
AM
1774 struct bfd_link_hash_entry *bh = NULL;
1775 struct elf_link_hash_entry *h;
6edf0760
NC
1776 if (! (_bfd_generic_link_add_one_symbol
1777 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1778 (bfd_vma) 0, (const char *) NULL, FALSE,
41978308 1779 get_elf_backend_data (abfd)->collect, &bh)))
6edf0760 1780 return FALSE;
41978308 1781 h = (struct elf_link_hash_entry *) bh;
f5385ebf 1782 h->def_regular = 1;
6edf0760
NC
1783 h->type = STT_OBJECT;
1784
1785 if (info->shared
c152c796 1786 && ! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
1787 return FALSE;
1788 }
1789
3496cb2a
L
1790 s = bfd_make_section_with_flags (abfd,
1791 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt",
1792 flags | SEC_READONLY);
6edf0760
NC
1793 htab->srelplt = s;
1794 if (s == NULL
6edf0760
NC
1795 || ! bfd_set_section_alignment (abfd, s, ptralign))
1796 return FALSE;
1797
1798 if (htab->sgot == NULL
1799 && ! create_got_section (abfd, info))
1800 return FALSE;
1801
1802 {
1803 const char *secname;
1804 char *relname;
1805 flagword secflags;
1806 asection *sec;
1807
1808 for (sec = abfd->sections; sec; sec = sec->next)
1809 {
1810 secflags = bfd_get_section_flags (abfd, sec);
1811 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1812 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1813 continue;
1814 secname = bfd_get_section_name (abfd, sec);
1815 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1816 strcpy (relname, ".rela");
1817 strcat (relname, secname);
1818 if (bfd_get_section_by_name (abfd, secname))
1819 continue;
3496cb2a
L
1820 s = bfd_make_section_with_flags (abfd, relname,
1821 flags | SEC_READONLY);
6edf0760 1822 if (s == NULL
6edf0760
NC
1823 || ! bfd_set_section_alignment (abfd, s, ptralign))
1824 return FALSE;
1825 }
1826 }
1827
1828 if (bed->want_dynbss)
1829 {
1830 /* The .dynbss section is a place to put symbols which are defined
1831 by dynamic objects, are referenced by regular objects, and are
1832 not functions. We must allocate space for them in the process
1833 image and use a R_*_COPY reloc to tell the dynamic linker to
1834 initialize them at run time. The linker script puts the .dynbss
1835 section into the .bss section of the final image. */
3496cb2a
L
1836 s = bfd_make_section_with_flags (abfd, ".dynbss",
1837 SEC_ALLOC | SEC_LINKER_CREATED);
6edf0760 1838 htab->sdynbss = s;
3496cb2a 1839 if (s == NULL)
6edf0760
NC
1840 return FALSE;
1841 /* The .rel[a].bss section holds copy relocs. This section is not
1842 normally needed. We need to create it here, though, so that the
1843 linker will map it to an output section. We can't just create it
1844 only if we need it, because we will not know whether we need it
1845 until we have seen all the input files, and the first time the
1846 main linker code calls BFD after examining all the input files
1847 (size_dynamic_sections) the input sections have already been
1848 mapped to the output sections. If the section turns out not to
1849 be needed, we can discard it later. We will never need this
1850 section when generating a shared object, since they do not use
1851 copy relocs. */
1852 if (! info->shared)
1853 {
3496cb2a
L
1854 s = bfd_make_section_with_flags (abfd,
1855 (bed->default_use_rela_p
1856 ? ".rela.bss" : ".rel.bss"),
1857 flags | SEC_READONLY);
6edf0760
NC
1858 htab->srelbss = s;
1859 if (s == NULL
6edf0760
NC
1860 || ! bfd_set_section_alignment (abfd, s, ptralign))
1861 return FALSE;
1862 }
1863 }
1864
1865 return TRUE;
1866}
1867
1868/* Copy the extra info we tack onto an elf_link_hash_entry. */
1869static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1870 struct elf_link_hash_entry *,
1871 struct elf_link_hash_entry *);
41978308 1872
6edf0760
NC
1873static void
1874m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1875 struct elf_link_hash_entry *dir,
1876 struct elf_link_hash_entry *ind)
1877{
1878 struct elf_m32r_link_hash_entry *edir, *eind;
41978308 1879
6edf0760
NC
1880 edir = (struct elf_m32r_link_hash_entry *) dir;
1881 eind = (struct elf_m32r_link_hash_entry *) ind;
41978308 1882
6edf0760
NC
1883 if (eind->dyn_relocs != NULL)
1884 {
1885 if (edir->dyn_relocs != NULL)
1886 {
1887 struct elf_m32r_dyn_relocs **pp;
1888 struct elf_m32r_dyn_relocs *p;
41978308 1889
6edf0760
NC
1890 if (ind->root.type == bfd_link_hash_indirect)
1891 abort ();
41978308 1892
6edf0760
NC
1893 /* Add reloc counts against the weak sym to the strong sym
1894 list. Merge any entries against the same section. */
1895 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1896 {
1897 struct elf_m32r_dyn_relocs *q;
41978308 1898
6edf0760
NC
1899 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1900 if (q->sec == p->sec)
1901 {
1902 q->pc_count += p->pc_count;
1903 q->count += p->count;
1904 *pp = p->next;
1905 break;
1906 }
1907 if (q == NULL)
1908 pp = &p->next;
1909 }
1910 *pp = edir->dyn_relocs;
1911 }
41978308 1912
6edf0760
NC
1913 edir->dyn_relocs = eind->dyn_relocs;
1914 eind->dyn_relocs = NULL;
1915 }
41978308 1916
6edf0760
NC
1917// if (ind->root.type == bfd_link_hash_indirect
1918// && dir->got.refcount <= 0)
1919// {
1920// edir->tls_type = eind->tls_type;
1921// eind->tls_type = GOT_UNKNOWN;
1922// }
1923 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1924}
1925
1926\f
1927/* Adjust a symbol defined by a dynamic object and referenced by a
1928 regular object. The current definition is in some section of the
1929 dynamic object, but we're not including those sections. We have to
1930 change the definition to something the rest of the link can
1931 understand. */
41978308 1932
6edf0760
NC
1933static bfd_boolean
1934m32r_elf_adjust_dynamic_symbol (info, h)
1935 struct bfd_link_info *info;
1936 struct elf_link_hash_entry *h;
1937{
1938 struct elf_m32r_link_hash_table *htab;
1939 struct elf_m32r_link_hash_entry *eh;
1940 struct elf_m32r_dyn_relocs *p;
1941 bfd *dynobj;
1942 asection *s;
1943 unsigned int power_of_two;
1944
1945#ifdef DEBUG_PIC
1946printf("m32r_elf_adjust_dynamic_symbol()\n");
1947#endif
41978308 1948
6edf0760 1949 dynobj = elf_hash_table (info)->dynobj;
41978308 1950
6edf0760
NC
1951 /* Make sure we know what is going on here. */
1952 BFD_ASSERT (dynobj != NULL
f5385ebf 1953 && (h->needs_plt
f6e332e6 1954 || h->u.weakdef != NULL
f5385ebf
AM
1955 || (h->def_dynamic
1956 && h->ref_regular
1957 && !h->def_regular)));
6edf0760 1958
41978308 1959
6edf0760
NC
1960 /* If this is a function, put it in the procedure linkage table. We
1961 will fill in the contents of the procedure linkage table later,
1962 when we know the address of the .got section. */
1963 if (h->type == STT_FUNC
f5385ebf 1964 || h->needs_plt)
6edf0760
NC
1965 {
1966 if (! info->shared
f5385ebf
AM
1967 && !h->def_dynamic
1968 && !h->ref_dynamic
6edf0760
NC
1969 && h->root.type != bfd_link_hash_undefweak
1970 && h->root.type != bfd_link_hash_undefined)
1971 {
1972 /* This case can occur if we saw a PLT reloc in an input
1973 file, but the symbol was never referred to by a dynamic
1974 object. In such a case, we don't actually need to build
1975 a procedure linkage table, and we can just do a PCREL
1976 reloc instead. */
1977 h->plt.offset = (bfd_vma) -1;
f5385ebf 1978 h->needs_plt = 0;
6edf0760
NC
1979 }
1980
1981 return TRUE;
1982 }
1983 else
1984 h->plt.offset = (bfd_vma) -1;
41978308 1985
6edf0760
NC
1986 /* If this is a weak symbol, and there is a real definition, the
1987 processor independent code will have arranged for us to see the
1988 real definition first, and we can just use the same value. */
f6e332e6 1989 if (h->u.weakdef != NULL)
6edf0760 1990 {
f6e332e6
AM
1991 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1992 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1993 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1994 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6edf0760
NC
1995 return TRUE;
1996 }
41978308 1997
6edf0760
NC
1998 /* This is a reference to a symbol defined by a dynamic object which
1999 is not a function. */
41978308 2000
6edf0760
NC
2001 /* If we are creating a shared library, we must presume that the
2002 only references to the symbol are via the global offset table.
2003 For such cases we need not do anything here; the relocations will
2004 be handled correctly by relocate_section. */
2005 if (info->shared)
2006 return TRUE;
41978308 2007
6edf0760
NC
2008 /* If there are no references to this symbol that do not use the
2009 GOT, we don't need to generate a copy reloc. */
f5385ebf 2010 if (!h->non_got_ref)
6edf0760
NC
2011 return TRUE;
2012
2013 /* If -z nocopyreloc was given, we won't generate them either. */
2014 if (info->nocopyreloc)
2015 {
f5385ebf 2016 h->non_got_ref = 0;
6edf0760
NC
2017 return TRUE;
2018 }
2019
2020 eh = (struct elf_m32r_link_hash_entry *) h;
2021 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2022 {
2023 s = p->sec->output_section;
2024 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2025 break;
2026 }
2027
2028 /* If we didn't find any dynamic relocs in sections which needs the
2029 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2030 the copy reloc. */
2031 if (p == NULL)
2032 {
f5385ebf 2033 h->non_got_ref = 0;
6edf0760
NC
2034 return TRUE;
2035 }
2036
2037 /* We must allocate the symbol in our .dynbss section, which will
2038 become part of the .bss section of the executable. There will be
2039 an entry for this symbol in the .dynsym section. The dynamic
2040 object will contain position independent code, so all references
2041 from the dynamic object to this symbol will go through the global
2042 offset table. The dynamic linker will use the .dynsym entry to
2043 determine the address it must put in the global offset table, so
2044 both the dynamic object and the regular object will refer to the
2045 same memory location for the variable. */
41978308 2046
6edf0760
NC
2047 htab = m32r_elf_hash_table (info);
2048 s = htab->sdynbss;
2049 BFD_ASSERT (s != NULL);
41978308 2050
6edf0760
NC
2051 /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2052 to copy the initial value out of the dynamic object and into the
2053 runtime process image. We need to remember the offset into the
2054 .rela.bss section we are going to use. */
2055 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2056 {
2057 asection *srel;
41978308 2058
6edf0760
NC
2059 srel = htab->srelbss;
2060 BFD_ASSERT (srel != NULL);
eea6121a 2061 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 2062 h->needs_copy = 1;
6edf0760 2063 }
41978308 2064
6edf0760
NC
2065 /* We need to figure out the alignment required for this symbol. I
2066 have no idea how ELF linkers handle this. */
2067 power_of_two = bfd_log2 (h->size);
2068 if (power_of_two > 3)
2069 power_of_two = 3;
41978308 2070
6edf0760 2071 /* Apply the required alignment. */
eea6121a 2072 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
6edf0760
NC
2073 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2074 {
2075 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2076 return FALSE;
2077 }
41978308 2078
6edf0760
NC
2079 /* Define the symbol as being at this point in the section. */
2080 h->root.u.def.section = s;
eea6121a 2081 h->root.u.def.value = s->size;
41978308 2082
6edf0760 2083 /* Increment the section size to make room for the symbol. */
eea6121a 2084 s->size += h->size;
41978308 2085
6edf0760
NC
2086 return TRUE;
2087}
2088
6edf0760
NC
2089/* Allocate space in .plt, .got and associated reloc sections for
2090 dynamic relocs. */
41978308 2091
6edf0760
NC
2092static bfd_boolean
2093allocate_dynrelocs (h, inf)
2094 struct elf_link_hash_entry *h;
2095 PTR inf;
2096{
2097 struct bfd_link_info *info;
2098 struct elf_m32r_link_hash_table *htab;
2099 struct elf_m32r_link_hash_entry *eh;
2100 struct elf_m32r_dyn_relocs *p;
41978308 2101
6edf0760
NC
2102 if (h->root.type == bfd_link_hash_indirect)
2103 return TRUE;
41978308 2104
6edf0760
NC
2105 if (h->root.type == bfd_link_hash_warning)
2106 /* When warning symbols are created, they **replace** the "real"
2107 entry in the hash table, thus we never get to see the real
2108 symbol in a hash traversal. So look at it now. */
2109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
41978308 2110
6edf0760
NC
2111 info = (struct bfd_link_info *) inf;
2112 htab = m32r_elf_hash_table (info);
41978308 2113
6edf0760
NC
2114 eh = (struct elf_m32r_link_hash_entry *) h;
2115// if ((h->got.refcount > 0
f5385ebf 2116// || h->forced_local)
6edf0760
NC
2117// && eh->gotplt_refcount > 0)
2118// {
2119// /* The symbol has been forced local, or we have some direct got refs,
2120// so treat all the gotplt refs as got refs. */
2121// h->got.refcount += eh->gotplt_refcount;
2122// if (h->plt.refcount >= eh->gotplt_refcount)
2123// h->plt.refcount -= eh->gotplt_refcount;
2124// }
41978308 2125
6edf0760
NC
2126 if (htab->root.dynamic_sections_created
2127 && h->plt.refcount > 0)
2128 {
2129 /* Make sure this symbol is output as a dynamic symbol.
2130 Undefined weak syms won't yet be marked as dynamic. */
2131 if (h->dynindx == -1
f5385ebf 2132 && !h->forced_local)
6edf0760 2133 {
c152c796 2134 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
2135 return FALSE;
2136 }
41978308 2137
c152c796 2138 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
6edf0760
NC
2139 {
2140 asection *s = htab->splt;
41978308 2141
6edf0760
NC
2142 /* If this is the first .plt entry, make room for the special
2143 first entry. */
eea6121a
AM
2144 if (s->size == 0)
2145 s->size += PLT_ENTRY_SIZE;
41978308 2146
eea6121a 2147 h->plt.offset = s->size;
41978308 2148
6edf0760
NC
2149 /* If this symbol is not defined in a regular file, and we are
2150 not generating a shared library, then set the symbol to this
2151 location in the .plt. This is required to make function
2152 pointers compare as equal between the normal executable and
2153 the shared library. */
2154 if (! info->shared
f5385ebf 2155 && !h->def_regular)
6edf0760
NC
2156 {
2157 h->root.u.def.section = s;
2158 h->root.u.def.value = h->plt.offset;
2159 }
41978308 2160
6edf0760 2161 /* Make room for this entry. */
eea6121a 2162 s->size += PLT_ENTRY_SIZE;
41978308 2163
6edf0760
NC
2164 /* We also need to make an entry in the .got.plt section, which
2165 will be placed in the .got section by the linker script. */
eea6121a 2166 htab->sgotplt->size += 4;
41978308 2167
6edf0760 2168 /* We also need to make an entry in the .rel.plt section. */
eea6121a 2169 htab->srelplt->size += sizeof (Elf32_External_Rela);
6edf0760
NC
2170 }
2171 else
2172 {
2173 h->plt.offset = (bfd_vma) -1;
f5385ebf 2174 h->needs_plt = 0;
6edf0760
NC
2175 }
2176 }
2177 else
2178 {
2179 h->plt.offset = (bfd_vma) -1;
f5385ebf 2180 h->needs_plt = 0;
6edf0760 2181 }
41978308 2182
6edf0760
NC
2183 if (h->got.refcount > 0)
2184 {
2185 asection *s;
2186 bfd_boolean dyn;
41978308 2187
6edf0760
NC
2188 /* Make sure this symbol is output as a dynamic symbol.
2189 Undefined weak syms won't yet be marked as dynamic. */
2190 if (h->dynindx == -1
f5385ebf 2191 && !h->forced_local)
6edf0760 2192 {
c152c796 2193 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
2194 return FALSE;
2195 }
41978308 2196
6edf0760
NC
2197 s = htab->sgot;
2198
eea6121a
AM
2199 h->got.offset = s->size;
2200 s->size += 4;
6edf0760 2201 dyn = htab->root.dynamic_sections_created;
c152c796 2202 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
eea6121a 2203 htab->srelgot->size += sizeof (Elf32_External_Rela);
6edf0760
NC
2204 }
2205 else
2206 h->got.offset = (bfd_vma) -1;
41978308 2207
6edf0760
NC
2208 if (eh->dyn_relocs == NULL)
2209 return TRUE;
41978308 2210
6edf0760
NC
2211 /* In the shared -Bsymbolic case, discard space allocated for
2212 dynamic pc-relative relocs against symbols which turn out to be
2213 defined in regular objects. For the normal shared case, discard
2214 space for pc-relative relocs that have become local due to symbol
2215 visibility changes. */
2216
2217 if (info->shared)
2218 {
f5385ebf
AM
2219 if (h->def_regular
2220 && (h->forced_local
6edf0760
NC
2221 || info->symbolic))
2222 {
2223 struct elf_m32r_dyn_relocs **pp;
2224 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2225 {
2226 p->count -= p->pc_count;
2227 p->pc_count = 0;
2228 if (p->count == 0)
2229 *pp = p->next;
2230 else
2231 pp = &p->next;
2232 }
2233 }
2234 }
2235 else
2236 {
2237 /* For the non-shared case, discard space for relocs against
2238 symbols which turn out to need copy relocs or are not
2239 dynamic. */
41978308 2240
f5385ebf
AM
2241 if (!h->non_got_ref
2242 && ((h->def_dynamic
2243 && !h->def_regular)
6edf0760
NC
2244 || (htab->root.dynamic_sections_created
2245 && (h->root.type == bfd_link_hash_undefweak
2246 || h->root.type == bfd_link_hash_undefined))))
2247 {
2248 /* Make sure this symbol is output as a dynamic symbol.
2249 Undefined weak syms won't yet be marked as dynamic. */
2250 if (h->dynindx == -1
f5385ebf 2251 && !h->forced_local)
6edf0760 2252 {
c152c796 2253 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6edf0760
NC
2254 return FALSE;
2255 }
41978308 2256
6edf0760
NC
2257 /* If that succeeded, we know we'll be keeping all the
2258 relocs. */
2259 if (h->dynindx != -1)
2260 goto keep;
2261 }
41978308 2262
6edf0760 2263 eh->dyn_relocs = NULL;
41978308 2264
6edf0760
NC
2265 keep: ;
2266 }
41978308 2267
6edf0760
NC
2268 /* Finally, allocate space. */
2269 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2270 {
2271 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 2272 sreloc->size += p->count * sizeof (Elf32_External_Rela);
6edf0760 2273 }
41978308 2274
6edf0760
NC
2275 return TRUE;
2276}
2277/* Find any dynamic relocs that apply to read-only sections. */
41978308 2278
6edf0760
NC
2279static bfd_boolean
2280readonly_dynrelocs (h, inf)
2281 struct elf_link_hash_entry *h;
2282 PTR inf;
2283{
2284 struct elf_m32r_link_hash_entry *eh;
2285 struct elf_m32r_dyn_relocs *p;
2286
2287 if (h->root.type == bfd_link_hash_warning)
2288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
252b5132 2289
6edf0760
NC
2290 eh = (struct elf_m32r_link_hash_entry *) h;
2291 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2292 {
2293 asection *s = p->sec->output_section;
252b5132 2294
6edf0760
NC
2295 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2296 {
2297 struct bfd_link_info *info = (struct bfd_link_info *) inf;
252b5132 2298
6edf0760 2299 info->flags |= DF_TEXTREL;
252b5132 2300
6edf0760
NC
2301 /* Not an error, just cut short the traversal. */
2302 return FALSE;
2303 }
252b5132 2304 }
6edf0760
NC
2305 return TRUE;
2306}
252b5132 2307
6edf0760 2308/* Set the sizes of the dynamic sections. */
41978308 2309
6edf0760
NC
2310static bfd_boolean
2311m32r_elf_size_dynamic_sections (output_bfd, info)
2312 bfd *output_bfd ATTRIBUTE_UNUSED;
2313 struct bfd_link_info *info;
2314{
2315 struct elf_m32r_link_hash_table *htab;
2316 bfd *dynobj;
2317 asection *s;
2318 bfd_boolean relocs;
2319 bfd *ibfd;
2320
2321#ifdef DEBUG_PIC
2322printf("m32r_elf_size_dynamic_sections()\n");
2323#endif
2324
2325 htab = m32r_elf_hash_table (info);
2326 dynobj = htab->root.dynobj;
2327 BFD_ASSERT (dynobj != NULL);
2328
2329 if (htab->root.dynamic_sections_created)
252b5132 2330 {
6edf0760
NC
2331 /* Set the contents of the .interp section to the interpreter. */
2332 if (! info->shared)
2333 {
2334 s = bfd_get_section_by_name (dynobj, ".interp");
2335 BFD_ASSERT (s != NULL);
eea6121a 2336 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
6edf0760
NC
2337 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2338 }
252b5132
RH
2339 }
2340
6edf0760
NC
2341 /* Set up .got offsets for local syms, and space for local dynamic
2342 relocs. */
2343 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2344 {
2345 bfd_signed_vma *local_got;
2346 bfd_signed_vma *end_local_got;
2347 bfd_size_type locsymcount;
2348 Elf_Internal_Shdr *symtab_hdr;
2349 asection *srel;
252b5132 2350
6edf0760
NC
2351 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2352 continue;
252b5132 2353
6edf0760
NC
2354 for (s = ibfd->sections; s != NULL; s = s->next)
2355 {
2356 struct elf_m32r_dyn_relocs *p;
2357
2358 for (p = ((struct elf_m32r_dyn_relocs *)
2359 elf_section_data (s)->local_dynrel);
2360 p != NULL;
2361 p = p->next)
2362 {
2363 if (! bfd_is_abs_section (p->sec)
2364 && bfd_is_abs_section (p->sec->output_section))
2365 {
2366 /* Input section has been discarded, either because
2367 it is a copy of a linkonce section or due to
2368 linker script /DISCARD/, so we'll be discarding
2369 the relocs too. */
2370 }
2371 else if (p->count != 0)
2372 {
2373 srel = elf_section_data (p->sec)->sreloc;
eea6121a 2374 srel->size += p->count * sizeof (Elf32_External_Rela);
6edf0760
NC
2375 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2376 info->flags |= DF_TEXTREL;
2377 }
2378 }
2379 }
2380
2381 local_got = elf_local_got_refcounts (ibfd);
2382 if (!local_got)
2383 continue;
2384
2385 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2386 locsymcount = symtab_hdr->sh_info;
2387 end_local_got = local_got + locsymcount;
2388 s = htab->sgot;
2389 srel = htab->srelgot;
2390 for (; local_got < end_local_got; ++local_got)
2391 {
2392 if (*local_got > 0)
2393 {
eea6121a
AM
2394 *local_got = s->size;
2395 s->size += 4;
6edf0760 2396 if (info->shared)
eea6121a 2397 srel->size += sizeof (Elf32_External_Rela);
6edf0760
NC
2398 }
2399 else
2400 *local_got = (bfd_vma) -1;
2401 }
2402 }
2403
2404 /* Allocate global sym .plt and .got entries, and space for global
2405 sym dynamic relocs. */
2406 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2407
2408 /* We now have determined the sizes of the various dynamic sections.
2409 Allocate memory for them. */
2410 relocs = FALSE;
2411 for (s = dynobj->sections; s != NULL; s = s->next)
252b5132 2412 {
6edf0760
NC
2413 if ((s->flags & SEC_LINKER_CREATED) == 0)
2414 continue;
252b5132 2415
6edf0760
NC
2416 if (s == htab->splt
2417 || s == htab->sgot
2418 || s == htab->sgotplt)
2419 {
2420 /* Strip this section if we don't need it; see the
2421 comment below. */
2422 }
2423 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2424 {
eea6121a 2425 if (s->size != 0 && s != htab->srelplt)
6edf0760
NC
2426 relocs = TRUE;
2427
2428 /* We use the reloc_count field as a counter if we need
2429 to copy relocs into the output file. */
2430 s->reloc_count = 0;
2431 }
252b5132 2432 else
6edf0760
NC
2433 {
2434 /* It's not one of our sections, so don't allocate space. */
2435 continue;
2436 }
2437
eea6121a 2438 if (s->size == 0)
6edf0760
NC
2439 {
2440 /* If we don't need this section, strip it from the
2441 output file. This is mostly to handle .rela.bss and
2442 .rela.plt. We must create both sections in
2443 create_dynamic_sections, because they must be created
2444 before the linker maps input sections to output
2445 sections. The linker does that before
2446 adjust_dynamic_symbol is called, and it is that
2447 function which decides whether anything needs to go
2448 into these sections. */
8423293d 2449 s->flags |= SEC_EXCLUDE;
6edf0760
NC
2450 continue;
2451 }
2452
2453 /* Allocate memory for the section contents. We use bfd_zalloc
2454 here in case unused entries are not reclaimed before the
2455 section's contents are written out. This should not happen,
2456 but this way if it does, we get a R_M32R_NONE reloc instead
2457 of garbage. */
eea6121a 2458 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
6edf0760
NC
2459 if (s->contents == NULL)
2460 return FALSE;
2461 }
2462
2463 if (htab->root.dynamic_sections_created)
2464 {
2465 /* Add some entries to the .dynamic section. We fill in the
2466 values later, in m32r_elf_finish_dynamic_sections, but we
2467 must add the entries now so that we get the correct size for
2468 the .dynamic section. The DT_DEBUG entry is filled in by the
2469 dynamic linker and used by the debugger. */
2470#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2471 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
6edf0760
NC
2472
2473 if (! info->shared)
252b5132 2474 {
6edf0760
NC
2475 if (! add_dynamic_entry (DT_DEBUG, 0))
2476 return FALSE;
252b5132 2477 }
6edf0760 2478
eea6121a 2479 if (htab->splt->size != 0)
6edf0760
NC
2480 {
2481 if (! add_dynamic_entry (DT_PLTGOT, 0)
2482 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2483 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2484 || ! add_dynamic_entry (DT_JMPREL, 0))
2485 return FALSE;
2486 }
2487
2488 if (relocs)
2489 {
2490 if (! add_dynamic_entry (DT_RELA, 0)
2491 || ! add_dynamic_entry (DT_RELASZ, 0)
2492 || ! add_dynamic_entry (DT_RELAENT,
2493 sizeof (Elf32_External_Rela)))
2494 return FALSE;
2495
2496 /* If any dynamic relocs apply to a read-only section,
2497 then we need a DT_TEXTREL entry. */
2498 if ((info->flags & DF_TEXTREL) == 0)
2499 elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2500 (PTR) info);
2501
2502 if ((info->flags & DF_TEXTREL) != 0)
2503 {
2504 if (! add_dynamic_entry (DT_TEXTREL, 0))
2505 return FALSE;
2506 }
2507 }
252b5132 2508 }
6edf0760
NC
2509#undef add_dynamic_entry
2510
2511 return TRUE;
252b5132 2512}
252b5132
RH
2513/* Relocate an M32R/D ELF section.
2514 There is some attempt to make this function usable for many architectures,
f0fe0e16 2515 both for RELA and REL type relocs, if only to serve as a learning tool.
252b5132
RH
2516
2517 The RELOCATE_SECTION function is called by the new ELF backend linker
2518 to handle the relocations for a section.
2519
2520 The relocs are always passed as Rela structures; if the section
2521 actually uses Rel structures, the r_addend field will always be
2522 zero.
2523
2524 This function is responsible for adjust the section contents as
2525 necessary, and (if using Rela relocs and generating a
1049f94e 2526 relocatable output file) adjusting the reloc addend as
252b5132
RH
2527 necessary.
2528
2529 This function does not have to worry about setting the reloc
2530 address or the reloc symbol index.
2531
2532 LOCAL_SYMS is a pointer to the swapped in local symbols.
2533
2534 LOCAL_SECTIONS is an array giving the section in the input file
2535 corresponding to the st_shndx field of each local symbol.
2536
2537 The global hash table entry for the global symbols can be found
2538 via elf_sym_hashes (input_bfd).
2539
1049f94e 2540 When generating relocatable output, this function must handle
252b5132
RH
2541 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2542 going to be the section symbol corresponding to the output
2543 section, which means that the addend must be adjusted
2544 accordingly. */
2545
b34976b6 2546static bfd_boolean
252b5132
RH
2547m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2548 contents, relocs, local_syms, local_sections)
5f771d47 2549 bfd *output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
2550 struct bfd_link_info *info;
2551 bfd *input_bfd;
2552 asection *input_section;
2553 bfd_byte *contents;
2554 Elf_Internal_Rela *relocs;
2555 Elf_Internal_Sym *local_syms;
2556 asection **local_sections;
2557{
2558 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2559 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2560 Elf_Internal_Rela *rel, *relend;
2561 /* Assume success. */
b34976b6 2562 bfd_boolean ret = TRUE;
252b5132 2563
6edf0760
NC
2564 struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2565 bfd *dynobj;
2566 bfd_vma *local_got_offsets;
2567 asection *sgot, *splt, *sreloc;
07515404 2568 bfd_vma high_address = bfd_get_section_limit (input_bfd, input_section);
6edf0760
NC
2569
2570 dynobj = htab->root.dynobj;
2571 local_got_offsets = elf_local_got_offsets (input_bfd);
41978308 2572
6edf0760
NC
2573 sgot = htab->sgot;
2574 splt = htab->splt;
2575 sreloc = NULL;
b491616a 2576
252b5132
RH
2577 rel = relocs;
2578 relend = relocs + input_section->reloc_count;
2579 for (; rel < relend; rel++)
2580 {
2581 int r_type;
2582 reloc_howto_type *howto;
2583 unsigned long r_symndx;
6edf0760 2584 struct elf_link_hash_entry *h;
252b5132 2585 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
6edf0760
NC
2586 ensure it's zero (we use REL relocs, not RELA). Therefore this
2587 should be assigning zero to `addend', but for clarity we use
2588 `r_addend'. */
252b5132
RH
2589 bfd_vma addend = rel->r_addend;
2590 bfd_vma offset = rel->r_offset;
252b5132
RH
2591 Elf_Internal_Sym *sym;
2592 asection *sec;
2593 const char *sym_name;
2594 bfd_reloc_status_type r;
2595 const char *errmsg = NULL;
6edf0760 2596 bfd_boolean use_rel = FALSE;
252b5132
RH
2597
2598 h = NULL;
2599 r_type = ELF32_R_TYPE (rel->r_info);
2600 if (r_type < 0 || r_type >= (int) R_M32R_max)
2601 {
d003868e
AM
2602 (*_bfd_error_handler) (_("%B: unknown relocation type %d"),
2603 input_bfd,
252b5132
RH
2604 (int) r_type);
2605 bfd_set_error (bfd_error_bad_value);
b34976b6 2606 ret = FALSE;
252b5132
RH
2607 continue;
2608 }
2609
2610 if (r_type == R_M32R_GNU_VTENTRY
6edf0760
NC
2611 || r_type == R_M32R_GNU_VTINHERIT
2612 || r_type == R_M32R_NONE
2613 || r_type == R_M32R_RELA_GNU_VTENTRY
2614 || r_type == R_M32R_RELA_GNU_VTINHERIT)
252b5132
RH
2615 continue;
2616
6edf0760
NC
2617 if (r_type <= R_M32R_GNU_VTENTRY)
2618 use_rel = TRUE;
2619
252b5132
RH
2620 howto = m32r_elf_howto_table + r_type;
2621 r_symndx = ELF32_R_SYM (rel->r_info);
2622
688c58f3 2623 if (info->relocatable && use_rel)
252b5132 2624 {
1049f94e 2625 /* This is a relocatable link. We don't have to change
252b5132
RH
2626 anything, unless the reloc is against a section symbol,
2627 in which case we have to adjust according to where the
2628 section symbol winds up in the output section. */
2629 sec = NULL;
2630 if (r_symndx >= symtab_hdr->sh_info)
2631 {
2632 /* External symbol. */
2633 continue;
2634 }
2635
2636 /* Local symbol. */
2637 sym = local_syms + r_symndx;
2638 sym_name = "<local symbol>";
2639 /* STT_SECTION: symbol is associated with a section. */
2640 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2641 {
2642 /* Symbol isn't associated with a section. Nothing to do. */
2643 continue;
2644 }
2645
2646 sec = local_sections[r_symndx];
2647 addend += sec->output_offset + sym->st_value;
252b5132 2648
252b5132
RH
2649 /* If partial_inplace, we need to store any additional addend
2650 back in the section. */
2651 if (! howto->partial_inplace)
2652 continue;
2653 /* ??? Here is a nice place to call a special_function
2654 like handler. */
2655 if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2656 r = _bfd_relocate_contents (howto, input_bfd,
2657 addend, contents + offset);
2658 else
2659 {
2660 Elf_Internal_Rela *lorel;
2661
2662 /* We allow an arbitrary number of HI16 relocs before the
2663 LO16 reloc. This permits gcc to emit the HI and LO relocs
2664 itself. */
2665 for (lorel = rel + 1;
2666 (lorel < relend
2667 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2668 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2669 lorel++)
2670 continue;
2671 if (lorel < relend
2672 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2673 {
2674 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2675 contents, addend);
2676 r = bfd_reloc_ok;
2677 }
2678 else
2679 r = _bfd_relocate_contents (howto, input_bfd,
2680 addend, contents + offset);
2681 }
252b5132
RH
2682 }
2683 else
2684 {
2685 bfd_vma relocation;
2686
2687 /* This is a final link. */
2688 sym = NULL;
2689 sec = NULL;
6edf0760 2690 h = NULL;
252b5132
RH
2691
2692 if (r_symndx < symtab_hdr->sh_info)
2693 {
2694 /* Local symbol. */
2695 sym = local_syms + r_symndx;
2696 sec = local_sections[r_symndx];
2697 sym_name = "<local symbol>";
6edf0760 2698
688c58f3 2699 if (!use_rel)
6edf0760
NC
2700 {
2701 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2702 addend = rel->r_addend;
2703
2704 if (info->relocatable)
2705 {
2706 /* This is a relocatable link. We don't have to change
2707 anything, unless the reloc is against a section symbol,
2708 in which case we have to adjust according to where the
2709 section symbol winds up in the output section. */
2710 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2711 rel->r_addend += sec->output_offset + sym->st_value;
41978308 2712
6edf0760
NC
2713 continue;
2714 }
2715 }
2716 else
2717 {
2718 relocation = (sec->output_section->vma
2719 + sec->output_offset
2720 + sym->st_value);
2721 }
252b5132
RH
2722 }
2723 else
2724 {
2725 /* External symbol. */
688c58f3 2726 if (info->relocatable && !use_rel)
6edf0760
NC
2727 continue;
2728
252b5132
RH
2729 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2730 while (h->root.type == bfd_link_hash_indirect
2731 || h->root.type == bfd_link_hash_warning)
2732 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2733 sym_name = h->root.root.string;
2734
2735 if (h->root.type == bfd_link_hash_defined
2736 || h->root.type == bfd_link_hash_defweak)
2737 {
6edf0760 2738 bfd_boolean dyn;
252b5132 2739 sec = h->root.u.def.section;
6edf0760
NC
2740
2741 dyn = htab->root.dynamic_sections_created;
2742 sec = h->root.u.def.section;
2743 if (r_type == R_M32R_GOTPC24
2744 || (r_type == R_M32R_GOTPC_HI_ULO
2745 || r_type == R_M32R_GOTPC_HI_SLO
2746 || r_type == R_M32R_GOTPC_LO)
2747 || (r_type == R_M32R_26_PLTREL
2748 && h->plt.offset != (bfd_vma) -1)
2749 || ((r_type == R_M32R_GOT24
2750 || r_type == R_M32R_GOT16_HI_ULO
2751 || r_type == R_M32R_GOT16_HI_SLO
2752 || r_type == R_M32R_GOT16_LO)
c152c796
AM
2753 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2754 info->shared, h)
6edf0760
NC
2755 && (! info->shared
2756 || (! info->symbolic && h->dynindx != -1)
f5385ebf 2757 || !h->def_regular))
6edf0760
NC
2758 || (info->shared
2759 && ((! info->symbolic && h->dynindx != -1)
f5385ebf 2760 || !h->def_regular)
6edf0760
NC
2761 && (((r_type == R_M32R_16_RELA
2762 || r_type == R_M32R_32_RELA
2763 || r_type == R_M32R_24_RELA
2764 || r_type == R_M32R_HI16_ULO_RELA
2765 || r_type == R_M32R_HI16_SLO_RELA
2766 || r_type == R_M32R_LO16_RELA)
f5385ebf 2767 && !h->forced_local)
6edf0760
NC
2768 || r_type == R_M32R_10_PCREL_RELA
2769 || r_type == R_M32R_18_PCREL_RELA
2770 || r_type == R_M32R_26_PCREL_RELA)
2771 && ((input_section->flags & SEC_ALLOC) != 0
2772 /* DWARF will emit R_M32R_16(24,32) relocations
41978308 2773 in its sections against symbols defined
6edf0760
NC
2774 externally in shared libraries. We can't do
2775 anything with them here. */
2776 || ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 2777 && h->def_dynamic))))
6edf0760
NC
2778 {
2779 /* In these cases, we don't need the relocation
2780 value. We check specially because in some
2781 obscure cases sec->output_section will be NULL. */
2782 relocation = 0;
2783 }
2784 else if (sec->output_section == NULL)
2785 {
2786 (*_bfd_error_handler)
2787 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2788 bfd_get_filename (input_bfd), h->root.root.string,
2789 bfd_get_section_name (input_bfd, input_section));
2790
2791 relocation = 0;
2792 }
252b5132
RH
2793 else
2794 relocation = (h->root.u.def.value
2795 + sec->output_section->vma
2796 + sec->output_offset);
2797 }
2798 else if (h->root.type == bfd_link_hash_undefweak)
2799 relocation = 0;
59c2e50f 2800 else if (info->unresolved_syms_in_objects == RM_IGNORE
6edf0760
NC
2801 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2802 relocation = 0;
252b5132
RH
2803 else
2804 {
2805 if (! ((*info->callbacks->undefined_symbol)
2806 (info, h->root.root.string, input_bfd,
6edf0760 2807 input_section, offset,
59c2e50f 2808 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
6edf0760 2809 || ELF_ST_VISIBILITY (h->other)))))
b34976b6 2810 return FALSE;
252b5132
RH
2811 relocation = 0;
2812 }
2813 }
2814
2815 /* Sanity check the address. */
07515404 2816 if (offset > high_address)
252b5132
RH
2817 {
2818 r = bfd_reloc_outofrange;
2819 goto check_reloc;
2820 }
2821
2822 switch ((int) r_type)
2823 {
097f809a
NC
2824 case R_M32R_GOTOFF:
2825 /* Relocation is relative to the start of the global offset
2826 table (for ld24 rx, #uimm24). eg access at label+addend
f12123c0 2827
097f809a
NC
2828 ld24 rx. #label@GOTOFF + addend
2829 sub rx, r12. */
2830
2831 BFD_ASSERT (sgot != NULL);
2832
2833 relocation = -(relocation - sgot->output_section->vma);
2834 rel->r_addend = -rel->r_addend;
2835 break;
2836
2837 case R_M32R_GOTOFF_HI_ULO:
2838 case R_M32R_GOTOFF_HI_SLO:
2839 case R_M32R_GOTOFF_LO:
2840 BFD_ASSERT (sgot != NULL);
2841
2842 relocation -= sgot->output_section->vma;
2843
2844 if ((r_type == R_M32R_GOTOFF_HI_SLO)
2845 && ((relocation + rel->r_addend) & 0x8000))
2846 rel->r_addend += 0x10000;
2847 break;
2848
6edf0760
NC
2849 case R_M32R_GOTPC24:
2850 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2851 ld24 rx,#_GLOBAL_OFFSET_TABLE_
2852 */
2853 relocation = sgot->output_section->vma;
2854 break;
2855
2856 case R_M32R_GOTPC_HI_ULO:
2857 case R_M32R_GOTPC_HI_SLO:
41978308 2858 case R_M32R_GOTPC_LO:
6edf0760
NC
2859 {
2860 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2861 bl .+4
2862 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2863 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2864 or
2865 bl .+4
2866 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2867 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2868 */
2869 relocation = sgot->output_section->vma;
2870 relocation -= (input_section->output_section->vma
2871 + input_section->output_offset
2872 + rel->r_offset);
41978308 2873 if ((r_type == R_M32R_GOTPC_HI_SLO)
6edf0760
NC
2874 && ((relocation + rel->r_addend) & 0x8000))
2875 rel->r_addend += 0x10000;
2876
2877 break;
2878 }
2879 case R_M32R_GOT16_HI_ULO:
2880 case R_M32R_GOT16_HI_SLO:
41978308 2881 case R_M32R_GOT16_LO:
6edf0760
NC
2882 /* Fall through. */
2883 case R_M32R_GOT24:
2884 /* Relocation is to the entry for this symbol in the global
2885 offset table. */
2886 BFD_ASSERT (sgot != NULL);
41978308 2887
6edf0760
NC
2888 if (h != NULL)
2889 {
2890 bfd_boolean dyn;
2891 bfd_vma off;
41978308 2892
6edf0760
NC
2893 off = h->got.offset;
2894 BFD_ASSERT (off != (bfd_vma) -1);
2895
2896 dyn = htab->root.dynamic_sections_created;
c152c796 2897 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
6edf0760
NC
2898 || (info->shared
2899 && (info->symbolic
2900 || h->dynindx == -1
f5385ebf
AM
2901 || h->forced_local)
2902 && h->def_regular))
6edf0760
NC
2903 {
2904 /* This is actually a static link, or it is a
2905 -Bsymbolic link and the symbol is defined
2906 locally, or the symbol was forced to be local
2907 because of a version file. We must initialize
2908 this entry in the global offset table. Since the
2909 offset must always be a multiple of 4, we use the
2910 least significant bit to record whether we have
2911 initialized it already.
41978308 2912
6edf0760
NC
2913 When doing a dynamic link, we create a .rela.got
2914 relocation entry to initialize the value. This
2915 is done in the finish_dynamic_symbol routine. */
2916 if ((off & 1) != 0)
2917 off &= ~1;
2918 else
2919 {
2920 bfd_put_32 (output_bfd, relocation,
2921 sgot->contents + off);
2922 h->got.offset |= 1;
2923 }
2924 }
41978308 2925
6edf0760
NC
2926 relocation = sgot->output_offset + off;
2927 }
2928 else
2929 {
2930 bfd_vma off;
2931 bfd_byte *loc;
41978308 2932
6edf0760
NC
2933 BFD_ASSERT (local_got_offsets != NULL
2934 && local_got_offsets[r_symndx] != (bfd_vma) -1);
41978308 2935
6edf0760 2936 off = local_got_offsets[r_symndx];
41978308 2937
6edf0760
NC
2938 /* The offset must always be a multiple of 4. We use
2939 the least significant bit to record whether we have
2940 already processed this entry. */
2941 if ((off & 1) != 0)
2942 off &= ~1;
2943 else
2944 {
2945 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
41978308 2946
6edf0760
NC
2947 if (info->shared)
2948 {
2949 asection *srelgot;
2950 Elf_Internal_Rela outrel;
41978308 2951
6edf0760
NC
2952 /* We need to generate a R_M32R_RELATIVE reloc
2953 for the dynamic linker. */
2954 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2955 BFD_ASSERT (srelgot != NULL);
41978308 2956
6edf0760
NC
2957 outrel.r_offset = (sgot->output_section->vma
2958 + sgot->output_offset
2959 + off);
2960 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2961 outrel.r_addend = relocation;
2962 loc = srelgot->contents;
2963 loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2964 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2965 ++srelgot->reloc_count;
2966 }
41978308 2967
6edf0760
NC
2968 local_got_offsets[r_symndx] |= 1;
2969 }
41978308 2970
6edf0760
NC
2971 relocation = sgot->output_offset + off;
2972 }
2973 if ((r_type == R_M32R_GOT16_HI_SLO)
2974 && ((relocation + rel->r_addend) & 0x8000))
2975 rel->r_addend += 0x10000;
2976
2977 break;
41978308 2978
6edf0760
NC
2979 case R_M32R_26_PLTREL:
2980 /* Relocation is to the entry for this symbol in the
2981 procedure linkage table. */
41978308 2982
6edf0760
NC
2983 /* The native assembler will generate a 26_PLTREL reloc
2984 for a local symbol if you assemble a call from one
2985 section to another when using -K pic. */
2986 if (h == NULL)
2987 break;
2988
2989 //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2990 // || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2991 // break;
f5385ebf 2992 if (h->forced_local)
6edf0760
NC
2993 break;
2994
2995 if (h->plt.offset == (bfd_vma) -1)
2996 {
2997 /* We didn't make a PLT entry for this symbol. This
2998 happens when statically linking PIC code, or when
2999 using -Bsymbolic. */
3000 break;
3001 }
41978308 3002
6edf0760
NC
3003 relocation = (splt->output_section->vma
3004 + splt->output_offset
3005 + h->plt.offset);
3006 break;
41978308 3007
6edf0760
NC
3008 case R_M32R_HI16_SLO_RELA:
3009 {
3010 if ((relocation + rel->r_addend) & 0x8000)
3011 {
3012 rel->r_addend += 0x10000;
3013 }
3014 }
3015 /* Fall through. */
3016 case R_M32R_16_RELA:
3017 case R_M32R_24_RELA:
3018 case R_M32R_32_RELA:
3019 case R_M32R_18_PCREL_RELA:
3020 case R_M32R_26_PCREL_RELA:
3021 case R_M32R_HI16_ULO_RELA:
3022 case R_M32R_LO16_RELA:
6edf0760
NC
3023 if (info->shared
3024 && r_symndx != 0
3025 && (input_section->flags & SEC_ALLOC) != 0
3026 && ((r_type != R_M32R_18_PCREL_RELA
3027 && r_type != R_M32R_26_PCREL_RELA)
3028 || (h != NULL
3029 && h->dynindx != -1
3030 && (! info->symbolic
f5385ebf 3031 || !h->def_regular))))
6edf0760
NC
3032 {
3033 Elf_Internal_Rela outrel;
3034 bfd_boolean skip, relocate;
3035 bfd_byte *loc;
41978308 3036
6edf0760
NC
3037 /* When generating a shared object, these relocations
3038 are copied into the output file to be resolved at run
3039 time. */
41978308 3040
6edf0760
NC
3041 if (sreloc == NULL)
3042 {
3043 const char *name;
41978308 3044
6edf0760
NC
3045 name = (bfd_elf_string_from_elf_section
3046 (input_bfd,
3047 elf_elfheader (input_bfd)->e_shstrndx,
3048 elf_section_data (input_section)->rel_hdr.sh_name));
3049 if (name == NULL)
3050 return FALSE;
41978308 3051
6edf0760
NC
3052 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3053 && strcmp (bfd_get_section_name (input_bfd,
3054 input_section),
3055 name + 5) == 0);
41978308 3056
6edf0760
NC
3057 sreloc = bfd_get_section_by_name (dynobj, name);
3058 BFD_ASSERT (sreloc != NULL);
3059 }
41978308 3060
6edf0760
NC
3061 skip = FALSE;
3062 relocate = FALSE;
41978308 3063
6edf0760
NC
3064 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3065 info,
3066 input_section,
3067 rel->r_offset);
3068 if (outrel.r_offset == (bfd_vma) -1)
3069 skip = TRUE;
3070 else if (outrel.r_offset == (bfd_vma) -2)
3071 skip = TRUE, relocate = TRUE;
3072 outrel.r_offset += (input_section->output_section->vma
3073 + input_section->output_offset);
41978308 3074
6edf0760
NC
3075 if (skip)
3076 memset (&outrel, 0, sizeof outrel);
3077 else if (r_type == R_M32R_18_PCREL_RELA
3078 || r_type == R_M32R_26_PCREL_RELA)
3079 {
3080 BFD_ASSERT (h != NULL && h->dynindx != -1);
3081 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3082 outrel.r_addend = rel->r_addend;
3083 }
3084 else
3085 {
3086 /* h->dynindx may be -1 if this symbol was marked to
3087 become local. */
3088 if (h == NULL
3089 || ((info->symbolic || h->dynindx == -1)
f5385ebf 3090 && h->def_regular))
6edf0760
NC
3091 {
3092 relocate = TRUE;
3093 outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3094 outrel.r_addend = relocation + rel->r_addend;
3095 }
3096 else
3097 {
3098 BFD_ASSERT (h->dynindx != -1);
3099 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3100 outrel.r_addend = relocation + rel->r_addend;
3101 }
3102 }
3103
3104 loc = sreloc->contents;
3105 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3106 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3107 ++sreloc->reloc_count;
3108
3109 /* If this reloc is against an external symbol, we do
3110 not want to fiddle with the addend. Otherwise, we
3111 need to include the symbol value so that it becomes
3112 an addend for the dynamic reloc. */
3113 if (! relocate)
3114 continue;
3115 }
3116 break;
41978308 3117
252b5132
RH
3118 case (int) R_M32R_10_PCREL :
3119 r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3120 contents, offset,
3121 sec, relocation, addend);
6edf0760 3122 goto check_reloc;
252b5132
RH
3123
3124 case (int) R_M32R_HI16_SLO :
3125 case (int) R_M32R_HI16_ULO :
3126 {
3127 Elf_Internal_Rela *lorel;
3128
3129 /* We allow an arbitrary number of HI16 relocs before the
3130 LO16 reloc. This permits gcc to emit the HI and LO relocs
3131 itself. */
3132 for (lorel = rel + 1;
3133 (lorel < relend
3134 && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3135 || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3136 lorel++)
3137 continue;
3138 if (lorel < relend
3139 && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3140 {
3141 m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3142 contents, relocation + addend);
3143 r = bfd_reloc_ok;
3144 }
3145 else
3146 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3147 contents, offset,
3148 relocation, addend);
3149 }
6edf0760
NC
3150
3151 goto check_reloc;
252b5132 3152
af705ba8 3153 case (int) R_M32R_SDA16_RELA:
252b5132
RH
3154 case (int) R_M32R_SDA16 :
3155 {
3156 const char *name;
3157
3158 BFD_ASSERT (sec != NULL);
3159 name = bfd_get_section_name (abfd, sec);
3160
3161 if (strcmp (name, ".sdata") == 0
3162 || strcmp (name, ".sbss") == 0
3163 || strcmp (name, ".scommon") == 0)
3164 {
3165 bfd_vma sda_base;
3166 bfd *out_bfd = sec->output_section->owner;
3167
3168 r = m32r_elf_final_sda_base (out_bfd, info,
3169 &errmsg,
3170 &sda_base);
3171 if (r != bfd_reloc_ok)
3172 {
b34976b6 3173 ret = FALSE;
252b5132
RH
3174 goto check_reloc;
3175 }
3176
3177 /* At this point `relocation' contains the object's
3178 address. */
3179 relocation -= sda_base;
3180 /* Now it contains the offset from _SDA_BASE_. */
3181 }
3182 else
3183 {
8f615d07 3184 (*_bfd_error_handler)
d003868e
AM
3185 (_("%B: The target (%s) of an %s relocation is in the wrong section (%A)"),
3186 input_bfd,
3187 sec,
8f615d07 3188 sym_name,
d003868e 3189 m32r_elf_howto_table[(int) r_type].name);
252b5132 3190 /*bfd_set_error (bfd_error_bad_value); ??? why? */
b34976b6 3191 ret = FALSE;
252b5132
RH
3192 continue;
3193 }
3194 }
6edf0760
NC
3195 /* fall through */
3196
3197 default : /* OLD_M32R_RELOC */
252b5132 3198
252b5132
RH
3199 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3200 contents, offset,
3201 relocation, addend);
6edf0760 3202 goto check_reloc;
252b5132 3203 }
6edf0760
NC
3204
3205 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3206 contents, rel->r_offset,
3207 relocation, rel->r_addend);
3208
252b5132
RH
3209 }
3210
3211 check_reloc:
3212
3213 if (r != bfd_reloc_ok)
3214 {
3215 /* FIXME: This should be generic enough to go in a utility. */
3216 const char *name;
3217
3218 if (h != NULL)
3219 name = h->root.root.string;
3220 else
3221 {
3222 name = (bfd_elf_string_from_elf_section
3223 (input_bfd, symtab_hdr->sh_link, sym->st_name));
3224 if (name == NULL || *name == '\0')
3225 name = bfd_section_name (input_bfd, sec);
3226 }
3227
3228 if (errmsg != NULL)
3229 goto common_error;
3230
3231 switch (r)
3232 {
3233 case bfd_reloc_overflow:
3234 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3235 (info, (h ? &h->root : NULL), name, howto->name,
3236 (bfd_vma) 0, input_bfd, input_section, offset)))
b34976b6 3237 return FALSE;
252b5132
RH
3238 break;
3239
3240 case bfd_reloc_undefined:
3241 if (! ((*info->callbacks->undefined_symbol)
3242 (info, name, input_bfd, input_section,
b34976b6
AM
3243 offset, TRUE)))
3244 return FALSE;
252b5132
RH
3245 break;
3246
3247 case bfd_reloc_outofrange:
3248 errmsg = _("internal error: out of range error");
3249 goto common_error;
3250
3251 case bfd_reloc_notsupported:
3252 errmsg = _("internal error: unsupported relocation error");
3253 goto common_error;
3254
3255 case bfd_reloc_dangerous:
3256 errmsg = _("internal error: dangerous error");
3257 goto common_error;
3258
3259 default:
3260 errmsg = _("internal error: unknown error");
3261 /* fall through */
3262
3263 common_error:
3264 if (!((*info->callbacks->warning)
3265 (info, errmsg, name, input_bfd, input_section,
3266 offset)))
b34976b6 3267 return FALSE;
252b5132
RH
3268 break;
3269 }
3270 }
3271 }
3272
3273 return ret;
3274}
6edf0760
NC
3275
3276/* Finish up dynamic symbol handling. We set the contents of various
3277 dynamic sections here. */
3278static bfd_boolean
3279m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3280 bfd *output_bfd;
3281 struct bfd_link_info *info;
3282 struct elf_link_hash_entry *h;
3283 Elf_Internal_Sym *sym;
3284{
3285 struct elf_m32r_link_hash_table *htab;
3286 bfd *dynobj;
3287 bfd_byte *loc;
41978308 3288
6edf0760
NC
3289#ifdef DEBUG_PIC
3290printf("m32r_elf_finish_dynamic_symbol()\n");
3291#endif
41978308 3292
6edf0760
NC
3293 htab = m32r_elf_hash_table (info);
3294 dynobj = htab->root.dynobj;
3295
3296 if (h->plt.offset != (bfd_vma) -1)
3297 {
3298 asection *splt;
3299 asection *sgot;
3300 asection *srela;
3301
3302 bfd_vma plt_index;
3303 bfd_vma got_offset;
3304 Elf_Internal_Rela rela;
41978308 3305
6edf0760
NC
3306 /* This symbol has an entry in the procedure linkage table. Set
3307 it up. */
41978308 3308
6edf0760 3309 BFD_ASSERT (h->dynindx != -1);
41978308 3310
6edf0760
NC
3311 splt = htab->splt;
3312 sgot = htab->sgotplt;
3313 srela = htab->srelplt;
3314 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3315
3316 /* Get the index in the procedure linkage table which
3317 corresponds to this symbol. This is the index of this symbol
3318 in all the symbols for which we are making plt entries. The
3319 first entry in the procedure linkage table is reserved. */
3320 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3321
3322 /* Get the offset into the .got table of the entry that
3323 corresponds to this function. Each .got entry is 4 bytes.
3324 The first three are reserved. */
3325 got_offset = (plt_index + 3) * 4;
3326
3327 /* Fill in the entry in the procedure linkage table. */
3328 if (! info->shared)
3329 {
3330 bfd_put_32 (output_bfd,
3331 (PLT_ENTRY_WORD0b
3332 + (((sgot->output_section->vma
3333 + sgot->output_offset
3334 + got_offset) >> 16) & 0xffff)),
3335 splt->contents + h->plt.offset);
3336 bfd_put_32 (output_bfd,
3337 (PLT_ENTRY_WORD1b
3338 + ((sgot->output_section->vma
3339 + sgot->output_offset
3340 + got_offset) & 0xffff)),
3341 splt->contents + h->plt.offset + 4);
3342 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3343 splt->contents + h->plt.offset + 8);
3344 bfd_put_32 (output_bfd,
3345 (PLT_ENTRY_WORD3
3346 + plt_index * sizeof (Elf32_External_Rela)),
3347 splt->contents + h->plt.offset + 12);
3348 bfd_put_32 (output_bfd,
3349 (PLT_ENTRY_WORD4
3350 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3351 splt->contents + h->plt.offset + 16);
3352 }
3353 else
3354 {
3355 bfd_put_32 (output_bfd,
3356 PLT_ENTRY_WORD0 + got_offset,
3357 splt->contents + h->plt.offset);
3358 bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3359 splt->contents + h->plt.offset + 4);
3360 bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3361 splt->contents + h->plt.offset + 8);
3362 bfd_put_32 (output_bfd,
3363 (PLT_ENTRY_WORD3
3364 + plt_index * sizeof (Elf32_External_Rela)),
3365 splt->contents + h->plt.offset + 12);
3366 bfd_put_32 (output_bfd,
3367 (PLT_ENTRY_WORD4
3368 + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3369 splt->contents + h->plt.offset + 16);
3370 }
41978308 3371
6edf0760
NC
3372 /* Fill in the entry in the global offset table. */
3373 bfd_put_32 (output_bfd,
3374 (splt->output_section->vma
3375 + splt->output_offset
3376 + h->plt.offset
3377 + 12), /* same offset */
3378 sgot->contents + got_offset);
3379
3380 /* Fill in the entry in the .rela.plt section. */
3381 rela.r_offset = (sgot->output_section->vma
3382 + sgot->output_offset
3383 + got_offset);
3384 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3385 rela.r_addend = 0;
3386 loc = srela->contents;
3387 loc += plt_index * sizeof(Elf32_External_Rela);
3388 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
41978308 3389
f5385ebf 3390 if (!h->def_regular)
6edf0760
NC
3391 {
3392 /* Mark the symbol as undefined, rather than as defined in
3393 the .plt section. Leave the value alone. */
3394 sym->st_shndx = SHN_UNDEF;
3395 }
3396 }
3397
3398 if (h->got.offset != (bfd_vma) -1)
3399 {
3400 asection *sgot;
3401 asection *srela;
3402 Elf_Internal_Rela rela;
41978308 3403
6edf0760
NC
3404 /* This symbol has an entry in the global offset table. Set it
3405 up. */
41978308 3406
6edf0760
NC
3407 sgot = htab->sgot;
3408 srela = htab->srelgot;
3409 BFD_ASSERT (sgot != NULL && srela != NULL);
41978308 3410
6edf0760
NC
3411 rela.r_offset = (sgot->output_section->vma
3412 + sgot->output_offset
3413 + (h->got.offset &~ 1));
41978308 3414
6edf0760
NC
3415 /* If this is a -Bsymbolic link, and the symbol is defined
3416 locally, we just want to emit a RELATIVE reloc. Likewise if
3417 the symbol was forced to be local because of a version file.
3418 The entry in the global offset table will already have been
3419 initialized in the relocate_section function. */
3420 if (info->shared
3421 && (info->symbolic
3422 || h->dynindx == -1
f5385ebf
AM
3423 || h->forced_local)
3424 && h->def_regular)
6edf0760
NC
3425 {
3426 rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3427 rela.r_addend = (h->root.u.def.value
3428 + h->root.u.def.section->output_section->vma
3429 + h->root.u.def.section->output_offset);
3430 }
3431 else
3432 {
3433 BFD_ASSERT((h->got.offset & 1) == 0);
3434 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3435 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3436 rela.r_addend = 0;
3437 }
41978308 3438
6edf0760
NC
3439 loc = srela->contents;
3440 loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3441 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3442 ++srela->reloc_count;
3443 }
41978308 3444
f5385ebf 3445 if (h->needs_copy)
6edf0760
NC
3446 {
3447 asection *s;
3448 Elf_Internal_Rela rela;
41978308 3449
6edf0760 3450 /* This symbols needs a copy reloc. Set it up. */
41978308 3451
6edf0760
NC
3452 BFD_ASSERT (h->dynindx != -1
3453 && (h->root.type == bfd_link_hash_defined
3454 || h->root.type == bfd_link_hash_defweak));
41978308 3455
6edf0760
NC
3456 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3457 ".rela.bss");
3458 BFD_ASSERT (s != NULL);
41978308 3459
6edf0760
NC
3460 rela.r_offset = (h->root.u.def.value
3461 + h->root.u.def.section->output_section->vma
3462 + h->root.u.def.section->output_offset);
3463 rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3464 rela.r_addend = 0;
3465 loc = s->contents;
3466 loc += s->reloc_count * sizeof(Elf32_External_Rela);
3467 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3468 ++s->reloc_count;
3469 }
41978308 3470
6edf0760
NC
3471 /* Mark some specially defined symbols as absolute. */
3472 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3473 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3474 sym->st_shndx = SHN_ABS;
41978308 3475
6edf0760
NC
3476 return TRUE;
3477}
3478
3479
3480/* Finish up the dynamic sections. */
41978308 3481
6edf0760
NC
3482static bfd_boolean
3483m32r_elf_finish_dynamic_sections (output_bfd, info)
3484 bfd *output_bfd;
3485 struct bfd_link_info *info;
3486{
3487 struct elf_m32r_link_hash_table *htab;
3488 bfd *dynobj;
3489 asection *sdyn;
3490 asection *sgot;
3491
3492#ifdef DEBUG_PIC
3493printf("m32r_elf_finish_dynamic_sections()\n");
3494#endif
41978308 3495
6edf0760
NC
3496 htab = m32r_elf_hash_table (info);
3497 dynobj = htab->root.dynobj;
3498
3499 sgot = htab->sgotplt;
3500 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
41978308 3501
6edf0760
NC
3502 if (htab->root.dynamic_sections_created)
3503 {
3504 asection *splt;
3505 Elf32_External_Dyn *dyncon, *dynconend;
41978308 3506
6edf0760 3507 BFD_ASSERT (sgot != NULL && sdyn != NULL);
41978308 3508
6edf0760 3509 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 3510 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6edf0760
NC
3511
3512 for (; dyncon < dynconend; dyncon++)
3513 {
3514 Elf_Internal_Dyn dyn;
3515 const char *name;
3516 asection *s;
41978308 3517
6edf0760 3518 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
41978308 3519
6edf0760
NC
3520 switch (dyn.d_tag)
3521 {
3522 default:
3523 break;
3524
3525 case DT_PLTGOT:
3526 name = ".got";
41978308 3527 s = htab->sgot->output_section;
6edf0760
NC
3528 goto get_vma;
3529 case DT_JMPREL:
3530 name = ".rela.plt";
3531 s = htab->srelplt->output_section;
3532 get_vma:
3533 BFD_ASSERT (s != NULL);
3534 dyn.d_un.d_ptr = s->vma;
3535 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3536 break;
3537
3538 case DT_PLTRELSZ:
3539 s = htab->srelplt->output_section;
3540 BFD_ASSERT (s != NULL);
eea6121a 3541 dyn.d_un.d_val = s->size;
6edf0760
NC
3542 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3543 break;
3544
3545 case DT_RELASZ:
3546 /* My reading of the SVR4 ABI indicates that the
3547 procedure linkage table relocs (DT_JMPREL) should be
3548 included in the overall relocs (DT_RELA). This is
3549 what Solaris does. However, UnixWare can not handle
3550 that case. Therefore, we override the DT_RELASZ entry
3551 here to make it not include the JMPREL relocs. Since
3552 the linker script arranges for .rela.plt to follow all
3553 other relocation sections, we don't have to worry
3554 about changing the DT_RELA entry. */
3555 if (htab->srelplt != NULL)
3556 {
3557 s = htab->srelplt->output_section;
eea6121a 3558 dyn.d_un.d_val -= s->size;
6edf0760
NC
3559 }
3560 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3561 break;
3562 }
3563 }
41978308 3564
6edf0760
NC
3565 /* Fill in the first entry in the procedure linkage table. */
3566 splt = htab->splt;
eea6121a 3567 if (splt && splt->size > 0)
6edf0760
NC
3568 {
3569 if (info->shared)
3570 {
3571 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3572 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3573 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3574 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3575 bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3576 }
3577 else
3578 {
3579 unsigned long addr;
3580 /* addr = .got + 4 */
3581 addr = sgot->output_section->vma + sgot->output_offset + 4;
3582 bfd_put_32 (output_bfd,
3583 PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3584 splt->contents);
41978308 3585 bfd_put_32 (output_bfd,
6edf0760
NC
3586 PLT0_ENTRY_WORD1 | (addr & 0xffff),
3587 splt->contents + 4);
3588 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3589 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3590 bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3591 }
3592
3593 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3594 PLT_ENTRY_SIZE;
3595 }
3596 }
3597
3598 /* Fill in the first three entries in the global offset table. */
eea6121a 3599 if (sgot && sgot->size > 0)
6edf0760
NC
3600 {
3601 if (sdyn == NULL)
3602 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3603 else
3604 bfd_put_32 (output_bfd,
3605 sdyn->output_section->vma + sdyn->output_offset,
3606 sgot->contents);
3607 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3608 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3609
3610 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3611 }
41978308 3612
6edf0760
NC
3613 return TRUE;
3614}
3615
252b5132
RH
3616\f
3617/* Set the right machine number. */
b34976b6 3618static bfd_boolean
252b5132
RH
3619m32r_elf_object_p (abfd)
3620 bfd *abfd;
3621{
3622 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
3623 {
3624 default:
3625 case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
e916b64e 3626 case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
88845958 3627 case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
252b5132 3628 }
b34976b6 3629 return TRUE;
252b5132
RH
3630}
3631
3632/* Store the machine number in the flags field. */
3633static void
3634m32r_elf_final_write_processing (abfd, linker)
b34976b6
AM
3635 bfd *abfd;
3636 bfd_boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
3637{
3638 unsigned long val;
3639
3640 switch (bfd_get_mach (abfd))
3641 {
3642 default:
3643 case bfd_mach_m32r: val = E_M32R_ARCH; break;
e916b64e 3644 case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
88845958 3645 case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
252b5132
RH
3646 }
3647
3648 elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
3649 elf_elfheader (abfd)->e_flags |= val;
3650}
3651
c3668558 3652/* Function to keep M32R specific file flags. */
b34976b6 3653static bfd_boolean
252b5132 3654m32r_elf_set_private_flags (abfd, flags)
b34976b6 3655 bfd *abfd;
252b5132
RH
3656 flagword flags;
3657{
3658 BFD_ASSERT (!elf_flags_init (abfd)
3659 || elf_elfheader (abfd)->e_flags == flags);
3660
3661 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
3662 elf_flags_init (abfd) = TRUE;
3663 return TRUE;
252b5132
RH
3664}
3665
252b5132
RH
3666/* Merge backend specific data from an object file to the output
3667 object file when linking. */
b34976b6 3668static bfd_boolean
252b5132 3669m32r_elf_merge_private_bfd_data (ibfd, obfd)
b34976b6
AM
3670 bfd *ibfd;
3671 bfd *obfd;
252b5132
RH
3672{
3673 flagword out_flags;
3674 flagword in_flags;
3675
3676 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3677 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 3678 return TRUE;
252b5132
RH
3679
3680 in_flags = elf_elfheader (ibfd)->e_flags;
3681 out_flags = elf_elfheader (obfd)->e_flags;
3682
3683 if (! elf_flags_init (obfd))
3684 {
3685 /* If the input is the default architecture then do not
3686 bother setting the flags for the output architecture,
3687 instead allow future merges to do this. If no future
3688 merges ever set these flags then they will retain their
3689 unitialised values, which surprise surprise, correspond
3690 to the default values. */
3691 if (bfd_get_arch_info (ibfd)->the_default)
b34976b6 3692 return TRUE;
c3668558 3693
b34976b6 3694 elf_flags_init (obfd) = TRUE;
252b5132
RH
3695 elf_elfheader (obfd)->e_flags = in_flags;
3696
3697 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3698 && bfd_get_arch_info (obfd)->the_default)
3699 {
3700 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
3701 }
3702
b34976b6 3703 return TRUE;
252b5132
RH
3704 }
3705
3706 /* Check flag compatibility. */
3707 if (in_flags == out_flags)
b34976b6 3708 return TRUE;
252b5132
RH
3709
3710 if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
3711 {
88845958
NC
3712 if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
3713 || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3714 || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
252b5132 3715 {
8f615d07 3716 (*_bfd_error_handler)
d003868e 3717 (_("%B: Instruction set mismatch with previous modules"), ibfd);
252b5132
RH
3718
3719 bfd_set_error (bfd_error_bad_value);
b34976b6 3720 return FALSE;
252b5132
RH
3721 }
3722 }
3723
b34976b6 3724 return TRUE;
252b5132
RH
3725}
3726
3727/* Display the flags field */
b34976b6 3728static bfd_boolean
252b5132 3729m32r_elf_print_private_bfd_data (abfd, ptr)
b34976b6
AM
3730 bfd *abfd;
3731 PTR ptr;
252b5132
RH
3732{
3733 FILE * file = (FILE *) ptr;
c3668558 3734
f12123c0 3735 BFD_ASSERT (abfd != NULL && ptr != NULL);
c3668558 3736
252b5132 3737 _bfd_elf_print_private_bfd_data (abfd, ptr);
c3668558 3738
252b5132 3739 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
c3668558 3740
252b5132
RH
3741 switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
3742 {
3743 default:
3744 case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
e916b64e 3745 case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
88845958 3746 case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
252b5132 3747 }
c3668558 3748
252b5132 3749 fputc ('\n', file);
c3668558 3750
b34976b6 3751 return TRUE;
252b5132
RH
3752}
3753
3754asection *
1e2f5b6e
AM
3755m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
3756 asection *sec;
3757 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3758 Elf_Internal_Rela *rel;
3759 struct elf_link_hash_entry *h;
3760 Elf_Internal_Sym *sym;
252b5132
RH
3761{
3762 if (h != NULL)
3763 {
3764 switch (ELF32_R_TYPE (rel->r_info))
3765 {
3766 case R_M32R_GNU_VTINHERIT:
3767 case R_M32R_GNU_VTENTRY:
6edf0760
NC
3768 case R_M32R_RELA_GNU_VTINHERIT:
3769 case R_M32R_RELA_GNU_VTENTRY:
252b5132 3770 break;
c3668558 3771
252b5132
RH
3772 default:
3773 switch (h->root.type)
3774 {
3775 case bfd_link_hash_defined:
3776 case bfd_link_hash_defweak:
3777 return h->root.u.def.section;
c3668558 3778
252b5132
RH
3779 case bfd_link_hash_common:
3780 return h->root.u.c.p->section;
e049a0de
ILT
3781
3782 default:
3783 break;
252b5132
RH
3784 }
3785 }
3786 }
3787 else
1e2f5b6e
AM
3788 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3789
252b5132
RH
3790 return NULL;
3791}
3792
b34976b6 3793static bfd_boolean
252b5132 3794m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
5f771d47
ILT
3795 bfd *abfd ATTRIBUTE_UNUSED;
3796 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3797 asection *sec ATTRIBUTE_UNUSED;
3798 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
252b5132 3799{
6edf0760
NC
3800 /* Update the got entry reference counts for the section being removed. */
3801 Elf_Internal_Shdr *symtab_hdr;
3802 struct elf_link_hash_entry **sym_hashes;
3803 bfd_signed_vma *local_got_refcounts;
3804 const Elf_Internal_Rela *rel, *relend;
6edf0760
NC
3805
3806 elf_section_data (sec)->local_dynrel = NULL;
3807
3808 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3809 sym_hashes = elf_sym_hashes (abfd);
3810 local_got_refcounts = elf_local_got_refcounts (abfd);
3811
3812 relend = relocs + sec->reloc_count;
3813 for (rel = relocs; rel < relend; rel++)
3eb128b2
AM
3814 {
3815 unsigned long r_symndx;
3816 struct elf_link_hash_entry *h = NULL;
6edf0760 3817
3eb128b2
AM
3818 r_symndx = ELF32_R_SYM (rel->r_info);
3819 if (r_symndx >= symtab_hdr->sh_info)
3820 {
3821 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3822 while (h->root.type == bfd_link_hash_indirect
3823 || h->root.type == bfd_link_hash_warning)
3824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3825 }
6edf0760 3826
3eb128b2
AM
3827 switch (ELF32_R_TYPE (rel->r_info))
3828 {
3829 case R_M32R_GOT16_HI_ULO:
3830 case R_M32R_GOT16_HI_SLO:
3831 case R_M32R_GOT16_LO:
3832 case R_M32R_GOTOFF:
3833 case R_M32R_GOTOFF_HI_ULO:
3834 case R_M32R_GOTOFF_HI_SLO:
3835 case R_M32R_GOTOFF_LO:
3836 case R_M32R_GOT24:
3837 case R_M32R_GOTPC_HI_ULO:
3838 case R_M32R_GOTPC_HI_SLO:
3839 case R_M32R_GOTPC_LO:
3840 case R_M32R_GOTPC24:
3841 if (h != NULL)
3842 {
3843 if (h->got.refcount > 0)
3844 h->got.refcount--;
3845 }
3846 else
3847 {
3848 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
3849 local_got_refcounts[r_symndx]--;
3850 }
3851 break;
3852
3853 case R_M32R_16_RELA:
3854 case R_M32R_24_RELA:
3855 case R_M32R_32_RELA:
3856 case R_M32R_HI16_ULO_RELA:
3857 case R_M32R_HI16_SLO_RELA:
3858 case R_M32R_LO16_RELA:
3859 case R_M32R_SDA16_RELA:
3860 case R_M32R_18_PCREL_RELA:
3861 case R_M32R_26_PCREL_RELA:
3862 if (h != NULL)
3863 {
3864 struct elf_m32r_link_hash_entry *eh;
3865 struct elf_m32r_dyn_relocs **pp;
3866 struct elf_m32r_dyn_relocs *p;
6edf0760 3867
3eb128b2
AM
3868 if (!info->shared && h->plt.refcount > 0)
3869 h->plt.refcount -= 1;
6edf0760 3870
3eb128b2 3871 eh = (struct elf_m32r_link_hash_entry *) h;
6edf0760 3872
3eb128b2
AM
3873 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3874 if (p->sec == sec)
3875 {
3876 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
3877 || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
3878 p->pc_count -= 1;
3879 p->count -= 1;
3880 if (p->count == 0)
3881 *pp = p->next;
3882 break;
3883 }
3884 }
3885 break;
6edf0760 3886
3eb128b2
AM
3887 case R_M32R_26_PLTREL:
3888 if (h != NULL)
3889 {
3890 if (h->plt.refcount > 0)
3891 h->plt.refcount--;
3892 }
3893 break;
6edf0760 3894
3eb128b2
AM
3895 default:
3896 break;
3897 }
3898 }
6edf0760 3899
b34976b6 3900 return TRUE;
252b5132
RH
3901}
3902
252b5132
RH
3903/* Look through the relocs for a section during the first phase.
3904 Since we don't do .gots or .plts, we just need to consider the
3905 virtual table relocs for gc. */
c3668558 3906
b34976b6 3907static bfd_boolean
252b5132
RH
3908m32r_elf_check_relocs (abfd, info, sec, relocs)
3909 bfd *abfd;
3910 struct bfd_link_info *info;
3911 asection *sec;
3912 const Elf_Internal_Rela *relocs;
3913{
3914 Elf_Internal_Shdr *symtab_hdr;
3915 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3916 const Elf_Internal_Rela *rel;
3917 const Elf_Internal_Rela *rel_end;
6edf0760
NC
3918 struct elf_m32r_link_hash_table *htab;
3919 bfd *dynobj;
3920 bfd_vma *local_got_offsets;
3921 asection *sgot, *srelgot, *sreloc;
c3668558 3922
1049f94e 3923 if (info->relocatable)
b34976b6 3924 return TRUE;
c3668558 3925
6edf0760
NC
3926 sgot = srelgot = sreloc = NULL;
3927
252b5132
RH
3928 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3929 sym_hashes = elf_sym_hashes (abfd);
c3668558 3930 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
252b5132
RH
3931 if (!elf_bad_symtab (abfd))
3932 sym_hashes_end -= symtab_hdr->sh_info;
c3668558 3933
6edf0760
NC
3934 htab = m32r_elf_hash_table (info);
3935 dynobj = htab->root.dynobj;
3936 local_got_offsets = elf_local_got_offsets (abfd);
3937
252b5132
RH
3938 rel_end = relocs + sec->reloc_count;
3939 for (rel = relocs; rel < rel_end; rel++)
3940 {
6edf0760 3941 int r_type;
252b5132
RH
3942 struct elf_link_hash_entry *h;
3943 unsigned long r_symndx;
c3668558 3944
252b5132 3945 r_symndx = ELF32_R_SYM (rel->r_info);
6edf0760 3946 r_type = ELF32_R_TYPE (rel->r_info);
252b5132
RH
3947 if (r_symndx < symtab_hdr->sh_info)
3948 h = NULL;
3949 else
973a3492
L
3950 {
3951 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3952 while (h->root.type == bfd_link_hash_indirect
3953 || h->root.type == bfd_link_hash_warning)
3954 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3955 }
c3668558 3956
6edf0760
NC
3957 /* Some relocs require a global offset table. */
3958 if (htab->sgot == NULL)
3959 {
3960 switch (r_type)
3961 {
3962 case R_M32R_GOT16_HI_ULO:
3963 case R_M32R_GOT16_HI_SLO:
097f809a
NC
3964 case R_M32R_GOTOFF:
3965 case R_M32R_GOTOFF_HI_ULO:
3966 case R_M32R_GOTOFF_HI_SLO:
3967 case R_M32R_GOTOFF_LO:
6edf0760
NC
3968 case R_M32R_GOT16_LO:
3969 case R_M32R_GOTPC24:
3970 case R_M32R_GOTPC_HI_ULO:
3971 case R_M32R_GOTPC_HI_SLO:
3972 case R_M32R_GOTPC_LO:
3973 case R_M32R_GOT24:
3974 if (dynobj == NULL)
3975 htab->root.dynobj = dynobj = abfd;
3976 if (! create_got_section (dynobj, info))
3977 return FALSE;
3978 break;
3979
3980 default:
3981 break;
3982 }
3983 }
3984
3985 switch (r_type)
252b5132 3986 {
6edf0760
NC
3987 case R_M32R_GOT16_HI_ULO:
3988 case R_M32R_GOT16_HI_SLO:
41978308 3989 case R_M32R_GOT16_LO:
6edf0760
NC
3990 case R_M32R_GOT24:
3991
3992 if (h != NULL)
3993 h->got.refcount += 1;
3994 else
3995 {
3996 bfd_signed_vma *local_got_refcounts;
3997
3998 /* This is a global offset table entry for a local
3999 symbol. */
4000 local_got_refcounts = elf_local_got_refcounts (abfd);
4001 if (local_got_refcounts == NULL)
4002 {
4003 bfd_size_type size;
4004
4005 size = symtab_hdr->sh_info;
4006 size *= sizeof (bfd_signed_vma);
4007 local_got_refcounts = ((bfd_signed_vma *)
4008 bfd_zalloc (abfd, size));
4009 if (local_got_refcounts == NULL)
4010 return FALSE;
4011 elf_local_got_refcounts (abfd) = local_got_refcounts;
4012 }
4013 local_got_refcounts[r_symndx] += 1;
4014 }
4015 break;
4016
4017 case R_M32R_26_PLTREL:
4018 /* This symbol requires a procedure linkage table entry. We
4019 actually build the entry in adjust_dynamic_symbol,
4020 because this might be a case of linking PIC code without
4021 linking in any dynamic objects, in which case we don't
4022 need to generate a procedure linkage table after all. */
41978308 4023
6edf0760
NC
4024 /* If this is a local symbol, we resolve it directly without
4025 creating a procedure linkage table entry. */
4026 if (h == NULL)
4027 continue;
4028
f5385ebf 4029 if (h->forced_local)
6edf0760
NC
4030 break;
4031
f5385ebf 4032 h->needs_plt = 1;
6edf0760
NC
4033 h->plt.refcount += 1;
4034 break;
4035
4036 case R_M32R_16_RELA:
4037 case R_M32R_24_RELA:
4038 case R_M32R_32_RELA:
4039 case R_M32R_HI16_ULO_RELA:
4040 case R_M32R_HI16_SLO_RELA:
4041 case R_M32R_LO16_RELA:
4042 case R_M32R_SDA16_RELA:
4043 case R_M32R_18_PCREL_RELA:
4044 case R_M32R_26_PCREL_RELA:
4045
4046 if (h != NULL && !info->shared)
4047 {
f5385ebf 4048 h->non_got_ref = 1;
6edf0760
NC
4049 h->plt.refcount += 1;
4050 }
4051
4052 /* If we are creating a shared library, and this is a reloc
4053 against a global symbol, or a non PC relative reloc
4054 against a local symbol, then we need to copy the reloc
4055 into the shared library. However, if we are linking with
4056 -Bsymbolic, we do not need to copy a reloc against a
4057 global symbol which is defined in an object we are
4058 including in the link (i.e., DEF_REGULAR is set). At
4059 this point we have not seen all the input files, so it is
4060 possible that DEF_REGULAR is not set now but will be set
4061 later (it is never cleared). We account for that
4062 possibility below by storing information in the
4063 dyn_relocs field of the hash table entry. A similar
4064 situation occurs when creating shared libraries and symbol
4065 visibility changes render the symbol local.
4066
4067 If on the other hand, we are creating an executable, we
4068 may need to keep relocations for symbols satisfied by a
4069 dynamic library if we manage to avoid copy relocs for the
4070 symbol. */
4071 if ((info->shared
4072 && (sec->flags & SEC_ALLOC) != 0
4073 && ((r_type != R_M32R_26_PCREL_RELA
4074 && r_type != R_M32R_18_PCREL_RELA)
4075 || (h != NULL
4076 && (! info->symbolic
4077 || h->root.type == bfd_link_hash_defweak
f5385ebf 4078 || !h->def_regular))))
6edf0760
NC
4079 || (!info->shared
4080 && (sec->flags & SEC_ALLOC) != 0
4081 && h != NULL
4082 && (h->root.type == bfd_link_hash_defweak
f5385ebf 4083 || !h->def_regular)))
6edf0760
NC
4084 {
4085 struct elf_m32r_dyn_relocs *p;
4086 struct elf_m32r_dyn_relocs **head;
4087
4088 if (dynobj == NULL)
4089 htab->root.dynobj = dynobj = abfd;
4090
4091 /* When creating a shared object, we must copy these
4092 relocs into the output file. We create a reloc
4093 section in dynobj and make room for the reloc. */
4094 if (sreloc == NULL)
4095 {
4096 const char *name;
41978308 4097
6edf0760
NC
4098 name = (bfd_elf_string_from_elf_section
4099 (abfd,
4100 elf_elfheader (abfd)->e_shstrndx,
4101 elf_section_data (sec)->rel_hdr.sh_name));
4102 if (name == NULL)
4103 return FALSE;
41978308 4104
6edf0760
NC
4105 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4106 && strcmp (bfd_get_section_name (abfd, sec),
4107 name + 5) == 0);
41978308 4108
6edf0760
NC
4109 sreloc = bfd_get_section_by_name (dynobj, name);
4110 if (sreloc == NULL)
4111 {
4112 flagword flags;
41978308 4113
6edf0760
NC
4114 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4115 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4116 if ((sec->flags & SEC_ALLOC) != 0)
4117 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
4118 sreloc = bfd_make_section_with_flags (dynobj,
4119 name,
4120 flags);
6edf0760 4121 if (sreloc == NULL
6edf0760
NC
4122 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4123 return FALSE;
4124 }
4125 elf_section_data (sec)->sreloc = sreloc;
4126 }
41978308 4127
6edf0760
NC
4128 /* If this is a global symbol, we count the number of
4129 relocations we need for this symbol. */
4130 if (h != NULL)
4131 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4132 else
4133 {
4134 asection *s;
4135
4136 /* Track dynamic relocs needed for local syms too. */
4137 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4138 sec, r_symndx);
4139 if (s == NULL)
4140 return FALSE;
4141
4142 head = ((struct elf_m32r_dyn_relocs **)
4143 &elf_section_data (s)->local_dynrel);
4144 }
4145
4146 p = *head;
4147 if (p == NULL || p->sec != sec)
4148 {
4149 bfd_size_type amt = sizeof (*p);
4150 p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4151 if (p == NULL)
4152 return FALSE;
4153 p->next = *head;
4154 *head = p;
4155 p->sec = sec;
4156 p->count = 0;
4157 p->pc_count = 0;
4158 }
4159
4160 p->count += 1;
4161 if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4162 || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4163 p->pc_count += 1;
4164 }
4165 break;
4166
252b5132
RH
4167 /* This relocation describes the C++ object vtable hierarchy.
4168 Reconstruct it for later use during GC. */
6edf0760 4169 case R_M32R_RELA_GNU_VTINHERIT:
252b5132 4170 case R_M32R_GNU_VTINHERIT:
c152c796 4171 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4172 return FALSE;
252b5132 4173 break;
c3668558 4174
252b5132
RH
4175 /* This relocation describes which C++ vtable entries are actually
4176 used. Record for later use during GC. */
4177 case R_M32R_GNU_VTENTRY:
c152c796 4178 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 4179 return FALSE;
252b5132 4180 break;
6edf0760 4181 case R_M32R_RELA_GNU_VTENTRY:
c152c796 4182 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6edf0760
NC
4183 return FALSE;
4184 break;
252b5132
RH
4185 }
4186 }
c3668558 4187
b34976b6 4188 return TRUE;
252b5132 4189}
2f89ff8d 4190
7f4d3958
L
4191static struct bfd_elf_special_section const
4192 m32r_elf_special_sections_s[] =
2f89ff8d 4193{
7dcb9820
AM
4194 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4195 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
7f4d3958
L
4196 { NULL, 0, 0, 0, 0 }
4197};
4198
4199static struct bfd_elf_special_section const *
4200 m32r_elf_special_sections[27] =
4201{
4202 NULL, /* 'a' */
4203 NULL, /* 'b' */
4204 NULL, /* 'c' */
4205 NULL, /* 'd' */
4206 NULL, /* 'e' */
4207 NULL, /* 'f' */
4208 NULL, /* 'g' */
4209 NULL, /* 'h' */
4210 NULL, /* 'i' */
4211 NULL, /* 'j' */
4212 NULL, /* 'k' */
4213 NULL, /* 'l' */
4214 NULL, /* 'm' */
4215 NULL, /* 'n' */
4216 NULL, /* 'o' */
4217 NULL, /* 'p' */
4218 NULL, /* 'q' */
4219 NULL, /* 'r' */
4220 m32r_elf_special_sections_s, /* 's' */
4221 NULL, /* 't' */
4222 NULL, /* 'u' */
4223 NULL, /* 'v' */
4224 NULL, /* 'w' */
4225 NULL, /* 'x' */
4226 NULL, /* 'y' */
4227 NULL, /* 'z' */
4228 NULL /* other */
2f89ff8d 4229};
6edf0760
NC
4230
4231static bfd_boolean
4232m32r_elf_fake_sections (abfd, hdr, sec)
4233 bfd *abfd;
4234 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4235 asection *sec;
4236{
4237 register const char *name;
4238
4239 name = bfd_get_section_name (abfd, sec);
4240
4241 /* The generic elf_fake_sections will set up REL_HDR using the
4242 default kind of relocations. But, we may actually need both
4243 kinds of relocations, so we set up the second header here.
4244
4245 This is not necessary for the O32 ABI since that only uses Elf32_Rel
4246 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4247 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
4248 of the resulting empty .rela.<section> sections starts with
4249 sh_offset == object size, and ld doesn't allow that. While the check
4250 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4251 avoided by not emitting those useless sections in the first place. */
4252 if ((sec->flags & SEC_RELOC) != 0)
4253 {
4254 struct bfd_elf_section_data *esd;
4255 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4256
4257 esd = elf_section_data (sec);
4258 BFD_ASSERT (esd->rel_hdr2 == NULL);
4259 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4260 if (!esd->rel_hdr2)
4261 return FALSE;
4262 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4263 !sec->use_rela_p);
4264 }
4265
4266 return TRUE;
4267}
4268
4269static enum elf_reloc_type_class
4270m32r_elf_reloc_type_class (rela)
4271 const Elf_Internal_Rela *rela;
4272{
4273 switch ((int) ELF32_R_TYPE (rela->r_info))
4274 {
4275 case R_M32R_RELATIVE:
4276 return reloc_class_relative;
4277 case R_M32R_JMP_SLOT:
4278 return reloc_class_plt;
4279 case R_M32R_COPY:
4280 return reloc_class_copy;
4281 default:
4282 return reloc_class_normal;
4283 }
4284}
252b5132 4285\f
252b5132 4286#define ELF_ARCH bfd_arch_m32r
aa4f99bb
AO
4287#define ELF_MACHINE_CODE EM_M32R
4288#define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
e916b64e 4289#define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
252b5132
RH
4290
4291#define TARGET_BIG_SYM bfd_elf32_m32r_vec
4292#define TARGET_BIG_NAME "elf32-m32r"
6edf0760
NC
4293#define TARGET_LITTLE_SYM bfd_elf32_m32rle_vec
4294#define TARGET_LITTLE_NAME "elf32-m32rle"
252b5132 4295
6edf0760 4296#define elf_info_to_howto m32r_info_to_howto
252b5132
RH
4297#define elf_info_to_howto_rel m32r_info_to_howto_rel
4298#define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
4299#define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
4300#define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
4301#define elf_backend_relocate_section m32r_elf_relocate_section
4302#define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
4303#define elf_backend_gc_sweep_hook m32r_elf_gc_sweep_hook
4304#define elf_backend_check_relocs m32r_elf_check_relocs
4305
6edf0760
NC
4306#define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
4307#define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
4308#define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
4309#define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
4310#define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
4311#define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
4312#define elf_backend_reloc_type_class m32r_elf_reloc_type_class
4313#define elf_backend_copy_indirect_symbol m32r_elf_copy_indirect_symbol
4314
252b5132 4315#define elf_backend_can_gc_sections 1
6edf0760 4316/*#if !USE_REL
b491616a 4317#define elf_backend_rela_normal 1
6edf0760
NC
4318#endif*/
4319#define elf_backend_can_refcount 1
4320#define elf_backend_want_got_plt 1
4321#define elf_backend_plt_readonly 1
4322#define elf_backend_want_plt_sym 0
4323#define elf_backend_got_header_size 12
4324
4325#define elf_backend_may_use_rel_p 1
4326#ifdef USE_M32R_OLD_RELOC
4327#define elf_backend_default_use_rela_p 0
4328#define elf_backend_may_use_rela_p 0
4329#else
4330#define elf_backend_default_use_rela_p 1
4331#define elf_backend_may_use_rela_p 1
4332#define elf_backend_fake_sections m32r_elf_fake_sections
b491616a 4333#endif
6edf0760 4334
252b5132
RH
4335#define elf_backend_object_p m32r_elf_object_p
4336#define elf_backend_final_write_processing m32r_elf_final_write_processing
252b5132
RH
4337#define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
4338#define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
4339#define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
2f89ff8d 4340#define elf_backend_special_sections m32r_elf_special_sections
c3668558 4341
252b5132 4342#include "elf32-target.h"
6edf0760
NC
4343
4344#undef ELF_MAXPAGESIZE
4345#define ELF_MAXPAGESIZE 0x1000
4346
4347#undef TARGET_BIG_SYM
4348#define TARGET_BIG_SYM bfd_elf32_m32rlin_vec
4349#undef TARGET_BIG_NAME
4350#define TARGET_BIG_NAME "elf32-m32r-linux"
4351#undef TARGET_LITTLE_SYM
4352#define TARGET_LITTLE_SYM bfd_elf32_m32rlelin_vec
4353#undef TARGET_LITTLE_NAME
4354#define TARGET_LITTLE_NAME "elf32-m32rle-linux"
4355#undef elf32_bed
4356#define elf32_bed elf32_m32r_lin_bed
4357
4358#include "elf32-target.h"
4359
This page took 0.504812 seconds and 4 git commands to generate.