/src/binutils-gdb/bfd/elf-m10200.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Matsushita 10200 specific support for 32-bit ELF |
2 | | Copyright (C) 1996-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of BFD, the Binary File Descriptor library. |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | | MA 02110-1301, USA. */ |
20 | | |
21 | | #include "sysdep.h" |
22 | | #include "bfd.h" |
23 | | #include "libbfd.h" |
24 | | #include "elf-bfd.h" |
25 | | |
26 | | static bool |
27 | | mn10200_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int); |
28 | | static bool |
29 | | mn10200_elf_symbol_address_p (bfd *, asection *, Elf_Internal_Sym *, bfd_vma); |
30 | | |
31 | | enum reloc_type |
32 | | { |
33 | | R_MN10200_NONE = 0, |
34 | | R_MN10200_32, |
35 | | R_MN10200_16, |
36 | | R_MN10200_8, |
37 | | R_MN10200_24, |
38 | | R_MN10200_PCREL8, |
39 | | R_MN10200_PCREL16, |
40 | | R_MN10200_PCREL24, |
41 | | R_MN10200_MAX |
42 | | }; |
43 | | |
44 | | static reloc_howto_type elf_mn10200_howto_table[] = |
45 | | { |
46 | | /* Dummy relocation. Does nothing. */ |
47 | | HOWTO (R_MN10200_NONE, |
48 | | 0, |
49 | | 0, |
50 | | 0, |
51 | | false, |
52 | | 0, |
53 | | complain_overflow_dont, |
54 | | bfd_elf_generic_reloc, |
55 | | "R_MN10200_NONE", |
56 | | false, |
57 | | 0, |
58 | | 0, |
59 | | false), |
60 | | /* Standard 32 bit reloc. */ |
61 | | HOWTO (R_MN10200_32, |
62 | | 0, |
63 | | 4, |
64 | | 32, |
65 | | false, |
66 | | 0, |
67 | | complain_overflow_bitfield, |
68 | | bfd_elf_generic_reloc, |
69 | | "R_MN10200_32", |
70 | | false, |
71 | | 0xffffffff, |
72 | | 0xffffffff, |
73 | | false), |
74 | | /* Standard 16 bit reloc. */ |
75 | | HOWTO (R_MN10200_16, |
76 | | 0, |
77 | | 2, |
78 | | 16, |
79 | | false, |
80 | | 0, |
81 | | complain_overflow_bitfield, |
82 | | bfd_elf_generic_reloc, |
83 | | "R_MN10200_16", |
84 | | false, |
85 | | 0xffff, |
86 | | 0xffff, |
87 | | false), |
88 | | /* Standard 8 bit reloc. */ |
89 | | HOWTO (R_MN10200_8, |
90 | | 0, |
91 | | 1, |
92 | | 8, |
93 | | false, |
94 | | 0, |
95 | | complain_overflow_bitfield, |
96 | | bfd_elf_generic_reloc, |
97 | | "R_MN10200_8", |
98 | | false, |
99 | | 0xff, |
100 | | 0xff, |
101 | | false), |
102 | | /* Standard 24 bit reloc. */ |
103 | | HOWTO (R_MN10200_24, |
104 | | 0, |
105 | | 4, |
106 | | 24, |
107 | | false, |
108 | | 0, |
109 | | complain_overflow_bitfield, |
110 | | bfd_elf_generic_reloc, |
111 | | "R_MN10200_24", |
112 | | false, |
113 | | 0xffffff, |
114 | | 0xffffff, |
115 | | false), |
116 | | /* Simple 8 pc-relative reloc. */ |
117 | | HOWTO (R_MN10200_PCREL8, |
118 | | 0, |
119 | | 1, |
120 | | 8, |
121 | | true, |
122 | | 0, |
123 | | complain_overflow_bitfield, |
124 | | bfd_elf_generic_reloc, |
125 | | "R_MN10200_PCREL8", |
126 | | false, |
127 | | 0xff, |
128 | | 0xff, |
129 | | true), |
130 | | /* Simple 16 pc-relative reloc. */ |
131 | | HOWTO (R_MN10200_PCREL16, |
132 | | 0, |
133 | | 2, |
134 | | 16, |
135 | | true, |
136 | | 0, |
137 | | complain_overflow_bitfield, |
138 | | bfd_elf_generic_reloc, |
139 | | "R_MN10200_PCREL16", |
140 | | false, |
141 | | 0xffff, |
142 | | 0xffff, |
143 | | true), |
144 | | /* Simple 32bit pc-relative reloc with a 1 byte adjustment |
145 | | to get the pc-relative offset correct. */ |
146 | | HOWTO (R_MN10200_PCREL24, |
147 | | 0, |
148 | | 4, |
149 | | 24, |
150 | | true, |
151 | | 0, |
152 | | complain_overflow_bitfield, |
153 | | bfd_elf_generic_reloc, |
154 | | "R_MN10200_PCREL24", |
155 | | false, |
156 | | 0xffffff, |
157 | | 0xffffff, |
158 | | true), |
159 | | }; |
160 | | |
161 | | struct mn10200_reloc_map |
162 | | { |
163 | | bfd_reloc_code_real_type bfd_reloc_val; |
164 | | unsigned char elf_reloc_val; |
165 | | }; |
166 | | |
167 | | static const struct mn10200_reloc_map mn10200_reloc_map[] = |
168 | | { |
169 | | { BFD_RELOC_NONE , R_MN10200_NONE , }, |
170 | | { BFD_RELOC_32 , R_MN10200_32 , }, |
171 | | { BFD_RELOC_16 , R_MN10200_16 , }, |
172 | | { BFD_RELOC_8 , R_MN10200_8 , }, |
173 | | { BFD_RELOC_24 , R_MN10200_24 , }, |
174 | | { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , }, |
175 | | { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, }, |
176 | | { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, }, |
177 | | }; |
178 | | |
179 | | static reloc_howto_type * |
180 | | bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
181 | | bfd_reloc_code_real_type code) |
182 | 0 | { |
183 | 0 | unsigned int i; |
184 | |
|
185 | 0 | for (i = 0; |
186 | 0 | i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map); |
187 | 0 | i++) |
188 | 0 | { |
189 | 0 | if (mn10200_reloc_map[i].bfd_reloc_val == code) |
190 | 0 | return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val]; |
191 | 0 | } |
192 | | |
193 | 0 | return NULL; |
194 | 0 | } |
195 | | |
196 | | static reloc_howto_type * |
197 | | bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
198 | | const char *r_name) |
199 | 0 | { |
200 | 0 | unsigned int i; |
201 | |
|
202 | 0 | for (i = 0; |
203 | 0 | i < (sizeof (elf_mn10200_howto_table) |
204 | 0 | / sizeof (elf_mn10200_howto_table[0])); |
205 | 0 | i++) |
206 | 0 | if (elf_mn10200_howto_table[i].name != NULL |
207 | 0 | && strcasecmp (elf_mn10200_howto_table[i].name, r_name) == 0) |
208 | 0 | return &elf_mn10200_howto_table[i]; |
209 | | |
210 | 0 | return NULL; |
211 | 0 | } |
212 | | |
213 | | /* Set the howto pointer for an MN10200 ELF reloc. */ |
214 | | |
215 | | static bool |
216 | | mn10200_info_to_howto (bfd *abfd, |
217 | | arelent *cache_ptr, |
218 | | Elf_Internal_Rela *dst) |
219 | 0 | { |
220 | 0 | unsigned int r_type; |
221 | |
|
222 | 0 | r_type = ELF32_R_TYPE (dst->r_info); |
223 | 0 | if (r_type >= (unsigned int) R_MN10200_MAX) |
224 | 0 | { |
225 | | /* xgettext:c-format */ |
226 | 0 | _bfd_error_handler (_("%pB: unsupported relocation type %#x"), |
227 | 0 | abfd, r_type); |
228 | 0 | bfd_set_error (bfd_error_bad_value); |
229 | 0 | return false; |
230 | 0 | } |
231 | | |
232 | 0 | cache_ptr->howto = &elf_mn10200_howto_table[r_type]; |
233 | 0 | return cache_ptr->howto != NULL; |
234 | 0 | } |
235 | | |
236 | | /* Perform a relocation as part of a final link. */ |
237 | | |
238 | | static bfd_reloc_status_type |
239 | | mn10200_elf_final_link_relocate (reloc_howto_type *howto, |
240 | | bfd *input_bfd, |
241 | | bfd *output_bfd ATTRIBUTE_UNUSED, |
242 | | asection *input_section, |
243 | | bfd_byte *contents, |
244 | | bfd_vma offset, |
245 | | bfd_vma value, |
246 | | bfd_vma addend, |
247 | | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
248 | | asection *sym_sec ATTRIBUTE_UNUSED, |
249 | | int is_local ATTRIBUTE_UNUSED) |
250 | 0 | { |
251 | 0 | unsigned long r_type = howto->type; |
252 | 0 | bfd_byte *hit_data = contents + offset; |
253 | |
|
254 | 0 | switch (r_type) |
255 | 0 | { |
256 | | |
257 | 0 | case R_MN10200_NONE: |
258 | 0 | return bfd_reloc_ok; |
259 | | |
260 | 0 | case R_MN10200_32: |
261 | 0 | value += addend; |
262 | 0 | bfd_put_32 (input_bfd, value, hit_data); |
263 | 0 | return bfd_reloc_ok; |
264 | | |
265 | 0 | case R_MN10200_16: |
266 | 0 | value += addend; |
267 | |
|
268 | 0 | if ((long) value > 0x7fff || (long) value < -0x8000) |
269 | 0 | return bfd_reloc_overflow; |
270 | | |
271 | 0 | bfd_put_16 (input_bfd, value, hit_data); |
272 | 0 | return bfd_reloc_ok; |
273 | | |
274 | 0 | case R_MN10200_8: |
275 | 0 | value += addend; |
276 | |
|
277 | 0 | if ((long) value > 0x7f || (long) value < -0x80) |
278 | 0 | return bfd_reloc_overflow; |
279 | | |
280 | 0 | bfd_put_8 (input_bfd, value, hit_data); |
281 | 0 | return bfd_reloc_ok; |
282 | | |
283 | 0 | case R_MN10200_24: |
284 | 0 | value += addend; |
285 | |
|
286 | 0 | if ((long) value > 0x7fffff || (long) value < -0x800000) |
287 | 0 | return bfd_reloc_overflow; |
288 | | |
289 | 0 | value &= 0xffffff; |
290 | 0 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); |
291 | 0 | bfd_put_32 (input_bfd, value, hit_data); |
292 | 0 | return bfd_reloc_ok; |
293 | | |
294 | 0 | case R_MN10200_PCREL8: |
295 | 0 | value -= (input_section->output_section->vma |
296 | 0 | + input_section->output_offset); |
297 | 0 | value -= (offset + 1); |
298 | 0 | value += addend; |
299 | |
|
300 | 0 | if ((long) value > 0xff || (long) value < -0x100) |
301 | 0 | return bfd_reloc_overflow; |
302 | | |
303 | 0 | bfd_put_8 (input_bfd, value, hit_data); |
304 | 0 | return bfd_reloc_ok; |
305 | | |
306 | 0 | case R_MN10200_PCREL16: |
307 | 0 | value -= (input_section->output_section->vma |
308 | 0 | + input_section->output_offset); |
309 | 0 | value -= (offset + 2); |
310 | 0 | value += addend; |
311 | |
|
312 | 0 | if ((long) value > 0xffff || (long) value < -0x10000) |
313 | 0 | return bfd_reloc_overflow; |
314 | | |
315 | 0 | bfd_put_16 (input_bfd, value, hit_data); |
316 | 0 | return bfd_reloc_ok; |
317 | | |
318 | 0 | case R_MN10200_PCREL24: |
319 | 0 | value -= (input_section->output_section->vma |
320 | 0 | + input_section->output_offset); |
321 | 0 | value -= (offset + 3); |
322 | 0 | value += addend; |
323 | |
|
324 | 0 | if ((long) value > 0xffffff || (long) value < -0x1000000) |
325 | 0 | return bfd_reloc_overflow; |
326 | | |
327 | 0 | value &= 0xffffff; |
328 | 0 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); |
329 | 0 | bfd_put_32 (input_bfd, value, hit_data); |
330 | 0 | return bfd_reloc_ok; |
331 | | |
332 | 0 | default: |
333 | 0 | return bfd_reloc_notsupported; |
334 | 0 | } |
335 | 0 | } |
336 | | |
337 | | /* Relocate an MN10200 ELF section. */ |
338 | | static int |
339 | | mn10200_elf_relocate_section (bfd *output_bfd, |
340 | | struct bfd_link_info *info, |
341 | | bfd *input_bfd, |
342 | | asection *input_section, |
343 | | bfd_byte *contents, |
344 | | Elf_Internal_Rela *relocs, |
345 | | Elf_Internal_Sym *local_syms, |
346 | | asection **local_sections) |
347 | 0 | { |
348 | 0 | Elf_Internal_Shdr *symtab_hdr; |
349 | 0 | struct elf_link_hash_entry **sym_hashes; |
350 | 0 | Elf_Internal_Rela *rel, *relend; |
351 | |
|
352 | 0 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
353 | 0 | sym_hashes = elf_sym_hashes (input_bfd); |
354 | |
|
355 | 0 | rel = relocs; |
356 | 0 | relend = relocs + input_section->reloc_count; |
357 | 0 | for (; rel < relend; rel++) |
358 | 0 | { |
359 | 0 | int r_type; |
360 | 0 | reloc_howto_type *howto; |
361 | 0 | unsigned long r_symndx; |
362 | 0 | Elf_Internal_Sym *sym; |
363 | 0 | asection *sec; |
364 | 0 | struct elf_link_hash_entry *h; |
365 | 0 | bfd_vma relocation; |
366 | 0 | bfd_reloc_status_type r; |
367 | |
|
368 | 0 | r_symndx = ELF32_R_SYM (rel->r_info); |
369 | 0 | r_type = ELF32_R_TYPE (rel->r_info); |
370 | 0 | howto = elf_mn10200_howto_table + r_type; |
371 | |
|
372 | 0 | h = NULL; |
373 | 0 | sym = NULL; |
374 | 0 | sec = NULL; |
375 | 0 | if (r_symndx < symtab_hdr->sh_info) |
376 | 0 | { |
377 | 0 | sym = local_syms + r_symndx; |
378 | 0 | sec = local_sections[r_symndx]; |
379 | 0 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
380 | 0 | } |
381 | 0 | else |
382 | 0 | { |
383 | 0 | bool unresolved_reloc, warned, ignored; |
384 | |
|
385 | 0 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
386 | 0 | r_symndx, symtab_hdr, sym_hashes, |
387 | 0 | h, sec, relocation, |
388 | 0 | unresolved_reloc, warned, ignored); |
389 | 0 | } |
390 | | |
391 | 0 | if (sec != NULL && discarded_section (sec)) |
392 | 0 | RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, |
393 | 0 | rel, 1, relend, howto, 0, contents); |
394 | |
|
395 | 0 | if (bfd_link_relocatable (info)) |
396 | 0 | continue; |
397 | | |
398 | 0 | r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd, |
399 | 0 | input_section, |
400 | 0 | contents, rel->r_offset, |
401 | 0 | relocation, rel->r_addend, |
402 | 0 | info, sec, h == NULL); |
403 | |
|
404 | 0 | if (r != bfd_reloc_ok) |
405 | 0 | { |
406 | 0 | const char *name; |
407 | 0 | const char *msg = (const char *) 0; |
408 | |
|
409 | 0 | if (h != NULL) |
410 | 0 | name = h->root.root.string; |
411 | 0 | else |
412 | 0 | { |
413 | 0 | name = (bfd_elf_string_from_elf_section |
414 | 0 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); |
415 | 0 | if (name == NULL || *name == '\0') |
416 | 0 | name = bfd_section_name (sec); |
417 | 0 | } |
418 | |
|
419 | 0 | switch (r) |
420 | 0 | { |
421 | 0 | case bfd_reloc_overflow: |
422 | 0 | (*info->callbacks->reloc_overflow) |
423 | 0 | (info, (h ? &h->root : NULL), name, howto->name, |
424 | 0 | (bfd_vma) 0, input_bfd, input_section, rel->r_offset); |
425 | 0 | break; |
426 | | |
427 | 0 | case bfd_reloc_undefined: |
428 | 0 | (*info->callbacks->undefined_symbol) (info, name, input_bfd, |
429 | 0 | input_section, |
430 | 0 | rel->r_offset, true); |
431 | 0 | break; |
432 | | |
433 | 0 | case bfd_reloc_outofrange: |
434 | 0 | msg = _("internal error: out of range error"); |
435 | 0 | goto common_error; |
436 | | |
437 | 0 | case bfd_reloc_notsupported: |
438 | 0 | msg = _("internal error: unsupported relocation error"); |
439 | 0 | goto common_error; |
440 | | |
441 | 0 | case bfd_reloc_dangerous: |
442 | 0 | msg = _("internal error: dangerous error"); |
443 | 0 | goto common_error; |
444 | | |
445 | 0 | default: |
446 | 0 | msg = _("internal error: unknown error"); |
447 | | /* fall through */ |
448 | |
|
449 | 0 | common_error: |
450 | 0 | (*info->callbacks->warning) (info, msg, name, input_bfd, |
451 | 0 | input_section, rel->r_offset); |
452 | 0 | break; |
453 | 0 | } |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | 0 | return true; |
458 | 0 | } |
459 | | |
460 | | /* Delete some bytes from a section while relaxing. */ |
461 | | |
462 | | static bool |
463 | | mn10200_elf_relax_delete_bytes (bfd *abfd, asection *sec, |
464 | | bfd_vma addr, int count) |
465 | 0 | { |
466 | 0 | Elf_Internal_Shdr *symtab_hdr; |
467 | 0 | unsigned int sec_shndx; |
468 | 0 | bfd_byte *contents; |
469 | 0 | Elf_Internal_Rela *irel, *irelend; |
470 | 0 | bfd_vma toaddr; |
471 | 0 | Elf_Internal_Sym *isym; |
472 | 0 | Elf_Internal_Sym *isymend; |
473 | 0 | struct elf_link_hash_entry **sym_hashes; |
474 | 0 | struct elf_link_hash_entry **end_hashes; |
475 | 0 | unsigned int symcount; |
476 | |
|
477 | 0 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
478 | |
|
479 | 0 | contents = elf_section_data (sec)->this_hdr.contents; |
480 | |
|
481 | 0 | toaddr = sec->size; |
482 | |
|
483 | 0 | irel = elf_section_data (sec)->relocs; |
484 | 0 | irelend = irel + sec->reloc_count; |
485 | | |
486 | | /* Actually delete the bytes. */ |
487 | 0 | memmove (contents + addr, contents + addr + count, |
488 | 0 | (size_t) (toaddr - addr - count)); |
489 | 0 | sec->size -= count; |
490 | | |
491 | | /* Adjust all the relocs. */ |
492 | 0 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) |
493 | 0 | { |
494 | | /* Get the new reloc address. */ |
495 | 0 | if ((irel->r_offset > addr |
496 | 0 | && irel->r_offset < toaddr)) |
497 | 0 | irel->r_offset -= count; |
498 | 0 | } |
499 | | |
500 | | /* Adjust the local symbols defined in this section. */ |
501 | 0 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
502 | 0 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; |
503 | 0 | for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++) |
504 | 0 | { |
505 | 0 | if (isym->st_shndx == sec_shndx |
506 | 0 | && isym->st_value > addr |
507 | 0 | && isym->st_value < toaddr) |
508 | 0 | isym->st_value -= count; |
509 | 0 | } |
510 | | |
511 | | /* Now adjust the global symbols defined in this section. */ |
512 | 0 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
513 | 0 | - symtab_hdr->sh_info); |
514 | 0 | sym_hashes = elf_sym_hashes (abfd); |
515 | 0 | end_hashes = sym_hashes + symcount; |
516 | 0 | for (; sym_hashes < end_hashes; sym_hashes++) |
517 | 0 | { |
518 | 0 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
519 | 0 | if ((sym_hash->root.type == bfd_link_hash_defined |
520 | 0 | || sym_hash->root.type == bfd_link_hash_defweak) |
521 | 0 | && sym_hash->root.u.def.section == sec |
522 | 0 | && sym_hash->root.u.def.value > addr |
523 | 0 | && sym_hash->root.u.def.value < toaddr) |
524 | 0 | { |
525 | 0 | sym_hash->root.u.def.value -= count; |
526 | 0 | } |
527 | 0 | } |
528 | |
|
529 | 0 | return true; |
530 | 0 | } |
531 | | |
532 | | /* This function handles relaxing for the mn10200. |
533 | | |
534 | | There are quite a few relaxing opportunities available on the mn10200: |
535 | | |
536 | | * jsr:24 -> jsr:16 2 bytes |
537 | | |
538 | | * jmp:24 -> jmp:16 2 bytes |
539 | | * jmp:16 -> bra:8 1 byte |
540 | | |
541 | | * If the previous instruction is a conditional branch |
542 | | around the jump/bra, we may be able to reverse its condition |
543 | | and change its target to the jump's target. The jump/bra |
544 | | can then be deleted. 2 bytes |
545 | | |
546 | | * mov abs24 -> mov abs16 2 byte savings |
547 | | |
548 | | * Most instructions which accept imm24 can relax to imm16 2 bytes |
549 | | - Most instructions which accept imm16 can relax to imm8 1 byte |
550 | | |
551 | | * Most instructions which accept d24 can relax to d16 2 bytes |
552 | | - Most instructions which accept d16 can relax to d8 1 byte |
553 | | |
554 | | abs24, imm24, d24 all look the same at the reloc level. It |
555 | | might make the code simpler if we had different relocs for |
556 | | the various relaxable operand types. |
557 | | |
558 | | We don't handle imm16->imm8 or d16->d8 as they're very rare |
559 | | and somewhat more difficult to support. */ |
560 | | |
561 | | static bool |
562 | | mn10200_elf_relax_section (bfd *abfd, |
563 | | asection *sec, |
564 | | struct bfd_link_info *link_info, |
565 | | bool *again) |
566 | 0 | { |
567 | 0 | Elf_Internal_Shdr *symtab_hdr; |
568 | 0 | Elf_Internal_Rela *internal_relocs; |
569 | 0 | Elf_Internal_Rela *irel, *irelend; |
570 | 0 | bfd_byte *contents = NULL; |
571 | 0 | Elf_Internal_Sym *isymbuf = NULL; |
572 | | |
573 | | /* Assume nothing changes. */ |
574 | 0 | *again = false; |
575 | | |
576 | | /* We don't have to do anything for a relocatable link, if |
577 | | this section does not have relocs, or if this is not a |
578 | | code section. */ |
579 | 0 | if (bfd_link_relocatable (link_info) |
580 | 0 | || sec->reloc_count == 0 |
581 | 0 | || (sec->flags & SEC_RELOC) == 0 |
582 | 0 | || (sec->flags & SEC_HAS_CONTENTS) == 0 |
583 | 0 | || (sec->flags & SEC_CODE) == 0) |
584 | 0 | return true; |
585 | | |
586 | 0 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
587 | | |
588 | | /* Get a copy of the native relocations. */ |
589 | 0 | internal_relocs = (_bfd_elf_link_read_relocs |
590 | 0 | (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, |
591 | 0 | link_info->keep_memory)); |
592 | 0 | if (internal_relocs == NULL) |
593 | 0 | goto error_return; |
594 | | |
595 | | /* Walk through them looking for relaxing opportunities. */ |
596 | 0 | irelend = internal_relocs + sec->reloc_count; |
597 | 0 | for (irel = internal_relocs; irel < irelend; irel++) |
598 | 0 | { |
599 | 0 | bfd_vma symval; |
600 | | |
601 | | /* If this isn't something that can be relaxed, then ignore |
602 | | this reloc. */ |
603 | 0 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE |
604 | 0 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8 |
605 | 0 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX) |
606 | 0 | continue; |
607 | | |
608 | | /* Get the section contents if we haven't done so already. */ |
609 | 0 | if (contents == NULL) |
610 | 0 | { |
611 | | /* Get cached copy if it exists. */ |
612 | 0 | if (elf_section_data (sec)->this_hdr.contents != NULL) |
613 | 0 | contents = elf_section_data (sec)->this_hdr.contents; |
614 | 0 | else |
615 | 0 | { |
616 | | /* Go get them off disk. */ |
617 | 0 | if (!bfd_malloc_and_get_section (abfd, sec, &contents)) |
618 | 0 | goto error_return; |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | | /* Read this BFD's local symbols if we haven't done so already. */ |
623 | 0 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) |
624 | 0 | { |
625 | 0 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
626 | 0 | if (isymbuf == NULL) |
627 | 0 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, |
628 | 0 | symtab_hdr->sh_info, 0, |
629 | 0 | NULL, NULL, NULL); |
630 | 0 | if (isymbuf == NULL) |
631 | 0 | goto error_return; |
632 | 0 | } |
633 | | |
634 | | /* Get the value of the symbol referred to by the reloc. */ |
635 | 0 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) |
636 | 0 | { |
637 | | /* A local symbol. */ |
638 | 0 | Elf_Internal_Sym *isym; |
639 | 0 | asection *sym_sec; |
640 | |
|
641 | 0 | isym = isymbuf + ELF32_R_SYM (irel->r_info); |
642 | 0 | if (isym->st_shndx == SHN_UNDEF) |
643 | 0 | sym_sec = bfd_und_section_ptr; |
644 | 0 | else if (isym->st_shndx == SHN_ABS) |
645 | 0 | sym_sec = bfd_abs_section_ptr; |
646 | 0 | else if (isym->st_shndx == SHN_COMMON) |
647 | 0 | sym_sec = bfd_com_section_ptr; |
648 | 0 | else |
649 | 0 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
650 | 0 | symval = (isym->st_value |
651 | 0 | + sym_sec->output_section->vma |
652 | 0 | + sym_sec->output_offset); |
653 | 0 | } |
654 | 0 | else |
655 | 0 | { |
656 | 0 | unsigned long indx; |
657 | 0 | struct elf_link_hash_entry *h; |
658 | | |
659 | | /* An external symbol. */ |
660 | 0 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; |
661 | 0 | h = elf_sym_hashes (abfd)[indx]; |
662 | 0 | BFD_ASSERT (h != NULL); |
663 | 0 | if (h->root.type != bfd_link_hash_defined |
664 | 0 | && h->root.type != bfd_link_hash_defweak) |
665 | 0 | { |
666 | | /* This appears to be a reference to an undefined |
667 | | symbol. Just ignore it--it will be caught by the |
668 | | regular reloc processing. */ |
669 | 0 | continue; |
670 | 0 | } |
671 | | |
672 | 0 | symval = (h->root.u.def.value |
673 | 0 | + h->root.u.def.section->output_section->vma |
674 | 0 | + h->root.u.def.section->output_offset); |
675 | 0 | } |
676 | | |
677 | | /* For simplicity of coding, we are going to modify the section |
678 | | contents, the section relocs, and the BFD symbol table. We |
679 | | must tell the rest of the code not to free up this |
680 | | information. It would be possible to instead create a table |
681 | | of changes which have to be made, as is done in coff-mips.c; |
682 | | that would be more work, but would require less memory when |
683 | | the linker is run. */ |
684 | | |
685 | | /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative |
686 | | branch/call. */ |
687 | 0 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24) |
688 | 0 | { |
689 | 0 | bfd_vma value = symval; |
690 | | |
691 | | /* Deal with pc-relative gunk. */ |
692 | 0 | value -= (sec->output_section->vma + sec->output_offset); |
693 | 0 | value -= (irel->r_offset + 3); |
694 | 0 | value += irel->r_addend; |
695 | | |
696 | | /* See if the value will fit in 16 bits, note the high value is |
697 | | 0x7fff + 2 as the target will be two bytes closer if we are |
698 | | able to relax. */ |
699 | 0 | if ((long) value < 0x8001 && (long) value > -0x8000) |
700 | 0 | { |
701 | 0 | unsigned char code; |
702 | | |
703 | | /* Get the opcode. */ |
704 | 0 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
705 | |
|
706 | 0 | if (code != 0xe0 && code != 0xe1) |
707 | 0 | continue; |
708 | | |
709 | | /* Note that we've changed the relocs, section contents, etc. */ |
710 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
711 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
712 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
713 | | |
714 | | /* Fix the opcode. */ |
715 | 0 | if (code == 0xe0) |
716 | 0 | bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2); |
717 | 0 | else if (code == 0xe1) |
718 | 0 | bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2); |
719 | | |
720 | | /* Fix the relocation's type. */ |
721 | 0 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
722 | 0 | R_MN10200_PCREL16); |
723 | | |
724 | | /* The opcode got shorter too, so we have to fix the offset. */ |
725 | 0 | irel->r_offset -= 1; |
726 | | |
727 | | /* Delete two bytes of data. */ |
728 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
729 | 0 | irel->r_offset + 1, 2)) |
730 | 0 | goto error_return; |
731 | | |
732 | | /* That will change things, so, we should relax again. |
733 | | Note that this is not required, and it may be slow. */ |
734 | 0 | *again = true; |
735 | 0 | } |
736 | 0 | } |
737 | | |
738 | | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative |
739 | | branch. */ |
740 | 0 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16) |
741 | 0 | { |
742 | 0 | bfd_vma value = symval; |
743 | | |
744 | | /* Deal with pc-relative gunk. */ |
745 | 0 | value -= (sec->output_section->vma + sec->output_offset); |
746 | 0 | value -= (irel->r_offset + 2); |
747 | 0 | value += irel->r_addend; |
748 | | |
749 | | /* See if the value will fit in 8 bits, note the high value is |
750 | | 0x7f + 1 as the target will be one bytes closer if we are |
751 | | able to relax. */ |
752 | 0 | if ((long) value < 0x80 && (long) value > -0x80) |
753 | 0 | { |
754 | 0 | unsigned char code; |
755 | | |
756 | | /* Get the opcode. */ |
757 | 0 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
758 | |
|
759 | 0 | if (code != 0xfc) |
760 | 0 | continue; |
761 | | |
762 | | /* Note that we've changed the relocs, section contents, etc. */ |
763 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
764 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
765 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
766 | | |
767 | | /* Fix the opcode. */ |
768 | 0 | bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1); |
769 | | |
770 | | /* Fix the relocation's type. */ |
771 | 0 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
772 | 0 | R_MN10200_PCREL8); |
773 | | |
774 | | /* Delete one byte of data. */ |
775 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
776 | 0 | irel->r_offset + 1, 1)) |
777 | 0 | goto error_return; |
778 | | |
779 | | /* That will change things, so, we should relax again. |
780 | | Note that this is not required, and it may be slow. */ |
781 | 0 | *again = true; |
782 | 0 | } |
783 | 0 | } |
784 | | |
785 | | /* Try to eliminate an unconditional 8 bit pc-relative branch |
786 | | which immediately follows a conditional 8 bit pc-relative |
787 | | branch around the unconditional branch. |
788 | | |
789 | | original: new: |
790 | | bCC lab1 bCC' lab2 |
791 | | bra lab2 |
792 | | lab1: lab1: |
793 | | |
794 | | This happens when the bCC can't reach lab2 at assembly time, |
795 | | but due to other relaxations it can reach at link time. */ |
796 | 0 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8) |
797 | 0 | { |
798 | 0 | Elf_Internal_Rela *nrel; |
799 | 0 | unsigned char code; |
800 | | |
801 | | /* Do nothing if this reloc is the last byte in the section. */ |
802 | 0 | if (irel->r_offset == sec->size) |
803 | 0 | continue; |
804 | | |
805 | | /* See if the next instruction is an unconditional pc-relative |
806 | | branch, more often than not this test will fail, so we |
807 | | test it first to speed things up. */ |
808 | 0 | code = bfd_get_8 (abfd, contents + irel->r_offset + 1); |
809 | 0 | if (code != 0xea) |
810 | 0 | continue; |
811 | | |
812 | | /* Also make sure the next relocation applies to the next |
813 | | instruction and that it's a pc-relative 8 bit branch. */ |
814 | 0 | nrel = irel + 1; |
815 | 0 | if (nrel == irelend |
816 | 0 | || irel->r_offset + 2 != nrel->r_offset |
817 | 0 | || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8) |
818 | 0 | continue; |
819 | | |
820 | | /* Make sure our destination immediately follows the |
821 | | unconditional branch. */ |
822 | 0 | if (symval != (sec->output_section->vma + sec->output_offset |
823 | 0 | + irel->r_offset + 3)) |
824 | 0 | continue; |
825 | | |
826 | | /* Now make sure we are a conditional branch. This may not |
827 | | be necessary, but why take the chance. |
828 | | |
829 | | Note these checks assume that R_MN10200_PCREL8 relocs |
830 | | only occur on bCC and bCCx insns. If they occured |
831 | | elsewhere, we'd need to know the start of this insn |
832 | | for this check to be accurate. */ |
833 | 0 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
834 | 0 | if (code != 0xe0 && code != 0xe1 && code != 0xe2 |
835 | 0 | && code != 0xe3 && code != 0xe4 && code != 0xe5 |
836 | 0 | && code != 0xe6 && code != 0xe7 && code != 0xe8 |
837 | 0 | && code != 0xe9 && code != 0xec && code != 0xed |
838 | 0 | && code != 0xee && code != 0xef && code != 0xfc |
839 | 0 | && code != 0xfd && code != 0xfe && code != 0xff) |
840 | 0 | continue; |
841 | | |
842 | | /* We also have to be sure there is no symbol/label |
843 | | at the unconditional branch. */ |
844 | 0 | if (mn10200_elf_symbol_address_p (abfd, sec, isymbuf, |
845 | 0 | irel->r_offset + 1)) |
846 | 0 | continue; |
847 | | |
848 | | /* Note that we've changed the relocs, section contents, etc. */ |
849 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
850 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
851 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
852 | | |
853 | | /* Reverse the condition of the first branch. */ |
854 | 0 | switch (code) |
855 | 0 | { |
856 | 0 | case 0xfc: |
857 | 0 | code = 0xfd; |
858 | 0 | break; |
859 | 0 | case 0xfd: |
860 | 0 | code = 0xfc; |
861 | 0 | break; |
862 | 0 | case 0xfe: |
863 | 0 | code = 0xff; |
864 | 0 | break; |
865 | 0 | case 0xff: |
866 | 0 | code = 0xfe; |
867 | 0 | break; |
868 | 0 | case 0xe8: |
869 | 0 | code = 0xe9; |
870 | 0 | break; |
871 | 0 | case 0xe9: |
872 | 0 | code = 0xe8; |
873 | 0 | break; |
874 | 0 | case 0xe0: |
875 | 0 | code = 0xe2; |
876 | 0 | break; |
877 | 0 | case 0xe2: |
878 | 0 | code = 0xe0; |
879 | 0 | break; |
880 | 0 | case 0xe3: |
881 | 0 | code = 0xe1; |
882 | 0 | break; |
883 | 0 | case 0xe1: |
884 | 0 | code = 0xe3; |
885 | 0 | break; |
886 | 0 | case 0xe4: |
887 | 0 | code = 0xe6; |
888 | 0 | break; |
889 | 0 | case 0xe6: |
890 | 0 | code = 0xe4; |
891 | 0 | break; |
892 | 0 | case 0xe7: |
893 | 0 | code = 0xe5; |
894 | 0 | break; |
895 | 0 | case 0xe5: |
896 | 0 | code = 0xe7; |
897 | 0 | break; |
898 | 0 | case 0xec: |
899 | 0 | code = 0xed; |
900 | 0 | break; |
901 | 0 | case 0xed: |
902 | 0 | code = 0xec; |
903 | 0 | break; |
904 | 0 | case 0xee: |
905 | 0 | code = 0xef; |
906 | 0 | break; |
907 | 0 | case 0xef: |
908 | 0 | code = 0xee; |
909 | 0 | break; |
910 | 0 | } |
911 | 0 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
912 | | |
913 | | /* Set the reloc type and symbol for the first branch |
914 | | from the second branch. */ |
915 | 0 | irel->r_info = nrel->r_info; |
916 | | |
917 | | /* Make the reloc for the second branch a null reloc. */ |
918 | 0 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), |
919 | 0 | R_MN10200_NONE); |
920 | | |
921 | | /* Delete two bytes of data. */ |
922 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
923 | 0 | irel->r_offset + 1, 2)) |
924 | 0 | goto error_return; |
925 | | |
926 | | /* That will change things, so, we should relax again. |
927 | | Note that this is not required, and it may be slow. */ |
928 | 0 | *again = true; |
929 | 0 | } |
930 | | |
931 | | /* Try to turn a 24bit immediate, displacement or absolute address |
932 | | into a 16bit immediate, displacement or absolute address. */ |
933 | 0 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24) |
934 | 0 | { |
935 | 0 | bfd_vma value = symval; |
936 | | |
937 | | /* See if the value will fit in 16 bits. |
938 | | We allow any 16bit match here. We prune those we can't |
939 | | handle below. */ |
940 | 0 | if ((long) value < 0x7fff && (long) value > -0x8000) |
941 | 0 | { |
942 | 0 | unsigned char code; |
943 | | |
944 | | /* All insns which have 24bit operands are 5 bytes long, |
945 | | the first byte will always be 0xf4, but we double check |
946 | | it just in case. */ |
947 | | |
948 | | /* Get the first opcode. */ |
949 | 0 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); |
950 | |
|
951 | 0 | if (code != 0xf4) |
952 | 0 | continue; |
953 | | |
954 | | /* Get the second opcode. */ |
955 | 0 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
956 | |
|
957 | 0 | switch (code & 0xfc) |
958 | 0 | { |
959 | | /* mov imm24,dn -> mov imm16,dn */ |
960 | 0 | case 0x70: |
961 | | /* Not safe if the high bit is on as relaxing may |
962 | | move the value out of high mem and thus not fit |
963 | | in a signed 16bit value. */ |
964 | 0 | if (value & 0x8000) |
965 | 0 | continue; |
966 | | |
967 | | /* Note that we've changed the relocation contents, etc. */ |
968 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
969 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
970 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
971 | | |
972 | | /* Fix the opcode. */ |
973 | 0 | bfd_put_8 (abfd, 0xf8 + (code & 0x03), |
974 | 0 | contents + irel->r_offset - 2); |
975 | | |
976 | | /* Fix the relocation's type. */ |
977 | 0 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
978 | 0 | R_MN10200_16); |
979 | | |
980 | | /* The opcode got shorter too, so we have to fix the |
981 | | offset. */ |
982 | 0 | irel->r_offset -= 1; |
983 | | |
984 | | /* Delete two bytes of data. */ |
985 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
986 | 0 | irel->r_offset + 1, 2)) |
987 | 0 | goto error_return; |
988 | | |
989 | | /* That will change things, so, we should relax again. |
990 | | Note that this is not required, and it may be slow. */ |
991 | 0 | *again = true; |
992 | 0 | break; |
993 | | |
994 | | /* mov imm24,an -> mov imm16,an |
995 | | cmp imm24,an -> cmp imm16,an |
996 | | mov (abs24),dn -> mov (abs16),dn |
997 | | mov dn,(abs24) -> mov dn,(abs16) |
998 | | movb dn,(abs24) -> movb dn,(abs16) |
999 | | movbu (abs24),dn -> movbu (abs16),dn */ |
1000 | 0 | case 0x74: |
1001 | 0 | case 0x7c: |
1002 | 0 | case 0xc0: |
1003 | 0 | case 0x40: |
1004 | 0 | case 0x44: |
1005 | 0 | case 0xc8: |
1006 | | /* Note that we've changed the relocation contents, etc. */ |
1007 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
1008 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
1009 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
1010 | |
|
1011 | 0 | if ((code & 0xfc) == 0x74) |
1012 | 0 | code = 0xdc + (code & 0x03); |
1013 | 0 | else if ((code & 0xfc) == 0x7c) |
1014 | 0 | code = 0xec + (code & 0x03); |
1015 | 0 | else if ((code & 0xfc) == 0xc0) |
1016 | 0 | code = 0xc8 + (code & 0x03); |
1017 | 0 | else if ((code & 0xfc) == 0x40) |
1018 | 0 | code = 0xc0 + (code & 0x03); |
1019 | 0 | else if ((code & 0xfc) == 0x44) |
1020 | 0 | code = 0xc4 + (code & 0x03); |
1021 | 0 | else if ((code & 0xfc) == 0xc8) |
1022 | 0 | code = 0xcc + (code & 0x03); |
1023 | | |
1024 | | /* Fix the opcode. */ |
1025 | 0 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); |
1026 | | |
1027 | | /* Fix the relocation's type. */ |
1028 | 0 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
1029 | 0 | R_MN10200_16); |
1030 | | |
1031 | | /* The opcode got shorter too, so we have to fix the |
1032 | | offset. */ |
1033 | 0 | irel->r_offset -= 1; |
1034 | | |
1035 | | /* Delete two bytes of data. */ |
1036 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
1037 | 0 | irel->r_offset + 1, 2)) |
1038 | 0 | goto error_return; |
1039 | | |
1040 | | /* That will change things, so, we should relax again. |
1041 | | Note that this is not required, and it may be slow. */ |
1042 | 0 | *again = true; |
1043 | 0 | break; |
1044 | | |
1045 | | /* cmp imm24,dn -> cmp imm16,dn |
1046 | | mov (abs24),an -> mov (abs16),an |
1047 | | mov an,(abs24) -> mov an,(abs16) |
1048 | | add imm24,dn -> add imm16,dn |
1049 | | add imm24,an -> add imm16,an |
1050 | | sub imm24,dn -> sub imm16,dn |
1051 | | sub imm24,an -> sub imm16,an |
1052 | | And all d24->d16 in memory ops. */ |
1053 | 0 | case 0x78: |
1054 | 0 | case 0xd0: |
1055 | 0 | case 0x50: |
1056 | 0 | case 0x60: |
1057 | 0 | case 0x64: |
1058 | 0 | case 0x68: |
1059 | 0 | case 0x6c: |
1060 | 0 | case 0x80: |
1061 | 0 | case 0xf0: |
1062 | 0 | case 0x00: |
1063 | 0 | case 0x10: |
1064 | 0 | case 0xb0: |
1065 | 0 | case 0x30: |
1066 | 0 | case 0xa0: |
1067 | 0 | case 0x20: |
1068 | 0 | case 0x90: |
1069 | | /* Not safe if the high bit is on as relaxing may |
1070 | | move the value out of high mem and thus not fit |
1071 | | in a signed 16bit value. */ |
1072 | 0 | if (((code & 0xfc) == 0x78 |
1073 | 0 | || (code & 0xfc) == 0x60 |
1074 | 0 | || (code & 0xfc) == 0x64 |
1075 | 0 | || (code & 0xfc) == 0x68 |
1076 | 0 | || (code & 0xfc) == 0x6c |
1077 | 0 | || (code & 0xfc) == 0x80 |
1078 | 0 | || (code & 0xfc) == 0xf0 |
1079 | 0 | || (code & 0xfc) == 0x00 |
1080 | 0 | || (code & 0xfc) == 0x10 |
1081 | 0 | || (code & 0xfc) == 0xb0 |
1082 | 0 | || (code & 0xfc) == 0x30 |
1083 | 0 | || (code & 0xfc) == 0xa0 |
1084 | 0 | || (code & 0xfc) == 0x20 |
1085 | 0 | || (code & 0xfc) == 0x90) |
1086 | 0 | && (value & 0x8000) != 0) |
1087 | 0 | continue; |
1088 | | |
1089 | | /* Note that we've changed the relocation contents, etc. */ |
1090 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
1091 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
1092 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
1093 | | |
1094 | | /* Fix the opcode. */ |
1095 | 0 | bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2); |
1096 | |
|
1097 | 0 | if ((code & 0xfc) == 0x78) |
1098 | 0 | code = 0x48 + (code & 0x03); |
1099 | 0 | else if ((code & 0xfc) == 0xd0) |
1100 | 0 | code = 0x30 + (code & 0x03); |
1101 | 0 | else if ((code & 0xfc) == 0x50) |
1102 | 0 | code = 0x20 + (code & 0x03); |
1103 | 0 | else if ((code & 0xfc) == 0x60) |
1104 | 0 | code = 0x18 + (code & 0x03); |
1105 | 0 | else if ((code & 0xfc) == 0x64) |
1106 | 0 | code = 0x08 + (code & 0x03); |
1107 | 0 | else if ((code & 0xfc) == 0x68) |
1108 | 0 | code = 0x1c + (code & 0x03); |
1109 | 0 | else if ((code & 0xfc) == 0x6c) |
1110 | 0 | code = 0x0c + (code & 0x03); |
1111 | 0 | else if ((code & 0xfc) == 0x80) |
1112 | 0 | code = 0xc0 + (code & 0x07); |
1113 | 0 | else if ((code & 0xfc) == 0xf0) |
1114 | 0 | code = 0xb0 + (code & 0x07); |
1115 | 0 | else if ((code & 0xfc) == 0x00) |
1116 | 0 | code = 0x80 + (code & 0x07); |
1117 | 0 | else if ((code & 0xfc) == 0x10) |
1118 | 0 | code = 0xa0 + (code & 0x07); |
1119 | 0 | else if ((code & 0xfc) == 0xb0) |
1120 | 0 | code = 0x70 + (code & 0x07); |
1121 | 0 | else if ((code & 0xfc) == 0x30) |
1122 | 0 | code = 0x60 + (code & 0x07); |
1123 | 0 | else if ((code & 0xfc) == 0xa0) |
1124 | 0 | code = 0xd0 + (code & 0x07); |
1125 | 0 | else if ((code & 0xfc) == 0x20) |
1126 | 0 | code = 0x90 + (code & 0x07); |
1127 | 0 | else if ((code & 0xfc) == 0x90) |
1128 | 0 | code = 0x50 + (code & 0x07); |
1129 | |
|
1130 | 0 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
1131 | | |
1132 | | /* Fix the relocation's type. */ |
1133 | 0 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
1134 | 0 | R_MN10200_16); |
1135 | | |
1136 | | /* Delete one bytes of data. */ |
1137 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
1138 | 0 | irel->r_offset + 2, 1)) |
1139 | 0 | goto error_return; |
1140 | | |
1141 | | /* That will change things, so, we should relax again. |
1142 | | Note that this is not required, and it may be slow. */ |
1143 | 0 | *again = true; |
1144 | 0 | break; |
1145 | | |
1146 | | /* movb (abs24),dn ->movbu (abs16),dn extxb bn */ |
1147 | 0 | case 0xc4: |
1148 | | /* Note that we've changed the reldection contents, etc. */ |
1149 | 0 | elf_section_data (sec)->relocs = internal_relocs; |
1150 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
1151 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
1152 | |
|
1153 | 0 | bfd_put_8 (abfd, 0xcc + (code & 0x03), |
1154 | 0 | contents + irel->r_offset - 2); |
1155 | |
|
1156 | 0 | bfd_put_8 (abfd, 0xb8 + (code & 0x03), |
1157 | 0 | contents + irel->r_offset - 1); |
1158 | | |
1159 | | /* Fix the relocation's type. */ |
1160 | 0 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
1161 | 0 | R_MN10200_16); |
1162 | | |
1163 | | /* The reloc will be applied one byte in front of its |
1164 | | current location. */ |
1165 | 0 | irel->r_offset -= 1; |
1166 | | |
1167 | | /* Delete one bytes of data. */ |
1168 | 0 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
1169 | 0 | irel->r_offset + 2, 1)) |
1170 | 0 | goto error_return; |
1171 | | |
1172 | | /* That will change things, so, we should relax again. |
1173 | | Note that this is not required, and it may be slow. */ |
1174 | 0 | *again = true; |
1175 | 0 | break; |
1176 | 0 | } |
1177 | 0 | } |
1178 | 0 | } |
1179 | 0 | } |
1180 | | |
1181 | 0 | if (isymbuf != NULL |
1182 | 0 | && symtab_hdr->contents != (unsigned char *) isymbuf) |
1183 | 0 | { |
1184 | 0 | if (! link_info->keep_memory) |
1185 | 0 | free (isymbuf); |
1186 | 0 | else |
1187 | 0 | { |
1188 | | /* Cache the symbols for elf_link_input_bfd. */ |
1189 | 0 | symtab_hdr->contents = (unsigned char *) isymbuf; |
1190 | 0 | } |
1191 | 0 | } |
1192 | |
|
1193 | 0 | if (contents != NULL |
1194 | 0 | && elf_section_data (sec)->this_hdr.contents != contents) |
1195 | 0 | { |
1196 | 0 | if (! link_info->keep_memory) |
1197 | 0 | free (contents); |
1198 | 0 | else |
1199 | 0 | { |
1200 | | /* Cache the section contents for elf_link_input_bfd. */ |
1201 | 0 | elf_section_data (sec)->this_hdr.contents = contents; |
1202 | 0 | } |
1203 | 0 | } |
1204 | |
|
1205 | 0 | if (elf_section_data (sec)->relocs != internal_relocs) |
1206 | 0 | free (internal_relocs); |
1207 | |
|
1208 | 0 | return true; |
1209 | | |
1210 | 0 | error_return: |
1211 | 0 | if (symtab_hdr->contents != (unsigned char *) isymbuf) |
1212 | 0 | free (isymbuf); |
1213 | 0 | if (elf_section_data (sec)->this_hdr.contents != contents) |
1214 | 0 | free (contents); |
1215 | 0 | if (elf_section_data (sec)->relocs != internal_relocs) |
1216 | 0 | free (internal_relocs); |
1217 | |
|
1218 | 0 | return false; |
1219 | 0 | } |
1220 | | |
1221 | | /* Return TRUE if a symbol exists at the given address, else return |
1222 | | FALSE. */ |
1223 | | static bool |
1224 | | mn10200_elf_symbol_address_p (bfd *abfd, |
1225 | | asection *sec, |
1226 | | Elf_Internal_Sym *isym, |
1227 | | bfd_vma addr) |
1228 | 0 | { |
1229 | 0 | Elf_Internal_Shdr *symtab_hdr; |
1230 | 0 | unsigned int sec_shndx; |
1231 | 0 | Elf_Internal_Sym *isymend; |
1232 | 0 | struct elf_link_hash_entry **sym_hashes; |
1233 | 0 | struct elf_link_hash_entry **end_hashes; |
1234 | 0 | unsigned int symcount; |
1235 | |
|
1236 | 0 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
1237 | | |
1238 | | /* Examine all the local symbols. */ |
1239 | 0 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1240 | 0 | for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++) |
1241 | 0 | { |
1242 | 0 | if (isym->st_shndx == sec_shndx |
1243 | 0 | && isym->st_value == addr) |
1244 | 0 | return true; |
1245 | 0 | } |
1246 | | |
1247 | 0 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
1248 | 0 | - symtab_hdr->sh_info); |
1249 | 0 | sym_hashes = elf_sym_hashes (abfd); |
1250 | 0 | end_hashes = sym_hashes + symcount; |
1251 | 0 | for (; sym_hashes < end_hashes; sym_hashes++) |
1252 | 0 | { |
1253 | 0 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
1254 | 0 | if ((sym_hash->root.type == bfd_link_hash_defined |
1255 | 0 | || sym_hash->root.type == bfd_link_hash_defweak) |
1256 | 0 | && sym_hash->root.u.def.section == sec |
1257 | 0 | && sym_hash->root.u.def.value == addr) |
1258 | 0 | return true; |
1259 | 0 | } |
1260 | | |
1261 | 0 | return false; |
1262 | 0 | } |
1263 | | |
1264 | | /* This is a version of bfd_generic_get_relocated_section_contents |
1265 | | which uses mn10200_elf_relocate_section. */ |
1266 | | |
1267 | | static bfd_byte * |
1268 | | mn10200_elf_get_relocated_section_contents (bfd *output_bfd, |
1269 | | struct bfd_link_info *link_info, |
1270 | | struct bfd_link_order *link_order, |
1271 | | bfd_byte *data, |
1272 | | bool relocatable, |
1273 | | asymbol **symbols) |
1274 | 0 | { |
1275 | 0 | Elf_Internal_Shdr *symtab_hdr; |
1276 | 0 | asection *input_section = link_order->u.indirect.section; |
1277 | 0 | bfd *input_bfd = input_section->owner; |
1278 | 0 | asection **sections = NULL; |
1279 | 0 | Elf_Internal_Rela *internal_relocs = NULL; |
1280 | 0 | Elf_Internal_Sym *isymbuf = NULL; |
1281 | | |
1282 | | /* We only need to handle the case of relaxing, or of having a |
1283 | | particular set of section contents, specially. */ |
1284 | 0 | if (relocatable |
1285 | 0 | || elf_section_data (input_section)->this_hdr.contents == NULL) |
1286 | 0 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, |
1287 | 0 | link_order, data, |
1288 | 0 | relocatable, |
1289 | 0 | symbols); |
1290 | | |
1291 | 0 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
1292 | |
|
1293 | 0 | bfd_byte *orig_data = data; |
1294 | 0 | if (data == NULL) |
1295 | 0 | { |
1296 | 0 | data = bfd_malloc (input_section->size); |
1297 | 0 | if (data == NULL) |
1298 | 0 | return NULL; |
1299 | 0 | } |
1300 | 0 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, |
1301 | 0 | (size_t) input_section->size); |
1302 | |
|
1303 | 0 | if ((input_section->flags & SEC_RELOC) != 0 |
1304 | 0 | && input_section->reloc_count > 0) |
1305 | 0 | { |
1306 | 0 | Elf_Internal_Sym *isym; |
1307 | 0 | Elf_Internal_Sym *isymend; |
1308 | 0 | asection **secpp; |
1309 | 0 | bfd_size_type amt; |
1310 | |
|
1311 | 0 | internal_relocs = (_bfd_elf_link_read_relocs |
1312 | 0 | (input_bfd, input_section, NULL, |
1313 | 0 | (Elf_Internal_Rela *) NULL, false)); |
1314 | 0 | if (internal_relocs == NULL) |
1315 | 0 | goto error_return; |
1316 | | |
1317 | 0 | if (symtab_hdr->sh_info != 0) |
1318 | 0 | { |
1319 | 0 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
1320 | 0 | if (isymbuf == NULL) |
1321 | 0 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, |
1322 | 0 | symtab_hdr->sh_info, 0, |
1323 | 0 | NULL, NULL, NULL); |
1324 | 0 | if (isymbuf == NULL) |
1325 | 0 | goto error_return; |
1326 | 0 | } |
1327 | | |
1328 | 0 | amt = symtab_hdr->sh_info; |
1329 | 0 | amt *= sizeof (asection *); |
1330 | 0 | sections = (asection **) bfd_malloc (amt); |
1331 | 0 | if (sections == NULL && amt != 0) |
1332 | 0 | goto error_return; |
1333 | | |
1334 | 0 | isymend = isymbuf + symtab_hdr->sh_info; |
1335 | 0 | for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) |
1336 | 0 | { |
1337 | 0 | asection *isec; |
1338 | |
|
1339 | 0 | if (isym->st_shndx == SHN_UNDEF) |
1340 | 0 | isec = bfd_und_section_ptr; |
1341 | 0 | else if (isym->st_shndx == SHN_ABS) |
1342 | 0 | isec = bfd_abs_section_ptr; |
1343 | 0 | else if (isym->st_shndx == SHN_COMMON) |
1344 | 0 | isec = bfd_com_section_ptr; |
1345 | 0 | else |
1346 | 0 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
1347 | |
|
1348 | 0 | *secpp = isec; |
1349 | 0 | } |
1350 | |
|
1351 | 0 | if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd, |
1352 | 0 | input_section, data, internal_relocs, |
1353 | 0 | isymbuf, sections)) |
1354 | 0 | goto error_return; |
1355 | | |
1356 | 0 | free (sections); |
1357 | 0 | if (symtab_hdr->contents != (unsigned char *) isymbuf) |
1358 | 0 | free (isymbuf); |
1359 | 0 | if (elf_section_data (input_section)->relocs != internal_relocs) |
1360 | 0 | free (internal_relocs); |
1361 | 0 | } |
1362 | | |
1363 | 0 | return data; |
1364 | | |
1365 | 0 | error_return: |
1366 | 0 | free (sections); |
1367 | 0 | if (symtab_hdr->contents != (unsigned char *) isymbuf) |
1368 | 0 | free (isymbuf); |
1369 | 0 | if (elf_section_data (input_section)->relocs != internal_relocs) |
1370 | 0 | free (internal_relocs); |
1371 | 0 | if (orig_data == NULL) |
1372 | 0 | free (data); |
1373 | 0 | return NULL; |
1374 | 0 | } |
1375 | | |
1376 | | #define TARGET_LITTLE_SYM mn10200_elf32_vec |
1377 | | #define TARGET_LITTLE_NAME "elf32-mn10200" |
1378 | | #define ELF_ARCH bfd_arch_mn10200 |
1379 | | #define ELF_MACHINE_CODE EM_MN10200 |
1380 | | #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200 |
1381 | | #define ELF_MAXPAGESIZE 0x1000 |
1382 | | |
1383 | | #define elf_backend_rela_normal 1 |
1384 | | #define elf_info_to_howto mn10200_info_to_howto |
1385 | | #define elf_info_to_howto_rel NULL |
1386 | | #define elf_backend_relocate_section mn10200_elf_relocate_section |
1387 | | #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section |
1388 | | #define bfd_elf32_bfd_get_relocated_section_contents \ |
1389 | | mn10200_elf_get_relocated_section_contents |
1390 | | |
1391 | | #define elf_symbol_leading_char '_' |
1392 | | |
1393 | | #include "elf32-target.h" |