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