Add -Wshadow to the gcc command line options used when compiling the binutils.
[binutils.git] / bfd / elf-m10300.c
blob4a9d4b051e0df36505cff1b86747444d82349091
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/mn10300.h"
27 #include "libiberty.h"
29 /* The mn10300 linker needs to keep track of the number of relocs that
30 it decides to copy in check_relocs for each symbol. This is so
31 that it can discard PC relative relocs if it doesn't need them when
32 linking with -Bsymbolic. We store the information in a field
33 extending the regular ELF linker hash table. */
35 struct elf32_mn10300_link_hash_entry
37 /* The basic elf link hash table entry. */
38 struct elf_link_hash_entry root;
40 /* For function symbols, the number of times this function is
41 called directly (ie by name). */
42 unsigned int direct_calls;
44 /* For function symbols, the size of this function's stack
45 (if <= 255 bytes). We stuff this into "call" instructions
46 to this target when it's valid and profitable to do so.
48 This does not include stack allocated by movm! */
49 unsigned char stack_size;
51 /* For function symbols, arguments (if any) for movm instruction
52 in the prologue. We stuff this value into "call" instructions
53 to the target when it's valid and profitable to do so. */
54 unsigned char movm_args;
56 /* For function symbols, the amount of stack space that would be allocated
57 by the movm instruction. This is redundant with movm_args, but we
58 add it to the hash table to avoid computing it over and over. */
59 unsigned char movm_stack_size;
61 /* When set, convert all "call" instructions to this target into "calls"
62 instructions. */
63 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
65 /* Used to mark functions which have had redundant parts of their
66 prologue deleted. */
67 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
68 unsigned char flags;
70 /* Calculated value. */
71 bfd_vma value;
74 /* We derive a hash table from the main elf linker hash table so
75 we can store state variables and a secondary hash table without
76 resorting to global variables. */
77 struct elf32_mn10300_link_hash_table
79 /* The main hash table. */
80 struct elf_link_hash_table root;
82 /* A hash table for static functions. We could derive a new hash table
83 instead of using the full elf32_mn10300_link_hash_table if we wanted
84 to save some memory. */
85 struct elf32_mn10300_link_hash_table *static_hash_table;
87 /* Random linker state flags. */
88 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
89 char flags;
92 #ifndef streq
93 #define streq(a, b) (strcmp ((a),(b)) == 0)
94 #endif
96 /* For MN10300 linker hash table. */
98 /* Get the MN10300 ELF linker hash table from a link_info structure. */
100 #define elf32_mn10300_hash_table(p) \
101 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
103 #define elf32_mn10300_link_hash_traverse(table, func, info) \
104 (elf_link_hash_traverse \
105 (&(table)->root, \
106 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
107 (info)))
109 static reloc_howto_type elf_mn10300_howto_table[] =
111 /* Dummy relocation. Does nothing. */
112 HOWTO (R_MN10300_NONE,
116 FALSE,
118 complain_overflow_bitfield,
119 bfd_elf_generic_reloc,
120 "R_MN10300_NONE",
121 FALSE,
124 FALSE),
125 /* Standard 32 bit reloc. */
126 HOWTO (R_MN10300_32,
130 FALSE,
132 complain_overflow_bitfield,
133 bfd_elf_generic_reloc,
134 "R_MN10300_32",
135 FALSE,
136 0xffffffff,
137 0xffffffff,
138 FALSE),
139 /* Standard 16 bit reloc. */
140 HOWTO (R_MN10300_16,
144 FALSE,
146 complain_overflow_bitfield,
147 bfd_elf_generic_reloc,
148 "R_MN10300_16",
149 FALSE,
150 0xffff,
151 0xffff,
152 FALSE),
153 /* Standard 8 bit reloc. */
154 HOWTO (R_MN10300_8,
158 FALSE,
160 complain_overflow_bitfield,
161 bfd_elf_generic_reloc,
162 "R_MN10300_8",
163 FALSE,
164 0xff,
165 0xff,
166 FALSE),
167 /* Standard 32bit pc-relative reloc. */
168 HOWTO (R_MN10300_PCREL32,
172 TRUE,
174 complain_overflow_bitfield,
175 bfd_elf_generic_reloc,
176 "R_MN10300_PCREL32",
177 FALSE,
178 0xffffffff,
179 0xffffffff,
180 TRUE),
181 /* Standard 16bit pc-relative reloc. */
182 HOWTO (R_MN10300_PCREL16,
186 TRUE,
188 complain_overflow_bitfield,
189 bfd_elf_generic_reloc,
190 "R_MN10300_PCREL16",
191 FALSE,
192 0xffff,
193 0xffff,
194 TRUE),
195 /* Standard 8 pc-relative reloc. */
196 HOWTO (R_MN10300_PCREL8,
200 TRUE,
202 complain_overflow_bitfield,
203 bfd_elf_generic_reloc,
204 "R_MN10300_PCREL8",
205 FALSE,
206 0xff,
207 0xff,
208 TRUE),
210 /* GNU extension to record C++ vtable hierarchy. */
211 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
212 0, /* rightshift */
213 0, /* size (0 = byte, 1 = short, 2 = long) */
214 0, /* bitsize */
215 FALSE, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_dont, /* complain_on_overflow */
218 NULL, /* special_function */
219 "R_MN10300_GNU_VTINHERIT", /* name */
220 FALSE, /* partial_inplace */
221 0, /* src_mask */
222 0, /* dst_mask */
223 FALSE), /* pcrel_offset */
225 /* GNU extension to record C++ vtable member usage */
226 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
227 0, /* rightshift */
228 0, /* size (0 = byte, 1 = short, 2 = long) */
229 0, /* bitsize */
230 FALSE, /* pc_relative */
231 0, /* bitpos */
232 complain_overflow_dont, /* complain_on_overflow */
233 NULL, /* special_function */
234 "R_MN10300_GNU_VTENTRY", /* name */
235 FALSE, /* partial_inplace */
236 0, /* src_mask */
237 0, /* dst_mask */
238 FALSE), /* pcrel_offset */
240 /* Standard 24 bit reloc. */
241 HOWTO (R_MN10300_24,
245 FALSE,
247 complain_overflow_bitfield,
248 bfd_elf_generic_reloc,
249 "R_MN10300_24",
250 FALSE,
251 0xffffff,
252 0xffffff,
253 FALSE),
254 HOWTO (R_MN10300_GOTPC32, /* type */
255 0, /* rightshift */
256 2, /* size (0 = byte, 1 = short, 2 = long) */
257 32, /* bitsize */
258 TRUE, /* pc_relative */
259 0, /* bitpos */
260 complain_overflow_bitfield, /* complain_on_overflow */
261 bfd_elf_generic_reloc, /* */
262 "R_MN10300_GOTPC32", /* name */
263 FALSE, /* partial_inplace */
264 0xffffffff, /* src_mask */
265 0xffffffff, /* dst_mask */
266 TRUE), /* pcrel_offset */
268 HOWTO (R_MN10300_GOTPC16, /* type */
269 0, /* rightshift */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
271 16, /* bitsize */
272 TRUE, /* pc_relative */
273 0, /* bitpos */
274 complain_overflow_bitfield, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* */
276 "R_MN10300_GOTPC16", /* name */
277 FALSE, /* partial_inplace */
278 0xffff, /* src_mask */
279 0xffff, /* dst_mask */
280 TRUE), /* pcrel_offset */
282 HOWTO (R_MN10300_GOTOFF32, /* type */
283 0, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 32, /* bitsize */
286 FALSE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_bitfield, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* */
290 "R_MN10300_GOTOFF32", /* name */
291 FALSE, /* partial_inplace */
292 0xffffffff, /* src_mask */
293 0xffffffff, /* dst_mask */
294 FALSE), /* pcrel_offset */
296 HOWTO (R_MN10300_GOTOFF24, /* type */
297 0, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 24, /* bitsize */
300 FALSE, /* pc_relative */
301 0, /* bitpos */
302 complain_overflow_bitfield, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* */
304 "R_MN10300_GOTOFF24", /* name */
305 FALSE, /* partial_inplace */
306 0xffffff, /* src_mask */
307 0xffffff, /* dst_mask */
308 FALSE), /* pcrel_offset */
310 HOWTO (R_MN10300_GOTOFF16, /* type */
311 0, /* rightshift */
312 1, /* size (0 = byte, 1 = short, 2 = long) */
313 16, /* bitsize */
314 FALSE, /* pc_relative */
315 0, /* bitpos */
316 complain_overflow_bitfield, /* complain_on_overflow */
317 bfd_elf_generic_reloc, /* */
318 "R_MN10300_GOTOFF16", /* name */
319 FALSE, /* partial_inplace */
320 0xffff, /* src_mask */
321 0xffff, /* dst_mask */
322 FALSE), /* pcrel_offset */
324 HOWTO (R_MN10300_PLT32, /* type */
325 0, /* rightshift */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
327 32, /* bitsize */
328 TRUE, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* */
332 "R_MN10300_PLT32", /* name */
333 FALSE, /* partial_inplace */
334 0xffffffff, /* src_mask */
335 0xffffffff, /* dst_mask */
336 TRUE), /* pcrel_offset */
338 HOWTO (R_MN10300_PLT16, /* type */
339 0, /* rightshift */
340 1, /* size (0 = byte, 1 = short, 2 = long) */
341 16, /* bitsize */
342 TRUE, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_bitfield, /* complain_on_overflow */
345 bfd_elf_generic_reloc, /* */
346 "R_MN10300_PLT16", /* name */
347 FALSE, /* partial_inplace */
348 0xffff, /* src_mask */
349 0xffff, /* dst_mask */
350 TRUE), /* pcrel_offset */
352 HOWTO (R_MN10300_GOT32, /* type */
353 0, /* rightshift */
354 2, /* size (0 = byte, 1 = short, 2 = long) */
355 32, /* bitsize */
356 FALSE, /* pc_relative */
357 0, /* bitpos */
358 complain_overflow_bitfield, /* complain_on_overflow */
359 bfd_elf_generic_reloc, /* */
360 "R_MN10300_GOT32", /* name */
361 FALSE, /* partial_inplace */
362 0xffffffff, /* src_mask */
363 0xffffffff, /* dst_mask */
364 FALSE), /* pcrel_offset */
366 HOWTO (R_MN10300_GOT24, /* type */
367 0, /* rightshift */
368 2, /* size (0 = byte, 1 = short, 2 = long) */
369 24, /* bitsize */
370 FALSE, /* pc_relative */
371 0, /* bitpos */
372 complain_overflow_bitfield, /* complain_on_overflow */
373 bfd_elf_generic_reloc, /* */
374 "R_MN10300_GOT24", /* name */
375 FALSE, /* partial_inplace */
376 0xffffffff, /* src_mask */
377 0xffffffff, /* dst_mask */
378 FALSE), /* pcrel_offset */
380 HOWTO (R_MN10300_GOT16, /* type */
381 0, /* rightshift */
382 1, /* size (0 = byte, 1 = short, 2 = long) */
383 16, /* bitsize */
384 FALSE, /* pc_relative */
385 0, /* bitpos */
386 complain_overflow_bitfield, /* complain_on_overflow */
387 bfd_elf_generic_reloc, /* */
388 "R_MN10300_GOT16", /* name */
389 FALSE, /* partial_inplace */
390 0xffffffff, /* src_mask */
391 0xffffffff, /* dst_mask */
392 FALSE), /* pcrel_offset */
394 HOWTO (R_MN10300_COPY, /* type */
395 0, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 32, /* bitsize */
398 FALSE, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_bitfield, /* complain_on_overflow */
401 bfd_elf_generic_reloc, /* */
402 "R_MN10300_COPY", /* name */
403 FALSE, /* partial_inplace */
404 0xffffffff, /* src_mask */
405 0xffffffff, /* dst_mask */
406 FALSE), /* pcrel_offset */
408 HOWTO (R_MN10300_GLOB_DAT, /* type */
409 0, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 32, /* bitsize */
412 FALSE, /* pc_relative */
413 0, /* bitpos */
414 complain_overflow_bitfield, /* complain_on_overflow */
415 bfd_elf_generic_reloc, /* */
416 "R_MN10300_GLOB_DAT", /* name */
417 FALSE, /* partial_inplace */
418 0xffffffff, /* src_mask */
419 0xffffffff, /* dst_mask */
420 FALSE), /* pcrel_offset */
422 HOWTO (R_MN10300_JMP_SLOT, /* type */
423 0, /* rightshift */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
425 32, /* bitsize */
426 FALSE, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_bitfield, /* complain_on_overflow */
429 bfd_elf_generic_reloc, /* */
430 "R_MN10300_JMP_SLOT", /* name */
431 FALSE, /* partial_inplace */
432 0xffffffff, /* src_mask */
433 0xffffffff, /* dst_mask */
434 FALSE), /* pcrel_offset */
436 HOWTO (R_MN10300_RELATIVE, /* type */
437 0, /* rightshift */
438 2, /* size (0 = byte, 1 = short, 2 = long) */
439 32, /* bitsize */
440 FALSE, /* pc_relative */
441 0, /* bitpos */
442 complain_overflow_bitfield, /* complain_on_overflow */
443 bfd_elf_generic_reloc, /* */
444 "R_MN10300_RELATIVE", /* name */
445 FALSE, /* partial_inplace */
446 0xffffffff, /* src_mask */
447 0xffffffff, /* dst_mask */
448 FALSE), /* pcrel_offset */
450 EMPTY_HOWTO (24),
451 EMPTY_HOWTO (25),
452 EMPTY_HOWTO (26),
453 EMPTY_HOWTO (27),
454 EMPTY_HOWTO (28),
455 EMPTY_HOWTO (29),
456 EMPTY_HOWTO (30),
457 EMPTY_HOWTO (31),
458 EMPTY_HOWTO (32),
460 HOWTO (R_MN10300_SYM_DIFF, /* type */
461 0, /* rightshift */
462 2, /* size (0 = byte, 1 = short, 2 = long) */
463 32, /* bitsize */
464 FALSE, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_dont,/* complain_on_overflow */
467 NULL, /* special handler. */
468 "R_MN10300_SYM_DIFF", /* name */
469 FALSE, /* partial_inplace */
470 0xffffffff, /* src_mask */
471 0xffffffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
474 HOWTO (R_MN10300_ALIGN, /* type */
475 0, /* rightshift */
476 0, /* size (0 = byte, 1 = short, 2 = long) */
477 32, /* bitsize */
478 FALSE, /* pc_relative */
479 0, /* bitpos */
480 complain_overflow_dont,/* complain_on_overflow */
481 NULL, /* special handler. */
482 "R_MN10300_ALIGN", /* name */
483 FALSE, /* partial_inplace */
484 0, /* src_mask */
485 0, /* dst_mask */
486 FALSE) /* pcrel_offset */
489 struct mn10300_reloc_map
491 bfd_reloc_code_real_type bfd_reloc_val;
492 unsigned char elf_reloc_val;
495 static const struct mn10300_reloc_map mn10300_reloc_map[] =
497 { BFD_RELOC_NONE, R_MN10300_NONE, },
498 { BFD_RELOC_32, R_MN10300_32, },
499 { BFD_RELOC_16, R_MN10300_16, },
500 { BFD_RELOC_8, R_MN10300_8, },
501 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
502 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
503 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
504 { BFD_RELOC_24, R_MN10300_24, },
505 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
506 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
507 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
508 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
509 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
510 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
511 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
512 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
513 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
514 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
515 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
516 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
517 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
518 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
519 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
520 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
521 { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
522 { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
525 /* Create the GOT section. */
527 static bfd_boolean
528 _bfd_mn10300_elf_create_got_section (bfd * abfd,
529 struct bfd_link_info * info)
531 flagword flags;
532 flagword pltflags;
533 asection * s;
534 struct elf_link_hash_entry * h;
535 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
536 int ptralign;
538 /* This function may be called more than once. */
539 if (bfd_get_section_by_name (abfd, ".got") != NULL)
540 return TRUE;
542 switch (bed->s->arch_size)
544 case 32:
545 ptralign = 2;
546 break;
548 case 64:
549 ptralign = 3;
550 break;
552 default:
553 bfd_set_error (bfd_error_bad_value);
554 return FALSE;
557 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
558 | SEC_LINKER_CREATED);
560 pltflags = flags;
561 pltflags |= SEC_CODE;
562 if (bed->plt_not_loaded)
563 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
564 if (bed->plt_readonly)
565 pltflags |= SEC_READONLY;
567 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
568 if (s == NULL
569 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
570 return FALSE;
572 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
573 .plt section. */
574 if (bed->want_plt_sym)
576 h = _bfd_elf_define_linkage_sym (abfd, info, s,
577 "_PROCEDURE_LINKAGE_TABLE_");
578 elf_hash_table (info)->hplt = h;
579 if (h == NULL)
580 return FALSE;
583 s = bfd_make_section_with_flags (abfd, ".got", flags);
584 if (s == NULL
585 || ! bfd_set_section_alignment (abfd, s, ptralign))
586 return FALSE;
588 if (bed->want_got_plt)
590 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
591 if (s == NULL
592 || ! bfd_set_section_alignment (abfd, s, ptralign))
593 return FALSE;
596 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
597 (or .got.plt) section. We don't do this in the linker script
598 because we don't want to define the symbol if we are not creating
599 a global offset table. */
600 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
601 elf_hash_table (info)->hgot = h;
602 if (h == NULL)
603 return FALSE;
605 /* The first bit of the global offset table is the header. */
606 s->size += bed->got_header_size;
608 return TRUE;
611 static reloc_howto_type *
612 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
613 bfd_reloc_code_real_type code)
615 unsigned int i;
617 for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
618 if (mn10300_reloc_map[i].bfd_reloc_val == code)
619 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
621 return NULL;
624 static reloc_howto_type *
625 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
626 const char *r_name)
628 unsigned int i;
630 for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
631 if (elf_mn10300_howto_table[i].name != NULL
632 && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
633 return elf_mn10300_howto_table + i;
635 return NULL;
638 /* Set the howto pointer for an MN10300 ELF reloc. */
640 static void
641 mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
642 arelent *cache_ptr,
643 Elf_Internal_Rela *dst)
645 unsigned int r_type;
647 r_type = ELF32_R_TYPE (dst->r_info);
648 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
649 cache_ptr->howto = elf_mn10300_howto_table + r_type;
652 /* Look through the relocs for a section during the first phase.
653 Since we don't do .gots or .plts, we just need to consider the
654 virtual table relocs for gc. */
656 static bfd_boolean
657 mn10300_elf_check_relocs (bfd *abfd,
658 struct bfd_link_info *info,
659 asection *sec,
660 const Elf_Internal_Rela *relocs)
662 bfd_boolean sym_diff_reloc_seen;
663 Elf_Internal_Shdr *symtab_hdr;
664 Elf_Internal_Sym * isymbuf = NULL;
665 struct elf_link_hash_entry **sym_hashes;
666 const Elf_Internal_Rela *rel;
667 const Elf_Internal_Rela *rel_end;
668 bfd * dynobj;
669 bfd_vma * local_got_offsets;
670 asection * sgot;
671 asection * srelgot;
672 asection * sreloc;
673 bfd_boolean result = FALSE;
675 sgot = NULL;
676 srelgot = NULL;
677 sreloc = NULL;
679 if (info->relocatable)
680 return TRUE;
682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
683 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
684 sym_hashes = elf_sym_hashes (abfd);
686 dynobj = elf_hash_table (info)->dynobj;
687 local_got_offsets = elf_local_got_offsets (abfd);
688 rel_end = relocs + sec->reloc_count;
689 sym_diff_reloc_seen = FALSE;
691 for (rel = relocs; rel < rel_end; rel++)
693 struct elf_link_hash_entry *h;
694 unsigned long r_symndx;
695 unsigned int r_type;
697 r_symndx = ELF32_R_SYM (rel->r_info);
698 if (r_symndx < symtab_hdr->sh_info)
699 h = NULL;
700 else
702 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
703 while (h->root.type == bfd_link_hash_indirect
704 || h->root.type == bfd_link_hash_warning)
705 h = (struct elf_link_hash_entry *) h->root.u.i.link;
708 r_type = ELF32_R_TYPE (rel->r_info);
710 /* Some relocs require a global offset table. */
711 if (dynobj == NULL)
713 switch (r_type)
715 case R_MN10300_GOT32:
716 case R_MN10300_GOT24:
717 case R_MN10300_GOT16:
718 case R_MN10300_GOTOFF32:
719 case R_MN10300_GOTOFF24:
720 case R_MN10300_GOTOFF16:
721 case R_MN10300_GOTPC32:
722 case R_MN10300_GOTPC16:
723 elf_hash_table (info)->dynobj = dynobj = abfd;
724 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
725 goto fail;
726 break;
728 default:
729 break;
733 switch (r_type)
735 /* This relocation describes the C++ object vtable hierarchy.
736 Reconstruct it for later use during GC. */
737 case R_MN10300_GNU_VTINHERIT:
738 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
739 goto fail;
740 break;
742 /* This relocation describes which C++ vtable entries are actually
743 used. Record for later use during GC. */
744 case R_MN10300_GNU_VTENTRY:
745 BFD_ASSERT (h != NULL);
746 if (h != NULL
747 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
748 goto fail;
749 break;
751 case R_MN10300_GOT32:
752 case R_MN10300_GOT24:
753 case R_MN10300_GOT16:
754 /* This symbol requires a global offset table entry. */
756 if (sgot == NULL)
758 sgot = bfd_get_section_by_name (dynobj, ".got");
759 BFD_ASSERT (sgot != NULL);
762 if (srelgot == NULL
763 && (h != NULL || info->shared))
765 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
766 if (srelgot == NULL)
768 srelgot = bfd_make_section_with_flags (dynobj,
769 ".rela.got",
770 (SEC_ALLOC
771 | SEC_LOAD
772 | SEC_HAS_CONTENTS
773 | SEC_IN_MEMORY
774 | SEC_LINKER_CREATED
775 | SEC_READONLY));
776 if (srelgot == NULL
777 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
778 goto fail;
782 if (h != NULL)
784 if (h->got.offset != (bfd_vma) -1)
785 /* We have already allocated space in the .got. */
786 break;
788 h->got.offset = sgot->size;
790 /* Make sure this symbol is output as a dynamic symbol. */
791 if (h->dynindx == -1)
793 if (! bfd_elf_link_record_dynamic_symbol (info, h))
794 goto fail;
797 srelgot->size += sizeof (Elf32_External_Rela);
799 else
801 /* This is a global offset table entry for a local
802 symbol. */
803 if (local_got_offsets == NULL)
805 size_t size;
806 unsigned int i;
808 size = symtab_hdr->sh_info * sizeof (bfd_vma);
809 local_got_offsets = bfd_alloc (abfd, size);
811 if (local_got_offsets == NULL)
812 goto fail;
814 elf_local_got_offsets (abfd) = local_got_offsets;
816 for (i = 0; i < symtab_hdr->sh_info; i++)
817 local_got_offsets[i] = (bfd_vma) -1;
820 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
821 /* We have already allocated space in the .got. */
822 break;
824 local_got_offsets[r_symndx] = sgot->size;
826 if (info->shared)
827 /* If we are generating a shared object, we need to
828 output a R_MN10300_RELATIVE reloc so that the dynamic
829 linker can adjust this GOT entry. */
830 srelgot->size += sizeof (Elf32_External_Rela);
833 sgot->size += 4;
834 break;
836 case R_MN10300_PLT32:
837 case R_MN10300_PLT16:
838 /* This symbol requires a procedure linkage table entry. We
839 actually build the entry in adjust_dynamic_symbol,
840 because this might be a case of linking PIC code which is
841 never referenced by a dynamic object, in which case we
842 don't need to generate a procedure linkage table entry
843 after all. */
845 /* If this is a local symbol, we resolve it directly without
846 creating a procedure linkage table entry. */
847 if (h == NULL)
848 continue;
850 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
851 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
852 break;
854 h->needs_plt = 1;
855 break;
857 case R_MN10300_24:
858 case R_MN10300_16:
859 case R_MN10300_8:
860 case R_MN10300_PCREL32:
861 case R_MN10300_PCREL16:
862 case R_MN10300_PCREL8:
863 if (h != NULL)
864 h->non_got_ref = 1;
865 break;
867 case R_MN10300_SYM_DIFF:
868 sym_diff_reloc_seen = TRUE;
869 break;
871 case R_MN10300_32:
872 if (h != NULL)
873 h->non_got_ref = 1;
875 /* If we are creating a shared library, then we
876 need to copy the reloc into the shared library. */
877 if (info->shared
878 && (sec->flags & SEC_ALLOC) != 0
879 /* Do not generate a dynamic reloc for a
880 reloc associated with a SYM_DIFF operation. */
881 && ! sym_diff_reloc_seen)
883 asection * sym_section = NULL;
885 /* Find the section containing the
886 symbol involved in the relocation. */
887 if (h == NULL)
889 Elf_Internal_Sym * isym;
891 if (isymbuf == NULL)
892 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
893 symtab_hdr->sh_info, 0,
894 NULL, NULL, NULL);
895 if (isymbuf)
897 isym = isymbuf + r_symndx;
898 /* All we care about is whether this local symbol is absolute. */
899 if (isym->st_shndx == SHN_ABS)
900 sym_section = bfd_abs_section_ptr;
903 else
905 if (h->root.type == bfd_link_hash_defined
906 || h->root.type == bfd_link_hash_defweak)
907 sym_section = h->root.u.def.section;
910 /* If the symbol is absolute then the relocation can
911 be resolved during linking and there is no need for
912 a dynamic reloc. */
913 if (sym_section != bfd_abs_section_ptr)
915 /* When creating a shared object, we must copy these
916 reloc types into the output file. We create a reloc
917 section in dynobj and make room for this reloc. */
918 if (sreloc == NULL)
920 sreloc = _bfd_elf_make_dynamic_reloc_section
921 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
922 if (sreloc == NULL)
923 goto fail;
926 sreloc->size += sizeof (Elf32_External_Rela);
930 break;
933 if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
934 sym_diff_reloc_seen = FALSE;
937 result = TRUE;
938 fail:
939 if (isymbuf != NULL)
940 free (isymbuf);
942 return result;
945 /* Return the section that should be marked against GC for a given
946 relocation. */
948 static asection *
949 mn10300_elf_gc_mark_hook (asection *sec,
950 struct bfd_link_info *info,
951 Elf_Internal_Rela *rel,
952 struct elf_link_hash_entry *h,
953 Elf_Internal_Sym *sym)
955 if (h != NULL)
956 switch (ELF32_R_TYPE (rel->r_info))
958 case R_MN10300_GNU_VTINHERIT:
959 case R_MN10300_GNU_VTENTRY:
960 return NULL;
963 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
966 /* Perform a relocation as part of a final link. */
968 static bfd_reloc_status_type
969 mn10300_elf_final_link_relocate (reloc_howto_type *howto,
970 bfd *input_bfd,
971 bfd *output_bfd ATTRIBUTE_UNUSED,
972 asection *input_section,
973 bfd_byte *contents,
974 bfd_vma offset,
975 bfd_vma value,
976 bfd_vma addend,
977 struct elf_link_hash_entry * h,
978 unsigned long symndx,
979 struct bfd_link_info *info,
980 asection *sym_sec ATTRIBUTE_UNUSED,
981 int is_local ATTRIBUTE_UNUSED)
983 static asection * sym_diff_section;
984 static bfd_vma sym_diff_value;
985 bfd_boolean is_sym_diff_reloc;
986 unsigned long r_type = howto->type;
987 bfd_byte * hit_data = contents + offset;
988 bfd * dynobj;
989 bfd_vma * local_got_offsets;
990 asection * sgot;
991 asection * splt;
992 asection * sreloc;
994 dynobj = elf_hash_table (info)->dynobj;
995 local_got_offsets = elf_local_got_offsets (input_bfd);
997 sgot = NULL;
998 splt = NULL;
999 sreloc = NULL;
1001 switch (r_type)
1003 case R_MN10300_24:
1004 case R_MN10300_16:
1005 case R_MN10300_8:
1006 case R_MN10300_PCREL8:
1007 case R_MN10300_PCREL16:
1008 case R_MN10300_PCREL32:
1009 case R_MN10300_GOTOFF32:
1010 case R_MN10300_GOTOFF24:
1011 case R_MN10300_GOTOFF16:
1012 if (info->shared
1013 && (input_section->flags & SEC_ALLOC) != 0
1014 && h != NULL
1015 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1016 return bfd_reloc_dangerous;
1019 is_sym_diff_reloc = FALSE;
1020 if (sym_diff_section != NULL)
1022 BFD_ASSERT (sym_diff_section == input_section);
1024 switch (r_type)
1026 case R_MN10300_32:
1027 case R_MN10300_24:
1028 case R_MN10300_16:
1029 case R_MN10300_8:
1030 value -= sym_diff_value;
1031 /* If we are computing a 32-bit value for the location lists
1032 and the result is 0 then we add one to the value. A zero
1033 value can result because of linker relaxation deleteing
1034 prologue instructions and using a value of 1 (for the begin
1035 and end offsets in the location list entry) results in a
1036 nul entry which does not prevent the following entries from
1037 being parsed. */
1038 if (r_type == R_MN10300_32
1039 && value == 0
1040 && strcmp (input_section->name, ".debug_loc") == 0)
1041 value = 1;
1042 sym_diff_section = NULL;
1043 is_sym_diff_reloc = TRUE;
1044 break;
1046 default:
1047 sym_diff_section = NULL;
1048 break;
1052 switch (r_type)
1054 case R_MN10300_SYM_DIFF:
1055 BFD_ASSERT (addend == 0);
1056 /* Cache the input section and value.
1057 The offset is unreliable, since relaxation may
1058 have reduced the following reloc's offset. */
1059 sym_diff_section = input_section;
1060 sym_diff_value = value;
1061 return bfd_reloc_ok;
1063 case R_MN10300_ALIGN:
1064 case R_MN10300_NONE:
1065 return bfd_reloc_ok;
1067 case R_MN10300_32:
1068 if (info->shared
1069 /* Do not generate relocs when an R_MN10300_32 has been used
1070 with an R_MN10300_SYM_DIFF to compute a difference of two
1071 symbols. */
1072 && is_sym_diff_reloc == FALSE
1073 /* Also, do not generate a reloc when the symbol associated
1074 with the R_MN10300_32 reloc is absolute - there is no
1075 need for a run time computation in this case. */
1076 && sym_sec != bfd_abs_section_ptr
1077 /* If the section is not going to be allocated at load time
1078 then there is no need to generate relocs for it. */
1079 && (input_section->flags & SEC_ALLOC) != 0)
1081 Elf_Internal_Rela outrel;
1082 bfd_boolean skip, relocate;
1084 /* When generating a shared object, these relocations are
1085 copied into the output file to be resolved at run
1086 time. */
1087 if (sreloc == NULL)
1089 sreloc = _bfd_elf_get_dynamic_reloc_section
1090 (input_bfd, input_section, /*rela?*/ TRUE);
1091 if (sreloc == NULL)
1092 return FALSE;
1095 skip = FALSE;
1097 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1098 input_section, offset);
1099 if (outrel.r_offset == (bfd_vma) -1)
1100 skip = TRUE;
1102 outrel.r_offset += (input_section->output_section->vma
1103 + input_section->output_offset);
1105 if (skip)
1107 memset (&outrel, 0, sizeof outrel);
1108 relocate = FALSE;
1110 else
1112 /* h->dynindx may be -1 if this symbol was marked to
1113 become local. */
1114 if (h == NULL
1115 || SYMBOL_REFERENCES_LOCAL (info, h))
1117 relocate = TRUE;
1118 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1119 outrel.r_addend = value + addend;
1121 else
1123 BFD_ASSERT (h->dynindx != -1);
1124 relocate = FALSE;
1125 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1126 outrel.r_addend = value + addend;
1130 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1131 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1132 + sreloc->reloc_count));
1133 ++sreloc->reloc_count;
1135 /* If this reloc is against an external symbol, we do
1136 not want to fiddle with the addend. Otherwise, we
1137 need to include the symbol value so that it becomes
1138 an addend for the dynamic reloc. */
1139 if (! relocate)
1140 return bfd_reloc_ok;
1142 value += addend;
1143 bfd_put_32 (input_bfd, value, hit_data);
1144 return bfd_reloc_ok;
1146 case R_MN10300_24:
1147 value += addend;
1149 if ((long) value > 0x7fffff || (long) value < -0x800000)
1150 return bfd_reloc_overflow;
1152 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1153 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1154 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1155 return bfd_reloc_ok;
1157 case R_MN10300_16:
1158 value += addend;
1160 if ((long) value > 0x7fff || (long) value < -0x8000)
1161 return bfd_reloc_overflow;
1163 bfd_put_16 (input_bfd, value, hit_data);
1164 return bfd_reloc_ok;
1166 case R_MN10300_8:
1167 value += addend;
1169 if ((long) value > 0x7f || (long) value < -0x80)
1170 return bfd_reloc_overflow;
1172 bfd_put_8 (input_bfd, value, hit_data);
1173 return bfd_reloc_ok;
1175 case R_MN10300_PCREL8:
1176 value -= (input_section->output_section->vma
1177 + input_section->output_offset);
1178 value -= offset;
1179 value += addend;
1181 if ((long) value > 0x7f || (long) value < -0x80)
1182 return bfd_reloc_overflow;
1184 bfd_put_8 (input_bfd, value, hit_data);
1185 return bfd_reloc_ok;
1187 case R_MN10300_PCREL16:
1188 value -= (input_section->output_section->vma
1189 + input_section->output_offset);
1190 value -= offset;
1191 value += addend;
1193 if ((long) value > 0x7fff || (long) value < -0x8000)
1194 return bfd_reloc_overflow;
1196 bfd_put_16 (input_bfd, value, hit_data);
1197 return bfd_reloc_ok;
1199 case R_MN10300_PCREL32:
1200 value -= (input_section->output_section->vma
1201 + input_section->output_offset);
1202 value -= offset;
1203 value += addend;
1205 bfd_put_32 (input_bfd, value, hit_data);
1206 return bfd_reloc_ok;
1208 case R_MN10300_GNU_VTINHERIT:
1209 case R_MN10300_GNU_VTENTRY:
1210 return bfd_reloc_ok;
1212 case R_MN10300_GOTPC32:
1213 /* Use global offset table as symbol value. */
1214 value = bfd_get_section_by_name (dynobj,
1215 ".got")->output_section->vma;
1216 value -= (input_section->output_section->vma
1217 + input_section->output_offset);
1218 value -= offset;
1219 value += addend;
1221 bfd_put_32 (input_bfd, value, hit_data);
1222 return bfd_reloc_ok;
1224 case R_MN10300_GOTPC16:
1225 /* Use global offset table as symbol value. */
1226 value = bfd_get_section_by_name (dynobj,
1227 ".got")->output_section->vma;
1228 value -= (input_section->output_section->vma
1229 + input_section->output_offset);
1230 value -= offset;
1231 value += addend;
1233 if ((long) value > 0x7fff || (long) value < -0x8000)
1234 return bfd_reloc_overflow;
1236 bfd_put_16 (input_bfd, value, hit_data);
1237 return bfd_reloc_ok;
1239 case R_MN10300_GOTOFF32:
1240 value -= bfd_get_section_by_name (dynobj,
1241 ".got")->output_section->vma;
1242 value += addend;
1244 bfd_put_32 (input_bfd, value, hit_data);
1245 return bfd_reloc_ok;
1247 case R_MN10300_GOTOFF24:
1248 value -= bfd_get_section_by_name (dynobj,
1249 ".got")->output_section->vma;
1250 value += addend;
1252 if ((long) value > 0x7fffff || (long) value < -0x800000)
1253 return bfd_reloc_overflow;
1255 bfd_put_8 (input_bfd, value, hit_data);
1256 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1257 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1258 return bfd_reloc_ok;
1260 case R_MN10300_GOTOFF16:
1261 value -= bfd_get_section_by_name (dynobj,
1262 ".got")->output_section->vma;
1263 value += addend;
1265 if ((long) value > 0x7fff || (long) value < -0x8000)
1266 return bfd_reloc_overflow;
1268 bfd_put_16 (input_bfd, value, hit_data);
1269 return bfd_reloc_ok;
1271 case R_MN10300_PLT32:
1272 if (h != NULL
1273 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1274 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1275 && h->plt.offset != (bfd_vma) -1)
1277 splt = bfd_get_section_by_name (dynobj, ".plt");
1279 value = (splt->output_section->vma
1280 + splt->output_offset
1281 + h->plt.offset) - value;
1284 value -= (input_section->output_section->vma
1285 + input_section->output_offset);
1286 value -= offset;
1287 value += addend;
1289 bfd_put_32 (input_bfd, value, hit_data);
1290 return bfd_reloc_ok;
1292 case R_MN10300_PLT16:
1293 if (h != NULL
1294 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1295 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1296 && h->plt.offset != (bfd_vma) -1)
1298 splt = bfd_get_section_by_name (dynobj, ".plt");
1300 value = (splt->output_section->vma
1301 + splt->output_offset
1302 + h->plt.offset) - value;
1305 value -= (input_section->output_section->vma
1306 + input_section->output_offset);
1307 value -= offset;
1308 value += addend;
1310 if ((long) value > 0x7fff || (long) value < -0x8000)
1311 return bfd_reloc_overflow;
1313 bfd_put_16 (input_bfd, value, hit_data);
1314 return bfd_reloc_ok;
1316 case R_MN10300_GOT32:
1317 case R_MN10300_GOT24:
1318 case R_MN10300_GOT16:
1320 sgot = bfd_get_section_by_name (dynobj, ".got");
1322 if (h != NULL)
1324 bfd_vma off;
1326 off = h->got.offset;
1327 BFD_ASSERT (off != (bfd_vma) -1);
1329 if (! elf_hash_table (info)->dynamic_sections_created
1330 || SYMBOL_REFERENCES_LOCAL (info, h))
1331 /* This is actually a static link, or it is a
1332 -Bsymbolic link and the symbol is defined
1333 locally, or the symbol was forced to be local
1334 because of a version file. We must initialize
1335 this entry in the global offset table.
1337 When doing a dynamic link, we create a .rela.got
1338 relocation entry to initialize the value. This
1339 is done in the finish_dynamic_symbol routine. */
1340 bfd_put_32 (output_bfd, value,
1341 sgot->contents + off);
1343 value = sgot->output_offset + off;
1345 else
1347 bfd_vma off;
1349 off = elf_local_got_offsets (input_bfd)[symndx];
1351 bfd_put_32 (output_bfd, value, sgot->contents + off);
1353 if (info->shared)
1355 asection * srelgot;
1356 Elf_Internal_Rela outrel;
1358 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1359 BFD_ASSERT (srelgot != NULL);
1361 outrel.r_offset = (sgot->output_section->vma
1362 + sgot->output_offset
1363 + off);
1364 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1365 outrel.r_addend = value;
1366 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1367 (bfd_byte *) (((Elf32_External_Rela *)
1368 srelgot->contents)
1369 + srelgot->reloc_count));
1370 ++ srelgot->reloc_count;
1373 value = sgot->output_offset + off;
1377 value += addend;
1379 if (r_type == R_MN10300_GOT32)
1381 bfd_put_32 (input_bfd, value, hit_data);
1382 return bfd_reloc_ok;
1384 else if (r_type == R_MN10300_GOT24)
1386 if ((long) value > 0x7fffff || (long) value < -0x800000)
1387 return bfd_reloc_overflow;
1389 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1390 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1391 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1392 return bfd_reloc_ok;
1394 else if (r_type == R_MN10300_GOT16)
1396 if ((long) value > 0x7fff || (long) value < -0x8000)
1397 return bfd_reloc_overflow;
1399 bfd_put_16 (input_bfd, value, hit_data);
1400 return bfd_reloc_ok;
1402 /* Fall through. */
1404 default:
1405 return bfd_reloc_notsupported;
1409 /* Relocate an MN10300 ELF section. */
1411 static bfd_boolean
1412 mn10300_elf_relocate_section (bfd *output_bfd,
1413 struct bfd_link_info *info,
1414 bfd *input_bfd,
1415 asection *input_section,
1416 bfd_byte *contents,
1417 Elf_Internal_Rela *relocs,
1418 Elf_Internal_Sym *local_syms,
1419 asection **local_sections)
1421 Elf_Internal_Shdr *symtab_hdr;
1422 struct elf_link_hash_entry **sym_hashes;
1423 Elf_Internal_Rela *rel, *relend;
1425 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1426 sym_hashes = elf_sym_hashes (input_bfd);
1428 rel = relocs;
1429 relend = relocs + input_section->reloc_count;
1430 for (; rel < relend; rel++)
1432 int r_type;
1433 reloc_howto_type *howto;
1434 unsigned long r_symndx;
1435 Elf_Internal_Sym *sym;
1436 asection *sec;
1437 struct elf32_mn10300_link_hash_entry *h;
1438 bfd_vma relocation;
1439 bfd_reloc_status_type r;
1441 r_symndx = ELF32_R_SYM (rel->r_info);
1442 r_type = ELF32_R_TYPE (rel->r_info);
1443 howto = elf_mn10300_howto_table + r_type;
1445 /* Just skip the vtable gc relocs. */
1446 if (r_type == R_MN10300_GNU_VTINHERIT
1447 || r_type == R_MN10300_GNU_VTENTRY)
1448 continue;
1450 h = NULL;
1451 sym = NULL;
1452 sec = NULL;
1453 if (r_symndx < symtab_hdr->sh_info)
1455 sym = local_syms + r_symndx;
1456 sec = local_sections[r_symndx];
1457 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1459 else
1461 bfd_boolean unresolved_reloc;
1462 bfd_boolean warned;
1463 struct elf_link_hash_entry *hh;
1465 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1466 r_symndx, symtab_hdr, sym_hashes,
1467 hh, sec, relocation,
1468 unresolved_reloc, warned);
1470 h = (struct elf32_mn10300_link_hash_entry *) hh;
1472 if ((h->root.root.type == bfd_link_hash_defined
1473 || h->root.root.type == bfd_link_hash_defweak)
1474 && ( r_type == R_MN10300_GOTPC32
1475 || r_type == R_MN10300_GOTPC16
1476 || (( r_type == R_MN10300_PLT32
1477 || r_type == R_MN10300_PLT16)
1478 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1479 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1480 && h->root.plt.offset != (bfd_vma) -1)
1481 || (( r_type == R_MN10300_GOT32
1482 || r_type == R_MN10300_GOT24
1483 || r_type == R_MN10300_GOT16)
1484 && elf_hash_table (info)->dynamic_sections_created
1485 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1486 || (r_type == R_MN10300_32
1487 /* _32 relocs in executables force _COPY relocs,
1488 such that the address of the symbol ends up
1489 being local. */
1490 && !info->executable
1491 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1492 && ((input_section->flags & SEC_ALLOC) != 0
1493 /* DWARF will emit R_MN10300_32 relocations
1494 in its sections against symbols defined
1495 externally in shared libraries. We can't
1496 do anything with them here. */
1497 || ((input_section->flags & SEC_DEBUGGING) != 0
1498 && h->root.def_dynamic)))))
1499 /* In these cases, we don't need the relocation
1500 value. We check specially because in some
1501 obscure cases sec->output_section will be NULL. */
1502 relocation = 0;
1504 else if (!info->relocatable && unresolved_reloc)
1505 (*_bfd_error_handler)
1506 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1507 input_bfd,
1508 input_section,
1509 (long) rel->r_offset,
1510 howto->name,
1511 h->root.root.root.string);
1514 if (sec != NULL && elf_discarded_section (sec))
1516 /* For relocs against symbols from removed linkonce sections,
1517 or sections discarded by a linker script, we just want the
1518 section contents zeroed. Avoid any special processing. */
1519 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1520 rel->r_info = 0;
1521 rel->r_addend = 0;
1522 continue;
1525 if (info->relocatable)
1526 continue;
1528 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1529 input_section,
1530 contents, rel->r_offset,
1531 relocation, rel->r_addend,
1532 (struct elf_link_hash_entry *) h,
1533 r_symndx,
1534 info, sec, h == NULL);
1536 if (r != bfd_reloc_ok)
1538 const char *name;
1539 const char *msg = NULL;
1541 if (h != NULL)
1542 name = h->root.root.root.string;
1543 else
1545 name = (bfd_elf_string_from_elf_section
1546 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1547 if (name == NULL || *name == '\0')
1548 name = bfd_section_name (input_bfd, sec);
1551 switch (r)
1553 case bfd_reloc_overflow:
1554 if (! ((*info->callbacks->reloc_overflow)
1555 (info, (h ? &h->root.root : NULL), name,
1556 howto->name, (bfd_vma) 0, input_bfd,
1557 input_section, rel->r_offset)))
1558 return FALSE;
1559 break;
1561 case bfd_reloc_undefined:
1562 if (! ((*info->callbacks->undefined_symbol)
1563 (info, name, input_bfd, input_section,
1564 rel->r_offset, TRUE)))
1565 return FALSE;
1566 break;
1568 case bfd_reloc_outofrange:
1569 msg = _("internal error: out of range error");
1570 goto common_error;
1572 case bfd_reloc_notsupported:
1573 msg = _("internal error: unsupported relocation error");
1574 goto common_error;
1576 case bfd_reloc_dangerous:
1577 if (r_type == R_MN10300_PCREL32)
1578 msg = _("error: inappropriate relocation type for shared"
1579 " library (did you forget -fpic?)");
1580 else
1581 msg = _("internal error: suspicious relocation type used"
1582 " in shared library");
1583 goto common_error;
1585 default:
1586 msg = _("internal error: unknown error");
1587 /* Fall through. */
1589 common_error:
1590 if (!((*info->callbacks->warning)
1591 (info, msg, name, input_bfd, input_section,
1592 rel->r_offset)))
1593 return FALSE;
1594 break;
1599 return TRUE;
1602 /* Finish initializing one hash table entry. */
1604 static bfd_boolean
1605 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
1606 void * in_args)
1608 struct elf32_mn10300_link_hash_entry *entry;
1609 struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
1610 unsigned int byte_count = 0;
1612 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1614 if (entry->root.root.type == bfd_link_hash_warning)
1615 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1617 /* If we already know we want to convert "call" to "calls" for calls
1618 to this symbol, then return now. */
1619 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1620 return TRUE;
1622 /* If there are no named calls to this symbol, or there's nothing we
1623 can move from the function itself into the "call" instruction,
1624 then note that all "call" instructions should be converted into
1625 "calls" instructions and return. If a symbol is available for
1626 dynamic symbol resolution (overridable or overriding), avoid
1627 custom calling conventions. */
1628 if (entry->direct_calls == 0
1629 || (entry->stack_size == 0 && entry->movm_args == 0)
1630 || (elf_hash_table (link_info)->dynamic_sections_created
1631 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1632 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1634 /* Make a note that we should convert "call" instructions to "calls"
1635 instructions for calls to this symbol. */
1636 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1637 return TRUE;
1640 /* We may be able to move some instructions from the function itself into
1641 the "call" instruction. Count how many bytes we might be able to
1642 eliminate in the function itself. */
1644 /* A movm instruction is two bytes. */
1645 if (entry->movm_args)
1646 byte_count += 2;
1648 /* Count the insn to allocate stack space too. */
1649 if (entry->stack_size > 0)
1651 if (entry->stack_size <= 128)
1652 byte_count += 3;
1653 else
1654 byte_count += 4;
1657 /* If using "call" will result in larger code, then turn all
1658 the associated "call" instructions into "calls" instructions. */
1659 if (byte_count < entry->direct_calls)
1660 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1662 /* This routine never fails. */
1663 return TRUE;
1666 /* Used to count hash table entries. */
1668 static bfd_boolean
1669 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
1670 void * in_args)
1672 int *count = (int *) in_args;
1674 (*count) ++;
1675 return TRUE;
1678 /* Used to enumerate hash table entries into a linear array. */
1680 static bfd_boolean
1681 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
1682 void * in_args)
1684 struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
1686 **ptr = gen_entry;
1687 (*ptr) ++;
1688 return TRUE;
1691 /* Used to sort the array created by the above. */
1693 static int
1694 sort_by_value (const void *va, const void *vb)
1696 struct elf32_mn10300_link_hash_entry *a
1697 = *(struct elf32_mn10300_link_hash_entry **) va;
1698 struct elf32_mn10300_link_hash_entry *b
1699 = *(struct elf32_mn10300_link_hash_entry **) vb;
1701 return a->value - b->value;
1704 /* Compute the stack size and movm arguments for the function
1705 referred to by HASH at address ADDR in section with
1706 contents CONTENTS, store the information in the hash table. */
1708 static void
1709 compute_function_info (bfd *abfd,
1710 struct elf32_mn10300_link_hash_entry *hash,
1711 bfd_vma addr,
1712 unsigned char *contents)
1714 unsigned char byte1, byte2;
1715 /* We only care about a very small subset of the possible prologue
1716 sequences here. Basically we look for:
1718 movm [d2,d3,a2,a3],sp (optional)
1719 add <size>,sp (optional, and only for sizes which fit in an unsigned
1720 8 bit number)
1722 If we find anything else, we quit. */
1724 /* Look for movm [regs],sp. */
1725 byte1 = bfd_get_8 (abfd, contents + addr);
1726 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1728 if (byte1 == 0xcf)
1730 hash->movm_args = byte2;
1731 addr += 2;
1732 byte1 = bfd_get_8 (abfd, contents + addr);
1733 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1736 /* Now figure out how much stack space will be allocated by the movm
1737 instruction. We need this kept separate from the function's normal
1738 stack space. */
1739 if (hash->movm_args)
1741 /* Space for d2. */
1742 if (hash->movm_args & 0x80)
1743 hash->movm_stack_size += 4;
1745 /* Space for d3. */
1746 if (hash->movm_args & 0x40)
1747 hash->movm_stack_size += 4;
1749 /* Space for a2. */
1750 if (hash->movm_args & 0x20)
1751 hash->movm_stack_size += 4;
1753 /* Space for a3. */
1754 if (hash->movm_args & 0x10)
1755 hash->movm_stack_size += 4;
1757 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
1758 if (hash->movm_args & 0x08)
1759 hash->movm_stack_size += 8 * 4;
1761 if (bfd_get_mach (abfd) == bfd_mach_am33
1762 || bfd_get_mach (abfd) == bfd_mach_am33_2)
1764 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
1765 if (hash->movm_args & 0x1)
1766 hash->movm_stack_size += 6 * 4;
1768 /* exreg1 space. e4, e5, e6, e7 */
1769 if (hash->movm_args & 0x2)
1770 hash->movm_stack_size += 4 * 4;
1772 /* exreg0 space. e2, e3 */
1773 if (hash->movm_args & 0x4)
1774 hash->movm_stack_size += 2 * 4;
1778 /* Now look for the two stack adjustment variants. */
1779 if (byte1 == 0xf8 && byte2 == 0xfe)
1781 int temp = bfd_get_8 (abfd, contents + addr + 2);
1782 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
1784 hash->stack_size = -temp;
1786 else if (byte1 == 0xfa && byte2 == 0xfe)
1788 int temp = bfd_get_16 (abfd, contents + addr + 2);
1789 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
1790 temp = -temp;
1792 if (temp < 255)
1793 hash->stack_size = temp;
1796 /* If the total stack to be allocated by the call instruction is more
1797 than 255 bytes, then we can't remove the stack adjustment by using
1798 "call" (we might still be able to remove the "movm" instruction. */
1799 if (hash->stack_size + hash->movm_stack_size > 255)
1800 hash->stack_size = 0;
1803 /* Delete some bytes from a section while relaxing. */
1805 static bfd_boolean
1806 mn10300_elf_relax_delete_bytes (bfd *abfd,
1807 asection *sec,
1808 bfd_vma addr,
1809 int count)
1811 Elf_Internal_Shdr *symtab_hdr;
1812 unsigned int sec_shndx;
1813 bfd_byte *contents;
1814 Elf_Internal_Rela *irel, *irelend;
1815 Elf_Internal_Rela *irelalign;
1816 bfd_vma toaddr;
1817 Elf_Internal_Sym *isym, *isymend;
1818 struct elf_link_hash_entry **sym_hashes;
1819 struct elf_link_hash_entry **end_hashes;
1820 unsigned int symcount;
1822 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1824 contents = elf_section_data (sec)->this_hdr.contents;
1826 irelalign = NULL;
1827 toaddr = sec->size;
1829 irel = elf_section_data (sec)->relocs;
1830 irelend = irel + sec->reloc_count;
1832 if (sec->reloc_count > 0)
1834 /* If there is an align reloc at the end of the section ignore it.
1835 GAS creates these relocs for reasons of its own, and they just
1836 serve to keep the section artifically inflated. */
1837 if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
1838 --irelend;
1840 /* The deletion must stop at the next ALIGN reloc for an aligment
1841 power larger than, or not a multiple of, the number of bytes we
1842 are deleting. */
1843 for (; irel < irelend; irel++)
1845 int alignment = 1 << irel->r_addend;
1847 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1848 && irel->r_offset > addr
1849 && irel->r_offset < toaddr
1850 && (count < alignment
1851 || alignment % count != 0))
1853 irelalign = irel;
1854 toaddr = irel->r_offset;
1855 break;
1860 /* Actually delete the bytes. */
1861 memmove (contents + addr, contents + addr + count,
1862 (size_t) (toaddr - addr - count));
1864 /* Adjust the section's size if we are shrinking it, or else
1865 pad the bytes between the end of the shrunken region and
1866 the start of the next region with NOP codes. */
1867 if (irelalign == NULL)
1869 sec->size -= count;
1870 /* Include symbols at the end of the section, but
1871 not at the end of a sub-region of the section. */
1872 toaddr ++;
1874 else
1876 int i;
1878 #define NOP_OPCODE 0xcb
1880 for (i = 0; i < count; i ++)
1881 bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1884 /* Adjust all the relocs. */
1885 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1887 /* Get the new reloc address. */
1888 if ((irel->r_offset > addr
1889 && irel->r_offset < toaddr)
1890 || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1891 && irel->r_offset == toaddr))
1892 irel->r_offset -= count;
1895 /* Adjust the local symbols in the section, reducing their value
1896 by the number of bytes deleted. Note - symbols within the deleted
1897 region are moved to the address of the start of the region, which
1898 actually means that they will address the byte beyond the end of
1899 the region once the deletion has been completed. */
1900 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1901 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1902 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1904 if (isym->st_shndx == sec_shndx
1905 && isym->st_value > addr
1906 && isym->st_value < toaddr)
1908 if (isym->st_value < addr + count)
1909 isym->st_value = addr;
1910 else
1911 isym->st_value -= count;
1913 /* Adjust the function symbol's size as well. */
1914 else if (isym->st_shndx == sec_shndx
1915 && ELF_ST_TYPE (isym->st_info) == STT_FUNC
1916 && isym->st_value + isym->st_size > addr
1917 && isym->st_value + isym->st_size < toaddr)
1918 isym->st_size -= count;
1921 /* Now adjust the global symbols defined in this section. */
1922 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1923 - symtab_hdr->sh_info);
1924 sym_hashes = elf_sym_hashes (abfd);
1925 end_hashes = sym_hashes + symcount;
1926 for (; sym_hashes < end_hashes; sym_hashes++)
1928 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1930 if ((sym_hash->root.type == bfd_link_hash_defined
1931 || sym_hash->root.type == bfd_link_hash_defweak)
1932 && sym_hash->root.u.def.section == sec
1933 && sym_hash->root.u.def.value > addr
1934 && sym_hash->root.u.def.value < toaddr)
1936 if (sym_hash->root.u.def.value < addr + count)
1937 sym_hash->root.u.def.value = addr;
1938 else
1939 sym_hash->root.u.def.value -= count;
1941 /* Adjust the function symbol's size as well. */
1942 else if (sym_hash->root.type == bfd_link_hash_defined
1943 && sym_hash->root.u.def.section == sec
1944 && sym_hash->type == STT_FUNC
1945 && sym_hash->root.u.def.value + sym_hash->size > addr
1946 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1947 sym_hash->size -= count;
1950 /* See if we can move the ALIGN reloc forward.
1951 We have adjusted r_offset for it already. */
1952 if (irelalign != NULL)
1954 bfd_vma alignto, alignaddr;
1956 if ((int) irelalign->r_addend > 0)
1958 /* This is the old address. */
1959 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1960 /* This is where the align points to now. */
1961 alignaddr = BFD_ALIGN (irelalign->r_offset,
1962 1 << irelalign->r_addend);
1963 if (alignaddr < alignto)
1964 /* Tail recursion. */
1965 return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
1966 (int) (alignto - alignaddr));
1970 return TRUE;
1973 /* Return TRUE if a symbol exists at the given address, else return
1974 FALSE. */
1976 static bfd_boolean
1977 mn10300_elf_symbol_address_p (bfd *abfd,
1978 asection *sec,
1979 Elf_Internal_Sym *isym,
1980 bfd_vma addr)
1982 Elf_Internal_Shdr *symtab_hdr;
1983 unsigned int sec_shndx;
1984 Elf_Internal_Sym *isymend;
1985 struct elf_link_hash_entry **sym_hashes;
1986 struct elf_link_hash_entry **end_hashes;
1987 unsigned int symcount;
1989 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1991 /* Examine all the symbols. */
1992 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1993 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1994 if (isym->st_shndx == sec_shndx
1995 && isym->st_value == addr)
1996 return TRUE;
1998 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1999 - symtab_hdr->sh_info);
2000 sym_hashes = elf_sym_hashes (abfd);
2001 end_hashes = sym_hashes + symcount;
2002 for (; sym_hashes < end_hashes; sym_hashes++)
2004 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2006 if ((sym_hash->root.type == bfd_link_hash_defined
2007 || sym_hash->root.type == bfd_link_hash_defweak)
2008 && sym_hash->root.u.def.section == sec
2009 && sym_hash->root.u.def.value == addr)
2010 return TRUE;
2013 return FALSE;
2016 /* This function handles relaxing for the mn10300.
2018 There are quite a few relaxing opportunities available on the mn10300:
2020 * calls:32 -> calls:16 2 bytes
2021 * call:32 -> call:16 2 bytes
2023 * call:32 -> calls:32 1 byte
2024 * call:16 -> calls:16 1 byte
2025 * These are done anytime using "calls" would result
2026 in smaller code, or when necessary to preserve the
2027 meaning of the program.
2029 * call:32 varies
2030 * call:16
2031 * In some circumstances we can move instructions
2032 from a function prologue into a "call" instruction.
2033 This is only done if the resulting code is no larger
2034 than the original code.
2036 * jmp:32 -> jmp:16 2 bytes
2037 * jmp:16 -> bra:8 1 byte
2039 * If the previous instruction is a conditional branch
2040 around the jump/bra, we may be able to reverse its condition
2041 and change its target to the jump's target. The jump/bra
2042 can then be deleted. 2 bytes
2044 * mov abs32 -> mov abs16 1 or 2 bytes
2046 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2047 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2049 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2050 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2052 We don't handle imm16->imm8 or d16->d8 as they're very rare
2053 and somewhat more difficult to support. */
2055 static bfd_boolean
2056 mn10300_elf_relax_section (bfd *abfd,
2057 asection *sec,
2058 struct bfd_link_info *link_info,
2059 bfd_boolean *again)
2061 Elf_Internal_Shdr *symtab_hdr;
2062 Elf_Internal_Rela *internal_relocs = NULL;
2063 Elf_Internal_Rela *irel, *irelend;
2064 bfd_byte *contents = NULL;
2065 Elf_Internal_Sym *isymbuf = NULL;
2066 struct elf32_mn10300_link_hash_table *hash_table;
2067 asection *section = sec;
2068 bfd_vma align_gap_adjustment;
2070 if (link_info->relocatable)
2071 (*link_info->callbacks->einfo)
2072 (_("%P%F: --relax and -r may not be used together\n"));
2074 /* Assume nothing changes. */
2075 *again = FALSE;
2077 /* We need a pointer to the mn10300 specific hash table. */
2078 hash_table = elf32_mn10300_hash_table (link_info);
2080 /* Initialize fields in each hash table entry the first time through. */
2081 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2083 bfd *input_bfd;
2085 /* Iterate over all the input bfds. */
2086 for (input_bfd = link_info->input_bfds;
2087 input_bfd != NULL;
2088 input_bfd = input_bfd->link_next)
2090 /* We're going to need all the symbols for each bfd. */
2091 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2092 if (symtab_hdr->sh_info != 0)
2094 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2095 if (isymbuf == NULL)
2096 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2097 symtab_hdr->sh_info, 0,
2098 NULL, NULL, NULL);
2099 if (isymbuf == NULL)
2100 goto error_return;
2103 /* Iterate over each section in this bfd. */
2104 for (section = input_bfd->sections;
2105 section != NULL;
2106 section = section->next)
2108 struct elf32_mn10300_link_hash_entry *hash;
2109 Elf_Internal_Sym *sym;
2110 asection *sym_sec = NULL;
2111 const char *sym_name;
2112 char *new_name;
2114 /* If there's nothing to do in this section, skip it. */
2115 if (! ((section->flags & SEC_RELOC) != 0
2116 && section->reloc_count != 0))
2117 continue;
2118 if ((section->flags & SEC_ALLOC) == 0)
2119 continue;
2121 /* Get cached copy of section contents if it exists. */
2122 if (elf_section_data (section)->this_hdr.contents != NULL)
2123 contents = elf_section_data (section)->this_hdr.contents;
2124 else if (section->size != 0)
2126 /* Go get them off disk. */
2127 if (!bfd_malloc_and_get_section (input_bfd, section,
2128 &contents))
2129 goto error_return;
2131 else
2132 contents = NULL;
2134 /* If there aren't any relocs, then there's nothing to do. */
2135 if ((section->flags & SEC_RELOC) != 0
2136 && section->reloc_count != 0)
2138 /* Get a copy of the native relocations. */
2139 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2140 NULL, NULL,
2141 link_info->keep_memory);
2142 if (internal_relocs == NULL)
2143 goto error_return;
2145 /* Now examine each relocation. */
2146 irel = internal_relocs;
2147 irelend = irel + section->reloc_count;
2148 for (; irel < irelend; irel++)
2150 long r_type;
2151 unsigned long r_index;
2152 unsigned char code;
2154 r_type = ELF32_R_TYPE (irel->r_info);
2155 r_index = ELF32_R_SYM (irel->r_info);
2157 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2158 goto error_return;
2160 /* We need the name and hash table entry of the target
2161 symbol! */
2162 hash = NULL;
2163 sym = NULL;
2164 sym_sec = NULL;
2166 if (r_index < symtab_hdr->sh_info)
2168 /* A local symbol. */
2169 Elf_Internal_Sym *isym;
2170 struct elf_link_hash_table *elftab;
2171 bfd_size_type amt;
2173 isym = isymbuf + r_index;
2174 if (isym->st_shndx == SHN_UNDEF)
2175 sym_sec = bfd_und_section_ptr;
2176 else if (isym->st_shndx == SHN_ABS)
2177 sym_sec = bfd_abs_section_ptr;
2178 else if (isym->st_shndx == SHN_COMMON)
2179 sym_sec = bfd_com_section_ptr;
2180 else
2181 sym_sec
2182 = bfd_section_from_elf_index (input_bfd,
2183 isym->st_shndx);
2185 sym_name
2186 = bfd_elf_string_from_elf_section (input_bfd,
2187 (symtab_hdr
2188 ->sh_link),
2189 isym->st_name);
2191 /* If it isn't a function, then we don't care
2192 about it. */
2193 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2194 continue;
2196 /* Tack on an ID so we can uniquely identify this
2197 local symbol in the global hash table. */
2198 amt = strlen (sym_name) + 10;
2199 new_name = bfd_malloc (amt);
2200 if (new_name == NULL)
2201 goto error_return;
2203 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2204 sym_name = new_name;
2206 elftab = &hash_table->static_hash_table->root;
2207 hash = ((struct elf32_mn10300_link_hash_entry *)
2208 elf_link_hash_lookup (elftab, sym_name,
2209 TRUE, TRUE, FALSE));
2210 free (new_name);
2212 else
2214 r_index -= symtab_hdr->sh_info;
2215 hash = (struct elf32_mn10300_link_hash_entry *)
2216 elf_sym_hashes (input_bfd)[r_index];
2219 sym_name = hash->root.root.root.string;
2220 if ((section->flags & SEC_CODE) != 0)
2222 /* If this is not a "call" instruction, then we
2223 should convert "call" instructions to "calls"
2224 instructions. */
2225 code = bfd_get_8 (input_bfd,
2226 contents + irel->r_offset - 1);
2227 if (code != 0xdd && code != 0xcd)
2228 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2231 /* If this is a jump/call, then bump the
2232 direct_calls counter. Else force "call" to
2233 "calls" conversions. */
2234 if (r_type == R_MN10300_PCREL32
2235 || r_type == R_MN10300_PLT32
2236 || r_type == R_MN10300_PLT16
2237 || r_type == R_MN10300_PCREL16)
2238 hash->direct_calls++;
2239 else
2240 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2244 /* Now look at the actual contents to get the stack size,
2245 and a list of what registers were saved in the prologue
2246 (ie movm_args). */
2247 if ((section->flags & SEC_CODE) != 0)
2249 Elf_Internal_Sym *isym, *isymend;
2250 unsigned int sec_shndx;
2251 struct elf_link_hash_entry **hashes;
2252 struct elf_link_hash_entry **end_hashes;
2253 unsigned int symcount;
2255 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2256 section);
2258 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2259 - symtab_hdr->sh_info);
2260 hashes = elf_sym_hashes (input_bfd);
2261 end_hashes = hashes + symcount;
2263 /* Look at each function defined in this section and
2264 update info for that function. */
2265 isymend = isymbuf + symtab_hdr->sh_info;
2266 for (isym = isymbuf; isym < isymend; isym++)
2268 if (isym->st_shndx == sec_shndx
2269 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2271 struct elf_link_hash_table *elftab;
2272 bfd_size_type amt;
2273 struct elf_link_hash_entry **lhashes = hashes;
2275 /* Skip a local symbol if it aliases a
2276 global one. */
2277 for (; lhashes < end_hashes; lhashes++)
2279 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2280 if ((hash->root.root.type == bfd_link_hash_defined
2281 || hash->root.root.type == bfd_link_hash_defweak)
2282 && hash->root.root.u.def.section == section
2283 && hash->root.type == STT_FUNC
2284 && hash->root.root.u.def.value == isym->st_value)
2285 break;
2287 if (lhashes != end_hashes)
2288 continue;
2290 if (isym->st_shndx == SHN_UNDEF)
2291 sym_sec = bfd_und_section_ptr;
2292 else if (isym->st_shndx == SHN_ABS)
2293 sym_sec = bfd_abs_section_ptr;
2294 else if (isym->st_shndx == SHN_COMMON)
2295 sym_sec = bfd_com_section_ptr;
2296 else
2297 sym_sec
2298 = bfd_section_from_elf_index (input_bfd,
2299 isym->st_shndx);
2301 sym_name = (bfd_elf_string_from_elf_section
2302 (input_bfd, symtab_hdr->sh_link,
2303 isym->st_name));
2305 /* Tack on an ID so we can uniquely identify this
2306 local symbol in the global hash table. */
2307 amt = strlen (sym_name) + 10;
2308 new_name = bfd_malloc (amt);
2309 if (new_name == NULL)
2310 goto error_return;
2312 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2313 sym_name = new_name;
2315 elftab = &hash_table->static_hash_table->root;
2316 hash = ((struct elf32_mn10300_link_hash_entry *)
2317 elf_link_hash_lookup (elftab, sym_name,
2318 TRUE, TRUE, FALSE));
2319 free (new_name);
2320 compute_function_info (input_bfd, hash,
2321 isym->st_value, contents);
2322 hash->value = isym->st_value;
2326 for (; hashes < end_hashes; hashes++)
2328 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2329 if ((hash->root.root.type == bfd_link_hash_defined
2330 || hash->root.root.type == bfd_link_hash_defweak)
2331 && hash->root.root.u.def.section == section
2332 && hash->root.type == STT_FUNC)
2333 compute_function_info (input_bfd, hash,
2334 (hash)->root.root.u.def.value,
2335 contents);
2339 /* Cache or free any memory we allocated for the relocs. */
2340 if (internal_relocs != NULL
2341 && elf_section_data (section)->relocs != internal_relocs)
2342 free (internal_relocs);
2343 internal_relocs = NULL;
2345 /* Cache or free any memory we allocated for the contents. */
2346 if (contents != NULL
2347 && elf_section_data (section)->this_hdr.contents != contents)
2349 if (! link_info->keep_memory)
2350 free (contents);
2351 else
2353 /* Cache the section contents for elf_link_input_bfd. */
2354 elf_section_data (section)->this_hdr.contents = contents;
2357 contents = NULL;
2360 /* Cache or free any memory we allocated for the symbols. */
2361 if (isymbuf != NULL
2362 && symtab_hdr->contents != (unsigned char *) isymbuf)
2364 if (! link_info->keep_memory)
2365 free (isymbuf);
2366 else
2368 /* Cache the symbols for elf_link_input_bfd. */
2369 symtab_hdr->contents = (unsigned char *) isymbuf;
2372 isymbuf = NULL;
2375 /* Now iterate on each symbol in the hash table and perform
2376 the final initialization steps on each. */
2377 elf32_mn10300_link_hash_traverse (hash_table,
2378 elf32_mn10300_finish_hash_table_entry,
2379 link_info);
2380 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2381 elf32_mn10300_finish_hash_table_entry,
2382 link_info);
2385 /* This section of code collects all our local symbols, sorts
2386 them by value, and looks for multiple symbols referring to
2387 the same address. For those symbols, the flags are merged.
2388 At this point, the only flag that can be set is
2389 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2390 together. */
2391 int static_count = 0, i;
2392 struct elf32_mn10300_link_hash_entry **entries;
2393 struct elf32_mn10300_link_hash_entry **ptr;
2395 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2396 elf32_mn10300_count_hash_table_entries,
2397 &static_count);
2399 entries = bfd_malloc (static_count * sizeof (* ptr));
2401 ptr = entries;
2402 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2403 elf32_mn10300_list_hash_table_entries,
2404 & ptr);
2406 qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2408 for (i = 0; i < static_count - 1; i++)
2409 if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2411 int v = entries[i]->flags;
2412 int j;
2414 for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2415 v |= entries[j]->flags;
2417 for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2418 entries[j]->flags = v;
2420 i = j - 1;
2424 /* All entries in the hash table are fully initialized. */
2425 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2427 /* Now that everything has been initialized, go through each
2428 code section and delete any prologue insns which will be
2429 redundant because their operations will be performed by
2430 a "call" instruction. */
2431 for (input_bfd = link_info->input_bfds;
2432 input_bfd != NULL;
2433 input_bfd = input_bfd->link_next)
2435 /* We're going to need all the local symbols for each bfd. */
2436 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2437 if (symtab_hdr->sh_info != 0)
2439 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2440 if (isymbuf == NULL)
2441 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2442 symtab_hdr->sh_info, 0,
2443 NULL, NULL, NULL);
2444 if (isymbuf == NULL)
2445 goto error_return;
2448 /* Walk over each section in this bfd. */
2449 for (section = input_bfd->sections;
2450 section != NULL;
2451 section = section->next)
2453 unsigned int sec_shndx;
2454 Elf_Internal_Sym *isym, *isymend;
2455 struct elf_link_hash_entry **hashes;
2456 struct elf_link_hash_entry **end_hashes;
2457 unsigned int symcount;
2459 /* Skip non-code sections and empty sections. */
2460 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2461 continue;
2463 if (section->reloc_count != 0)
2465 /* Get a copy of the native relocations. */
2466 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2467 NULL, NULL,
2468 link_info->keep_memory);
2469 if (internal_relocs == NULL)
2470 goto error_return;
2473 /* Get cached copy of section contents if it exists. */
2474 if (elf_section_data (section)->this_hdr.contents != NULL)
2475 contents = elf_section_data (section)->this_hdr.contents;
2476 else
2478 /* Go get them off disk. */
2479 if (!bfd_malloc_and_get_section (input_bfd, section,
2480 &contents))
2481 goto error_return;
2484 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2485 section);
2487 /* Now look for any function in this section which needs
2488 insns deleted from its prologue. */
2489 isymend = isymbuf + symtab_hdr->sh_info;
2490 for (isym = isymbuf; isym < isymend; isym++)
2492 struct elf32_mn10300_link_hash_entry *sym_hash;
2493 asection *sym_sec = NULL;
2494 const char *sym_name;
2495 char *new_name;
2496 struct elf_link_hash_table *elftab;
2497 bfd_size_type amt;
2499 if (isym->st_shndx != sec_shndx)
2500 continue;
2502 if (isym->st_shndx == SHN_UNDEF)
2503 sym_sec = bfd_und_section_ptr;
2504 else if (isym->st_shndx == SHN_ABS)
2505 sym_sec = bfd_abs_section_ptr;
2506 else if (isym->st_shndx == SHN_COMMON)
2507 sym_sec = bfd_com_section_ptr;
2508 else
2509 sym_sec
2510 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2512 sym_name
2513 = bfd_elf_string_from_elf_section (input_bfd,
2514 symtab_hdr->sh_link,
2515 isym->st_name);
2517 /* Tack on an ID so we can uniquely identify this
2518 local symbol in the global hash table. */
2519 amt = strlen (sym_name) + 10;
2520 new_name = bfd_malloc (amt);
2521 if (new_name == NULL)
2522 goto error_return;
2523 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2524 sym_name = new_name;
2526 elftab = & hash_table->static_hash_table->root;
2527 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2528 elf_link_hash_lookup (elftab, sym_name,
2529 FALSE, FALSE, FALSE);
2531 free (new_name);
2532 if (sym_hash == NULL)
2533 continue;
2535 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2536 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2538 int bytes = 0;
2540 /* Note that we've changed things. */
2541 elf_section_data (section)->relocs = internal_relocs;
2542 elf_section_data (section)->this_hdr.contents = contents;
2543 symtab_hdr->contents = (unsigned char *) isymbuf;
2545 /* Count how many bytes we're going to delete. */
2546 if (sym_hash->movm_args)
2547 bytes += 2;
2549 if (sym_hash->stack_size > 0)
2551 if (sym_hash->stack_size <= 128)
2552 bytes += 3;
2553 else
2554 bytes += 4;
2557 /* Note that we've deleted prologue bytes for this
2558 function. */
2559 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2561 /* Actually delete the bytes. */
2562 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2563 section,
2564 isym->st_value,
2565 bytes))
2566 goto error_return;
2568 /* Something changed. Not strictly necessary, but
2569 may lead to more relaxing opportunities. */
2570 *again = TRUE;
2574 /* Look for any global functions in this section which
2575 need insns deleted from their prologues. */
2576 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2577 - symtab_hdr->sh_info);
2578 hashes = elf_sym_hashes (input_bfd);
2579 end_hashes = hashes + symcount;
2580 for (; hashes < end_hashes; hashes++)
2582 struct elf32_mn10300_link_hash_entry *sym_hash;
2584 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2585 if ((sym_hash->root.root.type == bfd_link_hash_defined
2586 || sym_hash->root.root.type == bfd_link_hash_defweak)
2587 && sym_hash->root.root.u.def.section == section
2588 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2589 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2591 int bytes = 0;
2592 bfd_vma symval;
2594 /* Note that we've changed things. */
2595 elf_section_data (section)->relocs = internal_relocs;
2596 elf_section_data (section)->this_hdr.contents = contents;
2597 symtab_hdr->contents = (unsigned char *) isymbuf;
2599 /* Count how many bytes we're going to delete. */
2600 if (sym_hash->movm_args)
2601 bytes += 2;
2603 if (sym_hash->stack_size > 0)
2605 if (sym_hash->stack_size <= 128)
2606 bytes += 3;
2607 else
2608 bytes += 4;
2611 /* Note that we've deleted prologue bytes for this
2612 function. */
2613 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2615 /* Actually delete the bytes. */
2616 symval = sym_hash->root.root.u.def.value;
2617 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2618 section,
2619 symval,
2620 bytes))
2621 goto error_return;
2623 /* Something changed. Not strictly necessary, but
2624 may lead to more relaxing opportunities. */
2625 *again = TRUE;
2629 /* Cache or free any memory we allocated for the relocs. */
2630 if (internal_relocs != NULL
2631 && elf_section_data (section)->relocs != internal_relocs)
2632 free (internal_relocs);
2633 internal_relocs = NULL;
2635 /* Cache or free any memory we allocated for the contents. */
2636 if (contents != NULL
2637 && elf_section_data (section)->this_hdr.contents != contents)
2639 if (! link_info->keep_memory)
2640 free (contents);
2641 else
2642 /* Cache the section contents for elf_link_input_bfd. */
2643 elf_section_data (section)->this_hdr.contents = contents;
2645 contents = NULL;
2648 /* Cache or free any memory we allocated for the symbols. */
2649 if (isymbuf != NULL
2650 && symtab_hdr->contents != (unsigned char *) isymbuf)
2652 if (! link_info->keep_memory)
2653 free (isymbuf);
2654 else
2655 /* Cache the symbols for elf_link_input_bfd. */
2656 symtab_hdr->contents = (unsigned char *) isymbuf;
2658 isymbuf = NULL;
2662 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2663 contents = NULL;
2664 internal_relocs = NULL;
2665 isymbuf = NULL;
2666 /* For error_return. */
2667 section = sec;
2669 /* We don't have to do anything for a relocatable link, if
2670 this section does not have relocs, or if this is not a
2671 code section. */
2672 if (link_info->relocatable
2673 || (sec->flags & SEC_RELOC) == 0
2674 || sec->reloc_count == 0
2675 || (sec->flags & SEC_CODE) == 0)
2676 return TRUE;
2678 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2680 /* Get a copy of the native relocations. */
2681 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2682 link_info->keep_memory);
2683 if (internal_relocs == NULL)
2684 goto error_return;
2686 /* Scan for worst case alignment gap changes. Note that this logic
2687 is not ideal; what we should do is run this scan for every
2688 opcode/address range and adjust accordingly, but that's
2689 expensive. Worst case is that for an alignment of N bytes, we
2690 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2691 all before it. Plus, this still doesn't cover cross-section
2692 jumps with section alignment. */
2693 irelend = internal_relocs + sec->reloc_count;
2694 align_gap_adjustment = 0;
2695 for (irel = internal_relocs; irel < irelend; irel++)
2697 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
2699 bfd_vma adj = 1 << irel->r_addend;
2700 bfd_vma aend = irel->r_offset;
2702 aend = BFD_ALIGN (aend, 1 << irel->r_addend);
2703 adj = 2 * adj - adj - 1;
2705 /* Record the biggest adjustmnet. Skip any alignment at the
2706 end of our section. */
2707 if (align_gap_adjustment < adj
2708 && aend < sec->output_section->vma + sec->output_offset + sec->size)
2709 align_gap_adjustment = adj;
2713 /* Walk through them looking for relaxing opportunities. */
2714 irelend = internal_relocs + sec->reloc_count;
2715 for (irel = internal_relocs; irel < irelend; irel++)
2717 bfd_vma symval;
2718 bfd_signed_vma jump_offset;
2719 asection *sym_sec = NULL;
2720 struct elf32_mn10300_link_hash_entry *h = NULL;
2722 /* If this isn't something that can be relaxed, then ignore
2723 this reloc. */
2724 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2725 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2726 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2727 continue;
2729 /* Get the section contents if we haven't done so already. */
2730 if (contents == NULL)
2732 /* Get cached copy if it exists. */
2733 if (elf_section_data (sec)->this_hdr.contents != NULL)
2734 contents = elf_section_data (sec)->this_hdr.contents;
2735 else
2737 /* Go get them off disk. */
2738 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2739 goto error_return;
2743 /* Read this BFD's symbols if we haven't done so already. */
2744 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2746 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2747 if (isymbuf == NULL)
2748 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2749 symtab_hdr->sh_info, 0,
2750 NULL, NULL, NULL);
2751 if (isymbuf == NULL)
2752 goto error_return;
2755 /* Get the value of the symbol referred to by the reloc. */
2756 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2758 Elf_Internal_Sym *isym;
2759 const char *sym_name;
2760 char *new_name;
2762 /* A local symbol. */
2763 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2764 if (isym->st_shndx == SHN_UNDEF)
2765 sym_sec = bfd_und_section_ptr;
2766 else if (isym->st_shndx == SHN_ABS)
2767 sym_sec = bfd_abs_section_ptr;
2768 else if (isym->st_shndx == SHN_COMMON)
2769 sym_sec = bfd_com_section_ptr;
2770 else
2771 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2773 sym_name = bfd_elf_string_from_elf_section (abfd,
2774 symtab_hdr->sh_link,
2775 isym->st_name);
2777 if ((sym_sec->flags & SEC_MERGE)
2778 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2780 symval = isym->st_value;
2782 /* GAS may reduce relocations against symbols in SEC_MERGE
2783 sections to a relocation against the section symbol when
2784 the original addend was zero. When the reloc is against
2785 a section symbol we should include the addend in the
2786 offset passed to _bfd_merged_section_offset, since the
2787 location of interest is the original symbol. On the
2788 other hand, an access to "sym+addend" where "sym" is not
2789 a section symbol should not include the addend; Such an
2790 access is presumed to be an offset from "sym"; The
2791 location of interest is just "sym". */
2792 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2793 symval += irel->r_addend;
2795 symval = _bfd_merged_section_offset (abfd, & sym_sec,
2796 elf_section_data (sym_sec)->sec_info,
2797 symval);
2799 if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2800 symval += irel->r_addend;
2802 symval += sym_sec->output_section->vma
2803 + sym_sec->output_offset - irel->r_addend;
2805 else
2806 symval = (isym->st_value
2807 + sym_sec->output_section->vma
2808 + sym_sec->output_offset);
2810 /* Tack on an ID so we can uniquely identify this
2811 local symbol in the global hash table. */
2812 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2813 if (new_name == NULL)
2814 goto error_return;
2815 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2816 sym_name = new_name;
2818 h = (struct elf32_mn10300_link_hash_entry *)
2819 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2820 sym_name, FALSE, FALSE, FALSE);
2821 free (new_name);
2823 else
2825 unsigned long indx;
2827 /* An external symbol. */
2828 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2829 h = (struct elf32_mn10300_link_hash_entry *)
2830 (elf_sym_hashes (abfd)[indx]);
2831 BFD_ASSERT (h != NULL);
2832 if (h->root.root.type != bfd_link_hash_defined
2833 && h->root.root.type != bfd_link_hash_defweak)
2834 /* This appears to be a reference to an undefined
2835 symbol. Just ignore it--it will be caught by the
2836 regular reloc processing. */
2837 continue;
2839 /* Check for a reference to a discarded symbol and ignore it. */
2840 if (h->root.root.u.def.section->output_section == NULL)
2841 continue;
2843 sym_sec = h->root.root.u.def.section->output_section;
2845 symval = (h->root.root.u.def.value
2846 + h->root.root.u.def.section->output_section->vma
2847 + h->root.root.u.def.section->output_offset);
2850 /* For simplicity of coding, we are going to modify the section
2851 contents, the section relocs, and the BFD symbol table. We
2852 must tell the rest of the code not to free up this
2853 information. It would be possible to instead create a table
2854 of changes which have to be made, as is done in coff-mips.c;
2855 that would be more work, but would require less memory when
2856 the linker is run. */
2858 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2859 branch/call, also deal with "call" -> "calls" conversions and
2860 insertion of prologue data into "call" instructions. */
2861 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2862 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2864 bfd_vma value = symval;
2866 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2867 && h != NULL
2868 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2869 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2870 && h->root.plt.offset != (bfd_vma) -1)
2872 asection * splt;
2874 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2875 ->dynobj, ".plt");
2877 value = ((splt->output_section->vma
2878 + splt->output_offset
2879 + h->root.plt.offset)
2880 - (sec->output_section->vma
2881 + sec->output_offset
2882 + irel->r_offset));
2885 /* If we've got a "call" instruction that needs to be turned
2886 into a "calls" instruction, do so now. It saves a byte. */
2887 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2889 unsigned char code;
2891 /* Get the opcode. */
2892 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2894 /* Make sure we're working with a "call" instruction! */
2895 if (code == 0xdd)
2897 /* Note that we've changed the relocs, section contents,
2898 etc. */
2899 elf_section_data (sec)->relocs = internal_relocs;
2900 elf_section_data (sec)->this_hdr.contents = contents;
2901 symtab_hdr->contents = (unsigned char *) isymbuf;
2903 /* Fix the opcode. */
2904 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2905 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2907 /* Fix irel->r_offset and irel->r_addend. */
2908 irel->r_offset += 1;
2909 irel->r_addend += 1;
2911 /* Delete one byte of data. */
2912 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2913 irel->r_offset + 3, 1))
2914 goto error_return;
2916 /* That will change things, so, we should relax again.
2917 Note that this is not required, and it may be slow. */
2918 *again = TRUE;
2921 else if (h)
2923 /* We've got a "call" instruction which needs some data
2924 from target function filled in. */
2925 unsigned char code;
2927 /* Get the opcode. */
2928 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2930 /* Insert data from the target function into the "call"
2931 instruction if needed. */
2932 if (code == 0xdd)
2934 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2935 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2936 contents + irel->r_offset + 5);
2940 /* Deal with pc-relative gunk. */
2941 value -= (sec->output_section->vma + sec->output_offset);
2942 value -= irel->r_offset;
2943 value += irel->r_addend;
2945 /* See if the value will fit in 16 bits, note the high value is
2946 0x7fff + 2 as the target will be two bytes closer if we are
2947 able to relax, if it's in the same section. */
2948 if (sec->output_section == sym_sec->output_section)
2949 jump_offset = 0x8001;
2950 else
2951 jump_offset = 0x7fff;
2953 /* Account for jumps across alignment boundaries using
2954 align_gap_adjustment. */
2955 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
2956 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
2958 unsigned char code;
2960 /* Get the opcode. */
2961 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2963 if (code != 0xdc && code != 0xdd && code != 0xff)
2964 continue;
2966 /* Note that we've changed the relocs, section contents, etc. */
2967 elf_section_data (sec)->relocs = internal_relocs;
2968 elf_section_data (sec)->this_hdr.contents = contents;
2969 symtab_hdr->contents = (unsigned char *) isymbuf;
2971 /* Fix the opcode. */
2972 if (code == 0xdc)
2973 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2974 else if (code == 0xdd)
2975 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2976 else if (code == 0xff)
2977 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2979 /* Fix the relocation's type. */
2980 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2981 (ELF32_R_TYPE (irel->r_info)
2982 == (int) R_MN10300_PLT32)
2983 ? R_MN10300_PLT16 :
2984 R_MN10300_PCREL16);
2986 /* Delete two bytes of data. */
2987 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2988 irel->r_offset + 1, 2))
2989 goto error_return;
2991 /* That will change things, so, we should relax again.
2992 Note that this is not required, and it may be slow. */
2993 *again = TRUE;
2997 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2998 branch. */
2999 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
3001 bfd_vma value = symval;
3003 /* If we've got a "call" instruction that needs to be turned
3004 into a "calls" instruction, do so now. It saves a byte. */
3005 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3007 unsigned char code;
3009 /* Get the opcode. */
3010 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3012 /* Make sure we're working with a "call" instruction! */
3013 if (code == 0xcd)
3015 /* Note that we've changed the relocs, section contents,
3016 etc. */
3017 elf_section_data (sec)->relocs = internal_relocs;
3018 elf_section_data (sec)->this_hdr.contents = contents;
3019 symtab_hdr->contents = (unsigned char *) isymbuf;
3021 /* Fix the opcode. */
3022 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3023 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3025 /* Fix irel->r_offset and irel->r_addend. */
3026 irel->r_offset += 1;
3027 irel->r_addend += 1;
3029 /* Delete one byte of data. */
3030 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3031 irel->r_offset + 1, 1))
3032 goto error_return;
3034 /* That will change things, so, we should relax again.
3035 Note that this is not required, and it may be slow. */
3036 *again = TRUE;
3039 else if (h)
3041 unsigned char code;
3043 /* Get the opcode. */
3044 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3046 /* Insert data from the target function into the "call"
3047 instruction if needed. */
3048 if (code == 0xcd)
3050 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3051 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3052 contents + irel->r_offset + 3);
3056 /* Deal with pc-relative gunk. */
3057 value -= (sec->output_section->vma + sec->output_offset);
3058 value -= irel->r_offset;
3059 value += irel->r_addend;
3061 /* See if the value will fit in 8 bits, note the high value is
3062 0x7f + 1 as the target will be one bytes closer if we are
3063 able to relax. */
3064 if ((long) value < 0x80 && (long) value > -0x80)
3066 unsigned char code;
3068 /* Get the opcode. */
3069 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3071 if (code != 0xcc)
3072 continue;
3074 /* Note that we've changed the relocs, section contents, etc. */
3075 elf_section_data (sec)->relocs = internal_relocs;
3076 elf_section_data (sec)->this_hdr.contents = contents;
3077 symtab_hdr->contents = (unsigned char *) isymbuf;
3079 /* Fix the opcode. */
3080 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3082 /* Fix the relocation's type. */
3083 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3084 R_MN10300_PCREL8);
3086 /* Delete one byte of data. */
3087 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3088 irel->r_offset + 1, 1))
3089 goto error_return;
3091 /* That will change things, so, we should relax again.
3092 Note that this is not required, and it may be slow. */
3093 *again = TRUE;
3097 /* Try to eliminate an unconditional 8 bit pc-relative branch
3098 which immediately follows a conditional 8 bit pc-relative
3099 branch around the unconditional branch.
3101 original: new:
3102 bCC lab1 bCC' lab2
3103 bra lab2
3104 lab1: lab1:
3106 This happens when the bCC can't reach lab2 at assembly time,
3107 but due to other relaxations it can reach at link time. */
3108 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3110 Elf_Internal_Rela *nrel;
3111 bfd_vma value = symval;
3112 unsigned char code;
3114 /* Deal with pc-relative gunk. */
3115 value -= (sec->output_section->vma + sec->output_offset);
3116 value -= irel->r_offset;
3117 value += irel->r_addend;
3119 /* Do nothing if this reloc is the last byte in the section. */
3120 if (irel->r_offset == sec->size)
3121 continue;
3123 /* See if the next instruction is an unconditional pc-relative
3124 branch, more often than not this test will fail, so we
3125 test it first to speed things up. */
3126 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3127 if (code != 0xca)
3128 continue;
3130 /* Also make sure the next relocation applies to the next
3131 instruction and that it's a pc-relative 8 bit branch. */
3132 nrel = irel + 1;
3133 if (nrel == irelend
3134 || irel->r_offset + 2 != nrel->r_offset
3135 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3136 continue;
3138 /* Make sure our destination immediately follows the
3139 unconditional branch. */
3140 if (symval != (sec->output_section->vma + sec->output_offset
3141 + irel->r_offset + 3))
3142 continue;
3144 /* Now make sure we are a conditional branch. This may not
3145 be necessary, but why take the chance.
3147 Note these checks assume that R_MN10300_PCREL8 relocs
3148 only occur on bCC and bCCx insns. If they occured
3149 elsewhere, we'd need to know the start of this insn
3150 for this check to be accurate. */
3151 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3152 if (code != 0xc0 && code != 0xc1 && code != 0xc2
3153 && code != 0xc3 && code != 0xc4 && code != 0xc5
3154 && code != 0xc6 && code != 0xc7 && code != 0xc8
3155 && code != 0xc9 && code != 0xe8 && code != 0xe9
3156 && code != 0xea && code != 0xeb)
3157 continue;
3159 /* We also have to be sure there is no symbol/label
3160 at the unconditional branch. */
3161 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3162 irel->r_offset + 1))
3163 continue;
3165 /* Note that we've changed the relocs, section contents, etc. */
3166 elf_section_data (sec)->relocs = internal_relocs;
3167 elf_section_data (sec)->this_hdr.contents = contents;
3168 symtab_hdr->contents = (unsigned char *) isymbuf;
3170 /* Reverse the condition of the first branch. */
3171 switch (code)
3173 case 0xc8:
3174 code = 0xc9;
3175 break;
3176 case 0xc9:
3177 code = 0xc8;
3178 break;
3179 case 0xc0:
3180 code = 0xc2;
3181 break;
3182 case 0xc2:
3183 code = 0xc0;
3184 break;
3185 case 0xc3:
3186 code = 0xc1;
3187 break;
3188 case 0xc1:
3189 code = 0xc3;
3190 break;
3191 case 0xc4:
3192 code = 0xc6;
3193 break;
3194 case 0xc6:
3195 code = 0xc4;
3196 break;
3197 case 0xc7:
3198 code = 0xc5;
3199 break;
3200 case 0xc5:
3201 code = 0xc7;
3202 break;
3203 case 0xe8:
3204 code = 0xe9;
3205 break;
3206 case 0x9d:
3207 code = 0xe8;
3208 break;
3209 case 0xea:
3210 code = 0xeb;
3211 break;
3212 case 0xeb:
3213 code = 0xea;
3214 break;
3216 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3218 /* Set the reloc type and symbol for the first branch
3219 from the second branch. */
3220 irel->r_info = nrel->r_info;
3222 /* Make the reloc for the second branch a null reloc. */
3223 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3224 R_MN10300_NONE);
3226 /* Delete two bytes of data. */
3227 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3228 irel->r_offset + 1, 2))
3229 goto error_return;
3231 /* That will change things, so, we should relax again.
3232 Note that this is not required, and it may be slow. */
3233 *again = TRUE;
3236 /* Try to turn a 24 immediate, displacement or absolute address
3237 into a 8 immediate, displacement or absolute address. */
3238 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3240 bfd_vma value = symval;
3241 value += irel->r_addend;
3243 /* See if the value will fit in 8 bits. */
3244 if ((long) value < 0x7f && (long) value > -0x80)
3246 unsigned char code;
3248 /* AM33 insns which have 24 operands are 6 bytes long and
3249 will have 0xfd as the first byte. */
3251 /* Get the first opcode. */
3252 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3254 if (code == 0xfd)
3256 /* Get the second opcode. */
3257 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3259 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3260 equivalent instructions exists. */
3261 if (code != 0x6b && code != 0x7b
3262 && code != 0x8b && code != 0x9b
3263 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3264 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3265 || (code & 0x0f) == 0x0e))
3267 /* Not safe if the high bit is on as relaxing may
3268 move the value out of high mem and thus not fit
3269 in a signed 8bit value. This is currently over
3270 conservative. */
3271 if ((value & 0x80) == 0)
3273 /* Note that we've changed the relocation contents,
3274 etc. */
3275 elf_section_data (sec)->relocs = internal_relocs;
3276 elf_section_data (sec)->this_hdr.contents = contents;
3277 symtab_hdr->contents = (unsigned char *) isymbuf;
3279 /* Fix the opcode. */
3280 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3281 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3283 /* Fix the relocation's type. */
3284 irel->r_info =
3285 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3286 R_MN10300_8);
3288 /* Delete two bytes of data. */
3289 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3290 irel->r_offset + 1, 2))
3291 goto error_return;
3293 /* That will change things, so, we should relax
3294 again. Note that this is not required, and it
3295 may be slow. */
3296 *again = TRUE;
3297 break;
3304 /* Try to turn a 32bit immediate, displacement or absolute address
3305 into a 16bit immediate, displacement or absolute address. */
3306 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3307 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3308 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3310 bfd_vma value = symval;
3312 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3314 asection * sgot;
3316 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3317 ->dynobj, ".got");
3319 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3321 value = sgot->output_offset;
3323 if (h)
3324 value += h->root.got.offset;
3325 else
3326 value += (elf_local_got_offsets
3327 (abfd)[ELF32_R_SYM (irel->r_info)]);
3329 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3330 value -= sgot->output_section->vma;
3331 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3332 value = (sgot->output_section->vma
3333 - (sec->output_section->vma
3334 + sec->output_offset
3335 + irel->r_offset));
3336 else
3337 abort ();
3340 value += irel->r_addend;
3342 /* See if the value will fit in 24 bits.
3343 We allow any 16bit match here. We prune those we can't
3344 handle below. */
3345 if ((long) value < 0x7fffff && (long) value > -0x800000)
3347 unsigned char code;
3349 /* AM33 insns which have 32bit operands are 7 bytes long and
3350 will have 0xfe as the first byte. */
3352 /* Get the first opcode. */
3353 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3355 if (code == 0xfe)
3357 /* Get the second opcode. */
3358 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3360 /* All the am33 32 -> 24 relaxing possibilities. */
3361 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3362 equivalent instructions exists. */
3363 if (code != 0x6b && code != 0x7b
3364 && code != 0x8b && code != 0x9b
3365 && (ELF32_R_TYPE (irel->r_info)
3366 != (int) R_MN10300_GOTPC32)
3367 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3368 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3369 || (code & 0x0f) == 0x0e))
3371 /* Not safe if the high bit is on as relaxing may
3372 move the value out of high mem and thus not fit
3373 in a signed 16bit value. This is currently over
3374 conservative. */
3375 if ((value & 0x8000) == 0)
3377 /* Note that we've changed the relocation contents,
3378 etc. */
3379 elf_section_data (sec)->relocs = internal_relocs;
3380 elf_section_data (sec)->this_hdr.contents = contents;
3381 symtab_hdr->contents = (unsigned char *) isymbuf;
3383 /* Fix the opcode. */
3384 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3385 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3387 /* Fix the relocation's type. */
3388 irel->r_info =
3389 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3390 (ELF32_R_TYPE (irel->r_info)
3391 == (int) R_MN10300_GOTOFF32)
3392 ? R_MN10300_GOTOFF24
3393 : (ELF32_R_TYPE (irel->r_info)
3394 == (int) R_MN10300_GOT32)
3395 ? R_MN10300_GOT24 :
3396 R_MN10300_24);
3398 /* Delete one byte of data. */
3399 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3400 irel->r_offset + 3, 1))
3401 goto error_return;
3403 /* That will change things, so, we should relax
3404 again. Note that this is not required, and it
3405 may be slow. */
3406 *again = TRUE;
3407 break;
3413 /* See if the value will fit in 16 bits.
3414 We allow any 16bit match here. We prune those we can't
3415 handle below. */
3416 if ((long) value < 0x7fff && (long) value > -0x8000)
3418 unsigned char code;
3420 /* Most insns which have 32bit operands are 6 bytes long;
3421 exceptions are pcrel insns and bit insns.
3423 We handle pcrel insns above. We don't bother trying
3424 to handle the bit insns here.
3426 The first byte of the remaining insns will be 0xfc. */
3428 /* Get the first opcode. */
3429 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3431 if (code != 0xfc)
3432 continue;
3434 /* Get the second opcode. */
3435 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3437 if ((code & 0xf0) < 0x80)
3438 switch (code & 0xf0)
3440 /* mov (d32,am),dn -> mov (d32,am),dn
3441 mov dm,(d32,am) -> mov dn,(d32,am)
3442 mov (d32,am),an -> mov (d32,am),an
3443 mov dm,(d32,am) -> mov dn,(d32,am)
3444 movbu (d32,am),dn -> movbu (d32,am),dn
3445 movbu dm,(d32,am) -> movbu dn,(d32,am)
3446 movhu (d32,am),dn -> movhu (d32,am),dn
3447 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3448 case 0x00:
3449 case 0x10:
3450 case 0x20:
3451 case 0x30:
3452 case 0x40:
3453 case 0x50:
3454 case 0x60:
3455 case 0x70:
3456 /* Not safe if the high bit is on as relaxing may
3457 move the value out of high mem and thus not fit
3458 in a signed 16bit value. */
3459 if (code == 0xcc
3460 && (value & 0x8000))
3461 continue;
3463 /* Note that we've changed the relocation contents, etc. */
3464 elf_section_data (sec)->relocs = internal_relocs;
3465 elf_section_data (sec)->this_hdr.contents = contents;
3466 symtab_hdr->contents = (unsigned char *) isymbuf;
3468 /* Fix the opcode. */
3469 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3470 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3472 /* Fix the relocation's type. */
3473 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3474 (ELF32_R_TYPE (irel->r_info)
3475 == (int) R_MN10300_GOTOFF32)
3476 ? R_MN10300_GOTOFF16
3477 : (ELF32_R_TYPE (irel->r_info)
3478 == (int) R_MN10300_GOT32)
3479 ? R_MN10300_GOT16
3480 : (ELF32_R_TYPE (irel->r_info)
3481 == (int) R_MN10300_GOTPC32)
3482 ? R_MN10300_GOTPC16 :
3483 R_MN10300_16);
3485 /* Delete two bytes of data. */
3486 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3487 irel->r_offset + 2, 2))
3488 goto error_return;
3490 /* That will change things, so, we should relax again.
3491 Note that this is not required, and it may be slow. */
3492 *again = TRUE;
3493 break;
3495 else if ((code & 0xf0) == 0x80
3496 || (code & 0xf0) == 0x90)
3497 switch (code & 0xf3)
3499 /* mov dn,(abs32) -> mov dn,(abs16)
3500 movbu dn,(abs32) -> movbu dn,(abs16)
3501 movhu dn,(abs32) -> movhu dn,(abs16) */
3502 case 0x81:
3503 case 0x82:
3504 case 0x83:
3505 /* Note that we've changed the relocation contents, etc. */
3506 elf_section_data (sec)->relocs = internal_relocs;
3507 elf_section_data (sec)->this_hdr.contents = contents;
3508 symtab_hdr->contents = (unsigned char *) isymbuf;
3510 if ((code & 0xf3) == 0x81)
3511 code = 0x01 + (code & 0x0c);
3512 else if ((code & 0xf3) == 0x82)
3513 code = 0x02 + (code & 0x0c);
3514 else if ((code & 0xf3) == 0x83)
3515 code = 0x03 + (code & 0x0c);
3516 else
3517 abort ();
3519 /* Fix the opcode. */
3520 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3522 /* Fix the relocation's type. */
3523 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3524 (ELF32_R_TYPE (irel->r_info)
3525 == (int) R_MN10300_GOTOFF32)
3526 ? R_MN10300_GOTOFF16
3527 : (ELF32_R_TYPE (irel->r_info)
3528 == (int) R_MN10300_GOT32)
3529 ? R_MN10300_GOT16
3530 : (ELF32_R_TYPE (irel->r_info)
3531 == (int) R_MN10300_GOTPC32)
3532 ? R_MN10300_GOTPC16 :
3533 R_MN10300_16);
3535 /* The opcode got shorter too, so we have to fix the
3536 addend and offset too! */
3537 irel->r_offset -= 1;
3539 /* Delete three bytes of data. */
3540 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3541 irel->r_offset + 1, 3))
3542 goto error_return;
3544 /* That will change things, so, we should relax again.
3545 Note that this is not required, and it may be slow. */
3546 *again = TRUE;
3547 break;
3549 /* mov am,(abs32) -> mov am,(abs16)
3550 mov am,(d32,sp) -> mov am,(d16,sp)
3551 mov dm,(d32,sp) -> mov dm,(d32,sp)
3552 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3553 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3554 case 0x80:
3555 case 0x90:
3556 case 0x91:
3557 case 0x92:
3558 case 0x93:
3559 /* sp-based offsets are zero-extended. */
3560 if (code >= 0x90 && code <= 0x93
3561 && (long) value < 0)
3562 continue;
3564 /* Note that we've changed the relocation contents, etc. */
3565 elf_section_data (sec)->relocs = internal_relocs;
3566 elf_section_data (sec)->this_hdr.contents = contents;
3567 symtab_hdr->contents = (unsigned char *) isymbuf;
3569 /* Fix the opcode. */
3570 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3571 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3573 /* Fix the relocation's type. */
3574 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3575 (ELF32_R_TYPE (irel->r_info)
3576 == (int) R_MN10300_GOTOFF32)
3577 ? R_MN10300_GOTOFF16
3578 : (ELF32_R_TYPE (irel->r_info)
3579 == (int) R_MN10300_GOT32)
3580 ? R_MN10300_GOT16
3581 : (ELF32_R_TYPE (irel->r_info)
3582 == (int) R_MN10300_GOTPC32)
3583 ? R_MN10300_GOTPC16 :
3584 R_MN10300_16);
3586 /* Delete two bytes of data. */
3587 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3588 irel->r_offset + 2, 2))
3589 goto error_return;
3591 /* That will change things, so, we should relax again.
3592 Note that this is not required, and it may be slow. */
3593 *again = TRUE;
3594 break;
3596 else if ((code & 0xf0) < 0xf0)
3597 switch (code & 0xfc)
3599 /* mov imm32,dn -> mov imm16,dn
3600 mov imm32,an -> mov imm16,an
3601 mov (abs32),dn -> mov (abs16),dn
3602 movbu (abs32),dn -> movbu (abs16),dn
3603 movhu (abs32),dn -> movhu (abs16),dn */
3604 case 0xcc:
3605 case 0xdc:
3606 case 0xa4:
3607 case 0xa8:
3608 case 0xac:
3609 /* Not safe if the high bit is on as relaxing may
3610 move the value out of high mem and thus not fit
3611 in a signed 16bit value. */
3612 if (code == 0xcc
3613 && (value & 0x8000))
3614 continue;
3616 /* mov imm16, an zero-extends the immediate. */
3617 if (code == 0xdc
3618 && (long) value < 0)
3619 continue;
3621 /* Note that we've changed the relocation contents, etc. */
3622 elf_section_data (sec)->relocs = internal_relocs;
3623 elf_section_data (sec)->this_hdr.contents = contents;
3624 symtab_hdr->contents = (unsigned char *) isymbuf;
3626 if ((code & 0xfc) == 0xcc)
3627 code = 0x2c + (code & 0x03);
3628 else if ((code & 0xfc) == 0xdc)
3629 code = 0x24 + (code & 0x03);
3630 else if ((code & 0xfc) == 0xa4)
3631 code = 0x30 + (code & 0x03);
3632 else if ((code & 0xfc) == 0xa8)
3633 code = 0x34 + (code & 0x03);
3634 else if ((code & 0xfc) == 0xac)
3635 code = 0x38 + (code & 0x03);
3636 else
3637 abort ();
3639 /* Fix the opcode. */
3640 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3642 /* Fix the relocation's type. */
3643 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3644 (ELF32_R_TYPE (irel->r_info)
3645 == (int) R_MN10300_GOTOFF32)
3646 ? R_MN10300_GOTOFF16
3647 : (ELF32_R_TYPE (irel->r_info)
3648 == (int) R_MN10300_GOT32)
3649 ? R_MN10300_GOT16
3650 : (ELF32_R_TYPE (irel->r_info)
3651 == (int) R_MN10300_GOTPC32)
3652 ? R_MN10300_GOTPC16 :
3653 R_MN10300_16);
3655 /* The opcode got shorter too, so we have to fix the
3656 addend and offset too! */
3657 irel->r_offset -= 1;
3659 /* Delete three bytes of data. */
3660 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3661 irel->r_offset + 1, 3))
3662 goto error_return;
3664 /* That will change things, so, we should relax again.
3665 Note that this is not required, and it may be slow. */
3666 *again = TRUE;
3667 break;
3669 /* mov (abs32),an -> mov (abs16),an
3670 mov (d32,sp),an -> mov (d16,sp),an
3671 mov (d32,sp),dn -> mov (d16,sp),dn
3672 movbu (d32,sp),dn -> movbu (d16,sp),dn
3673 movhu (d32,sp),dn -> movhu (d16,sp),dn
3674 add imm32,dn -> add imm16,dn
3675 cmp imm32,dn -> cmp imm16,dn
3676 add imm32,an -> add imm16,an
3677 cmp imm32,an -> cmp imm16,an
3678 and imm32,dn -> and imm16,dn
3679 or imm32,dn -> or imm16,dn
3680 xor imm32,dn -> xor imm16,dn
3681 btst imm32,dn -> btst imm16,dn */
3683 case 0xa0:
3684 case 0xb0:
3685 case 0xb1:
3686 case 0xb2:
3687 case 0xb3:
3688 case 0xc0:
3689 case 0xc8:
3691 case 0xd0:
3692 case 0xd8:
3693 case 0xe0:
3694 case 0xe1:
3695 case 0xe2:
3696 case 0xe3:
3697 /* cmp imm16, an zero-extends the immediate. */
3698 if (code == 0xdc
3699 && (long) value < 0)
3700 continue;
3702 /* So do sp-based offsets. */
3703 if (code >= 0xb0 && code <= 0xb3
3704 && (long) value < 0)
3705 continue;
3707 /* Note that we've changed the relocation contents, etc. */
3708 elf_section_data (sec)->relocs = internal_relocs;
3709 elf_section_data (sec)->this_hdr.contents = contents;
3710 symtab_hdr->contents = (unsigned char *) isymbuf;
3712 /* Fix the opcode. */
3713 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3714 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3716 /* Fix the relocation's type. */
3717 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3718 (ELF32_R_TYPE (irel->r_info)
3719 == (int) R_MN10300_GOTOFF32)
3720 ? R_MN10300_GOTOFF16
3721 : (ELF32_R_TYPE (irel->r_info)
3722 == (int) R_MN10300_GOT32)
3723 ? R_MN10300_GOT16
3724 : (ELF32_R_TYPE (irel->r_info)
3725 == (int) R_MN10300_GOTPC32)
3726 ? R_MN10300_GOTPC16 :
3727 R_MN10300_16);
3729 /* Delete two bytes of data. */
3730 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3731 irel->r_offset + 2, 2))
3732 goto error_return;
3734 /* That will change things, so, we should relax again.
3735 Note that this is not required, and it may be slow. */
3736 *again = TRUE;
3737 break;
3739 else if (code == 0xfe)
3741 /* add imm32,sp -> add imm16,sp */
3743 /* Note that we've changed the relocation contents, etc. */
3744 elf_section_data (sec)->relocs = internal_relocs;
3745 elf_section_data (sec)->this_hdr.contents = contents;
3746 symtab_hdr->contents = (unsigned char *) isymbuf;
3748 /* Fix the opcode. */
3749 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3750 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3752 /* Fix the relocation's type. */
3753 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3754 (ELF32_R_TYPE (irel->r_info)
3755 == (int) R_MN10300_GOT32)
3756 ? R_MN10300_GOT16
3757 : (ELF32_R_TYPE (irel->r_info)
3758 == (int) R_MN10300_GOTOFF32)
3759 ? R_MN10300_GOTOFF16
3760 : (ELF32_R_TYPE (irel->r_info)
3761 == (int) R_MN10300_GOTPC32)
3762 ? R_MN10300_GOTPC16 :
3763 R_MN10300_16);
3765 /* Delete two bytes of data. */
3766 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3767 irel->r_offset + 2, 2))
3768 goto error_return;
3770 /* That will change things, so, we should relax again.
3771 Note that this is not required, and it may be slow. */
3772 *again = TRUE;
3773 break;
3779 if (isymbuf != NULL
3780 && symtab_hdr->contents != (unsigned char *) isymbuf)
3782 if (! link_info->keep_memory)
3783 free (isymbuf);
3784 else
3786 /* Cache the symbols for elf_link_input_bfd. */
3787 symtab_hdr->contents = (unsigned char *) isymbuf;
3791 if (contents != NULL
3792 && elf_section_data (sec)->this_hdr.contents != contents)
3794 if (! link_info->keep_memory)
3795 free (contents);
3796 else
3798 /* Cache the section contents for elf_link_input_bfd. */
3799 elf_section_data (sec)->this_hdr.contents = contents;
3803 if (internal_relocs != NULL
3804 && elf_section_data (sec)->relocs != internal_relocs)
3805 free (internal_relocs);
3807 return TRUE;
3809 error_return:
3810 if (isymbuf != NULL
3811 && symtab_hdr->contents != (unsigned char *) isymbuf)
3812 free (isymbuf);
3813 if (contents != NULL
3814 && elf_section_data (section)->this_hdr.contents != contents)
3815 free (contents);
3816 if (internal_relocs != NULL
3817 && elf_section_data (section)->relocs != internal_relocs)
3818 free (internal_relocs);
3820 return FALSE;
3823 /* This is a version of bfd_generic_get_relocated_section_contents
3824 which uses mn10300_elf_relocate_section. */
3826 static bfd_byte *
3827 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
3828 struct bfd_link_info *link_info,
3829 struct bfd_link_order *link_order,
3830 bfd_byte *data,
3831 bfd_boolean relocatable,
3832 asymbol **symbols)
3834 Elf_Internal_Shdr *symtab_hdr;
3835 asection *input_section = link_order->u.indirect.section;
3836 bfd *input_bfd = input_section->owner;
3837 asection **sections = NULL;
3838 Elf_Internal_Rela *internal_relocs = NULL;
3839 Elf_Internal_Sym *isymbuf = NULL;
3841 /* We only need to handle the case of relaxing, or of having a
3842 particular set of section contents, specially. */
3843 if (relocatable
3844 || elf_section_data (input_section)->this_hdr.contents == NULL)
3845 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3846 link_order, data,
3847 relocatable,
3848 symbols);
3850 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3852 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3853 (size_t) input_section->size);
3855 if ((input_section->flags & SEC_RELOC) != 0
3856 && input_section->reloc_count > 0)
3858 asection **secpp;
3859 Elf_Internal_Sym *isym, *isymend;
3860 bfd_size_type amt;
3862 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3863 NULL, NULL, FALSE);
3864 if (internal_relocs == NULL)
3865 goto error_return;
3867 if (symtab_hdr->sh_info != 0)
3869 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3870 if (isymbuf == NULL)
3871 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3872 symtab_hdr->sh_info, 0,
3873 NULL, NULL, NULL);
3874 if (isymbuf == NULL)
3875 goto error_return;
3878 amt = symtab_hdr->sh_info;
3879 amt *= sizeof (asection *);
3880 sections = bfd_malloc (amt);
3881 if (sections == NULL && amt != 0)
3882 goto error_return;
3884 isymend = isymbuf + symtab_hdr->sh_info;
3885 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3887 asection *isec;
3889 if (isym->st_shndx == SHN_UNDEF)
3890 isec = bfd_und_section_ptr;
3891 else if (isym->st_shndx == SHN_ABS)
3892 isec = bfd_abs_section_ptr;
3893 else if (isym->st_shndx == SHN_COMMON)
3894 isec = bfd_com_section_ptr;
3895 else
3896 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3898 *secpp = isec;
3901 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3902 input_section, data, internal_relocs,
3903 isymbuf, sections))
3904 goto error_return;
3906 if (sections != NULL)
3907 free (sections);
3908 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3909 free (isymbuf);
3910 if (internal_relocs != elf_section_data (input_section)->relocs)
3911 free (internal_relocs);
3914 return data;
3916 error_return:
3917 if (sections != NULL)
3918 free (sections);
3919 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3920 free (isymbuf);
3921 if (internal_relocs != NULL
3922 && internal_relocs != elf_section_data (input_section)->relocs)
3923 free (internal_relocs);
3924 return NULL;
3927 /* Assorted hash table functions. */
3929 /* Initialize an entry in the link hash table. */
3931 /* Create an entry in an MN10300 ELF linker hash table. */
3933 static struct bfd_hash_entry *
3934 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
3935 struct bfd_hash_table *table,
3936 const char *string)
3938 struct elf32_mn10300_link_hash_entry *ret =
3939 (struct elf32_mn10300_link_hash_entry *) entry;
3941 /* Allocate the structure if it has not already been allocated by a
3942 subclass. */
3943 if (ret == NULL)
3944 ret = (struct elf32_mn10300_link_hash_entry *)
3945 bfd_hash_allocate (table, sizeof (* ret));
3946 if (ret == NULL)
3947 return (struct bfd_hash_entry *) ret;
3949 /* Call the allocation method of the superclass. */
3950 ret = (struct elf32_mn10300_link_hash_entry *)
3951 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3952 table, string);
3953 if (ret != NULL)
3955 ret->direct_calls = 0;
3956 ret->stack_size = 0;
3957 ret->movm_args = 0;
3958 ret->movm_stack_size = 0;
3959 ret->flags = 0;
3960 ret->value = 0;
3963 return (struct bfd_hash_entry *) ret;
3966 /* Create an mn10300 ELF linker hash table. */
3968 static struct bfd_link_hash_table *
3969 elf32_mn10300_link_hash_table_create (bfd *abfd)
3971 struct elf32_mn10300_link_hash_table *ret;
3972 bfd_size_type amt = sizeof (* ret);
3974 ret = bfd_malloc (amt);
3975 if (ret == NULL)
3976 return NULL;
3978 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3979 elf32_mn10300_link_hash_newfunc,
3980 sizeof (struct elf32_mn10300_link_hash_entry)))
3982 free (ret);
3983 return NULL;
3986 ret->flags = 0;
3987 amt = sizeof (struct elf_link_hash_table);
3988 ret->static_hash_table = bfd_malloc (amt);
3989 if (ret->static_hash_table == NULL)
3991 free (ret);
3992 return NULL;
3995 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3996 elf32_mn10300_link_hash_newfunc,
3997 sizeof (struct elf32_mn10300_link_hash_entry)))
3999 free (ret->static_hash_table);
4000 free (ret);
4001 return NULL;
4003 return & ret->root.root;
4006 /* Free an mn10300 ELF linker hash table. */
4008 static void
4009 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
4011 struct elf32_mn10300_link_hash_table *ret
4012 = (struct elf32_mn10300_link_hash_table *) hash;
4014 _bfd_generic_link_hash_table_free
4015 ((struct bfd_link_hash_table *) ret->static_hash_table);
4016 _bfd_generic_link_hash_table_free
4017 ((struct bfd_link_hash_table *) ret);
4020 static unsigned long
4021 elf_mn10300_mach (flagword flags)
4023 switch (flags & EF_MN10300_MACH)
4025 case E_MN10300_MACH_MN10300:
4026 default:
4027 return bfd_mach_mn10300;
4029 case E_MN10300_MACH_AM33:
4030 return bfd_mach_am33;
4032 case E_MN10300_MACH_AM33_2:
4033 return bfd_mach_am33_2;
4037 /* The final processing done just before writing out a MN10300 ELF object
4038 file. This gets the MN10300 architecture right based on the machine
4039 number. */
4041 static void
4042 _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4043 bfd_boolean linker ATTRIBUTE_UNUSED)
4045 unsigned long val;
4047 switch (bfd_get_mach (abfd))
4049 default:
4050 case bfd_mach_mn10300:
4051 val = E_MN10300_MACH_MN10300;
4052 break;
4054 case bfd_mach_am33:
4055 val = E_MN10300_MACH_AM33;
4056 break;
4058 case bfd_mach_am33_2:
4059 val = E_MN10300_MACH_AM33_2;
4060 break;
4063 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4064 elf_elfheader (abfd)->e_flags |= val;
4067 static bfd_boolean
4068 _bfd_mn10300_elf_object_p (bfd *abfd)
4070 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4071 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4072 return TRUE;
4075 /* Merge backend specific data from an object file to the output
4076 object file when linking. */
4078 static bfd_boolean
4079 _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4081 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4082 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4083 return TRUE;
4085 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4086 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4088 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4089 bfd_get_mach (ibfd)))
4090 return FALSE;
4093 return TRUE;
4096 #define PLT0_ENTRY_SIZE 15
4097 #define PLT_ENTRY_SIZE 20
4098 #define PIC_PLT_ENTRY_SIZE 24
4100 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4102 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4103 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4104 0xf0, 0xf4, /* jmp (a0) */
4107 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4109 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4110 0xf0, 0xf4, /* jmp (a0) */
4111 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4112 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4115 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4117 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4118 0xf0, 0xf4, /* jmp (a0) */
4119 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4120 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4121 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4122 0xf0, 0xf4, /* jmp (a0) */
4125 /* Return size of the first PLT entry. */
4126 #define elf_mn10300_sizeof_plt0(info) \
4127 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4129 /* Return size of a PLT entry. */
4130 #define elf_mn10300_sizeof_plt(info) \
4131 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4133 /* Return offset of the PLT0 address in an absolute PLT entry. */
4134 #define elf_mn10300_plt_plt0_offset(info) 16
4136 /* Return offset of the linker in PLT0 entry. */
4137 #define elf_mn10300_plt0_linker_offset(info) 2
4139 /* Return offset of the GOT id in PLT0 entry. */
4140 #define elf_mn10300_plt0_gotid_offset(info) 9
4142 /* Return offset of the temporary in PLT entry. */
4143 #define elf_mn10300_plt_temp_offset(info) 8
4145 /* Return offset of the symbol in PLT entry. */
4146 #define elf_mn10300_plt_symbol_offset(info) 2
4148 /* Return offset of the relocation in PLT entry. */
4149 #define elf_mn10300_plt_reloc_offset(info) 11
4151 /* The name of the dynamic interpreter. This is put in the .interp
4152 section. */
4154 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4156 /* Create dynamic sections when linking against a dynamic object. */
4158 static bfd_boolean
4159 _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4161 flagword flags;
4162 asection * s;
4163 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4164 int ptralign = 0;
4166 switch (bed->s->arch_size)
4168 case 32:
4169 ptralign = 2;
4170 break;
4172 case 64:
4173 ptralign = 3;
4174 break;
4176 default:
4177 bfd_set_error (bfd_error_bad_value);
4178 return FALSE;
4181 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4182 .rel[a].bss sections. */
4183 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4184 | SEC_LINKER_CREATED);
4186 s = bfd_make_section_with_flags (abfd,
4187 (bed->default_use_rela_p
4188 ? ".rela.plt" : ".rel.plt"),
4189 flags | SEC_READONLY);
4190 if (s == NULL
4191 || ! bfd_set_section_alignment (abfd, s, ptralign))
4192 return FALSE;
4194 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4195 return FALSE;
4198 const char * secname;
4199 char * relname;
4200 flagword secflags;
4201 asection * sec;
4203 for (sec = abfd->sections; sec; sec = sec->next)
4205 secflags = bfd_get_section_flags (abfd, sec);
4206 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4207 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4208 continue;
4210 secname = bfd_get_section_name (abfd, sec);
4211 relname = bfd_malloc (strlen (secname) + 6);
4212 strcpy (relname, ".rela");
4213 strcat (relname, secname);
4215 s = bfd_make_section_with_flags (abfd, relname,
4216 flags | SEC_READONLY);
4217 if (s == NULL
4218 || ! bfd_set_section_alignment (abfd, s, ptralign))
4219 return FALSE;
4223 if (bed->want_dynbss)
4225 /* The .dynbss section is a place to put symbols which are defined
4226 by dynamic objects, are referenced by regular objects, and are
4227 not functions. We must allocate space for them in the process
4228 image and use a R_*_COPY reloc to tell the dynamic linker to
4229 initialize them at run time. The linker script puts the .dynbss
4230 section into the .bss section of the final image. */
4231 s = bfd_make_section_with_flags (abfd, ".dynbss",
4232 SEC_ALLOC | SEC_LINKER_CREATED);
4233 if (s == NULL)
4234 return FALSE;
4236 /* The .rel[a].bss section holds copy relocs. This section is not
4237 normally needed. We need to create it here, though, so that the
4238 linker will map it to an output section. We can't just create it
4239 only if we need it, because we will not know whether we need it
4240 until we have seen all the input files, and the first time the
4241 main linker code calls BFD after examining all the input files
4242 (size_dynamic_sections) the input sections have already been
4243 mapped to the output sections. If the section turns out not to
4244 be needed, we can discard it later. We will never need this
4245 section when generating a shared object, since they do not use
4246 copy relocs. */
4247 if (! info->shared)
4249 s = bfd_make_section_with_flags (abfd,
4250 (bed->default_use_rela_p
4251 ? ".rela.bss" : ".rel.bss"),
4252 flags | SEC_READONLY);
4253 if (s == NULL
4254 || ! bfd_set_section_alignment (abfd, s, ptralign))
4255 return FALSE;
4259 return TRUE;
4262 /* Adjust a symbol defined by a dynamic object and referenced by a
4263 regular object. The current definition is in some section of the
4264 dynamic object, but we're not including those sections. We have to
4265 change the definition to something the rest of the link can
4266 understand. */
4268 static bfd_boolean
4269 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4270 struct elf_link_hash_entry * h)
4272 bfd * dynobj;
4273 asection * s;
4275 dynobj = elf_hash_table (info)->dynobj;
4277 /* Make sure we know what is going on here. */
4278 BFD_ASSERT (dynobj != NULL
4279 && (h->needs_plt
4280 || h->u.weakdef != NULL
4281 || (h->def_dynamic
4282 && h->ref_regular
4283 && !h->def_regular)));
4285 /* If this is a function, put it in the procedure linkage table. We
4286 will fill in the contents of the procedure linkage table later,
4287 when we know the address of the .got section. */
4288 if (h->type == STT_FUNC
4289 || h->needs_plt)
4291 if (! info->shared
4292 && !h->def_dynamic
4293 && !h->ref_dynamic)
4295 /* This case can occur if we saw a PLT reloc in an input
4296 file, but the symbol was never referred to by a dynamic
4297 object. In such a case, we don't actually need to build
4298 a procedure linkage table, and we can just do a REL32
4299 reloc instead. */
4300 BFD_ASSERT (h->needs_plt);
4301 return TRUE;
4304 /* Make sure this symbol is output as a dynamic symbol. */
4305 if (h->dynindx == -1)
4307 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4308 return FALSE;
4311 s = bfd_get_section_by_name (dynobj, ".plt");
4312 BFD_ASSERT (s != NULL);
4314 /* If this is the first .plt entry, make room for the special
4315 first entry. */
4316 if (s->size == 0)
4317 s->size += elf_mn10300_sizeof_plt0 (info);
4319 /* If this symbol is not defined in a regular file, and we are
4320 not generating a shared library, then set the symbol to this
4321 location in the .plt. This is required to make function
4322 pointers compare as equal between the normal executable and
4323 the shared library. */
4324 if (! info->shared
4325 && !h->def_regular)
4327 h->root.u.def.section = s;
4328 h->root.u.def.value = s->size;
4331 h->plt.offset = s->size;
4333 /* Make room for this entry. */
4334 s->size += elf_mn10300_sizeof_plt (info);
4336 /* We also need to make an entry in the .got.plt section, which
4337 will be placed in the .got section by the linker script. */
4338 s = bfd_get_section_by_name (dynobj, ".got.plt");
4339 BFD_ASSERT (s != NULL);
4340 s->size += 4;
4342 /* We also need to make an entry in the .rela.plt section. */
4343 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4344 BFD_ASSERT (s != NULL);
4345 s->size += sizeof (Elf32_External_Rela);
4347 return TRUE;
4350 /* If this is a weak symbol, and there is a real definition, the
4351 processor independent code will have arranged for us to see the
4352 real definition first, and we can just use the same value. */
4353 if (h->u.weakdef != NULL)
4355 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4356 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4357 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4358 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4359 return TRUE;
4362 /* This is a reference to a symbol defined by a dynamic object which
4363 is not a function. */
4365 /* If we are creating a shared library, we must presume that the
4366 only references to the symbol are via the global offset table.
4367 For such cases we need not do anything here; the relocations will
4368 be handled correctly by relocate_section. */
4369 if (info->shared)
4370 return TRUE;
4372 /* If there are no references to this symbol that do not use the
4373 GOT, we don't need to generate a copy reloc. */
4374 if (!h->non_got_ref)
4375 return TRUE;
4377 if (h->size == 0)
4379 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4380 h->root.root.string);
4381 return TRUE;
4384 /* We must allocate the symbol in our .dynbss section, which will
4385 become part of the .bss section of the executable. There will be
4386 an entry for this symbol in the .dynsym section. The dynamic
4387 object will contain position independent code, so all references
4388 from the dynamic object to this symbol will go through the global
4389 offset table. The dynamic linker will use the .dynsym entry to
4390 determine the address it must put in the global offset table, so
4391 both the dynamic object and the regular object will refer to the
4392 same memory location for the variable. */
4394 s = bfd_get_section_by_name (dynobj, ".dynbss");
4395 BFD_ASSERT (s != NULL);
4397 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4398 copy the initial value out of the dynamic object and into the
4399 runtime process image. We need to remember the offset into the
4400 .rela.bss section we are going to use. */
4401 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4403 asection * srel;
4405 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4406 BFD_ASSERT (srel != NULL);
4407 srel->size += sizeof (Elf32_External_Rela);
4408 h->needs_copy = 1;
4411 return _bfd_elf_adjust_dynamic_copy (h, s);
4414 /* Set the sizes of the dynamic sections. */
4416 static bfd_boolean
4417 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
4418 struct bfd_link_info * info)
4420 bfd * dynobj;
4421 asection * s;
4422 bfd_boolean plt;
4423 bfd_boolean relocs;
4424 bfd_boolean reltext;
4426 dynobj = elf_hash_table (info)->dynobj;
4427 BFD_ASSERT (dynobj != NULL);
4429 if (elf_hash_table (info)->dynamic_sections_created)
4431 /* Set the contents of the .interp section to the interpreter. */
4432 if (info->executable)
4434 s = bfd_get_section_by_name (dynobj, ".interp");
4435 BFD_ASSERT (s != NULL);
4436 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4437 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4440 else
4442 /* We may have created entries in the .rela.got section.
4443 However, if we are not creating the dynamic sections, we will
4444 not actually use these entries. Reset the size of .rela.got,
4445 which will cause it to get stripped from the output file
4446 below. */
4447 s = bfd_get_section_by_name (dynobj, ".rela.got");
4448 if (s != NULL)
4449 s->size = 0;
4452 /* The check_relocs and adjust_dynamic_symbol entry points have
4453 determined the sizes of the various dynamic sections. Allocate
4454 memory for them. */
4455 plt = FALSE;
4456 relocs = FALSE;
4457 reltext = FALSE;
4458 for (s = dynobj->sections; s != NULL; s = s->next)
4460 const char * name;
4462 if ((s->flags & SEC_LINKER_CREATED) == 0)
4463 continue;
4465 /* It's OK to base decisions on the section name, because none
4466 of the dynobj section names depend upon the input files. */
4467 name = bfd_get_section_name (dynobj, s);
4469 if (streq (name, ".plt"))
4471 /* Remember whether there is a PLT. */
4472 plt = s->size != 0;
4474 else if (CONST_STRNEQ (name, ".rela"))
4476 if (s->size != 0)
4478 asection * target;
4480 /* Remember whether there are any reloc sections other
4481 than .rela.plt. */
4482 if (! streq (name, ".rela.plt"))
4484 const char * outname;
4486 relocs = TRUE;
4488 /* If this relocation section applies to a read only
4489 section, then we probably need a DT_TEXTREL
4490 entry. The entries in the .rela.plt section
4491 really apply to the .got section, which we
4492 created ourselves and so know is not readonly. */
4493 outname = bfd_get_section_name (output_bfd,
4494 s->output_section);
4495 target = bfd_get_section_by_name (output_bfd, outname + 5);
4496 if (target != NULL
4497 && (target->flags & SEC_READONLY) != 0
4498 && (target->flags & SEC_ALLOC) != 0)
4499 reltext = TRUE;
4502 /* We use the reloc_count field as a counter if we need
4503 to copy relocs into the output file. */
4504 s->reloc_count = 0;
4507 else if (! CONST_STRNEQ (name, ".got")
4508 && ! streq (name, ".dynbss"))
4509 /* It's not one of our sections, so don't allocate space. */
4510 continue;
4512 if (s->size == 0)
4514 /* If we don't need this section, strip it from the
4515 output file. This is mostly to handle .rela.bss and
4516 .rela.plt. We must create both sections in
4517 create_dynamic_sections, because they must be created
4518 before the linker maps input sections to output
4519 sections. The linker does that before
4520 adjust_dynamic_symbol is called, and it is that
4521 function which decides whether anything needs to go
4522 into these sections. */
4523 s->flags |= SEC_EXCLUDE;
4524 continue;
4527 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4528 continue;
4530 /* Allocate memory for the section contents. We use bfd_zalloc
4531 here in case unused entries are not reclaimed before the
4532 section's contents are written out. This should not happen,
4533 but this way if it does, we get a R_MN10300_NONE reloc
4534 instead of garbage. */
4535 s->contents = bfd_zalloc (dynobj, s->size);
4536 if (s->contents == NULL)
4537 return FALSE;
4540 if (elf_hash_table (info)->dynamic_sections_created)
4542 /* Add some entries to the .dynamic section. We fill in the
4543 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4544 but we must add the entries now so that we get the correct
4545 size for the .dynamic section. The DT_DEBUG entry is filled
4546 in by the dynamic linker and used by the debugger. */
4547 if (! info->shared)
4549 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4550 return FALSE;
4553 if (plt)
4555 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4556 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4557 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4558 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4559 return FALSE;
4562 if (relocs)
4564 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4565 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4566 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4567 sizeof (Elf32_External_Rela)))
4568 return FALSE;
4571 if (reltext)
4573 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4574 return FALSE;
4578 return TRUE;
4581 /* Finish up dynamic symbol handling. We set the contents of various
4582 dynamic sections here. */
4584 static bfd_boolean
4585 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
4586 struct bfd_link_info * info,
4587 struct elf_link_hash_entry * h,
4588 Elf_Internal_Sym * sym)
4590 bfd * dynobj;
4592 dynobj = elf_hash_table (info)->dynobj;
4594 if (h->plt.offset != (bfd_vma) -1)
4596 asection * splt;
4597 asection * sgot;
4598 asection * srel;
4599 bfd_vma plt_index;
4600 bfd_vma got_offset;
4601 Elf_Internal_Rela rel;
4603 /* This symbol has an entry in the procedure linkage table. Set
4604 it up. */
4606 BFD_ASSERT (h->dynindx != -1);
4608 splt = bfd_get_section_by_name (dynobj, ".plt");
4609 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4610 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4611 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4613 /* Get the index in the procedure linkage table which
4614 corresponds to this symbol. This is the index of this symbol
4615 in all the symbols for which we are making plt entries. The
4616 first entry in the procedure linkage table is reserved. */
4617 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4618 / elf_mn10300_sizeof_plt (info));
4620 /* Get the offset into the .got table of the entry that
4621 corresponds to this function. Each .got entry is 4 bytes.
4622 The first three are reserved. */
4623 got_offset = (plt_index + 3) * 4;
4625 /* Fill in the entry in the procedure linkage table. */
4626 if (! info->shared)
4628 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4629 elf_mn10300_sizeof_plt (info));
4630 bfd_put_32 (output_bfd,
4631 (sgot->output_section->vma
4632 + sgot->output_offset
4633 + got_offset),
4634 (splt->contents + h->plt.offset
4635 + elf_mn10300_plt_symbol_offset (info)));
4637 bfd_put_32 (output_bfd,
4638 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4639 (splt->contents + h->plt.offset
4640 + elf_mn10300_plt_plt0_offset (info)));
4642 else
4644 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4645 elf_mn10300_sizeof_plt (info));
4647 bfd_put_32 (output_bfd, got_offset,
4648 (splt->contents + h->plt.offset
4649 + elf_mn10300_plt_symbol_offset (info)));
4652 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4653 (splt->contents + h->plt.offset
4654 + elf_mn10300_plt_reloc_offset (info)));
4656 /* Fill in the entry in the global offset table. */
4657 bfd_put_32 (output_bfd,
4658 (splt->output_section->vma
4659 + splt->output_offset
4660 + h->plt.offset
4661 + elf_mn10300_plt_temp_offset (info)),
4662 sgot->contents + got_offset);
4664 /* Fill in the entry in the .rela.plt section. */
4665 rel.r_offset = (sgot->output_section->vma
4666 + sgot->output_offset
4667 + got_offset);
4668 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4669 rel.r_addend = 0;
4670 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4671 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4672 + plt_index));
4674 if (!h->def_regular)
4675 /* Mark the symbol as undefined, rather than as defined in
4676 the .plt section. Leave the value alone. */
4677 sym->st_shndx = SHN_UNDEF;
4680 if (h->got.offset != (bfd_vma) -1)
4682 asection * sgot;
4683 asection * srel;
4684 Elf_Internal_Rela rel;
4686 /* This symbol has an entry in the global offset table. Set it up. */
4687 sgot = bfd_get_section_by_name (dynobj, ".got");
4688 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4689 BFD_ASSERT (sgot != NULL && srel != NULL);
4691 rel.r_offset = (sgot->output_section->vma
4692 + sgot->output_offset
4693 + (h->got.offset & ~1));
4695 /* If this is a -Bsymbolic link, and the symbol is defined
4696 locally, we just want to emit a RELATIVE reloc. Likewise if
4697 the symbol was forced to be local because of a version file.
4698 The entry in the global offset table will already have been
4699 initialized in the relocate_section function. */
4700 if (info->shared
4701 && (info->symbolic || h->dynindx == -1)
4702 && h->def_regular)
4704 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4705 rel.r_addend = (h->root.u.def.value
4706 + h->root.u.def.section->output_section->vma
4707 + h->root.u.def.section->output_offset);
4709 else
4711 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4712 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4713 rel.r_addend = 0;
4716 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4717 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4718 + srel->reloc_count));
4719 ++ srel->reloc_count;
4722 if (h->needs_copy)
4724 asection * s;
4725 Elf_Internal_Rela rel;
4727 /* This symbol needs a copy reloc. Set it up. */
4728 BFD_ASSERT (h->dynindx != -1
4729 && (h->root.type == bfd_link_hash_defined
4730 || h->root.type == bfd_link_hash_defweak));
4732 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4733 ".rela.bss");
4734 BFD_ASSERT (s != NULL);
4736 rel.r_offset = (h->root.u.def.value
4737 + h->root.u.def.section->output_section->vma
4738 + h->root.u.def.section->output_offset);
4739 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4740 rel.r_addend = 0;
4741 bfd_elf32_swap_reloca_out (output_bfd, & rel,
4742 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4743 + s->reloc_count));
4744 ++ s->reloc_count;
4747 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4748 if (streq (h->root.root.string, "_DYNAMIC")
4749 || h == elf_hash_table (info)->hgot)
4750 sym->st_shndx = SHN_ABS;
4752 return TRUE;
4755 /* Finish up the dynamic sections. */
4757 static bfd_boolean
4758 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
4759 struct bfd_link_info * info)
4761 bfd * dynobj;
4762 asection * sgot;
4763 asection * sdyn;
4765 dynobj = elf_hash_table (info)->dynobj;
4767 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4768 BFD_ASSERT (sgot != NULL);
4769 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4771 if (elf_hash_table (info)->dynamic_sections_created)
4773 asection * splt;
4774 Elf32_External_Dyn * dyncon;
4775 Elf32_External_Dyn * dynconend;
4777 BFD_ASSERT (sdyn != NULL);
4779 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4780 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4782 for (; dyncon < dynconend; dyncon++)
4784 Elf_Internal_Dyn dyn;
4785 const char * name;
4786 asection * s;
4788 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4790 switch (dyn.d_tag)
4792 default:
4793 break;
4795 case DT_PLTGOT:
4796 name = ".got";
4797 goto get_vma;
4799 case DT_JMPREL:
4800 name = ".rela.plt";
4801 get_vma:
4802 s = bfd_get_section_by_name (output_bfd, name);
4803 BFD_ASSERT (s != NULL);
4804 dyn.d_un.d_ptr = s->vma;
4805 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4806 break;
4808 case DT_PLTRELSZ:
4809 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4810 BFD_ASSERT (s != NULL);
4811 dyn.d_un.d_val = s->size;
4812 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4813 break;
4815 case DT_RELASZ:
4816 /* My reading of the SVR4 ABI indicates that the
4817 procedure linkage table relocs (DT_JMPREL) should be
4818 included in the overall relocs (DT_RELA). This is
4819 what Solaris does. However, UnixWare can not handle
4820 that case. Therefore, we override the DT_RELASZ entry
4821 here to make it not include the JMPREL relocs. Since
4822 the linker script arranges for .rela.plt to follow all
4823 other relocation sections, we don't have to worry
4824 about changing the DT_RELA entry. */
4825 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4826 if (s != NULL)
4827 dyn.d_un.d_val -= s->size;
4828 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4829 break;
4833 /* Fill in the first entry in the procedure linkage table. */
4834 splt = bfd_get_section_by_name (dynobj, ".plt");
4835 if (splt && splt->size > 0)
4837 if (info->shared)
4839 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4840 elf_mn10300_sizeof_plt (info));
4842 else
4844 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4845 bfd_put_32 (output_bfd,
4846 sgot->output_section->vma + sgot->output_offset + 4,
4847 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4848 bfd_put_32 (output_bfd,
4849 sgot->output_section->vma + sgot->output_offset + 8,
4850 splt->contents + elf_mn10300_plt0_linker_offset (info));
4853 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4854 really seem like the right value. */
4855 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4859 /* Fill in the first three entries in the global offset table. */
4860 if (sgot->size > 0)
4862 if (sdyn == NULL)
4863 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4864 else
4865 bfd_put_32 (output_bfd,
4866 sdyn->output_section->vma + sdyn->output_offset,
4867 sgot->contents);
4868 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4869 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4872 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4874 return TRUE;
4877 /* Classify relocation types, such that combreloc can sort them
4878 properly. */
4880 static enum elf_reloc_type_class
4881 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4883 switch ((int) ELF32_R_TYPE (rela->r_info))
4885 case R_MN10300_RELATIVE: return reloc_class_relative;
4886 case R_MN10300_JMP_SLOT: return reloc_class_plt;
4887 case R_MN10300_COPY: return reloc_class_copy;
4888 default: return reloc_class_normal;
4892 #ifndef ELF_ARCH
4893 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4894 #define TARGET_LITTLE_NAME "elf32-mn10300"
4895 #define ELF_ARCH bfd_arch_mn10300
4896 #define ELF_MACHINE_CODE EM_MN10300
4897 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4898 #define ELF_MAXPAGESIZE 0x1000
4899 #endif
4901 #define elf_info_to_howto mn10300_info_to_howto
4902 #define elf_info_to_howto_rel 0
4903 #define elf_backend_can_gc_sections 1
4904 #define elf_backend_rela_normal 1
4905 #define elf_backend_check_relocs mn10300_elf_check_relocs
4906 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4907 #define elf_backend_relocate_section mn10300_elf_relocate_section
4908 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4909 #define bfd_elf32_bfd_get_relocated_section_contents \
4910 mn10300_elf_get_relocated_section_contents
4911 #define bfd_elf32_bfd_link_hash_table_create \
4912 elf32_mn10300_link_hash_table_create
4913 #define bfd_elf32_bfd_link_hash_table_free \
4914 elf32_mn10300_link_hash_table_free
4916 #ifndef elf_symbol_leading_char
4917 #define elf_symbol_leading_char '_'
4918 #endif
4920 /* So we can set bits in e_flags. */
4921 #define elf_backend_final_write_processing \
4922 _bfd_mn10300_elf_final_write_processing
4923 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4925 #define bfd_elf32_bfd_merge_private_bfd_data \
4926 _bfd_mn10300_elf_merge_private_bfd_data
4928 #define elf_backend_can_gc_sections 1
4929 #define elf_backend_create_dynamic_sections \
4930 _bfd_mn10300_elf_create_dynamic_sections
4931 #define elf_backend_adjust_dynamic_symbol \
4932 _bfd_mn10300_elf_adjust_dynamic_symbol
4933 #define elf_backend_size_dynamic_sections \
4934 _bfd_mn10300_elf_size_dynamic_sections
4935 #define elf_backend_omit_section_dynsym \
4936 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4937 #define elf_backend_finish_dynamic_symbol \
4938 _bfd_mn10300_elf_finish_dynamic_symbol
4939 #define elf_backend_finish_dynamic_sections \
4940 _bfd_mn10300_elf_finish_dynamic_sections
4942 #define elf_backend_reloc_type_class \
4943 _bfd_mn10300_elf_reloc_type_class
4945 #define elf_backend_want_got_plt 1
4946 #define elf_backend_plt_readonly 1
4947 #define elf_backend_want_plt_sym 0
4948 #define elf_backend_got_header_size 12
4950 #include "elf32-target.h"