Coverage Report

Created: 2026-05-11 07:54

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