OSDN Git Service

(m32r_reloc_map_old): Use #ifdef USE_M32R_OLD_RELOC to protect the declaration of...
[pf3gnuchains/pf3gnuchains3x.git] / bfd / elf32-m32r.c
1 /* M32R-specific support for 32-bit ELF.
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/m32r.h"
26
27 static bfd_reloc_status_type m32r_elf_10_pcrel_reloc
28   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
29 static bfd_reloc_status_type m32r_elf_do_10_pcrel_reloc
30   PARAMS ((bfd *, reloc_howto_type *, asection *,
31            bfd_byte *, bfd_vma, asection *, bfd_vma, bfd_vma));
32 static bfd_reloc_status_type m32r_elf_hi16_reloc
33   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static void m32r_elf_relocate_hi16
35   PARAMS ((bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *,
36            bfd_byte *, bfd_vma));
37 bfd_reloc_status_type m32r_elf_lo16_reloc
38   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39 bfd_reloc_status_type m32r_elf_generic_reloc
40   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41 static bfd_reloc_status_type m32r_elf_sda16_reloc
42   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
44   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
45 static void m32r_info_to_howto_rel
46   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
47 static void m32r_info_to_howto
48   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
49 bfd_boolean _bfd_m32r_elf_section_from_bfd_section
50   PARAMS ((bfd *, asection *, int *));
51 void _bfd_m32r_elf_symbol_processing
52   PARAMS ((bfd *, asymbol *));
53 static bfd_boolean m32r_elf_add_symbol_hook
54   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
55            const char **, flagword *, asection **, bfd_vma *));
56 static bfd_boolean m32r_elf_relocate_section
57   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
58            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
59 static bfd_reloc_status_type m32r_elf_final_sda_base
60   PARAMS ((bfd *, struct bfd_link_info *, const char **, bfd_vma *));
61 static bfd_boolean m32r_elf_object_p
62   PARAMS ((bfd *));
63 static void m32r_elf_final_write_processing
64   PARAMS ((bfd *, bfd_boolean));
65 static bfd_boolean m32r_elf_set_private_flags
66   PARAMS ((bfd *, flagword));
67 static bfd_boolean m32r_elf_merge_private_bfd_data
68   PARAMS ((bfd *, bfd *));
69 static bfd_boolean m32r_elf_print_private_bfd_data
70   PARAMS ((bfd *, PTR));
71 static bfd_boolean m32r_elf_gc_sweep_hook
72   PARAMS ((bfd *, struct bfd_link_info *, asection *,
73            const Elf_Internal_Rela *));
74 static bfd_boolean m32r_elf_check_relocs
75   PARAMS ((bfd *, struct bfd_link_info *, asection *,
76            const Elf_Internal_Rela *));
77
78 static bfd_boolean m32r_elf_adjust_dynamic_symbol
79   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
80 static bfd_boolean m32r_elf_size_dynamic_sections
81   PARAMS ((bfd *, struct bfd_link_info *));
82
83 asection * m32r_elf_gc_mark_hook
84   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
85            struct elf_link_hash_entry *, Elf_Internal_Sym *));
86
87 static bfd_boolean m32r_elf_create_dynamic_sections
88   PARAMS ((bfd *, struct bfd_link_info *));
89
90 static bfd_boolean m32r_elf_finish_dynamic_sections
91   PARAMS ((bfd *, struct bfd_link_info *));
92
93 static bfd_boolean m32r_elf_finish_dynamic_symbol
94   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
95            Elf_Internal_Sym *));
96
97 static bfd_boolean allocate_dynrelocs
98   PARAMS ((struct elf_link_hash_entry *, PTR));
99 static bfd_boolean readonly_dynrelocs
100   PARAMS ((struct elf_link_hash_entry *, PTR));
101 static enum elf_reloc_type_class m32r_elf_reloc_type_class
102   PARAMS ((const Elf_Internal_Rela *));
103 static bfd_boolean m32r_elf_fake_sections
104   PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
105
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.  */
113 /* #define USE_REL      1
114
115 #ifndef USE_REL
116 #define USE_REL 0
117 #endif */
118 /* Use RELA. But use REL to link old objects for backwords compatibility. */
119
120 /* Functions for the M32R ELF linker.  */
121
122 /* The name of the dynamic interpreter.  This is put in the .interp
123    section.  */
124
125 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
126
127 /* The nop opcode we use.  */
128
129 #define M32R_NOP 0x7000f000
130
131 #define PLT_EMPTY   0x10101010  /* RIE  -> RIE */
132
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
137
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. */
141
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          */
147
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         */
153
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
162
163 static 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 */
170          FALSE,                 /* pc_relative */
171          0,                     /* bitpos */
172          complain_overflow_bitfield, /* complain_on_overflow */
173          bfd_elf_generic_reloc, /* special_function */
174          "R_M32R_NONE",         /* name */
175          FALSE,                 /* partial_inplace */
176          0,                     /* src_mask */
177          0,                     /* dst_mask */
178          FALSE),                /* pcrel_offset */
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 */
185          FALSE,                 /* pc_relative */
186          0,                     /* bitpos */
187          complain_overflow_bitfield, /* complain_on_overflow */
188          m32r_elf_generic_reloc,/* special_function */
189          "R_M32R_16",           /* name */
190          TRUE,                  /* partial_inplace */
191          0xffff,                /* src_mask */
192          0xffff,                /* dst_mask */
193          FALSE),                /* pcrel_offset */
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 */
200          FALSE,                 /* pc_relative */
201          0,                     /* bitpos */
202          complain_overflow_bitfield, /* complain_on_overflow */
203          m32r_elf_generic_reloc,/* special_function */
204          "R_M32R_32",           /* name */
205          TRUE,                  /* partial_inplace */
206          0xffffffff,            /* src_mask */
207          0xffffffff,            /* dst_mask */
208          FALSE),                /* pcrel_offset */
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 */
215          FALSE,                 /* pc_relative */
216          0,                     /* bitpos */
217          complain_overflow_unsigned, /* complain_on_overflow */
218          m32r_elf_generic_reloc,/* special_function */
219          "R_M32R_24",           /* name */
220          TRUE,                  /* partial_inplace */
221          0xffffff,              /* src_mask */
222          0xffffff,              /* dst_mask */
223          FALSE),                /* pcrel_offset */
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 */
237          TRUE,                  /* pc_relative */
238          0,                     /* bitpos */
239          complain_overflow_signed, /* complain_on_overflow */
240          m32r_elf_10_pcrel_reloc, /* special_function */
241          "R_M32R_10_PCREL",     /* name */
242          FALSE,                 /* partial_inplace */
243          0xff,                  /* src_mask */
244          0xff,                  /* dst_mask */
245          TRUE),                 /* pcrel_offset */
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 */
252          TRUE,                  /* pc_relative */
253          0,                     /* bitpos */
254          complain_overflow_signed, /* complain_on_overflow */
255          bfd_elf_generic_reloc, /* special_function */
256          "R_M32R_18_PCREL",     /* name */
257          FALSE,                 /* partial_inplace */
258          0xffff,                /* src_mask */
259          0xffff,                /* dst_mask */
260          TRUE),                 /* pcrel_offset */
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 */
271          TRUE,                  /* pc_relative */
272          0,                     /* bitpos */
273          complain_overflow_signed, /* complain_on_overflow */
274          bfd_elf_generic_reloc, /* special_function */
275          "R_M32R_26_PCREL",     /* name */
276          FALSE,                 /* partial_inplace */
277          0xffffff,              /* src_mask */
278          0xffffff,              /* dst_mask */
279          TRUE),                 /* pcrel_offset */
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 */
286          FALSE,                 /* pc_relative */
287          0,                     /* bitpos */
288          complain_overflow_dont, /* complain_on_overflow */
289          m32r_elf_hi16_reloc,   /* special_function */
290          "R_M32R_HI16_ULO",     /* name */
291          TRUE,                  /* partial_inplace */
292          0x0000ffff,            /* src_mask */
293          0x0000ffff,            /* dst_mask */
294          FALSE),                /* pcrel_offset */
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 */
301          FALSE,                 /* pc_relative */
302          0,                     /* bitpos */
303          complain_overflow_dont, /* complain_on_overflow */
304          m32r_elf_hi16_reloc,   /* special_function */
305          "R_M32R_HI16_SLO",     /* name */
306          TRUE,                  /* partial_inplace */
307          0x0000ffff,            /* src_mask */
308          0x0000ffff,            /* dst_mask */
309          FALSE),                /* pcrel_offset */
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 */
316          FALSE,                 /* pc_relative */
317          0,                     /* bitpos */
318          complain_overflow_dont, /* complain_on_overflow */
319          m32r_elf_lo16_reloc,   /* special_function */
320          "R_M32R_LO16",         /* name */
321          TRUE,                  /* partial_inplace */
322          0x0000ffff,            /* src_mask */
323          0x0000ffff,            /* dst_mask */
324          FALSE),                /* pcrel_offset */
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 */
331          FALSE,                 /* pc_relative */
332          0,                     /* bitpos */
333          complain_overflow_signed, /* complain_on_overflow */
334          m32r_elf_sda16_reloc,  /* special_function */
335          "R_M32R_SDA16",        /* name */
336          TRUE,                  /* partial_inplace */  /* FIXME: correct? */
337          0x0000ffff,            /* src_mask */
338          0x0000ffff,            /* dst_mask */
339          FALSE),                /* pcrel_offset */
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 */
346          FALSE,                 /* pc_relative */
347          0,                     /* bitpos */
348          complain_overflow_dont, /* complain_on_overflow */
349          NULL,                  /* special_function */
350          "R_M32R_GNU_VTINHERIT", /* name */
351          FALSE,                 /* partial_inplace */
352          0,                     /* src_mask */
353          0,                     /* dst_mask */
354          FALSE),                /* pcrel_offset */
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 */
361          FALSE,                 /* pc_relative */
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 */
366          FALSE,                 /* partial_inplace */
367          0,                     /* src_mask */
368          0,                     /* dst_mask */
369          FALSE),                /* pcrel_offset */
370
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 */
668          "R_M32R_RELATIVE",     /* name */
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) */
677          24,                    /* bitsize */
678          FALSE,                 /* pc_relative */
679          0,                     /* bitpos */
680          complain_overflow_bitfield, /* complain_on_overflow */
681          bfd_elf_generic_reloc, /* special_function */
682          "R_M32R_GOTOFF",       /* name */
683          FALSE,                 /* partial_inplace */
684          0xffffff,              /* src_mask */
685          0xffffff,              /* dst_mask */
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 */
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 */
844 };
845 \f
846 /* Handle the R_M32R_10_PCREL reloc.  */
847
848 static bfd_reloc_status_type
849 m32r_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;
857      char ** error_message ATTRIBUTE_UNUSED;
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
887 static bfd_reloc_status_type
888 m32r_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;
895      asection *symbol_section ATTRIBUTE_UNUSED;
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).  */
904   if (offset > bfd_get_section_limit (abfd, input_section))
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.  */
913   relocation -= (offset & -(bfd_vma) 4);
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);
924   bfd_put_16 (abfd, (bfd_vma) x, data + offset);
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
945 struct 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
954 static struct m32r_hi16 *m32r_hi16_list;
955
956 static bfd_reloc_status_type
957 m32r_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
958                      input_section, output_bfd, error_message)
959      bfd *abfd ATTRIBUTE_UNUSED;
960      arelent *reloc_entry;
961      asymbol *symbol;
962      PTR data;
963      asection *input_section;
964      bfd *output_bfd;
965      char **error_message ATTRIBUTE_UNUSED;
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).  */
983   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
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.  */
1001   n = (struct m32r_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
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
1017 static void
1018 m32r_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
1053 bfd_reloc_status_type
1054 m32r_elf_lo16_reloc (input_bfd, reloc_entry, symbol, data,
1055                      input_section, output_bfd, error_message)
1056      bfd *input_bfd;
1057      arelent *reloc_entry;
1058      asymbol *symbol;
1059      PTR data;
1060      asection *input_section;
1061      bfd *output_bfd;
1062      char **error_message;
1063 {
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
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.  */
1090           insn = bfd_get_32 (input_bfd, l->addr);
1091           vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
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
1100           insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1101           bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
1102
1103           next = l->next;
1104           free (l);
1105           l = next;
1106         }
1107
1108       m32r_hi16_list = NULL;
1109     }
1110
1111   /* Now do the LO16 reloc in the usual way.
1112      ??? It would be nice to call bfd_elf_generic_reloc here,
1113      but we have partial_inplace set.  bfd_elf_generic_reloc will
1114      pass the handling back to bfd_install_relocation which will install
1115      a section relative addend which is wrong.  */
1116   return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
1117                                 input_section, output_bfd, error_message);
1118 }
1119
1120 /* Do generic partial_inplace relocation.
1121    This is a local replacement for bfd_elf_generic_reloc.  */
1122
1123 bfd_reloc_status_type
1124 m32r_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;
1132      char **error_message ATTRIBUTE_UNUSED;
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
1149   /* Now do the reloc in the usual way.
1150      ??? It would be nice to call bfd_elf_generic_reloc here,
1151      but we have partial_inplace set.  bfd_elf_generic_reloc will
1152      pass the handling back to bfd_install_relocation which will install
1153      a section relative addend which is wrong.  */
1154
1155   /* Sanity check the address (offset in section).  */
1156   if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
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;
1178   inplace_address = (bfd_byte *) data + reloc_entry->address;
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))
1184
1185   switch (reloc_entry->howto->size)
1186     {
1187     case 1:
1188       {
1189         short x = bfd_get_16 (input_bfd, inplace_address);
1190         DOIT (x);
1191         bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
1192       }
1193       break;
1194     case 2:
1195       {
1196         unsigned long x = bfd_get_32 (input_bfd, inplace_address);
1197         DOIT (x);
1198         bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
1199       }
1200       break;
1201     default:
1202       BFD_ASSERT (0);
1203     }
1204
1205   if (output_bfd != (bfd *) NULL)
1206     reloc_entry->address += input_section->output_offset;
1207
1208   return ret;
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
1217 static bfd_reloc_status_type
1218 m32r_elf_sda16_reloc (abfd, reloc_entry, symbol, data,
1219                       input_section, output_bfd, error_message)
1220      bfd *abfd ATTRIBUTE_UNUSED;
1221      arelent *reloc_entry;
1222      asymbol *symbol;
1223      PTR data ATTRIBUTE_UNUSED;
1224      asection *input_section;
1225      bfd *output_bfd;
1226      char **error_message ATTRIBUTE_UNUSED;
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
1251 struct m32r_reloc_map
1252 {
1253   bfd_reloc_code_real_type bfd_reloc_val;
1254   unsigned char elf_reloc_val;
1255 };
1256
1257 #ifdef USE_M32R_OLD_RELOC
1258 static const struct m32r_reloc_map m32r_reloc_map_old[] =
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 };
1274 #else
1275 static 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 },
1290
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 },
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 },
1308 };
1309 #endif
1310
1311 static reloc_howto_type *
1312 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1313      bfd *abfd ATTRIBUTE_UNUSED;
1314      bfd_reloc_code_real_type code;
1315 {
1316   unsigned int i;
1317
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
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     }
1335 #endif
1336
1337   return NULL;
1338 }
1339
1340 /* Set the howto pointer for an M32R ELF reloc.  */
1341
1342 static void
1343 m32r_info_to_howto_rel (abfd, cache_ptr, dst)
1344      bfd *abfd ATTRIBUTE_UNUSED;
1345      arelent *cache_ptr;
1346      Elf_Internal_Rela *dst;
1347 {
1348   unsigned int r_type;
1349
1350   r_type = ELF32_R_TYPE (dst->r_info);
1351   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) <= (unsigned int) R_M32R_GNU_VTENTRY);
1352   cache_ptr->howto = &m32r_elf_howto_table[r_type];
1353 }
1354
1355 static void
1356 m32r_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
1367 \f
1368 /* Given a BFD section, try to locate the corresponding ELF section
1369    index.  */
1370
1371 bfd_boolean
1372 _bfd_m32r_elf_section_from_bfd_section (abfd, sec, retval)
1373      bfd *abfd ATTRIBUTE_UNUSED;
1374      asection *sec;
1375      int *retval;
1376 {
1377   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1378     {
1379       *retval = SHN_M32R_SCOMMON;
1380       return TRUE;
1381     }
1382   return FALSE;
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.  */
1391 static asection m32r_elf_scom_section;
1392 static asymbol m32r_elf_scom_symbol;
1393 static asymbol *m32r_elf_scom_symbol_ptr;
1394
1395 /* Handle the special M32R section numbers that a symbol may use.  */
1396
1397 void
1398 _bfd_m32r_elf_symbol_processing (abfd, asym)
1399      bfd *abfd ATTRIBUTE_UNUSED;
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
1433 static bfd_boolean
1434 m32r_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1435      bfd *abfd;
1436      struct bfd_link_info *info;
1437      Elf_Internal_Sym *sym;
1438      const char **namep;
1439      flagword *flagsp ATTRIBUTE_UNUSED;
1440      asection **secp;
1441      bfd_vma *valp;
1442 {
1443   if (! info->relocatable
1444       && (*namep)[0] == '_' && (*namep)[1] == 'S'
1445       && strcmp (*namep, "_SDA_BASE_") == 0
1446       && is_elf_hash_table (info->hash))
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;
1454       struct bfd_link_hash_entry *bh;
1455       asection *s = bfd_get_section_by_name (abfd, ".sdata");
1456
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
1464           s = bfd_make_section_anyway (abfd, ".sdata");
1465           if (s == NULL)
1466             return FALSE;
1467           bfd_set_section_flags (abfd, s, flags);
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
1508 static bfd_reloc_status_type
1509 m32r_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
1550 struct 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
1566 struct 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
1583 struct 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
1598 struct 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.  */
1630 static struct bfd_hash_entry *
1631 m32r_elf_link_hash_newfunc (struct bfd_hash_entry *, struct bfd_hash_table *,
1632                             const char * );
1633
1634 static struct bfd_hash_entry *
1635 m32r_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;
1642
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;
1651
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;
1659
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     }
1665
1666   return (struct bfd_hash_entry *) ret;
1667 }
1668
1669 /* Create an m32r ELF linker hash table.  */
1670 static struct bfd_link_hash_table *m32r_elf_link_hash_table_create (bfd *);
1671
1672 static struct bfd_link_hash_table *
1673 m32r_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);
1678
1679   ret = (struct elf_m32r_link_hash_table *) bfd_malloc (amt);
1680   if (ret == (struct elf_m32r_link_hash_table *) NULL)
1681     return NULL;
1682
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     }
1689
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;
1698
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.  */
1704 static bfd_boolean create_got_section (bfd *, struct bfd_link_info *);
1705
1706 static bfd_boolean
1707 create_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
1722   htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1723   if (htab->srelgot == NULL
1724       || ! bfd_set_section_flags (dynobj, htab->srelgot,
1725                                   (SEC_ALLOC
1726                                    | SEC_LOAD
1727                                    | SEC_HAS_CONTENTS
1728                                    | SEC_IN_MEMORY
1729                                    | SEC_LINKER_CREATED
1730                                    | SEC_READONLY))
1731       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1732     return FALSE;
1733
1734   return TRUE;
1735 }
1736
1737 /* Create dynamic sections when linking against a dynamic object.  */
1738
1739 static bfd_boolean
1740 m32r_elf_create_dynamic_sections (abfd, info)
1741      bfd *abfd;
1742      struct bfd_link_info *info;
1743 {
1744   struct elf_m32r_link_hash_table *htab;
1745   flagword flags, pltflags;
1746   register asection *s;
1747   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1748   int ptralign = 2; /* 32bit */
1749
1750   htab = m32r_elf_hash_table (info);
1751
1752   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
1753      .rel[a].bss sections.  */
1754
1755   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1756            | SEC_LINKER_CREATED);
1757
1758   pltflags = flags;
1759   pltflags |= SEC_CODE;
1760   if (bed->plt_not_loaded)
1761     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
1762   if (bed->plt_readonly)
1763     pltflags |= SEC_READONLY;
1764
1765   s = bfd_make_section (abfd, ".plt");
1766   htab->splt = s;
1767   if (s == NULL
1768       || ! bfd_set_section_flags (abfd, s, pltflags)
1769       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
1770     return FALSE;
1771
1772   if (bed->want_plt_sym)
1773     {
1774       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
1775          .plt section.  */
1776       struct bfd_link_hash_entry *bh = NULL;
1777       struct elf_link_hash_entry *h;
1778       if (! (_bfd_generic_link_add_one_symbol
1779              (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
1780               (bfd_vma) 0, (const char *) NULL, FALSE,
1781               get_elf_backend_data (abfd)->collect, &bh)))
1782         return FALSE;
1783       h = (struct elf_link_hash_entry *) bh;
1784       h->def_regular = 1;
1785       h->type = STT_OBJECT;
1786
1787       if (info->shared
1788           && ! bfd_elf_link_record_dynamic_symbol (info, h))
1789         return FALSE;
1790     }
1791
1792   s = bfd_make_section (abfd,
1793                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
1794   htab->srelplt = s;
1795   if (s == NULL
1796       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1797       || ! bfd_set_section_alignment (abfd, s, ptralign))
1798     return FALSE;
1799
1800   if (htab->sgot == NULL
1801       && ! create_got_section (abfd, info))
1802     return FALSE;
1803
1804   {
1805     const char *secname;
1806     char *relname;
1807     flagword secflags;
1808     asection *sec;
1809
1810     for (sec = abfd->sections; sec; sec = sec->next)
1811       {
1812         secflags = bfd_get_section_flags (abfd, sec);
1813         if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
1814             || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
1815           continue;
1816         secname = bfd_get_section_name (abfd, sec);
1817         relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
1818         strcpy (relname, ".rela");
1819         strcat (relname, secname);
1820         if (bfd_get_section_by_name (abfd, secname))
1821           continue;
1822         s = bfd_make_section (abfd, relname);
1823         if (s == NULL
1824             || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1825             || ! bfd_set_section_alignment (abfd, s, ptralign))
1826           return FALSE;
1827       }
1828   }
1829
1830   if (bed->want_dynbss)
1831     {
1832       /* The .dynbss section is a place to put symbols which are defined
1833          by dynamic objects, are referenced by regular objects, and are
1834          not functions.  We must allocate space for them in the process
1835          image and use a R_*_COPY reloc to tell the dynamic linker to
1836          initialize them at run time.  The linker script puts the .dynbss
1837          section into the .bss section of the final image.  */
1838       s = bfd_make_section (abfd, ".dynbss");
1839       htab->sdynbss = s;
1840       if (s == NULL
1841           || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1842         return FALSE;
1843       /* The .rel[a].bss section holds copy relocs.  This section is not
1844          normally needed.  We need to create it here, though, so that the
1845          linker will map it to an output section.  We can't just create it
1846          only if we need it, because we will not know whether we need it
1847          until we have seen all the input files, and the first time the
1848          main linker code calls BFD after examining all the input files
1849          (size_dynamic_sections) the input sections have already been
1850          mapped to the output sections.  If the section turns out not to
1851          be needed, we can discard it later.  We will never need this
1852          section when generating a shared object, since they do not use
1853          copy relocs.  */
1854       if (! info->shared)
1855         {
1856           s = bfd_make_section (abfd,
1857                                 (bed->default_use_rela_p
1858                                  ? ".rela.bss" : ".rel.bss"));
1859           htab->srelbss = s;
1860           if (s == NULL
1861               || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1862               || ! bfd_set_section_alignment (abfd, s, ptralign))
1863             return FALSE;
1864         }
1865     }
1866
1867   return TRUE;
1868 }
1869
1870 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1871 static void m32r_elf_copy_indirect_symbol (const struct elf_backend_data *,
1872                                            struct elf_link_hash_entry *,
1873                                            struct elf_link_hash_entry *);
1874
1875 static void
1876 m32r_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
1877                                struct elf_link_hash_entry *dir,
1878                                struct elf_link_hash_entry *ind)
1879 {
1880   struct elf_m32r_link_hash_entry *edir, *eind;
1881
1882   edir = (struct elf_m32r_link_hash_entry *) dir;
1883   eind = (struct elf_m32r_link_hash_entry *) ind;
1884
1885   if (eind->dyn_relocs != NULL)
1886     {
1887       if (edir->dyn_relocs != NULL)
1888         {
1889           struct elf_m32r_dyn_relocs **pp;
1890           struct elf_m32r_dyn_relocs *p;
1891
1892           if (ind->root.type == bfd_link_hash_indirect)
1893             abort ();
1894
1895           /* Add reloc counts against the weak sym to the strong sym
1896              list.  Merge any entries against the same section.  */
1897           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1898             {
1899               struct elf_m32r_dyn_relocs *q;
1900
1901               for (q = edir->dyn_relocs; q != NULL; q = q->next)
1902                 if (q->sec == p->sec)
1903                   {
1904                     q->pc_count += p->pc_count;
1905                     q->count += p->count;
1906                     *pp = p->next;
1907                     break;
1908                   }
1909               if (q == NULL)
1910                 pp = &p->next;
1911             }
1912           *pp = edir->dyn_relocs;
1913         }
1914
1915       edir->dyn_relocs = eind->dyn_relocs;
1916       eind->dyn_relocs = NULL;
1917     }
1918
1919 //  if (ind->root.type == bfd_link_hash_indirect
1920 //      && dir->got.refcount <= 0)
1921 //    {
1922 //      edir->tls_type = eind->tls_type;
1923 //      eind->tls_type = GOT_UNKNOWN;
1924 //    }
1925   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1926 }
1927
1928 \f
1929 /* Adjust a symbol defined by a dynamic object and referenced by a
1930    regular object.  The current definition is in some section of the
1931    dynamic object, but we're not including those sections.  We have to
1932    change the definition to something the rest of the link can
1933    understand.  */
1934
1935 static bfd_boolean
1936 m32r_elf_adjust_dynamic_symbol (info, h)
1937      struct bfd_link_info *info;
1938      struct elf_link_hash_entry *h;
1939 {
1940   struct elf_m32r_link_hash_table *htab;
1941   struct elf_m32r_link_hash_entry *eh;
1942   struct elf_m32r_dyn_relocs *p;
1943   bfd *dynobj;
1944   asection *s;
1945   unsigned int power_of_two;
1946
1947 #ifdef DEBUG_PIC
1948 printf("m32r_elf_adjust_dynamic_symbol()\n");
1949 #endif
1950
1951   dynobj = elf_hash_table (info)->dynobj;
1952
1953   /* Make sure we know what is going on here.  */
1954   BFD_ASSERT (dynobj != NULL
1955               && (h->needs_plt
1956                   || h->u.weakdef != NULL
1957                   || (h->def_dynamic
1958                       && h->ref_regular
1959                       && !h->def_regular)));
1960
1961
1962   /* If this is a function, put it in the procedure linkage table.  We
1963      will fill in the contents of the procedure linkage table later,
1964      when we know the address of the .got section.  */
1965   if (h->type == STT_FUNC
1966       || h->needs_plt)
1967     {
1968       if (! info->shared
1969           && !h->def_dynamic
1970           && !h->ref_dynamic
1971           && h->root.type != bfd_link_hash_undefweak
1972           && h->root.type != bfd_link_hash_undefined)
1973         {
1974           /* This case can occur if we saw a PLT reloc in an input
1975              file, but the symbol was never referred to by a dynamic
1976              object.  In such a case, we don't actually need to build
1977              a procedure linkage table, and we can just do a PCREL
1978              reloc instead.  */
1979           h->plt.offset = (bfd_vma) -1;
1980           h->needs_plt = 0;
1981         }
1982
1983       return TRUE;
1984     }
1985   else
1986     h->plt.offset = (bfd_vma) -1;
1987
1988   /* If this is a weak symbol, and there is a real definition, the
1989      processor independent code will have arranged for us to see the
1990      real definition first, and we can just use the same value.  */
1991   if (h->u.weakdef != NULL)
1992     {
1993       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1994                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1995       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1996       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1997       return TRUE;
1998     }
1999
2000   /* This is a reference to a symbol defined by a dynamic object which
2001      is not a function.  */
2002
2003   /* If we are creating a shared library, we must presume that the
2004      only references to the symbol are via the global offset table.
2005      For such cases we need not do anything here; the relocations will
2006      be handled correctly by relocate_section.  */
2007   if (info->shared)
2008     return TRUE;
2009
2010   /* If there are no references to this symbol that do not use the
2011      GOT, we don't need to generate a copy reloc.  */
2012   if (!h->non_got_ref)
2013     return TRUE;
2014
2015   /* If -z nocopyreloc was given, we won't generate them either.  */
2016   if (info->nocopyreloc)
2017     {
2018       h->non_got_ref = 0;
2019       return TRUE;
2020     }
2021
2022   eh = (struct elf_m32r_link_hash_entry *) h;
2023   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2024     {
2025       s = p->sec->output_section;
2026       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2027         break;
2028     }
2029
2030   /* If we didn't find any dynamic relocs in sections which needs the
2031      copy reloc, then we'll be keeping the dynamic relocs and avoiding
2032      the copy reloc.  */
2033   if (p == NULL)
2034     {
2035       h->non_got_ref = 0;
2036       return TRUE;
2037     }
2038
2039   /* We must allocate the symbol in our .dynbss section, which will
2040      become part of the .bss section of the executable.  There will be
2041      an entry for this symbol in the .dynsym section.  The dynamic
2042      object will contain position independent code, so all references
2043      from the dynamic object to this symbol will go through the global
2044      offset table.  The dynamic linker will use the .dynsym entry to
2045      determine the address it must put in the global offset table, so
2046      both the dynamic object and the regular object will refer to the
2047      same memory location for the variable.  */
2048
2049   htab = m32r_elf_hash_table (info);
2050   s = htab->sdynbss;
2051   BFD_ASSERT (s != NULL);
2052
2053   /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
2054      to copy the initial value out of the dynamic object and into the
2055      runtime process image.  We need to remember the offset into the
2056      .rela.bss section we are going to use.  */
2057   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2058     {
2059       asection *srel;
2060
2061       srel = htab->srelbss;
2062       BFD_ASSERT (srel != NULL);
2063       srel->size += sizeof (Elf32_External_Rela);
2064       h->needs_copy = 1;
2065     }
2066
2067   /* We need to figure out the alignment required for this symbol.  I
2068      have no idea how ELF linkers handle this.  */
2069   power_of_two = bfd_log2 (h->size);
2070   if (power_of_two > 3)
2071     power_of_two = 3;
2072
2073   /* Apply the required alignment.  */
2074   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2075   if (power_of_two > bfd_get_section_alignment (dynobj, s))
2076     {
2077       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2078         return FALSE;
2079     }
2080
2081   /* Define the symbol as being at this point in the section.  */
2082   h->root.u.def.section = s;
2083   h->root.u.def.value = s->size;
2084
2085   /* Increment the section size to make room for the symbol.  */
2086   s->size += h->size;
2087
2088   return TRUE;
2089 }
2090
2091 /* Allocate space in .plt, .got and associated reloc sections for
2092    dynamic relocs.  */
2093
2094 static bfd_boolean
2095 allocate_dynrelocs (h, inf)
2096      struct elf_link_hash_entry *h;
2097      PTR inf;
2098 {
2099   struct bfd_link_info *info;
2100   struct elf_m32r_link_hash_table *htab;
2101   struct elf_m32r_link_hash_entry *eh;
2102   struct elf_m32r_dyn_relocs *p;
2103
2104   if (h->root.type == bfd_link_hash_indirect)
2105     return TRUE;
2106
2107   if (h->root.type == bfd_link_hash_warning)
2108     /* When warning symbols are created, they **replace** the "real"
2109        entry in the hash table, thus we never get to see the real
2110        symbol in a hash traversal.  So look at it now.  */
2111     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2112
2113   info = (struct bfd_link_info *) inf;
2114   htab = m32r_elf_hash_table (info);
2115
2116   eh = (struct elf_m32r_link_hash_entry *) h;
2117 //  if ((h->got.refcount > 0
2118 //      || h->forced_local)
2119 //      && eh->gotplt_refcount > 0)
2120 //    {
2121 //      /* The symbol has been forced local, or we have some direct got refs,
2122 //         so treat all the gotplt refs as got refs. */
2123 //      h->got.refcount += eh->gotplt_refcount;
2124 //      if (h->plt.refcount >= eh->gotplt_refcount)
2125 //        h->plt.refcount -= eh->gotplt_refcount;
2126 //    }
2127
2128   if (htab->root.dynamic_sections_created
2129       && h->plt.refcount > 0)
2130     {
2131       /* Make sure this symbol is output as a dynamic symbol.
2132          Undefined weak syms won't yet be marked as dynamic.  */
2133       if (h->dynindx == -1
2134           && !h->forced_local)
2135         {
2136           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2137             return FALSE;
2138         }
2139
2140       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2141         {
2142           asection *s = htab->splt;
2143
2144           /* If this is the first .plt entry, make room for the special
2145              first entry.  */
2146           if (s->size == 0)
2147             s->size += PLT_ENTRY_SIZE;
2148
2149           h->plt.offset = s->size;
2150
2151           /* If this symbol is not defined in a regular file, and we are
2152              not generating a shared library, then set the symbol to this
2153              location in the .plt.  This is required to make function
2154              pointers compare as equal between the normal executable and
2155              the shared library.  */
2156           if (! info->shared
2157               && !h->def_regular)
2158             {
2159               h->root.u.def.section = s;
2160               h->root.u.def.value = h->plt.offset;
2161             }
2162
2163           /* Make room for this entry.  */
2164           s->size += PLT_ENTRY_SIZE;
2165
2166           /* We also need to make an entry in the .got.plt section, which
2167              will be placed in the .got section by the linker script.  */
2168           htab->sgotplt->size += 4;
2169
2170           /* We also need to make an entry in the .rel.plt section.  */
2171           htab->srelplt->size += sizeof (Elf32_External_Rela);
2172         }
2173       else
2174         {
2175           h->plt.offset = (bfd_vma) -1;
2176           h->needs_plt = 0;
2177         }
2178     }
2179   else
2180     {
2181       h->plt.offset = (bfd_vma) -1;
2182       h->needs_plt = 0;
2183     }
2184
2185   if (h->got.refcount > 0)
2186     {
2187       asection *s;
2188       bfd_boolean dyn;
2189
2190       /* Make sure this symbol is output as a dynamic symbol.
2191          Undefined weak syms won't yet be marked as dynamic.  */
2192       if (h->dynindx == -1
2193           && !h->forced_local)
2194         {
2195           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2196             return FALSE;
2197         }
2198
2199       s = htab->sgot;
2200
2201       h->got.offset = s->size;
2202       s->size += 4;
2203       dyn = htab->root.dynamic_sections_created;
2204       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
2205         htab->srelgot->size += sizeof (Elf32_External_Rela);
2206     }
2207   else
2208     h->got.offset = (bfd_vma) -1;
2209
2210   if (eh->dyn_relocs == NULL)
2211     return TRUE;
2212
2213   /* In the shared -Bsymbolic case, discard space allocated for
2214      dynamic pc-relative relocs against symbols which turn out to be
2215      defined in regular objects.  For the normal shared case, discard
2216      space for pc-relative relocs that have become local due to symbol
2217      visibility changes.  */
2218
2219   if (info->shared)
2220     {
2221       if (h->def_regular
2222           && (h->forced_local
2223               || info->symbolic))
2224         {
2225           struct elf_m32r_dyn_relocs **pp;
2226           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2227             {
2228               p->count -= p->pc_count;
2229               p->pc_count = 0;
2230               if (p->count == 0)
2231                 *pp = p->next;
2232               else
2233                 pp = &p->next;
2234             }
2235         }
2236     }
2237   else
2238     {
2239       /* For the non-shared case, discard space for relocs against
2240          symbols which turn out to need copy relocs or are not
2241          dynamic.  */
2242
2243       if (!h->non_got_ref
2244           && ((h->def_dynamic
2245                && !h->def_regular)
2246               || (htab->root.dynamic_sections_created
2247                   && (h->root.type == bfd_link_hash_undefweak
2248                       || h->root.type == bfd_link_hash_undefined))))
2249         {
2250           /* Make sure this symbol is output as a dynamic symbol.
2251              Undefined weak syms won't yet be marked as dynamic.  */
2252           if (h->dynindx == -1
2253               && !h->forced_local)
2254             {
2255               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2256                 return FALSE;
2257             }
2258
2259           /* If that succeeded, we know we'll be keeping all the
2260              relocs.  */
2261           if (h->dynindx != -1)
2262             goto keep;
2263         }
2264
2265       eh->dyn_relocs = NULL;
2266
2267     keep: ;
2268     }
2269
2270   /* Finally, allocate space.  */
2271   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2272     {
2273       asection *sreloc = elf_section_data (p->sec)->sreloc;
2274       sreloc->size += p->count * sizeof (Elf32_External_Rela);
2275     }
2276
2277   return TRUE;
2278 }
2279 /* Find any dynamic relocs that apply to read-only sections.  */
2280
2281 static bfd_boolean
2282 readonly_dynrelocs (h, inf)
2283      struct elf_link_hash_entry *h;
2284      PTR inf;
2285 {
2286   struct elf_m32r_link_hash_entry *eh;
2287   struct elf_m32r_dyn_relocs *p;
2288
2289   if (h->root.type == bfd_link_hash_warning)
2290     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2291
2292   eh = (struct elf_m32r_link_hash_entry *) h;
2293   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2294     {
2295       asection *s = p->sec->output_section;
2296
2297       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2298         {
2299           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2300
2301           info->flags |= DF_TEXTREL;
2302
2303           /* Not an error, just cut short the traversal.  */
2304           return FALSE;
2305         }
2306     }
2307   return TRUE;
2308 }
2309
2310 /* Set the sizes of the dynamic sections.  */
2311
2312 static bfd_boolean
2313 m32r_elf_size_dynamic_sections (output_bfd, info)
2314      bfd *output_bfd ATTRIBUTE_UNUSED;
2315      struct bfd_link_info *info;
2316 {
2317   struct elf_m32r_link_hash_table *htab;
2318   bfd *dynobj;
2319   asection *s;
2320   bfd_boolean relocs;
2321   bfd *ibfd;
2322
2323 #ifdef DEBUG_PIC
2324 printf("m32r_elf_size_dynamic_sections()\n");
2325 #endif
2326
2327   htab = m32r_elf_hash_table (info);
2328   dynobj = htab->root.dynobj;
2329   BFD_ASSERT (dynobj != NULL);
2330
2331   if (htab->root.dynamic_sections_created)
2332     {
2333       /* Set the contents of the .interp section to the interpreter.  */
2334       if (! info->shared)
2335         {
2336           s = bfd_get_section_by_name (dynobj, ".interp");
2337           BFD_ASSERT (s != NULL);
2338           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2339           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2340         }
2341     }
2342
2343   /* Set up .got offsets for local syms, and space for local dynamic
2344      relocs.  */
2345   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2346     {
2347       bfd_signed_vma *local_got;
2348       bfd_signed_vma *end_local_got;
2349       bfd_size_type locsymcount;
2350       Elf_Internal_Shdr *symtab_hdr;
2351       asection *srel;
2352
2353       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2354         continue;
2355
2356       for (s = ibfd->sections; s != NULL; s = s->next)
2357         {
2358           struct elf_m32r_dyn_relocs *p;
2359
2360           for (p = ((struct elf_m32r_dyn_relocs *)
2361                     elf_section_data (s)->local_dynrel);
2362                p != NULL;
2363                p = p->next)
2364             {
2365               if (! bfd_is_abs_section (p->sec)
2366                   && bfd_is_abs_section (p->sec->output_section))
2367                 {
2368                   /* Input section has been discarded, either because
2369                      it is a copy of a linkonce section or due to
2370                      linker script /DISCARD/, so we'll be discarding
2371                      the relocs too.  */
2372                 }
2373               else if (p->count != 0)
2374                 {
2375                   srel = elf_section_data (p->sec)->sreloc;
2376                   srel->size += p->count * sizeof (Elf32_External_Rela);
2377                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2378                     info->flags |= DF_TEXTREL;
2379                 }
2380             }
2381         }
2382
2383       local_got = elf_local_got_refcounts (ibfd);
2384       if (!local_got)
2385         continue;
2386
2387       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2388       locsymcount = symtab_hdr->sh_info;
2389       end_local_got = local_got + locsymcount;
2390       s = htab->sgot;
2391       srel = htab->srelgot;
2392       for (; local_got < end_local_got; ++local_got)
2393         {
2394           if (*local_got > 0)
2395             {
2396               *local_got = s->size;
2397               s->size += 4;
2398               if (info->shared)
2399                 srel->size += sizeof (Elf32_External_Rela);
2400             }
2401           else
2402             *local_got = (bfd_vma) -1;
2403         }
2404     }
2405
2406   /* Allocate global sym .plt and .got entries, and space for global
2407      sym dynamic relocs.  */
2408   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
2409
2410   /* We now have determined the sizes of the various dynamic sections.
2411      Allocate memory for them.  */
2412   relocs = FALSE;
2413   for (s = dynobj->sections; s != NULL; s = s->next)
2414     {
2415       if ((s->flags & SEC_LINKER_CREATED) == 0)
2416         continue;
2417
2418       if (s == htab->splt
2419           || s == htab->sgot
2420           || s == htab->sgotplt)
2421         {
2422           /* Strip this section if we don't need it; see the
2423              comment below.  */
2424         }
2425       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2426         {
2427           if (s->size != 0 && s != htab->srelplt)
2428             relocs = TRUE;
2429
2430           /* We use the reloc_count field as a counter if we need
2431              to copy relocs into the output file.  */
2432           s->reloc_count = 0;
2433         }
2434       else
2435         {
2436           /* It's not one of our sections, so don't allocate space.  */
2437           continue;
2438         }
2439
2440       if (s->size == 0)
2441         {
2442           /* If we don't need this section, strip it from the
2443              output file.  This is mostly to handle .rela.bss and
2444              .rela.plt.  We must create both sections in
2445              create_dynamic_sections, because they must be created
2446              before the linker maps input sections to output
2447              sections.  The linker does that before
2448              adjust_dynamic_symbol is called, and it is that
2449              function which decides whether anything needs to go
2450              into these sections.  */
2451           _bfd_strip_section_from_output (info, s);
2452           continue;
2453         }
2454
2455       /* Allocate memory for the section contents.  We use bfd_zalloc
2456          here in case unused entries are not reclaimed before the
2457          section's contents are written out.  This should not happen,
2458          but this way if it does, we get a R_M32R_NONE reloc instead
2459          of garbage.  */
2460       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2461       if (s->contents == NULL)
2462         return FALSE;
2463     }
2464
2465   if (htab->root.dynamic_sections_created)
2466     {
2467       /* Add some entries to the .dynamic section.  We fill in the
2468          values later, in m32r_elf_finish_dynamic_sections, but we
2469          must add the entries now so that we get the correct size for
2470          the .dynamic section.  The DT_DEBUG entry is filled in by the
2471          dynamic linker and used by the debugger.  */
2472 #define add_dynamic_entry(TAG, VAL) \
2473   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2474
2475       if (! info->shared)
2476         {
2477           if (! add_dynamic_entry (DT_DEBUG, 0))
2478             return FALSE;
2479         }
2480
2481       if (htab->splt->size != 0)
2482         {
2483           if (! add_dynamic_entry (DT_PLTGOT, 0)
2484               || ! add_dynamic_entry (DT_PLTRELSZ, 0)
2485               || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
2486               || ! add_dynamic_entry (DT_JMPREL, 0))
2487             return FALSE;
2488         }
2489
2490       if (relocs)
2491         {
2492           if (! add_dynamic_entry (DT_RELA, 0)
2493               || ! add_dynamic_entry (DT_RELASZ, 0)
2494               || ! add_dynamic_entry (DT_RELAENT,
2495                                       sizeof (Elf32_External_Rela)))
2496             return FALSE;
2497
2498           /* If any dynamic relocs apply to a read-only section,
2499              then we need a DT_TEXTREL entry.  */
2500           if ((info->flags & DF_TEXTREL) == 0)
2501             elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
2502                                     (PTR) info);
2503
2504           if ((info->flags & DF_TEXTREL) != 0)
2505             {
2506               if (! add_dynamic_entry (DT_TEXTREL, 0))
2507                 return FALSE;
2508             }
2509         }
2510     }
2511 #undef add_dynamic_entry
2512
2513   return TRUE;
2514 }
2515 /* Relocate an M32R/D ELF section.
2516    There is some attempt to make this function usable for many architectures,
2517    both for RELA and REL type relocs, if only to serve as a learning tool.
2518
2519    The RELOCATE_SECTION function is called by the new ELF backend linker
2520    to handle the relocations for a section.
2521
2522    The relocs are always passed as Rela structures; if the section
2523    actually uses Rel structures, the r_addend field will always be
2524    zero.
2525
2526    This function is responsible for adjust the section contents as
2527    necessary, and (if using Rela relocs and generating a
2528    relocatable output file) adjusting the reloc addend as
2529    necessary.
2530
2531    This function does not have to worry about setting the reloc
2532    address or the reloc symbol index.
2533
2534    LOCAL_SYMS is a pointer to the swapped in local symbols.
2535
2536    LOCAL_SECTIONS is an array giving the section in the input file
2537    corresponding to the st_shndx field of each local symbol.
2538
2539    The global hash table entry for the global symbols can be found
2540    via elf_sym_hashes (input_bfd).
2541
2542    When generating relocatable output, this function must handle
2543    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2544    going to be the section symbol corresponding to the output
2545    section, which means that the addend must be adjusted
2546    accordingly.  */
2547
2548 static bfd_boolean
2549 m32r_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2550                            contents, relocs, local_syms, local_sections)
2551      bfd *output_bfd ATTRIBUTE_UNUSED;
2552      struct bfd_link_info *info;
2553      bfd *input_bfd;
2554      asection *input_section;
2555      bfd_byte *contents;
2556      Elf_Internal_Rela *relocs;
2557      Elf_Internal_Sym *local_syms;
2558      asection **local_sections;
2559 {
2560   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2561   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2562   Elf_Internal_Rela *rel, *relend;
2563   /* Assume success.  */
2564   bfd_boolean ret = TRUE;
2565
2566   struct elf_m32r_link_hash_table *htab = m32r_elf_hash_table (info);
2567   bfd *dynobj;
2568   bfd_vma *local_got_offsets;
2569   asection *sgot, *splt, *sreloc;
2570   bfd_vma high_address = bfd_get_section_limit (input_bfd, input_section);
2571
2572   dynobj = htab->root.dynobj;
2573   local_got_offsets = elf_local_got_offsets (input_bfd);
2574
2575   sgot = htab->sgot;
2576   splt = htab->splt;
2577   sreloc = NULL;
2578
2579   rel = relocs;
2580   relend = relocs + input_section->reloc_count;
2581   for (; rel < relend; rel++)
2582     {
2583       int r_type;
2584       reloc_howto_type *howto;
2585       unsigned long r_symndx;
2586       struct elf_link_hash_entry *h;
2587       /* We can't modify r_addend here as elf_link_input_bfd has an assert to
2588          ensure it's zero (we use REL relocs, not RELA).  Therefore this
2589          should be assigning zero to `addend', but for clarity we use
2590          `r_addend'.  */
2591       bfd_vma addend = rel->r_addend;
2592       bfd_vma offset = rel->r_offset;
2593       Elf_Internal_Sym *sym;
2594       asection *sec;
2595       const char *sym_name;
2596       bfd_reloc_status_type r;
2597       const char *errmsg = NULL;
2598       bfd_boolean use_rel = FALSE;
2599
2600       h = NULL;
2601       r_type = ELF32_R_TYPE (rel->r_info);
2602       if (r_type < 0 || r_type >= (int) R_M32R_max)
2603         {
2604           (*_bfd_error_handler) (_("%B: unknown relocation type %d"),
2605                                  input_bfd,
2606                                  (int) r_type);
2607           bfd_set_error (bfd_error_bad_value);
2608           ret = FALSE;
2609           continue;
2610         }
2611
2612       if (r_type == R_M32R_GNU_VTENTRY
2613           || r_type == R_M32R_GNU_VTINHERIT
2614           || r_type == R_M32R_NONE
2615           || r_type == R_M32R_RELA_GNU_VTENTRY
2616           || r_type == R_M32R_RELA_GNU_VTINHERIT)
2617         continue;
2618
2619       if (r_type <= R_M32R_GNU_VTENTRY)
2620         use_rel = TRUE;
2621
2622       howto = m32r_elf_howto_table + r_type;
2623       r_symndx = ELF32_R_SYM (rel->r_info);
2624
2625       if (info->relocatable && use_rel)
2626         {
2627           /* This is a relocatable link.  We don't have to change
2628              anything, unless the reloc is against a section symbol,
2629              in which case we have to adjust according to where the
2630              section symbol winds up in the output section.  */
2631           sec = NULL;
2632           if (r_symndx >= symtab_hdr->sh_info)
2633             {
2634               /* External symbol.  */
2635               continue;
2636             }
2637
2638           /* Local symbol.  */
2639           sym = local_syms + r_symndx;
2640           sym_name = "<local symbol>";
2641           /* STT_SECTION: symbol is associated with a section.  */
2642           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2643             {
2644               /* Symbol isn't associated with a section.  Nothing to do.  */
2645               continue;
2646             }
2647
2648           sec = local_sections[r_symndx];
2649           addend += sec->output_offset + sym->st_value;
2650
2651           /* If partial_inplace, we need to store any additional addend
2652              back in the section.  */
2653           if (! howto->partial_inplace)
2654             continue;
2655           /* ??? Here is a nice place to call a special_function
2656              like handler.  */
2657           if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
2658             r = _bfd_relocate_contents (howto, input_bfd,
2659                                         addend, contents + offset);
2660           else
2661             {
2662               Elf_Internal_Rela *lorel;
2663
2664               /* We allow an arbitrary number of HI16 relocs before the
2665                  LO16 reloc.  This permits gcc to emit the HI and LO relocs
2666                  itself.  */
2667               for (lorel = rel + 1;
2668                    (lorel < relend
2669                     && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
2670                         || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
2671                    lorel++)
2672                 continue;
2673               if (lorel < relend
2674                   && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
2675                 {
2676                   m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
2677                                           contents, addend);
2678                   r = bfd_reloc_ok;
2679                 }
2680               else
2681                 r = _bfd_relocate_contents (howto, input_bfd,
2682                                             addend, contents + offset);
2683             }
2684         }
2685       else
2686         {
2687           bfd_vma relocation;
2688
2689           /* This is a final link.  */
2690           sym = NULL;
2691           sec = NULL;
2692           h = NULL;
2693
2694           if (r_symndx < symtab_hdr->sh_info)
2695             {
2696               /* Local symbol.  */
2697               sym = local_syms + r_symndx;
2698               sec = local_sections[r_symndx];
2699               sym_name = "<local symbol>";
2700
2701               if (!use_rel)
2702                 {
2703                   relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2704                   addend = rel->r_addend;
2705
2706                   if (info->relocatable)
2707                     {
2708                       /* This is a relocatable link.  We don't have to change
2709                          anything, unless the reloc is against a section symbol,
2710                          in which case we have to adjust according to where the
2711                          section symbol winds up in the output section.  */
2712                       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2713                         rel->r_addend += sec->output_offset + sym->st_value;
2714
2715                       continue;
2716                     }
2717                 }
2718               else
2719                 {
2720                   relocation = (sec->output_section->vma
2721                                 + sec->output_offset
2722                                 + sym->st_value);
2723                 }
2724             }
2725           else
2726             {
2727               /* External symbol.  */
2728               if (info->relocatable && !use_rel)
2729                 continue;
2730
2731               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2732               while (h->root.type == bfd_link_hash_indirect
2733                      || h->root.type == bfd_link_hash_warning)
2734                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2735               sym_name = h->root.root.string;
2736
2737               if (h->root.type == bfd_link_hash_defined
2738                   || h->root.type == bfd_link_hash_defweak)
2739                 {
2740                   bfd_boolean dyn;
2741                   sec = h->root.u.def.section;
2742
2743                   dyn = htab->root.dynamic_sections_created;
2744                   sec = h->root.u.def.section;
2745                   if (r_type == R_M32R_GOTPC24
2746                       || (r_type == R_M32R_GOTPC_HI_ULO
2747                           || r_type == R_M32R_GOTPC_HI_SLO
2748                           || r_type == R_M32R_GOTPC_LO)
2749                       || (r_type == R_M32R_26_PLTREL
2750                           && h->plt.offset != (bfd_vma) -1)
2751                       || ((r_type == R_M32R_GOT24
2752                            || r_type == R_M32R_GOT16_HI_ULO
2753                            || r_type == R_M32R_GOT16_HI_SLO
2754                            || r_type == R_M32R_GOT16_LO)
2755                           && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2756                                                               info->shared, h)
2757                           && (! info->shared
2758                               || (! info->symbolic && h->dynindx != -1)
2759                               || !h->def_regular))
2760                       || (info->shared
2761                           && ((! info->symbolic && h->dynindx != -1)
2762                               || !h->def_regular)
2763                           && (((r_type == R_M32R_16_RELA
2764                               || r_type == R_M32R_32_RELA
2765                               || r_type == R_M32R_24_RELA
2766                               || r_type == R_M32R_HI16_ULO_RELA
2767                               || r_type == R_M32R_HI16_SLO_RELA
2768                               || r_type == R_M32R_LO16_RELA)
2769                                   && !h->forced_local)
2770                               || r_type == R_M32R_10_PCREL_RELA
2771                               || r_type == R_M32R_18_PCREL_RELA
2772                               || r_type == R_M32R_26_PCREL_RELA)
2773                           && ((input_section->flags & SEC_ALLOC) != 0
2774                               /* DWARF will emit R_M32R_16(24,32) relocations
2775                                  in its sections against symbols defined
2776                                  externally in shared libraries.  We can't do
2777                                  anything with them here.  */
2778                               || ((input_section->flags & SEC_DEBUGGING) != 0
2779                                   && h->def_dynamic))))
2780                     {
2781                       /* In these cases, we don't need the relocation
2782                          value.  We check specially because in some
2783                          obscure cases sec->output_section will be NULL.  */
2784                       relocation = 0;
2785                     }
2786                   else if (sec->output_section == NULL)
2787                     {
2788                       (*_bfd_error_handler)
2789                         (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
2790                          bfd_get_filename (input_bfd), h->root.root.string,
2791                          bfd_get_section_name (input_bfd, input_section));
2792
2793                        relocation = 0;
2794                     }
2795                   else
2796                     relocation = (h->root.u.def.value
2797                                   + sec->output_section->vma
2798                                   + sec->output_offset);
2799                 }
2800               else if (h->root.type == bfd_link_hash_undefweak)
2801                 relocation = 0;
2802               else if (info->unresolved_syms_in_objects == RM_IGNORE
2803                        && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2804                 relocation = 0;
2805               else
2806                 {
2807                   if (! ((*info->callbacks->undefined_symbol)
2808                          (info, h->root.root.string, input_bfd,
2809                           input_section, offset,
2810                           (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
2811                            || ELF_ST_VISIBILITY (h->other)))))
2812                     return FALSE;
2813                   relocation = 0;
2814                 }
2815             }
2816
2817           /* Sanity check the address.  */
2818           if (offset > high_address)
2819             {
2820               r = bfd_reloc_outofrange;
2821               goto check_reloc;
2822             }
2823
2824           switch ((int) r_type)
2825             {
2826             case R_M32R_GOTOFF:
2827               /* Relocation is relative to the start of the global offset
2828                  table (for ld24 rx, #uimm24). eg access at label+addend
2829
2830                  ld24 rx. #label@GOTOFF + addend
2831                  sub  rx, r12.  */
2832
2833               BFD_ASSERT (sgot != NULL);
2834
2835               relocation = -(relocation - sgot->output_section->vma);
2836               rel->r_addend = -rel->r_addend;
2837               break;
2838
2839             case R_M32R_GOTOFF_HI_ULO:
2840             case R_M32R_GOTOFF_HI_SLO:
2841             case R_M32R_GOTOFF_LO:
2842               BFD_ASSERT (sgot != NULL);
2843
2844               relocation -= sgot->output_section->vma;
2845
2846               if ((r_type == R_M32R_GOTOFF_HI_SLO)
2847                   && ((relocation + rel->r_addend) & 0x8000))
2848                 rel->r_addend += 0x10000;
2849               break;
2850
2851             case R_M32R_GOTPC24:
2852               /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2853                  ld24 rx,#_GLOBAL_OFFSET_TABLE_
2854                */
2855              relocation = sgot->output_section->vma;
2856              break;
2857
2858             case R_M32R_GOTPC_HI_ULO:
2859             case R_M32R_GOTPC_HI_SLO:
2860             case R_M32R_GOTPC_LO:
2861               {
2862                 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
2863                    bl .+4
2864                    seth rx,#high(_GLOBAL_OFFSET_TABLE_)
2865                    or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2866                    or
2867                    bl .+4
2868                    seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
2869                    add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
2870                  */
2871                 relocation = sgot->output_section->vma;
2872                 relocation -= (input_section->output_section->vma
2873                                + input_section->output_offset
2874                                + rel->r_offset);
2875                 if ((r_type == R_M32R_GOTPC_HI_SLO)
2876                      && ((relocation + rel->r_addend) & 0x8000))
2877                   rel->r_addend += 0x10000;
2878
2879                 break;
2880               }
2881             case R_M32R_GOT16_HI_ULO:
2882             case R_M32R_GOT16_HI_SLO:
2883             case R_M32R_GOT16_LO:
2884               /* Fall through.  */
2885             case R_M32R_GOT24:
2886               /* Relocation is to the entry for this symbol in the global
2887                  offset table.  */
2888               BFD_ASSERT (sgot != NULL);
2889
2890               if (h != NULL)
2891                 {
2892                   bfd_boolean dyn;
2893                   bfd_vma off;
2894
2895                   off = h->got.offset;
2896                   BFD_ASSERT (off != (bfd_vma) -1);
2897
2898                   dyn = htab->root.dynamic_sections_created;
2899                   if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2900                       || (info->shared
2901                           && (info->symbolic
2902                               || h->dynindx == -1
2903                               || h->forced_local)
2904                           && h->def_regular))
2905                     {
2906                       /* This is actually a static link, or it is a
2907                          -Bsymbolic link and the symbol is defined
2908                          locally, or the symbol was forced to be local
2909                          because of a version file.  We must initialize
2910                          this entry in the global offset table.  Since the
2911                          offset must always be a multiple of 4, we use the
2912                          least significant bit to record whether we have
2913                          initialized it already.
2914
2915                          When doing a dynamic link, we create a .rela.got
2916                          relocation entry to initialize the value.  This
2917                          is done in the finish_dynamic_symbol routine.  */
2918                       if ((off & 1) != 0)
2919                         off &= ~1;
2920                       else
2921                         {
2922                           bfd_put_32 (output_bfd, relocation,
2923                                       sgot->contents + off);
2924                           h->got.offset |= 1;
2925                         }
2926                     }
2927
2928                   relocation = sgot->output_offset + off;
2929                 }
2930               else
2931                 {
2932                   bfd_vma off;
2933                   bfd_byte *loc;
2934
2935                   BFD_ASSERT (local_got_offsets != NULL
2936                               && local_got_offsets[r_symndx] != (bfd_vma) -1);
2937
2938                   off = local_got_offsets[r_symndx];
2939
2940                   /* The offset must always be a multiple of 4.  We use
2941                      the least significant bit to record whether we have
2942                      already processed this entry.  */
2943                   if ((off & 1) != 0)
2944                     off &= ~1;
2945                   else
2946                     {
2947                       bfd_put_32 (output_bfd, relocation, sgot->contents + off);
2948
2949                       if (info->shared)
2950                         {
2951                           asection *srelgot;
2952                           Elf_Internal_Rela outrel;
2953
2954                           /* We need to generate a R_M32R_RELATIVE reloc
2955                              for the dynamic linker.  */
2956                           srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2957                           BFD_ASSERT (srelgot != NULL);
2958
2959                           outrel.r_offset = (sgot->output_section->vma
2960                                              + sgot->output_offset
2961                                              + off);
2962                           outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
2963                           outrel.r_addend = relocation;
2964                           loc = srelgot->contents;
2965                           loc += srelgot->reloc_count * sizeof(Elf32_External_Rela);
2966                           bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
2967                           ++srelgot->reloc_count;
2968                         }
2969
2970                       local_got_offsets[r_symndx] |= 1;
2971                     }
2972
2973                   relocation = sgot->output_offset + off;
2974                 }
2975               if ((r_type == R_M32R_GOT16_HI_SLO)
2976                   && ((relocation + rel->r_addend) & 0x8000))
2977                 rel->r_addend += 0x10000;
2978
2979               break;
2980
2981             case R_M32R_26_PLTREL:
2982               /* Relocation is to the entry for this symbol in the
2983                  procedure linkage table.  */
2984
2985               /* The native assembler will generate a 26_PLTREL reloc
2986                  for a local symbol if you assemble a call from one
2987                  section to another when using -K pic. */
2988               if (h == NULL)
2989                 break;
2990
2991               //if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2992               //    || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
2993               //  break;
2994               if (h->forced_local)
2995                 break;
2996
2997               if (h->plt.offset == (bfd_vma) -1)
2998                 {
2999                   /* We didn't make a PLT entry for this symbol.  This
3000                      happens when statically linking PIC code, or when
3001                      using -Bsymbolic.  */
3002                   break;
3003                 }
3004
3005               relocation = (splt->output_section->vma
3006                             + splt->output_offset
3007                             + h->plt.offset);
3008               break;
3009
3010             case R_M32R_HI16_SLO_RELA:
3011               {
3012                  if ((relocation + rel->r_addend) & 0x8000)
3013                    {
3014                      rel->r_addend += 0x10000;
3015                    }
3016               }
3017               /* Fall through.  */
3018             case R_M32R_16_RELA:
3019             case R_M32R_24_RELA:
3020             case R_M32R_32_RELA:
3021             case R_M32R_18_PCREL_RELA:
3022             case R_M32R_26_PCREL_RELA:
3023             case R_M32R_HI16_ULO_RELA:
3024             case R_M32R_LO16_RELA:
3025               if (info->shared
3026                   && r_symndx != 0
3027                   && (input_section->flags & SEC_ALLOC) != 0
3028                   && ((r_type != R_M32R_18_PCREL_RELA
3029                        && r_type != R_M32R_26_PCREL_RELA)
3030                       || (h != NULL
3031                           && h->dynindx != -1
3032                           && (! info->symbolic
3033                               || !h->def_regular))))
3034                 {
3035                   Elf_Internal_Rela outrel;
3036                   bfd_boolean skip, relocate;
3037                   bfd_byte *loc;
3038
3039                   /* When generating a shared object, these relocations
3040                      are copied into the output file to be resolved at run
3041                      time.  */
3042
3043                   if (sreloc == NULL)
3044                     {
3045                       const char *name;
3046
3047                       name = (bfd_elf_string_from_elf_section
3048                               (input_bfd,
3049                                elf_elfheader (input_bfd)->e_shstrndx,
3050                                elf_section_data (input_section)->rel_hdr.sh_name));
3051                       if (name == NULL)
3052                         return FALSE;
3053
3054                       BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3055                                   && strcmp (bfd_get_section_name (input_bfd,
3056                                                                    input_section),
3057                                              name + 5) == 0);
3058
3059                       sreloc = bfd_get_section_by_name (dynobj, name);
3060                       BFD_ASSERT (sreloc != NULL);
3061                     }
3062
3063                   skip = FALSE;
3064                   relocate = FALSE;
3065
3066                   outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3067                                                              info,
3068                                                              input_section,
3069                                                              rel->r_offset);
3070                   if (outrel.r_offset == (bfd_vma) -1)
3071                     skip = TRUE;
3072                   else if (outrel.r_offset == (bfd_vma) -2)
3073                     skip = TRUE, relocate = TRUE;
3074                   outrel.r_offset += (input_section->output_section->vma
3075                                       + input_section->output_offset);
3076
3077                   if (skip)
3078                     memset (&outrel, 0, sizeof outrel);
3079                   else if (r_type == R_M32R_18_PCREL_RELA
3080                            || r_type == R_M32R_26_PCREL_RELA)
3081                     {
3082                       BFD_ASSERT (h != NULL && h->dynindx != -1);
3083                       outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3084                       outrel.r_addend = rel->r_addend;
3085                     }
3086                   else
3087                     {
3088                     /* h->dynindx may be -1 if this symbol was marked to
3089                        become local.  */
3090                     if (h == NULL
3091                         || ((info->symbolic || h->dynindx == -1)
3092                              && h->def_regular))
3093                       {
3094                         relocate = TRUE;
3095                         outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3096                         outrel.r_addend = relocation + rel->r_addend;
3097                       }
3098                     else
3099                       {
3100                         BFD_ASSERT (h->dynindx != -1);
3101                         outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3102                         outrel.r_addend = relocation + rel->r_addend;
3103                       }
3104                     }
3105
3106                   loc = sreloc->contents;
3107                   loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
3108                   bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
3109                   ++sreloc->reloc_count;
3110
3111                   /* If this reloc is against an external symbol, we do
3112                      not want to fiddle with the addend.  Otherwise, we
3113                      need to include the symbol value so that it becomes
3114                      an addend for the dynamic reloc.  */
3115                   if (! relocate)
3116                     continue;
3117                 }
3118               break;
3119
3120             case (int) R_M32R_10_PCREL :
3121               r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
3122                                               contents, offset,
3123                                               sec, relocation, addend);
3124               goto check_reloc;
3125
3126             case (int) R_M32R_HI16_SLO :
3127             case (int) R_M32R_HI16_ULO :
3128               {
3129                 Elf_Internal_Rela *lorel;
3130
3131                 /* We allow an arbitrary number of HI16 relocs before the
3132                    LO16 reloc.  This permits gcc to emit the HI and LO relocs
3133                    itself.  */
3134                 for (lorel = rel + 1;
3135                      (lorel < relend
3136                       && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
3137                           || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
3138                      lorel++)
3139                   continue;
3140                 if (lorel < relend
3141                     && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
3142                   {
3143                     m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
3144                                             contents, relocation + addend);
3145                     r = bfd_reloc_ok;
3146                   }
3147                 else
3148                   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3149                                                 contents, offset,
3150                                                 relocation, addend);
3151               }
3152
3153               goto check_reloc;
3154
3155             case (int) R_M32R_SDA16_RELA:
3156             case (int) R_M32R_SDA16 :
3157               {
3158                 const char *name;
3159
3160                 BFD_ASSERT (sec != NULL);
3161                 name = bfd_get_section_name (abfd, sec);
3162
3163                 if (strcmp (name, ".sdata") == 0
3164                     || strcmp (name, ".sbss") == 0
3165                     || strcmp (name, ".scommon") == 0)
3166                   {
3167                     bfd_vma sda_base;
3168                     bfd *out_bfd = sec->output_section->owner;
3169
3170                     r = m32r_elf_final_sda_base (out_bfd, info,
3171                                                  &errmsg,
3172                                                  &sda_base);
3173                     if (r != bfd_reloc_ok)
3174                       {
3175                         ret = FALSE;
3176                         goto check_reloc;
3177                       }
3178
3179                     /* At this point `relocation' contains the object's
3180                        address.  */
3181                     relocation -= sda_base;
3182                     /* Now it contains the offset from _SDA_BASE_.  */
3183                   }
3184                 else
3185                   {
3186                     (*_bfd_error_handler)
3187                       (_("%B: The target (%s) of an %s relocation is in the wrong section (%A)"),
3188                        input_bfd,
3189                        sec,
3190                        sym_name,
3191                        m32r_elf_howto_table[(int) r_type].name);
3192                     /*bfd_set_error (bfd_error_bad_value); ??? why? */
3193                     ret = FALSE;
3194                     continue;
3195                   }
3196               }
3197               /* fall through */
3198
3199             default : /* OLD_M32R_RELOC */
3200
3201               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3202                                             contents, offset,
3203                                             relocation, addend);
3204               goto check_reloc;
3205             }
3206
3207           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3208                                         contents, rel->r_offset,
3209                                         relocation, rel->r_addend);
3210
3211         }
3212
3213     check_reloc:
3214
3215       if (r != bfd_reloc_ok)
3216         {
3217           /* FIXME: This should be generic enough to go in a utility.  */
3218           const char *name;
3219
3220           if (h != NULL)
3221             name = h->root.root.string;
3222           else
3223             {
3224               name = (bfd_elf_string_from_elf_section
3225                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
3226               if (name == NULL || *name == '\0')
3227                 name = bfd_section_name (input_bfd, sec);
3228             }
3229
3230           if (errmsg != NULL)
3231             goto common_error;
3232
3233           switch (r)
3234             {
3235             case bfd_reloc_overflow:
3236               if (! ((*info->callbacks->reloc_overflow)
3237                      (info, (h ? &h->root : NULL), name, howto->name,
3238                       (bfd_vma) 0, input_bfd, input_section, offset)))
3239                 return FALSE;
3240               break;
3241
3242             case bfd_reloc_undefined:
3243               if (! ((*info->callbacks->undefined_symbol)
3244                      (info, name, input_bfd, input_section,
3245                       offset, TRUE)))
3246                 return FALSE;
3247               break;
3248
3249             case bfd_reloc_outofrange:
3250               errmsg = _("internal error: out of range error");
3251               goto common_error;
3252
3253             case bfd_reloc_notsupported:
3254               errmsg = _("internal error: unsupported relocation error");
3255               goto common_error;
3256
3257             case bfd_reloc_dangerous:
3258               errmsg = _("internal error: dangerous error");
3259               goto common_error;
3260
3261             default:
3262               errmsg = _("internal error: unknown error");
3263               /* fall through */
3264
3265             common_error:
3266               if (!((*info->callbacks->warning)
3267                     (info, errmsg, name, input_bfd, input_section,
3268                      offset)))
3269                 return FALSE;
3270               break;
3271             }
3272         }
3273     }
3274
3275   return ret;
3276 }
3277
3278 /* Finish up dynamic symbol handling.  We set the contents of various
3279    dynamic sections here.  */
3280 static bfd_boolean
3281 m32r_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
3282      bfd *output_bfd;
3283      struct bfd_link_info *info;
3284      struct elf_link_hash_entry *h;
3285      Elf_Internal_Sym *sym;
3286 {
3287   struct elf_m32r_link_hash_table *htab;
3288   bfd *dynobj;
3289   bfd_byte *loc;
3290
3291 #ifdef DEBUG_PIC
3292 printf("m32r_elf_finish_dynamic_symbol()\n");
3293 #endif
3294
3295   htab = m32r_elf_hash_table (info);
3296   dynobj = htab->root.dynobj;
3297
3298   if (h->plt.offset != (bfd_vma) -1)
3299     {
3300       asection *splt;
3301       asection *sgot;
3302       asection *srela;
3303
3304       bfd_vma plt_index;
3305       bfd_vma got_offset;
3306       Elf_Internal_Rela rela;
3307
3308       /* This symbol has an entry in the procedure linkage table.  Set
3309          it up.  */
3310
3311       BFD_ASSERT (h->dynindx != -1);
3312
3313       splt = htab->splt;
3314       sgot = htab->sgotplt;
3315       srela = htab->srelplt;
3316       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3317
3318       /* Get the index in the procedure linkage table which
3319          corresponds to this symbol.  This is the index of this symbol
3320          in all the symbols for which we are making plt entries.  The
3321          first entry in the procedure linkage table is reserved.  */
3322       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3323
3324       /* Get the offset into the .got table of the entry that
3325         corresponds to this function.  Each .got entry is 4 bytes.
3326         The first three are reserved.  */
3327       got_offset = (plt_index + 3) * 4;
3328
3329       /* Fill in the entry in the procedure linkage table.  */
3330       if (! info->shared)
3331         {
3332           bfd_put_32 (output_bfd,
3333               (PLT_ENTRY_WORD0b
3334                + (((sgot->output_section->vma
3335                     + sgot->output_offset
3336                     + got_offset) >> 16) & 0xffff)),
3337               splt->contents + h->plt.offset);
3338           bfd_put_32 (output_bfd,
3339               (PLT_ENTRY_WORD1b
3340                + ((sgot->output_section->vma
3341                    + sgot->output_offset
3342                    + got_offset) & 0xffff)),
3343               splt->contents + h->plt.offset + 4);
3344           bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3345               splt->contents + h->plt.offset + 8);
3346           bfd_put_32 (output_bfd,
3347               (PLT_ENTRY_WORD3
3348                + plt_index * sizeof (Elf32_External_Rela)),
3349               splt->contents + h->plt.offset + 12);
3350           bfd_put_32 (output_bfd,
3351               (PLT_ENTRY_WORD4
3352                + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3353               splt->contents + h->plt.offset + 16);
3354         }
3355       else
3356         {
3357           bfd_put_32 (output_bfd,
3358               PLT_ENTRY_WORD0 + got_offset,
3359               splt->contents + h->plt.offset);
3360           bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
3361               splt->contents + h->plt.offset + 4);
3362           bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
3363               splt->contents + h->plt.offset + 8);
3364           bfd_put_32 (output_bfd,
3365               (PLT_ENTRY_WORD3
3366                + plt_index * sizeof (Elf32_External_Rela)),
3367               splt->contents + h->plt.offset + 12);
3368           bfd_put_32 (output_bfd,
3369               (PLT_ENTRY_WORD4
3370                + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
3371               splt->contents + h->plt.offset + 16);
3372         }
3373
3374       /* Fill in the entry in the global offset table.  */
3375       bfd_put_32 (output_bfd,
3376                   (splt->output_section->vma
3377                    + splt->output_offset
3378                    + h->plt.offset
3379                    + 12), /* same offset */
3380                   sgot->contents + got_offset);
3381
3382       /* Fill in the entry in the .rela.plt section.  */
3383       rela.r_offset = (sgot->output_section->vma
3384                        + sgot->output_offset
3385                        + got_offset);
3386       rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
3387       rela.r_addend = 0;
3388       loc = srela->contents;
3389       loc += plt_index * sizeof(Elf32_External_Rela);
3390       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3391
3392       if (!h->def_regular)
3393         {
3394           /* Mark the symbol as undefined, rather than as defined in
3395              the .plt section.  Leave the value alone.  */
3396           sym->st_shndx = SHN_UNDEF;
3397         }
3398     }
3399
3400   if (h->got.offset != (bfd_vma) -1)
3401     {
3402       asection *sgot;
3403       asection *srela;
3404       Elf_Internal_Rela rela;
3405
3406       /* This symbol has an entry in the global offset table.  Set it
3407          up.  */
3408
3409       sgot = htab->sgot;
3410       srela = htab->srelgot;
3411       BFD_ASSERT (sgot != NULL && srela != NULL);
3412
3413       rela.r_offset = (sgot->output_section->vma
3414                        + sgot->output_offset
3415                        + (h->got.offset &~ 1));
3416
3417       /* If this is a -Bsymbolic link, and the symbol is defined
3418          locally, we just want to emit a RELATIVE reloc.  Likewise if
3419          the symbol was forced to be local because of a version file.
3420          The entry in the global offset table will already have been
3421          initialized in the relocate_section function.  */
3422       if (info->shared
3423           && (info->symbolic
3424               || h->dynindx == -1
3425               || h->forced_local)
3426           && h->def_regular)
3427         {
3428           rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
3429           rela.r_addend = (h->root.u.def.value
3430                            + h->root.u.def.section->output_section->vma
3431                            + h->root.u.def.section->output_offset);
3432         }
3433       else
3434         {
3435           BFD_ASSERT((h->got.offset & 1) == 0);
3436           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3437           rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
3438           rela.r_addend = 0;
3439         }
3440
3441       loc = srela->contents;
3442       loc += srela->reloc_count * sizeof(Elf32_External_Rela);
3443       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3444       ++srela->reloc_count;
3445     }
3446
3447   if (h->needs_copy)
3448     {
3449       asection *s;
3450       Elf_Internal_Rela rela;
3451
3452       /* This symbols needs a copy reloc.  Set it up.  */
3453
3454       BFD_ASSERT (h->dynindx != -1
3455                   && (h->root.type == bfd_link_hash_defined
3456                       || h->root.type == bfd_link_hash_defweak));
3457
3458       s = bfd_get_section_by_name (h->root.u.def.section->owner,
3459                                    ".rela.bss");
3460       BFD_ASSERT (s != NULL);
3461
3462       rela.r_offset = (h->root.u.def.value
3463                        + h->root.u.def.section->output_section->vma
3464                        + h->root.u.def.section->output_offset);
3465       rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
3466       rela.r_addend = 0;
3467       loc = s->contents;
3468       loc += s->reloc_count * sizeof(Elf32_External_Rela);
3469       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3470       ++s->reloc_count;
3471     }
3472
3473   /* Mark some specially defined symbols as absolute.  */
3474   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3475       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3476     sym->st_shndx = SHN_ABS;
3477
3478   return TRUE;
3479 }
3480
3481
3482 /* Finish up the dynamic sections.  */
3483
3484 static bfd_boolean
3485 m32r_elf_finish_dynamic_sections (output_bfd, info)
3486      bfd *output_bfd;
3487      struct bfd_link_info *info;
3488 {
3489   struct elf_m32r_link_hash_table *htab;
3490   bfd *dynobj;
3491   asection *sdyn;
3492   asection *sgot;
3493
3494 #ifdef DEBUG_PIC
3495 printf("m32r_elf_finish_dynamic_sections()\n");
3496 #endif
3497
3498   htab = m32r_elf_hash_table (info);
3499   dynobj = htab->root.dynobj;
3500
3501   sgot = htab->sgotplt;
3502   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3503
3504   if (htab->root.dynamic_sections_created)
3505     {
3506       asection *splt;
3507       Elf32_External_Dyn *dyncon, *dynconend;
3508
3509       BFD_ASSERT (sgot != NULL && sdyn != NULL);
3510
3511       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3512       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3513
3514       for (; dyncon < dynconend; dyncon++)
3515         {
3516           Elf_Internal_Dyn dyn;
3517           const char *name;
3518           asection *s;
3519
3520           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3521
3522           switch (dyn.d_tag)
3523             {
3524             default:
3525               break;
3526
3527             case DT_PLTGOT:
3528               name = ".got";
3529               s = htab->sgot->output_section;
3530               goto get_vma;
3531             case DT_JMPREL:
3532               name = ".rela.plt";
3533               s = htab->srelplt->output_section;
3534             get_vma:
3535               BFD_ASSERT (s != NULL);
3536               dyn.d_un.d_ptr = s->vma;
3537               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3538               break;
3539
3540             case DT_PLTRELSZ:
3541               s = htab->srelplt->output_section;
3542               BFD_ASSERT (s != NULL);
3543               dyn.d_un.d_val = s->size;
3544               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3545               break;
3546
3547             case DT_RELASZ:
3548               /* My reading of the SVR4 ABI indicates that the
3549                  procedure linkage table relocs (DT_JMPREL) should be
3550                  included in the overall relocs (DT_RELA).  This is
3551                  what Solaris does.  However, UnixWare can not handle
3552                  that case.  Therefore, we override the DT_RELASZ entry
3553                  here to make it not include the JMPREL relocs.  Since
3554                  the linker script arranges for .rela.plt to follow all
3555                  other relocation sections, we don't have to worry
3556                  about changing the DT_RELA entry.  */
3557               if (htab->srelplt != NULL)
3558                 {
3559                   s = htab->srelplt->output_section;
3560                   dyn.d_un.d_val -= s->size;
3561                 }
3562               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3563               break;
3564             }
3565         }
3566
3567       /* Fill in the first entry in the procedure linkage table.  */
3568       splt = htab->splt;
3569       if (splt && splt->size > 0)
3570         {
3571           if (info->shared)
3572             {
3573               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
3574               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
3575               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
3576               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
3577               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
3578             }
3579           else
3580             {
3581               unsigned long addr;
3582               /* addr = .got + 4 */
3583               addr = sgot->output_section->vma + sgot->output_offset + 4;
3584               bfd_put_32 (output_bfd,
3585                           PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
3586                           splt->contents);
3587               bfd_put_32 (output_bfd,
3588                           PLT0_ENTRY_WORD1 | (addr & 0xffff),
3589                           splt->contents + 4);
3590               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
3591               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
3592               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
3593             }
3594
3595           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3596             PLT_ENTRY_SIZE;
3597         }
3598     }
3599
3600   /* Fill in the first three entries in the global offset table.  */
3601   if (sgot && sgot->size > 0)
3602     {
3603       if (sdyn == NULL)
3604         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3605       else
3606         bfd_put_32 (output_bfd,
3607                     sdyn->output_section->vma + sdyn->output_offset,
3608                     sgot->contents);
3609       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
3610       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
3611
3612       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3613     }
3614
3615   return TRUE;
3616 }
3617
3618 \f
3619 /* Set the right machine number.  */
3620 static bfd_boolean
3621 m32r_elf_object_p (abfd)
3622      bfd *abfd;
3623 {
3624   switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
3625     {
3626     default:
3627     case E_M32R_ARCH:   (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r);  break;
3628     case E_M32RX_ARCH:  (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
3629     case E_M32R2_ARCH:  (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
3630     }
3631   return TRUE;
3632 }
3633
3634 /* Store the machine number in the flags field.  */
3635 static void
3636 m32r_elf_final_write_processing (abfd, linker)
3637      bfd *abfd;
3638      bfd_boolean linker ATTRIBUTE_UNUSED;
3639 {
3640   unsigned long val;
3641
3642   switch (bfd_get_mach (abfd))
3643     {
3644     default:
3645     case bfd_mach_m32r:  val = E_M32R_ARCH; break;
3646     case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
3647     case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
3648     }
3649
3650   elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
3651   elf_elfheader (abfd)->e_flags |= val;
3652 }
3653
3654 /* Function to keep M32R specific file flags.  */
3655 static bfd_boolean
3656 m32r_elf_set_private_flags (abfd, flags)
3657      bfd *abfd;
3658      flagword flags;
3659 {
3660   BFD_ASSERT (!elf_flags_init (abfd)
3661               || elf_elfheader (abfd)->e_flags == flags);
3662
3663   elf_elfheader (abfd)->e_flags = flags;
3664   elf_flags_init (abfd) = TRUE;
3665   return TRUE;
3666 }
3667
3668 /* Merge backend specific data from an object file to the output
3669    object file when linking.  */
3670 static bfd_boolean
3671 m32r_elf_merge_private_bfd_data (ibfd, obfd)
3672      bfd *ibfd;
3673      bfd *obfd;
3674 {
3675   flagword out_flags;
3676   flagword in_flags;
3677
3678   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3679       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3680     return TRUE;
3681
3682   in_flags  = elf_elfheader (ibfd)->e_flags;
3683   out_flags = elf_elfheader (obfd)->e_flags;
3684
3685   if (! elf_flags_init (obfd))
3686     {
3687       /* If the input is the default architecture then do not
3688          bother setting the flags for the output architecture,
3689          instead allow future merges to do this.  If no future
3690          merges ever set these flags then they will retain their
3691          unitialised values, which surprise surprise, correspond
3692          to the default values.  */
3693       if (bfd_get_arch_info (ibfd)->the_default)
3694         return TRUE;
3695
3696       elf_flags_init (obfd) = TRUE;
3697       elf_elfheader (obfd)->e_flags = in_flags;
3698
3699       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3700           && bfd_get_arch_info (obfd)->the_default)
3701         {
3702           return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
3703         }
3704
3705       return TRUE;
3706     }
3707
3708   /* Check flag compatibility.  */
3709   if (in_flags == out_flags)
3710     return TRUE;
3711
3712   if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
3713     {
3714       if (   ((in_flags  & EF_M32R_ARCH) != E_M32R_ARCH)
3715           || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3716           || ((in_flags  & EF_M32R_ARCH) == E_M32R2_ARCH))
3717         {
3718           (*_bfd_error_handler)
3719             (_("%B: Instruction set mismatch with previous modules"), ibfd);
3720
3721           bfd_set_error (bfd_error_bad_value);
3722           return FALSE;
3723         }
3724     }
3725
3726   return TRUE;
3727 }
3728
3729 /* Display the flags field */
3730 static bfd_boolean
3731 m32r_elf_print_private_bfd_data (abfd, ptr)
3732      bfd *abfd;
3733      PTR ptr;
3734 {
3735   FILE * file = (FILE *) ptr;
3736
3737   BFD_ASSERT (abfd != NULL && ptr != NULL);
3738
3739   _bfd_elf_print_private_bfd_data (abfd, ptr);
3740
3741   fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
3742
3743   switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
3744     {
3745     default:
3746     case E_M32R_ARCH:  fprintf (file, _(": m32r instructions"));  break;
3747     case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
3748     case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
3749     }
3750
3751   fputc ('\n', file);
3752
3753   return TRUE;
3754 }
3755
3756 asection *
3757 m32r_elf_gc_mark_hook (sec, info, rel, h, sym)
3758      asection *sec;
3759      struct bfd_link_info *info ATTRIBUTE_UNUSED;
3760      Elf_Internal_Rela *rel;
3761      struct elf_link_hash_entry *h;
3762      Elf_Internal_Sym *sym;
3763 {
3764   if (h != NULL)
3765     {
3766       switch (ELF32_R_TYPE (rel->r_info))
3767       {
3768       case R_M32R_GNU_VTINHERIT:
3769       case R_M32R_GNU_VTENTRY:
3770       case R_M32R_RELA_GNU_VTINHERIT:
3771       case R_M32R_RELA_GNU_VTENTRY:
3772         break;
3773
3774       default:
3775         switch (h->root.type)
3776           {
3777           case bfd_link_hash_defined:
3778           case bfd_link_hash_defweak:
3779             return h->root.u.def.section;
3780
3781           case bfd_link_hash_common:
3782             return h->root.u.c.p->section;
3783
3784           default:
3785             break;
3786           }
3787        }
3788      }
3789    else
3790      return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3791
3792   return NULL;
3793 }
3794
3795 static bfd_boolean
3796 m32r_elf_gc_sweep_hook (abfd, info, sec, relocs)
3797      bfd *abfd ATTRIBUTE_UNUSED;
3798      struct bfd_link_info *info ATTRIBUTE_UNUSED;
3799      asection *sec ATTRIBUTE_UNUSED;
3800      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
3801 {
3802   /* Update the got entry reference counts for the section being removed.  */
3803   Elf_Internal_Shdr *symtab_hdr;
3804   struct elf_link_hash_entry **sym_hashes;
3805   bfd_signed_vma *local_got_refcounts;
3806   const Elf_Internal_Rela *rel, *relend;
3807
3808   elf_section_data (sec)->local_dynrel = NULL;
3809
3810   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3811   sym_hashes = elf_sym_hashes (abfd);
3812   local_got_refcounts = elf_local_got_refcounts (abfd);
3813
3814   relend = relocs + sec->reloc_count;
3815   for (rel = relocs; rel < relend; rel++)
3816     {
3817       unsigned long r_symndx;
3818       struct elf_link_hash_entry *h = NULL;
3819
3820       r_symndx = ELF32_R_SYM (rel->r_info);
3821       if (r_symndx >= symtab_hdr->sh_info)
3822         {
3823           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3824           while (h->root.type == bfd_link_hash_indirect
3825                  || h->root.type == bfd_link_hash_warning)
3826             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3827         }
3828
3829       switch (ELF32_R_TYPE (rel->r_info))
3830         {
3831         case R_M32R_GOT16_HI_ULO:
3832         case R_M32R_GOT16_HI_SLO:
3833         case R_M32R_GOT16_LO:
3834         case R_M32R_GOTOFF:
3835         case R_M32R_GOTOFF_HI_ULO:
3836         case R_M32R_GOTOFF_HI_SLO:
3837         case R_M32R_GOTOFF_LO:
3838         case R_M32R_GOT24:
3839         case R_M32R_GOTPC_HI_ULO:
3840         case R_M32R_GOTPC_HI_SLO:
3841         case R_M32R_GOTPC_LO:
3842         case R_M32R_GOTPC24:
3843           if (h != NULL)
3844             {
3845               if (h->got.refcount > 0)
3846                 h->got.refcount--;
3847             }
3848           else
3849             {
3850               if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
3851                 local_got_refcounts[r_symndx]--;
3852             }
3853           break;
3854
3855         case R_M32R_16_RELA:
3856         case R_M32R_24_RELA:
3857         case R_M32R_32_RELA:
3858         case R_M32R_HI16_ULO_RELA:
3859         case R_M32R_HI16_SLO_RELA:
3860         case R_M32R_LO16_RELA:
3861         case R_M32R_SDA16_RELA:
3862         case R_M32R_18_PCREL_RELA:
3863         case R_M32R_26_PCREL_RELA:
3864           if (h != NULL)
3865             {
3866               struct elf_m32r_link_hash_entry *eh;
3867               struct elf_m32r_dyn_relocs **pp;
3868               struct elf_m32r_dyn_relocs *p;
3869
3870               if (!info->shared && h->plt.refcount > 0)
3871                 h->plt.refcount -= 1;
3872
3873               eh = (struct elf_m32r_link_hash_entry *) h;
3874
3875               for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3876                 if (p->sec == sec)
3877                   {
3878                     if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
3879                         || ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA)
3880                       p->pc_count -= 1;
3881                     p->count -= 1;
3882                     if (p->count == 0)
3883                       *pp = p->next;
3884                     break;
3885                   }
3886             }
3887           break;
3888
3889         case R_M32R_26_PLTREL:
3890           if (h != NULL)
3891             {
3892               if (h->plt.refcount > 0)
3893                 h->plt.refcount--;
3894             }
3895           break;
3896
3897         default:
3898           break;
3899         }
3900     }
3901
3902   return TRUE;
3903 }
3904
3905 /* Look through the relocs for a section during the first phase.
3906    Since we don't do .gots or .plts, we just need to consider the
3907    virtual table relocs for gc.  */
3908
3909 static bfd_boolean
3910 m32r_elf_check_relocs (abfd, info, sec, relocs)
3911      bfd *abfd;
3912      struct bfd_link_info *info;
3913      asection *sec;
3914      const Elf_Internal_Rela *relocs;
3915 {
3916   Elf_Internal_Shdr *symtab_hdr;
3917   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3918   const Elf_Internal_Rela *rel;
3919   const Elf_Internal_Rela *rel_end;
3920   struct elf_m32r_link_hash_table *htab;
3921   bfd *dynobj;
3922   bfd_vma *local_got_offsets;
3923   asection *sgot, *srelgot, *sreloc;
3924
3925   if (info->relocatable)
3926     return TRUE;
3927
3928   sgot = srelgot = sreloc = NULL;
3929
3930   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3931   sym_hashes = elf_sym_hashes (abfd);
3932   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
3933   if (!elf_bad_symtab (abfd))
3934     sym_hashes_end -= symtab_hdr->sh_info;
3935
3936   htab = m32r_elf_hash_table (info);
3937   dynobj = htab->root.dynobj;
3938   local_got_offsets = elf_local_got_offsets (abfd);
3939
3940   rel_end = relocs + sec->reloc_count;
3941   for (rel = relocs; rel < rel_end; rel++)
3942     {
3943       int r_type;
3944       struct elf_link_hash_entry *h;
3945       unsigned long r_symndx;
3946
3947       r_symndx = ELF32_R_SYM (rel->r_info);
3948       r_type = ELF32_R_TYPE (rel->r_info);
3949       if (r_symndx < symtab_hdr->sh_info)
3950         h = NULL;
3951       else
3952         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3953
3954       /* Some relocs require a global offset table.  */
3955       if (htab->sgot == NULL)
3956         {
3957           switch (r_type)
3958             {
3959             case R_M32R_GOT16_HI_ULO:
3960             case R_M32R_GOT16_HI_SLO:
3961             case R_M32R_GOTOFF:
3962             case R_M32R_GOTOFF_HI_ULO:
3963             case R_M32R_GOTOFF_HI_SLO:
3964             case R_M32R_GOTOFF_LO:
3965             case R_M32R_GOT16_LO:
3966             case R_M32R_GOTPC24:
3967             case R_M32R_GOTPC_HI_ULO:
3968             case R_M32R_GOTPC_HI_SLO:
3969             case R_M32R_GOTPC_LO:
3970             case R_M32R_GOT24:
3971               if (dynobj == NULL)
3972                 htab->root.dynobj = dynobj = abfd;
3973               if (! create_got_section (dynobj, info))
3974                 return FALSE;
3975               break;
3976
3977             default:
3978               break;
3979           }
3980         }
3981
3982       switch (r_type)
3983         {
3984         case R_M32R_GOT16_HI_ULO:
3985         case R_M32R_GOT16_HI_SLO:
3986         case R_M32R_GOT16_LO:
3987         case R_M32R_GOT24:
3988
3989           if (h != NULL)
3990             h->got.refcount += 1;
3991           else
3992             {
3993               bfd_signed_vma *local_got_refcounts;
3994
3995               /* This is a global offset table entry for a local
3996                  symbol.  */
3997               local_got_refcounts = elf_local_got_refcounts (abfd);
3998               if (local_got_refcounts == NULL)
3999                 {
4000                   bfd_size_type size;
4001
4002                   size = symtab_hdr->sh_info;
4003                   size *= sizeof (bfd_signed_vma);
4004                   local_got_refcounts = ((bfd_signed_vma *)
4005                                          bfd_zalloc (abfd, size));
4006                   if (local_got_refcounts == NULL)
4007                     return FALSE;
4008                   elf_local_got_refcounts (abfd) = local_got_refcounts;
4009                 }
4010               local_got_refcounts[r_symndx] += 1;
4011             }
4012           break;
4013
4014         case R_M32R_26_PLTREL:
4015           /* This symbol requires a procedure linkage table entry.  We
4016              actually build the entry in adjust_dynamic_symbol,
4017              because this might be a case of linking PIC code without
4018              linking in any dynamic objects, in which case we don't
4019              need to generate a procedure linkage table after all.  */
4020
4021           /* If this is a local symbol, we resolve it directly without
4022              creating a procedure linkage table entry.  */
4023           if (h == NULL)
4024             continue;
4025
4026           if (h->forced_local)
4027             break;
4028
4029           h->needs_plt = 1;
4030           h->plt.refcount += 1;
4031           break;
4032
4033         case R_M32R_16_RELA:
4034         case R_M32R_24_RELA:
4035         case R_M32R_32_RELA:
4036         case R_M32R_HI16_ULO_RELA:
4037         case R_M32R_HI16_SLO_RELA:
4038         case R_M32R_LO16_RELA:
4039         case R_M32R_SDA16_RELA:
4040         case R_M32R_18_PCREL_RELA:
4041         case R_M32R_26_PCREL_RELA:
4042
4043           if (h != NULL && !info->shared)
4044             {
4045               h->non_got_ref = 1;
4046               h->plt.refcount += 1;
4047             }
4048
4049           /* If we are creating a shared library, and this is a reloc
4050              against a global symbol, or a non PC relative reloc
4051              against a local symbol, then we need to copy the reloc
4052              into the shared library.  However, if we are linking with
4053              -Bsymbolic, we do not need to copy a reloc against a
4054              global symbol which is defined in an object we are
4055              including in the link (i.e., DEF_REGULAR is set).  At
4056              this point we have not seen all the input files, so it is
4057              possible that DEF_REGULAR is not set now but will be set
4058              later (it is never cleared).  We account for that
4059              possibility below by storing information in the
4060              dyn_relocs field of the hash table entry. A similar
4061              situation occurs when creating shared libraries and symbol
4062              visibility changes render the symbol local.
4063
4064              If on the other hand, we are creating an executable, we
4065              may need to keep relocations for symbols satisfied by a
4066              dynamic library if we manage to avoid copy relocs for the
4067              symbol.  */
4068           if ((info->shared
4069                && (sec->flags & SEC_ALLOC) != 0
4070                && ((r_type != R_M32R_26_PCREL_RELA
4071                     && r_type != R_M32R_18_PCREL_RELA)
4072                    || (h != NULL
4073                        && (! info->symbolic
4074                            || h->root.type == bfd_link_hash_defweak
4075                            || !h->def_regular))))
4076               || (!info->shared
4077                   && (sec->flags & SEC_ALLOC) != 0
4078                   && h != NULL
4079                   && (h->root.type == bfd_link_hash_defweak
4080                       || !h->def_regular)))
4081             {
4082               struct elf_m32r_dyn_relocs *p;
4083               struct elf_m32r_dyn_relocs **head;
4084
4085               if (dynobj == NULL)
4086                 htab->root.dynobj = dynobj = abfd;
4087
4088               /* When creating a shared object, we must copy these
4089                  relocs into the output file.  We create a reloc
4090                  section in dynobj and make room for the reloc.  */
4091               if (sreloc == NULL)
4092                 {
4093                   const char *name;
4094
4095                   name = (bfd_elf_string_from_elf_section
4096                           (abfd,
4097                            elf_elfheader (abfd)->e_shstrndx,
4098                            elf_section_data (sec)->rel_hdr.sh_name));
4099                   if (name == NULL)
4100                     return FALSE;
4101
4102                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4103                               && strcmp (bfd_get_section_name (abfd, sec),
4104                                          name + 5) == 0);
4105
4106                   sreloc = bfd_get_section_by_name (dynobj, name);
4107                   if (sreloc == NULL)
4108                     {
4109                       flagword flags;
4110
4111                       sreloc = bfd_make_section (dynobj, name);
4112                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
4113                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4114                       if ((sec->flags & SEC_ALLOC) != 0)
4115                         flags |= SEC_ALLOC | SEC_LOAD;
4116                       if (sreloc == NULL
4117                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
4118                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
4119                         return FALSE;
4120                     }
4121                   elf_section_data (sec)->sreloc = sreloc;
4122                 }
4123
4124               /* If this is a global symbol, we count the number of
4125                  relocations we need for this symbol.  */
4126               if (h != NULL)
4127                 head = &((struct elf_m32r_link_hash_entry *) h)->dyn_relocs;
4128               else
4129                 {
4130                   asection *s;
4131
4132                   /* Track dynamic relocs needed for local syms too.  */
4133                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4134                                                  sec, r_symndx);
4135                   if (s == NULL)
4136                     return FALSE;
4137
4138                   head = ((struct elf_m32r_dyn_relocs **)
4139                           &elf_section_data (s)->local_dynrel);
4140                 }
4141
4142               p = *head;
4143               if (p == NULL || p->sec != sec)
4144                 {
4145                   bfd_size_type amt = sizeof (*p);
4146                   p = ((struct elf_m32r_dyn_relocs *) bfd_alloc (dynobj, amt));
4147                   if (p == NULL)
4148                     return FALSE;
4149                   p->next = *head;
4150                   *head = p;
4151                   p->sec = sec;
4152                   p->count = 0;
4153                   p->pc_count = 0;
4154                 }
4155
4156               p->count += 1;
4157               if (ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
4158                   || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA)
4159                 p->pc_count += 1;
4160             }
4161           break;
4162
4163         /* This relocation describes the C++ object vtable hierarchy.
4164            Reconstruct it for later use during GC.  */
4165         case R_M32R_RELA_GNU_VTINHERIT:
4166         case R_M32R_GNU_VTINHERIT:
4167           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4168             return FALSE;
4169           break;
4170
4171         /* This relocation describes which C++ vtable entries are actually
4172            used.  Record for later use during GC.  */
4173         case R_M32R_GNU_VTENTRY:
4174           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4175             return FALSE;
4176           break;
4177         case R_M32R_RELA_GNU_VTENTRY:
4178           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4179             return FALSE;
4180           break;
4181         }
4182     }
4183
4184   return TRUE;
4185 }
4186
4187 static struct bfd_elf_special_section const m32r_elf_special_sections[]=
4188 {
4189   { ".sdata",   6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
4190   { ".sbss",    5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
4191   { NULL,       0,  0, 0,            0 }
4192 };
4193
4194 static bfd_boolean
4195 m32r_elf_fake_sections (abfd, hdr, sec)
4196      bfd *abfd;
4197      Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
4198      asection *sec;
4199 {
4200   register const char *name;
4201
4202   name = bfd_get_section_name (abfd, sec);
4203
4204   /* The generic elf_fake_sections will set up REL_HDR using the
4205      default kind of relocations.  But, we may actually need both
4206      kinds of relocations, so we set up the second header here.
4207
4208      This is not necessary for the O32 ABI since that only uses Elf32_Rel
4209      relocations (cf. System V ABI, MIPS RISC Processor Supplement,
4210      3rd Edition, p. 4-17).  It breaks the IRIX 5/6 32-bit ld, since one
4211      of the resulting empty .rela.<section> sections starts with
4212      sh_offset == object size, and ld doesn't allow that.  While the check
4213      is arguably bogus for empty or SHT_NOBITS sections, it can easily be
4214      avoided by not emitting those useless sections in the first place.  */
4215   if ((sec->flags & SEC_RELOC) != 0)
4216     {
4217       struct bfd_elf_section_data *esd;
4218       bfd_size_type amt = sizeof (Elf_Internal_Shdr);
4219
4220       esd = elf_section_data (sec);
4221       BFD_ASSERT (esd->rel_hdr2 == NULL);
4222       esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
4223       if (!esd->rel_hdr2)
4224         return FALSE;
4225       _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
4226                                 !sec->use_rela_p);
4227     }
4228
4229   return TRUE;
4230 }
4231
4232 static enum elf_reloc_type_class
4233 m32r_elf_reloc_type_class (rela)
4234      const Elf_Internal_Rela *rela;
4235 {
4236   switch ((int) ELF32_R_TYPE (rela->r_info))
4237     {
4238     case R_M32R_RELATIVE:
4239       return reloc_class_relative;
4240     case R_M32R_JMP_SLOT:
4241       return reloc_class_plt;
4242     case R_M32R_COPY:
4243       return reloc_class_copy;
4244     default:
4245       return reloc_class_normal;
4246     }
4247 }
4248 \f
4249 #define ELF_ARCH                bfd_arch_m32r
4250 #define ELF_MACHINE_CODE        EM_M32R
4251 #define ELF_MACHINE_ALT1        EM_CYGNUS_M32R
4252 #define ELF_MAXPAGESIZE         0x1 /* Explicitly requested by Mitsubishi.  */
4253
4254 #define TARGET_BIG_SYM          bfd_elf32_m32r_vec
4255 #define TARGET_BIG_NAME         "elf32-m32r"
4256 #define TARGET_LITTLE_SYM       bfd_elf32_m32rle_vec
4257 #define TARGET_LITTLE_NAME      "elf32-m32rle"
4258
4259 #define elf_info_to_howto                       m32r_info_to_howto
4260 #define elf_info_to_howto_rel                   m32r_info_to_howto_rel
4261 #define elf_backend_section_from_bfd_section    _bfd_m32r_elf_section_from_bfd_section
4262 #define elf_backend_symbol_processing           _bfd_m32r_elf_symbol_processing
4263 #define elf_backend_add_symbol_hook             m32r_elf_add_symbol_hook
4264 #define elf_backend_relocate_section            m32r_elf_relocate_section
4265 #define elf_backend_gc_mark_hook                m32r_elf_gc_mark_hook
4266 #define elf_backend_gc_sweep_hook               m32r_elf_gc_sweep_hook
4267 #define elf_backend_check_relocs                m32r_elf_check_relocs
4268
4269 #define elf_backend_create_dynamic_sections     m32r_elf_create_dynamic_sections
4270 #define bfd_elf32_bfd_link_hash_table_create    m32r_elf_link_hash_table_create
4271 #define elf_backend_size_dynamic_sections       m32r_elf_size_dynamic_sections
4272 #define elf_backend_finish_dynamic_sections     m32r_elf_finish_dynamic_sections
4273 #define elf_backend_adjust_dynamic_symbol       m32r_elf_adjust_dynamic_symbol
4274 #define elf_backend_finish_dynamic_symbol       m32r_elf_finish_dynamic_symbol
4275 #define elf_backend_reloc_type_class            m32r_elf_reloc_type_class
4276 #define elf_backend_copy_indirect_symbol        m32r_elf_copy_indirect_symbol
4277
4278 #define elf_backend_can_gc_sections             1
4279 /*#if !USE_REL
4280 #define elf_backend_rela_normal                 1
4281 #endif*/
4282 #define elf_backend_can_refcount 1
4283 #define elf_backend_want_got_plt 1
4284 #define elf_backend_plt_readonly 1
4285 #define elf_backend_want_plt_sym 0
4286 #define elf_backend_got_header_size 12
4287
4288 #define elf_backend_may_use_rel_p       1
4289 #ifdef USE_M32R_OLD_RELOC
4290 #define elf_backend_default_use_rela_p  0
4291 #define elf_backend_may_use_rela_p      0
4292 #else
4293 #define elf_backend_default_use_rela_p  1
4294 #define elf_backend_may_use_rela_p      1
4295 #define elf_backend_fake_sections       m32r_elf_fake_sections
4296 #endif
4297
4298 #define elf_backend_object_p                    m32r_elf_object_p
4299 #define elf_backend_final_write_processing      m32r_elf_final_write_processing
4300 #define bfd_elf32_bfd_merge_private_bfd_data    m32r_elf_merge_private_bfd_data
4301 #define bfd_elf32_bfd_set_private_flags         m32r_elf_set_private_flags
4302 #define bfd_elf32_bfd_print_private_bfd_data    m32r_elf_print_private_bfd_data
4303 #define elf_backend_special_sections            m32r_elf_special_sections
4304
4305 #include "elf32-target.h"
4306
4307 #undef ELF_MAXPAGESIZE
4308 #define ELF_MAXPAGESIZE         0x1000
4309
4310 #undef TARGET_BIG_SYM
4311 #define TARGET_BIG_SYM          bfd_elf32_m32rlin_vec
4312 #undef TARGET_BIG_NAME
4313 #define TARGET_BIG_NAME         "elf32-m32r-linux"
4314 #undef TARGET_LITTLE_SYM
4315 #define TARGET_LITTLE_SYM       bfd_elf32_m32rlelin_vec
4316 #undef TARGET_LITTLE_NAME
4317 #define TARGET_LITTLE_NAME      "elf32-m32rle-linux"
4318 #undef elf32_bed
4319 #define elf32_bed               elf32_m32r_lin_bed
4320
4321 #include "elf32-target.h"
4322