Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/elf32-rx.c
Line
Count
Source (jump to first uncovered line)
1
/* Renesas RX specific support for 32-bit ELF.
2
   Copyright (C) 2008-2025 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/rx.h"
26
#include "libiberty.h"
27
#include "elf32-rx.h"
28
29
#define RX_OPCODE_BIG_ENDIAN 0
30
31
/* This is a meta-target that's used only with objcopy, to avoid the
32
   endian-swap we would otherwise get.  We check for this in
33
   rx_elf_object_p().  */
34
const bfd_target rx_elf32_be_ns_vec;
35
const bfd_target rx_elf32_be_vec;
36
37
#ifdef DEBUG
38
char * rx_get_reloc (long);
39
void rx_dump_symtab (bfd *, void *, void *);
40
#endif
41
42
#define RXREL(n,sz,bit,shift,complain,pcrel)             \
43
  HOWTO (R_RX_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
44
   bfd_elf_generic_reloc, "R_RX_" #n, false, 0, ~0, false)
45
46
/* Note that the relocations around 0x7f are internal to this file;
47
   feel free to move them as needed to avoid conflicts with published
48
   relocation numbers.  */
49
50
static reloc_howto_type rx_elf_howto_table [] =
51
{
52
  RXREL (NONE,         0,  0, 0, dont,     false),
53
  RXREL (DIR32,        4, 32, 0, signed,   false),
54
  RXREL (DIR24S,       4, 24, 0, signed,   false),
55
  RXREL (DIR16,        2, 16, 0, dont,     false),
56
  RXREL (DIR16U,       2, 16, 0, unsigned, false),
57
  RXREL (DIR16S,       2, 16, 0, signed,   false),
58
  RXREL (DIR8,         1,  8, 0, dont,     false),
59
  RXREL (DIR8U,        1,  8, 0, unsigned, false),
60
  RXREL (DIR8S,        1,  8, 0, signed,   false),
61
  RXREL (DIR24S_PCREL, 4, 24, 0, signed,   true),
62
  RXREL (DIR16S_PCREL, 2, 16, 0, signed,   true),
63
  RXREL (DIR8S_PCREL,  1,  8, 0, signed,   true),
64
  RXREL (DIR16UL,      2, 16, 2, unsigned, false),
65
  RXREL (DIR16UW,      2, 16, 1, unsigned, false),
66
  RXREL (DIR8UL,       1,  8, 2, unsigned, false),
67
  RXREL (DIR8UW,       1,  8, 1, unsigned, false),
68
  RXREL (DIR32_REV,    2, 16, 0, dont,     false),
69
  RXREL (DIR16_REV,    2, 16, 0, dont,     false),
70
  RXREL (DIR3U_PCREL,  1,  3, 0, dont,     true),
71
72
  EMPTY_HOWTO (0x13),
73
  EMPTY_HOWTO (0x14),
74
  EMPTY_HOWTO (0x15),
75
  EMPTY_HOWTO (0x16),
76
  EMPTY_HOWTO (0x17),
77
  EMPTY_HOWTO (0x18),
78
  EMPTY_HOWTO (0x19),
79
  EMPTY_HOWTO (0x1a),
80
  EMPTY_HOWTO (0x1b),
81
  EMPTY_HOWTO (0x1c),
82
  EMPTY_HOWTO (0x1d),
83
  EMPTY_HOWTO (0x1e),
84
  EMPTY_HOWTO (0x1f),
85
86
  RXREL (RH_3_PCREL, 1,  3, 0, signed,   true),
87
  RXREL (RH_16_OP,   2, 16, 0, signed,   false),
88
  RXREL (RH_24_OP,   4, 24, 0, signed,   false),
89
  RXREL (RH_32_OP,   4, 32, 0, signed,   false),
90
  RXREL (RH_24_UNS,  4, 24, 0, unsigned, false),
91
  RXREL (RH_8_NEG,   1,  8, 0, signed,   false),
92
  RXREL (RH_16_NEG,  2, 16, 0, signed,   false),
93
  RXREL (RH_24_NEG,  4, 24, 0, signed,   false),
94
  RXREL (RH_32_NEG,  4, 32, 0, signed,   false),
95
  RXREL (RH_DIFF,    4, 32, 0, signed,   false),
96
  RXREL (RH_GPRELB,  2, 16, 0, unsigned, false),
97
  RXREL (RH_GPRELW,  2, 16, 0, unsigned, false),
98
  RXREL (RH_GPRELL,  2, 16, 0, unsigned, false),
99
  RXREL (RH_RELAX,   0,  0, 0, dont,   false),
100
101
  EMPTY_HOWTO (0x2e),
102
  EMPTY_HOWTO (0x2f),
103
  EMPTY_HOWTO (0x30),
104
  EMPTY_HOWTO (0x31),
105
  EMPTY_HOWTO (0x32),
106
  EMPTY_HOWTO (0x33),
107
  EMPTY_HOWTO (0x34),
108
  EMPTY_HOWTO (0x35),
109
  EMPTY_HOWTO (0x36),
110
  EMPTY_HOWTO (0x37),
111
  EMPTY_HOWTO (0x38),
112
  EMPTY_HOWTO (0x39),
113
  EMPTY_HOWTO (0x3a),
114
  EMPTY_HOWTO (0x3b),
115
  EMPTY_HOWTO (0x3c),
116
  EMPTY_HOWTO (0x3d),
117
  EMPTY_HOWTO (0x3e),
118
  EMPTY_HOWTO (0x3f),
119
  EMPTY_HOWTO (0x40),
120
121
  RXREL (ABS32,        4, 32, 0, dont,     false),
122
  RXREL (ABS24S,       4, 24, 0, signed,   false),
123
  RXREL (ABS16,        2, 16, 0, dont,     false),
124
  RXREL (ABS16U,       2, 16, 0, unsigned, false),
125
  RXREL (ABS16S,       2, 16, 0, signed,   false),
126
  RXREL (ABS8,         1,  8, 0, dont,     false),
127
  RXREL (ABS8U,        1,  8, 0, unsigned, false),
128
  RXREL (ABS8S,        1,  8, 0, signed,   false),
129
  RXREL (ABS24S_PCREL, 4, 24, 0, signed,   true),
130
  RXREL (ABS16S_PCREL, 2, 16, 0, signed,   true),
131
  RXREL (ABS8S_PCREL,  1,  8, 0, signed,   true),
132
  RXREL (ABS16UL,      2, 16, 0, unsigned, false),
133
  RXREL (ABS16UW,      2, 16, 0, unsigned, false),
134
  RXREL (ABS8UL,       1,  8, 0, unsigned, false),
135
  RXREL (ABS8UW,       1,  8, 0, unsigned, false),
136
  RXREL (ABS32_REV,    4, 32, 0, dont,     false),
137
  RXREL (ABS16_REV,    2, 16, 0, dont,     false),
138
139
0
#define STACK_REL_P(x) ((x) <= R_RX_ABS16_REV && (x) >= R_RX_ABS32)
140
141
  EMPTY_HOWTO (0x52),
142
  EMPTY_HOWTO (0x53),
143
  EMPTY_HOWTO (0x54),
144
  EMPTY_HOWTO (0x55),
145
  EMPTY_HOWTO (0x56),
146
  EMPTY_HOWTO (0x57),
147
  EMPTY_HOWTO (0x58),
148
  EMPTY_HOWTO (0x59),
149
  EMPTY_HOWTO (0x5a),
150
  EMPTY_HOWTO (0x5b),
151
  EMPTY_HOWTO (0x5c),
152
  EMPTY_HOWTO (0x5d),
153
  EMPTY_HOWTO (0x5e),
154
  EMPTY_HOWTO (0x5f),
155
  EMPTY_HOWTO (0x60),
156
  EMPTY_HOWTO (0x61),
157
  EMPTY_HOWTO (0x62),
158
  EMPTY_HOWTO (0x63),
159
  EMPTY_HOWTO (0x64),
160
  EMPTY_HOWTO (0x65),
161
  EMPTY_HOWTO (0x66),
162
  EMPTY_HOWTO (0x67),
163
  EMPTY_HOWTO (0x68),
164
  EMPTY_HOWTO (0x69),
165
  EMPTY_HOWTO (0x6a),
166
  EMPTY_HOWTO (0x6b),
167
  EMPTY_HOWTO (0x6c),
168
  EMPTY_HOWTO (0x6d),
169
  EMPTY_HOWTO (0x6e),
170
  EMPTY_HOWTO (0x6f),
171
  EMPTY_HOWTO (0x70),
172
  EMPTY_HOWTO (0x71),
173
  EMPTY_HOWTO (0x72),
174
  EMPTY_HOWTO (0x73),
175
  EMPTY_HOWTO (0x74),
176
  EMPTY_HOWTO (0x75),
177
  EMPTY_HOWTO (0x76),
178
  EMPTY_HOWTO (0x77),
179
180
  /* These are internal.  */
181
  /* A 5-bit unsigned displacement to a B/W/L address, at bit position 8/12.  */
182
  /* ---- ----   4--- 3210.  */
183
0
#define R_RX_RH_ABS5p8B 0x78
184
  RXREL (RH_ABS5p8B,   0,  0, 0, dont,     false),
185
0
#define R_RX_RH_ABS5p8W 0x79
186
  RXREL (RH_ABS5p8W,   0,  0, 0, dont,     false),
187
0
#define R_RX_RH_ABS5p8L 0x7a
188
  RXREL (RH_ABS5p8L,   0,  0, 0, dont,     false),
189
  /* A 5-bit unsigned displacement to a B/W/L address, at bit position 5/12.  */
190
  /* ---- -432   1--- 0---.  */
191
0
#define R_RX_RH_ABS5p5B 0x7b
192
  RXREL (RH_ABS5p5B,   0,  0, 0, dont,     false),
193
0
#define R_RX_RH_ABS5p5W 0x7c
194
  RXREL (RH_ABS5p5W,   0,  0, 0, dont,     false),
195
0
#define R_RX_RH_ABS5p5L 0x7d
196
  RXREL (RH_ABS5p5L,   0,  0, 0, dont,     false),
197
  /* A 4-bit unsigned immediate at bit position 8.  */
198
0
#define R_RX_RH_UIMM4p8 0x7e
199
  RXREL (RH_UIMM4p8,   0,  0, 0, dont,     false),
200
  /* A 4-bit negative unsigned immediate at bit position 8.  */
201
0
#define R_RX_RH_UNEG4p8 0x7f
202
  RXREL (RH_UNEG4p8,   0,  0, 0, dont,     false),
203
  /* End of internal relocs.  */
204
205
  RXREL (SYM,     4, 32, 0, dont, false),
206
  RXREL (OPneg,     4, 32, 0, dont, false),
207
  RXREL (OPadd,     4, 32, 0, dont, false),
208
  RXREL (OPsub,     4, 32, 0, dont, false),
209
  RXREL (OPmul,     4, 32, 0, dont, false),
210
  RXREL (OPdiv,     4, 32, 0, dont, false),
211
  RXREL (OPshla,    4, 32, 0, dont, false),
212
  RXREL (OPshra,    4, 32, 0, dont, false),
213
  RXREL (OPsctsize, 4, 32, 0, dont, false),
214
215
  EMPTY_HOWTO (0x89),
216
  EMPTY_HOWTO (0x8a),
217
  EMPTY_HOWTO (0x8b),
218
  EMPTY_HOWTO (0x8c),
219
220
  RXREL (OPscttop,  4, 32, 0, dont, false),
221
222
  EMPTY_HOWTO (0x8e),
223
  EMPTY_HOWTO (0x8f),
224
225
  RXREL (OPand,     4, 32, 0, dont, false),
226
  RXREL (OPor,      4, 32, 0, dont, false),
227
  RXREL (OPxor,     4, 32, 0, dont, false),
228
  RXREL (OPnot,     4, 32, 0, dont, false),
229
  RXREL (OPmod,     4, 32, 0, dont, false),
230
  RXREL (OPromtop,  4, 32, 0, dont, false),
231
  RXREL (OPramtop,  4, 32, 0, dont, false)
232
};
233

234
/* Map BFD reloc types to RX ELF reloc types.  */
235
236
struct rx_reloc_map
237
{
238
  bfd_reloc_code_real_type  bfd_reloc_val;
239
  unsigned int        rx_reloc_val;
240
};
241
242
static const struct rx_reloc_map rx_reloc_map [] =
243
{
244
  { BFD_RELOC_NONE,   R_RX_NONE },
245
  { BFD_RELOC_8,    R_RX_DIR8S },
246
  { BFD_RELOC_16,   R_RX_DIR16S },
247
  { BFD_RELOC_24,   R_RX_DIR24S },
248
  { BFD_RELOC_32,   R_RX_DIR32 },
249
  { BFD_RELOC_RX_16_OP,   R_RX_DIR16 },
250
  { BFD_RELOC_RX_DIR3U_PCREL, R_RX_DIR3U_PCREL },
251
  { BFD_RELOC_8_PCREL,    R_RX_DIR8S_PCREL },
252
  { BFD_RELOC_16_PCREL,   R_RX_DIR16S_PCREL },
253
  { BFD_RELOC_24_PCREL,   R_RX_DIR24S_PCREL },
254
  { BFD_RELOC_RX_8U,    R_RX_DIR8U },
255
  { BFD_RELOC_RX_16U,   R_RX_DIR16U },
256
  { BFD_RELOC_RX_24U,   R_RX_RH_24_UNS },
257
  { BFD_RELOC_RX_NEG8,    R_RX_RH_8_NEG },
258
  { BFD_RELOC_RX_NEG16,   R_RX_RH_16_NEG },
259
  { BFD_RELOC_RX_NEG24,   R_RX_RH_24_NEG },
260
  { BFD_RELOC_RX_NEG32,   R_RX_RH_32_NEG },
261
  { BFD_RELOC_RX_DIFF,    R_RX_RH_DIFF },
262
  { BFD_RELOC_RX_GPRELB,  R_RX_RH_GPRELB },
263
  { BFD_RELOC_RX_GPRELW,  R_RX_RH_GPRELW },
264
  { BFD_RELOC_RX_GPRELL,  R_RX_RH_GPRELL },
265
  { BFD_RELOC_RX_RELAX,   R_RX_RH_RELAX },
266
  { BFD_RELOC_RX_SYM,   R_RX_SYM },
267
  { BFD_RELOC_RX_OP_SUBTRACT, R_RX_OPsub },
268
  { BFD_RELOC_RX_OP_NEG,  R_RX_OPneg },
269
  { BFD_RELOC_RX_ABS8,    R_RX_ABS8 },
270
  { BFD_RELOC_RX_ABS16,   R_RX_ABS16 },
271
  { BFD_RELOC_RX_ABS16_REV, R_RX_ABS16_REV },
272
  { BFD_RELOC_RX_ABS32,   R_RX_ABS32 },
273
  { BFD_RELOC_RX_ABS32_REV, R_RX_ABS32_REV },
274
  { BFD_RELOC_RX_ABS16UL, R_RX_ABS16UL },
275
  { BFD_RELOC_RX_ABS16UW, R_RX_ABS16UW },
276
  { BFD_RELOC_RX_ABS16U,  R_RX_ABS16U }
277
};
278
279
0
#define BIGE(abfd)       ((abfd)->xvec->byteorder == BFD_ENDIAN_BIG)
280
281
static reloc_howto_type *
282
rx_reloc_type_lookup (bfd *          abfd ATTRIBUTE_UNUSED,
283
          bfd_reloc_code_real_type code)
284
0
{
285
0
  unsigned int i;
286
287
0
  if (code == BFD_RELOC_RX_32_OP)
288
0
    return rx_elf_howto_table + R_RX_DIR32;
289
290
0
  for (i = ARRAY_SIZE (rx_reloc_map); i--;)
291
0
    if (rx_reloc_map [i].bfd_reloc_val == code)
292
0
      return rx_elf_howto_table + rx_reloc_map[i].rx_reloc_val;
293
294
0
  return NULL;
295
0
}
296
297
static reloc_howto_type *
298
rx_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
299
0
{
300
0
  unsigned int i;
301
302
0
  for (i = 0; i < ARRAY_SIZE (rx_elf_howto_table); i++)
303
0
    if (rx_elf_howto_table[i].name != NULL
304
0
  && strcasecmp (rx_elf_howto_table[i].name, r_name) == 0)
305
0
      return rx_elf_howto_table + i;
306
307
0
  return NULL;
308
0
}
309
310
/* Set the howto pointer for an RX ELF reloc.  */
311
312
static bool
313
rx_info_to_howto_rela (bfd *       abfd,
314
           arelent *     cache_ptr,
315
           Elf_Internal_Rela * dst)
316
0
{
317
0
  unsigned int r_type;
318
319
0
  r_type = ELF32_R_TYPE (dst->r_info);
320
0
  BFD_ASSERT (R_RX_max == ARRAY_SIZE (rx_elf_howto_table));
321
0
  if (r_type >= ARRAY_SIZE (rx_elf_howto_table))
322
0
    {
323
      /* xgettext:c-format */
324
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
325
0
        abfd, r_type);
326
0
      bfd_set_error (bfd_error_bad_value);
327
0
      return false;
328
0
    }
329
0
  cache_ptr->howto = rx_elf_howto_table + r_type;
330
0
  if (cache_ptr->howto->name == NULL)
331
0
    {
332
      /* xgettext:c-format */
333
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
334
0
        abfd, r_type);
335
0
      bfd_set_error (bfd_error_bad_value);
336
0
      return false;
337
0
    }
338
0
  return true;
339
0
}
340

341
static bfd_vma
342
get_symbol_value (const char *      name,
343
      struct bfd_link_info *  info,
344
      bfd *       input_bfd,
345
      asection *      input_section,
346
      int       offset)
347
0
{
348
0
  bfd_vma value = 0;
349
0
  struct bfd_link_hash_entry * h;
350
351
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
352
353
0
  if (h == NULL
354
0
      || (h->type != bfd_link_hash_defined
355
0
    && h->type != bfd_link_hash_defweak))
356
0
    (*info->callbacks->undefined_symbol)
357
0
      (info, name, input_bfd, input_section, offset, true);
358
0
  else
359
0
    value = (h->u.def.value
360
0
       + h->u.def.section->output_section->vma
361
0
       + h->u.def.section->output_offset);
362
363
0
  return value;
364
0
}
365
366
static bfd_vma
367
get_symbol_value_maybe (const char *    name,
368
      struct bfd_link_info *  info)
369
0
{
370
0
  bfd_vma value = 0;
371
0
  struct bfd_link_hash_entry * h;
372
373
0
  h = bfd_link_hash_lookup (info->hash, name, false, false, true);
374
375
0
  if (h == NULL
376
0
      || (h->type != bfd_link_hash_defined
377
0
    && h->type != bfd_link_hash_defweak))
378
0
    return 0;
379
0
  else
380
0
    value = (h->u.def.value
381
0
       + h->u.def.section->output_section->vma
382
0
       + h->u.def.section->output_offset);
383
384
0
  return value;
385
0
}
386
387
static bfd_vma
388
get_gp (struct bfd_link_info *  info,
389
  bfd *     abfd,
390
  asection *    sec,
391
  int     offset)
392
0
{
393
0
  static bool cached = false;
394
0
  static bfd_vma cached_value = 0;
395
396
0
  if (!cached)
397
0
    {
398
0
      cached_value = get_symbol_value ("__gp", info, abfd, sec, offset);
399
0
      cached = true;
400
0
    }
401
0
  return cached_value;
402
0
}
403
404
static bfd_vma
405
get_romstart (struct bfd_link_info *  info,
406
        bfd *         abfd,
407
        asection *        sec,
408
        int         offset)
409
0
{
410
0
  static bool cached = false;
411
0
  static bfd_vma cached_value = 0;
412
413
0
  if (!cached)
414
0
    {
415
0
      cached_value = get_symbol_value ("_start", info, abfd, sec, offset);
416
0
      cached = true;
417
0
    }
418
0
  return cached_value;
419
0
}
420
421
static bfd_vma
422
get_ramstart (struct bfd_link_info *  info,
423
        bfd *         abfd,
424
        asection *        sec,
425
        int         offset)
426
0
{
427
0
  static bool cached = false;
428
0
  static bfd_vma cached_value = 0;
429
430
0
  if (!cached)
431
0
    {
432
0
      cached_value = get_symbol_value ("__datastart", info, abfd, sec, offset);
433
0
      cached = true;
434
0
    }
435
0
  return cached_value;
436
0
}
437
438
0
#define NUM_STACK_ENTRIES 16
439
static int32_t rx_stack [ NUM_STACK_ENTRIES ];
440
static unsigned int rx_stack_top;
441
442
#define RX_STACK_PUSH(val)      \
443
0
  do            \
