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