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