Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-avr.c
Line
Count
Source
1
/* AVR-specific support for 32-bit ELF
2
   Copyright (C) 1999-2026 Free Software Foundation, Inc.
3
   Contributed by Denis Chertykov <denisc@overta.ru>
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor,
20
   Boston, MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "libiberty.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "elf/avr.h"
28
#include "elf32-avr.h"
29
#include "libiberty.h"
30
31
/* Enable debugging printout at stdout with this variable.  */
32
static bool debug_relax = false;
33
34
/* Enable debugging printout at stdout with this variable.  */
35
static bool debug_stubs = false;
36
37
// When debug_relax is on, output a formatted message to stdout.
38
static void rout (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
39
static void rout (const char *fmt, ...)
40
0
{
41
0
  va_list args;
42
0
  va_start (args, fmt);
43
0
  if (debug_relax)
44
0
    {
45
0
      vprintf (fmt, args);
46
0
      fflush (stdout);
47
0
    }
48
0
  va_end (args);
49
0
}
50
51
52
static bfd_reloc_status_type
53
bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *,
54
      asection *, bfd *, char **);
55
56
/* Hash table initialization and handling.  Code is taken from the hppa port
57
   and adapted to the needs of AVR.  */
58
59
/* We use two hash tables to hold information for linking avr objects.
60
61
   The first is the elf32_avr_link_hash_table which is derived from the
62
   standard ELF linker hash table.  We use this as a place to attach the
63
   other hash table and some static information.
64
65
   The second is the stub hash table which is derived from the base BFD
66
   hash table.  The stub hash table holds the information on the linker
67
   stubs.  */
68
69
typedef struct
70
{
71
  /* Base hash table entry structure.  */
72
  struct bfd_hash_entry bh_root;
73
74
  /* Offset within stub_sec of the beginning of this stub.  */
75
  bfd_vma stub_offset;
76
77
  /* Given the symbol's value and its section we can determine its final
78
     value when building the stubs (so the stub knows where to jump).  */
79
  bfd_vma target_value;
80
81
  /* This way we could mark stubs to be no longer necessary.  */
82
  bool is_actually_needed;
83
} elf32_avr_stub_hash_entry_t;
84
85
typedef struct
86
{
87
  /* The main hash table.  */
88
  struct elf_link_hash_table etab;
89
90
  /* The stub hash table.  */
91
  struct bfd_hash_table bstab;
92
93
  bool no_stubs;
94
95
  /* Linker stub bfd.  */
96
  bfd *stub_bfd;
97
98
  /* The stub section.  */
99
  asection *stub_sec;
100
101
  /* Usually 0, unless we are generating code for a bootloader.  Will
102
     be initialized by elf32_avr_size_stubs to the vma offset of the
103
     output section associated with the stub section.  */
104
  bfd_vma vector_base;
105
106
  /* Assorted information used by elf32_avr_size_stubs.  */
107
  unsigned int bfd_count;
108
  unsigned int top_index;
109
  asection **input_list;
110
  Elf_Internal_Sym **all_local_syms;
111
112
  /* Tables for mapping vma beyond the 128k boundary to the address of the
113
     corresponding stub.  (AMT)
114
     "amt_max_entry_cnt" reflects the number of entries that memory is allocated
115
     for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
116
     "amt_entry_cnt" informs how many of these entries actually contain
117
     useful data.  */
118
  unsigned int amt_entry_cnt;
119
  unsigned int amt_max_entry_cnt;
120
  bfd_vma *amt_stub_offsets;
121
  bfd_vma *amt_destination_addr;
122
} elf32_avr_link_hash_table_t;
123
124
/* Various hash macros and functions.  */
125
#define avr_link_hash_table(p)            \
126
0
  ((is_elf_hash_table ((p)->hash)          \
127
0
    && elf_hash_table_id (elf_hash_table (p)) == AVR_ELF_DATA)   \
128
0
   ? (elf32_avr_link_hash_table_t *) (p)->hash : NULL)
129
130
#define avr_stub_hash_entry(ent) \
131
0
  ((elf32_avr_stub_hash_entry_t *)(ent))
132
133
#define avr_stub_hash_lookup(table, string, create, copy) \
134
0
  ((elf32_avr_stub_hash_entry_t *)        \
135
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
136
137
static reloc_howto_type elf_avr_howto_table[] =
138
{
139
  HOWTO (R_AVR_NONE,    /* type */
140
   0,     /* rightshift */
141
   0,     /* size */
142
   0,     /* bitsize */
143
   false,     /* pc_relative */
144
   0,     /* bitpos */
145
   complain_overflow_dont, /* complain_on_overflow */
146
   bfd_elf_generic_reloc, /* special_function */
147
   "R_AVR_NONE",    /* name */
148
   false,     /* partial_inplace */
149
   0,     /* src_mask */
150
   0,     /* dst_mask */
151
   false),    /* pcrel_offset */
152
153
  HOWTO (R_AVR_32,    /* type */
154
   0,     /* rightshift */
155
   4,     /* size */
156
   32,      /* bitsize */
157
   false,     /* pc_relative */
158
   0,     /* bitpos */
159
   complain_overflow_bitfield, /* complain_on_overflow */
160
   bfd_elf_generic_reloc, /* special_function */
161
   "R_AVR_32",    /* name */
162
   false,     /* partial_inplace */
163
   0xffffffff,    /* src_mask */
164
   0xffffffff,    /* dst_mask */
165
   false),    /* pcrel_offset */
166
167
  /* A 7 bit PC relative relocation.  */
168
  HOWTO (R_AVR_7_PCREL,   /* type */
169
   1,     /* rightshift */
170
   2,     /* size */
171
   7,     /* bitsize */
172
   true,      /* pc_relative */
173
   3,     /* bitpos */
174
   complain_overflow_bitfield, /* complain_on_overflow */
175
   bfd_elf_generic_reloc, /* special_function */
176
   "R_AVR_7_PCREL", /* name */
177
   false,     /* partial_inplace */
178
   0xffff,    /* src_mask */
179
   0xffff,    /* dst_mask */
180
   true),     /* pcrel_offset */
181
182
  /* A 13 bit PC relative relocation.  */
183
  HOWTO (R_AVR_13_PCREL,  /* type */
184
   1,     /* rightshift */
185
   2,     /* size */
186
   13,      /* bitsize */
187
   true,      /* pc_relative */
188
   0,     /* bitpos */
189
   complain_overflow_bitfield, /* complain_on_overflow */
190
   bfd_elf_generic_reloc, /* special_function */
191
   "R_AVR_13_PCREL",  /* name */
192
   false,     /* partial_inplace */
193
   0xfff,     /* src_mask */
194
   0xfff,     /* dst_mask */
195
   true),     /* pcrel_offset */
196
197
  /* A 16 bit absolute relocation.  */
198
  HOWTO (R_AVR_16,    /* type */
199
   0,     /* rightshift */
200
   2,     /* size */
201
   16,      /* bitsize */
202
   false,     /* pc_relative */
203
   0,     /* bitpos */
204
   complain_overflow_dont, /* complain_on_overflow */
205
   bfd_elf_generic_reloc, /* special_function */
206
   "R_AVR_16",    /* name */
207
   false,     /* partial_inplace */
208
   0xffff,    /* src_mask */
209
   0xffff,    /* dst_mask */
210
   false),    /* pcrel_offset */
211
212
  /* A 16 bit absolute relocation for command address
213
     Will be changed when linker stubs are needed.  */
214
  HOWTO (R_AVR_16_PM,   /* type */
215
   1,     /* rightshift */
216
   2,     /* size */
217
   16,      /* bitsize */
218
   false,     /* pc_relative */
219
   0,     /* bitpos */
220
   complain_overflow_bitfield, /* complain_on_overflow */
221
   bfd_elf_generic_reloc, /* special_function */
222
   "R_AVR_16_PM",   /* name */
223
   false,     /* partial_inplace */
224
   0xffff,    /* src_mask */
225
   0xffff,    /* dst_mask */
226
   false),    /* pcrel_offset */
227
  /* A low 8 bit absolute relocation of 16 bit address.
228
     For LDI command.  */
229
  HOWTO (R_AVR_LO8_LDI,   /* type */
230
   0,     /* rightshift */
231
   2,     /* size */
232
   8,     /* bitsize */
233
   false,     /* pc_relative */
234
   0,     /* bitpos */
235
   complain_overflow_dont, /* complain_on_overflow */
236
   bfd_elf_generic_reloc, /* special_function */
237
   "R_AVR_LO8_LDI", /* name */
238
   false,     /* partial_inplace */
239
   0xffff,    /* src_mask */
240
   0xffff,    /* dst_mask */
241
   false),    /* pcrel_offset */
242
  /* A high 8 bit absolute relocation of 16 bit address.
243
     For LDI command.  */
244
  HOWTO (R_AVR_HI8_LDI,   /* type */
245
   8,     /* rightshift */
246
   2,     /* size */
247
   8,     /* bitsize */
248
   false,     /* pc_relative */
249
   0,     /* bitpos */
250
   complain_overflow_dont, /* complain_on_overflow */
251
   bfd_elf_generic_reloc, /* special_function */
252
   "R_AVR_HI8_LDI", /* name */
253
   false,     /* partial_inplace */
254
   0xffff,    /* src_mask */
255
   0xffff,    /* dst_mask */
256
   false),    /* pcrel_offset */
257
  /* A high 6 bit absolute relocation of 22 bit address.
258
     For LDI command.  As well second most significant 8 bit value of
259
     a 32 bit link-time constant.  */
260
  HOWTO (R_AVR_HH8_LDI,   /* type */
261
   16,      /* rightshift */
262
   2,     /* size */
263
   8,     /* bitsize */
264
   false,     /* pc_relative */
265
   0,     /* bitpos */
266
   complain_overflow_dont, /* complain_on_overflow */
267
   bfd_elf_generic_reloc, /* special_function */
268
   "R_AVR_HH8_LDI", /* name */
269
   false,     /* partial_inplace */
270
   0xffff,    /* src_mask */
271
   0xffff,    /* dst_mask */
272
   false),    /* pcrel_offset */
273
  /* A negative low 8 bit absolute relocation of 16 bit address.
274
     For LDI command.  */
275
  HOWTO (R_AVR_LO8_LDI_NEG, /* type */
276
   0,     /* rightshift */
277
   2,     /* size */
278
   8,     /* bitsize */
279
   false,     /* pc_relative */
280
   0,     /* bitpos */
281
   complain_overflow_dont, /* complain_on_overflow */
282
   bfd_elf_generic_reloc, /* special_function */
283
   "R_AVR_LO8_LDI_NEG", /* name */
284
   false,     /* partial_inplace */
285
   0xffff,    /* src_mask */
286
   0xffff,    /* dst_mask */
287
   false),    /* pcrel_offset */
288
  /* A negative high 8 bit absolute relocation of 16 bit address.
289
     For LDI command.  */
290
  HOWTO (R_AVR_HI8_LDI_NEG, /* type */
291
   8,     /* rightshift */
292
   2,     /* size */
293
   8,     /* bitsize */
294
   false,     /* pc_relative */
295
   0,     /* bitpos */
296
   complain_overflow_dont, /* complain_on_overflow */
297
   bfd_elf_generic_reloc, /* special_function */
298
   "R_AVR_HI8_LDI_NEG", /* name */
299
   false,     /* partial_inplace */
300
   0xffff,    /* src_mask */
301
   0xffff,    /* dst_mask */
302
   false),    /* pcrel_offset */
303
  /* A negative high 6 bit absolute relocation of 22 bit address.
304
     For LDI command.  */
305
  HOWTO (R_AVR_HH8_LDI_NEG, /* type */
306
   16,      /* rightshift */
307
   2,     /* size */
308
   8,     /* bitsize */
309
   false,     /* pc_relative */
310
   0,     /* bitpos */
311
   complain_overflow_dont, /* complain_on_overflow */
312
   bfd_elf_generic_reloc, /* special_function */
313
   "R_AVR_HH8_LDI_NEG", /* name */
314
   false,     /* partial_inplace */
315
   0xffff,    /* src_mask */
316
   0xffff,    /* dst_mask */
317
   false),    /* pcrel_offset */
318
  /* A low 8 bit absolute relocation of 24 bit program memory address.
319
     For LDI command.  Will not be changed when linker stubs are needed. */
320
  HOWTO (R_AVR_LO8_LDI_PM,  /* type */
321
   1,     /* rightshift */
322
   2,     /* size */
323
   8,     /* bitsize */
324
   false,     /* pc_relative */
325
   0,     /* bitpos */
326
   complain_overflow_dont, /* complain_on_overflow */
327
   bfd_elf_generic_reloc, /* special_function */
328
   "R_AVR_LO8_LDI_PM",  /* name */
329
   false,     /* partial_inplace */
330
   0xffff,    /* src_mask */
331
   0xffff,    /* dst_mask */
332
   false),    /* pcrel_offset */
333
  /* A low 8 bit absolute relocation of 24 bit program memory address.
334
     For LDI command.  Will not be changed when linker stubs are needed. */
335
  HOWTO (R_AVR_HI8_LDI_PM,  /* type */
336
   9,     /* rightshift */
337
   2,     /* size */
338
   8,     /* bitsize */
339
   false,     /* pc_relative */
340
   0,     /* bitpos */
341
   complain_overflow_dont, /* complain_on_overflow */
342
   bfd_elf_generic_reloc, /* special_function */
343
   "R_AVR_HI8_LDI_PM",  /* name */
344
   false,     /* partial_inplace */
345
   0xffff,    /* src_mask */
346
   0xffff,    /* dst_mask */
347
   false),    /* pcrel_offset */
348
  /* A low 8 bit absolute relocation of 24 bit program memory address.
349
     For LDI command.  Will not be changed when linker stubs are needed. */
350
  HOWTO (R_AVR_HH8_LDI_PM,  /* type */
351
   17,      /* rightshift */
352
   2,     /* size */
353
   8,     /* bitsize */
354
   false,     /* pc_relative */
355
   0,     /* bitpos */
356
   complain_overflow_dont, /* complain_on_overflow */
357
   bfd_elf_generic_reloc, /* special_function */
358
   "R_AVR_HH8_LDI_PM",  /* name */
359
   false,     /* partial_inplace */
360
   0xffff,    /* src_mask */
361
   0xffff,    /* dst_mask */
362
   false),    /* pcrel_offset */
363
  /* A low 8 bit absolute relocation of 24 bit program memory address.
364
     For LDI command.  Will not be changed when linker stubs are needed. */
365
  HOWTO (R_AVR_LO8_LDI_PM_NEG,  /* type */
366
   1,     /* rightshift */
367
   2,     /* size */
368
   8,     /* bitsize */
369
   false,     /* pc_relative */
370
   0,     /* bitpos */
371
   complain_overflow_dont, /* complain_on_overflow */
372
   bfd_elf_generic_reloc, /* special_function */
373
   "R_AVR_LO8_LDI_PM_NEG", /* name */
374
   false,     /* partial_inplace */
375
   0xffff,    /* src_mask */
376
   0xffff,    /* dst_mask */
377
   false),    /* pcrel_offset */
378
  /* A low 8 bit absolute relocation of 24 bit program memory address.
379
     For LDI command.  Will not be changed when linker stubs are needed. */
380
  HOWTO (R_AVR_HI8_LDI_PM_NEG,  /* type */
381
   9,     /* rightshift */
382
   2,     /* size */
383
   8,     /* bitsize */
384
   false,     /* pc_relative */
385
   0,     /* bitpos */
386
   complain_overflow_dont, /* complain_on_overflow */
387
   bfd_elf_generic_reloc, /* special_function */
388
   "R_AVR_HI8_LDI_PM_NEG", /* name */
389
   false,     /* partial_inplace */
390
   0xffff,    /* src_mask */
391
   0xffff,    /* dst_mask */
392
   false),    /* pcrel_offset */
393
  /* A low 8 bit absolute relocation of 24 bit program memory address.
394
     For LDI command.  Will not be changed when linker stubs are needed. */
395
  HOWTO (R_AVR_HH8_LDI_PM_NEG,  /* type */
396
   17,      /* rightshift */
397
   2,     /* size */
398
   8,     /* bitsize */
399
   false,     /* pc_relative */
400
   0,     /* bitpos */
401
   complain_overflow_dont, /* complain_on_overflow */
402
   bfd_elf_generic_reloc, /* special_function */
403
   "R_AVR_HH8_LDI_PM_NEG", /* name */
404
   false,     /* partial_inplace */
405
   0xffff,    /* src_mask */
406
   0xffff,    /* dst_mask */
407
   false),    /* pcrel_offset */
408
  /* Relocation for CALL command in ATmega.  */
409
  HOWTO (R_AVR_CALL,    /* type */
410
   1,     /* rightshift */
411
   4,     /* size */
412
   23,      /* bitsize */
413
   false,     /* pc_relative */
414
   0,     /* bitpos */
415
   complain_overflow_dont,/* complain_on_overflow */
416
   bfd_elf_generic_reloc, /* special_function */
417
   "R_AVR_CALL",    /* name */
418
   false,     /* partial_inplace */
419
   0xffffffff,    /* src_mask */
420
   0xffffffff,    /* dst_mask */
421
   false),      /* pcrel_offset */
422
  /* A 16 bit absolute relocation of 16 bit address.
423
     For LDI command.  */
424
  HOWTO (R_AVR_LDI,   /* type */
425
   0,     /* rightshift */
426
   2,     /* size */
427
   16,      /* bitsize */
428
   false,     /* pc_relative */
429
   0,     /* bitpos */
430
   complain_overflow_dont,/* complain_on_overflow */
431
   bfd_elf_generic_reloc, /* special_function */
432
   "R_AVR_LDI",   /* name */
433
   false,     /* partial_inplace */
434
   0xffff,    /* src_mask */
435
   0xffff,    /* dst_mask */
436
   false),    /* pcrel_offset */
437
  /* A 6 bit absolute relocation of 6 bit offset.
438
     For ldd/sdd command.  */
439
  HOWTO (R_AVR_6,   /* type */
440
   0,     /* rightshift */
441
   1,     /* size */
442
   6,     /* bitsize */
443
   false,     /* pc_relative */
444
   0,     /* bitpos */
445
   complain_overflow_dont,/* complain_on_overflow */
446
   bfd_elf_generic_reloc, /* special_function */
447
   "R_AVR_6",   /* name */
448
   false,     /* partial_inplace */
449
   0xffff,    /* src_mask */
450
   0xffff,    /* dst_mask */
451
   false),    /* pcrel_offset */
452
  /* A 6 bit absolute relocation of 6 bit offset.
453
     For sbiw/adiw command.  */
454
  HOWTO (R_AVR_6_ADIW,    /* type */
455
   0,     /* rightshift */
456
   1,     /* size */
457
   6,     /* bitsize */
458
   false,     /* pc_relative */
459
   0,     /* bitpos */
460
   complain_overflow_dont,/* complain_on_overflow */
461
   bfd_elf_generic_reloc, /* special_function */
462
   "R_AVR_6_ADIW",  /* name */
463
   false,     /* partial_inplace */
464
   0xffff,    /* src_mask */
465
   0xffff,    /* dst_mask */
466
   false),    /* pcrel_offset */
467
  /* Most significant 8 bit value of a 32 bit link-time constant.  */
468
  HOWTO (R_AVR_MS8_LDI,   /* type */
469
   24,      /* rightshift */
470
   2,     /* size */
471
   8,     /* bitsize */
472
   false,     /* pc_relative */
473
   0,     /* bitpos */
474
   complain_overflow_dont, /* complain_on_overflow */
475
   bfd_elf_generic_reloc, /* special_function */
476
   "R_AVR_MS8_LDI", /* name */
477
   false,     /* partial_inplace */
478
   0xffff,    /* src_mask */
479
   0xffff,    /* dst_mask */
480
   false),    /* pcrel_offset */
481
  /* Negative most significant 8 bit value of a 32 bit link-time constant.  */
482
  HOWTO (R_AVR_MS8_LDI_NEG, /* type */
483
   24,      /* rightshift */
484
   2,     /* size */
485
   8,     /* bitsize */
486
   false,     /* pc_relative */
487
   0,     /* bitpos */
488
   complain_overflow_dont, /* complain_on_overflow */
489
   bfd_elf_generic_reloc, /* special_function */
490
   "R_AVR_MS8_LDI_NEG", /* name */
491
   false,     /* partial_inplace */
492
   0xffff,    /* src_mask */
493
   0xffff,    /* dst_mask */
494
   false),    /* pcrel_offset */
495
  /* A low 8 bit absolute relocation of 24 bit program memory address.
496
     For LDI command.  Will be changed when linker stubs are needed.  */
497
  HOWTO (R_AVR_LO8_LDI_GS,  /* type */
498
   1,     /* rightshift */
499
   2,     /* size */
500
   8,     /* bitsize */
501
   false,     /* pc_relative */
502
   0,     /* bitpos */
503
   complain_overflow_dont, /* complain_on_overflow */
504
   bfd_elf_generic_reloc, /* special_function */
505
   "R_AVR_LO8_LDI_GS",  /* name */
506
   false,     /* partial_inplace */
507
   0xffff,    /* src_mask */
508
   0xffff,    /* dst_mask */
509
   false),    /* pcrel_offset */
510
  /* A low 8 bit absolute relocation of 24 bit program memory address.
511
     For LDI command.  Will be changed when linker stubs are needed.  */
512
  HOWTO (R_AVR_HI8_LDI_GS,  /* type */
513
   9,     /* rightshift */
514
   2,     /* size */
515
   8,     /* bitsize */
516
   false,     /* pc_relative */
517
   0,     /* bitpos */
518
   complain_overflow_dont, /* complain_on_overflow */
519
   bfd_elf_generic_reloc, /* special_function */
520
   "R_AVR_HI8_LDI_GS",  /* name */
521
   false,     /* partial_inplace */
522
   0xffff,    /* src_mask */
523
   0xffff,    /* dst_mask */
524
   false),    /* pcrel_offset */
525
  /* 8 bit offset.  */
526
  HOWTO (R_AVR_8,   /* type */
527
   0,     /* rightshift */
528
   1,     /* size */
529
   8,     /* bitsize */
530
   false,     /* pc_relative */
531
   0,     /* bitpos */
532
   complain_overflow_bitfield,/* complain_on_overflow */
533
   bfd_elf_generic_reloc, /* special_function */
534
   "R_AVR_8",   /* name */
535
   false,     /* partial_inplace */
536
   0x000000ff,    /* src_mask */
537
   0x000000ff,    /* dst_mask */
538
   false),    /* pcrel_offset */
539
  /* lo8-part to use in  .byte lo8(sym).  */
540
  HOWTO (R_AVR_8_LO8,   /* type */
541
   0,     /* rightshift */
542
   1,     /* size */
543
   8,     /* bitsize */
544
   false,     /* pc_relative */
545
   0,     /* bitpos */
546
   complain_overflow_dont,/* complain_on_overflow */
547
   bfd_elf_generic_reloc, /* special_function */
548
   "R_AVR_8_LO8",   /* name */
549
   false,     /* partial_inplace */
550
   0xffffff,    /* src_mask */
551
   0xffffff,    /* dst_mask */
552
   false),    /* pcrel_offset */
553
  /* hi8-part to use in  .byte hi8(sym).  */
554
  HOWTO (R_AVR_8_HI8,   /* type */
555
   8,     /* rightshift */
556
   1,     /* size */
557
   8,     /* bitsize */
558
   false,     /* pc_relative */
559
   0,     /* bitpos */
560
   complain_overflow_dont,/* complain_on_overflow */
561
   bfd_elf_generic_reloc, /* special_function */
562
   "R_AVR_8_HI8",   /* name */
563
   false,     /* partial_inplace */
564
   0xffffff,    /* src_mask */
565
   0xffffff,    /* dst_mask */
566
   false),    /* pcrel_offset */
567
  /* hlo8-part to use in  .byte hlo8(sym).  */
568
  HOWTO (R_AVR_8_HLO8,    /* type */
569
   16,      /* rightshift */
570
   1,     /* size */
571
   8,     /* bitsize */
572
   false,     /* pc_relative */
573
   0,     /* bitpos */
574
   complain_overflow_dont,/* complain_on_overflow */
575
   bfd_elf_generic_reloc, /* special_function */
576
   "R_AVR_8_HLO8",  /* name */
577
   false,     /* partial_inplace */
578
   0xffffff,    /* src_mask */
579
   0xffffff,    /* dst_mask */
580
   false),    /* pcrel_offset */
581
  HOWTO (R_AVR_DIFF8,   /* type */
582
   0,     /* rightshift */
583
   1,     /* size */
584
   8,     /* bitsize */
585
   false,     /* pc_relative */
586
   0,     /* bitpos */
587
   complain_overflow_bitfield, /* complain_on_overflow */
588
   bfd_elf_avr_diff_reloc, /* special_function */
589
   "R_AVR_DIFF8",   /* name */
590
   false,     /* partial_inplace */
591
   0,     /* src_mask */
592
   0xff,      /* dst_mask */
593
   false),    /* pcrel_offset */
594
  HOWTO (R_AVR_DIFF16,    /* type */
595
   0,     /* rightshift */
596
   2,     /* size */
597
   16,      /* bitsize */
598
   false,     /* pc_relative */
599
   0,     /* bitpos */
600
   complain_overflow_bitfield, /* complain_on_overflow */
601
   bfd_elf_avr_diff_reloc,/* special_function */
602
   "R_AVR_DIFF16",  /* name */
603
   false,     /* partial_inplace */
604
   0,     /* src_mask */
605
   0xffff,    /* dst_mask */
606
   false),    /* pcrel_offset */
607
  HOWTO (R_AVR_DIFF32,    /* type */
608
   0,     /* rightshift */
609
   4,     /* size */
610
   32,      /* bitsize */
611
   false,     /* pc_relative */
612
   0,     /* bitpos */
613
   complain_overflow_bitfield, /* complain_on_overflow */
614
   bfd_elf_avr_diff_reloc,/* special_function */
615
   "R_AVR_DIFF32",  /* name */
616
   false,     /* partial_inplace */
617
   0,     /* src_mask */
618
   0xffffffff,    /* dst_mask */
619
   false),    /* pcrel_offset */
620
  /* 7 bit immediate for LDS/STS in Tiny core.  */
621
  HOWTO (R_AVR_LDS_STS_16,  /* type */
622
   0,     /* rightshift */
623
   2,     /* size */
624
   7,     /* bitsize */
625
   false,     /* pc_relative */
626
   0,     /* bitpos */
627
   complain_overflow_dont,/* complain_on_overflow */
628
   bfd_elf_generic_reloc, /* special_function */
629
   "R_AVR_LDS_STS_16",  /* name */
630
   false,     /* partial_inplace */
631
   0xffff,    /* src_mask */
632
   0xffff,    /* dst_mask */
633
   false),    /* pcrel_offset */
634
635
  HOWTO (R_AVR_PORT6,   /* type */
636
   0,     /* rightshift */
637
   1,     /* size */
638
   6,     /* bitsize */
639
   false,     /* pc_relative */
640
   0,     /* bitpos */
641
   complain_overflow_dont,/* complain_on_overflow */
642
   bfd_elf_generic_reloc, /* special_function */
643
   "R_AVR_PORT6",   /* name */
644
   false,     /* partial_inplace */
645
   0xffffff,    /* src_mask */
646
   0xffffff,    /* dst_mask */
647
   false),    /* pcrel_offset */
648
  HOWTO (R_AVR_PORT5,   /* type */
649
   0,     /* rightshift */
650
   1,     /* size */
651
   5,     /* bitsize */
652
   false,     /* pc_relative */
653
   0,     /* bitpos */
654
   complain_overflow_dont,/* complain_on_overflow */
655
   bfd_elf_generic_reloc, /* special_function */
656
   "R_AVR_PORT5",   /* name */
657
   false,     /* partial_inplace */
658
   0xffffff,    /* src_mask */
659
   0xffffff,    /* dst_mask */
660
   false),    /* pcrel_offset */
661
662
  /* A 32 bit PC relative relocation.  */
663
  HOWTO (R_AVR_32_PCREL,  /* type */
664
   0,     /* rightshift */
665
   4,     /* size */
666
   32,      /* bitsize */
667
   true,      /* pc_relative */
668
   0,     /* bitpos */
669
   complain_overflow_bitfield, /* complain_on_overflow */
670
   bfd_elf_generic_reloc, /* special_function */
671
   "R_AVR_32_PCREL",  /* name */
672
   false,     /* partial_inplace */
673
   0xffffffff,    /* src_mask */
674
   0xffffffff,    /* dst_mask */
675
   true),     /* pcrel_offset */
676
};
677
678
/* Map BFD reloc types to AVR ELF reloc types.  */
679
680
typedef struct
681
{
682
  bfd_reloc_code_real_type bfd_reloc_val;
683
  unsigned int elf_reloc_val;
684
} avr_reloc_map_t;
685
686
static const avr_reloc_map_t avr_reloc_map[] =
687
{
688
  { BFD_RELOC_NONE,       R_AVR_NONE },
689
  { BFD_RELOC_32,       R_AVR_32 },
690
  { BFD_RELOC_AVR_7_PCREL,      R_AVR_7_PCREL },
691
  { BFD_RELOC_AVR_13_PCREL,     R_AVR_13_PCREL },
692
  { BFD_RELOC_16,       R_AVR_16 },
693
  { BFD_RELOC_AVR_16_PM,      R_AVR_16_PM },
694
  { BFD_RELOC_AVR_LO8_LDI,      R_AVR_LO8_LDI},
695
  { BFD_RELOC_AVR_HI8_LDI,      R_AVR_HI8_LDI },
696
  { BFD_RELOC_AVR_HH8_LDI,      R_AVR_HH8_LDI },
697
  { BFD_RELOC_AVR_MS8_LDI,      R_AVR_MS8_LDI },
698
  { BFD_RELOC_AVR_LO8_LDI_NEG,      R_AVR_LO8_LDI_NEG },
699
  { BFD_RELOC_AVR_HI8_LDI_NEG,      R_AVR_HI8_LDI_NEG },
700
  { BFD_RELOC_AVR_HH8_LDI_NEG,      R_AVR_HH8_LDI_NEG },
701
  { BFD_RELOC_AVR_MS8_LDI_NEG,      R_AVR_MS8_LDI_NEG },
702
  { BFD_RELOC_AVR_LO8_LDI_PM,     R_AVR_LO8_LDI_PM },
703
  { BFD_RELOC_AVR_LO8_LDI_GS,     R_AVR_LO8_LDI_GS },
704
  { BFD_RELOC_AVR_HI8_LDI_PM,     R_AVR_HI8_LDI_PM },
705
  { BFD_RELOC_AVR_HI8_LDI_GS,     R_AVR_HI8_LDI_GS },
706
  { BFD_RELOC_AVR_HH8_LDI_PM,     R_AVR_HH8_LDI_PM },
707
  { BFD_RELOC_AVR_LO8_LDI_PM_NEG,   R_AVR_LO8_LDI_PM_NEG },
708
  { BFD_RELOC_AVR_HI8_LDI_PM_NEG,   R_AVR_HI8_LDI_PM_NEG },
709
  { BFD_RELOC_AVR_HH8_LDI_PM_NEG,   R_AVR_HH8_LDI_PM_NEG },
710
  { BFD_RELOC_AVR_CALL,       R_AVR_CALL },
711
  { BFD_RELOC_AVR_LDI,        R_AVR_LDI },
712
  { BFD_RELOC_AVR_6,        R_AVR_6 },
713
  { BFD_RELOC_AVR_6_ADIW,     R_AVR_6_ADIW },
714
  { BFD_RELOC_8,        R_AVR_8 },
715
  { BFD_RELOC_AVR_8_LO,       R_AVR_8_LO8 },
716
  { BFD_RELOC_AVR_8_HI,       R_AVR_8_HI8 },
717
  { BFD_RELOC_AVR_8_HLO,      R_AVR_8_HLO8 },
718
  { BFD_RELOC_AVR_DIFF8,      R_AVR_DIFF8 },
719
  { BFD_RELOC_AVR_DIFF16,     R_AVR_DIFF16 },
720
  { BFD_RELOC_AVR_DIFF32,     R_AVR_DIFF32 },
721
  { BFD_RELOC_AVR_LDS_STS_16,     R_AVR_LDS_STS_16 },
722
  { BFD_RELOC_AVR_PORT6,      R_AVR_PORT6 },
723
  { BFD_RELOC_AVR_PORT5,      R_AVR_PORT5 },
724
  { BFD_RELOC_32_PCREL,       R_AVR_32_PCREL }
725
};
726
727
static const struct bfd_elf_special_section elf_avr_special_sections[] =
728
{
729
  { STRING_COMMA_LEN (".noinit"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
730
  { NULL, 0,        0, 0,      0 }
731
};
732
733
/* Meant to be filled one day with the wrap around address for the
734
   specific device.  I.e. should get the value 0x4000 for 16k devices,
735
   0x8000 for 32k devices and so on.
736
737
   We initialize it here with a value of 0x1000000 resulting in
738
   that we will never suggest a wrap-around jump during relaxation.
739
   The logic of the source code later on assumes that in
740
   avr_pc_wrap_around one single bit is set.  */
741
static bfd_vma avr_pc_wrap_around = 0x10000000;
742
743
/* If this variable holds a value different from zero, the linker relaxation
744
   machine will try to optimize CALL/RET sequences by a single jump
745
   instruction. This can be switched off by --no-call-ret-replacement.  */
746
static bool avr_replace_call_ret_sequences = true;
747
748
/* If this variable holds true, the linker relaxation machine will
749
   try to remove RJMP instructions that are void.  This does not
750
   include plain RJMP .+0 which is used by GCC to delay 2 cycles.
751
   This can be switched off by --no-elide-rjmp0.  */
752
static bool avr_elide_rjmp0 = true;
753
754
/* Prune the vector table provided --relax.  */
755
static bool avr_prune_vectab = false;
756

757
758
/* Per-section relaxation related information for avr.  */
759
760
typedef struct
761
{
762
  /* Track the avr property records that apply to this section.  */
763
764
  struct
765
  {
766
    /* Number of records in the list.  */
767
    unsigned count;
768
769
    /* How many records worth of space have we allocated.  */
770
    unsigned allocated;
771
772
    /* The records, only COUNT records are initialised.  */
773
    struct avr_property_record *items;
774
  } records;
775
} avr_relax_info_t;
776
777
/* Per section data, specialised for avr.  */
778
779
typedef struct
780
{
781
  /* The standard data must appear first.  */
782
  struct bfd_elf_section_data elf;
783
784
  /* Relaxation related information.  */
785
  avr_relax_info_t relax_info;
786
} elf_avr_section_data_t;
787
788
/* Possibly initialise avr specific data for new section SEC from ABFD.  */
789
790
static bool
791
elf_avr_new_section_hook (bfd *abfd, asection *sec)
792
677
{
793
677
  elf_avr_section_data_t *sdata;
794
795
677
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
796
677
  if (sdata == NULL)
797
0
    return false;
798
677
  sec->used_by_bfd = sdata;
799
800
677
  return _bfd_elf_new_section_hook (abfd, sec);
801
677
}
802
803
/* Return a pointer to the relaxation information for SEC.  */
804
805
static avr_relax_info_t *
806
get_avr_relax_info (asection *sec)
807
0
{
808
0
  elf_avr_section_data_t *section_data;
809
810
  /* No info available if no section or if it is an output section.  */
811
0
  if (!sec || sec == sec->output_section)
812
0
    return NULL;
813
814
0
  section_data = (elf_avr_section_data_t *) elf_section_data (sec);
815
0
  return &section_data->relax_info;
816
0
}
817
818
/* Initialise the per section relaxation information for SEC.  */
819
820
static void
821
init_avr_relax_info (asection *sec)
822
0
{
823
0
  avr_relax_info_t *relax_info = get_avr_relax_info (sec);
824
825
0
  relax_info->records.count = 0;
826
0
  relax_info->records.allocated = 0;
827
0
  relax_info->records.items = NULL;
828
0
}
829
830
/* Initialize an entry in the stub hash table.  */
831
832
static struct bfd_hash_entry *
833
stub_hash_newfunc (struct bfd_hash_entry *entry,
834
       struct bfd_hash_table *table, const char *string)
835
0
{
836
  /* Allocate the structure if it has not already been allocated by a
837
     subclass.  */
838
0
  if (entry == NULL)
839
0
    {
840
0
      entry = bfd_hash_allocate (table, sizeof (elf32_avr_stub_hash_entry_t));
841
0
      if (entry == NULL)
842
0
  return entry;
843
0
    }
844
845
  /* Call the allocation method of the superclass.  */
846
0
  entry = bfd_hash_newfunc (entry, table, string);
847
0
  if (entry != NULL)
848
0
    {
849
0
      elf32_avr_stub_hash_entry_t *hsh;
850
851
      /* Initialize the local fields.  */
852
0
      hsh = avr_stub_hash_entry (entry);
853
0
      hsh->stub_offset = 0;
854
0
      hsh->target_value = 0;
855
0
    }
856
857
0
  return entry;
858
0
}
859
860
/* This function is just a straight passthrough to the real
861
   function in linker.c.  Its purpose is so that its address
862
   can be compared inside the avr_link_hash_table macro.  */
863
864
static struct bfd_hash_entry *
865
elf32_avr_link_hash_newfunc (struct bfd_hash_entry *entry,
866
           struct bfd_hash_table *table, const char *string)
867
0
{
868
0
  return _bfd_elf_link_hash_newfunc (entry, table, string);
869
0
}
870
871
/* Free the derived linker hash table.  */
872
873
static void
874
elf32_avr_link_hash_table_free (bfd *obfd)
875
0
{
876
0
  elf32_avr_link_hash_table_t *htab
877
0
    = (elf32_avr_link_hash_table_t *) obfd->link.hash;
878
879
  /* Free the address mapping table.  */
880
0
  free (htab->amt_stub_offsets);
881
0
  free (htab->amt_destination_addr);
882
883
0
  bfd_hash_table_free (&htab->bstab);
884
0
  _bfd_elf_link_hash_table_free (obfd);
885
0
}
886
887
/* Create the derived linker hash table.  The AVR ELF port uses the derived
888
   hash table to keep information specific to the AVR ELF linker (without
889
   using static variables).  */
890
891
static struct bfd_link_hash_table *
892
elf32_avr_link_hash_table_create (bfd *abfd)
893
0
{
894
0
  elf32_avr_link_hash_table_t *htab;
895
0
  size_t amt = sizeof (*htab);
896
897
0
  htab = bfd_zmalloc (amt);
898
0
  if (htab == NULL)
899
0
    return NULL;
900
901
0
  if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
902
0
              elf32_avr_link_hash_newfunc,
903
0
              sizeof (struct elf_link_hash_entry)))
904
0
    {
905
0
      free (htab);
906
0
      return NULL;
907
0
    }
908
909
  /* Init the stub hash table too.  */
910
0
  if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
911
0
          sizeof (elf32_avr_stub_hash_entry_t)))