444
0
    {           \
445
0
      if (rx_stack_top < NUM_STACK_ENTRIES) \
446
0
  rx_stack [rx_stack_top ++] = (val); \
447
0
      else          \
448
0
  r = bfd_reloc_dangerous;   \
449
0
    }           \
450
0
  while (0)
451
452
#define RX_STACK_POP(dest)      \
453
0
  do            \
454
0
    {           \
455
0
      if (rx_stack_top > 0)     \
456
0
  (dest) = rx_stack [-- rx_stack_top]; \
457
0
      else          \
458
0
  (dest) = 0, r = bfd_reloc_dangerous; \
459
0
    }           \
460
0
  while (0)
461
462
/* Relocate an RX ELF section.
463
   There is some attempt to make this function usable for many architectures,
464
   both USE_REL and USE_RELA ['twould be nice if such a critter existed],
465
   if only to serve as a learning tool.
466
467
   The RELOCATE_SECTION function is called by the new ELF backend linker
468
   to handle the relocations for a section.
469
470
   The relocs are always passed as Rela structures; if the section
471
   actually uses Rel structures, the r_addend field will always be
472
   zero.
473
474
   This function is responsible for adjusting the section contents as
475
   necessary, and (if using Rela relocs and generating a relocatable
476
   output file) adjusting the reloc addend as necessary.
477
478
   This function does not have to worry about setting the reloc
479
   address or the reloc symbol index.
480
481
   LOCAL_SYMS is a pointer to the swapped in local symbols.
482
483
   LOCAL_SECTIONS is an array giving the section in the input file
484
   corresponding to the st_shndx field of each local symbol.
485
486
   The global hash table entry for the global symbols can be found
487
   via elf_sym_hashes (input_bfd).
488
489
   When generating relocatable output, this function must handle
490
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
491
   going to be the section symbol corresponding to the output
492
   section, which means that the addend must be adjusted
493
   accordingly.  */
494
495
static int
496
rx_elf_relocate_section
497
    (bfd *         output_bfd,
498
     struct bfd_link_info *  info,
499
     bfd *         input_bfd,
500
     asection *        input_section,
501
     bfd_byte *        contents,
502
     Elf_Internal_Rela *     relocs,
503
     Elf_Internal_Sym *      local_syms,
504
     asection **       local_sections)
505
0
{
506
0
  Elf_Internal_Shdr *symtab_hdr;
507
0
  struct elf_link_hash_entry **sym_hashes;
508
0
  Elf_Internal_Rela *rel;
509
0
  Elf_Internal_Rela *relend;
510
0
  bool pid_mode;
511
0
  bool saw_subtract = false;
512
0
  const char *table_default_cache = NULL;
513
0
  bfd_vma table_start_cache = 0;
514
0
  bfd_vma table_end_cache = 0;
515
516
0
  if (elf_elfheader (output_bfd)->e_flags & E_FLAG_RX_PID)
517
0
    pid_mode = true;
518
0
  else
519
0
    pid_mode = false;
520
521
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
522
0
  sym_hashes = elf_sym_hashes (input_bfd);
523
0
  relend     = relocs + input_section->reloc_count;
524
0
  for (rel = relocs; rel < relend; rel ++)
525
0
    {
526
0
      reloc_howto_type *howto;
527
0
      unsigned long r_symndx;
528
0
      Elf_Internal_Sym *sym;
529
0
      asection *sec;
530
0
      struct elf_link_hash_entry *h;
531
0
      bfd_vma relocation;
532
0
      bfd_reloc_status_type r;
533
0
      const char * name = NULL;
534
0
      bool unresolved_reloc = true;
535
0
      int r_type;
536
537
0
      r_type = ELF32_R_TYPE (rel->r_info);
538
0
      r_symndx = ELF32_R_SYM (rel->r_info);
539
540
0
      howto  = rx_elf_howto_table + ELF32_R_TYPE (rel->r_info);
541
0
      h      = NULL;
542
0
      sym    = NULL;
543
0
      sec    = NULL;
544
0
      relocation = 0;
545
546
0
      if (rx_stack_top == 0)
547
0
  saw_subtract = false;
548
549
0
      if (r_symndx < symtab_hdr->sh_info)
550
0
  {
551
0
    sym = local_syms + r_symndx;
552
0
    sec = local_sections [r_symndx];
553
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
554
555
0
    name = bfd_elf_string_from_elf_section
556
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
557
0
    name = sym->st_name == 0 ? bfd_section_name (sec) : name;
558
0
  }
559
0
      else
560
0
  {
561
0
    bool warned, ignored;
562
563
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
564
0
           r_symndx, symtab_hdr, sym_hashes, h,
565
0
           sec, relocation, unresolved_reloc,
566
0
           warned, ignored);
567
568
0
    name = h->root.root.string;
569
0
  }
570
571
0
      if (startswith (name, "$tableentry$default$"))
572
0
  {
573
0
    bfd_vma entry_vma;
574
0
    int idx;
575
0
    char *buf;
576
577
0
    if (table_default_cache != name)
578
0
      {
579
580
        /* All relocs for a given table should be to the same
581
     (weak) default symbol) so we can use it to detect a
582
     cache miss.  We use the offset into the table to find
583
     the "real" symbol.  Calculate and store the table's
584
     offset here.  */
585
586
0
        table_default_cache = name;
587
588
        /* We have already done error checking in rx_table_find().  */
589
590
0
        buf = (char *) bfd_malloc (13 + strlen (name + 20));
591
0
        if (buf == NULL)
592
0
    return false;
593
594
0
        sprintf (buf, "$tablestart$%s", name + 20);
595
0
        table_start_cache = get_symbol_value (buf,
596
0
                info,
597
0
                input_bfd,
598
0
                input_section,
599
0
                rel->r_offset);
600
601
0
        sprintf (buf, "$tableend$%s", name + 20);
602
0
        table_end_cache = get_symbol_value (buf,
603
0
              info,
604
0
              input_bfd,
605
0
              input_section,
606
0
              rel->r_offset);
607
608
0
        free (buf);
609
0
      }
610
611
0
    entry_vma = (input_section->output_section->vma
612
0
           + input_section->output_offset
613
0
           + rel->r_offset);
614
615
0
    if (table_end_cache <= entry_vma || entry_vma < table_start_cache)
616
0
      {
617
        /* xgettext:c-format */
618
0
        _bfd_error_handler (_("%pB:%pA: table entry %s outside table"),
619
0
          input_bfd, input_section,
620
0
          name);
621
0
      }
622
0
    else if ((int) (entry_vma - table_start_cache) % 4)
623
0
      {
624
        /* xgettext:c-format */
625
0
        _bfd_error_handler (_("%pB:%pA: table entry %s not word-aligned within table"),
626
0
          input_bfd, input_section,
627
0
          name);
628
0
      }
629
0
    else
630
0
      {
631
0
        idx = (int) (entry_vma - table_start_cache) / 4;
632
633
        /* This will look like $tableentry$<N>$<name> */
634
0
        buf = (char *) bfd_malloc (12 + 20 + strlen (name + 20));
635
0
        if (buf == NULL)
636
0
    return false;
637
638
0
        sprintf (buf, "$tableentry$%d$%s", idx, name + 20);
639
640
0
        h = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash, buf, false, false, true);
641
642
0
        if (h)
643
0
    {
644
0
      relocation = (h->root.u.def.value
645
0
        + h->root.u.def.section->output_section->vma
646
0
        + h->root.u.def.section->output_offset);;
647
0
    }
648
649
0
        free (buf);
650
0
      }
651
0
  }
652
653
0
      if (sec != NULL && discarded_section (sec))
654
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
655
0
           rel, 1, relend, howto, 0, contents);
656
657
0
      if (bfd_link_relocatable (info))
658
0
  {
659
    /* This is a relocatable link.  We don't have to change
660
       anything, unless the reloc is against a section symbol,
661
       in which case we have to adjust according to where the
662
       section symbol winds up in the output section.  */
663
0
    if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
664
0
      rel->r_addend += sec->output_offset;
665
0
    continue;
666
0
  }
667
668
0
      if (h != NULL && h->root.type == bfd_link_hash_undefweak)
669
  /* If the symbol is undefined and weak
670
     then the relocation resolves to zero.  */
671
0
  relocation = 0;
672
0
      else
673
0
  {
674
0
    if (howto->pc_relative)
675
0
      {
676
0
        relocation -= (input_section->output_section->vma
677
0
           + input_section->output_offset
678
0
           + rel->r_offset);
679
0
        if (r_type != R_RX_RH_3_PCREL
680
0
      && r_type != R_RX_DIR3U_PCREL)
681
0
    relocation ++;
682
0
      }
683
684
0
    relocation += rel->r_addend;
685
0
  }
686
687
0
      r = bfd_reloc_ok;
688
689
0
#define RANGE(a,b) \
690
0
  if (a > (long) relocation || (long) relocation > b)   \
691
0
    r = bfd_reloc_overflow
692
0
#define ALIGN(m) \
693
0
  if (relocation & m)           \
694
0
    r = bfd_reloc_other
695
0
#define OP(i) \
696
0
  (contents[rel->r_offset + (i)])
697
0
#define WARN_REDHAT(type) \
698
  /* xgettext:c-format */          \
699
0
  _bfd_error_handler            \
