Coverage Report

Created: 2026-07-25 10:20

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