912
0
    {
913
0
      _bfd_elf_link_hash_table_free (abfd);
914
0
      return NULL;
915
0
    }
916
0
  htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
917
918
0
  return &htab->etab.root;
919
0
}
920
921
/* Calculates the effective distance of a PC-relative jump/call.  */
922
923
static int
924
avr_relative_distance_considering_wrap_around (unsigned int distance)
925
0
{
926
0
  unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
927
0
  int dist_with_wrap_around = distance & wrap_around_mask;
928
929
0
  if (dist_with_wrap_around >= ((int) (avr_pc_wrap_around >> 1)))
930
0
    dist_with_wrap_around -= avr_pc_wrap_around;
931
932
0
  return dist_with_wrap_around;
933
0
}
934
935
936
static reloc_howto_type *
937
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
938
         bfd_reloc_code_real_type code)
939
0
{
940
0
  for (size_t i = 0; i < ARRAY_SIZE (avr_reloc_map); ++i)
941
0
    if (avr_reloc_map[i].bfd_reloc_val == code)
942
0
      return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
943
944
0
  return NULL;
945
0
}
946
947
static reloc_howto_type *
948
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
949
         const char *r_name)
950
0
{
951
0
  for (size_t i = 0; i < ARRAY_SIZE (elf_avr_howto_table); ++i)
952
0
    if (elf_avr_howto_table[i].name != NULL
953
0
  && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
954
0
      return &elf_avr_howto_table[i];
955
956
0
  return NULL;
957
0
}
958
959
/* Set the howto pointer for an AVR ELF reloc.  */
960
961
static bool
962
avr_info_to_howto_rela (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
963
364
{
964
364
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
965
966
364
  if (r_type >= (unsigned int) R_AVR_max)
967
20
    {
968
      /* xgettext:c-format */
969
20
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
970
20
        abfd, r_type);
971
20
      bfd_set_error (bfd_error_bad_value);
972
20
      return false;
973
20
    }
974
344
  cache_ptr->howto = &elf_avr_howto_table[r_type];
975
344
  return true;
976
364
}
977
978
979
static uint16_t
980
avr_word (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *addr)
981
0
{
982
0
  const uint8_t lsb = bfd_get_8 (abfd, addr);
983
0
  const uint8_t msb = bfd_get_8 (abfd, addr + 1);
984
0
  return 0x100 * msb + lsb;
985
0
}
986
987
988
/* Return true iff W is a CALL instruction.  */
989
990
static bool
991
avr_is_CALL (uint16_t w)
992
0
{
993
0
  return 0x940e == (w & 0xfe0e);
994
0
}
995
996
/* Return true iff W is a JMP instruction.  */
997
998
static bool
999
avr_is_JMP (uint16_t w)
1000
0
{
1001
0
  return 0x940c == (w & 0xfe0e);
1002
0
}
1003
1004
/* Return true iff W is a RCALL instruction.  */
1005
1006
static bool
1007
avr_is_RCALL (uint16_t w)
1008
0
{
1009
0
  return 0xd000 == (w & 0xf000);
1010
0
}
1011
1012
/* Return true iff W is a RJMP instruction.  */
1013
1014
static bool
1015
avr_is_RJMP (uint16_t w)
1016
0
{
1017
0
  return 0xc000 == (w & 0xf000);
1018
0
}
1019
1020
/* Return true iff W is a CLH instruction.  */
1021
1022
static bool
1023
avr_is_CLH (uint16_t w)
1024
0
{
1025
0
  return 0x94d8 == w;
1026
0
}
1027
1028
/* Return true iff W is a RET instruction.  */
1029
1030
static bool
1031
avr_is_RET (uint16_t w)
1032
0
{
1033
0
  return 0x9508 == w;
1034
0
}
1035
1036
1037
/* Return true iff W is one of the five AVR skip instructions
1038
   SBIC, SBIS, SBRC, SBRS and CPSE.  */
1039
1040
static bool
1041
avr_is_skip (uint16_t w)
1042
0
{
1043
  // SBIC
1044
0
  bool skip = 0x9900 == (w & 0xff00);
1045
1046
  // SBIS
1047
0
  skip |= 0x9b00 == (w & 0xff00);
1048
1049
  // SBRC
1050
0
  skip |= 0xfc00 == (w & 0xfe08);
1051
1052
  // SBRC
1053
0
  skip |= 0xfe00 == (w & 0xfe08);
1054
1055
  // CPSE
1056
0
  skip |= 0x1000 == (w & 0xfc00);
1057
1058
0
  return skip;
1059
0
}
1060
1061
static bool
1062
avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
1063
0
{
1064
0
  return relocation >= 0x020000;
1065
0
}
1066
1067
/* Returns the address of the corresponding stub if there is one.
1068
   Returns otherwise an address above 0x020000.  This function
1069
   could also be used, if there is no knowledge on the section where
1070
   the destination is found.  */
1071
1072
static bfd_vma
1073
avr_get_stub_addr (bfd_vma srel, elf32_avr_link_hash_table_t *htab)
1074
0
{
1075
0
  bfd_vma stub_sec_addr
1076
0
    = htab->stub_sec->output_section->vma + htab->stub_sec->output_offset;
1077
1078
0
  for (unsigned int sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
1079
0
    if (htab->amt_destination_addr[sindex] == srel)
1080
0
      return htab->amt_stub_offsets[sindex] + stub_sec_addr;
1081
1082
  /* Return an address that could not be reached by 16 bit relocs.  */
1083
0
  return 0x020000;
1084
0
}
1085
1086
/* Perform a diff relocation. Nothing to do, as the difference value is already
1087
   written into the section's contents. */
1088
1089
static bfd_reloc_status_type
1090
bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1091
      arelent *reloc_entry ATTRIBUTE_UNUSED,
1092
      asymbol *symbol ATTRIBUTE_UNUSED,
1093
      void *data ATTRIBUTE_UNUSED,
1094
      asection *input_section ATTRIBUTE_UNUSED,
1095
      bfd *output_bfd ATTRIBUTE_UNUSED,
1096
      char **error_message ATTRIBUTE_UNUSED)
1097
33
{
1098
33
  return bfd_reloc_ok;
1099
33
}
1100
1101
1102
/* Perform a single relocation.  By default we use the standard BFD
1103
   routines, but a few relocs we have to do ourselves.  */
1104
1105
static bfd_reloc_status_type
1106
avr_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
1107
       asection *input_section, bfd_byte *contents,
1108
       Elf_Internal_Rela *rel, bfd_vma relocation,
1109
       elf32_avr_link_hash_table_t *htab)