700
0
    (_("%pB:%pA: warning: deprecated Red Hat reloc "    \
701
0
       "%s detected against: %s"),        \
702
0
     input_bfd, input_section, #type, name)
703
704
      /* Check for unsafe relocs in PID mode.  These are any relocs where
705
   an absolute address is being computed.  There are special cases
706
   for relocs against symbols that are known to be referenced in
707
   crt0.o before the PID base address register has been initialised.  */
708
0
#define UNSAFE_FOR_PID              \
709
0
  do                  \
710
0
    {                 \
711
0
      if (pid_mode              \
712
0
    && sec != NULL            \
713
0
    && sec->flags & SEC_READONLY          \
714
0
    && !(input_section->flags & SEC_DEBUGGING)     \
715
0
    && strcmp (name, "__pid_base") != 0        \
716
0
    && strcmp (name, "__gp") != 0          \
717
0
    && strcmp (name, "__romdatastart") != 0      \
718
0
    && !saw_subtract)           \
719
  /* xgettext:c-format */            \
720
0
  _bfd_error_handler (_("%pB(%pA): unsafe PID relocation %s "  \
721
0
            "at %#" PRIx64 " (against %s in %s)"),  \
722
0
          input_bfd, input_section, howto->name,  \
723
0
          (uint64_t) (input_section->output_section->vma \
724
0
          + input_section->output_offset  \
725
0
          + rel->r_offset),   \
726
0
          name, sec->name);       \
727
0
    }                 \
728
0
  while (0)
729
730
      /* Opcode relocs are always big endian.  Data relocs are bi-endian.  */
731
0
      switch (r_type)
732
0
  {
733
0
  case R_RX_NONE:
734
0
    break;
735
736
0
  case R_RX_RH_RELAX:
737
0
    break;
738
739
0
  case R_RX_RH_3_PCREL:
740
0
    WARN_REDHAT ("RX_RH_3_PCREL");
741
0
    RANGE (3, 10);
742
0
    OP (0) &= 0xf8;
743
0
    OP (0) |= relocation & 0x07;
744
0
    break;
745
746
0
  case R_RX_RH_8_NEG:
747
0
    WARN_REDHAT ("RX_RH_8_NEG");
748
0
    relocation = - relocation;
749
    /* Fall through.  */
750
0
  case R_RX_DIR8S_PCREL:
751
0
    UNSAFE_FOR_PID;
752
0
    RANGE (-128, 127);
753
0
    OP (0) = relocation;
754
0
    break;
755
756
0
  case R_RX_DIR8S:
757
0
    UNSAFE_FOR_PID;
758
0
    RANGE (-128, 255);
759
0
    OP (0) = relocation;
760
0
    break;
761
762
0
  case R_RX_DIR8U:
763
0
    UNSAFE_FOR_PID;
764
0
    RANGE (0, 255);
765
0
    OP (0) = relocation;
766
0
    break;
767
768
0
  case R_RX_RH_16_NEG:
769
0
    WARN_REDHAT ("RX_RH_16_NEG");
770
0
    relocation = - relocation;
771
    /* Fall through.  */
772
0
  case R_RX_DIR16S_PCREL:
773
0
    UNSAFE_FOR_PID;
774
0
    RANGE (-32768, 32767);
775
#if RX_OPCODE_BIG_ENDIAN
776
#else
777
0
    OP (0) = relocation;
778
0
    OP (1) = relocation >> 8;
779
0
#endif
780
0
    break;
781
782
0
  case R_RX_RH_16_OP:
783
0
    WARN_REDHAT ("RX_RH_16_OP");
784
0
    UNSAFE_FOR_PID;
785
0
    RANGE (-32768, 32767);
786
#if RX_OPCODE_BIG_ENDIAN
787
    OP (1) = relocation;
788
    OP (0) = relocation >> 8;
789
#else
790
0
    OP (0) = relocation;
791
0
    OP (1) = relocation >> 8;
792
0
#endif
793
0
    break;
794
795
0
  case R_RX_DIR16S:
796
0
    UNSAFE_FOR_PID;
797
0
    RANGE (-32768, 65535);
798
0
    if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
799
0
      {
800
0
        OP (1) = relocation;
801
0
        OP (0) = relocation >> 8;
802
0
      }
803
0
    else
804
0
      {
805
0
        OP (0) = relocation;
806
0
        OP (1) = relocation >> 8;
807
0
      }
808
0
    break;
809
810
0
  case R_RX_DIR16U:
811
0
    UNSAFE_FOR_PID;
812
0
    RANGE (0, 65536);
813
#if RX_OPCODE_BIG_ENDIAN
814
    OP (1) = relocation;
815
    OP (0) = relocation >> 8;
816
#else
817
0
    OP (0) = relocation;
818
0
    OP (1) = relocation >> 8;
819
0
#endif
820
0
    break;
821
822
0
  case R_RX_DIR16:
823
0
    UNSAFE_FOR_PID;
824
0
    RANGE (-32768, 65536);
825
#if RX_OPCODE_BIG_ENDIAN
826
    OP (1) = relocation;
827
    OP (0) = relocation >> 8;
828
#else
829
0
    OP (0) = relocation;
830
0
    OP (1) = relocation >> 8;
831
0
#endif
832
0
    break;
833
834
0
  case R_RX_DIR16_REV:
835
0
    UNSAFE_FOR_PID;
836
0
    RANGE (-32768, 65536);
837
#if RX_OPCODE_BIG_ENDIAN
838
    OP (0) = relocation;
839
    OP (1) = relocation >> 8;
840
#else
841
0
    OP (1) = relocation;
842
0
    OP (0) = relocation >> 8;
843
0
#endif
844
0
    break;
845
846
0
  case R_RX_DIR3U_PCREL:
847
0
    RANGE (3, 10);
848
0
    OP (0) &= 0xf8;
849
0
    OP (0) |= relocation & 0x07;
850
0
    break;
851
852
0
  case R_RX_RH_24_NEG:
853
0
    UNSAFE_FOR_PID;
854
0
    WARN_REDHAT ("RX_RH_24_NEG");
855
0
    relocation = - relocation;
856
    /* Fall through.  */
857
0
  case R_RX_DIR24S_PCREL:
858
0
    RANGE (-0x800000, 0x7fffff);
859
#if RX_OPCODE_BIG_ENDIAN
860
    OP (2) = relocation;
861
    OP (1) = relocation >> 8;
862
    OP (0) = relocation >> 16;
863
#else
864
0
    OP (0) = relocation;
865
0
    OP (1) = relocation >> 8;
866
0
    OP (2) = relocation >> 16;
867
0
#endif
868
0
    break;
869
870
0
  case R_RX_RH_24_OP:
871
0
    UNSAFE_FOR_PID;
872
0
    WARN_REDHAT ("RX_RH_24_OP");
873
0
    RANGE (-0x800000, 0x7fffff);
874
#if RX_OPCODE_BIG_ENDIAN
875
    OP (2) = relocation;
876
    OP (1) = relocation >> 8;
877
    OP (0) = relocation >> 16;
878
#else
879
0
    OP (0) = relocation;
880
0
    OP (1) = relocation >> 8;
881
0
    OP (2) = relocation >> 16;
882
0
#endif
883
0
    break;
884
885
0
  case R_RX_DIR24S:
886
0
    UNSAFE_FOR_PID;
887
0
    RANGE (-0x800000, 0x7fffff);
888
0
    if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
889
0
      {
890
0
        OP (2) = relocation;
891
0
        OP (1) = relocation >> 8;
892
0
        OP (0) = relocation >> 16;
893
0
      }
894
0
    else
895
0
      {
896
0
        OP (0) = relocation;
897
0
        OP (1) = relocation >> 8;
898
0
        OP (2) = relocation >> 16;
899
0
      }
900
0
    break;
901
902
0
  case R_RX_RH_24_UNS:
903
0
    UNSAFE_FOR_PID;
904
0
    WARN_REDHAT ("RX_RH_24_UNS");
905
0
    RANGE (0, 0xffffff);
906
#if RX_OPCODE_BIG_ENDIAN
907
    OP (2) = relocation;
908
    OP (1) = relocation >> 8;
909
    OP (0) = relocation >> 16;
910
#else
911
0
    OP (0) = relocation;
912
0
    OP (1) = relocation >> 8;
913
0
    OP (2) = relocation >> 16;
914
0
#endif
915
0
    break;
916
917
0
  case R_RX_RH_32_NEG:
918
0
    UNSAFE_FOR_PID;
919
0
    WARN_REDHAT ("RX_RH_32_NEG");
920
0
    relocation = - relocation;
921
#if RX_OPCODE_BIG_ENDIAN
922
    OP (3) = relocation;
923
    OP (2) = relocation >> 8;
924
    OP (1) = relocation >> 16;
925
    OP (0) = relocation >> 24;
926
#else
927
0
    OP (0) = relocation;
928
0
    OP (1) = relocation >> 8;
929
0
    OP (2) = relocation >> 16;
930
0
    OP (3) = relocation >> 24;
931
0
#endif
932
0
    break;
933
934
0
  case R_RX_RH_32_OP:
935
0
    UNSAFE_FOR_PID;
936
0
    WARN_REDHAT ("RX_RH_32_OP");
937
#if RX_OPCODE_BIG_ENDIAN
938
    OP (3) = relocation;
939
    OP (2) = relocation >> 8;
940
    OP (1) = relocation >> 16;
941
    OP (0) = relocation >> 24;
942
#else
943
0
    OP (0) = relocation;
944
0
    OP (1) = relocation >> 8;
945
0
    OP (2) = relocation >> 16;
946
0
    OP (3) = relocation >> 24;
947
0
#endif
948
0
    break;
949
950
0
  case R_RX_DIR32:
951
0
    if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
952
0
      {
953
0
        OP (3) = relocation;
954
0
        OP (2) = relocation >> 8;
955
0
        OP (1) = relocation >> 16;
956
0
        OP (0) = relocation >> 24;
957
0
      }
958
0
    else
959
0
      {
960
0
        OP (0) = relocation;
961
0
        OP (1) = relocation >> 8;
962
0
        OP (2) = relocation >> 16;
963
0
        OP (3) = relocation >> 24;
964
0
      }
965
0
    break;
966
967
0
  case R_RX_DIR32_REV:
968
0
    if (BIGE (output_bfd))
969
0
      {
970
0
        OP (0) = relocation;
971
0
        OP (1) = relocation >> 8;
972
0
        OP (2) = relocation >> 16;
973
0
        OP (3) = relocation >> 24;
974
0
      }
975
0
    else
976
0
      {
977
0
        OP (3) = relocation;
978
0
        OP (2) = relocation >> 8;
979
0
        OP (1) = relocation >> 16;
980
0
        OP (0) = relocation >> 24;
981
0
      }
982
0
    break;
983
984
0
  case R_RX_RH_DIFF:
985
0
    {
986
0
      bfd_vma val;
987
0
      WARN_REDHAT ("RX_RH_DIFF");
988
0
      val = bfd_get_32 (output_bfd, & OP (0));
989
0
      val -= relocation;
990
0
      bfd_put_32 (output_bfd, val, & OP (0));
991
0
    }
992
0
    break;
993
994
0
  case R_RX_RH_GPRELB:
995
0
    WARN_REDHAT ("RX_RH_GPRELB");
996
0
    relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
997
0
    RANGE (0, 65535);
998
#if RX_OPCODE_BIG_ENDIAN
999
    OP (1) = relocation;
1000
    OP (0) = relocation >> 8;
1001
#else
1002
0
    OP (0) = relocation;
1003
0
    OP (1) = relocation >> 8;
1004
0
#endif
1005
0
    break;
1006
1007
0
  case R_RX_RH_GPRELW:
1008
0
    WARN_REDHAT ("RX_RH_GPRELW");
1009
0
    relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
1010
0
    ALIGN (1);
1011
0
    relocation >>= 1;
1012
0
    RANGE (0, 65535);
1013
#if RX_OPCODE_BIG_ENDIAN
1014
    OP (1) = relocation;
1015
    OP (0) = relocation >> 8;
1016
#else
1017
0
    OP (0) = relocation;
1018
0
    OP (1) = relocation >> 8;
1019
0
#endif
1020
0
    break;
1021
1022
0
  case R_RX_RH_GPRELL:
1023
0
    WARN_REDHAT ("RX_RH_GPRELL");
1024
0
    relocation -= get_gp (info, input_bfd, input_section, rel->r_offset);
1025
0
    ALIGN (3);
1026
0
    relocation >>= 2;
1027
0
    RANGE (0, 65535);
1028
#if RX_OPCODE_BIG_ENDIAN
1029
    OP (1) = relocation;
1030
    OP (0) = relocation >> 8;
1031
#else
1032
0
    OP (0) = relocation;
1033
0
    OP (1) = relocation >> 8;
1034
0
#endif
1035
0
    break;
1036
1037
  /* Internal relocations just for relaxation:  */
1038
0
  case R_RX_RH_ABS5p5B:
1039
0
    RX_STACK_POP (relocation);
1040
0
    RANGE (0, 31);
1041
0
    OP (0) &= 0xf8;
1042
0
    OP (0) |= relocation >> 2;
1043
0
    OP (1) &= 0x77;
1044
0
    OP (1) |= (relocation << 6) & 0x80;
1045
0
    OP (1) |= (relocation << 3) & 0x08;
1046
0
    break;
1047
1048
0
  case R_RX_RH_ABS5p5W:
1049
0
    RX_STACK_POP (relocation);
1050
0
    RANGE (0, 62);
1051
0
    ALIGN (1);
1052
0
    relocation >>= 1;
1053
0
    OP (0) &= 0xf8;
1054
0
    OP (0) |= relocation >> 2;
1055
0
    OP (1) &= 0x77;
1056
0
    OP (1) |= (relocation << 6) & 0x80;
1057
0
    OP (1) |= (relocation << 3) & 0x08;
1058
0
    break;
1059
1060
0
  case R_RX_RH_ABS5p5L:
1061
0
    RX_STACK_POP (relocation);
1062
0
    RANGE (0, 124);
1063
0
    ALIGN (3);
1064
0
    relocation >>= 2;
1065
0
    OP (0) &= 0xf8;
1066
0
    OP (0) |= relocation >> 2;
1067
0
    OP (1) &= 0x77;
1068
0
    OP (1) |= (relocation << 6) & 0x80;
1069
0
    OP (1) |= (relocation << 3) & 0x08;
1070
0
    break;
1071
1072
0
  case R_RX_RH_ABS5p8B:
1073
0
    RX_STACK_POP (relocation);
1074
0
    RANGE (0, 31);
1075
0
    OP (0) &= 0x70;
1076
0
    OP (0) |= (relocation << 3) & 0x80;
1077
0
    OP (0) |= relocation & 0x0f;
1078
0
    break;
1079
1080
0
  case R_RX_RH_ABS5p8W:
1081
0
    RX_STACK_POP (relocation);
1082
0
    RANGE (0, 62);
1083
0
    ALIGN (1);
1084
0
    relocation >>= 1;
1085
0
    OP (0) &= 0x70;
1086
0
    OP (0) |= (relocation << 3) & 0x80;
1087
0
    OP (0) |= relocation & 0x0f;
1088
0
    break;
1089
1090
0
  case R_RX_RH_ABS5p8L:
1091
0
    RX_STACK_POP (relocation);
1092
0
    RANGE (0, 124);
1093
0
    ALIGN (3);
1094
0
    relocation >>= 2;
1095
0
    OP (0) &= 0x70;
1096
0
    OP (0) |= (relocation << 3) & 0x80;
1097
0
    OP (0) |= relocation & 0x0f;
1098
0
    break;
1099
1100
0
  case R_RX_RH_UIMM4p8:
1101
0
    RANGE (0, 15);
1102
0
    OP (0) &= 0x0f;
1103
0
    OP (0) |= relocation << 4;
1104
0
    break;
1105
1106
0
  case R_RX_RH_UNEG4p8:
1107
0
    RANGE (-15, 0);
1108
0
    OP (0) &= 0x0f;
1109
0
    OP (0) |= (-relocation) << 4;
1110
0
    break;
1111
1112
    /* Complex reloc handling:  */
1113
1114
0
  case R_RX_ABS32:
1115
0
    UNSAFE_FOR_PID;
1116
0
    RX_STACK_POP (relocation);
1117
#if RX_OPCODE_BIG_ENDIAN
1118
    OP (3) = relocation;
1119
    OP (2) = relocation >> 8;
1120
    OP (1) = relocation >> 16;
1121
    OP (0) = relocation >> 24;
1122
#else
1123
0
    OP (0) = relocation;
1124
0
    OP (1) = relocation >> 8;
1125
0
    OP (2) = relocation >> 16;
1126
0
    OP (3) = relocation >> 24;
1127
0
#endif
1128
0
    break;
1129
1130
0
  case R_RX_ABS32_REV:
1131
0
    UNSAFE_FOR_PID;
1132
0
    RX_STACK_POP (relocation);
1133
#if RX_OPCODE_BIG_ENDIAN
1134
    OP (0) = relocation;
1135
    OP (1) = relocation >> 8;
1136
    OP (2) = relocation >> 16;
1137
    OP (3) = relocation >> 24;
1138
#else
1139
0
    OP (3) = relocation;
1140
0
    OP (2) = relocation >> 8;
1141
0
    OP (1) = relocation >> 16;
1142
0
    OP (0) = relocation >> 24;
1143
0
#endif
1144
0
    break;
1145
1146
0
  case R_RX_ABS24S_PCREL:
1147
0
  case R_RX_ABS24S:
1148
0
    UNSAFE_FOR_PID;
1149
0
    RX_STACK_POP (relocation);
1150
0
    RANGE (-0x800000, 0x7fffff);
1151
0
    if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1152
0
      {
1153
0
        OP (2) = relocation;
1154
0
        OP (1) = relocation >> 8;
1155
0
        OP (0) = relocation >> 16;
1156
0
      }
1157
0
    else
1158
0
      {
1159
0
        OP (0) = relocation;
1160
0
        OP (1) = relocation >> 8;
1161
0
        OP (2) = relocation >> 16;
1162
0
      }
1163
0
    break;
1164
1165
0
  case R_RX_ABS16:
1166
0
    UNSAFE_FOR_PID;
1167
0
    RX_STACK_POP (relocation);
1168
0
    RANGE (-32768, 65535);
1169
#if RX_OPCODE_BIG_ENDIAN
1170
    OP (1) = relocation;
1171
    OP (0) = relocation >> 8;
1172
#else
1173
0
    OP (0) = relocation;
1174
0
    OP (1) = relocation >> 8;
1175
0
#endif
1176
0
    break;
1177
1178
0
  case R_RX_ABS16_REV:
1179
0
    UNSAFE_FOR_PID;
1180
0
    RX_STACK_POP (relocation);
1181
0
    RANGE (-32768, 65535);
1182
#if RX_OPCODE_BIG_ENDIAN
1183
    OP (0) = relocation;
1184
    OP (1) = relocation >> 8;
1185
#else
1186
0
    OP (1) = relocation;
1187
0
    OP (0) = relocation >> 8;
1188
0
#endif
1189
0
    break;
1190
1191
0
  case R_RX_ABS16S_PCREL:
1192
0
  case R_RX_ABS16S:
1193
0
    RX_STACK_POP (relocation);
1194
0
    RANGE (-32768, 32767);
1195
0
    if (BIGE (output_bfd) && !(input_section->flags & SEC_CODE))
1196
0
      {
1197
0
        OP (1) = relocation;
1198
0
        OP (0) = relocation >> 8;
1199
0
      }
1200
0
    else
1201
0
      {
1202
0
        OP (0) = relocation;
1203
0
        OP (1) = relocation >> 8;
1204
0
      }
1205
0
    break;
1206
1207
0
  case R_RX_ABS16U:
1208
0
    UNSAFE_FOR_PID;
1209
0
    RX_STACK_POP (relocation);
1210
0
    RANGE (0, 65536);
1211
#if RX_OPCODE_BIG_ENDIAN
1212
    OP (1) = relocation;
1213
    OP (0) = relocation >> 8;
1214
#else
1215
0
    OP (0) = relocation;
1216
0
    OP (1) = relocation >> 8;
1217
0
#endif
1218
0
    break;
1219
1220
0
  case R_RX_ABS16UL:
1221
0
    UNSAFE_FOR_PID;
1222
0
    RX_STACK_POP (relocation);
1223
0
    relocation >>= 2;
1224
0
    RANGE (0, 65536);
1225
#if RX_OPCODE_BIG_ENDIAN
1226
    OP (1) = relocation;
1227
    OP (0) = relocation >> 8;
1228
#else
1229
0
    OP (0) = relocation;
1230
0
    OP (1) = relocation >> 8;
1231
0
#endif
1232
0
    break;
1233
1234
0
  case R_RX_ABS16UW:
1235
0
    UNSAFE_FOR_PID;
1236
0
    RX_STACK_POP (relocation);
1237
0
    relocation >>= 1;
1238
0
    RANGE (0, 65536);
1239
#if RX_OPCODE_BIG_ENDIAN
1240
    OP (1) = relocation;
1241
    OP (0) = relocation >> 8;
1242
#else
1243
0
    OP (0) = relocation;
1244
0
    OP (1) = relocation >> 8;
1245
0
#endif
1246
0
    break;
1247
1248
0
  case R_RX_ABS8:
1249
0
    UNSAFE_FOR_PID;
1250
0
    RX_STACK_POP (relocation);
1251
0
    RANGE (-128, 255);
1252
0
    OP (0) = relocation;
1253
0
    break;
1254
1255
0
  case R_RX_ABS8U:
1256
0
    UNSAFE_FOR_PID;
1257
0
    RX_STACK_POP (relocation);
1258
0
    RANGE (0, 255);
1259
0
    OP (0) = relocation;
1260
0
    break;
1261
1262
0
  case R_RX_ABS8UL:
1263
0
    UNSAFE_FOR_PID;
1264
0
    RX_STACK_POP (relocation);
1265
0
    relocation >>= 2;
1266
0
    RANGE (0, 255);
1267
0
    OP (0) = relocation;
1268
0
    break;
1269
1270
0
  case R_RX_ABS8UW:
1271
0
    UNSAFE_FOR_PID;
1272
0
    RX_STACK_POP (relocation);
1273
0
    relocation >>= 1;
1274
0
    RANGE (0, 255);
1275
0
    OP (0) = relocation;
1276
0
    break;
1277
1278
0
  case R_RX_ABS8S:
1279
0
    UNSAFE_FOR_PID;
1280
    /* Fall through.  */
1281
0
  case R_RX_ABS8S_PCREL:
1282
0
    RX_STACK_POP (relocation);
1283
0
    RANGE (-128, 127);
1284
0
    OP (0) = relocation;
1285
0
    break;
1286
1287
0
  case R_RX_SYM:
1288
0
    if (r_symndx < symtab_hdr->sh_info)
1289
0
      RX_STACK_PUSH (sec->output_section->vma
1290
0
         + sec->output_offset
1291
0
         + sym->st_value
1292
0
         + rel->r_addend);
1293
0
    else
1294
0
      {
1295
0
        if (h != NULL
1296
0
      && (h->root.type == bfd_link_hash_defined
1297
0
          || h->root.type == bfd_link_hash_defweak))
1298
0
    RX_STACK_PUSH (h->root.u.def.value
1299
0
             + sec->output_section->vma
1300
0
             + sec->output_offset
1301
0
             + rel->r_addend);
1302
0
        else
1303
0
    _bfd_error_handler
1304
0
      (_("warning: RX_SYM reloc with an unknown symbol"));
1305
0
      }
1306
0
    break;
1307
1308
0
  case R_RX_OPneg:
1309
0
    {
1310
0
      int32_t tmp;
1311
1312
0
      saw_subtract = true;
1313
0
      RX_STACK_POP (tmp);
1314
0
      tmp = - tmp;
1315
0
      RX_STACK_PUSH (tmp);
1316
0
    }
1317
0
    break;
1318
1319
0
  case R_RX_OPadd:
1320
0
    {
1321
0
      int32_t tmp1, tmp2;
1322
1323
0
      RX_STACK_POP (tmp1);
1324
0
      RX_STACK_POP (tmp2);
1325
0
      tmp1 += tmp2;
1326
0
      RX_STACK_PUSH (tmp1);
1327
0
    }
1328
0
    break;
1329
1330
0
  case R_RX_OPsub:
1331
0
    {
1332
0
      int32_t tmp1, tmp2;
1333
1334
0
      saw_subtract = true;
1335
0
      RX_STACK_POP (tmp1);
1336
0
      RX_STACK_POP (tmp2);
1337
0
      tmp2 -= tmp1;
1338
0
      RX_STACK_PUSH (tmp2);
1339
0
    }
1340
0
    break;
1341
1342
0
  case R_RX_OPmul:
1343
0
    {
1344
0
      int32_t tmp1, tmp2;
1345
1346
0
      RX_STACK_POP (tmp1);
1347
0
      RX_STACK_POP (tmp2);
1348
0
      tmp1 *= tmp2;
1349
0
      RX_STACK_PUSH (tmp1);
1350
0
    }
1351
0
    break;
1352
1353
0
  case R_RX_OPdiv:
1354
0
    {
1355
0
      int32_t tmp1, tmp2;
1356
1357
0
      RX_STACK_POP (tmp1);
1358
0
      RX_STACK_POP (tmp2);
1359
0
      tmp1 /= tmp2;
1360
0
      RX_STACK_PUSH (tmp1);
1361
0
    }
1362
0
    break;
1363
1364
0
  case R_RX_OPshla:
1365
0
    {
1366
0
      int32_t tmp1, tmp2;
1367
1368
0
      RX_STACK_POP (tmp1);
1369
0
      RX_STACK_POP (tmp2);
1370
0
      tmp1 <<= tmp2;
1371
0
      RX_STACK_PUSH (tmp1);
1372
0
    }
1373
0
    break;
1374
1375
0
  case R_RX_OPshra:
1376
0
    {
1377
0
      int32_t tmp1, tmp2;
1378
1379
0
      RX_STACK_POP (tmp1);
1380
0
      RX_STACK_POP (tmp2);
1381
0
      tmp1 >>= tmp2;
1382
0
      RX_STACK_PUSH (tmp1);
1383
0
    }
1384
0
    break;
1385
1386
0
  case R_RX_OPsctsize:
1387
0
    RX_STACK_PUSH (input_section->size);
1388
0
    break;
1389
1390
0
  case R_RX_OPscttop:
1391
0
    RX_STACK_PUSH (input_section->output_section->vma);
1392
0
    break;
1393
1394
0
  case R_RX_OPand:
1395
0
    {
1396
0
      int32_t tmp1, tmp2;
1397
1398
0
      RX_STACK_POP (tmp1);
1399
0
      RX_STACK_POP (tmp2);
1400
0
      tmp1 &= tmp2;
1401
0
      RX_STACK_PUSH (tmp1);
1402
0
    }
1403
0
    break;
1404
1405
0
  case R_RX_OPor:
1406
0
    {
1407
0
      int32_t tmp1, tmp2;
1408
1409
0
      RX_STACK_POP (tmp1);
1410
0
      RX_STACK_POP (tmp2);
1411
0
      tmp1 |= tmp2;
1412
0
      RX_STACK_PUSH (tmp1);
1413
0
    }
1414
0
    break;
1415
1416
0
  case R_RX_OPxor:
1417
0
    {
1418
0
      int32_t tmp1, tmp2;
1419
1420
0
      RX_STACK_POP (tmp1);
1421
0
      RX_STACK_POP (tmp2);
1422
0
      tmp1 ^= tmp2;
1423
0
      RX_STACK_PUSH (tmp1);
1424
0
    }
1425
0
    break;
1426
1427
0
  case R_RX_OPnot:
1428
0
    {
1429
0
      int32_t tmp;
1430
1431
0
      RX_STACK_POP (tmp);
1432
0
      tmp = ~ tmp;
1433
0
      RX_STACK_PUSH (tmp);
1434
0
    }
1435
0
    break;
1436
1437
0
  case R_RX_OPmod:
1438
0
    {
1439
0
      int32_t tmp1, tmp2;
1440
1441
0
      RX_STACK_POP (tmp1);
1442
0
      RX_STACK_POP (tmp2);
1443
0
      tmp1 %= tmp2;
1444
0
      RX_STACK_PUSH (tmp1);
1445
0
    }
1446
0
    break;
1447
1448
0
  case R_RX_OPromtop:
1449
0
    RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
1450
0
    break;
1451
1452
0
  case R_RX_OPramtop:
1453
0
    RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
1454
0
    break;
1455
1456
0
  default:
1457
0
    r = bfd_reloc_notsupported;
1458
0
    break;
1459
0
  }
1460
1461
0
      if (r != bfd_reloc_ok)
1462
0
  {
1463
0
    const char * msg = NULL;
1464
1465
0
    switch (r)
1466
0
      {
1467
0
      case bfd_reloc_overflow:
1468
        /* Catch the case of a missing function declaration
1469
     and emit a more helpful error message.  */
1470
0
        if (r_type == R_RX_DIR24S_PCREL)
1471
    /* xgettext:c-format */
1472
0
    msg = _("%pB(%pA): error: call to undefined function '%s'");
1473
0
        else
1474
0
    (*info->callbacks->reloc_overflow)
1475
0
      (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
1476
0
       input_bfd, input_section, rel->r_offset);
1477
0
        break;
1478
1479
0
      case bfd_reloc_undefined:
1480
0
        (*info->callbacks->undefined_symbol)
1481
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
1482
0
        break;
1483
1484
0
      case bfd_reloc_other:
1485
        /* xgettext:c-format */
1486
0
        msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1487
0
        break;
1488
1489
0
      case bfd_reloc_outofrange:
1490
        /* xgettext:c-format */
1491
0
        msg = _("%pB(%pA): internal error: out of range error");
1492
0
        break;
1493
1494
0
      case bfd_reloc_notsupported:
1495
        /* xgettext:c-format */
1496
0
        msg = _("%pB(%pA): internal error: unsupported relocation error");
1497
0
        break;
1498
1499
0
      case bfd_reloc_dangerous:
1500
        /* xgettext:c-format */
1501
0
        msg = _("%pB(%pA): internal error: dangerous relocation");
1502
0
        break;
1503
1504
0
      default:
1505
        /* xgettext:c-format */
1506
0
        msg = _("%pB(%pA): internal error: unknown error");
1507
0
        break;
1508
0
      }
1509
1510
0
    if (msg)
1511
0
      _bfd_error_handler (msg, input_bfd, input_section, name);
1512
0
  }
1513
0
    }
1514
1515
0
  return true;
1516
0
}
1517

1518
/* Relaxation Support.  */
1519
1520
/* Progression of relocations from largest operand size to smallest
1521
   operand size.  */
