(linux_test_for_tracefork): Don't leave zombie
[deliverable/binutils-gdb.git] / bfd / elf32-avr.c
CommitLineData
adde6300 1/* AVR-specific support for 32-bit ELF
df406460 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006
b2a8e766 3 Free Software Foundation, Inc.
adde6300
AM
4 Contributed by Denis Chertykov <denisc@overta.ru>
5
750bce0e 6 This file is part of BFD, the Binary File Descriptor library.
adde6300 7
750bce0e
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
adde6300 12
750bce0e
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
adde6300 17
750bce0e
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
4cdc7696 20 Foundation, Inc., 51 Franklin Street - Fifth Floor,
df406460 21 Boston, MA 02110-1301, USA. */
adde6300 22
adde6300
AM
23#include "bfd.h"
24#include "sysdep.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
27#include "elf/avr.h"
28c9d252
NC
28#include "elf32-avr.h"
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
36/* Hash table initialization and handling. Code is taken from the hppa port
37 and adapted to the needs of AVR. */
38
39/* We use two hash tables to hold information for linking avr objects.
40
41 The first is the elf32_avr_link_hash_tablse which is derived from the
42 stanard ELF linker hash table. We use this as a place to attach the other
43 hash table and some static information.
44
45 The second is the stub hash table which is derived from the base BFD
46 hash table. The stub hash table holds the information on the linker
47 stubs. */
48
49struct elf32_avr_stub_hash_entry
50{
51 /* Base hash table entry structure. */
52 struct bfd_hash_entry bh_root;
53
54 /* Offset within stub_sec of the beginning of this stub. */
55 bfd_vma stub_offset;
56
57 /* Given the symbol's value and its section we can determine its final
58 value when building the stubs (so the stub knows where to jump). */
59 bfd_vma target_value;
60
61 /* This way we could mark stubs to be no longer necessary. */
62 bfd_boolean is_actually_needed;
63};
64
65struct elf32_avr_link_hash_table
66{
67 /* The main hash table. */
68 struct elf_link_hash_table etab;
69
70 /* The stub hash table. */
71 struct bfd_hash_table bstab;
72
73 bfd_boolean no_stubs;
74
75 /* Linker stub bfd. */
76 bfd *stub_bfd;
77
78 /* The stub section. */
79 asection *stub_sec;
80
81 /* Usually 0, unless we are generating code for a bootloader. Will
82 be initialized by elf32_avr_size_stubs to the vma offset of the
83 output section associated with the stub section. */
84 bfd_vma vector_base;
85
86 /* Assorted information used by elf32_avr_size_stubs. */
87 unsigned int bfd_count;
88 int top_index;
89 asection ** input_list;
90 Elf_Internal_Sym ** all_local_syms;
91
92 /* Tables for mapping vma beyond the 128k boundary to the address of the
93 corresponding stub. (AMT)
94 "amt_max_entry_cnt" reflects the number of entries that memory is allocated
95 for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
96 "amt_entry_cnt" informs how many of these entries actually contain
97 useful data. */
98 unsigned int amt_entry_cnt;
99 unsigned int amt_max_entry_cnt;
100 bfd_vma * amt_stub_offsets;
101 bfd_vma * amt_destination_addr;
102};
103
104/* Various hash macros and functions. */
105#define avr_link_hash_table(p) \
106 ((struct elf32_avr_link_hash_table *) ((p)->hash))
107
108#define avr_stub_hash_entry(ent) \
109 ((struct elf32_avr_stub_hash_entry *)(ent))
110
111#define avr_stub_hash_lookup(table, string, create, copy) \
112 ((struct elf32_avr_stub_hash_entry *) \
113 bfd_hash_lookup ((table), (string), (create), (copy)))
adde6300 114
adde6300
AM
115static reloc_howto_type elf_avr_howto_table[] =
116{
117 HOWTO (R_AVR_NONE, /* type */
118 0, /* rightshift */
119 2, /* size (0 = byte, 1 = short, 2 = long) */
120 32, /* bitsize */
b34976b6 121 FALSE, /* pc_relative */
adde6300
AM
122 0, /* bitpos */
123 complain_overflow_bitfield, /* complain_on_overflow */
124 bfd_elf_generic_reloc, /* special_function */
125 "R_AVR_NONE", /* name */
b34976b6 126 FALSE, /* partial_inplace */
adde6300
AM
127 0, /* src_mask */
128 0, /* dst_mask */
b34976b6 129 FALSE), /* pcrel_offset */
adde6300
AM
130
131 HOWTO (R_AVR_32, /* type */
132 0, /* rightshift */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
134 32, /* bitsize */
b34976b6 135 FALSE, /* pc_relative */
adde6300
AM
136 0, /* bitpos */
137 complain_overflow_bitfield, /* complain_on_overflow */
138 bfd_elf_generic_reloc, /* special_function */
139 "R_AVR_32", /* name */
b34976b6 140 FALSE, /* partial_inplace */
adde6300
AM
141 0xffffffff, /* src_mask */
142 0xffffffff, /* dst_mask */
b34976b6 143 FALSE), /* pcrel_offset */
adde6300
AM
144
145 /* A 7 bit PC relative relocation. */
146 HOWTO (R_AVR_7_PCREL, /* type */
147 1, /* rightshift */
148 1, /* size (0 = byte, 1 = short, 2 = long) */
149 7, /* bitsize */
b34976b6 150 TRUE, /* pc_relative */
adde6300
AM
151 3, /* bitpos */
152 complain_overflow_bitfield, /* complain_on_overflow */
153 bfd_elf_generic_reloc, /* special_function */
154 "R_AVR_7_PCREL", /* name */
b34976b6 155 FALSE, /* partial_inplace */
adde6300
AM
156 0xffff, /* src_mask */
157 0xffff, /* dst_mask */
b34976b6 158 TRUE), /* pcrel_offset */
adde6300
AM
159
160 /* A 13 bit PC relative relocation. */
161 HOWTO (R_AVR_13_PCREL, /* type */
162 1, /* rightshift */
163 1, /* size (0 = byte, 1 = short, 2 = long) */
164 13, /* bitsize */
b34976b6 165 TRUE, /* pc_relative */
adde6300
AM
166 0, /* bitpos */
167 complain_overflow_bitfield, /* complain_on_overflow */
168 bfd_elf_generic_reloc, /* special_function */
169 "R_AVR_13_PCREL", /* name */
b34976b6 170 FALSE, /* partial_inplace */
adde6300
AM
171 0xfff, /* src_mask */
172 0xfff, /* dst_mask */
b34976b6 173 TRUE), /* pcrel_offset */
adde6300
AM
174
175 /* A 16 bit absolute relocation. */
176 HOWTO (R_AVR_16, /* type */
177 0, /* rightshift */
178 1, /* size (0 = byte, 1 = short, 2 = long) */
179 16, /* bitsize */
b34976b6 180 FALSE, /* pc_relative */
adde6300
AM
181 0, /* bitpos */
182 complain_overflow_dont, /* complain_on_overflow */
183 bfd_elf_generic_reloc, /* special_function */
184 "R_AVR_16", /* name */
b34976b6 185 FALSE, /* partial_inplace */
adde6300
AM
186 0xffff, /* src_mask */
187 0xffff, /* dst_mask */
b34976b6 188 FALSE), /* pcrel_offset */
adde6300 189
28c9d252
NC
190 /* A 16 bit absolute relocation for command address
191 Will be changed when linker stubs are needed. */
adde6300
AM
192 HOWTO (R_AVR_16_PM, /* type */
193 1, /* rightshift */
194 1, /* size (0 = byte, 1 = short, 2 = long) */
195 16, /* bitsize */
b34976b6 196 FALSE, /* pc_relative */
adde6300
AM
197 0, /* bitpos */
198 complain_overflow_bitfield, /* complain_on_overflow */
199 bfd_elf_generic_reloc, /* special_function */
200 "R_AVR_16_PM", /* name */
b34976b6 201 FALSE, /* partial_inplace */
adde6300
AM
202 0xffff, /* src_mask */
203 0xffff, /* dst_mask */
b34976b6 204 FALSE), /* pcrel_offset */
adde6300
AM
205 /* A low 8 bit absolute relocation of 16 bit address.
206 For LDI command. */
207 HOWTO (R_AVR_LO8_LDI, /* type */
208 0, /* rightshift */
209 1, /* size (0 = byte, 1 = short, 2 = long) */
210 8, /* bitsize */
b34976b6 211 FALSE, /* pc_relative */
adde6300
AM
212 0, /* bitpos */
213 complain_overflow_dont, /* complain_on_overflow */
214 bfd_elf_generic_reloc, /* special_function */
215 "R_AVR_LO8_LDI", /* name */
b34976b6 216 FALSE, /* partial_inplace */
adde6300
AM
217 0xffff, /* src_mask */
218 0xffff, /* dst_mask */
b34976b6 219 FALSE), /* pcrel_offset */
adde6300
AM
220 /* A high 8 bit absolute relocation of 16 bit address.
221 For LDI command. */
222 HOWTO (R_AVR_HI8_LDI, /* type */
223 8, /* rightshift */
224 1, /* size (0 = byte, 1 = short, 2 = long) */
225 8, /* bitsize */
b34976b6 226 FALSE, /* pc_relative */
adde6300
AM
227 0, /* bitpos */
228 complain_overflow_dont, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_AVR_HI8_LDI", /* name */
b34976b6 231 FALSE, /* partial_inplace */
adde6300
AM
232 0xffff, /* src_mask */
233 0xffff, /* dst_mask */
b34976b6 234 FALSE), /* pcrel_offset */
adde6300 235 /* A high 6 bit absolute relocation of 22 bit address.
4cdc7696 236 For LDI command. As well second most significant 8 bit value of
df406460 237 a 32 bit link-time constant. */
adde6300
AM
238 HOWTO (R_AVR_HH8_LDI, /* type */
239 16, /* rightshift */
240 1, /* size (0 = byte, 1 = short, 2 = long) */
241 8, /* bitsize */
b34976b6 242 FALSE, /* pc_relative */
adde6300
AM
243 0, /* bitpos */
244 complain_overflow_dont, /* complain_on_overflow */
245 bfd_elf_generic_reloc, /* special_function */
246 "R_AVR_HH8_LDI", /* name */
b34976b6 247 FALSE, /* partial_inplace */
adde6300
AM
248 0xffff, /* src_mask */
249 0xffff, /* dst_mask */
b34976b6 250 FALSE), /* pcrel_offset */
adde6300
AM
251 /* A negative low 8 bit absolute relocation of 16 bit address.
252 For LDI command. */
253 HOWTO (R_AVR_LO8_LDI_NEG, /* type */
254 0, /* rightshift */
255 1, /* size (0 = byte, 1 = short, 2 = long) */
256 8, /* bitsize */
b34976b6 257 FALSE, /* pc_relative */
adde6300
AM
258 0, /* bitpos */
259 complain_overflow_dont, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_AVR_LO8_LDI_NEG", /* name */
b34976b6 262 FALSE, /* partial_inplace */
adde6300
AM
263 0xffff, /* src_mask */
264 0xffff, /* dst_mask */
b34976b6 265 FALSE), /* pcrel_offset */
df406460 266 /* A negative high 8 bit absolute relocation of 16 bit address.
adde6300
AM
267 For LDI command. */
268 HOWTO (R_AVR_HI8_LDI_NEG, /* type */
269 8, /* rightshift */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
271 8, /* bitsize */
b34976b6 272 FALSE, /* pc_relative */
adde6300
AM
273 0, /* bitpos */
274 complain_overflow_dont, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_AVR_HI8_LDI_NEG", /* name */
b34976b6 277 FALSE, /* partial_inplace */
adde6300
AM
278 0xffff, /* src_mask */
279 0xffff, /* dst_mask */
b34976b6 280 FALSE), /* pcrel_offset */
df406460 281 /* A negative high 6 bit absolute relocation of 22 bit address.
adde6300
AM
282 For LDI command. */
283 HOWTO (R_AVR_HH8_LDI_NEG, /* type */
284 16, /* rightshift */
285 1, /* size (0 = byte, 1 = short, 2 = long) */
286 8, /* bitsize */
b34976b6 287 FALSE, /* pc_relative */
adde6300
AM
288 0, /* bitpos */
289 complain_overflow_dont, /* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_AVR_HH8_LDI_NEG", /* name */
b34976b6 292 FALSE, /* partial_inplace */
adde6300
AM
293 0xffff, /* src_mask */
294 0xffff, /* dst_mask */
b34976b6 295 FALSE), /* pcrel_offset */
adde6300 296 /* A low 8 bit absolute relocation of 24 bit program memory address.
28c9d252 297 For LDI command. Will not be changed when linker stubs are needed. */
adde6300
AM
298 HOWTO (R_AVR_LO8_LDI_PM, /* type */
299 1, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 8, /* bitsize */
b34976b6 302 FALSE, /* pc_relative */
adde6300
AM
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_AVR_LO8_LDI_PM", /* name */
b34976b6 307 FALSE, /* partial_inplace */
adde6300
AM
308 0xffff, /* src_mask */
309 0xffff, /* dst_mask */
b34976b6 310 FALSE), /* pcrel_offset */
28c9d252
NC
311 /* A low 8 bit absolute relocation of 24 bit program memory address.
312 For LDI command. Will not be changed when linker stubs are needed. */
adde6300
AM
313 HOWTO (R_AVR_HI8_LDI_PM, /* type */
314 9, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 8, /* bitsize */
b34976b6 317 FALSE, /* pc_relative */
adde6300
AM
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
320 bfd_elf_generic_reloc, /* special_function */
321 "R_AVR_HI8_LDI_PM", /* name */
b34976b6 322 FALSE, /* partial_inplace */
adde6300
AM
323 0xffff, /* src_mask */
324 0xffff, /* dst_mask */
b34976b6 325 FALSE), /* pcrel_offset */
28c9d252
NC
326 /* A low 8 bit absolute relocation of 24 bit program memory address.
327 For LDI command. Will not be changed when linker stubs are needed. */
adde6300
AM
328 HOWTO (R_AVR_HH8_LDI_PM, /* type */
329 17, /* rightshift */
330 1, /* size (0 = byte, 1 = short, 2 = long) */
331 8, /* bitsize */
b34976b6 332 FALSE, /* pc_relative */
adde6300
AM
333 0, /* bitpos */
334 complain_overflow_dont, /* complain_on_overflow */
335 bfd_elf_generic_reloc, /* special_function */
336 "R_AVR_HH8_LDI_PM", /* name */
b34976b6 337 FALSE, /* partial_inplace */
adde6300
AM
338 0xffff, /* src_mask */
339 0xffff, /* dst_mask */
b34976b6 340 FALSE), /* pcrel_offset */
28c9d252
NC
341 /* A low 8 bit absolute relocation of 24 bit program memory address.
342 For LDI command. Will not be changed when linker stubs are needed. */
adde6300
AM
343 HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */
344 1, /* rightshift */
345 1, /* size (0 = byte, 1 = short, 2 = long) */
346 8, /* bitsize */
b34976b6 347 FALSE, /* pc_relative */
adde6300
AM
348 0, /* bitpos */
349 complain_overflow_dont, /* complain_on_overflow */
350 bfd_elf_generic_reloc, /* special_function */
351 "R_AVR_LO8_LDI_PM_NEG", /* name */
b34976b6 352 FALSE, /* partial_inplace */
adde6300
AM
353 0xffff, /* src_mask */
354 0xffff, /* dst_mask */
b34976b6 355 FALSE), /* pcrel_offset */
28c9d252
NC
356 /* A low 8 bit absolute relocation of 24 bit program memory address.
357 For LDI command. Will not be changed when linker stubs are needed. */
adde6300
AM
358 HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */
359 9, /* rightshift */
360 1, /* size (0 = byte, 1 = short, 2 = long) */
361 8, /* bitsize */
b34976b6 362 FALSE, /* pc_relative */
adde6300
AM
363 0, /* bitpos */
364 complain_overflow_dont, /* complain_on_overflow */
365 bfd_elf_generic_reloc, /* special_function */
366 "R_AVR_HI8_LDI_PM_NEG", /* name */
b34976b6 367 FALSE, /* partial_inplace */
adde6300
AM
368 0xffff, /* src_mask */
369 0xffff, /* dst_mask */
b34976b6 370 FALSE), /* pcrel_offset */
28c9d252
NC
371 /* A low 8 bit absolute relocation of 24 bit program memory address.
372 For LDI command. Will not be changed when linker stubs are needed. */
adde6300
AM
373 HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */
374 17, /* rightshift */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
376 8, /* bitsize */
b34976b6 377 FALSE, /* pc_relative */
adde6300
AM
378 0, /* bitpos */
379 complain_overflow_dont, /* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_AVR_HH8_LDI_PM_NEG", /* name */
b34976b6 382 FALSE, /* partial_inplace */
adde6300
AM
383 0xffff, /* src_mask */
384 0xffff, /* dst_mask */
b34976b6 385 FALSE), /* pcrel_offset */
adde6300
AM
386 /* Relocation for CALL command in ATmega. */
387 HOWTO (R_AVR_CALL, /* type */
388 1, /* rightshift */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
390 23, /* bitsize */
b34976b6 391 FALSE, /* pc_relative */
adde6300 392 0, /* bitpos */
750bce0e 393 complain_overflow_dont,/* complain_on_overflow */
adde6300
AM
394 bfd_elf_generic_reloc, /* special_function */
395 "R_AVR_CALL", /* name */
b34976b6 396 FALSE, /* partial_inplace */
adde6300
AM
397 0xffffffff, /* src_mask */
398 0xffffffff, /* dst_mask */
750bce0e
NC
399 FALSE), /* pcrel_offset */
400 /* A 16 bit absolute relocation of 16 bit address.
401 For LDI command. */
402 HOWTO (R_AVR_LDI, /* type */
403 0, /* rightshift */
404 1, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 FALSE, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_dont,/* complain_on_overflow */
409 bfd_elf_generic_reloc, /* special_function */
410 "R_AVR_LDI", /* name */
411 FALSE, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 FALSE), /* pcrel_offset */
415 /* A 6 bit absolute relocation of 6 bit offset.
416 For ldd/sdd command. */
417 HOWTO (R_AVR_6, /* type */
418 0, /* rightshift */
419 0, /* size (0 = byte, 1 = short, 2 = long) */
420 6, /* bitsize */
421 FALSE, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_dont,/* complain_on_overflow */
424 bfd_elf_generic_reloc, /* special_function */
425 "R_AVR_6", /* name */
426 FALSE, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
430 /* A 6 bit absolute relocation of 6 bit offset.
431 For sbiw/adiw command. */
432 HOWTO (R_AVR_6_ADIW, /* type */
433 0, /* rightshift */
434 0, /* size (0 = byte, 1 = short, 2 = long) */
435 6, /* bitsize */
436 FALSE, /* pc_relative */
437 0, /* bitpos */
438 complain_overflow_dont,/* complain_on_overflow */
439 bfd_elf_generic_reloc, /* special_function */
440 "R_AVR_6_ADIW", /* name */
441 FALSE, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
df406460
NC
444 FALSE), /* pcrel_offset */
445 /* Most significant 8 bit value of a 32 bit link-time constant. */
446 HOWTO (R_AVR_MS8_LDI, /* type */
447 24, /* rightshift */
448 1, /* size (0 = byte, 1 = short, 2 = long) */
449 8, /* bitsize */
450 FALSE, /* pc_relative */
451 0, /* bitpos */
452 complain_overflow_dont, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_AVR_MS8_LDI", /* name */
455 FALSE, /* partial_inplace */
456 0xffff, /* src_mask */
457 0xffff, /* dst_mask */
458 FALSE), /* pcrel_offset */
459 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
460 HOWTO (R_AVR_MS8_LDI_NEG, /* type */
461 24, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 8, /* bitsize */
464 FALSE, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_dont, /* complain_on_overflow */
467 bfd_elf_generic_reloc, /* special_function */
468 "R_AVR_MS8_LDI_NEG", /* name */
469 FALSE, /* partial_inplace */
470 0xffff, /* src_mask */
471 0xffff, /* dst_mask */
28c9d252
NC
472 FALSE), /* pcrel_offset */
473 /* A low 8 bit absolute relocation of 24 bit program memory address.
474 For LDI command. Will be changed when linker stubs are needed. */
475 HOWTO (R_AVR_LO8_LDI_GS, /* type */
476 1, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 8, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont, /* complain_on_overflow */
482 bfd_elf_generic_reloc, /* special_function */
483 "R_AVR_LO8_LDI_GS", /* name */
484 FALSE, /* partial_inplace */
485 0xffff, /* src_mask */
486 0xffff, /* dst_mask */
487 FALSE), /* pcrel_offset */
488 /* A low 8 bit absolute relocation of 24 bit program memory address.
489 For LDI command. Will be changed when linker stubs are needed. */
490 HOWTO (R_AVR_HI8_LDI_GS, /* type */
491 9, /* rightshift */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
493 8, /* bitsize */
494 FALSE, /* pc_relative */
495 0, /* bitpos */
496 complain_overflow_dont, /* complain_on_overflow */
497 bfd_elf_generic_reloc, /* special_function */
498 "R_AVR_HI8_LDI_GS", /* name */
499 FALSE, /* partial_inplace */
500 0xffff, /* src_mask */
501 0xffff, /* dst_mask */
502 FALSE) /* pcrel_offset */
adde6300
AM
503};
504
505/* Map BFD reloc types to AVR ELF reloc types. */
506
507struct avr_reloc_map
508{
509 bfd_reloc_code_real_type bfd_reloc_val;
510 unsigned int elf_reloc_val;
511};
512
28c9d252 513static const struct avr_reloc_map avr_reloc_map[] =
adde6300
AM
514{
515 { BFD_RELOC_NONE, R_AVR_NONE },
516 { BFD_RELOC_32, R_AVR_32 },
517 { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL },
518 { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL },
519 { BFD_RELOC_16, R_AVR_16 },
520 { BFD_RELOC_AVR_16_PM, R_AVR_16_PM },
521 { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI},
522 { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI },
523 { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI },
df406460 524 { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI },
adde6300
AM
525 { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG },
526 { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG },
527 { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG },
df406460 528 { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG },
adde6300 529 { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM },
28c9d252 530 { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS },
adde6300 531 { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM },
28c9d252 532 { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS },
adde6300
AM
533 { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM },
534 { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG },
535 { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG },
536 { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG },
750bce0e
NC
537 { BFD_RELOC_AVR_CALL, R_AVR_CALL },
538 { BFD_RELOC_AVR_LDI, R_AVR_LDI },
539 { BFD_RELOC_AVR_6, R_AVR_6 },
540 { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW }
adde6300
AM
541};
542
df406460 543/* Meant to be filled one day with the wrap around address for the
4cdc7696 544 specific device. I.e. should get the value 0x4000 for 16k devices,
df406460 545 0x8000 for 32k devices and so on.
4cdc7696 546
df406460 547 We initialize it here with a value of 0x1000000 resulting in
4cdc7696
NC
548 that we will never suggest a wrap-around jump during relaxation.
549 The logic of the source code later on assumes that in
df406460 550 avr_pc_wrap_around one single bit is set. */
28c9d252
NC
551static bfd_vma avr_pc_wrap_around = 0x10000000;
552
553/* If this variable holds a value different from zero, the linker relaxation
554 machine will try to optimize call/ret sequences by a single jump
555 instruction. This option could be switched off by a linker switch. */
556static int avr_replace_call_ret_sequences = 1;
557\f
558/* Initialize an entry in the stub hash table. */
559
560static struct bfd_hash_entry *
561stub_hash_newfunc (struct bfd_hash_entry *entry,
562 struct bfd_hash_table *table,
563 const char *string)
564{
565 /* Allocate the structure if it has not already been allocated by a
566 subclass. */
567 if (entry == NULL)
568 {
569 entry = bfd_hash_allocate (table,
570 sizeof (struct elf32_avr_stub_hash_entry));
571 if (entry == NULL)
572 return entry;
573 }
574
575 /* Call the allocation method of the superclass. */
576 entry = bfd_hash_newfunc (entry, table, string);
577 if (entry != NULL)
578 {
579 struct elf32_avr_stub_hash_entry *hsh;
580
581 /* Initialize the local fields. */
582 hsh = avr_stub_hash_entry (entry);
583 hsh->stub_offset = 0;
584 hsh->target_value = 0;
585 }
586
587 return entry;
588}
589
590/* Create the derived linker hash table. The AVR ELF port uses the derived
591 hash table to keep information specific to the AVR ELF linker (without
592 using static variables). */
593
594static struct bfd_link_hash_table *
595elf32_avr_link_hash_table_create (bfd *abfd)
596{
597 struct elf32_avr_link_hash_table *htab;
598 bfd_size_type amt = sizeof (*htab);
599
600 htab = bfd_malloc (amt);
601 if (htab == NULL)
602 return NULL;
603
604 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
605 _bfd_elf_link_hash_newfunc,
606 sizeof (struct elf_link_hash_entry)))
607 {
608 free (htab);
609 return NULL;
610 }
611
612 /* Init the stub hash table too. */
613 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
614 sizeof (struct elf32_avr_stub_hash_entry)))
615 return NULL;
4cdc7696 616
28c9d252
NC
617 htab->stub_bfd = NULL;
618 htab->stub_sec = NULL;
619
620 /* Initialize the address mapping table. */
621 htab->amt_stub_offsets = NULL;
622 htab->amt_destination_addr = NULL;
623 htab->amt_entry_cnt = 0;
624 htab->amt_max_entry_cnt = 0;
625
626 return &htab->etab.root;
627}
628
629/* Free the derived linker hash table. */
630
631static void
632elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
633{
634 struct elf32_avr_link_hash_table *htab
635 = (struct elf32_avr_link_hash_table *) btab;
636
637 /* Free the address mapping table. */
638 if (htab->amt_stub_offsets != NULL)
639 free (htab->amt_stub_offsets);
640 if (htab->amt_destination_addr != NULL)
641 free (htab->amt_destination_addr);
642
643 bfd_hash_table_free (&htab->bstab);
644 _bfd_generic_link_hash_table_free (btab);
645}
df406460
NC
646
647/* Calculates the effective distance of a pc relative jump/call. */
73160847 648
df406460
NC
649static int
650avr_relative_distance_considering_wrap_around (unsigned int distance)
4cdc7696 651{
df406460 652 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
df406460
NC
653 int dist_with_wrap_around = distance & wrap_around_mask;
654
4cdc7696 655 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
df406460
NC
656 dist_with_wrap_around -= avr_pc_wrap_around;
657
658 return dist_with_wrap_around;
659}
660
661
adde6300 662static reloc_howto_type *
4cdc7696
NC
663bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
664 bfd_reloc_code_real_type code)
adde6300
AM
665{
666 unsigned int i;
667
668 for (i = 0;
669 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
670 i++)
73160847
NC
671 if (avr_reloc_map[i].bfd_reloc_val == code)
672 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
adde6300
AM
673
674 return NULL;
675}
676
677/* Set the howto pointer for an AVR ELF reloc. */
678
679static void
4cdc7696
NC
680avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
681 arelent *cache_ptr,
682 Elf_Internal_Rela *dst)
adde6300
AM
683{
684 unsigned int r_type;
685
686 r_type = ELF32_R_TYPE (dst->r_info);
687 BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
688 cache_ptr->howto = &elf_avr_howto_table[r_type];
689}
690
691static asection *
4cdc7696
NC
692elf32_avr_gc_mark_hook (asection *sec,
693 struct bfd_link_info *info ATTRIBUTE_UNUSED,
694 Elf_Internal_Rela *rel,
695 struct elf_link_hash_entry *h,
696 Elf_Internal_Sym *sym)
adde6300
AM
697{
698 if (h != NULL)
699 {
700 switch (ELF32_R_TYPE (rel->r_info))
701 {
702 default:
703 switch (h->root.type)
704 {
705 case bfd_link_hash_defined:
706 case bfd_link_hash_defweak:
707 return h->root.u.def.section;
708
709 case bfd_link_hash_common:
710 return h->root.u.c.p->section;
711
712 default:
713 break;
714 }
715 }
716 }
717 else
1e2f5b6e 718 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
9ad5cbcf 719
adde6300
AM
720 return NULL;
721}
722
b34976b6 723static bfd_boolean
4cdc7696
NC
724elf32_avr_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
725 struct bfd_link_info *info ATTRIBUTE_UNUSED,
726 asection *sec ATTRIBUTE_UNUSED,
727 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
adde6300
AM
728{
729 /* We don't use got and plt entries for avr. */
b34976b6 730 return TRUE;
adde6300
AM
731}
732
733/* Look through the relocs for a section during the first phase.
734 Since we don't do .gots or .plts, we just need to consider the
735 virtual table relocs for gc. */
736
b34976b6 737static bfd_boolean
4cdc7696
NC
738elf32_avr_check_relocs (bfd *abfd,
739 struct bfd_link_info *info,
740 asection *sec,
741 const Elf_Internal_Rela *relocs)
adde6300
AM
742{
743 Elf_Internal_Shdr *symtab_hdr;
744 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
745 const Elf_Internal_Rela *rel;
746 const Elf_Internal_Rela *rel_end;
747
1049f94e 748 if (info->relocatable)
b34976b6 749 return TRUE;
adde6300
AM
750
751 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
752 sym_hashes = elf_sym_hashes (abfd);
4cdc7696 753 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
adde6300
AM
754 if (!elf_bad_symtab (abfd))
755 sym_hashes_end -= symtab_hdr->sh_info;
756
757 rel_end = relocs + sec->reloc_count;
758 for (rel = relocs; rel < rel_end; rel++)
759 {
760 struct elf_link_hash_entry *h;
761 unsigned long r_symndx;
762
763 r_symndx = ELF32_R_SYM (rel->r_info);
764 if (r_symndx < symtab_hdr->sh_info)
765 h = NULL;
766 else
973a3492
L
767 {
768 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
769 while (h->root.type == bfd_link_hash_indirect
770 || h->root.type == bfd_link_hash_warning)
771 h = (struct elf_link_hash_entry *) h->root.u.i.link;
772 }
adde6300
AM
773 }
774
b34976b6 775 return TRUE;
adde6300
AM
776}
777
28c9d252
NC
778static bfd_boolean
779avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
780{
781 return (relocation >= 0x020000);
782}
783
784/* Returns the address of the corresponding stub if there is one.
785 Returns otherwise an address above 0x020000. This function
786 could also be used, if there is no knowledge on the section where
787 the destination is found. */
788
789static bfd_vma
790avr_get_stub_addr (bfd_vma srel,
791 struct elf32_avr_link_hash_table *htab)
792{
793 unsigned int index;
794 bfd_vma stub_sec_addr =
795 (htab->stub_sec->output_section->vma +
796 htab->stub_sec->output_offset);
797
798 for (index = 0; index < htab->amt_max_entry_cnt; index ++)
799 if (htab->amt_destination_addr[index] == srel)
800 return htab->amt_stub_offsets[index] + stub_sec_addr;
801
802 /* Return an address that could not be reached by 16 bit relocs. */
803 return 0x020000;
804}
805
adde6300
AM
806/* Perform a single relocation. By default we use the standard BFD
807 routines, but a few relocs, we have to do them ourselves. */
808
809static bfd_reloc_status_type
28c9d252
NC
810avr_final_link_relocate (reloc_howto_type * howto,
811 bfd * input_bfd,
812 asection * input_section,
813 bfd_byte * contents,
814 Elf_Internal_Rela * rel,
815 bfd_vma relocation,
816 struct elf32_avr_link_hash_table * htab)
adde6300
AM
817{
818 bfd_reloc_status_type r = bfd_reloc_ok;
819 bfd_vma x;
820 bfd_signed_vma srel;
28c9d252
NC
821 bfd_signed_vma reloc_addr;
822 bfd_boolean use_stubs = FALSE;
823 /* Usually is 0, unless we are generating code for a bootloader. */
824 bfd_signed_vma base_addr = htab->vector_base;
825
826 /* Absolute addr of the reloc in the final excecutable. */
827 reloc_addr = rel->r_offset + input_section->output_section->vma
828 + input_section->output_offset;
adde6300
AM
829
830 switch (howto->type)
831 {
832 case R_AVR_7_PCREL:
833 contents += rel->r_offset;
834 srel = (bfd_signed_vma) relocation;
835 srel += rel->r_addend;
836 srel -= rel->r_offset;
a7c10850 837 srel -= 2; /* Branch instructions add 2 to the PC... */
adde6300
AM
838 srel -= (input_section->output_section->vma +
839 input_section->output_offset);
840
841 if (srel & 1)
842 return bfd_reloc_outofrange;
843 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
844 return bfd_reloc_overflow;
845 x = bfd_get_16 (input_bfd, contents);
846 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
847 bfd_put_16 (input_bfd, x, contents);
848 break;
849
850 case R_AVR_13_PCREL:
851 contents += rel->r_offset;
852 srel = (bfd_signed_vma) relocation;
853 srel += rel->r_addend;
854 srel -= rel->r_offset;
a7c10850 855 srel -= 2; /* Branch instructions add 2 to the PC... */
adde6300
AM
856 srel -= (input_section->output_section->vma +
857 input_section->output_offset);
858
859 if (srel & 1)
860 return bfd_reloc_outofrange;
861
df406460
NC
862 srel = avr_relative_distance_considering_wrap_around (srel);
863
adde6300
AM
864 /* AVR addresses commands as words. */
865 srel >>= 1;
866
867 /* Check for overflow. */
868 if (srel < -2048 || srel > 2047)
869 {
df406460
NC
870 /* Relative distance is too large. */
871
872 /* Always apply WRAPAROUND for avr2 and avr4. */
65aa24b6 873 switch (bfd_get_mach (input_bfd))
adde6300 874 {
65aa24b6
NC
875 case bfd_mach_avr2:
876 case bfd_mach_avr4:
877 break;
878
879 default:
880 return bfd_reloc_overflow;
adde6300 881 }
adde6300
AM
882 }
883
884 x = bfd_get_16 (input_bfd, contents);
885 x = (x & 0xf000) | (srel & 0xfff);
886 bfd_put_16 (input_bfd, x, contents);
887 break;
888
889 case R_AVR_LO8_LDI:
890 contents += rel->r_offset;
891 srel = (bfd_signed_vma) relocation + rel->r_addend;
892 x = bfd_get_16 (input_bfd, contents);
893 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
894 bfd_put_16 (input_bfd, x, contents);
895 break;
896
750bce0e
NC
897 case R_AVR_LDI:
898 contents += rel->r_offset;
899 srel = (bfd_signed_vma) relocation + rel->r_addend;
4cdc7696
NC
900 if (((srel > 0) && (srel & 0xffff) > 255)
901 || ((srel < 0) && ((-srel) & 0xffff) > 128))
df406460
NC
902 /* Remove offset for data/eeprom section. */
903 return bfd_reloc_overflow;
904
750bce0e
NC
905 x = bfd_get_16 (input_bfd, contents);
906 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
907 bfd_put_16 (input_bfd, x, contents);
908 break;
909
910 case R_AVR_6:
911 contents += rel->r_offset;
912 srel = (bfd_signed_vma) relocation + rel->r_addend;
913 if (((srel & 0xffff) > 63) || (srel < 0))
914 /* Remove offset for data/eeprom section. */
915 return bfd_reloc_overflow;
916 x = bfd_get_16 (input_bfd, contents);
4cdc7696 917 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
df406460 918 | ((srel & (1 << 5)) << 8));
750bce0e
NC
919 bfd_put_16 (input_bfd, x, contents);
920 break;
921
922 case R_AVR_6_ADIW:
923 contents += rel->r_offset;
924 srel = (bfd_signed_vma) relocation + rel->r_addend;
925 if (((srel & 0xffff) > 63) || (srel < 0))
926 /* Remove offset for data/eeprom section. */
927 return bfd_reloc_overflow;
928 x = bfd_get_16 (input_bfd, contents);
4cdc7696 929 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
750bce0e
NC
930 bfd_put_16 (input_bfd, x, contents);
931 break;
932
adde6300
AM
933 case R_AVR_HI8_LDI:
934 contents += rel->r_offset;
935 srel = (bfd_signed_vma) relocation + rel->r_addend;
936 srel = (srel >> 8) & 0xff;
937 x = bfd_get_16 (input_bfd, contents);
938 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
939 bfd_put_16 (input_bfd, x, contents);
940 break;
941
942 case R_AVR_HH8_LDI:
943 contents += rel->r_offset;
944 srel = (bfd_signed_vma) relocation + rel->r_addend;
945 srel = (srel >> 16) & 0xff;
946 x = bfd_get_16 (input_bfd, contents);
947 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
948 bfd_put_16 (input_bfd, x, contents);
949 break;
950
df406460
NC
951 case R_AVR_MS8_LDI:
952 contents += rel->r_offset;
953 srel = (bfd_signed_vma) relocation + rel->r_addend;
954 srel = (srel >> 24) & 0xff;
955 x = bfd_get_16 (input_bfd, contents);
956 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
957 bfd_put_16 (input_bfd, x, contents);
958 break;
959
adde6300
AM
960 case R_AVR_LO8_LDI_NEG:
961 contents += rel->r_offset;
962 srel = (bfd_signed_vma) relocation + rel->r_addend;
963 srel = -srel;
964 x = bfd_get_16 (input_bfd, contents);
965 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
966 bfd_put_16 (input_bfd, x, contents);
967 break;
968
969 case R_AVR_HI8_LDI_NEG:
970 contents += rel->r_offset;
971 srel = (bfd_signed_vma) relocation + rel->r_addend;
972 srel = -srel;
973 srel = (srel >> 8) & 0xff;
974 x = bfd_get_16 (input_bfd, contents);
975 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
976 bfd_put_16 (input_bfd, x, contents);
977 break;
978
979 case R_AVR_HH8_LDI_NEG:
980 contents += rel->r_offset;
981 srel = (bfd_signed_vma) relocation + rel->r_addend;
982 srel = -srel;
983 srel = (srel >> 16) & 0xff;
984 x = bfd_get_16 (input_bfd, contents);
985 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
986 bfd_put_16 (input_bfd, x, contents);
987 break;
988
df406460
NC
989 case R_AVR_MS8_LDI_NEG:
990 contents += rel->r_offset;
991 srel = (bfd_signed_vma) relocation + rel->r_addend;
992 srel = -srel;
993 srel = (srel >> 24) & 0xff;
994 x = bfd_get_16 (input_bfd, contents);
995 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
996 bfd_put_16 (input_bfd, x, contents);
997 break;
998
28c9d252
NC
999 case R_AVR_LO8_LDI_GS:
1000 use_stubs = (!htab->no_stubs);
1001 /* Fall through. */
adde6300
AM
1002 case R_AVR_LO8_LDI_PM:
1003 contents += rel->r_offset;
1004 srel = (bfd_signed_vma) relocation + rel->r_addend;
28c9d252
NC
1005
1006 if (use_stubs
1007 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1008 {
1009 bfd_vma old_srel = srel;
1010
1011 /* We need to use the address of the stub instead. */
1012 srel = avr_get_stub_addr (srel, htab);
1013 if (debug_stubs)
1014 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1015 "reloc at address 0x%x.\n",
1016 (unsigned int) srel,
1017 (unsigned int) old_srel,
1018 (unsigned int) reloc_addr);
1019
1020 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1021 return bfd_reloc_outofrange;
1022 }
1023
adde6300
AM
1024 if (srel & 1)
1025 return bfd_reloc_outofrange;
1026 srel = srel >> 1;
1027 x = bfd_get_16 (input_bfd, contents);
1028 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1029 bfd_put_16 (input_bfd, x, contents);
1030 break;
1031
28c9d252
NC
1032 case R_AVR_HI8_LDI_GS:
1033 use_stubs = (!htab->no_stubs);
1034 /* Fall through. */
adde6300
AM
1035 case R_AVR_HI8_LDI_PM:
1036 contents += rel->r_offset;
1037 srel = (bfd_signed_vma) relocation + rel->r_addend;
28c9d252
NC
1038
1039 if (use_stubs
1040 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1041 {
1042 bfd_vma old_srel = srel;
1043
1044 /* We need to use the address of the stub instead. */
1045 srel = avr_get_stub_addr (srel, htab);
1046 if (debug_stubs)
1047 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1048 "reloc at address 0x%x.\n",
1049 (unsigned int) srel,
1050 (unsigned int) old_srel,
1051 (unsigned int) reloc_addr);
1052
1053 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1054 return bfd_reloc_outofrange;
1055 }
1056
adde6300
AM
1057 if (srel & 1)
1058 return bfd_reloc_outofrange;
1059 srel = srel >> 1;
1060 srel = (srel >> 8) & 0xff;
1061 x = bfd_get_16 (input_bfd, contents);
1062 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1063 bfd_put_16 (input_bfd, x, contents);
1064 break;
1065
1066 case R_AVR_HH8_LDI_PM:
1067 contents += rel->r_offset;
1068 srel = (bfd_signed_vma) relocation + rel->r_addend;
1069 if (srel & 1)
1070 return bfd_reloc_outofrange;
1071 srel = srel >> 1;
1072 srel = (srel >> 16) & 0xff;
1073 x = bfd_get_16 (input_bfd, contents);
1074 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1075 bfd_put_16 (input_bfd, x, contents);
1076 break;
1077
1078 case R_AVR_LO8_LDI_PM_NEG:
1079 contents += rel->r_offset;
1080 srel = (bfd_signed_vma) relocation + rel->r_addend;
1081 srel = -srel;
1082 if (srel & 1)
1083 return bfd_reloc_outofrange;
1084 srel = srel >> 1;
1085 x = bfd_get_16 (input_bfd, contents);
1086 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1087 bfd_put_16 (input_bfd, x, contents);
1088 break;
1089
1090 case R_AVR_HI8_LDI_PM_NEG:
1091 contents += rel->r_offset;
1092 srel = (bfd_signed_vma) relocation + rel->r_addend;
1093 srel = -srel;
1094 if (srel & 1)
1095 return bfd_reloc_outofrange;
1096 srel = srel >> 1;
1097 srel = (srel >> 8) & 0xff;
1098 x = bfd_get_16 (input_bfd, contents);
1099 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1100 bfd_put_16 (input_bfd, x, contents);
1101 break;
1102
1103 case R_AVR_HH8_LDI_PM_NEG:
1104 contents += rel->r_offset;
1105 srel = (bfd_signed_vma) relocation + rel->r_addend;
1106 srel = -srel;
1107 if (srel & 1)
1108 return bfd_reloc_outofrange;
1109 srel = srel >> 1;
1110 srel = (srel >> 16) & 0xff;
1111 x = bfd_get_16 (input_bfd, contents);
1112 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1113 bfd_put_16 (input_bfd, x, contents);
1114 break;
1115
1116 case R_AVR_CALL:
1117 contents += rel->r_offset;
1118 srel = (bfd_signed_vma) relocation + rel->r_addend;
1119 if (srel & 1)
1120 return bfd_reloc_outofrange;
1121 srel = srel >> 1;
1122 x = bfd_get_16 (input_bfd, contents);
1123 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1124 bfd_put_16 (input_bfd, x, contents);
dc810e39 1125 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
adde6300
AM
1126 break;
1127
28c9d252
NC
1128 case R_AVR_16_PM:
1129 use_stubs = (!htab->no_stubs);
1130 contents += rel->r_offset;
1131 srel = (bfd_signed_vma) relocation + rel->r_addend;
1132
1133 if (use_stubs
1134 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1135 {
1136 bfd_vma old_srel = srel;
1137
1138 /* We need to use the address of the stub instead. */
1139 srel = avr_get_stub_addr (srel,htab);
1140 if (debug_stubs)
1141 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1142 "reloc at address 0x%x.\n",
1143 (unsigned int) srel,
1144 (unsigned int) old_srel,
1145 (unsigned int) reloc_addr);
1146
1147 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1148 return bfd_reloc_outofrange;
1149 }
1150
1151 if (srel & 1)
1152 return bfd_reloc_outofrange;
1153 srel = srel >> 1;
1154 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1155 break;
1156
adde6300
AM
1157 default:
1158 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1159 contents, rel->r_offset,
1160 relocation, rel->r_addend);
1161 }
1162
1163 return r;
1164}
1165
1166/* Relocate an AVR ELF section. */
4cdc7696 1167
b34976b6 1168static bfd_boolean
4cdc7696
NC
1169elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1170 struct bfd_link_info *info,
1171 bfd *input_bfd,
1172 asection *input_section,
1173 bfd_byte *contents,
1174 Elf_Internal_Rela *relocs,
1175 Elf_Internal_Sym *local_syms,
1176 asection **local_sections)
adde6300
AM
1177{
1178 Elf_Internal_Shdr * symtab_hdr;
1179 struct elf_link_hash_entry ** sym_hashes;
1180 Elf_Internal_Rela * rel;
1181 Elf_Internal_Rela * relend;
28c9d252 1182 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
adde6300 1183
1049f94e 1184 if (info->relocatable)
b34976b6 1185 return TRUE;
f0fe0e16 1186
adde6300
AM
1187 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1188 sym_hashes = elf_sym_hashes (input_bfd);
1189 relend = relocs + input_section->reloc_count;
1190
1191 for (rel = relocs; rel < relend; rel ++)
1192 {
1193 reloc_howto_type * howto;
1194 unsigned long r_symndx;
1195 Elf_Internal_Sym * sym;
1196 asection * sec;
1197 struct elf_link_hash_entry * h;
1198 bfd_vma relocation;
1199 bfd_reloc_status_type r;
dfeffb9f 1200 const char * name;
adde6300
AM
1201 int r_type;
1202
f0fe0e16 1203 /* This is a final link. */
adde6300
AM
1204 r_type = ELF32_R_TYPE (rel->r_info);
1205 r_symndx = ELF32_R_SYM (rel->r_info);
adde6300
AM
1206 howto = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
1207 h = NULL;
1208 sym = NULL;
1209 sec = NULL;
1210
1211 if (r_symndx < symtab_hdr->sh_info)
1212 {
1213 sym = local_syms + r_symndx;
1214 sec = local_sections [r_symndx];
8517fae7 1215 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
adde6300
AM
1216
1217 name = bfd_elf_string_from_elf_section
1218 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1219 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1220 }
1221 else
1222 {
59c2e50f 1223 bfd_boolean unresolved_reloc, warned;
adde6300 1224
b2a8e766
AM
1225 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1226 r_symndx, symtab_hdr, sym_hashes,
1227 h, sec, relocation,
1228 unresolved_reloc, warned);
dfeffb9f
L
1229
1230 name = h->root.root.string;
adde6300
AM
1231 }
1232
1233 r = avr_final_link_relocate (howto, input_bfd, input_section,
28c9d252 1234 contents, rel, relocation, htab);
adde6300
AM
1235
1236 if (r != bfd_reloc_ok)
1237 {
1238 const char * msg = (const char *) NULL;
1239
1240 switch (r)
1241 {
1242 case bfd_reloc_overflow:
1243 r = info->callbacks->reloc_overflow
dfeffb9f
L
1244 (info, (h ? &h->root : NULL),
1245 name, howto->name, (bfd_vma) 0,
adde6300
AM
1246 input_bfd, input_section, rel->r_offset);
1247 break;
1248
1249 case bfd_reloc_undefined:
1250 r = info->callbacks->undefined_symbol
b34976b6 1251 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
adde6300
AM
1252 break;
1253
1254 case bfd_reloc_outofrange:
1255 msg = _("internal error: out of range error");
1256 break;
1257
1258 case bfd_reloc_notsupported:
1259 msg = _("internal error: unsupported relocation error");
1260 break;
1261
1262 case bfd_reloc_dangerous:
1263 msg = _("internal error: dangerous relocation");
1264 break;
1265
1266 default:
1267 msg = _("internal error: unknown error");
1268 break;
1269 }
1270
1271 if (msg)
1272 r = info->callbacks->warning
1273 (info, msg, name, input_bfd, input_section, rel->r_offset);
1274
1275 if (! r)
b34976b6 1276 return FALSE;
adde6300
AM
1277 }
1278 }
1279
b34976b6 1280 return TRUE;
adde6300
AM
1281}
1282
1283/* The final processing done just before writing out a AVR ELF object
1284 file. This gets the AVR architecture right based on the machine
1285 number. */
1286
1287static void
4cdc7696
NC
1288bfd_elf_avr_final_write_processing (bfd *abfd,
1289 bfd_boolean linker ATTRIBUTE_UNUSED)
adde6300
AM
1290{
1291 unsigned long val;
1292
1293 switch (bfd_get_mach (abfd))
1294 {
1295 default:
1296 case bfd_mach_avr2:
1297 val = E_AVR_MACH_AVR2;
1298 break;
1299
1300 case bfd_mach_avr1:
1301 val = E_AVR_MACH_AVR1;
1302 break;
1303
1304 case bfd_mach_avr3:
1305 val = E_AVR_MACH_AVR3;
1306 break;
1307
1308 case bfd_mach_avr4:
1309 val = E_AVR_MACH_AVR4;
1310 break;
1311
65aa24b6
NC
1312 case bfd_mach_avr5:
1313 val = E_AVR_MACH_AVR5;
1314 break;
28c9d252
NC
1315
1316 case bfd_mach_avr6:
1317 val = E_AVR_MACH_AVR6;
1318 break;
adde6300
AM
1319 }
1320
1321 elf_elfheader (abfd)->e_machine = EM_AVR;
1322 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1323 elf_elfheader (abfd)->e_flags |= val;
df406460 1324 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
adde6300
AM
1325}
1326
1327/* Set the right machine number. */
1328
b34976b6 1329static bfd_boolean
4cdc7696 1330elf32_avr_object_p (bfd *abfd)
adde6300 1331{
dc810e39 1332 unsigned int e_set = bfd_mach_avr2;
4cdc7696 1333
aa4f99bb
AO
1334 if (elf_elfheader (abfd)->e_machine == EM_AVR
1335 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
adde6300
AM
1336 {
1337 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
4cdc7696 1338
adde6300
AM
1339 switch (e_mach)
1340 {
1341 default:
1342 case E_AVR_MACH_AVR2:
1343 e_set = bfd_mach_avr2;
1344 break;
1345
1346 case E_AVR_MACH_AVR1:
1347 e_set = bfd_mach_avr1;
1348 break;
1349
1350 case E_AVR_MACH_AVR3:
1351 e_set = bfd_mach_avr3;
1352 break;
1353
1354 case E_AVR_MACH_AVR4:
1355 e_set = bfd_mach_avr4;
1356 break;
65aa24b6
NC
1357
1358 case E_AVR_MACH_AVR5:
1359 e_set = bfd_mach_avr5;
1360 break;
28c9d252
NC
1361
1362 case E_AVR_MACH_AVR6:
1363 e_set = bfd_mach_avr6;
1364 break;
adde6300
AM
1365 }
1366 }
1367 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1368 e_set);
1369}
1370
df406460 1371
4cdc7696
NC
1372/* Delete some bytes from a section while changing the size of an instruction.
1373 The parameter "addr" denotes the section-relative offset pointing just
1374 behind the shrinked instruction. "addr+count" point at the first
1375 byte just behind the original unshrinked instruction. */
1376
1377static bfd_boolean
1378elf32_avr_relax_delete_bytes (bfd *abfd,
73160847 1379 asection *sec,
4cdc7696 1380 bfd_vma addr,
73160847 1381 int count)
4cdc7696
NC
1382{
1383 Elf_Internal_Shdr *symtab_hdr;
1384 unsigned int sec_shndx;
1385 bfd_byte *contents;
1386 Elf_Internal_Rela *irel, *irelend;
1387 Elf_Internal_Rela *irelalign;
1388 Elf_Internal_Sym *isym;
1389 Elf_Internal_Sym *isymbuf = NULL;
1390 Elf_Internal_Sym *isymend;
1391 bfd_vma toaddr;
1392 struct elf_link_hash_entry **sym_hashes;
1393 struct elf_link_hash_entry **end_hashes;
1394 unsigned int symcount;
1395
1396 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1397 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1398 contents = elf_section_data (sec)->this_hdr.contents;
1399
1400 /* The deletion must stop at the next ALIGN reloc for an aligment
1401 power larger than the number of bytes we are deleting. */
1402
1403 irelalign = NULL;
1404 toaddr = sec->size;
1405
1406 irel = elf_section_data (sec)->relocs;
1407 irelend = irel + sec->reloc_count;
1408
1409 /* Actually delete the bytes. */
1410 if (toaddr - addr - count > 0)
1411 memmove (contents + addr, contents + addr + count,
1412 (size_t) (toaddr - addr - count));
1413 sec->size -= count;
1414
73160847 1415 /* Adjust all the reloc addresses. */
4cdc7696
NC
1416 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1417 {
4cdc7696
NC
1418 bfd_vma old_reloc_address;
1419 bfd_vma shrinked_insn_address;
1420
1421 old_reloc_address = (sec->output_section->vma
1422 + sec->output_offset + irel->r_offset);
1423 shrinked_insn_address = (sec->output_section->vma
1424 + sec->output_offset + addr - count);
1425
1426 /* Get the new reloc address. */
1427 if ((irel->r_offset > addr
1428 && irel->r_offset < toaddr))
1429 {
28c9d252 1430 if (debug_relax)
4cdc7696
NC
1431 printf ("Relocation at address 0x%x needs to be moved.\n"
1432 "Old section offset: 0x%x, New section offset: 0x%x \n",
1433 (unsigned int) old_reloc_address,
1434 (unsigned int) irel->r_offset,
1435 (unsigned int) ((irel->r_offset) - count));
1436
1437 irel->r_offset -= count;
1438 }
1439
73160847 1440 }
4cdc7696 1441
73160847
NC
1442 /* The reloc's own addresses are now ok. However, we need to readjust
1443 the reloc's addend, i.e. the reloc's value if two conditions are met:
1444 1.) the reloc is relative to a symbol in this section that
1445 is located in front of the shrinked instruction
28c9d252
NC
1446 2.) symbol plus addend end up behind the shrinked instruction.
1447
73160847
NC
1448 The most common case where this happens are relocs relative to
1449 the section-start symbol.
28c9d252 1450
73160847
NC
1451 This step needs to be done for all of the sections of the bfd. */
1452
1453 {
1454 struct bfd_section *isec;
1455
1456 for (isec = abfd->sections; isec; isec = isec->next)
1457 {
1458 bfd_vma symval;
1459 bfd_vma shrinked_insn_address;
1460
1461 shrinked_insn_address = (sec->output_section->vma
1462 + sec->output_offset + addr - count);
1463
1464 irelend = elf_section_data (isec)->relocs + isec->reloc_count;
28c9d252 1465 for (irel = elf_section_data (isec)->relocs;
73160847
NC
1466 irel < irelend;
1467 irel++)
1468 {
28c9d252 1469 /* Read this BFD's local symbols if we haven't done
73160847
NC
1470 so already. */
1471 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1472 {
1473 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1474 if (isymbuf == NULL)
1475 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1476 symtab_hdr->sh_info, 0,
1477 NULL, NULL, NULL);
1478 if (isymbuf == NULL)
1479 return FALSE;
1480 }
1481
1482 /* Get the value of the symbol referred to by the reloc. */
1483 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1484 {
1485 /* A local symbol. */
1486 Elf_Internal_Sym *isym;
1487 asection *sym_sec;
1488
1489 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1490 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1491 symval = isym->st_value;
1492 /* If the reloc is absolute, it will not have
1493 a symbol or section associated with it. */
1494 if (sym_sec == sec)
28c9d252 1495 {
73160847
NC
1496 symval += sym_sec->output_section->vma
1497 + sym_sec->output_offset;
4cdc7696 1498
28c9d252 1499 if (debug_relax)
73160847
NC
1500 printf ("Checking if the relocation's "
1501 "addend needs corrections.\n"
1502 "Address of anchor symbol: 0x%x \n"
1503 "Address of relocation target: 0x%x \n"
1504 "Address of relaxed insn: 0x%x \n",
1505 (unsigned int) symval,
1506 (unsigned int) (symval + irel->r_addend),
1507 (unsigned int) shrinked_insn_address);
1508
1509 if (symval <= shrinked_insn_address
1510 && (symval + irel->r_addend) > shrinked_insn_address)
1511 {
1512 irel->r_addend -= count;
1513
28c9d252 1514 if (debug_relax)
73160847
NC
1515 printf ("Relocation's addend needed to be fixed \n");
1516 }
4cdc7696 1517 }
73160847 1518 /* else...Reference symbol is absolute. No adjustment needed. */
28c9d252
NC
1519 }
1520 /* else...Reference symbol is extern. No need for adjusting
73160847 1521 the addend. */
28c9d252 1522 }
73160847
NC
1523 }
1524 }
4cdc7696
NC
1525
1526 /* Adjust the local symbols defined in this section. */
1527 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1528 isymend = isym + symtab_hdr->sh_info;
1529 for (; isym < isymend; isym++)
1530 {
1531 if (isym->st_shndx == sec_shndx
1532 && isym->st_value > addr
1533 && isym->st_value < toaddr)
1534 isym->st_value -= count;
1535 }
1536
1537 /* Now adjust the global symbols defined in this section. */
1538 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1539 - symtab_hdr->sh_info);
1540 sym_hashes = elf_sym_hashes (abfd);
1541 end_hashes = sym_hashes + symcount;
1542 for (; sym_hashes < end_hashes; sym_hashes++)
1543 {
1544 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1545 if ((sym_hash->root.type == bfd_link_hash_defined
1546 || sym_hash->root.type == bfd_link_hash_defweak)
1547 && sym_hash->root.u.def.section == sec
1548 && sym_hash->root.u.def.value > addr
1549 && sym_hash->root.u.def.value < toaddr)
1550 {
1551 sym_hash->root.u.def.value -= count;
1552 }
1553 }
1554
1555 return TRUE;
1556}
1557
df406460
NC
1558/* This function handles relaxing for the avr.
1559 Many important relaxing opportunities within functions are already
1560 realized by the compiler itself.
1561 Here we try to replace call (4 bytes) -> rcall (2 bytes)
4cdc7696
NC
1562 and jump -> rjmp (safes also 2 bytes).
1563 As well we now optimize seqences of
df406460
NC
1564 - call/rcall function
1565 - ret
1566 to yield
1567 - jmp/rjmp function
1568 - ret
1569 . In case that within a sequence
1570 - jmp/rjmp label
1571 - ret
1572 the ret could no longer be reached it is optimized away. In order
1573 to check if the ret is no longer needed, it is checked that the ret's address
1574 is not the target of a branch or jump within the same section, it is checked
1575 that there is no skip instruction before the jmp/rjmp and that there
1576 is no local or global label place at the address of the ret.
4cdc7696 1577
df406460 1578 We refrain from relaxing within sections ".vectors" and
4cdc7696 1579 ".jumptables" in order to maintain the position of the instructions.
df406460 1580 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
4cdc7696 1581 if possible. (In future one could possibly use the space of the nop
df406460
NC
1582 for the first instruction of the irq service function.
1583
1584 The .jumptables sections is meant to be used for a future tablejump variant
1585 for the devices with 3-byte program counter where the table itself
4cdc7696 1586 contains 4-byte jump instructions whose relative offset must not
df406460 1587 be changed. */
4cdc7696 1588
28c9d252 1589static bfd_boolean
4cdc7696
NC
1590elf32_avr_relax_section (bfd *abfd,
1591 asection *sec,
df406460
NC
1592 struct bfd_link_info *link_info,
1593 bfd_boolean *again)
1594{
1595 Elf_Internal_Shdr *symtab_hdr;
1596 Elf_Internal_Rela *internal_relocs;
1597 Elf_Internal_Rela *irel, *irelend;
1598 bfd_byte *contents = NULL;
1599 Elf_Internal_Sym *isymbuf = NULL;
1600 static asection *last_input_section = NULL;
1601 static Elf_Internal_Rela *last_reloc = NULL;
28c9d252
NC
1602 struct elf32_avr_link_hash_table *htab;
1603
1604 htab = avr_link_hash_table (link_info);
df406460
NC
1605
1606 /* Assume nothing changes. */
1607 *again = FALSE;
1608
28c9d252
NC
1609 if ((!htab->no_stubs) && (sec == htab->stub_sec))
1610 {
1611 /* We are just relaxing the stub section.
1612 Let's calculate the size needed again. */
1613 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
1614
1615 if (debug_relax)
1616 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
1617 (int) last_estimated_stub_section_size);
1618
1619 elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
1620 link_info, FALSE);
1621
1622 /* Check if the number of trampolines changed. */
1623 if (last_estimated_stub_section_size != htab->stub_sec->size)
1624 *again = TRUE;
1625
1626 if (debug_relax)
1627 printf ("Size of stub section after this pass: %i\n",
1628 (int) htab->stub_sec->size);
1629
1630 return TRUE;
1631 }
1632
df406460
NC
1633 /* We don't have to do anything for a relocatable link, if
1634 this section does not have relocs, or if this is not a
1635 code section. */
1636 if (link_info->relocatable
1637 || (sec->flags & SEC_RELOC) == 0
1638 || sec->reloc_count == 0
1639 || (sec->flags & SEC_CODE) == 0)
1640 return TRUE;
4cdc7696 1641
df406460
NC
1642 /* Check if the object file to relax uses internal symbols so that we
1643 could fix up the relocations. */
df406460
NC
1644 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
1645 return TRUE;
df406460
NC
1646
1647 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1648
1649 /* Get a copy of the native relocations. */
1650 internal_relocs = (_bfd_elf_link_read_relocs
4cdc7696 1651 (abfd, sec, NULL, NULL, link_info->keep_memory));
df406460
NC
1652 if (internal_relocs == NULL)
1653 goto error_return;
1654
1655 if (sec != last_input_section)
1656 last_reloc = NULL;
1657
1658 last_input_section = sec;
1659
1660 /* Walk through the relocs looking for relaxing opportunities. */
1661 irelend = internal_relocs + sec->reloc_count;
1662 for (irel = internal_relocs; irel < irelend; irel++)
1663 {
1664 bfd_vma symval;
1665
4cdc7696 1666 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
df406460
NC
1667 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
1668 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
1669 continue;
4cdc7696 1670
df406460
NC
1671 /* Get the section contents if we haven't done so already. */
1672 if (contents == NULL)
1673 {
1674 /* Get cached copy if it exists. */
1675 if (elf_section_data (sec)->this_hdr.contents != NULL)
1676 contents = elf_section_data (sec)->this_hdr.contents;
1677 else
1678 {
1679 /* Go get them off disk. */
4cdc7696 1680 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
df406460
NC
1681 goto error_return;
1682 }
1683 }
1684
1685 /* Read this BFD's local symbols if we haven't done so already. */
1686 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1687 {
1688 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1689 if (isymbuf == NULL)
1690 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1691 symtab_hdr->sh_info, 0,
1692 NULL, NULL, NULL);
1693 if (isymbuf == NULL)
1694 goto error_return;
1695 }
1696
1697
1698 /* Get the value of the symbol referred to by the reloc. */
1699 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1700 {
1701 /* A local symbol. */
1702 Elf_Internal_Sym *isym;
1703 asection *sym_sec;
1704
1705 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1706 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1707 symval = isym->st_value;
1708 /* If the reloc is absolute, it will not have
1709 a symbol or section associated with it. */
1710 if (sym_sec)
1711 symval += sym_sec->output_section->vma
1712 + sym_sec->output_offset;
1713 }
1714 else
1715 {
1716 unsigned long indx;
1717 struct elf_link_hash_entry *h;
1718
1719 /* An external symbol. */
1720 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1721 h = elf_sym_hashes (abfd)[indx];
1722 BFD_ASSERT (h != NULL);
1723 if (h->root.type != bfd_link_hash_defined
1724 && h->root.type != bfd_link_hash_defweak)
4cdc7696
NC
1725 /* This appears to be a reference to an undefined
1726 symbol. Just ignore it--it will be caught by the
1727 regular reloc processing. */
1728 continue;
1729
df406460
NC
1730 symval = (h->root.u.def.value
1731 + h->root.u.def.section->output_section->vma
1732 + h->root.u.def.section->output_offset);
1733 }
1734
1735 /* For simplicity of coding, we are going to modify the section
1736 contents, the section relocs, and the BFD symbol table. We
1737 must tell the rest of the code not to free up this
1738 information. It would be possible to instead create a table
1739 of changes which have to be made, as is done in coff-mips.c;
1740 that would be more work, but would require less memory when
1741 the linker is run. */
1742 switch (ELF32_R_TYPE (irel->r_info))
1743 {
1744 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1745 pc-relative rcall/rjmp. */
1746 case R_AVR_CALL:
1747 {
1748 bfd_vma value = symval + irel->r_addend;
1749 bfd_vma dot, gap;
1750 int distance_short_enough = 0;
1751
1752 /* Get the address of this instruction. */
1753 dot = (sec->output_section->vma
1754 + sec->output_offset + irel->r_offset);
1755
1756 /* Compute the distance from this insn to the branch target. */
1757 gap = value - dot;
1758
1759 /* If the distance is within -4094..+4098 inclusive, then we can
1760 relax this jump/call. +4098 because the call/jump target
4cdc7696 1761 will be closer after the relaxation. */
df406460
NC
1762 if ((int) gap >= -4094 && (int) gap <= 4098)
1763 distance_short_enough = 1;
1764
1765 /* Here we handle the wrap-around case. E.g. for a 16k device
4cdc7696 1766 we could use a rjmp to jump from address 0x100 to 0x3d00!
df406460
NC
1767 In order to make this work properly, we need to fill the
1768 vaiable avr_pc_wrap_around with the appropriate value.
1769 I.e. 0x4000 for a 16k device. */
1770 {
1771 /* Shrinking the code size makes the gaps larger in the
1772 case of wrap-arounds. So we use a heuristical safety
1773 margin to avoid that during relax the distance gets
1774 again too large for the short jumps. Let's assume
1775 a typical code-size reduction due to relax for a
1776 16k device of 600 bytes. So let's use twice the
4cdc7696 1777 typical value as safety margin. */
df406460
NC
1778 int rgap;
1779 int safety_margin;
1780
1781 int assumed_shrink = 600;
1782 if (avr_pc_wrap_around > 0x4000)
1783 assumed_shrink = 900;
4cdc7696 1784
df406460
NC
1785 safety_margin = 2 * assumed_shrink;
1786
1787 rgap = avr_relative_distance_considering_wrap_around (gap);
4cdc7696
NC
1788
1789 if (rgap >= (-4092 + safety_margin)
df406460 1790 && rgap <= (4094 - safety_margin))
4cdc7696
NC
1791 distance_short_enough = 1;
1792 }
df406460
NC
1793
1794 if (distance_short_enough)
1795 {
1796 unsigned char code_msb;
1797 unsigned char code_lsb;
1798
28c9d252 1799 if (debug_relax)
df406460
NC
1800 printf ("shrinking jump/call instruction at address 0x%x"
1801 " in section %s\n\n",
1802 (int) dot, sec->name);
1803
1804 /* Note that we've changed the relocs, section contents,
1805 etc. */
1806 elf_section_data (sec)->relocs = internal_relocs;
1807 elf_section_data (sec)->this_hdr.contents = contents;
1808 symtab_hdr->contents = (unsigned char *) isymbuf;
1809
1810 /* Get the instruction code for relaxing. */
1811 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
1812 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1813
1814 /* Mask out the relocation bits. */
1815 code_msb &= 0x94;
1816 code_lsb &= 0x0E;
1817 if (code_msb == 0x94 && code_lsb == 0x0E)
1818 {
1819 /* we are changing call -> rcall . */
1820 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1821 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
1822 }
1823 else if (code_msb == 0x94 && code_lsb == 0x0C)
1824 {
1825 /* we are changeing jump -> rjmp. */
1826 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1827 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
1828 }
4cdc7696 1829 else
df406460
NC
1830 abort ();
1831
1832 /* Fix the relocation's type. */
1833 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1834 R_AVR_13_PCREL);
1835
1836 /* Check for the vector section. There we don't want to
1837 modify the ordering! */
1838
1839 if (!strcmp (sec->name,".vectors")
1840 || !strcmp (sec->name,".jumptables"))
1841 {
1842 /* Let's insert a nop. */
1843 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
1844 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
1845 }
1846 else
1847 {
1848 /* Delete two bytes of data. */
1849 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1850 irel->r_offset + 2, 2))
1851 goto error_return;
1852
1853 /* That will change things, so, we should relax again.
1854 Note that this is not required, and it may be slow. */
1855 *again = TRUE;
1856 }
1857 }
1858 }
4cdc7696 1859
df406460
NC
1860 default:
1861 {
1862 unsigned char code_msb;
1863 unsigned char code_lsb;
1864 bfd_vma dot;
1865
1866 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1867 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
1868
1869 /* Get the address of this instruction. */
1870 dot = (sec->output_section->vma
1871 + sec->output_offset + irel->r_offset);
4cdc7696
NC
1872
1873 /* Here we look for rcall/ret or call/ret sequences that could be
28c9d252
NC
1874 safely replaced by rjmp/ret or jmp/ret. */
1875 if (((code_msb & 0xf0) == 0xd0)
1876 && avr_replace_call_ret_sequences)
df406460
NC
1877 {
1878 /* This insn is a rcall. */
1879 unsigned char next_insn_msb = 0;
1880 unsigned char next_insn_lsb = 0;
1881
1882 if (irel->r_offset + 3 < sec->size)
1883 {
4cdc7696 1884 next_insn_msb =
df406460 1885 bfd_get_8 (abfd, contents + irel->r_offset + 3);
4cdc7696 1886 next_insn_lsb =
df406460
NC
1887 bfd_get_8 (abfd, contents + irel->r_offset + 2);
1888 }
4cdc7696
NC
1889
1890 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
df406460
NC
1891 {
1892 /* The next insn is a ret. We now convert the rcall insn
1893 into a rjmp instruction. */
df406460
NC
1894 code_msb &= 0xef;
1895 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
28c9d252 1896 if (debug_relax)
df406460
NC
1897 printf ("converted rcall/ret sequence at address 0x%x"
1898 " into rjmp/ret sequence. Section is %s\n\n",
1899 (int) dot, sec->name);
1900 *again = TRUE;
1901 break;
1902 }
1903 }
1904 else if ((0x94 == (code_msb & 0xfe))
28c9d252
NC
1905 && (0x0e == (code_lsb & 0x0e))
1906 && avr_replace_call_ret_sequences)
df406460
NC
1907 {
1908 /* This insn is a call. */
1909 unsigned char next_insn_msb = 0;
1910 unsigned char next_insn_lsb = 0;
1911
1912 if (irel->r_offset + 5 < sec->size)
1913 {
1914 next_insn_msb =
1915 bfd_get_8 (abfd, contents + irel->r_offset + 5);
1916 next_insn_lsb =
1917 bfd_get_8 (abfd, contents + irel->r_offset + 4);
1918 }
4cdc7696 1919
df406460
NC
1920 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1921 {
1922 /* The next insn is a ret. We now convert the call insn
1923 into a jmp instruction. */
1924
1925 code_lsb &= 0xfd;
1926 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
28c9d252 1927 if (debug_relax)
df406460
NC
1928 printf ("converted call/ret sequence at address 0x%x"
1929 " into jmp/ret sequence. Section is %s\n\n",
1930 (int) dot, sec->name);
1931 *again = TRUE;
1932 break;
1933 }
1934 }
4cdc7696
NC
1935 else if ((0xc0 == (code_msb & 0xf0))
1936 || ((0x94 == (code_msb & 0xfe))
df406460
NC
1937 && (0x0c == (code_lsb & 0x0e))))
1938 {
4cdc7696 1939 /* This insn is a rjmp or a jmp. */
df406460
NC
1940 unsigned char next_insn_msb = 0;
1941 unsigned char next_insn_lsb = 0;
1942 int insn_size;
1943
1944 if (0xc0 == (code_msb & 0xf0))
1945 insn_size = 2; /* rjmp insn */
1946 else
1947 insn_size = 4; /* jmp insn */
1948
1949 if (irel->r_offset + insn_size + 1 < sec->size)
1950 {
4cdc7696
NC
1951 next_insn_msb =
1952 bfd_get_8 (abfd, contents + irel->r_offset
df406460 1953 + insn_size + 1);
4cdc7696
NC
1954 next_insn_lsb =
1955 bfd_get_8 (abfd, contents + irel->r_offset
df406460
NC
1956 + insn_size);
1957 }
1958
1959 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1960 {
1961 /* The next insn is a ret. We possibly could delete
1962 this ret. First we need to check for preceeding
1963 sbis/sbic/sbrs or cpse "skip" instructions. */
1964
1965 int there_is_preceeding_non_skip_insn = 1;
1966 bfd_vma address_of_ret;
1967
1968 address_of_ret = dot + insn_size;
1969
28c9d252 1970 if (debug_relax && (insn_size == 2))
4cdc7696 1971 printf ("found rjmp / ret sequence at address 0x%x\n",
df406460 1972 (int) dot);
28c9d252 1973 if (debug_relax && (insn_size == 4))
4cdc7696 1974 printf ("found jmp / ret sequence at address 0x%x\n",
df406460
NC
1975 (int) dot);
1976
1977 /* We have to make sure that there is a preceeding insn. */
1978 if (irel->r_offset >= 2)
1979 {
1980 unsigned char preceeding_msb;
1981 unsigned char preceeding_lsb;
4cdc7696 1982 preceeding_msb =
df406460 1983 bfd_get_8 (abfd, contents + irel->r_offset - 1);
4cdc7696 1984 preceeding_lsb =
df406460
NC
1985 bfd_get_8 (abfd, contents + irel->r_offset - 2);
1986
1987 /* sbic. */
4cdc7696 1988 if (0x99 == preceeding_msb)
df406460
NC
1989 there_is_preceeding_non_skip_insn = 0;
1990
1991 /* sbis. */
4cdc7696 1992 if (0x9b == preceeding_msb)
df406460
NC
1993 there_is_preceeding_non_skip_insn = 0;
1994
1995 /* sbrc */
1996 if ((0xfc == (preceeding_msb & 0xfe)
1997 && (0x00 == (preceeding_lsb & 0x08))))
1998 there_is_preceeding_non_skip_insn = 0;
1999
4cdc7696 2000 /* sbrs */
df406460
NC
2001 if ((0xfe == (preceeding_msb & 0xfe)
2002 && (0x00 == (preceeding_lsb & 0x08))))
2003 there_is_preceeding_non_skip_insn = 0;
4cdc7696 2004
df406460
NC
2005 /* cpse */
2006 if (0x10 == (preceeding_msb & 0xfc))
2007 there_is_preceeding_non_skip_insn = 0;
4cdc7696 2008
df406460 2009 if (there_is_preceeding_non_skip_insn == 0)
28c9d252 2010 if (debug_relax)
df406460
NC
2011 printf ("preceeding skip insn prevents deletion of"
2012 " ret insn at addr 0x%x in section %s\n",
2013 (int) dot + 2, sec->name);
2014 }
2015 else
2016 {
2017 /* There is no previous instruction. */
2018 there_is_preceeding_non_skip_insn = 0;
4cdc7696 2019 }
df406460
NC
2020
2021 if (there_is_preceeding_non_skip_insn)
2022 {
2023 /* We now only have to make sure that there is no
2024 local label defined at the address of the ret
2025 instruction and that there is no local relocation
2026 in this section pointing to the ret. */
2027
2028 int deleting_ret_is_safe = 1;
4cdc7696 2029 unsigned int section_offset_of_ret_insn =
df406460
NC
2030 irel->r_offset + insn_size;
2031 Elf_Internal_Sym *isym, *isymend;
2032 unsigned int sec_shndx;
4cdc7696
NC
2033
2034 sec_shndx =
2035 _bfd_elf_section_from_bfd_section (abfd, sec);
df406460
NC
2036
2037 /* Check for local symbols. */
2038 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2039 isymend = isym + symtab_hdr->sh_info;
2040 for (; isym < isymend; isym++)
2041 {
2042 if (isym->st_value == section_offset_of_ret_insn
2043 && isym->st_shndx == sec_shndx)
2044 {
2045 deleting_ret_is_safe = 0;
28c9d252 2046 if (debug_relax)
df406460
NC
2047 printf ("local label prevents deletion of ret "
2048 "insn at address 0x%x\n",
2049 (int) dot + insn_size);
2050 }
2051 }
2052
2053 /* Now check for global symbols. */
2054 {
2055 int symcount;
2056 struct elf_link_hash_entry **sym_hashes;
2057 struct elf_link_hash_entry **end_hashes;
2058
4cdc7696 2059 symcount = (symtab_hdr->sh_size
df406460
NC
2060 / sizeof (Elf32_External_Sym)
2061 - symtab_hdr->sh_info);
2062 sym_hashes = elf_sym_hashes (abfd);
2063 end_hashes = sym_hashes + symcount;
2064 for (; sym_hashes < end_hashes; sym_hashes++)
2065 {
4cdc7696 2066 struct elf_link_hash_entry *sym_hash =
df406460
NC
2067 *sym_hashes;
2068 if ((sym_hash->root.type == bfd_link_hash_defined
4cdc7696
NC
2069 || sym_hash->root.type ==
2070 bfd_link_hash_defweak)
df406460 2071 && sym_hash->root.u.def.section == sec
4cdc7696 2072 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
df406460
NC
2073 {
2074 deleting_ret_is_safe = 0;
28c9d252 2075 if (debug_relax)
df406460
NC
2076 printf ("global label prevents deletion of "
2077 "ret insn at address 0x%x\n",
2078 (int) dot + insn_size);
2079 }
2080 }
2081 }
2082 /* Now we check for relocations pointing to ret. */
2083 {
2084 Elf_Internal_Rela *irel;
2085 Elf_Internal_Rela *relend;
2086 Elf_Internal_Shdr *symtab_hdr;
2087
4cdc7696
NC
2088 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2089 relend = elf_section_data (sec)->relocs
df406460
NC
2090 + sec->reloc_count;
2091
4cdc7696 2092 for (irel = elf_section_data (sec)->relocs;
df406460
NC
2093 irel < relend; irel++)
2094 {
2095 bfd_vma reloc_target = 0;
2096 bfd_vma symval;
2097 Elf_Internal_Sym *isymbuf = NULL;
4cdc7696
NC
2098
2099 /* Read this BFD's local symbols if we haven't
df406460
NC
2100 done so already. */
2101 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2102 {
4cdc7696 2103 isymbuf = (Elf_Internal_Sym *)
df406460
NC
2104 symtab_hdr->contents;
2105 if (isymbuf == NULL)
4cdc7696
NC
2106 isymbuf = bfd_elf_get_elf_syms
2107 (abfd,
2108 symtab_hdr,
2109 symtab_hdr->sh_info, 0,
2110 NULL, NULL, NULL);
df406460
NC
2111 if (isymbuf == NULL)
2112 break;
2113 }
4cdc7696
NC
2114
2115 /* Get the value of the symbol referred to
df406460 2116 by the reloc. */
4cdc7696 2117 if (ELF32_R_SYM (irel->r_info)
df406460
NC
2118 < symtab_hdr->sh_info)
2119 {
2120 /* A local symbol. */
2121 Elf_Internal_Sym *isym;
2122 asection *sym_sec;
2123
4cdc7696 2124 isym = isymbuf
df406460 2125 + ELF32_R_SYM (irel->r_info);
4cdc7696
NC
2126 sym_sec = bfd_section_from_elf_index
2127 (abfd, isym->st_shndx);
2128 symval = isym->st_value;
2129
2130 /* If the reloc is absolute, it will not
df406460
NC
2131 have a symbol or section associated
2132 with it. */
4cdc7696 2133
df406460 2134 if (sym_sec)
4cdc7696
NC
2135 {
2136 symval +=
df406460
NC
2137 sym_sec->output_section->vma
2138 + sym_sec->output_offset;
2139 reloc_target = symval + irel->r_addend;
2140 }
2141 else
2142 {
2143 reloc_target = symval + irel->r_addend;
4cdc7696 2144 /* Reference symbol is absolute. */
df406460
NC
2145 }
2146 }
4cdc7696
NC
2147 /* else ... reference symbol is extern. */
2148
df406460 2149 if (address_of_ret == reloc_target)
4cdc7696 2150 {
df406460 2151 deleting_ret_is_safe = 0;
28c9d252 2152 if (debug_relax)
df406460
NC
2153 printf ("ret from "
2154 "rjmp/jmp ret sequence at address"
2155 " 0x%x could not be deleted. ret"
2156 " is target of a relocation.\n",
2157 (int) address_of_ret);
2158 }
2159 }
2160 }
2161
2162 if (deleting_ret_is_safe)
2163 {
28c9d252 2164 if (debug_relax)
df406460
NC
2165 printf ("unreachable ret instruction "
2166 "at address 0x%x deleted.\n",
2167 (int) dot + insn_size);
4cdc7696 2168
df406460
NC
2169 /* Delete two bytes of data. */
2170 if (!elf32_avr_relax_delete_bytes (abfd, sec,
2171 irel->r_offset + insn_size, 2))
2172 goto error_return;
2173
4cdc7696
NC
2174 /* That will change things, so, we should relax
2175 again. Note that this is not required, and it
df406460 2176 may be slow. */
df406460
NC
2177 *again = TRUE;
2178 break;
2179 }
2180 }
4cdc7696
NC
2181
2182 }
2183 }
df406460
NC
2184 break;
2185 }
2186 }
2187 }
2188
2189 if (contents != NULL
2190 && elf_section_data (sec)->this_hdr.contents != contents)
2191 {
2192 if (! link_info->keep_memory)
2193 free (contents);
2194 else
2195 {
2196 /* Cache the section contents for elf_link_input_bfd. */
2197 elf_section_data (sec)->this_hdr.contents = contents;
2198 }
2199 }
2200
2201 if (internal_relocs != NULL
2202 && elf_section_data (sec)->relocs != internal_relocs)
2203 free (internal_relocs);
2204
2205 return TRUE;
2206
2207 error_return:
2208 if (isymbuf != NULL
2209 && symtab_hdr->contents != (unsigned char *) isymbuf)
2210 free (isymbuf);
2211 if (contents != NULL
2212 && elf_section_data (sec)->this_hdr.contents != contents)
2213 free (contents);
2214 if (internal_relocs != NULL
2215 && elf_section_data (sec)->relocs != internal_relocs)
2216 free (internal_relocs);
2217
4cdc7696 2218 return FALSE;
df406460
NC
2219}
2220
2221/* This is a version of bfd_generic_get_relocated_section_contents
4cdc7696 2222 which uses elf32_avr_relocate_section.
df406460 2223
4cdc7696 2224 For avr it's essentially a cut and paste taken from the H8300 port.
df406460 2225 The author of the relaxation support patch for avr had absolutely no
4cdc7696 2226 clue what is happening here but found out that this part of the code
df406460
NC
2227 seems to be important. */
2228
2229static bfd_byte *
2230elf32_avr_get_relocated_section_contents (bfd *output_bfd,
2231 struct bfd_link_info *link_info,
2232 struct bfd_link_order *link_order,
2233 bfd_byte *data,
2234 bfd_boolean relocatable,
2235 asymbol **symbols)
2236{
2237 Elf_Internal_Shdr *symtab_hdr;
2238 asection *input_section = link_order->u.indirect.section;
2239 bfd *input_bfd = input_section->owner;
2240 asection **sections = NULL;
2241 Elf_Internal_Rela *internal_relocs = NULL;
2242 Elf_Internal_Sym *isymbuf = NULL;
2243
2244 /* We only need to handle the case of relaxing, or of having a
2245 particular set of section contents, specially. */
2246 if (relocatable
2247 || elf_section_data (input_section)->this_hdr.contents == NULL)
2248 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2249 link_order, data,
2250 relocatable,
2251 symbols);
2252 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2253
2254 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2255 (size_t) input_section->size);
2256
2257 if ((input_section->flags & SEC_RELOC) != 0
2258 && input_section->reloc_count > 0)
2259 {
2260 asection **secpp;
2261 Elf_Internal_Sym *isym, *isymend;
2262 bfd_size_type amt;
2263
2264 internal_relocs = (_bfd_elf_link_read_relocs
4cdc7696 2265 (input_bfd, input_section, NULL, NULL, FALSE));
df406460
NC
2266 if (internal_relocs == NULL)
2267 goto error_return;
2268
2269 if (symtab_hdr->sh_info != 0)
2270 {
2271 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2272 if (isymbuf == NULL)
2273 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2274 symtab_hdr->sh_info, 0,
2275 NULL, NULL, NULL);
2276 if (isymbuf == NULL)
2277 goto error_return;
2278 }
2279
2280 amt = symtab_hdr->sh_info;
2281 amt *= sizeof (asection *);
4cdc7696 2282 sections = bfd_malloc (amt);
df406460
NC
2283 if (sections == NULL && amt != 0)
2284 goto error_return;
2285
2286 isymend = isymbuf + symtab_hdr->sh_info;
2287 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2288 {
2289 asection *isec;
2290
2291 if (isym->st_shndx == SHN_UNDEF)
2292 isec = bfd_und_section_ptr;
2293 else if (isym->st_shndx == SHN_ABS)
2294 isec = bfd_abs_section_ptr;
2295 else if (isym->st_shndx == SHN_COMMON)
2296 isec = bfd_com_section_ptr;
2297 else
2298 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2299
2300 *secpp = isec;
2301 }
2302
2303 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
2304 input_section, data, internal_relocs,
2305 isymbuf, sections))
2306 goto error_return;
2307
2308 if (sections != NULL)
2309 free (sections);
2310 if (isymbuf != NULL
2311 && symtab_hdr->contents != (unsigned char *) isymbuf)
2312 free (isymbuf);
2313 if (elf_section_data (input_section)->relocs != internal_relocs)
2314 free (internal_relocs);
2315 }
2316
2317 return data;
2318
2319 error_return:
2320 if (sections != NULL)
2321 free (sections);
2322 if (isymbuf != NULL
2323 && symtab_hdr->contents != (unsigned char *) isymbuf)
2324 free (isymbuf);
2325 if (internal_relocs != NULL
2326 && elf_section_data (input_section)->relocs != internal_relocs)
2327 free (internal_relocs);
2328 return NULL;
2329}
2330
2331
28c9d252
NC
2332/* Determines the hash entry name for a particular reloc. It consists of
2333 the identifier of the symbol section and the added reloc addend and
2334 symbol offset relative to the section the symbol is attached to. */
2335
2336static char *
2337avr_stub_name (const asection *symbol_section,
2338 const bfd_vma symbol_offset,
2339 const Elf_Internal_Rela *rela)
2340{
2341 char *stub_name;
2342 bfd_size_type len;
2343
2344 len = 8 + 1 + 8 + 1 + 1;
2345 stub_name = bfd_malloc (len);
2346
2347 sprintf (stub_name, "%08x+%08x",
2348 symbol_section->id & 0xffffffff,
2349 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
2350
2351 return stub_name;
2352}
2353
2354
2355/* Add a new stub entry to the stub hash. Not all fields of the new
2356 stub entry are initialised. */
2357
2358static struct elf32_avr_stub_hash_entry *
2359avr_add_stub (const char *stub_name,
2360 struct elf32_avr_link_hash_table *htab)
2361{
2362 struct elf32_avr_stub_hash_entry *hsh;
2363
2364 /* Enter this entry into the linker stub hash table. */
2365 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
2366
2367 if (hsh == NULL)
2368 {
2369 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
2370 NULL, stub_name);
2371 return NULL;
2372 }
2373
2374 hsh->stub_offset = 0;
2375 return hsh;
2376}
2377
2378/* We assume that there is already space allocated for the stub section
2379 contents and that before building the stubs the section size is
2380 initialized to 0. We assume that within the stub hash table entry,
2381 the absolute position of the jmp target has been written in the
2382 target_value field. We write here the offset of the generated jmp insn
2383 relative to the trampoline section start to the stub_offset entry in
2384 the stub hash table entry. */
2385
2386static bfd_boolean
2387avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2388{
2389 struct elf32_avr_stub_hash_entry *hsh;
2390 struct bfd_link_info *info;
2391 struct elf32_avr_link_hash_table *htab;
2392 bfd *stub_bfd;
2393 bfd_byte *loc;
2394 bfd_vma target;
2395 bfd_vma starget;
2396
2397 /* Basic opcode */
2398 bfd_vma jmp_insn = 0x0000940c;
2399
2400 /* Massage our args to the form they really have. */
2401 hsh = avr_stub_hash_entry (bh);
2402
2403 if (!hsh->is_actually_needed)
2404 return TRUE;
2405
2406 info = (struct bfd_link_info *) in_arg;
2407
2408 htab = avr_link_hash_table (info);
2409
2410 target = hsh->target_value;
2411
2412 /* Make a note of the offset within the stubs for this entry. */
2413 hsh->stub_offset = htab->stub_sec->size;
2414 loc = htab->stub_sec->contents + hsh->stub_offset;
2415
2416 stub_bfd = htab->stub_sec->owner;
2417
2418 if (debug_stubs)
2419 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2420 (unsigned int) target,
2421 (unsigned int) hsh->stub_offset);
2422
2423 /* We now have to add the information on the jump target to the bare
2424 opcode bits already set in jmp_insn. */
2425
2426 /* Check for the alignment of the address. */
2427 if (target & 1)
2428 return FALSE;
2429
2430 starget = target >> 1;
2431 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
2432 bfd_put_16 (stub_bfd, jmp_insn, loc);
2433 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
2434
2435 htab->stub_sec->size += 4;
2436
2437 /* Now add the entries in the address mapping table if there is still
2438 space left. */
2439 {
2440 unsigned int nr;
2441
2442 nr = htab->amt_entry_cnt + 1;
2443 if (nr <= htab->amt_max_entry_cnt)
2444 {
2445 htab->amt_entry_cnt = nr;
2446
2447 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
2448 htab->amt_destination_addr[nr - 1] = target;
2449 }
2450 }
2451
2452 return TRUE;
2453}
2454
2455static bfd_boolean
2456avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
2457 void *in_arg)
2458{
2459 struct elf32_avr_stub_hash_entry *hsh;
2460 struct elf32_avr_link_hash_table *htab;
2461
2462 htab = in_arg;
2463 hsh = avr_stub_hash_entry (bh);
2464 hsh->is_actually_needed = FALSE;
2465
2466 return TRUE;
2467}
2468
2469static bfd_boolean
2470avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2471{
2472 struct elf32_avr_stub_hash_entry *hsh;
2473 struct elf32_avr_link_hash_table *htab;
2474 int size;
2475
2476 /* Massage our args to the form they really have. */
2477 hsh = avr_stub_hash_entry (bh);
2478 htab = in_arg;
2479
2480 if (hsh->is_actually_needed)
2481 size = 4;
2482 else
2483 size = 0;
2484
2485 htab->stub_sec->size += size;
2486 return TRUE;
2487}
2488
2489void
2490elf32_avr_setup_params (struct bfd_link_info *info,
2491 bfd *avr_stub_bfd,
2492 asection *avr_stub_section,
2493 bfd_boolean no_stubs,
2494 bfd_boolean deb_stubs,
2495 bfd_boolean deb_relax,
2496 bfd_vma pc_wrap_around,
2497 bfd_boolean call_ret_replacement)
2498{
2499 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2500
2501 htab->stub_sec = avr_stub_section;
2502 htab->stub_bfd = avr_stub_bfd;
2503 htab->no_stubs = no_stubs;
2504
2505 debug_relax = deb_relax;
2506 debug_stubs = deb_stubs;
2507 avr_pc_wrap_around = pc_wrap_around;
2508 avr_replace_call_ret_sequences = call_ret_replacement;
2509}
2510
2511
2512/* Set up various things so that we can make a list of input sections
2513 for each output section included in the link. Returns -1 on error,
2514 0 when no stubs will be needed, and 1 on success. It also sets
2515 information on the stubs bfd and the stub section in the info
2516 struct. */
2517
2518int
2519elf32_avr_setup_section_lists (bfd *output_bfd,
2520 struct bfd_link_info *info)
2521{
2522 bfd *input_bfd;
2523 unsigned int bfd_count;
2524 int top_id, top_index;
2525 asection *section;
2526 asection **input_list, **list;
2527 bfd_size_type amt;
2528 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2529
2530 if (htab->no_stubs)
2531 return 0;
2532
2533 /* Count the number of input BFDs and find the top input section id. */
2534 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2535 input_bfd != NULL;
2536 input_bfd = input_bfd->link_next)
2537 {
2538 bfd_count += 1;
2539 for (section = input_bfd->sections;
2540 section != NULL;
2541 section = section->next)
2542 if (top_id < section->id)
2543 top_id = section->id;
2544 }
2545
2546 htab->bfd_count = bfd_count;
2547
2548 /* We can't use output_bfd->section_count here to find the top output
2549 section index as some sections may have been removed, and
2550 strip_excluded_output_sections doesn't renumber the indices. */
2551 for (section = output_bfd->sections, top_index = 0;
2552 section != NULL;
2553 section = section->next)
2554 if (top_index < section->index)
2555 top_index = section->index;
2556
2557 htab->top_index = top_index;
2558 amt = sizeof (asection *) * (top_index + 1);
2559 input_list = bfd_malloc (amt);
2560 htab->input_list = input_list;
2561 if (input_list == NULL)
2562 return -1;
2563
2564 /* For sections we aren't interested in, mark their entries with a
2565 value we can check later. */
2566 list = input_list + top_index;
2567 do
2568 *list = bfd_abs_section_ptr;
2569 while (list-- != input_list);
2570
2571 for (section = output_bfd->sections;
2572 section != NULL;
2573 section = section->next)
2574 if ((section->flags & SEC_CODE) != 0)
2575 input_list[section->index] = NULL;
2576
2577 return 1;
2578}
2579
2580
2581/* Read in all local syms for all input bfds, and create hash entries
2582 for export stubs if we are building a multi-subspace shared lib.
2583 Returns -1 on error, 0 otherwise. */
2584
2585static int
2586get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
2587{
2588 unsigned int bfd_indx;
2589 Elf_Internal_Sym *local_syms, **all_local_syms;
2590 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2591
2592 /* We want to read in symbol extension records only once. To do this
2593 we need to read in the local symbols in parallel and save them for
2594 later use; so hold pointers to the local symbols in an array. */
2595 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2596 all_local_syms = bfd_zmalloc (amt);
2597 htab->all_local_syms = all_local_syms;
2598 if (all_local_syms == NULL)
2599 return -1;
2600
2601 /* Walk over all the input BFDs, swapping in local symbols.
2602 If we are creating a shared library, create hash entries for the
2603 export stubs. */
2604 for (bfd_indx = 0;
2605 input_bfd != NULL;
2606 input_bfd = input_bfd->link_next, bfd_indx++)
2607 {
2608 Elf_Internal_Shdr *symtab_hdr;
2609
2610 /* We'll need the symbol table in a second. */
2611 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2612 if (symtab_hdr->sh_info == 0)
2613 continue;
2614
2615 /* We need an array of the local symbols attached to the input bfd. */
2616 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2617 if (local_syms == NULL)
2618 {
2619 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2620 symtab_hdr->sh_info, 0,
2621 NULL, NULL, NULL);
2622 /* Cache them for elf_link_input_bfd. */
2623 symtab_hdr->contents = (unsigned char *) local_syms;
2624 }
2625 if (local_syms == NULL)
2626 return -1;
2627
2628 all_local_syms[bfd_indx] = local_syms;
2629 }
2630
2631 return 0;
2632}
2633
2634#define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2635
2636bfd_boolean
2637elf32_avr_size_stubs (bfd *output_bfd,
2638 struct bfd_link_info *info,
2639 bfd_boolean is_prealloc_run)
2640{
2641 struct elf32_avr_link_hash_table *htab;
2642 int stub_changed = 0;
2643
2644 htab = avr_link_hash_table (info);
2645
2646 /* At this point we initialize htab->vector_base
2647 To the start of the text output section. */
2648 htab->vector_base = htab->stub_sec->output_section->vma;
2649
2650 if (get_local_syms (info->input_bfds, info))
2651 {
2652 if (htab->all_local_syms)
2653 goto error_ret_free_local;
2654 return FALSE;
2655 }
2656
2657 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
2658 {
2659 struct elf32_avr_stub_hash_entry *test;
2660
2661 test = avr_add_stub ("Hugo",htab);
2662 test->target_value = 0x123456;
2663 test->stub_offset = 13;
2664
2665 test = avr_add_stub ("Hugo2",htab);
2666 test->target_value = 0x84210;
2667 test->stub_offset = 14;
2668 }
2669
2670 while (1)
2671 {
2672 bfd *input_bfd;
2673 unsigned int bfd_indx;
2674
2675 /* We will have to re-generate the stub hash table each time anything
2676 in memory has changed. */
2677
2678 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
2679 for (input_bfd = info->input_bfds, bfd_indx = 0;
2680 input_bfd != NULL;
2681 input_bfd = input_bfd->link_next, bfd_indx++)
2682 {
2683 Elf_Internal_Shdr *symtab_hdr;
2684 asection *section;
2685 Elf_Internal_Sym *local_syms;
2686
2687 /* We'll need the symbol table in a second. */
2688 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2689 if (symtab_hdr->sh_info == 0)
2690 continue;
2691
2692 local_syms = htab->all_local_syms[bfd_indx];
2693
2694 /* Walk over each section attached to the input bfd. */
2695 for (section = input_bfd->sections;
2696 section != NULL;
2697 section = section->next)
2698 {
2699 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2700
2701 /* If there aren't any relocs, then there's nothing more
2702 to do. */
2703 if ((section->flags & SEC_RELOC) == 0
2704 || section->reloc_count == 0)
2705 continue;
2706
2707 /* If this section is a link-once section that will be
2708 discarded, then don't create any stubs. */
2709 if (section->output_section == NULL
2710 || section->output_section->owner != output_bfd)
2711 continue;
2712
2713 /* Get the relocs. */
2714 internal_relocs
2715 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2716 info->keep_memory);
2717 if (internal_relocs == NULL)
2718 goto error_ret_free_local;
2719
2720 /* Now examine each relocation. */
2721 irela = internal_relocs;
2722 irelaend = irela + section->reloc_count;
2723 for (; irela < irelaend; irela++)
2724 {
2725 unsigned int r_type, r_indx;
2726 struct elf32_avr_stub_hash_entry *hsh;
2727 asection *sym_sec;
2728 bfd_vma sym_value;
2729 bfd_vma destination;
2730 struct elf_link_hash_entry *hh;
2731 char *stub_name;
2732
2733 r_type = ELF32_R_TYPE (irela->r_info);
2734 r_indx = ELF32_R_SYM (irela->r_info);
2735
2736 /* Only look for 16 bit GS relocs. No other reloc will need a
2737 stub. */
2738 if (!((r_type == R_AVR_16_PM)
2739 || (r_type == R_AVR_LO8_LDI_GS)
2740 || (r_type == R_AVR_HI8_LDI_GS)))
2741 continue;
2742
2743 /* Now determine the call target, its name, value,
2744 section. */
2745 sym_sec = NULL;
2746 sym_value = 0;
2747 destination = 0;
2748 hh = NULL;
2749 if (r_indx < symtab_hdr->sh_info)
2750 {
2751 /* It's a local symbol. */
2752 Elf_Internal_Sym *sym;
2753 Elf_Internal_Shdr *hdr;
2754
2755 sym = local_syms + r_indx;
2756 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2757 sym_sec = hdr->bfd_section;
2758 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2759 sym_value = sym->st_value;
2760 destination = (sym_value + irela->r_addend
2761 + sym_sec->output_offset
2762 + sym_sec->output_section->vma);
2763 }
2764 else
2765 {
2766 /* It's an external symbol. */
2767 int e_indx;
2768
2769 e_indx = r_indx - symtab_hdr->sh_info;
2770 hh = elf_sym_hashes (input_bfd)[e_indx];
2771
2772 while (hh->root.type == bfd_link_hash_indirect
2773 || hh->root.type == bfd_link_hash_warning)
2774 hh = (struct elf_link_hash_entry *)
2775 (hh->root.u.i.link);
2776
2777 if (hh->root.type == bfd_link_hash_defined
2778 || hh->root.type == bfd_link_hash_defweak)
2779 {
2780 sym_sec = hh->root.u.def.section;
2781 sym_value = hh->root.u.def.value;
2782 if (sym_sec->output_section != NULL)
2783 destination = (sym_value + irela->r_addend
2784 + sym_sec->output_offset
2785 + sym_sec->output_section->vma);
2786 }
2787 else if (hh->root.type == bfd_link_hash_undefweak)
2788 {
2789 if (! info->shared)
2790 continue;
2791 }
2792 else if (hh->root.type == bfd_link_hash_undefined)
2793 {
2794 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2795 && (ELF_ST_VISIBILITY (hh->other)
2796 == STV_DEFAULT)))
2797 continue;
2798 }
2799 else
2800 {
2801 bfd_set_error (bfd_error_bad_value);
2802
2803 error_ret_free_internal:
2804 if (elf_section_data (section)->relocs == NULL)
2805 free (internal_relocs);
2806 goto error_ret_free_local;
2807 }
2808 }
2809
2810 if (! avr_stub_is_required_for_16_bit_reloc
2811 (destination - htab->vector_base))
2812 {
2813 if (!is_prealloc_run)
2814 /* We are having a reloc that does't need a stub. */
2815 continue;
2816
2817 /* We don't right now know if a stub will be needed.
2818 Let's rather be on the safe side. */
2819 }
2820
2821 /* Get the name of this stub. */
2822 stub_name = avr_stub_name (sym_sec, sym_value, irela);
2823
2824 if (!stub_name)
2825 goto error_ret_free_internal;
2826
2827
2828 hsh = avr_stub_hash_lookup (&htab->bstab,
2829 stub_name,
2830 FALSE, FALSE);
2831 if (hsh != NULL)
2832 {
2833 /* The proper stub has already been created. Mark it
2834 to be used and write the possibly changed destination
2835 value. */
2836 hsh->is_actually_needed = TRUE;
2837 hsh->target_value = destination;
2838 free (stub_name);
2839 continue;
2840 }
2841
2842 hsh = avr_add_stub (stub_name, htab);
2843 if (hsh == NULL)
2844 {
2845 free (stub_name);
2846 goto error_ret_free_internal;
2847 }
2848
2849 hsh->is_actually_needed = TRUE;
2850 hsh->target_value = destination;
2851
2852 if (debug_stubs)
2853 printf ("Adding stub with destination 0x%x to the"
2854 " hash table.\n", (unsigned int) destination);
2855 if (debug_stubs)
2856 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
2857
2858 stub_changed = TRUE;
2859 }
2860
2861 /* We're done with the internal relocs, free them. */
2862 if (elf_section_data (section)->relocs == NULL)
2863 free (internal_relocs);
2864 }
2865 }
2866
2867 /* Re-Calculate the number of needed stubs. */
2868 htab->stub_sec->size = 0;
2869 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
2870
2871 if (!stub_changed)
2872 break;
2873
2874 stub_changed = FALSE;
2875 }
2876
2877 free (htab->all_local_syms);
2878 return TRUE;
2879
2880 error_ret_free_local:
2881 free (htab->all_local_syms);
2882 return FALSE;
2883}
2884
2885
2886/* Build all the stubs associated with the current output file. The
2887 stubs are kept in a hash table attached to the main linker hash
2888 table. We also set up the .plt entries for statically linked PIC
2889 functions here. This function is called via hppaelf_finish in the
2890 linker. */
2891
2892bfd_boolean
2893elf32_avr_build_stubs (struct bfd_link_info *info)
2894{
2895 asection *stub_sec;
2896 struct bfd_hash_table *table;
2897 struct elf32_avr_link_hash_table *htab;
2898 bfd_size_type total_size = 0;
2899
2900 htab = avr_link_hash_table (info);
2901
2902 /* In case that there were several stub sections: */
2903 for (stub_sec = htab->stub_bfd->sections;
2904 stub_sec != NULL;
2905 stub_sec = stub_sec->next)
2906 {
2907 bfd_size_type size;
2908
2909 /* Allocate memory to hold the linker stubs. */
2910 size = stub_sec->size;
2911 total_size += size;
2912
2913 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2914 if (stub_sec->contents == NULL && size != 0)
2915 return FALSE;
2916 stub_sec->size = 0;
2917 }
2918
2919 /* Allocate memory for the adress mapping table. */
2920 htab->amt_entry_cnt = 0;
2921 htab->amt_max_entry_cnt = total_size / 4;
2922 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
2923 * htab->amt_max_entry_cnt);
2924 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
2925 * htab->amt_max_entry_cnt );
2926
2927 if (debug_stubs)
2928 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
2929
2930 /* Build the stubs as directed by the stub hash table. */
2931 table = &htab->bstab;
2932 bfd_hash_traverse (table, avr_build_one_stub, info);
2933
2934 if (debug_stubs)
2935 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
2936
2937 return TRUE;
2938}
2939
adde6300
AM
2940#define ELF_ARCH bfd_arch_avr
2941#define ELF_MACHINE_CODE EM_AVR
aa4f99bb 2942#define ELF_MACHINE_ALT1 EM_AVR_OLD
adde6300
AM
2943#define ELF_MAXPAGESIZE 1
2944
2945#define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2946#define TARGET_LITTLE_NAME "elf32-avr"
2947
28c9d252
NC
2948#define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
2949#define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
2950
adde6300
AM
2951#define elf_info_to_howto avr_info_to_howto_rela
2952#define elf_info_to_howto_rel NULL
2953#define elf_backend_relocate_section elf32_avr_relocate_section
2954#define elf_backend_gc_mark_hook elf32_avr_gc_mark_hook
2955#define elf_backend_gc_sweep_hook elf32_avr_gc_sweep_hook
2956#define elf_backend_check_relocs elf32_avr_check_relocs
2957#define elf_backend_can_gc_sections 1
f0fe0e16 2958#define elf_backend_rela_normal 1
adde6300
AM
2959#define elf_backend_final_write_processing \
2960 bfd_elf_avr_final_write_processing
2961#define elf_backend_object_p elf32_avr_object_p
2962
df406460
NC
2963#define bfd_elf32_bfd_relax_section elf32_avr_relax_section
2964#define bfd_elf32_bfd_get_relocated_section_contents \
2965 elf32_avr_get_relocated_section_contents
2966
adde6300 2967#include "elf32-target.h"
This page took 0.445052 seconds and 4 git commands to generate.