1110
0
{
1111
0
  bfd_reloc_status_type r = bfd_reloc_ok;
1112
0
  bfd_vma x;
1113
0
  bfd_signed_vma srel;
1114
0
  bool use_stubs = false;
1115
1116
  /* Usually is 0, unless we are generating code for a bootloader.  */
1117
0
  bfd_signed_vma base_addr = htab->vector_base;
1118
1119
  /* Absolute address of the reloc in the final executable.  */
1120
0
  bfd_signed_vma reloc_addr = (rel->r_offset
1121
0
             + input_section->output_section->vma
1122
0
             + input_section->output_offset);
1123
0
  switch (howto->type)
1124
0
    {
1125
0
    case R_AVR_7_PCREL:
1126
0
      contents += rel->r_offset;
1127
0
      srel = (bfd_signed_vma) relocation;
1128
0
      srel += rel->r_addend;
1129
0
      srel -= rel->r_offset;
1130
0
      srel -= 2;  /* Branch instructions add 2 to the PC...  */
1131
0
      srel -= (input_section->output_section->vma
1132
0
         + input_section->output_offset);
1133
1134
0
      if (srel & 1)
1135
0
  return bfd_reloc_other;
1136
0
      if (srel > ((1 << 7) - 1) || srel < - (1 << 7))
1137
0
  return bfd_reloc_overflow;
1138
0
      x = bfd_get_16 (input_bfd, contents);
1139
0
      x = (x & 0xfc07) | (((srel >> 1) * 8) & 0x3f8);
1140
0
      bfd_put_16 (input_bfd, x, contents);
1141
0
      break;
1142
1143
0
    case R_AVR_13_PCREL:
1144
0
      contents   += rel->r_offset;
1145
0
      srel = (bfd_signed_vma) relocation;
1146
0
      srel += rel->r_addend;
1147
0
      srel -= rel->r_offset;
1148
0
      srel -= 2;  /* Branch instructions add 2 to the PC...  */
1149
0
      srel -= (input_section->output_section->vma
1150
0
         + input_section->output_offset);
1151
1152
0
      if (srel & 1)
1153
0
  return bfd_reloc_other;
1154
1155
0
      srel = avr_relative_distance_considering_wrap_around (srel);
1156
1157
      /* AVR addresses commands as words.  */
1158
0
      srel >>= 1;
1159
1160
      /* Check for overflow.  */
1161
0
      if (srel < -2048 || srel > 2047)
1162
0
  {
1163
    /* Relative distance is too large.  */
1164
1165
    /* Always apply WRAPAROUND for avr2, avr25, and avr4.  */
1166
0
    switch (bfd_get_mach (input_bfd))
1167
0
      {
1168
0
      case bfd_mach_avr2:
1169
0
      case bfd_mach_avr25:
1170
0
      case bfd_mach_avr4:
1171
0
        break;
1172
1173
0
      default:
1174
0
        return bfd_reloc_overflow;
1175
0
      }
1176
0
  }
1177
1178
0
      x = bfd_get_16 (input_bfd, contents);
1179
0
      x = (x & 0xf000) | (srel & 0xfff);
1180
0
      bfd_put_16 (input_bfd, x, contents);
1181
0
      break;
1182
1183
0
    case R_AVR_LO8_LDI:
1184
0
      contents += rel->r_offset;
1185
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1186
0
      x = bfd_get_16 (input_bfd, contents);
1187
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1188
0
      bfd_put_16 (input_bfd, x, contents);
1189
0
      break;
1190
1191
0
    case R_AVR_LDI:
1192
0
      contents += rel->r_offset;
1193
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1194
0
      if ((srel > 0 && (srel & 0xffff) > 255)
1195
0
    || (srel < 0 && ((-srel) & 0xffff) > 128))
1196
  /* Remove offset for data/eeprom section.  */
1197
0
  return bfd_reloc_overflow;
1198
1199
0
      x = bfd_get_16 (input_bfd, contents);
1200
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1201
0
      bfd_put_16 (input_bfd, x, contents);
1202
0
      break;
1203
1204
0
    case R_AVR_6:
1205
0
      contents += rel->r_offset;
1206
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1207
0
      if ((srel & 0xffff) > 63 || srel < 0)
1208
  /* Remove offset for data/eeprom section.  */
1209
0
  return bfd_reloc_overflow;
1210
0
      x = bfd_get_16 (input_bfd, contents);
1211
0
      x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
1212
0
           | ((srel & (1 << 5)) << 8));
1213
0
      bfd_put_16 (input_bfd, x, contents);
1214
0
      break;
1215
1216
0
    case R_AVR_6_ADIW:
1217
0
      contents += rel->r_offset;
1218
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1219
0
      if ((srel & 0xffff) > 63 || srel < 0)
1220
  /* Remove offset for data/eeprom section.  */
1221
0
  return bfd_reloc_overflow;
1222
0
      x = bfd_get_16 (input_bfd, contents);
1223
0
      x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
1224
0
      bfd_put_16 (input_bfd, x, contents);
1225
0
      break;
1226
1227
0
    case R_AVR_HI8_LDI:
1228
0
      contents += rel->r_offset;
1229
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1230
0
      srel = (srel >> 8) & 0xff;
1231
0
      x = bfd_get_16 (input_bfd, contents);
1232
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1233
0
      bfd_put_16 (input_bfd, x, contents);
1234
0
      break;
1235
1236
0
    case R_AVR_HH8_LDI:
1237
0
      contents += rel->r_offset;
1238
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1239
0
      srel = (srel >> 16) & 0xff;
1240
0
      x = bfd_get_16 (input_bfd, contents);
1241
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1242
0
      bfd_put_16 (input_bfd, x, contents);
1243
0
      break;
1244
1245
0
    case R_AVR_MS8_LDI:
1246
0
      contents += rel->r_offset;
1247
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1248
0
      srel = (srel >> 24) & 0xff;
1249
0
      x = bfd_get_16 (input_bfd, contents);
1250
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1251
0
      bfd_put_16 (input_bfd, x, contents);
1252
0
      break;
1253
1254
0
    case R_AVR_LO8_LDI_NEG:
1255
0
      contents += rel->r_offset;
1256
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1257
0
      srel = -srel;
1258
0
      x = bfd_get_16 (input_bfd, contents);
1259
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1260
0
      bfd_put_16 (input_bfd, x, contents);
1261
0
      break;
1262
1263
0
    case R_AVR_HI8_LDI_NEG:
1264
0
      contents += rel->r_offset;
1265
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1266
0
      srel = -srel;
1267
0
      srel = (srel >> 8) & 0xff;
1268
0
      x = bfd_get_16 (input_bfd, contents);
1269
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1270
0
      bfd_put_16 (input_bfd, x, contents);
1271
0
      break;
1272
1273
0
    case R_AVR_HH8_LDI_NEG:
1274
0
      contents += rel->r_offset;
1275
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1276
0
      srel = -srel;
1277
0
      srel = (srel >> 16) & 0xff;
1278
0
      x = bfd_get_16 (input_bfd, contents);
1279
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1280
0
      bfd_put_16 (input_bfd, x, contents);
1281
0
      break;
1282
1283
0
    case R_AVR_MS8_LDI_NEG:
1284
0
      contents += rel->r_offset;
1285
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1286
0
      srel = -srel;
1287
0
      srel = (srel >> 24) & 0xff;
1288
0
      x = bfd_get_16 (input_bfd, contents);
1289
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1290
0
      bfd_put_16 (input_bfd, x, contents);
1291
0
      break;
1292
1293
0
    case R_AVR_LO8_LDI_GS:
1294
0
      use_stubs = (!htab->no_stubs);
1295
      /* Fall through.  */
1296
0
    case R_AVR_LO8_LDI_PM:
1297
0
      contents += rel->r_offset;
1298
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1299
1300
0
      if (use_stubs
1301
0
    && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1302
0
  {
1303
0
    bfd_vma old_srel = srel;
1304
1305
    /* We need to use the address of the stub instead.  */
1306
0
    srel = avr_get_stub_addr (srel, htab);
1307
0
    if (debug_stubs)
1308
0
      printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1309
0
        "reloc at address 0x%x.\n",
1310
0
        (unsigned int) srel,
1311
0
        (unsigned int) old_srel,
1312
0
        (unsigned int) reloc_addr);
1313
1314
0
    if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1315
0
      return bfd_reloc_overflow;
1316
0
  }
1317
1318
0
      if (srel & 1)
1319
0
  return bfd_reloc_other;
1320
0
      srel = srel >> 1;
1321
0
      x = bfd_get_16 (input_bfd, contents);
1322
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1323
0
      bfd_put_16 (input_bfd, x, contents);
1324
0
      break;
1325
1326
0
    case R_AVR_HI8_LDI_GS:
1327
0
      use_stubs = (!htab->no_stubs);
1328
      /* Fall through.  */
1329
0
    case R_AVR_HI8_LDI_PM:
1330
0
      contents += rel->r_offset;
1331
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1332
1333
0
      if (use_stubs
1334
0
    && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1335
0
  {
1336
0
    bfd_vma old_srel = srel;
1337
1338
    /* We need to use the address of the stub instead.  */
1339
0
    srel = avr_get_stub_addr (srel, htab);
1340
0
    if (debug_stubs)
1341
0
      printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1342
0
        "reloc at address 0x%x.\n",
1343
0
        (unsigned int) srel,
1344
0
        (unsigned int) old_srel,
1345
0
        (unsigned int) reloc_addr);
1346
1347
0
    if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1348
0
      return bfd_reloc_overflow;
1349
0
  }
1350
1351
0
      if (srel & 1)
1352
0
  return bfd_reloc_other;
1353
0
      srel = srel >> 1;
1354
0
      srel = (srel >> 8) & 0xff;
1355
0
      x = bfd_get_16 (input_bfd, contents);
1356
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1357
0
      bfd_put_16 (input_bfd, x, contents);
1358
0
      break;
1359
1360
0
    case R_AVR_HH8_LDI_PM:
1361
0
      contents += rel->r_offset;
1362
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1363
0
      if (srel & 1)
1364
0
  return bfd_reloc_other;
1365
0
      srel = srel >> 1;
1366
0
      srel = (srel >> 16) & 0xff;
1367
0
      x = bfd_get_16 (input_bfd, contents);
1368
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1369
0
      bfd_put_16 (input_bfd, x, contents);
1370
0
      break;
1371
1372
0
    case R_AVR_LO8_LDI_PM_NEG:
1373
0
      contents += rel->r_offset;
1374
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1375
0
      srel = -srel;
1376
0
      if (srel & 1)
1377
0
  return bfd_reloc_other;
1378
0
      srel = srel >> 1;
1379
0
      x = bfd_get_16 (input_bfd, contents);
1380
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1381
0
      bfd_put_16 (input_bfd, x, contents);
1382
0
      break;
1383
1384
0
    case R_AVR_HI8_LDI_PM_NEG:
1385
0
      contents += rel->r_offset;
1386
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1387
0
      srel = -srel;
1388
0
      if (srel & 1)
1389
0
  return bfd_reloc_other;
1390
0
      srel = srel >> 1;
1391
0
      srel = (srel >> 8) & 0xff;
1392
0
      x = bfd_get_16 (input_bfd, contents);
1393
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1394
0
      bfd_put_16 (input_bfd, x, contents);
1395
0
      break;
1396
1397
0
    case R_AVR_HH8_LDI_PM_NEG:
1398
0
      contents += rel->r_offset;
1399
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1400
0
      srel = -srel;
1401
0
      if (srel & 1)
1402
0
  return bfd_reloc_other;
1403
0
      srel = srel >> 1;
1404
0
      srel = (srel >> 16) & 0xff;
1405
0
      x = bfd_get_16 (input_bfd, contents);
1406
0
      x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1407
0
      bfd_put_16 (input_bfd, x, contents);
1408
0
      break;
1409
1410
0
    case R_AVR_CALL:
1411
0
      contents += rel->r_offset;
1412
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1413
0
      if (srel & 1)
1414
0
  return bfd_reloc_other;
1415
0
      srel = srel >> 1;
1416
0
      x = bfd_get_16 (input_bfd, contents);
1417
0
      x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1418
0
      bfd_put_16 (input_bfd, x, contents);
1419
0
      bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1420
0
      break;
1421
1422
0
    case R_AVR_16_PM:
1423
0
      use_stubs = (!htab->no_stubs);
1424
0
      contents += rel->r_offset;
1425
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1426
1427
0
      if (use_stubs
1428
0
    && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1429
0
  {
1430
0
    bfd_vma old_srel = srel;
1431
1432
    /* We need to use the address of the stub instead.  */
1433
0
    srel = avr_get_stub_addr (srel,htab);
1434
0
    if (debug_stubs)
1435
0
      printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1436
0
        "reloc at address 0x%x.\n",
1437
0
        (unsigned int) srel,
1438
0
        (unsigned int) old_srel,
1439
0
        (unsigned int) reloc_addr);
1440
1441
0
    if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1442
0
      return bfd_reloc_overflow;
1443
0
  }
1444
1445
0
      if (srel & 1)
1446
0
  return bfd_reloc_other;
1447
0
      srel = srel >> 1;
1448
0
      bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1449
0
      break;
1450
1451
0
    case R_AVR_DIFF8:
1452
0
    case R_AVR_DIFF16:
1453
0
    case R_AVR_DIFF32:
1454
      /* Nothing to do here, as contents already contains the diff value. */
1455
0
      r = bfd_reloc_ok;
1456
0
      break;
1457
1458
0
   case R_AVR_LDS_STS_16:
1459
0
      contents += rel->r_offset;
1460
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1461
0
      if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
1462
0
  return bfd_reloc_overflow;
1463
0
      srel = srel & 0x7f;
1464
0
      x = bfd_get_16 (input_bfd, contents);
1465
0
      x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
1466
0
      bfd_put_16 (input_bfd, x, contents);
1467
0
      break;
1468
1469
0
    case R_AVR_PORT6:
1470
0
      contents += rel->r_offset;
1471
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1472
0
      if ((srel & 0xffff) > 0x3f)
1473
0
  return bfd_reloc_overflow;
1474
0
      x = bfd_get_16 (input_bfd, contents);
1475
0
      x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
1476
0
      bfd_put_16 (input_bfd, x, contents);
1477
0
      break;
1478
1479
0
    case R_AVR_PORT5:
1480
0
      contents += rel->r_offset;
1481
0
      srel = (bfd_signed_vma) relocation + rel->r_addend;
1482
0
      if ((srel & 0xffff) > 0x1f)
1483
0
  return bfd_reloc_overflow;
1484
0
      x = bfd_get_16 (input_bfd, contents);
1485
0
      x = (x & 0xff07) | ((srel & 0x1f) << 3);
1486
0
      bfd_put_16 (input_bfd, x, contents);
1487
0
      break;
1488
1489
0
    default:
1490
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1491
0
            contents, rel->r_offset,
1492
0
            relocation, rel->r_addend);
1493
0
    }
1494
1495
0
  return r;
1496
0
}
1497
1498
/* Relocate an AVR ELF section.  */
1499
1500
static int
1501
elf32_avr_relocate_section (struct bfd_link_info *info, bfd *input_bfd,
1502
          asection *input_section, bfd_byte *contents,
1503
          Elf_Internal_Rela *relocs,
1504
          Elf_Internal_Sym *local_syms,
1505
          asection **local_sections)
1506
0
{
1507
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
1508
1509
0
  if (htab == NULL)
1510
0
    return false;
1511
1512
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1513
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1514
0
  Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
1515
1516
0
  for (Elf_Internal_Rela *rel = relocs; rel < relend; rel++)
1517
0
    {
1518
0
      bfd_vma relocation;
1519
0
      bfd_reloc_status_type r;
1520
0
      const char *name;
1521
1522
0
      int r_type = ELF32_R_TYPE (rel->r_info);
1523
0
      unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
1524
0
      reloc_howto_type *howto = elf_avr_howto_table + r_type;
1525
0
      struct elf_link_hash_entry *h = NULL;
1526
0
      const Elf_Internal_Sym *sym = NULL;
1527
0
      asection *sec = NULL;
1528
1529
0
      if (r_symndx < symtab_hdr->sh_info)
1530
0
  {
1531
0
    sym = local_syms + r_symndx;
1532
0
    sec = local_sections [r_symndx];
1533
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
1534
0
            sym, &sec, rel);
1535
1536
0
    name = bfd_elf_string_from_elf_section
1537
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
1538
0
    name = name == NULL ? bfd_section_name (sec) : name;
1539
0
  }
1540
0
      else
1541
0
  {
1542
0
    bool unresolved_reloc, warned, ignored;
1543
1544
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1545
0
           r_symndx, symtab_hdr, sym_hashes,
1546
0
           h, sec, relocation,
1547
0
           unresolved_reloc, warned, ignored);
1548
1549
0
    name = h->root.root.string;
1550
0
  }
1551
1552
0
      if (sec != NULL && discarded_section (sec))
1553
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1554
0
           rel, 1, relend, R_AVR_NONE,
1555
0
           howto, 0, contents);
1556
1557
0
      if (bfd_link_relocatable (info))
1558
0
  continue;
1559
1560
0
      r = avr_final_link_relocate (howto, input_bfd, input_section,
1561
0
           contents, rel, relocation, htab);
1562
1563
0
      if (r != bfd_reloc_ok)
1564
0
  {
1565
0
    switch (r)
1566
0
      {
1567
0
      case bfd_reloc_overflow:
1568
0
        (*info->callbacks->reloc_overflow)
1569
0
    (info, (h ? &h->root : NULL), name, howto->name,
1570
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1571
0
        break;
1572
1573
0
      case bfd_reloc_undefined:
1574
0
        (*info->callbacks->undefined_symbol)
1575
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
1576
0
        break;
1577
1578
0
      case bfd_reloc_outofrange:
1579
        /* xgettext:c-format */
1580
0
        (*info->callbacks->einfo)
1581
0
    (_("%X%H: %s against `%s':"
1582
0
       " error: relocation applies outside section\n"),
1583
0
     input_bfd, input_section, rel->r_offset, howto->name, name);
1584
0
        break;
1585
1586
0
      case bfd_reloc_other:
1587
        /* xgettext:c-format */
1588
0
        (*info->callbacks->einfo)
1589
0
    (_("%X%H: %s against `%s':"
1590
0
       " error: relocation target address is odd\n"),
1591
0
     input_bfd, input_section, rel->r_offset, howto->name, name);
1592
0
        break;
1593
1594
0
      default:
1595
        /* xgettext:c-format */
1596
0
        (*info->callbacks->einfo)
1597
0
    (_("%X%H: %s against `%s':"
1598
0
       " internal error: unexpected relocation result %d\n"),
1599
0
     input_bfd, input_section, rel->r_offset, howto->name, name, r);
1600
0
        break;
1601
0
      }
1602
0
  }
1603
0
    }
1604
1605
0
  return true;
1606
0
}
1607
1608
/* The final processing done just before writing out a AVR ELF object
1609
   file.  This gets the AVR architecture right based on the machine
1610
   number.  */
1611
1612
static bool
1613
bfd_elf_avr_final_write_processing (bfd *abfd)
1614
19
{
1615
19
  unsigned long val;
1616
1617
19
  switch (bfd_get_mach (abfd))
1618
19
    {
1619
0
    default:
1620
4
    case bfd_mach_avr2:
1621
4
      val = E_AVR_MACH_AVR2;
1622
4
      break;
1623
1624
1
    case bfd_mach_avr1:
1625
1
      val = E_AVR_MACH_AVR1;
1626
1
      break;
1627
1628
1
    case bfd_mach_avr25:
1629
1
      val = E_AVR_MACH_AVR25;
1630
1
      break;
1631
1632
1
    case bfd_mach_avr3:
1633
1
      val = E_AVR_MACH_AVR3;
1634
1
      break;
1635
1636
0
    case bfd_mach_avr31:
1637
0
      val = E_AVR_MACH_AVR31;
1638
0
      break;
1639
1640
1
    case bfd_mach_avr35:
1641
1
      val = E_AVR_MACH_AVR35;
1642
1
      break;
1643
1644
1
    case bfd_mach_avr4:
1645
1
      val = E_AVR_MACH_AVR4;
1646
1
      break;
1647
1648
1
    case bfd_mach_avr5:
1649
1
      val = E_AVR_MACH_AVR5;
1650
1
      break;
1651
1652
2
    case bfd_mach_avr51:
1653
2
      val = E_AVR_MACH_AVR51;
1654
2
      break;
1655
1656
1
    case bfd_mach_avr6:
1657
1
      val = E_AVR_MACH_AVR6;
1658
1
      break;
1659
1660
1
    case bfd_mach_avrxmega1:
1661
1
      val = E_AVR_MACH_XMEGA1;
1662
1
      break;
1663
1664
1
    case bfd_mach_avrxmega2:
1665
1
      val = E_AVR_MACH_XMEGA2;
1666
1
      break;
1667
1668
1
    case bfd_mach_avrxmega3:
1669
1
      val = E_AVR_MACH_XMEGA3;
1670
1
      break;
1671
1672
0
    case bfd_mach_avrxmega4:
1673
0
      val = E_AVR_MACH_XMEGA4;
1674
0
      break;
1675
1676
1
    case bfd_mach_avrxmega5:
1677
1
      val = E_AVR_MACH_XMEGA5;
1678
1
      break;
1679
1680
1
    case bfd_mach_avrxmega6:
1681
1
      val = E_AVR_MACH_XMEGA6;
1682
1
      break;
1683
1684
1
    case bfd_mach_avrxmega7:
1685
1
      val = E_AVR_MACH_XMEGA7;
1686
1
      break;
1687
1688
0
    case bfd_mach_avrtiny:
1689
0
      val = E_AVR_MACH_AVRTINY;
1690
0
      break;
1691
19
    }
1692
1693
19
  elf_elfheader (abfd)->e_machine = EM_AVR;
1694
19
  elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1695
19
  elf_elfheader (abfd)->e_flags |= val;
1696
19
  return _bfd_elf_final_write_processing (abfd);
1697
19
}
1698
1699
/* Set the right machine number.  */
1700
1701
static bool
1702
elf32_avr_object_p (bfd *abfd)
1703
367
{
1704
367
  unsigned int e_set = bfd_mach_avr2;
1705
1706
367
  if (elf_elfheader (abfd)->e_machine == EM_AVR
1707
87
      || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1708
367
    {
1709
367
      int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1710
1711
367
      switch (e_mach)
1712
367
  {
1713
182
  default:
1714
182
  case E_AVR_MACH_AVR2:
1715
182
    e_set = bfd_mach_avr2;
1716
182
    break;
1717
1718
10
  case E_AVR_MACH_AVR1:
1719
10
    e_set = bfd_mach_avr1;
1720
10
    break;
1721
1722
9
  case E_AVR_MACH_AVR25:
1723
9
    e_set = bfd_mach_avr25;
1724
9
    break;
1725
1726
11
  case E_AVR_MACH_AVR3:
1727
11
    e_set = bfd_mach_avr3;
1728
11
    break;
1729
1730
14
  case E_AVR_MACH_AVR31:
1731
14
    e_set = bfd_mach_avr31;
1732
14
    break;
1733
1734
10
  case E_AVR_MACH_AVR35:
1735
10
    e_set = bfd_mach_avr35;
1736
10
    break;
1737
1738
14
  case E_AVR_MACH_AVR4:
1739
14
    e_set = bfd_mach_avr4;
1740
14
    break;
1741
1742
20
  case E_AVR_MACH_AVR5:
1743
20
    e_set = bfd_mach_avr5;
1744
20
    break;
1745
1746
12
  case E_AVR_MACH_AVR51:
1747
12
    e_set = bfd_mach_avr51;
1748
12
    break;
1749
1750
8
  case E_AVR_MACH_AVR6:
1751
8
    e_set = bfd_mach_avr6;
1752
8
    break;
1753
1754
13
  case E_AVR_MACH_XMEGA1:
1755
13
    e_set = bfd_mach_avrxmega1;
1756
13
    break;
1757
1758
6
  case E_AVR_MACH_XMEGA2:
1759
6
    e_set = bfd_mach_avrxmega2;
1760
6
    break;
1761
1762
14
  case E_AVR_MACH_XMEGA3:
1763
14
    e_set = bfd_mach_avrxmega3;
1764
14
    break;
1765
1766
6
  case E_AVR_MACH_XMEGA4:
1767
6
    e_set = bfd_mach_avrxmega4;
1768
6
    break;
1769
1770
8
  case E_AVR_MACH_XMEGA5:
1771
8
    e_set = bfd_mach_avrxmega5;
1772
8
    break;
1773
1774
8
  case E_AVR_MACH_XMEGA6:
1775
8
    e_set = bfd_mach_avrxmega6;
1776
8
    break;
1777
1778
10
  case E_AVR_MACH_XMEGA7:
1779
10
    e_set = bfd_mach_avrxmega7;
1780
10
    break;
1781
1782
12
  case E_AVR_MACH_AVRTINY:
1783
12
    e_set = bfd_mach_avrtiny;
1784
12
    break;
1785
367
  }
1786
367
    }
1787
367
  return bfd_default_set_arch_mach (abfd, bfd_arch_avr, e_set);
1788
367
}
1789
1790
/* Returns whether the relocation type passed is a diff reloc. */
1791
1792
static bool
1793
elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
1794
0
{
1795
0
  return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
1796
0
    || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
1797
0
    || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
1798
0
}
1799
1800
/* Reduce the diff value written in the section by count if the shrunk
1801
   insn address happens to fall between the two symbols for which this
1802
   diff reloc was emitted.  */
1803
1804
static void
1805
elf32_avr_adjust_diff_reloc_value (bfd *abfd, struct bfd_section *isec,
1806
           Elf_Internal_Rela *irel,
1807
           bfd_vma symval,
1808
           bfd_vma shrunk_insn_address, int count)
1809
0
{
1810
0
  unsigned char *reloc_contents = NULL;
1811
0
  unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
1812
0
  if (isec_contents == NULL)
1813
0
  {
1814
0
    if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
1815
0
      return;
1816
1817
0
    elf_section_data (isec)->this_hdr.contents = isec_contents;
1818
0
  }
1819
1820
0
  reloc_contents = isec_contents + irel->r_offset;
1821
1822
  /* Read value written in object file. */
1823
0
  bfd_signed_vma x = 0;
1824
0
  switch (ELF32_R_TYPE (irel->r_info))
1825
0
    {
1826
0
    case R_AVR_DIFF8:
1827
0
      {
1828
0
  x = bfd_get_signed_8 (abfd, reloc_contents);
1829
0
  break;
1830
0
      }
1831
0
    case R_AVR_DIFF16:
1832
0
      {
1833
0
  x = bfd_get_signed_16 (abfd, reloc_contents);
1834
0
  break;
1835
0
      }
1836
0
    case R_AVR_DIFF32:
1837
0
      {
1838
0
  x = bfd_get_signed_32 (abfd, reloc_contents);
1839
0
  break;
1840
0
      }
1841
0
    default:
1842
0
      {
1843
0
  BFD_FAIL();
1844
0
      }
1845
0
    }
1846
1847
  /* For a diff reloc sym1 - sym2, the diff at assembly time (x) is written
1848
     into the object file at the reloc offset. sym2's logical value is
1849
     symval (<start_of_section>) + reloc addend. Compute the start and end
1850
     addresses and check if the shrunk insn falls between sym1 and sym2. */
1851
1852
0
  bfd_vma sym2_address = symval + irel->r_addend;
1853
0
  bfd_vma sym1_address = sym2_address - x;
1854
1855
  /* Don't assume sym2 is bigger than sym1 as the difference
1856
     could be negative. Compute start and end addresses, and
1857
     use those to see if they span shrunk_insn_address. */
1858
1859
0
  bfd_vma start_address = sym1_address < sym2_address
1860
0
    ? sym1_address : sym2_address;
1861
0
  bfd_vma end_address = sym1_address > sym2_address
1862
0
    ? sym1_address : sym2_address;
1863
1864
0
  if (shrunk_insn_address >= start_address
1865
0
      && shrunk_insn_address < end_address)
1866
0
    {
1867
      /* Reduce the diff value by count bytes and write it back into section
1868
   contents. */
1869
0
      bfd_signed_vma new_diff = x < 0 ? x + count : x - count;
1870
1871
0
      if (sym2_address > shrunk_insn_address)
1872
0
  irel->r_addend -= count;
1873
1874
0
      switch (ELF32_R_TYPE (irel->r_info))
1875
0
  {
1876
0
  case R_AVR_DIFF8:
1877
0
    {
1878
0
      bfd_put_signed_8 (abfd, new_diff, reloc_contents);
1879
0
      break;
1880
0
    }
1881
0
  case R_AVR_DIFF16:
1882
0
    {
1883
0
      bfd_put_signed_16 (abfd, new_diff & 0xFFFF, reloc_contents);
1884
0
      break;
1885
0
    }
1886
0
  case R_AVR_DIFF32:
1887
0
    {
1888
0
      bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents);
1889
0
      break;
1890
0
    }
1891
0
  default:
1892
0
    {
1893
0
      BFD_FAIL();
1894
0
    }
1895
0
  }
1896
0
    }
1897
0
}
1898
1899
static void
1900
elf32_avr_adjust_reloc_if_spans_insn (bfd *abfd, asection *isec,
1901
              Elf_Internal_Rela *irel,  bfd_vma symval,
1902
              bfd_vma shrunk_insn_address,
1903
              bfd_vma shrink_boundary,
1904
              int count)
