Coverage Report

Created: 2026-07-25 10:20

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