1522
1523
static int
1524
next_smaller_reloc (int r)
1525
0
{
1526
0
  switch (r)
1527
0
    {
1528
0
    case R_RX_DIR32:    return R_RX_DIR24S;
1529
0
    case R_RX_DIR24S:   return R_RX_DIR16S;
1530
0
    case R_RX_DIR16S:   return R_RX_DIR8S;
1531
0
    case R_RX_DIR8S:    return R_RX_NONE;
1532
1533
0
    case R_RX_DIR16:    return R_RX_DIR8;
1534
0
    case R_RX_DIR8:   return R_RX_NONE;
1535
1536
0
    case R_RX_DIR16U:   return R_RX_DIR8U;
1537
0
    case R_RX_DIR8U:    return R_RX_NONE;
1538
1539
0
    case R_RX_DIR24S_PCREL: return R_RX_DIR16S_PCREL;
1540
0
    case R_RX_DIR16S_PCREL: return R_RX_DIR8S_PCREL;
1541
0
    case R_RX_DIR8S_PCREL:  return R_RX_DIR3U_PCREL;
1542
1543
0
    case R_RX_DIR16UL:    return R_RX_DIR8UL;
1544
0
    case R_RX_DIR8UL:   return R_RX_NONE;
1545
0
    case R_RX_DIR16UW:    return R_RX_DIR8UW;
1546
0
    case R_RX_DIR8UW:   return R_RX_NONE;
1547
1548
0
    case R_RX_RH_32_OP:   return R_RX_RH_24_OP;
1549
0
    case R_RX_RH_24_OP:   return R_RX_RH_16_OP;
1550
0
    case R_RX_RH_16_OP:   return R_RX_DIR8;
1551
1552
0
    case R_RX_ABS32:    return R_RX_ABS24S;
1553
0
    case R_RX_ABS24S:   return R_RX_ABS16S;
1554
0
    case R_RX_ABS16:    return R_RX_ABS8;
1555
0
    case R_RX_ABS16U:   return R_RX_ABS8U;
1556
0
    case R_RX_ABS16S:   return R_RX_ABS8S;
1557
0
    case R_RX_ABS8:   return R_RX_NONE;
1558
0
    case R_RX_ABS8U:    return R_RX_NONE;
1559
0
    case R_RX_ABS8S:    return R_RX_NONE;
1560
0
    case R_RX_ABS24S_PCREL: return R_RX_ABS16S_PCREL;
1561
0
    case R_RX_ABS16S_PCREL: return R_RX_ABS8S_PCREL;
1562
0
    case R_RX_ABS8S_PCREL:  return R_RX_NONE;
1563
0
    case R_RX_ABS16UL:    return R_RX_ABS8UL;
1564
0
    case R_RX_ABS16UW:    return R_RX_ABS8UW;
1565
0
    case R_RX_ABS8UL:   return R_RX_NONE;
1566
0
    case R_RX_ABS8UW:   return R_RX_NONE;
1567
0
    }
1568
0
  return r;
1569
0
};
1570
1571
/* Delete some bytes from a section while relaxing.  */
1572
1573
static bool
1574
elf32_rx_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1575
           Elf_Internal_Rela *alignment_rel, int force_snip,
1576
           Elf_Internal_Rela *irelstart)
1577
0
{
1578
0
  Elf_Internal_Shdr * symtab_hdr;
1579
0
  unsigned int        sec_shndx;
1580
0
  bfd_byte *        contents;
1581
0
  Elf_Internal_Rela * irel;
1582
0
  Elf_Internal_Rela * irelend;
1583
0
  Elf_Internal_Sym *  isym;
1584
0
  Elf_Internal_Sym *  isymend;
1585
0
  bfd_vma       toaddr;
1586
0
  unsigned int        symcount;
1587
0
  struct elf_link_hash_entry ** sym_hashes;
1588
0
  struct elf_link_hash_entry ** end_hashes;
1589
1590
0
  if (!alignment_rel)
1591
0
    force_snip = 1;
1592
1593
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1594
1595
0
  contents = elf_section_data (sec)->this_hdr.contents;
1596
1597
  /* The deletion must stop at the next alignment boundary, if
1598
     ALIGNMENT_REL is non-NULL.  */
1599
0
  toaddr = sec->size;
1600
0
  if (alignment_rel)
1601
0
    toaddr = alignment_rel->r_offset;
1602
1603
0
  BFD_ASSERT (toaddr > addr);
1604
1605
  /* Actually delete the bytes.  */
1606
0
  memmove (contents + addr, contents + addr + count,
1607
0
     (size_t) (toaddr - addr - count));
1608
1609
  /* If we don't have an alignment marker to worry about, we can just
1610
     shrink the section.  Otherwise, we have to fill in the newly
1611
     created gap with NOP insns (0x03).  */
1612
0
  if (force_snip)
1613
0
    sec->size -= count;
1614
0
  else
1615
0
    memset (contents + toaddr - count, 0x03, count);
1616
1617
0
  irel = irelstart;
1618
0
  BFD_ASSERT (irel != NULL || sec->reloc_count == 0);
1619
0
  irelend = irel + sec->reloc_count;
1620
1621
  /* Adjust all the relocs.  */
1622
0
  for (; irel < irelend; irel++)
1623
0
    {
1624
      /* Get the new reloc address.  */
1625
0
      if (irel->r_offset > addr
1626
0
    && (irel->r_offset < toaddr
1627
0
        || (force_snip && irel->r_offset == toaddr)))
1628
0
  irel->r_offset -= count;
1629
1630
      /* If we see an ALIGN marker at the end of the gap, we move it
1631
   to the beginning of the gap, since marking these gaps is what
1632
   they're for.  */
1633
0
      if (irel->r_offset == toaddr
1634
0
    && ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
1635
0
    && irel->r_addend & RX_RELAXA_ALIGN)
1636
0
  irel->r_offset -= count;
1637
0
    }
1638
1639
  /* Adjust the local symbols defined in this section.  */
1640
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1641
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1642
0
  isymend = isym + symtab_hdr->sh_info;
1643
1644
0
  for (; isym < isymend; isym++)
1645
0
    {
1646
      /* If the symbol is in the range of memory we just moved, we
1647
   have to adjust its value.  */
1648
0
      if (isym->st_shndx == sec_shndx
1649
0
    && isym->st_value > addr
1650
0
    && isym->st_value < toaddr)
1651
0
  isym->st_value -= count;
1652
1653
      /* If the symbol *spans* the bytes we just deleted (i.e. it's
1654
   *end* is in the moved bytes but it's *start* isn't), then we
1655
   must adjust its size.  */
1656
0
      if (isym->st_shndx == sec_shndx
1657
0
    && isym->st_value < addr
1658
0
    && isym->st_value + isym->st_size > addr
1659
0
    && isym->st_value + isym->st_size < toaddr)
1660
0
  isym->st_size -= count;
1661
0
    }
1662
1663
  /* Now adjust the global symbols defined in this section.  */
1664
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1665
0
        - symtab_hdr->sh_info);
1666
0
  sym_hashes = elf_sym_hashes (abfd);
1667
0
  end_hashes = sym_hashes + symcount;
1668
1669
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1670
0
    {
1671
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1672
1673
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1674
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1675
0
    && sym_hash->root.u.def.section == sec)
1676
0
  {
1677
    /* As above, adjust the value if needed.  */
1678
0
    if (sym_hash->root.u.def.value > addr
1679
0
        && sym_hash->root.u.def.value < toaddr)
1680
0
      sym_hash->root.u.def.value -= count;
1681
1682
    /* As above, adjust the size if needed.  */
1683
0
    if (sym_hash->root.u.def.value < addr
1684
0
        && sym_hash->root.u.def.value + sym_hash->size > addr
1685
0
        && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1686
0
      sym_hash->size -= count;
1687
0
  }
1688
0
    }
1689
1690
0
  return true;
1691
0
}
1692
1693
/* Used to sort relocs by address.  If relocs have the same address,
1694
   we maintain their relative order, except that R_RX_RH_RELAX
1695
   alignment relocs must be the first reloc for any given address.  */
1696
1697
static void
1698
reloc_bubblesort (Elf_Internal_Rela * r, int count)
1699
0
{
1700
0
  int i;
1701
0
  bool again;
1702
0
  bool swappit;
1703
1704
  /* This is almost a classic bubblesort.  It's the slowest sort, but
1705
     we're taking advantage of the fact that the relocations are
1706
     mostly in order already (the assembler emits them that way) and
1707
     we need relocs with the same address to remain in the same
1708
     relative order.  */
1709
0
  again = true;
1710
0
  while (again)
1711
0
    {
1712
0
      again = false;
1713
0
      for (i = 0; i < count - 1; i ++)
1714
0
  {
1715
0
    if (r[i].r_offset > r[i + 1].r_offset)
1716
0
      swappit = true;
1717
0
    else if (r[i].r_offset < r[i + 1].r_offset)
1718
0
      swappit = false;
1719
0
    else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1720
0
       && (r[i + 1].r_addend & RX_RELAXA_ALIGN))
1721
0
      swappit = true;
1722
0
    else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RX_RH_RELAX
1723
0
       && (r[i + 1].r_addend & RX_RELAXA_ELIGN)
1724
0
       && !(ELF32_R_TYPE (r[i].r_info) == R_RX_RH_RELAX
1725
0
      && (r[i].r_addend & RX_RELAXA_ALIGN)))
1726
0
      swappit = true;
1727
0
    else
1728
0
      swappit = false;
1729
1730
0
    if (swappit)
1731
0
      {
1732
0
        Elf_Internal_Rela tmp;
1733
1734
0
        tmp = r[i];
1735
0
        r[i] = r[i + 1];
1736
0
        r[i + 1] = tmp;
1737
        /* If we do move a reloc back, re-scan to see if it
1738
     needs to be moved even further back.  This avoids
1739
     most of the O(n^2) behavior for our cases.  */
1740
0
        if (i > 0)
1741
0
    i -= 2;
1742
0
        again = true;
1743
0
      }
1744
0
  }
1745
0
    }
1746
0
}
1747
1748
1749
#define OFFSET_FOR_RELOC(rel, lrel, scale) \
1750
0
  rx_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
1751
0
           lrel, abfd, sec, link_info, scale)
1752
1753
static bfd_vma
1754
rx_offset_for_reloc (bfd *          abfd,
1755
         Elf_Internal_Rela *      rel,
1756
         Elf_Internal_Shdr *      symtab_hdr,
1757
         bfd_byte *         shndx_buf ATTRIBUTE_UNUSED,
1758
         Elf_Internal_Sym *       intsyms,
1759
         Elf_Internal_Rela **     lrel,
1760
         bfd *          input_bfd,
1761
         asection *         input_section,
1762
         struct bfd_link_info *   info,
1763
         int *          scale)
