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