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