Coverage Report

Created: 2025-06-24 06:45

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