1764
0
{
1765
0
  bfd_vma symval;
1766
0
  bfd_reloc_status_type r;
1767
1768
0
  *scale = 1;
1769
1770
  /* REL is the first of 1..N relocations.  We compute the symbol
1771
     value for each relocation, then combine them if needed.  LREL
1772
     gets a pointer to the last relocation used.  */
1773
0
  while (1)
1774
0
    {
1775
0
      int32_t tmp1, tmp2;
1776
1777
      /* Get the value of the symbol referred to by the reloc.  */
1778
0
      if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1779
0
  {
1780
    /* A local symbol.  */
1781
0
    Elf_Internal_Sym *isym;
1782
0
    asection *ssec;
1783
1784
0
    isym = intsyms + ELF32_R_SYM (rel->r_info);
1785
1786
0
    if (isym->st_shndx == SHN_UNDEF)
1787
0
      ssec = bfd_und_section_ptr;
1788
0
    else if (isym->st_shndx == SHN_ABS)
1789
0
      ssec = bfd_abs_section_ptr;
1790
0
    else if (isym->st_shndx == SHN_COMMON)
1791
0
      ssec = bfd_com_section_ptr;
1792
0
    else
1793
0
      ssec = bfd_section_from_elf_index (abfd,
1794
0
                 isym->st_shndx);
1795
1796
    /* Initial symbol value.  */
1797
0
    symval = isym->st_value;
1798
1799
    /* GAS may have made this symbol relative to a section, in
1800
       which case, we have to add the addend to find the
1801
       symbol.  */
1802
0
    if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
1803
0
      symval += rel->r_addend;
1804
1805
0
    if (ssec)
1806
0
      {
1807
0
        if ((ssec->flags & SEC_MERGE)
1808
0
      && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
1809
0
    symval = _bfd_merged_section_offset (abfd, & ssec,
1810
0
                 elf_section_data (ssec)->sec_info,
1811
0
                 symval);
1812
0
      }
1813
1814
    /* Now make the offset relative to where the linker is putting it.  */
1815
0
    if (ssec)
1816
0
      symval +=
1817
0
        ssec->output_section->vma + ssec->output_offset;
1818
1819
0
    symval += rel->r_addend;
1820
0
  }
1821
0
      else
1822
0
  {
1823
0
    unsigned long indx;
1824
0
    struct elf_link_hash_entry * h;
1825
1826
    /* An external symbol.  */
1827
0
    indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1828
0
    h = elf_sym_hashes (abfd)[indx];
1829
0
    BFD_ASSERT (h != NULL);
1830
1831
0
    if (h->root.type != bfd_link_hash_defined
1832
0
        && h->root.type != bfd_link_hash_defweak)
1833
0
      {
1834
        /* This appears to be a reference to an undefined
1835
     symbol.  Just ignore it--it will be caught by the
1836
     regular reloc processing.  */
1837
0
        if (lrel)
1838
0
    *lrel = rel;
1839
0
        return 0;
1840
0
      }
1841
1842
0
    symval = (h->root.u.def.value
1843
0
        + h->root.u.def.section->output_section->vma
1844
0
        + h->root.u.def.section->output_offset);
1845
1846
0
    symval += rel->r_addend;
1847
0
  }
1848
1849
0
      switch (ELF32_R_TYPE (rel->r_info))
1850
0
  {
1851
0
  case R_RX_SYM:
1852
0
    RX_STACK_PUSH (symval);
1853
0
    break;
1854
1855
0
  case R_RX_OPneg:
1856
0
    RX_STACK_POP (tmp1);
1857
0
    tmp1 = - tmp1;
1858
0
    RX_STACK_PUSH (tmp1);
1859
0
    break;
1860
1861
0
  case R_RX_OPadd:
1862
0
    RX_STACK_POP (tmp1);
1863
0
    RX_STACK_POP (tmp2);
1864
0
    tmp1 += tmp2;
1865
0
    RX_STACK_PUSH (tmp1);
1866
0
    break;
1867
1868
0
  case R_RX_OPsub:
1869
0
    RX_STACK_POP (tmp1);
1870
0
    RX_STACK_POP (tmp2);
1871
0
    tmp2 -= tmp1;
1872
0
    RX_STACK_PUSH (tmp2);
1873
0
    break;
1874
1875
0
  case R_RX_OPmul:
1876
0
    RX_STACK_POP (tmp1);
1877
0
    RX_STACK_POP (tmp2);
1878
0
    tmp1 *= tmp2;
1879
0
    RX_STACK_PUSH (tmp1);
1880
0
    break;
1881
1882
0
  case R_RX_OPdiv:
1883
0
    RX_STACK_POP (tmp1);
1884
0
    RX_STACK_POP (tmp2);
1885
0
    tmp1 /= tmp2;
1886
0
    RX_STACK_PUSH (tmp1);
1887
0
    break;
1888
1889
0
  case R_RX_OPshla:
1890
0
    RX_STACK_POP (tmp1);
1891
0
    RX_STACK_POP (tmp2);
1892
0
    tmp1 <<= tmp2;
1893
0
    RX_STACK_PUSH (tmp1);
1894
0
    break;
1895
1896
0
  case R_RX_OPshra:
1897
0
    RX_STACK_POP (tmp1);
1898
0
    RX_STACK_POP (tmp2);
1899
0
    tmp1 >>= tmp2;
1900
0
    RX_STACK_PUSH (tmp1);
1901
0
    break;
1902
1903
0
  case R_RX_OPsctsize:
1904
0
    RX_STACK_PUSH (input_section->size);
1905
0
    break;
1906
1907
0
  case R_RX_OPscttop:
1908
0
    RX_STACK_PUSH (input_section->output_section->vma);
1909
0
    break;
1910
1911
0
  case R_RX_OPand:
1912
0
    RX_STACK_POP (tmp1);
1913
0
    RX_STACK_POP (tmp2);
1914
0
    tmp1 &= tmp2;
1915
0
    RX_STACK_PUSH (tmp1);
1916
0
    break;
1917
1918
0
  case R_RX_OPor:
1919
0
    RX_STACK_POP (tmp1);
1920
0
    RX_STACK_POP (tmp2);
1921
0
    tmp1 |= tmp2;
1922
0
    RX_STACK_PUSH (tmp1);
1923
0
    break;
1924
1925
0
  case R_RX_OPxor:
1926
0
    RX_STACK_POP (tmp1);
1927
0
    RX_STACK_POP (tmp2);
1928
0
    tmp1 ^= tmp2;
1929
0
    RX_STACK_PUSH (tmp1);
1930
0
    break;
1931
1932
0
  case R_RX_OPnot:
1933
0
    RX_STACK_POP (tmp1);
1934
0
    tmp1 = ~ tmp1;
1935
0
    RX_STACK_PUSH (tmp1);
1936
0
    break;
1937
1938
0
  case R_RX_OPmod:
1939
0
    RX_STACK_POP (tmp1);
1940
0
    RX_STACK_POP (tmp2);
1941
0
    tmp1 %= tmp2;
1942
0
    RX_STACK_PUSH (tmp1);
1943
0
    break;
1944
1945
0
  case R_RX_OPromtop:
1946
0
    RX_STACK_PUSH (get_romstart (info, input_bfd, input_section, rel->r_offset));
1947
0
    break;
1948
1949
0
  case R_RX_OPramtop:
1950
0
    RX_STACK_PUSH (get_ramstart (info, input_bfd, input_section, rel->r_offset));
1951
0
    break;
1952
1953
0
  case R_RX_DIR16UL:
1954
0
  case R_RX_DIR8UL:
1955
0
  case R_RX_ABS16UL:
1956
0
  case R_RX_ABS8UL:
1957
0
    if (rx_stack_top)
1958
0
      RX_STACK_POP (symval);
1959
0
    if (lrel)
1960
0
      *lrel = rel;
1961
0
    *scale = 4;
1962
0
    return symval;
1963
1964
0
  case R_RX_DIR16UW:
1965
0
  case R_RX_DIR8UW:
1966
0
  case R_RX_ABS16UW:
1967
0
  case R_RX_ABS8UW:
1968
0
    if (rx_stack_top)
1969
0
      RX_STACK_POP (symval);
1970
0
    if (lrel)
1971
0
      *lrel = rel;
1972
0
    *scale = 2;
1973
0
    return symval;
1974
1975
0
  default:
1976
0
    if (rx_stack_top)
1977
0
      RX_STACK_POP (symval);
1978
0
    if (lrel)
1979
0
      *lrel = rel;
1980
0
    return symval;
1981
0
  }
1982
1983
0
      rel ++;
1984
0
    }
1985
  /* FIXME.  */
1986
0
  (void) r;
1987
0
}
1988
1989
static void
1990
move_reloc (Elf_Internal_Rela * irel, Elf_Internal_Rela * srel, int delta)
1991
0
{
1992
0
  bfd_vma old_offset = srel->r_offset;
1993
1994
0
  irel ++;
1995
0
  while (irel <= srel)
1996
0
    {
1997
0
      if (irel->r_offset == old_offset)
1998
0
  irel->r_offset += delta;
1999
0
      irel ++;
2000
0
    }
2001
0
}
2002
2003
/* Relax one section.  */
2004
2005
static bool
2006
elf32_rx_relax_section (bfd *abfd,
2007
      asection *sec,
2008
      struct bfd_link_info *link_info,
2009
      bool *again,
2010
      bool allow_pcrel3)
2011
0
{
2012
0
  Elf_Internal_Shdr *symtab_hdr;
2013
0
  Elf_Internal_Shdr *shndx_hdr;
2014
0
  Elf_Internal_Rela *internal_relocs;
2015
0
  Elf_Internal_Rela *irel;
2016
0
  Elf_Internal_Rela *srel;
2017
0
  Elf_Internal_Rela *irelend;
2018
0
  Elf_Internal_Rela *next_alignment;
2019
0
  Elf_Internal_Rela *prev_alignment;
2020
0
  bfd_byte *contents = NULL;
2021
0
  bfd_byte *free_contents = NULL;
2022
0
  Elf_Internal_Sym *intsyms = NULL;
2023
0
  Elf_Internal_Sym *free_intsyms = NULL;
2024
0
  bfd_byte *shndx_buf = NULL;
2025
0
  bfd_vma pc;
2026
0
  bfd_vma sec_start;
2027
0
  bfd_vma symval = 0;
2028
0
  int pcrel = 0;
2029
0
  int code = 0;
2030
0
  int section_alignment_glue;
2031
  /* how much to scale the relocation by - 1, 2, or 4.  */
2032
0
  int scale;
2033
2034
  /* Assume nothing changes.  */
2035
0
  *again = false;
2036
2037
  /* We don't have to do anything for a relocatable link, if
2038
     this section does not have relocs, or if this is not a
2039
     code section.  */
2040
0
  if (bfd_link_relocatable (link_info)
2041
0
      || sec->reloc_count == 0
2042
0
      || (sec->flags & SEC_RELOC) == 0
2043
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
2044
0
      || (sec->flags & SEC_CODE) == 0)
2045
0
    return true;
2046
2047
0
  symtab_hdr = & elf_symtab_hdr (abfd);
2048
0
  if (elf_symtab_shndx_list (abfd))
2049
0
    shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2050
0
  else
2051
0
    shndx_hdr = NULL;
2052
2053
0
  sec_start = sec->output_section->vma + sec->output_offset;
2054
2055
  /* Get the section contents.  */
2056
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
2057
0
    contents = elf_section_data (sec)->this_hdr.contents;
2058
  /* Go get them off disk.  */
2059
0
  else
2060
0
    {
2061
0
      if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2062
0
  goto error_return;
2063
0
      elf_section_data (sec)->this_hdr.contents = contents;
2064
0
    }
2065
2066
  /* Read this BFD's symbols.  */
2067
  /* Get cached copy if it exists.  */
2068
0
  if (symtab_hdr->contents != NULL)
2069
0
    intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2070
0
  else
2071
0
    {
2072
0
      intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
2073
0
      symtab_hdr->contents = (bfd_byte *) intsyms;
2074
0
    }
2075
2076
0
  if (shndx_hdr && shndx_hdr->sh_size != 0)
2077
0
    {
2078
0
      size_t amt;
2079
2080
0
      if (_bfd_mul_overflow (symtab_hdr->sh_info,
2081
0
           sizeof (Elf_External_Sym_Shndx), &amt))
2082
0
  {
2083
0
    bfd_set_error (bfd_error_file_too_big);
2084
0
    goto error_return;
2085
0
  }
2086
0
      if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
2087
0
  goto error_return;
2088
0
      shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
2089
0
      if (shndx_buf == NULL)
2090
0
  goto error_return;
2091
0
      shndx_hdr->contents = shndx_buf;
2092
0
    }
2093
2094
  /* Get a copy of the native relocations.  */
2095
  /* Note - we ignore the setting of link_info->keep_memory when reading
2096
     in these relocs.  We have to maintain a permanent copy of the relocs
2097
     because we are going to walk over them multiple times, adjusting them
2098
     as bytes are deleted from the section, and with this relaxation
2099
     function itself being called multiple times on the same section...  */
2100
0
  internal_relocs = _bfd_elf_link_read_relocs
2101
0
    (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, true);
2102
0
  if (internal_relocs == NULL)
2103
0
    goto error_return;
2104
2105
  /* The RL_ relocs must be just before the operand relocs they go
2106
     with, so we must sort them to guarantee this.  We use bubblesort
2107
     instead of qsort so we can guarantee that relocs with the same
2108
     address remain in the same relative order.  */
2109
0
  reloc_bubblesort (internal_relocs, sec->reloc_count);
2110
2111
  /* Walk through them looking for relaxing opportunities.  */
2112
0
  irelend = internal_relocs + sec->reloc_count;
2113
2114
  /* This will either be NULL or a pointer to the next alignment
2115
     relocation.  */
2116
0
  next_alignment = internal_relocs;
2117
  /* This will be the previous alignment, although at first it points
2118
     to the first real relocation.  */
2119
0
  prev_alignment = internal_relocs;
2120
2121
  /* We calculate worst case shrinkage caused by alignment directives.
2122
     No fool-proof, but better than either ignoring the problem or
2123
     doing heavy duty analysis of all the alignment markers in all
2124
     input sections.  */
2125
0
  section_alignment_glue = 0;
2126
0
  for (irel = internal_relocs; irel < irelend; irel++)
2127
0
      if (ELF32_R_TYPE (irel->r_info) == R_RX_RH_RELAX
2128
0
    && irel->r_addend & RX_RELAXA_ALIGN)
2129
0
  {
2130
0
    int this_glue = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2131
2132
0
    if (section_alignment_glue < this_glue)
2133
0
      section_alignment_glue = this_glue;
2134
0
  }
2135
  /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
2136
     shrinkage.  */
2137
0
  section_alignment_glue *= 2;
2138
2139
0
  for (irel = internal_relocs; irel < irelend; irel++)
2140
0
    {
2141
0
      unsigned char *insn;
2142
0
      int nrelocs;
2143
2144
      /* The insns we care about are all marked with one of these.  */
2145
0
      if (ELF32_R_TYPE (irel->r_info) != R_RX_RH_RELAX)
2146
0
  continue;
2147
2148
0
      if (irel->r_addend & RX_RELAXA_ALIGN
2149
0
    || next_alignment == internal_relocs)
2150
0
  {
2151
    /* When we delete bytes, we need to maintain all the alignments
2152
       indicated.  In addition, we need to be careful about relaxing
2153
       jumps across alignment boundaries - these displacements
2154
       *grow* when we delete bytes.  For now, don't shrink
2155
       displacements across an alignment boundary, just in case.
2156
       Note that this only affects relocations to the same
2157
       section.  */
2158
0
    prev_alignment = next_alignment;
2159
0
    next_alignment += 2;
2160
0
    while (next_alignment < irelend
2161
0
     && (ELF32_R_TYPE (next_alignment->r_info) != R_RX_RH_RELAX
2162
0
         || !(next_alignment->r_addend & RX_RELAXA_ELIGN)))
2163
0
      next_alignment ++;
2164
0
    if (next_alignment >= irelend || next_alignment->r_offset == 0)
2165
0
      next_alignment = NULL;
2166
0
  }
2167
2168
      /* When we hit alignment markers, see if we've shrunk enough
2169
   before them to reduce the gap without violating the alignment
2170
   requirements.  */
2171
0
      if (irel->r_addend & RX_RELAXA_ALIGN)
2172
0
  {
2173
    /* At this point, the next relocation *should* be the ELIGN
2174
       end marker.  */
2175
0
    Elf_Internal_Rela *erel = irel + 1;
2176
0
    unsigned int alignment, nbytes;
2177
2178
0
    if (ELF32_R_TYPE (erel->r_info) != R_RX_RH_RELAX)
2179
0
      continue;
2180
0
    if (!(erel->r_addend & RX_RELAXA_ELIGN))
2181
0
      continue;
2182
2183
0
    alignment = 1 << (irel->r_addend & RX_RELAXA_ANUM);
2184
2185
0
    if (erel->r_offset - irel->r_offset < alignment)
2186
0
      continue;
2187
2188
0
    nbytes = erel->r_offset - irel->r_offset;
2189
0
    nbytes /= alignment;
2190
0
    nbytes *= alignment;
2191
2192
0
    elf32_rx_relax_delete_bytes (abfd, sec, erel->r_offset-nbytes, nbytes, next_alignment,
2193
0
               erel->r_offset == sec->size, internal_relocs);
2194
0
    *again = true;
2195
2196
0
    continue;
2197
0
  }
2198
2199
0
      if (irel->r_addend & RX_RELAXA_ELIGN)
2200
0
    continue;
2201
2202
0
      insn = contents + irel->r_offset;
2203
2204
0
      nrelocs = irel->r_addend & RX_RELAXA_RNUM;
2205
2206
      /* At this point, we have an insn that is a candidate for linker
2207
   relaxation.  There are NRELOCS relocs following that may be
2208
   relaxed, although each reloc may be made of more than one
2209
   reloc entry (such as gp-rel symbols).  */
2210
2211
      /* Get the value of the symbol referred to by the reloc.  Just
2212
   in case this is the last reloc in the list, use the RL's
2213
   addend to choose between this reloc (no addend) or the next
2214
   (yes addend, which means at least one following reloc).  */
2215
2216
      /* srel points to the "current" reloction for this insn -
2217
   actually the last reloc for a given operand, which is the one
2218
   we need to update.  We check the relaxations in the same
2219
   order that the relocations happen, so we'll just push it
2220
   along as we go.  */
2221
0
      srel = irel;
2222
2223
0
      pc = sec->output_section->vma + sec->output_offset
2224
0
  + srel->r_offset;
2225
2226
0
#define GET_RELOC \
2227
0
      symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
2228
0
      pcrel = symval - pc + srel->r_addend; \
2229
0
      nrelocs --;
2230
2231
0
#define SNIPNR(offset, nbytes) \
2232
0
      elf32_rx_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0, internal_relocs);
2233
0
#define SNIP(offset, nbytes, newtype) \
2234
0
  SNIPNR (offset, nbytes);            \
2235
0
  srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
2236
2237
      /* The order of these bit tests must match the order that the
2238
   relocs appear in.  Since we sorted those by offset, we can
2239
   predict them.  */
2240
2241
      /* Note that the numbers in, say, DSP6 are the bit offsets of
2242
   the code fields that describe the operand.  Bits number 0 for
2243
   the MSB of insn[0].  */
2244
2245
      /* DSP* codes:
2246
     0  00  [reg]
2247
     1  01  dsp:8[reg]
2248
     2  10  dsp:16[reg]
2249
     3  11  reg  */
2250
0
      if (irel->r_addend & RX_RELAXA_DSP6)
2251
0
  {
2252
0
    GET_RELOC;
2253
2254
0
    code = insn[0] & 3;
2255
0
    if (code == 2 && symval/scale <= 255)
2256
0
      {
2257
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2258
0
        insn[0] &= 0xfc;
2259
0
        insn[0] |= 0x01;
2260
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2261
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2262
0
    {
2263
0
      SNIP (3, 1, newrel);
2264
0
      *again = true;
2265
0
    }
2266
0
      }
2267
2268
0
    else if (code == 1 && symval == 0)
2269
0
      {
2270
0
        insn[0] &= 0xfc;
2271
0
        SNIP (2, 1, R_RX_NONE);
2272
0
        *again = true;
2273
0
      }
2274
2275
    /* Special case DSP:5 format: MOV.bwl dsp:5[Rsrc],Rdst.  */
2276
0
    else if (code == 1 && symval/scale <= 31
2277
       /* Decodable bits.  */
2278
0
       && (insn[0] & 0xcc) == 0xcc
2279
       /* Width.  */
2280
0
       && (insn[0] & 0x30) != 0x30
2281
       /* Register MSBs.  */
2282
0
       && (insn[1] & 0x88)  == 0x00)
2283
0
      {
2284
0
        int newrel = 0;
2285
2286
0
        insn[0] = 0x88 | (insn[0] & 0x30);
2287
        /* The register fields are in the right place already.  */
2288
2289
        /* We can't relax this new opcode.  */
2290
0
        irel->r_addend = 0;
2291
2292
0
        switch ((insn[0] & 0x30) >> 4)
2293
0
    {
2294
0
    case 0:
2295
0
      newrel = R_RX_RH_ABS5p5B;
2296
0
      break;
2297
0
    case 1:
2298
0
      newrel = R_RX_RH_ABS5p5W;
2299
0
      break;
2300
0
    case 2:
2301
0
      newrel = R_RX_RH_ABS5p5L;
2302
0
      break;
2303
0
    }
2304
2305
0
        move_reloc (irel, srel, -2);
2306
0
        SNIP (2, 1, newrel);
2307
0
      }
2308
2309
    /* Special case DSP:5 format: MOVU.bw dsp:5[Rsrc],Rdst.  */
2310
0
    else if (code == 1 && symval/scale <= 31
2311
       /* Decodable bits.  */
2312
0
       && (insn[0] & 0xf8) == 0x58
2313
       /* Register MSBs.  */
2314
0
       && (insn[1] & 0x88)  == 0x00)
2315
0
      {
2316
0
        int newrel = 0;
2317
2318
0
        insn[0] = 0xb0 | ((insn[0] & 0x04) << 1);
2319
        /* The register fields are in the right place already.  */
2320
2321
        /* We can't relax this new opcode.  */
2322
0
        irel->r_addend = 0;
2323
2324
0
        switch ((insn[0] & 0x08) >> 3)
2325
0
    {
2326
0
    case 0:
2327
0
      newrel = R_RX_RH_ABS5p5B;
2328
0
      break;
2329
0
    case 1:
2330
0
      newrel = R_RX_RH_ABS5p5W;
2331
0
      break;
2332
0
    }
2333
2334
0
        move_reloc (irel, srel, -2);
2335
0
        SNIP (2, 1, newrel);
2336
0
      }
2337
0
  }
2338
2339
      /* A DSP4 operand always follows a DSP6 operand, even if there's
2340
   no relocation for it.  We have to read the code out of the
2341
   opcode to calculate the offset of the operand.  */
2342
0
      if (irel->r_addend & RX_RELAXA_DSP4)
2343
0
  {
2344
0
    int code6, offset = 0;
2345
2346
0
    GET_RELOC;
2347
2348
0
    code6 = insn[0] & 0x03;
2349
0
    switch (code6)
2350
0
      {
2351
0
      case 0: offset = 2; break;
2352
0
      case 1: offset = 3; break;
2353
0
      case 2: offset = 4; break;
2354
0
      case 3: offset = 2; break;
2355
0
      }
2356
2357
0
    code = (insn[0] & 0x0c) >> 2;
2358
2359
0
    if (code == 2 && symval / scale <= 255)
2360
0
      {
2361
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2362
2363
0
        insn[0] &= 0xf3;
2364
0
        insn[0] |= 0x04;
2365
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2366
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2367
0
    {
2368
0
      SNIP (offset+1, 1, newrel);
2369
0
      *again = true;
2370
0
    }
2371
0
      }
2372
2373
0
    else if (code == 1 && symval == 0)
2374
0
      {
2375
0
        insn[0] &= 0xf3;
2376
0
        SNIP (offset, 1, R_RX_NONE);
2377
0
        *again = true;
2378
0
      }
2379
    /* Special case DSP:5 format: MOV.bwl Rsrc,dsp:5[Rdst] */
2380
0
    else if (code == 1 && symval/scale <= 31
2381
       /* Decodable bits.  */
2382
0
       && (insn[0] & 0xc3) == 0xc3
2383
       /* Width.  */
2384
0
       && (insn[0] & 0x30) != 0x30
2385
       /* Register MSBs.  */
2386
0
       && (insn[1] & 0x88)  == 0x00)
2387
0
      {
2388
0
        int newrel = 0;
2389
2390
0
        insn[0] = 0x80 | (insn[0] & 0x30);
2391
        /* The register fields are in the right place already.  */
2392
2393
        /* We can't relax this new opcode.  */
2394
0
        irel->r_addend = 0;
2395
2396
0
        switch ((insn[0] & 0x30) >> 4)
2397
0
    {
2398
0
    case 0:
2399
0
      newrel = R_RX_RH_ABS5p5B;
2400
0
      break;
2401
0
    case 1:
2402
0
      newrel = R_RX_RH_ABS5p5W;
2403
0
      break;
2404
0
    case 2:
2405
0
      newrel = R_RX_RH_ABS5p5L;
2406
0
      break;
2407
0
    }
2408
2409
0
        move_reloc (irel, srel, -2);
2410
0
        SNIP (2, 1, newrel);
2411
0
      }
2412
0
  }
2413
2414
      /* These always occur alone, but the offset depends on whether
2415
   it's a MEMEX opcode (0x06) or not.  */
2416
0
      if (irel->r_addend & RX_RELAXA_DSP14)
2417
0
  {
2418
0
    int offset;
2419
0
    GET_RELOC;
2420
2421
0
    if (insn[0] == 0x06)
2422
0
      offset = 3;
2423
0
    else
2424
0
      offset = 4;
2425
2426
0
    code = insn[1] & 3;
2427
2428
0
    if (code == 2 && symval / scale <= 255)
2429
0
      {
2430
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2431
2432
0
        insn[1] &= 0xfc;
2433
0
        insn[1] |= 0x01;
2434
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2435
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2436
0
    {
2437
0
      SNIP (offset, 1, newrel);
2438
0
      *again = true;
2439
0
    }
2440
0
      }
2441
0
    else if (code == 1 && symval == 0)
2442
0
      {
2443
0
        insn[1] &= 0xfc;
2444
0
        SNIP (offset, 1, R_RX_NONE);
2445
0
        *again = true;
2446
0
      }
2447
0
  }
2448
2449
      /* IMM* codes:
2450
     0  00  imm:32
2451
     1  01  simm:8
2452
     2  10  simm:16
2453
     3  11  simm:24.  */
2454
2455
      /* These always occur alone.  */
2456
0
      if (irel->r_addend & RX_RELAXA_IMM6)
2457
0
  {
2458
0
    long ssymval;
2459
2460
0
    GET_RELOC;
2461
2462
    /* These relocations sign-extend, so we must do signed compares.  */
2463
0
    ssymval = (long) symval;
2464
2465
0
    code = insn[0] & 0x03;
2466
2467
0
    if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2468
0
      {
2469
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2470
2471
0
        insn[0] &= 0xfc;
2472
0
        insn[0] |= 0x03;
2473
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2474
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2475
0
    {
2476
0
      SNIP (2, 1, newrel);
2477
0
      *again = true;
2478
0
    }
2479
0
      }
2480
2481
0
    else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2482
0
      {
2483
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2484
2485
0
        insn[0] &= 0xfc;
2486
0
        insn[0] |= 0x02;
2487
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2488
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2489
0
    {
2490
0
      SNIP (2, 1, newrel);
2491
0
      *again = true;
2492
0
    }
2493
0
      }
2494
2495
    /* Special case UIMM8 format: CMP #uimm8,Rdst.  */
2496
0
    else if (code == 2 && ssymval <= 255 && ssymval >= 16
2497
       /* Decodable bits.  */
2498
0
       && (insn[0] & 0xfc) == 0x74
2499
       /* Decodable bits.  */
2500
0
       && ((insn[1] & 0xf0) == 0x00))
2501
0
      {
2502
0
        int newrel;
2503
2504
0
        insn[0] = 0x75;
2505
0
        insn[1] = 0x50 | (insn[1] & 0x0f);
2506
2507
        /* We can't relax this new opcode.  */
2508
0
        irel->r_addend = 0;
2509
2510
0
        if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2511
0
    newrel = R_RX_ABS8U;
2512
0
        else
2513
0
    newrel = R_RX_DIR8U;
2514
2515
0
        SNIP (2, 1, newrel);
2516
0
        *again = true;
2517
0
      }
2518
2519
0
    else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2520
0
      {
2521
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2522
2523
0
        insn[0] &= 0xfc;
2524
0
        insn[0] |= 0x01;
2525
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2526
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2527
0
    {
2528
0
      SNIP (2, 1, newrel);
2529
0
      *again = true;
2530
0
    }
2531
0
      }
2532
2533
    /* Special case UIMM4 format: CMP, MUL, AND, OR.  */
2534
0
    else if (code == 1 && ssymval <= 15 && ssymval >= 0
2535
       /* Decodable bits and immediate type.  */
2536
0
       && insn[0] == 0x75
2537
       /* Decodable bits.  */
2538
0
       && (insn[1] & 0xc0)  == 0x00)
2539
0
      {
2540
0
        static const int newop[4] = { 1, 3, 4, 5 };
2541
2542
0
        insn[0] = 0x60 | newop[insn[1] >> 4];
2543
        /* The register number doesn't move.  */
2544
2545
        /* We can't relax this new opcode.  */
2546
0
        irel->r_addend = 0;
2547
2548
0
        move_reloc (irel, srel, -1);
2549
2550
0
        SNIP (2, 1, R_RX_RH_UIMM4p8);
2551
0
        *again = true;
2552
0
      }
2553
2554
    /* Special case UIMM4 format: ADD -> ADD/SUB.  */
2555
0
    else if (code == 1 && ssymval <= 15 && ssymval >= -15
2556
       /* Decodable bits and immediate type.  */
2557
0
       && insn[0] == 0x71
2558
       /* Same register for source and destination.  */
2559
0
       && ((insn[1] >> 4) == (insn[1] & 0x0f)))
2560
0
      {
2561
0
        int newrel;
2562
2563
        /* Note that we can't turn "add $0,Rs" into a NOP
2564
     because the flags need to be set right.  */
2565
2566
0
        if (ssymval < 0)
2567
0
    {
2568
0
      insn[0] = 0x60; /* Subtract.  */
2569
0
      newrel = R_RX_RH_UNEG4p8;
2570
0
    }
2571
0
        else
2572
0
    {
2573
0
      insn[0] = 0x62; /* Add.  */
2574
0
      newrel = R_RX_RH_UIMM4p8;
2575
0
    }
2576
2577
        /* The register number is in the right place.  */
2578
2579
        /* We can't relax this new opcode.  */
2580
0
        irel->r_addend = 0;
2581
2582
0
        move_reloc (irel, srel, -1);
2583
2584
0
        SNIP (2, 1, newrel);
2585
0
        *again = true;
2586
0
      }
2587
0
  }
2588
2589
      /* These are either matched with a DSP6 (2-byte base) or an id24
2590
   (3-byte base).  */
2591
0
      if (irel->r_addend & RX_RELAXA_IMM12)
2592
0
  {
2593
0
    int dspcode, offset = 0;
2594
0
    long ssymval;
2595
2596
0
    GET_RELOC;
2597
2598
0
    if ((insn[0] & 0xfc) == 0xfc)
2599
0
      dspcode = 1; /* Just something with one byte operand.  */
2600
0
    else
2601
0
      dspcode = insn[0] & 3;
2602
0
    switch (dspcode)
2603
0
      {
2604
0
      case 0: offset = 2; break;
2605
0
      case 1: offset = 3; break;
2606
0
      case 2: offset = 4; break;
2607
0
      case 3: offset = 2; break;
2608
0
      }
2609
2610
    /* These relocations sign-extend, so we must do signed compares.  */
2611
0
    ssymval = (long) symval;
2612
2613
0
    code = (insn[1] >> 2) & 3;
2614
0
    if (code == 0 && ssymval <= 8388607 && ssymval >= -8388608)
2615
0
      {
2616
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2617
2618
0
        insn[1] &= 0xf3;
2619
0
        insn[1] |= 0x0c;
2620
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2621
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2622
0
    {
2623
0
      SNIP (offset, 1, newrel);
2624
0
      *again = true;
2625
0
    }
2626
0
      }
2627
2628
0
    else if (code == 3 && ssymval <= 32767 && ssymval >= -32768)
2629
0
      {
2630
0
        unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2631
2632
0
        insn[1] &= 0xf3;
2633
0
        insn[1] |= 0x08;
2634
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2635
0
        if (newrel != ELF32_R_TYPE (srel->r_info))
2636
0
    {
2637
0
      SNIP (offset, 1, newrel);
2638
0
      *again = true;
2639
0
    }
2640
0
      }
2641
2642
    /* Special case UIMM8 format: MOV #uimm8,Rdst.  */
2643
0
    else if (code == 2 && ssymval <= 255 && ssymval >= 16
2644
       /* Decodable bits.  */
2645
0
       && insn[0] == 0xfb
2646
       /* Decodable bits.  */
2647
0
       && ((insn[1] & 0x03) == 0x02))
2648
0
      {
2649
0
        int newrel;
2650
2651
0
        insn[0] = 0x75;
2652
0
        insn[1] = 0x40 | (insn[1] >> 4);
2653
2654
        /* We can't relax this new opcode.  */
2655
0
        irel->r_addend = 0;
2656
2657
0
        if (STACK_REL_P (ELF32_R_TYPE (srel->r_info)))
2658
0
    newrel = R_RX_ABS8U;
2659
0
        else
2660
0
    newrel = R_RX_DIR8U;
2661
2662
0
        SNIP (2, 1, newrel);
2663
0
        *again = true;
2664
0
      }
2665
2666
0
    else if (code == 2 && ssymval <= 127 && ssymval >= -128)
2667
0
      {
2668
0
        unsigned int newrel = ELF32_R_TYPE(srel->r_info);
2669
2670
0
        insn[1] &= 0xf3;
2671
0
        insn[1] |= 0x04;
2672
0
        newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2673
0
        if (newrel != ELF32_R_TYPE(srel->r_info))
2674
0
    {
2675
0
      SNIP (offset, 1, newrel);
2676
0
      *again = true;
2677
0
    }
2678
0
      }
2679
2680
    /* Special case UIMM4 format: MOV #uimm4,Rdst.  */
2681
0
    else if (code == 1 && ssymval <= 15 && ssymval >= 0
2682
       /* Decodable bits.  */
2683
0
       && insn[0] == 0xfb
2684
       /* Decodable bits.  */
2685
0
       && ((insn[1] & 0x03) == 0x02))
2686
0
      {
2687
0
        insn[0] = 0x66;
2688
0
        insn[1] = insn[1] >> 4;
2689
2690
        /* We can't relax this new opcode.  */
2691
0
        irel->r_addend = 0;
2692
2693
0
        move_reloc (irel, srel, -1);
2694
2695
0
        SNIP (2, 1, R_RX_RH_UIMM4p8);
2696
0
        *again = true;
2697
0
      }
2698
0
  }
2699
2700
0
      if (irel->r_addend & RX_RELAXA_BRA)
2701
0
  {
2702
0
    unsigned int newrel = ELF32_R_TYPE (srel->r_info);
2703
0
    int max_pcrel3 = 4;
2704
0
    int alignment_glue = 0;
2705
2706
0
    GET_RELOC;
2707
2708
    /* Branches over alignment chunks are problematic, as
2709
       deleting bytes here makes the branch *further* away.  We
2710
       can be agressive with branches within this alignment
2711
       block, but not branches outside it.  */
2712
0
    if ((prev_alignment == NULL
2713
0
         || symval < (bfd_vma)(sec_start + prev_alignment->r_offset))
2714
0
        && (next_alignment == NULL
2715
0
      || symval > (bfd_vma)(sec_start + next_alignment->r_offset)))
2716
0
      alignment_glue = section_alignment_glue;
2717
2718
0
    if (ELF32_R_TYPE(srel[1].r_info) == R_RX_RH_RELAX
2719
0
        && srel[1].r_addend & RX_RELAXA_BRA
2720
0
        && srel[1].r_offset < irel->r_offset + pcrel)
2721
0
      max_pcrel3 ++;
2722
2723
0
    newrel = next_smaller_reloc (ELF32_R_TYPE (srel->r_info));
2724
2725
    /* The values we compare PCREL with are not what you'd
2726
       expect; they're off by a little to compensate for (1)
2727
       where the reloc is relative to the insn, and (2) how much
2728
       the insn is going to change when we relax it.  */
2729
2730
    /* These we have to decode.  */
2731
0
    switch (insn[0])
2732
0
      {
2733
0
      case 0x04: /* BRA pcdsp:24 */
2734
0
        if (-32768 + alignment_glue <= pcrel
2735
0
      && pcrel <= 32765 - alignment_glue)
2736
0
    {
2737
0
      insn[0] = 0x38;
2738
0
      SNIP (3, 1, newrel);
2739
0
      *again = true;
2740
0
    }
2741
0
        break;
2742
2743
0
      case 0x38: /* BRA pcdsp:16 */
2744
0
        if (-128 + alignment_glue <= pcrel
2745
0
      && pcrel <= 127 - alignment_glue)
2746
0
    {
2747
0
      insn[0] = 0x2e;
2748
0
      SNIP (2, 1, newrel);
2749
0
      *again = true;
2750
0
    }
2751
0
        break;
2752
2753
0
      case 0x2e: /* BRA pcdsp:8 */
2754
        /* Note that there's a risk here of shortening things so
2755
     much that we no longer fit this reloc; it *should*
2756
     only happen when you branch across a branch, and that
2757
     branch also devolves into BRA.S.  "Real" code should
2758
     be OK.  */
2759
0
        if (max_pcrel3 + alignment_glue <= pcrel
2760
0
      && pcrel <= 10 - alignment_glue
2761
0
      && allow_pcrel3)
2762
0
    {
2763
0
      insn[0] = 0x08;
2764
0
      SNIP (1, 1, newrel);
2765
0
      move_reloc (irel, srel, -1);
2766
0
      *again = true;
2767
0
    }
2768
0
        break;
2769
2770
0
      case 0x05: /* BSR pcdsp:24 */
2771
0
        if (-32768 + alignment_glue <= pcrel
2772
0
      && pcrel <= 32765 - alignment_glue)
2773
0
    {
2774
0
      insn[0] = 0x39;
2775
0
      SNIP (1, 1, newrel);
2776
0
      *again = true;
2777
0
    }
2778
0
        break;
2779
2780
0
      case 0x3a: /* BEQ.W pcdsp:16 */
2781
0
      case 0x3b: /* BNE.W pcdsp:16 */
2782
0
        if (-128 + alignment_glue <= pcrel
2783
0
      && pcrel <= 127 - alignment_glue)
2784
0
    {
2785
0
      insn[0] = 0x20 | (insn[0] & 1);
2786
0
      SNIP (1, 1, newrel);
2787
0
      *again = true;
2788
0
    }
2789
0
        break;
2790
2791
0
      case 0x20: /* BEQ.B pcdsp:8 */
2792
0
      case 0x21: /* BNE.B pcdsp:8 */
2793
0
        if (max_pcrel3 + alignment_glue <= pcrel
2794
0
      && pcrel - alignment_glue <= 10
2795
0
      && allow_pcrel3)
2796
0
    {
2797
0
      insn[0] = 0x10 | ((insn[0] & 1) << 3);
2798
0
      SNIP (1, 1, newrel);
2799
0
      move_reloc (irel, srel, -1);
2800
0
      *again = true;
2801
0
    }
2802
0
        break;
2803
2804
0
      case 0x16: /* synthetic BNE dsp24 */
2805
0
      case 0x1e: /* synthetic BEQ dsp24 */
2806
0
        if (-32767 + alignment_glue <= pcrel
2807
0
      && pcrel <= 32766 - alignment_glue
2808
0
      && insn[1] == 0x04)
2809
0
    {
2810
0
      if (insn[0] == 0x16)
2811
0
        insn[0] = 0x3b;
2812
0
      else
2813
0
        insn[0] = 0x3a;
2814
      /* We snip out the bytes at the end else the reloc
2815
         will get moved too, and too much.  */
2816
0
      SNIP (3, 2, newrel);
2817
0
      move_reloc (irel, srel, -1);
2818
0
      *again = true;
2819
0
    }
2820
0
        break;
2821
0
      }
2822
2823
    /* Special case - synthetic conditional branches, pcrel24.
2824
       Note that EQ and NE have been handled above.  */
2825
0
    if ((insn[0] & 0xf0) == 0x20
2826
0
        && insn[1] == 0x06
2827
0
        && insn[2] == 0x04
2828
0
        && srel->r_offset != irel->r_offset + 1
2829
0
        && -32767 + alignment_glue <= pcrel
2830
0
        && pcrel <= 32766 - alignment_glue)
2831
0
      {
2832
0
        insn[1] = 0x05;
2833
0
        insn[2] = 0x38;
2834
0
        SNIP (5, 1, newrel);
2835
0
        *again = true;
2836
0
      }
2837
2838
    /* Special case - synthetic conditional branches, pcrel16 */
2839
0
    if ((insn[0] & 0xf0) == 0x20
2840
0
        && insn[1] == 0x05
2841
0
        && insn[2] == 0x38
2842
0
        && srel->r_offset != irel->r_offset + 1
2843
0
        && -127 + alignment_glue <= pcrel
2844
0
        && pcrel <= 126 - alignment_glue)
2845
0
      {
2846
0
        int cond = (insn[0] & 0x0f) ^ 0x01;
2847
2848
0
        insn[0] = 0x20 | cond;
2849
        /* By moving the reloc first, we avoid having
2850
     delete_bytes move it also.  */
2851
0
        move_reloc (irel, srel, -2);
2852
0
        SNIP (2, 3, newrel);
2853
0
        *again = true;
2854
0
      }
2855
0
  }
2856
2857
0
      BFD_ASSERT (nrelocs == 0);
2858
2859
      /* Special case - check MOV.bwl #IMM, dsp[reg] and see if we can
2860
   use MOV.bwl #uimm:8, dsp:5[r7] format.  This is tricky
2861
   because it may have one or two relocations.  */
2862
0
      if ((insn[0] & 0xfc) == 0xf8
2863
0
    && (insn[1] & 0x80) == 0x00
2864
0
    && (insn[0] & 0x03) != 0x03)
2865
0
  {
2866
0
    int dcode, icode, reg, ioff, dscale, ilen;
2867
0
    bfd_vma disp_val = 0;
2868
0
    long imm_val = 0;
2869
0
    Elf_Internal_Rela * disp_rel = 0;
2870
0
    Elf_Internal_Rela * imm_rel = 0;
2871
2872
    /* Reset this.  */
2873
0
    srel = irel;
2874
2875
0
    dcode = insn[0] & 0x03;
2876
0
    icode = (insn[1] >> 2) & 0x03;
2877
0
    reg = (insn[1] >> 4) & 0x0f;
2878
2879
0
    ioff = dcode == 1 ? 3 : dcode == 2 ? 4 : 2;
2880
2881
    /* Figure out what the dispacement is.  */
2882
0
    if (dcode == 1 || dcode == 2)
2883
0
      {
2884
        /* There's a displacement.  See if there's a reloc for it.  */
2885
0
        if (srel[1].r_offset == irel->r_offset + 2)
2886
0
    {
2887
0
      GET_RELOC;
2888
0
      disp_val = symval;
2889
0
      disp_rel = srel;
2890
0
    }
2891
0
        else
2892
0
    {
2893
0
      if (dcode == 1)
2894
0
        disp_val = insn[2];
2895
0
      else
2896
0
        {
2897
#if RX_OPCODE_BIG_ENDIAN
2898
          disp_val = insn[2] * 256 + insn[3];
2899
#else
2900
0
          disp_val = insn[2] + insn[3] * 256;
2901
0
#endif
2902
0
        }
2903
0
      switch (insn[1] & 3)
2904
0
        {
2905
0
        case 1:
2906
0
          disp_val *= 2;
2907
0
          scale = 2;
2908
0
          break;
2909
0
        case 2:
2910
0
          disp_val *= 4;
2911
0
          scale = 4;
2912
0
          break;
2913
0
        }
2914
0
    }
2915
0
      }
2916
2917
0
    dscale = scale;
2918
2919
    /* Figure out what the immediate is.  */
2920
0
    if (srel[1].r_offset == irel->r_offset + ioff)
2921
0
      {
2922
0
        GET_RELOC;
2923
0
        imm_val = (long) symval;
2924
0
        imm_rel = srel;
2925
0
      }
2926
0
    else
2927
0
      {
2928
0
        unsigned char * ip = insn + ioff;
2929
2930
0
        switch (icode)
2931
0
    {
2932
0
    case 1:
2933
      /* For byte writes, we don't sign extend.  Makes the math easier later.  */
2934
0
      if (scale == 1)
2935
0
        imm_val = ip[0];
2936
0
      else
2937
0
        imm_val = (char) ip[0];
2938
0
      break;
2939
0
    case 2:
2940
#if RX_OPCODE_BIG_ENDIAN
2941
      imm_val = ((char) ip[0] << 8) | ip[1];
2942
#else
2943
0
      imm_val = ((char) ip[1] << 8) | ip[0];
2944
0
#endif
2945
0
      break;
2946
0
    case 3:
2947
#if RX_OPCODE_BIG_ENDIAN
2948
      imm_val = ((char) ip[0] << 16) | (ip[1] << 8) | ip[2];
2949
#else
2950
0
      imm_val = ((char) ip[2] << 16) | (ip[1] << 8) | ip[0];
2951
0
#endif
2952
0
      break;
2953
0
    case 0:
2954
#if RX_OPCODE_BIG_ENDIAN
2955
      imm_val = ((unsigned) ip[0] << 24) | (ip[1] << 16) | (ip[2] << 8) | ip[3];
2956
#else
2957
0
      imm_val = ((unsigned) ip[3] << 24) | (ip[2] << 16) | (ip[1] << 8) | ip[0];
2958
0
#endif
2959
0
      break;
2960
0
    }
2961
0
      }
2962
2963
0
    ilen = 2;
2964
2965
0
    switch (dcode)
2966
0
      {
2967
0
      case 1:
2968
0
        ilen += 1;
2969
0
        break;
2970
0
      case 2:
2971
0
        ilen += 2;
2972
0
        break;
2973
0
      }
2974
2975
0
    switch (icode)
2976
0
      {
2977
0
      case 1:
2978
0
        ilen += 1;
2979
0
        break;
2980
0
      case 2:
2981
0
        ilen += 2;
2982
0
        break;
2983
0
      case 3:
2984
0
        ilen += 3;
2985
0
        break;
2986
0
      case 4:
2987
0
        ilen += 4;
2988
0
        break;
2989
0
      }
2990
2991
    /* The shortcut happens when the immediate is 0..255,
2992
       register r0 to r7, and displacement (scaled) 0..31.  */
2993
2994
0
    if (0 <= imm_val && imm_val <= 255
2995
0
        && 0 <= reg && reg <= 7
2996
0
        && disp_val / dscale <= 31)
2997
0
      {
2998
0
        insn[0] = 0x3c | (insn[1] & 0x03);
2999
0
        insn[1] = (((disp_val / dscale) << 3) & 0x80) | (reg << 4) | ((disp_val/dscale) & 0x0f);
3000
0
        insn[2] = imm_val;
3001
3002
0
        if (disp_rel)
3003
0
    {
3004
0
      int newrel = R_RX_NONE;
3005
3006
0
      switch (dscale)
3007
0
        {
3008
0
        case 1:
3009
0
          newrel = R_RX_RH_ABS5p8B;
3010
0
          break;
3011
0
        case 2:
3012
0
          newrel = R_RX_RH_ABS5p8W;
3013
0
          break;
3014
0
        case 4:
3015
0
          newrel = R_RX_RH_ABS5p8L;
3016
0
          break;
3017
0
        }
3018
0
      disp_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (disp_rel->r_info), newrel);
3019
0
      move_reloc (irel, disp_rel, -1);
3020
0
    }
3021
0
        if (imm_rel)
3022
0
    {
3023
0
      imm_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (imm_rel->r_info), R_RX_DIR8U);
3024
0
      move_reloc (disp_rel ? disp_rel : irel,
3025
0
            imm_rel,
3026
0
            irel->r_offset - imm_rel->r_offset + 2);
3027
0
    }
3028
3029
0
        SNIPNR (3, ilen - 3);
3030
0
        *again = true;
3031
3032
        /* We can't relax this new opcode.  */
3033
0
        irel->r_addend = 0;
3034
0
      }
3035
0
  }
