Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Matsushita 10300 specific support for 32-bit ELF |
7898deda | 2 | Copyright 1996, 1997, 1998, 1999, 2000, 2001 |
010ac81f | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | |
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "libbfd.h" | |
24 | #include "elf-bfd.h" | |
25 | #include "elf/mn10300.h" | |
26 | ||
917583ad NC |
27 | static bfd_reloc_status_type mn10300_elf_final_link_relocate |
28 | PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *, | |
29 | bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *, | |
30 | asection *, int)); | |
31 | static boolean mn10300_elf_relocate_section | |
32 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
33 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
34 | static boolean mn10300_elf_relax_section | |
35 | PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *)); | |
36 | static bfd_byte * mn10300_elf_get_relocated_section_contents | |
37 | PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, | |
38 | bfd_byte *, boolean, asymbol **)); | |
dc810e39 | 39 | static unsigned long elf_mn10300_mach PARAMS ((flagword)); |
917583ad NC |
40 | |
41 | void _bfd_mn10300_elf_final_write_processing PARAMS ((bfd *, boolean)); | |
42 | boolean _bfd_mn10300_elf_object_p PARAMS ((bfd *)); | |
43 | boolean _bfd_mn10300_elf_merge_private_bfd_data PARAMS ((bfd *,bfd *)); | |
44 | ||
010ac81f | 45 | struct elf32_mn10300_link_hash_entry { |
252b5132 RH |
46 | /* The basic elf link hash table entry. */ |
47 | struct elf_link_hash_entry root; | |
48 | ||
49 | /* For function symbols, the number of times this function is | |
50 | called directly (ie by name). */ | |
51 | unsigned int direct_calls; | |
52 | ||
53 | /* For function symbols, the size of this function's stack | |
54 | (if <= 255 bytes). We stuff this into "call" instructions | |
55 | to this target when it's valid and profitable to do so. | |
56 | ||
57 | This does not include stack allocated by movm! */ | |
58 | unsigned char stack_size; | |
59 | ||
60 | /* For function symbols, arguments (if any) for movm instruction | |
61 | in the prologue. We stuff this value into "call" instructions | |
62 | to the target when it's valid and profitable to do so. */ | |
63 | unsigned char movm_args; | |
64 | ||
65 | /* For funtion symbols, the amount of stack space that would be allocated | |
66 | by the movm instruction. This is redundant with movm_args, but we | |
67 | add it to the hash table to avoid computing it over and over. */ | |
68 | unsigned char movm_stack_size; | |
69 | ||
70 | /* When set, convert all "call" instructions to this target into "calls" | |
71 | instructions. */ | |
72 | #define MN10300_CONVERT_CALL_TO_CALLS 0x1 | |
73 | ||
74 | /* Used to mark functions which have had redundant parts of their | |
75 | prologue deleted. */ | |
76 | #define MN10300_DELETED_PROLOGUE_BYTES 0x2 | |
77 | unsigned char flags; | |
78 | }; | |
79 | ||
80 | /* We derive a hash table from the main elf linker hash table so | |
81 | we can store state variables and a secondary hash table without | |
82 | resorting to global variables. */ | |
010ac81f | 83 | struct elf32_mn10300_link_hash_table { |
252b5132 RH |
84 | /* The main hash table. */ |
85 | struct elf_link_hash_table root; | |
86 | ||
87 | /* A hash table for static functions. We could derive a new hash table | |
88 | instead of using the full elf32_mn10300_link_hash_table if we wanted | |
89 | to save some memory. */ | |
90 | struct elf32_mn10300_link_hash_table *static_hash_table; | |
91 | ||
92 | /* Random linker state flags. */ | |
93 | #define MN10300_HASH_ENTRIES_INITIALIZED 0x1 | |
94 | char flags; | |
95 | }; | |
96 | ||
97 | /* For MN10300 linker hash table. */ | |
98 | ||
99 | /* Get the MN10300 ELF linker hash table from a link_info structure. */ | |
100 | ||
101 | #define elf32_mn10300_hash_table(p) \ | |
102 | ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) | |
103 | ||
104 | #define elf32_mn10300_link_hash_traverse(table, func, info) \ | |
105 | (elf_link_hash_traverse \ | |
106 | (&(table)->root, \ | |
107 | (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ | |
108 | (info))) | |
109 | ||
110 | static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc | |
111 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
112 | static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create | |
113 | PARAMS ((bfd *)); | |
114 | ||
115 | static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup | |
116 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); | |
117 | static void mn10300_info_to_howto | |
118 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); | |
119 | static boolean mn10300_elf_check_relocs | |
120 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
121 | const Elf_Internal_Rela *)); | |
122 | static asection *mn10300_elf_gc_mark_hook | |
123 | PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *, | |
124 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); | |
125 | static boolean mn10300_elf_relax_delete_bytes | |
126 | PARAMS ((bfd *, asection *, bfd_vma, int)); | |
127 | static boolean mn10300_elf_symbol_address_p | |
128 | PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma)); | |
129 | static boolean elf32_mn10300_finish_hash_table_entry | |
130 | PARAMS ((struct bfd_hash_entry *, PTR)); | |
131 | static void compute_function_info | |
132 | PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *, | |
133 | bfd_vma, unsigned char *)); | |
134 | ||
135 | /* We have to use RELA instructions since md_apply_fix3 in the assembler | |
136 | does absolutely nothing. */ | |
137 | #define USE_RELA | |
138 | ||
010ac81f | 139 | static reloc_howto_type elf_mn10300_howto_table[] = { |
252b5132 RH |
140 | /* Dummy relocation. Does nothing. */ |
141 | HOWTO (R_MN10300_NONE, | |
142 | 0, | |
143 | 2, | |
144 | 16, | |
145 | false, | |
146 | 0, | |
147 | complain_overflow_bitfield, | |
148 | bfd_elf_generic_reloc, | |
149 | "R_MN10300_NONE", | |
150 | false, | |
151 | 0, | |
152 | 0, | |
153 | false), | |
154 | /* Standard 32 bit reloc. */ | |
155 | HOWTO (R_MN10300_32, | |
156 | 0, | |
157 | 2, | |
158 | 32, | |
159 | false, | |
160 | 0, | |
161 | complain_overflow_bitfield, | |
162 | bfd_elf_generic_reloc, | |
163 | "R_MN10300_32", | |
164 | false, | |
165 | 0xffffffff, | |
166 | 0xffffffff, | |
167 | false), | |
168 | /* Standard 16 bit reloc. */ | |
169 | HOWTO (R_MN10300_16, | |
170 | 0, | |
171 | 1, | |
172 | 16, | |
173 | false, | |
174 | 0, | |
175 | complain_overflow_bitfield, | |
176 | bfd_elf_generic_reloc, | |
177 | "R_MN10300_16", | |
178 | false, | |
179 | 0xffff, | |
180 | 0xffff, | |
181 | false), | |
182 | /* Standard 8 bit reloc. */ | |
183 | HOWTO (R_MN10300_8, | |
184 | 0, | |
185 | 0, | |
186 | 8, | |
187 | false, | |
188 | 0, | |
189 | complain_overflow_bitfield, | |
190 | bfd_elf_generic_reloc, | |
191 | "R_MN10300_8", | |
192 | false, | |
193 | 0xff, | |
194 | 0xff, | |
195 | false), | |
196 | /* Standard 32bit pc-relative reloc. */ | |
197 | HOWTO (R_MN10300_PCREL32, | |
198 | 0, | |
199 | 2, | |
200 | 32, | |
201 | true, | |
202 | 0, | |
203 | complain_overflow_bitfield, | |
204 | bfd_elf_generic_reloc, | |
205 | "R_MN10300_PCREL32", | |
206 | false, | |
207 | 0xffffffff, | |
208 | 0xffffffff, | |
209 | true), | |
210 | /* Standard 16bit pc-relative reloc. */ | |
211 | HOWTO (R_MN10300_PCREL16, | |
212 | 0, | |
213 | 1, | |
214 | 16, | |
215 | true, | |
216 | 0, | |
217 | complain_overflow_bitfield, | |
218 | bfd_elf_generic_reloc, | |
219 | "R_MN10300_PCREL16", | |
220 | false, | |
221 | 0xffff, | |
222 | 0xffff, | |
223 | true), | |
224 | /* Standard 8 pc-relative reloc. */ | |
225 | HOWTO (R_MN10300_PCREL8, | |
226 | 0, | |
227 | 0, | |
228 | 8, | |
229 | true, | |
230 | 0, | |
231 | complain_overflow_bitfield, | |
232 | bfd_elf_generic_reloc, | |
233 | "R_MN10300_PCREL8", | |
234 | false, | |
235 | 0xff, | |
236 | 0xff, | |
237 | true), | |
238 | ||
239 | /* GNU extension to record C++ vtable hierarchy */ | |
240 | HOWTO (R_MN10300_GNU_VTINHERIT, /* type */ | |
241 | 0, /* rightshift */ | |
242 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
243 | 0, /* bitsize */ | |
244 | false, /* pc_relative */ | |
245 | 0, /* bitpos */ | |
246 | complain_overflow_dont, /* complain_on_overflow */ | |
247 | NULL, /* special_function */ | |
248 | "R_MN10300_GNU_VTINHERIT", /* name */ | |
249 | false, /* partial_inplace */ | |
250 | 0, /* src_mask */ | |
251 | 0, /* dst_mask */ | |
252 | false), /* pcrel_offset */ | |
253 | ||
254 | /* GNU extension to record C++ vtable member usage */ | |
255 | HOWTO (R_MN10300_GNU_VTENTRY, /* type */ | |
256 | 0, /* rightshift */ | |
257 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
258 | 0, /* bitsize */ | |
259 | false, /* pc_relative */ | |
260 | 0, /* bitpos */ | |
261 | complain_overflow_dont, /* complain_on_overflow */ | |
262 | NULL, /* special_function */ | |
263 | "R_MN10300_GNU_VTENTRY", /* name */ | |
264 | false, /* partial_inplace */ | |
265 | 0, /* src_mask */ | |
266 | 0, /* dst_mask */ | |
267 | false), /* pcrel_offset */ | |
268 | ||
269 | /* Standard 24 bit reloc. */ | |
270 | HOWTO (R_MN10300_24, | |
271 | 0, | |
272 | 2, | |
273 | 24, | |
274 | false, | |
275 | 0, | |
276 | complain_overflow_bitfield, | |
277 | bfd_elf_generic_reloc, | |
278 | "R_MN10300_24", | |
279 | false, | |
280 | 0xffffff, | |
281 | 0xffffff, | |
282 | false), | |
252b5132 RH |
283 | }; |
284 | ||
010ac81f | 285 | struct mn10300_reloc_map { |
252b5132 RH |
286 | bfd_reloc_code_real_type bfd_reloc_val; |
287 | unsigned char elf_reloc_val; | |
288 | }; | |
289 | ||
010ac81f | 290 | static const struct mn10300_reloc_map mn10300_reloc_map[] = { |
252b5132 RH |
291 | { BFD_RELOC_NONE, R_MN10300_NONE, }, |
292 | { BFD_RELOC_32, R_MN10300_32, }, | |
293 | { BFD_RELOC_16, R_MN10300_16, }, | |
294 | { BFD_RELOC_8, R_MN10300_8, }, | |
295 | { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, }, | |
296 | { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, }, | |
297 | { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, }, | |
298 | { BFD_RELOC_24, R_MN10300_24, }, | |
299 | { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT }, | |
300 | { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY }, | |
301 | }; | |
302 | ||
303 | static reloc_howto_type * | |
304 | bfd_elf32_bfd_reloc_type_lookup (abfd, code) | |
5f771d47 | 305 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
306 | bfd_reloc_code_real_type code; |
307 | { | |
308 | unsigned int i; | |
309 | ||
310 | for (i = 0; | |
311 | i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map); | |
312 | i++) | |
313 | { | |
314 | if (mn10300_reloc_map[i].bfd_reloc_val == code) | |
315 | return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val]; | |
316 | } | |
317 | ||
318 | return NULL; | |
319 | } | |
320 | ||
321 | /* Set the howto pointer for an MN10300 ELF reloc. */ | |
322 | ||
323 | static void | |
324 | mn10300_info_to_howto (abfd, cache_ptr, dst) | |
5f771d47 | 325 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
326 | arelent *cache_ptr; |
327 | Elf32_Internal_Rela *dst; | |
328 | { | |
329 | unsigned int r_type; | |
330 | ||
331 | r_type = ELF32_R_TYPE (dst->r_info); | |
332 | BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX); | |
333 | cache_ptr->howto = &elf_mn10300_howto_table[r_type]; | |
334 | } | |
335 | ||
336 | /* Look through the relocs for a section during the first phase. | |
337 | Since we don't do .gots or .plts, we just need to consider the | |
338 | virtual table relocs for gc. */ | |
339 | ||
340 | static boolean | |
341 | mn10300_elf_check_relocs (abfd, info, sec, relocs) | |
342 | bfd *abfd; | |
343 | struct bfd_link_info *info; | |
344 | asection *sec; | |
345 | const Elf_Internal_Rela *relocs; | |
346 | { | |
347 | Elf_Internal_Shdr *symtab_hdr; | |
348 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
349 | const Elf_Internal_Rela *rel; | |
350 | const Elf_Internal_Rela *rel_end; | |
351 | ||
352 | if (info->relocateable) | |
353 | return true; | |
354 | ||
355 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
356 | sym_hashes = elf_sym_hashes (abfd); | |
a7c10850 | 357 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym); |
252b5132 RH |
358 | if (!elf_bad_symtab (abfd)) |
359 | sym_hashes_end -= symtab_hdr->sh_info; | |
360 | ||
361 | rel_end = relocs + sec->reloc_count; | |
362 | for (rel = relocs; rel < rel_end; rel++) | |
363 | { | |
364 | struct elf_link_hash_entry *h; | |
365 | unsigned long r_symndx; | |
366 | ||
367 | r_symndx = ELF32_R_SYM (rel->r_info); | |
368 | if (r_symndx < symtab_hdr->sh_info) | |
369 | h = NULL; | |
370 | else | |
371 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
372 | ||
373 | switch (ELF32_R_TYPE (rel->r_info)) | |
374 | { | |
375 | /* This relocation describes the C++ object vtable hierarchy. | |
376 | Reconstruct it for later use during GC. */ | |
377 | case R_MN10300_GNU_VTINHERIT: | |
378 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
379 | return false; | |
380 | break; | |
381 | ||
382 | /* This relocation describes which C++ vtable entries are actually | |
383 | used. Record for later use during GC. */ | |
384 | case R_MN10300_GNU_VTENTRY: | |
385 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend)) | |
386 | return false; | |
387 | break; | |
388 | } | |
389 | } | |
390 | ||
391 | return true; | |
392 | } | |
393 | ||
394 | /* Return the section that should be marked against GC for a given | |
395 | relocation. */ | |
396 | ||
397 | static asection * | |
398 | mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym) | |
399 | bfd *abfd; | |
5f771d47 | 400 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
401 | Elf_Internal_Rela *rel; |
402 | struct elf_link_hash_entry *h; | |
403 | Elf_Internal_Sym *sym; | |
404 | { | |
405 | if (h != NULL) | |
406 | { | |
407 | switch (ELF32_R_TYPE (rel->r_info)) | |
408 | { | |
409 | case R_MN10300_GNU_VTINHERIT: | |
410 | case R_MN10300_GNU_VTENTRY: | |
411 | break; | |
412 | ||
413 | default: | |
414 | switch (h->root.type) | |
415 | { | |
416 | case bfd_link_hash_defined: | |
417 | case bfd_link_hash_defweak: | |
418 | return h->root.u.def.section; | |
419 | ||
420 | case bfd_link_hash_common: | |
421 | return h->root.u.c.p->section; | |
e049a0de ILT |
422 | |
423 | default: | |
424 | break; | |
252b5132 RH |
425 | } |
426 | } | |
427 | } | |
428 | else | |
429 | { | |
430 | if (!(elf_bad_symtab (abfd) | |
431 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
432 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
433 | && sym->st_shndx != SHN_COMMON)) | |
434 | { | |
435 | return bfd_section_from_elf_index (abfd, sym->st_shndx); | |
436 | } | |
437 | } | |
438 | ||
439 | return NULL; | |
440 | } | |
441 | ||
442 | /* Perform a relocation as part of a final link. */ | |
443 | static bfd_reloc_status_type | |
444 | mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd, | |
445 | input_section, contents, offset, value, | |
446 | addend, info, sym_sec, is_local) | |
447 | reloc_howto_type *howto; | |
448 | bfd *input_bfd; | |
5f771d47 | 449 | bfd *output_bfd ATTRIBUTE_UNUSED; |
252b5132 RH |
450 | asection *input_section; |
451 | bfd_byte *contents; | |
452 | bfd_vma offset; | |
453 | bfd_vma value; | |
454 | bfd_vma addend; | |
5f771d47 ILT |
455 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
456 | asection *sym_sec ATTRIBUTE_UNUSED; | |
457 | int is_local ATTRIBUTE_UNUSED; | |
252b5132 RH |
458 | { |
459 | unsigned long r_type = howto->type; | |
460 | bfd_byte *hit_data = contents + offset; | |
461 | ||
462 | switch (r_type) | |
463 | { | |
464 | case R_MN10300_NONE: | |
465 | return bfd_reloc_ok; | |
466 | ||
467 | case R_MN10300_32: | |
468 | value += addend; | |
469 | bfd_put_32 (input_bfd, value, hit_data); | |
470 | return bfd_reloc_ok; | |
471 | ||
472 | case R_MN10300_24: | |
473 | value += addend; | |
474 | ||
010ac81f | 475 | if ((long) value > 0x7fffff || (long) value < -0x800000) |
252b5132 RH |
476 | return bfd_reloc_overflow; |
477 | ||
478 | bfd_put_8 (input_bfd, value & 0xff, hit_data); | |
479 | bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1); | |
480 | bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2); | |
481 | return bfd_reloc_ok; | |
482 | ||
483 | case R_MN10300_16: | |
484 | value += addend; | |
485 | ||
010ac81f | 486 | if ((long) value > 0x7fff || (long) value < -0x8000) |
252b5132 RH |
487 | return bfd_reloc_overflow; |
488 | ||
489 | bfd_put_16 (input_bfd, value, hit_data); | |
490 | return bfd_reloc_ok; | |
491 | ||
492 | case R_MN10300_8: | |
493 | value += addend; | |
494 | ||
010ac81f | 495 | if ((long) value > 0x7f || (long) value < -0x80) |
252b5132 RH |
496 | return bfd_reloc_overflow; |
497 | ||
498 | bfd_put_8 (input_bfd, value, hit_data); | |
499 | return bfd_reloc_ok; | |
500 | ||
501 | case R_MN10300_PCREL8: | |
502 | value -= (input_section->output_section->vma | |
503 | + input_section->output_offset); | |
504 | value -= offset; | |
505 | value += addend; | |
506 | ||
010ac81f | 507 | if ((long) value > 0xff || (long) value < -0x100) |
252b5132 RH |
508 | return bfd_reloc_overflow; |
509 | ||
510 | bfd_put_8 (input_bfd, value, hit_data); | |
511 | return bfd_reloc_ok; | |
512 | ||
513 | case R_MN10300_PCREL16: | |
514 | value -= (input_section->output_section->vma | |
515 | + input_section->output_offset); | |
516 | value -= offset; | |
517 | value += addend; | |
518 | ||
010ac81f | 519 | if ((long) value > 0xffff || (long) value < -0x10000) |
252b5132 RH |
520 | return bfd_reloc_overflow; |
521 | ||
522 | bfd_put_16 (input_bfd, value, hit_data); | |
523 | return bfd_reloc_ok; | |
524 | ||
525 | case R_MN10300_PCREL32: | |
526 | value -= (input_section->output_section->vma | |
527 | + input_section->output_offset); | |
528 | value -= offset; | |
529 | value += addend; | |
530 | ||
531 | bfd_put_32 (input_bfd, value, hit_data); | |
532 | return bfd_reloc_ok; | |
533 | ||
534 | case R_MN10300_GNU_VTINHERIT: | |
535 | case R_MN10300_GNU_VTENTRY: | |
536 | return bfd_reloc_ok; | |
537 | ||
538 | default: | |
539 | return bfd_reloc_notsupported; | |
540 | } | |
541 | } | |
252b5132 RH |
542 | \f |
543 | /* Relocate an MN10300 ELF section. */ | |
544 | static boolean | |
545 | mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section, | |
546 | contents, relocs, local_syms, local_sections) | |
547 | bfd *output_bfd; | |
548 | struct bfd_link_info *info; | |
549 | bfd *input_bfd; | |
550 | asection *input_section; | |
551 | bfd_byte *contents; | |
552 | Elf_Internal_Rela *relocs; | |
553 | Elf_Internal_Sym *local_syms; | |
554 | asection **local_sections; | |
555 | { | |
556 | Elf_Internal_Shdr *symtab_hdr; | |
557 | struct elf32_mn10300_link_hash_entry **sym_hashes; | |
558 | Elf_Internal_Rela *rel, *relend; | |
559 | ||
560 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
561 | sym_hashes = (struct elf32_mn10300_link_hash_entry **) | |
562 | (elf_sym_hashes (input_bfd)); | |
563 | ||
564 | rel = relocs; | |
565 | relend = relocs + input_section->reloc_count; | |
566 | for (; rel < relend; rel++) | |
567 | { | |
568 | int r_type; | |
569 | reloc_howto_type *howto; | |
570 | unsigned long r_symndx; | |
571 | Elf_Internal_Sym *sym; | |
572 | asection *sec; | |
573 | struct elf32_mn10300_link_hash_entry *h; | |
574 | bfd_vma relocation; | |
575 | bfd_reloc_status_type r; | |
576 | ||
577 | r_symndx = ELF32_R_SYM (rel->r_info); | |
578 | r_type = ELF32_R_TYPE (rel->r_info); | |
579 | howto = elf_mn10300_howto_table + r_type; | |
580 | ||
581 | /* Just skip the vtable gc relocs. */ | |
582 | if (r_type == R_MN10300_GNU_VTINHERIT | |
583 | || r_type == R_MN10300_GNU_VTENTRY) | |
584 | continue; | |
585 | ||
586 | if (info->relocateable) | |
587 | { | |
588 | /* This is a relocateable link. We don't have to change | |
589 | anything, unless the reloc is against a section symbol, | |
590 | in which case we have to adjust according to where the | |
591 | section symbol winds up in the output section. */ | |
592 | if (r_symndx < symtab_hdr->sh_info) | |
593 | { | |
594 | sym = local_syms + r_symndx; | |
595 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
596 | { | |
597 | sec = local_sections[r_symndx]; | |
598 | rel->r_addend += sec->output_offset + sym->st_value; | |
599 | } | |
600 | } | |
601 | ||
602 | continue; | |
603 | } | |
604 | ||
605 | /* This is a final link. */ | |
606 | h = NULL; | |
607 | sym = NULL; | |
608 | sec = NULL; | |
609 | if (r_symndx < symtab_hdr->sh_info) | |
610 | { | |
611 | sym = local_syms + r_symndx; | |
612 | sec = local_sections[r_symndx]; | |
f8df10f4 | 613 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); |
252b5132 RH |
614 | } |
615 | else | |
616 | { | |
617 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
17ea6349 AO |
618 | while (h->root.root.type == bfd_link_hash_indirect |
619 | || h->root.root.type == bfd_link_hash_warning) | |
252b5132 RH |
620 | h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link; |
621 | if (h->root.root.type == bfd_link_hash_defined | |
622 | || h->root.root.type == bfd_link_hash_defweak) | |
623 | { | |
624 | sec = h->root.root.u.def.section; | |
625 | relocation = (h->root.root.u.def.value | |
626 | + sec->output_section->vma | |
627 | + sec->output_offset); | |
628 | } | |
629 | else if (h->root.root.type == bfd_link_hash_undefweak) | |
630 | relocation = 0; | |
631 | else | |
632 | { | |
633 | if (! ((*info->callbacks->undefined_symbol) | |
634 | (info, h->root.root.root.string, input_bfd, | |
5cc7c785 | 635 | input_section, rel->r_offset, true))) |
252b5132 RH |
636 | return false; |
637 | relocation = 0; | |
638 | } | |
639 | } | |
640 | ||
641 | r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd, | |
642 | input_section, | |
643 | contents, rel->r_offset, | |
644 | relocation, rel->r_addend, | |
645 | info, sec, h == NULL); | |
646 | ||
647 | if (r != bfd_reloc_ok) | |
648 | { | |
649 | const char *name; | |
010ac81f | 650 | const char *msg = (const char *) 0; |
252b5132 RH |
651 | |
652 | if (h != NULL) | |
653 | name = h->root.root.root.string; | |
654 | else | |
655 | { | |
656 | name = (bfd_elf_string_from_elf_section | |
657 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
658 | if (name == NULL || *name == '\0') | |
659 | name = bfd_section_name (input_bfd, sec); | |
660 | } | |
661 | ||
662 | switch (r) | |
663 | { | |
664 | case bfd_reloc_overflow: | |
665 | if (! ((*info->callbacks->reloc_overflow) | |
666 | (info, name, howto->name, (bfd_vma) 0, | |
667 | input_bfd, input_section, rel->r_offset))) | |
668 | return false; | |
669 | break; | |
670 | ||
671 | case bfd_reloc_undefined: | |
672 | if (! ((*info->callbacks->undefined_symbol) | |
673 | (info, name, input_bfd, input_section, | |
5cc7c785 | 674 | rel->r_offset, true))) |
252b5132 RH |
675 | return false; |
676 | break; | |
677 | ||
678 | case bfd_reloc_outofrange: | |
679 | msg = _("internal error: out of range error"); | |
680 | goto common_error; | |
681 | ||
682 | case bfd_reloc_notsupported: | |
683 | msg = _("internal error: unsupported relocation error"); | |
684 | goto common_error; | |
685 | ||
686 | case bfd_reloc_dangerous: | |
687 | msg = _("internal error: dangerous error"); | |
688 | goto common_error; | |
689 | ||
690 | default: | |
691 | msg = _("internal error: unknown error"); | |
692 | /* fall through */ | |
693 | ||
694 | common_error: | |
695 | if (!((*info->callbacks->warning) | |
696 | (info, msg, name, input_bfd, input_section, | |
697 | rel->r_offset))) | |
698 | return false; | |
699 | break; | |
700 | } | |
701 | } | |
702 | } | |
703 | ||
704 | return true; | |
705 | } | |
706 | ||
707 | /* Finish initializing one hash table entry. */ | |
708 | static boolean | |
709 | elf32_mn10300_finish_hash_table_entry (gen_entry, in_args) | |
710 | struct bfd_hash_entry *gen_entry; | |
5f771d47 | 711 | PTR in_args ATTRIBUTE_UNUSED; |
252b5132 RH |
712 | { |
713 | struct elf32_mn10300_link_hash_entry *entry; | |
714 | unsigned int byte_count = 0; | |
715 | ||
010ac81f | 716 | entry = (struct elf32_mn10300_link_hash_entry *) gen_entry; |
252b5132 RH |
717 | |
718 | /* If we already know we want to convert "call" to "calls" for calls | |
719 | to this symbol, then return now. */ | |
720 | if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS) | |
721 | return true; | |
722 | ||
723 | /* If there are no named calls to this symbol, or there's nothing we | |
724 | can move from the function itself into the "call" instruction, then | |
725 | note that all "call" instructions should be converted into "calls" | |
726 | instructions and return. */ | |
727 | if (entry->direct_calls == 0 | |
728 | || (entry->stack_size == 0 && entry->movm_args == 0)) | |
729 | { | |
730 | /* Make a note that we should convert "call" instructions to "calls" | |
731 | instructions for calls to this symbol. */ | |
732 | entry->flags |= MN10300_CONVERT_CALL_TO_CALLS; | |
733 | return true; | |
734 | } | |
735 | ||
736 | /* We may be able to move some instructions from the function itself into | |
737 | the "call" instruction. Count how many bytes we might be able to | |
738 | eliminate in the function itself. */ | |
739 | ||
740 | /* A movm instruction is two bytes. */ | |
741 | if (entry->movm_args) | |
742 | byte_count += 2; | |
743 | ||
744 | /* Count the insn to allocate stack space too. */ | |
745 | if (entry->stack_size > 0 && entry->stack_size <= 128) | |
746 | byte_count += 3; | |
747 | else if (entry->stack_size > 0 && entry->stack_size < 256) | |
748 | byte_count += 4; | |
749 | ||
750 | /* If using "call" will result in larger code, then turn all | |
751 | the associated "call" instructions into "calls" instrutions. */ | |
752 | if (byte_count < entry->direct_calls) | |
753 | entry->flags |= MN10300_CONVERT_CALL_TO_CALLS; | |
754 | ||
755 | /* This routine never fails. */ | |
756 | return true; | |
757 | } | |
758 | ||
759 | /* This function handles relaxing for the mn10300. | |
760 | ||
761 | There's quite a few relaxing opportunites available on the mn10300: | |
762 | ||
763 | * calls:32 -> calls:16 2 bytes | |
764 | * call:32 -> call:16 2 bytes | |
765 | ||
766 | * call:32 -> calls:32 1 byte | |
767 | * call:16 -> calls:16 1 byte | |
768 | * These are done anytime using "calls" would result | |
769 | in smaller code, or when necessary to preserve the | |
770 | meaning of the program. | |
771 | ||
772 | * call:32 varies | |
773 | * call:16 | |
774 | * In some circumstances we can move instructions | |
775 | from a function prologue into a "call" instruction. | |
776 | This is only done if the resulting code is no larger | |
777 | than the original code. | |
778 | ||
252b5132 RH |
779 | * jmp:32 -> jmp:16 2 bytes |
780 | * jmp:16 -> bra:8 1 byte | |
781 | ||
782 | * If the previous instruction is a conditional branch | |
783 | around the jump/bra, we may be able to reverse its condition | |
784 | and change its target to the jump's target. The jump/bra | |
785 | can then be deleted. 2 bytes | |
786 | ||
787 | * mov abs32 -> mov abs16 1 or 2 bytes | |
788 | ||
789 | * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes | |
790 | - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes | |
791 | ||
792 | * Most instructions which accept d32 can relax to d16 1 or 2 bytes | |
793 | - Most instructions which accept d16 can relax to d8 1 or 2 bytes | |
794 | ||
795 | We don't handle imm16->imm8 or d16->d8 as they're very rare | |
796 | and somewhat more difficult to support. */ | |
797 | ||
798 | static boolean | |
799 | mn10300_elf_relax_section (abfd, sec, link_info, again) | |
800 | bfd *abfd; | |
801 | asection *sec; | |
802 | struct bfd_link_info *link_info; | |
803 | boolean *again; | |
804 | { | |
805 | Elf_Internal_Shdr *symtab_hdr; | |
806 | Elf_Internal_Rela *internal_relocs = NULL; | |
807 | Elf_Internal_Rela *free_relocs = NULL; | |
808 | Elf_Internal_Rela *irel, *irelend; | |
809 | bfd_byte *contents = NULL; | |
810 | bfd_byte *free_contents = NULL; | |
811 | Elf32_External_Sym *extsyms = NULL; | |
812 | Elf32_External_Sym *free_extsyms = NULL; | |
813 | struct elf32_mn10300_link_hash_table *hash_table; | |
814 | ||
815 | /* Assume nothing changes. */ | |
816 | *again = false; | |
817 | ||
818 | /* We need a pointer to the mn10300 specific hash table. */ | |
819 | hash_table = elf32_mn10300_hash_table (link_info); | |
820 | ||
821 | /* Initialize fields in each hash table entry the first time through. */ | |
822 | if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0) | |
823 | { | |
824 | bfd *input_bfd; | |
825 | ||
826 | /* Iterate over all the input bfds. */ | |
827 | for (input_bfd = link_info->input_bfds; | |
828 | input_bfd != NULL; | |
829 | input_bfd = input_bfd->link_next) | |
830 | { | |
831 | asection *section; | |
832 | ||
833 | /* We're going to need all the symbols for each bfd. */ | |
834 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
835 | ||
010ac81f KH |
836 | /* Get cached copy if it exists. */ |
837 | if (symtab_hdr->contents != NULL) | |
838 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
839 | else | |
840 | { | |
dc810e39 | 841 | bfd_size_type amt = symtab_hdr->sh_size; |
010ac81f | 842 | /* Go get them off disk. */ |
dc810e39 | 843 | extsyms = (Elf32_External_Sym *) bfd_malloc (amt); |
010ac81f KH |
844 | if (extsyms == NULL) |
845 | goto error_return; | |
846 | free_extsyms = extsyms; | |
847 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
dc810e39 | 848 | || bfd_bread (extsyms, amt, input_bfd) != amt) |
010ac81f KH |
849 | goto error_return; |
850 | } | |
252b5132 RH |
851 | |
852 | /* Iterate over each section in this bfd. */ | |
853 | for (section = input_bfd->sections; | |
854 | section != NULL; | |
855 | section = section->next) | |
856 | { | |
857 | struct elf32_mn10300_link_hash_entry *hash; | |
858 | Elf_Internal_Sym *sym; | |
86033394 | 859 | asection *sym_sec = NULL; |
252b5132 RH |
860 | const char *sym_name; |
861 | char *new_name; | |
252b5132 RH |
862 | |
863 | /* Get cached copy of section contents if it exists. */ | |
864 | if (elf_section_data (section)->this_hdr.contents != NULL) | |
865 | contents = elf_section_data (section)->this_hdr.contents; | |
866 | else if (section->_raw_size != 0) | |
867 | { | |
868 | /* Go get them off disk. */ | |
010ac81f | 869 | contents = (bfd_byte *) bfd_malloc (section->_raw_size); |
252b5132 RH |
870 | if (contents == NULL) |
871 | goto error_return; | |
872 | free_contents = contents; | |
873 | ||
874 | if (!bfd_get_section_contents (input_bfd, section, | |
875 | contents, (file_ptr) 0, | |
876 | section->_raw_size)) | |
877 | goto error_return; | |
878 | } | |
879 | else | |
880 | { | |
881 | contents = NULL; | |
882 | free_contents = NULL; | |
883 | } | |
884 | ||
885 | /* If there aren't any relocs, then there's nothing to do. */ | |
886 | if ((section->flags & SEC_RELOC) != 0 | |
887 | && section->reloc_count != 0) | |
888 | { | |
889 | ||
890 | /* Get a copy of the native relocations. */ | |
891 | internal_relocs = (_bfd_elf32_link_read_relocs | |
892 | (input_bfd, section, (PTR) NULL, | |
893 | (Elf_Internal_Rela *) NULL, | |
894 | link_info->keep_memory)); | |
895 | if (internal_relocs == NULL) | |
896 | goto error_return; | |
897 | if (! link_info->keep_memory) | |
898 | free_relocs = internal_relocs; | |
899 | ||
900 | /* Now examine each relocation. */ | |
901 | irel = internal_relocs; | |
902 | irelend = irel + section->reloc_count; | |
903 | for (; irel < irelend; irel++) | |
904 | { | |
905 | long r_type; | |
906 | unsigned long r_index; | |
907 | unsigned char code; | |
908 | ||
909 | r_type = ELF32_R_TYPE (irel->r_info); | |
910 | r_index = ELF32_R_SYM (irel->r_info); | |
911 | ||
010ac81f | 912 | if (r_type < 0 || r_type >= (int) R_MN10300_MAX) |
252b5132 RH |
913 | goto error_return; |
914 | ||
915 | /* We need the name and hash table entry of the target | |
916 | symbol! */ | |
917 | hash = NULL; | |
918 | sym = NULL; | |
919 | sym_sec = NULL; | |
920 | ||
921 | if (r_index < symtab_hdr->sh_info) | |
922 | { | |
923 | /* A local symbol. */ | |
924 | Elf_Internal_Sym isym; | |
dc810e39 AM |
925 | struct elf_link_hash_table *elftab; |
926 | bfd_size_type amt; | |
252b5132 RH |
927 | |
928 | bfd_elf32_swap_symbol_in (input_bfd, | |
929 | extsyms + r_index, &isym); | |
930 | ||
931 | if (isym.st_shndx == SHN_UNDEF) | |
932 | sym_sec = bfd_und_section_ptr; | |
933 | else if (isym.st_shndx > 0 | |
934 | && isym.st_shndx < SHN_LORESERVE) | |
935 | sym_sec | |
936 | = bfd_section_from_elf_index (input_bfd, | |
937 | isym.st_shndx); | |
938 | else if (isym.st_shndx == SHN_ABS) | |
939 | sym_sec = bfd_abs_section_ptr; | |
940 | else if (isym.st_shndx == SHN_COMMON) | |
941 | sym_sec = bfd_com_section_ptr; | |
a7c10850 | 942 | |
252b5132 RH |
943 | sym_name = bfd_elf_string_from_elf_section (input_bfd, |
944 | symtab_hdr->sh_link, | |
945 | isym.st_name); | |
946 | ||
947 | /* If it isn't a function, then we don't care | |
948 | about it. */ | |
949 | if (r_index < symtab_hdr->sh_info | |
950 | && ELF_ST_TYPE (isym.st_info) != STT_FUNC) | |
951 | continue; | |
952 | ||
953 | /* Tack on an ID so we can uniquely identify this | |
954 | local symbol in the global hash table. */ | |
dc810e39 AM |
955 | amt = strlen (sym_name) + 10; |
956 | new_name = bfd_malloc (amt); | |
252b5132 RH |
957 | if (new_name == 0) |
958 | goto error_return; | |
959 | ||
010ac81f KH |
960 | sprintf (new_name, "%s_%08x", |
961 | sym_name, (int) sym_sec); | |
252b5132 RH |
962 | sym_name = new_name; |
963 | ||
dc810e39 AM |
964 | elftab = &hash_table->static_hash_table->root; |
965 | hash = ((struct elf32_mn10300_link_hash_entry *) | |
966 | elf_link_hash_lookup (elftab, sym_name, | |
967 | true, true, false)); | |
252b5132 RH |
968 | free (new_name); |
969 | } | |
970 | else | |
971 | { | |
972 | r_index -= symtab_hdr->sh_info; | |
973 | hash = (struct elf32_mn10300_link_hash_entry *) | |
974 | elf_sym_hashes (input_bfd)[r_index]; | |
975 | } | |
976 | ||
977 | /* If this is not a "call" instruction, then we | |
978 | should convert "call" instructions to "calls" | |
979 | instructions. */ | |
980 | code = bfd_get_8 (input_bfd, | |
981 | contents + irel->r_offset - 1); | |
982 | if (code != 0xdd && code != 0xcd) | |
983 | hash->flags |= MN10300_CONVERT_CALL_TO_CALLS; | |
984 | ||
985 | /* If this is a jump/call, then bump the direct_calls | |
986 | counter. Else force "call" to "calls" conversions. */ | |
987 | if (r_type == R_MN10300_PCREL32 | |
988 | || r_type == R_MN10300_PCREL16) | |
989 | hash->direct_calls++; | |
990 | else | |
991 | hash->flags |= MN10300_CONVERT_CALL_TO_CALLS; | |
992 | } | |
993 | } | |
994 | ||
995 | /* Now look at the actual contents to get the stack size, | |
996 | and a list of what registers were saved in the prologue | |
997 | (ie movm_args). */ | |
998 | if ((section->flags & SEC_CODE) != 0) | |
999 | { | |
1000 | ||
1001 | Elf32_External_Sym *esym, *esymend; | |
1002 | int idx, shndx; | |
1003 | ||
1004 | shndx = _bfd_elf_section_from_bfd_section (input_bfd, | |
1005 | section); | |
1006 | ||
252b5132 RH |
1007 | /* Look at each function defined in this section and |
1008 | update info for that function. */ | |
1009 | esym = extsyms; | |
1010 | esymend = esym + symtab_hdr->sh_info; | |
1011 | for (; esym < esymend; esym++) | |
1012 | { | |
1013 | Elf_Internal_Sym isym; | |
1014 | ||
1015 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); | |
1016 | if (isym.st_shndx == shndx | |
1017 | && ELF_ST_TYPE (isym.st_info) == STT_FUNC) | |
1018 | { | |
dc810e39 AM |
1019 | struct elf_link_hash_table *elftab; |
1020 | bfd_size_type amt; | |
1021 | ||
252b5132 RH |
1022 | if (isym.st_shndx == SHN_UNDEF) |
1023 | sym_sec = bfd_und_section_ptr; | |
1024 | else if (isym.st_shndx > 0 | |
1025 | && isym.st_shndx < SHN_LORESERVE) | |
1026 | sym_sec | |
1027 | = bfd_section_from_elf_index (input_bfd, | |
1028 | isym.st_shndx); | |
1029 | else if (isym.st_shndx == SHN_ABS) | |
1030 | sym_sec = bfd_abs_section_ptr; | |
1031 | else if (isym.st_shndx == SHN_COMMON) | |
1032 | sym_sec = bfd_com_section_ptr; | |
1033 | ||
dc810e39 AM |
1034 | sym_name = (bfd_elf_string_from_elf_section |
1035 | (input_bfd, symtab_hdr->sh_link, | |
1036 | isym.st_name)); | |
252b5132 RH |
1037 | |
1038 | /* Tack on an ID so we can uniquely identify this | |
1039 | local symbol in the global hash table. */ | |
dc810e39 AM |
1040 | amt = strlen (sym_name) + 10; |
1041 | new_name = bfd_malloc (amt); | |
252b5132 RH |
1042 | if (new_name == 0) |
1043 | goto error_return; | |
1044 | ||
010ac81f KH |
1045 | sprintf (new_name, "%s_%08x", |
1046 | sym_name, (int) sym_sec); | |
252b5132 RH |
1047 | sym_name = new_name; |
1048 | ||
dc810e39 AM |
1049 | elftab = &hash_table->static_hash_table->root; |
1050 | hash = ((struct elf32_mn10300_link_hash_entry *) | |
1051 | elf_link_hash_lookup (elftab, sym_name, | |
1052 | true, true, false)); | |
252b5132 RH |
1053 | free (new_name); |
1054 | compute_function_info (input_bfd, hash, | |
010ac81f | 1055 | isym.st_value, contents); |
252b5132 RH |
1056 | } |
1057 | } | |
1058 | ||
1059 | esym = extsyms + symtab_hdr->sh_info; | |
1060 | esymend = extsyms + (symtab_hdr->sh_size | |
1061 | / sizeof (Elf32_External_Sym)); | |
1062 | for (idx = 0; esym < esymend; esym++, idx++) | |
1063 | { | |
1064 | Elf_Internal_Sym isym; | |
1065 | ||
1066 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); | |
1067 | hash = (struct elf32_mn10300_link_hash_entry *) | |
1068 | elf_sym_hashes (input_bfd)[idx]; | |
1069 | if (isym.st_shndx == shndx | |
1070 | && ELF_ST_TYPE (isym.st_info) == STT_FUNC | |
1071 | && (hash)->root.root.u.def.section == section | |
1072 | && ((hash)->root.root.type == bfd_link_hash_defined | |
1073 | || (hash)->root.root.type == bfd_link_hash_defweak)) | |
1074 | compute_function_info (input_bfd, hash, | |
1075 | (hash)->root.root.u.def.value, | |
1076 | contents); | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | /* Cache or free any memory we allocated for the relocs. */ | |
1081 | if (free_relocs != NULL) | |
1082 | { | |
1083 | free (free_relocs); | |
1084 | free_relocs = NULL; | |
1085 | } | |
1086 | ||
1087 | /* Cache or free any memory we allocated for the contents. */ | |
1088 | if (free_contents != NULL) | |
1089 | { | |
1090 | if (! link_info->keep_memory) | |
1091 | free (free_contents); | |
1092 | else | |
1093 | { | |
1094 | /* Cache the section contents for elf_link_input_bfd. */ | |
1095 | elf_section_data (section)->this_hdr.contents = contents; | |
1096 | } | |
1097 | free_contents = NULL; | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | /* Cache or free any memory we allocated for the symbols. */ | |
1102 | if (free_extsyms != NULL) | |
1103 | { | |
1104 | if (! link_info->keep_memory) | |
1105 | free (free_extsyms); | |
1106 | else | |
1107 | { | |
1108 | /* Cache the symbols for elf_link_input_bfd. */ | |
973ffd63 | 1109 | symtab_hdr->contents = (unsigned char *) extsyms; |
252b5132 RH |
1110 | } |
1111 | free_extsyms = NULL; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | /* Now iterate on each symbol in the hash table and perform | |
1116 | the final initialization steps on each. */ | |
1117 | elf32_mn10300_link_hash_traverse (hash_table, | |
1118 | elf32_mn10300_finish_hash_table_entry, | |
1119 | NULL); | |
1120 | elf32_mn10300_link_hash_traverse (hash_table->static_hash_table, | |
1121 | elf32_mn10300_finish_hash_table_entry, | |
1122 | NULL); | |
1123 | ||
1124 | /* All entries in the hash table are fully initialized. */ | |
1125 | hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED; | |
1126 | ||
1127 | /* Now that everything has been initialized, go through each | |
1128 | code section and delete any prologue insns which will be | |
1129 | redundant because their operations will be performed by | |
1130 | a "call" instruction. */ | |
1131 | for (input_bfd = link_info->input_bfds; | |
1132 | input_bfd != NULL; | |
1133 | input_bfd = input_bfd->link_next) | |
1134 | { | |
1135 | asection *section; | |
1136 | ||
1137 | /* We're going to need all the symbols for each bfd. */ | |
1138 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1139 | ||
010ac81f KH |
1140 | /* Get cached copy if it exists. */ |
1141 | if (symtab_hdr->contents != NULL) | |
1142 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
1143 | else | |
1144 | { | |
dc810e39 | 1145 | bfd_size_type amt = symtab_hdr->sh_size; |
010ac81f | 1146 | /* Go get them off disk. */ |
dc810e39 | 1147 | extsyms = (Elf32_External_Sym *) bfd_malloc (amt); |
010ac81f KH |
1148 | if (extsyms == NULL) |
1149 | goto error_return; | |
1150 | free_extsyms = extsyms; | |
1151 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
dc810e39 | 1152 | || bfd_bread (extsyms, amt, input_bfd) != amt) |
010ac81f KH |
1153 | goto error_return; |
1154 | } | |
252b5132 RH |
1155 | |
1156 | /* Walk over each section in this bfd. */ | |
1157 | for (section = input_bfd->sections; | |
1158 | section != NULL; | |
1159 | section = section->next) | |
1160 | { | |
1161 | int shndx; | |
1162 | Elf32_External_Sym *esym, *esymend; | |
1163 | int idx; | |
1164 | ||
1165 | /* Skip non-code sections and empty sections. */ | |
1166 | if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0) | |
1167 | continue; | |
1168 | ||
1169 | if (section->reloc_count != 0) | |
1170 | { | |
010ac81f KH |
1171 | /* Get a copy of the native relocations. */ |
1172 | internal_relocs = (_bfd_elf32_link_read_relocs | |
1173 | (input_bfd, section, (PTR) NULL, | |
1174 | (Elf_Internal_Rela *) NULL, | |
1175 | link_info->keep_memory)); | |
1176 | if (internal_relocs == NULL) | |
1177 | goto error_return; | |
1178 | if (! link_info->keep_memory) | |
1179 | free_relocs = internal_relocs; | |
252b5132 RH |
1180 | } |
1181 | ||
1182 | /* Get cached copy of section contents if it exists. */ | |
1183 | if (elf_section_data (section)->this_hdr.contents != NULL) | |
1184 | contents = elf_section_data (section)->this_hdr.contents; | |
1185 | else | |
1186 | { | |
1187 | /* Go get them off disk. */ | |
010ac81f | 1188 | contents = (bfd_byte *) bfd_malloc (section->_raw_size); |
252b5132 RH |
1189 | if (contents == NULL) |
1190 | goto error_return; | |
1191 | free_contents = contents; | |
1192 | ||
1193 | if (!bfd_get_section_contents (input_bfd, section, | |
1194 | contents, (file_ptr) 0, | |
1195 | section->_raw_size)) | |
1196 | goto error_return; | |
1197 | } | |
1198 | ||
252b5132 RH |
1199 | shndx = _bfd_elf_section_from_bfd_section (input_bfd, section); |
1200 | ||
1201 | /* Now look for any function in this section which needs | |
1202 | insns deleted from its prologue. */ | |
1203 | esym = extsyms; | |
1204 | esymend = esym + symtab_hdr->sh_info; | |
1205 | for (; esym < esymend; esym++) | |
1206 | { | |
1207 | Elf_Internal_Sym isym; | |
1208 | struct elf32_mn10300_link_hash_entry *sym_hash; | |
86033394 | 1209 | asection *sym_sec = NULL; |
252b5132 | 1210 | const char *sym_name; |
252b5132 | 1211 | char *new_name; |
dc810e39 AM |
1212 | struct elf_link_hash_table *elftab; |
1213 | bfd_size_type amt; | |
252b5132 RH |
1214 | |
1215 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); | |
1216 | ||
1217 | if (isym.st_shndx != shndx) | |
1218 | continue; | |
1219 | ||
1220 | if (isym.st_shndx == SHN_UNDEF) | |
1221 | sym_sec = bfd_und_section_ptr; | |
1222 | else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE) | |
1223 | sym_sec | |
1224 | = bfd_section_from_elf_index (input_bfd, isym.st_shndx); | |
1225 | else if (isym.st_shndx == SHN_ABS) | |
1226 | sym_sec = bfd_abs_section_ptr; | |
1227 | else if (isym.st_shndx == SHN_COMMON) | |
1228 | sym_sec = bfd_com_section_ptr; | |
86033394 NC |
1229 | else |
1230 | abort (); | |
a7c10850 | 1231 | |
252b5132 RH |
1232 | sym_name = bfd_elf_string_from_elf_section (input_bfd, |
1233 | symtab_hdr->sh_link, | |
1234 | isym.st_name); | |
1235 | ||
1236 | /* Tack on an ID so we can uniquely identify this | |
1237 | local symbol in the global hash table. */ | |
dc810e39 AM |
1238 | amt = strlen (sym_name) + 10; |
1239 | new_name = bfd_malloc (amt); | |
252b5132 RH |
1240 | if (new_name == 0) |
1241 | goto error_return; | |
010ac81f | 1242 | sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec); |
252b5132 RH |
1243 | sym_name = new_name; |
1244 | ||
dc810e39 AM |
1245 | elftab = &hash_table->static_hash_table->root; |
1246 | sym_hash = ((struct elf32_mn10300_link_hash_entry *) | |
1247 | elf_link_hash_lookup (elftab, sym_name, | |
1248 | false, false, false)); | |
252b5132 RH |
1249 | |
1250 | free (new_name); | |
1251 | if (sym_hash == NULL) | |
1252 | continue; | |
1253 | ||
1254 | if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS) | |
1255 | && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES)) | |
1256 | { | |
1257 | int bytes = 0; | |
1258 | ||
1259 | /* Note that we've changed things. */ | |
1260 | elf_section_data (section)->relocs = internal_relocs; | |
1261 | free_relocs = NULL; | |
1262 | ||
1263 | elf_section_data (section)->this_hdr.contents = contents; | |
1264 | free_contents = NULL; | |
1265 | ||
010ac81f | 1266 | symtab_hdr->contents = (bfd_byte *) extsyms; |
252b5132 RH |
1267 | free_extsyms = NULL; |
1268 | ||
1269 | /* Count how many bytes we're going to delete. */ | |
1270 | if (sym_hash->movm_args) | |
1271 | bytes += 2; | |
1272 | ||
1273 | if (sym_hash->stack_size && sym_hash->stack_size <= 128) | |
1274 | bytes += 3; | |
1275 | else if (sym_hash->stack_size | |
1276 | && sym_hash->stack_size < 256) | |
1277 | bytes += 4; | |
1278 | ||
1279 | /* Note that we've deleted prologue bytes for this | |
1280 | function. */ | |
1281 | sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES; | |
1282 | ||
1283 | /* Actually delete the bytes. */ | |
1284 | if (!mn10300_elf_relax_delete_bytes (input_bfd, | |
1285 | section, | |
1286 | isym.st_value, | |
1287 | bytes)) | |
1288 | goto error_return; | |
1289 | ||
1290 | /* Something changed. Not strictly necessary, but | |
1291 | may lead to more relaxing opportunities. */ | |
1292 | *again = true; | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | /* Look for any global functions in this section which | |
1297 | need insns deleted from their prologues. */ | |
1298 | esym = extsyms + symtab_hdr->sh_info; | |
1299 | esymend = extsyms + (symtab_hdr->sh_size | |
1300 | / sizeof (Elf32_External_Sym)); | |
1301 | for (idx = 0; esym < esymend; esym++, idx++) | |
1302 | { | |
1303 | Elf_Internal_Sym isym; | |
1304 | struct elf32_mn10300_link_hash_entry *sym_hash; | |
1305 | ||
1306 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); | |
1307 | sym_hash = (struct elf32_mn10300_link_hash_entry *) | |
1308 | (elf_sym_hashes (input_bfd)[idx]); | |
1309 | if (isym.st_shndx == shndx | |
1310 | && (sym_hash)->root.root.u.def.section == section | |
1311 | && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS) | |
1312 | && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES)) | |
1313 | { | |
1314 | int bytes = 0; | |
1315 | ||
1316 | /* Note that we've changed things. */ | |
1317 | elf_section_data (section)->relocs = internal_relocs; | |
1318 | free_relocs = NULL; | |
1319 | ||
1320 | elf_section_data (section)->this_hdr.contents = contents; | |
1321 | free_contents = NULL; | |
1322 | ||
010ac81f | 1323 | symtab_hdr->contents = (bfd_byte *) extsyms; |
252b5132 RH |
1324 | free_extsyms = NULL; |
1325 | ||
1326 | /* Count how many bytes we're going to delete. */ | |
1327 | if (sym_hash->movm_args) | |
1328 | bytes += 2; | |
1329 | ||
1330 | if (sym_hash->stack_size && sym_hash->stack_size <= 128) | |
1331 | bytes += 3; | |
1332 | else if (sym_hash->stack_size | |
1333 | && sym_hash->stack_size < 256) | |
1334 | bytes += 4; | |
1335 | ||
1336 | /* Note that we've deleted prologue bytes for this | |
1337 | function. */ | |
1338 | sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES; | |
1339 | ||
1340 | /* Actually delete the bytes. */ | |
1341 | if (!mn10300_elf_relax_delete_bytes (input_bfd, | |
1342 | section, | |
1343 | (sym_hash)->root.root.u.def.value, | |
1344 | bytes)) | |
1345 | goto error_return; | |
1346 | ||
1347 | /* Something changed. Not strictly necessary, but | |
1348 | may lead to more relaxing opportunities. */ | |
1349 | *again = true; | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | /* Cache or free any memory we allocated for the relocs. */ | |
1354 | if (free_relocs != NULL) | |
1355 | { | |
1356 | free (free_relocs); | |
1357 | free_relocs = NULL; | |
1358 | } | |
1359 | ||
1360 | /* Cache or free any memory we allocated for the contents. */ | |
1361 | if (free_contents != NULL) | |
1362 | { | |
1363 | if (! link_info->keep_memory) | |
1364 | free (free_contents); | |
1365 | else | |
1366 | { | |
1367 | /* Cache the section contents for elf_link_input_bfd. */ | |
1368 | elf_section_data (section)->this_hdr.contents = contents; | |
1369 | } | |
1370 | free_contents = NULL; | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | /* Cache or free any memory we allocated for the symbols. */ | |
1375 | if (free_extsyms != NULL) | |
1376 | { | |
1377 | if (! link_info->keep_memory) | |
1378 | free (free_extsyms); | |
1379 | else | |
1380 | { | |
1381 | /* Cache the symbols for elf_link_input_bfd. */ | |
973ffd63 | 1382 | symtab_hdr->contents = (unsigned char *) extsyms; |
252b5132 RH |
1383 | } |
1384 | free_extsyms = NULL; | |
1385 | } | |
1386 | } | |
1387 | } | |
1388 | ||
252b5132 RH |
1389 | /* (Re)initialize for the basic instruction shortening/relaxing pass. */ |
1390 | contents = NULL; | |
1391 | extsyms = NULL; | |
1392 | internal_relocs = NULL; | |
1393 | free_relocs = NULL; | |
1394 | free_contents = NULL; | |
1395 | free_extsyms = NULL; | |
1396 | ||
1397 | /* We don't have to do anything for a relocateable link, if | |
1398 | this section does not have relocs, or if this is not a | |
1399 | code section. */ | |
1400 | if (link_info->relocateable | |
1401 | || (sec->flags & SEC_RELOC) == 0 | |
1402 | || sec->reloc_count == 0 | |
1403 | || (sec->flags & SEC_CODE) == 0) | |
1404 | return true; | |
1405 | ||
1406 | /* If this is the first time we have been called for this section, | |
1407 | initialize the cooked size. */ | |
1408 | if (sec->_cooked_size == 0) | |
1409 | sec->_cooked_size = sec->_raw_size; | |
1410 | ||
1411 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1412 | ||
1413 | /* Get a copy of the native relocations. */ | |
1414 | internal_relocs = (_bfd_elf32_link_read_relocs | |
1415 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
1416 | link_info->keep_memory)); | |
1417 | if (internal_relocs == NULL) | |
1418 | goto error_return; | |
1419 | if (! link_info->keep_memory) | |
1420 | free_relocs = internal_relocs; | |
1421 | ||
1422 | /* Walk through them looking for relaxing opportunities. */ | |
1423 | irelend = internal_relocs + sec->reloc_count; | |
1424 | for (irel = internal_relocs; irel < irelend; irel++) | |
1425 | { | |
1426 | bfd_vma symval; | |
1427 | struct elf32_mn10300_link_hash_entry *h = NULL; | |
1428 | ||
1429 | /* If this isn't something that can be relaxed, then ignore | |
1430 | this reloc. */ | |
1431 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE | |
1432 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8 | |
1433 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX) | |
1434 | continue; | |
1435 | ||
1436 | /* Get the section contents if we haven't done so already. */ | |
1437 | if (contents == NULL) | |
1438 | { | |
1439 | /* Get cached copy if it exists. */ | |
1440 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
1441 | contents = elf_section_data (sec)->this_hdr.contents; | |
1442 | else | |
1443 | { | |
1444 | /* Go get them off disk. */ | |
1445 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
1446 | if (contents == NULL) | |
1447 | goto error_return; | |
1448 | free_contents = contents; | |
1449 | ||
1450 | if (! bfd_get_section_contents (abfd, sec, contents, | |
1451 | (file_ptr) 0, sec->_raw_size)) | |
1452 | goto error_return; | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | /* Read this BFD's symbols if we haven't done so already. */ | |
1457 | if (extsyms == NULL) | |
1458 | { | |
1459 | /* Get cached copy if it exists. */ | |
1460 | if (symtab_hdr->contents != NULL) | |
1461 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
1462 | else | |
1463 | { | |
dc810e39 | 1464 | bfd_size_type amt = symtab_hdr->sh_size; |
252b5132 | 1465 | /* Go get them off disk. */ |
dc810e39 | 1466 | extsyms = (Elf32_External_Sym *) bfd_malloc (amt); |
252b5132 RH |
1467 | if (extsyms == NULL) |
1468 | goto error_return; | |
1469 | free_extsyms = extsyms; | |
1470 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
dc810e39 | 1471 | || bfd_bread (extsyms, amt, abfd) != amt) |
252b5132 RH |
1472 | goto error_return; |
1473 | } | |
1474 | } | |
1475 | ||
1476 | /* Get the value of the symbol referred to by the reloc. */ | |
1477 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
1478 | { | |
1479 | Elf_Internal_Sym isym; | |
86033394 | 1480 | asection *sym_sec = NULL; |
252b5132 RH |
1481 | const char *sym_name; |
1482 | char *new_name; | |
1483 | ||
1484 | /* A local symbol. */ | |
1485 | bfd_elf32_swap_symbol_in (abfd, | |
1486 | extsyms + ELF32_R_SYM (irel->r_info), | |
1487 | &isym); | |
1488 | ||
1489 | if (isym.st_shndx == SHN_UNDEF) | |
1490 | sym_sec = bfd_und_section_ptr; | |
1491 | else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE) | |
1492 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); | |
1493 | else if (isym.st_shndx == SHN_ABS) | |
1494 | sym_sec = bfd_abs_section_ptr; | |
1495 | else if (isym.st_shndx == SHN_COMMON) | |
1496 | sym_sec = bfd_com_section_ptr; | |
86033394 NC |
1497 | else |
1498 | abort (); | |
a7c10850 | 1499 | |
252b5132 RH |
1500 | symval = (isym.st_value |
1501 | + sym_sec->output_section->vma | |
1502 | + sym_sec->output_offset); | |
1503 | sym_name = bfd_elf_string_from_elf_section (abfd, | |
1504 | symtab_hdr->sh_link, | |
1505 | isym.st_name); | |
1506 | ||
1507 | /* Tack on an ID so we can uniquely identify this | |
1508 | local symbol in the global hash table. */ | |
dc810e39 | 1509 | new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10); |
252b5132 RH |
1510 | if (new_name == 0) |
1511 | goto error_return; | |
010ac81f | 1512 | sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec); |
252b5132 RH |
1513 | sym_name = new_name; |
1514 | ||
1515 | h = (struct elf32_mn10300_link_hash_entry *) | |
1516 | elf_link_hash_lookup (&hash_table->static_hash_table->root, | |
1517 | sym_name, false, false, false); | |
1518 | free (new_name); | |
1519 | } | |
1520 | else | |
1521 | { | |
1522 | unsigned long indx; | |
1523 | ||
1524 | /* An external symbol. */ | |
1525 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
1526 | h = (struct elf32_mn10300_link_hash_entry *) | |
1527 | (elf_sym_hashes (abfd)[indx]); | |
1528 | BFD_ASSERT (h != NULL); | |
1529 | if (h->root.root.type != bfd_link_hash_defined | |
1530 | && h->root.root.type != bfd_link_hash_defweak) | |
1531 | { | |
1532 | /* This appears to be a reference to an undefined | |
1533 | symbol. Just ignore it--it will be caught by the | |
1534 | regular reloc processing. */ | |
1535 | continue; | |
1536 | } | |
1537 | ||
1538 | symval = (h->root.root.u.def.value | |
1539 | + h->root.root.u.def.section->output_section->vma | |
1540 | + h->root.root.u.def.section->output_offset); | |
1541 | } | |
1542 | ||
1543 | /* For simplicity of coding, we are going to modify the section | |
1544 | contents, the section relocs, and the BFD symbol table. We | |
1545 | must tell the rest of the code not to free up this | |
1546 | information. It would be possible to instead create a table | |
1547 | of changes which have to be made, as is done in coff-mips.c; | |
1548 | that would be more work, but would require less memory when | |
1549 | the linker is run. */ | |
1550 | ||
1551 | /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative | |
1552 | branch/call, also deal with "call" -> "calls" conversions and | |
1553 | insertion of prologue data into "call" instructions. */ | |
1554 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32) | |
1555 | { | |
1556 | bfd_vma value = symval; | |
1557 | ||
1558 | /* If we've got a "call" instruction that needs to be turned | |
1559 | into a "calls" instruction, do so now. It saves a byte. */ | |
1560 | if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS)) | |
1561 | { | |
1562 | unsigned char code; | |
1563 | ||
1564 | /* Get the opcode. */ | |
1565 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1566 | ||
1567 | /* Make sure we're working with a "call" instruction! */ | |
1568 | if (code == 0xdd) | |
1569 | { | |
1570 | /* Note that we've changed the relocs, section contents, | |
1571 | etc. */ | |
1572 | elf_section_data (sec)->relocs = internal_relocs; | |
1573 | free_relocs = NULL; | |
1574 | ||
1575 | elf_section_data (sec)->this_hdr.contents = contents; | |
1576 | free_contents = NULL; | |
1577 | ||
1578 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
1579 | free_extsyms = NULL; | |
1580 | ||
1581 | /* Fix the opcode. */ | |
1582 | bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1); | |
1583 | bfd_put_8 (abfd, 0xff, contents + irel->r_offset); | |
1584 | ||
1585 | /* Fix irel->r_offset and irel->r_addend. */ | |
1586 | irel->r_offset += 1; | |
1587 | irel->r_addend += 1; | |
1588 | ||
1589 | /* Delete one byte of data. */ | |
1590 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
1591 | irel->r_offset + 3, 1)) | |
1592 | goto error_return; | |
1593 | ||
1594 | /* That will change things, so, we should relax again. | |
1595 | Note that this is not required, and it may be slow. */ | |
1596 | *again = true; | |
1597 | } | |
1598 | } | |
1599 | else if (h) | |
1600 | { | |
1601 | /* We've got a "call" instruction which needs some data | |
1602 | from target function filled in. */ | |
1603 | unsigned char code; | |
1604 | ||
1605 | /* Get the opcode. */ | |
1606 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1607 | ||
1608 | /* Insert data from the target function into the "call" | |
1609 | instruction if needed. */ | |
1610 | if (code == 0xdd) | |
1611 | { | |
1612 | bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4); | |
1613 | bfd_put_8 (abfd, h->stack_size + h->movm_stack_size, | |
1614 | contents + irel->r_offset + 5); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | /* Deal with pc-relative gunk. */ | |
1619 | value -= (sec->output_section->vma + sec->output_offset); | |
1620 | value -= irel->r_offset; | |
1621 | value += irel->r_addend; | |
1622 | ||
1623 | /* See if the value will fit in 16 bits, note the high value is | |
1624 | 0x7fff + 2 as the target will be two bytes closer if we are | |
1625 | able to relax. */ | |
010ac81f | 1626 | if ((long) value < 0x8001 && (long) value > -0x8000) |
252b5132 RH |
1627 | { |
1628 | unsigned char code; | |
1629 | ||
1630 | /* Get the opcode. */ | |
1631 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1632 | ||
1633 | if (code != 0xdc && code != 0xdd && code != 0xff) | |
1634 | continue; | |
1635 | ||
1636 | /* Note that we've changed the relocs, section contents, etc. */ | |
1637 | elf_section_data (sec)->relocs = internal_relocs; | |
1638 | free_relocs = NULL; | |
1639 | ||
1640 | elf_section_data (sec)->this_hdr.contents = contents; | |
1641 | free_contents = NULL; | |
1642 | ||
1643 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
1644 | free_extsyms = NULL; | |
1645 | ||
1646 | /* Fix the opcode. */ | |
1647 | if (code == 0xdc) | |
1648 | bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1); | |
1649 | else if (code == 0xdd) | |
1650 | bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1); | |
1651 | else if (code == 0xff) | |
1652 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); | |
1653 | ||
1654 | /* Fix the relocation's type. */ | |
1655 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1656 | R_MN10300_PCREL16); | |
1657 | ||
1658 | /* Delete two bytes of data. */ | |
1659 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
1660 | irel->r_offset + 1, 2)) | |
1661 | goto error_return; | |
1662 | ||
1663 | /* That will change things, so, we should relax again. | |
1664 | Note that this is not required, and it may be slow. */ | |
1665 | *again = true; | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative | |
1670 | branch. */ | |
1671 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16) | |
1672 | { | |
1673 | bfd_vma value = symval; | |
1674 | ||
1675 | /* If we've got a "call" instruction that needs to be turned | |
1676 | into a "calls" instruction, do so now. It saves a byte. */ | |
1677 | if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS)) | |
1678 | { | |
1679 | unsigned char code; | |
1680 | ||
1681 | /* Get the opcode. */ | |
1682 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1683 | ||
1684 | /* Make sure we're working with a "call" instruction! */ | |
1685 | if (code == 0xcd) | |
1686 | { | |
1687 | /* Note that we've changed the relocs, section contents, | |
1688 | etc. */ | |
1689 | elf_section_data (sec)->relocs = internal_relocs; | |
1690 | free_relocs = NULL; | |
1691 | ||
1692 | elf_section_data (sec)->this_hdr.contents = contents; | |
1693 | free_contents = NULL; | |
1694 | ||
1695 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
1696 | free_extsyms = NULL; | |
1697 | ||
1698 | /* Fix the opcode. */ | |
1699 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1); | |
1700 | bfd_put_8 (abfd, 0xff, contents + irel->r_offset); | |
1701 | ||
1702 | /* Fix irel->r_offset and irel->r_addend. */ | |
1703 | irel->r_offset += 1; | |
1704 | irel->r_addend += 1; | |
1705 | ||
1706 | /* Delete one byte of data. */ | |
1707 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
1708 | irel->r_offset + 1, 1)) | |
1709 | goto error_return; | |
1710 | ||
1711 | /* That will change things, so, we should relax again. | |
1712 | Note that this is not required, and it may be slow. */ | |
1713 | *again = true; | |
1714 | } | |
1715 | } | |
1716 | else if (h) | |
1717 | { | |
1718 | unsigned char code; | |
1719 | ||
1720 | /* Get the opcode. */ | |
1721 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1722 | ||
1723 | /* Insert data from the target function into the "call" | |
1724 | instruction if needed. */ | |
1725 | if (code == 0xcd) | |
1726 | { | |
1727 | bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2); | |
1728 | bfd_put_8 (abfd, h->stack_size + h->movm_stack_size, | |
1729 | contents + irel->r_offset + 3); | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | /* Deal with pc-relative gunk. */ | |
1734 | value -= (sec->output_section->vma + sec->output_offset); | |
1735 | value -= irel->r_offset; | |
1736 | value += irel->r_addend; | |
1737 | ||
1738 | /* See if the value will fit in 8 bits, note the high value is | |
1739 | 0x7f + 1 as the target will be one bytes closer if we are | |
1740 | able to relax. */ | |
010ac81f | 1741 | if ((long) value < 0x80 && (long) value > -0x80) |
252b5132 RH |
1742 | { |
1743 | unsigned char code; | |
1744 | ||
1745 | /* Get the opcode. */ | |
1746 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1747 | ||
1748 | if (code != 0xcc) | |
1749 | continue; | |
1750 | ||
1751 | /* Note that we've changed the relocs, section contents, etc. */ | |
1752 | elf_section_data (sec)->relocs = internal_relocs; | |
1753 | free_relocs = NULL; | |
1754 | ||
1755 | elf_section_data (sec)->this_hdr.contents = contents; | |
1756 | free_contents = NULL; | |
1757 | ||
1758 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
1759 | free_extsyms = NULL; | |
1760 | ||
1761 | /* Fix the opcode. */ | |
1762 | bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1); | |
1763 | ||
1764 | /* Fix the relocation's type. */ | |
1765 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1766 | R_MN10300_PCREL8); | |
1767 | ||
1768 | /* Delete one byte of data. */ | |
1769 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
1770 | irel->r_offset + 1, 1)) | |
1771 | goto error_return; | |
1772 | ||
1773 | /* That will change things, so, we should relax again. | |
1774 | Note that this is not required, and it may be slow. */ | |
1775 | *again = true; | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | /* Try to eliminate an unconditional 8 bit pc-relative branch | |
1780 | which immediately follows a conditional 8 bit pc-relative | |
1781 | branch around the unconditional branch. | |
1782 | ||
1783 | original: new: | |
1784 | bCC lab1 bCC' lab2 | |
1785 | bra lab2 | |
1786 | lab1: lab1: | |
1787 | ||
252b5132 RH |
1788 | This happens when the bCC can't reach lab2 at assembly time, |
1789 | but due to other relaxations it can reach at link time. */ | |
1790 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8) | |
1791 | { | |
1792 | Elf_Internal_Rela *nrel; | |
1793 | bfd_vma value = symval; | |
1794 | unsigned char code; | |
1795 | ||
1796 | /* Deal with pc-relative gunk. */ | |
1797 | value -= (sec->output_section->vma + sec->output_offset); | |
1798 | value -= irel->r_offset; | |
1799 | value += irel->r_addend; | |
1800 | ||
1801 | /* Do nothing if this reloc is the last byte in the section. */ | |
1802 | if (irel->r_offset == sec->_cooked_size) | |
1803 | continue; | |
1804 | ||
1805 | /* See if the next instruction is an unconditional pc-relative | |
1806 | branch, more often than not this test will fail, so we | |
1807 | test it first to speed things up. */ | |
1808 | code = bfd_get_8 (abfd, contents + irel->r_offset + 1); | |
1809 | if (code != 0xca) | |
1810 | continue; | |
1811 | ||
1812 | /* Also make sure the next relocation applies to the next | |
1813 | instruction and that it's a pc-relative 8 bit branch. */ | |
1814 | nrel = irel + 1; | |
1815 | if (nrel == irelend | |
1816 | || irel->r_offset + 2 != nrel->r_offset | |
1817 | || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8) | |
1818 | continue; | |
1819 | ||
1820 | /* Make sure our destination immediately follows the | |
1821 | unconditional branch. */ | |
1822 | if (symval != (sec->output_section->vma + sec->output_offset | |
1823 | + irel->r_offset + 3)) | |
1824 | continue; | |
1825 | ||
1826 | /* Now make sure we are a conditional branch. This may not | |
1827 | be necessary, but why take the chance. | |
1828 | ||
1829 | Note these checks assume that R_MN10300_PCREL8 relocs | |
1830 | only occur on bCC and bCCx insns. If they occured | |
1831 | elsewhere, we'd need to know the start of this insn | |
1832 | for this check to be accurate. */ | |
1833 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1834 | if (code != 0xc0 && code != 0xc1 && code != 0xc2 | |
1835 | && code != 0xc3 && code != 0xc4 && code != 0xc5 | |
1836 | && code != 0xc6 && code != 0xc7 && code != 0xc8 | |
1837 | && code != 0xc9 && code != 0xe8 && code != 0xe9 | |
1838 | && code != 0xea && code != 0xeb) | |
1839 | continue; | |
1840 | ||
1841 | /* We also have to be sure there is no symbol/label | |
1842 | at the unconditional branch. */ | |
1843 | if (mn10300_elf_symbol_address_p (abfd, sec, extsyms, | |
1844 | irel->r_offset + 1)) | |
1845 | continue; | |
1846 | ||
1847 | /* Note that we've changed the relocs, section contents, etc. */ | |
1848 | elf_section_data (sec)->relocs = internal_relocs; | |
1849 | free_relocs = NULL; | |
1850 | ||
1851 | elf_section_data (sec)->this_hdr.contents = contents; | |
1852 | free_contents = NULL; | |
1853 | ||
1854 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
1855 | free_extsyms = NULL; | |
1856 | ||
1857 | /* Reverse the condition of the first branch. */ | |
1858 | switch (code) | |
1859 | { | |
010ac81f KH |
1860 | case 0xc8: |
1861 | code = 0xc9; | |
1862 | break; | |
1863 | case 0xc9: | |
1864 | code = 0xc8; | |
1865 | break; | |
1866 | case 0xc0: | |
1867 | code = 0xc2; | |
1868 | break; | |
1869 | case 0xc2: | |
1870 | code = 0xc0; | |
1871 | break; | |
1872 | case 0xc3: | |
1873 | code = 0xc1; | |
1874 | break; | |
1875 | case 0xc1: | |
1876 | code = 0xc3; | |
1877 | break; | |
1878 | case 0xc4: | |
1879 | code = 0xc6; | |
1880 | break; | |
1881 | case 0xc6: | |
1882 | code = 0xc4; | |
1883 | break; | |
1884 | case 0xc7: | |
1885 | code = 0xc5; | |
1886 | break; | |
1887 | case 0xc5: | |
1888 | code = 0xc7; | |
1889 | break; | |
1890 | case 0xe8: | |
1891 | code = 0xe9; | |
1892 | break; | |
1893 | case 0x9d: | |
1894 | code = 0xe8; | |
1895 | break; | |
1896 | case 0xea: | |
1897 | code = 0xeb; | |
1898 | break; | |
1899 | case 0xeb: | |
1900 | code = 0xea; | |
1901 | break; | |
252b5132 RH |
1902 | } |
1903 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
1904 | ||
1905 | /* Set the reloc type and symbol for the first branch | |
1906 | from the second branch. */ | |
1907 | irel->r_info = nrel->r_info; | |
1908 | ||
1909 | /* Make the reloc for the second branch a null reloc. */ | |
1910 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), | |
1911 | R_MN10300_NONE); | |
1912 | ||
1913 | /* Delete two bytes of data. */ | |
1914 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
1915 | irel->r_offset + 1, 2)) | |
1916 | goto error_return; | |
1917 | ||
1918 | /* That will change things, so, we should relax again. | |
1919 | Note that this is not required, and it may be slow. */ | |
1920 | *again = true; | |
1921 | } | |
1922 | ||
31f8dc8f JL |
1923 | /* Try to turn a 24 immediate, displacement or absolute address |
1924 | into a 8 immediate, displacement or absolute address. */ | |
1925 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24) | |
1926 | { | |
1927 | bfd_vma value = symval; | |
1928 | value += irel->r_addend; | |
1929 | ||
1930 | /* See if the value will fit in 8 bits. */ | |
010ac81f | 1931 | if ((long) value < 0x7f && (long) value > -0x80) |
31f8dc8f JL |
1932 | { |
1933 | unsigned char code; | |
1934 | ||
1935 | /* AM33 insns which have 24 operands are 6 bytes long and | |
1936 | will have 0xfd as the first byte. */ | |
1937 | ||
1938 | /* Get the first opcode. */ | |
1939 | code = bfd_get_8 (abfd, contents + irel->r_offset - 3); | |
1940 | ||
1941 | if (code == 0xfd) | |
1942 | { | |
010ac81f KH |
1943 | /* Get the second opcode. */ |
1944 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
31f8dc8f JL |
1945 | |
1946 | /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit | |
1947 | equivalent instructions exists. */ | |
1948 | if (code != 0x6b && code != 0x7b | |
1949 | && code != 0x8b && code != 0x9b | |
1950 | && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08 | |
1951 | || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b | |
1952 | || (code & 0x0f) == 0x0e)) | |
1953 | { | |
1954 | /* Not safe if the high bit is on as relaxing may | |
1955 | move the value out of high mem and thus not fit | |
1956 | in a signed 8bit value. This is currently over | |
1957 | conservative. */ | |
1958 | if ((value & 0x80) == 0) | |
1959 | { | |
1960 | /* Note that we've changed the relocation contents, | |
1961 | etc. */ | |
1962 | elf_section_data (sec)->relocs = internal_relocs; | |
1963 | free_relocs = NULL; | |
1964 | ||
1965 | elf_section_data (sec)->this_hdr.contents = contents; | |
1966 | free_contents = NULL; | |
1967 | ||
1968 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
1969 | free_extsyms = NULL; | |
1970 | ||
1971 | /* Fix the opcode. */ | |
1972 | bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3); | |
1973 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
1974 | ||
1975 | /* Fix the relocation's type. */ | |
010ac81f KH |
1976 | irel->r_info = |
1977 | ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1978 | R_MN10300_8); | |
31f8dc8f JL |
1979 | |
1980 | /* Delete two bytes of data. */ | |
1981 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
1982 | irel->r_offset + 1, 2)) | |
1983 | goto error_return; | |
1984 | ||
1985 | /* That will change things, so, we should relax | |
1986 | again. Note that this is not required, and it | |
010ac81f | 1987 | may be slow. */ |
31f8dc8f JL |
1988 | *again = true; |
1989 | break; | |
1990 | } | |
1991 | } | |
31f8dc8f JL |
1992 | } |
1993 | } | |
1994 | } | |
252b5132 RH |
1995 | |
1996 | /* Try to turn a 32bit immediate, displacement or absolute address | |
1997 | into a 16bit immediate, displacement or absolute address. */ | |
1998 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32) | |
1999 | { | |
2000 | bfd_vma value = symval; | |
2001 | value += irel->r_addend; | |
2002 | ||
31f8dc8f JL |
2003 | /* See if the value will fit in 24 bits. |
2004 | We allow any 16bit match here. We prune those we can't | |
2005 | handle below. */ | |
010ac81f | 2006 | if ((long) value < 0x7fffff && (long) value > -0x800000) |
31f8dc8f JL |
2007 | { |
2008 | unsigned char code; | |
2009 | ||
2010 | /* AM33 insns which have 32bit operands are 7 bytes long and | |
2011 | will have 0xfe as the first byte. */ | |
2012 | ||
2013 | /* Get the first opcode. */ | |
2014 | code = bfd_get_8 (abfd, contents + irel->r_offset - 3); | |
2015 | ||
2016 | if (code == 0xfe) | |
2017 | { | |
2018 | /* Get the second opcode. */ | |
2019 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
2020 | ||
2021 | /* All the am33 32 -> 24 relaxing possibilities. */ | |
2022 | /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit | |
2023 | equivalent instructions exists. */ | |
010ac81f | 2024 | if (code != 0x6b && code != 0x7b |
31f8dc8f JL |
2025 | && code != 0x8b && code != 0x9b |
2026 | && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08 | |
2027 | || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b | |
2028 | || (code & 0x0f) == 0x0e)) | |
2029 | { | |
2030 | /* Not safe if the high bit is on as relaxing may | |
2031 | move the value out of high mem and thus not fit | |
2032 | in a signed 16bit value. This is currently over | |
2033 | conservative. */ | |
2034 | if ((value & 0x8000) == 0) | |
2035 | { | |
2036 | /* Note that we've changed the relocation contents, | |
2037 | etc. */ | |
2038 | elf_section_data (sec)->relocs = internal_relocs; | |
2039 | free_relocs = NULL; | |
2040 | ||
2041 | elf_section_data (sec)->this_hdr.contents = contents; | |
2042 | free_contents = NULL; | |
2043 | ||
2044 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2045 | free_extsyms = NULL; | |
2046 | ||
2047 | /* Fix the opcode. */ | |
2048 | bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3); | |
2049 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
2050 | ||
2051 | /* Fix the relocation's type. */ | |
010ac81f KH |
2052 | irel->r_info = |
2053 | ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2054 | R_MN10300_24); | |
31f8dc8f JL |
2055 | |
2056 | /* Delete one byte of data. */ | |
2057 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2058 | irel->r_offset + 3, 1)) | |
2059 | goto error_return; | |
2060 | ||
2061 | /* That will change things, so, we should relax | |
2062 | again. Note that this is not required, and it | |
010ac81f | 2063 | may be slow. */ |
31f8dc8f JL |
2064 | *again = true; |
2065 | break; | |
2066 | } | |
2067 | } | |
31f8dc8f JL |
2068 | } |
2069 | } | |
252b5132 RH |
2070 | |
2071 | /* See if the value will fit in 16 bits. | |
2072 | We allow any 16bit match here. We prune those we can't | |
2073 | handle below. */ | |
010ac81f | 2074 | if ((long) value < 0x7fff && (long) value > -0x8000) |
252b5132 RH |
2075 | { |
2076 | unsigned char code; | |
2077 | ||
2078 | /* Most insns which have 32bit operands are 6 bytes long; | |
2079 | exceptions are pcrel insns and bit insns. | |
2080 | ||
2081 | We handle pcrel insns above. We don't bother trying | |
2082 | to handle the bit insns here. | |
2083 | ||
2084 | The first byte of the remaining insns will be 0xfc. */ | |
2085 | ||
2086 | /* Get the first opcode. */ | |
2087 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
2088 | ||
2089 | if (code != 0xfc) | |
2090 | continue; | |
2091 | ||
2092 | /* Get the second opcode. */ | |
2093 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
2094 | ||
2095 | if ((code & 0xf0) < 0x80) | |
2096 | switch (code & 0xf0) | |
2097 | { | |
2098 | /* mov (d32,am),dn -> mov (d32,am),dn | |
2099 | mov dm,(d32,am) -> mov dn,(d32,am) | |
2100 | mov (d32,am),an -> mov (d32,am),an | |
2101 | mov dm,(d32,am) -> mov dn,(d32,am) | |
2102 | movbu (d32,am),dn -> movbu (d32,am),dn | |
2103 | movbu dm,(d32,am) -> movbu dn,(d32,am) | |
2104 | movhu (d32,am),dn -> movhu (d32,am),dn | |
2105 | movhu dm,(d32,am) -> movhu dn,(d32,am) */ | |
2106 | case 0x00: | |
2107 | case 0x10: | |
2108 | case 0x20: | |
2109 | case 0x30: | |
2110 | case 0x40: | |
2111 | case 0x50: | |
2112 | case 0x60: | |
2113 | case 0x70: | |
2114 | /* Not safe if the high bit is on as relaxing may | |
2115 | move the value out of high mem and thus not fit | |
2116 | in a signed 16bit value. */ | |
2117 | if (code == 0xcc | |
2118 | && (value & 0x8000)) | |
2119 | continue; | |
2120 | ||
2121 | /* Note that we've changed the relocation contents, etc. */ | |
2122 | elf_section_data (sec)->relocs = internal_relocs; | |
2123 | free_relocs = NULL; | |
2124 | ||
2125 | elf_section_data (sec)->this_hdr.contents = contents; | |
2126 | free_contents = NULL; | |
2127 | ||
2128 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2129 | free_extsyms = NULL; | |
2130 | ||
2131 | /* Fix the opcode. */ | |
2132 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); | |
2133 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
2134 | ||
2135 | /* Fix the relocation's type. */ | |
2136 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2137 | R_MN10300_16); | |
2138 | ||
2139 | /* Delete two bytes of data. */ | |
2140 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2141 | irel->r_offset + 2, 2)) | |
2142 | goto error_return; | |
2143 | ||
2144 | /* That will change things, so, we should relax again. | |
2145 | Note that this is not required, and it may be slow. */ | |
2146 | *again = true; | |
2147 | break; | |
2148 | } | |
2149 | else if ((code & 0xf0) == 0x80 | |
2150 | || (code & 0xf0) == 0x90) | |
2151 | switch (code & 0xf3) | |
2152 | { | |
2153 | /* mov dn,(abs32) -> mov dn,(abs16) | |
2154 | movbu dn,(abs32) -> movbu dn,(abs16) | |
2155 | movhu dn,(abs32) -> movhu dn,(abs16) */ | |
2156 | case 0x81: | |
2157 | case 0x82: | |
2158 | case 0x83: | |
2159 | /* Note that we've changed the relocation contents, etc. */ | |
2160 | elf_section_data (sec)->relocs = internal_relocs; | |
2161 | free_relocs = NULL; | |
2162 | ||
2163 | elf_section_data (sec)->this_hdr.contents = contents; | |
2164 | free_contents = NULL; | |
2165 | ||
2166 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2167 | free_extsyms = NULL; | |
2168 | ||
2169 | if ((code & 0xf3) == 0x81) | |
2170 | code = 0x01 + (code & 0x0c); | |
2171 | else if ((code & 0xf3) == 0x82) | |
2172 | code = 0x02 + (code & 0x0c); | |
2173 | else if ((code & 0xf3) == 0x83) | |
2174 | code = 0x03 + (code & 0x0c); | |
2175 | else | |
2176 | abort (); | |
2177 | ||
2178 | /* Fix the opcode. */ | |
2179 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
2180 | ||
2181 | /* Fix the relocation's type. */ | |
2182 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2183 | R_MN10300_16); | |
2184 | ||
2185 | /* The opcode got shorter too, so we have to fix the | |
2186 | addend and offset too! */ | |
2187 | irel->r_offset -= 1; | |
2188 | ||
2189 | /* Delete three bytes of data. */ | |
2190 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2191 | irel->r_offset + 1, 3)) | |
2192 | goto error_return; | |
2193 | ||
2194 | /* That will change things, so, we should relax again. | |
2195 | Note that this is not required, and it may be slow. */ | |
2196 | *again = true; | |
2197 | break; | |
2198 | ||
2199 | /* mov am,(abs32) -> mov am,(abs16) | |
2200 | mov am,(d32,sp) -> mov am,(d16,sp) | |
2201 | mov dm,(d32,sp) -> mov dm,(d32,sp) | |
2202 | movbu dm,(d32,sp) -> movbu dm,(d32,sp) | |
2203 | movhu dm,(d32,sp) -> movhu dm,(d32,sp) */ | |
2204 | case 0x80: | |
2205 | case 0x90: | |
2206 | case 0x91: | |
2207 | case 0x92: | |
2208 | case 0x93: | |
2a0fa943 AO |
2209 | /* sp-based offsets are zero-extended. */ |
2210 | if (code >= 0x90 && code <= 0x93 | |
2211 | && (long)value < 0) | |
2212 | continue; | |
2213 | ||
252b5132 RH |
2214 | /* Note that we've changed the relocation contents, etc. */ |
2215 | elf_section_data (sec)->relocs = internal_relocs; | |
2216 | free_relocs = NULL; | |
2217 | ||
2218 | elf_section_data (sec)->this_hdr.contents = contents; | |
2219 | free_contents = NULL; | |
2220 | ||
2221 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2222 | free_extsyms = NULL; | |
2223 | ||
2224 | /* Fix the opcode. */ | |
2225 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); | |
2226 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
2227 | ||
2228 | /* Fix the relocation's type. */ | |
2229 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2230 | R_MN10300_16); | |
2231 | ||
2232 | /* Delete two bytes of data. */ | |
2233 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2234 | irel->r_offset + 2, 2)) | |
2235 | goto error_return; | |
2236 | ||
2237 | /* That will change things, so, we should relax again. | |
2238 | Note that this is not required, and it may be slow. */ | |
2239 | *again = true; | |
2240 | break; | |
2241 | } | |
2242 | else if ((code & 0xf0) < 0xf0) | |
2243 | switch (code & 0xfc) | |
2244 | { | |
2245 | /* mov imm32,dn -> mov imm16,dn | |
2246 | mov imm32,an -> mov imm16,an | |
2247 | mov (abs32),dn -> mov (abs16),dn | |
2248 | movbu (abs32),dn -> movbu (abs16),dn | |
2249 | movhu (abs32),dn -> movhu (abs16),dn */ | |
2250 | case 0xcc: | |
2251 | case 0xdc: | |
2252 | case 0xa4: | |
2253 | case 0xa8: | |
2254 | case 0xac: | |
2255 | /* Not safe if the high bit is on as relaxing may | |
2256 | move the value out of high mem and thus not fit | |
2257 | in a signed 16bit value. */ | |
2258 | if (code == 0xcc | |
2259 | && (value & 0x8000)) | |
2260 | continue; | |
2261 | ||
2a0fa943 AO |
2262 | /* mov imm16, an zero-extends the immediate. */ |
2263 | if (code == 0xdc | |
2264 | && (long)value < 0) | |
2265 | continue; | |
2266 | ||
252b5132 RH |
2267 | /* Note that we've changed the relocation contents, etc. */ |
2268 | elf_section_data (sec)->relocs = internal_relocs; | |
2269 | free_relocs = NULL; | |
2270 | ||
2271 | elf_section_data (sec)->this_hdr.contents = contents; | |
2272 | free_contents = NULL; | |
2273 | ||
2274 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2275 | free_extsyms = NULL; | |
2276 | ||
2277 | if ((code & 0xfc) == 0xcc) | |
2278 | code = 0x2c + (code & 0x03); | |
2279 | else if ((code & 0xfc) == 0xdc) | |
2280 | code = 0x24 + (code & 0x03); | |
2281 | else if ((code & 0xfc) == 0xa4) | |
2282 | code = 0x30 + (code & 0x03); | |
2283 | else if ((code & 0xfc) == 0xa8) | |
2284 | code = 0x34 + (code & 0x03); | |
2285 | else if ((code & 0xfc) == 0xac) | |
2286 | code = 0x38 + (code & 0x03); | |
2287 | else | |
2288 | abort (); | |
2289 | ||
2290 | /* Fix the opcode. */ | |
2291 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
2292 | ||
2293 | /* Fix the relocation's type. */ | |
2294 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2295 | R_MN10300_16); | |
2296 | ||
2297 | /* The opcode got shorter too, so we have to fix the | |
2298 | addend and offset too! */ | |
2299 | irel->r_offset -= 1; | |
2300 | ||
2301 | /* Delete three bytes of data. */ | |
2302 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2303 | irel->r_offset + 1, 3)) | |
2304 | goto error_return; | |
2305 | ||
2306 | /* That will change things, so, we should relax again. | |
2307 | Note that this is not required, and it may be slow. */ | |
2308 | *again = true; | |
2309 | break; | |
2310 | ||
2311 | /* mov (abs32),an -> mov (abs16),an | |
2a0fa943 AO |
2312 | mov (d32,sp),an -> mov (d16,sp),an |
2313 | mov (d32,sp),dn -> mov (d16,sp),dn | |
2314 | movbu (d32,sp),dn -> movbu (d16,sp),dn | |
2315 | movhu (d32,sp),dn -> movhu (d16,sp),dn | |
252b5132 RH |
2316 | add imm32,dn -> add imm16,dn |
2317 | cmp imm32,dn -> cmp imm16,dn | |
2318 | add imm32,an -> add imm16,an | |
2319 | cmp imm32,an -> cmp imm16,an | |
2a0fa943 AO |
2320 | and imm32,dn -> and imm16,dn |
2321 | or imm32,dn -> or imm16,dn | |
2322 | xor imm32,dn -> xor imm16,dn | |
2323 | btst imm32,dn -> btst imm16,dn */ | |
252b5132 RH |
2324 | |
2325 | case 0xa0: | |
2326 | case 0xb0: | |
2327 | case 0xb1: | |
2328 | case 0xb2: | |
2329 | case 0xb3: | |
2330 | case 0xc0: | |
2331 | case 0xc8: | |
2332 | ||
2333 | case 0xd0: | |
2334 | case 0xd8: | |
2335 | case 0xe0: | |
2336 | case 0xe1: | |
2337 | case 0xe2: | |
2338 | case 0xe3: | |
2a0fa943 AO |
2339 | /* cmp imm16, an zero-extends the immediate. */ |
2340 | if (code == 0xdc | |
2341 | && (long)value < 0) | |
2342 | continue; | |
2343 | ||
2344 | /* So do sp-based offsets. */ | |
2345 | if (code >= 0xb0 && code <= 0xb3 | |
2346 | && (long)value < 0) | |
2347 | continue; | |
2348 | ||
252b5132 RH |
2349 | /* Note that we've changed the relocation contents, etc. */ |
2350 | elf_section_data (sec)->relocs = internal_relocs; | |
2351 | free_relocs = NULL; | |
2352 | ||
2353 | elf_section_data (sec)->this_hdr.contents = contents; | |
2354 | free_contents = NULL; | |
2355 | ||
2356 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2357 | free_extsyms = NULL; | |
2358 | ||
2359 | /* Fix the opcode. */ | |
2360 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); | |
2361 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
2362 | ||
2363 | /* Fix the relocation's type. */ | |
2364 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2365 | R_MN10300_16); | |
2366 | ||
2367 | /* Delete two bytes of data. */ | |
2368 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2369 | irel->r_offset + 2, 2)) | |
2370 | goto error_return; | |
2371 | ||
2372 | /* That will change things, so, we should relax again. | |
2373 | Note that this is not required, and it may be slow. */ | |
2374 | *again = true; | |
2375 | break; | |
2376 | } | |
2377 | else if (code == 0xfe) | |
2378 | { | |
2379 | /* add imm32,sp -> add imm16,sp */ | |
2380 | ||
2381 | /* Note that we've changed the relocation contents, etc. */ | |
2382 | elf_section_data (sec)->relocs = internal_relocs; | |
2383 | free_relocs = NULL; | |
2384 | ||
2385 | elf_section_data (sec)->this_hdr.contents = contents; | |
2386 | free_contents = NULL; | |
2387 | ||
2388 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
2389 | free_extsyms = NULL; | |
2390 | ||
2391 | /* Fix the opcode. */ | |
2392 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); | |
2393 | bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1); | |
2394 | ||
2395 | /* Fix the relocation's type. */ | |
2396 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
010ac81f | 2397 | R_MN10300_16); |
252b5132 RH |
2398 | |
2399 | /* Delete two bytes of data. */ | |
2400 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, | |
2401 | irel->r_offset + 2, 2)) | |
2402 | goto error_return; | |
2403 | ||
2404 | /* That will change things, so, we should relax again. | |
2405 | Note that this is not required, and it may be slow. */ | |
2406 | *again = true; | |
2407 | break; | |
2408 | } | |
2409 | } | |
2410 | } | |
2411 | } | |
2412 | ||
2413 | if (free_relocs != NULL) | |
2414 | { | |
2415 | free (free_relocs); | |
2416 | free_relocs = NULL; | |
2417 | } | |
2418 | ||
2419 | if (free_contents != NULL) | |
2420 | { | |
2421 | if (! link_info->keep_memory) | |
2422 | free (free_contents); | |
2423 | else | |
2424 | { | |
2425 | /* Cache the section contents for elf_link_input_bfd. */ | |
2426 | elf_section_data (sec)->this_hdr.contents = contents; | |
2427 | } | |
2428 | free_contents = NULL; | |
2429 | } | |
2430 | ||
2431 | if (free_extsyms != NULL) | |
2432 | { | |
2433 | if (! link_info->keep_memory) | |
2434 | free (free_extsyms); | |
2435 | else | |
2436 | { | |
2437 | /* Cache the symbols for elf_link_input_bfd. */ | |
973ffd63 | 2438 | symtab_hdr->contents = (unsigned char *) extsyms; |
252b5132 RH |
2439 | } |
2440 | free_extsyms = NULL; | |
2441 | } | |
2442 | ||
2443 | return true; | |
2444 | ||
2445 | error_return: | |
2446 | if (free_relocs != NULL) | |
2447 | free (free_relocs); | |
2448 | if (free_contents != NULL) | |
2449 | free (free_contents); | |
2450 | if (free_extsyms != NULL) | |
2451 | free (free_extsyms); | |
2452 | return false; | |
2453 | } | |
2454 | ||
2455 | /* Compute the stack size and movm arguments for the function | |
2456 | referred to by HASH at address ADDR in section with | |
2457 | contents CONTENTS, store the information in the hash table. */ | |
2458 | static void | |
2459 | compute_function_info (abfd, hash, addr, contents) | |
2460 | bfd *abfd; | |
2461 | struct elf32_mn10300_link_hash_entry *hash; | |
2462 | bfd_vma addr; | |
2463 | unsigned char *contents; | |
2464 | { | |
2465 | unsigned char byte1, byte2; | |
2466 | /* We only care about a very small subset of the possible prologue | |
2467 | sequences here. Basically we look for: | |
2468 | ||
2469 | movm [d2,d3,a2,a3],sp (optional) | |
2470 | add <size>,sp (optional, and only for sizes which fit in an unsigned | |
2471 | 8 bit number) | |
2472 | ||
2473 | If we find anything else, we quit. */ | |
2474 | ||
2475 | /* Look for movm [regs],sp */ | |
2476 | byte1 = bfd_get_8 (abfd, contents + addr); | |
2477 | byte2 = bfd_get_8 (abfd, contents + addr + 1); | |
2478 | ||
2479 | if (byte1 == 0xcf) | |
2480 | { | |
2481 | hash->movm_args = byte2; | |
2482 | addr += 2; | |
2483 | byte1 = bfd_get_8 (abfd, contents + addr); | |
2484 | byte2 = bfd_get_8 (abfd, contents + addr + 1); | |
2485 | } | |
2486 | ||
2487 | /* Now figure out how much stack space will be allocated by the movm | |
2488 | instruction. We need this kept separate from the funtion's normal | |
2489 | stack space. */ | |
2490 | if (hash->movm_args) | |
2491 | { | |
2492 | /* Space for d2. */ | |
2493 | if (hash->movm_args & 0x80) | |
2494 | hash->movm_stack_size += 4; | |
2495 | ||
2496 | /* Space for d3. */ | |
2497 | if (hash->movm_args & 0x40) | |
2498 | hash->movm_stack_size += 4; | |
2499 | ||
2500 | /* Space for a2. */ | |
2501 | if (hash->movm_args & 0x20) | |
2502 | hash->movm_stack_size += 4; | |
2503 | ||
2504 | /* Space for a3. */ | |
2505 | if (hash->movm_args & 0x10) | |
2506 | hash->movm_stack_size += 4; | |
2507 | ||
2508 | /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */ | |
2509 | if (hash->movm_args & 0x08) | |
2510 | hash->movm_stack_size += 8 * 4; | |
2511 | ||
31f8dc8f JL |
2512 | if (bfd_get_mach (abfd) == bfd_mach_am33) |
2513 | { | |
2514 | /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */ | |
2515 | if (hash->movm_args & 0x1) | |
2516 | hash->movm_stack_size += 6 * 4; | |
2517 | ||
2518 | /* exreg1 space. e4, e5, e6, e7 */ | |
2519 | if (hash->movm_args & 0x2) | |
2520 | hash->movm_stack_size += 4 * 4; | |
2521 | ||
2522 | /* exreg0 space. e2, e3 */ | |
2523 | if (hash->movm_args & 0x4) | |
2524 | hash->movm_stack_size += 2 * 4; | |
2525 | } | |
252b5132 RH |
2526 | } |
2527 | ||
2528 | /* Now look for the two stack adjustment variants. */ | |
2529 | if (byte1 == 0xf8 && byte2 == 0xfe) | |
2530 | { | |
2531 | int temp = bfd_get_8 (abfd, contents + addr + 2); | |
2532 | temp = ((temp & 0xff) ^ (~0x7f)) + 0x80; | |
2533 | ||
2534 | hash->stack_size = -temp; | |
2535 | } | |
2536 | else if (byte1 == 0xfa && byte2 == 0xfe) | |
2537 | { | |
2538 | int temp = bfd_get_16 (abfd, contents + addr + 2); | |
2539 | temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000; | |
2540 | temp = -temp; | |
2541 | ||
2542 | if (temp < 255) | |
2543 | hash->stack_size = temp; | |
2544 | } | |
2545 | ||
2546 | /* If the total stack to be allocated by the call instruction is more | |
2547 | than 255 bytes, then we can't remove the stack adjustment by using | |
2548 | "call" (we might still be able to remove the "movm" instruction. */ | |
2549 | if (hash->stack_size + hash->movm_stack_size > 255) | |
2550 | hash->stack_size = 0; | |
2551 | ||
2552 | return; | |
2553 | } | |
2554 | ||
2555 | /* Delete some bytes from a section while relaxing. */ | |
2556 | ||
2557 | static boolean | |
2558 | mn10300_elf_relax_delete_bytes (abfd, sec, addr, count) | |
2559 | bfd *abfd; | |
2560 | asection *sec; | |
2561 | bfd_vma addr; | |
2562 | int count; | |
2563 | { | |
2564 | Elf_Internal_Shdr *symtab_hdr; | |
2565 | Elf32_External_Sym *extsyms; | |
2566 | int shndx, index; | |
2567 | bfd_byte *contents; | |
2568 | Elf_Internal_Rela *irel, *irelend; | |
2569 | Elf_Internal_Rela *irelalign; | |
2570 | bfd_vma toaddr; | |
2571 | Elf32_External_Sym *esym, *esymend; | |
2572 | struct elf32_mn10300_link_hash_entry *sym_hash; | |
2573 | ||
2574 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2575 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
2576 | ||
2577 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
2578 | ||
2579 | contents = elf_section_data (sec)->this_hdr.contents; | |
2580 | ||
2581 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
2582 | power larger than the number of bytes we are deleting. */ | |
2583 | ||
2584 | irelalign = NULL; | |
2585 | toaddr = sec->_cooked_size; | |
2586 | ||
2587 | irel = elf_section_data (sec)->relocs; | |
2588 | irelend = irel + sec->reloc_count; | |
2589 | ||
2590 | /* Actually delete the bytes. */ | |
dc810e39 AM |
2591 | memmove (contents + addr, contents + addr + count, |
2592 | (size_t) (toaddr - addr - count)); | |
252b5132 RH |
2593 | sec->_cooked_size -= count; |
2594 | ||
2595 | /* Adjust all the relocs. */ | |
2596 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) | |
2597 | { | |
2598 | /* Get the new reloc address. */ | |
2599 | if ((irel->r_offset > addr | |
2600 | && irel->r_offset < toaddr)) | |
2601 | irel->r_offset -= count; | |
2602 | } | |
2603 | ||
2604 | /* Adjust the local symbols defined in this section. */ | |
2605 | esym = extsyms; | |
2606 | esymend = esym + symtab_hdr->sh_info; | |
2607 | for (; esym < esymend; esym++) | |
2608 | { | |
2609 | Elf_Internal_Sym isym; | |
2610 | ||
2611 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); | |
2612 | ||
2613 | if (isym.st_shndx == shndx | |
2614 | && isym.st_value > addr | |
2615 | && isym.st_value < toaddr) | |
2616 | { | |
2617 | isym.st_value -= count; | |
2618 | bfd_elf32_swap_symbol_out (abfd, &isym, esym); | |
2619 | } | |
2620 | } | |
2621 | ||
2622 | /* Now adjust the global symbols defined in this section. */ | |
2623 | esym = extsyms + symtab_hdr->sh_info; | |
2624 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); | |
2625 | for (index = 0; esym < esymend; esym++, index++) | |
2626 | { | |
2627 | Elf_Internal_Sym isym; | |
2628 | ||
2629 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); | |
2630 | sym_hash = (struct elf32_mn10300_link_hash_entry *) | |
2631 | (elf_sym_hashes (abfd)[index]); | |
2632 | if (isym.st_shndx == shndx | |
2633 | && ((sym_hash)->root.root.type == bfd_link_hash_defined | |
2634 | || (sym_hash)->root.root.type == bfd_link_hash_defweak) | |
2635 | && (sym_hash)->root.root.u.def.section == sec | |
2636 | && (sym_hash)->root.root.u.def.value > addr | |
2637 | && (sym_hash)->root.root.u.def.value < toaddr) | |
2638 | { | |
2639 | (sym_hash)->root.root.u.def.value -= count; | |
2640 | } | |
2641 | } | |
2642 | ||
2643 | return true; | |
2644 | } | |
2645 | ||
2646 | /* Return true if a symbol exists at the given address, else return | |
2647 | false. */ | |
2648 | static boolean | |
2649 | mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr) | |
2650 | bfd *abfd; | |
2651 | asection *sec; | |
2652 | Elf32_External_Sym *extsyms; | |
2653 | bfd_vma addr; | |
2654 | { | |
2655 | Elf_Internal_Shdr *symtab_hdr; | |
2656 | int shndx; | |
2657 | Elf32_External_Sym *esym, *esymend; | |
2658 | struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end; | |
2659 | ||
2660 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2661 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
2662 | ||
2663 | /* Examine all the symbols. */ | |
2664 | esym = extsyms; | |
2665 | esymend = esym + symtab_hdr->sh_info; | |
2666 | for (; esym < esymend; esym++) | |
2667 | { | |
2668 | Elf_Internal_Sym isym; | |
2669 | ||
2670 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); | |
2671 | ||
2672 | if (isym.st_shndx == shndx | |
2673 | && isym.st_value == addr) | |
2674 | return true; | |
2675 | } | |
2676 | ||
a7c10850 | 2677 | sym_hash = (struct elf32_mn10300_link_hash_entry **) (elf_sym_hashes (abfd)); |
252b5132 RH |
2678 | sym_hash_end = (sym_hash |
2679 | + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
2680 | - symtab_hdr->sh_info)); | |
2681 | for (; sym_hash < sym_hash_end; sym_hash++) | |
2682 | { | |
2683 | if (((*sym_hash)->root.root.type == bfd_link_hash_defined | |
2684 | || (*sym_hash)->root.root.type == bfd_link_hash_defweak) | |
2685 | && (*sym_hash)->root.root.u.def.section == sec | |
2686 | && (*sym_hash)->root.root.u.def.value == addr) | |
2687 | return true; | |
2688 | } | |
2689 | return false; | |
2690 | } | |
2691 | ||
2692 | /* This is a version of bfd_generic_get_relocated_section_contents | |
2693 | which uses mn10300_elf_relocate_section. */ | |
2694 | ||
2695 | static bfd_byte * | |
2696 | mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order, | |
2697 | data, relocateable, symbols) | |
2698 | bfd *output_bfd; | |
2699 | struct bfd_link_info *link_info; | |
2700 | struct bfd_link_order *link_order; | |
2701 | bfd_byte *data; | |
2702 | boolean relocateable; | |
2703 | asymbol **symbols; | |
2704 | { | |
2705 | Elf_Internal_Shdr *symtab_hdr; | |
2706 | asection *input_section = link_order->u.indirect.section; | |
2707 | bfd *input_bfd = input_section->owner; | |
2708 | asection **sections = NULL; | |
2709 | Elf_Internal_Rela *internal_relocs = NULL; | |
2710 | Elf32_External_Sym *external_syms = NULL; | |
2711 | Elf_Internal_Sym *internal_syms = NULL; | |
2712 | ||
2713 | /* We only need to handle the case of relaxing, or of having a | |
2714 | particular set of section contents, specially. */ | |
2715 | if (relocateable | |
2716 | || elf_section_data (input_section)->this_hdr.contents == NULL) | |
2717 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, | |
2718 | link_order, data, | |
2719 | relocateable, | |
2720 | symbols); | |
2721 | ||
2722 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
2723 | ||
2724 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, | |
dc810e39 | 2725 | (size_t) input_section->_raw_size); |
252b5132 RH |
2726 | |
2727 | if ((input_section->flags & SEC_RELOC) != 0 | |
2728 | && input_section->reloc_count > 0) | |
2729 | { | |
2730 | Elf_Internal_Sym *isymp; | |
2731 | asection **secpp; | |
2732 | Elf32_External_Sym *esym, *esymend; | |
dc810e39 | 2733 | bfd_size_type size; |
252b5132 RH |
2734 | |
2735 | if (symtab_hdr->contents != NULL) | |
2736 | external_syms = (Elf32_External_Sym *) symtab_hdr->contents; | |
2737 | else | |
2738 | { | |
dc810e39 AM |
2739 | size = symtab_hdr->sh_info; |
2740 | size *= sizeof (Elf32_External_Sym); | |
2741 | external_syms = (Elf32_External_Sym *) bfd_malloc (size); | |
2742 | if (external_syms == NULL && size != 0) | |
252b5132 RH |
2743 | goto error_return; |
2744 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
dc810e39 | 2745 | || bfd_bread (external_syms, size, input_bfd) != size) |
252b5132 RH |
2746 | goto error_return; |
2747 | } | |
2748 | ||
2749 | internal_relocs = (_bfd_elf32_link_read_relocs | |
2750 | (input_bfd, input_section, (PTR) NULL, | |
2751 | (Elf_Internal_Rela *) NULL, false)); | |
2752 | if (internal_relocs == NULL) | |
2753 | goto error_return; | |
2754 | ||
dc810e39 AM |
2755 | size = symtab_hdr->sh_info; |
2756 | size *= sizeof (Elf_Internal_Sym); | |
2757 | internal_syms = (Elf_Internal_Sym *) bfd_malloc (size); | |
2758 | if (internal_syms == NULL && size != 0) | |
252b5132 RH |
2759 | goto error_return; |
2760 | ||
dc810e39 AM |
2761 | size = symtab_hdr->sh_info; |
2762 | size *= sizeof (asection *); | |
2763 | sections = (asection **) bfd_malloc (size); | |
2764 | if (sections == NULL && size != 0) | |
252b5132 RH |
2765 | goto error_return; |
2766 | ||
2767 | isymp = internal_syms; | |
2768 | secpp = sections; | |
2769 | esym = external_syms; | |
2770 | esymend = esym + symtab_hdr->sh_info; | |
2771 | for (; esym < esymend; ++esym, ++isymp, ++secpp) | |
2772 | { | |
2773 | asection *isec; | |
2774 | ||
2775 | bfd_elf32_swap_symbol_in (input_bfd, esym, isymp); | |
2776 | ||
2777 | if (isymp->st_shndx == SHN_UNDEF) | |
2778 | isec = bfd_und_section_ptr; | |
2779 | else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE) | |
2780 | isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx); | |
2781 | else if (isymp->st_shndx == SHN_ABS) | |
2782 | isec = bfd_abs_section_ptr; | |
2783 | else if (isymp->st_shndx == SHN_COMMON) | |
2784 | isec = bfd_com_section_ptr; | |
2785 | else | |
2786 | { | |
2787 | /* Who knows? */ | |
2788 | isec = NULL; | |
2789 | } | |
2790 | ||
2791 | *secpp = isec; | |
2792 | } | |
2793 | ||
2794 | if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd, | |
2795 | input_section, data, internal_relocs, | |
2796 | internal_syms, sections)) | |
2797 | goto error_return; | |
2798 | ||
2799 | if (sections != NULL) | |
2800 | free (sections); | |
2801 | sections = NULL; | |
2802 | if (internal_syms != NULL) | |
2803 | free (internal_syms); | |
2804 | internal_syms = NULL; | |
2805 | if (external_syms != NULL && symtab_hdr->contents == NULL) | |
2806 | free (external_syms); | |
2807 | external_syms = NULL; | |
2808 | if (internal_relocs != elf_section_data (input_section)->relocs) | |
2809 | free (internal_relocs); | |
2810 | internal_relocs = NULL; | |
2811 | } | |
2812 | ||
2813 | return data; | |
2814 | ||
2815 | error_return: | |
2816 | if (internal_relocs != NULL | |
2817 | && internal_relocs != elf_section_data (input_section)->relocs) | |
2818 | free (internal_relocs); | |
2819 | if (external_syms != NULL && symtab_hdr->contents == NULL) | |
2820 | free (external_syms); | |
2821 | if (internal_syms != NULL) | |
2822 | free (internal_syms); | |
2823 | if (sections != NULL) | |
2824 | free (sections); | |
2825 | return NULL; | |
2826 | } | |
2827 | ||
2828 | /* Assorted hash table functions. */ | |
2829 | ||
2830 | /* Initialize an entry in the link hash table. */ | |
2831 | ||
2832 | /* Create an entry in an MN10300 ELF linker hash table. */ | |
2833 | ||
2834 | static struct bfd_hash_entry * | |
2835 | elf32_mn10300_link_hash_newfunc (entry, table, string) | |
2836 | struct bfd_hash_entry *entry; | |
2837 | struct bfd_hash_table *table; | |
2838 | const char *string; | |
2839 | { | |
2840 | struct elf32_mn10300_link_hash_entry *ret = | |
2841 | (struct elf32_mn10300_link_hash_entry *) entry; | |
2842 | ||
2843 | /* Allocate the structure if it has not already been allocated by a | |
2844 | subclass. */ | |
2845 | if (ret == (struct elf32_mn10300_link_hash_entry *) NULL) | |
2846 | ret = ((struct elf32_mn10300_link_hash_entry *) | |
2847 | bfd_hash_allocate (table, | |
2848 | sizeof (struct elf32_mn10300_link_hash_entry))); | |
2849 | if (ret == (struct elf32_mn10300_link_hash_entry *) NULL) | |
2850 | return (struct bfd_hash_entry *) ret; | |
2851 | ||
2852 | /* Call the allocation method of the superclass. */ | |
2853 | ret = ((struct elf32_mn10300_link_hash_entry *) | |
2854 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
2855 | table, string)); | |
2856 | if (ret != (struct elf32_mn10300_link_hash_entry *) NULL) | |
2857 | { | |
2858 | ret->direct_calls = 0; | |
2859 | ret->stack_size = 0; | |
2860 | ret->movm_stack_size = 0; | |
2861 | ret->flags = 0; | |
2862 | ret->movm_args = 0; | |
2863 | } | |
2864 | ||
2865 | return (struct bfd_hash_entry *) ret; | |
2866 | } | |
2867 | ||
2868 | /* Create an mn10300 ELF linker hash table. */ | |
2869 | ||
2870 | static struct bfd_link_hash_table * | |
2871 | elf32_mn10300_link_hash_table_create (abfd) | |
2872 | bfd *abfd; | |
2873 | { | |
2874 | struct elf32_mn10300_link_hash_table *ret; | |
dc810e39 | 2875 | bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table); |
252b5132 | 2876 | |
dc810e39 | 2877 | ret = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt); |
252b5132 RH |
2878 | if (ret == (struct elf32_mn10300_link_hash_table *) NULL) |
2879 | return NULL; | |
2880 | ||
2881 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, | |
2882 | elf32_mn10300_link_hash_newfunc)) | |
2883 | { | |
2884 | bfd_release (abfd, ret); | |
2885 | return NULL; | |
2886 | } | |
2887 | ||
2888 | ret->flags = 0; | |
dc810e39 | 2889 | amt = sizeof (struct elf_link_hash_table); |
252b5132 | 2890 | ret->static_hash_table |
dc810e39 | 2891 | = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt); |
252b5132 RH |
2892 | if (ret->static_hash_table == NULL) |
2893 | { | |
2894 | bfd_release (abfd, ret); | |
2895 | return NULL; | |
2896 | } | |
2897 | ||
2898 | if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd, | |
2899 | elf32_mn10300_link_hash_newfunc)) | |
2900 | { | |
2901 | bfd_release (abfd, ret->static_hash_table); | |
2902 | bfd_release (abfd, ret); | |
2903 | return NULL; | |
2904 | } | |
2905 | return &ret->root.root; | |
2906 | } | |
2907 | ||
dc810e39 | 2908 | static unsigned long |
252b5132 RH |
2909 | elf_mn10300_mach (flags) |
2910 | flagword flags; | |
2911 | { | |
2912 | switch (flags & EF_MN10300_MACH) | |
2913 | { | |
010ac81f KH |
2914 | case E_MN10300_MACH_MN10300: |
2915 | default: | |
2916 | return bfd_mach_mn10300; | |
252b5132 | 2917 | |
010ac81f KH |
2918 | case E_MN10300_MACH_AM33: |
2919 | return bfd_mach_am33; | |
252b5132 RH |
2920 | } |
2921 | } | |
2922 | ||
2923 | /* The final processing done just before writing out a MN10300 ELF object | |
2924 | file. This gets the MN10300 architecture right based on the machine | |
2925 | number. */ | |
2926 | ||
252b5132 RH |
2927 | void |
2928 | _bfd_mn10300_elf_final_write_processing (abfd, linker) | |
2929 | bfd *abfd; | |
5f771d47 | 2930 | boolean linker ATTRIBUTE_UNUSED; |
252b5132 RH |
2931 | { |
2932 | unsigned long val; | |
252b5132 RH |
2933 | |
2934 | switch (bfd_get_mach (abfd)) | |
2935 | { | |
010ac81f KH |
2936 | default: |
2937 | case bfd_mach_mn10300: | |
2938 | val = E_MN10300_MACH_MN10300; | |
2939 | break; | |
2940 | ||
2941 | case bfd_mach_am33: | |
2942 | val = E_MN10300_MACH_AM33; | |
2943 | break; | |
252b5132 RH |
2944 | } |
2945 | ||
2946 | elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH); | |
2947 | elf_elfheader (abfd)->e_flags |= val; | |
2948 | } | |
2949 | ||
2950 | boolean | |
2951 | _bfd_mn10300_elf_object_p (abfd) | |
2952 | bfd *abfd; | |
2953 | { | |
2954 | bfd_default_set_arch_mach (abfd, bfd_arch_mn10300, | |
010ac81f | 2955 | elf_mn10300_mach (elf_elfheader (abfd)->e_flags)); |
252b5132 RH |
2956 | return true; |
2957 | } | |
2958 | ||
2959 | /* Merge backend specific data from an object file to the output | |
2960 | object file when linking. */ | |
2961 | ||
2962 | boolean | |
2963 | _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd) | |
2964 | bfd *ibfd; | |
2965 | bfd *obfd; | |
2966 | { | |
2967 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2968 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
2969 | return true; | |
2970 | ||
2971 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) | |
2972 | && bfd_get_mach (obfd) < bfd_get_mach (ibfd)) | |
2973 | { | |
2974 | if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), | |
2975 | bfd_get_mach (ibfd))) | |
2976 | return false; | |
2977 | } | |
2978 | ||
2979 | return true; | |
2980 | } | |
2981 | ||
252b5132 RH |
2982 | #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec |
2983 | #define TARGET_LITTLE_NAME "elf32-mn10300" | |
2984 | #define ELF_ARCH bfd_arch_mn10300 | |
6f4514dc AO |
2985 | #define ELF_MACHINE_CODE EM_MN10300 |
2986 | #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300 | |
252b5132 RH |
2987 | #define ELF_MAXPAGESIZE 0x1000 |
2988 | ||
2989 | #define elf_info_to_howto mn10300_info_to_howto | |
2990 | #define elf_info_to_howto_rel 0 | |
2991 | #define elf_backend_can_gc_sections 1 | |
2992 | #define elf_backend_check_relocs mn10300_elf_check_relocs | |
2993 | #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook | |
2994 | #define elf_backend_relocate_section mn10300_elf_relocate_section | |
2995 | #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section | |
2996 | #define bfd_elf32_bfd_get_relocated_section_contents \ | |
2997 | mn10300_elf_get_relocated_section_contents | |
2998 | #define bfd_elf32_bfd_link_hash_table_create \ | |
2999 | elf32_mn10300_link_hash_table_create | |
3000 | ||
3001 | #define elf_symbol_leading_char '_' | |
3002 | ||
3003 | /* So we can set bits in e_flags. */ | |
3004 | #define elf_backend_final_write_processing \ | |
3005 | _bfd_mn10300_elf_final_write_processing | |
3006 | #define elf_backend_object_p _bfd_mn10300_elf_object_p | |
3007 | ||
3008 | #define bfd_elf32_bfd_merge_private_bfd_data \ | |
3009 | _bfd_mn10300_elf_merge_private_bfd_data | |
3010 | ||
252b5132 | 3011 | #include "elf32-target.h" |