1905
0
{
1906
0
  if (elf32_avr_is_diff_reloc (irel))
1907
0
    {
1908
0
      elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
1909
0
           symval, shrunk_insn_address, count);
1910
0
    }
1911
0
  else
1912
0
    {
1913
0
      bfd_vma reloc_value = symval + irel->r_addend;
1914
0
      bool addend_within_shrink_boundary = reloc_value <= shrink_boundary;
1915
1916
0
      bool reloc_spans_insn
1917
0
  = (symval <= shrunk_insn_address
1918
0
     && reloc_value > shrunk_insn_address
1919
0
     && addend_within_shrink_boundary);
1920
1921
0
      if (! reloc_spans_insn)
1922
0
  return;
1923
1924
0
      irel->r_addend -= count;
1925
1926
0
      if (debug_relax)
1927
0
  printf ("Relocation's addend needed to be fixed \n");
1928
0
    }
1929
0
}
1930
1931
static bool
1932
avr_should_move_sym (symvalue symval,
1933
         bfd_vma start, bfd_vma end, bool did_pad)
1934
0
{
1935
0
  bool sym_within_boundary = did_pad ? symval < end : symval <= end;
1936
0
  return symval > start && sym_within_boundary;
1937
0
}
1938
1939
static bool
1940
avr_should_reduce_sym_size (symvalue symval, symvalue symend,
1941
          bfd_vma start, bfd_vma end, bool did_pad)
1942
0
{
1943
0
  bool sym_end_within_boundary = did_pad ? symend < end : symend <= end;
1944
0
  return symval <= start && symend > start && sym_end_within_boundary;
1945
0
}
1946
1947
static bool
1948
avr_should_increase_sym_size (symvalue symval, symvalue symend,
1949
            bfd_vma start, bfd_vma end, bool did_pad)
1950
0
{
1951
0
  return (avr_should_move_sym (symval, start, end, did_pad)
1952
0
    && symend >= end && did_pad);
1953
0
}
1954
1955
1956
/* Read this BFD's local symbols.  */
1957
1958
static Elf_Internal_Sym *
1959
avr_read_symbuf (bfd *abfd, Elf_Internal_Shdr *symtab_hdr)
1960
0
{
1961
0
  Elf_Internal_Sym *isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1962
0
  if (isymbuf == NULL)
1963
0
    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info,
1964
0
            0, NULL, NULL, NULL);
1965
0
  return isymbuf;
1966
0
}
1967
1968
1969
/* Delete some bytes from a section while changing the size of an instruction.
1970
   The parameter "addr" denotes the section-relative offset pointing just
1971
   behind the shrunk instruction. "addr+count" point at the first
1972
   byte just behind the original unshrunk instruction. If delete_shrinks_insn
1973
   is FALSE, we are deleting redundant padding bytes from relax_info prop
1974
   record handling. In that case, addr is section-relative offset of start
1975
   of padding, and count is the number of padding bytes to delete. */
1976
1977
static bool
1978
elf32_avr_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
1979
            bool delete_shrinks_insn)
1980
0
{
1981
0
  Elf_Internal_Sym *isym;
1982
0
  Elf_Internal_Sym *isymbuf = NULL;
1983
0
  struct elf_link_hash_entry **sym_hashes;
1984
0
  struct elf_link_hash_entry **end_hashes;
1985
0
  unsigned int symcount;
1986
0
  struct avr_property_record *prop_record = NULL;
1987
0
  bool did_shrink = false;
1988
0
  bool did_pad = false;
1989
1990
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
1991
0
  unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1992
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1993
0
  avr_relax_info_t *relax_info = get_avr_relax_info (sec);
1994
1995
0
  bfd_vma toaddr = sec->size;
1996
1997
0
  if (relax_info->records.count > 0)
1998
0
    {
1999
      /* There should be no property record within the range of deleted
2000
   bytes, however, there might be a property record for ADDR, this is
2001
   how we handle alignment directives.
2002
   Find the next (if any) property record after the deleted bytes.  */
2003
2004
0
      for (unsigned int i = 0; i < relax_info->records.count; ++i)
2005
0
  {
2006
0
    bfd_vma offset = relax_info->records.items [i].offset;
2007
2008
0
    BFD_ASSERT (offset <= addr || offset >= addr + count);
2009
0
    if (offset >= addr + count)
2010
0
      {
2011
0
        prop_record = &relax_info->records.items [i];
2012
0
        toaddr = offset;
2013
0
        break;
2014
0
      }
2015
0
  }
2016
0
    }
2017
2018
0
  Elf_Internal_Rela *irel = elf_section_data (sec)->relocs;
2019
0
  Elf_Internal_Rela *irelend = irel + sec->reloc_count;
2020
2021
  /* Actually delete the bytes.  */
2022
0
  if (toaddr - addr - count > 0)
2023
0
    {
2024
0
      memmove (contents + addr, contents + addr + count,
2025
0
         (size_t) (toaddr - addr - count));
2026
0
      did_shrink = true;
2027
0
    }
2028
0
  if (prop_record == NULL)
2029
0
    {
2030
0
      sec->size -= count;
2031
0
      did_shrink = true;
2032
0
    }
2033
0
  else
2034
0
    {
2035
      /* Use the property record to fill in the bytes we've opened up.  */
2036
0
      int fill = 0;
2037
0
      switch (prop_record->type)
2038
0
  {
2039
0
  case RECORD_ORG_AND_FILL:
2040
0
    fill = prop_record->data.org.fill;
2041
    /* Fall through.  */
2042
0
  case RECORD_ORG:
2043
0
    break;
2044
0
  case RECORD_ALIGN_AND_FILL:
2045
0
    fill = prop_record->data.align.fill;
2046
    /* Fall through.  */
2047
0
  case RECORD_ALIGN:
2048
0
    prop_record->data.align.preceding_deleted += count;
2049
0
    break;
2050
0
  }
2051
      /* If toaddr == addr + count, then we didn't delete anything, yet
2052
   we fill count bytes backwards from toaddr. This is still ok - we
2053
   end up overwriting the bytes we would have deleted. We just need
2054
   to remember we didn't delete anything i.e. don't set did_shrink,
2055
   so that we don't corrupt reloc offsets or symbol values.*/
2056
0
      memset (contents + toaddr - count, fill, count);
2057
0
      did_pad = true;
2058
0
    }
2059
2060
0
  if (!did_shrink)
2061
0
    return true;
2062
2063
  /* Adjust all the reloc addresses.  */
2064
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2065
0
    {
2066
0
      bfd_vma old_reloc_address;
2067
2068
0
      old_reloc_address = (sec->output_section->vma
2069
0
         + sec->output_offset + irel->r_offset);
2070
2071
      /* Get the new reloc address.  */
2072
0
      if ((irel->r_offset > addr
2073
0
     && irel->r_offset < toaddr))
2074
0
  {
2075
0
    if (debug_relax)
2076
0
      printf ("Relocation at address 0x%x needs to be moved.\n"
2077
0
        "Old section offset: 0x%x, New section offset: 0x%x \n",
2078
0
        (unsigned int) old_reloc_address,
2079
0
        (unsigned int) irel->r_offset,
2080
0
        (unsigned int) ((irel->r_offset) - count));
2081
2082
0
    irel->r_offset -= count;
2083
0
  }
2084
0
    }
2085
2086
   /* The reloc's own addresses are now ok. However, we need to readjust
2087
      the reloc's addend, i.e. the reloc's value if two conditions are met:
2088
      1.) the reloc is relative to a symbol in this section that
2089
    is located in front of the shrunk instruction
2090
      2.) symbol plus addend end up behind the shrunk instruction.
2091
2092
      The most common case where this happens are relocs relative to
2093
      the section-start symbol.
2094
2095
      This step needs to be done for all of the sections of the bfd.  */
2096
2097
0
  for (struct bfd_section *isec = abfd->sections; isec; isec = isec->next)
2098
0
    {
2099
0
      bfd_vma symval;
2100
0
      bfd_vma shrunk_insn_address;
2101
2102
0
      if (isec->reloc_count == 0)
2103
0
  continue;
2104
2105
0
      shrunk_insn_address = (sec->output_section->vma
2106
0
           + sec->output_offset + addr);
2107
0
      if (delete_shrinks_insn)
2108
0
  shrunk_insn_address -= count;
2109
2110
0
      irel = elf_section_data (isec)->relocs;
2111
      /* PR 12161: Read in the relocs for this section if necessary.  */
2112
0
      if (irel == NULL)
2113
0
  irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, true);
2114
2115
0
      for (irelend = irel + isec->reloc_count; irel < irelend; irel++)
2116
0
  {
2117
    /* Read this BFD's local symbols if we haven't done so already.  */
2118
0
    if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2119
0
      {
2120
0
        isymbuf = avr_read_symbuf (abfd, symtab_hdr);
2121
0
        if (isymbuf == NULL)
2122
0
    return false;
2123
0
      }
2124
2125
    /* Get the value of the symbol referred to by the reloc.  */
2126
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2127
0
      {
2128
        /* A local symbol.  */
2129
0
        asection *sym_sec;
2130
2131
0
        isym = isymbuf + ELF32_R_SYM (irel->r_info);
2132
0
        sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2133
0
        symval = isym->st_value;
2134
        /* If the reloc is absolute, it will not have
2135
     a symbol or section associated with it.  */
2136
0
        if (sym_sec == sec)
2137
0
    {
2138
      /* If there is an alignment boundary, we only need to
2139
         adjust addends that end up below the boundary. */
2140
0
      bfd_vma shrink_boundary = (toaddr
2141
0
               + sec->output_section->vma
2142
0
               + sec->output_offset);
2143
2144
0
      symval += (sym_sec->output_section->vma
2145
0
           + sym_sec->output_offset);
2146
2147
0
      if (debug_relax)
2148
0
        printf ("Checking if the relocation's "
2149
0
          "addend needs corrections.\n"
2150
0
          "Address of anchor symbol: 0x%x \n"
2151
0
          "Address of relocation target: 0x%x \n"
2152
0
          "Address of relaxed insn: 0x%x \n",
2153
0
          (unsigned int) symval,
2154
0
          (unsigned int) (symval + irel->r_addend),
2155
0
          (unsigned int) shrunk_insn_address);
2156
2157
0
      elf32_avr_adjust_reloc_if_spans_insn (abfd, isec, irel,
2158
0
              symval,
2159
0
              shrunk_insn_address,
2160
0
              shrink_boundary,
2161
0
              count);
2162
0
    }
2163
        // else...Reference symbol is absolute:  No adjustment needed.
2164
0
      }
2165
    // else...Reference symbol is extern:  Addend needs no adjustment.
2166
0
  }
2167
0
    } // for bfd_section *isec
2168
2169
  /* Adjust the local symbols defined in this section.  */
2170
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2171
2172
  /* Fix PR 9841, there may be no local symbols.  */
2173
0
  if (isym != NULL)
2174
0
    {
2175
0
      Elf_Internal_Sym *isymend;
2176
2177
0
      isymend = isym + symtab_hdr->sh_info;
2178
0
      for (; isym < isymend; isym++)
2179
0
  {
2180
0
    if (isym->st_shndx == sec_shndx)
2181
0
      {
2182
0
        symvalue symval = isym->st_value;
2183
0
        symvalue symend = symval + isym->st_size;
2184
0
        if (avr_should_reduce_sym_size (symval, symend,
2185
0
                addr, toaddr, did_pad))
2186
0
    {
2187
      /* If this assert fires then we have a symbol that ends
2188
         part way through an instruction.  Does that make sense?  */
2189
0
      BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
2190
0
      isym->st_size -= count;
2191
0
    }
2192
0
        else if (avr_should_increase_sym_size (symval, symend,
2193
0
                 addr, toaddr, did_pad))
2194
0
    isym->st_size += count;
2195
2196
0
        if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2197
0
    isym->st_value -= count;
2198
0
      }
2199
0
  }
2200
0
    }
2201
2202
  /* Now adjust the global symbols defined in this section.  */
2203
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2204
0
        - symtab_hdr->sh_info);
2205
0
  sym_hashes = elf_sym_hashes (abfd);
2206
0
  end_hashes = sym_hashes + symcount;
2207
0
  for (; sym_hashes < end_hashes; sym_hashes++)
2208
0
    {
2209
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
2210
0
      if ((sym_hash->root.type == bfd_link_hash_defined
2211
0
     || sym_hash->root.type == bfd_link_hash_defweak)
2212
0
    && sym_hash->root.u.def.section == sec)
2213
0
  {
2214
0
    symvalue symval = sym_hash->root.u.def.value;
2215
0
    symvalue symend = symval + sym_hash->size;
2216
2217
0
    if (avr_should_reduce_sym_size (symval, symend,
2218
0
            addr, toaddr, did_pad))
2219
0
      {
2220
        /* If this assert fires then we have a symbol that ends
2221
     part way through an instruction.  Does that make
2222
     sense?  */
2223
0
        BFD_ASSERT (symend >= addr + count);
2224
0
        sym_hash->size -= count;
2225
0
      }
2226
0
    else if (avr_should_increase_sym_size (symval, symend,
2227
0
             addr, toaddr, did_pad))
2228
0
      sym_hash->size += count;
2229
2230
0
    if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2231
0
      sym_hash->root.u.def.value -= count;
2232
0
  }
2233
0
    }
2234
2235
0
  return true;
2236
0
}
2237
2238
static Elf_Internal_Sym *
2239
retrieve_local_syms (bfd *input_bfd)
2240
0
{
2241
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
2242
0
  size_t locsymcount = symtab_hdr->sh_info;
2243
2244
0
  Elf_Internal_Sym *isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2245
0
  if (isymbuf == NULL && locsymcount != 0)
2246
0
    isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
2247
0
            NULL, NULL, NULL);
2248
2249
  /* Save the symbols for this input file so they won't be read again.  */
2250
0
  if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
2251
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
2252
2253
0
  return isymbuf;
2254
0
}
2255
2256
/* Get the input section for a given symbol index.
2257
   If the symbol is:
2258
   . a section symbol, return the section;
2259
   . a common symbol, return the common section;
2260
   . an undefined symbol, return the undefined section;
2261
   . an indirect symbol, follow the links;
2262
   . an absolute value, return the absolute section.  */
2263
2264
static asection *
2265
get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
2266
0
{
2267
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2268
0
  asection *target_sec = NULL;
2269
0
  if (r_symndx < symtab_hdr->sh_info)
2270
0
    {
2271
0
      Elf_Internal_Sym *isymbuf;
2272
0
      unsigned int section_index;
2273
2274
0
      isymbuf = retrieve_local_syms (abfd);
2275
0
      section_index = isymbuf[r_symndx].st_shndx;
2276
2277
0
      if (section_index == SHN_UNDEF)
2278
0
  target_sec = bfd_und_section_ptr;
2279
0
      else if (section_index == SHN_ABS)
2280
0
  target_sec = bfd_abs_section_ptr;
2281
0
      else if (section_index == SHN_COMMON)
2282
0
  target_sec = bfd_com_section_ptr;
2283
0
      else
2284
0
  target_sec = bfd_section_from_elf_index (abfd, section_index);
2285
0
    }
2286
0
  else
2287
0
    {
2288
0
      unsigned long indx = r_symndx - symtab_hdr->sh_info;
2289
0
      struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
2290
2291
0
      while (h->root.type == bfd_link_hash_indirect
2292
0
       || h->root.type == bfd_link_hash_warning)
2293
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
2294
2295
0
      switch (h->root.type)
2296
0
  {
2297
0
  case bfd_link_hash_defined:
2298
0
  case  bfd_link_hash_defweak:
2299
0
    target_sec = h->root.u.def.section;
2300
0
    break;
2301
0
  case bfd_link_hash_common:
2302
0
    target_sec = bfd_com_section_ptr;
2303
0
    break;
2304
0
  case bfd_link_hash_undefined:
2305
0
  case bfd_link_hash_undefweak:
2306
0
    target_sec = bfd_und_section_ptr;
2307
0
    break;
2308
0
  default: /* New indirect warning.  */
2309
0
    target_sec = bfd_und_section_ptr;
2310
0
    break;
2311
0
  }
2312
0
    }
2313
0
  return target_sec;
2314
0
}
2315
2316
/* Get the section-relative offset for a symbol number.  */
2317
2318
static bfd_vma
2319
get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
2320
0
{
2321
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2322
0
  bfd_vma offset = 0;
2323
2324
0
  if (r_symndx < symtab_hdr->sh_info)
2325
0
    {
2326
0
      Elf_Internal_Sym *isymbuf;
2327
0
      isymbuf = retrieve_local_syms (abfd);
2328
0
      offset = isymbuf[r_symndx].st_value;
2329
0
    }
2330
0
  else
2331
0
    {
2332
0
      unsigned long indx = r_symndx - symtab_hdr->sh_info;
2333
0
      struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
2334
2335
0
      while (h->root.type == bfd_link_hash_indirect
2336
0
       || h->root.type == bfd_link_hash_warning)
2337
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
2338
0
      if (h->root.type == bfd_link_hash_defined
2339
0
    || h->root.type == bfd_link_hash_defweak)
2340
0
  offset = h->root.u.def.value;
2341
0
    }
2342
0
  return offset;
2343
0
}
2344
2345
/* Iterate over the property records in R_LIST, and copy each record into
2346
   the list of records within the relaxation information for the section to
2347
   which the record applies.  */
2348
2349
static void
2350
avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list)
2351
0
{
2352
0
  for (unsigned int i = 0; i < r_list->record_count; ++i)
2353
0
    {
2354
0
      avr_relax_info_t *relax_info;
2355
2356
0
      relax_info = get_avr_relax_info (r_list->records [i].section);
2357
0
      BFD_ASSERT (relax_info != NULL);
2358
2359
0
      if (relax_info->records.count == relax_info->records.allocated)
2360
0
  {
2361
    /* Allocate more space.  */
2362
0
    bfd_size_type size;
2363
2364
0
    relax_info->records.allocated += 10;
2365
0
    size = (sizeof (struct avr_property_record)
2366
0
      * relax_info->records.allocated);
2367
0
    relax_info->records.items
2368
0
      = bfd_realloc (relax_info->records.items, size);
2369
0
  }
2370
2371
0
      memcpy (&relax_info->records.items [relax_info->records.count],
2372
0
        &r_list->records [i],
2373
0
        sizeof (struct avr_property_record));
2374
0
      relax_info->records.count++;
2375
0
    }
2376
0
}
2377
2378
/* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2379
   ordering callback from QSORT.  */
