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