3036
0
    }
3037
3038
  /* We can't reliably relax branches to DIR3U_PCREL unless we know
3039
     whatever they're branching over won't shrink any more.  If we're
3040
     basically done here, do one more pass just for branches - but
3041
     don't request a pass after that one!  */
3042
0
  if (!*again && !allow_pcrel3)
3043
0
    {
3044
0
      bool ignored;
3045
3046
0
      elf32_rx_relax_section (abfd, sec, link_info, &ignored, true);
3047
0
    }
3048
3049
0
  return true;
3050
3051
0
 error_return:
3052
0
  free (free_contents);
3053
3054
0
  if (shndx_buf != NULL)
3055
0
    {
3056
0
      shndx_hdr->contents = NULL;
3057
0
      free (shndx_buf);
3058
0
    }
3059
3060
0
  free (free_intsyms);
3061
3062
0
  return false;
3063
0
}
3064
3065
static bool
3066
elf32_rx_relax_section_wrapper (bfd *abfd,
3067
        asection *sec,
3068
        struct bfd_link_info *link_info,
3069
        bool *again)
3070
0
{
3071
0
  return elf32_rx_relax_section (abfd, sec, link_info, again, false);
3072
0
}
3073

3074
/* Function to set the ELF flag bits.  */
3075
3076
static bool
3077
rx_elf_set_private_flags (bfd * abfd, flagword flags)
3078
0
{
3079
0
  elf_elfheader (abfd)->e_flags = flags;
3080
0
  elf_flags_init (abfd) = true;
3081
0
  return true;
3082
0
}
3083
3084
static bool no_warn_mismatch = false;
3085
static bool ignore_lma = true;
3086
3087
void bfd_elf32_rx_set_target_flags (bool, bool);
3088
3089
void
3090
bfd_elf32_rx_set_target_flags (bool user_no_warn_mismatch,
3091
             bool user_ignore_lma)
3092
0
{
3093
0
  no_warn_mismatch = user_no_warn_mismatch;
3094
0
  ignore_lma = user_ignore_lma;
3095
0
}
3096
3097
/* Converts FLAGS into a descriptive string.
3098
   Returns a static pointer.  */
3099
3100
static const char *
3101
describe_flags (flagword flags, char *buf)
3102
2.21k
{
3103
2.21k
  buf[0] = 0;
3104
3105
2.21k
  if (flags & E_FLAG_RX_64BIT_DOUBLES)
3106
45
    strcat (buf, "64-bit doubles");
3107
2.17k
  else
3108
2.17k
    strcat (buf, "32-bit doubles");
3109
3110
2.21k
  if (flags & E_FLAG_RX_DSP)
3111
60
    strcat (buf, ", dsp");
3112
2.15k
  else
3113
2.15k
    strcat (buf, ", no dsp");
3114
3115
2.21k
  if (flags & E_FLAG_RX_PID)
3116
55
    strcat (buf, ", pid");
3117
2.16k
  else
3118
2.16k
    strcat (buf, ", no pid");
3119
3120
2.21k
  if (flags & E_FLAG_RX_ABI)
3121
36
    strcat (buf, ", RX ABI");
3122
2.18k
  else
3123
2.18k
    strcat (buf, ", GCC ABI");
3124
3125
2.21k
  if (flags & E_FLAG_RX_SINSNS_SET)
3126
55
    strcat (buf, flags & E_FLAG_RX_SINSNS_YES ? ", uses String instructions" : ", bans String instructions");
3127
3128
2.21k
  return buf;
3129
2.21k
}
3130
3131
/* Merge backend specific data from an object file to the output
3132
   object file when linking.  */