2380
2381
static int
2382
avr_property_record_compare (const void *ap, const void *bp)
2383
0
{
2384
0
  const struct avr_property_record *a = (struct avr_property_record *) ap;
2385
0
  const struct avr_property_record *b = (struct avr_property_record *) bp;
2386
2387
0
  if (a->offset != b->offset)
2388
0
    return a->offset - b->offset;
2389
2390
0
  if (a->section != b->section)
2391
0
    return bfd_section_vma (a->section) - bfd_section_vma (b->section);
2392
2393
0
  return a->type - b->type;
2394
0
}
2395
2396
/* Load all of the avr property sections from all of the bfd objects
2397
   referenced from LINK_INFO.  All of the records within each property
2398
   section are assigned to the AVR_RELAX_INFO_T within the section
2399
   specific data of the appropriate section.  */
2400
2401
static void
2402
avr_load_all_property_sections (struct bfd_link_info *link_info)
2403
0
{
2404
0
  bfd *abfd;
2405
0
  asection *sec;
2406
2407
  /* Initialize the per-section relaxation info.  */
2408
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2409
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
2410
0
      {
2411
0
  init_avr_relax_info (sec);
2412
0
      }
2413
2414
  /* Load the descriptor tables from .avr.prop sections.  */
2415
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2416
0
    {
2417
0
      struct avr_property_record_list *r_list;
2418
2419
0
      r_list = avr_elf32_load_property_records (abfd);
2420
0
      if (r_list != NULL)
2421
0
  avr_elf32_assign_records_to_sections (r_list);
2422
2423
0
      free (r_list);
2424
0
    }
2425
2426
  /* Now, for every section, ensure that the descriptor list in the
2427
     relaxation data is sorted by ascending offset within the section.  */
2428
0
  for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2429
0
    for (sec = abfd->sections; sec != NULL; sec = sec->next)
2430
0
      {
2431
0
  avr_relax_info_t *relax_info = get_avr_relax_info (sec);
2432
0
  if (relax_info && relax_info->records.count > 0)
2433
0
    {
2434
0
      qsort (relax_info->records.items,
2435
0
       relax_info->records.count,
2436
0
       sizeof (struct avr_property_record),
2437
0
       avr_property_record_compare);
2438
2439
      /* For debug purposes, list all the descriptors.  */
2440
0
      for (unsigned int i = 0; i < relax_info->records.count; ++i)
2441
0
        {
2442
0
    switch (relax_info->records.items [i].type)
2443
0
      {
2444
0
      case RECORD_ORG:
2445
0
        break;
2446
0
      case RECORD_ORG_AND_FILL:
2447
0
        break;
2448
0
      case RECORD_ALIGN:
2449
0
        break;
2450
0
      case RECORD_ALIGN_AND_FILL:
2451
0
        break;
2452
0
      }
2453
0
        }
2454
0
    }
2455
0
      }
2456
0
}
2457
2458
2459
/* Return TRUE when there is a local label at OFFSET in the section.  */
2460
2461
static bool
2462
avr_local_label_at (bfd *abfd, asection *sec, Elf_Internal_Shdr *symtab_hdr,
2463
        unsigned int offset)
2464
0
{
2465
0
  const unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2466
  // Check for local symbols.
2467
0
  Elf_Internal_Sym *isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2468
0
  Elf_Internal_Sym *isymend = isym + symtab_hdr->sh_info;
2469
2470
  // PR 6019: There may not be any local symbols.
2471
0
  for (; isym != NULL && isym < isymend; isym++)
2472
0
    if (isym->st_value == offset
2473
0
  && isym->st_shndx == sec_shndx)
2474
0
      return true;
2475
2476
0
  return false;
2477
0
}
2478
2479
2480
/* Return TRUE when there is a global label at OFFSET in the section.  */
2481
2482
static bool
2483
avr_global_label_at (bfd *abfd, asection *sec, Elf_Internal_Shdr *symtab_hdr,
2484
         unsigned int offset)
2485
0
{
2486
0
  int symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2487
0
      - symtab_hdr->sh_info);
2488
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
2489
0
  struct elf_link_hash_entry **end_hashes = sym_hashes + symcount;
2490
2491
0
  for (; sym_hashes < end_hashes; ++sym_hashes)
2492
0
    {
2493
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
2494
0
      if ((sym_hash->root.type == bfd_link_hash_defined
2495
0
     || sym_hash->root.type == bfd_link_hash_defweak)
2496
0
    && sym_hash->root.u.def.section == sec
2497
0
    && sym_hash->root.u.def.value == offset)
2498
0
  return true;
2499
0
    }
2500
2501
0
  return false;
2502
0
}
2503
2504
2505
/* Return TRUE when there is a RELOC that points to ADDRESS.  */
2506
2507
static bool
2508
avr_reloc_at (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
2509
        Elf_Internal_Sym **pisymbuf, bfd_vma address)
2510
0
{
2511
0
  for (struct bfd_section *isec = abfd->sections; isec; isec = isec->next)
2512
0
    {
2513
0
      Elf_Internal_Rela *rel = elf_section_data (isec)->relocs;
2514
0
      if (rel == NULL)
2515
0
  rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, true);
2516
2517
0
      Elf_Internal_Rela *relend = rel + isec->reloc_count;
2518
2519
0
      for (; rel && rel < relend; rel++)
2520
0
  {
2521
0
    bfd_vma reloc_target = 0;
2522
2523
    // Read this BFD's local symbols if we haven't done so already.
2524
0
    if (*pisymbuf == NULL && symtab_hdr->sh_info != 0)
2525
0
      {
2526
0
        *pisymbuf = avr_read_symbuf (abfd, symtab_hdr);
2527
0
        if (*pisymbuf == NULL)
2528
0
    break;
2529
0
      }
2530
2531
    // Get the value of the symbol referred to by the reloc.
2532
0
    if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
2533
0
      {
2534
        // A local symbol.
2535
0
        asection *sym_sec;
2536
2537
0
        Elf_Internal_Sym *isym = *pisymbuf + ELF32_R_SYM (rel->r_info);
2538
0
        sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2539
0
        bfd_vma symval = isym->st_value;
2540
2541
        // If the reloc is absolute, it will not have a symbol
2542
        // or section associated to it.
2543
2544
0
        if (sym_sec)
2545
0
    {
2546
0
      symval += (sym_sec->output_section->vma
2547
0
           + sym_sec->output_offset);
2548
0
      reloc_target = symval + rel->r_addend;
2549
0
    }
2550
0
        else
2551
0
    {
2552
      // Reference symbol is absolute.
2553
0
      reloc_target = symval + rel->r_addend;
2554
0
    }
2555
0
      }
2556
    // else ... reference symbol is extern.
2557
2558
0
    if (address == reloc_target)
2559
0
      return true;
2560
0
  }
2561
0
    }
2562
2563
0
  return false;
2564
0
}
2565
2566
2567
/* Return the target address of a global symbol NAME, or -1 if not found.  */
2568
2569
static int
2570
avr_global_symbol_address (struct bfd_link_info *link_info, const char *name)
2571
0
{
2572
0
  struct bfd_link_hash_entry *h;
2573
0
  h = bfd_link_hash_lookup (link_info->hash, name,
2574
0
          false /*create*/, false /*copy*/, true /*follow*/);
2575
0
  if (h)
2576
0
    {
2577
0
      const bfd_vma value = h->u.def.value;
2578
0
      const asection *sec = h->u.def.section;
2579
0
      return (int) (value
2580
0
        + sec->output_offset
2581
0
        + sec->output_section->vma);
2582
0
    }
2583
2584
0
  return -1;
2585
0
}
2586
2587
2588
/* Return NULL or the name of the global symbol used by reloc REL.  */
2589
2590
static const char *
2591
avr_reloc_global_symbol_name (bfd *abfd, Elf_Internal_Rela *rel)
2592
0
{
2593
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2594
2595
0
  if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
2596
    // This is a local symbol.
2597
0
    return NULL;
2598
2599
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
2600
0
  struct elf_link_hash_entry *h;
2601
0
  h = sym_hashes[ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info];
2602
0
  if (h != NULL)
2603
0
    {
2604
0
      while (h->root.type == bfd_link_hash_indirect
2605
0
       || h->root.type == bfd_link_hash_warning)
2606
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
2607
2608
0
      return h->root.root.string;
2609
0
    }
2610
2611
0
  return NULL;
2612
0
}
2613
2614
2615
// AVR-LibC produces vectabs with up to 128 entries at most.
2616
0
#define N_IRQS 128
2617
2618
// Symbol name of an unused vectab entry.  Vectab entries that target
2619
// the address of this global symbol are regarded as unused.
2620
0
#define S_BAD_IRQ "__bad_interrupt"
2621
2622
// An antry in the vector table.
2623
typedef struct
2624
{
2625
  int idx;
2626
  // Targte address of jump.
2627
  unsigned symval;
2628
  Elf_Internal_Rela *reloc;
2629
  // Code address of the jump / vectab entry.
2630
  bfd_vma dot;
2631
  // Whether something is targeting the vectab entry.
2632
  bool has_label_or_reloc;
2633
} avr_irq_t;
2634
2635
// The entire vector table.
2636
typedef struct
2637
{
2638
  // Size in bytes of .vectors without an optional trailing CLH.
2639
  unsigned size;
2640
  // The number of (expected) IRQs.
2641
  int n_irqs;
2642
  // The size of one entry is 2 bytes or 4 bytes.
2643
  int vec_size;
2644
  // Code address of __bad_interrupt.
2645
  int bad_irq;
2646
  // The very table.
2647
  avr_irq_t irqs[N_IRQS];
2648
  // Whether vectab pruning has been attempted.
2649
  bool pruned;
2650
} avr_vectab_t;
2651
2652
2653
/* Traverse all input bfds and find all CALL / JMP relocs that point
2654
   into the vectab.  This allows code like
2655
2656
  JMP __vectors + 0x8
2657
2658
   to work as expected, i.e. the respective IRQ entry won't be pruned.
2659
   Notice that  JMP 0x8  is *NOT SUPPORTED* since it doesn't have a
2660
   reloc, and hence we can't find it without scanning the entire code.
2661
   Set  vtab->irqs[].has_label_or_reloc  when such a reloc is found.  */
2662
2663
static bool
2664
avr_find_relocs_to_vectab (struct bfd_link_info *link_info, avr_vectab_t *vtab)
2665
0
{
2666
0
  for (bfd *abfd = link_info->input_bfds; abfd; abfd = abfd->link.next)
2667
0
    {
2668
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2669
2670
0
      for (struct bfd_section *sec = abfd->sections; sec; sec = sec->next)
2671
0
  {
2672
0
    if (sec->reloc_count == 0
2673
0
        || (sec->flags & SEC_RELOC) == 0
2674
0
        || (sec->flags & SEC_HAS_CONTENTS) == 0
2675
0
        || (sec->flags & SEC_CODE) == 0)
2676
0
      continue;
2677
2678
0
    Elf_Internal_Rela *rel = elf_section_data (sec)->relocs;
2679
0
    if (rel == NULL)
2680
0
      rel = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
2681
2682
0
    Elf_Internal_Rela *relend = rel + sec->reloc_count;
2683
0
    for (; rel && rel < relend; ++rel)
2684
0
      {
2685
0
        if (ELF32_R_SYM (rel->r_info) >= symtab_hdr->sh_info
2686
0
      && (ELF32_R_TYPE (rel->r_info) == R_AVR_CALL
2687
0
          || ELF32_R_TYPE (rel->r_info) == R_AVR_13_PCREL))
2688
0
    {
2689
      // A global symbol.
2690
0
      const char *sname = avr_reloc_global_symbol_name (abfd, rel);
2691
0
      if (sname && !strcmp (sname, "__vectors"))
2692
0
        {
2693
0
          const int idx = rel->r_addend / vtab->vec_size;
2694
0
          if (idx >= 0 && idx < vtab->n_irqs)
2695
0
      vtab->irqs[idx].has_label_or_reloc = true;
2696
0
        }
2697
0
    } // Jump / call related
2698
0
      } // Relocs
2699
0
  } // Input sections
2700
0
    } // BFDs
2701
2702
0
  return false;
2703
0
}
2704
2705
2706
/* Fill *vtab according to an AVR-LibC vector table as found in section *sec
2707
   and return NULL.  When the vector table is not as expected, then return
2708
   the respective reason as a static constant string.  */
2709
2710
static const char *
2711
avr_init_vectab (bfd *abfd, struct bfd_link_info *link_info, asection *sec,
2712
     Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
2713
     avr_vectab_t *vtab)
2714
0
{
2715
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2716
2717
  // Look up __bad_interrupt in the linker's global hash table.
2718
0
  vtab->bad_irq = avr_global_symbol_address (link_info, S_BAD_IRQ);
2719
0
  if (vtab->bad_irq < 0)
2720
0
    return S_BAD_IRQ " not found";
2721
2722
0
  rout ("Prune: %s = 0x%x\n", S_BAD_IRQ, vtab->bad_irq);
2723
2724
  // The very first word must be a JMP or a RJMP.  It heralds the
2725
  // sizes of all the following entries.
2726
0
  const uint16_t code_word0 = avr_word (abfd, contents + 0);
2727
0
  vtab->vec_size = 0?0
2728
0
    : avr_is_RJMP (code_word0) ? 2
2729
0
    : avr_is_JMP (code_word0) ? 4
2730
0
    : 0;
2731
2732
0
  if (vtab->vec_size == 0)
2733
0
    return "vectab doesn't start with JMP nor RJMP";
2734
2735
0
  vtab->size = (unsigned) sec->size;
2736
0
  const uint16_t code_last = avr_word (abfd, contents + vtab->size - 2);
2737
2738
0
  if (vtab->vec_size == 4
2739
0
      && vtab->size % 4 == 2
2740
0
      && avr_is_CLH (code_last))
2741
0
    {
2742
      // This is the case for AVR-SD devices that need a valid insn after each
2743
      // executed instruction.  AVR-LibC inserts a CLH after the very vectab.
2744
0
      rout ("Prune: vectab has a trailing CLH\n");
2745
0
      vtab->size -= 2;
2746
0
    }
2747
0
  else if (vtab->size % vtab->vec_size != 0)
2748
0
    return "odd vectab layout";
2749
2750
0
  vtab->n_irqs = vtab->size / vtab->vec_size;
2751
2752
0
  rout ("Prune: expecting %d %sJMP entries\n", vtab->n_irqs,
2753
0
  vtab->vec_size == 2 ? "R" : "");
2754
2755
0
  if (vtab->n_irqs > N_IRQS)
2756
0
    return "vectab has more than 128 entries";
2757
2758
  // As soon as we know the vec_size, find any relocs that point
2759
  // into the vectab.  Mark .has_label_or_reloc accordingly.
2760
0
  avr_find_relocs_to_vectab (link_info, vtab);
2761
2762
0
  int n_irqs = 0;
2763
0
  Elf_Internal_Rela *irelend = internal_relocs + sec->reloc_count;
2764
0
  for (Elf_Internal_Rela *irel = internal_relocs; irel < irelend; ++irel)
2765
0
    {
2766
0
      rout ("Prune: reloc #%d off=%x, add=%x", (int) (irel - internal_relocs),
2767
0
      (int) irel->r_offset, (int) irel->r_addend);
2768
2769
0
      if (irel->r_addend)
2770
0
  return "non-zero addend";
2771
2772
      // The size in bytes of the entry.
2773
0
      const int sz = 0?0
2774
0
  : ELF32_R_TYPE (irel->r_info) == R_AVR_13_PCREL ? 2
2775
0
  : ELF32_R_TYPE (irel->r_info) == R_AVR_CALL ? 4
2776
0
  : 0;
2777
2778
      // The sizes of all relocs must be the same, otherwise it's not
2779
      // an AVR-LibC vector table.
2780
0
      if (sz == 0
2781
0
    || (vtab->vec_size != 0 && sz != vtab->vec_size))
2782
0
  return "unexpected reloc";
2783
2784
0
      vtab->vec_size = sz;
2785
2786
0
      if (irel->r_offset % sz != 0
2787
0
    || irel->r_offset + sz > vtab->size)
2788
0
  return "unexpected reloc offset";
2789
2790
0
      rout (" %s", sz == 2 ? "R_AVR_13_PCREL" : "R_AVR_CALL");
2791
2792
0
      const int idx = irel->r_offset / sz;
2793
0
      if (idx >= N_IRQS)
2794
0
  return "vectab has more than 128 entries";
2795
2796
0
      avr_irq_t *irq = &vtab->irqs[idx];
2797
2798
0
      if (irq->reloc != NULL)
2799
0
  return "overlapping relocs";
2800
2801
0
      irq->idx = idx;
2802
0
      irq->reloc = irel;
2803
0
      n_irqs += 1;
2804
2805
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2806
0
  return "symbol is not global";
2807
0
      else
2808
0
  {
2809
    // Symbol is external.  Get its value.
2810
0
    unsigned long i = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2811
0
    struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[i];
2812
0
    BFD_ASSERT (h != NULL);
2813
0
    if (h->root.type != bfd_link_hash_defined
2814
0
        && h->root.type != bfd_link_hash_defweak)
2815
0
      return "symbol is not defined";
2816
2817
0
    irq->symval = (h->root.u.def.value
2818
0
       + h->root.u.def.section->output_section->vma
2819
0
       + h->root.u.def.section->output_offset);
2820
0
  }
2821
2822
0
      rout (" symval=0x%x idx=%d", irq->symval, irq->idx);
2823
2824
0
      const char *s_code = NULL;
2825
0
      const uint16_t code_word = avr_word (abfd, contents + irel->r_offset);
2826
0
      if (vtab->vec_size == 2 && avr_is_RJMP (code_word))
2827
0
  s_code = "rjmp";
2828
0
      else if (vtab->vec_size == 4 && avr_is_JMP (code_word))
2829
0
  s_code = "jmp";
2830
0
      else
2831
0
  return "unexpected opcode";
2832
2833
      // Get the address of this instruction.
2834
0
      irq->dot = sec->output_section->vma + sec->output_offset + irel->r_offset;
2835
2836
0
      rout (" %s .=0x%x", s_code, (unsigned) irq->dot);
2837
2838
0
      if (idx == 0
2839
0
    || avr_local_label_at (abfd, sec, symtab_hdr, irel->r_offset)
2840
0
    || avr_global_label_at (abfd, sec, symtab_hdr, irel->r_offset))
2841
0
  irq->has_label_or_reloc = true;
2842
2843
0
      if (irq->has_label_or_reloc)
2844
0
  rout (" label_or_reloc");
2845
0
      if (irq->symval != (unsigned) vtab->bad_irq)
2846
0
  rout (" required");
2847
2848
0
      rout ("\n");
2849
0
    } // Relocs
2850
2851
0
  if (n_irqs != vtab->n_irqs)
2852
0
    return "incomplete vectab";
2853
2854
  // All is fine and *vtab has been initialized.
2855
0
  return NULL;
2856
0
}
2857
2858
2859
/* Try to prune a vector table from AVR-LibC.  When entries are unused, i.e.
2860
   they effectively jump to __bad_interrupt, then we can remove them provided
2861
   all later entries can also be removed.  The trigger is --prune-vectab,
2862
   which only takes effect when linker relaxation is on.
2863
   return 1:  Vectab has just been changed.
2864
   return 0:  Nothing changed and no internal error occurred.
2865
   return -1: An error occurred.  */
2866
2867
static int
2868
avr_maybe_prune_vectab (bfd *abfd, struct bfd_link_info *link_info,
2869
      asection *sec, Elf_Internal_Rela *internal_relocs,
2870
      bfd_byte **pcontents)
2871
0
{
2872
0
  static avr_vectab_t avr_vectab;
2873
0
  avr_vectab_t *vtab = &avr_vectab;
2874
2875
0
  if (!avr_prune_vectab || vtab->pruned || strcmp (sec->name, ".vectors"))
2876
0
    return 0;
2877
2878
0
  vtab->pruned = true;
2879
2880
0
  rout ("== Pruning .vectors of size 0x%x\n", (unsigned) sec->size);
2881
2882
  // Get a cached copy of the contents if it exists.
2883
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
2884
0
    *pcontents = elf_section_data (sec)->this_hdr.contents;
2885
0
  else
2886
    // Go get them off disk.
2887
0
    if (! bfd_malloc_and_get_section (abfd, sec, pcontents))
2888
0
      {
2889
0
  rout ("Prune failed: error reading contents\n");
2890
0
  return -1;
2891
0
      }
2892
2893
  // Init the vtab structure, or give a reason for why it's not a vector table.
2894
0
  const char *reason = avr_init_vectab (abfd, link_info, sec, internal_relocs,
2895
0
          *pcontents, vtab);
2896
0
  if (reason)
2897
0
    {
2898
0
      rout ("\nPrune failed: %s\n", reason);
2899
0
      return 0;
2900
0
    }
2901
2902
  // Finally, we have all the checks done and data in place, so that
2903
  // we can perform the actual pruning.
2904
2905
0
  int last_idx = 0;
2906
0
  for (int i = 0; i < vtab->n_irqs; ++i)
2907
0
    if (vtab->irqs[i].has_label_or_reloc
2908
0
  || vtab->irqs[i].symval != (unsigned) vtab->bad_irq)
2909
0
      last_idx = i;
2910
2911
0
  const int n_delete = (vtab->n_irqs - 1 - last_idx) * vtab->vec_size;
2912
0
  rout ("Prune: last remaining irq:%d, pruning 0x%x=%d bytes from %d irqs\n",
2913
0
  last_idx, n_delete, n_delete, n_delete / vtab->vec_size);
2914
0
  if (n_delete == 0)
2915
0
    return 0;
2916
2917
0
  rout ("Prune: remove irq");
2918
2919
0
  elf_section_data (sec)->this_hdr.contents = *pcontents;
2920
2921
0
  for (int i = vtab->n_irqs - 1; i > last_idx; --i)
2922
0
    {
2923
0
      const avr_irq_t *irq = &vtab->irqs[i];
2924
2925
0
      rout (" %d", irq->idx);
2926
0
      if (!elf32_avr_relax_delete_bytes (abfd, sec, irq->reloc->r_offset,
2927
0
           vtab->vec_size, true))
2928
0
  return -1;
2929
      // Avoid relocs to point outside of the section.
2930
      // This is needed even for R_AVR_NONE.
2931
0
      irq->reloc->r_offset = 0;
2932
0
      irq->reloc->r_info = R_AVR_NONE;
2933
0
    }
2934
2935
0
  rout ("\nPrune vectab accomplished.\n");
2936
2937
0
  return 1;
2938
0
}
2939
2940
2941
/* This function handles relaxing for the avr.
2942
   Many important relaxing opportunities within functions are already
2943
   realized by the compiler itself.  Though when the back end emits
2944
   calls by hand, that is bypassing tail call optimizations, and it may
2945
   lead to [R]CALL+RET sequences.
2946
2947
   Here we try to replace  CALL (4 bytes) ->  RCALL (2 bytes)
2948
   and JMP -> RJMP (safes also 2 bytes).
2949
2950
   As well we now optimize sequences of
2951
      CALL/RCALL function
2952
      RET
2953
   to the faster and less stack consuming
2954
      JMP/RJMP function
2955
      RET
2956
2957
   In case where the RET is not reachable by a label, a skip instruction
2958
   or a reloc, that RET is optimized out.  In order to check if the RET is
2959
   no longer needed, it is checked that
2960
   . there is no jump or branch in the same section targeting the RET, and
2961
   . there is no skip instruction before the JMP/RJMP, and
2962
   . there is no local or global label placed at RET, and
2963
   . there is no reloc pointing to the RET.
2964
2965
   We refrain from relaxing within sections .vectors and .jumptables in
2966
   order to maintain the position of the instructions.  There, however,
2967
   we substitute JMP / CALL by a sequence RJMP+NOP / RCALL+NOP if possible.
2968
2969
   Though in .vectors we remove unused vector entries if --prune-vectab.
2970
   These are entries that target __bad_interrupt (which is the weak default
2971
   for all the vectors), and where all the following entries are also unused.
2972
   This is performed by avr_maybe_prune_vectab() above, which runs prior to
2973
   the very relaxing of .vectors.
2974
2975
   The .jumptables section is meant to be used for a future tablejump variant
2976
   for the devices with 3-byte program counter where the table itself contains
2977
   4-byte jump instructions whose relative offset must not be changed.
2978
2979
   Finally, we elide RJMP instructions that are void and not a delay.
2980
   This may occur when a function is tailcalling some other function,
2981
   and the latter happens to be located right after the former.  */
