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