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