2982
2983
static bool
2984
elf32_avr_relax_section (bfd *abfd,  asection *sec,
2985
       struct bfd_link_info *link_info, bool *again)
2986
0
{
2987
0
  Elf_Internal_Shdr *symtab_hdr;
2988
0
  Elf_Internal_Rela *internal_relocs;
2989
0
  Elf_Internal_Rela *irel, *irelend;
2990
0
  bfd_byte *contents = NULL;
2991
0
  Elf_Internal_Sym *isymbuf = NULL;
2992
0
  elf32_avr_link_hash_table_t *htab;
2993
0
  static bool relaxation_initialised = false;
2994
2995
0
  if (!relaxation_initialised)
2996
0
    {
2997
0
      relaxation_initialised = true;
2998
2999
      /* Load entries from the .avr.prop sections.  */
3000
0
      avr_load_all_property_sections (link_info);
3001
0
    }
3002
3003
  /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
3004
     relaxing. Such shrinking can cause issues for the sections such
3005
     as .vectors and .jumptables. Instead, the unused bytes should be
3006
     filled with NOP instructions. */
3007
0
  bool shrinkable = true;
3008
3009
0
  if (!strcmp (sec->name, ".vectors")
3010
0
      || !strcmp (sec->name, ".jumptables"))
3011
0
    shrinkable = false;
3012
3013
0
  if (bfd_link_relocatable (link_info))
3014
0
    link_info->callbacks->fatal
3015
0
      (_("%P: --relax and -r may not be used together\n"));
3016
3017
0
  htab = avr_link_hash_table (link_info);
3018
0
  if (htab == NULL)
3019
0
    return false;
3020
3021
  /* Assume nothing changes.  */
3022
0
  *again = false;
3023
3024
0
  if (!htab->no_stubs && sec == htab->stub_sec)
3025
0
    {
3026
      /* We are just relaxing the stub section.
3027
   Let's calculate the size needed again.  */
3028
0
      bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
3029
3030
0
      if (debug_relax)
3031
0
  printf ("Relaxing the stub section. Size prior to this pass: %i\n",
3032
0
    (int) last_estimated_stub_section_size);
3033
3034
0
      elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
3035
0
          link_info, false);
3036
3037
      /* Check if the number of trampolines changed.  */
3038
0
      if (last_estimated_stub_section_size != htab->stub_sec->size)
3039
0
  *again = true;
3040
3041
0
      if (debug_relax)
3042
0
  printf ("Size of stub section after this pass: %i\n",
3043
0
    (int) htab->stub_sec->size);
3044
3045
0
      return true;
3046
0
    }
3047
3048
  /* We don't have to do anything for a relocatable link, if this section
3049
     does not have relocs, or if this is not a code section.  */
3050
0
  if (bfd_link_relocatable (link_info)
3051
0
      || sec->reloc_count == 0
3052
0
      || (sec->flags & SEC_RELOC) == 0
3053
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
3054
0
      || (sec->flags & SEC_CODE) == 0)
3055
0
    return true;
3056
3057
  /* Check if the object file to relax uses internal symbols so that we
3058
     could fix up the relocations.  */
3059
0
  if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
3060
0
    return true;
3061
3062
0
  symtab_hdr = &elf_symtab_hdr (abfd);
3063
3064
  /* Get a copy of the native relocations.  */
3065
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3066
0
                 link_info->keep_memory);
3067
0
  if (internal_relocs == NULL)
3068
0
    goto error_return;
3069
3070
0
  const int mpv = avr_maybe_prune_vectab (abfd, link_info, sec,
3071
0
            internal_relocs, &contents);
3072
0
  if (mpv < 0)
3073
0
    goto error_return;
3074
0
  else if (mpv > 0)
3075
0
    *again = true;
3076
3077
  /* Walk through the relocs looking for relaxing opportunities.  */
3078
0
  irelend = internal_relocs + sec->reloc_count;
3079
0
  for (irel = internal_relocs; irel < irelend && !mpv; irel++)
3080
0
    {
3081
0
      bfd_vma symval;
3082
0
      bool sym_is_global = false;
3083
3084
0
      if (ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
3085
0
    && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
3086
0
    && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
3087
0
  continue;
3088
3089
      /* Get the section contents if we haven't done so already.  */
3090
0
      if (contents == NULL)
3091
0
  {
3092
    /* Get cached copy if it exists.  */
3093
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
3094
0
      contents = elf_section_data (sec)->this_hdr.contents;
3095
0
    else
3096
0
      {
3097
        /* Go get them off disk.  */
3098
0
        if (! bfd_malloc_and_get_section (abfd, sec, &contents))
3099
0
    goto error_return;
3100
0
      }
3101
0
  }
3102
3103
      /* Read this BFD's local symbols if we haven't done so already.  */
3104
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3105
0
  {
3106
0
    isymbuf = avr_read_symbuf (abfd, symtab_hdr);
3107
0
    if (isymbuf == NULL)
3108
0
      goto error_return;
3109
0
  }
3110
3111
      /* Get the value of the symbol referred to by the reloc.  */
3112
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3113
0
  {
3114
    /* A local symbol.  */
3115
0
    Elf_Internal_Sym *isym;
3116
0
    asection *sym_sec;
3117
3118
0
    isym = isymbuf + ELF32_R_SYM (irel->r_info);
3119
0
    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3120
0
    symval = isym->st_value;
3121
    /* If the reloc is absolute, it will not have
3122
       a symbol or section associated with it.  */
3123
0
    if (sym_sec)
3124
0
      symval += (sym_sec->output_section->vma
3125
0
           + sym_sec->output_offset);
3126
0
  }
3127
0
      else
3128
0
  {
3129
0
    unsigned long indx;
3130
0
    struct elf_link_hash_entry *h;
3131
3132
    /* An external symbol.  */
3133
0
    indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3134
0
    h = elf_sym_hashes (abfd)[indx];
3135
0
    BFD_ASSERT (h != NULL);
3136
0
    if (h->root.type != bfd_link_hash_defined
3137
0
        && h->root.type != bfd_link_hash_defweak)
3138
      /* This appears to be a reference to an undefined
3139
         symbol.  Just ignore it -- it will be caught by the
3140
         regular reloc processing.  */
3141
0
      continue;
3142
3143
0
    symval = (h->root.u.def.value
3144
0
        + h->root.u.def.section->output_section->vma
3145
0
        + h->root.u.def.section->output_offset);
3146
0
    sym_is_global = true;
3147
0
  }
3148
3149
      /* For simplicity of coding, we are going to modify the section
3150
   contents, the section relocs, and the BFD symbol table.  We
3151
   must tell the rest of the code not to free up this
3152
   information.  It would be possible to instead create a table
3153
   of changes which have to be made, as is done in coff-mips.c;
3154
   that would be more work, but would require less memory when
3155
   the linker is run.  */
3156
0
      switch (ELF32_R_TYPE (irel->r_info))
3157
0
  {
3158
    /* Try to turn a 22-bit absolute CALL/JMP into an 13-bit
3159
       PC-relative RCALL/RJMP.  */
3160
0
  case R_AVR_CALL:
3161
0
    {
3162
0
      bfd_vma value = symval + irel->r_addend;
3163
0
      bool distance_short_enough = false;
3164
3165
      /* Get the address of this instruction.  */
3166
0
      const bfd_vma dot = (sec->output_section->vma
3167
0
         + sec->output_offset + irel->r_offset);
3168
3169
      /* Compute the distance from this insn to the branch target.  */
3170
0
      const bfd_vma gap = value - dot;
3171
3172
      /* The ISA manual states that addressable range is PC - 2k + 1 to
3173
         PC + 2k. In bytes, that would be -4094 <= PC <= 4096. The range
3174
         is shifted one word to the right, since pc-relative instructions
3175
         implicitly add one word, i.e. "RJMP 0" jumps to next insn, not
3176
         the current one.
3177
         Therefore, for the !shrinkable case, the range is as above.
3178
         If shrinkable, then the current code only deletes bytes 3 and 4
3179
         of the absolute CALL/JMP, so the forward jump range increases
3180
         by 2 bytes, but the backward (negative) jump range remains
3181
         the same. */
3182
3183
      /* Check if the gap falls in the range that can be accommodated
3184
         in 13bits signed.  As we are dealing with wird addressing,
3185
         this becomes 12bits when encoded.  */
3186
0
      if (!shrinkable && ((int) gap >= -4094 && (int) gap <= 4096))
3187
0
        distance_short_enough = true;
3188
      /* If shrinkable, then we can check for a range of distance which
3189
         is two bytes farther on the positive direction because the call
3190
         or jump target will be closer by two bytes after the
3191
         relaxation. */
3192
0
      else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4098))
3193
0
        distance_short_enough = true;
3194
3195
      /* Here we handle the wrap-around case.  E.g. for a 16k device
3196
         we could use a RJMP to jump from address 0x100 to 0x3d00!
3197
         In order to make this work properly, we need to fill the
3198
         variable avr_pc_wrap_around with the appropriate value.
3199
         I.e. 0x4000 for a 16k device.  */
3200
3201
      /* Shrinking the code size makes the gaps larger in the case of
3202
         wrap-arounds.  So we use a heuristical safety margin to avoid
3203
         that during relax the distance gets again too large for the
3204
         short jumps.  Let's assume a typical code-size reduction due
3205
         to relax for a 16k device of 600 bytes.  So let's use twice
3206
         the typical value as safety margin.  */
3207
3208
0
      int assumed_shrink = avr_pc_wrap_around > 0x4000 ? 900 : 600;
3209
3210
0
      int safety_margin = 2 * assumed_shrink;
3211
0
      int rgap = avr_relative_distance_considering_wrap_around (gap);
3212
3213
0
      if (rgap >= -4092 + safety_margin
3214
0
    && rgap <= 4094 - safety_margin)
3215
0
        distance_short_enough = true;
3216
3217
0
      if (distance_short_enough)
3218
0
        {
3219
0
    if (debug_relax)
3220
0
      printf ("shrinking jump/call instruction at address 0x%x "
3221
0
        "in section %s\n\n", (int) dot, sec->name);
3222
3223
    // Note that we've changed the relocs, section contents, etc.
3224
0
    elf_section_data (sec)->relocs = internal_relocs;
3225
0
    elf_section_data (sec)->this_hdr.contents = contents;
3226
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
3227
3228
    // Get the instruction code for relaxing.
3229
0
    uint16_t code_word = avr_word (abfd, contents + irel->r_offset);
3230
3231
0
    if (avr_is_CALL (code_word))
3232
0
      {
3233
        // We are changing CALL -> RCALL.
3234
0
        bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
3235
0
        bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
3236
0
      }
3237
0
    else if (avr_is_JMP (code_word))
3238
0
      {
3239
        // We are changing JMP -> RJMP.
3240
0
        bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
3241
0
        bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
3242
0
      }
3243
0
    else
3244
0
      abort ();
3245
3246
    // Fix the relocation's type.
3247
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3248
0
               R_AVR_13_PCREL);
3249
3250
    // Allow to ditch the final NOP?
3251
0
    bfd_byte *pmaybe_clh = contents + irel->r_offset + 4;
3252
0
    const bool may_prune_last_vectab_entry
3253
0
      = (avr_prune_vectab
3254
0
         && !strcmp (sec->name, ".vectors")
3255
0
         && (irel->r_offset + 4 == sec->size
3256
0
       || (irel->r_offset + 6 == sec->size
3257
0
           && avr_is_CLH (avr_word (abfd, pmaybe_clh)))));
3258
3259
    // We should not modify the ordering if 'shrinkable' is FALSE.
3260
0
    if (!shrinkable
3261
        // ...except --prune-vectab allows it for the last entry.
3262
0
        && !may_prune_last_vectab_entry)
3263
0
      {
3264
        // Let's insert a NOP.
3265
0
        bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
3266
0
        bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
3267
0
      }
3268
0
    else
3269
0
      {
3270
        // Delete two bytes of data.
3271
0
        if (!elf32_avr_relax_delete_bytes (abfd, sec,
3272
0
                   irel->r_offset + 2, 2,
3273
0
                   true))
3274
0
          goto error_return;
3275
3276
        // That will change things, so, we should relax again.
3277
        // Note that this is not required, and it may be slow.
3278
0
        *again = true;
3279
0
      }
3280
0
        } // Distance short enough
3281
0
    } // case R_AVR_CALL
3282
    /* Fall through.  */
3283
3284
0
  default:
3285
0
    {
3286
0
      uint16_t code_word = avr_word (abfd, contents + irel->r_offset);
3287
3288
      // Get the address of this instruction.
3289
0
      bfd_vma dot = (sec->output_section->vma
3290
0
         + sec->output_offset + irel->r_offset);
3291
3292
      // Look for RCALL+RET or CALL+RET sequences that can
3293
      // be safely replaced by RJMP+RET or JMP+RET.
3294
0
      if (avr_replace_call_ret_sequences
3295
0
    && avr_is_RCALL (code_word))
3296
0
        {
3297
    // This insn is a RCALL.
3298
0
    const bool has_next = irel->r_offset + 3 < sec->size;
3299
0
    const uint16_t next_word = has_next
3300
0
      ? avr_word (abfd, contents + irel->r_offset + 2)
3301
0
      : 0;
3302
3303
0
    if (!avr_is_RET (next_word))
3304
0
      break;
3305
3306
    // The next insn is a RET.  Convert the RCALL insn to a RJMP.
3307
0
    const uint8_t code_msb = (code_word >> 8) & 0xef;
3308
0
    bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
3309
0
    if (debug_relax)
3310
0
      printf ("converted rcall/ret sequence at address 0x%x "
3311
0
        "into rjmp/ret sequence in section %s\n\n",
3312
0
        (int) dot, sec->name);
3313
0
    *again = true;
3314
0
    break;
3315
0
        }
3316
0
      else if (avr_replace_call_ret_sequences
3317
0
         && avr_is_CALL (code_word))
3318
0
        {
3319
    // This insn is a CALL.
3320
0
    const bool has_next = irel->r_offset + 5 < sec->size;
3321
0
    const uint16_t next_word = has_next
3322
0
      ? avr_word (abfd, contents + irel->r_offset + 4)
3323
0
      : 0;
3324
3325
0
    if (!avr_is_RET (next_word))
3326
0
      break;
3327
3328
    // The next insn is a RET.  Convert the CALL insn into a JMP.
3329
0
    const uint8_t code_lsb = code_word & 0xfd;
3330
0
    bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
3331
0
    if (debug_relax)
3332
0
      printf ("converted call/ret sequence at address 0x%x "
3333
0
        "into jmp/ret sequence in section %s\n\n",
3334
0
        (int) dot, sec->name);
3335
0
    *again = true;
3336
0
    break;
3337
0
        }
3338
0
      else if (avr_elide_rjmp0
3339
         // Elide no-op RJMP tail calls like in
3340
         //    RJMP func     ;; in module A
3341
         //    .global func  ;; in module B
3342
         //    func:
3343
0
         && avr_is_RJMP (code_word)
3344
         // Plain RJMP .+0 is used by GCC to delay 2 cycles, thus
3345
         // we are only interested in global jump targets...
3346
0
         && sym_is_global
3347
         // ...without offset, and...
3348
0
         && irel->r_addend == 0
3349
         // ...where the RJMP targets the insn directly after it.
3350
0
         && symval + irel->r_addend == dot + 2)
3351
0
        {
3352
0
    if (debug_relax)
3353
0
      printf ("found rjmp .+0 at address 0x%x in section %s\n",
3354
0
        (int) dot, sec->name);
3355
3356
0
    const bool has_prev = irel->r_offset >= 2;
3357
0
    const uint16_t prev_word = has_prev
3358
0
      ? avr_word (abfd, contents + irel->r_offset - 2)
3359
0
      : 0;
3360
3361
    // The assumption in the following condition is that there is
3362
    // no dangling skip at the end of a section.  Note that a skip
3363
    // insn at that place doesn't make sense in a real program.
3364
0
    if (has_prev
3365
0
        && avr_is_skip (prev_word))
3366
0
      {
3367
0
        if (debug_relax)
3368
0
          printf ("skip insn prevents deletion of rjmp .+0 at "
3369
0
            "address 0x%x\n", (int) dot);
3370
0
        break;
3371
0
      }
3372
3373
    // Avoid the paranoid case where the RJMP is at the end of
3374
    // the program memory and jumps to 0x0.  We don't have the
3375
    // flash size handy, so assume a size of 0.5 KiB.
3376
0
    if ((dot + 2) % 0x200 == 0)
3377
0
      {
3378
0
        if (debug_relax)
3379
0
          printf ("not deleting rjmp .+0 at address 0x%x that may "
3380
0
            "be at the end of program memory\n", (int) dot);
3381
0
        break;
3382
0
      }
3383
3384
    // Ditch the RJMP.
3385
    // Notice that labels or relocs at the RJMP are no issue.
3386
3387
0
    if (debug_relax)
3388
0
      printf ("deleted rjmp .+0 instruction at address 0x%x\n",
3389
0
        (int) dot);
3390
3391
    // Read this BFD's local symbols if we haven't done so already.
3392
0
    if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3393
0
      {
3394
0
        isymbuf = avr_read_symbuf (abfd, symtab_hdr);
3395
0
        if (isymbuf == NULL)
3396
0
          break;
3397
0
      }
3398
3399
0
    elf_section_data (sec)->relocs = internal_relocs;
3400
0
    elf_section_data (sec)->this_hdr.contents = contents;
3401
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
3402
3403
    // Delete the two RJMP bytes, and...
3404
0
    if (!elf32_avr_relax_delete_bytes (abfd, sec,
3405
0
               irel->r_offset, 2, true))
3406
0
      goto error_return;
3407
3408
    // ...decommission the reloc.
3409
0
    irel->r_info = R_AVR_NONE;
3410
3411
    // That will change things, so we should relax again.
3412
    // Note that this is not required, and it may be slow.
3413
0
    *again = true;
3414
0
    break;
3415
0
        }
3416
0
      else if (avr_is_RJMP (code_word)
3417
0
         || avr_is_JMP (code_word))
3418
0
        {
3419
0
    const int insn_size = avr_is_JMP (code_word) ? 4 : 2;
3420
0
    const unsigned int next_offset = irel->r_offset + insn_size;
3421
0
    const bool has_next = next_offset + 1 < sec->size;
3422
0
    const bool has_prev = irel->r_offset >= 2;
3423
3424
0
    const uint16_t next_word = has_next
3425
0
      ? avr_word (abfd, contents + irel->r_offset + insn_size)
3426
0
      : 0;
3427
3428
0
    if (!avr_is_RET (next_word))
3429
0
      break;
3430
3431
    // The next insn is a RET. We possibly could delete this RET.
3432
3433
0
    if (debug_relax)
3434
0
      printf ("found %s / ret sequence at address 0x%x\n",
3435
0
        insn_size == 2 ? "rjmp" : "jmp", (int) dot);
3436
3437
    /* Make sure that
3438
       - There is no skip insn preceding the jump insn, and
3439
       - there is no local label at the RET, and
3440
       - there is no global label at the RET, and
3441
       - there is no reloc at the RET.  */
3442
3443
0
    const char *s_cause = NULL;
3444
0
    const bfd_vma address_of_ret = dot + insn_size;
3445
0
    const unsigned int section_offset_of_ret = next_offset;
3446
0
    const uint16_t prev_word = has_prev
3447
0
      ? avr_word (abfd, contents + irel->r_offset - 2)
3448
0
      : 0;
3449
3450
    // The assumption in the following condition is that there
3451
    // is no dangling skip at the end of a section.
3452
0
    if (has_prev
3453
0
        && avr_is_skip (prev_word))
3454
0
      {
3455
0
        s_cause = "skip insn prior to jmp/rjmp";
3456
0
      }
3457
0
    else if (avr_local_label_at (abfd, sec, symtab_hdr,
3458
0
               section_offset_of_ret))
3459
0
      {
3460
0
        s_cause = "local label";
3461
0
      }
3462
0
    else if (avr_global_label_at (abfd, sec, symtab_hdr,
3463
0
                section_offset_of_ret))
3464
0
      {
3465
0
        s_cause = "global label";
3466
0
      }
3467
0
    else if (avr_reloc_at (abfd, symtab_hdr, &isymbuf,
3468
0
               address_of_ret))
3469
0
      {
3470
0
        s_cause = "reloc";
3471
0
      }
3472
3473
0
    if (s_cause)
3474
0
      {
3475
0
        if (debug_relax)
3476
0
          printf ("%s prevents deletion of ret insn at address "
3477
0
            "0x%x\n", s_cause, (int) address_of_ret);
3478
0
        break;
3479
0
      }
3480
3481
    // Deleting RET is safe.
3482
3483
0
    if (debug_relax)
3484
0
      printf ("unreachable ret instruction at address 0x%x "
3485
0
        "deleted.\n", (int) address_of_ret);
3486
3487
0
    elf_section_data (sec)->relocs = internal_relocs;
3488
0
    elf_section_data (sec)->this_hdr.contents = contents;
3489
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
3490
3491
    // Delete two bytes of data.
3492
0
    if (!elf32_avr_relax_delete_bytes (abfd, sec,
3493
0
               section_offset_of_ret, 2,
3494
0
               true))
3495
0
      goto error_return;
3496
3497
    // That will change things, so we should relax again.
3498
    // Note that this is not required, and it may be slow.
3499
0
    *again = true;
3500
0
    break;
3501
0
        } // if CALL, JMP, RCALL, RJMP
3502
0
      break;
3503
0
    } // default
3504
0
  } // switch ELF32_R_TYPE
3505
0
    } // for internal_relocs
3506
3507
0
  if (!*again)
