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