Commit | Line | Data |
---|---|---|
49f58d10 | 1 | /* M16C/M32C specific support for 32-bit ELF. |
6772dd07 | 2 | Copyright (C) 2005, 2006 |
49f58d10 JB |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "libbfd.h" | |
24 | #include "elf-bfd.h" | |
25 | #include "elf/m32c.h" | |
26 | #include "libiberty.h" | |
27 | ||
28 | /* Forward declarations. */ | |
29 | static reloc_howto_type * m32c_reloc_type_lookup | |
30 | (bfd *, bfd_reloc_code_real_type); | |
31 | static void m32c_info_to_howto_rela | |
32 | (bfd *, arelent *, Elf_Internal_Rela *); | |
33 | static bfd_boolean m32c_elf_relocate_section | |
34 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
49f58d10 JB |
35 | static bfd_boolean m32c_elf_check_relocs |
36 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
37 | static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int); | |
38 | #ifdef DEBUG | |
e460dd0d AM |
39 | char * m32c_get_reloc (long reloc); |
40 | void dump_symtab (bfd *, void *, void *); | |
49f58d10 JB |
41 | #endif |
42 | static bfd_boolean m32c_elf_relax_section | |
43 | (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again); | |
44 | ||
45 | ||
46 | static reloc_howto_type m32c_elf_howto_table [] = | |
47 | { | |
48 | /* This reloc does nothing. */ | |
49 | HOWTO (R_M32C_NONE, /* type */ | |
50 | 0, /* rightshift */ | |
51 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
52 | 32, /* bitsize */ | |
53 | FALSE, /* pc_relative */ | |
54 | 0, /* bitpos */ | |
55 | complain_overflow_bitfield, /* complain_on_overflow */ | |
56 | bfd_elf_generic_reloc, /* special_function */ | |
57 | "R_M32C_NONE", /* name */ | |
58 | FALSE, /* partial_inplace */ | |
59 | 0, /* src_mask */ | |
60 | 0, /* dst_mask */ | |
61 | FALSE), /* pcrel_offset */ | |
62 | ||
63 | HOWTO (R_M32C_16, /* type */ | |
64 | 0, /* rightshift */ | |
65 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
66 | 16, /* bitsize */ | |
67 | FALSE, /* pc_relative */ | |
68 | 0, /* bitpos */ | |
69 | complain_overflow_bitfield, /* complain_on_overflow */ | |
70 | bfd_elf_generic_reloc, /* special_function */ | |
71 | "R_M32C_16", /* name */ | |
72 | FALSE, /* partial_inplace */ | |
73 | 0, /* src_mask */ | |
6772dd07 | 74 | 0xffff, /* dst_mask */ |
49f58d10 JB |
75 | FALSE), /* pcrel_offset */ |
76 | ||
77 | HOWTO (R_M32C_24, /* type */ | |
78 | 0, /* rightshift */ | |
79 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
80 | 24, /* bitsize */ | |
81 | FALSE, /* pc_relative */ | |
82 | 0, /* bitpos */ | |
83 | complain_overflow_bitfield, /* complain_on_overflow */ | |
84 | bfd_elf_generic_reloc, /* special_function */ | |
85 | "R_M32C_24", /* name */ | |
86 | FALSE, /* partial_inplace */ | |
87 | 0, /* src_mask */ | |
6772dd07 | 88 | 0xffffff, /* dst_mask */ |
49f58d10 JB |
89 | FALSE), /* pcrel_offset */ |
90 | ||
91 | HOWTO (R_M32C_32, /* type */ | |
92 | 0, /* rightshift */ | |
93 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
94 | 32, /* bitsize */ | |
95 | FALSE, /* pc_relative */ | |
96 | 0, /* bitpos */ | |
97 | complain_overflow_bitfield, /* complain_on_overflow */ | |
98 | bfd_elf_generic_reloc, /* special_function */ | |
99 | "R_M32C_32", /* name */ | |
100 | FALSE, /* partial_inplace */ | |
101 | 0, /* src_mask */ | |
102 | 0xffffffff, /* dst_mask */ | |
103 | FALSE), /* pcrel_offset */ | |
104 | ||
105 | HOWTO (R_M32C_8_PCREL, /* type */ | |
106 | 0, /* rightshift */ | |
107 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
108 | 8, /* bitsize */ | |
109 | TRUE, /* pc_relative */ | |
110 | 0, /* bitpos */ | |
111 | complain_overflow_signed, /* complain_on_overflow */ | |
112 | bfd_elf_generic_reloc, /* special_function */ | |
113 | "R_M32C_8_PCREL", /* name */ | |
114 | FALSE, /* partial_inplace */ | |
115 | 0, /* src_mask */ | |
6772dd07 | 116 | 0xff, /* dst_mask */ |
49f58d10 JB |
117 | TRUE), /* pcrel_offset */ |
118 | ||
119 | HOWTO (R_M32C_16_PCREL, /* type */ | |
120 | 0, /* rightshift */ | |
121 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
122 | 16, /* bitsize */ | |
123 | TRUE, /* pc_relative */ | |
124 | 0, /* bitpos */ | |
125 | complain_overflow_signed, /* complain_on_overflow */ | |
126 | bfd_elf_generic_reloc, /* special_function */ | |
127 | "R_M32C_16_PCREL", /* name */ | |
128 | FALSE, /* partial_inplace */ | |
129 | 0, /* src_mask */ | |
6772dd07 | 130 | 0xffff, /* dst_mask */ |
49f58d10 | 131 | TRUE), /* pcrel_offset */ |
fd54057a DD |
132 | |
133 | HOWTO (R_M32C_8, /* type */ | |
134 | 0, /* rightshift */ | |
135 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
136 | 8, /* bitsize */ | |
137 | FALSE, /* pc_relative */ | |
138 | 0, /* bitpos */ | |
139 | complain_overflow_unsigned, /* complain_on_overflow */ | |
140 | bfd_elf_generic_reloc, /* special_function */ | |
141 | "R_M32C_8", /* name */ | |
142 | FALSE, /* partial_inplace */ | |
143 | 0, /* src_mask */ | |
6772dd07 | 144 | 0xff, /* dst_mask */ |
fd54057a DD |
145 | FALSE), /* pcrel_offset */ |
146 | ||
147 | HOWTO (R_M32C_LO16, /* type */ | |
148 | 0, /* rightshift */ | |
149 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
150 | 16, /* bitsize */ | |
151 | FALSE, /* pc_relative */ | |
152 | 0, /* bitpos */ | |
153 | complain_overflow_dont, /* complain_on_overflow */ | |
154 | bfd_elf_generic_reloc, /* special_function */ | |
155 | "R_M32C_LO16", /* name */ | |
156 | FALSE, /* partial_inplace */ | |
157 | 0, /* src_mask */ | |
6772dd07 | 158 | 0xffff, /* dst_mask */ |
fd54057a DD |
159 | FALSE), /* pcrel_offset */ |
160 | ||
161 | HOWTO (R_M32C_HI8, /* type */ | |
162 | 0, /* rightshift */ | |
163 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
164 | 8, /* bitsize */ | |
165 | FALSE, /* pc_relative */ | |
166 | 0, /* bitpos */ | |
167 | complain_overflow_dont, /* complain_on_overflow */ | |
168 | bfd_elf_generic_reloc, /* special_function */ | |
169 | "R_M32C_HI8", /* name */ | |
170 | FALSE, /* partial_inplace */ | |
171 | 0, /* src_mask */ | |
6772dd07 | 172 | 0xff, /* dst_mask */ |
fd54057a DD |
173 | FALSE), /* pcrel_offset */ |
174 | ||
175 | HOWTO (R_M32C_HI16, /* type */ | |
176 | 0, /* rightshift */ | |
177 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
178 | 16, /* bitsize */ | |
179 | FALSE, /* pc_relative */ | |
180 | 0, /* bitpos */ | |
181 | complain_overflow_dont, /* complain_on_overflow */ | |
182 | bfd_elf_generic_reloc, /* special_function */ | |
183 | "R_M32C_HI16", /* name */ | |
184 | FALSE, /* partial_inplace */ | |
185 | 0, /* src_mask */ | |
6772dd07 | 186 | 0xffff, /* dst_mask */ |
fd54057a | 187 | FALSE), /* pcrel_offset */ |
6772dd07 DD |
188 | |
189 | HOWTO (R_M32C_RL_JUMP, /* type */ | |
190 | 0, /* rightshift */ | |
191 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
192 | 0, /* bitsize */ | |
193 | FALSE, /* pc_relative */ | |
194 | 0, /* bitpos */ | |
195 | complain_overflow_signed, /* complain_on_overflow */ | |
196 | bfd_elf_generic_reloc, /* special_function */ | |
197 | "R_M32C_RL_JUMP", /* name */ | |
198 | FALSE, /* partial_inplace */ | |
199 | 0, /* src_mask */ | |
200 | 0, /* dst_mask */ | |
201 | FALSE), /* pcrel_offset */ | |
202 | ||
203 | HOWTO (R_M32C_RL_1ADDR, /* type */ | |
204 | 0, /* rightshift */ | |
205 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
206 | 0, /* bitsize */ | |
207 | FALSE, /* pc_relative */ | |
208 | 0, /* bitpos */ | |
209 | complain_overflow_signed, /* complain_on_overflow */ | |
210 | bfd_elf_generic_reloc, /* special_function */ | |
211 | "R_M32C_RL_1ADDR", /* name */ | |
212 | FALSE, /* partial_inplace */ | |
213 | 0, /* src_mask */ | |
214 | 0, /* dst_mask */ | |
215 | FALSE), /* pcrel_offset */ | |
216 | ||
217 | HOWTO (R_M32C_RL_2ADDR, /* type */ | |
218 | 0, /* rightshift */ | |
219 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
220 | 0, /* bitsize */ | |
221 | FALSE, /* pc_relative */ | |
222 | 0, /* bitpos */ | |
223 | complain_overflow_signed, /* complain_on_overflow */ | |
224 | bfd_elf_generic_reloc, /* special_function */ | |
225 | "R_M32C_RL_2ADDR", /* name */ | |
226 | FALSE, /* partial_inplace */ | |
227 | 0, /* src_mask */ | |
228 | 0, /* dst_mask */ | |
229 | FALSE), /* pcrel_offset */ | |
230 | ||
49f58d10 JB |
231 | }; |
232 | \f | |
233 | /* Map BFD reloc types to M32C ELF reloc types. */ | |
234 | ||
235 | struct m32c_reloc_map | |
236 | { | |
237 | bfd_reloc_code_real_type bfd_reloc_val; | |
238 | unsigned int m32c_reloc_val; | |
239 | }; | |
240 | ||
241 | static const struct m32c_reloc_map m32c_reloc_map [] = | |
242 | { | |
243 | { BFD_RELOC_NONE, R_M32C_NONE }, | |
244 | { BFD_RELOC_16, R_M32C_16 }, | |
245 | { BFD_RELOC_24, R_M32C_24 }, | |
246 | { BFD_RELOC_32, R_M32C_32 }, | |
247 | { BFD_RELOC_8_PCREL, R_M32C_8_PCREL }, | |
fd54057a DD |
248 | { BFD_RELOC_16_PCREL, R_M32C_16_PCREL }, |
249 | { BFD_RELOC_8, R_M32C_8 }, | |
250 | { BFD_RELOC_LO16, R_M32C_LO16 }, | |
251 | { BFD_RELOC_HI16, R_M32C_HI16 }, | |
6772dd07 DD |
252 | { BFD_RELOC_M32C_HI8, R_M32C_HI8 }, |
253 | { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP }, | |
254 | { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR }, | |
255 | { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR } | |
49f58d10 JB |
256 | }; |
257 | ||
258 | static reloc_howto_type * | |
259 | m32c_reloc_type_lookup | |
260 | (bfd * abfd ATTRIBUTE_UNUSED, | |
261 | bfd_reloc_code_real_type code) | |
262 | { | |
263 | unsigned int i; | |
264 | ||
265 | for (i = ARRAY_SIZE (m32c_reloc_map); --i;) | |
266 | if (m32c_reloc_map [i].bfd_reloc_val == code) | |
267 | return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val]; | |
268 | ||
269 | return NULL; | |
270 | } | |
271 | ||
272 | /* Set the howto pointer for an M32C ELF reloc. */ | |
273 | ||
274 | static void | |
275 | m32c_info_to_howto_rela | |
276 | (bfd * abfd ATTRIBUTE_UNUSED, | |
277 | arelent * cache_ptr, | |
278 | Elf_Internal_Rela * dst) | |
279 | { | |
280 | unsigned int r_type; | |
281 | ||
282 | r_type = ELF32_R_TYPE (dst->r_info); | |
283 | BFD_ASSERT (r_type < (unsigned int) R_M32C_max); | |
284 | cache_ptr->howto = & m32c_elf_howto_table [r_type]; | |
285 | } | |
286 | ||
287 | \f | |
288 | ||
289 | /* Relocate an M32C ELF section. | |
290 | There is some attempt to make this function usable for many architectures, | |
291 | both USE_REL and USE_RELA ['twould be nice if such a critter existed], | |
292 | if only to serve as a learning tool. | |
293 | ||
294 | The RELOCATE_SECTION function is called by the new ELF backend linker | |
295 | to handle the relocations for a section. | |
296 | ||
297 | The relocs are always passed as Rela structures; if the section | |
298 | actually uses Rel structures, the r_addend field will always be | |
299 | zero. | |
300 | ||
301 | This function is responsible for adjusting the section contents as | |
302 | necessary, and (if using Rela relocs and generating a relocatable | |
303 | output file) adjusting the reloc addend as necessary. | |
304 | ||
305 | This function does not have to worry about setting the reloc | |
306 | address or the reloc symbol index. | |
307 | ||
308 | LOCAL_SYMS is a pointer to the swapped in local symbols. | |
309 | ||
310 | LOCAL_SECTIONS is an array giving the section in the input file | |
311 | corresponding to the st_shndx field of each local symbol. | |
312 | ||
313 | The global hash table entry for the global symbols can be found | |
314 | via elf_sym_hashes (input_bfd). | |
315 | ||
316 | When generating relocatable output, this function must handle | |
317 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is | |
318 | going to be the section symbol corresponding to the output | |
319 | section, which means that the addend must be adjusted | |
320 | accordingly. */ | |
321 | ||
322 | static bfd_boolean | |
323 | m32c_elf_relocate_section | |
324 | (bfd * output_bfd ATTRIBUTE_UNUSED, | |
325 | struct bfd_link_info * info, | |
326 | bfd * input_bfd, | |
327 | asection * input_section, | |
328 | bfd_byte * contents, | |
329 | Elf_Internal_Rela * relocs, | |
330 | Elf_Internal_Sym * local_syms, | |
331 | asection ** local_sections) | |
332 | { | |
333 | Elf_Internal_Shdr * symtab_hdr; | |
334 | struct elf_link_hash_entry ** sym_hashes; | |
335 | Elf_Internal_Rela * rel; | |
336 | Elf_Internal_Rela * relend; | |
337 | bfd *dynobj; | |
338 | asection *splt; | |
339 | ||
340 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
341 | sym_hashes = elf_sym_hashes (input_bfd); | |
342 | relend = relocs + input_section->reloc_count; | |
343 | ||
344 | dynobj = elf_hash_table (info)->dynobj; | |
345 | splt = NULL; | |
346 | if (dynobj != NULL) | |
347 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
348 | ||
349 | for (rel = relocs; rel < relend; rel ++) | |
350 | { | |
351 | reloc_howto_type * howto; | |
352 | unsigned long r_symndx; | |
353 | Elf_Internal_Sym * sym; | |
354 | asection * sec; | |
355 | struct elf_link_hash_entry * h; | |
356 | bfd_vma relocation; | |
357 | bfd_reloc_status_type r; | |
358 | const char * name = NULL; | |
359 | int r_type; | |
360 | ||
361 | r_type = ELF32_R_TYPE (rel->r_info); | |
6772dd07 DD |
362 | |
363 | /* These are only used for relaxing; we don't actually relocate | |
364 | anything with them, so skip them. */ | |
365 | if (r_type == R_M32C_RL_JUMP | |
366 | || r_type == R_M32C_RL_1ADDR | |
367 | || r_type == R_M32C_RL_2ADDR) | |
368 | continue; | |
49f58d10 JB |
369 | |
370 | r_symndx = ELF32_R_SYM (rel->r_info); | |
371 | ||
372 | if (info->relocatable) | |
373 | { | |
374 | /* This is a relocatable link. We don't have to change | |
375 | anything, unless the reloc is against a section symbol, | |
376 | in which case we have to adjust according to where the | |
377 | section symbol winds up in the output section. */ | |
378 | if (r_symndx < symtab_hdr->sh_info) | |
379 | { | |
380 | sym = local_syms + r_symndx; | |
381 | ||
382 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
383 | { | |
384 | sec = local_sections [r_symndx]; | |
385 | rel->r_addend += sec->output_offset + sym->st_value; | |
386 | } | |
387 | } | |
388 | ||
389 | continue; | |
390 | } | |
391 | ||
392 | /* This is a final link. */ | |
393 | howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info); | |
394 | h = NULL; | |
395 | sym = NULL; | |
396 | sec = NULL; | |
6772dd07 | 397 | |
49f58d10 JB |
398 | if (r_symndx < symtab_hdr->sh_info) |
399 | { | |
400 | sym = local_syms + r_symndx; | |
401 | sec = local_sections [r_symndx]; | |
402 | relocation = (sec->output_section->vma | |
403 | + sec->output_offset | |
404 | + sym->st_value); | |
405 | ||
406 | name = bfd_elf_string_from_elf_section | |
407 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
6772dd07 | 408 | name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name; |
49f58d10 JB |
409 | } |
410 | else | |
411 | { | |
412 | h = sym_hashes [r_symndx - symtab_hdr->sh_info]; | |
413 | ||
414 | while (h->root.type == bfd_link_hash_indirect | |
415 | || h->root.type == bfd_link_hash_warning) | |
416 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
417 | ||
418 | name = h->root.root.string; | |
419 | ||
420 | if (h->root.type == bfd_link_hash_defined | |
421 | || h->root.type == bfd_link_hash_defweak) | |
422 | { | |
423 | sec = h->root.u.def.section; | |
424 | relocation = (h->root.u.def.value | |
425 | + sec->output_section->vma | |
426 | + sec->output_offset); | |
427 | } | |
428 | else if (h->root.type == bfd_link_hash_undefweak) | |
429 | { | |
430 | relocation = 0; | |
431 | } | |
432 | else | |
433 | { | |
434 | if (! ((*info->callbacks->undefined_symbol) | |
435 | (info, h->root.root.string, input_bfd, | |
436 | input_section, rel->r_offset, TRUE))) | |
437 | return FALSE; | |
438 | relocation = 0; | |
439 | } | |
440 | } | |
441 | ||
442 | switch (ELF32_R_TYPE (rel->r_info)) | |
443 | { | |
444 | case R_M32C_16: | |
445 | { | |
446 | bfd_vma *plt_offset; | |
447 | ||
448 | if (h != NULL) | |
449 | plt_offset = &h->plt.offset; | |
450 | else | |
451 | plt_offset = elf_local_got_offsets (input_bfd) + r_symndx; | |
452 | ||
453 | /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)", | |
454 | relocation, *plt_offset);*/ | |
455 | if (relocation <= 0xffff) | |
456 | { | |
457 | /* If the symbol is in range for a 16-bit address, we should | |
458 | have deallocated the plt entry in relax_section. */ | |
459 | BFD_ASSERT (*plt_offset == (bfd_vma) -1); | |
460 | } | |
461 | else | |
462 | { | |
463 | /* If the symbol is out of range for a 16-bit address, | |
464 | we must have allocated a plt entry. */ | |
465 | BFD_ASSERT (*plt_offset != (bfd_vma) -1); | |
466 | ||
467 | /* If this is the first time we've processed this symbol, | |
468 | fill in the plt entry with the correct symbol address. */ | |
469 | if ((*plt_offset & 1) == 0) | |
470 | { | |
471 | unsigned int x; | |
472 | ||
473 | x = 0x000000fc; /* jmpf */ | |
474 | x |= (relocation << 8) & 0xffffff00; | |
475 | bfd_put_32 (input_bfd, x, splt->contents + *plt_offset); | |
476 | *plt_offset |= 1; | |
477 | } | |
478 | ||
479 | relocation = (splt->output_section->vma | |
480 | + splt->output_offset | |
481 | + (*plt_offset & -2)); | |
35520cb7 DD |
482 | if (name) |
483 | { | |
484 | char *newname = bfd_malloc (strlen(name)+5); | |
485 | strcpy (newname, name); | |
486 | strcat(newname, ".plt"); | |
487 | _bfd_generic_link_add_one_symbol (info, | |
488 | input_bfd, | |
489 | newname, | |
490 | BSF_FUNCTION | BSF_WEAK, | |
491 | splt, | |
492 | (*plt_offset & -2), | |
493 | 0, | |
494 | 1, | |
495 | 0, | |
496 | 0); | |
497 | } | |
49f58d10 JB |
498 | } |
499 | } | |
500 | break; | |
fd54057a DD |
501 | |
502 | case R_M32C_HI8: | |
503 | case R_M32C_HI16: | |
504 | relocation >>= 16; | |
505 | break; | |
49f58d10 JB |
506 | } |
507 | ||
6772dd07 | 508 | #if 0 |
a1013894 DD |
509 | printf ("relocate %s at %06lx relocation %06lx addend %ld ", |
510 | m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name, | |
511 | rel->r_offset + input_section->output_section->vma + input_section->output_offset, | |
512 | relocation, rel->r_addend); | |
6772dd07 DD |
513 | { |
514 | int i; | |
515 | for (i=0; i<4; i++) | |
a1013894 DD |
516 | printf (" %02x", contents[rel->r_offset+i]); |
517 | printf ("\n"); | |
6772dd07 DD |
518 | } |
519 | #endif | |
49f58d10 JB |
520 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
521 | contents, rel->r_offset, relocation, | |
522 | rel->r_addend); | |
523 | ||
524 | if (r != bfd_reloc_ok) | |
525 | { | |
526 | const char * msg = (const char *) NULL; | |
527 | ||
528 | switch (r) | |
529 | { | |
530 | case bfd_reloc_overflow: | |
531 | r = info->callbacks->reloc_overflow | |
532 | (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0, | |
533 | input_bfd, input_section, rel->r_offset); | |
534 | break; | |
535 | ||
536 | case bfd_reloc_undefined: | |
537 | r = info->callbacks->undefined_symbol | |
538 | (info, name, input_bfd, input_section, rel->r_offset, | |
539 | TRUE); | |
540 | break; | |
541 | ||
542 | case bfd_reloc_outofrange: | |
543 | msg = _("internal error: out of range error"); | |
544 | break; | |
545 | ||
546 | case bfd_reloc_notsupported: | |
547 | msg = _("internal error: unsupported relocation error"); | |
548 | break; | |
549 | ||
550 | case bfd_reloc_dangerous: | |
551 | msg = _("internal error: dangerous relocation"); | |
552 | break; | |
553 | ||
554 | default: | |
555 | msg = _("internal error: unknown error"); | |
556 | break; | |
557 | } | |
558 | ||
559 | if (msg) | |
560 | r = info->callbacks->warning | |
561 | (info, msg, name, input_bfd, input_section, rel->r_offset); | |
562 | ||
563 | if (! r) | |
564 | return FALSE; | |
565 | } | |
566 | } | |
567 | ||
568 | return TRUE; | |
569 | } | |
570 | \f | |
49f58d10 JB |
571 | /* We support 16-bit pointers to code above 64k by generating a thunk |
572 | below 64k containing a JMP instruction to the final address. */ | |
573 | ||
574 | static bfd_boolean | |
575 | m32c_elf_check_relocs | |
576 | (bfd * abfd, | |
577 | struct bfd_link_info * info, | |
578 | asection * sec, | |
579 | const Elf_Internal_Rela * relocs) | |
580 | { | |
581 | Elf_Internal_Shdr * symtab_hdr; | |
582 | struct elf_link_hash_entry ** sym_hashes; | |
583 | struct elf_link_hash_entry ** sym_hashes_end; | |
584 | const Elf_Internal_Rela * rel; | |
585 | const Elf_Internal_Rela * rel_end; | |
586 | bfd_vma *local_plt_offsets; | |
587 | asection *splt; | |
588 | bfd *dynobj; | |
589 | ||
590 | if (info->relocatable) | |
591 | return TRUE; | |
592 | ||
593 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
594 | sym_hashes = elf_sym_hashes (abfd); | |
595 | local_plt_offsets = elf_local_got_offsets (abfd); | |
596 | splt = NULL; | |
597 | dynobj = elf_hash_table(info)->dynobj; | |
598 | ||
599 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym); | |
600 | if (!elf_bad_symtab (abfd)) | |
601 | sym_hashes_end -= symtab_hdr->sh_info; | |
602 | ||
603 | rel_end = relocs + sec->reloc_count; | |
604 | for (rel = relocs; rel < rel_end; rel++) | |
605 | { | |
606 | struct elf_link_hash_entry *h; | |
607 | unsigned long r_symndx; | |
608 | bfd_vma *offset; | |
609 | ||
610 | r_symndx = ELF32_R_SYM (rel->r_info); | |
611 | if (r_symndx < symtab_hdr->sh_info) | |
612 | h = NULL; | |
613 | else | |
614 | { | |
615 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
616 | while (h->root.type == bfd_link_hash_indirect | |
617 | || h->root.type == bfd_link_hash_warning) | |
618 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
619 | } | |
620 | ||
621 | switch (ELF32_R_TYPE (rel->r_info)) | |
622 | { | |
623 | /* This relocation describes a 16-bit pointer to a function. | |
624 | We may need to allocate a thunk in low memory; reserve memory | |
625 | for it now. */ | |
626 | case R_M32C_16: | |
627 | if (dynobj == NULL) | |
628 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
629 | if (splt == NULL) | |
630 | { | |
631 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
632 | if (splt == NULL) | |
633 | { | |
117ed4f8 AM |
634 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
635 | | SEC_IN_MEMORY | SEC_LINKER_CREATED | |
636 | | SEC_READONLY | SEC_CODE); | |
637 | splt = bfd_make_section_with_flags (dynobj, ".plt", flags); | |
49f58d10 | 638 | if (splt == NULL |
49f58d10 JB |
639 | || ! bfd_set_section_alignment (dynobj, splt, 1)) |
640 | return FALSE; | |
641 | } | |
642 | } | |
643 | ||
644 | if (h != NULL) | |
645 | offset = &h->plt.offset; | |
646 | else | |
647 | { | |
648 | if (local_plt_offsets == NULL) | |
649 | { | |
650 | size_t size; | |
651 | unsigned int i; | |
652 | ||
653 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
654 | local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
655 | if (local_plt_offsets == NULL) | |
656 | return FALSE; | |
657 | elf_local_got_offsets (abfd) = local_plt_offsets; | |
658 | ||
659 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
660 | local_plt_offsets[i] = (bfd_vma) -1; | |
661 | } | |
662 | offset = &local_plt_offsets[r_symndx]; | |
663 | } | |
664 | ||
665 | if (*offset == (bfd_vma) -1) | |
666 | { | |
667 | *offset = splt->size; | |
668 | splt->size += 4; | |
669 | } | |
670 | break; | |
671 | } | |
672 | } | |
673 | ||
674 | return TRUE; | |
675 | } | |
676 | ||
677 | /* This must exist if dynobj is ever set. */ | |
678 | ||
679 | static bfd_boolean | |
680 | m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED, | |
681 | struct bfd_link_info *info) | |
682 | { | |
683 | bfd *dynobj; | |
684 | asection *splt; | |
685 | ||
686 | /* As an extra sanity check, verify that all plt entries have | |
687 | been filled in. */ | |
688 | ||
689 | if ((dynobj = elf_hash_table (info)->dynobj) != NULL | |
690 | && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL) | |
691 | { | |
692 | bfd_byte *contents = splt->contents; | |
693 | unsigned int i, size = splt->size; | |
694 | for (i = 0; i < size; i += 4) | |
695 | { | |
696 | unsigned int x = bfd_get_32 (dynobj, contents + i); | |
697 | BFD_ASSERT (x != 0); | |
698 | } | |
699 | } | |
700 | ||
701 | return TRUE; | |
702 | } | |
703 | ||
704 | static bfd_boolean | |
705 | m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED, | |
706 | struct bfd_link_info *info) | |
707 | { | |
708 | bfd *dynobj; | |
709 | asection *splt; | |
710 | ||
711 | if (info->relocatable) | |
712 | return TRUE; | |
713 | ||
714 | dynobj = elf_hash_table (info)->dynobj; | |
715 | if (dynobj == NULL) | |
716 | return TRUE; | |
717 | ||
718 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
719 | BFD_ASSERT (splt != NULL); | |
720 | ||
721 | splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size); | |
722 | if (splt->contents == NULL) | |
723 | return FALSE; | |
724 | ||
725 | return TRUE; | |
726 | } | |
727 | \f | |
728 | /* Function to set the ELF flag bits. */ | |
729 | ||
730 | static bfd_boolean | |
731 | m32c_elf_set_private_flags (bfd *abfd, flagword flags) | |
732 | { | |
733 | elf_elfheader (abfd)->e_flags = flags; | |
734 | elf_flags_init (abfd) = TRUE; | |
735 | return TRUE; | |
736 | } | |
737 | ||
738 | /* Merge backend specific data from an object file to the output | |
739 | object file when linking. */ | |
740 | ||
741 | static bfd_boolean | |
742 | m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd) | |
743 | { | |
744 | flagword old_flags, old_partial; | |
745 | flagword new_flags, new_partial; | |
746 | bfd_boolean error = FALSE; | |
747 | char new_opt[80]; | |
748 | char old_opt[80]; | |
749 | ||
750 | new_opt[0] = old_opt[0] = '\0'; | |
751 | new_flags = elf_elfheader (ibfd)->e_flags; | |
752 | old_flags = elf_elfheader (obfd)->e_flags; | |
753 | ||
754 | #ifdef DEBUG | |
755 | (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s", | |
756 | old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no", | |
757 | bfd_get_filename (ibfd)); | |
758 | #endif | |
759 | ||
760 | if (!elf_flags_init (obfd)) | |
761 | { | |
762 | /* First call, no flags set. */ | |
763 | elf_flags_init (obfd) = TRUE; | |
764 | elf_elfheader (obfd)->e_flags = new_flags; | |
765 | } | |
766 | ||
767 | else if (new_flags == old_flags) | |
768 | /* Compatible flags are ok. */ | |
769 | ; | |
770 | ||
771 | else /* Possibly incompatible flags. */ | |
772 | { | |
773 | /* Warn if different cpu is used (allow a specific cpu to override | |
774 | the generic cpu). */ | |
775 | new_partial = (new_flags & EF_M32C_CPU_MASK); | |
776 | old_partial = (old_flags & EF_M32C_CPU_MASK); | |
777 | if (new_partial == old_partial) | |
778 | ; | |
779 | ||
780 | else | |
781 | { | |
782 | switch (new_partial) | |
783 | { | |
784 | default: strcat (new_opt, " -m16c"); break; | |
785 | case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break; | |
786 | case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break; | |
787 | } | |
788 | ||
789 | switch (old_partial) | |
790 | { | |
791 | default: strcat (old_opt, " -m16c"); break; | |
792 | case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break; | |
793 | case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break; | |
794 | } | |
795 | } | |
796 | ||
797 | /* Print out any mismatches from above. */ | |
798 | if (new_opt[0]) | |
799 | { | |
800 | error = TRUE; | |
801 | (*_bfd_error_handler) | |
802 | (_("%s: compiled with %s and linked with modules compiled with %s"), | |
803 | bfd_get_filename (ibfd), new_opt, old_opt); | |
804 | } | |
805 | ||
806 | new_flags &= ~ EF_M32C_ALL_FLAGS; | |
807 | old_flags &= ~ EF_M32C_ALL_FLAGS; | |
808 | ||
809 | /* Warn about any other mismatches. */ | |
810 | if (new_flags != old_flags) | |
811 | { | |
812 | error = TRUE; | |
813 | (*_bfd_error_handler) | |
814 | (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"), | |
815 | bfd_get_filename (ibfd), (long)new_flags, (long)old_flags); | |
816 | } | |
817 | } | |
818 | ||
819 | if (error) | |
820 | bfd_set_error (bfd_error_bad_value); | |
821 | ||
822 | return !error; | |
823 | } | |
824 | ||
825 | \f | |
826 | static bfd_boolean | |
827 | m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr) | |
828 | { | |
829 | FILE *file = (FILE *) ptr; | |
830 | flagword flags; | |
831 | ||
832 | BFD_ASSERT (abfd != NULL && ptr != NULL); | |
833 | ||
834 | /* Print normal ELF private data. */ | |
835 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
836 | ||
837 | flags = elf_elfheader (abfd)->e_flags; | |
838 | fprintf (file, _("private flags = 0x%lx:"), (long)flags); | |
839 | ||
840 | switch (flags & EF_M32C_CPU_MASK) | |
841 | { | |
842 | default: break; | |
843 | case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break; | |
844 | case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break; | |
845 | } | |
846 | ||
847 | fputc ('\n', file); | |
848 | return TRUE; | |
849 | } | |
850 | ||
851 | /* Return the MACH for an e_flags value. */ | |
852 | ||
853 | static int | |
854 | elf32_m32c_machine (bfd *abfd) | |
855 | { | |
856 | switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) | |
857 | { | |
858 | case EF_M32C_CPU_M16C: return bfd_mach_m16c; | |
859 | case EF_M32C_CPU_M32C: return bfd_mach_m32c; | |
860 | } | |
861 | ||
862 | return bfd_mach_m16c; | |
863 | } | |
864 | ||
865 | static bfd_boolean | |
866 | m32c_elf_object_p (bfd *abfd) | |
867 | { | |
868 | bfd_default_set_arch_mach (abfd, bfd_arch_m32c, | |
869 | elf32_m32c_machine (abfd)); | |
870 | return TRUE; | |
871 | } | |
872 | \f | |
873 | ||
874 | #ifdef DEBUG | |
e460dd0d | 875 | void |
49f58d10 JB |
876 | dump_symtab (bfd * abfd, void *internal_syms, void *external_syms) |
877 | { | |
878 | size_t locsymcount; | |
879 | Elf_Internal_Sym *isymbuf; | |
880 | Elf_Internal_Sym *isymend; | |
881 | Elf_Internal_Sym *isym; | |
882 | Elf_Internal_Shdr *symtab_hdr; | |
883 | bfd_boolean free_internal = 0, free_external = 0; | |
884 | char * st_info_str; | |
885 | char * st_info_stb_str; | |
886 | char * st_other_str; | |
887 | char * st_shndx_str; | |
888 | ||
889 | if (! internal_syms) | |
890 | { | |
891 | internal_syms = bfd_malloc (1000); | |
892 | free_internal = 1; | |
893 | } | |
894 | if (! external_syms) | |
895 | { | |
896 | external_syms = bfd_malloc (1000); | |
897 | free_external = 1; | |
898 | } | |
899 | ||
900 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
901 | locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym; | |
902 | if (free_internal) | |
903 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
904 | symtab_hdr->sh_info, 0, | |
905 | internal_syms, external_syms, NULL); | |
906 | else | |
907 | isymbuf = internal_syms; | |
908 | isymend = isymbuf + locsymcount; | |
909 | ||
910 | for (isym = isymbuf ; isym < isymend ; isym++) | |
911 | { | |
912 | switch (ELF_ST_TYPE (isym->st_info)) | |
913 | { | |
914 | case STT_FUNC: st_info_str = "STT_FUNC"; | |
915 | case STT_SECTION: st_info_str = "STT_SECTION"; | |
49f58d10 JB |
916 | case STT_FILE: st_info_str = "STT_FILE"; |
917 | case STT_OBJECT: st_info_str = "STT_OBJECT"; | |
918 | case STT_TLS: st_info_str = "STT_TLS"; | |
919 | default: st_info_str = ""; | |
920 | } | |
921 | switch (ELF_ST_BIND (isym->st_info)) | |
922 | { | |
923 | case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; | |
924 | case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; | |
925 | default: st_info_stb_str = ""; | |
926 | } | |
927 | switch (ELF_ST_VISIBILITY (isym->st_other)) | |
928 | { | |
929 | case STV_DEFAULT: st_other_str = "STV_DEFAULT"; | |
930 | case STV_INTERNAL: st_other_str = "STV_INTERNAL"; | |
931 | case STV_PROTECTED: st_other_str = "STV_PROTECTED"; | |
932 | default: st_other_str = ""; | |
933 | } | |
934 | switch (isym->st_shndx) | |
935 | { | |
936 | case SHN_ABS: st_shndx_str = "SHN_ABS"; | |
937 | case SHN_COMMON: st_shndx_str = "SHN_COMMON"; | |
938 | case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; | |
939 | default: st_shndx_str = ""; | |
940 | } | |
941 | ||
942 | printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s " | |
943 | "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n", | |
944 | isym, | |
945 | (unsigned long) isym->st_value, | |
946 | (unsigned long) isym->st_size, | |
947 | isym->st_name, | |
948 | bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link, | |
949 | isym->st_name), | |
950 | isym->st_info, st_info_str, st_info_stb_str, | |
951 | isym->st_other, st_other_str, | |
952 | isym->st_shndx, st_shndx_str); | |
953 | } | |
954 | if (free_internal) | |
955 | free (internal_syms); | |
956 | if (free_external) | |
957 | free (external_syms); | |
958 | } | |
959 | ||
e460dd0d | 960 | char * |
49f58d10 JB |
961 | m32c_get_reloc (long reloc) |
962 | { | |
963 | if (0 <= reloc && reloc < R_M32C_max) | |
964 | return m32c_elf_howto_table[reloc].name; | |
965 | else | |
966 | return ""; | |
967 | } | |
968 | #endif /* DEBUG */ | |
969 | ||
970 | /* Handle relaxing. */ | |
971 | ||
972 | /* A subroutine of m32c_elf_relax_section. If the global symbol H | |
973 | is within the low 64k, remove any entry for it in the plt. */ | |
974 | ||
975 | struct relax_plt_data | |
976 | { | |
977 | asection *splt; | |
978 | bfd_boolean *again; | |
979 | }; | |
980 | ||
981 | static bfd_boolean | |
982 | m32c_relax_plt_check (struct elf_link_hash_entry *h, | |
983 | PTR xdata) | |
984 | { | |
985 | struct relax_plt_data *data = (struct relax_plt_data *) xdata; | |
986 | ||
987 | if (h->root.type == bfd_link_hash_warning) | |
988 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
989 | ||
990 | if (h->plt.offset != (bfd_vma) -1) | |
991 | { | |
992 | bfd_vma address; | |
993 | ||
994 | if (h->root.type == bfd_link_hash_undefined | |
995 | || h->root.type == bfd_link_hash_undefweak) | |
996 | address = 0; | |
997 | else | |
998 | address = (h->root.u.def.section->output_section->vma | |
999 | + h->root.u.def.section->output_offset | |
1000 | + h->root.u.def.value); | |
1001 | ||
1002 | if (address <= 0xffff) | |
1003 | { | |
1004 | h->plt.offset = -1; | |
1005 | data->splt->size -= 4; | |
1006 | *data->again = TRUE; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | return TRUE; | |
1011 | } | |
1012 | ||
1013 | /* A subroutine of m32c_elf_relax_section. If the global symbol H | |
1014 | previously had a plt entry, give it a new entry offset. */ | |
1015 | ||
1016 | static bfd_boolean | |
1017 | m32c_relax_plt_realloc (struct elf_link_hash_entry *h, | |
1018 | PTR xdata) | |
1019 | { | |
1020 | bfd_vma *entry = (bfd_vma *) xdata; | |
1021 | ||
1022 | if (h->root.type == bfd_link_hash_warning) | |
1023 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1024 | ||
1025 | if (h->plt.offset != (bfd_vma) -1) | |
1026 | { | |
1027 | h->plt.offset = *entry; | |
1028 | *entry += 4; | |
1029 | } | |
1030 | ||
1031 | return TRUE; | |
1032 | } | |
1033 | ||
1034 | static bfd_boolean | |
1035 | m32c_elf_relax_plt_section (bfd *dynobj, | |
1036 | asection *splt, | |
1037 | struct bfd_link_info *info, | |
1038 | bfd_boolean *again) | |
1039 | { | |
1040 | struct relax_plt_data relax_plt_data; | |
1041 | bfd *ibfd; | |
1042 | ||
1043 | /* Assume nothing changes. */ | |
1044 | *again = FALSE; | |
1045 | ||
1046 | if (info->relocatable) | |
1047 | return TRUE; | |
1048 | ||
1049 | /* We only relax the .plt section at the moment. */ | |
1050 | if (dynobj != elf_hash_table (info)->dynobj | |
1051 | || strcmp (splt->name, ".plt") != 0) | |
1052 | return TRUE; | |
1053 | ||
1054 | /* Quick check for an empty plt. */ | |
1055 | if (splt->size == 0) | |
1056 | return TRUE; | |
1057 | ||
1058 | /* Map across all global symbols; see which ones happen to | |
1059 | fall in the low 64k. */ | |
1060 | relax_plt_data.splt = splt; | |
1061 | relax_plt_data.again = again; | |
1062 | elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check, | |
1063 | &relax_plt_data); | |
1064 | ||
1065 | /* Likewise for local symbols, though that's somewhat less convenient | |
1066 | as we have to walk the list of input bfds and swap in symbol data. */ | |
1067 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) | |
1068 | { | |
1069 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); | |
1070 | Elf_Internal_Shdr *symtab_hdr; | |
1071 | Elf_Internal_Sym *isymbuf = NULL; | |
1072 | unsigned int idx; | |
1073 | ||
1074 | if (! local_plt_offsets) | |
1075 | continue; | |
1076 | ||
1077 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1078 | if (symtab_hdr->sh_info != 0) | |
1079 | { | |
1080 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1081 | if (isymbuf == NULL) | |
1082 | isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr, | |
1083 | symtab_hdr->sh_info, 0, | |
1084 | NULL, NULL, NULL); | |
1085 | if (isymbuf == NULL) | |
1086 | return FALSE; | |
1087 | } | |
1088 | ||
1089 | for (idx = 0; idx < symtab_hdr->sh_info; ++idx) | |
1090 | { | |
1091 | Elf_Internal_Sym *isym; | |
1092 | asection *tsec; | |
1093 | bfd_vma address; | |
1094 | ||
1095 | if (local_plt_offsets[idx] == (bfd_vma) -1) | |
1096 | continue; | |
1097 | ||
1098 | isym = &isymbuf[idx]; | |
1099 | if (isym->st_shndx == SHN_UNDEF) | |
1100 | continue; | |
1101 | else if (isym->st_shndx == SHN_ABS) | |
1102 | tsec = bfd_abs_section_ptr; | |
1103 | else if (isym->st_shndx == SHN_COMMON) | |
1104 | tsec = bfd_com_section_ptr; | |
1105 | else | |
1106 | tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx); | |
1107 | ||
1108 | address = (tsec->output_section->vma | |
1109 | + tsec->output_offset | |
1110 | + isym->st_value); | |
1111 | if (address <= 0xffff) | |
1112 | { | |
1113 | local_plt_offsets[idx] = -1; | |
1114 | splt->size -= 4; | |
1115 | *again = TRUE; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | if (isymbuf != NULL | |
1120 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1121 | { | |
1122 | if (! info->keep_memory) | |
1123 | free (isymbuf); | |
1124 | else | |
1125 | { | |
1126 | /* Cache the symbols for elf_link_input_bfd. */ | |
1127 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | /* If we changed anything, walk the symbols again to reallocate | |
1133 | .plt entry addresses. */ | |
1134 | if (*again && splt->size > 0) | |
1135 | { | |
1136 | bfd_vma entry = 0; | |
1137 | ||
1138 | elf_link_hash_traverse (elf_hash_table (info), | |
1139 | m32c_relax_plt_realloc, &entry); | |
1140 | ||
1141 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) | |
1142 | { | |
1143 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); | |
1144 | unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info; | |
1145 | unsigned int idx; | |
1146 | ||
1147 | if (! local_plt_offsets) | |
1148 | continue; | |
1149 | ||
1150 | for (idx = 0; idx < nlocals; ++idx) | |
1151 | if (local_plt_offsets[idx] != (bfd_vma) -1) | |
1152 | { | |
1153 | local_plt_offsets[idx] = entry; | |
1154 | entry += 4; | |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | return TRUE; | |
1160 | } | |
1161 | ||
6772dd07 DD |
1162 | static int |
1163 | compare_reloc (const void *e1, const void *e2) | |
49f58d10 | 1164 | { |
6772dd07 DD |
1165 | const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1; |
1166 | const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2; | |
1167 | ||
1168 | if (i1->r_offset == i2->r_offset) | |
1169 | return 0; | |
1170 | else | |
1171 | return i1->r_offset < i2->r_offset ? -1 : 1; | |
1172 | } | |
1173 | ||
a1013894 | 1174 | #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms) |
6772dd07 DD |
1175 | static bfd_vma |
1176 | m32c_offset_for_reloc (bfd *abfd, | |
6772dd07 DD |
1177 | Elf_Internal_Rela *rel, |
1178 | Elf_Internal_Shdr *symtab_hdr, | |
1179 | Elf_External_Sym_Shndx *shndx_buf, | |
1180 | Elf_Internal_Sym *intsyms) | |
1181 | { | |
1182 | bfd_vma symval; | |
1183 | ||
1184 | /* Get the value of the symbol referred to by the reloc. */ | |
1185 | if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info) | |
49f58d10 | 1186 | { |
6772dd07 DD |
1187 | /* A local symbol. */ |
1188 | Elf_Internal_Sym *isym; | |
1189 | Elf_External_Sym_Shndx *shndx; | |
a1013894 DD |
1190 | asection *ssec; |
1191 | ||
6772dd07 DD |
1192 | |
1193 | isym = intsyms + ELF32_R_SYM (rel->r_info); | |
a1013894 | 1194 | ssec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
6772dd07 DD |
1195 | shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0); |
1196 | ||
a1013894 DD |
1197 | symval = isym->st_value; |
1198 | if (ssec) | |
1199 | symval += ssec->output_section->vma | |
1200 | + ssec->output_offset; | |
6772dd07 DD |
1201 | } |
1202 | else | |
49f58d10 | 1203 | { |
6772dd07 DD |
1204 | unsigned long indx; |
1205 | struct elf_link_hash_entry *h; | |
1206 | ||
1207 | /* An external symbol. */ | |
1208 | indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info; | |
1209 | h = elf_sym_hashes (abfd)[indx]; | |
1210 | BFD_ASSERT (h != NULL); | |
1211 | ||
1212 | if (h->root.type != bfd_link_hash_defined | |
1213 | && h->root.type != bfd_link_hash_defweak) | |
1214 | /* This appears to be a reference to an undefined | |
1215 | symbol. Just ignore it--it will be caught by the | |
1216 | regular reloc processing. */ | |
1217 | return 0; | |
1218 | ||
1219 | symval = (h->root.u.def.value | |
1220 | + h->root.u.def.section->output_section->vma | |
1221 | + h->root.u.def.section->output_offset); | |
49f58d10 | 1222 | } |
6772dd07 DD |
1223 | return symval; |
1224 | } | |
1225 | ||
1226 | static int bytes_saved = 0; | |
1227 | ||
1228 | static int bytes_to_reloc[] = { | |
1229 | R_M32C_NONE, | |
1230 | R_M32C_8, | |
1231 | R_M32C_16, | |
1232 | R_M32C_24, | |
1233 | R_M32C_32 | |
1234 | }; | |
1235 | ||
1236 | /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */ | |
1237 | ||
1238 | /* Mask for the number of relocs associated with this insn. */ | |
1239 | #define RLA_RELOCS 0x0000000f | |
1240 | /* Number of bytes gas emitted (before gas's relaxing) */ | |
1241 | #define RLA_NBYTES 0x00000ff0 | |
1242 | ||
1243 | /* If the displacement is within the given range and the new encoding | |
1244 | differs from the old encoding (the index), then the insn can be | |
1245 | relaxed to the new encoding. */ | |
1246 | typedef struct { | |
1247 | int bytes; | |
1248 | unsigned int max_disp; | |
1249 | unsigned char new_encoding; | |
1250 | } EncodingTable; | |
1251 | ||
1252 | static EncodingTable m16c_addr_encodings[] = { | |
1253 | { 0, 0, 0 }, /* R0 */ | |
1254 | { 0, 0, 1 }, /* R1 */ | |
1255 | { 0, 0, 2 }, /* R2 */ | |
1256 | { 0, 0, 3 }, /* R3 */ | |
1257 | { 0, 0, 4 }, /* A0 */ | |
1258 | { 0, 0, 5 }, /* A1 */ | |
1259 | { 0, 0, 6 }, /* [A0] */ | |
1260 | { 0, 0, 7 }, /* [A1] */ | |
1261 | { 1, 0, 6 }, /* udsp:8[A0] */ | |
1262 | { 1, 0, 7 }, /* udsp:8[A1] */ | |
1263 | { 1, 0, 10 }, /* udsp:8[SB] */ | |
1264 | { 1, 0, 11 }, /* sdsp:8[FB] */ | |
1265 | { 2, 255, 8 }, /* udsp:16[A0] */ | |
1266 | { 2, 255, 9 }, /* udsp:16[A1] */ | |
1267 | { 2, 255, 10 }, /* udsp:16[SB] */ | |
1268 | { 2, 0, 15 }, /* abs:16 */ | |
1269 | }; | |
1270 | ||
1271 | static EncodingTable m16c_jmpaddr_encodings[] = { | |
1272 | { 0, 0, 0 }, /* R0 */ | |
1273 | { 0, 0, 1 }, /* R1 */ | |
1274 | { 0, 0, 2 }, /* R2 */ | |
1275 | { 0, 0, 3 }, /* R3 */ | |
1276 | { 0, 0, 4 }, /* A0 */ | |
1277 | { 0, 0, 5 }, /* A1 */ | |
1278 | { 0, 0, 6 }, /* [A0] */ | |
1279 | { 0, 0, 7 }, /* [A1] */ | |
1280 | { 1, 0, 6 }, /* udsp:8[A0] */ | |
1281 | { 1, 0, 7 }, /* udsp:8[A1] */ | |
1282 | { 1, 0, 10 }, /* udsp:8[SB] */ | |
1283 | { 1, 0, 11 }, /* sdsp:8[FB] */ | |
1284 | { 3, 255, 8 }, /* udsp:20[A0] */ | |
1285 | { 3, 255, 9 }, /* udsp:20[A1] */ | |
1286 | { 2, 255, 10 }, /* udsp:16[SB] */ | |
1287 | { 2, 0, 15 }, /* abs:16 */ | |
1288 | }; | |
1289 | ||
1290 | static EncodingTable m32c_addr_encodings[] = { | |
1291 | { 0, 0, 0 }, /* [A0] */ | |
1292 | { 0, 0, 1 }, /* [A1] */ | |
1293 | { 0, 0, 2 }, /* A0 */ | |
1294 | { 0, 0, 3 }, /* A1 */ | |
1295 | { 1, 0, 0 }, /* udsp:8[A0] */ | |
1296 | { 1, 0, 1 }, /* udsp:8[A1] */ | |
1297 | { 1, 0, 6 }, /* udsp:8[SB] */ | |
1298 | { 1, 0, 7 }, /* sdsp:8[FB] */ | |
1299 | { 2, 255, 4 }, /* udsp:16[A0] */ | |
1300 | { 2, 255, 5 }, /* udsp:16[A1] */ | |
1301 | { 2, 255, 6 }, /* udsp:16[SB] */ | |
1302 | { 2, 127, 7 }, /* sdsp:16[FB] */ | |
1303 | { 3, 65535, 8 }, /* udsp:24[A0] */ | |
1304 | { 3, 65535, 9 }, /* udsp:24[A1] */ | |
1305 | { 3, 65535, 15 }, /* abs24 */ | |
1306 | { 2, 0, 15 }, /* abs16 */ | |
1307 | { 0, 0, 16 }, /* R2 */ | |
1308 | { 0, 0, 17 }, /* R3 */ | |
1309 | { 0, 0, 18 }, /* R0 */ | |
1310 | { 0, 0, 19 }, /* R1 */ | |
1311 | { 0, 0, 20 }, /* */ | |
1312 | { 0, 0, 21 }, /* */ | |
1313 | { 0, 0, 22 }, /* */ | |
1314 | { 0, 0, 23 }, /* */ | |
1315 | { 0, 0, 24 }, /* */ | |
1316 | { 0, 0, 25 }, /* */ | |
1317 | { 0, 0, 26 }, /* */ | |
1318 | { 0, 0, 27 }, /* */ | |
1319 | { 0, 0, 28 }, /* */ | |
1320 | { 0, 0, 29 }, /* */ | |
1321 | { 0, 0, 30 }, /* */ | |
1322 | { 0, 0, 31 }, /* */ | |
1323 | }; | |
1324 | ||
49f58d10 JB |
1325 | static bfd_boolean |
1326 | m32c_elf_relax_section | |
1327 | (bfd * abfd, | |
1328 | asection * sec, | |
1329 | struct bfd_link_info * link_info, | |
1330 | bfd_boolean * again) | |
1331 | { | |
1332 | Elf_Internal_Shdr *symtab_hdr; | |
1333 | Elf_Internal_Shdr *shndx_hdr; | |
1334 | Elf_Internal_Rela *internal_relocs; | |
1335 | Elf_Internal_Rela *free_relocs = NULL; | |
6772dd07 | 1336 | Elf_Internal_Rela *irel, *irelend, *srel; |
49f58d10 JB |
1337 | bfd_byte * contents = NULL; |
1338 | bfd_byte * free_contents = NULL; | |
6772dd07 DD |
1339 | Elf_Internal_Sym *intsyms = NULL; |
1340 | Elf_Internal_Sym *free_intsyms = NULL; | |
49f58d10 JB |
1341 | Elf_External_Sym_Shndx *shndx_buf = NULL; |
1342 | int machine; | |
1343 | ||
1344 | if (abfd == elf_hash_table (link_info)->dynobj | |
1345 | && strcmp (sec->name, ".plt") == 0) | |
1346 | return m32c_elf_relax_plt_section (abfd, sec, link_info, again); | |
1347 | ||
1348 | /* Assume nothing changes. */ | |
1349 | *again = FALSE; | |
1350 | ||
1351 | machine = elf32_m32c_machine (abfd); | |
1352 | ||
1353 | /* We don't have to do anything for a relocatable link, if | |
1354 | this section does not have relocs, or if this is not a | |
1355 | code section. */ | |
1356 | if (link_info->relocatable | |
1357 | || (sec->flags & SEC_RELOC) == 0 | |
1358 | || sec->reloc_count == 0 | |
1359 | || (sec->flags & SEC_CODE) == 0) | |
1360 | return TRUE; | |
1361 | ||
49f58d10 JB |
1362 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1363 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1364 | ||
6772dd07 DD |
1365 | /* Get the section contents. */ |
1366 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
1367 | contents = elf_section_data (sec)->this_hdr.contents; | |
1368 | /* Go get them off disk. */ | |
1369 | else if (!bfd_malloc_and_get_section (abfd, sec, &contents)) | |
1370 | goto error_return; | |
1371 | ||
1372 | /* Read this BFD's symbols. */ | |
1373 | /* Get cached copy if it exists. */ | |
1374 | if (symtab_hdr->contents != NULL) | |
1375 | { | |
1376 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1377 | } | |
1378 | else | |
1379 | { | |
1380 | intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL); | |
1381 | symtab_hdr->contents = (bfd_byte *) intsyms; | |
1382 | } | |
1383 | ||
1384 | if (shndx_hdr->sh_size != 0) | |
1385 | { | |
1386 | bfd_size_type amt; | |
1387 | ||
1388 | amt = symtab_hdr->sh_info; | |
1389 | amt *= sizeof (Elf_External_Sym_Shndx); | |
1390 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
1391 | if (shndx_buf == NULL) | |
1392 | goto error_return; | |
1393 | if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 | |
1394 | || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt) | |
1395 | goto error_return; | |
1396 | shndx_hdr->contents = (bfd_byte *) shndx_buf; | |
1397 | } | |
1398 | ||
49f58d10 JB |
1399 | /* Get a copy of the native relocations. */ |
1400 | internal_relocs = (_bfd_elf_link_read_relocs | |
1401 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
1402 | link_info->keep_memory)); | |
1403 | if (internal_relocs == NULL) | |
1404 | goto error_return; | |
1405 | if (! link_info->keep_memory) | |
1406 | free_relocs = internal_relocs; | |
1407 | ||
6772dd07 DD |
1408 | /* The RL_ relocs must be just before the operand relocs they go |
1409 | with, so we must sort them to guarantee this. */ | |
1410 | qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela), | |
1411 | compare_reloc); | |
1412 | ||
49f58d10 JB |
1413 | /* Walk through them looking for relaxing opportunities. */ |
1414 | irelend = internal_relocs + sec->reloc_count; | |
1415 | ||
1416 | for (irel = internal_relocs; irel < irelend; irel++) | |
1417 | { | |
1418 | bfd_vma symval; | |
6772dd07 | 1419 | unsigned char *insn, *gap, *einsn; |
49f58d10 | 1420 | bfd_vma pc; |
6772dd07 DD |
1421 | bfd_signed_vma pcrel; |
1422 | int relax_relocs; | |
1423 | int gap_size; | |
1424 | int new_type; | |
1425 | int posn; | |
1426 | int enc; | |
1427 | EncodingTable *enctbl; | |
1428 | EncodingTable *e; | |
1429 | ||
1430 | if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP | |
1431 | && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR | |
1432 | && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR) | |
1433 | continue; | |
49f58d10 | 1434 | |
6772dd07 | 1435 | srel = irel; |
49f58d10 | 1436 | |
6772dd07 DD |
1437 | /* There will always be room for the relaxed insn, since it is smaller |
1438 | than the one it would replace. */ | |
1439 | BFD_ASSERT (irel->r_offset < sec->size); | |
1440 | ||
1441 | insn = contents + irel->r_offset; | |
1442 | relax_relocs = irel->r_addend % 16; | |
1443 | ||
1444 | /* Ok, we only have three relocs we care about, and they're all | |
1445 | fake. The lower four bits of the addend is always the number | |
1446 | of following relocs (hence the qsort above) that are assigned | |
1447 | to this opcode. The next 8 bits of the addend indicates the | |
1448 | number of bytes in the insn. We use the rest of them | |
1449 | ourselves as flags for the more expensive operations (defines | |
1450 | above). The three relocs are: | |
1451 | ||
1452 | RL_JUMP: This marks all direct jump insns. We check the | |
1453 | displacement and replace them with shorter jumps if | |
1454 | they're in range. We also use this to find JMP.S | |
1455 | insns and manually shorten them when we delete bytes. | |
1456 | We have to decode these insns to figure out what to | |
1457 | do. | |
1458 | ||
1459 | RL_1ADDR: This is a :G or :Q insn, which has a single | |
1460 | "standard" operand. We have to extract the type | |
1461 | field, see if it's a wide displacement, then figure | |
1462 | out if we can replace it with a narrow displacement. | |
1463 | We don't have to decode these insns. | |
1464 | ||
1465 | RL_2ADDR: Similarly, but two "standard" operands. Note that | |
1466 | r_addend may still be 1, as standard operands don't | |
1467 | always have displacements. Gas shouldn't give us one | |
1468 | with zero operands, but since we don't know which one | |
1469 | has the displacement, we check them both anyway. | |
1470 | ||
1471 | These all point to the beginning of the insn itself, not the | |
1472 | operands. | |
1473 | ||
1474 | Note that we only relax one step at a time, relying on the | |
1475 | linker to call us repeatedly. Thus, there is no code for | |
1476 | JMP.A->JMP.B although that will happen in two steps. | |
1477 | Likewise, for 2ADDR relaxes, we do one operand per cycle. | |
1478 | */ | |
1479 | ||
1480 | /* Get the value of the symbol referred to by the reloc. Just | |
1481 | in case this is the last reloc in the list, use the RL's | |
1482 | addend to choose between this reloc (no addend) or the next | |
1483 | (yes addend, which means at least one following reloc). */ | |
1484 | srel = irel + (relax_relocs ? 1 : 0); | |
1485 | symval = OFFSET_FOR_RELOC (srel); | |
1486 | ||
1487 | /* Setting gap_size nonzero is the flag which means "something | |
1488 | shrunk". */ | |
1489 | gap_size = 0; | |
e970df31 | 1490 | gap = NULL; |
6772dd07 DD |
1491 | new_type = ELF32_R_TYPE(srel->r_info); |
1492 | ||
1493 | pc = sec->output_section->vma + sec->output_offset | |
1494 | + srel->r_offset; | |
1495 | pcrel = symval - pc + srel->r_addend; | |
1496 | ||
1497 | if (machine == bfd_mach_m16c) | |
49f58d10 | 1498 | { |
6772dd07 | 1499 | /* R8C / M16C */ |
49f58d10 | 1500 | |
6772dd07 | 1501 | switch (ELF32_R_TYPE(irel->r_info)) |
49f58d10 | 1502 | { |
49f58d10 | 1503 | |
6772dd07 DD |
1504 | case R_M32C_RL_JUMP: |
1505 | switch (insn[0]) | |
1506 | { | |
1507 | case 0xfe: /* jmp.b */ | |
1508 | if (pcrel >= 2 && pcrel <= 9) | |
1509 | { | |
1510 | /* Relax JMP.B -> JMP.S. We need to get rid of | |
1511 | the following reloc though. */ | |
1512 | insn[0] = 0x60 | (pcrel - 2); | |
1513 | new_type = R_M32C_NONE; | |
1514 | irel->r_addend = 0x10; | |
1515 | gap_size = 1; | |
1516 | gap = insn + 1; | |
1517 | } | |
1518 | break; | |
1519 | ||
1520 | case 0xf4: /* jmp.w */ | |
1521 | /* 128 is allowed because it will be one byte closer | |
1522 | after relaxing. Likewise for all other pc-rel | |
1523 | jumps. */ | |
1524 | if (pcrel <= 128 && pcrel >= -128) | |
1525 | { | |
1526 | /* Relax JMP.W -> JMP.B */ | |
1527 | insn[0] = 0xfe; | |
1528 | insn[1] = 0; | |
1529 | new_type = R_M32C_8_PCREL; | |
1530 | gap_size = 1; | |
1531 | gap = insn + 2; | |
1532 | } | |
1533 | break; | |
1534 | ||
1535 | case 0xfc: /* jmp.a */ | |
1536 | if (pcrel <= 32768 && pcrel >= -32768) | |
1537 | { | |
1538 | /* Relax JMP.A -> JMP.W */ | |
1539 | insn[0] = 0xf4; | |
1540 | insn[1] = 0; | |
1541 | insn[2] = 0; | |
1542 | new_type = R_M32C_16_PCREL; | |
1543 | gap_size = 1; | |
1544 | gap = insn + 3; | |
1545 | } | |
1546 | break; | |
1547 | ||
1548 | case 0xfd: /* jsr.a */ | |
1549 | if (pcrel <= 32768 && pcrel >= -32768) | |
1550 | { | |
1551 | /* Relax JSR.A -> JSR.W */ | |
1552 | insn[0] = 0xf5; | |
1553 | insn[1] = 0; | |
1554 | insn[2] = 0; | |
1555 | new_type = R_M32C_16_PCREL; | |
1556 | gap_size = 1; | |
1557 | gap = insn + 3; | |
1558 | } | |
1559 | break; | |
1560 | } | |
1561 | break; | |
49f58d10 | 1562 | |
6772dd07 DD |
1563 | case R_M32C_RL_2ADDR: |
1564 | /* xxxx xxxx srce dest [src-disp] [dest-disp]*/ | |
49f58d10 | 1565 | |
6772dd07 DD |
1566 | enctbl = m16c_addr_encodings; |
1567 | posn = 2; | |
1568 | enc = (insn[1] >> 4) & 0x0f; | |
1569 | e = & enctbl[enc]; | |
1570 | ||
1571 | if (srel->r_offset == irel->r_offset + posn | |
1572 | && e->new_encoding != enc | |
1573 | && symval <= e->max_disp) | |
1574 | { | |
1575 | insn[1] &= 0x0f; | |
1576 | insn[1] |= e->new_encoding << 4; | |
1577 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1578 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1579 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1580 | break; | |
1581 | } | |
1582 | if (relax_relocs == 2) | |
1583 | srel ++; | |
1584 | posn += e->bytes; | |
1585 | ||
1586 | goto try_1addr_16; | |
49f58d10 | 1587 | |
6772dd07 DD |
1588 | case R_M32C_RL_1ADDR: |
1589 | /* xxxx xxxx xxxx dest [disp] */ | |
49f58d10 | 1590 | |
6772dd07 DD |
1591 | enctbl = m16c_addr_encodings; |
1592 | posn = 2; | |
1593 | ||
1594 | /* Check the opcode for jumps. We know it's safe to | |
1595 | do this because all 2ADDR insns are at least two | |
1596 | bytes long. */ | |
1597 | enc = insn[0] * 256 + insn[1]; | |
1598 | enc &= 0xfff0; | |
1599 | if (enc == 0x7d20 | |
1600 | || enc == 0x7d00 | |
1601 | || enc == 0x7d30 | |
1602 | || enc == 0x7d10) | |
1603 | { | |
1604 | enctbl = m16c_jmpaddr_encodings; | |
1605 | } | |
1606 | ||
1607 | try_1addr_16: | |
1608 | /* srel, posn, and enc must be set here. */ | |
1609 | ||
1610 | symval = OFFSET_FOR_RELOC (srel); | |
1611 | enc = insn[1] & 0x0f; | |
1612 | e = & enctbl[enc]; | |
1613 | ||
1614 | if (srel->r_offset == irel->r_offset + posn | |
1615 | && e->new_encoding != enc | |
1616 | && symval <= e->max_disp) | |
1617 | { | |
1618 | insn[1] &= 0xf0; | |
1619 | insn[1] |= e->new_encoding; | |
1620 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1621 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1622 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1623 | break; | |
1624 | } | |
1625 | ||
1626 | break; | |
1627 | ||
1628 | } /* Ends switch (reloc type) for m16c. */ | |
1629 | } | |
1630 | else /* machine == bfd_mach_m32c */ | |
49f58d10 | 1631 | { |
6772dd07 | 1632 | /* M32CM / M32C */ |
49f58d10 | 1633 | |
6772dd07 | 1634 | switch (ELF32_R_TYPE(irel->r_info)) |
49f58d10 | 1635 | { |
49f58d10 | 1636 | |
6772dd07 DD |
1637 | case R_M32C_RL_JUMP: |
1638 | switch (insn[0]) | |
1639 | { | |
1640 | case 0xbb: /* jmp.b */ | |
1641 | if (pcrel >= 2 && pcrel <= 9) | |
1642 | { | |
1643 | int p = pcrel - 2; | |
1644 | /* Relax JMP.B -> JMP.S. We need to get rid of | |
1645 | the following reloc though. */ | |
1646 | insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1); | |
1647 | new_type = R_M32C_NONE; | |
1648 | irel->r_addend = 0x10; | |
1649 | gap_size = 1; | |
1650 | gap = insn + 1; | |
1651 | } | |
1652 | break; | |
1653 | ||
1654 | case 0xce: /* jmp.w */ | |
1655 | if (pcrel <= 128 && pcrel >= -128) | |
1656 | { | |
1657 | /* Relax JMP.W -> JMP.B */ | |
1658 | insn[0] = 0xbb; | |
1659 | insn[1] = 0; | |
1660 | new_type = R_M32C_8_PCREL; | |
1661 | gap_size = 1; | |
1662 | gap = insn + 2; | |
1663 | } | |
1664 | break; | |
1665 | ||
1666 | case 0xcc: /* jmp.a */ | |
1667 | if (pcrel <= 32768 && pcrel >= -32768) | |
1668 | { | |
1669 | /* Relax JMP.A -> JMP.W */ | |
1670 | insn[0] = 0xce; | |
1671 | insn[1] = 0; | |
1672 | insn[2] = 0; | |
1673 | new_type = R_M32C_16_PCREL; | |
1674 | gap_size = 1; | |
1675 | gap = insn + 3; | |
1676 | } | |
1677 | break; | |
1678 | ||
1679 | case 0xcd: /* jsr.a */ | |
1680 | if (pcrel <= 32768 && pcrel >= -32768) | |
1681 | { | |
1682 | /* Relax JSR.A -> JSR.W */ | |
1683 | insn[0] = 0xcf; | |
1684 | insn[1] = 0; | |
1685 | insn[2] = 0; | |
1686 | new_type = R_M32C_16_PCREL; | |
1687 | gap_size = 1; | |
1688 | gap = insn + 3; | |
1689 | } | |
1690 | break; | |
1691 | } | |
1692 | break; | |
49f58d10 | 1693 | |
6772dd07 DD |
1694 | case R_M32C_RL_2ADDR: |
1695 | /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/ | |
49f58d10 | 1696 | |
6772dd07 DD |
1697 | einsn = insn; |
1698 | posn = 2; | |
1699 | if (einsn[0] == 1) | |
1700 | { | |
1701 | /* prefix; remove it as far as the RL reloc is concerned. */ | |
1702 | einsn ++; | |
1703 | posn ++; | |
1704 | } | |
49f58d10 | 1705 | |
6772dd07 DD |
1706 | enctbl = m32c_addr_encodings; |
1707 | enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4); | |
1708 | e = & enctbl[enc]; | |
49f58d10 | 1709 | |
6772dd07 DD |
1710 | if (srel->r_offset == irel->r_offset + posn |
1711 | && e->new_encoding != enc | |
1712 | && symval <= e->max_disp) | |
1713 | { | |
1714 | einsn[0] &= 0x8f; | |
1715 | einsn[0] |= (e->new_encoding & 0x1c) << 2; | |
1716 | einsn[1] &= 0xcf; | |
1717 | einsn[1] |= (e->new_encoding & 0x03) << 4; | |
1718 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1719 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1720 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1721 | break; | |
1722 | } | |
1723 | if (relax_relocs == 2) | |
1724 | srel ++; | |
1725 | posn += e->bytes; | |
1726 | ||
1727 | goto try_1addr_32; | |
1728 | ||
1729 | case R_M32C_RL_1ADDR: | |
1730 | /* xxxx DDDx DDxx xxxx [disp] */ | |
1731 | ||
1732 | einsn = insn; | |
1733 | posn = 2; | |
1734 | if (einsn[0] == 1) | |
1735 | { | |
1736 | /* prefix; remove it as far as the RL reloc is concerned. */ | |
1737 | einsn ++; | |
1738 | posn ++; | |
1739 | } | |
1740 | ||
1741 | enctbl = m32c_addr_encodings; | |
49f58d10 | 1742 | |
6772dd07 DD |
1743 | try_1addr_32: |
1744 | /* srel, posn, and enc must be set here. */ | |
49f58d10 | 1745 | |
6772dd07 DD |
1746 | symval = OFFSET_FOR_RELOC (srel); |
1747 | enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6); | |
1748 | e = & enctbl[enc]; | |
49f58d10 | 1749 | |
6772dd07 DD |
1750 | if (srel->r_offset == irel->r_offset + posn |
1751 | && e->new_encoding != enc | |
1752 | && symval <= e->max_disp) | |
1753 | { | |
1754 | einsn[0] &= 0xf1; | |
1755 | einsn[0] |= (e->new_encoding & 0x1c) >> 1; | |
1756 | einsn[1] &= 0x3f; | |
1757 | einsn[1] |= (e->new_encoding & 0x03) << 6; | |
1758 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1759 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1760 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1761 | break; | |
1762 | } | |
1763 | ||
1764 | break; | |
1765 | ||
1766 | } /* Ends switch (reloc type) for m32c. */ | |
1767 | } | |
1768 | ||
1769 | if (gap_size == 0) | |
1770 | continue; | |
1771 | ||
1772 | *again = TRUE; | |
1773 | ||
1774 | srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type); | |
1775 | ||
1776 | /* Note that we've changed the relocs, section contents, etc. */ | |
1777 | elf_section_data (sec)->relocs = internal_relocs; | |
1778 | free_relocs = NULL; | |
1779 | ||
1780 | elf_section_data (sec)->this_hdr.contents = contents; | |
1781 | free_contents = NULL; | |
1782 | ||
1783 | symtab_hdr->contents = (bfd_byte *) intsyms; | |
1784 | free_intsyms = NULL; | |
1785 | ||
1786 | bytes_saved += gap_size; | |
1787 | ||
1788 | if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size)) | |
1789 | goto error_return; | |
49f58d10 | 1790 | |
49f58d10 JB |
1791 | } /* next relocation */ |
1792 | ||
1793 | if (free_relocs != NULL) | |
1794 | { | |
1795 | free (free_relocs); | |
1796 | free_relocs = NULL; | |
1797 | } | |
1798 | ||
1799 | if (free_contents != NULL) | |
1800 | { | |
1801 | if (! link_info->keep_memory) | |
1802 | free (free_contents); | |
1803 | /* Cache the section contents for elf_link_input_bfd. */ | |
1804 | else | |
1805 | elf_section_data (sec)->this_hdr.contents = contents; | |
1806 | ||
1807 | free_contents = NULL; | |
1808 | } | |
1809 | ||
1810 | if (shndx_buf != NULL) | |
1811 | { | |
1812 | shndx_hdr->contents = NULL; | |
1813 | free (shndx_buf); | |
1814 | } | |
1815 | ||
6772dd07 | 1816 | if (free_intsyms != NULL) |
49f58d10 JB |
1817 | { |
1818 | if (! link_info->keep_memory) | |
6772dd07 | 1819 | free (free_intsyms); |
49f58d10 JB |
1820 | /* Cache the symbols for elf_link_input_bfd. */ |
1821 | else | |
6772dd07 DD |
1822 | { |
1823 | symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/; | |
1824 | } | |
49f58d10 | 1825 | |
6772dd07 | 1826 | free_intsyms = NULL; |
49f58d10 | 1827 | } |
49f58d10 JB |
1828 | |
1829 | return TRUE; | |
1830 | ||
1831 | error_return: | |
1832 | if (free_relocs != NULL) | |
1833 | free (free_relocs); | |
1834 | if (free_contents != NULL) | |
1835 | free (free_contents); | |
1836 | if (shndx_buf != NULL) | |
1837 | { | |
1838 | shndx_hdr->contents = NULL; | |
1839 | free (shndx_buf); | |
1840 | } | |
6772dd07 DD |
1841 | if (free_intsyms != NULL) |
1842 | free (free_intsyms); | |
49f58d10 JB |
1843 | return FALSE; |
1844 | } | |
1845 | ||
1846 | /* Delete some bytes from a section while relaxing. */ | |
1847 | ||
1848 | static bfd_boolean | |
1849 | m32c_elf_relax_delete_bytes | |
1850 | (bfd * abfd, | |
1851 | asection * sec, | |
1852 | bfd_vma addr, | |
1853 | int count) | |
1854 | { | |
1855 | Elf_Internal_Shdr *symtab_hdr; | |
1856 | Elf_Internal_Shdr *shndx_hdr; | |
1857 | int sec_shndx; | |
1858 | bfd_byte *contents; | |
1859 | Elf_Internal_Rela *irel; | |
1860 | Elf_Internal_Rela *irelend; | |
1861 | Elf_Internal_Rela *irelalign; | |
1862 | bfd_vma toaddr; | |
6772dd07 DD |
1863 | Elf_Internal_Sym *isym; |
1864 | Elf_Internal_Sym *isymend; | |
1865 | Elf_Internal_Sym *intsyms; | |
49f58d10 JB |
1866 | Elf_External_Sym_Shndx *shndx_buf; |
1867 | Elf_External_Sym_Shndx *shndx; | |
1868 | struct elf_link_hash_entry ** sym_hashes; | |
1869 | struct elf_link_hash_entry ** end_hashes; | |
1870 | unsigned int symcount; | |
6772dd07 | 1871 | |
49f58d10 JB |
1872 | contents = elf_section_data (sec)->this_hdr.contents; |
1873 | ||
1874 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
1875 | power larger than the number of bytes we are deleting. */ | |
1876 | irelalign = NULL; | |
1877 | toaddr = sec->size; | |
1878 | ||
1879 | irel = elf_section_data (sec)->relocs; | |
1880 | irelend = irel + sec->reloc_count; | |
1881 | ||
1882 | /* Actually delete the bytes. */ | |
1883 | memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count)); | |
1884 | sec->size -= count; | |
1885 | ||
1886 | /* Adjust all the relocs. */ | |
1887 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++) | |
1888 | { | |
1889 | /* Get the new reloc address. */ | |
1890 | if (irel->r_offset > addr && irel->r_offset < toaddr) | |
1891 | irel->r_offset -= count; | |
6772dd07 DD |
1892 | |
1893 | if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP | |
1894 | && irel->r_addend == 0x10 /* one byte insn, no relocs */ | |
1895 | && irel->r_offset + 1 < addr | |
1896 | && irel->r_offset + 7 > addr) | |
1897 | { | |
1898 | bfd_vma disp; | |
1899 | unsigned char *insn = &contents[irel->r_offset]; | |
1900 | disp = *insn; | |
1901 | /* This is a JMP.S, which we have to manually update. */ | |
1902 | if (elf32_m32c_machine (abfd) == bfd_mach_m16c) | |
1903 | { | |
1904 | if ((*insn & 0xf8) != 0x60) | |
1905 | continue; | |
1906 | disp = (disp & 7); | |
1907 | } | |
1908 | else | |
1909 | { | |
1910 | if ((*insn & 0xce) != 0x4a) | |
1911 | continue; | |
1912 | disp = ((disp & 0x30) >> 3) | (disp & 1); | |
1913 | } | |
1914 | if (irel->r_offset + disp + 2 >= addr+count) | |
1915 | { | |
1916 | disp -= count; | |
1917 | if (elf32_m32c_machine (abfd) == bfd_mach_m16c) | |
1918 | { | |
1919 | *insn = (*insn & 0xf8) | disp; | |
1920 | } | |
1921 | else | |
1922 | { | |
1923 | *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1); | |
1924 | } | |
1925 | } | |
1926 | } | |
49f58d10 JB |
1927 | } |
1928 | ||
1929 | /* Adjust the local symbols defined in this section. */ | |
6772dd07 DD |
1930 | symtab_hdr = & elf_tdata (abfd)->symtab_hdr; |
1931 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1932 | isym = intsyms; | |
1933 | isymend = isym + symtab_hdr->sh_info; | |
1934 | ||
1935 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1936 | shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr; | |
1937 | shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
49f58d10 | 1938 | shndx = shndx_buf; |
49f58d10 | 1939 | |
6772dd07 DD |
1940 | for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL)) |
1941 | { | |
49f58d10 | 1942 | |
6772dd07 DD |
1943 | if ((int) isym->st_shndx == sec_shndx |
1944 | && isym->st_value > addr | |
1945 | && isym->st_value < toaddr) | |
49f58d10 | 1946 | { |
6772dd07 | 1947 | isym->st_value -= count; |
49f58d10 JB |
1948 | } |
1949 | } | |
1950 | ||
1951 | /* Now adjust the global symbols defined in this section. */ | |
1952 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
1953 | - symtab_hdr->sh_info); | |
1954 | sym_hashes = elf_sym_hashes (abfd); | |
1955 | // sym_hashes += symtab_hdr->sh_info; | |
1956 | end_hashes = sym_hashes + symcount; | |
1957 | ||
1958 | for (; sym_hashes < end_hashes; sym_hashes ++) | |
1959 | { | |
1960 | struct elf_link_hash_entry * sym_hash = * sym_hashes; | |
1961 | ||
1962 | if (sym_hash && | |
1963 | ( sym_hash->root.type == bfd_link_hash_defined | |
1964 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1965 | && sym_hash->root.u.def.section == sec | |
1966 | && sym_hash->root.u.def.value > addr | |
1967 | && sym_hash->root.u.def.value < toaddr) | |
6772dd07 DD |
1968 | { |
1969 | sym_hash->root.u.def.value -= count; | |
1970 | } | |
49f58d10 JB |
1971 | } |
1972 | ||
1973 | return TRUE; | |
1974 | } | |
1975 | \f | |
1976 | ||
1977 | #define ELF_ARCH bfd_arch_m32c | |
1978 | #define ELF_MACHINE_CODE EM_M32C | |
1979 | #define ELF_MAXPAGESIZE 0x1000 | |
1980 | ||
1981 | #if 0 | |
1982 | #define TARGET_BIG_SYM bfd_elf32_m32c_vec | |
1983 | #define TARGET_BIG_NAME "elf32-m32c" | |
1984 | #else | |
1985 | #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec | |
1986 | #define TARGET_LITTLE_NAME "elf32-m32c" | |
1987 | #endif | |
1988 | ||
1989 | #define elf_info_to_howto_rel NULL | |
1990 | #define elf_info_to_howto m32c_info_to_howto_rela | |
1991 | #define elf_backend_object_p m32c_elf_object_p | |
1992 | #define elf_backend_relocate_section m32c_elf_relocate_section | |
49f58d10 JB |
1993 | #define elf_backend_check_relocs m32c_elf_check_relocs |
1994 | #define elf_backend_object_p m32c_elf_object_p | |
1995 | #define elf_symbol_leading_char ('_') | |
1996 | #define elf_backend_always_size_sections \ | |
1997 | m32c_elf_always_size_sections | |
1998 | #define elf_backend_finish_dynamic_sections \ | |
1999 | m32c_elf_finish_dynamic_sections | |
2000 | ||
2001 | #define elf_backend_can_gc_sections 1 | |
2002 | ||
2003 | #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup | |
2004 | #define bfd_elf32_bfd_relax_section m32c_elf_relax_section | |
2005 | #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags | |
2006 | #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data | |
2007 | #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data | |
2008 | ||
2009 | #include "elf32-target.h" |