3508
0
    {
3509
      /* Look through all the property records in this section to see if
3510
   there's any alignment records that can be moved.  */
3511
3512
0
      avr_relax_info_t *relax_info = get_avr_relax_info (sec);
3513
0
      if (relax_info->records.count > 0)
3514
0
  {
3515
0
    for (unsigned int i = 0; i < relax_info->records.count; ++i)
3516
0
      {
3517
0
        switch (relax_info->records.items [i].type)
3518
0
    {
3519
0
    case RECORD_ORG:
3520
0
    case RECORD_ORG_AND_FILL:
3521
0
      break;
3522
0
    case RECORD_ALIGN:
3523
0
    case RECORD_ALIGN_AND_FILL:
3524
0
      {
3525
0
        struct avr_property_record *record;
3526
0
        unsigned long bytes_to_align;
3527
0
        int count = 0;
3528
3529
        /* Look for alignment directives that have had enough
3530
           bytes deleted before them, such that the directive
3531
           can be moved backwards and still maintains the
3532
           required alignment.  */
3533
0
        record = &relax_info->records.items [i];
3534
0
        bytes_to_align
3535
0
          = (unsigned long) (1 << record->data.align.bytes);
3536
0
        while (record->data.align.preceding_deleted
3537
0
         >= bytes_to_align)
3538
0
          {
3539
0
      record->data.align.preceding_deleted -= bytes_to_align;
3540
0
      count += bytes_to_align;
3541
0
          }
3542
3543
0
        if (count > 0)
3544
0
          {
3545
0
      bfd_vma addr = record->offset;
3546
3547
      /* We can delete COUNT bytes and this alignment
3548
         directive will still be correctly aligned.
3549
         First move the alignment directive, then delete
3550
         the bytes.  */
3551
0
      record->offset -= count;
3552
0
      elf32_avr_relax_delete_bytes (abfd, sec, addr - count,
3553
0
                  count, false);
3554
0
      *again = true;
3555
0
          }
3556
0
      }
3557
0
      break;
3558
0
    }
3559
0
      }
3560
0
  }
3561
0
    }
3562
3563
0
  if (contents != NULL
3564
0
      && elf_section_data (sec)->this_hdr.contents != contents)
3565
0
    {
3566
0
      if (! link_info->keep_memory)
3567
0
  free (contents);
3568
0
      else
3569
0
  {
3570
    /* Cache the section contents for elf_link_input_bfd.  */
3571
0
    elf_section_data (sec)->this_hdr.contents = contents;
3572
0
  }
3573
0
    }
3574
3575
0
  if (elf_section_data (sec)->relocs != internal_relocs)
3576
0
    free (internal_relocs);
3577
3578
0
  return true;
3579
3580
0
 error_return:
3581
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
3582
0
    free (isymbuf);
3583
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
3584
0
    free (contents);
3585
0
  if (elf_section_data (sec)->relocs != internal_relocs)
3586
0
    free (internal_relocs);
3587
3588
0
  return false;
3589
0
}
3590
3591
/* This is a version of bfd_generic_get_relocated_section_contents
3592
   which uses elf32_avr_relocate_section.
3593
3594
   For avr it's essentially a cut and paste taken from the H8300 port.
3595
   The author of the relaxation support patch for avr had absolutely no
3596
   clue what is happening here but found out that this part of the code
3597
   seems to be important.  */
3598
3599
static bfd_byte *
3600
elf32_avr_get_relocated_section_contents
3601
  (bfd *output_bfd,
3602
   struct bfd_link_info *link_info,
3603
   const struct bfd_link_order *link_order,
3604
   bfd_byte *data, bool relocatable,
3605
   asymbol **symbols)
3606
11
{
3607
11
  Elf_Internal_Shdr *symtab_hdr;
3608
11
  asection *input_section = link_order->u.indirect.section;
3609
11
  bfd *input_bfd = input_section->owner;
3610
11
  asection **sections = NULL;
3611
11
  Elf_Internal_Rela *internal_relocs = NULL;
3612
11
  Elf_Internal_Sym *isymbuf = NULL;
3613
3614
  /* We only need to handle the case of relaxing, or of having a
3615
     particular set of section contents, specially.  */
3616
11
  if (relocatable
3617
11
      || elf_section_data (input_section)->this_hdr.contents == NULL)
3618
11
    return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3619
11
                   link_order, data,
3620
11
                   relocatable, symbols);
3621
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
3622
3623
0
  bfd_byte *orig_data = data;
3624
0
  if (data == NULL)
3625
0
    {
3626
0
      data = bfd_malloc (input_section->size);
3627
0
      if (data == NULL)
3628
0
  return NULL;
3629
0
    }
3630
0
  memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3631
0
    (size_t) input_section->size);
3632
3633
0
  if ((input_section->flags & SEC_RELOC) != 0
3634
0
      && input_section->reloc_count > 0)
3635
0
    {
3636
0
      asection **secpp;
3637
0
      Elf_Internal_Sym *isym, *isymend;
3638
0
      bfd_size_type amt;
3639
3640
0
      internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3641
0
               NULL, NULL, false);
3642
0
      if (internal_relocs == NULL)
3643
0
  goto error_return;
3644
3645
0
      if (symtab_hdr->sh_info != 0)
3646
0
  {
3647
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3648
0
    if (isymbuf == NULL)
3649
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3650
0
              symtab_hdr->sh_info, 0,
3651
0
              NULL, NULL, NULL);
3652
0
    if (isymbuf == NULL)
3653
0
      goto error_return;
3654
0
  }
3655
3656
0
      amt = symtab_hdr->sh_info;
3657
0
      amt *= sizeof (asection *);
3658
0
      sections = bfd_malloc (amt);
3659
0
      if (sections == NULL && amt != 0)
3660
0
  goto error_return;
3661
3662
0
      isymend = isymbuf + symtab_hdr->sh_info;
3663
0
      for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3664
0
  {
3665
0
    asection *isec;
3666
3667
0
    if (isym->st_shndx == SHN_UNDEF)
3668
0
      isec = bfd_und_section_ptr;
3669
0
    else if (isym->st_shndx == SHN_ABS)
3670
0
      isec = bfd_abs_section_ptr;
3671
0
    else if (isym->st_shndx == SHN_COMMON)
3672
0
      isec = bfd_com_section_ptr;
3673
0
    else
3674
0
      isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3675
3676
0
    *secpp = isec;
3677
0
  }
3678
3679
0
      if (! elf32_avr_relocate_section (link_info, input_bfd,
3680
0
          input_section, data, internal_relocs,
3681
0
          isymbuf, sections))
3682
0
  goto error_return;
3683
3684
0
      free (sections);
3685
0
      if (symtab_hdr->contents != (unsigned char *) isymbuf)
3686
0
  free (isymbuf);
3687
0
      if (elf_section_data (input_section)->relocs != internal_relocs)
3688
0
  free (internal_relocs);
3689
0
    }
3690
3691
0
  return data;
3692
3693
0
 error_return:
3694
0
  free (sections);
3695
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
3696
0
    free (isymbuf);
3697
0
  if (elf_section_data (input_section)->relocs != internal_relocs)
3698
0
    free (internal_relocs);
3699
0
  if (orig_data == NULL)
3700
0
    free (data);
3701
0
  return NULL;
3702
0
}
3703
3704
3705
/* Determines the hash entry name for a particular reloc. It consists of
3706
   the identifier of the symbol section and the added reloc addend and
3707
   symbol offset relative to the section the symbol is attached to.  */
3708
3709
static char *
3710
avr_stub_name (const asection *symbol_section, const bfd_vma symbol_offset,
3711
         const Elf_Internal_Rela *rela)
3712
0
{
3713
0
  bfd_size_type len = 8 + 1 + 8 + 1 + 1;
3714
0
  char *stub_name = bfd_malloc (len);
3715
0
  if (stub_name != NULL)
3716
0
    sprintf (stub_name, "%08x+%08x",
3717
0
       symbol_section->id & 0xffffffff,
3718
0
       (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
3719
3720
0
  return stub_name;
3721
0
}
3722
3723
3724
/* Add a new stub entry to the stub hash.  Not all fields of the new
3725
   stub entry are initialised.  */
3726
3727
static elf32_avr_stub_hash_entry_t *
3728
avr_add_stub (const char *stub_name, elf32_avr_link_hash_table_t *htab)
3729
0
{
3730
0
  elf32_avr_stub_hash_entry_t *hsh;
3731
3732
  /* Enter this entry into the linker stub hash table.  */
3733
0
  hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, true, false);
3734
3735
0
  if (hsh == NULL)
3736
0
    {
3737
      /* xgettext:c-format */
3738
0
      _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
3739
0
      return NULL;
3740
0
    }
3741
3742
0
  hsh->stub_offset = 0;
3743
0
  return hsh;
3744
0
}
3745
3746
/* We assume that there is already space allocated for the stub section
3747
   contents, and that before building the stubs the section size is
3748
   initialized to 0.  We assume that within the stub hash table entry,
3749
   the absolute position of the jmp target has been written in the
3750
   target_value field.  We write here the offset of the generated JMP insn
3751
   relative to the trampoline section start to the stub_offset entry in
3752
   the stub hash table entry.  */
3753
3754
static  bool
3755
avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3756
0
{
3757
  /* Basic opcode */
3758
0
  bfd_vma jmp_insn = 0x0000940c;
3759
3760
  /* Massage our args to the form they really have.  */
3761
0
  elf32_avr_stub_hash_entry_t *hsh = avr_stub_hash_entry (bh);
3762
3763
0
  if (!hsh->is_actually_needed)
3764
0
    return true;
3765
3766
0
  struct bfd_link_info *info = (struct bfd_link_info *) in_arg;
3767
3768
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
3769
0
  if (htab == NULL)
3770
0
    return false;
3771
3772
0
  bfd_vma target = hsh->target_value;
3773
3774
  /* Make a note of the offset within the stubs for this entry.  */
3775
0
  hsh->stub_offset = htab->stub_sec->size;
3776
0
  bfd_byte *loc = htab->stub_sec->contents + hsh->stub_offset;
3777
3778
0
  bfd *stub_bfd = htab->stub_sec->owner;
3779
3780
0
  if (debug_stubs)
3781
0
    printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3782
0
       (unsigned int) target,
3783
0
       (unsigned int) hsh->stub_offset);
3784
3785
  /* We now have to add the information on the jump target to the bare
3786
     opcode bits already set in jmp_insn.  */
3787
3788
  /* Check for the alignment of the address.  */
3789
0
  if (target & 1)
3790
0
     return false;
3791
3792
0
  bfd_vma starget = target >> 1;
3793
0
  jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
3794
0
  bfd_put_16 (stub_bfd, jmp_insn, loc);
3795
0
  bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
3796
3797
0
  htab->stub_sec->size += 4;
3798
3799
  /* Now add the entries in the address mapping table if there is still
3800
     space left.  */
3801
0
  {
3802
0
    unsigned int nr;
3803
3804
0
    nr = htab->amt_entry_cnt + 1;
3805
0
    if (nr <= htab->amt_max_entry_cnt)
3806
0
      {
3807
0
  htab->amt_entry_cnt = nr;
3808
3809
0
  htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
3810
0
  htab->amt_destination_addr[nr - 1] = target;
3811
0
      }
3812
0
  }
3813
3814
0
  return true;
3815
0
}
3816
3817
static bool
3818
avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
3819
           void *in_arg ATTRIBUTE_UNUSED)
3820
0
{
3821
0
  elf32_avr_stub_hash_entry_t *hsh = avr_stub_hash_entry (bh);
3822
0
  hsh->is_actually_needed = false;
3823
3824
0
  return true;
3825
0
}
3826
3827
static bool
3828
avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3829
0
{
3830
  /* Massage our args to the form they really have.  */
3831
0
  elf32_avr_stub_hash_entry_t *hsh = avr_stub_hash_entry (bh);
3832
0
  elf32_avr_link_hash_table_t *htab = in_arg;
3833
3834
0
  int size = hsh->is_actually_needed ? 4 : 0;
3835
3836
0
  htab->stub_sec->size += size;
3837
0
  return true;
3838
0
}
3839
3840
void
3841
elf32_avr_setup_params (struct bfd_link_info *info, bfd *avr_stub_bfd,
3842
      asection *avr_stub_section,
3843
      bool no_stubs, bool deb_stubs, bool deb_relax,
3844
      bfd_vma pc_wrap_around, bool call_ret_replacement,
3845
      bool elide_rjmp0, bool prune_vectab)
3846
0
{
3847
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
3848
3849
0
  if (htab == NULL)
3850
0
    return;
3851
0
  htab->stub_sec = avr_stub_section;
3852
0
  htab->stub_bfd = avr_stub_bfd;
3853
0
  htab->no_stubs = no_stubs;
3854
3855
0
  debug_relax = deb_relax;
3856
0
  debug_stubs = deb_stubs;
3857
0
  avr_pc_wrap_around = pc_wrap_around;
3858
0
  avr_replace_call_ret_sequences = call_ret_replacement;
3859
0
  avr_elide_rjmp0 = elide_rjmp0;
3860
0
  avr_prune_vectab = prune_vectab;
3861
0
}
3862
3863
3864
/* Set up various things so that we can make a list of input sections
3865
   for each output section included in the link.  Returns -1 on error,
3866
   0 when no stubs will be needed, and 1 on success.  It also sets
3867
   information on the stubs bfd and the stub section in the info
3868
   struct.  */
3869
3870
int
3871
elf32_avr_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3872
0
{
3873
0
  bfd *input_bfd;
3874
0
  unsigned int bfd_count;
3875
0
  unsigned int top_id, top_index;
3876
0
  asection *section;
3877
0
  asection **input_list, **list;
3878
0
  size_t amt;
3879
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
3880
3881
0
  if (htab == NULL || htab->no_stubs)
3882
0
    return 0;
3883
3884
  /* Count the number of input BFDs and find the top input section id.  */
3885
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3886
0
       input_bfd != NULL;
3887
0
       input_bfd = input_bfd->link.next)
3888
0
    {
3889
0
      bfd_count += 1;
3890
0
      for (section = input_bfd->sections;
3891
0
     section != NULL;
3892
0
     section = section->next)
3893
0
  if (top_id < section->id)
3894
0
    top_id = section->id;
3895
0
    }
3896
3897
0
  htab->bfd_count = bfd_count;
3898
3899
  /* We can't use output_bfd->section_count here to find the top output
3900
     section index as some sections may have been removed, and
3901
     strip_excluded_output_sections doesn't renumber the indices.  */
3902
0
  for (section = output_bfd->sections, top_index = 0;
3903
0
       section != NULL;
3904
0
       section = section->next)
3905
0
    if (top_index < section->index)
3906
0
      top_index = section->index;
3907
3908
0
  htab->top_index = top_index;
3909
0
  amt = sizeof (asection *) * (top_index + 1);
3910
0
  input_list = bfd_malloc (amt);
3911
0
  htab->input_list = input_list;
3912
0
  if (input_list == NULL)
3913
0
    return -1;
3914
3915
  /* For sections we aren't interested in, mark their entries with a
3916
     value we can check later.  */
3917
0
  list = input_list + top_index;
3918
0
  do
3919
0
    *list = bfd_abs_section_ptr;
3920
0
  while (list-- != input_list);
3921
3922
0
  for (section = output_bfd->sections;
3923
0
       section != NULL;
3924
0
       section = section->next)
3925
0
    if ((section->flags & SEC_CODE) != 0)
3926
0
      input_list[section->index] = NULL;
3927
3928
0
  return 1;
3929
0
}
3930
3931
3932
/* Read in all local syms for all input bfds, and create hash entries
3933
   for export stubs if we are building a multi-subspace shared lib.
3934
   Returns -1 on error, 0 otherwise.  */
3935
3936
static int
3937
get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
3938
0
{
3939
0
  unsigned int bfd_indx;
3940
0
  Elf_Internal_Sym *local_syms, **all_local_syms;
3941
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
3942
0
  size_t amt;
3943
3944
0
  if (htab == NULL)
3945
0
    return -1;
3946
3947
  /* We want to read in symbol extension records only once.  To do this
3948
     we need to read in the local symbols in parallel and save them for
3949
     later use; so hold pointers to the local symbols in an array.  */
3950
0
  amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3951
0
  all_local_syms = bfd_zmalloc (amt);
3952
0
  htab->all_local_syms = all_local_syms;
3953
0
  if (all_local_syms == NULL)
3954
0
    return -1;
3955
3956
  /* Walk over all the input BFDs, swapping in local symbols.
3957
     If we are creating a shared library, create hash entries for the
3958
     export stubs.  */
3959
0
  for (bfd_indx = 0;
3960
0
       input_bfd != NULL;
3961
0
       input_bfd = input_bfd->link.next, bfd_indx++)
3962
0
    {
3963
0
      Elf_Internal_Shdr *symtab_hdr;
3964
3965
      /* We'll need the symbol table in a second.  */
3966
0
      symtab_hdr = &elf_symtab_hdr (input_bfd);
3967
0
      if (symtab_hdr->sh_info == 0)
3968
0
  continue;
3969
3970
      /* We need an array of the local symbols attached to the input bfd.  */
3971
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3972
0
      if (local_syms == NULL)
3973
0
  {
3974
0
    local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3975
0
               symtab_hdr->sh_info, 0,
3976
0
               NULL, NULL, NULL);
3977
    /* Cache them for elf_link_input_bfd.  */
3978
0
    symtab_hdr->contents = (unsigned char *) local_syms;
3979
0
  }
3980
0
      if (local_syms == NULL)
3981
0
  return -1;
3982
3983
0
      all_local_syms[bfd_indx] = local_syms;
3984
0
    }
3985
3986
0
  return 0;
3987
0
}
3988
3989
0
#define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3990
3991
bool
3992
elf32_avr_size_stubs (bfd *output_bfd, struct bfd_link_info *info,
3993
          bool is_prealloc_run)
3994
0
{
3995
0
  bool stub_changed = false;
3996
3997
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
3998
0
  if (htab == NULL)
3999
0
    return false;
4000
4001
  /* At this point we initialize htab->vector_base
4002
     To the start of the text output section.  */
4003
0
  htab->vector_base = htab->stub_sec->output_section->vma;
4004
4005
0
  if (get_local_syms (info->input_bfds, info))
4006
0
    {
4007
0
      if (htab->all_local_syms)
4008
0
  goto error_ret_free_local;
4009
0
      return false;
4010
0
    }
4011
4012
0
  if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
4013
0
    {
4014
0
      elf32_avr_stub_hash_entry_t *test;
4015
4016
0
      test = avr_add_stub ("Hugo",htab);
4017
0
      test->target_value = 0x123456;
4018
0
      test->stub_offset = 13;
4019
4020
0
      test = avr_add_stub ("Hugo2",htab);
4021
0
      test->target_value = 0x84210;
4022
0
      test->stub_offset = 14;
4023
0
    }
4024
4025
0
  while (1)
4026
0
    {
4027
0
      bfd *input_bfd;
4028
0
      unsigned int bfd_indx;
4029
4030
      /* We will have to re-generate the stub hash table each time anything
4031
   in memory has changed.  */
4032
4033
0
      bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
4034
0
      for (input_bfd = info->input_bfds, bfd_indx = 0;
4035
0
     input_bfd != NULL;
4036
0
     input_bfd = input_bfd->link.next, bfd_indx++)
4037
0
  {
4038
0
    Elf_Internal_Shdr *symtab_hdr;
4039
0
    asection *section;
4040
0
    Elf_Internal_Sym *local_syms;
4041
4042
    /* We'll need the symbol table in a second.  */
4043
0
    symtab_hdr = &elf_symtab_hdr (input_bfd);
4044
0
    if (symtab_hdr->sh_info == 0)
4045
0
      continue;
4046
4047
0
    local_syms = htab->all_local_syms[bfd_indx];
4048
4049
    /* Walk over each section attached to the input bfd.  */
4050
0
    for (section = input_bfd->sections;
4051
0
         section != NULL;
4052
0
         section = section->next)
4053
0
      {
4054
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4055
4056
        /* If there aren't any relocs, then there's nothing more
4057
     to do.  */
4058
0
        if ((section->flags & SEC_RELOC) == 0
4059
0
      || section->reloc_count == 0)
4060
0
    continue;
4061
4062
        /* If this section is a link-once section that will be
4063
     discarded, then don't create any stubs.  */
4064
0
        if (section->output_section == NULL
4065
0
      || section->output_section->owner != output_bfd)
4066
0
    continue;
4067
4068
        /* Get the relocs.  */
4069
0
        internal_relocs
4070
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
4071
0
               info->keep_memory);
4072
0
        if (internal_relocs == NULL)
4073
0
    goto error_ret_free_local;
4074
4075
        /* Now examine each relocation.  */
4076
0
        irela = internal_relocs;
4077
0
        irelaend = irela + section->reloc_count;
4078
0
        for (; irela < irelaend; irela++)
4079
0
    {
4080
0
      unsigned int r_type, r_indx;
4081
0
      elf32_avr_stub_hash_entry_t *hsh;
4082
0
      asection *sym_sec;
4083
0
      bfd_vma sym_value;
4084
0
      bfd_vma destination;
4085
0
      struct elf_link_hash_entry *hh;
4086
0
      char *stub_name;
4087
4088
0
      r_type = ELF32_R_TYPE (irela->r_info);
4089
0
      r_indx = ELF32_R_SYM (irela->r_info);
4090
4091
      /* Only look for 16 bit GS relocs. No other reloc will need a
4092
         stub.  */
4093
0
      if (!(r_type == R_AVR_16_PM
4094
0
      || r_type == R_AVR_LO8_LDI_GS
4095
0
      || r_type == R_AVR_HI8_LDI_GS))
4096
0
        continue;
4097
4098
      /* Determine the call target's name, value and section.  */
4099
0
      sym_sec = NULL;
4100
0
      sym_value = 0;
4101
0
      destination = 0;
4102
0
      hh = NULL;
4103
0
      if (r_indx < symtab_hdr->sh_info)
4104
0
        {
4105
          /* It's a local symbol.  */
4106
0
          Elf_Internal_Sym *sym;
4107
0
          Elf_Internal_Shdr *hdr;
4108
0
          unsigned int shndx;
4109
4110
0
          sym = local_syms + r_indx;
4111
0
          if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4112
0
      sym_value = sym->st_value;
4113
0
          shndx = sym->st_shndx;
4114
0
          if (shndx < elf_numsections (input_bfd))
4115
0
      {
4116
0
        hdr = elf_elfsections (input_bfd)[shndx];
4117
0
        sym_sec = hdr->bfd_section;
4118
0
        destination = (sym_value + irela->r_addend
4119
0
           + sym_sec->output_offset
4120
0
           + sym_sec->output_section->vma);
4121
0
      }
4122
0
        }
4123
0
      else
4124
0
        {
4125
          /* It's an external symbol.  */
4126
4127
0
          int e_indx = r_indx - symtab_hdr->sh_info;
4128
0
          hh = elf_sym_hashes (input_bfd)[e_indx];
4129
4130
0
          while (hh->root.type == bfd_link_hash_indirect
4131
0
           || hh->root.type == bfd_link_hash_warning)
4132
0
      hh = (struct elf_link_hash_entry *) hh->root.u.i.link;
4133
4134
0
          if (hh->root.type == bfd_link_hash_defined
4135
0
        || hh->root.type == bfd_link_hash_defweak)
4136
0
      {
4137
0
        sym_sec = hh->root.u.def.section;
4138
0
        sym_value = hh->root.u.def.value;
4139
0
        if (sym_sec->output_section != NULL)
4140
0
        destination = (sym_value + irela->r_addend
4141
0
           + sym_sec->output_offset
4142
0
           + sym_sec->output_section->vma);
4143
0
      }
4144
0
          else if (hh->root.type == bfd_link_hash_undefweak)
4145
0
      {
4146
0
        if (! bfd_link_pic (info))
4147
0
          continue;
4148
0
      }
4149
0
          else if (hh->root.type == bfd_link_hash_undefined)
4150
0
      {
4151
0
        if (! (info->unresolved_syms_in_objects == RM_IGNORE
4152
0
         && (ELF_ST_VISIBILITY (hh->other)
4153
0
             == STV_DEFAULT)))
4154
0
          continue;
4155
0
      }
4156
0
          else
4157
0
      {
4158
0
        bfd_set_error (bfd_error_bad_value);
4159
4160
0
        error_ret_free_internal:
4161
0
        if (elf_section_data (section)->relocs == NULL)
4162
0
          free (internal_relocs);
4163
0
        goto error_ret_free_local;
4164
0
      }
4165
0
        }
4166
4167
0
      if (! avr_stub_is_required_for_16_bit_reloc
4168
0
          (destination - htab->vector_base))
4169
0
        {
4170
0
          if (!is_prealloc_run)
4171
      /* We are having a reloc that doesn't need a stub.  */
4172
0
      continue;
4173
4174
          /* We don't right now know if a stub will be needed.
4175
       Let's rather be on the safe side.  */
4176
0
        }
4177
4178
      /* Get the name of this stub.  */
4179
0
      stub_name = avr_stub_name (sym_sec, sym_value, irela);
4180
4181
0
      if (!stub_name)
4182
0
        goto error_ret_free_internal;
4183
4184
0
      hsh = avr_stub_hash_lookup (&htab->bstab, stub_name,
4185
0
                false, false);
4186
0
      if (hsh != NULL)
4187
0
        {
4188
          /* The proper stub has already been created.  Mark it
4189
       to be used and write the possibly changed destination
4190
       value.  */
4191
0
          hsh->is_actually_needed = true;
4192
0
          hsh->target_value = destination;
4193
0
          free (stub_name);
4194
0
          continue;
4195
0
        }
4196
4197
0
      hsh = avr_add_stub (stub_name, htab);
4198
0
      if (hsh == NULL)
4199
0
        {
4200
0
          free (stub_name);
4201
0
          goto error_ret_free_internal;
4202
0
        }
4203
4204
0
      hsh->is_actually_needed = true;
4205
0
      hsh->target_value = destination;
4206
4207
0
      if (debug_stubs)
4208
0
        printf ("Adding stub with destination 0x%x to the"
4209
0
          " hash table.\n", (unsigned int) destination);
4210
0
      if (debug_stubs)
4211
0
        printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
4212
4213
0
      stub_changed = true;
4214
0
    }
4215
4216
        /* We're done with the internal relocs, free them.  */
4217
0
        if (elf_section_data (section)->relocs == NULL)
4218
0
    free (internal_relocs);
4219
0
      }
4220
0
  }
4221
4222
      /* Re-Calculate the number of needed stubs.  */
4223
0
      htab->stub_sec->size = 0;
4224
0
      bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
4225
4226
0
      if (!stub_changed)
4227
0
  break;
4228
4229
0
      stub_changed = false;
4230
0
    }
