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