OSDN Git Service

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