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