Coverage Report

Created: 2026-04-04 08:16

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