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