Coverage Report

Created: 2026-07-25 10:20

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