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 | |
9ad5cbcf | 33 | PARAMS ((bfd *, asection *, 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; | |
9ad5cbcf | 511 | Elf_Internal_Shdr *shndx_hdr; |
252b5132 RH |
512 | Elf_Internal_Rela *internal_relocs; |
513 | Elf_Internal_Rela *free_relocs = NULL; | |
514 | Elf_Internal_Rela *irel, *irelend; | |
515 | bfd_byte *contents = NULL; | |
516 | bfd_byte *free_contents = NULL; | |
517 | Elf32_External_Sym *extsyms = NULL; | |
518 | Elf32_External_Sym *free_extsyms = NULL; | |
9ad5cbcf | 519 | Elf_External_Sym_Shndx *shndx_buf = NULL; |
252b5132 RH |
520 | |
521 | /* Assume nothing changes. */ | |
522 | *again = false; | |
523 | ||
524 | /* We don't have to do anything for a relocateable link, if | |
525 | this section does not have relocs, or if this is not a | |
526 | code section. */ | |
527 | if (link_info->relocateable | |
528 | || (sec->flags & SEC_RELOC) == 0 | |
529 | || sec->reloc_count == 0 | |
530 | || (sec->flags & SEC_CODE) == 0) | |
531 | return true; | |
532 | ||
533 | /* If this is the first time we have been called for this section, | |
534 | initialize the cooked size. */ | |
535 | if (sec->_cooked_size == 0) | |
536 | sec->_cooked_size = sec->_raw_size; | |
537 | ||
538 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
9ad5cbcf | 539 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
252b5132 RH |
540 | |
541 | /* Get a copy of the native relocations. */ | |
542 | internal_relocs = (_bfd_elf32_link_read_relocs | |
543 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
544 | link_info->keep_memory)); | |
545 | if (internal_relocs == NULL) | |
546 | goto error_return; | |
547 | if (! link_info->keep_memory) | |
548 | free_relocs = internal_relocs; | |
549 | ||
550 | /* Walk through them looking for relaxing opportunities. */ | |
551 | irelend = internal_relocs + sec->reloc_count; | |
552 | for (irel = internal_relocs; irel < irelend; irel++) | |
553 | { | |
554 | bfd_vma symval; | |
555 | ||
556 | /* If this isn't something that can be relaxed, then ignore | |
557 | this reloc. */ | |
558 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE | |
559 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8 | |
560 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX) | |
561 | continue; | |
562 | ||
563 | /* Get the section contents if we haven't done so already. */ | |
564 | if (contents == NULL) | |
565 | { | |
566 | /* Get cached copy if it exists. */ | |
567 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
568 | contents = elf_section_data (sec)->this_hdr.contents; | |
569 | else | |
570 | { | |
571 | /* Go get them off disk. */ | |
572 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
573 | if (contents == NULL) | |
574 | goto error_return; | |
575 | free_contents = contents; | |
576 | ||
577 | if (! bfd_get_section_contents (abfd, sec, contents, | |
578 | (file_ptr) 0, sec->_raw_size)) | |
579 | goto error_return; | |
580 | } | |
581 | } | |
582 | ||
583 | /* Read this BFD's symbols if we haven't done so already. */ | |
584 | if (extsyms == NULL) | |
585 | { | |
586 | /* Get cached copy if it exists. */ | |
587 | if (symtab_hdr->contents != NULL) | |
588 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
589 | else | |
590 | { | |
591 | /* Go get them off disk. */ | |
9ad5cbcf AM |
592 | bfd_size_type amt; |
593 | ||
594 | amt = symtab_hdr->sh_info; | |
595 | amt *= sizeof (Elf32_External_Sym); | |
dc810e39 | 596 | extsyms = (Elf32_External_Sym *) bfd_malloc (amt); |
252b5132 RH |
597 | if (extsyms == NULL) |
598 | goto error_return; | |
599 | free_extsyms = extsyms; | |
600 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
9ad5cbcf | 601 | || bfd_bread ((PTR) extsyms, amt, abfd) != amt) |
252b5132 | 602 | goto error_return; |
9ad5cbcf AM |
603 | symtab_hdr->contents = (bfd_byte *) extsyms; |
604 | } | |
605 | ||
606 | if (shndx_hdr->sh_size != 0) | |
607 | { | |
608 | bfd_size_type amt; | |
609 | ||
610 | amt = symtab_hdr->sh_info; | |
611 | amt *= sizeof (Elf_External_Sym_Shndx); | |
612 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
613 | if (shndx_buf == NULL) | |
614 | goto error_return; | |
615 | if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 | |
616 | || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt) | |
617 | goto error_return; | |
618 | shndx_hdr->contents = (bfd_byte *) shndx_buf; | |
252b5132 RH |
619 | } |
620 | } | |
621 | ||
622 | /* Get the value of the symbol referred to by the reloc. */ | |
623 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
624 | { | |
9ad5cbcf AM |
625 | /* A local symbol. */ |
626 | Elf32_External_Sym *esym; | |
627 | Elf_External_Sym_Shndx *shndx; | |
252b5132 RH |
628 | Elf_Internal_Sym isym; |
629 | asection *sym_sec; | |
630 | ||
9ad5cbcf AM |
631 | esym = extsyms + ELF32_R_SYM (irel->r_info); |
632 | shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0); | |
f8ecb12b AM |
633 | bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx, |
634 | &isym); | |
252b5132 | 635 | |
9ad5cbcf AM |
636 | if (isym.st_shndx == SHN_UNDEF) |
637 | sym_sec = bfd_und_section_ptr; | |
638 | else if (isym.st_shndx == SHN_ABS) | |
639 | sym_sec = bfd_abs_section_ptr; | |
640 | else if (isym.st_shndx == SHN_COMMON) | |
641 | sym_sec = bfd_com_section_ptr; | |
642 | else | |
643 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); | |
252b5132 RH |
644 | symval = (isym.st_value |
645 | + sym_sec->output_section->vma | |
646 | + sym_sec->output_offset); | |
647 | } | |
648 | else | |
649 | { | |
650 | unsigned long indx; | |
651 | struct elf_link_hash_entry *h; | |
652 | ||
653 | /* An external symbol. */ | |
654 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
655 | h = elf_sym_hashes (abfd)[indx]; | |
656 | BFD_ASSERT (h != NULL); | |
657 | if (h->root.type != bfd_link_hash_defined | |
658 | && h->root.type != bfd_link_hash_defweak) | |
659 | { | |
660 | /* This appears to be a reference to an undefined | |
661 | symbol. Just ignore it--it will be caught by the | |
662 | regular reloc processing. */ | |
663 | continue; | |
664 | } | |
665 | ||
666 | symval = (h->root.u.def.value | |
667 | + h->root.u.def.section->output_section->vma | |
668 | + h->root.u.def.section->output_offset); | |
669 | } | |
670 | ||
671 | /* For simplicity of coding, we are going to modify the section | |
672 | contents, the section relocs, and the BFD symbol table. We | |
673 | must tell the rest of the code not to free up this | |
674 | information. It would be possible to instead create a table | |
675 | of changes which have to be made, as is done in coff-mips.c; | |
676 | that would be more work, but would require less memory when | |
677 | the linker is run. */ | |
678 | ||
252b5132 RH |
679 | /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative |
680 | branch/call. */ | |
681 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24) | |
682 | { | |
683 | bfd_vma value = symval; | |
684 | ||
685 | /* Deal with pc-relative gunk. */ | |
686 | value -= (sec->output_section->vma + sec->output_offset); | |
687 | value -= (irel->r_offset + 3); | |
688 | value += irel->r_addend; | |
689 | ||
690 | /* See if the value will fit in 16 bits, note the high value is | |
691 | 0x7fff + 2 as the target will be two bytes closer if we are | |
692 | able to relax. */ | |
27def10f | 693 | if ((long) value < 0x8001 && (long) value > -0x8000) |
252b5132 RH |
694 | { |
695 | unsigned char code; | |
696 | ||
697 | /* Get the opcode. */ | |
698 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
699 | ||
700 | if (code != 0xe0 && code != 0xe1) | |
701 | continue; | |
702 | ||
703 | /* Note that we've changed the relocs, section contents, etc. */ | |
704 | elf_section_data (sec)->relocs = internal_relocs; | |
705 | free_relocs = NULL; | |
706 | ||
707 | elf_section_data (sec)->this_hdr.contents = contents; | |
708 | free_contents = NULL; | |
709 | ||
252b5132 RH |
710 | free_extsyms = NULL; |
711 | ||
712 | /* Fix the opcode. */ | |
713 | if (code == 0xe0) | |
714 | bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2); | |
715 | else if (code == 0xe1) | |
716 | bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2); | |
717 | ||
718 | /* Fix the relocation's type. */ | |
719 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
720 | R_MN10200_PCREL16); | |
721 | ||
722 | /* The opcode got shorter too, so we have to fix the offset. */ | |
723 | irel->r_offset -= 1; | |
724 | ||
725 | /* Delete two bytes of data. */ | |
726 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
727 | irel->r_offset + 1, 2)) | |
728 | goto error_return; | |
729 | ||
730 | /* That will change things, so, we should relax again. | |
731 | Note that this is not required, and it may be slow. */ | |
732 | *again = true; | |
733 | } | |
734 | } | |
735 | ||
736 | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative | |
737 | branch. */ | |
738 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16) | |
739 | { | |
740 | bfd_vma value = symval; | |
741 | ||
742 | /* Deal with pc-relative gunk. */ | |
743 | value -= (sec->output_section->vma + sec->output_offset); | |
744 | value -= (irel->r_offset + 2); | |
745 | value += irel->r_addend; | |
746 | ||
747 | /* See if the value will fit in 8 bits, note the high value is | |
748 | 0x7f + 1 as the target will be one bytes closer if we are | |
749 | able to relax. */ | |
27def10f | 750 | if ((long) value < 0x80 && (long) value > -0x80) |
252b5132 RH |
751 | { |
752 | unsigned char code; | |
753 | ||
754 | /* Get the opcode. */ | |
755 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
756 | ||
757 | if (code != 0xfc) | |
758 | continue; | |
759 | ||
760 | /* Note that we've changed the relocs, section contents, etc. */ | |
761 | elf_section_data (sec)->relocs = internal_relocs; | |
762 | free_relocs = NULL; | |
763 | ||
764 | elf_section_data (sec)->this_hdr.contents = contents; | |
765 | free_contents = NULL; | |
766 | ||
252b5132 RH |
767 | free_extsyms = NULL; |
768 | ||
769 | /* Fix the opcode. */ | |
770 | bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1); | |
771 | ||
772 | /* Fix the relocation's type. */ | |
773 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
774 | R_MN10200_PCREL8); | |
775 | ||
776 | /* Delete one byte of data. */ | |
777 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
778 | irel->r_offset + 1, 1)) | |
779 | goto error_return; | |
780 | ||
781 | /* That will change things, so, we should relax again. | |
782 | Note that this is not required, and it may be slow. */ | |
783 | *again = true; | |
784 | } | |
785 | } | |
786 | ||
787 | /* Try to eliminate an unconditional 8 bit pc-relative branch | |
788 | which immediately follows a conditional 8 bit pc-relative | |
789 | branch around the unconditional branch. | |
790 | ||
791 | original: new: | |
792 | bCC lab1 bCC' lab2 | |
793 | bra lab2 | |
794 | lab1: lab1: | |
795 | ||
252b5132 RH |
796 | This happens when the bCC can't reach lab2 at assembly time, |
797 | but due to other relaxations it can reach at link time. */ | |
798 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8) | |
799 | { | |
800 | Elf_Internal_Rela *nrel; | |
801 | bfd_vma value = symval; | |
802 | unsigned char code; | |
803 | ||
804 | /* Deal with pc-relative gunk. */ | |
805 | value -= (sec->output_section->vma + sec->output_offset); | |
806 | value -= (irel->r_offset + 1); | |
807 | value += irel->r_addend; | |
808 | ||
809 | /* Do nothing if this reloc is the last byte in the section. */ | |
810 | if (irel->r_offset == sec->_cooked_size) | |
811 | continue; | |
812 | ||
813 | /* See if the next instruction is an unconditional pc-relative | |
814 | branch, more often than not this test will fail, so we | |
815 | test it first to speed things up. */ | |
816 | code = bfd_get_8 (abfd, contents + irel->r_offset + 1); | |
817 | if (code != 0xea) | |
818 | continue; | |
819 | ||
820 | /* Also make sure the next relocation applies to the next | |
821 | instruction and that it's a pc-relative 8 bit branch. */ | |
822 | nrel = irel + 1; | |
823 | if (nrel == irelend | |
824 | || irel->r_offset + 2 != nrel->r_offset | |
825 | || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8) | |
826 | continue; | |
827 | ||
828 | /* Make sure our destination immediately follows the | |
829 | unconditional branch. */ | |
830 | if (symval != (sec->output_section->vma + sec->output_offset | |
831 | + irel->r_offset + 3)) | |
832 | continue; | |
833 | ||
834 | /* Now make sure we are a conditional branch. This may not | |
a7c10850 | 835 | be necessary, but why take the chance. |
252b5132 RH |
836 | |
837 | Note these checks assume that R_MN10200_PCREL8 relocs | |
838 | only occur on bCC and bCCx insns. If they occured | |
839 | elsewhere, we'd need to know the start of this insn | |
840 | for this check to be accurate. */ | |
841 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
842 | if (code != 0xe0 && code != 0xe1 && code != 0xe2 | |
843 | && code != 0xe3 && code != 0xe4 && code != 0xe5 | |
844 | && code != 0xe6 && code != 0xe7 && code != 0xe8 | |
845 | && code != 0xe9 && code != 0xec && code != 0xed | |
846 | && code != 0xee && code != 0xef && code != 0xfc | |
847 | && code != 0xfd && code != 0xfe && code != 0xff) | |
848 | continue; | |
849 | ||
850 | /* We also have to be sure there is no symbol/label | |
851 | at the unconditional branch. */ | |
9ad5cbcf | 852 | if (mn10200_elf_symbol_address_p (abfd, sec, irel->r_offset + 1)) |
252b5132 RH |
853 | continue; |
854 | ||
855 | /* Note that we've changed the relocs, section contents, etc. */ | |
856 | elf_section_data (sec)->relocs = internal_relocs; | |
857 | free_relocs = NULL; | |
858 | ||
859 | elf_section_data (sec)->this_hdr.contents = contents; | |
860 | free_contents = NULL; | |
861 | ||
252b5132 RH |
862 | free_extsyms = NULL; |
863 | ||
864 | /* Reverse the condition of the first branch. */ | |
865 | switch (code) | |
866 | { | |
27def10f KH |
867 | case 0xfc: |
868 | code = 0xfd; | |
869 | break; | |
870 | case 0xfd: | |
871 | code = 0xfc; | |
872 | break; | |
873 | case 0xfe: | |
874 | code = 0xff; | |
875 | break; | |
876 | case 0xff: | |
877 | code = 0xfe; | |
878 | break; | |
879 | case 0xe8: | |
880 | code = 0xe9; | |
881 | break; | |
882 | case 0xe9: | |
883 | code = 0xe8; | |
884 | break; | |
885 | case 0xe0: | |
886 | code = 0xe2; | |
887 | break; | |
888 | case 0xe2: | |
889 | code = 0xe0; | |
890 | break; | |
891 | case 0xe3: | |
892 | code = 0xe1; | |
893 | break; | |
894 | case 0xe1: | |
895 | code = 0xe3; | |
896 | break; | |
897 | case 0xe4: | |
898 | code = 0xe6; | |
899 | break; | |
900 | case 0xe6: | |
901 | code = 0xe4; | |
902 | break; | |
903 | case 0xe7: | |
904 | code = 0xe5; | |
905 | break; | |
906 | case 0xe5: | |
907 | code = 0xe7; | |
908 | break; | |
909 | case 0xec: | |
910 | code = 0xed; | |
911 | break; | |
912 | case 0xed: | |
913 | code = 0xec; | |
914 | break; | |
915 | case 0xee: | |
916 | code = 0xef; | |
917 | break; | |
918 | case 0xef: | |
919 | code = 0xee; | |
920 | break; | |
252b5132 RH |
921 | } |
922 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
a7c10850 | 923 | |
252b5132 RH |
924 | /* Set the reloc type and symbol for the first branch |
925 | from the second branch. */ | |
926 | irel->r_info = nrel->r_info; | |
927 | ||
928 | /* Make the reloc for the second branch a null reloc. */ | |
929 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), | |
930 | R_MN10200_NONE); | |
931 | ||
932 | /* Delete two bytes of data. */ | |
933 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
934 | irel->r_offset + 1, 2)) | |
935 | goto error_return; | |
936 | ||
937 | /* That will change things, so, we should relax again. | |
938 | Note that this is not required, and it may be slow. */ | |
939 | *again = true; | |
940 | } | |
941 | ||
942 | /* Try to turn a 24bit immediate, displacement or absolute address | |
943 | into a 16bit immediate, displacement or absolute address. */ | |
944 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24) | |
945 | { | |
946 | bfd_vma value = symval; | |
947 | ||
a7c10850 | 948 | /* See if the value will fit in 16 bits. |
252b5132 RH |
949 | We allow any 16bit match here. We prune those we can't |
950 | handle below. */ | |
27def10f | 951 | if ((long) value < 0x7fff && (long) value > -0x8000) |
252b5132 RH |
952 | { |
953 | unsigned char code; | |
954 | ||
955 | /* All insns which have 24bit operands are 5 bytes long, | |
956 | the first byte will always be 0xf4, but we double check | |
957 | it just in case. */ | |
958 | ||
959 | /* Get the first opcode. */ | |
960 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
961 | ||
962 | if (code != 0xf4) | |
963 | continue; | |
964 | ||
965 | /* Get the second opcode. */ | |
966 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
967 | ||
968 | switch (code & 0xfc) | |
969 | { | |
970 | /* mov imm24,dn -> mov imm16,dn */ | |
971 | case 0x70: | |
972 | /* Not safe if the high bit is on as relaxing may | |
973 | move the value out of high mem and thus not fit | |
974 | in a signed 16bit value. */ | |
975 | if (value & 0x8000) | |
976 | continue; | |
977 | ||
978 | /* Note that we've changed the reldection contents, etc. */ | |
979 | elf_section_data (sec)->relocs = internal_relocs; | |
980 | free_relocs = NULL; | |
981 | ||
982 | elf_section_data (sec)->this_hdr.contents = contents; | |
983 | free_contents = NULL; | |
984 | ||
252b5132 RH |
985 | free_extsyms = NULL; |
986 | ||
987 | /* Fix the opcode. */ | |
988 | bfd_put_8 (abfd, 0xf8 + (code & 0x03), | |
989 | contents + irel->r_offset - 2); | |
990 | ||
991 | /* Fix the relocation's type. */ | |
992 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
993 | R_MN10200_16); | |
994 | ||
995 | /* The opcode got shorter too, so we have to fix the | |
996 | offset. */ | |
997 | irel->r_offset -= 1; | |
998 | ||
999 | /* Delete two bytes of data. */ | |
1000 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
1001 | irel->r_offset + 1, 2)) | |
1002 | goto error_return; | |
1003 | ||
1004 | /* That will change things, so, we should relax again. | |
1005 | Note that this is not required, and it may be slow. */ | |
1006 | *again = true; | |
1007 | break; | |
1008 | ||
a7c10850 | 1009 | /* mov imm24,an -> mov imm16,an |
252b5132 RH |
1010 | cmp imm24,an -> cmp imm16,an |
1011 | mov (abs24),dn -> mov (abs16),dn | |
1012 | mov dn,(abs24) -> mov dn,(abs16) | |
1013 | movb dn,(abs24) -> movb dn,(abs16) | |
1014 | movbu (abs24),dn -> movbu (abs16),dn */ | |
1015 | case 0x74: | |
1016 | case 0x7c: | |
1017 | case 0xc0: | |
1018 | case 0x40: | |
1019 | case 0x44: | |
1020 | case 0xc8: | |
1021 | /* Note that we've changed the reldection contents, etc. */ | |
1022 | elf_section_data (sec)->relocs = internal_relocs; | |
1023 | free_relocs = NULL; | |
1024 | ||
1025 | elf_section_data (sec)->this_hdr.contents = contents; | |
1026 | free_contents = NULL; | |
1027 | ||
252b5132 RH |
1028 | free_extsyms = NULL; |
1029 | ||
1030 | if ((code & 0xfc) == 0x74) | |
1031 | code = 0xdc + (code & 0x03); | |
1032 | else if ((code & 0xfc) == 0x7c) | |
1033 | code = 0xec + (code & 0x03); | |
1034 | else if ((code & 0xfc) == 0xc0) | |
1035 | code = 0xc8 + (code & 0x03); | |
1036 | else if ((code & 0xfc) == 0x40) | |
1037 | code = 0xc0 + (code & 0x03); | |
1038 | else if ((code & 0xfc) == 0x44) | |
1039 | code = 0xc4 + (code & 0x03); | |
1040 | else if ((code & 0xfc) == 0xc8) | |
1041 | code = 0xcc + (code & 0x03); | |
1042 | ||
1043 | /* Fix the opcode. */ | |
1044 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
1045 | ||
1046 | /* Fix the relocation's type. */ | |
1047 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1048 | R_MN10200_16); | |
1049 | ||
1050 | /* The opcode got shorter too, so we have to fix the | |
1051 | offset. */ | |
1052 | irel->r_offset -= 1; | |
1053 | ||
1054 | /* Delete two bytes of data. */ | |
1055 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
1056 | irel->r_offset + 1, 2)) | |
1057 | goto error_return; | |
1058 | ||
1059 | /* That will change things, so, we should relax again. | |
1060 | Note that this is not required, and it may be slow. */ | |
1061 | *again = true; | |
1062 | break; | |
1063 | ||
1064 | /* cmp imm24,dn -> cmp imm16,dn | |
1065 | mov (abs24),an -> mov (abs16),an | |
1066 | mov an,(abs24) -> mov an,(abs16) | |
1067 | add imm24,dn -> add imm16,dn | |
1068 | add imm24,an -> add imm16,an | |
1069 | sub imm24,dn -> sub imm16,dn | |
a7c10850 | 1070 | sub imm24,an -> sub imm16,an |
252b5132 RH |
1071 | And all d24->d16 in memory ops. */ |
1072 | case 0x78: | |
1073 | case 0xd0: | |
1074 | case 0x50: | |
1075 | case 0x60: | |
1076 | case 0x64: | |
1077 | case 0x68: | |
1078 | case 0x6c: | |
1079 | case 0x80: | |
1080 | case 0xf0: | |
1081 | case 0x00: | |
1082 | case 0x10: | |
1083 | case 0xb0: | |
1084 | case 0x30: | |
1085 | case 0xa0: | |
1086 | case 0x20: | |
1087 | case 0x90: | |
1088 | /* Not safe if the high bit is on as relaxing may | |
1089 | move the value out of high mem and thus not fit | |
1090 | in a signed 16bit value. */ | |
1091 | if (((code & 0xfc) == 0x78 | |
27def10f KH |
1092 | || (code & 0xfc) == 0x60 |
1093 | || (code & 0xfc) == 0x64 | |
1094 | || (code & 0xfc) == 0x68 | |
1095 | || (code & 0xfc) == 0x6c | |
1096 | || (code & 0xfc) == 0x80 | |
1097 | || (code & 0xfc) == 0xf0 | |
1098 | || (code & 0xfc) == 0x00 | |
1099 | || (code & 0xfc) == 0x10 | |
1100 | || (code & 0xfc) == 0xb0 | |
1101 | || (code & 0xfc) == 0x30 | |
1102 | || (code & 0xfc) == 0xa0 | |
1103 | || (code & 0xfc) == 0x20 | |
1104 | || (code & 0xfc) == 0x90) | |
1105 | && (value & 0x8000) != 0) | |
252b5132 RH |
1106 | continue; |
1107 | ||
1108 | /* Note that we've changed the reldection contents, etc. */ | |
1109 | elf_section_data (sec)->relocs = internal_relocs; | |
1110 | free_relocs = NULL; | |
1111 | ||
1112 | elf_section_data (sec)->this_hdr.contents = contents; | |
1113 | free_contents = NULL; | |
1114 | ||
252b5132 RH |
1115 | free_extsyms = NULL; |
1116 | ||
1117 | /* Fix the opcode. */ | |
1118 | bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2); | |
1119 | ||
1120 | if ((code & 0xfc) == 0x78) | |
1121 | code = 0x48 + (code & 0x03); | |
1122 | else if ((code & 0xfc) == 0xd0) | |
1123 | code = 0x30 + (code & 0x03); | |
1124 | else if ((code & 0xfc) == 0x50) | |
1125 | code = 0x20 + (code & 0x03); | |
1126 | else if ((code & 0xfc) == 0x60) | |
1127 | code = 0x18 + (code & 0x03); | |
1128 | else if ((code & 0xfc) == 0x64) | |
1129 | code = 0x08 + (code & 0x03); | |
1130 | else if ((code & 0xfc) == 0x68) | |
1131 | code = 0x1c + (code & 0x03); | |
1132 | else if ((code & 0xfc) == 0x6c) | |
1133 | code = 0x0c + (code & 0x03); | |
1134 | else if ((code & 0xfc) == 0x80) | |
1135 | code = 0xc0 + (code & 0x07); | |
1136 | else if ((code & 0xfc) == 0xf0) | |
1137 | code = 0xb0 + (code & 0x07); | |
1138 | else if ((code & 0xfc) == 0x00) | |
1139 | code = 0x80 + (code & 0x07); | |
1140 | else if ((code & 0xfc) == 0x10) | |
1141 | code = 0xa0 + (code & 0x07); | |
1142 | else if ((code & 0xfc) == 0xb0) | |
1143 | code = 0x70 + (code & 0x07); | |
1144 | else if ((code & 0xfc) == 0x30) | |
1145 | code = 0x60 + (code & 0x07); | |
1146 | else if ((code & 0xfc) == 0xa0) | |
1147 | code = 0xd0 + (code & 0x07); | |
1148 | else if ((code & 0xfc) == 0x20) | |
1149 | code = 0x90 + (code & 0x07); | |
1150 | else if ((code & 0xfc) == 0x90) | |
1151 | code = 0x50 + (code & 0x07); | |
1152 | ||
1153 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
1154 | ||
1155 | /* Fix the relocation's type. */ | |
1156 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1157 | R_MN10200_16); | |
1158 | ||
1159 | /* Delete one bytes of data. */ | |
1160 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
1161 | irel->r_offset + 2, 1)) | |
1162 | goto error_return; | |
1163 | ||
1164 | /* That will change things, so, we should relax again. | |
1165 | Note that this is not required, and it may be slow. */ | |
1166 | *again = true; | |
1167 | break; | |
1168 | ||
1169 | /* movb (abs24),dn ->movbu (abs16),dn extxb bn */ | |
1170 | case 0xc4: | |
1171 | /* Note that we've changed the reldection contents, etc. */ | |
1172 | elf_section_data (sec)->relocs = internal_relocs; | |
1173 | free_relocs = NULL; | |
1174 | ||
1175 | elf_section_data (sec)->this_hdr.contents = contents; | |
1176 | free_contents = NULL; | |
1177 | ||
252b5132 RH |
1178 | free_extsyms = NULL; |
1179 | ||
1180 | bfd_put_8 (abfd, 0xcc + (code & 0x03), | |
1181 | contents + irel->r_offset - 2); | |
1182 | ||
1183 | bfd_put_8 (abfd, 0xb8 + (code & 0x03), | |
1184 | contents + irel->r_offset - 1); | |
1185 | ||
1186 | /* Fix the relocation's type. */ | |
1187 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1188 | R_MN10200_16); | |
1189 | ||
1190 | /* The reloc will be applied one byte in front of its | |
1191 | current location. */ | |
1192 | irel->r_offset -= 1; | |
1193 | ||
1194 | /* Delete one bytes of data. */ | |
1195 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, | |
1196 | irel->r_offset + 2, 1)) | |
1197 | goto error_return; | |
1198 | ||
1199 | /* That will change things, so, we should relax again. | |
1200 | Note that this is not required, and it may be slow. */ | |
1201 | *again = true; | |
1202 | break; | |
1203 | } | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | if (free_relocs != NULL) | |
9ad5cbcf | 1209 | free (free_relocs); |
252b5132 RH |
1210 | |
1211 | if (free_contents != NULL) | |
1212 | { | |
1213 | if (! link_info->keep_memory) | |
1214 | free (free_contents); | |
1215 | else | |
1216 | { | |
1217 | /* Cache the section contents for elf_link_input_bfd. */ | |
1218 | elf_section_data (sec)->this_hdr.contents = contents; | |
1219 | } | |
9ad5cbcf AM |
1220 | } |
1221 | ||
1222 | if (shndx_buf != NULL) | |
1223 | { | |
1224 | shndx_hdr->contents = NULL; | |
1225 | free (shndx_buf); | |
252b5132 RH |
1226 | } |
1227 | ||
1228 | if (free_extsyms != NULL) | |
1229 | { | |
1230 | if (! link_info->keep_memory) | |
252b5132 | 1231 | { |
9ad5cbcf AM |
1232 | symtab_hdr->contents = NULL; |
1233 | free (free_extsyms); | |
252b5132 | 1234 | } |
252b5132 RH |
1235 | } |
1236 | ||
1237 | return true; | |
1238 | ||
1239 | error_return: | |
1240 | if (free_relocs != NULL) | |
1241 | free (free_relocs); | |
1242 | if (free_contents != NULL) | |
1243 | free (free_contents); | |
9ad5cbcf AM |
1244 | if (shndx_buf != NULL) |
1245 | { | |
1246 | shndx_hdr->contents = NULL; | |
1247 | free (shndx_buf); | |
1248 | } | |
252b5132 | 1249 | if (free_extsyms != NULL) |
9ad5cbcf AM |
1250 | { |
1251 | symtab_hdr->contents = NULL; | |
1252 | free (free_extsyms); | |
1253 | } | |
1254 | ||
252b5132 RH |
1255 | return false; |
1256 | } | |
1257 | ||
1258 | /* Delete some bytes from a section while relaxing. */ | |
1259 | ||
1260 | static boolean | |
1261 | mn10200_elf_relax_delete_bytes (abfd, sec, addr, count) | |
1262 | bfd *abfd; | |
1263 | asection *sec; | |
1264 | bfd_vma addr; | |
1265 | int count; | |
1266 | { | |
1267 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 1268 | Elf_Internal_Shdr *shndx_hdr; |
252b5132 | 1269 | Elf32_External_Sym *extsyms; |
9ad5cbcf | 1270 | unsigned int sec_shndx; |
252b5132 RH |
1271 | bfd_byte *contents; |
1272 | Elf_Internal_Rela *irel, *irelend; | |
1273 | Elf_Internal_Rela *irelalign; | |
1274 | bfd_vma toaddr; | |
1275 | Elf32_External_Sym *esym, *esymend; | |
9ad5cbcf AM |
1276 | Elf_External_Sym_Shndx *shndx; |
1277 | struct elf_link_hash_entry **sym_hashes; | |
1278 | struct elf_link_hash_entry **end_hashes; | |
1279 | unsigned int symcount; | |
252b5132 RH |
1280 | |
1281 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1282 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
1283 | ||
9ad5cbcf | 1284 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
252b5132 RH |
1285 | |
1286 | contents = elf_section_data (sec)->this_hdr.contents; | |
1287 | ||
1288 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
1289 | power larger than the number of bytes we are deleting. */ | |
1290 | ||
1291 | irelalign = NULL; | |
1292 | toaddr = sec->_cooked_size; | |
1293 | ||
1294 | irel = elf_section_data (sec)->relocs; | |
1295 | irelend = irel + sec->reloc_count; | |
1296 | ||
1297 | /* Actually delete the bytes. */ | |
dc810e39 AM |
1298 | memmove (contents + addr, contents + addr + count, |
1299 | (size_t) (toaddr - addr - count)); | |
252b5132 RH |
1300 | sec->_cooked_size -= count; |
1301 | ||
1302 | /* Adjust all the relocs. */ | |
1303 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) | |
1304 | { | |
1305 | /* Get the new reloc address. */ | |
1306 | if ((irel->r_offset > addr | |
1307 | && irel->r_offset < toaddr)) | |
1308 | irel->r_offset -= count; | |
1309 | } | |
1310 | ||
1311 | /* Adjust the local symbols defined in this section. */ | |
9ad5cbcf AM |
1312 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
1313 | shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
252b5132 RH |
1314 | esym = extsyms; |
1315 | esymend = esym + symtab_hdr->sh_info; | |
9ad5cbcf | 1316 | for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL)) |
252b5132 RH |
1317 | { |
1318 | Elf_Internal_Sym isym; | |
9ad5cbcf | 1319 | Elf_External_Sym_Shndx dummy; |
252b5132 | 1320 | |
f8ecb12b | 1321 | bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx, |
73ff0d56 | 1322 | &isym); |
252b5132 | 1323 | |
9ad5cbcf | 1324 | if (isym.st_shndx == sec_shndx |
252b5132 RH |
1325 | && isym.st_value > addr |
1326 | && isym.st_value < toaddr) | |
1327 | { | |
1328 | isym.st_value -= count; | |
9ad5cbcf | 1329 | bfd_elf32_swap_symbol_out (abfd, &isym, (PTR) esym, (PTR) &dummy); |
252b5132 RH |
1330 | } |
1331 | } | |
1332 | ||
1333 | /* Now adjust the global symbols defined in this section. */ | |
9ad5cbcf AM |
1334 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
1335 | - symtab_hdr->sh_info); | |
1336 | sym_hashes = elf_sym_hashes (abfd); | |
1337 | end_hashes = sym_hashes + symcount; | |
1338 | for (; sym_hashes < end_hashes; sym_hashes++) | |
252b5132 | 1339 | { |
9ad5cbcf AM |
1340 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
1341 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1342 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1343 | && sym_hash->root.u.def.section == sec | |
1344 | && sym_hash->root.u.def.value > addr | |
1345 | && sym_hash->root.u.def.value < toaddr) | |
252b5132 | 1346 | { |
9ad5cbcf | 1347 | sym_hash->root.u.def.value -= count; |
252b5132 RH |
1348 | } |
1349 | } | |
1350 | ||
1351 | return true; | |
1352 | } | |
1353 | ||
1354 | /* Return true if a symbol exists at the given address, else return | |
1355 | false. */ | |
1356 | static boolean | |
9ad5cbcf | 1357 | mn10200_elf_symbol_address_p (abfd, sec, addr) |
252b5132 RH |
1358 | bfd *abfd; |
1359 | asection *sec; | |
252b5132 RH |
1360 | bfd_vma addr; |
1361 | { | |
1362 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf AM |
1363 | Elf_Internal_Shdr *shndx_hdr; |
1364 | unsigned int sec_shndx; | |
252b5132 | 1365 | Elf32_External_Sym *esym, *esymend; |
9ad5cbcf AM |
1366 | Elf_External_Sym_Shndx *shndx; |
1367 | struct elf_link_hash_entry **sym_hashes; | |
1368 | struct elf_link_hash_entry **end_hashes; | |
1369 | unsigned int symcount; | |
252b5132 | 1370 | |
9ad5cbcf | 1371 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
252b5132 RH |
1372 | |
1373 | /* Examine all the symbols. */ | |
9ad5cbcf AM |
1374 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1375 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1376 | shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
1377 | esym = (Elf32_External_Sym *) symtab_hdr->contents; | |
252b5132 | 1378 | esymend = esym + symtab_hdr->sh_info; |
9ad5cbcf | 1379 | for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL)) |
252b5132 RH |
1380 | { |
1381 | Elf_Internal_Sym isym; | |
1382 | ||
f8ecb12b | 1383 | bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx, |
73ff0d56 | 1384 | &isym); |
252b5132 | 1385 | |
9ad5cbcf | 1386 | if (isym.st_shndx == sec_shndx |
252b5132 RH |
1387 | && isym.st_value == addr) |
1388 | return true; | |
1389 | } | |
1390 | ||
9ad5cbcf AM |
1391 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
1392 | - symtab_hdr->sh_info); | |
1393 | sym_hashes = elf_sym_hashes (abfd); | |
1394 | end_hashes = sym_hashes + symcount; | |
1395 | for (; sym_hashes < end_hashes; sym_hashes++) | |
252b5132 | 1396 | { |
9ad5cbcf AM |
1397 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
1398 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1399 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1400 | && sym_hash->root.u.def.section == sec | |
1401 | && sym_hash->root.u.def.value == addr) | |
252b5132 RH |
1402 | return true; |
1403 | } | |
9ad5cbcf | 1404 | |
252b5132 RH |
1405 | return false; |
1406 | } | |
1407 | ||
1408 | /* This is a version of bfd_generic_get_relocated_section_contents | |
1409 | which uses mn10200_elf_relocate_section. */ | |
1410 | ||
1411 | static bfd_byte * | |
1412 | mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order, | |
1413 | data, relocateable, symbols) | |
1414 | bfd *output_bfd; | |
1415 | struct bfd_link_info *link_info; | |
1416 | struct bfd_link_order *link_order; | |
1417 | bfd_byte *data; | |
1418 | boolean relocateable; | |
1419 | asymbol **symbols; | |
1420 | { | |
1421 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 1422 | Elf_Internal_Shdr *shndx_hdr; |
252b5132 RH |
1423 | asection *input_section = link_order->u.indirect.section; |
1424 | bfd *input_bfd = input_section->owner; | |
1425 | asection **sections = NULL; | |
1426 | Elf_Internal_Rela *internal_relocs = NULL; | |
1427 | Elf32_External_Sym *external_syms = NULL; | |
9ad5cbcf AM |
1428 | Elf_External_Sym_Shndx *shndx_buf = NULL; |
1429 | Elf_External_Sym_Shndx *shndx; | |
252b5132 RH |
1430 | Elf_Internal_Sym *internal_syms = NULL; |
1431 | ||
1432 | /* We only need to handle the case of relaxing, or of having a | |
1433 | particular set of section contents, specially. */ | |
1434 | if (relocateable | |
1435 | || elf_section_data (input_section)->this_hdr.contents == NULL) | |
1436 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, | |
1437 | link_order, data, | |
1438 | relocateable, | |
1439 | symbols); | |
1440 | ||
1441 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
9ad5cbcf | 1442 | shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr; |
252b5132 RH |
1443 | |
1444 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, | |
dc810e39 | 1445 | (size_t) input_section->_raw_size); |
252b5132 RH |
1446 | |
1447 | if ((input_section->flags & SEC_RELOC) != 0 | |
1448 | && input_section->reloc_count > 0) | |
1449 | { | |
1450 | Elf_Internal_Sym *isymp; | |
1451 | asection **secpp; | |
1452 | Elf32_External_Sym *esym, *esymend; | |
9ad5cbcf | 1453 | bfd_size_type amt; |
252b5132 RH |
1454 | |
1455 | if (symtab_hdr->contents != NULL) | |
1456 | external_syms = (Elf32_External_Sym *) symtab_hdr->contents; | |
9ad5cbcf | 1457 | else if (symtab_hdr->sh_info != 0) |
252b5132 | 1458 | { |
9ad5cbcf AM |
1459 | amt = symtab_hdr->sh_info; |
1460 | amt *= sizeof (Elf32_External_Sym); | |
1461 | external_syms = (Elf32_External_Sym *) bfd_malloc (amt); | |
1462 | if (external_syms == NULL) | |
252b5132 RH |
1463 | goto error_return; |
1464 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
9ad5cbcf AM |
1465 | || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt) |
1466 | goto error_return; | |
1467 | } | |
1468 | ||
1469 | if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0) | |
1470 | { | |
1471 | amt = symtab_hdr->sh_info; | |
1472 | amt *= sizeof (Elf_External_Sym_Shndx); | |
1473 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
1474 | if (shndx_buf == NULL) | |
1475 | goto error_return; | |
1476 | if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0 | |
1477 | || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt) | |
252b5132 RH |
1478 | goto error_return; |
1479 | } | |
1480 | ||
1481 | internal_relocs = (_bfd_elf32_link_read_relocs | |
1482 | (input_bfd, input_section, (PTR) NULL, | |
1483 | (Elf_Internal_Rela *) NULL, false)); | |
1484 | if (internal_relocs == NULL) | |
1485 | goto error_return; | |
1486 | ||
9ad5cbcf AM |
1487 | amt = symtab_hdr->sh_info; |
1488 | amt *= sizeof (Elf_Internal_Sym); | |
1489 | internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); | |
1490 | if (internal_syms == NULL && amt != 0) | |
252b5132 RH |
1491 | goto error_return; |
1492 | ||
9ad5cbcf AM |
1493 | amt = symtab_hdr->sh_info; |
1494 | amt *= sizeof (asection *); | |
1495 | sections = (asection **) bfd_malloc (amt); | |
1496 | if (sections == NULL && amt != 0) | |
252b5132 RH |
1497 | goto error_return; |
1498 | ||
9ad5cbcf AM |
1499 | for (isymp = internal_syms, secpp = sections, shndx = shndx_buf, |
1500 | esym = external_syms, esymend = esym + symtab_hdr->sh_info; | |
1501 | esym < esymend; | |
1502 | ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL)) | |
252b5132 RH |
1503 | { |
1504 | asection *isec; | |
1505 | ||
f8ecb12b AM |
1506 | bfd_elf32_swap_symbol_in (input_bfd, (const PTR) esym, |
1507 | (const PTR) shndx, isymp); | |
252b5132 RH |
1508 | |
1509 | if (isymp->st_shndx == SHN_UNDEF) | |
1510 | isec = bfd_und_section_ptr; | |
252b5132 RH |
1511 | else if (isymp->st_shndx == SHN_ABS) |
1512 | isec = bfd_abs_section_ptr; | |
1513 | else if (isymp->st_shndx == SHN_COMMON) | |
1514 | isec = bfd_com_section_ptr; | |
1515 | else | |
9ad5cbcf | 1516 | isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx); |
252b5132 RH |
1517 | |
1518 | *secpp = isec; | |
1519 | } | |
1520 | ||
1521 | if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd, | |
1522 | input_section, data, internal_relocs, | |
1523 | internal_syms, sections)) | |
1524 | goto error_return; | |
1525 | ||
1526 | if (sections != NULL) | |
1527 | free (sections); | |
252b5132 RH |
1528 | if (internal_syms != NULL) |
1529 | free (internal_syms); | |
9ad5cbcf AM |
1530 | if (shndx_buf != NULL) |
1531 | free (shndx_buf); | |
252b5132 RH |
1532 | if (external_syms != NULL && symtab_hdr->contents == NULL) |
1533 | free (external_syms); | |
252b5132 RH |
1534 | if (internal_relocs != elf_section_data (input_section)->relocs) |
1535 | free (internal_relocs); | |
252b5132 RH |
1536 | } |
1537 | ||
1538 | return data; | |
1539 | ||
1540 | error_return: | |
1541 | if (internal_relocs != NULL | |
1542 | && internal_relocs != elf_section_data (input_section)->relocs) | |
1543 | free (internal_relocs); | |
9ad5cbcf AM |
1544 | if (shndx_buf != NULL) |
1545 | free (shndx_buf); | |
252b5132 RH |
1546 | if (external_syms != NULL && symtab_hdr->contents == NULL) |
1547 | free (external_syms); | |
1548 | if (internal_syms != NULL) | |
1549 | free (internal_syms); | |
1550 | if (sections != NULL) | |
1551 | free (sections); | |
1552 | return NULL; | |
1553 | } | |
1554 | ||
252b5132 RH |
1555 | #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec |
1556 | #define TARGET_LITTLE_NAME "elf32-mn10200" | |
1557 | #define ELF_ARCH bfd_arch_mn10200 | |
aa4f99bb AO |
1558 | #define ELF_MACHINE_CODE EM_MN10200 |
1559 | #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200 | |
252b5132 RH |
1560 | #define ELF_MAXPAGESIZE 0x1000 |
1561 | ||
b491616a | 1562 | #define elf_backend_rela_normal 1 |
252b5132 RH |
1563 | #define elf_info_to_howto mn10200_info_to_howto |
1564 | #define elf_info_to_howto_rel 0 | |
1565 | #define elf_backend_relocate_section mn10200_elf_relocate_section | |
1566 | #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section | |
1567 | #define bfd_elf32_bfd_get_relocated_section_contents \ | |
1568 | mn10200_elf_get_relocated_section_contents | |
1569 | ||
1570 | #define elf_symbol_leading_char '_' | |
1571 | ||
1572 | #include "elf32-target.h" |