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