3133
3134
static bool
3135
rx_elf_merge_private_bfd_data (bfd * ibfd, struct bfd_link_info *info)
3136
0
{
3137
0
  bfd *obfd = info->output_bfd;
3138
0
  flagword old_flags;
3139
0
  flagword new_flags;
3140
0
  bool error = false;
3141
3142
0
  new_flags = elf_elfheader (ibfd)->e_flags;
3143
0
  old_flags = elf_elfheader (obfd)->e_flags;
3144
3145
0
  if (!elf_flags_init (obfd))
3146
0
    {
3147
      /* First call, no flags set.  */
3148
0
      elf_flags_init (obfd) = true;
3149
0
      elf_elfheader (obfd)->e_flags = new_flags;
3150
0
    }
3151
0
  else if (old_flags != new_flags)
3152
0
    {
3153
0
      flagword known_flags;
3154
3155
0
      if (old_flags & E_FLAG_RX_SINSNS_SET)
3156
0
  {
3157
0
    if ((new_flags & E_FLAG_RX_SINSNS_SET) == 0)
3158
0
      {
3159
0
        new_flags &= ~ E_FLAG_RX_SINSNS_MASK;
3160
0
        new_flags |= (old_flags & E_FLAG_RX_SINSNS_MASK);
3161
0
      }
3162
0
  }
3163
0
      else if (new_flags & E_FLAG_RX_SINSNS_SET)
3164
0
  {
3165
0
    old_flags &= ~ E_FLAG_RX_SINSNS_MASK;
3166
0
    old_flags |= (new_flags & E_FLAG_RX_SINSNS_MASK);
3167
0
  }
3168
3169
0
      known_flags = E_FLAG_RX_ABI | E_FLAG_RX_64BIT_DOUBLES
3170
0
  | E_FLAG_RX_DSP | E_FLAG_RX_PID | E_FLAG_RX_SINSNS_MASK;
3171
3172
0
      if ((old_flags ^ new_flags) & known_flags)
3173
0
  {
3174
    /* Only complain if flag bits we care about do not match.
3175
       Other bits may be set, since older binaries did use some
3176
       deprecated flags.  */
3177
0
    if (no_warn_mismatch)
3178
0
      {
3179
0
        elf_elfheader (obfd)->e_flags = (new_flags | old_flags) & known_flags;
3180
0
      }
3181
0
    else
3182
0
      {
3183
0
        char buf[128];
3184
3185
0
        _bfd_error_handler (_("there is a conflict merging the"
3186
0
            " ELF header flags from %pB"),
3187
0
          ibfd);
3188
0
        _bfd_error_handler (_("  the input  file's flags: %s"),
3189
0
          describe_flags (new_flags, buf));
3190
0
        _bfd_error_handler (_("  the output file's flags: %s"),
3191
0
          describe_flags (old_flags, buf));
3192
0
        error = true;
3193
0
      }
3194
0
  }
3195
0
      else
3196
0
  elf_elfheader (obfd)->e_flags = new_flags & known_flags;
3197
0
    }
3198
3199
0
  if (error)
3200
0
    bfd_set_error (bfd_error_bad_value);
3201
3202
0
  return !error;
3203
0
}
3204

3205
static bool
3206
rx_elf_print_private_bfd_data (bfd * abfd, void * ptr)
3207
2.21k
{
3208
2.21k
  FILE * file = (FILE *) ptr;
3209
2.21k
  flagword flags;
3210
2.21k
  char buf[128];
3211
3212
2.21k
  BFD_ASSERT (abfd != NULL && ptr != NULL);
3213
3214
  /* Print normal ELF private data.  */
3215
2.21k
  _bfd_elf_print_private_bfd_data (abfd, ptr);
3216
3217
2.21k
  flags = elf_elfheader (abfd)->e_flags;
3218
2.21k
  fprintf (file, _("private flags = 0x%lx:"), (long) flags);
3219
3220
2.21k
  fprintf (file, "%s", describe_flags (flags, buf));
3221
2.21k
  return true;
3222
2.21k
}
3223
3224
/* Return the MACH for an e_flags value.  */
3225
3226
static int
3227
elf32_rx_machine (bfd * abfd ATTRIBUTE_UNUSED)
3228
143k
{
3229
#if 0 /* FIXME: EF_RX_CPU_MASK collides with E_FLAG_RX_...
3230
   Need to sort out how these flag bits are used.
3231
   For now we assume that the flags are OK.  */
3232
  if ((elf_elfheader (abfd)->e_flags & EF_RX_CPU_MASK) == EF_RX_CPU_RX)
3233
#endif
3234
143k
    if ((elf_elfheader (abfd)->e_flags & E_FLAG_RX_V2))
3235
58.0k
      return bfd_mach_rx_v2;
3236
85.6k
    else if ((elf_elfheader (abfd)->e_flags & E_FLAG_RX_V3))
3237
57.6k
      return bfd_mach_rx_v3;
3238
28.0k
    else
3239
28.0k
      return bfd_mach_rx;
3240
3241
0
  return 0;
3242
143k
}
3243
3244
static bool
3245
rx_elf_object_p (bfd * abfd)
3246
148k
{
3247
148k
  int i;
3248
148k
  unsigned int u;
3249
148k
  Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
3250
148k
  Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
3251
148k
  int nphdrs = ehdr->e_phnum;
3252
148k
  sec_ptr bsec;
3253
148k
  static int saw_be = false;
3254
148k
  bfd_vma end_phdroff;
3255
3256
  /* We never want to automatically choose the non-swapping big-endian
3257
     target.  The user can only get that explicitly, such as with -I
3258
     and objcopy.  */
3259
148k
  if (abfd->xvec == &rx_elf32_be_ns_vec
3260
148k
      && abfd->target_defaulted)
3261
2.17k
    return false;
3262
3263
  /* BFD->target_defaulted is not set to TRUE when a target is chosen
3264
     as a fallback, so we check for "scanning" to know when to stop
3265
     using the non-swapping target.  */
3266
146k
  if (abfd->xvec == &rx_elf32_be_ns_vec
3267
146k
      && saw_be)
3268
2.68k
    return false;
3269
143k
  if (abfd->xvec == &rx_elf32_be_vec)
3270
9.74k
    saw_be = true;
3271
3272
143k
  bfd_default_set_arch_mach (abfd, bfd_arch_rx,
3273
143k
           elf32_rx_machine (abfd));
3274
3275
  /* For each PHDR in the object, we must find some section that
3276
     corresponds (based on matching file offsets) and use its VMA
3277
     information to reconstruct the p_vaddr field we clobbered when we
3278
     wrote it out.  */
3279
  /* If PT_LOAD headers include the ELF file header or program headers
3280
     then the PT_LOAD header does not start with some section contents.
3281
     Making adjustments based on the difference between sh_offset and
3282
     p_offset is nonsense in such cases.  Exclude them.  Note that
3283
     since standard linker scripts for RX do not use SIZEOF_HEADERS,
3284
     the linker won't normally create PT_LOAD segments covering the
3285
     headers so this is mainly for passing the ld testsuite.
3286
     FIXME.  Why are we looking at non-PT_LOAD headers here?  */
3287
143k
  end_phdroff = ehdr->e_ehsize;
3288
143k
  if (ehdr->e_phoff != 0)
3289
79.1k
    end_phdroff = ehdr->e_phoff + nphdrs * ehdr->e_phentsize;
3290
10.8M
  for (i=0; i<nphdrs; i++)
3291
10.7M
    {
3292
37.2M
      for (u=0; u<elf_tdata(abfd)->num_elf_sections; u++)
3293
28.0M
  {
3294
28.0M
    Elf_Internal_Shdr *sec = elf_tdata(abfd)->elf_sect_ptr[u];
3295
3296
28.0M
    if (phdr[i].p_filesz
3297
28.0M
        && phdr[i].p_offset >= end_phdroff
3298
28.0M
        && phdr[i].p_offset <= (bfd_vma) sec->sh_offset
3299
28.0M
        && sec->sh_size > 0
3300
28.0M
        && sec->sh_type != SHT_NOBITS
3301
28.0M
        && (bfd_vma)sec->sh_offset <= phdr[i].p_offset + (phdr[i].p_filesz - 1))
3302
1.50M
      {
3303
        /* Found one!  The difference between the two addresses,
3304
     plus the difference between the two file offsets, is
3305
     enough information to reconstruct the lma.  */
3306
3307
        /* Example where they aren't:
3308
     PHDR[1] = lma fffc0100 offset 00002010 size 00000100
3309
     SEC[6]  = vma 00000050 offset 00002050 size 00000040
3310
3311
     The correct LMA for the section is fffc0140 + (2050-2010).
3312
        */
3313
3314
1.50M
        phdr[i].p_vaddr = sec->sh_addr + (sec->sh_offset - phdr[i].p_offset);
3315
1.50M
        break;
3316
1.50M
      }
3317
28.0M
  }
3318
3319
      /* We must update the bfd sections as well, so we don't stop
3320
   with one match.  */
3321
10.7M
      bsec = abfd->sections;
3322
18.5M
      while (bsec)
3323
7.81M
  {
3324
7.81M
    if (phdr[i].p_filesz
3325
7.81M
        && phdr[i].p_vaddr <= bsec->vma
3326
7.81M
        && bsec->vma <= phdr[i].p_vaddr + (phdr[i].p_filesz - 1))
3327
1.10M
      {
3328
1.10M
        bsec->lma = phdr[i].p_paddr + (bsec->vma - phdr[i].p_vaddr);
3329
1.10M
      }
3330
7.81M
    bsec = bsec->next;
3331
7.81M
  }
3332
10.7M
    }
3333
3334
143k
  return true;
3335
146k
}
3336
3337
static bool
3338
rx_linux_object_p (bfd * abfd)
3339
0
{
3340
0
  bfd_default_set_arch_mach (abfd, bfd_arch_rx, elf32_rx_machine (abfd));
3341
0
  return true;
3342
0
}
3343
 
3344
3345
#ifdef DEBUG
3346
void
3347
rx_dump_symtab (bfd * abfd, void * internal_syms, void * external_syms)
3348
{
3349
  size_t locsymcount;
3350
  Elf_Internal_Sym * isymbuf;
3351
  Elf_Internal_Sym * isymend;
3352
  Elf_Internal_Sym * isym;
3353
  Elf_Internal_Shdr * symtab_hdr;
3354
  char * st_info_str;
3355
  char * st_info_stb_str;
3356
  char * st_other_str;
3357
  char * st_shndx_str;
3358
3359
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3360
  locsymcount = symtab_hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3361
  if (!internal_syms)
3362
    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3363
            symtab_hdr->sh_info, 0,
3364
            internal_syms, external_syms, NULL);
3365
  else
3366
    isymbuf = internal_syms;
3367
  isymend = isymbuf + locsymcount;
3368
3369
  for (isym = isymbuf ; isym < isymend ; isym++)
3370
    {
3371
      switch (ELF_ST_TYPE (isym->st_info))
3372
  {
3373
  case STT_FUNC: st_info_str = "STT_FUNC"; break;
3374
  case STT_SECTION: st_info_str = "STT_SECTION"; break;
3375
  case STT_FILE: st_info_str = "STT_FILE"; break;
3376
  case STT_OBJECT: st_info_str = "STT_OBJECT"; break;
3377
  case STT_TLS: st_info_str = "STT_TLS"; break;
3378
  default: st_info_str = "";
3379
  }
3380
      switch (ELF_ST_BIND (isym->st_info))
3381
  {
3382
  case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; break;
3383
  case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; break;
3384
  default: st_info_stb_str = "";
3385
  }
3386
      switch (ELF_ST_VISIBILITY (isym->st_other))
3387
  {
3388
  case STV_DEFAULT: st_other_str = "STV_DEFAULT"; break;
3389
  case STV_INTERNAL: st_other_str = "STV_INTERNAL"; break;
3390
  case STV_PROTECTED: st_other_str = "STV_PROTECTED"; break;
3391
  default: st_other_str = "";
3392
  }
3393
      switch (isym->st_shndx)
3394
  {
3395
  case SHN_ABS: st_shndx_str = "SHN_ABS"; break;
3396
  case SHN_COMMON: st_shndx_str = "SHN_COMMON"; break;
3397
  case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; break;
3398
  default: st_shndx_str = "";
3399
  }
3400
3401
      printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
3402
        "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
3403
        isym,
3404
        (unsigned long) isym->st_value,
3405
        (unsigned long) isym->st_size,
3406
        isym->st_name,
3407
        bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
3408
                 isym->st_name),
3409
        isym->st_info, st_info_str, st_info_stb_str,
3410
        isym->st_other, st_other_str,
3411
        isym->st_shndx, st_shndx_str);
3412
    }
3413
}
3414
3415
char *
3416
rx_get_reloc (long reloc)
3417
{
3418
  if (0 <= reloc && reloc < R_RX_max)
3419
    return rx_elf_howto_table[reloc].name;
3420
  return "";
3421
}
3422
#endif /* DEBUG */
3423
3424

3425
/* We must take care to keep the on-disk copy of any code sections
3426
   that are fully linked swapped if the target is big endian, to match
3427
   the Renesas tools.  */
3428
3429
/* The rule is: big endian object that are final-link executables,
3430
   have code sections stored with 32-bit words swapped relative to
3431
   what you'd get by default.  */
3432
3433
static bool
3434
rx_get_section_contents (bfd *         abfd,
3435
       sec_ptr       section,
3436
       void *        location,
3437
       file_ptr      offset,
3438
       bfd_size_type count)
3439
239k
{
3440
239k
  int exec = (abfd->flags & EXEC_P) ? 1 : 0;
3441
239k
  int s_code = (section->flags & SEC_CODE) ? 1 : 0;
3442
239k
  bool rv;
3443
3444
#ifdef DJDEBUG
3445
  fprintf (stderr, "dj: get %ld %ld from %s  %s e%d sc%d  %08lx:%08lx\n",
3446
     (long) offset, (long) count, section->name,
3447
     bfd_big_endian(abfd) ? "be" : "le",
3448
     exec, s_code, (long unsigned) section->filepos,
3449
     (long unsigned) offset);
3450
#endif
3451
3452
239k
  if (exec && s_code && bfd_big_endian (abfd))
3453
0
    {
3454
0
      char * cloc = (char *) location;
3455
0
      bfd_size_type cnt, end_cnt;
3456
3457
0
      rv = true;
3458
3459
      /* Fetch and swap unaligned bytes at the beginning.  */
3460
0
      if (offset % 4)
3461
0
  {
3462
0
    char buf[4];
3463
3464
0
    rv = _bfd_generic_get_section_contents (abfd, section, buf,
3465
0
              (offset & -4), 4);
3466
0
    if (!rv)
3467
0
      return false;
3468
3469
0
    bfd_putb32 (bfd_getl32 (buf), buf);
3470
3471
0
    cnt = 4 - (offset % 4);
3472
0
    if (cnt > count)
3473
0
      cnt = count;
3474
3475
0
    memcpy (location, buf + (offset % 4), cnt);
3476
3477
0
    count -= cnt;
3478
0
    offset += cnt;
3479
0
    cloc += count;
3480
0
  }
3481
3482
0
      end_cnt = count % 4;
3483
3484
      /* Fetch and swap the middle bytes.  */
3485
0
      if (count >= 4)
3486
0
  {
3487
0
    rv = _bfd_generic_get_section_contents (abfd, section, cloc, offset,
3488
0
              count - end_cnt);
3489
0
    if (!rv)
3490
0
      return false;
3491
3492
0
    for (cnt = count; cnt >= 4; cnt -= 4, cloc += 4)
3493
0
      bfd_putb32 (bfd_getl32 (cloc), cloc);
3494
0
  }
3495
3496
      /* Fetch and swap the end bytes.  */
3497
0
      if (end_cnt > 0)
3498
0
  {
3499
0
    char buf[4];
3500
3501
    /* Fetch the end bytes.  */
3502
0
    rv = _bfd_generic_get_section_contents (abfd, section, buf,
3503
0
              offset + count - end_cnt, 4);
3504
0
    if (!rv)
3505
0
      return false;
3506
3507
0
    bfd_putb32 (bfd_getl32 (buf), buf);
3508
0
    memcpy (cloc, buf, end_cnt);
3509
0
  }
3510
0
    }
3511
239k
  else
3512
239k
    rv = _bfd_generic_get_section_contents (abfd, section, location, offset, count);
3513
3514
239k
  return rv;
3515
239k
}
3516
3517
#ifdef DJDEBUG
3518
static bool
3519
rx2_set_section_contents (bfd *        abfd,
3520
       sec_ptr       section,
3521
       const void *  location,
3522
       file_ptr      offset,
3523
       bfd_size_type count)
3524
{
3525
  bfd_size_type i;
3526
3527
  fprintf (stderr, "   set sec %s %08x loc %p offset %#x count %#x\n",
3528
     section->name, (unsigned) section->vma, location, (int) offset, (int) count);
3529
  for (i = 0; i < count; i++)
3530
    {
3531
      if (i % 16 == 0 && i > 0)
3532
  fprintf (stderr, "\n");
3533
3534
      if (i % 16  && i % 4 == 0)
3535
  fprintf (stderr, " ");
3536
3537
      if (i % 16 == 0)
3538
  fprintf (stderr, "      %08x:", (int) (section->vma + offset + i));
3539
3540
      fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3541
    }
3542
  fprintf (stderr, "\n");
3543
3544
  return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3545
}
3546
#define _bfd_elf_set_section_contents rx2_set_section_contents
3547
#endif
3548
3549
static bool
3550
rx_set_section_contents (bfd *         abfd,
3551
       sec_ptr       section,
3552
       const void *  location,
3553
       file_ptr      offset,
3554
       bfd_size_type count)
