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