4231
4232
0
  free (htab->all_local_syms);
4233
0
  return true;
4234
4235
0
 error_ret_free_local:
4236
0
  free (htab->all_local_syms);
4237
0
  return false;
4238
0
}
4239
4240
4241
/* Build all the stubs associated with the current output file.  The
4242
   stubs are kept in a hash table attached to the main linker hash
4243
   table.  We also set up the .plt entries for statically linked PIC
4244
   functions here.  This function is called via hppaelf_finish in the
4245
   linker.  */
4246
4247
bool
4248
elf32_avr_build_stubs (struct bfd_link_info *info)
4249
0
{
4250
0
  asection *stub_sec;
4251
0
  struct bfd_hash_table *table;
4252
0
  bfd_size_type total_size = 0;
4253
4254
0
  elf32_avr_link_hash_table_t *htab = avr_link_hash_table (info);
4255
0
  if (htab == NULL)
4256
0
    return false;
4257
4258
  /* In case that there were several stub sections:  */
4259
0
  for (stub_sec = htab->stub_bfd->sections;
4260
0
       stub_sec != NULL;
4261
0
       stub_sec = stub_sec->next)
4262
0
    {
4263
0
      bfd_size_type size;
4264
4265
      /* Allocate memory to hold the linker stubs.  */
4266
0
      size = stub_sec->size;
4267
0
      total_size += size;
4268
4269
0
      stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4270
0
      if (stub_sec->contents == NULL && size != 0)
4271
0
  return false;
4272
0
      stub_sec->alloced = 1;
4273
0
      stub_sec->size = 0;
4274
0
    }
4275
4276
  /* Allocate memory for the address mapping table.  */
4277
0
  htab->amt_entry_cnt = 0;
4278
0
  htab->amt_max_entry_cnt = total_size / 4;
4279
0
  htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
4280
0
               * htab->amt_max_entry_cnt);
4281
0
  htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
4282
0
             * htab->amt_max_entry_cnt);
4283
4284
0
  if (debug_stubs)
4285
0
    printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
4286
4287
  /* Build the stubs as directed by the stub hash table.  */
4288
0
  table = &htab->bstab;
4289
0
  bfd_hash_traverse (table, avr_build_one_stub, info);
4290
4291
0
  if (debug_stubs)
4292
0
    printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
4293
4294
0
  return true;
4295
0
}
4296
4297
/* Callback used by QSORT to order relocations AP and BP.  */
4298
4299
static int
4300
internal_reloc_compare (const void *ap, const void *bp)
4301
0
{
4302
0
  const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
4303
0
  const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
4304
4305
0
  if (a->r_offset != b->r_offset)
4306
0
    return a->r_offset - b->r_offset;
4307
4308
  /* We don't need to sort on these criteria for correctness,
4309
     but enforcing a more strict ordering prevents unstable qsort
4310
     from behaving differently with different implementations.
4311
     Without the code below we get correct but different results
4312
     on Solaris 2.7 and 2.8.  We would like to always produce the
4313
     same results no matter the host.  */
4314
4315
0
  if (a->r_info != b->r_info)
4316
0
    return a->r_info - b->r_info;
4317
4318
0
  return a->r_addend - b->r_addend;
4319
0
}
4320
4321
/* Return true if ADDRESS is within the vma range of SECTION from ABFD.  */
4322
4323
static bool
4324
avr_is_section_for_address (asection *section, bfd_vma address)
4325
0
{
4326
0
  bfd_vma vma = bfd_section_vma (section);
4327
0
  if (address < vma)
4328
0
    return false;
4329
4330
0
  bfd_size_type size = section->size;
4331
0
  if (address >= vma + size)
4332
0
    return false;
4333
4334
0
  return true;
4335
0
}
4336
4337
/* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS.  */
4338
4339
typedef struct
4340
{
4341
  /* The address we're looking for.  */
4342
  bfd_vma address;
4343
4344
  /* The section we've found.  */
4345
  asection *section;
4346
} avr_find_section_data_t;
4347
4348
/* Helper function to locate the section holding a certain virtual memory
4349
   address.  This is called via bfd_map_over_sections.  The DATA is an
4350
   instance of AVR_FIND_SECTION_DATA_T, the address field of which
4351
   has been set to the address to search for, and the section field has
4352
   been set to NULL.  If SECTION from ABFD contains ADDRESS then the
4353
   section field in DATA will be set to SECTION.  As an optimisation, if
4354
   the section field is already non-null then this function does not
4355
   perform any checks, and just returns.  */
4356
4357
static void
4358
avr_find_section_for_address (bfd *abfd ATTRIBUTE_UNUSED,
4359
            asection *section, void *data)
4360
0
{
4361
0
  avr_find_section_data_t *fs_data = (avr_find_section_data_t *) data;
4362
4363
  /* Return if already found.  */
4364
0
  if (fs_data->section != NULL)
4365
0
    return;
4366
4367
  /* If this section isn't part of the addressable code content, skip it.  */
4368
0
  if ((bfd_section_flags (section) & SEC_ALLOC) == 0
4369
0
      && (bfd_section_flags (section) & SEC_CODE) == 0)
4370
0
    return;
4371
4372
0
  if (avr_is_section_for_address (section, fs_data->address))
4373
0
    fs_data->section = section;
4374
0
}
4375
4376
/* Load all of the property records from SEC, a section from ABFD.  Return
4377
   a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records.  The
4378
   memory for the returned structure, and all of the records pointed too by
4379
   the structure are allocated with a single call to malloc, so, only the
4380
   pointer returned needs to be free'd.  */
4381
4382
static struct avr_property_record_list *
4383
avr_elf32_load_records_from_section (bfd *abfd, asection *sec)
4384
0
{
4385
0
  bfd_byte *contents, *ptr;
4386
0
  bfd_size_type size, mem_size;
4387
0
  bfd_byte version, flags;
4388
0
  uint16_t record_count, i;
4389
0
  struct avr_property_record_list *r_list = NULL;
4390
0
  Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end;
4391
0
  avr_find_section_data_t fs_data;
4392
4393
0
  fs_data.section = NULL;
4394
4395
0
  if (!bfd_malloc_and_get_section (abfd, sec, &contents))
4396
0
    goto load_failed;
4397
0
  ptr = contents;
4398
4399
  /* Load the relocations for the '.avr.prop' section if there are any, and
4400
     sort them.  */
4401
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, false);
4402
0
  if (internal_relocs)
4403
0
    qsort (internal_relocs, sec->reloc_count,
4404
0
     sizeof (Elf_Internal_Rela), internal_reloc_compare);
4405
4406
  /* There is a header at the start of the property record section SEC, the
4407
     format of this header is:
4408
       uint8_t  : version number
4409
       uint8_t  : flags
4410
       uint16_t : record counter
4411
  */
4412
4413
  /* Check we have at least got a headers worth of bytes.  */
4414
0
  size = bfd_section_size (sec);
4415
0
  if (size < AVR_PROPERTY_SECTION_HEADER_SIZE)
4416
0
    goto load_failed;
4417
4418
0
  version = *ptr++;
4419
0
  flags = *ptr++;
4420
0
  record_count = bfd_get_16 (abfd, ptr);
4421
0
  ptr += 2;
4422
0
  BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE);
4423
4424
  /* Now allocate space for the list structure, and all of the list
4425
     elements in a single block.  */
4426
0
  mem_size = (sizeof (struct avr_property_record_list)
4427
0
        + sizeof (struct avr_property_record) * record_count);
4428
0
  r_list = bfd_malloc (mem_size);
4429
0
  if (r_list == NULL)
4430
0
    goto load_failed;
4431
4432
0
  r_list->version = version;
4433
0
  r_list->flags = flags;
4434
0
  r_list->section = sec;
4435
0
  r_list->record_count = record_count;
4436
0
  r_list->records = (struct avr_property_record *) (&r_list [1]);
4437
0
  size -= AVR_PROPERTY_SECTION_HEADER_SIZE;
4438
4439
  /* Check that we understand the version number.  There is only one
4440
     version number right now, anything else is an error.  */
4441
0
  if (r_list->version != AVR_PROPERTY_RECORDS_VERSION)
4442
0
    goto load_failed;
4443
4444
0
  rel = internal_relocs;
4445
0
  rel_end = rel + sec->reloc_count;
4446
0
  for (i = 0; i < record_count; ++i)
4447
0
    {
4448
0
      bfd_vma address;
4449
4450
      /* Each entry is a 32-bit address, followed by a single byte type.
4451
   After that is the type specific data.  We must take care to
4452
   ensure that we don't read beyond the end of the section data.  */
4453
0
      if (size < 5)
4454
0
  goto load_failed;
4455
4456
0
      r_list->records [i].section = NULL;
4457
0
      r_list->records [i].offset = 0;
4458
4459
0
      if (rel)
4460
0
  {
4461
    /* The offset of the address within the .avr.prop section.  */
4462
0
    size_t offset = ptr - contents;
4463
4464
0
    while (rel < rel_end && rel->r_offset < offset)
4465
0
      ++rel;
4466
4467
0
    if (rel == rel_end)
4468
0
      rel = NULL;
4469
0
    else if (rel->r_offset == offset)
4470
0
      {
4471
        /* Find section and section offset.  */
4472
0
        unsigned long r_symndx;
4473
4474
0
        asection *rel_sec;
4475
0
        bfd_vma sec_offset;
4476
4477
0
        r_symndx = ELF32_R_SYM (rel->r_info);
4478
0
        rel_sec = get_elf_r_symndx_section (abfd, r_symndx);
4479
0
        sec_offset = (get_elf_r_symndx_offset (abfd, r_symndx)
4480
0
          + rel->r_addend);
4481
4482
0
        r_list->records [i].section = rel_sec;
4483
0
        r_list->records [i].offset = sec_offset;
4484
0
      }
4485
0
  }
4486
4487
0
      address = bfd_get_32 (abfd, ptr);
4488
0
      ptr += 4;
4489
0
      size -= 4;
4490
4491
0
      if (r_list->records [i].section == NULL)
4492
0
  {
4493
    /* Try to find section and offset from address.  */
4494
0
    if (fs_data.section != NULL
4495
0
        && !avr_is_section_for_address (fs_data.section, address))
4496
0
      fs_data.section = NULL;
4497
4498
0
    if (fs_data.section == NULL)
4499
0
      {
4500
0
        fs_data.address = address;
4501
0
        bfd_map_over_sections (abfd, avr_find_section_for_address,
4502
0
             &fs_data);
4503
0
      }
4504
4505
0
    if (fs_data.section == NULL)
4506
0
      {
4507
0
        fprintf (stderr, "Failed to find matching section.\n");
4508
0
        goto load_failed;
4509
0
      }
4510
4511
0
    r_list->records [i].section = fs_data.section;
4512
0
    r_list->records [i].offset
4513
0
      = address - bfd_section_vma (fs_data.section);
4514
0
  }
4515
4516
0
      r_list->records [i].type = *ptr;
4517
0
      ptr += 1;
4518
0
      size -= 1;
4519
4520
0
      switch (r_list->records [i].type)
4521
0
  {
4522
0
  case RECORD_ORG:
4523
    /* Nothing else to load.  */
4524
0
    break;
4525
0
  case RECORD_ORG_AND_FILL:
4526
    /* Just a 4-byte fill to load.  */
4527
0
    if (size < 4)
4528
0
      goto load_failed;
4529
0
    r_list->records [i].data.org.fill = bfd_get_32 (abfd, ptr);
4530
0
    ptr += 4;
4531
0
    size -= 4;
4532
0
    break;
4533
0
  case RECORD_ALIGN:
4534
    /* Just a 4-byte alignment to load.  */
4535
0
    if (size < 4)
4536
0
      goto load_failed;
4537
0
    r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr);
4538
0
    ptr += 4;
4539
0
    size -= 4;
4540
    /* Just initialise PRECEDING_DELETED field, this field is
4541
       used during linker relaxation.  */
4542
0
    r_list->records [i].data.align.preceding_deleted = 0;
4543
0
    break;
4544
0
  case RECORD_ALIGN_AND_FILL:
4545
    /* A 4-byte alignment, and a 4-byte fill to load.  */
4546
0
    if (size < 8)
4547
0
      goto load_failed;
4548
0
    r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr);
4549
0
    ptr += 4;
4550
0
    r_list->records [i].data.align.fill = bfd_get_32 (abfd, ptr);
4551
0
    ptr += 4;
4552
0
    size -= 8;
4553
    /* Just initialise PRECEDING_DELETED field, this field is
4554
       used during linker relaxation.  */
4555
0
    r_list->records [i].data.align.preceding_deleted = 0;
4556
0
    break;
4557
0
  default:
4558
0
    goto load_failed;
4559
0
  }
4560
0
    }
4561
4562
0
  free (contents);
4563
0
  if (elf_section_data (sec)->relocs != internal_relocs)
4564
0
    free (internal_relocs);
4565
0
  return r_list;
4566
4567
0
 load_failed:
4568
0
  if (elf_section_data (sec)->relocs != internal_relocs)
4569
0
    free (internal_relocs);
4570
0
  free (contents);
4571
0
  free (r_list);
4572
0
  return NULL;
4573
0
}
4574
4575
/* Load all of the property records from ABFD.  See
4576
   AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value.  */
4577
4578
struct avr_property_record_list *
4579
avr_elf32_load_property_records (bfd *abfd)
4580
0
{
4581
0
  asection *sec;
4582
4583
  /* Find the '.avr.prop' section and load the contents into memory.  */
4584
0
  sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME);
4585
0
  if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0)
4586
0
    return NULL;
4587
0
  return avr_elf32_load_records_from_section (abfd, sec);
4588
0
}
4589
4590
const char *
4591
avr_elf32_property_record_name (struct avr_property_record *rec)
4592
0
{
4593
0
  const char *str;
4594
4595
0
  switch (rec->type)
4596
0
    {
4597
0
    case RECORD_ORG:
4598
0
      str = "ORG";
4599
0
      break;
4600
0
    case RECORD_ORG_AND_FILL:
4601
0
      str = "ORG+FILL";
4602
0
      break;
4603
0
    case RECORD_ALIGN:
4604
0
      str = "ALIGN";
4605
0
      break;
4606
0
    case RECORD_ALIGN_AND_FILL:
4607
0
      str = "ALIGN+FILL";
4608
0
      break;
4609
0
    default:
4610
0
      str = "unknown";
4611
0
    }
4612
4613
0
  return str;
4614
0
}
4615
4616
4617
/* Merge object attributes from IBFD into OBFD.  Error if there are
4618
   conflicting attributes.  The follwing attributes are supported:
4619
   Tag_GNU_AVR_VTABLE_AS
4620
      One plus avr-g++'s named address-space for VTABLEs.
4621
*/
4622
4623
static bool
4624
avr_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
4625
0
{
4626
0
  obj_attribute *in_attr, *in_attrs;
4627
0
  obj_attribute *out_attr, *out_attrs;
4628
0
  bfd *obfd = info->output_bfd;
4629
4630
0
  in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4631
0
  out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4632
4633
  // Merge Tag_GNU_AVR_VTABLE_AS (4).
4634
4635
0
  static bfd *last_fp_vtab;
4636
0
  in_attr = &in_attrs[Tag_GNU_AVR_VTABLE_AS];
4637
0
  out_attr = &out_attrs[Tag_GNU_AVR_VTABLE_AS];
4638
4639
0
  if (in_attr->i == Val_GNU_AVR_VTABLE_NONE
4640
0
      || out_attr->i == Val_GNU_AVR_VTABLE_NONE)
4641
0
    {
4642
0
      if (in_attr->i != Val_GNU_AVR_VTABLE_NONE)
4643
0
  {
4644
0
    out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
4645
0
    out_attr->i = in_attr->i;
4646
0
    last_fp_vtab = ibfd;
4647
0
  }
4648
0
    }
4649
0
  else if (in_attr->i != out_attr->i)
4650
0
    {
4651
0
      const char *const tag = "Tag_GNU_AVR_VTABLE_AS";
4652
0
      const char *const iname = avr_tag_vtable_as_name (in_attr->i);
4653
0
      const char *const oname = avr_tag_vtable_as_name (out_attr->i);
4654
4655
      // xgettext:c-format
4656
0
      _bfd_error_handler (_("%pB uses %s tag %d (%s), %pB uses %s tag %d (%s)"),
4657
0
        ibfd, tag, in_attr->i, iname,
4658
0
        last_fp_vtab, tag, out_attr->i, oname);
4659
4660
0
      out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4661
0
      bfd_set_error (bfd_error_bad_value);
4662
0
      return false;
4663
0
    }
4664
4665
  // Merge Tag_GNU_AVR_BITS_DOUBLE (8).
4666
4667
0
  static bfd *last_fp_dbl;
4668
0
  in_attr = &in_attrs[Tag_GNU_AVR_BITS_DOUBLE];
4669
0
  out_attr = &out_attrs[Tag_GNU_AVR_BITS_DOUBLE];
4670
4671
0
  if (in_attr->i == 0
4672
0
      || out_attr->i == 0)
4673
0
    {
4674
0
      if (in_attr->i != 0)
4675
0
  {
4676
0
    out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
4677
0
    out_attr->i = in_attr->i;
4678
0
    last_fp_dbl = ibfd;
4679
0
  }
4680
0
    }
4681
0
  else if (in_attr->i != out_attr->i)
4682
0
    {
4683
0
      const char *const tag = "Tag_GNU_AVR_BITS_DOUBLE";
4684
4685
      // xgettext:c-format
4686
0
      _bfd_error_handler (_("%pB uses %s tag %d, %pB uses %s tag %d"),
4687
0
        ibfd, tag, in_attr->i,
4688
0
        last_fp_dbl, tag, out_attr->i);
4689
4690
0
      out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4691
0
      bfd_set_error (bfd_error_bad_value);
4692
0
      return false;
4693
0
    }
4694
4695
  // Merge Tag_GNU_AVR_BITS_LONG_DOUBLE (12).
4696
4697
0
  static bfd *last_fp_ldbl;
4698
0
  in_attr = &in_attrs[Tag_GNU_AVR_BITS_LONG_DOUBLE];
4699
0
  out_attr = &out_attrs[Tag_GNU_AVR_BITS_LONG_DOUBLE];
4700
4701
0
  if (in_attr->i == 0
4702
0
      || out_attr->i == 0)
4703
0
    {
4704
0
      if (in_attr->i != 0)
4705
0
  {
4706
0
    out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
4707
0
    out_attr->i = in_attr->i;
4708
0
    last_fp_ldbl = ibfd;
4709
0
  }
4710
0
    }
4711
0
  else if (in_attr->i != out_attr->i)
4712
0
    {
4713
0
      const char *const tag = "Tag_GNU_AVR_BITS_LONG_DOUBLE";
4714
4715
      // xgettext:c-format
4716
0
      _bfd_error_handler (_("%pB uses %s tag %d, %pB uses %s tag %d"),
4717
0
        ibfd, tag, in_attr->i,
4718
0
        last_fp_ldbl, tag, out_attr->i);
4719
4720
0
      out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
4721
0
      bfd_set_error (bfd_error_bad_value);
4722
0
      return false;
4723
0
    }
4724
4725
  // Merge any common GNU attributes.
4726
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
4727
0
}
4728
4729
4730
/* Merge backend specific data from an object file to the output
4731
   object file when linking.  */
4732
4733
bool bfd_avr_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4734
0
{
4735
0
  return avr_elf_merge_obj_attributes (ibfd, info);
4736
0
}
4737
4738
4739
#define ELF_ARCH    bfd_arch_avr
4740
#define ELF_TARGET_ID   AVR_ELF_DATA
4741
#define ELF_MACHINE_CODE  EM_AVR
4742
#define ELF_MACHINE_ALT1  EM_AVR_OLD
4743
#define ELF_MAXPAGESIZE   1
4744
4745
#define TARGET_LITTLE_SYM avr_elf32_vec
4746
#define TARGET_LITTLE_NAME  "elf32-avr"
4747
4748
#define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4749
4750
#define elf_info_to_howto        avr_info_to_howto_rela
4751
#define elf_info_to_howto_rel        NULL
4752
#define elf_backend_relocate_section       elf32_avr_relocate_section
4753
#define elf_backend_can_gc_sections      1
4754
#define elf_backend_rela_normal        1
4755
#define elf_backend_final_write_processing \
4756
          bfd_elf_avr_final_write_processing
4757
#define elf_backend_object_p    elf32_avr_object_p
4758
4759
#define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4760
#define bfd_elf32_bfd_get_relocated_section_contents \
4761
          elf32_avr_get_relocated_section_contents
4762
#define bfd_elf32_new_section_hook  elf_avr_new_section_hook
4763
#define bfd_elf32_bfd_merge_private_bfd_data \
4764
          bfd_avr_elf_merge_private_bfd_data
4765
#define elf_backend_special_sections  elf_avr_special_sections
4766
#define elf_backend_want_stub_bfd 1
4767
4768
#include "elf32-target.h"