3555
0
{
3556
0
  bool exec = (abfd->flags & EXEC_P) != 0;
3557
0
  bool s_code = (section->flags & SEC_CODE) != 0;
3558
0
  bool rv;
3559
0
  char * swapped_data = NULL;
3560
0
  bfd_size_type i;
3561
0
  bfd_vma caddr = section->vma + offset;
3562
0
  file_ptr faddr = 0;
3563
0
  bfd_size_type scount;
3564
3565
#ifdef DJDEBUG
3566
  bfd_size_type i;
3567
3568
  fprintf (stderr, "\ndj: set %ld %ld to %s  %s e%d sc%d\n",
3569
     (long) offset, (long) count, section->name,
3570
     bfd_big_endian (abfd) ? "be" : "le",
3571
     exec, s_code);
3572
3573
  for (i = 0; i < count; i++)
3574
    {
3575
      int a = section->vma + offset + i;
3576
3577
      if (a % 16 == 0 && a > 0)
3578
  fprintf (stderr, "\n");
3579
3580
      if (a % 16  && a % 4 == 0)
3581
  fprintf (stderr, " ");
3582
3583
      if (a % 16 == 0 || i == 0)
3584
  fprintf (stderr, "      %08x:", (int) (section->vma + offset + i));
3585
3586
      fprintf (stderr, " %02x", ((unsigned char *) location)[i]);
3587
    }
3588
3589
  fprintf (stderr, "\n");
3590
#endif
3591
3592
0
  if (! exec || ! s_code || ! bfd_big_endian (abfd))
3593
0
    return _bfd_elf_set_section_contents (abfd, section, location, offset, count);
3594
3595
0
  while (count > 0 && caddr > 0 && caddr % 4)
3596
0
    {
3597
0
      switch (caddr % 4)
3598
0
  {
3599
0
  case 0: faddr = offset + 3; break;
3600
0
  case 1: faddr = offset + 1; break;
3601
0
  case 2: faddr = offset - 1; break;
3602
0
  case 3: faddr = offset - 3; break;
3603
0
  }
3604
3605
0
      rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3606
0
      if (! rv)
3607
0
  return rv;
3608
3609
0
      location = (bfd_byte *) location + 1;
3610
0
      offset ++;
3611
0
      count --;
3612
0
      caddr ++;
3613
0
    }
3614
3615
0
  scount = (int)(count / 4) * 4;
3616
0
  if (scount > 0)
3617
0
    {
3618
0
      char * cloc = (char *) location;
3619
3620
0
      swapped_data = (char *) bfd_alloc (abfd, count);
3621
0
      if (swapped_data == NULL)
3622
0
  return false;
3623
3624
0
      for (i = 0; i < count; i += 4)
3625
0
  {
3626
0
    bfd_vma v = bfd_getl32 (cloc + i);
3627
0
    bfd_putb32 (v, swapped_data + i);
3628
0
  }
3629
3630
0
      rv = _bfd_elf_set_section_contents (abfd, section, swapped_data, offset, scount);
3631
3632
0
      if (!rv)
3633
0
  return rv;
3634
0
    }
3635
3636
0
  count -= scount;
3637
0
  location = (bfd_byte *) location + scount;
3638
0
  offset += scount;
3639
3640
0
  if (count > 0)
3641
0
    {
3642
0
      caddr = section->vma + offset;
3643
0
      while (count > 0)
3644
0
  {
3645
0
    switch (caddr % 4)
3646
0
      {
3647
0
      case 0: faddr = offset + 3; break;
3648
0
      case 1: faddr = offset + 1; break;
3649
0
      case 2: faddr = offset - 1; break;
3650
0
      case 3: faddr = offset - 3; break;
3651
0
      }
3652
0
    rv = _bfd_elf_set_section_contents (abfd, section, location, faddr, 1);
3653
0
    if (! rv)
3654
0
      return rv;
3655
3656
0
    location = (bfd_byte *) location + 1;
3657
0
    offset ++;
3658
0
    count --;
3659
0
    caddr ++;
3660
0
  }
3661
0
    }
3662
3663
0
  return true;
3664
0
}
3665
3666
static bool
3667
rx_final_link (bfd * abfd, struct bfd_link_info * info)
3668
0
{
3669
0
  asection * o;
3670
3671
0
  for (o = abfd->sections; o != NULL; o = o->next)
3672
0
    {
3673
#ifdef DJDEBUG
3674
      fprintf (stderr, "sec %s fl %x vma %lx lma %lx size %lx raw %lx\n",
3675
         o->name, o->flags, o->vma, o->lma, o->size, o->rawsize);
3676
#endif
3677
0
      if (o->flags & SEC_CODE
3678
0
    && bfd_big_endian (abfd)
3679
0
    && o->size % 4)
3680
0
  {
3681
#ifdef DJDEBUG
3682
    fprintf (stderr, "adjusting...\n");
3683
#endif
3684
0
    o->size += 4 - (o->size % 4);
3685
0
  }
3686
0
    }
3687
3688
0
  return bfd_elf_final_link (abfd, info);
3689
0
}
3690
3691
static bool
3692
elf32_rx_modify_headers (bfd *abfd, struct bfd_link_info *info)
3693
0
{
3694
0
  const struct elf_backend_data * bed;
3695
0
  struct elf_obj_tdata * tdata;
3696
0
  Elf_Internal_Phdr * phdr;
3697
0
  unsigned int count;
3698
0
  unsigned int i;
3699
3700
0
  bed = get_elf_backend_data (abfd);
3701
0
  tdata = elf_tdata (abfd);
3702
0
  phdr = tdata->phdr;
3703
0
  count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
3704
3705
0
  if (ignore_lma)
3706
0
    for (i = count; i-- != 0;)
3707
0
      if (phdr[i].p_type == PT_LOAD)
3708
0
  {
3709
    /* The Renesas tools expect p_paddr to be zero.  However,
3710
       there is no other way to store the writable data in ROM for
3711
       startup initialization.  So, we let the linker *think*
3712
       we're using paddr and vaddr the "usual" way, but at the
3713
       last minute we move the paddr into the vaddr (which is what
3714
       the simulator uses) and zero out paddr.  Note that this
3715
       does not affect the section headers, just the program
3716
       headers.  We hope.  */
3717
0
    phdr[i].p_vaddr = phdr[i].p_paddr;
3718
#if 0   /* If we zero out p_paddr, then the LMA in the section table
3719
       becomes wrong.  */
3720
    phdr[i].p_paddr = 0;
3721
#endif
3722
0
  }
3723
3724
0
  return _bfd_elf_modify_headers (abfd, info);
3725
0
}
3726
3727
/* The default literal sections should always be marked as "code" (i.e.,
3728
   SHF_EXECINSTR).  This is particularly important for big-endian mode
3729
   when we do not want their contents byte reversed.  */
3730
static const struct bfd_elf_special_section elf32_rx_special_sections[] =
3731
{
3732
  { STRING_COMMA_LEN (".init_array"),  0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3733
  { STRING_COMMA_LEN (".fini_array"),  0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3734
  { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_EXECINSTR },
3735
  { NULL,      0,  0, 0,      0 }
3736
};
3737

3738
typedef struct {
3739
  bfd *abfd;
3740
  struct bfd_link_info *info;
3741
  bfd_vma table_start;
3742
  int table_size;
3743
  bfd_vma *table_handlers;
3744
  bfd_vma table_default_handler;
3745
  struct bfd_link_hash_entry **table_entries;
3746
  struct bfd_link_hash_entry *table_default_entry;
3747
  FILE *mapfile;
3748
} RX_Table_Info;
3749
3750
static bool
3751
rx_table_find (struct bfd_hash_entry *vent, void *vinfo)
3752
0
{
3753
0
  RX_Table_Info *info = (RX_Table_Info *)vinfo;
3754
0
  struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3755
0
  const char *name; /* of the symbol we've found */
3756
0
  asection *sec;
3757
0
  struct bfd *abfd;
3758
0
  int idx;
3759
0
  const char *tname; /* name of the table */
3760
0
  bfd_vma start_addr, end_addr;
3761
0
  char *buf;
3762
0
  struct bfd_link_hash_entry * h;
3763
3764
  /* We're looking for globally defined symbols of the form
3765
     $tablestart$<NAME>.  */
3766
0
  if (ent->type != bfd_link_hash_defined
3767
0
      && ent->type != bfd_link_hash_defweak)
3768
0
    return true;
3769
3770
0
  name = ent->root.string;
3771
0
  sec = ent->u.def.section;
3772
0
  abfd = sec->owner;
3773
3774
0
  if (!startswith (name, "$tablestart$"))
3775
0
    return true;
3776
3777
0
  sec->flags |= SEC_KEEP;
3778
3779
0
  tname = name + 12;
3780
3781
0
  start_addr = ent->u.def.value;
3782
3783
  /* At this point, we can't build the table but we can (and must)
3784
     find all the related symbols and mark their sections as SEC_KEEP
3785
     so we don't garbage collect them.  */
3786
3787
0
  buf = (char *) bfd_malloc (12 + 10 + strlen (tname));
3788
0
  if (buf == NULL)
3789
0
    return false;
3790
3791
0
  sprintf (buf, "$tableend$%s", tname);
3792
0
  h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3793
0
  if (!h || (h->type != bfd_link_hash_defined
3794
0
       && h->type != bfd_link_hash_defweak))
3795
0
    {
3796
      /* xgettext:c-format */
3797
0
      _bfd_error_handler (_("%pB:%pA: table %s missing corresponding %s"),
3798
0
        abfd, sec, name, buf);
3799
0
      return true;
3800
0
    }
3801
3802
0
  if (h->u.def.section != ent->u.def.section)
3803
0
    {
3804
      /* xgettext:c-format */
3805
0
      _bfd_error_handler (_("%pB:%pA: %s and %s must be in the same input section"),
3806
0
        h->u.def.section->owner, h->u.def.section,
3807
0
        name, buf);
3808
0
      return true;
3809
0
    }
3810
3811
0
  end_addr = h->u.def.value;
3812
3813
0
  sprintf (buf, "$tableentry$default$%s", tname);
3814
0
  h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3815
0
  if (h && (h->type == bfd_link_hash_defined
3816
0
      || h->type == bfd_link_hash_defweak))
3817
0
    {
3818
0
      h->u.def.section->flags |= SEC_KEEP;
3819
0
    }
3820
3821
0
  for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3822
0
    {
3823
0
      sprintf (buf, "$tableentry$%d$%s", idx, tname);
3824
0
      h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3825
0
      if (h && (h->type == bfd_link_hash_defined
3826
0
    || h->type == bfd_link_hash_defweak))
3827
0
  {
3828
0
    h->u.def.section->flags |= SEC_KEEP;
3829
0
  }
3830
0
    }
3831
3832
  /* Return TRUE to keep scanning, FALSE to end the traversal.  */
3833
0
  return true;
3834
0
}
3835
3836
/* We need to check for table entry symbols and build the tables, and
3837
   we need to do it before the linker does garbage collection.  This function is
3838
   called once per input object file.  */
3839
static bool
3840
rx_check_directives
3841
    (bfd *           abfd ATTRIBUTE_UNUSED,
3842
     struct bfd_link_info *    info ATTRIBUTE_UNUSED)
3843
0
{
3844
0
  RX_Table_Info stuff;
3845
3846
0
  stuff.abfd = abfd;
3847
0
  stuff.info = info;
3848
0
  bfd_hash_traverse (&(info->hash->table), rx_table_find, &stuff);
3849
3850
0
  return true;
3851
0
}
3852
3853

3854
static bool
3855
rx_table_map_2 (struct bfd_hash_entry *vent, void *vinfo)
3856
0
{
3857
0
  RX_Table_Info *info = (RX_Table_Info *)vinfo;
3858
0
  struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3859
0
  int idx;
3860
0
  const char *name;
3861
0
  bfd_vma addr;
3862
3863
  /* See if the symbol ENT has an address listed in the table, and
3864
     isn't a debug/special symbol.  If so, put it in the table.  */
3865
3866
0
  if (ent->type != bfd_link_hash_defined
3867
0
      && ent->type != bfd_link_hash_defweak)
3868
0
    return true;
3869
3870
0
  name = ent->root.string;
3871
3872
0
  if (name[0] == '$' || name[0] == '.' || name[0] < ' ')
3873
0
    return true;
3874
3875
0
  addr = (ent->u.def.value
3876
0
    + ent->u.def.section->output_section->vma
3877
0
    + ent->u.def.section->output_offset);
3878
3879
0
  for (idx = 0; idx < info->table_size; idx ++)
3880
0
    if (addr == info->table_handlers[idx])
3881
0
      info->table_entries[idx] = ent;
3882
3883
0
  if (addr == info->table_default_handler)
3884
0
    info->table_default_entry = ent;
3885
3886
0
  return true;
3887
0
}
3888
3889
static bool
3890
rx_table_map (struct bfd_hash_entry *vent, void *vinfo)
3891
0
{
3892
0
  RX_Table_Info *info = (RX_Table_Info *)vinfo;
3893
0
  struct bfd_link_hash_entry *ent = (struct bfd_link_hash_entry *)vent;
3894
0
  const char *name; /* of the symbol we've found */
3895
0
  int idx;
3896
0
  const char *tname; /* name of the table */
3897
0
  bfd_vma start_addr, end_addr;
3898
0
  char *buf;
3899
0
  struct bfd_link_hash_entry * h;
3900
0
  int need_elipses;
3901
3902
  /* We're looking for globally defined symbols of the form
3903
     $tablestart$<NAME>.  */
3904
0
  if (ent->type != bfd_link_hash_defined
3905
0
      && ent->type != bfd_link_hash_defweak)
3906
0
    return true;
3907
3908
0
  name = ent->root.string;
3909
3910
0
  if (!startswith (name, "$tablestart$"))
3911
0
    return true;
3912
3913
0
  tname = name + 12;
3914
0
  start_addr = (ent->u.def.value
3915
0
    + ent->u.def.section->output_section->vma
3916
0
    + ent->u.def.section->output_offset);
3917
3918
0
  buf = (char *) bfd_malloc (12 + 10 + strlen (tname));
3919
0
  if (buf == NULL)
3920
0
    return false;
3921
3922
0
  sprintf (buf, "$tableend$%s", tname);
3923
0
  end_addr = get_symbol_value_maybe (buf, info->info);
3924
3925
0
  sprintf (buf, "$tableentry$default$%s", tname);
3926
0
  h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3927
0
  if (h)
3928
0
    {
3929
0
      info->table_default_handler = (h->u.def.value
3930
0
             + h->u.def.section->output_section->vma
3931
0
             + h->u.def.section->output_offset);
3932
0
    }
3933
0
  else
3934
    /* Zero is a valid handler address!  */
3935
0
    info->table_default_handler = (bfd_vma) (-1);
3936
0
  info->table_default_entry = NULL;
3937
3938
0
  info->table_start = start_addr;
3939
0
  info->table_size = (int) (end_addr - start_addr) / 4;
3940
0
  info->table_handlers = (bfd_vma *)
3941
0
    bfd_malloc (info->table_size * sizeof (bfd_vma));
3942
0
  if (info->table_handlers == NULL)
3943
0
    {
3944
0
      free (buf);
3945
0
      return false;
3946
0
    }
3947
0
  info->table_entries = (struct bfd_link_hash_entry **)
3948
0
    bfd_malloc (info->table_size * sizeof (struct bfd_link_hash_entry));
3949
0
  if (info->table_entries == NULL)
3950
0
    {
3951
0
      free (info->table_handlers);
3952
0
      free (buf);
3953
0
      return false;
3954
0
    }
3955
3956
0
  for (idx = 0; idx < (int) (end_addr - start_addr) / 4; idx ++)
3957
0
    {
3958
0
      sprintf (buf, "$tableentry$%d$%s", idx, tname);
3959
0
      h = bfd_link_hash_lookup (info->info->hash, buf, false, false, true);
3960
0
      if (h && (h->type == bfd_link_hash_defined
3961
0
    || h->type == bfd_link_hash_defweak))
3962
0
  {
3963
0
    info->table_handlers[idx] = (h->u.def.value
3964
0
               + h->u.def.section->output_section->vma
3965
0
               + h->u.def.section->output_offset);
3966
0
  }
3967
0
      else
3968
0
  info->table_handlers[idx] = info->table_default_handler;
3969
0
      info->table_entries[idx] = NULL;
3970
0
    }
3971
3972
0
  free (buf);
3973
3974
0
  bfd_hash_traverse (&(info->info->hash->table), rx_table_map_2, info);
3975
3976
0
  fprintf (info->mapfile,
3977
0
     "\nRX Vector Table: %s has %d entries at 0x%08" PRIx64 "\n\n",
3978
0
     tname, info->table_size, (uint64_t) start_addr);
3979
3980
0
  if (info->table_default_entry)
3981
0
    fprintf (info->mapfile, "  default handler is: %s at 0x%08" PRIx64 "\n",
3982
0
       info->table_default_entry->root.string,
3983
0
       (uint64_t) info->table_default_handler);
3984
0
  else if (info->table_default_handler != (bfd_vma)(-1))
3985
0
    fprintf (info->mapfile, "  default handler is at 0x%08" PRIx64 "\n",
3986
0
       (uint64_t) info->table_default_handler);
3987
0
  else
3988
0
    fprintf (info->mapfile, "  no default handler\n");
3989
3990
0
  need_elipses = 1;
3991
0
  for (idx = 0; idx < info->table_size; idx ++)
3992
0
    {
3993
0
      if (info->table_handlers[idx] == info->table_default_handler)
3994
0
  {
3995
0
    if (need_elipses)
3996
0
      fprintf (info->mapfile, "  . . .\n");
3997
0
    need_elipses = 0;
3998
0
    continue;
3999
0
  }
4000
0
      need_elipses = 1;
4001
4002
0
      fprintf (info->mapfile,
4003
0
         "  0x%08" PRIx64 " [%3d] ", (uint64_t) start_addr + 4 * idx, idx);
4004
4005
0
      if (info->table_handlers[idx] == (bfd_vma) (-1))
4006
0
  fprintf (info->mapfile, "(no handler found)\n");
4007
4008
0
      else if (info->table_handlers[idx] == info->table_default_handler)
4009
0
  {
4010
0
    if (info->table_default_entry)
4011
0
      fprintf (info->mapfile, "(default)\n");
4012
0
    else
4013
0
      fprintf (info->mapfile, "(default)\n");
4014
0
  }
4015
4016
0
      else if (info->table_entries[idx])
4017
0
  {
4018
0
    fprintf (info->mapfile, "0x%08" PRIx64 " %s\n",
4019
0
       (uint64_t) info->table_handlers[idx],
4020
0
       info->table_entries[idx]->root.string);
4021
0
  }
4022
4023
0
      else
4024
0
  {
4025
0
    fprintf (info->mapfile, "0x%08" PRIx64 " ???\n",
4026
0
       (uint64_t) info->table_handlers[idx]);
4027
0
  }
4028
0
    }
4029
0
  if (need_elipses)
4030
0
    fprintf (info->mapfile, "  . . .\n");
4031
4032
0
  return true;
4033
0
}
4034
4035
void
4036
rx_additional_link_map_text (bfd *obfd, struct bfd_link_info *info, FILE *mapfile)
4037
0
{
4038
  /* We scan the symbol table looking for $tableentry$'s, and for
4039
     each, try to deduce which handlers go with which entries.  */
4040
4041
0
  RX_Table_Info stuff;
4042
4043
0
  stuff.abfd = obfd;
4044
0
  stuff.info = info;
4045
0
  stuff.mapfile = mapfile;
4046
0
  bfd_hash_traverse (&(info->hash->table), rx_table_map, &stuff);
4047
0
}
4048
4049

4050
#define ELF_ARCH    bfd_arch_rx
4051
#define ELF_MACHINE_CODE  EM_RX
4052
#define ELF_MAXPAGESIZE   0x1000
4053
4054
#define TARGET_BIG_SYM    rx_elf32_be_vec
4055
#define TARGET_BIG_NAME   "elf32-rx-be"
4056
4057
#define TARGET_LITTLE_SYM rx_elf32_le_vec
4058
#define TARGET_LITTLE_NAME  "elf32-rx-le"
4059
4060
#define elf_info_to_howto_rel     NULL
4061
#define elf_info_to_howto     rx_info_to_howto_rela
4062
#define elf_backend_object_p      rx_elf_object_p
4063
#define elf_backend_relocate_section    rx_elf_relocate_section
4064
#define elf_symbol_leading_char     ('_')
4065
#define elf_backend_can_gc_sections   1
4066
#define elf_backend_modify_headers    elf32_rx_modify_headers
4067
4068
#define bfd_elf32_bfd_reloc_type_lookup   rx_reloc_type_lookup
4069
#define bfd_elf32_bfd_reloc_name_lookup   rx_reloc_name_lookup
4070
#define bfd_elf32_bfd_set_private_flags   rx_elf_set_private_flags
4071
#define bfd_elf32_bfd_merge_private_bfd_data  rx_elf_merge_private_bfd_data
4072
#define bfd_elf32_bfd_print_private_bfd_data  rx_elf_print_private_bfd_data
4073
#define bfd_elf32_get_section_contents    rx_get_section_contents
4074
#define bfd_elf32_set_section_contents    rx_set_section_contents
4075
#define bfd_elf32_bfd_final_link    rx_final_link
4076
#define bfd_elf32_bfd_relax_section   elf32_rx_relax_section_wrapper
4077
#define elf_backend_special_sections    elf32_rx_special_sections
4078
#define elf_backend_check_directives    rx_check_directives
4079
4080
#include "elf32-target.h"
4081
4082
/* We define a second big-endian target that doesn't have the custom
4083
   section get/set hooks, for times when we want to preserve the
4084
   pre-swapped .text sections (like objcopy).  */
4085
4086
#undef  TARGET_BIG_SYM
4087
#define TARGET_BIG_SYM    rx_elf32_be_ns_vec
4088
#undef  TARGET_BIG_NAME
4089
#define TARGET_BIG_NAME   "elf32-rx-be-ns"
4090
#undef  TARGET_LITTLE_SYM
4091
4092
#undef bfd_elf32_get_section_contents
4093
#undef bfd_elf32_set_section_contents
4094
4095
#undef  elf32_bed
4096
#define elf32_bed       elf32_rx_be_ns_bed
4097
4098
#include "elf32-target.h"
4099
4100
#undef  TARGET_LITTLE_SYM
4101
#define TARGET_LITTLE_SYM rx_elf32_linux_le_vec
4102
#undef  TARGET_LITTLE_NAME
4103
#define TARGET_LITTLE_NAME  "elf32-rx-linux"
4104
#undef  TARGET_BIG_SYM
4105
#undef  TARGET_BIG_NAME
4106
4107
#undef  elf_backend_object_p
4108
#define elf_backend_object_p      rx_linux_object_p
4109
#undef  elf_symbol_leading_char
4110
#undef  elf32_bed
4111
#define elf32_bed       elf32_rx_le_linux_bed
4112
4113
#include "elf32-target.h"