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